Skip to contentSkip to conversation

Add xHCI USB support

PATCH Superseded Patrick Rudolph 1 participant 7 messages Dec 2, 2020 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add xHCI USB support

Add basic support for xHCI USB controllers.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.

Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoff

Tested:
* Qemu system x86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* Intel C246 integrated xHCI (Supermicro X11SSH-F)
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* External USB HID keyboard

TODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllers

Patrick Rudolph (6):
grub-core/bus/usb: Parse SuperSpeed companion descriptors
usb: Add enum for xHCI
usbtrans: Set default maximum packet size grub-core/bus/usb: Add function pointer for attach/detach events grub-core/bus/usb/usbhub: Add new private fields for xHCI controller grub-core/bus/usb: Add xhci support

 Makefile.am                       |    2 +-
 grub-core/Makefile.core.def       |    7 +
 grub-core/bus/usb/ehci.c          |    2 +
 grub-core/bus/usb/ohci.c          |    2 +
 grub-core/bus/usb/serial/common.c |    2 +-
 grub-core/bus/usb/uhci.c          |    2 +
 grub-core/bus/usb/usb.c           |   44 +-
 grub-core/bus/usb/usbhub.c        |   55 +-
 grub-core/bus/usb/usbtrans.c      |    2 +
 grub-core/bus/usb/xhci-pci.c      |  195 +++
 grub-core/bus/usb/xhci.c          | 2383 +++++++++++++++++++++++++++++
 grub-core/commands/usbtest.c      |    2 +-
 grub-core/disk/usbms.c            |    2 +-
 grub-core/term/usb_keyboard.c     |    2 +-
 include/grub/usb.h                |   18 +-
 include/grub/usbdesc.h            |   11 +-
 16 files changed, 2697 insertions(+), 34 deletions(-)
 create mode 100644 grub-core/bus/usb/xhci-pci.c
 create mode 100644 grub-core/bus/usb/xhci.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/6 grub-core/bus/usb: Parse SuperSpeed companion descriptors

Parse the SS_ENDPOINT_COMPANION descriptor, which is only present on USB 3.0 capable devices and xHCI controllers. Make the descendp an array of pointers to the endpoint descriptor as it's no longer an continous array.

---
 grub-core/bus/usb/serial/common.c |  2 +-
 grub-core/bus/usb/usb.c           | 44 +++++++++++++++++++------------
 grub-core/bus/usb/usbhub.c        | 22 ++++++++++++----
 grub-core/commands/usbtest.c      |  2 +-
 grub-core/disk/usbms.c            |  2 +-
 grub-core/term/usb_keyboard.c     |  2 +-
 include/grub/usb.h                |  2 +-
 include/grub/usbdesc.h            | 11 +++++++-
 8 files changed, 59 insertions(+), 28 deletions(-)
grub-core/bus/usb/serial/common.c +1 -1
index 8e94c7dc0..63e64cc0a 100644
@@ -72,7 +72,7 @@ grub_usbserial_attach (grub_usb_device_t usbdev, int configno, int interfno,
   for (j = 0; j < interf->endpointcnt; j++)
     {
       struct grub_usb_desc_endp *endp;
-      endp = &usbdev->config[0].interf[interfno].descendp[j];
+      endp = usbdev->config[0].interf[interfno].descendp[j];
 
       if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2
 	  && (in_endp == GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING
grub-core/bus/usb/usb.c +27 -17

72 lines not on this page · raw message

grub-core/bus/usb/usbhub.c +17 -5
index a06cce302..136bf6ee4 100644
@@ -82,8 +82,14 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller,
   if (i == GRUB_USBHUB_MAX_DEVICES)
     {
       grub_error (GRUB_ERR_IO, "can't assign address to USB device");
-      for (i = 0; i < 8; i++)
-        grub_free (dev->config[i].descconf);
+      for (i = 0; i < 8; i++) {
+	int currif;
+
+	for (currif = 0; currif < dev->config[i].descconf->numif; currif++)
+	  grub_free (dev->config[i].interf[currif].descendp);
+
+	grub_free (dev->config[i].descconf);
+      }
       grub_free (dev);
       return NULL;
     }
@@ -96,8 +102,14 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller,
 			      i, 0, 0, NULL);
   if (err)
     {
-      for (i = 0; i < 8; i++)
-        grub_free (dev->config[i].descconf);
+      for (i = 0; i < 8; i++) {
+	int currif;
+
+	for (currif = 0; currif < dev->config[i].descconf->numif; currif++)
+	  grub_free (dev->config[i].interf[currif].descendp);
+
+	grub_free (dev->config[i].descconf);
+      }
       grub_free (dev);
       return NULL;
     }
@@ -176,7 +188,7 @@ grub_usb_add_hub (grub_usb_device_t dev)
        i++)
     {
       struct grub_usb_desc_endp *endp = NULL;
-      endp = &dev->config[0].interf[0].descendp[i];
+      endp = dev->config[0].interf[0].descendp[i];
 
       if ((endp->endp_addr & 128) && grub_usb_get_ep_type(endp)
 	  == GRUB_USB_EP_INTERRUPT)
grub-core/commands/usbtest.c +1 -1
index 2c6d93fe6..55a657635 100644
@@ -185,7 +185,7 @@ usb_iterate (grub_usb_device_t dev, void *data __attribute__ ((unused)))
       for (j = 0; j < interf->endpointcnt; j++)
 	{
 	  struct grub_usb_desc_endp *endp;
-	  endp = &dev->config[0].interf[i].descendp[j];
+	  endp = dev->config[0].interf[i].descendp[j];
 
 	  grub_printf ("Endpoint #%d: %s, max packed size: %d, transfer type: %s, latency: %d\n",
 		       endp->endp_addr & 15,
grub-core/disk/usbms.c +1 -1
index 380ca4c4c..64e1be8ff 100644
@@ -184,7 +184,7 @@ grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)
   for (j = 0; j < interf->endpointcnt; j++)
     {
       struct grub_usb_desc_endp *endp;
-      endp = &usbdev->config[0].interf[interfno].descendp[j];
+      endp = usbdev->config[0].interf[interfno].descendp[j];
 
       if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2)
 	/* Bulk IN endpoint.  */
grub-core/term/usb_keyboard.c +1 -1
index e67b8f785..d65e3474d 100644
@@ -175,7 +175,7 @@ grub_usb_keyboard_attach (grub_usb_device_t usbdev, int configno, int interfno)
   for (j = 0; j < usbdev->config[configno].interf[interfno].descif->endpointcnt;
        j++)
     {
-      endp = &usbdev->config[configno].interf[interfno].descendp[j];
+      endp = usbdev->config[configno].interf[interfno].descendp[j];
 
       if ((endp->endp_addr & 128) && grub_usb_get_ep_type(endp)
 	  == GRUB_USB_EP_INTERRUPT)
include/grub/usb.h +1 -1
index 512ae1dd0..0527a3e2b 100644
@@ -149,7 +149,7 @@ struct grub_usb_interface
 {
   struct grub_usb_desc_if *descif;
 
-  struct grub_usb_desc_endp *descendp;
+  struct grub_usb_desc_endp **descendp;
 
   /* A driver is handling this interface. Do we need to support multiple drivers
      for single interface?
include/grub/usbdesc.h +10 -1
index aac5ab05a..bb2ab2e27 100644
@@ -29,7 +29,8 @@ typedef enum {
   GRUB_USB_DESCRIPTOR_INTERFACE,
   GRUB_USB_DESCRIPTOR_ENDPOINT,
   GRUB_USB_DESCRIPTOR_DEBUG = 10,
-  GRUB_USB_DESCRIPTOR_HUB = 0x29
+  GRUB_USB_DESCRIPTOR_HUB = 0x29,
+  GRUB_USB_DESCRIPTOR_SS_ENDPOINT_COMPANION = 0x30
 } grub_usb_descriptor_t;
 
 struct grub_usb_desc
@@ -105,6 +106,14 @@ struct grub_usb_desc_endp
   grub_uint8_t interval;
 } GRUB_PACKED;
 
+struct grub_usb_desc_ssep {
+  grub_uint8_t  length;
+  grub_uint8_t  type;
+  grub_uint8_t  maxburst;
+  grub_uint8_t  attrib;
+  grub_uint16_t interval;
+} GRUB_PACKED;
+
 struct grub_usb_desc_str
 {
   grub_uint8_t length;
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/6 usb: Add enum for xHCI

In reply to Patrick Rudolph

Will be used in future patches.

---
 include/grub/usb.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/grub/usb.h +2 -1
index 0527a3e2b..f65ac4f5d 100644
@@ -47,7 +47,8 @@ typedef enum
     GRUB_USB_SPEED_NONE,
     GRUB_USB_SPEED_LOW,
     GRUB_USB_SPEED_FULL,
-    GRUB_USB_SPEED_HIGH
+    GRUB_USB_SPEED_HIGH,
+    GRUB_USB_SPEED_SUPER
   } grub_usb_speed_t;
 
 typedef int (*grub_usb_iterate_hook_t) (grub_usb_device_t dev, void *data);
Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/6 usbtrans: Set default maximum packet size

In reply to Patrick Rudolph

Set the maximum packet size to 512 for SuperSpeed devices.

---
 grub-core/bus/usb/usbtrans.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/bus/usb/usbtrans.c +2 -0
index 85f081fff..5b68f17f9 100644
@@ -130,6 +130,8 @@ grub_usb_control_msg (grub_usb_device_t dev,
   /* Determine the maximum packet size.  */
   if (dev->descdev.maxsize0)
     max = dev->descdev.maxsize0;
+  else if (dev->speed == GRUB_USB_SPEED_SUPER)
+    max = 512;
   else
     max = 64;
 
Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/6 grub-core/bus/usb: Add function pointer for attach/detach events

In reply to Patrick Rudolph

The xHCI code needs to be called for attaching or detaching a device. Introduce two functions pointers and call it from the USB hub code.

Will be used in future commits, currently this doesn't change any functionality.

---
 grub-core/bus/usb/ehci.c   |  2 ++
 grub-core/bus/usb/ohci.c   |  2 ++
 grub-core/bus/usb/uhci.c   |  2 ++
 grub-core/bus/usb/usbhub.c | 19 +++++++++++++++++++
 include/grub/usb.h         |  4 ++++
 5 files changed, 29 insertions(+)
grub-core/bus/usb/ehci.c +2 -0
index d966fc210..6b3f2f87a 100644
@@ -1812,6 +1812,8 @@ static struct grub_usb_controller_dev usb_controller = {
   .hubports = grub_ehci_hubports,
   .portstatus = grub_ehci_portstatus,
   .detect_dev = grub_ehci_detect_dev,
+  .attach_dev = NULL,
+  .detach_dev = NULL,
   /* estimated max. count of TDs for one bulk transfer */
   .max_bulk_tds = GRUB_EHCI_N_TD * 3 / 4 
 };
grub-core/bus/usb/ohci.c +2 -0
index f0be533d4..44c7e94a9 100644
@@ -1440,6 +1440,8 @@ static struct grub_usb_controller_dev usb_controller =
   .hubports = grub_ohci_hubports,
   .portstatus = grub_ohci_portstatus,
   .detect_dev = grub_ohci_detect_dev,
+  .attach_dev = NULL,
+  .detach_dev = NULL,
   /* estimated max. count of TDs for one bulk transfer */
   .max_bulk_tds = GRUB_OHCI_TDS * 3 / 4
 };
grub-core/bus/usb/uhci.c +2 -0
index 7c5811fd6..4143d24fe 100644
@@ -845,6 +845,8 @@ static struct grub_usb_controller_dev usb_controller =
   .hubports = grub_uhci_hubports,
   .portstatus = grub_uhci_portstatus,
   .detect_dev = grub_uhci_detect_dev,
+  .attach_dev = NULL,
+  .detach_dev = NULL,
   /* estimated max. count of TDs for one bulk transfer */
   .max_bulk_tds = N_TD * 3 / 4
 };
grub-core/bus/usb/usbhub.c +19 -0
index 136bf6ee4..df1913aee 100644
@@ -66,6 +66,15 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller,
   dev->split_hubport = split_hubport;
   dev->split_hubaddr = split_hubaddr;
 
+  if (controller->dev->attach_dev) {
+    err = controller->dev->attach_dev (controller, dev);
+    if (err)
+      {
+	grub_free (dev);
+	return NULL;
+      }
+  }
+
   err = grub_usb_device_initialize (dev);
   if (err)
     {
@@ -405,6 +414,8 @@ static void
 detach_device (grub_usb_device_t dev)
 {
   unsigned i;
+  grub_usb_err_t err;
+
   int k;
   if (!dev)
     return;
@@ -425,6 +436,14 @@ detach_device (grub_usb_device_t dev)
 	  if (inter && inter->detach_hook)
 	    inter->detach_hook (dev, i, k);
 	}
+  if (dev->controller.dev->detach_dev) {
+    err = dev->controller.dev->detach_dev (&dev->controller, dev);
+    if (err)
+      {
+	// XXX
+      }
+  }
+
   grub_usb_devs[dev->addr] = 0;
 }
 
include/grub/usb.h +4 -0
index f65ac4f5d..5c5fb5ec4 100644
@@ -122,6 +122,10 @@ struct grub_usb_controller_dev
 
   grub_usb_speed_t (*detect_dev) (grub_usb_controller_t dev, int port, int *changed);
 
+  grub_usb_err_t (*attach_dev) (grub_usb_controller_t ctrl, grub_usb_device_t dev);
+
+  grub_usb_err_t (*detach_dev) (grub_usb_controller_t ctrl, grub_usb_device_t dev);
+
   /* Per controller flag - port reset pending, don't do another reset */
   grub_uint64_t pending_reset;
 
Signature · 1 line
2.26.2
#5 lore raw

PATCH 5/6 grub-core/bus/usb/usbhub: Add new private fields for xHCI controller

In reply to Patrick Rudolph

Store the root port number, the route, consisting out of the port ID in each nibble, and a pointer to driver private data.

---
 grub-core/bus/usb/usbhub.c | 14 ++++++++++----
 include/grub/usb.h         |  5 +++++
 2 files changed, 15 insertions(+), 4 deletions(-)
grub-core/bus/usb/usbhub.c +10 -4
index df1913aee..63bee1d1a 100644
@@ -49,7 +49,9 @@ static grub_usb_controller_dev_t grub_usb_list;
 static grub_usb_device_t
 grub_usb_hub_add_dev (grub_usb_controller_t controller,
                       grub_usb_speed_t speed,
-                      int split_hubport, int split_hubaddr)
+                      int split_hubport, int split_hubaddr,
+                      int root_portno,
+                      grub_uint32_t route)
 {
   grub_usb_device_t dev;
   int i;
@@ -65,6 +67,8 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller,
   dev->speed = speed;
   dev->split_hubport = split_hubport;
   dev->split_hubaddr = split_hubaddr;
+  dev->root_port = root_portno;
+  dev->route = route;
 
   if (controller->dev->attach_dev) {
     err = controller->dev->attach_dev (controller, dev);
@@ -245,7 +249,7 @@ attach_root_port (struct grub_usb_hub *hub, int portno,
      and full/low speed device connected to OHCI/UHCI needs not
      transaction translation - e.g. hubport and hubaddr should be
      always none (zero) for any device connected to any root hub. */
-  dev = grub_usb_hub_add_dev (hub->controller, speed, 0, 0);
+  dev = grub_usb_hub_add_dev (hub->controller, speed, 0, 0, portno, 0);
   hub->controller->dev->pending_reset = 0;
   npending--;
   if (! dev)
@@ -673,10 +677,12 @@ poll_nonroot_hub (grub_usb_device_t dev)
 		    split_hubport = dev->split_hubport;
 		    split_hubaddr = dev->split_hubaddr;
 		  }
-		
+
+
 	      /* Add the device and assign a device address to it.  */
 	      next_dev = grub_usb_hub_add_dev (&dev->controller, speed,
-					       split_hubport, split_hubaddr);
+					       split_hubport, split_hubaddr, dev->root_port,
+					       dev->route << 4 | (i & 0xf));
 	      if (dev->controller.dev->pending_reset)
 		{
 		  dev->controller.dev->pending_reset = 0;
include/grub/usb.h +5 -0
index 5c5fb5ec4..5a3325b7d 100644
@@ -233,6 +233,11 @@ struct grub_usb_device
   int split_hubport;
 
   int split_hubaddr;
+
+  /* xHCI specific information */
+  int root_port;
+  grub_uint32_t route;
+  void *xhci_priv;
 };
 
 
Signature · 1 line
2.26.2
#6 lore raw

PATCH 6/6 grub-core/bus/usb: Add xhci support

In reply to Patrick Rudolph

Add support for xHCI USB controllers.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.

Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoff

Tested:
* Qemu system x86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* init Supermicro X11SSH-F
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* USB HID keyboard

TODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllers

---
 Makefile.am                  |    2 +-
 grub-core/Makefile.core.def  |    7 +
 grub-core/bus/usb/xhci-pci.c |  195 +++
 grub-core/bus/usb/xhci.c     | 2383 ++++++++++++++++++++++++++++++++++
 include/grub/usb.h           |    4 +
 5 files changed, 2590 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/bus/usb/xhci-pci.c
 create mode 100644 grub-core/bus/usb/xhci.c
Makefile.am +1 -1
index bf9c1ba64..81b9d1092 100644
@@ -425,7 +425,7 @@ if COND_i386_coreboot
 FS_PAYLOAD_MODULES ?= $(shell cat grub-core/fs.lst)
 default_payload.elf: grub-mkstandalone grub-mkimage FORCE
 	test -f $@ && rm $@ || true
-	pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O i386-coreboot -o $@ --modules='ahci pata ehci uhci ohci usb_keyboard usbms part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' --install-modules='ls linux search configfile normal cbtime cbls memrw iorw minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu $(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= --themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg
+	pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O i386-coreboot -o $@ --modules='ahci pata xhci ehci uhci ohci usb_keyboard usbms part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' --install-modules='ls linux search configfile normal cbtime cbls memrw iorw minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu $(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= --themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg
 endif
 
 endif
grub-core/Makefile.core.def +7 -0
index b5f47fc41..d5f550419 100644
@@ -644,6 +644,13 @@ module = {
   enable = arm_coreboot;
 };
 
+module = {
+  name = xhci;
+  common = bus/usb/xhci.c;
+  pci = bus/usb/xhci-pci.c;
+  enable = pci;
+};
+
 module = {
   name = pci;
   common = bus/pci.c;
grub-core/bus/usb/xhci-pci.c new file +195 -0

195 lines not on this page · raw message

grub-core/bus/usb/xhci.c new file +2383 -0

2,383 lines not on this page · raw message

include/grub/usb.h +4 -0
index 5a3325b7d..ac0f8c141 100644
@@ -334,6 +334,10 @@ grub_usb_cancel_transfer (grub_usb_transfer_t trans);
 void
 grub_ehci_init_device (volatile void *regs);
 void
+grub_xhci_init_device (volatile void *regs);
+void
 grub_ehci_pci_scan (void);
+void
+grub_xhci_pci_scan (void);
 
 #endif /* GRUB_USB_H */
Signature · 1 line
2.26.2