1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/version.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/completion.h>
30#include <linux/utsname.h>
31#include <linux/mm.h>
32#include <asm/io.h>
33#include <linux/device.h>
34#include <linux/dma-mapping.h>
35#include <linux/mutex.h>
36#include <asm/irq.h>
37#include <asm/byteorder.h>
38#include <asm/unaligned.h>
39#include <linux/platform_device.h>
40#include <linux/workqueue.h>
41
42#include <linux/usb.h>
43
44#include "usb.h"
45#include "hcd.h"
46#include "hub.h"
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85unsigned long usb_hcds_loaded;
86EXPORT_SYMBOL_GPL(usb_hcds_loaded);
87
88
89LIST_HEAD (usb_bus_list);
90EXPORT_SYMBOL_GPL (usb_bus_list);
91
92
93#define USB_MAXBUS 64
94struct usb_busmap {
95 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
96};
97static struct usb_busmap busmap;
98
99
100DEFINE_MUTEX(usb_bus_list_lock);
101EXPORT_SYMBOL_GPL (usb_bus_list_lock);
102
103
104static DEFINE_SPINLOCK(hcd_root_hub_lock);
105
106
107static DEFINE_SPINLOCK(hcd_urb_list_lock);
108
109
110static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
111
112
113DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
114
115static inline int is_root_hub(struct usb_device *udev)
116{
117 return (udev->parent == NULL);
118}
119
120
121
122
123
124
125
126
127
128#define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
129#define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
130
131
132static const u8 usb3_rh_dev_descriptor[18] = {
133 0x12,
134 0x01,
135 0x00, 0x03,
136
137 0x09,
138 0x00,
139 0x03,
140 0x09,
141
142 0x6b, 0x1d,
143 0x02, 0x00,
144 KERNEL_VER, KERNEL_REL,
145
146 0x03,
147 0x02,
148 0x01,
149 0x01
150};
151
152
153static const u8 usb2_rh_dev_descriptor [18] = {
154 0x12,
155 0x01,
156 0x00, 0x02,
157
158 0x09,
159 0x00,
160 0x00,
161 0x40,
162
163 0x6b, 0x1d,
164 0x02, 0x00,
165 KERNEL_VER, KERNEL_REL,
166
167 0x03,
168 0x02,
169 0x01,
170 0x01
171};
172
173
174
175
176static const u8 usb11_rh_dev_descriptor [18] = {
177 0x12,
178 0x01,
179 0x10, 0x01,
180
181 0x09,
182 0x00,
183 0x00,
184 0x40,
185
186 0x6b, 0x1d,
187 0x01, 0x00,
188 KERNEL_VER, KERNEL_REL,
189
190 0x03,
191 0x02,
192 0x01,
193 0x01
194};
195
196
197
198
199
200
201static const u8 fs_rh_config_descriptor [] = {
202
203
204 0x09,
205 0x02,
206 0x19, 0x00,
207 0x01,
208 0x01,
209 0x00,
210 0xc0,
211
212
213
214
215 0x00,
216
217
218
219
220
221
222
223
224
225
226
227
228
229 0x09,
230 0x04,
231 0x00,
232 0x00,
233 0x01,
234 0x09,
235 0x00,
236 0x00,
237 0x00,
238
239
240 0x07,
241 0x05,
242 0x81,
243 0x03,
244 0x02, 0x00,
245 0xff
246};
247
248static const u8 hs_rh_config_descriptor [] = {
249
250
251 0x09,
252 0x02,
253 0x19, 0x00,
254 0x01,
255 0x01,
256 0x00,
257 0xc0,
258
259
260
261
262 0x00,
263
264
265
266
267
268
269
270
271
272
273
274
275
276 0x09,
277 0x04,
278 0x00,
279 0x00,
280 0x01,
281 0x09,
282 0x00,
283 0x00,
284 0x00,
285
286
287 0x07,
288 0x05,
289 0x81,
290 0x03,
291
292
293 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
294 0x0c
295};
296
297static const u8 ss_rh_config_descriptor[] = {
298
299 0x09,
300 0x02,
301 0x19, 0x00,
302 0x01,
303 0x01,
304 0x00,
305 0xc0,
306
307
308
309
310 0x00,
311
312
313 0x09,
314 0x04,
315 0x00,
316 0x00,
317 0x01,
318 0x09,
319 0x00,
320 0x00,
321 0x00,
322
323
324 0x07,
325 0x05,
326 0x81,
327 0x03,
328
329
330 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
331 0x0c
332
333
334
335
336};
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352static unsigned
353ascii2desc(char const *s, u8 *buf, unsigned len)
354{
355 unsigned n, t = 2 + 2*strlen(s);
356
357 if (t > 254)
358 t = 254;
359 if (len > t)
360 len = t;
361
362 t += USB_DT_STRING << 8;
363
364 n = len;
365 while (n--) {
366 *buf++ = t;
367 if (!n--)
368 break;
369 *buf++ = t >> 8;
370 t = (unsigned char)*s++;
371 }
372 return len;
373}
374
375
376
377
378
379
380
381
382
383
384
385
386
387static unsigned
388rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
389{
390 char buf[100];
391 char const *s;
392 static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
393
394
395 switch (id) {
396 case 0:
397
398
399 if (len > 4)
400 len = 4;
401 memcpy(data, langids, len);
402 return len;
403 case 1:
404
405 s = hcd->self.bus_name;
406 break;
407 case 2:
408
409 s = hcd->product_desc;
410 break;
411 case 3:
412
413 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
414 init_utsname()->release, hcd->driver->description);
415 s = buf;
416 break;
417 default:
418
419 return 0;
420 }
421
422 return ascii2desc(s, data, len);
423}
424
425
426
427static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
428{
429 struct usb_ctrlrequest *cmd;
430 u16 typeReq, wValue, wIndex, wLength;
431 u8 *ubuf = urb->transfer_buffer;
432 u8 tbuf [sizeof (struct usb_hub_descriptor)]
433 __attribute__((aligned(4)));
434 const u8 *bufp = tbuf;
435 unsigned len = 0;
436 int status;
437 u8 patch_wakeup = 0;
438 u8 patch_protocol = 0;
439
440 might_sleep();
441
442 spin_lock_irq(&hcd_root_hub_lock);
443 status = usb_hcd_link_urb_to_ep(hcd, urb);
444 spin_unlock_irq(&hcd_root_hub_lock);
445 if (status)
446 return status;
447 urb->hcpriv = hcd;
448
449 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
450 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
451 wValue = le16_to_cpu (cmd->wValue);
452 wIndex = le16_to_cpu (cmd->wIndex);
453 wLength = le16_to_cpu (cmd->wLength);
454
455 if (wLength > urb->transfer_buffer_length)
456 goto error;
457
458 urb->actual_length = 0;
459 switch (typeReq) {
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479 case DeviceRequest | USB_REQ_GET_STATUS:
480 tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
481 << USB_DEVICE_REMOTE_WAKEUP)
482 | (1 << USB_DEVICE_SELF_POWERED);
483 tbuf [1] = 0;
484 len = 2;
485 break;
486 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
487 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
488 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
489 else
490 goto error;
491 break;
492 case DeviceOutRequest | USB_REQ_SET_FEATURE:
493 if (device_can_wakeup(&hcd->self.root_hub->dev)
494 && wValue == USB_DEVICE_REMOTE_WAKEUP)
495 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
496 else
497 goto error;
498 break;
499 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
500 tbuf [0] = 1;
501 len = 1;
502
503 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
504 break;
505 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
506 switch (wValue & 0xff00) {
507 case USB_DT_DEVICE << 8:
508 switch (hcd->driver->flags & HCD_MASK) {
509 case HCD_USB3:
510 bufp = usb3_rh_dev_descriptor;
511 break;
512 case HCD_USB2:
513 bufp = usb2_rh_dev_descriptor;
514 break;
515 case HCD_USB11:
516 bufp = usb11_rh_dev_descriptor;
517 break;
518 default:
519 goto error;
520 }
521 len = 18;
522 if (hcd->has_tt)
523 patch_protocol = 1;
524 break;
525 case USB_DT_CONFIG << 8:
526 switch (hcd->driver->flags & HCD_MASK) {
527 case HCD_USB3:
528 bufp = ss_rh_config_descriptor;
529 len = sizeof ss_rh_config_descriptor;
530 break;
531 case HCD_USB2:
532 bufp = hs_rh_config_descriptor;
533 len = sizeof hs_rh_config_descriptor;
534 break;
535 case HCD_USB11:
536 bufp = fs_rh_config_descriptor;
537 len = sizeof fs_rh_config_descriptor;
538 break;
539 default:
540 goto error;
541 }
542 if (device_can_wakeup(&hcd->self.root_hub->dev))
543 patch_wakeup = 1;
544 break;
545 case USB_DT_STRING << 8:
546 if ((wValue & 0xff) < 4)
547 urb->actual_length = rh_string(wValue & 0xff,
548 hcd, ubuf, wLength);
549 else
550 goto error;
551 break;
552 default:
553 goto error;
554 }
555 break;
556 case DeviceRequest | USB_REQ_GET_INTERFACE:
557 tbuf [0] = 0;
558 len = 1;
559
560 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
561 break;
562 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
563
564 dev_dbg (hcd->self.controller, "root hub device address %d\n",
565 wValue);
566 break;
567
568
569
570
571
572 case EndpointRequest | USB_REQ_GET_STATUS:
573
574 tbuf [0] = 0;
575 tbuf [1] = 0;
576 len = 2;
577
578 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
579 case EndpointOutRequest | USB_REQ_SET_FEATURE:
580 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
581 break;
582
583
584
585 default:
586
587 switch (typeReq) {
588 case GetHubStatus:
589 case GetPortStatus:
590 len = 4;
591 break;
592 case GetHubDescriptor:
593 len = sizeof (struct usb_hub_descriptor);
594 break;
595 }
596 status = hcd->driver->hub_control (hcd,
597 typeReq, wValue, wIndex,
598 tbuf, wLength);
599 break;
600error:
601
602 status = -EPIPE;
603 }
604
605 if (status) {
606 len = 0;
607 if (status != -EPIPE) {
608 dev_dbg (hcd->self.controller,
609 "CTRL: TypeReq=0x%x val=0x%x "
610 "idx=0x%x len=%d ==> %d\n",
611 typeReq, wValue, wIndex,
612 wLength, status);
613 }
614 }
615 if (len) {
616 if (urb->transfer_buffer_length < len)
617 len = urb->transfer_buffer_length;
618 urb->actual_length = len;
619
620 memcpy (ubuf, bufp, len);
621
622
623 if (patch_wakeup &&
624 len > offsetof (struct usb_config_descriptor,
625 bmAttributes))
626 ((struct usb_config_descriptor *)ubuf)->bmAttributes
627 |= USB_CONFIG_ATT_WAKEUP;
628
629
630 if (patch_protocol &&
631 len > offsetof(struct usb_device_descriptor,
632 bDeviceProtocol))
633 ((struct usb_device_descriptor *) ubuf)->
634 bDeviceProtocol = 1;
635 }
636
637
638 spin_lock_irq(&hcd_root_hub_lock);
639 usb_hcd_unlink_urb_from_ep(hcd, urb);
640
641
642
643
644
645 spin_unlock(&hcd_root_hub_lock);
646 usb_hcd_giveback_urb(hcd, urb, status);
647 spin_lock(&hcd_root_hub_lock);
648
649 spin_unlock_irq(&hcd_root_hub_lock);
650 return 0;
651}
652
653
654
655
656
657
658
659
660
661
662
663void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
664{
665 struct urb *urb;
666 int length;
667 unsigned long flags;
668 char buffer[6];
669
670 if (unlikely(!hcd->rh_registered))
671 return;
672 if (!hcd->uses_new_polling && !hcd->status_urb)
673 return;
674
675 length = hcd->driver->hub_status_data(hcd, buffer);
676 if (length > 0) {
677
678
679 spin_lock_irqsave(&hcd_root_hub_lock, flags);
680 urb = hcd->status_urb;
681 if (urb) {
682 hcd->poll_pending = 0;
683 hcd->status_urb = NULL;
684 urb->actual_length = length;
685 memcpy(urb->transfer_buffer, buffer, length);
686
687 usb_hcd_unlink_urb_from_ep(hcd, urb);
688 spin_unlock(&hcd_root_hub_lock);
689 usb_hcd_giveback_urb(hcd, urb, 0);
690 spin_lock(&hcd_root_hub_lock);
691 } else {
692 length = 0;
693 hcd->poll_pending = 1;
694 }
695 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
696 }
697
698
699
700
701
702 if (hcd->uses_new_polling ? hcd->poll_rh :
703 (length == 0 && hcd->status_urb != NULL))
704 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
705}
706EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
707
708
709static void rh_timer_func (unsigned long _hcd)
710{
711 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
712}
713
714
715
716static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
717{
718 int retval;
719 unsigned long flags;
720 unsigned len = 1 + (urb->dev->maxchild / 8);
721
722 spin_lock_irqsave (&hcd_root_hub_lock, flags);
723 if (hcd->status_urb || urb->transfer_buffer_length < len) {
724 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
725 retval = -EINVAL;
726 goto done;
727 }
728
729 retval = usb_hcd_link_urb_to_ep(hcd, urb);
730 if (retval)
731 goto done;
732
733 hcd->status_urb = urb;
734 urb->hcpriv = hcd;
735 if (!hcd->uses_new_polling)
736 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
737
738
739 else if (hcd->poll_pending)
740 mod_timer(&hcd->rh_timer, jiffies);
741 retval = 0;
742 done:
743 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
744 return retval;
745}
746
747static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
748{
749 if (usb_endpoint_xfer_int(&urb->ep->desc))
750 return rh_queue_status (hcd, urb);
751 if (usb_endpoint_xfer_control(&urb->ep->desc))
752 return rh_call_control (hcd, urb);
753 return -EINVAL;
754}
755
756
757
758
759
760
761static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
762{
763 unsigned long flags;
764 int rc;
765
766 spin_lock_irqsave(&hcd_root_hub_lock, flags);
767 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
768 if (rc)
769 goto done;
770
771 if (usb_endpoint_num(&urb->ep->desc) == 0) {
772 ;
773
774 } else {
775 if (!hcd->uses_new_polling)
776 del_timer (&hcd->rh_timer);
777 if (urb == hcd->status_urb) {
778 hcd->status_urb = NULL;
779 usb_hcd_unlink_urb_from_ep(hcd, urb);
780
781 spin_unlock(&hcd_root_hub_lock);
782 usb_hcd_giveback_urb(hcd, urb, status);
783 spin_lock(&hcd_root_hub_lock);
784 }
785 }
786 done:
787 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
788 return rc;
789}
790
791
792
793
794
795
796static ssize_t usb_host_authorized_default_show(struct device *dev,
797 struct device_attribute *attr,
798 char *buf)
799{
800 struct usb_device *rh_usb_dev = to_usb_device(dev);
801 struct usb_bus *usb_bus = rh_usb_dev->bus;
802 struct usb_hcd *usb_hcd;
803
804 if (usb_bus == NULL)
805 return -ENODEV;
806 usb_hcd = bus_to_hcd(usb_bus);
807 return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
808}
809
810static ssize_t usb_host_authorized_default_store(struct device *dev,
811 struct device_attribute *attr,
812 const char *buf, size_t size)
813{
814 ssize_t result;
815 unsigned val;
816 struct usb_device *rh_usb_dev = to_usb_device(dev);
817 struct usb_bus *usb_bus = rh_usb_dev->bus;
818 struct usb_hcd *usb_hcd;
819
820 if (usb_bus == NULL)
821 return -ENODEV;
822 usb_hcd = bus_to_hcd(usb_bus);
823 result = sscanf(buf, "%u\n", &val);
824 if (result == 1) {
825 usb_hcd->authorized_default = val? 1 : 0;
826 result = size;
827 }
828 else
829 result = -EINVAL;
830 return result;
831}
832
833static DEVICE_ATTR(authorized_default, 0644,
834 usb_host_authorized_default_show,
835 usb_host_authorized_default_store);
836
837
838
839static struct attribute *usb_bus_attrs[] = {
840 &dev_attr_authorized_default.attr,
841 NULL,
842};
843
844static struct attribute_group usb_bus_attr_group = {
845 .name = NULL,
846 .attrs = usb_bus_attrs,
847};
848
849
850
851
852
853
854
855
856
857
858
859
860static void usb_bus_init (struct usb_bus *bus)
861{
862 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
863
864 bus->devnum_next = 1;
865
866 bus->root_hub = NULL;
867 bus->busnum = -1;
868 bus->bandwidth_allocated = 0;
869 bus->bandwidth_int_reqs = 0;
870 bus->bandwidth_isoc_reqs = 0;
871
872 INIT_LIST_HEAD (&bus->bus_list);
873}
874
875
876
877
878
879
880
881
882
883
884
885static int usb_register_bus(struct usb_bus *bus)
886{
887 int result = -E2BIG;
888 int busnum;
889
890 mutex_lock(&usb_bus_list_lock);
891 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
892 if (busnum >= USB_MAXBUS) {
893 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
894 goto error_find_busnum;
895 }
896 set_bit (busnum, busmap.busmap);
897 bus->busnum = busnum;
898
899
900 list_add (&bus->bus_list, &usb_bus_list);
901 mutex_unlock(&usb_bus_list_lock);
902
903 usb_notify_add_bus(bus);
904
905 dev_info (bus->controller, "new USB bus registered, assigned bus "
906 "number %d\n", bus->busnum);
907 return 0;
908
909error_find_busnum:
910 mutex_unlock(&usb_bus_list_lock);
911 return result;
912}
913
914
915
916
917
918
919
920
921
922static void usb_deregister_bus (struct usb_bus *bus)
923{
924 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
925
926
927
928
929
930
931 mutex_lock(&usb_bus_list_lock);
932 list_del (&bus->bus_list);
933 mutex_unlock(&usb_bus_list_lock);
934
935 usb_notify_remove_bus(bus);
936
937 clear_bit (bus->busnum, busmap.busmap);
938}
939
940
941
942
943
944
945
946
947
948
949static int register_root_hub(struct usb_hcd *hcd)
950{
951 struct device *parent_dev = hcd->self.controller;
952 struct usb_device *usb_dev = hcd->self.root_hub;
953 const int devnum = 1;
954 int retval;
955
956 usb_dev->devnum = devnum;
957 usb_dev->bus->devnum_next = devnum + 1;
958 memset (&usb_dev->bus->devmap.devicemap, 0,
959 sizeof usb_dev->bus->devmap.devicemap);
960 set_bit (devnum, usb_dev->bus->devmap.devicemap);
961 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
962
963 mutex_lock(&usb_bus_list_lock);
964
965 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
966 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
967 if (retval != sizeof usb_dev->descriptor) {
968 mutex_unlock(&usb_bus_list_lock);
969 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
970 dev_name(&usb_dev->dev), retval);
971 return (retval < 0) ? retval : -EMSGSIZE;
972 }
973
974 retval = usb_new_device (usb_dev);
975 if (retval) {
976 dev_err (parent_dev, "can't register root hub for %s, %d\n",
977 dev_name(&usb_dev->dev), retval);
978 }
979 mutex_unlock(&usb_bus_list_lock);
980
981 if (retval == 0) {
982 spin_lock_irq (&hcd_root_hub_lock);
983 hcd->rh_registered = 1;
984 spin_unlock_irq (&hcd_root_hub_lock);
985
986
987 if (hcd->state == HC_STATE_HALT)
988 usb_hc_died (hcd);
989 }
990
991 return retval;
992}
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
1009{
1010 unsigned long tmp;
1011
1012 switch (speed) {
1013 case USB_SPEED_LOW:
1014 if (is_input) {
1015 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
1016 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
1017 } else {
1018 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
1019 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
1020 }
1021 case USB_SPEED_FULL:
1022 if (isoc) {
1023 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1024 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
1025 } else {
1026 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1027 return (9107L + BW_HOST_DELAY + tmp);
1028 }
1029 case USB_SPEED_HIGH:
1030
1031 if (isoc)
1032 tmp = HS_NSECS_ISO (bytecount);
1033 else
1034 tmp = HS_NSECS (bytecount);
1035 return tmp;
1036 default:
1037 pr_debug ("%s: bogus device speed!\n", usbcore_name);
1038 return -1;
1039 }
1040}
1041EXPORT_SYMBOL_GPL(usb_calc_bus_time);
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
1068{
1069 int rc = 0;
1070
1071 spin_lock(&hcd_urb_list_lock);
1072
1073
1074 if (unlikely(atomic_read(&urb->reject))) {
1075 rc = -EPERM;
1076 goto done;
1077 }
1078
1079 if (unlikely(!urb->ep->enabled)) {
1080 rc = -ENOENT;
1081 goto done;
1082 }
1083
1084 if (unlikely(!urb->dev->can_submit)) {
1085 rc = -EHOSTUNREACH;
1086 goto done;
1087 }
1088
1089
1090
1091
1092
1093 switch (hcd->state) {
1094 case HC_STATE_RUNNING:
1095 case HC_STATE_RESUMING:
1096 urb->unlinked = 0;
1097 list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1098 break;
1099 default:
1100 rc = -ESHUTDOWN;
1101 goto done;
1102 }
1103 done:
1104 spin_unlock(&hcd_urb_list_lock);
1105 return rc;
1106}
1107EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
1129 int status)
1130{
1131 struct list_head *tmp;
1132
1133
1134 list_for_each(tmp, &urb->ep->urb_list) {
1135 if (tmp == &urb->urb_list)
1136 break;
1137 }
1138 if (tmp != &urb->urb_list)
1139 return -EIDRM;
1140
1141
1142
1143
1144 if (urb->unlinked)
1145 return -EBUSY;
1146 urb->unlinked = status;
1147
1148
1149
1150
1151
1152
1153 if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) &&
1154 !is_root_hub(urb->dev)) {
1155 dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
1156 "Controller is probably using the wrong IRQ.\n");
1157 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1158 }
1159
1160 return 0;
1161}
1162EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
1175{
1176
1177 spin_lock(&hcd_urb_list_lock);
1178 list_del_init(&urb->urb_list);
1179 spin_unlock(&hcd_urb_list_lock);
1180}
1181EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215static int hcd_alloc_coherent(struct usb_bus *bus,
1216 gfp_t mem_flags, dma_addr_t *dma_handle,
1217 void **vaddr_handle, size_t size,
1218 enum dma_data_direction dir)
1219{
1220 unsigned char *vaddr;
1221
1222 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
1223 mem_flags, dma_handle);
1224 if (!vaddr)
1225 return -ENOMEM;
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 put_unaligned((unsigned long)*vaddr_handle,
1236 (unsigned long *)(vaddr + size));
1237
1238 if (dir == DMA_TO_DEVICE)
1239 memcpy(vaddr, *vaddr_handle, size);
1240
1241 *vaddr_handle = vaddr;
1242 return 0;
1243}
1244
1245static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
1246 void **vaddr_handle, size_t size,
1247 enum dma_data_direction dir)
1248{
1249 unsigned char *vaddr = *vaddr_handle;
1250
1251 vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
1252
1253 if (dir == DMA_FROM_DEVICE)
1254 memcpy(vaddr, *vaddr_handle, size);
1255
1256 hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
1257
1258 *vaddr_handle = vaddr;
1259 *dma_handle = 0;
1260}
1261
1262static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1263 gfp_t mem_flags)
1264{
1265 enum dma_data_direction dir;
1266 int ret = 0;
1267
1268
1269
1270
1271
1272
1273 if (is_root_hub(urb->dev))
1274 return 0;
1275
1276 if (usb_endpoint_xfer_control(&urb->ep->desc)
1277 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1278 if (hcd->self.uses_dma)
1279 urb->setup_dma = dma_map_single(
1280 hcd->self.controller,
1281 urb->setup_packet,
1282 sizeof(struct usb_ctrlrequest),
1283 DMA_TO_DEVICE);
1284 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1285 ret = hcd_alloc_coherent(
1286 urb->dev->bus, mem_flags,
1287 &urb->setup_dma,
1288 (void **)&urb->setup_packet,
1289 sizeof(struct usb_ctrlrequest),
1290 DMA_TO_DEVICE);
1291 }
1292
1293 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1294 if (ret == 0 && urb->transfer_buffer_length != 0
1295 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1296 if (hcd->self.uses_dma)
1297 urb->transfer_dma = dma_map_single (
1298 hcd->self.controller,
1299 urb->transfer_buffer,
1300 urb->transfer_buffer_length,
1301 dir);
1302 else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1303 ret = hcd_alloc_coherent(
1304 urb->dev->bus, mem_flags,
1305 &urb->transfer_dma,
1306 &urb->transfer_buffer,
1307 urb->transfer_buffer_length,
1308 dir);
1309
1310 if (ret && usb_endpoint_xfer_control(&urb->ep->desc)
1311 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1312 hcd_free_coherent(urb->dev->bus,
1313 &urb->setup_dma,
1314 (void **)&urb->setup_packet,
1315 sizeof(struct usb_ctrlrequest),
1316 DMA_TO_DEVICE);
1317 }
1318 }
1319 return ret;
1320}
1321
1322static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1323{
1324 enum dma_data_direction dir;
1325
1326 if (is_root_hub(urb->dev))
1327 return;
1328
1329 if (usb_endpoint_xfer_control(&urb->ep->desc)
1330 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1331 if (hcd->self.uses_dma)
1332 dma_unmap_single(hcd->self.controller, urb->setup_dma,
1333 sizeof(struct usb_ctrlrequest),
1334 DMA_TO_DEVICE);
1335 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1336 hcd_free_coherent(urb->dev->bus, &urb->setup_dma,
1337 (void **)&urb->setup_packet,
1338 sizeof(struct usb_ctrlrequest),
1339 DMA_TO_DEVICE);
1340 }
1341
1342 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1343 if (urb->transfer_buffer_length != 0
1344 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1345 if (hcd->self.uses_dma)
1346 dma_unmap_single(hcd->self.controller,
1347 urb->transfer_dma,
1348 urb->transfer_buffer_length,
1349 dir);
1350 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1351 hcd_free_coherent(urb->dev->bus, &urb->transfer_dma,
1352 &urb->transfer_buffer,
1353 urb->transfer_buffer_length,
1354 dir);
1355 }
1356}
1357
1358
1359
1360
1361
1362
1363
1364
1365int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1366{
1367 int status;
1368 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1369
1370
1371
1372
1373
1374 usb_get_urb(urb);
1375 atomic_inc(&urb->use_count);
1376 atomic_inc(&urb->dev->urbnum);
1377 usbmon_urb_submit(&hcd->self, urb);
1378
1379
1380
1381
1382
1383
1384
1385
1386 status = map_urb_for_dma(hcd, urb, mem_flags);
1387 if (unlikely(status)) {
1388 usbmon_urb_submit_error(&hcd->self, urb, status);
1389 goto error;
1390 }
1391
1392 if (is_root_hub(urb->dev))
1393 status = rh_urb_enqueue(hcd, urb);
1394 else
1395 status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
1396
1397 if (unlikely(status)) {
1398 usbmon_urb_submit_error(&hcd->self, urb, status);
1399 unmap_urb_for_dma(hcd, urb);
1400 error:
1401 urb->hcpriv = NULL;
1402 INIT_LIST_HEAD(&urb->urb_list);
1403 atomic_dec(&urb->use_count);
1404 atomic_dec(&urb->dev->urbnum);
1405 if (atomic_read(&urb->reject))
1406 wake_up(&usb_kill_urb_queue);
1407 usb_put_urb(urb);
1408 }
1409 return status;
1410}
1411
1412
1413
1414
1415
1416
1417
1418
1419static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
1420{
1421 int value;
1422
1423 if (is_root_hub(urb->dev))
1424 value = usb_rh_urb_dequeue(hcd, urb, status);
1425 else {
1426
1427
1428
1429
1430 value = hcd->driver->urb_dequeue(hcd, urb, status);
1431 }
1432 return value;
1433}
1434
1435
1436
1437
1438
1439
1440
1441int usb_hcd_unlink_urb (struct urb *urb, int status)
1442{
1443 struct usb_hcd *hcd;
1444 int retval = -EIDRM;
1445 unsigned long flags;
1446
1447
1448
1449
1450
1451
1452 spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1453 if (atomic_read(&urb->use_count) > 0) {
1454 retval = 0;
1455 usb_get_dev(urb->dev);
1456 }
1457 spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1458 if (retval == 0) {
1459 hcd = bus_to_hcd(urb->dev->bus);
1460 retval = unlink1(hcd, urb, status);
1461 usb_put_dev(urb->dev);
1462 }
1463
1464 if (retval == 0)
1465 retval = -EINPROGRESS;
1466 else if (retval != -EIDRM && retval != -EBUSY)
1467 dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
1468 urb, retval);
1469 return retval;
1470}
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
1492{
1493 urb->hcpriv = NULL;
1494 if (unlikely(urb->unlinked))
1495 status = urb->unlinked;
1496 else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
1497 urb->actual_length < urb->transfer_buffer_length &&
1498 !status))
1499 status = -EREMOTEIO;
1500
1501 unmap_urb_for_dma(hcd, urb);
1502 usbmon_urb_complete(&hcd->self, urb, status);
1503 usb_unanchor_urb(urb);
1504
1505
1506 urb->status = status;
1507 urb->complete (urb);
1508 atomic_dec (&urb->use_count);
1509 if (unlikely(atomic_read(&urb->reject)))
1510 wake_up (&usb_kill_urb_queue);
1511 usb_put_urb (urb);
1512}
1513EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
1514
1515
1516
1517
1518
1519
1520
1521void usb_hcd_flush_endpoint(struct usb_device *udev,
1522 struct usb_host_endpoint *ep)
1523{
1524 struct usb_hcd *hcd;
1525 struct urb *urb;
1526
1527 if (!ep)
1528 return;
1529 might_sleep();
1530 hcd = bus_to_hcd(udev->bus);
1531
1532
1533 spin_lock_irq(&hcd_urb_list_lock);
1534rescan:
1535 list_for_each_entry (urb, &ep->urb_list, urb_list) {
1536 int is_in;
1537
1538 if (urb->unlinked)
1539 continue;
1540 usb_get_urb (urb);
1541 is_in = usb_urb_dir_in(urb);
1542 spin_unlock(&hcd_urb_list_lock);
1543
1544
1545 unlink1(hcd, urb, -ESHUTDOWN);
1546 dev_dbg (hcd->self.controller,
1547 "shutdown urb %p ep%d%s%s\n",
1548 urb, usb_endpoint_num(&ep->desc),
1549 is_in ? "in" : "out",
1550 ({ char *s;
1551
1552 switch (usb_endpoint_type(&ep->desc)) {
1553 case USB_ENDPOINT_XFER_CONTROL:
1554 s = ""; break;
1555 case USB_ENDPOINT_XFER_BULK:
1556 s = "-bulk"; break;
1557 case USB_ENDPOINT_XFER_INT:
1558 s = "-intr"; break;
1559 default:
1560 s = "-iso"; break;
1561 };
1562 s;
1563 }));
1564 usb_put_urb (urb);
1565
1566
1567 spin_lock(&hcd_urb_list_lock);
1568 goto rescan;
1569 }
1570 spin_unlock_irq(&hcd_urb_list_lock);
1571
1572
1573 while (!list_empty (&ep->urb_list)) {
1574 spin_lock_irq(&hcd_urb_list_lock);
1575
1576
1577 urb = NULL;
1578 if (!list_empty (&ep->urb_list)) {
1579 urb = list_entry (ep->urb_list.prev, struct urb,
1580 urb_list);
1581 usb_get_urb (urb);
1582 }
1583 spin_unlock_irq(&hcd_urb_list_lock);
1584
1585 if (urb) {
1586 usb_kill_urb (urb);
1587 usb_put_urb (urb);
1588 }
1589 }
1590}
1591
1592
1593
1594
1595
1596
1597
1598int usb_hcd_check_bandwidth(struct usb_device *udev,
1599 struct usb_host_config *new_config,
1600 struct usb_interface *new_intf)
1601{
1602 int num_intfs, i, j;
1603 struct usb_interface_cache *intf_cache;
1604 struct usb_host_interface *alt = 0;
1605 int ret = 0;
1606 struct usb_hcd *hcd;
1607 struct usb_host_endpoint *ep;
1608
1609 hcd = bus_to_hcd(udev->bus);
1610 if (!hcd->driver->check_bandwidth)
1611 return 0;
1612
1613
1614 if (!new_config && !new_intf) {
1615 for (i = 1; i < 16; ++i) {
1616 ep = udev->ep_out[i];
1617 if (ep)
1618 hcd->driver->drop_endpoint(hcd, udev, ep);
1619 ep = udev->ep_in[i];
1620 if (ep)
1621 hcd->driver->drop_endpoint(hcd, udev, ep);
1622 }
1623 hcd->driver->check_bandwidth(hcd, udev);
1624 return 0;
1625 }
1626
1627
1628
1629
1630
1631 if (new_config) {
1632 num_intfs = new_config->desc.bNumInterfaces;
1633
1634
1635
1636 for (i = 1; i < 16; ++i) {
1637 ep = udev->ep_out[i];
1638 if (ep) {
1639 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1640 if (ret < 0)
1641 goto reset;
1642 }
1643 ep = udev->ep_in[i];
1644 if (ep) {
1645 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1646 if (ret < 0)
1647 goto reset;
1648 }
1649 }
1650 for (i = 0; i < num_intfs; ++i) {
1651
1652
1653
1654
1655 intf_cache = new_config->intf_cache[i];
1656 for (j = 0; j < intf_cache->num_altsetting; j++) {
1657 if (intf_cache->altsetting[j].desc.bAlternateSetting == 0)
1658 alt = &intf_cache->altsetting[j];
1659 }
1660 if (!alt) {
1661 printk(KERN_DEBUG "Did not find alt setting 0 for intf %d\n", i);
1662 continue;
1663 }
1664 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1665 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1666 if (ret < 0)
1667 goto reset;
1668 }
1669 }
1670 }
1671 ret = hcd->driver->check_bandwidth(hcd, udev);
1672reset:
1673 if (ret < 0)
1674 hcd->driver->reset_bandwidth(hcd, udev);
1675 return ret;
1676}
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686void usb_hcd_disable_endpoint(struct usb_device *udev,
1687 struct usb_host_endpoint *ep)
1688{
1689 struct usb_hcd *hcd;
1690
1691 might_sleep();
1692 hcd = bus_to_hcd(udev->bus);
1693 if (hcd->driver->endpoint_disable)
1694 hcd->driver->endpoint_disable(hcd, ep);
1695}
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705void usb_hcd_reset_endpoint(struct usb_device *udev,
1706 struct usb_host_endpoint *ep)
1707{
1708 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1709
1710 if (hcd->driver->endpoint_reset)
1711 hcd->driver->endpoint_reset(hcd, ep);
1712 else {
1713 int epnum = usb_endpoint_num(&ep->desc);
1714 int is_out = usb_endpoint_dir_out(&ep->desc);
1715 int is_control = usb_endpoint_xfer_control(&ep->desc);
1716
1717 usb_settoggle(udev, epnum, is_out, 0);
1718 if (is_control)
1719 usb_settoggle(udev, epnum, !is_out, 0);
1720 }
1721}
1722
1723
1724
1725
1726
1727
1728void usb_hcd_synchronize_unlinks(struct usb_device *udev)
1729{
1730 spin_lock_irq(&hcd_urb_unlink_lock);
1731 spin_unlock_irq(&hcd_urb_unlink_lock);
1732}
1733
1734
1735
1736
1737int usb_hcd_get_frame_number (struct usb_device *udev)
1738{
1739 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1740
1741 if (!HC_IS_RUNNING (hcd->state))
1742 return -ESHUTDOWN;
1743 return hcd->driver->get_frame_number (hcd);
1744}
1745
1746
1747
1748#ifdef CONFIG_PM
1749
1750int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
1751{
1752 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1753 int status;
1754 int old_state = hcd->state;
1755
1756 dev_dbg(&rhdev->dev, "bus %s%s\n",
1757 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend");
1758 if (!hcd->driver->bus_suspend) {
1759 status = -ENOENT;
1760 } else {
1761 hcd->state = HC_STATE_QUIESCING;
1762 status = hcd->driver->bus_suspend(hcd);
1763 }
1764 if (status == 0) {
1765 usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
1766 hcd->state = HC_STATE_SUSPENDED;
1767 } else {
1768 hcd->state = old_state;
1769 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
1770 "suspend", status);
1771 }
1772 return status;
1773}
1774
1775int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
1776{
1777 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1778 int status;
1779 int old_state = hcd->state;
1780
1781 dev_dbg(&rhdev->dev, "usb %s%s\n",
1782 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume");
1783 if (!hcd->driver->bus_resume)
1784 return -ENOENT;
1785 if (hcd->state == HC_STATE_RUNNING)
1786 return 0;
1787
1788 hcd->state = HC_STATE_RESUMING;
1789 status = hcd->driver->bus_resume(hcd);
1790 if (status == 0) {
1791
1792 msleep(10);
1793 usb_set_device_state(rhdev, rhdev->actconfig
1794 ? USB_STATE_CONFIGURED
1795 : USB_STATE_ADDRESS);
1796 hcd->state = HC_STATE_RUNNING;
1797 } else {
1798 hcd->state = old_state;
1799 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
1800 "resume", status);
1801 if (status != -ESHUTDOWN)
1802 usb_hc_died(hcd);
1803 }
1804 return status;
1805}
1806
1807
1808static void hcd_resume_work(struct work_struct *work)
1809{
1810 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
1811 struct usb_device *udev = hcd->self.root_hub;
1812
1813 usb_lock_device(udev);
1814 usb_mark_last_busy(udev);
1815 usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
1816 usb_unlock_device(udev);
1817}
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1829{
1830 unsigned long flags;
1831
1832 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1833 if (hcd->rh_registered)
1834 queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
1835 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1836}
1837EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
1838
1839#endif
1840
1841
1842
1843#ifdef CONFIG_USB_OTG
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1857{
1858 struct usb_hcd *hcd;
1859 int status = -EOPNOTSUPP;
1860
1861
1862
1863
1864
1865 hcd = container_of (bus, struct usb_hcd, self);
1866 if (port_num && hcd->driver->start_port_reset)
1867 status = hcd->driver->start_port_reset(hcd, port_num);
1868
1869
1870
1871
1872 if (status == 0)
1873 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1874 return status;
1875}
1876EXPORT_SYMBOL_GPL(usb_bus_start_enum);
1877
1878#endif
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890irqreturn_t usb_hcd_irq (int irq, void *__hcd)
1891{
1892 struct usb_hcd *hcd = __hcd;
1893 unsigned long flags;
1894 irqreturn_t rc;
1895
1896
1897
1898
1899
1900 local_irq_save(flags);
1901
1902 if (unlikely(hcd->state == HC_STATE_HALT ||
1903 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
1904 rc = IRQ_NONE;
1905 } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
1906 rc = IRQ_NONE;
1907 } else {
1908 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1909
1910 if (unlikely(hcd->state == HC_STATE_HALT))
1911 usb_hc_died(hcd);
1912 rc = IRQ_HANDLED;
1913 }
1914
1915 local_irq_restore(flags);
1916 return rc;
1917}
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929void usb_hc_died (struct usb_hcd *hcd)
1930{
1931 unsigned long flags;
1932
1933 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1934
1935 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1936 if (hcd->rh_registered) {
1937 hcd->poll_rh = 0;
1938
1939
1940 usb_set_device_state (hcd->self.root_hub,
1941 USB_STATE_NOTATTACHED);
1942 usb_kick_khubd (hcd->self.root_hub);
1943 }
1944 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1945}
1946EXPORT_SYMBOL_GPL (usb_hc_died);
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
1964 struct device *dev, const char *bus_name)
1965{
1966 struct usb_hcd *hcd;
1967
1968 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
1969 if (!hcd) {
1970 dev_dbg (dev, "hcd alloc failed\n");
1971 return NULL;
1972 }
1973 dev_set_drvdata(dev, hcd);
1974 kref_init(&hcd->kref);
1975
1976 usb_bus_init(&hcd->self);
1977 hcd->self.controller = dev;
1978 hcd->self.bus_name = bus_name;
1979 hcd->self.uses_dma = (dev->dma_mask != NULL);
1980
1981 init_timer(&hcd->rh_timer);
1982 hcd->rh_timer.function = rh_timer_func;
1983 hcd->rh_timer.data = (unsigned long) hcd;
1984#ifdef CONFIG_PM
1985 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
1986#endif
1987
1988 hcd->driver = driver;
1989 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
1990 "USB Host Controller";
1991 return hcd;
1992}
1993EXPORT_SYMBOL_GPL(usb_create_hcd);
1994
1995static void hcd_release (struct kref *kref)
1996{
1997 struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
1998
1999 kfree(hcd);
2000}
2001
2002struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
2003{
2004 if (hcd)
2005 kref_get (&hcd->kref);
2006 return hcd;
2007}
2008EXPORT_SYMBOL_GPL(usb_get_hcd);
2009
2010void usb_put_hcd (struct usb_hcd *hcd)
2011{
2012 if (hcd)
2013 kref_put (&hcd->kref, hcd_release);
2014}
2015EXPORT_SYMBOL_GPL(usb_put_hcd);
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027int usb_add_hcd(struct usb_hcd *hcd,
2028 unsigned int irqnum, unsigned long irqflags)
2029{
2030 int retval;
2031 struct usb_device *rhdev;
2032
2033 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2034
2035 hcd->authorized_default = hcd->wireless? 0 : 1;
2036 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2037
2038
2039
2040
2041
2042 if ((retval = hcd_buffer_create(hcd)) != 0) {
2043 dev_dbg(hcd->self.controller, "pool alloc failed\n");
2044 return retval;
2045 }
2046
2047 if ((retval = usb_register_bus(&hcd->self)) < 0)
2048 goto err_register_bus;
2049
2050 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
2051 dev_err(hcd->self.controller, "unable to allocate root hub\n");
2052 retval = -ENOMEM;
2053 goto err_allocate_root_hub;
2054 }
2055
2056 switch (hcd->driver->flags & HCD_MASK) {
2057 case HCD_USB11:
2058 rhdev->speed = USB_SPEED_FULL;
2059 break;
2060 case HCD_USB2:
2061 rhdev->speed = USB_SPEED_HIGH;
2062 break;
2063 case HCD_USB3:
2064 rhdev->speed = USB_SPEED_SUPER;
2065 break;
2066 default:
2067 goto err_allocate_root_hub;
2068 }
2069 hcd->self.root_hub = rhdev;
2070
2071
2072
2073
2074
2075 device_init_wakeup(&rhdev->dev, 1);
2076
2077
2078
2079
2080 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
2081 dev_err(hcd->self.controller, "can't setup\n");
2082 goto err_hcd_driver_setup;
2083 }
2084
2085
2086 if (device_can_wakeup(hcd->self.controller)
2087 && device_can_wakeup(&hcd->self.root_hub->dev))
2088 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
2089
2090
2091 if (hcd->driver->irq) {
2092
2093
2094
2095
2096
2097 if (irqflags & IRQF_SHARED)
2098 irqflags &= ~IRQF_DISABLED;
2099
2100 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
2101 hcd->driver->description, hcd->self.busnum);
2102 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
2103 hcd->irq_descr, hcd)) != 0) {
2104 dev_err(hcd->self.controller,
2105 "request interrupt %d failed\n", irqnum);
2106 goto err_request_irq;
2107 }
2108 hcd->irq = irqnum;
2109 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
2110 (hcd->driver->flags & HCD_MEMORY) ?
2111 "io mem" : "io base",
2112 (unsigned long long)hcd->rsrc_start);
2113 } else {
2114 hcd->irq = -1;
2115 if (hcd->rsrc_start)
2116 dev_info(hcd->self.controller, "%s 0x%08llx\n",
2117 (hcd->driver->flags & HCD_MEMORY) ?
2118 "io mem" : "io base",
2119 (unsigned long long)hcd->rsrc_start);
2120 }
2121
2122 if ((retval = hcd->driver->start(hcd)) < 0) {
2123 dev_err(hcd->self.controller, "startup error %d\n", retval);
2124 goto err_hcd_driver_start;
2125 }
2126
2127
2128 rhdev->bus_mA = min(500u, hcd->power_budget);
2129 if ((retval = register_root_hub(hcd)) != 0)
2130 goto err_register_root_hub;
2131
2132 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2133 if (retval < 0) {
2134 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2135 retval);
2136 goto error_create_attr_group;
2137 }
2138 if (hcd->uses_new_polling && hcd->poll_rh)
2139 usb_hcd_poll_rh_status(hcd);
2140 return retval;
2141
2142error_create_attr_group:
2143 mutex_lock(&usb_bus_list_lock);
2144 usb_disconnect(&hcd->self.root_hub);
2145 mutex_unlock(&usb_bus_list_lock);
2146err_register_root_hub:
2147 hcd->driver->stop(hcd);
2148err_hcd_driver_start:
2149 if (hcd->irq >= 0)
2150 free_irq(irqnum, hcd);
2151err_request_irq:
2152err_hcd_driver_setup:
2153 hcd->self.root_hub = NULL;
2154 usb_put_dev(rhdev);
2155err_allocate_root_hub:
2156 usb_deregister_bus(&hcd->self);
2157err_register_bus:
2158 hcd_buffer_destroy(hcd);
2159 return retval;
2160}
2161EXPORT_SYMBOL_GPL(usb_add_hcd);
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171void usb_remove_hcd(struct usb_hcd *hcd)
2172{
2173 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2174
2175 if (HC_IS_RUNNING (hcd->state))
2176 hcd->state = HC_STATE_QUIESCING;
2177
2178 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
2179 spin_lock_irq (&hcd_root_hub_lock);
2180 hcd->rh_registered = 0;
2181 spin_unlock_irq (&hcd_root_hub_lock);
2182
2183#ifdef CONFIG_PM
2184 cancel_work_sync(&hcd->wakeup_work);
2185#endif
2186
2187 sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
2188 mutex_lock(&usb_bus_list_lock);
2189 usb_disconnect(&hcd->self.root_hub);
2190 mutex_unlock(&usb_bus_list_lock);
2191
2192 hcd->driver->stop(hcd);
2193 hcd->state = HC_STATE_HALT;
2194
2195 hcd->poll_rh = 0;
2196 del_timer_sync(&hcd->rh_timer);
2197
2198 if (hcd->irq >= 0)
2199 free_irq(hcd->irq, hcd);
2200 usb_deregister_bus(&hcd->self);
2201 hcd_buffer_destroy(hcd);
2202}
2203EXPORT_SYMBOL_GPL(usb_remove_hcd);
2204
2205void
2206usb_hcd_platform_shutdown(struct platform_device* dev)
2207{
2208 struct usb_hcd *hcd = platform_get_drvdata(dev);
2209
2210 if (hcd->driver->shutdown)
2211 hcd->driver->shutdown(hcd);
2212}
2213EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
2214
2215
2216
2217#if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
2218
2219struct usb_mon_operations *mon_ops;
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229int usb_mon_register (struct usb_mon_operations *ops)
2230{
2231
2232 if (mon_ops)
2233 return -EBUSY;
2234
2235 mon_ops = ops;
2236 mb();
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL (usb_mon_register);
2240
2241void usb_mon_deregister (void)
2242{
2243
2244 if (mon_ops == NULL) {
2245 printk(KERN_ERR "USB: monitor was not registered\n");
2246 return;
2247 }
2248 mon_ops = NULL;
2249 mb();
2250}
2251EXPORT_SYMBOL_GPL (usb_mon_deregister);
2252
2253#endif
2254