Skip to content
Draft
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f5b8489
usbd: Add USB device drivers implemented in Python.
projectgus Oct 26, 2022
e2a3e45
usbd: Add midi interface definition from @paulhamsh.
projectgus Feb 9, 2023
c8ad6ca
usbd: Major cleanup, refactor.
projectgus Feb 14, 2023
65762f6
Add basic keypad support
turmoni Jun 3, 2023
944e107
Fix report count, remove irrelevant comments
turmoni Jun 3, 2023
5b5871c
Add basic, read-only MSC support, and add LED status to keypad.
turmoni Jun 28, 2023
e8bd164
Actually add the changes methoned in the previous commit message, and…
turmoni Jun 28, 2023
eb47fa0
usbd: Bugfixes around data transfer, support using an AbstractBlockDe…
turmoni Jun 30, 2023
24f7422
usbd: Add USB device drivers implemented in Python.
projectgus Oct 26, 2022
581a662
usbd: Add midi interface definition from @paulhamsh.
projectgus Feb 9, 2023
7472ef5
Merge remote-tracking branch 'upstream/feature/usbd_python' into feat…
turmoni Jul 10, 2023
e24951a
usbd: Add copyright notices (+delete file that has gone from upstream…
turmoni Jul 10, 2023
e85b368
usbd: Run "black" with the right options for the style checker to be …
turmoni Jul 10, 2023
82f1e47
usbd: Use EP_IN_FLAG from utils for mass storage
turmoni Jul 10, 2023
5c51a9e
usbd: Re-run black to fix the missing comma
turmoni Jul 10, 2023
9d4d843
usbd: Add support for configuration open and reset callbacks.
projectgus Jul 25, 2023
29e9185
usbd: Add USB interface functions for endpoint STALL support.
projectgus Jul 25, 2023
9d7ce9f
usbd: Implement SET_REPORT support for OUT direction HID data.
projectgus Jul 26, 2023
92711ea
usbd: Rename ustruct->struct.
projectgus Jul 26, 2023
3765d04
usbd: Add hid keypad example from @turmoni .
projectgus Jul 26, 2023
756d761
usbd: Update hid_keypad example module.
projectgus Jul 26, 2023
bb389e3
usbd: Implement ruff, black linter & formatting fixes.
projectgus Jul 26, 2023
2baaf58
usbd: Add missing manifest file.
projectgus Jul 26, 2023
83364c0
Merge remote-tracking branch 'upstream/feature/usbd_python' into feat…
turmoni Aug 3, 2023
cd4f51c
usbd: Theoretically handle resets and bad CBWs better in msc
turmoni Sep 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
usbd: Theoretically handle resets and bad CBWs better in msc
  • Loading branch information
turmoni committed Sep 2, 2023
commit cd4f51ca11a18134bb764fe478a477391a278a6e
35 changes: 21 additions & 14 deletions micropython/usbd/msc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def from_binary(self, binary):
) = ustruct.unpack("<LLLBBB16s", binary)


class BadCbw(RuntimeError):
pass


class CSW:
"""Command Status Wrapper - handles status messages from the device to the host"""

Expand Down Expand Up @@ -194,7 +198,8 @@ def try_to_prepare_cbw(self, args=None):
"""
try:
self.prepare_cbw()
except KeyError:
except (KeyError, RuntimeError):
# RuntimeError is raised when the device isn't open yet, so let's just retry
self.timer.init(mode=Timer.ONE_SHOT, period=2000, callback=self.try_to_prepare_cbw)

def handle_interface_control_xfer(self, stage, request):
Expand All @@ -221,13 +226,13 @@ def handle_interface_control_xfer(self, stage, request):
return False

def reset(self):
"""Theoretically reset, in reality just break things a bit at the moment"""
"""Perform a Reset Revovery"""
self.log("reset()")
# This doesn't work properly at the moment, needs additional
# functionality in the C side
self.stage = type(self).MSC_STAGE_CMD
self.transferred_length = 0
self.storage_device.reset()
self.set_ep_stall(self.ep_in, False)
self.set_ep_stall(self.ep_out, False)
self.prepare_cbw()
return True

Expand Down Expand Up @@ -296,7 +301,14 @@ def handle_cbw(self):
self.csw.dCSWDataResidue = 0
self.csw.bCSWStatus = CSW.STATUS_PASSED

status = int(self.validate_cbw())
try:
status = int(self.validate_cbw())
except BadCbw as exc:
self.log(str(exc))
self.set_ep_stall(self.ep_in, True)
self.set_ep_stall(self.ep_out, True)
return False

if status != CSW.STATUS_PASSED:
self.log(f"Didn't pass: {status}")
self.prepare_for_csw(status=status)
Expand Down Expand Up @@ -388,22 +400,17 @@ def validate_cbw(self) -> bool:
return CSW.STATUS_PHASE_ERROR

if len(self.rx_data) != 31:
self.log("Wrong length")
return CSW.STATUS_FAILED
raise BadCbw("Invalid: Wrong CBW length")

if self.cbw.dCBWSignature != type(self).CBW_SIGNATURE:
self.log("Wrong sig")
self.log(str(self.cbw.dCBWSignature))
return CSW.STATUS_FAILED
raise BadCbw(f"Invalid: Wrong sig: {str(self.cbw.dCBWSignature)}")

# Meaningful checks (6.2.2)
if self.cbw.bCBWLUN > 15 or not 0 < self.cbw.bCBWCBLength < 17:
self.log("Wrong length")
return CSW.STATUS_FAILED
raise BadCbw("Not meaningful: Wrong length command or invalid LUN")

if self.cbw.bCBWLUN != self.lun:
self.log("Wrong LUN")
return CSW.STATUS_FAILED
raise BadCbw("Not meaningful: Wrong LUN")

# Check if this is a valid SCSI command
try:
Expand Down