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;
53 struct device_qualifier
64 struct config_descriptor
66 struct desc_header hdr;
67 uint16_t total_length;
75 struct interface_descriptor
77 struct desc_header hdr;
80 uint8_t num_endpoints;
82 uint8_t iface_subclass;
87 struct endpoint_descriptor
89 struct desc_header hdr;
90 /* end point address */
91 uint8_t endpoint_num:4;
108 #define USB_CLASS_HUB 0x09
111 struct usb_host *dev; /* host driver */
112 host_info info; /* private to host */
113 struct bitmap *usb_dev_addrs; /* addrs used on device */
114 struct list host_usb_devs; /* usb devices on host */
115 struct list_elem peers; /* other hosts on system */
120 struct usb_class *dev; /* class driver */
121 struct list usb_ifaces; /* usb devices on class */
122 struct list_elem peers;
125 static struct list usb_dev_list; /* list of all usb devices */
126 static struct list class_list; /* list of all classes */
127 static struct list host_list; /* list of all hosts on system */
128 static struct lock usb_sys_lock; /* big usb lock */
130 #define usb_lock() lock_acquire(&usb_sys_lock)
131 #define usb_unlock() lock_release(&usb_sys_lock)
133 static void usb_scan_devices (struct host *h);
134 static struct usb_dev *usb_configure_default (struct host *h);
135 static char *usb_get_string (struct usb_dev *d, int ndx);
136 static struct class *usb_get_class_by_id (int id);
137 static void usb_setup_dev_addr (struct usb_dev *dev);
138 static void usb_config_dev (struct usb_dev *dev, int config_val);
139 static int usb_load_config (struct usb_dev *dev, int idx, void *data,
141 static int usb_tx_all (struct usb_endpoint *eop, void *buf,
142 int max_bytes, int bailout, bool in);
143 static void usb_attach_interfaces (struct usb_dev *dev);
144 static void usb_apply_class_to_interfaces (struct class *c);
145 static size_t wchar_to_ascii (char *dst, const char *src);
147 extern void uhci_init (void);
148 extern void ehci_init (void);
153 list_init (&host_list);
154 list_init (&class_list);
155 list_init (&usb_dev_list);
156 lock_init (&usb_sys_lock);
161 printf ("Initializing EHCI\n");
163 printf ("Initializing UHCI\n");
167 /* add host controller device to usb layer */
169 usb_register_host (struct usb_host *uh, host_info info)
173 h = malloc (sizeof (struct host));
176 h->usb_dev_addrs = bitmap_create (ADDRS_PER_HOST);
177 list_init (&h->host_usb_devs);
180 list_push_back (&host_list, &h->peers);
183 usb_scan_devices (h);
187 usb_register_class (struct usb_class *uc)
193 /* check to make sure class is not in list */
194 if (usb_get_class_by_id (uc->class_id) != NULL)
201 /* add class to class list */
202 c = malloc (sizeof (struct class));
204 list_init (&c->usb_ifaces);
205 list_push_back (&class_list, &c->peers);
207 usb_apply_class_to_interfaces (c);
215 usb_scan_devices (struct host *h)
217 /* scan until there all devices using default pipe are configured */
219 printf ("USB: scanning devices...\n");
224 dev = usb_configure_default (h);
228 if (dev->ignore_device == false)
229 printf ("USB Device %d: %s (%s)\n",
230 dev->addr, dev->product, dev->manufacturer);
232 list_push_back (&h->host_usb_devs, &dev->host_peers);
233 list_push_back (&usb_dev_list, &dev->sys_peers);
234 usb_attach_interfaces (dev);
239 static struct usb_dev *
240 usb_configure_default (struct host *h)
243 struct usb_setup_pkt sp;
245 struct device_descriptor *dd;
247 host_eop_info cfg_eop;
248 bool ignore_device = false;
252 hi = h->dev->create_dev_channel (h->info, ADDR_DEFAULT, USB_VERSION_1_1);
253 cfg_eop = h->dev->create_eop (hi, 0, 64);
255 /* determine device descriptor */
256 sp.recipient = USB_SETUP_RECIP_DEV;
257 sp.type = USB_SETUP_TYPE_STD;
259 sp.request = REQ_STD_GET_DESC;
260 sp.value = SETUP_DESC_DEVICE << 8;
262 sp.length = sizeof (struct device_descriptor);
265 h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
267 /* did it timeout? - no device */
268 if (err != USB_HOST_ERR_NONE)
270 h->dev->remove_eop (cfg_eop);
271 h->dev->remove_dev_channel (hi);
276 memset (dd, 0, sizeof (data));
278 h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data, 0, sizeof (data) - txed, &sz,
281 /* some devices only send 16 bytes for the device descriptor -
282 * they wind up with a NAK, waiting for another command to be issued
284 * if it's a 16 byte max packet device, we just take the first 16 bytes */
286 if (dd->usb_spec == USB_VERSION_1_0)
288 /* USB 1.0 devices have strict schedule requirements not yet supported */
289 printf ("USB 1.0 device detected - skipping\n");
293 /* read in the full descriptor */
298 tx_pkt (cfg_eop, USB_TOKEN_IN, data + txed, 0,
299 sizeof (data) - txed, &sz, true) != USB_HOST_ERR_NONE)
304 if (dd->num_configs == 0)
305 ignore_device = true;
307 h->dev->remove_eop (cfg_eop);
308 cfg_eop = h->dev->create_eop (hi, 0, dd->max_pktsz);
310 /* device exists - create device structure */
311 dev = malloc (sizeof (struct usb_dev));
312 memset (dev, 0, sizeof (struct usb_dev));
313 list_init (&dev->interfaces);
315 dev->ignore_device = ignore_device;
317 dev->h_cfg_eop = cfg_eop;
319 dev->cfg_eop.h_eop = cfg_eop;
321 dev->usb_version = dd->usb_spec;
322 dev->default_iface.class_id = dd->dev_class;
323 dev->default_iface.subclass_id = dd->dev_subclass;
324 dev->default_iface.iface_num = 0;
325 dev->default_iface.proto = dd->dev_proto;
326 dev->default_iface.class = NULL;
327 dev->default_iface.c_info = NULL;
328 dev->default_iface.dev = dev;
330 dev->cfg_eop.iface = &dev->default_iface;
331 dev->cfg_eop.max_pkt = dd->max_pktsz;
332 dev->cfg_eop.eop = 0;
336 dev->vendor_id = dd->vendor_id;
337 dev->product_id = dd->product_id;
338 dev->device_id = dd->device_id;
340 if (ignore_device == false)
342 dev->product = usb_get_string (dev, dd->product);
343 dev->manufacturer = usb_get_string (dev, dd->manufacturer);
347 /* read in configuration data if there are configurations available */
348 /* (which there should be...) */
349 if (dd->num_configs > 0 && ignore_device == false)
351 config_val = usb_load_config (dev, 0, data, sizeof (data));
355 ("USB: Invalid configuration value %d on '%s (%s)' (%x,%x,%x)\n",
356 config_val, dev->product, dev->manufacturer, dev->vendor_id,
357 dev->product_id, dev->device_id);
361 usb_setup_dev_addr (dev);
363 usb_config_dev (dev, (config_val < 0) ? 1 : config_val);
369 * Load in data for 'idx' configuration into device structure
370 * XXX support multiple configurations
373 usb_load_config (struct usb_dev *dev, int idx, void *data, int dsz)
375 struct usb_setup_pkt sp;
376 struct config_descriptor *cd;
380 int config_val, err, sz;
384 cfg = dev->h_cfg_eop;
386 sp.recipient = USB_SETUP_RECIP_DEV;
387 sp.type = USB_SETUP_TYPE_STD;
389 sp.request = REQ_STD_GET_DESC;
390 sp.value = SETUP_DESC_CONFIG << 8 | idx;
395 err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), &sz, true);
396 if (err != USB_HOST_ERR_NONE)
398 printf ("USB: Could not setup GET descriptor\n");
402 sz = usb_tx_all (&dev->cfg_eop, cd, dsz,
403 sizeof (struct config_descriptor), true);
404 if (sz < sizeof (struct config_descriptor))
406 printf ("USB: Did not rx GET descriptor (%d bytes, expected %d)\n", sz,
407 sizeof (struct config_descriptor));
411 if (sz == 0 || cd->hdr.type != SETUP_DESC_CONFIG)
413 printf ("USB: Invalid descriptor\n");
417 if (sz < cd->total_length)
418 sz += usb_tx_all (&dev->cfg_eop, data+sz, dsz, cd->total_length - sz, true);
420 dev->pwr = cd->max_power;
422 /* interface information comes right after config data */
423 /* scan interfaces */
424 ptr = data + sizeof (struct config_descriptor);
426 for (i = 0; i < cd->num_ifaces; i++)
428 struct interface_descriptor *iface;
429 struct usb_iface *ui;
433 if (iface->hdr.type != SETUP_DESC_IFACE)
435 hex_dump (0, iface, 64, false);
436 PANIC ("Expected %d, found %d\n", SETUP_DESC_IFACE,
440 ui = malloc (sizeof (struct usb_iface));
441 ui->class_id = iface->iface_class;
442 ui->subclass_id = iface->iface_subclass;
443 ui->iface_num = iface->iface_num;
444 ui->proto = iface->iface_proto;
448 list_init (&ui->endpoints);
450 /* endpoint data comes after interfaces */
452 ptr += sizeof (struct interface_descriptor);
453 for (j = 0; j < iface->num_endpoints;
454 j++, ptr += sizeof (struct endpoint_descriptor))
456 struct usb_endpoint *ue;
457 struct endpoint_descriptor *ed;
461 ue = malloc (sizeof (struct usb_endpoint));
462 ue->eop = ed->endpoint_num;
463 ue->direction = ed->direction;
464 ue->attr = ed->transfer;
465 ue->max_pkt = ed->max_pktsz;
466 ue->interval = ed->interval;
468 ue->h_eop = h->dev->create_eop (dev->h_dev, ue->eop, ue->max_pkt);
470 list_push_back (&ui->endpoints, &ue->peers);
473 list_push_back (&dev->interfaces, &ui->peers);
476 config_val = cd->config_val;
482 * Set USB device configuration to desired configuration value
485 usb_config_dev (struct usb_dev *dev, int config_val)
487 struct usb_setup_pkt sp;
493 cfg = dev->h_cfg_eop;
495 sp.recipient = USB_SETUP_RECIP_DEV;
496 sp.type = USB_SETUP_TYPE_STD;
498 sp.request = REQ_STD_SET_CONFIG;
499 sp.value = config_val;
502 err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
503 if (err != USB_HOST_ERR_NONE)
504 PANIC ("USB: Config setup packet did not tx\n");
506 err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
507 if (err != USB_HOST_ERR_NONE)
508 PANIC ("USB: Could not configure device!\n");
512 * Set a device address to something other than the default pipe
515 usb_setup_dev_addr (struct usb_dev *dev)
517 struct usb_setup_pkt sp;
522 ASSERT (dev->addr == 0);
525 cfg = dev->h_cfg_eop;
527 dev->addr = bitmap_scan_and_flip (h->usb_dev_addrs, 1, 1, false);
529 sp.recipient = USB_SETUP_RECIP_DEV;
530 sp.type = USB_SETUP_TYPE_STD;
532 sp.request = REQ_STD_SET_ADDRESS;
533 sp.value = dev->addr;
537 h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
538 if (err != USB_HOST_ERR_NONE)
540 PANIC ("USB: WHOOPS!!!!!!!\n");
542 err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
543 if (err != USB_HOST_ERR_NONE)
545 PANIC ("USB: Error on setting device address (err = %d)\n", err);
548 h->dev->modify_dev_channel (dev->h_dev, dev->addr, dev->usb_version);
551 #define MAX_USB_STR 128
552 /* read string by string descriptor index from usb device */
554 usb_get_string (struct usb_dev *udev, int ndx)
556 struct usb_setup_pkt sp;
557 char str[MAX_USB_STR];
561 sp.recipient = USB_SETUP_RECIP_DEV;
562 sp.type = USB_SETUP_TYPE_STD;
564 sp.request = REQ_STD_GET_DESC;
565 sp.value = (SETUP_DESC_STRING << 8) | ndx;
567 sp.length = MAX_USB_STR;
568 udev->host->dev->tx_pkt (udev->h_cfg_eop, USB_TOKEN_SETUP,
569 &sp, 0, sizeof (sp), NULL, false);
570 sz = usb_tx_all (&udev->cfg_eop, &str, MAX_USB_STR, 2, true);
572 usb_tx_all (&udev->cfg_eop, str + sz, (uint8_t) (str[0]) - sz, 0, true);
574 /* string failed to tx? */
578 /* some devices don't respect the string descriptor length value (str[0])
579 * and just send any old value they want, so we can't use it */
581 str[(sz < (MAX_USB_STR - 1)) ? (sz) : (MAX_USB_STR - 1)] = '\0';
583 /* usb uses wchars for strings, convert to ASCII */
584 wchar_to_ascii (str, str + 2);
585 ret = malloc (strlen (str) + 1);
586 strlcpy (ret, str, MAX_USB_STR);
590 static struct class *
591 usb_get_class_by_id (int id)
593 struct list_elem *li;
595 li = list_begin (&class_list);
596 while (li != list_end (&class_list))
599 c = list_entry (li, struct class, peers);
600 if (c->dev->class_id == id)
609 * Asssociate interfaces on a device with their respective device classes
612 usb_attach_interfaces (struct usb_dev *dev)
615 struct list_elem *li;
616 li = list_begin (&dev->interfaces);
617 while (li != list_end (&dev->interfaces))
620 struct usb_iface *ui;
622 ui = list_entry (li, struct usb_iface, peers);
624 cl = usb_get_class_by_id (ui->class_id);
626 /* no matching class? try next interface */
630 ui->c_info = cl->dev->attached (ui);
631 /* did class driver initialize it OK? */
632 if (ui->c_info != NULL)
634 /* yes, add to list of usable interfaces */
635 list_push_back (&cl->usb_ifaces, &ui->class_peers);
642 * Scan all interfaces for interfaces that match class's class id
643 * Attach interfaces that match
646 usb_apply_class_to_interfaces (struct class *c)
648 struct list_elem *li;
650 li = list_begin (&usb_dev_list);
651 while (li != list_end (&usb_dev_list))
654 struct list_elem *lii;
656 ud = list_entry (li, struct usb_dev, sys_peers);
658 lii = list_begin (&ud->interfaces);
659 while (lii != list_end (&ud->interfaces))
661 struct usb_iface *ui;
663 ui = list_entry (lii, struct usb_iface, peers);
665 lii = list_next (lii);
666 if (ui->class_id != c->dev->class_id)
669 ui->c_info = c->dev->attached (ui);
670 if (ui->c_info == NULL)
673 list_push_back (&c->usb_ifaces, &ui->class_peers);
682 usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx)
688 ASSERT (eop != NULL);
689 h = eop->iface->dev->host;
691 if (eop->direction == 0)
692 token = USB_TOKEN_OUT;
694 token = USB_TOKEN_IN;
696 err = h->dev->tx_pkt (eop->h_eop, token, buf, sz, sz, tx, true);
702 usb_dev_setup (struct usb_endpoint *eop, bool in,
703 struct usb_setup_pkt *s, void *buf, int sz)
708 ASSERT (eop != NULL);
709 h = eop->iface->dev->host;
711 err = h->dev->tx_pkt (eop->h_eop, USB_TOKEN_SETUP, s,
712 0, sizeof (struct usb_setup_pkt), NULL, true);
713 if (err != USB_HOST_ERR_NONE)
715 printf ("usb_dev_setup: failed\n");
719 err = h->dev->tx_pkt (eop->h_eop, (in) ? USB_TOKEN_IN : USB_TOKEN_OUT,
720 buf, 0, sz, &sz, true);
725 /** convert a wchar string to ascii in place */
727 wchar_to_ascii (char *dst, const char *src)
730 for (sz = 0; src[sz * 2] != '\0'; sz++)
732 dst[sz] = src[sz * 2];
738 /* this is used for variable sized transfers where a normal bulk transfer would
739 probably fail, since it expects some minimum size - we just want to
740 read/write as much to the pipe as we can
743 usb_tx_all (struct usb_endpoint *eop, void *buf,
744 int max_bytes, int bailout, bool in)
758 token = (in) ? USB_TOKEN_IN : USB_TOKEN_OUT;
759 h = eop->iface->dev->host;
760 while (txed < max_bytes && txed < bailout)
764 err = h->dev->tx_pkt (eop->h_eop, token,
765 buf + txed, 0, max_bytes - txed, &sz, true);
769 /* this should probably be using short packet detection */
770 if (err != USB_HOST_ERR_NONE || sz != prev_sz || sz == 0)