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/bcd.h>
26#include <linux/module.h>
27#include <linux/version.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/completion.h>
31#include <linux/utsname.h>
32#include <linux/mm.h>
33#include <asm/io.h>
34#include <linux/device.h>
35#include <linux/dma-mapping.h>
36#include <linux/mutex.h>
37#include <asm/irq.h>
38#include <asm/byteorder.h>
39#include <asm/unaligned.h>
40#include <linux/platform_device.h>
41#include <linux/workqueue.h>
42#include <linux/pm_runtime.h>
43#include <linux/types.h>
44
45#include <linux/usb.h>
46#include <linux/usb/hcd.h>
47#include <linux/usb/phy.h>
48
49#include "usb.h"
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
85
86
87
88unsigned long usb_hcds_loaded;
89EXPORT_SYMBOL_GPL(usb_hcds_loaded);
90
91
92LIST_HEAD (usb_bus_list);
93EXPORT_SYMBOL_GPL (usb_bus_list);
94
95
96#define USB_MAXBUS 64
97static DECLARE_BITMAP(busmap, USB_MAXBUS);
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#define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff))
128#define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff))
129
130
131static const u8 usb3_rh_dev_descriptor[18] = {
132 0x12,
133 0x01,
134 0x00, 0x03,
135
136 0x09,
137 0x00,
138 0x03,
139 0x09,
140
141 0x6b, 0x1d,
142 0x03, 0x00,
143 KERNEL_VER, KERNEL_REL,
144
145 0x03,
146 0x02,
147 0x01,
148 0x01
149};
150
151
152static const u8 usb25_rh_dev_descriptor[18] = {
153 0x12,
154 0x01,
155 0x50, 0x02,
156
157 0x09,
158 0x00,
159 0x00,
160 0xFF,
161
162 0x6b, 0x1d,
163 0x02, 0x00,
164 KERNEL_VER, KERNEL_REL,
165
166 0x03,
167 0x02,
168 0x01,
169 0x01
170};
171
172
173static const u8 usb2_rh_dev_descriptor[18] = {
174 0x12,
175 0x01,
176 0x00, 0x02,
177
178 0x09,
179 0x00,
180 0x00,
181 0x40,
182
183 0x6b, 0x1d,
184 0x02, 0x00,
185 KERNEL_VER, KERNEL_REL,
186
187 0x03,
188 0x02,
189 0x01,
190 0x01
191};
192
193
194
195
196static const u8 usb11_rh_dev_descriptor[18] = {
197 0x12,
198 0x01,
199 0x10, 0x01,
200
201 0x09,
202 0x00,
203 0x00,
204 0x40,
205
206 0x6b, 0x1d,
207 0x01, 0x00,
208 KERNEL_VER, KERNEL_REL,
209
210 0x03,
211 0x02,
212 0x01,
213 0x01
214};
215
216
217
218
219
220
221static const u8 fs_rh_config_descriptor[] = {
222
223
224 0x09,
225 0x02,
226 0x19, 0x00,
227 0x01,
228 0x01,
229 0x00,
230 0xc0,
231
232
233
234
235 0x00,
236
237
238
239
240
241
242
243
244
245
246
247
248
249 0x09,
250 0x04,
251 0x00,
252 0x00,
253 0x01,
254 0x09,
255 0x00,
256 0x00,
257 0x00,
258
259
260 0x07,
261 0x05,
262 0x81,
263 0x03,
264 0x02, 0x00,
265 0xff
266};
267
268static const u8 hs_rh_config_descriptor[] = {
269
270
271 0x09,
272 0x02,
273 0x19, 0x00,
274 0x01,
275 0x01,
276 0x00,
277 0xc0,
278
279
280
281
282 0x00,
283
284
285
286
287
288
289
290
291
292
293
294
295
296 0x09,
297 0x04,
298 0x00,
299 0x00,
300 0x01,
301 0x09,
302 0x00,
303 0x00,
304 0x00,
305
306
307 0x07,
308 0x05,
309 0x81,
310 0x03,
311
312
313 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
314 0x0c
315};
316
317static const u8 ss_rh_config_descriptor[] = {
318
319 0x09,
320 0x02,
321 0x1f, 0x00,
322 0x01,
323 0x01,
324 0x00,
325 0xc0,
326
327
328
329
330 0x00,
331
332
333 0x09,
334 0x04,
335 0x00,
336 0x00,
337 0x01,
338 0x09,
339 0x00,
340 0x00,
341 0x00,
342
343
344 0x07,
345 0x05,
346 0x81,
347 0x03,
348
349
350 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
351 0x0c,
352
353
354 0x06,
355 0x30,
356 0x00,
357 0x00,
358 0x02, 0x00
359};
360
361
362
363
364
365
366static int authorized_default = -1;
367module_param(authorized_default, int, S_IRUGO|S_IWUSR);
368MODULE_PARM_DESC(authorized_default,
369 "Default USB device authorization: 0 is not authorized, 1 is "
370 "authorized, -1 is authorized except for wireless USB (default, "
371 "old behaviour");
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387static unsigned
388ascii2desc(char const *s, u8 *buf, unsigned len)
389{
390 unsigned n, t = 2 + 2*strlen(s);
391
392 if (t > 254)
393 t = 254;
394 if (len > t)
395 len = t;
396
397 t += USB_DT_STRING << 8;
398
399 n = len;
400 while (n--) {
401 *buf++ = t;
402 if (!n--)
403 break;
404 *buf++ = t >> 8;
405 t = (unsigned char)*s++;
406 }
407 return len;
408}
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423static unsigned
424rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
425{
426 char buf[100];
427 char const *s;
428 static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
429
430
431 switch (id) {
432 case 0:
433
434
435 if (len > 4)
436 len = 4;
437 memcpy(data, langids, len);
438 return len;
439 case 1:
440
441 s = hcd->self.bus_name;
442 break;
443 case 2:
444
445 s = hcd->product_desc;
446 break;
447 case 3:
448
449 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
450 init_utsname()->release, hcd->driver->description);
451 s = buf;
452 break;
453 default:
454
455 return 0;
456 }
457
458 return ascii2desc(s, data, len);
459}
460
461
462
463static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
464{
465 struct usb_ctrlrequest *cmd;
466 u16 typeReq, wValue, wIndex, wLength;
467 u8 *ubuf = urb->transfer_buffer;
468 unsigned len = 0;
469 int status;
470 u8 patch_wakeup = 0;
471 u8 patch_protocol = 0;
472 u16 tbuf_size;
473 u8 *tbuf = NULL;
474 const u8 *bufp;
475
476 might_sleep();
477
478 spin_lock_irq(&hcd_root_hub_lock);
479 status = usb_hcd_link_urb_to_ep(hcd, urb);
480 spin_unlock_irq(&hcd_root_hub_lock);
481 if (status)
482 return status;
483 urb->hcpriv = hcd;
484
485 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
486 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
487 wValue = le16_to_cpu (cmd->wValue);
488 wIndex = le16_to_cpu (cmd->wIndex);
489 wLength = le16_to_cpu (cmd->wLength);
490
491 if (wLength > urb->transfer_buffer_length)
492 goto error;
493
494
495
496
497
498 tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength);
499 tbuf = kzalloc(tbuf_size, GFP_KERNEL);
500 if (!tbuf)
501 return -ENOMEM;
502
503 bufp = tbuf;
504
505
506 urb->actual_length = 0;
507 switch (typeReq) {
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527 case DeviceRequest | USB_REQ_GET_STATUS:
528 tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev)
529 << USB_DEVICE_REMOTE_WAKEUP)
530 | (1 << USB_DEVICE_SELF_POWERED);
531 tbuf[1] = 0;
532 len = 2;
533 break;
534 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
535 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
536 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
537 else
538 goto error;
539 break;
540 case DeviceOutRequest | USB_REQ_SET_FEATURE:
541 if (device_can_wakeup(&hcd->self.root_hub->dev)
542 && wValue == USB_DEVICE_REMOTE_WAKEUP)
543 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
544 else
545 goto error;
546 break;
547 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
548 tbuf[0] = 1;
549 len = 1;
550
551 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
552 break;
553 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
554 switch (wValue & 0xff00) {
555 case USB_DT_DEVICE << 8:
556 switch (hcd->speed) {
557 case HCD_USB3:
558 bufp = usb3_rh_dev_descriptor;
559 break;
560 case HCD_USB25:
561 bufp = usb25_rh_dev_descriptor;
562 break;
563 case HCD_USB2:
564 bufp = usb2_rh_dev_descriptor;
565 break;
566 case HCD_USB11:
567 bufp = usb11_rh_dev_descriptor;
568 break;
569 default:
570 goto error;
571 }
572 len = 18;
573 if (hcd->has_tt)
574 patch_protocol = 1;
575 break;
576 case USB_DT_CONFIG << 8:
577 switch (hcd->speed) {
578 case HCD_USB3:
579 bufp = ss_rh_config_descriptor;
580 len = sizeof ss_rh_config_descriptor;
581 break;
582 case HCD_USB25:
583 case HCD_USB2:
584 bufp = hs_rh_config_descriptor;
585 len = sizeof hs_rh_config_descriptor;
586 break;
587 case HCD_USB11:
588 bufp = fs_rh_config_descriptor;
589 len = sizeof fs_rh_config_descriptor;
590 break;
591 default:
592 goto error;
593 }
594 if (device_can_wakeup(&hcd->self.root_hub->dev))
595 patch_wakeup = 1;
596 break;
597 case USB_DT_STRING << 8:
598 if ((wValue & 0xff) < 4)
599 urb->actual_length = rh_string(wValue & 0xff,
600 hcd, ubuf, wLength);
601 else
602 goto error;
603 break;
604 case USB_DT_BOS << 8:
605 goto nongeneric;
606 default:
607 goto error;
608 }
609 break;
610 case DeviceRequest | USB_REQ_GET_INTERFACE:
611 tbuf[0] = 0;
612 len = 1;
613
614 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
615 break;
616 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
617
618 dev_dbg (hcd->self.controller, "root hub device address %d\n",
619 wValue);
620 break;
621
622
623
624
625
626 case EndpointRequest | USB_REQ_GET_STATUS:
627
628 tbuf[0] = 0;
629 tbuf[1] = 0;
630 len = 2;
631
632 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
633 case EndpointOutRequest | USB_REQ_SET_FEATURE:
634 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
635 break;
636
637
638
639 default:
640nongeneric:
641
642 switch (typeReq) {
643 case GetHubStatus:
644 case GetPortStatus:
645 len = 4;
646 break;
647 case GetHubDescriptor:
648 len = sizeof (struct usb_hub_descriptor);
649 break;
650 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
651
652 break;
653 }
654 status = hcd->driver->hub_control (hcd,
655 typeReq, wValue, wIndex,
656 tbuf, wLength);
657
658 if (typeReq == GetHubDescriptor)
659 usb_hub_adjust_deviceremovable(hcd->self.root_hub,
660 (struct usb_hub_descriptor *)tbuf);
661 break;
662error:
663
664 status = -EPIPE;
665 }
666
667 if (status < 0) {
668 len = 0;
669 if (status != -EPIPE) {
670 dev_dbg (hcd->self.controller,
671 "CTRL: TypeReq=0x%x val=0x%x "
672 "idx=0x%x len=%d ==> %d\n",
673 typeReq, wValue, wIndex,
674 wLength, status);
675 }
676 } else if (status > 0) {
677
678 len = status;
679 status = 0;
680 }
681 if (len) {
682 if (urb->transfer_buffer_length < len)
683 len = urb->transfer_buffer_length;
684 urb->actual_length = len;
685
686 memcpy (ubuf, bufp, len);
687
688
689 if (patch_wakeup &&
690 len > offsetof (struct usb_config_descriptor,
691 bmAttributes))
692 ((struct usb_config_descriptor *)ubuf)->bmAttributes
693 |= USB_CONFIG_ATT_WAKEUP;
694
695
696 if (patch_protocol &&
697 len > offsetof(struct usb_device_descriptor,
698 bDeviceProtocol))
699 ((struct usb_device_descriptor *) ubuf)->
700 bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
701 }
702
703 kfree(tbuf);
704
705
706 spin_lock_irq(&hcd_root_hub_lock);
707 usb_hcd_unlink_urb_from_ep(hcd, urb);
708 usb_hcd_giveback_urb(hcd, urb, status);
709 spin_unlock_irq(&hcd_root_hub_lock);
710 return 0;
711}
712
713
714
715
716
717
718
719
720
721
722
723void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
724{
725 struct urb *urb;
726 int length;
727 unsigned long flags;
728 char buffer[6];
729
730 if (unlikely(!hcd->rh_pollable))
731 return;
732 if (!hcd->uses_new_polling && !hcd->status_urb)
733 return;
734
735 length = hcd->driver->hub_status_data(hcd, buffer);
736 if (length > 0) {
737
738
739 spin_lock_irqsave(&hcd_root_hub_lock, flags);
740 urb = hcd->status_urb;
741 if (urb) {
742 clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
743 hcd->status_urb = NULL;
744 urb->actual_length = length;
745 memcpy(urb->transfer_buffer, buffer, length);
746
747 usb_hcd_unlink_urb_from_ep(hcd, urb);
748 usb_hcd_giveback_urb(hcd, urb, 0);
749 } else {
750 length = 0;
751 set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
752 }
753 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
754 }
755
756
757
758
759
760 if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
761 (length == 0 && hcd->status_urb != NULL))
762 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
763}
764EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
765
766
767static void rh_timer_func (unsigned long _hcd)
768{
769 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
770}
771
772
773
774static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
775{
776 int retval;
777 unsigned long flags;
778 unsigned len = 1 + (urb->dev->maxchild / 8);
779
780 spin_lock_irqsave (&hcd_root_hub_lock, flags);
781 if (hcd->status_urb || urb->transfer_buffer_length < len) {
782 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
783 retval = -EINVAL;
784 goto done;
785 }
786
787 retval = usb_hcd_link_urb_to_ep(hcd, urb);
788 if (retval)
789 goto done;
790
791 hcd->status_urb = urb;
792 urb->hcpriv = hcd;
793 if (!hcd->uses_new_polling)
794 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
795
796
797 else if (HCD_POLL_PENDING(hcd))
798 mod_timer(&hcd->rh_timer, jiffies);
799 retval = 0;
800 done:
801 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
802 return retval;
803}
804
805static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
806{
807 if (usb_endpoint_xfer_int(&urb->ep->desc))
808 return rh_queue_status (hcd, urb);
809 if (usb_endpoint_xfer_control(&urb->ep->desc))
810 return rh_call_control (hcd, urb);
811 return -EINVAL;
812}
813
814
815
816
817
818
819static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
820{
821 unsigned long flags;
822 int rc;
823
824 spin_lock_irqsave(&hcd_root_hub_lock, flags);
825 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
826 if (rc)
827 goto done;
828
829 if (usb_endpoint_num(&urb->ep->desc) == 0) {
830 ;
831
832 } else {
833 if (!hcd->uses_new_polling)
834 del_timer (&hcd->rh_timer);
835 if (urb == hcd->status_urb) {
836 hcd->status_urb = NULL;
837 usb_hcd_unlink_urb_from_ep(hcd, urb);
838 usb_hcd_giveback_urb(hcd, urb, status);
839 }
840 }
841 done:
842 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
843 return rc;
844}
845
846
847
848
849
850
851static ssize_t authorized_default_show(struct device *dev,
852 struct device_attribute *attr, char *buf)
853{
854 struct usb_device *rh_usb_dev = to_usb_device(dev);
855 struct usb_bus *usb_bus = rh_usb_dev->bus;
856 struct usb_hcd *usb_hcd;
857
858 usb_hcd = bus_to_hcd(usb_bus);
859 return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
860}
861
862static ssize_t authorized_default_store(struct device *dev,
863 struct device_attribute *attr,
864 const char *buf, size_t size)
865{
866 ssize_t result;
867 unsigned val;
868 struct usb_device *rh_usb_dev = to_usb_device(dev);
869 struct usb_bus *usb_bus = rh_usb_dev->bus;
870 struct usb_hcd *usb_hcd;
871
872 usb_hcd = bus_to_hcd(usb_bus);
873 result = sscanf(buf, "%u\n", &val);
874 if (result == 1) {
875 usb_hcd->authorized_default = val ? 1 : 0;
876 result = size;
877 } else {
878 result = -EINVAL;
879 }
880 return result;
881}
882static DEVICE_ATTR_RW(authorized_default);
883
884
885static struct attribute *usb_bus_attrs[] = {
886 &dev_attr_authorized_default.attr,
887 NULL,
888};
889
890static struct attribute_group usb_bus_attr_group = {
891 .name = NULL,
892 .attrs = usb_bus_attrs,
893};
894
895
896
897
898
899
900
901
902
903
904
905
906static void usb_bus_init (struct usb_bus *bus)
907{
908 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
909
910 bus->devnum_next = 1;
911
912 bus->root_hub = NULL;
913 bus->busnum = -1;
914 bus->bandwidth_allocated = 0;
915 bus->bandwidth_int_reqs = 0;
916 bus->bandwidth_isoc_reqs = 0;
917 mutex_init(&bus->usb_address0_mutex);
918
919 INIT_LIST_HEAD (&bus->bus_list);
920}
921
922
923
924
925
926
927
928
929
930
931
932
933
934static int usb_register_bus(struct usb_bus *bus)
935{
936 int result = -E2BIG;
937 int busnum;
938
939 mutex_lock(&usb_bus_list_lock);
940 busnum = find_next_zero_bit(busmap, USB_MAXBUS, 1);
941 if (busnum >= USB_MAXBUS) {
942 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
943 goto error_find_busnum;
944 }
945 set_bit(busnum, busmap);
946 bus->busnum = busnum;
947
948
949 list_add (&bus->bus_list, &usb_bus_list);
950 mutex_unlock(&usb_bus_list_lock);
951
952 usb_notify_add_bus(bus);
953
954 dev_info (bus->controller, "new USB bus registered, assigned bus "
955 "number %d\n", bus->busnum);
956 return 0;
957
958error_find_busnum:
959 mutex_unlock(&usb_bus_list_lock);
960 return result;
961}
962
963
964
965
966
967
968
969
970
971static void usb_deregister_bus (struct usb_bus *bus)
972{
973 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
974
975
976
977
978
979
980 mutex_lock(&usb_bus_list_lock);
981 list_del (&bus->bus_list);
982 mutex_unlock(&usb_bus_list_lock);
983
984 usb_notify_remove_bus(bus);
985
986 clear_bit(bus->busnum, busmap);
987}
988
989
990
991
992
993
994
995
996
997
998
999
1000static int register_root_hub(struct usb_hcd *hcd)
1001{
1002 struct device *parent_dev = hcd->self.controller;
1003 struct usb_device *usb_dev = hcd->self.root_hub;
1004 const int devnum = 1;
1005 int retval;
1006
1007 usb_dev->devnum = devnum;
1008 usb_dev->bus->devnum_next = devnum + 1;
1009 memset (&usb_dev->bus->devmap.devicemap, 0,
1010 sizeof usb_dev->bus->devmap.devicemap);
1011 set_bit (devnum, usb_dev->bus->devmap.devicemap);
1012 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
1013
1014 mutex_lock(&usb_bus_list_lock);
1015
1016 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1017 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
1018 if (retval != sizeof usb_dev->descriptor) {
1019 mutex_unlock(&usb_bus_list_lock);
1020 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
1021 dev_name(&usb_dev->dev), retval);
1022 return (retval < 0) ? retval : -EMSGSIZE;
1023 }
1024 if (usb_dev->speed == USB_SPEED_SUPER) {
1025 retval = usb_get_bos_descriptor(usb_dev);
1026 if (retval < 0) {
1027 mutex_unlock(&usb_bus_list_lock);
1028 dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
1029 dev_name(&usb_dev->dev), retval);
1030 return retval;
1031 }
1032 }
1033
1034 retval = usb_new_device (usb_dev);
1035 if (retval) {
1036 dev_err (parent_dev, "can't register root hub for %s, %d\n",
1037 dev_name(&usb_dev->dev), retval);
1038 } else {
1039 spin_lock_irq (&hcd_root_hub_lock);
1040 hcd->rh_registered = 1;
1041 spin_unlock_irq (&hcd_root_hub_lock);
1042
1043
1044 if (HCD_DEAD(hcd))
1045 usb_hc_died (hcd);
1046 }
1047 mutex_unlock(&usb_bus_list_lock);
1048
1049 return retval;
1050}
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
1064{
1065 unsigned bit = 1 << portnum;
1066
1067 if (!(bus->resuming_ports & bit)) {
1068 bus->resuming_ports |= bit;
1069 pm_runtime_get_noresume(&bus->root_hub->dev);
1070 }
1071}
1072EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
1086{
1087 unsigned bit = 1 << portnum;
1088
1089 if (bus->resuming_ports & bit) {
1090 bus->resuming_ports &= ~bit;
1091 pm_runtime_put_noidle(&bus->root_hub->dev);
1092 }
1093}
1094EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
1112{
1113 unsigned long tmp;
1114
1115 switch (speed) {
1116 case USB_SPEED_LOW:
1117 if (is_input) {
1118 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
1119 return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
1120 } else {
1121 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
1122 return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
1123 }
1124 case USB_SPEED_FULL:
1125 if (isoc) {
1126 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1127 return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
1128 } else {
1129 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1130 return 9107L + BW_HOST_DELAY + tmp;
1131 }
1132 case USB_SPEED_HIGH:
1133
1134 if (isoc)
1135 tmp = HS_NSECS_ISO (bytecount);
1136 else
1137 tmp = HS_NSECS (bytecount);
1138 return tmp;
1139 default:
1140 pr_debug ("%s: bogus device speed!\n", usbcore_name);
1141 return -1;
1142 }
1143}
1144EXPORT_SYMBOL_GPL(usb_calc_bus_time);
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
1171{
1172 int rc = 0;
1173
1174 spin_lock(&hcd_urb_list_lock);
1175
1176
1177 if (unlikely(atomic_read(&urb->reject))) {
1178 rc = -EPERM;
1179 goto done;
1180 }
1181
1182 if (unlikely(!urb->ep->enabled)) {
1183 rc = -ENOENT;
1184 goto done;
1185 }
1186
1187 if (unlikely(!urb->dev->can_submit)) {
1188 rc = -EHOSTUNREACH;
1189 goto done;
1190 }
1191
1192
1193
1194
1195
1196 if (HCD_RH_RUNNING(hcd)) {
1197 urb->unlinked = 0;
1198 list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1199 } else {
1200 rc = -ESHUTDOWN;
1201 goto done;
1202 }
1203 done:
1204 spin_unlock(&hcd_urb_list_lock);
1205 return rc;
1206}
1207EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
1229 int status)
1230{
1231 struct list_head *tmp;
1232
1233
1234 list_for_each(tmp, &urb->ep->urb_list) {
1235 if (tmp == &urb->urb_list)
1236 break;
1237 }
1238 if (tmp != &urb->urb_list)
1239 return -EIDRM;
1240
1241
1242
1243
1244 if (urb->unlinked)
1245 return -EBUSY;
1246 urb->unlinked = status;
1247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
1262{
1263
1264 spin_lock(&hcd_urb_list_lock);
1265 list_del_init(&urb->urb_list);
1266 spin_unlock(&hcd_urb_list_lock);
1267}
1268EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302static int hcd_alloc_coherent(struct usb_bus *bus,
1303 gfp_t mem_flags, dma_addr_t *dma_handle,
1304 void **vaddr_handle, size_t size,
1305 enum dma_data_direction dir)
1306{
1307 unsigned char *vaddr;
1308
1309 if (*vaddr_handle == NULL) {
1310 WARN_ON_ONCE(1);
1311 return -EFAULT;
1312 }
1313
1314 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
1315 mem_flags, dma_handle);
1316 if (!vaddr)
1317 return -ENOMEM;
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327 put_unaligned((unsigned long)*vaddr_handle,
1328 (unsigned long *)(vaddr + size));
1329
1330 if (dir == DMA_TO_DEVICE)
1331 memcpy(vaddr, *vaddr_handle, size);
1332
1333 *vaddr_handle = vaddr;
1334 return 0;
1335}
1336
1337static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
1338 void **vaddr_handle, size_t size,
1339 enum dma_data_direction dir)
1340{
1341 unsigned char *vaddr = *vaddr_handle;
1342
1343 vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
1344
1345 if (dir == DMA_FROM_DEVICE)
1346 memcpy(vaddr, *vaddr_handle, size);
1347
1348 hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
1349
1350 *vaddr_handle = vaddr;
1351 *dma_handle = 0;
1352}
1353
1354void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
1355{
1356 if (urb->transfer_flags & URB_SETUP_MAP_SINGLE)
1357 dma_unmap_single(hcd->self.controller,
1358 urb->setup_dma,
1359 sizeof(struct usb_ctrlrequest),
1360 DMA_TO_DEVICE);
1361 else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
1362 hcd_free_coherent(urb->dev->bus,
1363 &urb->setup_dma,
1364 (void **) &urb->setup_packet,
1365 sizeof(struct usb_ctrlrequest),
1366 DMA_TO_DEVICE);
1367
1368
1369 urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
1370}
1371EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
1372
1373static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1374{
1375 if (hcd->driver->unmap_urb_for_dma)
1376 hcd->driver->unmap_urb_for_dma(hcd, urb);
1377 else
1378 usb_hcd_unmap_urb_for_dma(hcd, urb);
1379}
1380
1381void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1382{
1383 enum dma_data_direction dir;
1384
1385 usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
1386
1387 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1388 if (urb->transfer_flags & URB_DMA_MAP_SG)
1389 dma_unmap_sg(hcd->self.controller,
1390 urb->sg,
1391 urb->num_sgs,
1392 dir);
1393 else if (urb->transfer_flags & URB_DMA_MAP_PAGE)
1394 dma_unmap_page(hcd->self.controller,
1395 urb->transfer_dma,
1396 urb->transfer_buffer_length,
1397 dir);
1398 else if (urb->transfer_flags & URB_DMA_MAP_SINGLE)
1399 dma_unmap_single(hcd->self.controller,
1400 urb->transfer_dma,
1401 urb->transfer_buffer_length,
1402 dir);
1403 else if (urb->transfer_flags & URB_MAP_LOCAL)
1404 hcd_free_coherent(urb->dev->bus,
1405 &urb->transfer_dma,
1406 &urb->transfer_buffer,
1407 urb->transfer_buffer_length,
1408 dir);
1409
1410
1411 urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
1412 URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
1413}
1414EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
1415
1416static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1417 gfp_t mem_flags)
1418{
1419 if (hcd->driver->map_urb_for_dma)
1420 return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
1421 else
1422 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
1423}
1424
1425int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1426 gfp_t mem_flags)
1427{
1428 enum dma_data_direction dir;
1429 int ret = 0;
1430
1431
1432
1433
1434
1435
1436
1437 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1438 if (hcd->self.uses_pio_for_control)
1439 return ret;
1440 if (hcd->self.uses_dma) {
1441 urb->setup_dma = dma_map_single(
1442 hcd->self.controller,
1443 urb->setup_packet,
1444 sizeof(struct usb_ctrlrequest),
1445 DMA_TO_DEVICE);
1446 if (dma_mapping_error(hcd->self.controller,
1447 urb->setup_dma))
1448 return -EAGAIN;
1449 urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
1450 } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1451 ret = hcd_alloc_coherent(
1452 urb->dev->bus, mem_flags,
1453 &urb->setup_dma,
1454 (void **)&urb->setup_packet,
1455 sizeof(struct usb_ctrlrequest),
1456 DMA_TO_DEVICE);
1457 if (ret)
1458 return ret;
1459 urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
1460 }
1461 }
1462
1463 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1464 if (urb->transfer_buffer_length != 0
1465 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1466 if (hcd->self.uses_dma) {
1467 if (urb->num_sgs) {
1468 int n;
1469
1470
1471 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1472 WARN_ON(1);
1473 return -EINVAL;
1474 }
1475
1476 n = dma_map_sg(
1477 hcd->self.controller,
1478 urb->sg,
1479 urb->num_sgs,
1480 dir);
1481 if (n <= 0)
1482 ret = -EAGAIN;
1483 else
1484 urb->transfer_flags |= URB_DMA_MAP_SG;
1485 urb->num_mapped_sgs = n;
1486 if (n != urb->num_sgs)
1487 urb->transfer_flags |=
1488 URB_DMA_SG_COMBINED;
1489 } else if (urb->sg) {
1490 struct scatterlist *sg = urb->sg;
1491 urb->transfer_dma = dma_map_page(
1492 hcd->self.controller,
1493 sg_page(sg),
1494 sg->offset,
1495 urb->transfer_buffer_length,
1496 dir);
1497 if (dma_mapping_error(hcd->self.controller,
1498 urb->transfer_dma))
1499 ret = -EAGAIN;
1500 else
1501 urb->transfer_flags |= URB_DMA_MAP_PAGE;
1502 } else if (is_vmalloc_addr(urb->transfer_buffer)) {
1503 WARN_ONCE(1, "transfer buffer not dma capable\n");
1504 ret = -EAGAIN;
1505 } else {
1506 urb->transfer_dma = dma_map_single(
1507 hcd->self.controller,
1508 urb->transfer_buffer,
1509 urb->transfer_buffer_length,
1510 dir);
1511 if (dma_mapping_error(hcd->self.controller,
1512 urb->transfer_dma))
1513 ret = -EAGAIN;
1514 else
1515 urb->transfer_flags |= URB_DMA_MAP_SINGLE;
1516 }
1517 } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1518 ret = hcd_alloc_coherent(
1519 urb->dev->bus, mem_flags,
1520 &urb->transfer_dma,
1521 &urb->transfer_buffer,
1522 urb->transfer_buffer_length,
1523 dir);
1524 if (ret == 0)
1525 urb->transfer_flags |= URB_MAP_LOCAL;
1526 }
1527 if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
1528 URB_SETUP_MAP_LOCAL)))
1529 usb_hcd_unmap_urb_for_dma(hcd, urb);
1530 }
1531 return ret;
1532}
1533EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
1534
1535
1536
1537
1538
1539
1540
1541
1542int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1543{
1544 int status;
1545 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1546
1547
1548
1549
1550
1551 usb_get_urb(urb);
1552 atomic_inc(&urb->use_count);
1553 atomic_inc(&urb->dev->urbnum);
1554 usbmon_urb_submit(&hcd->self, urb);
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564 if (is_root_hub(urb->dev)) {
1565 status = rh_urb_enqueue(hcd, urb);
1566 } else {
1567 status = map_urb_for_dma(hcd, urb, mem_flags);
1568 if (likely(status == 0)) {
1569 status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
1570 if (unlikely(status))
1571 unmap_urb_for_dma(hcd, urb);
1572 }
1573 }
1574
1575 if (unlikely(status)) {
1576 usbmon_urb_submit_error(&hcd->self, urb, status);
1577 urb->hcpriv = NULL;
1578 INIT_LIST_HEAD(&urb->urb_list);
1579 atomic_dec(&urb->use_count);
1580 atomic_dec(&urb->dev->urbnum);
1581 if (atomic_read(&urb->reject))
1582 wake_up(&usb_kill_urb_queue);
1583 usb_put_urb(urb);
1584 }
1585 return status;
1586}
1587
1588
1589
1590
1591
1592
1593
1594
1595static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
1596{
1597 int value;
1598
1599 if (is_root_hub(urb->dev))
1600 value = usb_rh_urb_dequeue(hcd, urb, status);
1601 else {
1602
1603
1604
1605
1606 value = hcd->driver->urb_dequeue(hcd, urb, status);
1607 }
1608 return value;
1609}
1610
1611
1612
1613
1614
1615
1616
1617int usb_hcd_unlink_urb (struct urb *urb, int status)
1618{
1619 struct usb_hcd *hcd;
1620 int retval = -EIDRM;
1621 unsigned long flags;
1622
1623
1624
1625
1626
1627
1628 spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1629 if (atomic_read(&urb->use_count) > 0) {
1630 retval = 0;
1631 usb_get_dev(urb->dev);
1632 }
1633 spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1634 if (retval == 0) {
1635 hcd = bus_to_hcd(urb->dev->bus);
1636 retval = unlink1(hcd, urb, status);
1637 usb_put_dev(urb->dev);
1638 }
1639
1640 if (retval == 0)
1641 retval = -EINPROGRESS;
1642 else if (retval != -EIDRM && retval != -EBUSY)
1643 dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
1644 urb, retval);
1645 return retval;
1646}
1647
1648
1649
1650static void __usb_hcd_giveback_urb(struct urb *urb)
1651{
1652 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1653 struct usb_anchor *anchor = urb->anchor;
1654 int status = urb->unlinked;
1655 unsigned long flags;
1656
1657 urb->hcpriv = NULL;
1658 if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
1659 urb->actual_length < urb->transfer_buffer_length &&
1660 !status))
1661 status = -EREMOTEIO;
1662
1663 unmap_urb_for_dma(hcd, urb);
1664 usbmon_urb_complete(&hcd->self, urb, status);
1665 usb_anchor_suspend_wakeups(anchor);
1666 usb_unanchor_urb(urb);
1667
1668
1669 urb->status = status;
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681 local_irq_save(flags);
1682 urb->complete(urb);
1683 local_irq_restore(flags);
1684
1685 usb_anchor_resume_wakeups(anchor);
1686 atomic_dec(&urb->use_count);
1687 if (unlikely(atomic_read(&urb->reject)))
1688 wake_up(&usb_kill_urb_queue);
1689 usb_put_urb(urb);
1690}
1691
1692static void usb_giveback_urb_bh(unsigned long param)
1693{
1694 struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param;
1695 struct list_head local_list;
1696
1697 spin_lock_irq(&bh->lock);
1698 bh->running = true;
1699 restart:
1700 list_replace_init(&bh->head, &local_list);
1701 spin_unlock_irq(&bh->lock);
1702
1703 while (!list_empty(&local_list)) {
1704 struct urb *urb;
1705
1706 urb = list_entry(local_list.next, struct urb, urb_list);
1707 list_del_init(&urb->urb_list);
1708 bh->completing_ep = urb->ep;
1709 __usb_hcd_giveback_urb(urb);
1710 bh->completing_ep = NULL;
1711 }
1712
1713
1714 spin_lock_irq(&bh->lock);
1715 if (!list_empty(&bh->head))
1716 goto restart;
1717 bh->running = false;
1718 spin_unlock_irq(&bh->lock);
1719}
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
1739{
1740 struct giveback_urb_bh *bh;
1741 bool running, high_prio_bh;
1742
1743
1744 if (likely(!urb->unlinked))
1745 urb->unlinked = status;
1746
1747 if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
1748 __usb_hcd_giveback_urb(urb);
1749 return;
1750 }
1751
1752 if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) {
1753 bh = &hcd->high_prio_bh;
1754 high_prio_bh = true;
1755 } else {
1756 bh = &hcd->low_prio_bh;
1757 high_prio_bh = false;
1758 }
1759
1760 spin_lock(&bh->lock);
1761 list_add_tail(&urb->urb_list, &bh->head);
1762 running = bh->running;
1763 spin_unlock(&bh->lock);
1764
1765 if (running)
1766 ;
1767 else if (high_prio_bh)
1768 tasklet_hi_schedule(&bh->bh);
1769 else
1770 tasklet_schedule(&bh->bh);
1771}
1772EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
1773
1774
1775
1776
1777
1778
1779
1780void usb_hcd_flush_endpoint(struct usb_device *udev,
1781 struct usb_host_endpoint *ep)
1782{
1783 struct usb_hcd *hcd;
1784 struct urb *urb;
1785
1786 if (!ep)
1787 return;
1788 might_sleep();
1789 hcd = bus_to_hcd(udev->bus);
1790
1791
1792 spin_lock_irq(&hcd_urb_list_lock);
1793rescan:
1794 list_for_each_entry (urb, &ep->urb_list, urb_list) {
1795 int is_in;
1796
1797 if (urb->unlinked)
1798 continue;
1799 usb_get_urb (urb);
1800 is_in = usb_urb_dir_in(urb);
1801 spin_unlock(&hcd_urb_list_lock);
1802
1803
1804 unlink1(hcd, urb, -ESHUTDOWN);
1805 dev_dbg (hcd->self.controller,
1806 "shutdown urb %p ep%d%s%s\n",
1807 urb, usb_endpoint_num(&ep->desc),
1808 is_in ? "in" : "out",
1809 ({ char *s;
1810
1811 switch (usb_endpoint_type(&ep->desc)) {
1812 case USB_ENDPOINT_XFER_CONTROL:
1813 s = ""; break;
1814 case USB_ENDPOINT_XFER_BULK:
1815 s = "-bulk"; break;
1816 case USB_ENDPOINT_XFER_INT:
1817 s = "-intr"; break;
1818 default:
1819 s = "-iso"; break;
1820 };
1821 s;
1822 }));
1823 usb_put_urb (urb);
1824
1825
1826 spin_lock(&hcd_urb_list_lock);
1827 goto rescan;
1828 }
1829 spin_unlock_irq(&hcd_urb_list_lock);
1830
1831
1832 while (!list_empty (&ep->urb_list)) {
1833 spin_lock_irq(&hcd_urb_list_lock);
1834
1835
1836 urb = NULL;
1837 if (!list_empty (&ep->urb_list)) {
1838 urb = list_entry (ep->urb_list.prev, struct urb,
1839 urb_list);
1840 usb_get_urb (urb);
1841 }
1842 spin_unlock_irq(&hcd_urb_list_lock);
1843
1844 if (urb) {
1845 usb_kill_urb (urb);
1846 usb_put_urb (urb);
1847 }
1848 }
1849}
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872int usb_hcd_alloc_bandwidth(struct usb_device *udev,
1873 struct usb_host_config *new_config,
1874 struct usb_host_interface *cur_alt,
1875 struct usb_host_interface *new_alt)
1876{
1877 int num_intfs, i, j;
1878 struct usb_host_interface *alt = NULL;
1879 int ret = 0;
1880 struct usb_hcd *hcd;
1881 struct usb_host_endpoint *ep;
1882
1883 hcd = bus_to_hcd(udev->bus);
1884 if (!hcd->driver->check_bandwidth)
1885 return 0;
1886
1887
1888 if (!new_config && !cur_alt) {
1889 for (i = 1; i < 16; ++i) {
1890 ep = udev->ep_out[i];
1891 if (ep)
1892 hcd->driver->drop_endpoint(hcd, udev, ep);
1893 ep = udev->ep_in[i];
1894 if (ep)
1895 hcd->driver->drop_endpoint(hcd, udev, ep);
1896 }
1897 hcd->driver->check_bandwidth(hcd, udev);
1898 return 0;
1899 }
1900
1901
1902
1903
1904
1905 if (new_config) {
1906 num_intfs = new_config->desc.bNumInterfaces;
1907
1908
1909
1910 for (i = 1; i < 16; ++i) {
1911 ep = udev->ep_out[i];
1912 if (ep) {
1913 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1914 if (ret < 0)
1915 goto reset;
1916 }
1917 ep = udev->ep_in[i];
1918 if (ep) {
1919 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1920 if (ret < 0)
1921 goto reset;
1922 }
1923 }
1924 for (i = 0; i < num_intfs; ++i) {
1925 struct usb_host_interface *first_alt;
1926 int iface_num;
1927
1928 first_alt = &new_config->intf_cache[i]->altsetting[0];
1929 iface_num = first_alt->desc.bInterfaceNumber;
1930
1931 alt = usb_find_alt_setting(new_config, iface_num, 0);
1932 if (!alt)
1933
1934 alt = first_alt;
1935
1936 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1937 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1938 if (ret < 0)
1939 goto reset;
1940 }
1941 }
1942 }
1943 if (cur_alt && new_alt) {
1944 struct usb_interface *iface = usb_ifnum_to_if(udev,
1945 cur_alt->desc.bInterfaceNumber);
1946
1947 if (!iface)
1948 return -EINVAL;
1949 if (iface->resetting_device) {
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959 cur_alt = usb_altnum_to_altsetting(iface, 0);
1960 if (!cur_alt)
1961 cur_alt = &iface->altsetting[0];
1962 }
1963
1964
1965 for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
1966 ret = hcd->driver->drop_endpoint(hcd, udev,
1967 &cur_alt->endpoint[i]);
1968 if (ret < 0)
1969 goto reset;
1970 }
1971
1972 for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
1973 ret = hcd->driver->add_endpoint(hcd, udev,
1974 &new_alt->endpoint[i]);
1975 if (ret < 0)
1976 goto reset;
1977 }
1978 }
1979 ret = hcd->driver->check_bandwidth(hcd, udev);
1980reset:
1981 if (ret < 0)
1982 hcd->driver->reset_bandwidth(hcd, udev);
1983 return ret;
1984}
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994void usb_hcd_disable_endpoint(struct usb_device *udev,
1995 struct usb_host_endpoint *ep)
1996{
1997 struct usb_hcd *hcd;
1998
1999 might_sleep();
2000 hcd = bus_to_hcd(udev->bus);
2001 if (hcd->driver->endpoint_disable)
2002 hcd->driver->endpoint_disable(hcd, ep);
2003}
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013void usb_hcd_reset_endpoint(struct usb_device *udev,
2014 struct usb_host_endpoint *ep)
2015{
2016 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2017
2018 if (hcd->driver->endpoint_reset)
2019 hcd->driver->endpoint_reset(hcd, ep);
2020 else {
2021 int epnum = usb_endpoint_num(&ep->desc);
2022 int is_out = usb_endpoint_dir_out(&ep->desc);
2023 int is_control = usb_endpoint_xfer_control(&ep->desc);
2024
2025 usb_settoggle(udev, epnum, is_out, 0);
2026 if (is_control)
2027 usb_settoggle(udev, epnum, !is_out, 0);
2028 }
2029}
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046int usb_alloc_streams(struct usb_interface *interface,
2047 struct usb_host_endpoint **eps, unsigned int num_eps,
2048 unsigned int num_streams, gfp_t mem_flags)
2049{
2050 struct usb_hcd *hcd;
2051 struct usb_device *dev;
2052 int i, ret;
2053
2054 dev = interface_to_usbdev(interface);
2055 hcd = bus_to_hcd(dev->bus);
2056 if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
2057 return -EINVAL;
2058 if (dev->speed != USB_SPEED_SUPER)
2059 return -EINVAL;
2060
2061 for (i = 0; i < num_eps; i++) {
2062
2063 if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
2064 return -EINVAL;
2065
2066 if (eps[i]->streams)
2067 return -EINVAL;
2068 }
2069
2070 ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
2071 num_streams, mem_flags);
2072 if (ret < 0)
2073 return ret;
2074
2075 for (i = 0; i < num_eps; i++)
2076 eps[i]->streams = ret;
2077
2078 return ret;
2079}
2080EXPORT_SYMBOL_GPL(usb_alloc_streams);
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094int usb_free_streams(struct usb_interface *interface,
2095 struct usb_host_endpoint **eps, unsigned int num_eps,
2096 gfp_t mem_flags)
2097{
2098 struct usb_hcd *hcd;
2099 struct usb_device *dev;
2100 int i, ret;
2101
2102 dev = interface_to_usbdev(interface);
2103 hcd = bus_to_hcd(dev->bus);
2104 if (dev->speed != USB_SPEED_SUPER)
2105 return -EINVAL;
2106
2107
2108 for (i = 0; i < num_eps; i++)
2109 if (!eps[i] || !eps[i]->streams)
2110 return -EINVAL;
2111
2112 ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
2113 if (ret < 0)
2114 return ret;
2115
2116 for (i = 0; i < num_eps; i++)
2117 eps[i]->streams = 0;
2118
2119 return ret;
2120}
2121EXPORT_SYMBOL_GPL(usb_free_streams);
2122
2123
2124
2125
2126
2127
2128void usb_hcd_synchronize_unlinks(struct usb_device *udev)
2129{
2130 spin_lock_irq(&hcd_urb_unlink_lock);
2131 spin_unlock_irq(&hcd_urb_unlink_lock);
2132}
2133
2134
2135
2136
2137int usb_hcd_get_frame_number (struct usb_device *udev)
2138{
2139 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2140
2141 if (!HCD_RH_RUNNING(hcd))
2142 return -ESHUTDOWN;
2143 return hcd->driver->get_frame_number (hcd);
2144}
2145
2146
2147
2148#ifdef CONFIG_PM
2149
2150int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
2151{
2152 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
2153 int status;
2154 int old_state = hcd->state;
2155
2156 dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
2157 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2158 rhdev->do_remote_wakeup);
2159 if (HCD_DEAD(hcd)) {
2160 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
2161 return 0;
2162 }
2163
2164 if (!hcd->driver->bus_suspend) {
2165 status = -ENOENT;
2166 } else {
2167 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2168 hcd->state = HC_STATE_QUIESCING;
2169 status = hcd->driver->bus_suspend(hcd);
2170 }
2171 if (status == 0) {
2172 usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
2173 hcd->state = HC_STATE_SUSPENDED;
2174
2175
2176 if (rhdev->do_remote_wakeup) {
2177 char buffer[6];
2178
2179 status = hcd->driver->hub_status_data(hcd, buffer);
2180 if (status != 0) {
2181 dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
2182 hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
2183 status = -EBUSY;
2184 }
2185 }
2186 } else {
2187 spin_lock_irq(&hcd_root_hub_lock);
2188 if (!HCD_DEAD(hcd)) {
2189 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2190 hcd->state = old_state;
2191 }
2192 spin_unlock_irq(&hcd_root_hub_lock);
2193 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
2194 "suspend", status);
2195 }
2196 return status;
2197}
2198
2199int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
2200{
2201 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
2202 int status;
2203 int old_state = hcd->state;
2204
2205 dev_dbg(&rhdev->dev, "usb %sresume\n",
2206 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
2207 if (HCD_DEAD(hcd)) {
2208 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
2209 return 0;
2210 }
2211 if (!hcd->driver->bus_resume)
2212 return -ENOENT;
2213 if (HCD_RH_RUNNING(hcd))
2214 return 0;
2215
2216 hcd->state = HC_STATE_RESUMING;
2217 status = hcd->driver->bus_resume(hcd);
2218 clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
2219 if (status == 0) {
2220 struct usb_device *udev;
2221 int port1;
2222
2223 spin_lock_irq(&hcd_root_hub_lock);
2224 if (!HCD_DEAD(hcd)) {
2225 usb_set_device_state(rhdev, rhdev->actconfig
2226 ? USB_STATE_CONFIGURED
2227 : USB_STATE_ADDRESS);
2228 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2229 hcd->state = HC_STATE_RUNNING;
2230 }
2231 spin_unlock_irq(&hcd_root_hub_lock);
2232
2233
2234
2235
2236
2237
2238
2239 usb_hub_for_each_child(rhdev, port1, udev) {
2240 if (udev->state != USB_STATE_NOTATTACHED &&
2241 !udev->port_is_suspended) {
2242 usleep_range(10000, 11000);
2243 break;
2244 }
2245 }
2246 } else {
2247 hcd->state = old_state;
2248 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
2249 "resume", status);
2250 if (status != -ESHUTDOWN)
2251 usb_hc_died(hcd);
2252 }
2253 return status;
2254}
2255
2256#endif
2257
2258#ifdef CONFIG_PM_RUNTIME
2259
2260
2261static void hcd_resume_work(struct work_struct *work)
2262{
2263 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
2264 struct usb_device *udev = hcd->self.root_hub;
2265
2266 usb_remote_wakeup(udev);
2267}
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
2279{
2280 unsigned long flags;
2281
2282 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2283 if (hcd->rh_registered) {
2284 set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
2285 queue_work(pm_wq, &hcd->wakeup_work);
2286 }
2287 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2288}
2289EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
2290
2291#endif
2292
2293
2294
2295#ifdef CONFIG_USB_OTG
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
2311{
2312 struct usb_hcd *hcd;
2313 int status = -EOPNOTSUPP;
2314
2315
2316
2317
2318
2319 hcd = container_of (bus, struct usb_hcd, self);
2320 if (port_num && hcd->driver->start_port_reset)
2321 status = hcd->driver->start_port_reset(hcd, port_num);
2322
2323
2324
2325
2326 if (status == 0)
2327 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
2328 return status;
2329}
2330EXPORT_SYMBOL_GPL(usb_bus_start_enum);
2331
2332#endif
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346irqreturn_t usb_hcd_irq (int irq, void *__hcd)
2347{
2348 struct usb_hcd *hcd = __hcd;
2349 irqreturn_t rc;
2350
2351 if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
2352 rc = IRQ_NONE;
2353 else if (hcd->driver->irq(hcd) == IRQ_NONE)
2354 rc = IRQ_NONE;
2355 else
2356 rc = IRQ_HANDLED;
2357
2358 return rc;
2359}
2360EXPORT_SYMBOL_GPL(usb_hcd_irq);
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374void usb_hc_died (struct usb_hcd *hcd)
2375{
2376 unsigned long flags;
2377
2378 dev_err (hcd->self.controller, "HC died; cleaning up\n");
2379
2380 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2381 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2382 set_bit(HCD_FLAG_DEAD, &hcd->flags);
2383 if (hcd->rh_registered) {
2384 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2385
2386
2387 usb_set_device_state (hcd->self.root_hub,
2388 USB_STATE_NOTATTACHED);
2389 usb_kick_khubd (hcd->self.root_hub);
2390 }
2391 if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
2392 hcd = hcd->shared_hcd;
2393 if (hcd->rh_registered) {
2394 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2395
2396
2397 usb_set_device_state(hcd->self.root_hub,
2398 USB_STATE_NOTATTACHED);
2399 usb_kick_khubd(hcd->self.root_hub);
2400 }
2401 }
2402 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2403
2404}
2405EXPORT_SYMBOL_GPL (usb_hc_died);
2406
2407
2408
2409static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
2410{
2411
2412 spin_lock_init(&bh->lock);
2413 INIT_LIST_HEAD(&bh->head);
2414 tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh);
2415}
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
2434 struct device *dev, const char *bus_name,
2435 struct usb_hcd *primary_hcd)
2436{
2437 struct usb_hcd *hcd;
2438
2439 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
2440 if (!hcd) {
2441 dev_dbg (dev, "hcd alloc failed\n");
2442 return NULL;
2443 }
2444 if (primary_hcd == NULL) {
2445 hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
2446 GFP_KERNEL);
2447 if (!hcd->bandwidth_mutex) {
2448 kfree(hcd);
2449 dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
2450 return NULL;
2451 }
2452 mutex_init(hcd->bandwidth_mutex);
2453 dev_set_drvdata(dev, hcd);
2454 } else {
2455 mutex_lock(&usb_port_peer_mutex);
2456 hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
2457 hcd->primary_hcd = primary_hcd;
2458 primary_hcd->primary_hcd = primary_hcd;
2459 hcd->shared_hcd = primary_hcd;
2460 primary_hcd->shared_hcd = hcd;
2461 mutex_unlock(&usb_port_peer_mutex);
2462 }
2463
2464 kref_init(&hcd->kref);
2465
2466 usb_bus_init(&hcd->self);
2467 hcd->self.controller = dev;
2468 hcd->self.bus_name = bus_name;
2469 hcd->self.uses_dma = (dev->dma_mask != NULL);
2470
2471 init_timer(&hcd->rh_timer);
2472 hcd->rh_timer.function = rh_timer_func;
2473 hcd->rh_timer.data = (unsigned long) hcd;
2474#ifdef CONFIG_PM_RUNTIME
2475 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2476#endif
2477
2478 hcd->driver = driver;
2479 hcd->speed = driver->flags & HCD_MASK;
2480 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
2481 "USB Host Controller";
2482 return hcd;
2483}
2484EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
2501 struct device *dev, const char *bus_name)
2502{
2503 return usb_create_shared_hcd(driver, dev, bus_name, NULL);
2504}
2505EXPORT_SYMBOL_GPL(usb_create_hcd);
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517static void hcd_release(struct kref *kref)
2518{
2519 struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
2520
2521 mutex_lock(&usb_port_peer_mutex);
2522 if (usb_hcd_is_primary_hcd(hcd))
2523 kfree(hcd->bandwidth_mutex);
2524 if (hcd->shared_hcd) {
2525 struct usb_hcd *peer = hcd->shared_hcd;
2526
2527 peer->shared_hcd = NULL;
2528 if (peer->primary_hcd == hcd)
2529 peer->primary_hcd = NULL;
2530 }
2531 mutex_unlock(&usb_port_peer_mutex);
2532 kfree(hcd);
2533}
2534
2535struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
2536{
2537 if (hcd)
2538 kref_get (&hcd->kref);
2539 return hcd;
2540}
2541EXPORT_SYMBOL_GPL(usb_get_hcd);
2542
2543void usb_put_hcd (struct usb_hcd *hcd)
2544{
2545 if (hcd)
2546 kref_put (&hcd->kref, hcd_release);
2547}
2548EXPORT_SYMBOL_GPL(usb_put_hcd);
2549
2550int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
2551{
2552 if (!hcd->primary_hcd)
2553 return 1;
2554 return hcd == hcd->primary_hcd;
2555}
2556EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
2557
2558int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
2559{
2560 if (!hcd->driver->find_raw_port_number)
2561 return port1;
2562
2563 return hcd->driver->find_raw_port_number(hcd, port1);
2564}
2565
2566static int usb_hcd_request_irqs(struct usb_hcd *hcd,
2567 unsigned int irqnum, unsigned long irqflags)
2568{
2569 int retval;
2570
2571 if (hcd->driver->irq) {
2572
2573 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
2574 hcd->driver->description, hcd->self.busnum);
2575 retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
2576 hcd->irq_descr, hcd);
2577 if (retval != 0) {
2578 dev_err(hcd->self.controller,
2579 "request interrupt %d failed\n",
2580 irqnum);
2581 return retval;
2582 }
2583 hcd->irq = irqnum;
2584 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
2585 (hcd->driver->flags & HCD_MEMORY) ?
2586 "io mem" : "io base",
2587 (unsigned long long)hcd->rsrc_start);
2588 } else {
2589 hcd->irq = 0;
2590 if (hcd->rsrc_start)
2591 dev_info(hcd->self.controller, "%s 0x%08llx\n",
2592 (hcd->driver->flags & HCD_MEMORY) ?
2593 "io mem" : "io base",
2594 (unsigned long long)hcd->rsrc_start);
2595 }
2596 return 0;
2597}
2598
2599
2600
2601
2602
2603static void usb_put_invalidate_rhdev(struct usb_hcd *hcd)
2604{
2605 struct usb_device *rhdev;
2606
2607 mutex_lock(&usb_port_peer_mutex);
2608 rhdev = hcd->self.root_hub;
2609 hcd->self.root_hub = NULL;
2610 mutex_unlock(&usb_port_peer_mutex);
2611 usb_put_dev(rhdev);
2612}
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624int usb_add_hcd(struct usb_hcd *hcd,
2625 unsigned int irqnum, unsigned long irqflags)
2626{
2627 int retval;
2628 struct usb_device *rhdev;
2629
2630 if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->phy) {
2631 struct usb_phy *phy = usb_get_phy_dev(hcd->self.controller, 0);
2632
2633 if (IS_ERR(phy)) {
2634 retval = PTR_ERR(phy);
2635 if (retval == -EPROBE_DEFER)
2636 return retval;
2637 } else {
2638 retval = usb_phy_init(phy);
2639 if (retval) {
2640 usb_put_phy(phy);
2641 return retval;
2642 }
2643 hcd->phy = phy;
2644 hcd->remove_phy = 1;
2645 }
2646 }
2647
2648 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2649
2650
2651 if (authorized_default < 0 || authorized_default > 1)
2652 hcd->authorized_default = hcd->wireless ? 0 : 1;
2653 else
2654 hcd->authorized_default = authorized_default;
2655 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2656
2657
2658
2659
2660
2661 if ((retval = hcd_buffer_create(hcd)) != 0) {
2662 dev_dbg(hcd->self.controller, "pool alloc failed\n");
2663 goto err_remove_phy;
2664 }
2665
2666 if ((retval = usb_register_bus(&hcd->self)) < 0)
2667 goto err_register_bus;
2668
2669 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
2670 dev_err(hcd->self.controller, "unable to allocate root hub\n");
2671 retval = -ENOMEM;
2672 goto err_allocate_root_hub;
2673 }
2674 mutex_lock(&usb_port_peer_mutex);
2675 hcd->self.root_hub = rhdev;
2676 mutex_unlock(&usb_port_peer_mutex);
2677
2678 switch (hcd->speed) {
2679 case HCD_USB11:
2680 rhdev->speed = USB_SPEED_FULL;
2681 break;
2682 case HCD_USB2:
2683 rhdev->speed = USB_SPEED_HIGH;
2684 break;
2685 case HCD_USB25:
2686 rhdev->speed = USB_SPEED_WIRELESS;
2687 break;
2688 case HCD_USB3:
2689 rhdev->speed = USB_SPEED_SUPER;
2690 break;
2691 default:
2692 retval = -EINVAL;
2693 goto err_set_rh_speed;
2694 }
2695
2696
2697
2698
2699
2700 device_set_wakeup_capable(&rhdev->dev, 1);
2701
2702
2703
2704
2705
2706 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2707
2708
2709
2710
2711 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
2712 dev_err(hcd->self.controller, "can't setup: %d\n", retval);
2713 goto err_hcd_driver_setup;
2714 }
2715 hcd->rh_pollable = 1;
2716
2717
2718 if (device_can_wakeup(hcd->self.controller)
2719 && device_can_wakeup(&hcd->self.root_hub->dev))
2720 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
2721
2722
2723 init_giveback_urb_bh(&hcd->high_prio_bh);
2724 init_giveback_urb_bh(&hcd->low_prio_bh);
2725
2726
2727
2728
2729 if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
2730 retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
2731 if (retval)
2732 goto err_request_irq;
2733 }
2734
2735 hcd->state = HC_STATE_RUNNING;
2736 retval = hcd->driver->start(hcd);
2737 if (retval < 0) {
2738 dev_err(hcd->self.controller, "startup error %d\n", retval);
2739 goto err_hcd_driver_start;
2740 }
2741
2742
2743 if ((retval = register_root_hub(hcd)) != 0)
2744 goto err_register_root_hub;
2745
2746 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2747 if (retval < 0) {
2748 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2749 retval);
2750 goto error_create_attr_group;
2751 }
2752 if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
2753 usb_hcd_poll_rh_status(hcd);
2754
2755 return retval;
2756
2757error_create_attr_group:
2758 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2759 if (HC_IS_RUNNING(hcd->state))
2760 hcd->state = HC_STATE_QUIESCING;
2761 spin_lock_irq(&hcd_root_hub_lock);
2762 hcd->rh_registered = 0;
2763 spin_unlock_irq(&hcd_root_hub_lock);
2764
2765#ifdef CONFIG_PM_RUNTIME
2766 cancel_work_sync(&hcd->wakeup_work);
2767#endif
2768 mutex_lock(&usb_bus_list_lock);
2769 usb_disconnect(&rhdev);
2770 mutex_unlock(&usb_bus_list_lock);
2771err_register_root_hub:
2772 hcd->rh_pollable = 0;
2773 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2774 del_timer_sync(&hcd->rh_timer);
2775 hcd->driver->stop(hcd);
2776 hcd->state = HC_STATE_HALT;
2777 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2778 del_timer_sync(&hcd->rh_timer);
2779err_hcd_driver_start:
2780 if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
2781 free_irq(irqnum, hcd);
2782err_request_irq:
2783err_hcd_driver_setup:
2784err_set_rh_speed:
2785 usb_put_invalidate_rhdev(hcd);
2786err_allocate_root_hub:
2787 usb_deregister_bus(&hcd->self);
2788err_register_bus:
2789 hcd_buffer_destroy(hcd);
2790err_remove_phy:
2791 if (hcd->remove_phy && hcd->phy) {
2792 usb_phy_shutdown(hcd->phy);
2793 usb_put_phy(hcd->phy);
2794 hcd->phy = NULL;
2795 }
2796 return retval;
2797}
2798EXPORT_SYMBOL_GPL(usb_add_hcd);
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808void usb_remove_hcd(struct usb_hcd *hcd)
2809{
2810 struct usb_device *rhdev = hcd->self.root_hub;
2811
2812 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2813
2814 usb_get_dev(rhdev);
2815 sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2816
2817 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2818 if (HC_IS_RUNNING (hcd->state))
2819 hcd->state = HC_STATE_QUIESCING;
2820
2821 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
2822 spin_lock_irq (&hcd_root_hub_lock);
2823 hcd->rh_registered = 0;
2824 spin_unlock_irq (&hcd_root_hub_lock);
2825
2826#ifdef CONFIG_PM_RUNTIME
2827 cancel_work_sync(&hcd->wakeup_work);
2828#endif
2829
2830 mutex_lock(&usb_bus_list_lock);
2831 usb_disconnect(&rhdev);
2832 mutex_unlock(&usb_bus_list_lock);
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849 hcd->rh_pollable = 0;
2850 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2851 del_timer_sync(&hcd->rh_timer);
2852
2853 hcd->driver->stop(hcd);
2854 hcd->state = HC_STATE_HALT;
2855
2856
2857 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2858 del_timer_sync(&hcd->rh_timer);
2859
2860 if (usb_hcd_is_primary_hcd(hcd)) {
2861 if (hcd->irq > 0)
2862 free_irq(hcd->irq, hcd);
2863 }
2864
2865 usb_deregister_bus(&hcd->self);
2866 hcd_buffer_destroy(hcd);
2867 if (hcd->remove_phy && hcd->phy) {
2868 usb_phy_shutdown(hcd->phy);
2869 usb_put_phy(hcd->phy);
2870 hcd->phy = NULL;
2871 }
2872
2873 usb_put_invalidate_rhdev(hcd);
2874}
2875EXPORT_SYMBOL_GPL(usb_remove_hcd);
2876
2877void
2878usb_hcd_platform_shutdown(struct platform_device *dev)
2879{
2880 struct usb_hcd *hcd = platform_get_drvdata(dev);
2881
2882 if (hcd->driver->shutdown)
2883 hcd->driver->shutdown(hcd);
2884}
2885EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
2886
2887
2888
2889#if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
2890
2891struct usb_mon_operations *mon_ops;
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901int usb_mon_register (struct usb_mon_operations *ops)
2902{
2903
2904 if (mon_ops)
2905 return -EBUSY;
2906
2907 mon_ops = ops;
2908 mb();
2909 return 0;
2910}
2911EXPORT_SYMBOL_GPL (usb_mon_register);
2912
2913void usb_mon_deregister (void)
2914{
2915
2916 if (mon_ops == NULL) {
2917 printk(KERN_ERR "USB: monitor was not registered\n");
2918 return;
2919 }
2920 mon_ops = NULL;
2921 mb();
2922}
2923EXPORT_SYMBOL_GPL (usb_mon_deregister);
2924
2925#endif
2926