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