Import 2.4.0-test5pre2
[davej-history.git] / drivers / usb / hub.c
blob71d6aabb933b5c60a54c815111e1dbe6cf77755b
1 /*
2 * USB hub driver.
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 */
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/list.h>
13 #include <linux/malloc.h>
14 #include <linux/smp_lock.h>
15 #ifdef CONFIG_USB_DEBUG
16 #define DEBUG
17 #else
18 #undef DEBUG
19 #endif
20 #include <linux/usb.h>
21 #include <linux/usbdevice_fs.h>
23 #include <asm/semaphore.h>
24 #include <asm/uaccess.h>
25 #include <asm/byteorder.h>
27 #include "hub.h"
29 /* Wakes up khubd */
30 static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
31 static DECLARE_MUTEX(usb_address0_sem);
33 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
34 static LIST_HEAD(hub_list); /* List containing all of the hubs (for cleanup) */
36 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
37 static int khubd_pid = 0; /* PID of khubd */
38 static int khubd_running = 0;
40 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
42 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
43 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
44 USB_DT_HUB << 8, 0, data, size, HZ);
47 static int usb_clear_hub_feature(struct usb_device *dev, int feature)
49 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
50 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
53 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
55 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
56 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
59 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
61 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
62 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
65 static int usb_get_hub_status(struct usb_device *dev, void *data)
67 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
68 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
69 data, sizeof(struct usb_hub_status), HZ);
72 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
74 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
75 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
76 data, sizeof(struct usb_hub_status), HZ);
80 * A irq handler returns non-zero to indicate to
81 * the low-level driver that it wants to be re-activated,
82 * or zero to say "I'm done".
84 static void hub_irq(struct urb *urb)
86 struct usb_hub *hub = (struct usb_hub *)urb->context;
87 unsigned long flags;
89 if (urb->status) {
90 if (urb->status != -ENOENT)
91 dbg("nonzero status in irq %d", urb->status);
93 return;
96 /* Something happened, let khubd figure it out */
97 if (waitqueue_active(&khubd_wait)) {
98 /* Add the hub to the event queue */
99 spin_lock_irqsave(&hub_event_lock, flags);
100 if (hub->event_list.next == &hub->event_list) {
101 list_add(&hub->event_list, &hub_event_list);
102 /* Wake up khubd */
103 wake_up(&khubd_wait);
105 spin_unlock_irqrestore(&hub_event_lock, flags);
109 static void usb_hub_power_on(struct usb_hub *hub)
111 int i;
113 /* Enable power to the ports */
114 dbg("enabling power on all ports");
115 for (i = 0; i < hub->nports; i++)
116 usb_set_port_feature(hub->dev, i + 1, USB_PORT_FEAT_POWER);
119 static int usb_hub_configure(struct usb_hub *hub)
121 struct usb_device *dev = hub->dev;
122 unsigned char buffer[HUB_DESCRIPTOR_MAX_SIZE], *bitmap;
123 struct usb_hub_descriptor *descriptor;
124 struct usb_descriptor_header *header;
125 struct usb_hub_status *hubsts;
126 int i, ret;
128 /* Request the entire hub descriptor. */
129 header = (struct usb_descriptor_header *)buffer;
130 ret = usb_get_hub_descriptor(dev, buffer, sizeof(buffer));
131 /* <buffer> is large enough for a hub with 127 ports;
132 * the hub can/will return fewer bytes here. */
133 if (ret < 0) {
134 err("Unable to get hub descriptor (err = %d)", ret);
135 return -1;
138 bitmap = kmalloc(header->bLength, GFP_KERNEL);
139 if (!bitmap) {
140 err("Unable to kmalloc %d bytes for bitmap", header->bLength);
141 return -1;
144 memcpy (bitmap, buffer, header->bLength);
145 descriptor = (struct usb_hub_descriptor *)bitmap;
147 hub->nports = dev->maxchild = descriptor->bNbrPorts;
148 info("%d port%s detected", hub->nports, (hub->nports == 1) ? "" : "s");
150 switch (descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
151 case 0x00:
152 dbg("ganged power switching");
153 break;
154 case 0x01:
155 dbg("individual port power switching");
156 break;
157 case 0x02:
158 case 0x03:
159 dbg("unknown reserved power switching mode");
160 break;
163 if (descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND)
164 dbg("part of a compound device");
165 else
166 dbg("standalone hub");
168 switch (descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
169 case 0x00:
170 dbg("global over-current protection");
171 break;
172 case 0x08:
173 dbg("individual port over-current protection");
174 break;
175 case 0x10:
176 case 0x18:
177 dbg("no over-current protection");
178 break;
181 dbg("power on to power good time: %dms", descriptor->bPwrOn2PwrGood * 2);
182 dbg("hub controller current requirement: %dmA", descriptor->bHubContrCurrent);
184 for (i = 0; i < dev->maxchild; i++)
185 dbg("port %d is%s removable", i + 1,
186 bitmap[7 + ((i + 1)/8)] & (1 << ((i + 1) % 8))
187 ? " not" : "");
189 kfree(bitmap);
191 ret = usb_get_hub_status(dev, buffer);
192 if (ret < 0) {
193 err("Unable to get hub status (err = %d)", ret);
194 return -1;
197 hubsts = (struct usb_hub_status *)buffer;
198 dbg("local power source is %s",
199 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
201 dbg("%sover-current condition exists",
202 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? "" : "no ");
204 usb_hub_power_on(hub);
206 return 0;
209 static void *hub_probe(struct usb_device *dev, unsigned int i)
211 struct usb_interface_descriptor *interface;
212 struct usb_endpoint_descriptor *endpoint;
213 struct usb_hub *hub;
214 unsigned long flags;
215 unsigned int pipe;
216 int maxp, ret;
218 interface = &dev->actconfig->interface[i].altsetting[0];
220 /* Is it a hub? */
221 if (interface->bInterfaceClass != USB_CLASS_HUB)
222 return NULL;
224 /* Some hubs have a subclass of 1, which AFAICT according to the */
225 /* specs is not defined, but it works */
226 if ((interface->bInterfaceSubClass != 0) &&
227 (interface->bInterfaceSubClass != 1))
228 return NULL;
230 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
231 if (interface->bNumEndpoints != 1)
232 return NULL;
234 endpoint = &interface->endpoint[0];
236 /* Output endpoint? Curiousier and curiousier.. */
237 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
238 err("Device is hub class, but has output endpoint?");
239 return NULL;
242 /* If it's not an interrupt endpoint, we'd better punt! */
243 if ((endpoint->bmAttributes & 3) != 3) {
244 err("Device is hub class, but has endpoint other than interrupt?");
245 return NULL;
248 /* We found a hub */
249 info("USB hub found");
251 hub = kmalloc(sizeof(*hub), GFP_KERNEL);
252 if (!hub) {
253 err("couldn't kmalloc hub struct");
254 return NULL;
257 memset(hub, 0, sizeof(*hub));
259 INIT_LIST_HEAD(&hub->event_list);
260 hub->dev = dev;
262 /* Record the new hub's existence */
263 spin_lock_irqsave(&hub_event_lock, flags);
264 INIT_LIST_HEAD(&hub->hub_list);
265 list_add(&hub->hub_list, &hub_list);
266 spin_unlock_irqrestore(&hub_event_lock, flags);
268 if (usb_hub_configure(hub) >= 0) {
269 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
270 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
272 if (maxp > sizeof(hub->buffer))
273 maxp = sizeof(hub->buffer);
275 hub->urb = usb_alloc_urb(0);
276 if (!hub->urb) {
277 err("couldn't allocate interrupt urb");
278 goto fail;
281 FILL_INT_URB(hub->urb, dev, pipe, hub->buffer, maxp, hub_irq,
282 hub, endpoint->bInterval);
283 ret = usb_submit_urb(hub->urb);
284 if (ret) {
285 err("usb_submit_urb failed (%d)", ret);
286 goto fail;
289 /* Wake up khubd */
290 wake_up(&khubd_wait);
293 return hub;
295 fail:
296 /* free hub, but first clean up its list. */
297 spin_lock_irqsave(&hub_event_lock, flags);
299 /* Delete it and then reset it */
300 list_del(&hub->event_list);
301 INIT_LIST_HEAD(&hub->event_list);
302 list_del(&hub->hub_list);
303 INIT_LIST_HEAD(&hub->hub_list);
305 spin_unlock_irqrestore(&hub_event_lock, flags);
307 kfree(hub);
309 return NULL;
312 static void hub_disconnect(struct usb_device *dev, void *ptr)
314 struct usb_hub *hub = (struct usb_hub *)ptr;
315 unsigned long flags;
317 spin_lock_irqsave(&hub_event_lock, flags);
319 /* Delete it and then reset it */
320 list_del(&hub->event_list);
321 INIT_LIST_HEAD(&hub->event_list);
322 list_del(&hub->hub_list);
323 INIT_LIST_HEAD(&hub->hub_list);
325 spin_unlock_irqrestore(&hub_event_lock, flags);
327 if (hub->urb) {
328 usb_unlink_urb(hub->urb);
329 usb_free_urb(hub->urb);
330 hub->urb = NULL;
333 /* Free the memory */
334 kfree(hub);
337 static int hub_ioctl (struct usb_device *hub, unsigned int code, void *user_data)
339 /* assert ifno == 0 (part of hub spec) */
340 switch (code) {
341 case USBDEVFS_HUB_PORTINFO: {
342 struct usbdevfs_hub_portinfo *info = user_data;
343 unsigned long flags;
344 int i;
346 spin_lock_irqsave (&hub_event_lock, flags);
347 if (hub->devnum <= 0)
348 info->nports = 0;
349 else {
350 info->nports = hub->maxchild;
351 for (i = 0; i < info->nports; i++) {
352 if (hub->children [i] == NULL)
353 info->port [i] = 0;
354 else
355 info->port [i] = hub->children [i]->devnum;
358 spin_unlock_irqrestore (&hub_event_lock, flags);
360 return info->nports + 1;
363 default:
364 return -ENOSYS;
368 static void usb_hub_port_connect_change(struct usb_device *hub, int port)
370 struct usb_device *usb;
371 struct usb_port_status portsts;
372 unsigned short portstatus, portchange;
373 int ret, tries;
375 wait_ms(100);
377 ret = usb_get_port_status(hub, port + 1, &portsts);
378 if (ret < 0) {
379 err("get_port_status(%d) failed (err = %d)", port + 1, ret);
380 return;
383 portstatus = le16_to_cpu(portsts.wPortStatus);
384 portchange = le16_to_cpu(portsts.wPortChange);
385 dbg("portstatus %x, change %x, %s", portstatus, portchange,
386 portstatus&(1<<USB_PORT_FEAT_LOWSPEED) ? "1.5 Mb/s" : "12 Mb/s");
388 /* Clear the connection change status */
389 usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
391 /* Disconnect any existing devices under this port */
392 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
393 (!(portstatus & USB_PORT_STAT_ENABLE)))|| (hub->children[port])) {
394 usb_disconnect(&hub->children[port]);
395 /* Return now if nothing is connected */
396 if (!(portstatus & USB_PORT_STAT_CONNECTION))
397 return;
399 wait_ms(400);
401 down(&usb_address0_sem);
403 #define MAX_TRIES 5
404 /* Reset the port */
405 for (tries = 0; tries < MAX_TRIES ; tries++) {
406 usb_set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
407 wait_ms(200);
409 ret = usb_get_port_status(hub, port + 1, &portsts);
410 if (ret < 0) {
411 err("get_port_status(%d) failed (err = %d)", port + 1, ret);
412 goto out;
415 portstatus = le16_to_cpu(portsts.wPortStatus);
416 portchange = le16_to_cpu(portsts.wPortChange);
417 dbg("portstatus %x, change %x, %s", portstatus ,portchange,
418 portstatus&(1<<USB_PORT_FEAT_LOWSPEED) ? "1.5 Mb/s" : "12 Mb/s");
420 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
421 !(portstatus & USB_PORT_STAT_CONNECTION))
422 goto out;
424 if (portstatus & USB_PORT_STAT_ENABLE)
425 break;
427 wait_ms(200);
430 if (tries >= MAX_TRIES) {
431 err("Cannot enable port %i after %i retries, disabling port.", port+1, MAX_TRIES);
432 err("Maybe the USB cable is bad?");
433 goto out;
436 usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_RESET);
438 /* Allocate a new device struct for it */
439 usb = usb_alloc_dev(hub, hub->bus);
440 if (!usb) {
441 err("couldn't allocate usb_device");
442 goto out;
445 usb->slow = (portstatus & USB_PORT_STAT_LOW_SPEED) ? 1 : 0;
447 hub->children[port] = usb;
449 /* Find a new device ID for it */
450 usb_connect(usb);
452 /* Run it through the hoops (find a driver, etc) */
453 ret = usb_new_device(usb);
454 if (ret) {
455 /* Try resetting the device. Windows does this and it */
456 /* gets some devices working correctly */
457 usb_set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
459 ret = usb_new_device(usb);
460 if (ret) {
461 usb_disconnect(&hub->children[port]);
463 /* Woops, disable the port */
464 dbg("hub: disabling port %d", port + 1);
465 usb_clear_port_feature(hub, port + 1,
466 USB_PORT_FEAT_ENABLE);
470 out:
471 up(&usb_address0_sem);
474 static void usb_hub_events(void)
476 unsigned long flags;
477 int i;
478 struct list_head *tmp;
479 struct usb_device *dev;
480 struct usb_hub *hub;
481 struct usb_hub_status hubsts;
482 unsigned short hubstatus, hubchange;
485 * We restart the list everytime to avoid a deadlock with
486 * deleting hubs downstream from this one. This should be
487 * safe since we delete the hub from the event list.
488 * Not the most efficient, but avoids deadlocks.
490 while (1) {
491 spin_lock_irqsave(&hub_event_lock, flags);
493 if (list_empty(&hub_event_list))
494 goto he_unlock;
496 /* Grab the next entry from the beginning of the list */
497 tmp = hub_event_list.next;
499 hub = list_entry(tmp, struct usb_hub, event_list);
500 dev = hub->dev;
502 list_del(tmp);
503 INIT_LIST_HEAD(tmp);
505 spin_unlock_irqrestore(&hub_event_lock, flags);
507 for (i = 0; i < hub->nports; i++) {
508 struct usb_port_status portsts;
509 unsigned short portstatus, portchange;
511 if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
512 err("get_port_status failed");
513 continue;
516 portstatus = le16_to_cpu(portsts.wPortStatus);
517 portchange = le16_to_cpu(portsts.wPortChange);
519 if (portchange & USB_PORT_STAT_C_CONNECTION) {
520 dbg("port %d connection change", i + 1);
522 usb_hub_port_connect_change(dev, i);
525 if (portchange & USB_PORT_STAT_C_ENABLE) {
526 dbg("port %d enable change, status %x", i + 1, portstatus);
527 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
529 // EM interference sometimes causes bad shielded USB devices to
530 // be shutdown by the hub, this hack enables them again.
531 // Works at least with mouse driver.
532 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
533 (portstatus & USB_PORT_STAT_CONNECTION) && (dev->children[i])) {
534 err("already running port %i disabled by hub (EMI?), re-enabling...",
535 i + 1);
536 usb_hub_port_connect_change(dev, i);
540 if (portstatus & USB_PORT_STAT_SUSPEND) {
541 dbg("port %d suspend change", i + 1);
542 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
545 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
546 err("port %d over-current change", i + 1);
547 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
548 usb_hub_power_on(hub);
551 if (portchange & USB_PORT_STAT_C_RESET) {
552 dbg("port %d reset change", i + 1);
553 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
555 } /* end for i */
557 /* deal with hub status changes */
558 if (usb_get_hub_status(dev, &hubsts) < 0) {
559 err("get_hub_status failed");
560 } else {
561 hubstatus = le16_to_cpup(&hubsts.wHubStatus);
562 hubchange = le16_to_cpup(&hubsts.wHubChange);
563 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
564 dbg("hub power change");
565 usb_clear_hub_feature(dev, C_HUB_LOCAL_POWER);
567 if (hubchange & HUB_CHANGE_OVERCURRENT) {
568 dbg("hub overcurrent change");
569 wait_ms(500); //Cool down
570 usb_clear_hub_feature(dev, C_HUB_OVER_CURRENT);
571 usb_hub_power_on(hub);
574 } /* end while (1) */
576 he_unlock:
577 spin_unlock_irqrestore(&hub_event_lock, flags);
580 static int usb_hub_thread(void *__hub)
582 khubd_running = 1;
584 lock_kernel();
587 * This thread doesn't need any user-level access,
588 * so get rid of all our resources
590 exit_files(current); /* daemonize doesn't do exit_files */
591 current->files = init_task.files;
592 atomic_inc(&current->files->count);
593 daemonize();
595 /* Setup a nice name */
596 strcpy(current->comm, "khubd");
598 /* Send me a signal to get me die (for debugging) */
599 do {
600 usb_hub_events();
601 interruptible_sleep_on(&khubd_wait);
602 } while (!signal_pending(current));
604 dbg("usb_hub_thread exiting");
605 khubd_running = 0;
607 return 0;
610 static struct usb_driver hub_driver = {
611 name: "hub",
612 probe: hub_probe,
613 ioctl: hub_ioctl,
614 disconnect: hub_disconnect
618 * This should be a separate module.
620 int usb_hub_init(void)
622 int pid;
624 if (usb_register(&hub_driver) < 0) {
625 err("Unable to register USB hub driver");
626 return -1;
629 pid = kernel_thread(usb_hub_thread, NULL,
630 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
631 if (pid >= 0) {
632 khubd_pid = pid;
634 return 0;
637 /* Fall through if kernel_thread failed */
638 usb_deregister(&hub_driver);
640 return -1;
643 void usb_hub_cleanup(void)
645 int ret;
647 /* Kill the thread */
648 ret = kill_proc(khubd_pid, SIGTERM, 1);
649 if (!ret) {
650 /* Wait 10 seconds */
651 int count = 10 * 100;
653 while (khubd_running && --count) {
654 current->state = TASK_INTERRUPTIBLE;
655 schedule_timeout(1);
658 if (!count)
659 err("giving up on killing khubd");
663 * Hub resources are freed for us by usb_deregister. It
664 * usb_driver_purge on every device which in turn calls that
665 * devices disconnect function if it is using this driver.
666 * The hub_disconnect function takes care of releasing the
667 * individual hub resources. -greg
669 usb_deregister(&hub_driver);
670 } /* usb_hub_cleanup() */
673 * WARNING - If a driver calls usb_reset_device, you should simulate a
674 * disconnect() and probe() for other interfaces you doesn't claim. This
675 * is left up to the driver writer right now. This insures other drivers
676 * have a chance to re-setup their interface.
678 * Take a look at proc_resetdevice in devio.c for some sample code to
679 * do this.
681 int usb_reset_device(struct usb_device *dev)
683 struct usb_device *parent = dev->parent;
684 struct usb_device_descriptor descriptor;
685 int i, ret, port = -1;
687 if (!parent) {
688 err("attempting to reset root hub!");
689 return -EINVAL;
692 for (i = 0; i < parent->maxchild; i++)
693 if (parent->children[i] == dev) {
694 port = i;
695 break;
698 if (port < 0)
699 return -ENOENT;
701 down(&usb_address0_sem);
703 /* Send a reset to the device */
704 usb_set_port_feature(parent, port + 1, USB_PORT_FEAT_RESET);
706 wait_ms(200);
708 usb_clear_port_feature(parent, port + 1, USB_PORT_FEAT_C_RESET);
710 /* Reprogram the Address */
711 ret = usb_set_address(dev);
712 if (ret < 0) {
713 err("USB device not accepting new address (error=%d)", ret);
714 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
715 dev->devnum = -1;
716 up(&usb_address0_sem);
717 return ret;
720 wait_ms(10); /* Let the SET_ADDRESS settle */
722 up(&usb_address0_sem);
725 * Now we fetch the configuration descriptors for the device and
726 * see if anything has changed. If it has, we dump the current
727 * parsed descriptors and reparse from scratch. Then we leave
728 * the device alone for the caller to finish setting up.
730 * If nothing changed, we reprogram the configuration and then
731 * the alternate settings.
733 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &descriptor,
734 sizeof(descriptor));
735 if (ret < 0)
736 return ret;
738 le16_to_cpus(&descriptor.bcdUSB);
739 le16_to_cpus(&descriptor.idVendor);
740 le16_to_cpus(&descriptor.idProduct);
741 le16_to_cpus(&descriptor.bcdDevice);
743 if (memcmp(&dev->descriptor, &descriptor, sizeof(descriptor))) {
744 usb_destroy_configuration(dev);
746 ret = usb_get_device_descriptor(dev);
747 if (ret < sizeof(dev->descriptor)) {
748 if (ret < 0)
749 err("unable to get device descriptor (error=%d)", ret);
750 else
751 err("USB device descriptor short read (expected %i, got %i)", sizeof(dev->descriptor), ret);
753 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
754 dev->devnum = -1;
755 return -EIO;
758 ret = usb_get_configuration(dev);
759 if (ret < 0) {
760 err("unable to get configuration (error=%d)", ret);
761 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
762 dev->devnum = -1;
763 return 1;
766 dev->actconfig = dev->config;
767 usb_set_maxpacket(dev);
769 return 1;
770 } else {
771 ret = usb_set_configuration(dev,
772 dev->actconfig->bConfigurationValue);
773 if (ret < 0) {
774 err("failed to set active configuration (error=%d)",
775 ret);
776 return ret;
779 for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
780 struct usb_interface *intf =
781 &dev->actconfig->interface[i];
782 struct usb_interface_descriptor *as =
783 &intf->altsetting[intf->act_altsetting];
785 ret = usb_set_interface(dev, as->bInterfaceNumber,
786 as->bAlternateSetting);
787 if (ret < 0) {
788 err("failed to set active alternate setting for interface %d (error=%d)", i, ret);
789 return ret;
793 return 0;
796 return 0;