From kevin at tyndur.org Tue Apr 7 21:47:19 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 7 Apr 2015 21:47:19 +0200 Subject: [cdi-devel] [PATCH] ahci: Initialisation/Reset improvements Message-ID: <1428436039-18482-1-git-send-email-kevin@tyndur.org> Surprisingly, real hardware works much better when the disk gets power and is spun up. Also disable any automatic transition to power management modes just to be on the safe side. Before resetting a port, the processing of the command list must be stopped. Signed-off-by: Kevin Wolf --- ahci/ahci.h | 10 +++++++++- ahci/main.c | 32 +++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/ahci/ahci.h b/ahci/ahci.h index 24a830b..2515f5a 100644 --- a/ahci/ahci.h +++ b/ahci/ahci.h @@ -94,9 +94,14 @@ enum { enum { PxCMD_ST = (1 << 0), /* Start */ + PxCMD_SUD = (1 << 1), /* Spin-Up Device */ + PxCMD_POD = (1 << 2), /* Power On Device */ PxCMD_FRE = (1 << 4), /* FIS Receive Enable */ PxCMD_FR = (1 << 14), /* FIS Receive Running */ PxCMD_CR = (1 << 15), /* Command List Running */ + + PxCMD_ICC_MASK = (0xf << 28), + PxCMD_ICC_ACTIVE = (0x1 << 28), }; enum { @@ -114,7 +119,10 @@ enum { }; enum { - PxSCTL_DET_MASK = (0xf << 0), + PxSCTL_DET_MASK = (0xf << 0), /* Device Detection Initialization */ + PxSCTL_IPM_MASK = (0xf << 8), /* Interface Power Management + Transitions Allowed */ + PxSCTL_IPM_NONE = (0x3 << 8), }; /* SATA 2.6: "11.3 Software reset protocol" */ diff --git a/ahci/main.c b/ahci/main.c index 1afc03c..714a96f 100644 --- a/ahci/main.c +++ b/ahci/main.c @@ -46,7 +46,7 @@ static int ahci_init_hardware(struct ahci_device* ahci) { int port; bool all_idle; - uint32_t cmd, ssts, sig; + uint32_t cmd, ssts, sig, sctl; int retries; /* HBA reset (see AHCI 1.3, chapter 10.4.3) */ @@ -95,15 +95,35 @@ static int ahci_init_hardware(struct ahci_device* ahci) continue; } - /* If there is a device attached, register it */ - ssts = pxreg_inl(ahci, port, REG_PxSSTS); + /* Power On Device, Spin-Up Device, Link Active */ + cmd = pxreg_inl(ahci, port, REG_PxCMD); + cmd &= ~PxCMD_ICC_MASK; + cmd |= PxCMD_POD | PxCMD_SUD | PxCMD_ICC_ACTIVE; + pxreg_outl(ahci, port, REG_PxCMD, cmd); + + retries = 10; + do { + ssts = pxreg_inl(ahci, port, REG_PxSSTS); + if ((ssts & PxSSTS_DET_MASK) == PxSSTS_DET_PRESENT) { + break; + } else { + cdi_sleep_ms(100); + } + } while (--retries); + /* If there is a device attached, register it */ if ((ssts & PxSSTS_DET_MASK) != PxSSTS_DET_PRESENT || (ssts & PxSSTS_IPM_MASK) != PxSSTS_IPM_ACTIVE) { continue; } + /* Forbid transition to any power mangement states */ + sctl = pxreg_inl(ahci, port, REG_PxSCTL); + sctl &= ~PxSCTL_IPM_MASK; + sctl |= PxSCTL_IPM_NONE; + pxreg_outl(ahci, port, REG_PxSCTL, sctl); + /* Create a device with the right driver */ sig = pxreg_inl(ahci, port, REG_PxSIG); @@ -145,6 +165,10 @@ static int ahci_init_hardware(struct ahci_device* ahci) void ahci_port_comreset(struct ahci_device* ahci, int port) { uint32_t sctl = pxreg_inl(ahci, port, REG_PxSCTL); + uint32_t cmd = pxreg_inl(ahci, port, REG_PxCMD); + + pxreg_outl(ahci, port, REG_PxCMD, cmd & ~PxCMD_ST); + while (pxreg_inl(ahci, port, REG_PxCMD) & PxCMD_CR); sctl &= ~PxSCTL_DET_MASK; sctl |= 0x1; @@ -154,6 +178,8 @@ void ahci_port_comreset(struct ahci_device* ahci, int port) sctl &= ~PxSCTL_DET_MASK; pxreg_outl(ahci, port, REG_PxSCTL, sctl); + + pxreg_outl(ahci, port, REG_PxCMD, cmd); } static void irq_handler(struct cdi_device* dev) -- 2.1.4 From kevin at tyndur.org Tue Apr 7 23:32:03 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 7 Apr 2015 23:32:03 +0200 Subject: [cdi-devel] [PATCH v2] ahci: Initialisation/Reset improvements Message-ID: <1428442323-22448-1-git-send-email-kevin@tyndur.org> Surprisingly, real hardware works much better when the disk gets power and is spun up. Also disable any automatic transition to power management modes just to be on the safe side. Before resetting a port, the processing of the command list must be stopped. Signed-off-by: Kevin Wolf --- v2: - Removed the long delays that v1 had while trying to wait for a device on an empty port ahci/ahci.h | 12 +++++++++++- ahci/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/ahci/ahci.h b/ahci/ahci.h index 24a830b..85d2cd5 100644 --- a/ahci/ahci.h +++ b/ahci/ahci.h @@ -94,9 +94,16 @@ enum { enum { PxCMD_ST = (1 << 0), /* Start */ + PxCMD_SUD = (1 << 1), /* Spin-Up Device */ + PxCMD_POD = (1 << 2), /* Power On Device */ PxCMD_FRE = (1 << 4), /* FIS Receive Enable */ PxCMD_FR = (1 << 14), /* FIS Receive Running */ PxCMD_CR = (1 << 15), /* Command List Running */ + PxCMD_CPS = (1 << 16), /* Cold Presence State */ + PxCMD_CPD = (1 << 20), /* Cold Presence Detection */ + + PxCMD_ICC_MASK = (0xf << 28), + PxCMD_ICC_ACTIVE = (0x1 << 28), }; enum { @@ -114,7 +121,10 @@ enum { }; enum { - PxSCTL_DET_MASK = (0xf << 0), + PxSCTL_DET_MASK = (0xf << 0), /* Device Detection Initialization */ + PxSCTL_IPM_MASK = (0xf << 8), /* Interface Power Management + Transitions Allowed */ + PxSCTL_IPM_NONE = (0x3 << 8), }; /* SATA 2.6: "11.3 Software reset protocol" */ diff --git a/ahci/main.c b/ahci/main.c index 1afc03c..35f5141 100644 --- a/ahci/main.c +++ b/ahci/main.c @@ -46,7 +46,7 @@ static int ahci_init_hardware(struct ahci_device* ahci) { int port; bool all_idle; - uint32_t cmd, ssts, sig; + uint32_t cmd, ssts, sig, sctl; int retries; /* HBA reset (see AHCI 1.3, chapter 10.4.3) */ @@ -89,21 +89,59 @@ static int ahci_init_hardware(struct ahci_device* ahci) /* Determine number of command slots */ ahci->cmd_slots = (reg_inl(ahci, REG_CAP) & CAP_NCS_MASK) >> CAP_NCS_SHIFT; - /* Allocate Command List and FIS for each port */ + /* All ports: Power On Device, Spin-Up Device, Link Active */ for (port = 0; port < MAX_PORTS; port++) { if (!(ahci->ports & BIT(port))) { continue; } + cmd = pxreg_inl(ahci, port, REG_PxCMD); + cmd &= ~PxCMD_ICC_MASK; + cmd |= PxCMD_POD | PxCMD_SUD | PxCMD_ICC_ACTIVE; + pxreg_outl(ahci, port, REG_PxCMD, cmd); + } - /* If there is a device attached, register it */ - ssts = pxreg_inl(ahci, port, REG_PxSSTS); + /* Detect and initialise devices for each port */ + retries = 10; + for (port = 0; port < MAX_PORTS; port++) { + if (!(ahci->ports & BIT(port))) { + continue; + } + + /* If we know there is nothing (Cold Presence Detection supported, but + * no device detected), skip the device. */ + cmd = pxreg_inl(ahci, port, REG_PxCMD); + if ((cmd & (PxCMD_CPD | PxCMD_CPS)) == PxCMD_CPD) { + continue; + } + + /* Check whether there is a device */ + do { + ssts = pxreg_inl(ahci, port, REG_PxSSTS); + if ((ssts & PxSSTS_DET_MASK) == PxSSTS_DET_PRESENT) { + break; + } else if ((ssts & PxSSTS_DET_MASK) == 0 && retries < 10) { + /* At least device presence should be detected after 100ms, + * even if Phy communication isn't established yet. */ + break; + } else if (retries) { + cdi_sleep_ms(100); + retries--; + } + } while (retries); + /* If there is a device attached, register it */ if ((ssts & PxSSTS_DET_MASK) != PxSSTS_DET_PRESENT || (ssts & PxSSTS_IPM_MASK) != PxSSTS_IPM_ACTIVE) { continue; } + /* Forbid transition to any power mangement states */ + sctl = pxreg_inl(ahci, port, REG_PxSCTL); + sctl &= ~PxSCTL_IPM_MASK; + sctl |= PxSCTL_IPM_NONE; + pxreg_outl(ahci, port, REG_PxSCTL, sctl); + /* Create a device with the right driver */ sig = pxreg_inl(ahci, port, REG_PxSIG); @@ -145,6 +183,10 @@ static int ahci_init_hardware(struct ahci_device* ahci) void ahci_port_comreset(struct ahci_device* ahci, int port) { uint32_t sctl = pxreg_inl(ahci, port, REG_PxSCTL); + uint32_t cmd = pxreg_inl(ahci, port, REG_PxCMD); + + pxreg_outl(ahci, port, REG_PxCMD, cmd & ~PxCMD_ST); + while (pxreg_inl(ahci, port, REG_PxCMD) & PxCMD_CR); sctl &= ~PxSCTL_DET_MASK; sctl |= 0x1; @@ -154,6 +196,8 @@ void ahci_port_comreset(struct ahci_device* ahci, int port) sctl &= ~PxSCTL_DET_MASK; pxreg_outl(ahci, port, REG_PxSCTL, sctl); + + pxreg_outl(ahci, port, REG_PxCMD, cmd); } static void irq_handler(struct cdi_device* dev) -- 2.1.4 From max at tyndur.org Thu Apr 16 00:13:50 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:50 +0200 Subject: [cdi-devel] [PATCH v2 1/9] Makefile: Heed Makefile.conf In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-2-git-send-email-max@tyndur.org> + Apparently it is allowed to specify additional CFLAGS (as "CC_FLAGS") in a file named Makefile.conf (formatted as a shell script) in a driver's root directory; or at least ext2 just does this. This patch makes the CDI Makefile take the additional flags from that file into account, if present. Thanks to this, the ext2 driver can be compiled. Signed-off-by: Max Reitz --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5360efb..bc04e58 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,9 @@ run_command = $(if $(V),$(2), at echo $(1);$(2)) all: $(OBJS) %.o: %.c - $(call run_command,"CC $^",$(CC) $(CFLAGS) $(addprefix -I,$(addprefix $(dir $^),. include)) -c -o $@ $^) + $(call run_command,"CC $^",$(CC) $(CFLAGS) \ + $(if $(shell if [ -f $(dir $^)/Makefile.conf ]; then echo .; fi),$(shell cd $(dir $^); echo -e "$$(< Makefile.conf)\necho \$$CC_FLAGS" | sh -s),) \ + $(addprefix -I,$(addprefix $(dir $^),. include)) -c -o $@ $^) clean: -rm $(OBJS) 2> /dev/null -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:49 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:49 +0200 Subject: [cdi-devel] [PATCH v2 0/9] USB support Message-ID: <1429136038-29537-1-git-send-email-max@tyndur.org> This series adds some long-missing USB drivers to the CDI repository (and defines the required interfaces). For now, it only supports control and bulk transfers (which are enough for mass storage devices), no hotplugging, no suspend/resume, neither a lot of other things. So among others, there is no USB keyboard support. Maybe writing the driver and implementing support for it in the USB bus driver would not be so difficult, but I'm really hesitant to define the necessary CDI interface for it (CDI.hid? CDI.keyboard? CDI.serial?). The drivers added by this series are an EHCI driver, a USB 2.0 bus driver (including a hub driver), and a bulk-only mass storage driver (with a SCSI interface; should be the right thing to drive your average thumb drive (and maybe even HDDs), but will not work for e.g. USB floppy drives). I tested these drivers using ?xo?cota and t?ndur in qemu and on a real machine (which only has EHCIs and xHCIs; every EHCI has a high speed hub to communicate with low and full speed devices, so there is no need for companion controllers). v2: - Added patch 1 so that the ext2 driver can be compiled - Patch 2: Added 64 bit endianness conversion functions - Patch 4: Added a LUN addressing field to cdi_scsi_packet (it turns out that the LUN is not part of the SCSI command) - Patch 7: - Added some timeouts - Dropped some redundant macros - Added the 64 bit fields to ehci_qtd - Calculate the alignment for ehci_qtd and ehci_qh during compile time instead of hard-coding it - Call the IRQ handler sometimes manually to ensure the HC is not getting stuck somewhere because the driver didn't acknowledge some IRQ - Patch 8: stdio.h should always be included (due to the usage of fprintf()) - Patch 9: - Removed printf() statements left over from debugging - Read LUN from cdi_scsi_packet instead of the SCSI command - Include stdbool.h git-backport-diff against v1: Key: [----] : patches are identical [####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively 001/9:[down] 'Makefile: Heed Makefile.conf' 002/9:[0020] [FC] 'cdi/misc: Add CDI_UPCAST and endianness functions' 003/9:[----] [--] 'cdi/scsi, cdi.h: Remove t?ndur-specific fields' 004/9:[down] 'cdi/scsi: Add support for multiple LUNs' ^^^^ false negative, actually [0003] [FC] 005/9:[----] [-C] 'cdi.h: Add cdi_handle_bus_device()' 006/9:[----] [--] 'cdi/usb: Add headers' 007/9:[0189] [FC] 'ehci: Add EHCI driver' 008/9:[0002] [FC] 'usb: Add USB 2.0 bus driver' 009/9:[0007] [FC] 'usb-storage: Add USB mass storage driver' Max Reitz (9): Makefile: Heed Makefile.conf cdi/misc: Add CDI_UPCAST and endianness functions cdi/scsi, cdi.h: Remove t?ndur-specific fields cdi/scsi: Add support for multiple LUNs cdi.h: Add cdi_handle_bus_device() cdi/usb: Add headers ehci: Add EHCI driver usb: Add USB 2.0 bus driver usb-storage: Add USB mass storage driver Makefile | 4 +- ehci/ehci.c | 661 +++++++++++++++++++++++++++++++++ ehci/ehci.h | 170 +++++++++ ehci/main.c | 68 ++++ include/cdi.h | 27 +- include/cdi/misc.h | 78 ++++ include/cdi/scsi.h | 11 +- include/cdi/usb-structures.h | 118 ++++++ include/cdi/usb.h | 217 +++++++++++ include/cdi/usb_hcd.h | 159 ++++++++ usb-storage/main.c | 76 ++++ usb-storage/usb-storage.c | 213 +++++++++++ usb-storage/usb-storage.h | 34 ++ usb/main.c | 61 +++ usb/usb-hubs.h | 78 ++++ usb/usb.c | 864 +++++++++++++++++++++++++++++++++++++++++++ usb/usb.h | 86 +++++ 17 files changed, 2915 insertions(+), 10 deletions(-) create mode 100644 ehci/ehci.c create mode 100644 ehci/ehci.h create mode 100644 ehci/main.c create mode 100644 include/cdi/usb-structures.h create mode 100644 include/cdi/usb.h create mode 100644 include/cdi/usb_hcd.h create mode 100644 usb-storage/main.c create mode 100644 usb-storage/usb-storage.c create mode 100644 usb-storage/usb-storage.h create mode 100644 usb/main.c create mode 100644 usb/usb-hubs.h create mode 100644 usb/usb.c create mode 100644 usb/usb.h -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:51 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:51 +0200 Subject: [cdi-devel] [PATCH v2 2/9] cdi/misc: Add CDI_UPCAST and endianness functions In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-3-git-send-email-max@tyndur.org> + Type inheritance is a common pattern seen in CDI. The new CDI_UPCAST() macro makes conversion to superclasses safer and allows for multiple inheritance if the need arises. + Add some endianness conversion functions. Even though support for big endian platforms is nowhere in sight, endianness conversion is still a common operation which should not be implemented in the driver itself but provided by the CDI implementation. Signed-off-by: Max Reitz --- include/cdi/misc.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/include/cdi/misc.h b/include/cdi/misc.h index 07967ec..3193505 100644 --- a/include/cdi/misc.h +++ b/include/cdi/misc.h @@ -85,6 +85,84 @@ int cdi_ioports_free(uint16_t start, uint16_t count); */ void cdi_sleep_ms(uint32_t ms); +/** + * Casts @object (must be a pointer) to a pointer to an object of type @to which + * must be a baseclass of @object's type. @field is the entry in @to which + * contains @object. + * + * Example: + * struct cdi_driver *driver; + * struct cdi_storage_driver *storage_driver = + * CDI_UPCAST(driver, drv, struct cdi_storage_driver); + */ +#define CDI_UPCAST(object, field, to) \ + (to *)((uintptr_t)object - (uintptr_t)&((to *)0)->field) + +/** + * Functions for endianness conversion. Be aware that most if not all CDI + * drivers only support little endian machines as a host. + */ + +static inline uint16_t cdi_be16_to_cpu(uint16_t x) +{ + return (x >> 8) | (x << 8); +} + +static inline uint32_t cdi_be32_to_cpu(uint32_t x) +{ + return ((uint32_t)cdi_be16_to_cpu(x) << 16) | (uint32_t)cdi_be16_to_cpu(x >> 16); +} + +static inline uint64_t cdi_be64_to_cpu(uint64_t x) +{ + return ((uint64_t)cdi_be32_to_cpu(x) << 32) | (uint64_t)cdi_be32_to_cpu(x >> 32); +} + +static inline uint16_t cdi_cpu_to_be16(uint16_t x) +{ + return cdi_be16_to_cpu(x); +} + +static inline uint32_t cdi_cpu_to_be32(uint32_t x) +{ + return cdi_be32_to_cpu(x); +} + +static inline uint64_t cdi_cpu_to_be64(uint64_t x) +{ + return cdi_be64_to_cpu(x); +} + +static inline uint16_t cdi_le16_to_cpu(uint16_t x) +{ + return x; +} + +static inline uint32_t cdi_le32_to_cpu(uint32_t x) +{ + return x; +} + +static inline uint64_t cdi_le64_to_cpu(uint64_t x) +{ + return x; +} + +static inline uint16_t cdi_cpu_to_le16(uint16_t x) +{ + return cdi_le16_to_cpu(x); +} + +static inline uint32_t cdi_cpu_to_le32(uint32_t x) +{ + return cdi_le32_to_cpu(x); +} + +static inline uint64_t cdi_cpu_to_le64(uint64_t x) +{ + return cdi_le64_to_cpu(x); +} + #ifdef __cplusplus }; // extern "C" #endif -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:52 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:52 +0200 Subject: [cdi-devel] =?utf-8?q?=5BPATCH_v2_3/9=5D_cdi/scsi=2C_cdi=2Eh=3A_R?= =?utf-8?q?emove_t=C3=BDndur-specific_fields?= In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-4-git-send-email-max@tyndur.org> - Remove t?ndur-specific fields from non-OS-dependant structures Signed-off-by: Max Reitz --- include/cdi.h | 3 --- include/cdi/scsi.h | 7 ------- 2 files changed, 10 deletions(-) diff --git a/include/cdi.h b/include/cdi.h index a9207db..998e080 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -178,9 +178,6 @@ struct cdi_device { * \endenglish */ struct cdi_bus_data* bus_data; - - // tyndur-spezifisch - void* backdev; }; /** diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h index 8edccbf..cef96ed 100644 --- a/include/cdi/scsi.h +++ b/include/cdi/scsi.h @@ -51,13 +51,6 @@ struct cdi_scsi_device { /// Geraetetyp, der ueber SCSI angesteuert wird cdi_device_type_t type; - - /** - * tyndur-spezifisch: Frontend-Geraet (z.B. CDI_STORAGE-Geraet, das ueber - * SCSI angesteuert wird) - */ - void* frontdev; - }; /// SCSI-Treiber -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:53 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:53 +0200 Subject: [cdi-devel] [PATCH v2 4/9] cdi/scsi: Add support for multiple LUNs In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-5-git-send-email-max@tyndur.org> + One SCSI device may provide multiple LUNs. Add a field for the LUN count to cdi_scsi_device. + Furthermore, add a field to select the LUN to cdi_scsi_packet. Signed-off-by: Max Reitz --- include/cdi/scsi.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h index cef96ed..f36f9aa 100644 --- a/include/cdi/scsi.h +++ b/include/cdi/scsi.h @@ -24,6 +24,9 @@ /// SCSI-Paket struct cdi_scsi_packet { + /// LUN to address + int lun; + /// Buffer zum Senden oder Empfangen von Daten void *buffer; @@ -51,6 +54,9 @@ struct cdi_scsi_device { /// Geraetetyp, der ueber SCSI angesteuert wird cdi_device_type_t type; + + /// Number of LUNs (if 0, there is one) + int lun_count; }; /// SCSI-Treiber -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:54 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:54 +0200 Subject: [cdi-devel] [PATCH v2 5/9] cdi.h: Add cdi_handle_bus_device() In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-6-git-send-email-max@tyndur.org> + Currently, all devices provided on a bus have to be passed to the init_device() function of all drivers handling any device type on that bus. cdi_handle_bus_device() can be used by the driver to specify a pattern (struct cdi_bus_device_pattern) which allows the CDI implementation to speed up the selection of the correct driver for a new device. Signed-off-by: Max Reitz --- include/cdi.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/cdi.h b/include/cdi.h index 998e080..9c1c1b1 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -141,6 +141,17 @@ struct cdi_bus_data { }; /** + * This structure, which is extended by any bus type supporting it, contains + * information about which devices can be handled by a certain driver. + * Typically, it will contain some of the fields provided by the appropriate + * cdi_bus_data structure (the ones which are used to identify a device like + * device/vendor ID) where negative values signify a "don't care". + */ +struct cdi_bus_device_pattern { + cdi_device_type_t bus_type; +}; + +/** * \german * Beschreibt ein Ger?t * \endgerman @@ -406,6 +417,19 @@ int cdi_provide_device(struct cdi_bus_data* device); int cdi_provide_device_internal_drv(struct cdi_bus_data* device, struct cdi_driver* driver); +/** + * Tells the CDI implementation that the driver @drv is able to handle devices + * with the pattern @pattern (appearing on the bus @pattern->bus_type). This + * function can be called multiple times. Any device appearing on the bus not + * fitting the pattern will not be passed to the @drv->init_device() function. + * That function may however return NULL even after the driver has used this + * function, that is, it is valid to call this function with a @pattern that + * does not only fit devices handled by @drv but others as well (which can then + * be rejected by letting @drv->init_device() return NULL). + */ +void cdi_handle_bus_device(struct cdi_driver* drv, + struct cdi_bus_device_pattern* pattern); + #endif /*\@}*/ -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:55 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:55 +0200 Subject: [cdi-devel] [PATCH v2 6/9] cdi/usb: Add headers In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-7-git-send-email-max@tyndur.org> + Add headers to be used by USB host controller drivers, the USB bus driver, and USB device drivers. These headers are still lacking functions for periodic transactions (interrupt/isochronous), hotplugging, suspend/resume, etc. pp.. Signed-off-by: Max Reitz --- include/cdi/usb-structures.h | 118 +++++++++++++++++++++++ include/cdi/usb.h | 217 +++++++++++++++++++++++++++++++++++++++++++ include/cdi/usb_hcd.h | 159 +++++++++++++++++++++++++++++++ 3 files changed, 494 insertions(+) create mode 100644 include/cdi/usb-structures.h create mode 100644 include/cdi/usb.h create mode 100644 include/cdi/usb_hcd.h diff --git a/include/cdi/usb-structures.h b/include/cdi/usb-structures.h new file mode 100644 index 0000000..70a74ce --- /dev/null +++ b/include/cdi/usb-structures.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2015 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +/** + * This file contains some standard USB structures such as descriptors. + */ + +#ifndef _CDI_USB_STRUCTURES_H_ +#define _CDI_USB_STRUCTURES_H_ + +#include + + +struct cdi_usb_device_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t bcd_usb; + uint8_t b_device_class; + uint8_t b_device_sub_class; + uint8_t b_device_protocol; + uint8_t b_max_packet_size_0; + uint16_t id_vendor; + uint16_t id_product; + uint16_t bcd_device; + uint8_t i_manufacturer; + uint8_t i_product; + uint8_t i_serial_number; + uint8_t b_num_configurations; +} __attribute__((packed)); + +struct cdi_usb_configuration_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t w_total_length; + uint8_t b_num_interfaces; + uint8_t b_configuration_value; + uint8_t i_configuration; + uint8_t bm_attributes; + uint8_t b_max_power; +} __attribute__((packed)); + +struct cdi_usb_string_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t b_string[127]; +} __attribute__((packed)); + +struct cdi_usb_interface_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_interface_number; + uint8_t b_alternate_setting; + uint8_t b_num_endpoints; + uint8_t b_interface_class; + uint8_t b_interface_sub_class; + uint8_t b_interface_protocol; + uint8_t i_interface; +} __attribute__((packed)); + +struct cdi_usb_endpoint_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_endpoint_address; + uint8_t bm_attributes; + uint16_t w_max_packet_size; + uint8_t b_interval; +} __attribute__((packed)); + +struct cdi_usb_setup_packet { + uint8_t bm_request_type; + uint8_t b_request; + uint16_t w_value; + uint16_t w_index; + uint16_t w_length; +} __attribute__((packed)); + + +/// Control request numbers +enum { + CDI_USB_CREQ_GET_STATUS = 0, + CDI_USB_CREQ_CLEAR_FEATURE = 1, + CDI_USB_CREQ_SET_FEATURE = 3, + CDI_USB_CREQ_SET_ADDRESS = 5, + CDI_USB_CREQ_GET_DESCRIPTOR = 6, + CDI_USB_CREQ_SET_DESCRIPTOR = 7, + CDI_USB_CREQ_GET_CONFIGURATION = 8, + CDI_USB_CREQ_SET_CONFIGURATION = 9, + CDI_USB_CREQ_GET_INTERFACE = 10, + CDI_USB_CREQ_SET_INTERFACE = 11, + CDI_USB_CREQ_SYNCH_FRAME = 12, +}; + +/// Descriptor IDs +enum { + CDI_USB_DESC_DEVICE = 1, + CDI_USB_DESC_CONFIGURATION = 2, + CDI_USB_DESC_STRING = 3, + CDI_USB_DESC_INTERFACE = 4, + CDI_USB_DESC_ENDPOINT = 5, +}; + +/// Values constituting cdi_usb_setup_packet.bm_request_type +#define CDI_USB_CREQ_DEVICE 0 +#define CDI_USB_CREQ_INTERFACE 1 +#define CDI_USB_CREQ_ENDPOINT 2 +#define CDI_USB_CREQ_OTHER 3 +#define CDI_USB_CREQ_CLASS (1 << 5) +#define CDI_USB_CREQ_OUT (0 << 7) +#define CDI_USB_CREQ_IN (1 << 7) + +#endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h new file mode 100644 index 0000000..111e723 --- /dev/null +++ b/include/cdi/usb.h @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2015 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include + +#include +#include + + +struct cdi_usb_hc; + +typedef enum { + CDI_USB_CONTROL, + CDI_USB_BULK, + CDI_USB_INTERRUPT, + CDI_USB_ISOCHRONOUS, +} cdi_usb_endpoint_type_t; + +typedef enum { + CDI_USB_LOW_SPEED, + CDI_USB_FULL_SPEED, + CDI_USB_HIGH_SPEED, + CDI_USB_SUPERSPEED, +} cdi_usb_speed_t; + +typedef enum { + CDI_USB_OK = 0, + + CDI_USB_DRIVER_ERROR, + CDI_USB_TIMEOUT, + CDI_USB_BABBLE, + CDI_USB_STALL, + CDI_USB_HC_ERROR, + CDI_USB_BAD_RESPONSE, +} cdi_usb_transmission_result_t; + +/* Bitmask of CDI_USB_PORT_* and CDI_USB_*_SPEED values */ +typedef uint32_t cdi_usb_port_status_t; + +#define CDI_USB_PORT_SPEED_MASK (0x7) +#define CDI_USB_PORT_CONNECTED (1 << 3) + +struct cdi_usb_hub; + +/** + * Describes a hub driver. This is not a CDI driver in the common sense. When + * used for a root hub, it is part of the host controller driver; when used for + * a device hub, it is only used internally by the USB driver. + */ +struct cdi_usb_hub_driver { + /** + * Makes a port unreachable by traffic which reaches the hub. @index is a + * 0-based index. + */ + void (*port_disable)(struct cdi_usb_hub* hub, int index); + + /** + * Makes a port reachable by traffic which reaches the hub and drives the + * reset signal for at least 50 ms on the port. @index is a 0-based index. + */ + void (*port_reset_enable)(struct cdi_usb_hub* hub, int index); + + /** + * Retrieves information about the given port. This should only be called + * after the port has been reset using port_reset_enable(). @index is a + * 0-based index. + */ + cdi_usb_port_status_t (*port_status)(struct cdi_usb_hub* hub, int index); +}; + +/** + * Describes a USB hub. This is not a CDI device in the common sense. When used + * for a root hub, it is part of the host controller object; when used for a + * device hub, it is only used internally by the USB driver. + * + * For this reason, there is no struct cdi_usb_hub_driver object reachable from + * this object. If it is a root hub, the hub driver will be part of the host + * controller driver. Otherwise (if it is a device hub), the hub driver will be + * internal to the USB driver. + */ +struct cdi_usb_hub { + /** + * Number of ports provided by this hub. + */ + int ports; +}; + +/** + * Describes a USB device as given to cdi_provide_device() and the init_device() + * function provided by USB device drivers. + */ +struct cdi_usb_device { + struct cdi_bus_data bus_data; + + /** + * Various information which can be used for identifying a USB device. + */ + uint16_t vendor_id, product_id; + uint8_t class_id, subclass_id, protocol_id; + + /** + * If negative, this logical device is defined on the USB device level; + * otherwise, this is the index of the interface which this logical device + * represents. + */ + int interface; + + /** + * Number of endpoints available for this device; this does include the + * default pipe (endpoint 0) which can be used both on the physical device + * level and from any interface. + */ + int endpoint_count; +}; + +/** + * Describes the USB bus driver. + */ +struct cdi_usb_driver { + struct cdi_driver drv; + + /** + * Returns the endpoint descriptor for endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev in @desc. + */ + void (*get_endpoint_descriptor)(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc); + + /** + * Executes a control transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, using @setup for the setup stage + * and @data for the data stage. The length and direction of transfer is + * deduced from @setup. + */ + cdi_usb_transmission_result_t + (*control_transfer)(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data); + + /** + * Executes a bulk transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, transferring @size bytes from/to + * @data. The direction of transfer is deduced from the endpoint given. + */ + cdi_usb_transmission_result_t (*bulk_transfer)(struct cdi_usb_device* dev, + int ep, void* data, + size_t size); +}; + +/** + * Describes patterns for USB devices a device driver can use to specify which + * devices it can handle. + */ +struct cdi_usb_bus_device_pattern { + struct cdi_bus_device_pattern pattern; + + /** + * Any field may be negative to signify "don't care". + */ + int vendor_id, product_id; + int class_id, subclass_id, protocol_id; +}; + + +/** + * This function is provided by the reference USB bus driver, it must only be + * called by the CDI library. + * + * It is called once a host controller driver provides a host controller device + * (returned by its init_device() function) which is passed to this function. + */ +void cdi_usb_provide_hc(struct cdi_device* dev); + +/** + * Registers a USB bus driver. Should be called by the CDI library, ideally, + * after the USB bus driver's init() function has run. + */ +void cdi_usb_driver_register(struct cdi_usb_driver* drv); + +/** + * Returns the endpoint descriptor for endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev in @desc. + */ +void cdi_usb_get_endpoint_descriptor(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc); + +/** + * Executes a control transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, using @setup for the setup stage and + * @data for the data stage. The length and direction of transfer is deduced + * from @setup. + */ +cdi_usb_transmission_result_t + cdi_usb_control_transfer(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data); + +/** + * Executes a bulk transfer to endpoint @ep (0 up until dev->endpoint_count - 1) + * of device @dev, transferring @size bytes from/to @data. The direction of + * transfer is deduced from the endpoint given. + */ +cdi_usb_transmission_result_t cdi_usb_bulk_transfer(struct cdi_usb_device* dev, + int ep, void* data, + size_t size); + +#endif diff --git a/include/cdi/usb_hcd.h b/include/cdi/usb_hcd.h new file mode 100644 index 0000000..774c0a9 --- /dev/null +++ b/include/cdi/usb_hcd.h @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2015 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include +#include + + +/** + * Opaque value identifying a transaction for the host controller. + */ +typedef void* cdi_usb_hc_transaction_t; + +typedef enum { + CDI_USB_IN, + CDI_USB_OUT, + CDI_USB_SETUP, +} cdi_usb_transfer_token_t; + + +/** + * Describes a USB host controller. + */ +struct cdi_usb_hc { + struct cdi_device dev; + + /** + * Root hub provided by the HC. The hub driver responsible for this root hub + * is part of the cdi_usb_hcd structure. + */ + struct cdi_usb_hub rh; +}; + +/** + * Describes the USB bus provided by a USB host controller and its root hub. All + * buses are handled by the USB bus driver, therefore there are no "identifying" + * fields in this structure. + */ +struct cdi_usb_hc_bus { + struct cdi_bus_data bus_data; + struct cdi_usb_hc* hc; +}; + +/** + * Contains information about a communication pipe to a USB device endpoint used + * for USB transactions. + */ +struct cdi_usb_hc_ep_info { + /// Device ID + int dev; + + /// Endpoint ID + int ep; + + /// Endpoint type + cdi_usb_endpoint_type_t ep_type; + + /// Transfer speed + cdi_usb_speed_t speed; + + /// Maximum packet size for this endpoint + int mps; + + /** + * For low and full speed devices operated behind a high speed hub: @tt_addr + * is the USB device address of the translating USB hub (the last high speed + * hub), @tt_port is the port (0-based index) of that hub where the first + * non-high-speed device of this device's tree is plugged into. + */ + int tt_addr, tt_port; +}; + +/** + * Describes a single USB transmission. + */ +struct cdi_usb_hc_transmission { + /// Transaction this transmission is part of + cdi_usb_hc_transaction_t ta; + + /// Type of transfer to be performed + cdi_usb_transfer_token_t token; + + /// Buffer to be used (may be NULL if @size is 0) + void* buffer; + + /// Number of bytes to be transferred (may be 0, and may exceed the MPS) + size_t size; + + /** + * Toggle bit to be used for the first packet issued in this transmission. + * If @size exceeds the MPS and thus multiple packets are transmitted, the + * toggle bit will be toggled automatically. + */ + int toggle; + + /** + * Result of this transmission. Only valid after wait_transaction() has + * returned. + */ + cdi_usb_transmission_result_t* result; +}; + +/** + * Describes a USB host controller driver. + */ +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * USB hub driver which is responsible for all root hubs provided by this + * HCD. + */ + struct cdi_usb_hub_driver rh_drv; + + /** + * Creates a USB transaction. A transaction consists of one or more + * transmissions to a single endpoint. + */ + cdi_usb_hc_transaction_t + (*create_transaction)(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_ep_info* target); + + /** + * Enters a transmission into a transaction. The transmission will be + * executed asynchronously. + */ + void (*enqueue)(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_transmission* trans); + + /** + * Wait until all transmissions enqueued in the given transaction have been + * executed. The variables pointed to by the @result field of the + * transmissions enqueued will only be valid when the first call to this + * function after the enqueue() calls returns. + */ + void (*wait_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); + + /** + * Destroys a transaction. Transmissions which have not been waited on may + * or may not have been executed by the time this function returns. The + * values of the variables pointed to by the @result field of unwaited-for + * transmissions are not valid, neither are the buffer contents for + * receiving transmissions. + */ + void (*destroy_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); +}; + +#endif -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:56 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:56 +0200 Subject: [cdi-devel] [PATCH v2 7/9] ehci: Add EHCI driver In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-8-git-send-email-max@tyndur.org> + This adds an EHCI USB host controller driver. It is missing support for periodic schedule (interrupt and isochronous transfers), hotplugging, suspend/resume, etc. pp.. Signed-off-by: Max Reitz --- ehci/ehci.c | 661 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ehci/ehci.h | 170 ++++++++++++++++ ehci/main.c | 68 +++++++ 3 files changed, 899 insertions(+) create mode 100644 ehci/ehci.c create mode 100644 ehci/ehci.h create mode 100644 ehci/main.c diff --git a/ehci/ehci.c b/ehci/ehci.c new file mode 100644 index 0000000..6da316e --- /dev/null +++ b/ehci/ehci.c @@ -0,0 +1,661 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ehci.h" + + +#define EHCI_TIMEOUT 1000 // ms + + +static bool bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc); +static void irq_handler(struct cdi_device *dev); + + +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data) +{ + struct cdi_pci_device *pci_dev = (struct cdi_pci_device *)bus_data; + static int dev_counter = 0; + + if (pci_dev->class_id != 0x0c || + pci_dev->subclass_id != 0x03 || + pci_dev->interface_id != 0x20) + { + return NULL; + } + + struct ehci *hc = calloc(1, sizeof(*hc)); + + hc->hc.dev.name = malloc(8); + sprintf((char *)hc->hc.dev.name, "ehci%i", dev_counter++); + + hc->retired_qhs = cdi_list_create(); + + // Enable bus mastering + cdi_pci_config_writew(pci_dev, 4, cdi_pci_config_readw(pci_dev, 4) | 0x6); + + cdi_pci_alloc_memory(pci_dev); + + struct cdi_pci_resource *res; + for (int i = 0; (res = cdi_list_get(pci_dev->resources, i)); i++) { + if (res->type == CDI_PCI_MEMORY && !res->index) { + hc->caps = res->address; + hc->regs = (struct ehci_registers *)((uintptr_t)res->address + + hc->caps->caplength); + } + } + + if (!bios_handoff(pci_dev, hc)) { + goto fail; + } + + // Stop operation + hc->regs->usbcmd = (hc->regs->usbcmd & 0xff00ff00u) | 0x00080010u; + + // Wait until HCHALTED becomes true + int timeout_counter = 0; + while (!(hc->regs->usbsts & (1 << 12)) && ++timeout_counter < 50) { + cdi_sleep_ms(1); + } + if (timeout_counter >= 50) { + goto fail; + } + + hc->regs->usbcmd |= (1 << 1); // Reset + timeout_counter = 0; + while ((hc->regs->usbcmd & (1 << 1)) && ++timeout_counter < 50) { + cdi_sleep_ms(1); + } + if (timeout_counter >= 50) { + goto fail; + } + + if (hc->caps->hccparams & 1) { + // 64-bit addressing capability + hc->regs->ctrldssegment = 0; + mem_barrier(); + } + + // async advance doorbell + hc->regs->usbintr = (1 << 5); + hc->regs->usbsts = hc->regs->usbsts; + cdi_register_irq(pci_dev->irq, &irq_handler, &hc->hc.dev); + + hc->periodic_list_mem = cdi_mem_alloc(4096, 12 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + hc->periodic_list = hc->periodic_list_mem->vaddr; + for (int i = 0; i < 1024; i++) { + hc->periodic_list[i] = EHCI_T; + } + mem_barrier(); + hc->regs->periodiclistbase = hc->periodic_list_mem->paddr.items[0].start; + + // Create anchor + struct cdi_mem_area *anchor_qh; + anchor_qh = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + + hc->async_start = anchor_qh->vaddr; + memset(hc->async_start, 0, sizeof(*hc->async_start)); + + hc->async_start->cdi_mem_area = anchor_qh; + hc->async_start->paddr = anchor_qh->paddr.items[0].start + + offsetof(ehci_fat_qh_t, qh); + + hc->async_start->next = hc->async_start; + hc->async_start->previous = hc->async_start; + + hc->async_start->qh.next_qh = hc->async_start->paddr | EHCI_QH; + hc->async_start->qh.ep_state[0] = 0x0008e000u; + hc->async_start->qh.ep_state[1] = 0x4000ff00u; + + hc->async_start->qh.overlay.next_qtd = EHCI_T; + hc->async_start->qh.overlay.alt_next_qtd = EHCI_T; + + hc->regs->asynclistaddr = hc->async_start->paddr; + + mem_barrier(); + + // Start operation + hc->regs->usbcmd = (hc->regs->usbcmd & 0xff00ff00u) | 0x00080031u; + + // Wait until HCHalted becomes false and the schedules are running + timeout_counter = 0; + while ((hc->regs->usbsts & 0xd000) != 0xc000 && ++timeout_counter < 50) { + cdi_sleep_ms(1); + } + if (timeout_counter >= 50) { + goto fail; + } + + // Route everything to this HC + hc->regs->configflag |= 1; + + mem_barrier(); + cdi_sleep_ms(5); + + hc->hc.rh.ports = hc->caps->hcsparams & 0xf; + + // Power up all ports + for (int i = 0; i < hc->hc.rh.ports; i++) { + hc->regs->portsc[i] |= EHCI_PSC_PP; + } + + mem_barrier(); + + return &hc->hc.dev; + +fail: + if (hc->async_start) { + cdi_mem_free(hc->async_start->cdi_mem_area); + } + if (hc->periodic_list_mem) { + cdi_mem_free(hc->periodic_list_mem); + } + free((void *)hc->hc.dev.name); + free(hc); + cdi_pci_free_memory(pci_dev); + + return NULL; +} + + +static bool bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc) +{ + int eecp = (hc->caps->hccparams >> 8) & 0xff; + + // Iterate through the list of capabilities + while (eecp >= 0x40) { + uint32_t cap = cdi_pci_config_readl(pci_dev, eecp); + + // Look for USBLEGSUP + if ((cap & 0xff) == 0x01) { + // Set OS semaphore + cdi_pci_config_writeb(pci_dev, eecp + 3, 1); + + // Wait for BIOS semaphore to get unset + int timeout_counter = 0; + while ((cdi_pci_config_readb(pci_dev, eecp + 2) & 1) && + ++timeout_counter < 1000) + { + cdi_sleep_ms(1); + } + if (timeout_counter >= 1000) { + return false; + } + } + + eecp = (cap >> 8) & 0xff; + } + + return true; +} + + +static void irq_handler(struct cdi_device *dev) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(dev, dev, struct cdi_usb_hc); + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + + uint32_t usbsts = hc->regs->usbsts; + mem_barrier(); + hc->regs->usbsts = usbsts; + + // async advance doorbell + if (!(usbsts & (1 << 5))) { + return; + } + + ehci_fat_qh_t *rqh; + while ((rqh = cdi_list_pop(hc->retired_qhs))) { + cdi_mem_free(rqh->cdi_mem_area); + } +} + + +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + + hc->regs->portsc[index] &= ~EHCI_PSC_PED; + + mem_barrier(); +} + + +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + uint32_t portsc = hc->regs->portsc[index]; + + if ((portsc & EHCI_PSC_LS_MASK) == EHCI_PSC_LS_K) { + // Low-speed device, release ownership + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; + mem_barrier(); + return; + } + + // Assert reset signal + hc->regs->portsc[index] = ((portsc & ~(EHCI_PSC_PO | EHCI_PSC_SUS | + EHCI_PSC_FPR | EHCI_PSC_PED)) + | EHCI_PSC_RES); + mem_barrier(); + cdi_sleep_ms(50); + + // De-assert reset signal + int timeout_counter = 0; + hc->regs->portsc[index] = portsc & ~EHCI_PSC_RES; + do { + portsc = hc->regs->portsc[index]; + cdi_sleep_ms(1); + } while ((portsc & EHCI_PSC_RES) && ++timeout_counter < 50); + + if (portsc & EHCI_PSC_RES) { + // Disable port, route on to companion controller + // (maybe it can do at least something with it) + hc->regs->portsc[index] = (portsc & ~EHCI_PSC_PED) | EHCI_PSC_PO; + } else if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == EHCI_PSC_CCS) { + // Device connected but port disabled: + // Full-speed device, release ownership + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; + } + + mem_barrier(); +} + + +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + uint32_t portsc = hc->regs->portsc[index]; + cdi_usb_port_status_t status = 0; + + if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == + (EHCI_PSC_PED | EHCI_PSC_CCS)) + { + // Device connected and port enabled + status |= CDI_USB_PORT_CONNECTED | CDI_USB_HIGH_SPEED; + } + + return status; +} + + +static void enter_async_qh(struct ehci *hc, ehci_fat_qh_t *qh) +{ + // Enter the new QH as last + + qh->next = hc->async_start; + qh->previous = hc->async_start->previous; + + qh->previous->next = qh; + hc->async_start->previous = qh; + + qh->qh.next_qh = hc->async_start->paddr | EHCI_QH; + mem_barrier(); + + qh->previous->qh.next_qh = qh->paddr | EHCI_QH; + mem_barrier(); +} + + +static void retire_qh(struct ehci *hc, ehci_fat_qh_t *qh) +{ + qh->previous->qh.next_qh = qh->qh.next_qh; + mem_barrier(); + + qh->previous->next = qh->next; + qh->next->previous = qh->previous; + + cdi_list_push(hc->retired_qhs, qh); + // Request async advance doorbell + hc->regs->usbcmd |= (1 << 6); +} + + +static void retire_qtd(ehci_fat_qtd_t *qtd) +{ + ehci_fat_qh_t *qh = qtd->qh; + + if (qtd->qtd.status & 0x7c) { + if (qtd->qtd.status & (1 << 4)) { + *qtd->result = CDI_USB_BABBLE; + } else if (qtd->qtd.status & (1 << 3)) { + *qtd->result = CDI_USB_BAD_RESPONSE; + } else if (qtd->qtd.status & ((1 << 5) | (1 << 2))) { + *qtd->result = CDI_USB_HC_ERROR; + } else if (qtd->qtd.status & (1 << 6)) { + *qtd->result = CDI_USB_STALL; + } else { + *qtd->result = CDI_USB_HC_ERROR; + } + } + + if (qtd->write_back) { + size_t rem = qtd->write_back_size; + for (int i = 0; i < 5 && rem; i++) { + size_t sz = rem > 4096 ? 4096 : rem; + memcpy((void *)((uintptr_t)qtd->write_back + i * 4096), + qtd->buffers[i]->vaddr, sz); + rem -= sz; + } + assert(!rem); + } + + qh->first_qtd = qtd->next; + if (qh->last_qtd == qtd) { + qh->last_qtd = NULL; + } + + for (int i = 0; i < 5; i++) { + if (qtd->buffers[i]) { + cdi_mem_free(qtd->buffers[i]); + } + } + + cdi_mem_free(qtd->cdi_mem_area); +} + + +static void enter_qtd(ehci_fat_qh_t *qh, ehci_fat_qtd_t *qtd) +{ + bool first; + + qtd->qh = qh; + qtd->qtd.next_qtd = EHCI_T; + qtd->qtd.alt_next_qtd = EHCI_T; + mem_barrier(); + + while (qh->first_qtd) { + /* Break if an active qTD is encountered */ + if (qh->first_qtd->qtd.status & (1 << 7)) { + break; + } + + retire_qtd(qh->first_qtd); + } + + first = !qh->first_qtd; + + // Note: Race condition (a qTD might retire here and we are thus appending + // the one to be queued to an inactive qTD); it's fine as long as we are + // constantly rescanning the queue (here and in ehci_wait_transaction()) + + if (first) { + qh->first_qtd = qtd; + } else { + qh->last_qtd->next = qtd; + } + qh->last_qtd = qtd; + + if (!first) { + qh->last_qtd->qtd.next_qtd = qtd->paddr; + mem_barrier(); + } + + // If the QH is not active, it's done with its queue of qTDs and we should + // continue from this qTD + if (!(qh->qh.overlay.status & (1 << 7))) { + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; + mem_barrier(); + } +} + + +cdi_usb_hc_transaction_t ehci_create_transaction(struct cdi_usb_hc *cdi_hc, + const struct cdi_usb_hc_ep_info *target) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + struct cdi_mem_area *qh_mem; + + qh_mem = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + ehci_fat_qh_t *qh = qh_mem->vaddr; + + memset(qh, 0, sizeof(*qh)); + + int non_high_speed_control = target->speed != CDI_USB_HIGH_SPEED + && target->ep_type == CDI_USB_CONTROL; + + qh->qh.ep_state[0] = target->dev | (target->ep << 8) | (1 << 14) + | (target->mps << 16) | (non_high_speed_control << 27); + switch (target->speed) { + case CDI_USB_LOW_SPEED: qh->qh.ep_state[0] |= (1 << 12); break; + case CDI_USB_FULL_SPEED: qh->qh.ep_state[0] |= (0 << 12); break; + case CDI_USB_HIGH_SPEED: qh->qh.ep_state[0] |= (2 << 12); break; + case CDI_USB_SUPERSPEED: + default: + abort(); + } + + qh->qh.ep_state[1] = (0xff << 8) | (1 << 30) | (target->tt_addr << 16) + | ((target->tt_port + 1) << 23); + + qh->qh.overlay.next_qtd = EHCI_T; + qh->qh.overlay.alt_next_qtd = EHCI_T; + + qh->cdi_mem_area = qh_mem; + qh->paddr = qh_mem->paddr.items[0].start + offsetof(ehci_fat_qh_t, qh); + + mem_barrier(); + + enter_async_qh(hc, qh); + + return qh; +} + + +static void create_qtd(ehci_fat_qh_t *qh, cdi_usb_transfer_token_t token, + int toggle, void *buffer, size_t size, + cdi_usb_transmission_result_t *result) +{ + struct cdi_mem_area *qtd_mem; + + qtd_mem = cdi_mem_alloc(sizeof(ehci_fat_qtd_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + ehci_fat_qtd_t *qtd = qtd_mem->vaddr; + + memset(qtd, 0, sizeof(*qtd)); + + qtd->qtd.status = (1 << 7) | (3 << 10) | (size << 16) + | ((uint32_t)toggle << 31); + + switch (token) { + case CDI_USB_IN: qtd->qtd.status |= (1 << 8); break; + case CDI_USB_OUT: qtd->qtd.status |= (0 << 8); break; + case CDI_USB_SETUP: qtd->qtd.status |= (2 << 8); break; + default: + abort(); + } + + if (token == CDI_USB_IN) { + qtd->write_back = buffer; + qtd->write_back_size = size; + } + + for (int i = 0; i < 5 && size; i++) { + size_t sz = size > 4096 ? 4096 : size; + + qtd->buffers[i] = cdi_mem_alloc(4096, 12 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + qtd->qtd.buffers[i] = qtd->buffers[i]->paddr.items[0].start; + + if (token == CDI_USB_OUT || token == CDI_USB_SETUP) { + memcpy(qtd->buffers[i]->vaddr, + (void *)((uintptr_t)buffer + i * 4096), sz); + } + + size -= sz; + } + + qtd->cdi_mem_area = qtd_mem; + qtd->paddr = qtd_mem->paddr.items[0].start + offsetof(ehci_fat_qtd_t, qtd); + + qtd->result = result; + + mem_barrier(); + + enter_qtd(qh, qtd); +} + + +void ehci_enqueue(struct cdi_usb_hc *cdi_hc, + const struct cdi_usb_hc_transmission *trans) +{ + (void)cdi_hc; + + ehci_fat_qh_t *qh = trans->ta; + bool first = true; + size_t sz = trans->size; + void *buf = trans->buffer; + int toggle = trans->toggle; + size_t mps = (qh->qh.ep_state[0] >> 16) & 0x7ff; + + *trans->result = CDI_USB_OK; + + while (sz || first) { + first = false; + + size_t iter_sz = sz > 4096 * 5 ? 4096 * 5 : sz; + + create_qtd(qh, trans->token, toggle, buf, iter_sz, trans->result); + + buf = (void *)((uintptr_t)buf + iter_sz); + sz -= iter_sz; + + toggle ^= ((iter_sz + mps - 1) / mps) & 1; + } +} + + +void ehci_wait_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + ehci_fat_qh_t *qh = ta; + bool had_timeout = false; + + irq_handler(&cdi_hc->dev); + + while (qh->first_qtd) { + if (!had_timeout) { + if (!(qh->qh.overlay.status & (1 << 7))) { + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; + mem_barrier(); + } + + int frame_counter = 0, lframe = (hc->regs->frindex >> 3) % 1024; + + while ((qh->first_qtd->qtd.status & (1 << 7)) && + frame_counter < EHCI_TIMEOUT) + { + int cframe = (hc->regs->frindex >> 3) % 1024; + frame_counter += (cframe + 1024 - lframe) % 1024; + lframe = cframe; + + // I don't really know who's to blame for this (qemu or + // t?ndur), and actually, I don't even want to know. + // As a matter of fact, qemu will not work on the async + // schedule if the async advance doorbell is set but the driver + // has not acknowledged this; however, the interrupt handler + // will always clear that bit and the interrupt handler is + // active all the time, so something is apparently sometimes + // blocking the handler from being executed. Force it here to + // prevent qemu's HC from stalling. + irq_handler(&cdi_hc->dev); + } + + had_timeout = frame_counter >= EHCI_TIMEOUT; + } + + if (had_timeout) { + // Stop this queue + qh->qh.overlay.next_qtd = EHCI_T; + qh->qh.overlay.status &= ~(1u << 7); + + *qh->first_qtd->result = CDI_USB_TIMEOUT; + } + + retire_qtd(qh->first_qtd); + } +} + + +void ehci_destroy_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); + ehci_fat_qh_t *qh = ta; + + // TODO: Does this work without having called ehci_wait_transaction() + // before? + qh->qh.overlay.next_qtd = EHCI_T; + + int frame_counter = 0, lframe = (hc->regs->frindex >> 3) % 1024; + + while ((qh->qh.overlay.status & (1 << 7)) && frame_counter < EHCI_TIMEOUT) { + int cframe = (hc->regs->frindex >> 3) % 1024; + frame_counter += (cframe + 1024 - lframe) % 1024; + lframe = cframe; + + // See above. + irq_handler(&cdi_hc->dev); + } + + if (frame_counter >= EHCI_TIMEOUT) { + qh->qh.overlay.status &= ~(1 << 7); + // Could probably be solved more elegantly, but we are in a timeout + // already, so waiting a bit longer will not be that big of a problem + // (the sleep is here to ensure that the HC is no longer trying to work + // on the qTDs belonging to this QH) + cdi_sleep_ms(100); + } + + while (qh->first_qtd) { + retire_qtd(qh->first_qtd); + } + retire_qh(hc, qh); +} diff --git a/ehci/ehci.h b/ehci/ehci.h new file mode 100644 index 0000000..d1aa99a --- /dev/null +++ b/ehci/ehci.h @@ -0,0 +1,170 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef EHCI_H +#define EHCI_H + +#include +#include + +#include +#include +#include +#include + + +struct ehci_registers { + uint32_t usbcmd; + uint32_t usbsts; + uint32_t usbintr; + uint32_t frindex; + uint32_t ctrldssegment; + uint32_t periodiclistbase; + uint32_t asynclistaddr; + uint8_t reserved[0x24]; + uint32_t configflag; + uint32_t portsc[]; +} __attribute__((packed)); + +struct ehci_capabilities { + uint8_t caplength; + uint8_t reserved; + uint16_t hciversion; + uint32_t hcsparams; + uint32_t hccparams; + uint64_t hcsp_portroute; +} __attribute__((packed)); + +struct ehci_qtd { + uint32_t next_qtd; + uint32_t alt_next_qtd; + uint32_t status; + uint32_t buffers[5]; + + // 64-bit structures + uint32_t ext_buffer[5]; +} __attribute__((packed)); + +struct ehci_qh { + uint32_t next_qh; + uint32_t ep_state[2]; + uint32_t current_qtd; + struct ehci_qtd overlay; +} __attribute__((packed)); + +typedef struct ehci_fat_qh ehci_fat_qh_t; +typedef struct ehci_fat_qtd ehci_fat_qtd_t; + +#define DIFF_TO_MULTIPLE(value, divisor) \ + ((divisor) - ((((value) + (divisor) - 1) % (divisor)) + 1)) + +struct ehci_fat_qh { + volatile struct ehci_qh qh; + + // Align the QH to 128 bytes (something with cache lines?). Otherwise, + // there may be conflicts between data we are writing to our very own + // fields provided by ehci_fat_qh_t and writes done by the HC to this + // structure. + uint8_t alignment[DIFF_TO_MULTIPLE(sizeof(struct ehci_qh), 128)]; + + struct cdi_mem_area *cdi_mem_area; + uintptr_t paddr; + + ehci_fat_qh_t *next, *previous; + ehci_fat_qtd_t *first_qtd, *last_qtd; +}; + +struct ehci_fat_qtd { + volatile struct ehci_qtd qtd; + + // Align the qTD to a multiple of 128 bytes. + uint8_t alignment[DIFF_TO_MULTIPLE(sizeof(struct ehci_qtd), 128)]; + + struct cdi_mem_area *cdi_mem_area; + uintptr_t paddr; + + ehci_fat_qh_t *qh; + ehci_fat_qtd_t *next; + + struct cdi_mem_area *buffers[5]; + + void *write_back; + size_t write_back_size; + + cdi_usb_transmission_result_t *result; +}; + +struct ehci { + struct cdi_usb_hc hc; + + volatile struct ehci_capabilities *caps; + volatile struct ehci_registers *regs; + + struct cdi_mem_area *periodic_list_mem; + volatile uint32_t *periodic_list; + + ehci_fat_qh_t *async_start; + cdi_list_t retired_qhs; +}; + +#define EHCI_T (1u << 0) +#define EHCI_ITD (0u << 1) +#define EHCI_QH (1u << 1) +#define EHCI_SITD (2u << 1) +#define EHCI_FSTN (3u << 1) + +#define EHCI_PSC_CCS (1u << 0) // current connect status +#define EHCI_PSC_PED (1u << 2) // port enable/disable +#define EHCI_PSC_FPR (1u << 6) // force port resume +#define EHCI_PSC_SUS (1u << 7) // suspend +#define EHCI_PSC_RES (1u << 8) // reset +#define EHCI_PSC_PP (1u << 12) // port power +#define EHCI_PSC_PO (1u << 13) // port owner + +#define EHCI_PSC_LS_MASK (0x3u << 10) +#define EHCI_PSC_LS_SE0 (0x0u << 10) +#define EHCI_PSC_LS_K (0x1u << 10) +#define EHCI_PSC_LS_J (0x2u << 10) + + +static inline void mem_barrier(void) +{ + __asm__ __volatile("" ::: "memory"); +} + + +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data); + +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index); +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index); +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index); + +cdi_usb_hc_transaction_t + ehci_create_transaction(struct cdi_usb_hc *hc, + const struct cdi_usb_hc_ep_info *target); +void ehci_enqueue(struct cdi_usb_hc *hc, + const struct cdi_usb_hc_transmission *trans); +void ehci_wait_transaction(struct cdi_usb_hc *hc, cdi_usb_hc_transaction_t ta); +void ehci_destroy_transaction(struct cdi_usb_hc *hc, + cdi_usb_hc_transaction_t ta); + +#endif diff --git a/ehci/main.c b/ehci/main.c new file mode 100644 index 0000000..62939b2 --- /dev/null +++ b/ehci/main.c @@ -0,0 +1,68 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include + +#include "ehci.h" + + +#define DRIVER_NAME "ehci" + + +static struct cdi_usb_hcd ehcd; + +static int ehcd_init(void) +{ + cdi_driver_init(&ehcd.drv); + return 0; +} + +static int ehcd_destroy(void) +{ + cdi_driver_destroy(&ehcd.drv); + return 0; +} + +static struct cdi_usb_hcd ehcd = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_USB_HCD, + .bus = CDI_PCI, + .init = ehcd_init, + .destroy = ehcd_destroy, + .init_device = ehci_init_device, + }, + + .rh_drv = { + .port_disable = ehci_rh_port_down, + .port_reset_enable = ehci_rh_port_up, + .port_status = ehci_rh_port_status, + }, + + .create_transaction = ehci_create_transaction, + .enqueue = ehci_enqueue, + .wait_transaction = ehci_wait_transaction, + .destroy_transaction = ehci_destroy_transaction, +}; + +CDI_DRIVER(DRIVER_NAME, ehcd) -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:57 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:57 +0200 Subject: [cdi-devel] [PATCH v2 8/9] usb: Add USB 2.0 bus driver In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-9-git-send-email-max@tyndur.org> + This adds a USB 2.0 bus driver. It is missing support for USB 3.x (obviously), and also interrupt and isochronous transfers (and other things, too). Also, it is probably bad (e.g. there is only a single counter for device IDs instead of keeping track of which IDs are used and which are not). Signed-off-by: Max Reitz --- usb/main.c | 61 ++++ usb/usb-hubs.h | 78 ++++++ usb/usb.c | 864 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ usb/usb.h | 86 ++++++ 4 files changed, 1089 insertions(+) create mode 100644 usb/main.c create mode 100644 usb/usb-hubs.h create mode 100644 usb/usb.c create mode 100644 usb/usb.h diff --git a/usb/main.c b/usb/main.c new file mode 100644 index 0000000..3add372 --- /dev/null +++ b/usb/main.c @@ -0,0 +1,61 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include + +#include "usb.h" + + +#define DRIVER_NAME "usb" + + +static struct cdi_usb_driver usbd; + +static int usbd_init(void) +{ + cdi_driver_init(&usbd.drv); + return 0; +} + +static int usbd_destroy(void) +{ + cdi_driver_destroy(&usbd.drv); + return 0; +} + +static struct cdi_usb_driver usbd = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_USB, + .bus = CDI_USB_HCD, + .init = usbd_init, + .destroy = usbd_destroy, + .init_device = usb_init_hc, + }, + + .get_endpoint_descriptor = usb_get_endpoint_descriptor, + .control_transfer = usb_control_transfer, + .bulk_transfer = usb_bulk_transfer, +}; + +CDI_DRIVER(DRIVER_NAME, usbd) diff --git a/usb/usb-hubs.h b/usb/usb-hubs.h new file mode 100644 index 0000000..da8ac3e --- /dev/null +++ b/usb/usb-hubs.h @@ -0,0 +1,78 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef USB_HUBS_H +#define USB_HUBS_H + +#include + +#include "usb.h" + + +struct usb_hub_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_nbr_ports; + uint16_t w_hub_characteristics; + uint8_t b_pwr_on_2_pwr_good; + uint8_t b_hub_contr_current; + uint8_t device_removable[]; +} __attribute__((packed)); + + +enum { + USB_DESC_HUB = 0x29, +}; + +// hub device features +enum { + C_HUB_LOCAL_POWER = 0, + C_HUB_OVER_CURRENT, +}; + +// hub port features +enum { + PORT_CONNECTION = 0, + PORT_ENABLE, + PORT_SUSPEND, + PORT_OVER_CURRENT, + PORT_RESET, + + PORT_POWER = 8, + PORT_LOW_SPEED, + PORT_HIGH_SPEED, + + C_PORT_CONNECTION = 16, + C_PORT_ENABLE, + C_PORT_SUSPEND, + C_PORT_OVER_CURRENT, + C_PORT_RESET, + PORT_TEST, + PORT_INDICATOR, +}; + +typedef struct usb_hub_device { + struct cdi_usb_hub cdi; + usb_hub_t *hub; +} usb_hub_device_t; + +#endif diff --git a/usb/usb.c b/usb/usb.c new file mode 100644 index 0000000..125da2c --- /dev/null +++ b/usb/usb.c @@ -0,0 +1,864 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "usb.h" +#include "usb-hubs.h" + + +// #define DEBUG + +#ifdef DEBUG +#define DPRINTF(...) printf(__VA_ARGS__) +#else +#define DPRINTF(...) +#endif + + +static int current_usb_addr = 1; + + +static cdi_usb_transmission_result_t + do_control_transfer(struct cdi_usb_hc *hc, int dev, cdi_usb_speed_t speed, + int tt_addr, int tt_port, const usb_endpoint_t *ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + struct cdi_usb_hcd *hcd = CDI_UPCAST(hc->dev.driver, drv, + struct cdi_usb_hcd); + cdi_usb_hc_transaction_t ta; + cdi_usb_transmission_result_t r1, r2, r3; + + ta = hcd->create_transaction(hc, &(struct cdi_usb_hc_ep_info){ + .dev = dev, + .ep = ep->ep, + .ep_type = ep->type, + .speed = speed, + .mps = ep->mps, + .tt_addr = tt_addr, + .tt_port = tt_port + }); + + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = CDI_USB_SETUP, + .buffer = (void *)setup, + .size = sizeof(*setup), + .toggle = 0, + .result = &r1 + }); + + if (data) { + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = setup->bm_request_type & CDI_USB_CREQ_IN + ? CDI_USB_IN : CDI_USB_OUT, + .buffer = data, + .size = setup->w_length, + .toggle = 1, + .result = &r2 + }); + } else { + r2 = CDI_USB_OK; + } + + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = setup->bm_request_type & CDI_USB_CREQ_IN ? CDI_USB_OUT + : CDI_USB_IN, + .toggle = 1, + .result = &r3 + }); + + hcd->wait_transaction(hc, ta); + hcd->destroy_transaction(hc, ta); + + return r1 ? r1 : r2 ? r2 : r3; +} + +static cdi_usb_transmission_result_t + control_transfer(const usb_device_t *dev, const usb_endpoint_t *ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + return do_control_transfer(dev->hc, dev->id, dev->speed, dev->tt_addr, + dev->tt_port, ep, setup, data); +} + + +static int get_mps0(usb_hub_t *hub, int hub_port, cdi_usb_speed_t speed) +{ + cdi_usb_transmission_result_t res; + struct cdi_usb_device_descriptor device_descriptor; + int tt_addr = 0, tt_port = 0; + + if (speed < CDI_USB_HIGH_SPEED && hub->ldev) { + if (hub->ldev->dev->speed < CDI_USB_HIGH_SPEED) { + tt_addr = hub->ldev->dev->tt_addr; + tt_port = hub->ldev->dev->tt_port; + } else { + tt_addr = hub->ldev->dev->id; + tt_port = hub_port; + } + } + + res = do_control_transfer(hub->hc, 0, speed, tt_addr, tt_port, + &(usb_endpoint_t){ + .type = CDI_USB_CONTROL, + .mps = 8 + }, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_DEVICE << 8) | 0, + .w_length = 8 + }, &device_descriptor); + + if (res != CDI_USB_OK) { + return -EIO; + } + + return device_descriptor.b_max_packet_size_0; +} + + +static usb_device_t *enumerate(usb_hub_t *hub, int hub_port, + cdi_usb_speed_t speed, int mps0) +{ + cdi_usb_transmission_result_t res; + usb_device_t *dev = calloc(1, sizeof(*dev)); + + int new_id = current_usb_addr++; + dev->hc = hub->hc; + dev->speed = speed; + + if (speed < CDI_USB_HIGH_SPEED && hub->ldev) { + if (hub->ldev->dev->speed < CDI_USB_HIGH_SPEED) { + dev->tt_addr = hub->ldev->dev->tt_addr; + dev->tt_port = hub->ldev->dev->tt_port; + } else { + dev->tt_addr = hub->ldev->dev->id; + dev->tt_port = hub_port; + } + } + + dev->eps[0][0].type = CDI_USB_CONTROL; + dev->eps[0][0].mps = mps0; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_ADDRESS, + .w_value = new_id + }, NULL); + if (res != CDI_USB_OK) { + free(dev); + return NULL; + } + + dev->id = new_id; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_DEVICE << 8) | 0, + .w_length = sizeof(dev->dev_desc) + }, &dev->dev_desc); + if (res != CDI_USB_OK) { + free(dev); + return NULL; + } + + return dev; +} + + +#ifdef DEBUG +static void get_string_descriptor(usb_device_t *dev, int index, + struct cdi_usb_string_descriptor *dst) +{ + cdi_usb_transmission_result_t res; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_STRING << 8) | index, + .w_length = 1 + }, dst); + if (res != CDI_USB_OK || !dst->b_length) { + dst->b_length = 0; + return; + } + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_STRING << 8) | index, + .w_length = dst->b_length + }, dst); + if (res != CDI_USB_OK) { + dst->b_length = 0; + return; + } +} +#endif + + +static int get_configurations(usb_device_t *dev) +{ + cdi_usb_transmission_result_t res; + struct cdi_usb_configuration_descriptor config; + + dev->configs = calloc(1, dev->dev_desc.b_num_configurations + * sizeof(*dev->configs)); + + for (int i = 0; i < (int)dev->dev_desc.b_num_configurations; i++) { + res = control_transfer(dev, &dev->eps[0][0], + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_CONFIGURATION << 8) | i, + .w_length = sizeof(config) + }, &config); + if (res != CDI_USB_OK) { + goto fail; + } + + dev->configs[i] = malloc(config.w_total_length); + res = control_transfer(dev, &dev->eps[0][0], + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_CONFIGURATION << 8) | i, + .w_length = config.w_total_length + }, dev->configs[i]); + if (res != CDI_USB_OK) { + goto fail; + } + } + + return 0; + +fail: + for (int i = 0; i < (int)dev->dev_desc.b_num_configurations; i++) { + free(dev->configs[i]); + } + free(dev->configs); + return -EIO; +} + + +static bool compare_configurations(usb_device_t *dev, int i, int j) +{ + // TODO: Prefer self-powered + if (dev->configs[i]->b_max_power != dev->configs[j]->b_max_power) { + return dev->configs[i]->b_max_power > dev->configs[j]->b_max_power; + } + + if (dev->configs[i]->b_num_interfaces != dev->configs[j]->b_num_interfaces) + { + return dev->configs[i]->b_num_interfaces + > dev->configs[j]->b_num_interfaces; + } + + return false; +} + + +static int choose_configuration(usb_device_t *dev) +{ + cdi_usb_transmission_result_t res; + int best_config = 0; + + for (int i = 1; i < (int)dev->dev_desc.b_num_configurations; i++) { + if (compare_configurations(dev, i, best_config)) { + best_config = i; + } + } + + dev->config = dev->configs[best_config]; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_CONFIGURATION, + .w_value = dev->config->b_configuration_value, + }, NULL); + if (res != CDI_USB_OK) { + dev->config = NULL; + return -EIO; + } + + return 0; +} + + +#ifdef DEBUG +static void debug_device(usb_device_t *dev) +{ + printf("[usb] new %s device %04x:%04x (%x.%x.%x) ", + dev->speed == CDI_USB_LOW_SPEED ? "low speed" : + dev->speed == CDI_USB_FULL_SPEED ? "full speed" : + dev->speed == CDI_USB_HIGH_SPEED ? "high speed" : + dev->speed == CDI_USB_SUPERSPEED ? "superspeed" : + "futurespeed", + dev->dev_desc.id_vendor, dev->dev_desc.id_product, + dev->dev_desc.b_device_class, + dev->dev_desc.b_device_sub_class, + dev->dev_desc.b_device_protocol); + + struct cdi_usb_string_descriptor str; + + if (dev->dev_desc.i_manufacturer) { + get_string_descriptor(dev, dev->dev_desc.i_manufacturer, &str); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(' '); + } + + if (dev->dev_desc.i_product) { + get_string_descriptor(dev, dev->dev_desc.i_product, &str); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(' '); + } + + putchar('\n'); +} + +static void debug_configuration(usb_device_t *dev) +{ + printf("[usb] chose configuration %i ", dev->config->b_configuration_value); + + struct cdi_usb_string_descriptor str; + + if (dev->config->i_configuration) { + get_string_descriptor(dev, dev->config->i_configuration, &str); + putchar('('); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(')'); + putchar(' '); + } + + printf("with %i interface(s):\n", dev->config->b_num_interfaces); + + struct cdi_usb_interface_descriptor *ifc = (void *)(dev->config + 1); + for (int i = 0; i < (int)dev->config->b_num_interfaces; i++) { + printf("[usb] "); + if (ifc->i_interface) { + get_string_descriptor(dev, ifc->i_interface, &str); + for (int j = 0; j < (int)dev->config->b_num_interfaces; j++) { + putchar(str.b_string[j] > 0x7f ? '?' : str.b_string[j]); + } + putchar(' '); + } + printf("%x.%x.%x\n", ifc->b_interface_class, + ifc->b_interface_sub_class, ifc->b_interface_protocol); + + ifc = (void *)((struct cdi_usb_endpoint_descriptor *)(ifc + 1) + + ifc->b_num_endpoints); + } +} +#endif + + +static void new_hub_device(usb_logical_device_t *ldev); + +static void new_hub(usb_hub_t *hub) +{ + for (int i = 0; i < hub->cdi->ports; i++) { + hub->drv->port_disable(hub->cdi, i); + } + + DPRINTF("[usb] new hub with %i ports detected\n", hub->cdi->ports); + + for (int i = 0; i < hub->cdi->ports; i++) { + hub->drv->port_reset_enable(hub->cdi, i); + cdi_sleep_ms(5); + + cdi_usb_port_status_t status = hub->drv->port_status(hub->cdi, i); + if (!(status & CDI_USB_PORT_CONNECTED)) { + hub->drv->port_disable(hub->cdi, i); + continue; + } + + int mps0 = get_mps0(hub, i, status & CDI_USB_PORT_SPEED_MASK); + if (mps0 < 0) { + fprintf(stderr, "[usb] failed to inquire MPS0\n"); + hub->drv->port_disable(hub->cdi, i); + continue; + } + + hub->drv->port_reset_enable(hub->cdi, i); + cdi_sleep_ms(5); + + status = hub->drv->port_status(hub->cdi, i); + if (!(status & CDI_USB_PORT_CONNECTED)) { + hub->drv->port_disable(hub->cdi, i); + continue; + } + + usb_device_t *dev = enumerate(hub, i, status & CDI_USB_PORT_SPEED_MASK, + mps0); + if (!dev) { + fprintf(stderr, "[usb] failed to enumerate device\n"); + hub->drv->port_disable(hub->cdi, i); + continue; + } + +#ifdef DEBUG + debug_device(dev); +#endif + + bool interface_level = !dev->dev_desc.b_device_class; + usb_logical_device_t *ldev = NULL; + int ldev_ep_idx = 0; + if (!interface_level) { + ldev = calloc(1, sizeof(*ldev)); + ldev->cdi.bus_data.bus_type = CDI_USB; + ldev->cdi.interface = -1; + ldev->cdi.endpoint_count = 1; // EP0 + ldev->cdi.vendor_id = dev->dev_desc.id_vendor; + ldev->cdi.product_id = dev->dev_desc.id_product; + ldev->cdi.class_id = dev->dev_desc.b_device_class; + ldev->cdi.subclass_id = dev->dev_desc.b_device_sub_class; + ldev->cdi.protocol_id = dev->dev_desc.b_device_protocol; + + ldev->dev = dev; + + ldev->endpoints = malloc(1 * sizeof(ldev->endpoints[0])); + ldev->endpoints[0] = 0; // EP0 + ldev_ep_idx = 1; + } + + if (get_configurations(dev) < 0) { + fprintf(stderr, "[usb] failed to get device configurations\n"); + free(ldev->endpoints); + free(ldev); + free(dev); + hub->drv->port_disable(hub->cdi, i); + continue; + } + if (choose_configuration(dev) < 0) { + fprintf(stderr, "[usb] failed to select device configuration\n"); + free(ldev->endpoints); + free(ldev); + for (int j = 0; j < (int)dev->dev_desc.b_num_configurations; j++) { + free(dev->configs[j]); + } + free(dev->config); + free(dev); + hub->drv->port_disable(hub->cdi, i); + continue; + } + +#ifdef DEBUG + debug_configuration(dev); +#endif + + struct cdi_usb_interface_descriptor *ifc = (void *)(dev->config + 1); + for (int j = 0; j < (int)dev->config->b_num_interfaces; j++) { + if (interface_level) { + ldev = calloc(1, sizeof(*ldev)); + ldev->cdi.bus_data.bus_type = CDI_USB; + ldev->cdi.interface = j; + // Include EP0 + ldev->cdi.endpoint_count = ifc->b_num_endpoints + 1; + ldev->cdi.vendor_id = dev->dev_desc.id_vendor; + ldev->cdi.product_id = dev->dev_desc.id_product; + ldev->cdi.class_id = ifc->b_interface_class; + ldev->cdi.subclass_id = ifc->b_interface_sub_class; + ldev->cdi.protocol_id = ifc->b_interface_protocol; + + ldev->dev = dev; + + ldev->endpoints = malloc(ldev->cdi.endpoint_count + * sizeof(ldev->endpoints[0])); + ldev->endpoints[0] = 0; // EP0 + ldev_ep_idx = 1; + } else { + ldev->cdi.endpoint_count += ifc->b_num_endpoints; + ldev->endpoints = realloc(ldev->endpoints, + ldev->cdi.endpoint_count + * sizeof(ldev->endpoints[0])); + } + + struct cdi_usb_endpoint_descriptor *ep = (void *)(ifc + 1); + for (int k = 0; k < (int)ifc->b_num_endpoints; k++, ep++) { + bool in = ep->b_endpoint_address & 0x80; + int id = ep->b_endpoint_address & 0x0f; + + dev->eps[in][id].ep = id; + dev->eps[in][id].mps = ep->w_max_packet_size; + dev->eps[in][id].desc = ep; + + switch (ep->bm_attributes & 0x3) { + case 0: dev->eps[in][id].type = CDI_USB_CONTROL; + break; + case 1: dev->eps[in][id].type = CDI_USB_ISOCHRONOUS; + break; + case 2: dev->eps[in][id].type = CDI_USB_BULK; + break; + case 3: dev->eps[in][id].type = CDI_USB_INTERRUPT; + break; + } + + ldev->endpoints[ldev_ep_idx++] = ((int)in << 4) | id; + } + + if (interface_level) { + if (ldev->cdi.class_id == 0x09) { + new_hub_device(ldev); + } else { + cdi_provide_device(&ldev->cdi.bus_data); + } + } + + ifc = (void *)ep; + } + + if (!interface_level) { + if (ldev->cdi.class_id == 0x09) { + new_hub_device(ldev); + } else { + cdi_provide_device(&ldev->cdi.bus_data); + } + } + } +} + +struct cdi_device *usb_init_hc(struct cdi_bus_data *bus_data) +{ + struct cdi_usb_hc_bus *hc_bus = CDI_UPCAST(bus_data, bus_data, + struct cdi_usb_hc_bus); + struct cdi_usb_hc *hc = hc_bus->hc; + struct cdi_usb_hcd *hcd = CDI_UPCAST(hc->dev.driver, drv, + struct cdi_usb_hcd); + + usb_hub_t *hub = calloc(1, sizeof(*hub)); + hub->cdi = &hc->rh; + hub->hc = hc; + hub->drv = &hcd->rh_drv; + + new_hub(hub); + + return NULL; +} + + +void usb_get_endpoint_descriptor(struct cdi_usb_device *dev, int ep, + struct cdi_usb_endpoint_descriptor *desc) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, cdi, usb_logical_device_t); + + if (ep < 0 || ep >= dev->endpoint_count) { + return; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_idx) { + assert(ep_info->desc); + memcpy(desc, ep_info->desc, sizeof(*desc)); + } else { + *desc = (struct cdi_usb_endpoint_descriptor){ + .b_length = sizeof(*desc), + .b_descriptor_type = CDI_USB_DESC_ENDPOINT, + .b_endpoint_address = 0, + .bm_attributes = 0x00, + .w_max_packet_size = ep_info->mps, + .b_interval = 0 + }; + } +} + + +cdi_usb_transmission_result_t + usb_control_transfer(struct cdi_usb_device *dev, int ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, cdi, usb_logical_device_t); + + if (ep < 0 || ep >= dev->endpoint_count) { + return CDI_USB_DRIVER_ERROR; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_info->type != CDI_USB_CONTROL) { + return CDI_USB_DRIVER_ERROR; + } + + return control_transfer(ldev->dev, ep_info, setup, data); +} + + +cdi_usb_transmission_result_t usb_bulk_transfer(struct cdi_usb_device *dev, + int ep, void *data, size_t size) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, cdi, usb_logical_device_t); + + if (ep < 0 || ep >= dev->endpoint_count) { + return CDI_USB_DRIVER_ERROR; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_info->type != CDI_USB_BULK) { + return CDI_USB_DRIVER_ERROR; + } + + struct cdi_usb_hcd *hcd = CDI_UPCAST(ldev->dev->hc->dev.driver, drv, + struct cdi_usb_hcd); + cdi_usb_hc_transaction_t ta; + cdi_usb_transmission_result_t res; + + ta = hcd->create_transaction(ldev->dev->hc, &(struct cdi_usb_hc_ep_info){ + .dev = ldev->dev->id, + .ep = ep_info->ep, + .ep_type = ep_info->type, + .speed = ldev->dev->speed, + .mps = ep_info->mps, + .tt_addr = ldev->dev->tt_addr, + .tt_port = ldev->dev->tt_port + }); + + hcd->enqueue(ldev->dev->hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = ep_idx >> 4 ? CDI_USB_IN : CDI_USB_OUT, + .buffer = data, + .size = size, + .toggle = ep_info->toggle, + .result = &res + }); + ep_info->toggle ^= (((size + ep_info->mps - 1) / ep_info->mps) & 1); + + hcd->wait_transaction(ldev->dev->hc, ta); + hcd->destroy_transaction(ldev->dev->hc, ta); + + return res; +} + + +static struct cdi_usb_hub_driver hub_drv; + + +static void new_hub_device(usb_logical_device_t *ldev) +{ + cdi_usb_transmission_result_t res; + usb_hub_device_t *hub; + struct usb_hub_descriptor *hub_desc = NULL; + + assert(ldev->cdi.interface < 0); + + hub = calloc(1, sizeof(*hub)); + + hub->hub = calloc(1, sizeof(*hub->hub)); + hub->hub->cdi = &hub->cdi; + hub->hub->drv = &hub_drv; + hub->hub->hc = ldev->dev->hc; + hub->hub->ldev = ldev; + + uint8_t hub_desc_length; + res = usb_control_transfer(&ldev->cdi, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (USB_DESC_HUB << 8) | 0, + .w_length = 1 + }, &hub_desc_length); + if (res != CDI_USB_OK || + hub_desc_length < sizeof(struct usb_hub_descriptor)) + { + goto fail; + } + + hub_desc = malloc(hub_desc_length); + res = usb_control_transfer(&ldev->cdi, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (USB_DESC_HUB << 8) | 0, + .w_length = hub_desc_length + }, hub_desc); + if (res != CDI_USB_OK) { + goto fail; + } + + int ports = hub_desc->b_nbr_ports; + hub->cdi.ports = ports; + + // Enable port power + if (!(hub_desc->w_hub_characteristics & (1 << 1))) { + // Hub supports power switching, enable global power + res = usb_control_transfer(&ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE + | CDI_USB_CREQ_CLASS | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = C_HUB_LOCAL_POWER + }, NULL); + if (res != CDI_USB_OK) { + goto fail; + } + } + + if ((hub_desc->w_hub_characteristics & 0x3) == 1) { + // Hub supports per-port power switching, enable power for each port + for (int port = 0; port < ports; port++) { + res = usb_control_transfer(&ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = PORT_POWER, + .w_index = port + 1 + }, NULL); + if (res != CDI_USB_OK) { + goto fail; + } + } + } + + cdi_sleep_ms(hub_desc->b_pwr_on_2_pwr_good * 2); + + free(hub_desc); + + new_hub(hub->hub); + + return; + +fail: + free(hub_desc); + free(hub->hub); + free(hub); + return; +} + + +static void hub_port_down(struct cdi_usb_hub *cdi_hub, int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, cdi, usb_hub_device_t); + + usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_CLEAR_FEATURE, + .w_value = PORT_ENABLE, + .w_index = index + 1 + }, NULL); +} + + +static void hub_port_up(struct cdi_usb_hub *cdi_hub, int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, cdi, usb_hub_device_t); + + usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = PORT_ENABLE, + .w_index = index + 1 + }, NULL); + + usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = PORT_RESET, + .w_index = index + 1 + }, NULL); + + cdi_sleep_ms(50); + + usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_CLEAR_FEATURE, + .w_value = PORT_RESET, + .w_index = index + 1 + }, NULL); + + cdi_sleep_ms(5); +} + + +static cdi_usb_port_status_t hub_port_status(struct cdi_usb_hub *cdi_hub, + int index) +{ + cdi_usb_transmission_result_t res; + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, cdi, usb_hub_device_t); + + uint32_t portsc; + res = usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_STATUS, + .w_index = index + 1, + .w_length = sizeof(portsc) + }, &portsc); + if (res != CDI_USB_OK) { + return 0; + } + + cdi_usb_port_status_t status = 0; + + if (portsc & ((1 << PORT_ENABLE) | (1 << PORT_CONNECTION))) { + status |= CDI_USB_PORT_CONNECTED; + if (portsc & (1 << PORT_LOW_SPEED)) { + status |= CDI_USB_LOW_SPEED; + } else if (portsc & (1 << PORT_HIGH_SPEED)) { + status |= CDI_USB_HIGH_SPEED; + } else { + status |= CDI_USB_FULL_SPEED; + } + } + + return status; +} + + +static struct cdi_usb_hub_driver hub_drv = { + .port_disable = hub_port_down, + .port_reset_enable = hub_port_up, + .port_status = hub_port_status, +}; diff --git a/usb/usb.h b/usb/usb.h new file mode 100644 index 0000000..27dc061 --- /dev/null +++ b/usb/usb.h @@ -0,0 +1,86 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef USB_H +#define USB_H + +#include +#include + +#include +#include +#include + + +typedef struct usb_logical_device usb_logical_device_t; + +typedef struct usb_hub { + struct cdi_usb_hub *cdi; + + const struct cdi_usb_hub_driver *drv; + struct cdi_usb_hc *hc; + usb_logical_device_t *ldev; +} usb_hub_t; + +typedef struct usb_endpoint { + int ep; + cdi_usb_endpoint_type_t type; + int mps; + int toggle; + struct cdi_usb_endpoint_descriptor *desc; +} usb_endpoint_t; + +typedef struct usb_device { + struct cdi_usb_hc *hc; + int id; + cdi_usb_speed_t speed; + + int tt_addr, tt_port; + + // First index: OUT (0) / IN (1) + usb_endpoint_t eps[2][16]; + + struct cdi_usb_device_descriptor dev_desc; + struct cdi_usb_configuration_descriptor **configs, *config; +} usb_device_t; + +struct usb_logical_device { + struct cdi_usb_device cdi; + usb_device_t *dev; + int *endpoints; +}; + + +struct cdi_device *usb_init_hc(struct cdi_bus_data *bus_data); + +void usb_get_endpoint_descriptor(struct cdi_usb_device *dev, int ep, + struct cdi_usb_endpoint_descriptor *desc); + +cdi_usb_transmission_result_t + usb_control_transfer(struct cdi_usb_device *dev, int ep, + const struct cdi_usb_setup_packet *setup, void *data); + +cdi_usb_transmission_result_t usb_bulk_transfer(struct cdi_usb_device *dev, + int ep, void *data, + size_t size); + +#endif -- 2.3.5 From max at tyndur.org Thu Apr 16 00:13:58 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 00:13:58 +0200 Subject: [cdi-devel] [PATCH v2 9/9] usb-storage: Add USB mass storage driver In-Reply-To: <1429136038-29537-1-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> Message-ID: <1429136038-29537-10-git-send-email-max@tyndur.org> + This adds a USB mass storage device driver. It is missing proper error handling (e.g. for STALLed endpoints) and support for any USB MSD class other than bulk-only (e.g. control-bulk-interrupt for USB floppy drives). Signed-off-by: Max Reitz --- usb-storage/main.c | 76 +++++++++++++++++ usb-storage/usb-storage.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++ usb-storage/usb-storage.h | 34 ++++++++ 3 files changed, 323 insertions(+) create mode 100644 usb-storage/main.c create mode 100644 usb-storage/usb-storage.c create mode 100644 usb-storage/usb-storage.h diff --git a/usb-storage/main.c b/usb-storage/main.c new file mode 100644 index 0000000..76d783e --- /dev/null +++ b/usb-storage/main.c @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include + +#include "usb-storage.h" + + +#define DRIVER_NAME "usb-storage" + + +static struct cdi_scsi_driver drv; +static struct cdi_usb_bus_device_pattern bus_pattern; + +static int drv_init(void) +{ + cdi_scsi_driver_init(&drv); + cdi_handle_bus_device(&drv.drv, &bus_pattern.pattern); + return 0; +} + +static int drv_destroy(void) +{ + cdi_driver_destroy(&drv.drv); + return 0; +} + + +static struct cdi_scsi_driver drv = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_SCSI, + .bus = CDI_USB, + .init = drv_init, + .destroy = drv_destroy, + .init_device = init_usb_device, + }, + + .request = usb_storage_request, +}; + +static struct cdi_usb_bus_device_pattern bus_pattern = { + .pattern = { + .bus_type = CDI_USB, + }, + + .vendor_id = -1, + .product_id = -1, + + .class_id = 0x08, + .subclass_id = -1, + .protocol_id = -1, +}; + +CDI_DRIVER(DRIVER_NAME, drv) diff --git a/usb-storage/usb-storage.c b/usb-storage/usb-storage.c new file mode 100644 index 0000000..f9ba1b2 --- /dev/null +++ b/usb-storage/usb-storage.c @@ -0,0 +1,213 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "usb-storage.h" + + +#define CBW_IN 0x80 +#define CBW_OUT 0x00 + +#define CBW_SIGNATURE 0x43425355 // 'USBC' => USB command +#define CSW_SIGNATURE 0x53425355 // 'USBS' => USB status + +struct msd_bo_cbw { + uint32_t signature; + uint32_t tag; + uint32_t data_length; + uint8_t flags; + uint8_t lun; + uint8_t cb_length; + uint8_t cb[16]; +} __attribute__((packed)); + +struct msd_bo_csw { + uint32_t signature; + uint32_t tag; + uint32_t data_residue; + uint8_t status; +} __attribute__((packed)); + + +typedef enum usb_mass_storage_type { + USBMS_BULK_ONLY, +} usb_mass_storage_type_t; + + +typedef struct usb_mass_storage_device { + struct cdi_scsi_device dev; + struct cdi_usb_device *usb; + + usb_mass_storage_type_t type; + int luns; + + // FIXME: put into own class + int bulk_in, bulk_out; +} usb_mass_storage_device_t; + + +static int bulk_only_reset(usb_mass_storage_device_t *dev) +{ + cdi_usb_transmission_result_t res; + + res = cdi_usb_control_transfer(dev->usb, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_CLASS | CDI_USB_CREQ_INTERFACE + | CDI_USB_CREQ_OUT, + .b_request = 0xff, + .w_index = dev->usb->interface + }, NULL); + if (res != CDI_USB_OK) { + return -EIO; + } + + if (dev->luns > 0) { + return 0; + } + + uint8_t lun_count; + res = cdi_usb_control_transfer(dev->usb, 0, &(struct cdi_usb_setup_packet) { + .bm_request_type = CDI_USB_CREQ_CLASS | CDI_USB_CREQ_INTERFACE + | CDI_USB_CREQ_IN, + .b_request = 0xfe, + .w_index = dev->usb->interface, + .w_length = 1 + }, &lun_count); + if (res == CDI_USB_OK) { + dev->luns = lun_count + 1; + } else { + dev->luns = 1; + } + + return 0; +} + + +struct cdi_device *init_usb_device(struct cdi_bus_data *bus_data) +{ + static int dev_counter = 0; + usb_mass_storage_device_t *dev = calloc(1, sizeof(*dev)); + dev->usb = CDI_UPCAST(bus_data, bus_data, struct cdi_usb_device); + + if ((dev->usb->subclass_id == 0x00 || dev->usb->subclass_id == 0x06) + && dev->usb->protocol_id == 0x50) + { + dev->type = USBMS_BULK_ONLY; + } else { + free(dev); + return NULL; + } + + if (dev->type == USBMS_BULK_ONLY) { + if (bulk_only_reset(dev) < 0) { + free(dev); + return NULL; + } + + struct cdi_usb_endpoint_descriptor ep_desc; + for (int ep = 1; ep < dev->usb->endpoint_count; ep++) { + cdi_usb_get_endpoint_descriptor(dev->usb, ep, &ep_desc); + + if ((ep_desc.bm_attributes & 0x3) == 0x2) { + // Bulk + bool is_in = ep_desc.b_endpoint_address & 0x80; + if (!dev->bulk_in && is_in) { + dev->bulk_in = ep; + } else if (dev->bulk_in && !is_in) { + dev->bulk_out = ep; + } + } + } + + if (!dev->bulk_in || !dev->bulk_out) { + free(dev); + return NULL; + } + } + + dev->dev.dev.name = malloc(5); + sprintf((char *)dev->dev.dev.name, "msd%c", 'a' + dev_counter++); + + dev->dev.type = CDI_STORAGE; + dev->dev.lun_count = dev->luns; + + return &dev->dev.dev; +} + + +int usb_storage_request(struct cdi_scsi_device *device, + struct cdi_scsi_packet *packet) +{ + cdi_usb_transmission_result_t res; + usb_mass_storage_device_t *dev = CDI_UPCAST(device, dev, + usb_mass_storage_device_t); + + struct msd_bo_cbw cbw = { + .signature = CBW_SIGNATURE, + .tag = 42, + .data_length = packet->bufsize, + .flags = packet->direction == CDI_SCSI_READ ? CBW_IN : CBW_OUT, + .lun = packet->lun, + .cb_length = packet->cmdsize + }; + memcpy(cbw.cb, packet->command, sizeof(cbw.cb)); + + res = cdi_usb_bulk_transfer(dev->usb, dev->bulk_out, &cbw, sizeof(cbw)); + if (res != CDI_USB_OK) { + bulk_only_reset(dev); + return -1; + } + + if (packet->direction == CDI_SCSI_READ) { + res = cdi_usb_bulk_transfer(dev->usb, dev->bulk_in, + packet->buffer, packet->bufsize); + } else if (packet->direction == CDI_SCSI_WRITE) { + res = cdi_usb_bulk_transfer(dev->usb, dev->bulk_out, + packet->buffer, packet->bufsize); + } + + if (res != CDI_USB_OK) { + bulk_only_reset(dev); + return -1; + } + + struct msd_bo_csw csw; + res = cdi_usb_bulk_transfer(dev->usb, dev->bulk_in, &csw, sizeof(csw)); + + if (res != CDI_USB_OK || csw.signature != CSW_SIGNATURE || csw.tag != 42 || + csw.data_residue || csw.status) + { + bulk_only_reset(dev); + return -1; + } + + return 0; +} diff --git a/usb-storage/usb-storage.h b/usb-storage/usb-storage.h new file mode 100644 index 0000000..13d717f --- /dev/null +++ b/usb-storage/usb-storage.h @@ -0,0 +1,34 @@ +/****************************************************************************** + * Copyright (c) 2015 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef USB_STORAGE_H +#define USB_STORAGE_H + +#include +#include + + +struct cdi_device *init_usb_device(struct cdi_bus_data *bus_data); +int usb_storage_request(struct cdi_scsi_device *device, + struct cdi_scsi_packet *packet); + +#endif -- 2.3.5 From kevin at tyndur.org Thu Apr 16 21:28:41 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 21:28:41 +0200 Subject: [cdi-devel] [PATCH v2] ahci: Initialisation/Reset improvements In-Reply-To: <1428442323-22448-1-git-send-email-kevin@tyndur.org> References: <1428442323-22448-1-git-send-email-kevin@tyndur.org> Message-ID: <20150416192841.GA3922@silvanus> On Tue, Apr 07, 2015 at 11:32:03PM +0200, Kevin Wolf wrote: > Surprisingly, real hardware works much better when the disk gets power > and is spun up. Also disable any automatic transition to power > management modes just to be on the safe side. > > Before resetting a port, the processing of the command list must be > stopped. > > Signed-off-by: Kevin Wolf > --- > v2: > - Removed the long delays that v1 had while trying to wait for a device on an > empty port Without objection, applied to master. Kevin From kevin at tyndur.org Thu Apr 16 21:45:52 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 21:45:52 +0200 Subject: [cdi-devel] [PATCH v2 1/9] Makefile: Heed Makefile.conf In-Reply-To: <1429136038-29537-2-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-2-git-send-email-max@tyndur.org> Message-ID: <20150416194552.GB3922@silvanus> On Thu, Apr 16, 2015 at 12:13:50AM +0200, Max Reitz wrote: > + Apparently it is allowed to specify additional CFLAGS (as "CC_FLAGS") > in a file named Makefile.conf (formatted as a shell script) in a > driver's root directory; or at least ext2 just does this. > This patch makes the CDI Makefile take the additional flags from that > file into account, if present. > Thanks to this, the ext2 driver can be compiled. > > Signed-off-by: Max Reitz It builds fine for me, but I think now I know why... lrwxrwxrwx 1 kevin users 16 21. Aug 2009 ext2/include -> libext2/include/ > diff --git a/Makefile b/Makefile > index 5360efb..bc04e58 100644 > --- a/Makefile > +++ b/Makefile > @@ -9,7 +9,9 @@ run_command = $(if $(V),$(2), at echo $(1);$(2)) > all: $(OBJS) > > %.o: %.c > - $(call run_command,"CC $^",$(CC) $(CFLAGS) $(addprefix -I,$(addprefix $(dir $^),. include)) -c -o $@ $^) > + $(call run_command,"CC $^",$(CC) $(CFLAGS) \ > + $(if $(shell if [ -f $(dir $^)/Makefile.conf ]; then echo .; fi),$(shell cd $(dir $^); echo -e "$$(< Makefile.conf)\necho \$$CC_FLAGS" | sh -s),) \ Not exactly sure why you need a make $(if) here when there is already a shell if, but it looks as if it could work. Someone would have to say something about portability, but I'll stay quiet for now. ;-) > + $(addprefix -I,$(addprefix $(dir $^),. include)) -c -o $@ $^) > > clean: > -rm $(OBJS) 2> /dev/null Acked-by: Kevin Wolf From kevin at tyndur.org Thu Apr 16 22:18:58 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 22:18:58 +0200 Subject: [cdi-devel] [PATCH v2 2/9] cdi/misc: Add CDI_UPCAST and endianness functions In-Reply-To: <1429136038-29537-3-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-3-git-send-email-max@tyndur.org> Message-ID: <20150416201856.GC3922@silvanus> On Thu, Apr 16, 2015 at 12:13:51AM +0200, Max Reitz wrote: > + Type inheritance is a common pattern seen in CDI. The new CDI_UPCAST() > macro makes conversion to superclasses safer and allows for multiple > inheritance if the need arises. > + Add some endianness conversion functions. Even though support for big > endian platforms is nowhere in sight, endianness conversion is still a > common operation which should not be implemented in the driver itself > but provided by the CDI implementation. > > Signed-off-by: Max Reitz > --- > include/cdi/misc.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 78 insertions(+) > > diff --git a/include/cdi/misc.h b/include/cdi/misc.h > index 07967ec..3193505 100644 > --- a/include/cdi/misc.h > +++ b/include/cdi/misc.h > @@ -85,6 +85,84 @@ int cdi_ioports_free(uint16_t start, uint16_t count); > */ > void cdi_sleep_ms(uint32_t ms); > > +/** > + * Casts @object (must be a pointer) to a pointer to an object of type @to which > + * must be a baseclass of @object's type. @field is the entry in @to which > + * contains @object. > + * > + * Example: > + * struct cdi_driver *driver; > + * struct cdi_storage_driver *storage_driver = > + * CDI_UPCAST(driver, drv, struct cdi_storage_driver); > + */ > +#define CDI_UPCAST(object, field, to) \ > + (to *)((uintptr_t)object - (uintptr_t)&((to *)0)->field) With calculations on char* instead of uintptr_t, I think the result would actually be defined. container_of() used in Linux (and qemu) has a different argument order (object, to, field), perhaps worth following suit? It also checks type compatibility between object and to->field, which could perhaps be portably achieved with comparing both pointers and the throwing away the result using the comma operator. > + > +/** > + * Functions for endianness conversion. Be aware that most if not all CDI > + * drivers only support little endian machines as a host. > + */ > + > +static inline uint16_t cdi_be16_to_cpu(uint16_t x) > +{ > + return (x >> 8) | (x << 8); > +} > + > +static inline uint32_t cdi_be32_to_cpu(uint32_t x) > +{ > + return ((uint32_t)cdi_be16_to_cpu(x) << 16) | (uint32_t)cdi_be16_to_cpu(x >> 16); > +} > + > +static inline uint64_t cdi_be64_to_cpu(uint64_t x) > +{ > + return ((uint64_t)cdi_be32_to_cpu(x) << 32) | (uint64_t)cdi_be32_to_cpu(x >> 32); > +} > + > +static inline uint16_t cdi_cpu_to_be16(uint16_t x) > +{ > + return cdi_be16_to_cpu(x); > +} > + > +static inline uint32_t cdi_cpu_to_be32(uint32_t x) > +{ > + return cdi_be32_to_cpu(x); > +} > + > +static inline uint64_t cdi_cpu_to_be64(uint64_t x) > +{ > + return cdi_be64_to_cpu(x); > +} > + > +static inline uint16_t cdi_le16_to_cpu(uint16_t x) > +{ > + return x; > +} > + > +static inline uint32_t cdi_le32_to_cpu(uint32_t x) > +{ > + return x; > +} > + > +static inline uint64_t cdi_le64_to_cpu(uint64_t x) > +{ > + return x; > +} > + > +static inline uint16_t cdi_cpu_to_le16(uint16_t x) > +{ > + return cdi_le16_to_cpu(x); > +} > + > +static inline uint32_t cdi_cpu_to_le32(uint32_t x) > +{ > + return cdi_le32_to_cpu(x); > +} > + > +static inline uint64_t cdi_cpu_to_le64(uint64_t x) > +{ > + return cdi_le64_to_cpu(x); > +} The endianess functions look good (enough for now). Kevin From kevin at tyndur.org Thu Apr 16 22:27:24 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 22:27:24 +0200 Subject: [cdi-devel] =?iso-8859-1?q?=5BPATCH_v2_3/9=5D_cdi/scsi=2C_cdi=2Eh?= =?iso-8859-1?q?=3A_Remove_t=FDndur-specific_fields?= In-Reply-To: <1429136038-29537-4-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-4-git-send-email-max@tyndur.org> Message-ID: <20150416202724.GD3922@silvanus> On Thu, Apr 16, 2015 at 12:13:52AM +0200, Max Reitz wrote: > - Remove t?ndur-specific fields from non-OS-dependant structures > > Signed-off-by: Max Reitz Acked-by: Kevin Wolf From kevin at tyndur.org Thu Apr 16 22:46:24 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 22:46:24 +0200 Subject: [cdi-devel] [PATCH v2 4/9] cdi/scsi: Add support for multiple LUNs In-Reply-To: <1429136038-29537-5-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-5-git-send-email-max@tyndur.org> Message-ID: <20150416204624.GE3922@silvanus> On Thu, Apr 16, 2015 at 12:13:53AM +0200, Max Reitz wrote: > + One SCSI device may provide multiple LUNs. Add a field for the LUN > count to cdi_scsi_device. > + Furthermore, add a field to select the LUN to cdi_scsi_packet. > > Signed-off-by: Max Reitz > --- > include/cdi/scsi.h | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h > index cef96ed..f36f9aa 100644 > --- a/include/cdi/scsi.h > +++ b/include/cdi/scsi.h > @@ -24,6 +24,9 @@ > > /// SCSI-Paket > struct cdi_scsi_packet { > + /// LUN to address > + int lun; > + > /// Buffer zum Senden oder Empfangen von Daten > void *buffer; > > @@ -51,6 +54,9 @@ struct cdi_scsi_device { > > /// Geraetetyp, der ueber SCSI angesteuert wird > cdi_device_type_t type; > + > + /// Number of LUNs (if 0, there is one) > + int lun_count; Why special-case 0 instead of putting 1 there for a single LUN? Or do you mean number of LUNs - 1 even for more than one LUN? Kevin From max at tyndur.org Thu Apr 16 22:50:27 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 22:50:27 +0200 Subject: [cdi-devel] [PATCH v2 2/9] cdi/misc: Add CDI_UPCAST and endianness functions In-Reply-To: <20150416201856.GC3922@silvanus> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-3-git-send-email-max@tyndur.org> <20150416201856.GC3922@silvanus> Message-ID: <55302093.90800@tyndur.org> On 16.04.2015 22:18, Kevin Wolf wrote: > On Thu, Apr 16, 2015 at 12:13:51AM +0200, Max Reitz wrote: >> + Type inheritance is a common pattern seen in CDI. The new CDI_UPCAST() >> macro makes conversion to superclasses safer and allows for multiple >> inheritance if the need arises. >> + Add some endianness conversion functions. Even though support for big >> endian platforms is nowhere in sight, endianness conversion is still a >> common operation which should not be implemented in the driver itself >> but provided by the CDI implementation. >> >> Signed-off-by: Max Reitz >> --- >> include/cdi/misc.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 78 insertions(+) >> >> diff --git a/include/cdi/misc.h b/include/cdi/misc.h >> index 07967ec..3193505 100644 >> --- a/include/cdi/misc.h >> +++ b/include/cdi/misc.h >> @@ -85,6 +85,84 @@ int cdi_ioports_free(uint16_t start, uint16_t count); >> */ >> void cdi_sleep_ms(uint32_t ms); >> >> +/** >> + * Casts @object (must be a pointer) to a pointer to an object of type @to which >> + * must be a baseclass of @object's type. @field is the entry in @to which >> + * contains @object. >> + * >> + * Example: >> + * struct cdi_driver *driver; >> + * struct cdi_storage_driver *storage_driver = >> + * CDI_UPCAST(driver, drv, struct cdi_storage_driver); >> + */ >> +#define CDI_UPCAST(object, field, to) \ >> + (to *)((uintptr_t)object - (uintptr_t)&((to *)0)->field) > With calculations on char* instead of uintptr_t, I think the result > would actually be defined. I don't know the rules for casting to/from uintptr_t by heart, I guess it's the same as casting to a different pointer type, so I think it should be the same. But I don't mind either way. > container_of() used in Linux (and qemu) has a different argument order > (object, to, field), perhaps worth following suit? I won't say "Why not" because I know why not (I have to change it everywhere in this series, in my ?xo?cota and in my t?ndur implementation...), but will do. > It also checks type > compatibility between object and to->field, which could perhaps be > portably achieved with comparing both pointers and the throwing away the > result using the comma operator. Seems like a nice thing to have. I'll look into it. Max >> + >> +/** >> + * Functions for endianness conversion. Be aware that most if not all CDI >> + * drivers only support little endian machines as a host. >> + */ >> + >> +static inline uint16_t cdi_be16_to_cpu(uint16_t x) >> +{ >> + return (x >> 8) | (x << 8); >> +} >> + >> +static inline uint32_t cdi_be32_to_cpu(uint32_t x) >> +{ >> + return ((uint32_t)cdi_be16_to_cpu(x) << 16) | (uint32_t)cdi_be16_to_cpu(x >> 16); >> +} >> + >> +static inline uint64_t cdi_be64_to_cpu(uint64_t x) >> +{ >> + return ((uint64_t)cdi_be32_to_cpu(x) << 32) | (uint64_t)cdi_be32_to_cpu(x >> 32); >> +} >> + >> +static inline uint16_t cdi_cpu_to_be16(uint16_t x) >> +{ >> + return cdi_be16_to_cpu(x); >> +} >> + >> +static inline uint32_t cdi_cpu_to_be32(uint32_t x) >> +{ >> + return cdi_be32_to_cpu(x); >> +} >> + >> +static inline uint64_t cdi_cpu_to_be64(uint64_t x) >> +{ >> + return cdi_be64_to_cpu(x); >> +} >> + >> +static inline uint16_t cdi_le16_to_cpu(uint16_t x) >> +{ >> + return x; >> +} >> + >> +static inline uint32_t cdi_le32_to_cpu(uint32_t x) >> +{ >> + return x; >> +} >> + >> +static inline uint64_t cdi_le64_to_cpu(uint64_t x) >> +{ >> + return x; >> +} >> + >> +static inline uint16_t cdi_cpu_to_le16(uint16_t x) >> +{ >> + return cdi_le16_to_cpu(x); >> +} >> + >> +static inline uint32_t cdi_cpu_to_le32(uint32_t x) >> +{ >> + return cdi_le32_to_cpu(x); >> +} >> + >> +static inline uint64_t cdi_cpu_to_le64(uint64_t x) >> +{ >> + return cdi_le64_to_cpu(x); >> +} > The endianess functions look good (enough for now). > > Kevin > _______________________________________________ > cdi-devel mailing list > cdi-devel at tyndur.org > http://list.tyndur.org/cgi-bin/mailman/listinfo/cdi-devel From max at tyndur.org Thu Apr 16 22:51:23 2015 From: max at tyndur.org (Max Reitz) Date: Thu, 16 Apr 2015 22:51:23 +0200 Subject: [cdi-devel] [PATCH v2 4/9] cdi/scsi: Add support for multiple LUNs In-Reply-To: <20150416204624.GE3922@silvanus> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-5-git-send-email-max@tyndur.org> <20150416204624.GE3922@silvanus> Message-ID: <553020CB.2060106@tyndur.org> On 16.04.2015 22:46, Kevin Wolf wrote: > On Thu, Apr 16, 2015 at 12:13:53AM +0200, Max Reitz wrote: >> + One SCSI device may provide multiple LUNs. Add a field for the LUN >> count to cdi_scsi_device. >> + Furthermore, add a field to select the LUN to cdi_scsi_packet. >> >> Signed-off-by: Max Reitz >> --- >> include/cdi/scsi.h | 6 ++++++ >> 1 file changed, 6 insertions(+) >> >> diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h >> index cef96ed..f36f9aa 100644 >> --- a/include/cdi/scsi.h >> +++ b/include/cdi/scsi.h >> @@ -24,6 +24,9 @@ >> >> /// SCSI-Paket >> struct cdi_scsi_packet { >> + /// LUN to address >> + int lun; >> + >> /// Buffer zum Senden oder Empfangen von Daten >> void *buffer; >> >> @@ -51,6 +54,9 @@ struct cdi_scsi_device { >> >> /// Geraetetyp, der ueber SCSI angesteuert wird >> cdi_device_type_t type; >> + >> + /// Number of LUNs (if 0, there is one) >> + int lun_count; > Why special-case 0 instead of putting 1 there for a single LUN? Or do > you mean number of LUNs - 1 even for more than one LUN? For legacy reasons. A legacy SCSI driver will not set this field, thus it will remain 0 (which we have to interpret somehow). Max From kevin at tyndur.org Thu Apr 16 23:14:38 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 23:14:38 +0200 Subject: [cdi-devel] [PATCH v2 4/9] cdi/scsi: Add support for multiple LUNs In-Reply-To: <553020CB.2060106@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-5-git-send-email-max@tyndur.org> <20150416204624.GE3922@silvanus> <553020CB.2060106@tyndur.org> Message-ID: <20150416211438.GF3922@silvanus> On Thu, Apr 16, 2015 at 10:51:23PM +0200, Max Reitz wrote: > On 16.04.2015 22:46, Kevin Wolf wrote: > >On Thu, Apr 16, 2015 at 12:13:53AM +0200, Max Reitz wrote: > >>+ One SCSI device may provide multiple LUNs. Add a field for the LUN > >> count to cdi_scsi_device. > >>+ Furthermore, add a field to select the LUN to cdi_scsi_packet. > >> > >>Signed-off-by: Max Reitz > >>--- > >> include/cdi/scsi.h | 6 ++++++ > >> 1 file changed, 6 insertions(+) > >> > >>diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h > >>index cef96ed..f36f9aa 100644 > >>--- a/include/cdi/scsi.h > >>+++ b/include/cdi/scsi.h > >>@@ -24,6 +24,9 @@ > >> /// SCSI-Paket > >> struct cdi_scsi_packet { > >>+ /// LUN to address > >>+ int lun; > >>+ > >> /// Buffer zum Senden oder Empfangen von Daten > >> void *buffer; > >>@@ -51,6 +54,9 @@ struct cdi_scsi_device { > >> /// Geraetetyp, der ueber SCSI angesteuert wird > >> cdi_device_type_t type; > >>+ > >>+ /// Number of LUNs (if 0, there is one) > >>+ int lun_count; > >Why special-case 0 instead of putting 1 there for a single LUN? Or do > >you mean number of LUNs - 1 even for more than one LUN? > > For legacy reasons. A legacy SCSI driver will not set this field, > thus it will remain 0 (which we have to interpret somehow). You don't want to go there. SCSI drivers can just be updated, that's much better than bothering with legacy code. I didn't reply to the cover letter yet, where you say that you're not confident about the HID interface, but it's really the same thing: The compatibility policy of CDI is that there is no compatibility between versions, neither in binary nor in source. This allows us to take any hack that works, and if it turns out that we messed up, we simply change the interface and adjust the drivers and the OS implementations. We can start caring about compatibility once we're convinced that CDI is mature and widespread so that updating OSes would be more pain than maintaining compatibility code, but I don't think we're even close to that. Kevin From kevin at tyndur.org Thu Apr 16 23:50:26 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Thu, 16 Apr 2015 23:50:26 +0200 Subject: [cdi-devel] [PATCH v2 5/9] cdi.h: Add cdi_handle_bus_device() In-Reply-To: <1429136038-29537-6-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-6-git-send-email-max@tyndur.org> Message-ID: <20150416215026.GG3922@silvanus> On Thu, Apr 16, 2015 at 12:13:54AM +0200, Max Reitz wrote: > + Currently, all devices provided on a bus have to be passed to the > init_device() function of all drivers handling any device type on that > bus. cdi_handle_bus_device() can be used by the driver to specify a > pattern (struct cdi_bus_device_pattern) which allows the CDI > implementation to speed up the selection of the correct driver for a > new device. > > Signed-off-by: Max Reitz As you still haven't convinced me that you only speak Saxionian and no German, bilingual documentation, please. ;-) > diff --git a/include/cdi.h b/include/cdi.h > index 998e080..9c1c1b1 100644 > --- a/include/cdi.h > +++ b/include/cdi.h > @@ -141,6 +141,17 @@ struct cdi_bus_data { > }; > > /** > + * This structure, which is extended by any bus type supporting it, contains > + * information about which devices can be handled by a certain driver. > + * Typically, it will contain some of the fields provided by the appropriate > + * cdi_bus_data structure (the ones which are used to identify a device like > + * device/vendor ID) where negative values signify a "don't care". > + */ > +struct cdi_bus_device_pattern { > + cdi_device_type_t bus_type; > +}; > + > +/** > * \german > * Beschreibt ein Ger?t > * \endgerman > @@ -406,6 +417,19 @@ int cdi_provide_device(struct cdi_bus_data* device); > int cdi_provide_device_internal_drv(struct cdi_bus_data* device, > struct cdi_driver* driver); > > +/** > + * Tells the CDI implementation that the driver @drv is able to handle devices > + * with the pattern @pattern (appearing on the bus @pattern->bus_type). This > + * function can be called multiple times. Any device appearing on the bus not > + * fitting the pattern will not be passed to the @drv->init_device() function. > + * That function may however return NULL even after the driver has used this > + * function, that is, it is valid to call this function with a @pattern that > + * does not only fit devices handled by @drv but others as well (which can then > + * be rejected by letting @drv->init_device() return NULL). > + */ > +void cdi_handle_bus_device(struct cdi_driver* drv, > + struct cdi_bus_device_pattern* pattern); Is this supposed to be called in drv->init_driver()? If so, could be worth mentioning. Ideally it would just be part of the static driver declaration instead of a separate function, but I imagine this is hard to achieve with both the driver and device pattern being subclassed. Kevin From max at tyndur.org Fri Apr 17 00:12:05 2015 From: max at tyndur.org (Max Reitz) Date: Fri, 17 Apr 2015 00:12:05 +0200 Subject: [cdi-devel] [PATCH v2 4/9] cdi/scsi: Add support for multiple LUNs In-Reply-To: <20150416211438.GF3922@silvanus> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-5-git-send-email-max@tyndur.org> <20150416204624.GE3922@silvanus> <553020CB.2060106@tyndur.org> <20150416211438.GF3922@silvanus> Message-ID: <553033B5.5030901@tyndur.org> On 16.04.2015 23:14, Kevin Wolf wrote: > On Thu, Apr 16, 2015 at 10:51:23PM +0200, Max Reitz wrote: >> On 16.04.2015 22:46, Kevin Wolf wrote: >>> On Thu, Apr 16, 2015 at 12:13:53AM +0200, Max Reitz wrote: >>>> + One SCSI device may provide multiple LUNs. Add a field for the LUN >>>> count to cdi_scsi_device. >>>> + Furthermore, add a field to select the LUN to cdi_scsi_packet. >>>> >>>> Signed-off-by: Max Reitz >>>> --- >>>> include/cdi/scsi.h | 6 ++++++ >>>> 1 file changed, 6 insertions(+) >>>> >>>> diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h >>>> index cef96ed..f36f9aa 100644 >>>> --- a/include/cdi/scsi.h >>>> +++ b/include/cdi/scsi.h >>>> @@ -24,6 +24,9 @@ >>>> /// SCSI-Paket >>>> struct cdi_scsi_packet { >>>> + /// LUN to address >>>> + int lun; >>>> + >>>> /// Buffer zum Senden oder Empfangen von Daten >>>> void *buffer; >>>> @@ -51,6 +54,9 @@ struct cdi_scsi_device { >>>> /// Geraetetyp, der ueber SCSI angesteuert wird >>>> cdi_device_type_t type; >>>> + >>>> + /// Number of LUNs (if 0, there is one) >>>> + int lun_count; >>> Why special-case 0 instead of putting 1 there for a single LUN? Or do >>> you mean number of LUNs - 1 even for more than one LUN? >> For legacy reasons. A legacy SCSI driver will not set this field, >> thus it will remain 0 (which we have to interpret somehow). > You don't want to go there. SCSI drivers can just be updated, that's > much better than bothering with legacy code. > > I didn't reply to the cover letter yet, where you say that you're not > confident about the HID interface, but it's really the same thing: The > compatibility policy of CDI is that there is no compatibility between > versions, neither in binary nor in source. > > This allows us to take any hack that works, and if it turns out that we > messed up, we simply change the interface and adjust the drivers and the > OS implementations. > > We can start caring about compatibility once we're convinced that CDI is > mature and widespread so that updating OSes would be more pain than > maintaining compatibility code, but I don't think we're even close to > that. Well, it's your call. Max From max at tyndur.org Fri Apr 17 00:16:18 2015 From: max at tyndur.org (Max Reitz) Date: Fri, 17 Apr 2015 00:16:18 +0200 Subject: [cdi-devel] [PATCH v2 5/9] cdi.h: Add cdi_handle_bus_device() In-Reply-To: <20150416215026.GG3922@silvanus> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-6-git-send-email-max@tyndur.org> <20150416215026.GG3922@silvanus> Message-ID: <553034B2.3080008@tyndur.org> On 16.04.2015 23:50, Kevin Wolf wrote: > On Thu, Apr 16, 2015 at 12:13:54AM +0200, Max Reitz wrote: >> + Currently, all devices provided on a bus have to be passed to the >> init_device() function of all drivers handling any device type on that >> bus. cdi_handle_bus_device() can be used by the driver to specify a >> pattern (struct cdi_bus_device_pattern) which allows the CDI >> implementation to speed up the selection of the correct driver for a >> new device. >> >> Signed-off-by: Max Reitz > As you still haven't convinced me that you only speak Saxionian and no > German, bilingual documentation, please. ;-) Bah. ?hmal Dogumendazschioun zu schraim war mir ganz bers?hnlisch nu or schon fast ? bissel zu viel, da w?sch nu ni, obsch das ohch noch dobblt hinkriesche. ;-) >> diff --git a/include/cdi.h b/include/cdi.h >> index 998e080..9c1c1b1 100644 >> --- a/include/cdi.h >> +++ b/include/cdi.h >> @@ -141,6 +141,17 @@ struct cdi_bus_data { >> }; >> >> /** >> + * This structure, which is extended by any bus type supporting it, contains >> + * information about which devices can be handled by a certain driver. >> + * Typically, it will contain some of the fields provided by the appropriate >> + * cdi_bus_data structure (the ones which are used to identify a device like >> + * device/vendor ID) where negative values signify a "don't care". >> + */ >> +struct cdi_bus_device_pattern { >> + cdi_device_type_t bus_type; >> +}; >> + >> +/** >> * \german >> * Beschreibt ein Ger?t >> * \endgerman >> @@ -406,6 +417,19 @@ int cdi_provide_device(struct cdi_bus_data* device); >> int cdi_provide_device_internal_drv(struct cdi_bus_data* device, >> struct cdi_driver* driver); >> >> +/** >> + * Tells the CDI implementation that the driver @drv is able to handle devices >> + * with the pattern @pattern (appearing on the bus @pattern->bus_type). This >> + * function can be called multiple times. Any device appearing on the bus not >> + * fitting the pattern will not be passed to the @drv->init_device() function. >> + * That function may however return NULL even after the driver has used this >> + * function, that is, it is valid to call this function with a @pattern that >> + * does not only fit devices handled by @drv but others as well (which can then >> + * be rejected by letting @drv->init_device() return NULL). >> + */ >> +void cdi_handle_bus_device(struct cdi_driver* drv, >> + struct cdi_bus_device_pattern* pattern); > Is this supposed to be called in drv->init_driver()? If so, could be > worth mentioning. I didn't intend any constraints for it, but limiting it to init_driver() seems reasonable. > Ideally it would just be part of the static driver declaration instead > of a separate function, but I imagine this is hard to achieve with both > the driver and device pattern being subclassed. Initially, I made it part of the static driver declaration, but then I didn't really want to go into having a variable-length array for multiple bus device patterns as part of the driver structure... Max From kevin at tyndur.org Sat Apr 18 00:35:37 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 18 Apr 2015 00:35:37 +0200 Subject: [cdi-devel] [PATCH v2 6/9] cdi/usb: Add headers In-Reply-To: <1429136038-29537-7-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-7-git-send-email-max@tyndur.org> Message-ID: <20150417223445.GA3745@silvanus> On Thu, Apr 16, 2015 at 12:13:55AM +0200, Max Reitz wrote: > + Add headers to be used by USB host controller drivers, the USB bus > driver, and USB device drivers. > These headers are still lacking functions for periodic transactions > (interrupt/isochronous), hotplugging, suspend/resume, etc. pp.. > > Signed-off-by: Max Reitz Lots of documentation texts here. FWIW, I'm okay with leaving the USB-specific headers in English only. It just felt out of place in the existing bilingual (or even German-only) headers. > +/** > + * This function is provided by the reference USB bus driver, it must only be > + * called by the CDI library. > + * > + * It is called once a host controller driver provides a host controller device > + * (returned by its init_device() function) which is passed to this function. > + */ > +void cdi_usb_provide_hc(struct cdi_device* dev); Hm, I'm afraid I don't fully understand this function yet. If this is part of the USB bus driver, why isn't it in struct cdi_usb_driver? Okay, I looked in the tyndur patches, and it seems this is actually _not_ provided by the USB bus driver, but part of the CDI USB library (and used in the HCD driver process, not the USB one). In other words, since you also say it must only be called by the library, isn't this a purely internal implementation detail that isn't necessary in the public header files? > +/** > + * Contains information about a communication pipe to a USB device endpoint used > + * for USB transactions. > + */ > +struct cdi_usb_hc_ep_info { > + /// Device ID > + int dev; > + > + /// Endpoint ID > + int ep; > + > + /// Endpoint type > + cdi_usb_endpoint_type_t ep_type; > + > + /// Transfer speed > + cdi_usb_speed_t speed; > + > + /// Maximum packet size for this endpoint > + int mps; > + > + /** > + * For low and full speed devices operated behind a high speed hub: @tt_addr > + * is the USB device address of the translating USB hub (the last high speed > + * hub), @tt_port is the port (0-based index) of that hub where the first > + * non-high-speed device of this device's tree is plugged into. > + */ > + int tt_addr, tt_port; > +}; I wonder if all the ints in this struct should be unsigned (and then consistently so throughout all the header files). > + > +/** > + * Describes a single USB transmission. > + */ > +struct cdi_usb_hc_transmission { > + /// Transaction this transmission is part of > + cdi_usb_hc_transaction_t ta; > + > + /// Type of transfer to be performed > + cdi_usb_transfer_token_t token; > + > + /// Buffer to be used (may be NULL if @size is 0) > + void* buffer; > + > + /// Number of bytes to be transferred (may be 0, and may exceed the MPS) > + size_t size; Perhaps this suggests that the right unsigned type for mps is size_t, too. > + /** > + * Toggle bit to be used for the first packet issued in this transmission. > + * If @size exceeds the MPS and thus multiple packets are transmitted, the > + * toggle bit will be toggled automatically. > + */ > + int toggle; bool? (Honest question; "bit" just sounds like it, but I don't know how this actually works) > + /** > + * Result of this transmission. Only valid after wait_transaction() has > + * returned. > + */ > + cdi_usb_transmission_result_t* result; > +}; I won't say that this patch is right, but it looks plausible at least. ;-) Kevin From max at tyndur.org Sat Apr 18 00:47:16 2015 From: max at tyndur.org (Max Reitz) Date: Sat, 18 Apr 2015 00:47:16 +0200 Subject: [cdi-devel] [PATCH v2 6/9] cdi/usb: Add headers In-Reply-To: <20150417223445.GA3745@silvanus> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-7-git-send-email-max@tyndur.org> <20150417223445.GA3745@silvanus> Message-ID: <55318D74.9060607@tyndur.org> On 18.04.2015 00:35, Kevin Wolf wrote: > On Thu, Apr 16, 2015 at 12:13:55AM +0200, Max Reitz wrote: >> + Add headers to be used by USB host controller drivers, the USB bus >> driver, and USB device drivers. >> These headers are still lacking functions for periodic transactions >> (interrupt/isochronous), hotplugging, suspend/resume, etc. pp.. >> >> Signed-off-by: Max Reitz > Lots of documentation texts here. FWIW, I'm okay with leaving the > USB-specific headers in English only. It just felt out of place in the > existing bilingual (or even German-only) headers. > >> +/** >> + * This function is provided by the reference USB bus driver, it must only be >> + * called by the CDI library. >> + * >> + * It is called once a host controller driver provides a host controller device >> + * (returned by its init_device() function) which is passed to this function. >> + */ >> +void cdi_usb_provide_hc(struct cdi_device* dev); > Hm, I'm afraid I don't fully understand this function yet. > > If this is part of the USB bus driver, why isn't it in struct > cdi_usb_driver? > > Okay, I looked in the tyndur patches, and it seems this is actually > _not_ provided by the USB bus driver, but part of the CDI USB library > (and used in the HCD driver process, not the USB one). Oops, right. The comment is still from when I intended to make the bus driver part of the CDI library. > In other words, since you also say it must only be called by the > library, isn't this a purely internal implementation detail that isn't > necessary in the public header files? Yes, it should be removed from the public interface. It appears to be just left over in this header; in the implementation, it's actually called cdi_osdep_provide_hc() already. >> +/** >> + * Contains information about a communication pipe to a USB device endpoint used >> + * for USB transactions. >> + */ >> +struct cdi_usb_hc_ep_info { >> + /// Device ID >> + int dev; >> + >> + /// Endpoint ID >> + int ep; >> + >> + /// Endpoint type >> + cdi_usb_endpoint_type_t ep_type; >> + >> + /// Transfer speed >> + cdi_usb_speed_t speed; >> + >> + /// Maximum packet size for this endpoint >> + int mps; >> + >> + /** >> + * For low and full speed devices operated behind a high speed hub: @tt_addr >> + * is the USB device address of the translating USB hub (the last high speed >> + * hub), @tt_port is the port (0-based index) of that hub where the first >> + * non-high-speed device of this device's tree is plugged into. >> + */ >> + int tt_addr, tt_port; >> +}; > I wonder if all the ints in this struct should be unsigned (and then > consistently so throughout all the header files). Well... If you're going that far, dev should be uint8_t, ep should be uint4_t (well...), and we can probably just make mps a size_t. tt_addr should be uint8_t again, and tt_port... Well, maybe uint8_t, too. >> + >> +/** >> + * Describes a single USB transmission. >> + */ >> +struct cdi_usb_hc_transmission { >> + /// Transaction this transmission is part of >> + cdi_usb_hc_transaction_t ta; >> + >> + /// Type of transfer to be performed >> + cdi_usb_transfer_token_t token; >> + >> + /// Buffer to be used (may be NULL if @size is 0) >> + void* buffer; >> + >> + /// Number of bytes to be transferred (may be 0, and may exceed the MPS) >> + size_t size; > Perhaps this suggests that the right unsigned type for mps is size_t, > too. > >> + /** >> + * Toggle bit to be used for the first packet issued in this transmission. >> + * If @size exceeds the MPS and thus multiple packets are transmitted, the >> + * toggle bit will be toggled automatically. >> + */ >> + int toggle; > bool? (Honest question; "bit" just sounds like it, but I don't know how > this actually works) Good question, I asked myself the same. For normal data transfers, this bit can be only 0 or 1. But for isochronous transfers, I (think I) know it can switch between 0, 1, 2, and even something else. Also, bool wouldn't be the right type even if it can be only 0 or 1. It's a bit that's switched for every transfer (so that lost packets can be detected, a bit like the TCP sequence number; only you don't need a full-fledged sequence number for USB because every (non-isochronous) packet is replied to with an ACK, and you're waiting for that ACK before sending the next packet), so it's not "false"/"true", but really just an integer mod 2. >> + /** >> + * Result of this transmission. Only valid after wait_transaction() has >> + * returned. >> + */ >> + cdi_usb_transmission_result_t* result; >> +}; > I won't say that this patch is right, but it looks plausible at least. ;-) That's good enough, I suppose. :-) Thanks! I'll change the types and remove cdi_usb_provide_hc(). Max From kevin at tyndur.org Sat Apr 18 15:31:58 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 18 Apr 2015 15:31:58 +0200 Subject: [cdi-devel] [PATCH v2 7/9] ehci: Add EHCI driver In-Reply-To: <1429136038-29537-8-git-send-email-max@tyndur.org> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-8-git-send-email-max@tyndur.org> Message-ID: <20150418133155.GA2796@silvanus> On Thu, Apr 16, 2015 at 12:13:56AM +0200, Max Reitz wrote: > + This adds an EHCI USB host controller driver. It is missing support > for periodic schedule (interrupt and isochronous transfers), > hotplugging, suspend/resume, etc. pp.. > > Signed-off-by: Max Reitz > --- > ehci/ehci.c | 661 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > ehci/ehci.h | 170 ++++++++++++++++ > ehci/main.c | 68 +++++++ > 3 files changed, 899 insertions(+) > create mode 100644 ehci/ehci.c > create mode 100644 ehci/ehci.h > create mode 100644 ehci/main.c > > diff --git a/ehci/ehci.c b/ehci/ehci.c > new file mode 100644 > index 0000000..6da316e > --- /dev/null > +++ b/ehci/ehci.c > @@ -0,0 +1,661 @@ > +/****************************************************************************** > + * Copyright (c) 2015 Max Reitz * > + * * > + * Permission is hereby granted, free of charge, to any person obtaining a * > + * copy of this software and associated documentation files (the "Software"), * > + * to deal in the Software without restriction, including without limitation * > + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * > + * and/or sell copies of the Software, and to permit persons to whom the * > + * Software is furnished to do so, subject to the following conditions: * > + * * > + * The above copyright notice and this permission notice shall be included in * > + * all copies or substantial portions of the Software. * > + * * > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * > + * DEALINGS IN THE SOFTWARE. * > + ******************************************************************************/ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "ehci.h" > + > + > +#define EHCI_TIMEOUT 1000 // ms > + > + > +static bool bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc); > +static void irq_handler(struct cdi_device *dev); > + > + > +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data) > +{ > + struct cdi_pci_device *pci_dev = (struct cdi_pci_device *)bus_data; > + static int dev_counter = 0; > + > + if (pci_dev->class_id != 0x0c || > + pci_dev->subclass_id != 0x03 || > + pci_dev->interface_id != 0x20) > + { > + return NULL; > + } Magic numbers? > + > + struct ehci *hc = calloc(1, sizeof(*hc)); > + > + hc->hc.dev.name = malloc(8); > + sprintf((char *)hc->hc.dev.name, "ehci%i", dev_counter++); > + > + hc->retired_qhs = cdi_list_create(); > + > + // Enable bus mastering > + cdi_pci_config_writew(pci_dev, 4, cdi_pci_config_readw(pci_dev, 4) | 0x6); This one as well. I also thought we had specified somewhere that bus mastering must be enabled by the CDI library, but I can't find it. Maybe we haven't. > + > + cdi_pci_alloc_memory(pci_dev); > + > + struct cdi_pci_resource *res; > + for (int i = 0; (res = cdi_list_get(pci_dev->resources, i)); i++) { > + if (res->type == CDI_PCI_MEMORY && !res->index) { > + hc->caps = res->address; > + hc->regs = (struct ehci_registers *)((uintptr_t)res->address + > + hc->caps->caplength); > + } > + } Check that hc->caps is set and fail otherwise. > + > + if (!bios_handoff(pci_dev, hc)) { > + goto fail; > + } > + > + // Stop operation > + hc->regs->usbcmd = (hc->regs->usbcmd & 0xff00ff00u) | 0x00080010u; Right, because these are obvious and nice numbers. :-) (Won't mention it everywhere, but there are _lots_ of magic numbers in this patch that should be replaced by defines.) > + // Wait until HCHALTED becomes true > + int timeout_counter = 0; > + while (!(hc->regs->usbsts & (1 << 12)) && ++timeout_counter < 50) { > + cdi_sleep_ms(1); > + } > + if (timeout_counter >= 50) { > + goto fail; > + } This is pretty common pattern. Worth providing a function/macro for it, possiby even in the CDI headers? > + hc->regs->usbcmd |= (1 << 1); // Reset > + timeout_counter = 0; > + while ((hc->regs->usbcmd & (1 << 1)) && ++timeout_counter < 50) { > + cdi_sleep_ms(1); > + } > + if (timeout_counter >= 50) { > + goto fail; > + } > + > + if (hc->caps->hccparams & 1) { > + // 64-bit addressing capability > + hc->regs->ctrldssegment = 0; > + mem_barrier(); Make it cdi_barrier() and move it to cdi/misc.h (I would avoid calling it a "memory" barrier because that could imply that CPU caches are flushed, which is not the case; it's just a compiler barrier). This is definitely a function that other drivers can use. That said, with regs being volatile, what does the barrier achieve here? Except when it's really obvious, comments are appropriate to explain the purpose of each barrier. > + } > + > + // async advance doorbell > + hc->regs->usbintr = (1 << 5); > + hc->regs->usbsts = hc->regs->usbsts; > + cdi_register_irq(pci_dev->irq, &irq_handler, &hc->hc.dev); > + > + hc->periodic_list_mem = cdi_mem_alloc(4096, 12 > + | CDI_MEM_PHYS_CONTIGUOUS > + | CDI_MEM_DMA_4G > + | CDI_MEM_NOINIT); > + hc->periodic_list = hc->periodic_list_mem->vaddr; > + for (int i = 0; i < 1024; i++) { > + hc->periodic_list[i] = EHCI_T; > + } > + mem_barrier(); > + hc->regs->periodiclistbase = hc->periodic_list_mem->paddr.items[0].start; The same question here because periodic_list is declared as volatile. The difference is that I'm actually not sure whether it should be volatile or whether relying on the barriers isn't better. > + > + // Create anchor > + struct cdi_mem_area *anchor_qh; > + anchor_qh = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 > + | CDI_MEM_PHYS_CONTIGUOUS > + | CDI_MEM_DMA_4G > + | CDI_MEM_NOINIT); > + > + hc->async_start = anchor_qh->vaddr; > + memset(hc->async_start, 0, sizeof(*hc->async_start)); > + > + hc->async_start->cdi_mem_area = anchor_qh; > + hc->async_start->paddr = anchor_qh->paddr.items[0].start > + + offsetof(ehci_fat_qh_t, qh); > + > + hc->async_start->next = hc->async_start; > + hc->async_start->previous = hc->async_start; > + > + hc->async_start->qh.next_qh = hc->async_start->paddr | EHCI_QH; > + hc->async_start->qh.ep_state[0] = 0x0008e000u; > + hc->async_start->qh.ep_state[1] = 0x4000ff00u; > + > + hc->async_start->qh.overlay.next_qtd = EHCI_T; > + hc->async_start->qh.overlay.alt_next_qtd = EHCI_T; > + > + hc->regs->asynclistaddr = hc->async_start->paddr; > + > + mem_barrier(); > + > + // Start operation > + hc->regs->usbcmd = (hc->regs->usbcmd & 0xff00ff00u) | 0x00080031u; > + > + // Wait until HCHalted becomes false and the schedules are running > + timeout_counter = 0; > + while ((hc->regs->usbsts & 0xd000) != 0xc000 && ++timeout_counter < 50) { > + cdi_sleep_ms(1); > + } > + if (timeout_counter >= 50) { > + goto fail; > + } > + > + // Route everything to this HC > + hc->regs->configflag |= 1; > + > + mem_barrier(); > + cdi_sleep_ms(5); > + > + hc->hc.rh.ports = hc->caps->hcsparams & 0xf; > + > + // Power up all ports > + for (int i = 0; i < hc->hc.rh.ports; i++) { > + hc->regs->portsc[i] |= EHCI_PSC_PP; > + } > + > + mem_barrier(); This one looks really random. > + > + return &hc->hc.dev; > + > +fail: > + if (hc->async_start) { > + cdi_mem_free(hc->async_start->cdi_mem_area); > + } > + if (hc->periodic_list_mem) { > + cdi_mem_free(hc->periodic_list_mem); > + } > + free((void *)hc->hc.dev.name); > + free(hc); > + cdi_pci_free_memory(pci_dev); retired_qhs is leaked. It seems that other drivers have the same problem, but it would be nice to unregister the IRQ. (For microkernels, the problem probably isn't serious because the process ends and the kernel should clean up, but for monolithic ones a system crash might not be far away.) > + return NULL; > +} > + > + > +static bool bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc) > +{ > + int eecp = (hc->caps->hccparams >> 8) & 0xff; > + > + // Iterate through the list of capabilities > + while (eecp >= 0x40) { > + uint32_t cap = cdi_pci_config_readl(pci_dev, eecp); > + > + // Look for USBLEGSUP > + if ((cap & 0xff) == 0x01) { > + // Set OS semaphore > + cdi_pci_config_writeb(pci_dev, eecp + 3, 1); > + > + // Wait for BIOS semaphore to get unset > + int timeout_counter = 0; > + while ((cdi_pci_config_readb(pci_dev, eecp + 2) & 1) && > + ++timeout_counter < 1000) > + { > + cdi_sleep_ms(1); > + } > + if (timeout_counter >= 1000) { > + return false; > + } > + } > + > + eecp = (cap >> 8) & 0xff; > + } > + > + return true; > +} > + > + > +static void irq_handler(struct cdi_device *dev) > +{ > + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(dev, dev, struct cdi_usb_hc); > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + > + uint32_t usbsts = hc->regs->usbsts; > + mem_barrier(); > + hc->regs->usbsts = usbsts; > + > + // async advance doorbell > + if (!(usbsts & (1 << 5))) { > + return; > + } > + > + ehci_fat_qh_t *rqh; > + while ((rqh = cdi_list_pop(hc->retired_qhs))) { > + cdi_mem_free(rqh->cdi_mem_area); > + } > +} As always, I don't really know what I'm doing, so let me ask a stupid question: You use a list here, so I assume that multiple QHs can be retired. You don't immediately free them but only after getting a doorbell IRQ, so I assume that they are still in use by the hardware until then. Does the hardware complete the use of all retired QHs at once, or what does ensure that we don't free all retired QHs when the hardware is only done with the first one? (Disclaimer: I don't really know what a QH is.) > + > +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index) > +{ > + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + > + hc->regs->portsc[index] &= ~EHCI_PSC_PED; > + > + mem_barrier(); > +} > + > + > +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index) > +{ > + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + uint32_t portsc = hc->regs->portsc[index]; > + > + if ((portsc & EHCI_PSC_LS_MASK) == EHCI_PSC_LS_K) { > + // Low-speed device, release ownership > + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; > + mem_barrier(); > + return; > + } > + > + // Assert reset signal > + hc->regs->portsc[index] = ((portsc & ~(EHCI_PSC_PO | EHCI_PSC_SUS | > + EHCI_PSC_FPR | EHCI_PSC_PED)) > + | EHCI_PSC_RES); > + mem_barrier(); > + cdi_sleep_ms(50); > + > + // De-assert reset signal > + int timeout_counter = 0; > + hc->regs->portsc[index] = portsc & ~EHCI_PSC_RES; > + do { > + portsc = hc->regs->portsc[index]; > + cdi_sleep_ms(1); > + } while ((portsc & EHCI_PSC_RES) && ++timeout_counter < 50); > + > + if (portsc & EHCI_PSC_RES) { > + // Disable port, route on to companion controller > + // (maybe it can do at least something with it) > + hc->regs->portsc[index] = (portsc & ~EHCI_PSC_PED) | EHCI_PSC_PO; > + } else if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == EHCI_PSC_CCS) { > + // Device connected but port disabled: > + // Full-speed device, release ownership > + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; > + } > + > + mem_barrier(); > +} > + > + > +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index) > +{ > + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + uint32_t portsc = hc->regs->portsc[index]; > + cdi_usb_port_status_t status = 0; > + > + if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == > + (EHCI_PSC_PED | EHCI_PSC_CCS)) > + { > + // Device connected and port enabled > + status |= CDI_USB_PORT_CONNECTED | CDI_USB_HIGH_SPEED; > + } > + > + return status; > +} > + > + > +static void enter_async_qh(struct ehci *hc, ehci_fat_qh_t *qh) > +{ > + // Enter the new QH as last > + > + qh->next = hc->async_start; > + qh->previous = hc->async_start->previous; > + > + qh->previous->next = qh; > + hc->async_start->previous = qh; Shouldn't the barrier actually be here so that the hardware is guaranteed to see the new links instead of some intermediate state when qh->qh.next_qh is updated? > + qh->qh.next_qh = hc->async_start->paddr | EHCI_QH; > + mem_barrier(); > + > + qh->previous->qh.next_qh = qh->paddr | EHCI_QH; > + mem_barrier(); > +} > + > + > +static void retire_qh(struct ehci *hc, ehci_fat_qh_t *qh) > +{ > + qh->previous->qh.next_qh = qh->qh.next_qh; > + mem_barrier(); > + > + qh->previous->next = qh->next; > + qh->next->previous = qh->previous; > + > + cdi_list_push(hc->retired_qhs, qh); > + // Request async advance doorbell > + hc->regs->usbcmd |= (1 << 6); > +} > + > + > +static void retire_qtd(ehci_fat_qtd_t *qtd) > +{ > + ehci_fat_qh_t *qh = qtd->qh; > + > + if (qtd->qtd.status & 0x7c) { > + if (qtd->qtd.status & (1 << 4)) { > + *qtd->result = CDI_USB_BABBLE; > + } else if (qtd->qtd.status & (1 << 3)) { > + *qtd->result = CDI_USB_BAD_RESPONSE; > + } else if (qtd->qtd.status & ((1 << 5) | (1 << 2))) { > + *qtd->result = CDI_USB_HC_ERROR; > + } else if (qtd->qtd.status & (1 << 6)) { > + *qtd->result = CDI_USB_STALL; > + } else { > + *qtd->result = CDI_USB_HC_ERROR; > + } > + } > + > + if (qtd->write_back) { > + size_t rem = qtd->write_back_size; > + for (int i = 0; i < 5 && rem; i++) { > + size_t sz = rem > 4096 ? 4096 : rem; > + memcpy((void *)((uintptr_t)qtd->write_back + i * 4096), > + qtd->buffers[i]->vaddr, sz); > + rem -= sz; > + } > + assert(!rem); > + } > + > + qh->first_qtd = qtd->next; > + if (qh->last_qtd == qtd) { > + qh->last_qtd = NULL; > + } > + > + for (int i = 0; i < 5; i++) { > + if (qtd->buffers[i]) { > + cdi_mem_free(qtd->buffers[i]); > + } > + } > + > + cdi_mem_free(qtd->cdi_mem_area); > +} > + > + > +static void enter_qtd(ehci_fat_qh_t *qh, ehci_fat_qtd_t *qtd) > +{ > + bool first; > + > + qtd->qh = qh; > + qtd->qtd.next_qtd = EHCI_T; > + qtd->qtd.alt_next_qtd = EHCI_T; > + mem_barrier(); > + > + while (qh->first_qtd) { > + /* Break if an active qTD is encountered */ > + if (qh->first_qtd->qtd.status & (1 << 7)) { > + break; > + } > + > + retire_qtd(qh->first_qtd); > + } > + > + first = !qh->first_qtd; > + > + // Note: Race condition (a qTD might retire here and we are thus appending > + // the one to be queued to an inactive qTD); it's fine as long as we are > + // constantly rescanning the queue (here and in ehci_wait_transaction()) > + > + if (first) { > + qh->first_qtd = qtd; > + } else { > + qh->last_qtd->next = qtd; > + } > + qh->last_qtd = qtd; > + > + if (!first) { > + qh->last_qtd->qtd.next_qtd = qtd->paddr; > + mem_barrier(); > + } > + > + // If the QH is not active, it's done with its queue of qTDs and we should > + // continue from this qTD > + if (!(qh->qh.overlay.status & (1 << 7))) { > + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; > + mem_barrier(); > + } > +} > + > + > +cdi_usb_hc_transaction_t ehci_create_transaction(struct cdi_usb_hc *cdi_hc, > + const struct cdi_usb_hc_ep_info *target) > +{ > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + struct cdi_mem_area *qh_mem; > + > + qh_mem = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 > + | CDI_MEM_PHYS_CONTIGUOUS > + | CDI_MEM_DMA_4G > + | CDI_MEM_NOINIT); > + ehci_fat_qh_t *qh = qh_mem->vaddr; > + > + memset(qh, 0, sizeof(*qh)); > + > + int non_high_speed_control = target->speed != CDI_USB_HIGH_SPEED > + && target->ep_type == CDI_USB_CONTROL; > + > + qh->qh.ep_state[0] = target->dev | (target->ep << 8) | (1 << 14) > + | (target->mps << 16) | (non_high_speed_control << 27); > + switch (target->speed) { > + case CDI_USB_LOW_SPEED: qh->qh.ep_state[0] |= (1 << 12); break; > + case CDI_USB_FULL_SPEED: qh->qh.ep_state[0] |= (0 << 12); break; > + case CDI_USB_HIGH_SPEED: qh->qh.ep_state[0] |= (2 << 12); break; > + case CDI_USB_SUPERSPEED: > + default: > + abort(); > + } > + > + qh->qh.ep_state[1] = (0xff << 8) | (1 << 30) | (target->tt_addr << 16) > + | ((target->tt_port + 1) << 23); > + > + qh->qh.overlay.next_qtd = EHCI_T; > + qh->qh.overlay.alt_next_qtd = EHCI_T; > + > + qh->cdi_mem_area = qh_mem; > + qh->paddr = qh_mem->paddr.items[0].start + offsetof(ehci_fat_qh_t, qh); > + > + mem_barrier(); > + > + enter_async_qh(hc, qh); > + > + return qh; > +} > + > + > +static void create_qtd(ehci_fat_qh_t *qh, cdi_usb_transfer_token_t token, > + int toggle, void *buffer, size_t size, > + cdi_usb_transmission_result_t *result) > +{ > + struct cdi_mem_area *qtd_mem; > + > + qtd_mem = cdi_mem_alloc(sizeof(ehci_fat_qtd_t), 6 > + | CDI_MEM_PHYS_CONTIGUOUS > + | CDI_MEM_DMA_4G > + | CDI_MEM_NOINIT); > + ehci_fat_qtd_t *qtd = qtd_mem->vaddr; > + > + memset(qtd, 0, sizeof(*qtd)); > + > + qtd->qtd.status = (1 << 7) | (3 << 10) | (size << 16) > + | ((uint32_t)toggle << 31); > + > + switch (token) { > + case CDI_USB_IN: qtd->qtd.status |= (1 << 8); break; > + case CDI_USB_OUT: qtd->qtd.status |= (0 << 8); break; > + case CDI_USB_SETUP: qtd->qtd.status |= (2 << 8); break; > + default: > + abort(); > + } > + > + if (token == CDI_USB_IN) { > + qtd->write_back = buffer; > + qtd->write_back_size = size; > + } > + > + for (int i = 0; i < 5 && size; i++) { > + size_t sz = size > 4096 ? 4096 : size; > + > + qtd->buffers[i] = cdi_mem_alloc(4096, 12 > + | CDI_MEM_PHYS_CONTIGUOUS > + | CDI_MEM_DMA_4G > + | CDI_MEM_NOINIT); > + qtd->qtd.buffers[i] = qtd->buffers[i]->paddr.items[0].start; > + > + if (token == CDI_USB_OUT || token == CDI_USB_SETUP) { > + memcpy(qtd->buffers[i]->vaddr, > + (void *)((uintptr_t)buffer + i * 4096), sz); > + } > + > + size -= sz; > + } > + > + qtd->cdi_mem_area = qtd_mem; > + qtd->paddr = qtd_mem->paddr.items[0].start + offsetof(ehci_fat_qtd_t, qtd); > + > + qtd->result = result; > + > + mem_barrier(); > + > + enter_qtd(qh, qtd); > +} > + > + > +void ehci_enqueue(struct cdi_usb_hc *cdi_hc, > + const struct cdi_usb_hc_transmission *trans) > +{ > + (void)cdi_hc; > + > + ehci_fat_qh_t *qh = trans->ta; > + bool first = true; > + size_t sz = trans->size; > + void *buf = trans->buffer; Make it uint8_t* to save some casts? > + int toggle = trans->toggle; > + size_t mps = (qh->qh.ep_state[0] >> 16) & 0x7ff; > + > + *trans->result = CDI_USB_OK; > + > + while (sz || first) { > + first = false; > + > + size_t iter_sz = sz > 4096 * 5 ? 4096 * 5 : sz; > + > + create_qtd(qh, trans->token, toggle, buf, iter_sz, trans->result); > + > + buf = (void *)((uintptr_t)buf + iter_sz); > + sz -= iter_sz; > + > + toggle ^= ((iter_sz + mps - 1) / mps) & 1; > + } > +} > + > + > +void ehci_wait_transaction(struct cdi_usb_hc *cdi_hc, > + cdi_usb_hc_transaction_t ta) > +{ > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + ehci_fat_qh_t *qh = ta; > + bool had_timeout = false; > + > + irq_handler(&cdi_hc->dev); > + > + while (qh->first_qtd) { > + if (!had_timeout) { > + if (!(qh->qh.overlay.status & (1 << 7))) { > + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; > + mem_barrier(); > + } > + > + int frame_counter = 0, lframe = (hc->regs->frindex >> 3) % 1024; > + > + while ((qh->first_qtd->qtd.status & (1 << 7)) && > + frame_counter < EHCI_TIMEOUT) > + { > + int cframe = (hc->regs->frindex >> 3) % 1024; > + frame_counter += (cframe + 1024 - lframe) % 1024; > + lframe = cframe; > + > + // I don't really know who's to blame for this (qemu or > + // t?ndur), and actually, I don't even want to know. > + // As a matter of fact, qemu will not work on the async > + // schedule if the async advance doorbell is set but the driver > + // has not acknowledged this; however, the interrupt handler > + // will always clear that bit and the interrupt handler is > + // active all the time, so something is apparently sometimes > + // blocking the handler from being executed. Force it here to > + // prevent qemu's HC from stalling. > + irq_handler(&cdi_hc->dev); > + } > + > + had_timeout = frame_counter >= EHCI_TIMEOUT; > + } > + > + if (had_timeout) { > + // Stop this queue > + qh->qh.overlay.next_qtd = EHCI_T; > + qh->qh.overlay.status &= ~(1u << 7); > + > + *qh->first_qtd->result = CDI_USB_TIMEOUT; > + } > + > + retire_qtd(qh->first_qtd); > + } > +} > + > + > +void ehci_destroy_transaction(struct cdi_usb_hc *cdi_hc, > + cdi_usb_hc_transaction_t ta) > +{ > + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); > + ehci_fat_qh_t *qh = ta; > + > + // TODO: Does this work without having called ehci_wait_transaction() > + // before? Is this TODO intentionally left unresolved? > + qh->qh.overlay.next_qtd = EHCI_T; > + > + int frame_counter = 0, lframe = (hc->regs->frindex >> 3) % 1024; > + > + while ((qh->qh.overlay.status & (1 << 7)) && frame_counter < EHCI_TIMEOUT) { > + int cframe = (hc->regs->frindex >> 3) % 1024; > + frame_counter += (cframe + 1024 - lframe) % 1024; > + lframe = cframe; > + > + // See above. > + irq_handler(&cdi_hc->dev); > + } > + > + if (frame_counter >= EHCI_TIMEOUT) { > + qh->qh.overlay.status &= ~(1 << 7); > + // Could probably be solved more elegantly, but we are in a timeout > + // already, so waiting a bit longer will not be that big of a problem > + // (the sleep is here to ensure that the HC is no longer trying to work > + // on the qTDs belonging to this QH) > + cdi_sleep_ms(100); > + } > + > + while (qh->first_qtd) { > + retire_qtd(qh->first_qtd); > + } > + retire_qh(hc, qh); > +} Well, that's what I can say without actually understanding the hardware. I hope it's better than nothing. Kevin From max at tyndur.org Sun Apr 26 15:52:32 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 26 Apr 2015 15:52:32 +0200 Subject: [cdi-devel] [PATCH v2 7/9] ehci: Add EHCI driver In-Reply-To: <20150418133155.GA2796@silvanus> References: <1429136038-29537-1-git-send-email-max@tyndur.org> <1429136038-29537-8-git-send-email-max@tyndur.org> <20150418133155.GA2796@silvanus> Message-ID: <553CEDA0.1000808@tyndur.org> On 18.04.2015 15:31, Kevin Wolf wrote: > On Thu, Apr 16, 2015 at 12:13:56AM +0200, Max Reitz wrote: >> + This adds an EHCI USB host controller driver. It is missing support >> for periodic schedule (interrupt and isochronous transfers), >> hotplugging, suspend/resume, etc. pp.. >> >> Signed-off-by: Max Reitz >> --- >> ehci/ehci.c | 661 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> ehci/ehci.h | 170 ++++++++++++++++ >> ehci/main.c | 68 +++++++ >> 3 files changed, 899 insertions(+) >> create mode 100644 ehci/ehci.c >> create mode 100644 ehci/ehci.h >> create mode 100644 ehci/main.c >> >> diff --git a/ehci/ehci.c b/ehci/ehci.c >> new file mode 100644 >> index 0000000..6da316e >> --- /dev/null >> +++ b/ehci/ehci.c >> @@ -0,0 +1,661 @@ >> +/****************************************************************************** >> + * Copyright (c) 2015 Max Reitz * >> + * * >> + * Permission is hereby granted, free of charge, to any person obtaining a * >> + * copy of this software and associated documentation files (the "Software"), * >> + * to deal in the Software without restriction, including without limitation * >> + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * >> + * and/or sell copies of the Software, and to permit persons to whom the * >> + * Software is furnished to do so, subject to the following conditions: * >> + * * >> + * The above copyright notice and this permission notice shall be included in * >> + * all copies or substantial portions of the Software. * >> + * * >> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * >> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * >> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * >> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * >> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * >> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * >> + * DEALINGS IN THE SOFTWARE. * >> + ******************************************************************************/ >> + >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> + >> +#include "ehci.h" >> + >> + >> +#define EHCI_TIMEOUT 1000 // ms >> + >> + >> +static bool bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc); >> +static void irq_handler(struct cdi_device *dev); >> + >> + >> +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data) >> +{ >> + struct cdi_pci_device *pci_dev = (struct cdi_pci_device *)bus_data; >> + static int dev_counter = 0; >> + >> + if (pci_dev->class_id != 0x0c || >> + pci_dev->subclass_id != 0x03 || >> + pci_dev->interface_id != 0x20) >> + { >> + return NULL; >> + } > Magic numbers? Yes, because there's not enough magic in this code, so I'll have to make up for it somewhere. >> + >> + struct ehci *hc = calloc(1, sizeof(*hc)); >> + >> + hc->hc.dev.name = malloc(8); >> + sprintf((char *)hc->hc.dev.name, "ehci%i", dev_counter++); >> + >> + hc->retired_qhs = cdi_list_create(); >> + >> + // Enable bus mastering >> + cdi_pci_config_writew(pci_dev, 4, cdi_pci_config_readw(pci_dev, 4) | 0x6); > This one as well. > > I also thought we had specified somewhere that bus mastering must be > enabled by the CDI library, but I can't find it. Maybe we haven't. Well, it doesn't only enable bus mastering but also the MMIO area. Both are mostly a relic of the "Eliminate all possible and impossible error cases" phase. Indeed, bus mastering should generally be enabled by the CDI library, and the MMIO space needs to be enabled in the cdi_pci_alloc_memory() function (same for I/O and cdi_pci_alloc_ioports()). >> + >> + cdi_pci_alloc_memory(pci_dev); >> + >> + struct cdi_pci_resource *res; >> + for (int i = 0; (res = cdi_list_get(pci_dev->resources, i)); i++) { >> + if (res->type == CDI_PCI_MEMORY && !res->index) { >> + hc->caps = res->address; >> + hc->regs = (struct ehci_registers *)((uintptr_t)res->address + >> + hc->caps->caplength); >> + } >> + } > Check that hc->caps is set and fail otherwise. But the specification defines it this way! :-) Will fix. >> + >> + if (!bios_handoff(pci_dev, hc)) { >> + goto fail; >> + } >> + >> + // Stop operation >> + hc->regs->usbcmd = (hc->regs->usbcmd & 0xff00ff00u) | 0x00080010u; > Right, because these are obvious and nice numbers. :-) Of course. All you need to know is given in the comment above. ;-) > (Won't mention it everywhere, but there are _lots_ of magic numbers in > this patch that should be replaced by defines.) > >> + // Wait until HCHALTED becomes true >> + int timeout_counter = 0; >> + while (!(hc->regs->usbsts & (1 << 12)) && ++timeout_counter < 50) { >> + cdi_sleep_ms(1); >> + } >> + if (timeout_counter >= 50) { >> + goto fail; >> + } > This is pretty common pattern. Worth providing a function/macro for it, > possiby even in the CDI headers? Well, if I would have to add a function/macro for it, that probably means I should do it right which means not relying on cdi_sleep_ms() to be exact, but to introduce cdi_elapsed_ms() or something like that. That would have the nice side-effect of not relying on the frame index as a timer below in ehci_destroy_transaction(). I guess it should be simple enough to introduce for both t?ndur and ?xo?cota, so I'd be fine with that. What do you think? >> + hc->regs->usbcmd |= (1 << 1); // Reset >> + timeout_counter = 0; >> + while ((hc->regs->usbcmd & (1 << 1)) && ++timeout_counter < 50) { >> + cdi_sleep_ms(1); >> + } >> + if (timeout_counter >= 50) { >> + goto fail; >> + } >> + >> + if (hc->caps->hccparams & 1) { >> + // 64-bit addressing capability >> + hc->regs->ctrldssegment = 0; >> + mem_barrier(); > Make it cdi_barrier() and move it to cdi/misc.h (I would avoid calling > it a "memory" barrier because that could imply that CPU caches are > flushed, which is not the case; it's just a compiler barrier). This is > definitely a function that other drivers can use. > > That said, with regs being volatile, what does the barrier achieve here? > Except when it's really obvious, comments are appropriate to explain the > purpose of each barrier. I'll just drop all the barriers and hope it'll still work. >> + } >> + >> + // async advance doorbell >> + hc->regs->usbintr = (1 << 5); >> + hc->regs->usbsts = hc->regs->usbsts; >> + cdi_register_irq(pci_dev->irq, &irq_handler, &hc->hc.dev); >> + >> + hc->periodic_list_mem = cdi_mem_alloc(4096, 12 >> + | CDI_MEM_PHYS_CONTIGUOUS >> + | CDI_MEM_DMA_4G >> + | CDI_MEM_NOINIT); >> + hc->periodic_list = hc->periodic_list_mem->vaddr; >> + for (int i = 0; i < 1024; i++) { >> + hc->periodic_list[i] = EHCI_T; >> + } >> + mem_barrier(); >> + hc->regs->periodiclistbase = hc->periodic_list_mem->paddr.items[0].start; > The same question here because periodic_list is declared as volatile. > The difference is that I'm actually not sure whether it should be > volatile or whether relying on the barriers isn't better. > >> + >> + // Create anchor >> + struct cdi_mem_area *anchor_qh; >> + anchor_qh = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 >> + | CDI_MEM_PHYS_CONTIGUOUS >> + | CDI_MEM_DMA_4G >> + | CDI_MEM_NOINIT); >> + >> + hc->async_start = anchor_qh->vaddr; >> + memset(hc->async_start, 0, sizeof(*hc->async_start)); >> + >> + hc->async_start->cdi_mem_area = anchor_qh; >> + hc->async_start->paddr = anchor_qh->paddr.items[0].start >> + + offsetof(ehci_fat_qh_t, qh); >> + >> + hc->async_start->next = hc->async_start; >> + hc->async_start->previous = hc->async_start; >> + >> + hc->async_start->qh.next_qh = hc->async_start->paddr | EHCI_QH; >> + hc->async_start->qh.ep_state[0] = 0x0008e000u; >> + hc->async_start->qh.ep_state[1] = 0x4000ff00u; >> + >> + hc->async_start->qh.overlay.next_qtd = EHCI_T; >> + hc->async_start->qh.overlay.alt_next_qtd = EHCI_T; >> + >> + hc->regs->asynclistaddr = hc->async_start->paddr; >> + >> + mem_barrier(); >> + >> + // Start operation >> + hc->regs->usbcmd = (hc->regs->usbcmd & 0xff00ff00u) | 0x00080031u; >> + >> + // Wait until HCHalted becomes false and the schedules are running >> + timeout_counter = 0; >> + while ((hc->regs->usbsts & 0xd000) != 0xc000 && ++timeout_counter < 50) { >> + cdi_sleep_ms(1); >> + } >> + if (timeout_counter >= 50) { >> + goto fail; >> + } >> + >> + // Route everything to this HC >> + hc->regs->configflag |= 1; >> + >> + mem_barrier(); >> + cdi_sleep_ms(5); >> + >> + hc->hc.rh.ports = hc->caps->hcsparams & 0xf; >> + >> + // Power up all ports >> + for (int i = 0; i < hc->hc.rh.ports; i++) { >> + hc->regs->portsc[i] |= EHCI_PSC_PP; >> + } >> + >> + mem_barrier(); > This one looks really random. > >> + >> + return &hc->hc.dev; >> + >> +fail: >> + if (hc->async_start) { >> + cdi_mem_free(hc->async_start->cdi_mem_area); >> + } >> + if (hc->periodic_list_mem) { >> + cdi_mem_free(hc->periodic_list_mem); >> + } >> + free((void *)hc->hc.dev.name); >> + free(hc); >> + cdi_pci_free_memory(pci_dev); > retired_qhs is leaked. cdi_pci_free_memory() crashes t?ndur anyway, so why would you care? :-) I'll fix it. > It seems that other drivers have the same problem, but it would be nice to > unregister the IRQ. (For microkernels, the problem probably isn't serious > because the process ends and the kernel should clean up, but for > monolithic ones a system crash might not be far away.) Or I'll just see to registering the IRQ only once everything is set up and this path can no longer be reached. >> + return NULL; >> +} >> + >> + >> +static bool bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc) >> +{ >> + int eecp = (hc->caps->hccparams >> 8) & 0xff; >> + >> + // Iterate through the list of capabilities >> + while (eecp >= 0x40) { >> + uint32_t cap = cdi_pci_config_readl(pci_dev, eecp); >> + >> + // Look for USBLEGSUP >> + if ((cap & 0xff) == 0x01) { >> + // Set OS semaphore >> + cdi_pci_config_writeb(pci_dev, eecp + 3, 1); >> + >> + // Wait for BIOS semaphore to get unset >> + int timeout_counter = 0; >> + while ((cdi_pci_config_readb(pci_dev, eecp + 2) & 1) && >> + ++timeout_counter < 1000) >> + { >> + cdi_sleep_ms(1); >> + } >> + if (timeout_counter >= 1000) { >> + return false; >> + } >> + } >> + >> + eecp = (cap >> 8) & 0xff; >> + } >> + >> + return true; >> +} >> + >> + >> +static void irq_handler(struct cdi_device *dev) >> +{ >> + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(dev, dev, struct cdi_usb_hc); >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + >> + uint32_t usbsts = hc->regs->usbsts; >> + mem_barrier(); >> + hc->regs->usbsts = usbsts; >> + >> + // async advance doorbell >> + if (!(usbsts & (1 << 5))) { >> + return; >> + } >> + >> + ehci_fat_qh_t *rqh; >> + while ((rqh = cdi_list_pop(hc->retired_qhs))) { >> + cdi_mem_free(rqh->cdi_mem_area); >> + } >> +} > As always, I don't really know what I'm doing, so let me ask a stupid > question: > > You use a list here, so I assume that multiple QHs can be retired. You > don't immediately free them but only after getting a doorbell IRQ, so I > assume that they are still in use by the hardware until then. > > Does the hardware complete the use of all retired QHs at once, or what > does ensure that we don't free all retired QHs when the hardware is only > done with the first one? > > (Disclaimer: I don't really know what a QH is.) I'll look into it, maybe it's wrong, maybe it isn't (as discussed on IRC). I hope it isn't. >> + >> +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index) >> +{ >> + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + >> + hc->regs->portsc[index] &= ~EHCI_PSC_PED; >> + >> + mem_barrier(); >> +} >> + >> + >> +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index) >> +{ >> + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + uint32_t portsc = hc->regs->portsc[index]; >> + >> + if ((portsc & EHCI_PSC_LS_MASK) == EHCI_PSC_LS_K) { >> + // Low-speed device, release ownership >> + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; >> + mem_barrier(); >> + return; >> + } >> + >> + // Assert reset signal >> + hc->regs->portsc[index] = ((portsc & ~(EHCI_PSC_PO | EHCI_PSC_SUS | >> + EHCI_PSC_FPR | EHCI_PSC_PED)) >> + | EHCI_PSC_RES); >> + mem_barrier(); >> + cdi_sleep_ms(50); >> + >> + // De-assert reset signal >> + int timeout_counter = 0; >> + hc->regs->portsc[index] = portsc & ~EHCI_PSC_RES; >> + do { >> + portsc = hc->regs->portsc[index]; >> + cdi_sleep_ms(1); >> + } while ((portsc & EHCI_PSC_RES) && ++timeout_counter < 50); >> + >> + if (portsc & EHCI_PSC_RES) { >> + // Disable port, route on to companion controller >> + // (maybe it can do at least something with it) >> + hc->regs->portsc[index] = (portsc & ~EHCI_PSC_PED) | EHCI_PSC_PO; >> + } else if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == EHCI_PSC_CCS) { >> + // Device connected but port disabled: >> + // Full-speed device, release ownership >> + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; >> + } >> + >> + mem_barrier(); >> +} >> + >> + >> +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index) >> +{ >> + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, rh, struct cdi_usb_hc); >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + uint32_t portsc = hc->regs->portsc[index]; >> + cdi_usb_port_status_t status = 0; >> + >> + if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == >> + (EHCI_PSC_PED | EHCI_PSC_CCS)) >> + { >> + // Device connected and port enabled >> + status |= CDI_USB_PORT_CONNECTED | CDI_USB_HIGH_SPEED; >> + } >> + >> + return status; >> +} >> + >> + >> +static void enter_async_qh(struct ehci *hc, ehci_fat_qh_t *qh) >> +{ >> + // Enter the new QH as last >> + >> + qh->next = hc->async_start; >> + qh->previous = hc->async_start->previous; >> + >> + qh->previous->next = qh; >> + hc->async_start->previous = qh; > Shouldn't the barrier actually be here so that the hardware is > guaranteed to see the new links instead of some intermediate state when > qh->qh.next_qh is updated? Well, until here we haven't done anything the hardware can see, and the hardware won't even see the QH at all until qh->previous->qh.next_qh is set. Anyway, I'll just drop the mem_barrier() calls... >> + qh->qh.next_qh = hc->async_start->paddr | EHCI_QH; >> + mem_barrier(); >> + >> + qh->previous->qh.next_qh = qh->paddr | EHCI_QH; >> + mem_barrier(); >> +} >> + >> + >> +static void retire_qh(struct ehci *hc, ehci_fat_qh_t *qh) >> +{ >> + qh->previous->qh.next_qh = qh->qh.next_qh; >> + mem_barrier(); >> + >> + qh->previous->next = qh->next; >> + qh->next->previous = qh->previous; >> + >> + cdi_list_push(hc->retired_qhs, qh); >> + // Request async advance doorbell >> + hc->regs->usbcmd |= (1 << 6); >> +} >> + >> + >> +static void retire_qtd(ehci_fat_qtd_t *qtd) >> +{ >> + ehci_fat_qh_t *qh = qtd->qh; >> + >> + if (qtd->qtd.status & 0x7c) { >> + if (qtd->qtd.status & (1 << 4)) { >> + *qtd->result = CDI_USB_BABBLE; >> + } else if (qtd->qtd.status & (1 << 3)) { >> + *qtd->result = CDI_USB_BAD_RESPONSE; >> + } else if (qtd->qtd.status & ((1 << 5) | (1 << 2))) { >> + *qtd->result = CDI_USB_HC_ERROR; >> + } else if (qtd->qtd.status & (1 << 6)) { >> + *qtd->result = CDI_USB_STALL; >> + } else { >> + *qtd->result = CDI_USB_HC_ERROR; >> + } >> + } >> + >> + if (qtd->write_back) { >> + size_t rem = qtd->write_back_size; >> + for (int i = 0; i < 5 && rem; i++) { >> + size_t sz = rem > 4096 ? 4096 : rem; >> + memcpy((void *)((uintptr_t)qtd->write_back + i * 4096), >> + qtd->buffers[i]->vaddr, sz); >> + rem -= sz; >> + } >> + assert(!rem); >> + } >> + >> + qh->first_qtd = qtd->next; >> + if (qh->last_qtd == qtd) { >> + qh->last_qtd = NULL; >> + } >> + >> + for (int i = 0; i < 5; i++) { >> + if (qtd->buffers[i]) { >> + cdi_mem_free(qtd->buffers[i]); >> + } >> + } >> + >> + cdi_mem_free(qtd->cdi_mem_area); >> +} >> + >> + >> +static void enter_qtd(ehci_fat_qh_t *qh, ehci_fat_qtd_t *qtd) >> +{ >> + bool first; >> + >> + qtd->qh = qh; >> + qtd->qtd.next_qtd = EHCI_T; >> + qtd->qtd.alt_next_qtd = EHCI_T; >> + mem_barrier(); >> + >> + while (qh->first_qtd) { >> + /* Break if an active qTD is encountered */ >> + if (qh->first_qtd->qtd.status & (1 << 7)) { >> + break; >> + } >> + >> + retire_qtd(qh->first_qtd); >> + } >> + >> + first = !qh->first_qtd; >> + >> + // Note: Race condition (a qTD might retire here and we are thus appending >> + // the one to be queued to an inactive qTD); it's fine as long as we are >> + // constantly rescanning the queue (here and in ehci_wait_transaction()) >> + >> + if (first) { >> + qh->first_qtd = qtd; >> + } else { >> + qh->last_qtd->next = qtd; >> + } >> + qh->last_qtd = qtd; >> + >> + if (!first) { >> + qh->last_qtd->qtd.next_qtd = qtd->paddr; >> + mem_barrier(); >> + } >> + >> + // If the QH is not active, it's done with its queue of qTDs and we should >> + // continue from this qTD >> + if (!(qh->qh.overlay.status & (1 << 7))) { >> + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; >> + mem_barrier(); >> + } >> +} >> + >> + >> +cdi_usb_hc_transaction_t ehci_create_transaction(struct cdi_usb_hc *cdi_hc, >> + const struct cdi_usb_hc_ep_info *target) >> +{ >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + struct cdi_mem_area *qh_mem; >> + >> + qh_mem = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 >> + | CDI_MEM_PHYS_CONTIGUOUS >> + | CDI_MEM_DMA_4G >> + | CDI_MEM_NOINIT); >> + ehci_fat_qh_t *qh = qh_mem->vaddr; >> + >> + memset(qh, 0, sizeof(*qh)); >> + >> + int non_high_speed_control = target->speed != CDI_USB_HIGH_SPEED >> + && target->ep_type == CDI_USB_CONTROL; >> + >> + qh->qh.ep_state[0] = target->dev | (target->ep << 8) | (1 << 14) >> + | (target->mps << 16) | (non_high_speed_control << 27); >> + switch (target->speed) { >> + case CDI_USB_LOW_SPEED: qh->qh.ep_state[0] |= (1 << 12); break; >> + case CDI_USB_FULL_SPEED: qh->qh.ep_state[0] |= (0 << 12); break; >> + case CDI_USB_HIGH_SPEED: qh->qh.ep_state[0] |= (2 << 12); break; >> + case CDI_USB_SUPERSPEED: >> + default: >> + abort(); >> + } >> + >> + qh->qh.ep_state[1] = (0xff << 8) | (1 << 30) | (target->tt_addr << 16) >> + | ((target->tt_port + 1) << 23); >> + >> + qh->qh.overlay.next_qtd = EHCI_T; >> + qh->qh.overlay.alt_next_qtd = EHCI_T; >> + >> + qh->cdi_mem_area = qh_mem; >> + qh->paddr = qh_mem->paddr.items[0].start + offsetof(ehci_fat_qh_t, qh); >> + >> + mem_barrier(); >> + >> + enter_async_qh(hc, qh); >> + >> + return qh; >> +} >> + >> + >> +static void create_qtd(ehci_fat_qh_t *qh, cdi_usb_transfer_token_t token, >> + int toggle, void *buffer, size_t size, >> + cdi_usb_transmission_result_t *result) >> +{ >> + struct cdi_mem_area *qtd_mem; >> + >> + qtd_mem = cdi_mem_alloc(sizeof(ehci_fat_qtd_t), 6 >> + | CDI_MEM_PHYS_CONTIGUOUS >> + | CDI_MEM_DMA_4G >> + | CDI_MEM_NOINIT); >> + ehci_fat_qtd_t *qtd = qtd_mem->vaddr; >> + >> + memset(qtd, 0, sizeof(*qtd)); >> + >> + qtd->qtd.status = (1 << 7) | (3 << 10) | (size << 16) >> + | ((uint32_t)toggle << 31); >> + >> + switch (token) { >> + case CDI_USB_IN: qtd->qtd.status |= (1 << 8); break; >> + case CDI_USB_OUT: qtd->qtd.status |= (0 << 8); break; >> + case CDI_USB_SETUP: qtd->qtd.status |= (2 << 8); break; >> + default: >> + abort(); >> + } >> + >> + if (token == CDI_USB_IN) { >> + qtd->write_back = buffer; >> + qtd->write_back_size = size; >> + } >> + >> + for (int i = 0; i < 5 && size; i++) { >> + size_t sz = size > 4096 ? 4096 : size; >> + >> + qtd->buffers[i] = cdi_mem_alloc(4096, 12 >> + | CDI_MEM_PHYS_CONTIGUOUS >> + | CDI_MEM_DMA_4G >> + | CDI_MEM_NOINIT); >> + qtd->qtd.buffers[i] = qtd->buffers[i]->paddr.items[0].start; >> + >> + if (token == CDI_USB_OUT || token == CDI_USB_SETUP) { >> + memcpy(qtd->buffers[i]->vaddr, >> + (void *)((uintptr_t)buffer + i * 4096), sz); >> + } >> + >> + size -= sz; >> + } >> + >> + qtd->cdi_mem_area = qtd_mem; >> + qtd->paddr = qtd_mem->paddr.items[0].start + offsetof(ehci_fat_qtd_t, qtd); >> + >> + qtd->result = result; >> + >> + mem_barrier(); >> + >> + enter_qtd(qh, qtd); >> +} >> + >> + >> +void ehci_enqueue(struct cdi_usb_hc *cdi_hc, >> + const struct cdi_usb_hc_transmission *trans) >> +{ >> + (void)cdi_hc; >> + >> + ehci_fat_qh_t *qh = trans->ta; >> + bool first = true; >> + size_t sz = trans->size; >> + void *buf = trans->buffer; > Make it uint8_t* to save some casts? I don't know... I don't like giving opaque pointers a type just without it being really necessary (e.g. for a memcpy() implementation, it is necessary). On the other hand, "buf += iter_sz" does look quite nice. >> + int toggle = trans->toggle; >> + size_t mps = (qh->qh.ep_state[0] >> 16) & 0x7ff; >> + >> + *trans->result = CDI_USB_OK; >> + >> + while (sz || first) { >> + first = false; >> + >> + size_t iter_sz = sz > 4096 * 5 ? 4096 * 5 : sz; >> + >> + create_qtd(qh, trans->token, toggle, buf, iter_sz, trans->result); >> + >> + buf = (void *)((uintptr_t)buf + iter_sz); >> + sz -= iter_sz; >> + >> + toggle ^= ((iter_sz + mps - 1) / mps) & 1; >> + } >> +} >> + >> + >> +void ehci_wait_transaction(struct cdi_usb_hc *cdi_hc, >> + cdi_usb_hc_transaction_t ta) >> +{ >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + ehci_fat_qh_t *qh = ta; >> + bool had_timeout = false; >> + >> + irq_handler(&cdi_hc->dev); >> + >> + while (qh->first_qtd) { >> + if (!had_timeout) { >> + if (!(qh->qh.overlay.status & (1 << 7))) { >> + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; >> + mem_barrier(); >> + } >> + >> + int frame_counter = 0, lframe = (hc->regs->frindex >> 3) % 1024; >> + >> + while ((qh->first_qtd->qtd.status & (1 << 7)) && >> + frame_counter < EHCI_TIMEOUT) >> + { >> + int cframe = (hc->regs->frindex >> 3) % 1024; >> + frame_counter += (cframe + 1024 - lframe) % 1024; >> + lframe = cframe; >> + >> + // I don't really know who's to blame for this (qemu or >> + // t?ndur), and actually, I don't even want to know. >> + // As a matter of fact, qemu will not work on the async >> + // schedule if the async advance doorbell is set but the driver >> + // has not acknowledged this; however, the interrupt handler >> + // will always clear that bit and the interrupt handler is >> + // active all the time, so something is apparently sometimes >> + // blocking the handler from being executed. Force it here to >> + // prevent qemu's HC from stalling. >> + irq_handler(&cdi_hc->dev); >> + } >> + >> + had_timeout = frame_counter >= EHCI_TIMEOUT; >> + } >> + >> + if (had_timeout) { >> + // Stop this queue >> + qh->qh.overlay.next_qtd = EHCI_T; >> + qh->qh.overlay.status &= ~(1u << 7); >> + >> + *qh->first_qtd->result = CDI_USB_TIMEOUT; >> + } >> + >> + retire_qtd(qh->first_qtd); >> + } >> +} >> + >> + >> +void ehci_destroy_transaction(struct cdi_usb_hc *cdi_hc, >> + cdi_usb_hc_transaction_t ta) >> +{ >> + struct ehci *hc = CDI_UPCAST(cdi_hc, hc, struct ehci); >> + ehci_fat_qh_t *qh = ta; >> + >> + // TODO: Does this work without having called ehci_wait_transaction() >> + // before? > Is this TODO intentionally left unresolved? Yes. Or do you want to resolve it? :-) I guess it works. It should work. But I haven't though really hard about it. The EHCI specification does mention this case in the QH unlinking section, and it states that the active bit must be cleared. This is achieved here by... >> + qh->qh.overlay.next_qtd = EHCI_T; ...clearing the list of transfers, ... >> + >> + int frame_counter = 0, lframe = (hc->regs->frindex >> 3) % 1024; >> + >> + while ((qh->qh.overlay.status & (1 << 7)) && frame_counter < EHCI_TIMEOUT) { ...waiting until either the active bit is cleared (by the HC, because the transfer list is empty) or a second has passed, ... >> + int cframe = (hc->regs->frindex >> 3) % 1024; >> + frame_counter += (cframe + 1024 - lframe) % 1024; >> + lframe = cframe; >> + >> + // See above. >> + irq_handler(&cdi_hc->dev); >> + } >> + >> + if (frame_counter >= EHCI_TIMEOUT) { >> + qh->qh.overlay.status &= ~(1 << 7); ...and if the bit still isn't cleared after that second, we'll just force-clear it and... >> + // Could probably be solved more elegantly, but we are in a timeout >> + // already, so waiting a bit longer will not be that big of a problem >> + // (the sleep is here to ensure that the HC is no longer trying to work >> + // on the qTDs belonging to this QH) >> + cdi_sleep_ms(100); ...wait a couple of milliseconds so the HC doesn't have any transfers in flight relating to this QH. >> + } >> + >> + while (qh->first_qtd) { >> + retire_qtd(qh->first_qtd); >> + } >> + retire_qh(hc, qh); And then we'll just retire the whole thing. The active bit is cleared at this point, so the specification says we're good to do so. >> +} > Well, that's what I can say without actually understanding the hardware. > I hope it's better than nothing. Indeed it is. Thank you! :-) Max > Kevin > _______________________________________________ > cdi-devel mailing list > cdi-devel at tyndur.org > http://list.tyndur.org/cgi-bin/mailman/listinfo/cdi-devel