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;
267 hi = h->dev->create_dev_channel (h->info, ADDR_DEFAULT, USB_VERSION_1_1);
268 cfg_eop = h->dev->create_eop (hi, 0, 64);
270 /* Get first 8 bytes of device descriptor. */
271 sp.recipient = USB_SETUP_RECIP_DEV;
272 sp.type = USB_SETUP_TYPE_STD;
274 sp.request = REQ_STD_GET_DESC;
275 sp.value = SETUP_DESC_DEVICE << 8;
280 h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
282 if (err != USB_HOST_ERR_NONE)
286 memset (dd, 0, sizeof (data));
288 err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data, 0, 8, &sz, true);
289 if (err != USB_HOST_ERR_NONE)
292 err = send_status (h, cfg_eop);
293 if (err != USB_HOST_ERR_NONE)
296 if (dd->usb_spec == USB_VERSION_1_0)
298 /* USB 1.0 devices have strict schedule requirements not yet supported */
299 printf ("USB 1.0 device detected - skipping\n");
303 /* Now we know the max packet size. */
304 if (dd->max_pktsz != 8 && dd->max_pktsz != 16
305 && dd->max_pktsz != 32 && dd->max_pktsz != 64)
308 h->dev->remove_eop (cfg_eop);
309 cfg_eop = h->dev->create_eop (hi, 0, dd->max_pktsz);
311 /* Get the whole descriptor. */
312 sp.length = sizeof *dd;
314 h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
316 if (err != USB_HOST_ERR_NONE)
320 while (txed < sizeof *dd)
322 err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data + txed, 0,
323 sizeof *dd - txed, &sz, true);
329 err = send_status (h, cfg_eop);
330 if (err != USB_HOST_ERR_NONE)
333 if (dd->num_configs == 0)
334 ignore_device = true;
336 /* device exists - create device structure */
337 dev = malloc (sizeof (struct usb_dev));
338 memset (dev, 0, sizeof (struct usb_dev));
339 list_init (&dev->interfaces);
341 dev->ignore_device = ignore_device;
343 dev->h_cfg_eop = cfg_eop;
345 dev->cfg_eop.h_eop = cfg_eop;
347 dev->usb_version = dd->usb_spec;
348 dev->default_iface.class_id = dd->dev_class;
349 dev->default_iface.subclass_id = dd->dev_subclass;
350 dev->default_iface.iface_num = 0;
351 dev->default_iface.proto = dd->dev_proto;
352 dev->default_iface.class = NULL;
353 dev->default_iface.c_info = NULL;
354 dev->default_iface.dev = dev;
356 dev->cfg_eop.iface = &dev->default_iface;
357 dev->cfg_eop.max_pkt = dd->max_pktsz;
358 dev->cfg_eop.eop = 0;
362 dev->vendor_id = dd->vendor_id;
363 dev->product_id = dd->product_id;
364 dev->device_id = dd->device_id;
366 if (ignore_device == false)
368 dev->product = usb_get_string (dev, dd->product);
369 dev->manufacturer = usb_get_string (dev, dd->manufacturer);
373 /* read in configuration data if there are configurations available */
374 /* (which there should be...) */
375 if (dd->num_configs > 0 && ignore_device == false)
377 config_val = usb_load_config (dev, 0, data, sizeof (data));
381 ("USB: Invalid configuration value %d on '%s (%s)' (%x,%x,%x)\n",
382 config_val, dev->product, dev->manufacturer, dev->vendor_id,
383 dev->product_id, dev->device_id);
387 usb_setup_dev_addr (dev);
389 usb_config_dev (dev, (config_val < 0) ? 1 : config_val);
394 h->dev->remove_eop (cfg_eop);
395 h->dev->remove_dev_channel (hi);
400 * Load in data for 'idx' configuration into device structure
401 * XXX support multiple configurations
404 usb_load_config (struct usb_dev *dev, int idx, void *data, int dsz)
406 struct usb_setup_pkt sp;
407 struct config_descriptor *cd;
411 int config_val, err, sz;
415 cfg = dev->h_cfg_eop;
417 sp.recipient = USB_SETUP_RECIP_DEV;
418 sp.type = USB_SETUP_TYPE_STD;
420 sp.request = REQ_STD_GET_DESC;
421 sp.value = SETUP_DESC_CONFIG << 8 | idx;
426 err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), &sz, true);
427 if (err != USB_HOST_ERR_NONE)
429 printf ("USB: Could not setup GET descriptor\n");
433 sz = usb_tx_all (&dev->cfg_eop, cd, dsz,
434 sizeof (struct config_descriptor), true);
435 if (sz < (int) sizeof (struct config_descriptor))
437 printf ("USB: Did not rx GET descriptor (%d bytes, expected %d)\n", sz,
438 sizeof (struct config_descriptor));
442 if (sz == 0 || cd->hdr.type != SETUP_DESC_CONFIG)
444 printf ("USB: Invalid descriptor\n");
448 if (sz < cd->total_length)
449 sz += usb_tx_all (&dev->cfg_eop, data+sz, dsz, cd->total_length - sz, true);
451 send_status (h, cfg);
453 dev->pwr = cd->max_power;
455 /* interface information comes right after config data */
456 /* scan interfaces */
457 ptr = data + sizeof (struct config_descriptor);
459 for (i = 0; i < cd->num_ifaces; i++)
461 struct interface_descriptor *iface;
462 struct usb_iface *ui;
466 if (iface->hdr.type != SETUP_DESC_IFACE)
468 hex_dump (0, iface, 64, false);
469 PANIC ("Expected %d, found %d\n", SETUP_DESC_IFACE,
473 ui = malloc (sizeof (struct usb_iface));
474 ui->class_id = iface->iface_class;
475 ui->subclass_id = iface->iface_subclass;
476 ui->iface_num = iface->iface_num;
477 ui->proto = iface->iface_proto;
481 list_init (&ui->endpoints);
483 /* endpoint data comes after interfaces */
485 ptr += sizeof (struct interface_descriptor);
486 for (j = 0; j < iface->num_endpoints;
487 j++, ptr += sizeof (struct endpoint_descriptor))
489 struct usb_endpoint *ue;
490 struct endpoint_descriptor *ed;
494 ue = malloc (sizeof (struct usb_endpoint));
495 ue->eop = ed->endpoint_num;
496 ue->direction = ed->direction;
497 ue->attr = ed->transfer;
498 ue->max_pkt = ed->max_pktsz;
499 ue->interval = ed->interval;
501 ue->h_eop = h->dev->create_eop (dev->h_dev, ue->eop, ue->max_pkt);
503 list_push_back (&ui->endpoints, &ue->peers);
506 list_push_back (&dev->interfaces, &ui->peers);
509 config_val = cd->config_val;
515 * Set USB device configuration to desired configuration value
518 usb_config_dev (struct usb_dev *dev, int config_val)
520 struct usb_setup_pkt sp;
526 cfg = dev->h_cfg_eop;
528 sp.recipient = USB_SETUP_RECIP_DEV;
529 sp.type = USB_SETUP_TYPE_STD;
531 sp.request = REQ_STD_SET_CONFIG;
532 sp.value = config_val;
535 err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
536 if (err != USB_HOST_ERR_NONE)
537 PANIC ("USB: Config setup packet did not tx\n");
539 err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
540 if (err != USB_HOST_ERR_NONE)
541 PANIC ("USB: Could not configure device!\n");
545 * Set a device address to something other than the default pipe
548 usb_setup_dev_addr (struct usb_dev *dev)
550 struct usb_setup_pkt sp;
555 ASSERT (dev->addr == 0);
558 cfg = dev->h_cfg_eop;
560 dev->addr = bitmap_scan_and_flip (h->usb_dev_addrs, 1, 1, false);
562 sp.recipient = USB_SETUP_RECIP_DEV;
563 sp.type = USB_SETUP_TYPE_STD;
565 sp.request = REQ_STD_SET_ADDRESS;
566 sp.value = dev->addr;
570 h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
571 if (err != USB_HOST_ERR_NONE)
573 PANIC ("USB: WHOOPS!!!!!!!\n");
575 err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
576 if (err != USB_HOST_ERR_NONE)
578 PANIC ("USB: Error on setting device address (err = %d)\n", err);
581 h->dev->modify_dev_channel (dev->h_dev, dev->addr, dev->usb_version);
584 #define MAX_USB_STR 128
585 /* read string by string descriptor index from usb device */
587 usb_get_string (struct usb_dev *udev, int ndx)
589 struct usb_setup_pkt sp;
590 char str[MAX_USB_STR];
594 sp.recipient = USB_SETUP_RECIP_DEV;
595 sp.type = USB_SETUP_TYPE_STD;
597 sp.request = REQ_STD_GET_DESC;
598 sp.value = (SETUP_DESC_STRING << 8) | ndx;
600 sp.length = MAX_USB_STR;
602 if (udev->host->dev->tx_pkt (udev->h_cfg_eop, USB_TOKEN_SETUP,
603 &sp, 0, sizeof (sp), NULL, true)
604 != USB_HOST_ERR_NONE)
607 sz = usb_tx_all (&udev->cfg_eop, &str, MAX_USB_STR, 2, true);
609 usb_tx_all (&udev->cfg_eop, str + sz, (uint8_t) (str[0]) - sz, 0, true);
611 /* string failed to tx? */
615 send_status (udev->host, udev->h_cfg_eop);
617 /* some devices don't respect the string descriptor length value (str[0])
618 * and just send any old value they want, so we can't use it */
620 str[(sz < (MAX_USB_STR - 1)) ? (sz) : (MAX_USB_STR - 1)] = '\0';
622 /* usb uses wchars for strings, convert to ASCII */
623 wchar_to_ascii (str, str + 2);
624 ret = malloc (strlen (str) + 1);
625 strlcpy (ret, str, MAX_USB_STR);
629 static struct class *
630 usb_get_class_by_id (int id)
632 struct list_elem *li;
634 li = list_begin (&class_list);
635 while (li != list_end (&class_list))
638 c = list_entry (li, struct class, peers);
639 if (c->dev->class_id == id)
648 * Asssociate interfaces on a device with their respective device classes
651 usb_attach_interfaces (struct usb_dev *dev)
654 struct list_elem *li;
655 li = list_begin (&dev->interfaces);
656 while (li != list_end (&dev->interfaces))
659 struct usb_iface *ui;
661 ui = list_entry (li, struct usb_iface, peers);
663 cl = usb_get_class_by_id (ui->class_id);
665 /* no matching class? try next interface */
669 ui->c_info = cl->dev->attached (ui);
670 /* did class driver initialize it OK? */
671 if (ui->c_info != NULL)
673 /* yes, add to list of usable interfaces */
674 list_push_back (&cl->usb_ifaces, &ui->class_peers);
681 * Scan all interfaces for interfaces that match class's class id
682 * Attach interfaces that match
685 usb_apply_class_to_interfaces (struct class *c)
687 struct list_elem *li;
689 li = list_begin (&usb_dev_list);
690 while (li != list_end (&usb_dev_list))
693 struct list_elem *lii;
695 ud = list_entry (li, struct usb_dev, sys_peers);
697 lii = list_begin (&ud->interfaces);
698 while (lii != list_end (&ud->interfaces))
700 struct usb_iface *ui;
702 ui = list_entry (lii, struct usb_iface, peers);
704 lii = list_next (lii);
705 if (ui->class_id != c->dev->class_id)
708 ui->c_info = c->dev->attached (ui);
709 if (ui->c_info == NULL)
712 list_push_back (&c->usb_ifaces, &ui->class_peers);
721 usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx)
727 ASSERT (eop != NULL);
728 h = eop->iface->dev->host;
730 if (eop->direction == 0)
731 token = USB_TOKEN_OUT;
733 token = USB_TOKEN_IN;
735 err = h->dev->tx_pkt (eop->h_eop, token, buf, sz, sz, tx, true);
741 usb_dev_setup (struct usb_endpoint *eop, bool in,
742 struct usb_setup_pkt *s, void *buf, int sz)
747 ASSERT (eop != NULL);
748 h = eop->iface->dev->host;
750 err = h->dev->tx_pkt (eop->h_eop, USB_TOKEN_SETUP, s,
751 0, sizeof (struct usb_setup_pkt), NULL, true);
752 if (err != USB_HOST_ERR_NONE)
754 printf ("usb_dev_setup: failed\n");
758 err = h->dev->tx_pkt (eop->h_eop, (in) ? USB_TOKEN_IN : USB_TOKEN_OUT,
759 buf, 0, sz, &sz, true);
764 /** convert a wchar string to ascii in place */
766 wchar_to_ascii (char *dst, const char *src)
769 for (sz = 0; src[sz * 2] != '\0'; sz++)
771 dst[sz] = src[sz * 2];
777 /* this is used for variable sized transfers where a normal bulk transfer would
778 probably fail, since it expects some minimum size - we just want to
779 read/write as much to the pipe as we can
782 usb_tx_all (struct usb_endpoint *eop, void *buf,
783 int max_bytes, int bailout, bool in)
797 token = (in) ? USB_TOKEN_IN : USB_TOKEN_OUT;
798 h = eop->iface->dev->host;
799 while (txed < max_bytes && txed < bailout)
803 err = h->dev->tx_pkt (eop->h_eop, token,
804 buf + txed, 0, max_bytes - txed, &sz, true);
808 /* this should probably be using short packet detection */
809 if (err != USB_HOST_ERR_NONE || sz != prev_sz || sz == 0)