1 #include "devices/usb.h"
2 #include "devices/usb_hub.h"
3 #include "threads/synch.h"
4 #include "threads/malloc.h"
5 #include "devices/timer.h"
6 #include <kernel/bitmap.h>
10 #define ADDRS_PER_HOST 127 /* number of configurable addresses */
11 #define ADDR_DEFAULT 0 /* default configuration address */
12 #define ADDR_FIRST 1 /* first configurable address */
14 /** sometimes used for ''value' */
15 /* used in desc_header */
16 #define SETUP_DESC_DEVICE 1
17 #define SETUP_DESC_CONFIG 2
18 #define SETUP_DESC_STRING 3
19 #define SETUP_DESC_IFACE 4
20 #define SETUP_DESC_ENDPOINT 5
21 #define SETUP_DESC_DEVQUAL 6
22 #define SETUP_DESC_SPDCONFIG 7
23 #define SETUP_DESC_IFACEPWR 8
33 * More details can be found in chapter 9 of the usb1.1 spec
36 struct device_descriptor
38 struct desc_header hdr;
54 struct device_qualifier
65 struct config_descriptor
67 struct desc_header hdr;
68 uint16_t total_length;
76 struct interface_descriptor
78 struct desc_header hdr;
81 uint8_t num_endpoints;
83 uint8_t iface_subclass;
88 struct endpoint_descriptor
90 struct desc_header hdr;
91 /* end point address */
92 uint8_t endpoint_num:4;
109 #define USB_CLASS_HUB 0x09
112 struct usb_host *dev; /* host driver */
113 host_info info; /* private to host */
114 struct bitmap *usb_dev_addrs; /* addrs used on device */
115 struct list host_usb_devs; /* usb devices on host */
116 struct list_elem peers; /* other hosts on system */
121 struct usb_class *dev; /* class driver */
122 struct list usb_ifaces; /* usb devices on class */
123 struct list_elem peers;
126 static struct list usb_dev_list; /* list of all usb devices */
127 static struct list class_list; /* list of all classes */
128 static struct list host_list; /* list of all hosts on system */
129 static struct lock usb_sys_lock; /* big usb lock */
131 #define usb_lock() lock_acquire(&usb_sys_lock)
132 #define usb_unlock() lock_release(&usb_sys_lock)
134 static void usb_scan_devices (struct host *h);
135 static struct usb_dev *usb_configure_default (struct host *h);
136 static char *usb_get_string (struct usb_dev *d, int ndx);
137 static struct class *usb_get_class_by_id (int id);
138 static void usb_setup_dev_addr (struct usb_dev *dev);
139 static void usb_config_dev (struct usb_dev *dev, int config_val);
140 static int usb_load_config (struct usb_dev *dev, int idx, void *data,
142 static int usb_tx_all (struct usb_endpoint *eop, void *buf,
143 int max_bytes, int bailout, bool in);
144 static void usb_attach_interfaces (struct usb_dev *dev);
145 static void usb_apply_class_to_interfaces (struct class *c);
146 static size_t wchar_to_ascii (char *dst, const char *src);
148 extern void uhci_init (void);
149 extern void ehci_init (void);
154 list_init (&host_list);
155 list_init (&class_list);
156 list_init (&usb_dev_list);
157 lock_init (&usb_sys_lock);
162 printf ("Initializing EHCI\n");
164 printf ("Initializing UHCI\n");
168 /* add host controller device to usb layer */
170 usb_register_host (struct usb_host *uh, host_info info)
174 h = malloc (sizeof (struct host));
177 h->usb_dev_addrs = bitmap_create (ADDRS_PER_HOST);
178 list_init (&h->host_usb_devs);
181 list_push_back (&host_list, &h->peers);
184 usb_scan_devices (h);
188 usb_register_class (struct usb_class *uc)
194 /* check to make sure class is not in list */
195 if (usb_get_class_by_id (uc->class_id) != NULL)
202 /* add class to class list */
203 c = malloc (sizeof (struct class));
205 list_init (&c->usb_ifaces);
206 list_push_back (&class_list, &c->peers);
208 usb_apply_class_to_interfaces (c);
216 usb_scan_devices (struct host *h)
218 /* scan until there all devices using default pipe are configured */
220 printf ("USB: scanning devices...\n");
225 dev = usb_configure_default (h);
229 if (dev->ignore_device == false)
230 printf ("USB Device %d: %s (%s)\n",
231 dev->addr, dev->product, dev->manufacturer);
233 list_push_back (&h->host_usb_devs, &dev->host_peers);
234 list_push_back (&usb_dev_list, &dev->sys_peers);
235 usb_attach_interfaces (dev);
240 send_status (struct host *h, host_eop_info cfg_eop)
245 h->dev->set_toggle (cfg_eop, 1);
247 err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_OUT, NULL, 0, 0, &sz, true);
249 printf ("error %d in status transaction\n", err);
253 static struct usb_dev *
254 usb_configure_default (struct host *h)
257 struct usb_setup_pkt sp;
259 struct device_descriptor *dd;
261 host_eop_info cfg_eop;
262 bool ignore_device = false;
266 hi = h->dev->create_dev_channel (h->info, ADDR_DEFAULT, USB_VERSION_1_1);
267 cfg_eop = h->dev->create_eop (hi, 0, 64);
269 /* Get first 8 bytes of device descriptor. */
270 sp.recipient = USB_SETUP_RECIP_DEV;
271 sp.type = USB_SETUP_TYPE_STD;
273 sp.request = REQ_STD_GET_DESC;
274 sp.value = SETUP_DESC_DEVICE << 8;
279 h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
281 if (err != USB_HOST_ERR_NONE)
285 memset (dd, 0, sizeof (data));
287 err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data, 0, 8, &sz, true);
288 if (err != USB_HOST_ERR_NONE)
291 err = send_status (h, cfg_eop);
292 if (err != USB_HOST_ERR_NONE)
295 if (dd->usb_spec == USB_VERSION_1_0)
297 /* USB 1.0 devices have strict schedule requirements not yet supported */
298 printf ("USB 1.0 device detected - skipping\n");
302 /* Now we know the max packet size. */
303 if (dd->max_pktsz != 8 && dd->max_pktsz != 16
304 && dd->max_pktsz != 32 && dd->max_pktsz != 64)
307 h->dev->remove_eop (cfg_eop);
308 cfg_eop = h->dev->create_eop (hi, 0, dd->max_pktsz);
310 /* Get the whole descriptor. */
311 sp.length = sizeof *dd;
313 h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
315 if (err != USB_HOST_ERR_NONE)
319 while (txed < sizeof *dd)
321 err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data + txed, 0,
322 sizeof *dd - txed, &sz, true);
328 err = send_status (h, cfg_eop);
329 if (err != USB_HOST_ERR_NONE)
332 if (dd->num_configs == 0)
333 ignore_device = true;
335 /* device exists - create device structure */
336 dev = malloc (sizeof (struct usb_dev));
337 memset (dev, 0, sizeof (struct usb_dev));
338 list_init (&dev->interfaces);
340 dev->ignore_device = ignore_device;
342 dev->h_cfg_eop = cfg_eop;
344 dev->cfg_eop.h_eop = cfg_eop;
346 dev->usb_version = dd->usb_spec;
347 dev->default_iface.class_id = dd->dev_class;
348 dev->default_iface.subclass_id = dd->dev_subclass;
349 dev->default_iface.iface_num = 0;
350 dev->default_iface.proto = dd->dev_proto;
351 dev->default_iface.class = NULL;
352 dev->default_iface.c_info = NULL;
353 dev->default_iface.dev = dev;
355 dev->cfg_eop.iface = &dev->default_iface;
356 dev->cfg_eop.max_pkt = dd->max_pktsz;
357 dev->cfg_eop.eop = 0;
361 dev->vendor_id = dd->vendor_id;
362 dev->product_id = dd->product_id;
363 dev->device_id = dd->device_id;
365 if (ignore_device == false)
367 dev->product = usb_get_string (dev, dd->product);
368 dev->manufacturer = usb_get_string (dev, dd->manufacturer);
372 /* read in configuration data if there are configurations available */
373 /* (which there should be...) */
374 if (dd->num_configs > 0 && ignore_device == false)
376 config_val = usb_load_config (dev, 0, data, sizeof (data));
380 ("USB: Invalid configuration value %d on '%s (%s)' (%x,%x,%x)\n",
381 config_val, dev->product, dev->manufacturer, dev->vendor_id,
382 dev->product_id, dev->device_id);
386 usb_setup_dev_addr (dev);
388 usb_config_dev (dev, (config_val < 0) ? 1 : config_val);
393 h->dev->remove_eop (cfg_eop);
394 h->dev->remove_dev_channel (hi);
399 * Load in data for 'idx' configuration into device structure
400 * XXX support multiple configurations
403 usb_load_config (struct usb_dev *dev, int idx, void *data, int dsz)
405 struct usb_setup_pkt sp;
406 struct config_descriptor *cd;
410 int config_val, err, sz;
414 cfg = dev->h_cfg_eop;
416 sp.recipient = USB_SETUP_RECIP_DEV;
417 sp.type = USB_SETUP_TYPE_STD;
419 sp.request = REQ_STD_GET_DESC;
420 sp.value = SETUP_DESC_CONFIG << 8 | idx;
425 err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), &sz, true);
426 if (err != USB_HOST_ERR_NONE)
428 printf ("USB: Could not setup GET descriptor\n");
432 sz = usb_tx_all (&dev->cfg_eop, cd, dsz,
433 sizeof (struct config_descriptor), true);
434 if (sz < sizeof (struct config_descriptor))
436 printf ("USB: Did not rx GET descriptor (%d bytes, expected %d)\n", sz,
437 sizeof (struct config_descriptor));
441 if (sz == 0 || cd->hdr.type != SETUP_DESC_CONFIG)
443 printf ("USB: Invalid descriptor\n");
447 if (sz < cd->total_length)
448 sz += usb_tx_all (&dev->cfg_eop, data+sz, dsz, cd->total_length - sz, true);
450 send_status (h, cfg);
452 dev->pwr = cd->max_power;
454 /* interface information comes right after config data */
455 /* scan interfaces */
456 ptr = data + sizeof (struct config_descriptor);
458 for (i = 0; i < cd->num_ifaces; i++)
460 struct interface_descriptor *iface;
461 struct usb_iface *ui;
465 if (iface->hdr.type != SETUP_DESC_IFACE)
467 hex_dump (0, iface, 64, false);
468 PANIC ("Expected %d, found %d\n", SETUP_DESC_IFACE,
472 ui = malloc (sizeof (struct usb_iface));
473 ui->class_id = iface->iface_class;
474 ui->subclass_id = iface->iface_subclass;
475 ui->iface_num = iface->iface_num;
476 ui->proto = iface->iface_proto;
480 list_init (&ui->endpoints);
482 /* endpoint data comes after interfaces */
484 ptr += sizeof (struct interface_descriptor);
485 for (j = 0; j < iface->num_endpoints;
486 j++, ptr += sizeof (struct endpoint_descriptor))
488 struct usb_endpoint *ue;
489 struct endpoint_descriptor *ed;
493 ue = malloc (sizeof (struct usb_endpoint));
494 ue->eop = ed->endpoint_num;
495 ue->direction = ed->direction;
496 ue->attr = ed->transfer;
497 ue->max_pkt = ed->max_pktsz;
498 ue->interval = ed->interval;
500 ue->h_eop = h->dev->create_eop (dev->h_dev, ue->eop, ue->max_pkt);
502 list_push_back (&ui->endpoints, &ue->peers);
505 list_push_back (&dev->interfaces, &ui->peers);
508 config_val = cd->config_val;
514 * Set USB device configuration to desired configuration value
517 usb_config_dev (struct usb_dev *dev, int config_val)
519 struct usb_setup_pkt sp;
525 cfg = dev->h_cfg_eop;
527 sp.recipient = USB_SETUP_RECIP_DEV;
528 sp.type = USB_SETUP_TYPE_STD;
530 sp.request = REQ_STD_SET_CONFIG;
531 sp.value = config_val;
534 err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
535 if (err != USB_HOST_ERR_NONE)
536 PANIC ("USB: Config setup packet did not tx\n");
538 err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
539 if (err != USB_HOST_ERR_NONE)
540 PANIC ("USB: Could not configure device!\n");
544 * Set a device address to something other than the default pipe
547 usb_setup_dev_addr (struct usb_dev *dev)
549 struct usb_setup_pkt sp;
554 ASSERT (dev->addr == 0);
557 cfg = dev->h_cfg_eop;
559 dev->addr = bitmap_scan_and_flip (h->usb_dev_addrs, 1, 1, false);
561 sp.recipient = USB_SETUP_RECIP_DEV;
562 sp.type = USB_SETUP_TYPE_STD;
564 sp.request = REQ_STD_SET_ADDRESS;
565 sp.value = dev->addr;
569 h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
570 if (err != USB_HOST_ERR_NONE)
572 PANIC ("USB: WHOOPS!!!!!!!\n");
574 err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
575 if (err != USB_HOST_ERR_NONE)
577 PANIC ("USB: Error on setting device address (err = %d)\n", err);
580 h->dev->modify_dev_channel (dev->h_dev, dev->addr, dev->usb_version);
583 #define MAX_USB_STR 128
584 /* read string by string descriptor index from usb device */
586 usb_get_string (struct usb_dev *udev, int ndx)
588 struct usb_setup_pkt sp;
589 char str[MAX_USB_STR];
593 sp.recipient = USB_SETUP_RECIP_DEV;
594 sp.type = USB_SETUP_TYPE_STD;
596 sp.request = REQ_STD_GET_DESC;
597 sp.value = (SETUP_DESC_STRING << 8) | ndx;
599 sp.length = MAX_USB_STR;
601 if (udev->host->dev->tx_pkt (udev->h_cfg_eop, USB_TOKEN_SETUP,
602 &sp, 0, sizeof (sp), NULL, true)
603 != USB_HOST_ERR_NONE)
606 sz = usb_tx_all (&udev->cfg_eop, &str, MAX_USB_STR, 2, true);
608 usb_tx_all (&udev->cfg_eop, str + sz, (uint8_t) (str[0]) - sz, 0, true);
610 /* string failed to tx? */
614 send_status (udev->host, udev->h_cfg_eop);
616 /* some devices don't respect the string descriptor length value (str[0])
617 * and just send any old value they want, so we can't use it */
619 str[(sz < (MAX_USB_STR - 1)) ? (sz) : (MAX_USB_STR - 1)] = '\0';
621 /* usb uses wchars for strings, convert to ASCII */
622 wchar_to_ascii (str, str + 2);
623 ret = malloc (strlen (str) + 1);
624 strlcpy (ret, str, MAX_USB_STR);
628 static struct class *
629 usb_get_class_by_id (int id)
631 struct list_elem *li;
633 li = list_begin (&class_list);
634 while (li != list_end (&class_list))
637 c = list_entry (li, struct class, peers);
638 if (c->dev->class_id == id)
647 * Asssociate interfaces on a device with their respective device classes
650 usb_attach_interfaces (struct usb_dev *dev)
653 struct list_elem *li;
654 li = list_begin (&dev->interfaces);
655 while (li != list_end (&dev->interfaces))
658 struct usb_iface *ui;
660 ui = list_entry (li, struct usb_iface, peers);
662 cl = usb_get_class_by_id (ui->class_id);
664 /* no matching class? try next interface */
668 ui->c_info = cl->dev->attached (ui);
669 /* did class driver initialize it OK? */
670 if (ui->c_info != NULL)
672 /* yes, add to list of usable interfaces */
673 list_push_back (&cl->usb_ifaces, &ui->class_peers);
680 * Scan all interfaces for interfaces that match class's class id
681 * Attach interfaces that match
684 usb_apply_class_to_interfaces (struct class *c)
686 struct list_elem *li;
688 li = list_begin (&usb_dev_list);
689 while (li != list_end (&usb_dev_list))
692 struct list_elem *lii;
694 ud = list_entry (li, struct usb_dev, sys_peers);
696 lii = list_begin (&ud->interfaces);
697 while (lii != list_end (&ud->interfaces))
699 struct usb_iface *ui;
701 ui = list_entry (lii, struct usb_iface, peers);
703 lii = list_next (lii);
704 if (ui->class_id != c->dev->class_id)
707 ui->c_info = c->dev->attached (ui);
708 if (ui->c_info == NULL)
711 list_push_back (&c->usb_ifaces, &ui->class_peers);
720 usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx)
726 ASSERT (eop != NULL);
727 h = eop->iface->dev->host;
729 if (eop->direction == 0)
730 token = USB_TOKEN_OUT;
732 token = USB_TOKEN_IN;
734 err = h->dev->tx_pkt (eop->h_eop, token, buf, sz, sz, tx, true);
740 usb_dev_setup (struct usb_endpoint *eop, bool in,
741 struct usb_setup_pkt *s, void *buf, int sz)
746 ASSERT (eop != NULL);
747 h = eop->iface->dev->host;
749 err = h->dev->tx_pkt (eop->h_eop, USB_TOKEN_SETUP, s,
750 0, sizeof (struct usb_setup_pkt), NULL, true);
751 if (err != USB_HOST_ERR_NONE)
753 printf ("usb_dev_setup: failed\n");
757 err = h->dev->tx_pkt (eop->h_eop, (in) ? USB_TOKEN_IN : USB_TOKEN_OUT,
758 buf, 0, sz, &sz, true);
763 /** convert a wchar string to ascii in place */
765 wchar_to_ascii (char *dst, const char *src)
768 for (sz = 0; src[sz * 2] != '\0'; sz++)
770 dst[sz] = src[sz * 2];
776 /* this is used for variable sized transfers where a normal bulk transfer would
777 probably fail, since it expects some minimum size - we just want to
778 read/write as much to the pipe as we can
781 usb_tx_all (struct usb_endpoint *eop, void *buf,
782 int max_bytes, int bailout, bool in)
796 token = (in) ? USB_TOKEN_IN : USB_TOKEN_OUT;
797 h = eop->iface->dev->host;
798 while (txed < max_bytes && txed < bailout)
802 err = h->dev->tx_pkt (eop->h_eop, token,
803 buf + txed, 0, max_bytes - txed, &sz, true);
807 /* this should probably be using short packet detection */
808 if (err != USB_HOST_ERR_NONE || sz != prev_sz || sz == 0)