Import 2.3.18pre1
[davej-history.git] / drivers / usb / usb.c
blob4afeb1a3064f3aa27a6e4db2d87475447edd2a26
1 /*
2 * drivers/usb/usb.c
4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999
7 * NOTE! This is not actually a driver at all, rather this is
8 * just a collection of helper routines that implement the
9 * generic USB things that the real drivers can use..
11 * Think of this as a "USB library" rather than anything else.
12 * It should be considered a slave, with no callbacks. Callbacks
13 * are evil.
16 #include <linux/config.h>
17 #include <linux/string.h>
18 #include <linux/bitops.h>
19 #include <linux/malloc.h>
21 #include "usb.h"
24 * We have a per-interface "registered driver" list.
26 static LIST_HEAD(usb_driver_list);
27 static LIST_HEAD(usb_bus_list);
29 int usb_register(struct usb_driver *new_driver)
31 struct list_head *tmp;
33 printk("usbcore: Registering new driver %s\n", new_driver->name);
35 /* Add it to the list of known drivers */
36 list_add(&new_driver->driver_list, &usb_driver_list);
39 * We go through all existing devices, and see if any of them would
40 * be acceptable to the new driver.. This is done using a depth-first
41 * search for devices without a registered driver already, then
42 * running 'probe' with each of the drivers registered on every one
43 * of these.
45 tmp = usb_bus_list.next;
46 while (tmp != &usb_bus_list) {
47 struct usb_bus *bus = list_entry(tmp,struct usb_bus, bus_list);
49 tmp = tmp->next;
50 usb_check_support(bus->root_hub);
52 return 0;
55 void usb_deregister(struct usb_driver *driver)
57 struct list_head *tmp;
59 printk("usbcore: Deregistering driver %s\n", driver->name);
62 * first we remove the driver, to be sure it doesn't get used by
63 * another thread while we are stepping through removing entries
65 list_del(&driver->driver_list);
67 tmp = usb_bus_list.next;
68 while (tmp != &usb_bus_list) {
69 struct usb_bus *bus = list_entry(tmp,struct usb_bus,bus_list);
71 tmp = tmp->next;
72 usb_driver_purge(driver, bus->root_hub);
77 * This function is part of a depth-first search down the device tree,
78 * removing any instances of a device driver.
80 static void usb_driver_purge(struct usb_driver *driver,struct usb_device *dev)
82 int i;
84 if (!dev) {
85 printk(KERN_ERR "usbcore: null device being purged!!!\n");
86 return;
89 for (i=0; i<USB_MAXCHILDREN; i++)
90 if (dev->children[i])
91 usb_driver_purge(driver, dev->children[i]);
93 /* now we check this device */
94 if (dev->driver == driver) {
96 * Note: this is not the correct way to do this, this
97 * uninitializes and reinitializes EVERY driver
99 printk(KERN_INFO "disconnect driverless device %d\n",
100 dev->devnum);
101 dev->driver->disconnect(dev);
102 dev->driver = NULL;
105 * This will go back through the list looking for a driver
106 * that can handle the device
108 usb_find_driver(dev);
113 * New functions for (de)registering a controller
115 struct usb_bus *usb_alloc_bus(struct usb_operations *op)
117 struct usb_bus *bus;
119 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
120 if (!bus)
121 return NULL;
123 memset(&bus->devmap, 0, sizeof(struct usb_devmap));
125 bus->op = op;
126 bus->root_hub = NULL;
127 bus->hcpriv = NULL;
129 INIT_LIST_HEAD(&bus->bus_list);
131 return bus;
134 void usb_free_bus(struct usb_bus *bus)
136 if (!bus)
137 return;
139 kfree(bus);
142 void usb_register_bus(struct usb_bus *bus)
144 proc_usb_add_bus(bus);
146 /* Add it to the list of buses */
147 list_add(&bus->bus_list, &usb_bus_list);
149 printk("New USB bus registered\n");
152 void usb_deregister_bus(struct usb_bus *bus)
155 * NOTE: make sure that all the devices are removed by the
156 * controller code, as well as having it call this when cleaning
157 * itself up
159 list_del(&bus->bus_list);
161 proc_usb_remove_bus(bus);
165 * This function is for doing a depth-first search for devices which
166 * have support, for dynamic loading of driver modules.
168 static void usb_check_support(struct usb_device *dev)
170 int i;
172 if (!dev) {
173 printk(KERN_ERR "usbcore: null device being checked!!!\n");
174 return;
177 for (i=0; i<USB_MAXCHILDREN; i++)
178 if (dev->children[i])
179 usb_check_support(dev->children[i]);
181 /* now we check this device */
182 if (!dev->driver && dev->devnum > 0)
183 usb_find_driver(dev);
186 * This entrypoint gets called for each new device.
188 * We now walk the list of registered USB drivers,
189 * looking for one that will accept this device as
190 * his..
192 static int usb_find_driver(struct usb_device *dev)
194 struct list_head *tmp = usb_driver_list.next;
196 while (tmp != &usb_driver_list) {
197 struct usb_driver *driver = list_entry(tmp, struct usb_driver,
198 driver_list);
199 tmp = tmp->next;
200 if (driver->probe(dev))
201 continue;
202 dev->driver = driver;
203 return 1;
207 * Ok, no driver accepted the device, so show the info
208 * for debugging..
210 return 0;
214 * Only HC's should call usb_alloc_dev and usb_free_dev directly
215 * Anybody may use usb_inc_dev_use or usb_dec_dev_use
217 struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
219 struct usb_device *dev;
221 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
222 if (!dev)
223 return NULL;
225 memset(dev, 0, sizeof(*dev));
227 dev->bus = bus;
228 dev->parent = parent;
229 atomic_set(&dev->refcnt, 1);
231 dev->bus->op->allocate(dev);
233 return dev;
236 void usb_free_dev(struct usb_device *dev)
238 if (atomic_dec_and_test(&dev->refcnt)) {
239 usb_destroy_configuration(dev);
241 dev->bus->op->deallocate(dev);
242 kfree(dev);
246 void usb_inc_dev_use(struct usb_device *dev)
248 atomic_inc(&dev->refcnt);
251 static int usb_parse_endpoint(struct usb_device *dev, struct usb_endpoint_descriptor *endpoint, unsigned char *buffer, int size)
253 struct usb_descriptor_header *header;
254 int parsed = 0;
256 header = (struct usb_descriptor_header *)buffer;
258 /* Everything should be fine being passed into here, but we sanity */
259 /* check JIC */
260 if (header->bLength > size) {
261 printk(KERN_ERR "usb: ran out of descriptors parsing\n");
262 return -1;
265 if (header->bDescriptorType != USB_DT_ENDPOINT) {
266 printk(KERN_INFO "usb: unexpected descriptor 0x%X\n",
267 endpoint->bDescriptorType);
268 return parsed;
271 memcpy(endpoint, buffer, USB_DT_ENDPOINT_SIZE);
272 le16_to_cpus(&endpoint->wMaxPacketSize);
274 buffer += header->bLength;
275 size -= header->bLength;
276 parsed += header->bLength;
278 /* Skip over the rest of the Class Specific or Vendor Specific */
279 /* descriptors */
280 while (size >= sizeof(struct usb_descriptor_header)) {
281 header = (struct usb_descriptor_header *)buffer;
283 if (header->bLength < 2) {
284 printk(KERN_ERR "usb: invalid descriptor length of %d\n", header->bLength);
285 return -1;
288 /* If we find another descriptor which is at or below us */
289 /* in the descriptor heirarchy then return */
290 if ((header->bDescriptorType == USB_DT_ENDPOINT) ||
291 (header->bDescriptorType == USB_DT_INTERFACE) ||
292 (header->bDescriptorType == USB_DT_CONFIG) ||
293 (header->bDescriptorType == USB_DT_DEVICE))
294 return parsed;
296 printk(KERN_INFO "usb: skipping descriptor 0x%X\n",
297 header->bDescriptorType);
299 buffer += header->bLength;
300 size -= header->bLength;
301 parsed += header->bLength;
304 return parsed;
307 #if 0
308 static int usb_parse_hid(struct usb_device *dev, struct usb_hid_descriptor *hid, unsigned char *ptr, int len)
310 int parsed = usb_expect_descriptor(ptr, len, USB_DT_HID, ptr[0]);
311 int i;
313 if (parsed < 0)
314 return parsed;
316 memcpy(hid, ptr + parsed, ptr[parsed]);
317 le16_to_cpus(&hid->bcdHID);
319 for (i=0; i<hid->bNumDescriptors; i++)
320 le16_to_cpus(&(hid->desc[i].wDescriptorLength));
322 return parsed + ptr[parsed];
324 #endif
326 static int usb_parse_interface(struct usb_device *dev, struct usb_interface *interface, unsigned char *buffer, int size)
328 int i;
329 int retval, parsed = 0;
330 struct usb_descriptor_header *header;
331 struct usb_interface_descriptor *ifp;
333 interface->act_altsetting = 0;
334 interface->num_altsetting = 0;
336 interface->altsetting = kmalloc(sizeof(struct usb_interface_descriptor) * USB_MAXALTSETTING, GFP_KERNEL);
337 if (!interface->altsetting) {
338 printk("couldn't kmalloc interface->altsetting\n");
339 return -1;
342 while (size > 0) {
343 ifp = interface->altsetting + interface->num_altsetting;
344 interface->num_altsetting++;
346 if (interface->num_altsetting >= USB_MAXALTSETTING) {
347 printk(KERN_WARNING "usb: too many alternate settings\n");
348 return -1;
350 memcpy(ifp, buffer, USB_DT_INTERFACE_SIZE);
352 /* Skip over the interface */
353 buffer += ifp->bLength;
354 parsed += ifp->bLength;
355 size -= ifp->bLength;
357 /* Skip over at Interface class or vendor descriptors */
358 while (size >= sizeof(struct usb_descriptor_header)) {
359 header = (struct usb_descriptor_header *)buffer;
361 if (header->bLength < 2) {
362 printk(KERN_ERR "usb: invalid descriptor length of %d\n", header->bLength);
363 return -1;
366 /* If we find another descriptor which is at or below us */
367 /* in the descriptor heirarchy then return */
368 if ((header->bDescriptorType == USB_DT_INTERFACE) ||
369 (header->bDescriptorType == USB_DT_ENDPOINT))
370 break;
372 if ((header->bDescriptorType == USB_DT_CONFIG) ||
373 (header->bDescriptorType == USB_DT_DEVICE))
374 return parsed;
376 if (header->bDescriptorType == USB_DT_HID)
377 printk(KERN_INFO "usb: skipping HID descriptor\n");
378 else
379 printk(KERN_INFO "usb: unexpected descriptor 0x%X\n",
380 header->bDescriptorType);
382 buffer += header->bLength;
383 parsed += header->bLength;
384 size -= header->bLength;
387 if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
388 printk(KERN_WARNING "usb: too many endpoints\n");
389 return -1;
392 ifp->endpoint = (struct usb_endpoint_descriptor *)
393 kmalloc(ifp->bNumEndpoints *
394 sizeof(struct usb_endpoint_descriptor), GFP_KERNEL);
395 if (!ifp->endpoint) {
396 printk(KERN_WARNING "usb: out of memory\n");
397 return -1;
400 memset(ifp->endpoint, 0, ifp->bNumEndpoints *
401 sizeof(struct usb_endpoint_descriptor));
403 for (i = 0; i < ifp->bNumEndpoints; i++) {
404 header = (struct usb_descriptor_header *)buffer;
406 if (header->bLength > size) {
407 printk(KERN_ERR "usb: ran out of descriptors parsing\n");
408 return -1;
411 retval = usb_parse_endpoint(dev, ifp->endpoint + i, buffer, size);
412 if (retval < 0)
413 return retval;
415 buffer += retval;
416 parsed += retval;
417 size -= retval;
420 /* We check to see if it's an alternate to this one */
421 ifp = (struct usb_interface_descriptor *)buffer;
422 if (size < USB_DT_INTERFACE_SIZE ||
423 ifp->bDescriptorType != USB_DT_INTERFACE ||
424 !ifp->bAlternateSetting)
425 return parsed;
428 return parsed;
431 static int usb_parse_configuration(struct usb_device *dev, struct usb_config_descriptor *config, char *buffer)
433 int i;
434 int retval;
435 int size;
436 struct usb_descriptor_header *header;
438 memcpy(config, buffer, USB_DT_INTERFACE_SIZE);
440 le16_to_cpus(&config->wTotalLength);
441 size = config->wTotalLength;
443 if (config->bNumInterfaces > USB_MAXINTERFACES) {
444 printk(KERN_WARNING "usb: too many interfaces\n");
445 return -1;
448 config->interface = (struct usb_interface *)
449 kmalloc(config->bNumInterfaces *
450 sizeof(struct usb_interface), GFP_KERNEL);
451 if (!config->interface) {
452 printk(KERN_WARNING "usb: out of memory\n");
453 return -1;
456 memset(config->interface, 0,
457 config->bNumInterfaces*sizeof(struct usb_interface_descriptor));
459 buffer += config->bLength;
460 size -= config->bLength;
462 for (i = 0; i < config->bNumInterfaces; i++) {
463 header = (struct usb_descriptor_header *)buffer;
464 if (header->bLength > size) {
465 printk(KERN_ERR "usb: ran out of descriptors parsing\n");
466 return -1;
469 if (header->bDescriptorType != USB_DT_INTERFACE) {
470 printk(KERN_INFO "usb: unexpected descriptor 0x%X\n",
471 header->bDescriptorType);
473 buffer += header->bLength;
474 size -= header->bLength;
475 continue;
478 retval = usb_parse_interface(dev, config->interface + i, buffer, size);
479 if (retval < 0)
480 return retval;
482 buffer += retval;
483 size -= retval;
486 return size;
489 void usb_destroy_configuration(struct usb_device *dev)
491 int c, i, j;
493 if (!dev->config)
494 return;
496 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
497 struct usb_config_descriptor *cf = &dev->config[c];
499 if (!cf->interface)
500 break;
502 for (i = 0; i < cf->bNumInterfaces; i++) {
503 struct usb_interface *ifp =
504 &cf->interface[i];
506 if (!ifp->altsetting)
507 break;
509 for (j = 0; j < ifp->num_altsetting; j++) {
510 struct usb_interface_descriptor *as =
511 &ifp->altsetting[j];
513 if (!as->endpoint)
514 break;
516 kfree(as->endpoint);
518 kfree(ifp->altsetting);
520 kfree(cf->interface);
522 kfree(dev->config);
524 if (dev->string) {
525 kfree(dev->string);
526 dev->string = 0;
530 void usb_init_root_hub(struct usb_device *dev)
532 dev->devnum = -1;
533 dev->slow = 0;
537 * Something got disconnected. Get rid of it, and all of its children.
539 void usb_disconnect(struct usb_device **pdev)
541 struct usb_device * dev = *pdev;
542 int i;
544 if (!dev)
545 return;
547 *pdev = NULL;
549 printk("USB disconnect on device %d\n", dev->devnum);
551 if (dev->driver)
552 dev->driver->disconnect(dev);
554 /* Free up all the children.. */
555 for (i = 0; i < USB_MAXCHILDREN; i++) {
556 struct usb_device **child = dev->children + i;
557 usb_disconnect(child);
560 /* remove /proc/bus/usb entry */
561 proc_usb_remove_device(dev);
563 /* Free up the device itself, including its device number */
564 if (dev->devnum > 0)
565 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
567 usb_free_dev(dev);
571 * Connect a new USB device. This basically just initializes
572 * the USB device information and sets up the topology - it's
573 * up to the low-level driver to reset the port and actually
574 * do the setup (the upper levels don't know how to do that).
576 void usb_connect(struct usb_device *dev)
578 int devnum;
580 dev->descriptor.bMaxPacketSize0 = 8; /* XXX fixed 8 bytes for now */
582 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, 1);
583 if (devnum < 128) {
584 set_bit(devnum, dev->bus->devmap.devicemap);
585 dev->devnum = devnum;
590 * These are the actual routines to send
591 * and receive control messages.
593 int usb_set_address(struct usb_device *dev)
595 devrequest dr;
597 dr.requesttype = 0;
598 dr.request = USB_REQ_SET_ADDRESS;
599 dr.value = dev->devnum;
600 dr.index = 0;
601 dr.length = 0;
603 return dev->bus->op->control_msg(dev, usb_snddefctrl(dev), &dr, NULL, 0);
606 int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
608 devrequest dr;
609 int i = 5;
610 int result;
612 dr.requesttype = 0x80;
613 dr.request = USB_REQ_GET_DESCRIPTOR;
614 dr.value = (type << 8) + index;
615 dr.index = 0;
616 dr.length = size;
618 while (i--) {
619 if (!(result = dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, size))
620 || result == USB_ST_STALL)
621 break;
623 return result;
626 int usb_get_string(struct usb_device *dev, unsigned short langid, unsigned char index, void *buf, int size)
628 devrequest dr;
630 dr.requesttype = 0x80;
631 dr.request = USB_REQ_GET_DESCRIPTOR;
632 dr.value = (USB_DT_STRING << 8) + index;
633 dr.index = langid;
634 dr.length = size;
636 return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, buf, size);
639 int usb_get_device_descriptor(struct usb_device *dev)
641 int ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor,
642 sizeof(dev->descriptor));
643 if (ret == 0) {
644 le16_to_cpus(&dev->descriptor.bcdUSB);
645 le16_to_cpus(&dev->descriptor.idVendor);
646 le16_to_cpus(&dev->descriptor.idProduct);
647 le16_to_cpus(&dev->descriptor.bcdDevice);
649 return ret;
652 int usb_get_status (struct usb_device *dev, int type, int target, void *data)
654 devrequest dr;
656 dr.requesttype = USB_DIR_IN | type; /* USB_RECIP_DEVICE, _INTERFACE, or _ENDPOINT */
657 dr.request = USB_REQ_GET_STATUS;
658 dr.value = 0;
659 dr.index = target;
660 dr.length = 2;
662 return dev->bus->op->control_msg (dev, usb_rcvctrlpipe (dev,0), &dr, data, 2);
665 int usb_get_protocol(struct usb_device *dev)
667 unsigned char buf[8];
668 devrequest dr;
670 dr.requesttype = USB_RT_HIDD | 0x80;
671 dr.request = USB_REQ_GET_PROTOCOL;
672 dr.value = 0;
673 dr.index = 1;
674 dr.length = 1;
676 if (dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev, 0), &dr, buf, 1))
677 return -1;
679 return buf[0];
682 int usb_set_protocol(struct usb_device *dev, int protocol)
684 devrequest dr;
686 dr.requesttype = USB_RT_HIDD;
687 dr.request = USB_REQ_SET_PROTOCOL;
688 dr.value = protocol;
689 dr.index = 1;
690 dr.length = 0;
692 if (dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev, 0), &dr, NULL, 0))
693 return -1;
695 return 0;
698 /* keyboards want a nonzero duration according to HID spec, but
699 mice should use infinity (0) -keryan */
700 int usb_set_idle(struct usb_device *dev, int duration, int report_id)
702 devrequest dr;
704 dr.requesttype = USB_RT_HIDD;
705 dr.request = USB_REQ_SET_IDLE;
706 dr.value = (duration << 8) | report_id;
707 dr.index = 1;
708 dr.length = 0;
710 if (dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev, 0), &dr, NULL, 0))
711 return -1;
713 return 0;
716 static void usb_set_maxpacket(struct usb_device *dev)
718 int i, j;
719 struct usb_interface *ifp;
721 for (i=0; i<dev->actconfig->bNumInterfaces; i++) {
722 ifp = dev->actconfig->interface + i;
724 for (j = 0; j < ifp->num_altsetting; j++) {
725 struct usb_interface_descriptor *as = ifp->altsetting + j;
726 struct usb_endpoint_descriptor *ep = as->endpoint;
727 int e;
729 for (e=0; e<as->bNumEndpoints; e++) {
730 if (usb_endpoint_out(ep[e].bEndpointAddress))
731 dev->epmaxpacketout[ep[e].bEndpointAddress & 0x0f] =
732 ep[e].wMaxPacketSize;
733 else
734 dev->epmaxpacketin [ep[e].bEndpointAddress & 0x0f] =
735 ep[e].wMaxPacketSize;
742 * endp: endpoint number in bits 0-3;
743 * direction flag in bit 7 (1 = IN, 0 = OUT)
745 int usb_clear_halt(struct usb_device *dev, int endp)
747 devrequest dr;
748 int result;
749 __u16 status;
751 //if (!usb_endpoint_halted(dev, endp & 0x0f, usb_endpoint_out(endp)))
752 // return 0;
754 dr.requesttype = USB_RT_ENDPOINT;
755 dr.request = USB_REQ_CLEAR_FEATURE;
756 dr.value = 0;
757 dr.index = endp;
758 dr.length = 0;
760 result = dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr, NULL, 0);
762 /* don't clear if failed */
763 if (result)
764 return result;
766 #if 1 /* let's be really tough */
767 dr.requesttype = 0x80 | USB_RT_ENDPOINT;
768 dr.request = USB_REQ_GET_STATUS;
769 dr.length = 2;
770 status = 0xffff;
772 result = dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, &status, 2);
774 if (result)
775 return result;
776 if (status & 1)
777 return 1; /* still halted */
778 #endif
779 usb_endpoint_running(dev, endp & 0x0f, usb_endpoint_out(endp));
781 /* toggle is reset on clear */
783 usb_settoggle(dev, endp & 0x0f, usb_endpoint_out(endp), 0);
785 return 0;
788 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
790 devrequest dr;
792 dr.requesttype = 1;
793 dr.request = USB_REQ_SET_INTERFACE;
794 dr.value = alternate;
795 dr.index = interface;
796 dr.length = 0;
798 if (dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev, 0), &dr, NULL, 0))
799 return -1;
801 dev->ifnum = interface;
802 dev->actconfig->interface[interface].act_altsetting = alternate;
803 usb_set_maxpacket(dev);
804 return 0;
807 int usb_set_configuration(struct usb_device *dev, int configuration)
809 devrequest dr;
810 int i;
811 struct usb_config_descriptor *cp = NULL;
813 dr.requesttype = 0;
814 dr.request = USB_REQ_SET_CONFIGURATION;
815 dr.value = configuration;
816 dr.index = 0;
817 dr.length = 0;
819 for (i=0; i<dev->descriptor.bNumConfigurations; i++) {
820 if (dev->config[i].bConfigurationValue == configuration) {
821 cp = &dev->config[i];
822 break;
825 if (!cp) {
826 printk(KERN_INFO "usb: selecting invalid configuration %d\n", configuration);
827 return -1;
829 if (dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev, 0), &dr, NULL, 0))
830 return -1;
832 dev->actconfig = cp;
833 dev->toggle[0] = 0;
834 dev->toggle[1] = 0;
835 usb_set_maxpacket(dev);
836 return 0;
839 int usb_get_report(struct usb_device *dev, unsigned char type, unsigned char id, unsigned char index, void *buf, int size)
841 devrequest dr;
843 dr.requesttype = USB_RT_HIDD | 0x80;
844 dr.request = USB_REQ_GET_REPORT;
845 dr.value = (type << 8) + id;
846 dr.index = index;
847 dr.length = size;
849 if (dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev, 0), &dr, buf, size))
850 return -1;
852 return 0;
855 int usb_get_configuration(struct usb_device *dev)
857 unsigned int cfgno;
858 unsigned char buffer[8];
859 unsigned char *bigbuffer;
860 struct usb_config_descriptor *desc =
861 (struct usb_config_descriptor *)buffer;
863 if (dev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
864 printk(KERN_WARNING "usb: too many configurations\n");
865 return -1;
868 dev->config = (struct usb_config_descriptor *)
869 kmalloc(dev->descriptor.bNumConfigurations *
870 sizeof(struct usb_config_descriptor), GFP_KERNEL);
871 if (!dev->config) {
872 printk(KERN_WARNING "usb: out of memory.\n");
873 return -1;
875 memset(dev->config, 0, dev->descriptor.bNumConfigurations *
876 sizeof(struct usb_config_descriptor));
878 for (cfgno = 0; cfgno < dev->descriptor.bNumConfigurations; cfgno++) {
879 int result;
881 /* We grab the first 8 bytes so we know how long the whole */
882 /* configuration is */
883 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 8);
884 if (result)
885 return -1;
887 /* Get the full buffer */
888 le16_to_cpus(&desc->wTotalLength);
890 bigbuffer = kmalloc(desc->wTotalLength, GFP_KERNEL);
891 if (!bigbuffer)
892 return -1;
894 /* Now that we know the length, get the whole thing */
895 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, bigbuffer, desc->wTotalLength);
896 if (result) {
897 kfree(bigbuffer);
898 return -1;
901 result = usb_parse_configuration(dev, &dev->config[cfgno], bigbuffer);
902 kfree(bigbuffer);
904 if (result > 0)
905 printk(KERN_INFO "usb: descriptor data left\n");
906 else if (result < 0)
907 return -1;
910 return 0;
914 char *usb_string(struct usb_device *dev, int index)
916 int len, i;
917 char *ptr;
918 union {
919 unsigned char buffer[256];
920 struct usb_string_descriptor desc;
921 } u;
923 if (index <= 0)
924 return 0;
925 if (dev->string)
926 kfree (dev->string);
928 if (dev->string_langid == 0) {
929 /* read string descriptor 0 */
930 if (usb_get_string(dev, 0, 0, u.buffer, 2) == 0
931 && u.desc.bLength >= 4
932 && usb_get_string(dev, 0, 0, u.buffer, 4) == 0)
933 dev->string_langid = le16_to_cpup(&u.desc.wData[0]);
934 dev->string_langid |= 0x10000; /* so it's non-zero */
937 if (usb_get_string(dev, dev->string_langid, index, u.buffer, 2) ||
938 usb_get_string(dev, dev->string_langid, index, u.buffer,
939 u.desc.bLength))
940 return 0;
942 len = u.desc.bLength / 2; /* includes terminating null */
944 ptr = kmalloc(len, GFP_KERNEL);
945 if (!ptr)
946 return 0;
948 for (i = 0; i < len - 1; ++i)
949 ptr[i] = le16_to_cpup(&u.desc.wData[i]);
950 ptr[i] = 0;
952 dev->string = ptr;
953 return ptr;
957 * By the time we get here, the device has gotten a new device ID
958 * and is in the default state. We need to identify the thing and
959 * get the ball rolling..
961 int usb_new_device(struct usb_device *dev)
963 int addr;
965 printk(KERN_INFO "USB new device connect, assigned device number %d\n",
966 dev->devnum);
968 dev->maxpacketsize = 0; /* Default to 8 byte max packet size */
969 dev->epmaxpacketin [0] = 8;
970 dev->epmaxpacketout[0] = 8;
972 /* We still haven't set the Address yet */
973 addr = dev->devnum;
974 dev->devnum = 0;
976 /* Slow devices */
977 if (usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8)) {
978 printk(KERN_ERR "usbcore: USB device not responding, giving up\n");
979 dev->devnum = -1;
980 return 1;
983 dev->epmaxpacketin [0] = dev->descriptor.bMaxPacketSize0;
984 dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
985 switch (dev->descriptor.bMaxPacketSize0) {
986 case 8: dev->maxpacketsize = 0; break;
987 case 16: dev->maxpacketsize = 1; break;
988 case 32: dev->maxpacketsize = 2; break;
989 case 64: dev->maxpacketsize = 3; break;
992 dev->devnum = addr;
994 if (usb_set_address(dev)) {
995 printk(KERN_ERR "usbcore: USB device not accepting new address\n");
996 dev->devnum = -1;
997 return 1;
1000 wait_ms(10); /* Let the SET_ADDRESS settle */
1002 if (usb_get_device_descriptor(dev)) {
1003 printk(KERN_ERR "usbcore: unable to get device descriptor\n");
1004 dev->devnum = -1;
1005 return 1;
1008 if (usb_get_configuration(dev)) {
1009 printk(KERN_ERR "usbcore: unable to get configuration\n");
1010 dev->devnum = -1;
1011 return 1;
1014 dev->actconfig = dev->config;
1015 dev->ifnum = 0;
1016 usb_set_maxpacket(dev);
1018 usb_show_string(dev, "Manufacturer", dev->descriptor.iManufacturer);
1019 usb_show_string(dev, "Product", dev->descriptor.iProduct);
1020 usb_show_string(dev, "SerialNumber", dev->descriptor.iSerialNumber);
1022 /* now that the basic setup is over, add a /proc/bus/usb entry */
1023 proc_usb_add_device(dev);
1025 if (!usb_find_driver(dev)) {
1027 * Ok, no driver accepted the device, so show the info for
1028 * debugging
1030 printk(KERN_DEBUG "Unknown new USB device:\n");
1031 usb_show_device(dev);
1034 return 0;
1037 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype, __u16 value, __u16 index, void *data, __u16 size)
1039 devrequest dr;
1041 dr.requesttype = requesttype;
1042 dr.request = request;
1043 dr.value = cpu_to_le16p(&value);
1044 dr.index = cpu_to_le16p(&index);
1045 dr.length = cpu_to_le16p(&size);
1047 return dev->bus->op->control_msg(dev, pipe, &dr, data, size);
1050 void *usb_request_irq(struct usb_device *dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
1052 return dev->bus->op->request_irq(dev, pipe, handler, period, dev_id);
1055 void *usb_request_bulk(struct usb_device *dev, unsigned int pipe, usb_device_irq handler, void *data, int len, void *dev_id)
1057 return dev->bus->op->request_bulk(dev, pipe, handler, data, len, dev_id);
1060 int usb_terminate_bulk(struct usb_device *dev, void *first)
1062 return dev->bus->op->terminate_bulk(dev, first);
1065 int usb_release_irq(struct usb_device *dev, void *handle)
1067 return dev->bus->op->release_irq(dev, handle);
1071 * usb_get_current_frame_number()
1073 * returns the current frame number for the parent USB bus/controller
1074 * of the given USB device.
1076 int usb_get_current_frame_number (struct usb_device *usb_dev)
1078 return usb_dev->bus->op->get_frame_number (usb_dev);
1081 int usb_init_isoc (struct usb_device *usb_dev,
1082 unsigned int pipe,
1083 int frame_count,
1084 void *context,
1085 struct usb_isoc_desc **isocdesc)
1087 return usb_dev->bus->op->init_isoc (usb_dev, pipe, frame_count, context, isocdesc);
1090 void usb_free_isoc (struct usb_isoc_desc *isocdesc)
1092 isocdesc->usb_dev->bus->op->free_isoc (isocdesc);
1095 int usb_run_isoc (struct usb_isoc_desc *isocdesc,
1096 struct usb_isoc_desc *pr_isocdesc)
1098 return isocdesc->usb_dev->bus->op->run_isoc (isocdesc, pr_isocdesc);
1101 int usb_kill_isoc (struct usb_isoc_desc *isocdesc)
1103 return isocdesc->usb_dev->bus->op->kill_isoc (isocdesc);
1106 #ifdef CONFIG_PROC_FS
1107 struct list_head *usb_driver_get_list(void)
1109 return &usb_driver_list;
1112 struct list_head *usb_bus_get_list(void)
1114 return &usb_bus_list;
1116 #endif