1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/pci.h>
22#include <linux/kernel.h>
23#include <linux/delay.h>
24#include <linux/ioport.h>
25#include <linux/sched.h>
26#include <linux/slab.h>
27#include <linux/smp_lock.h>
28#include <linux/errno.h>
29#include <linux/kmod.h>
30#include <linux/init.h>
31#include <linux/timer.h>
32#include <linux/list.h>
33#include <linux/interrupt.h>
34#include <linux/completion.h>
35#include <linux/uts.h>
36
37
38#ifdef CONFIG_USB_DEBUG
39 #define DEBUG
40#else
41 #undef DEBUG
42#endif
43
44#include <linux/usb.h>
45#include "hcd.h"
46
47#include <asm/io.h>
48#include <asm/irq.h>
49#include <asm/system.h>
50#include <asm/unaligned.h>
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
88
89
90
91static LIST_HEAD (hcd_list);
92
93
94static DECLARE_MUTEX (hcd_list_lock);
95
96
97static spinlock_t hcd_data_lock = SPIN_LOCK_UNLOCKED;
98
99static struct usb_operations hcd_operations;
100
101
102
103
104
105
106
107
108
109#define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
110#define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
111
112
113static const u8 usb2_rh_dev_descriptor [18] = {
114 0x12,
115 0x01,
116 0x00, 0x02,
117
118 0x09,
119 0x00,
120 0x01,
121 0x08,
122
123 0x00, 0x00,
124 0x00, 0x00,
125 KERNEL_VER, KERNEL_REL,
126
127 0x03,
128 0x02,
129 0x01,
130 0x01
131};
132
133
134
135
136static const u8 usb11_rh_dev_descriptor [18] = {
137 0x12,
138 0x01,
139 0x10, 0x01,
140
141 0x09,
142 0x00,
143 0x00,
144 0x08,
145
146 0x00, 0x00,
147 0x00, 0x00,
148 KERNEL_VER, KERNEL_REL,
149
150 0x03,
151 0x02,
152 0x01,
153 0x01
154};
155
156
157
158
159
160
161static const u8 fs_rh_config_descriptor [] = {
162
163
164 0x09,
165 0x02,
166 0x19, 0x00,
167 0x01,
168 0x01,
169 0x00,
170 0x40,
171
172
173
174
175 0x00,
176
177
178
179
180
181
182
183
184
185
186
187
188
189 0x09,
190 0x04,
191 0x00,
192 0x00,
193 0x01,
194 0x09,
195 0x00,
196 0x00,
197 0x00,
198
199
200 0x07,
201 0x05,
202 0x81,
203 0x03,
204 0x02, 0x00,
205 0xff
206};
207
208static const u8 hs_rh_config_descriptor [] = {
209
210
211 0x09,
212 0x02,
213 0x19, 0x00,
214 0x01,
215 0x01,
216 0x00,
217 0x40,
218
219
220
221
222 0x00,
223
224
225
226
227
228
229
230
231
232
233
234
235
236 0x09,
237 0x04,
238 0x00,
239 0x00,
240 0x01,
241 0x09,
242 0x00,
243 0x00,
244 0x00,
245
246
247 0x07,
248 0x05,
249 0x81,
250 0x03,
251 0x02, 0x00,
252 0x0c
253};
254
255
256
257
258
259
260
261static int ascii2utf (char *s, u8 *utf, int utfmax)
262{
263 int retval;
264
265 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
266 *utf++ = *s++;
267 *utf++ = 0;
268 }
269 return retval;
270}
271
272
273
274
275
276
277
278
279
280
281
282
283static int rh_string (
284 int id,
285 struct usb_hcd *hcd,
286 u8 *data,
287 int len
288) {
289 char buf [100];
290
291
292 if (id == 0) {
293 *data++ = 4; *data++ = 3;
294 *data++ = 0; *data++ = 0;
295 return 4;
296
297
298 } else if (id == 1) {
299 strcpy (buf, hcd->bus->bus_name);
300
301
302 } else if (id == 2) {
303 strcpy (buf, hcd->product_desc);
304
305
306 } else if (id == 3) {
307 sprintf (buf, "%s %s %s", UTS_SYSNAME, UTS_RELEASE,
308 hcd->description);
309
310
311 } else
312 return 0;
313
314 data [0] = 2 * (strlen (buf) + 1);
315 data [1] = 3;
316 return 2 + ascii2utf (buf, data + 2, len - 2);
317}
318
319
320
321static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
322{
323 struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
324 u16 typeReq, wValue, wIndex, wLength;
325 const u8 *bufp = 0;
326 u8 *ubuf = urb->transfer_buffer;
327 int len = 0;
328
329 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
330 wValue = le16_to_cpu (cmd->wValue);
331 wIndex = le16_to_cpu (cmd->wIndex);
332 wLength = le16_to_cpu (cmd->wLength);
333
334 if (wLength > urb->transfer_buffer_length)
335 goto error;
336
337
338 urb->status = 0;
339 urb->actual_length = wLength;
340 switch (typeReq) {
341
342
343
344 case DeviceRequest | USB_REQ_GET_STATUS:
345
346 ubuf [0] = 1;
347 ubuf [1] = 0;
348
349 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
350 case DeviceOutRequest | USB_REQ_SET_FEATURE:
351 dbg ("no device features yet yet");
352 break;
353 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
354 ubuf [0] = 1;
355
356 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
357 break;
358 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
359 switch (wValue & 0xff00) {
360 case USB_DT_DEVICE << 8:
361 if (hcd->driver->flags & HCD_USB2)
362 bufp = usb2_rh_dev_descriptor;
363 else if (hcd->driver->flags & HCD_USB11)
364 bufp = usb11_rh_dev_descriptor;
365 else
366 goto error;
367 len = 18;
368 break;
369 case USB_DT_CONFIG << 8:
370 if (hcd->driver->flags & HCD_USB2) {
371 bufp = hs_rh_config_descriptor;
372 len = sizeof hs_rh_config_descriptor;
373 } else {
374 bufp = fs_rh_config_descriptor;
375 len = sizeof fs_rh_config_descriptor;
376 }
377 break;
378 case USB_DT_STRING << 8:
379 urb->actual_length = rh_string (
380 wValue & 0xff, hcd,
381 ubuf, wLength);
382 break;
383 default:
384 goto error;
385 }
386 break;
387 case DeviceRequest | USB_REQ_GET_INTERFACE:
388 ubuf [0] = 0;
389
390 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
391 break;
392 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
393
394 dbg ("%s root hub device address %d",
395 hcd->bus->bus_name, wValue);
396 break;
397
398
399
400
401
402 case EndpointRequest | USB_REQ_GET_STATUS:
403
404 ubuf [0] = 0;
405 ubuf [1] = 0;
406
407 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
408 case EndpointOutRequest | USB_REQ_SET_FEATURE:
409 dbg ("no endpoint features yet");
410 break;
411
412
413
414 default:
415
416 urb->status = hcd->driver->hub_control (hcd,
417 typeReq, wValue, wIndex,
418 ubuf, wLength);
419 break;
420error:
421
422 urb->status = -EPIPE;
423 dbg ("unsupported hub control message (maxchild %d)",
424 urb->dev->maxchild);
425 }
426 if (urb->status) {
427 urb->actual_length = 0;
428 dbg ("CTRL: TypeReq=0x%x val=0x%x idx=0x%x len=%d ==> %d",
429 typeReq, wValue, wIndex, wLength, urb->status);
430 }
431 if (bufp) {
432 if (urb->transfer_buffer_length < len)
433 len = urb->transfer_buffer_length;
434 urb->actual_length = len;
435
436 memcpy (ubuf, bufp, len);
437 }
438
439
440 usb_hcd_giveback_urb (hcd, urb, 0);
441 return 0;
442}
443
444
445
446
447
448
449
450
451static void rh_report_status (unsigned long ptr);
452
453static int rh_status_urb (struct usb_hcd *hcd, struct urb *urb)
454{
455 int len = 1 + (urb->dev->maxchild / 8);
456
457
458 if (timer_pending (&hcd->rh_timer)
459 || urb->status != -EINPROGRESS
460 || !HCD_IS_RUNNING (hcd->state)
461 || urb->transfer_buffer_length < len) {
462 dbg ("not queuing status urb, stat %d", urb->status);
463 return -EINVAL;
464 }
465
466 urb->hcpriv = hcd;
467 init_timer (&hcd->rh_timer);
468 hcd->rh_timer.function = rh_report_status;
469 hcd->rh_timer.data = (unsigned long) urb;
470
471 hcd->rh_timer.expires = jiffies + HZ/4;
472 add_timer (&hcd->rh_timer);
473 return 0;
474}
475
476
477
478static void rh_report_status (unsigned long ptr)
479{
480 struct urb *urb;
481 struct usb_hcd *hcd;
482 int length;
483 unsigned long flags;
484
485 urb = (struct urb *) ptr;
486 spin_lock_irqsave (&urb->lock, flags);
487 if (!urb->dev) {
488 spin_unlock_irqrestore (&urb->lock, flags);
489 return;
490 }
491
492 hcd = urb->dev->bus->hcpriv;
493 if (urb->status == -EINPROGRESS) {
494 if (HCD_IS_RUNNING (hcd->state)) {
495 length = hcd->driver->hub_status_data (hcd,
496 urb->transfer_buffer);
497 spin_unlock_irqrestore (&urb->lock, flags);
498 if (length > 0) {
499 urb->actual_length = length;
500 urb->status = 0;
501 urb->complete (urb);
502 }
503 spin_lock_irqsave (&hcd_data_lock, flags);
504 urb->status = -EINPROGRESS;
505 if (HCD_IS_RUNNING (hcd->state)
506 && rh_status_urb (hcd, urb) != 0) {
507
508 dbg ("%s, can't resubmit roothub status urb?",
509 hcd->bus->bus_name);
510 spin_unlock_irqrestore (&hcd_data_lock, flags);
511 BUG ();
512 }
513 spin_unlock_irqrestore (&hcd_data_lock, flags);
514 } else
515 spin_unlock_irqrestore (&urb->lock, flags);
516 } else {
517
518 urb->hcpriv = 0;
519 spin_unlock_irqrestore (&urb->lock, flags);
520
521 usb_hcd_giveback_urb (hcd, urb, 0);
522 }
523}
524
525
526
527static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
528{
529 if (usb_pipeint (urb->pipe)) {
530 int retval;
531 unsigned long flags;
532
533 spin_lock_irqsave (&hcd_data_lock, flags);
534 retval = rh_status_urb (hcd, urb);
535 spin_unlock_irqrestore (&hcd_data_lock, flags);
536 return retval;
537 }
538 if (usb_pipecontrol (urb->pipe))
539 return rh_call_control (hcd, urb);
540 else
541 return -EINVAL;
542}
543
544
545
546static void rh_status_dequeue (struct usb_hcd *hcd, struct urb *urb)
547{
548 unsigned long flags;
549
550 spin_lock_irqsave (&hcd_data_lock, flags);
551 del_timer_sync (&hcd->rh_timer);
552 hcd->rh_timer.data = 0;
553 spin_unlock_irqrestore (&hcd_data_lock, flags);
554
555
556 usb_hcd_giveback_urb (hcd, urb, 0);
557}
558
559
560
561#ifdef CONFIG_PCI
562
563
564
565static void hcd_irq (int irq, void *__hcd, struct pt_regs *r);
566static void hc_died (struct usb_hcd *hcd);
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
586{
587 struct hc_driver *driver;
588 unsigned long resource, len;
589 void *base;
590 struct usb_bus *bus;
591 struct usb_hcd *hcd;
592 int retval, region;
593 char buf [8], *bufp = buf;
594
595 if (!id || !(driver = (struct hc_driver *) id->driver_data))
596 return -EINVAL;
597
598 if (pci_enable_device (dev) < 0)
599 return -ENODEV;
600
601 if (!dev->irq) {
602 err ("Found HC with no IRQ. Check BIOS/PCI %s setup!",
603 dev->slot_name);
604 return -ENODEV;
605 }
606
607 if (driver->flags & HCD_MEMORY) {
608 region = 0;
609 resource = pci_resource_start (dev, 0);
610 len = pci_resource_len (dev, 0);
611 if (!request_mem_region (resource, len, driver->description)) {
612 dbg ("controller already in use");
613 return -EBUSY;
614 }
615 base = ioremap_nocache (resource, len);
616 if (base == NULL) {
617 dbg ("error mapping memory");
618 retval = -EFAULT;
619clean_1:
620 release_mem_region (resource, len);
621 err ("init %s fail, %d", dev->slot_name, retval);
622 return retval;
623 }
624
625 } else {
626 resource = len = 0;
627 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
628 if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
629 continue;
630
631 resource = pci_resource_start (dev, region);
632 len = pci_resource_len (dev, region);
633 if (request_region (resource, len,
634 driver->description))
635 break;
636 }
637 if (region == PCI_ROM_RESOURCE) {
638 dbg ("no i/o regions available");
639 return -EBUSY;
640 }
641 base = (void *) resource;
642 }
643
644
645
646
647 pci_set_master (dev);
648 hcd = driver->hcd_alloc ();
649 if (hcd == NULL){
650 dbg ("hcd alloc fail");
651 retval = -ENOMEM;
652clean_2:
653 if (driver->flags & HCD_MEMORY) {
654 iounmap (base);
655 goto clean_1;
656 } else {
657 release_region (resource, len);
658 err ("init %s fail, %d", dev->slot_name, retval);
659 return retval;
660 }
661 }
662 pci_set_drvdata(dev, hcd);
663 hcd->driver = driver;
664 hcd->description = driver->description;
665 hcd->pdev = dev;
666 printk (KERN_INFO "%s %s: %s\n",
667 hcd->description, dev->slot_name, dev->name);
668
669#ifndef __sparc__
670 sprintf (buf, "%d", dev->irq);
671#else
672 bufp = __irq_itoa(dev->irq);
673#endif
674 if (request_irq (dev->irq, hcd_irq, SA_SHIRQ, hcd->description, hcd)
675 != 0) {
676 err ("request interrupt %s failed", bufp);
677 retval = -EBUSY;
678clean_3:
679 driver->hcd_free (hcd);
680 goto clean_2;
681 }
682 hcd->irq = dev->irq;
683
684 hcd->regs = base;
685 hcd->region = region;
686 printk (KERN_INFO "%s %s: irq %s, %s %p\n",
687 hcd->description, dev->slot_name, bufp,
688 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
689 base);
690
691
692
693 bus = usb_alloc_bus (&hcd_operations);
694 if (bus == NULL) {
695 dbg ("usb_alloc_bus fail");
696 retval = -ENOMEM;
697 free_irq (dev->irq, hcd);
698 goto clean_3;
699 }
700 hcd->bus = bus;
701 bus->bus_name = dev->slot_name;
702 hcd->product_desc = dev->name;
703 bus->hcpriv = (void *) hcd;
704
705 INIT_LIST_HEAD (&hcd->dev_list);
706 INIT_LIST_HEAD (&hcd->hcd_list);
707
708 down (&hcd_list_lock);
709 list_add (&hcd->hcd_list, &hcd_list);
710 up (&hcd_list_lock);
711
712 usb_register_bus (bus);
713
714 if ((retval = driver->start (hcd)) < 0)
715 usb_hcd_pci_remove (dev);
716
717 return retval;
718}
719EXPORT_SYMBOL (usb_hcd_pci_probe);
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736void usb_hcd_pci_remove (struct pci_dev *dev)
737{
738 struct usb_hcd *hcd;
739 struct usb_device *hub;
740
741 hcd = pci_get_drvdata(dev);
742 if (!hcd)
743 return;
744 printk (KERN_INFO "%s %s: remove state %x\n",
745 hcd->description, dev->slot_name, hcd->state);
746
747 if (in_interrupt ()) BUG ();
748
749 hub = hcd->bus->root_hub;
750 hcd->state = USB_STATE_QUIESCING;
751
752 dbg ("%s: roothub graceful disconnect", hcd->bus->bus_name);
753 usb_disconnect (&hub);
754
755
756 hcd->driver->stop (hcd);
757 hcd->state = USB_STATE_HALT;
758
759 free_irq (hcd->irq, hcd);
760 if (hcd->driver->flags & HCD_MEMORY) {
761 iounmap (hcd->regs);
762 release_mem_region (pci_resource_start (dev, 0),
763 pci_resource_len (dev, 0));
764 } else {
765 release_region (pci_resource_start (dev, hcd->region),
766 pci_resource_len (dev, hcd->region));
767 }
768
769 down (&hcd_list_lock);
770 list_del (&hcd->hcd_list);
771 up (&hcd_list_lock);
772
773 usb_deregister_bus (hcd->bus);
774 usb_free_bus (hcd->bus);
775 hcd->bus = NULL;
776
777 hcd->driver->hcd_free (hcd);
778}
779EXPORT_SYMBOL (usb_hcd_pci_remove);
780
781
782#ifdef CONFIG_PM
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
818{
819 struct usb_hcd *hcd;
820 int retval;
821
822 hcd = pci_get_drvdata(dev);
823 printk (KERN_INFO "%s %s: suspend to state %d\n",
824 hcd->description, dev->slot_name, state);
825
826 pci_save_state (dev, hcd->pci_state);
827
828
829
830
831
832
833 retval = hcd->driver->suspend (hcd, state);
834 hcd->state = USB_STATE_SUSPENDED;
835
836 pci_set_power_state (dev, state);
837 return retval;
838}
839EXPORT_SYMBOL (usb_hcd_pci_suspend);
840
841
842
843
844
845
846
847int usb_hcd_pci_resume (struct pci_dev *dev)
848{
849 struct usb_hcd *hcd;
850 int retval;
851
852 hcd = pci_get_drvdata(dev);
853 printk (KERN_INFO "%s %s: resume\n",
854 hcd->description, dev->slot_name);
855
856
857 atomic_inc (&hcd->resume_count);
858 if (atomic_read (&hcd->resume_count) != 1) {
859 err ("concurrent PCI resumes for %s", hcd->bus->bus_name);
860 retval = 0;
861 goto done;
862 }
863
864 retval = -EBUSY;
865 if (hcd->state != USB_STATE_SUSPENDED) {
866 dbg ("can't resume, not suspended!");
867 goto done;
868 }
869 hcd->state = USB_STATE_RESUMING;
870
871 pci_set_power_state (dev, 0);
872 pci_restore_state (dev, hcd->pci_state);
873
874 retval = hcd->driver->resume (hcd);
875 if (!HCD_IS_RUNNING (hcd->state)) {
876 dbg ("resume %s failure, retval %d",
877 hcd->bus->bus_name, retval);
878 hc_died (hcd);
879
880 } else {
881
882
883
884 }
885
886done:
887 atomic_dec (&hcd->resume_count);
888 return retval;
889}
890EXPORT_SYMBOL (usb_hcd_pci_resume);
891
892#endif
893
894#endif
895
896
897
898
899
900
901
902
903
904
905static int hcd_alloc_dev (struct usb_device *udev)
906{
907 struct hcd_dev *dev;
908 struct usb_hcd *hcd;
909 unsigned long flags;
910
911 if (!udev || udev->hcpriv)
912 return -EINVAL;
913 if (!udev->bus || !udev->bus->hcpriv)
914 return -ENODEV;
915 hcd = udev->bus->hcpriv;
916 if (hcd->state == USB_STATE_QUIESCING)
917 return -ENOLINK;
918
919 dev = (struct hcd_dev *) kmalloc (sizeof *dev, GFP_KERNEL);
920 if (dev == NULL)
921 return -ENOMEM;
922 memset (dev, 0, sizeof *dev);
923
924 INIT_LIST_HEAD (&dev->dev_list);
925 INIT_LIST_HEAD (&dev->urb_list);
926
927 spin_lock_irqsave (&hcd_data_lock, flags);
928 list_add (&dev->dev_list, &hcd->dev_list);
929
930 udev->hcpriv = dev;
931 spin_unlock_irqrestore (&hcd_data_lock, flags);
932
933 return 0;
934}
935
936
937
938static void hcd_panic (void *_hcd)
939{
940 struct usb_hcd *hcd = _hcd;
941 hcd->driver->stop (hcd);
942}
943
944static void hc_died (struct usb_hcd *hcd)
945{
946 struct list_head *devlist, *urblist;
947 struct hcd_dev *dev;
948 struct urb *urb;
949 unsigned long flags;
950
951
952 spin_lock_irqsave (&hcd_data_lock, flags);
953 list_for_each (devlist, &hcd->dev_list) {
954 dev = list_entry (devlist, struct hcd_dev, dev_list);
955 list_for_each (urblist, &dev->urb_list) {
956 urb = list_entry (urblist, struct urb, urb_list);
957 dbg ("shutdown %s urb %p pipe %x, current status %d",
958 hcd->bus->bus_name,
959 urb, urb->pipe, urb->status);
960 if (urb->status == -EINPROGRESS)
961 urb->status = -ESHUTDOWN;
962 }
963 }
964 urb = (struct urb *) hcd->rh_timer.data;
965 if (urb)
966 urb->status = -ESHUTDOWN;
967 spin_unlock_irqrestore (&hcd_data_lock, flags);
968
969 if (urb)
970 rh_status_dequeue (hcd, urb);
971
972
973 INIT_TQUEUE (&hcd->work, hcd_panic, hcd);
974 (void) schedule_task (&hcd->work);
975}
976
977
978
979static void urb_unlink (struct urb *urb)
980{
981 unsigned long flags;
982 struct usb_device *dev;
983
984
985 if (urb->bandwidth)
986 usb_release_bandwidth (urb->dev, urb,
987 usb_pipeisoc (urb->pipe));
988
989
990
991 spin_lock_irqsave (&hcd_data_lock, flags);
992 list_del_init (&urb->urb_list);
993 dev = urb->dev;
994 urb->dev = NULL;
995 usb_dec_dev_use (dev);
996 spin_unlock_irqrestore (&hcd_data_lock, flags);
997}
998
999
1000
1001
1002
1003static int hcd_submit_urb (struct urb *urb)
1004{
1005 int status;
1006 struct usb_hcd *hcd;
1007 struct hcd_dev *dev;
1008 unsigned long flags;
1009 int pipe, temp, max;
1010 int mem_flags;
1011
1012 if (!urb || urb->hcpriv || !urb->complete)
1013 return -EINVAL;
1014
1015 urb->status = -EINPROGRESS;
1016 urb->actual_length = 0;
1017 urb->bandwidth = 0;
1018 INIT_LIST_HEAD (&urb->urb_list);
1019
1020 if (!urb->dev || !urb->dev->bus || urb->dev->devnum <= 0)
1021 return -ENODEV;
1022 hcd = urb->dev->bus->hcpriv;
1023 dev = urb->dev->hcpriv;
1024 if (!hcd || !dev)
1025 return -ENODEV;
1026
1027
1028 if (hcd->state == USB_STATE_QUIESCING || !HCD_IS_RUNNING (hcd->state))
1029 return -ESHUTDOWN;
1030 pipe = urb->pipe;
1031 temp = usb_pipetype (urb->pipe);
1032 if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe),
1033 usb_pipeout (pipe)))
1034 return -EPIPE;
1035
1036
1037 mem_flags = GFP_ATOMIC;
1038
1039
1040
1041
1042
1043
1044 max = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1045 if (max <= 0) {
1046 err ("bogus endpoint (bad maxpacket)");
1047 return -EINVAL;
1048 }
1049
1050
1051 if (urb->dev->speed == USB_SPEED_HIGH) {
1052 int mult;
1053 switch (temp) {
1054 case PIPE_ISOCHRONOUS:
1055 case PIPE_INTERRUPT:
1056 mult = 1 + ((max >> 11) & 0x03);
1057 max &= 0x03ff;
1058 max *= mult;
1059 }
1060 }
1061
1062
1063 switch (temp) {
1064 case PIPE_ISOCHRONOUS: {
1065 int n, len;
1066
1067 if (urb->number_of_packets <= 0)
1068 return -EINVAL;
1069 for (n = 0; n < urb->number_of_packets; n++) {
1070 len = urb->iso_frame_desc [n].length;
1071 if (len < 0 || len > max)
1072 return -EINVAL;
1073 }
1074
1075 }
1076 break;
1077 case PIPE_INTERRUPT:
1078 if (urb->transfer_buffer_length > max)
1079 return -EINVAL;
1080 }
1081
1082
1083 if (urb->transfer_buffer_length < 0)
1084 return -EINVAL;
1085
1086 if (urb->next) {
1087 warn ("use explicit queuing not urb->next");
1088 return -EINVAL;
1089 }
1090
1091#ifdef DEBUG
1092
1093
1094
1095 {
1096 unsigned int orig_flags = urb->transfer_flags;
1097 unsigned int allowed;
1098
1099
1100 allowed = USB_ASYNC_UNLINK;
1101 allowed |= USB_NO_FSBR;
1102 switch (temp) {
1103 case PIPE_CONTROL:
1104 allowed |= USB_DISABLE_SPD;
1105 break;
1106 case PIPE_BULK:
1107 allowed |= USB_DISABLE_SPD | USB_QUEUE_BULK
1108 | USB_ZERO_PACKET | URB_NO_INTERRUPT;
1109 break;
1110 case PIPE_INTERRUPT:
1111 allowed |= USB_DISABLE_SPD;
1112 break;
1113 case PIPE_ISOCHRONOUS:
1114 allowed |= USB_ISO_ASAP;
1115 break;
1116 }
1117 urb->transfer_flags &= allowed;
1118
1119
1120 if (urb->transfer_flags != orig_flags) {
1121 err ("BOGUS urb flags, %x --> %x",
1122 orig_flags, urb->transfer_flags);
1123 return -EINVAL;
1124 }
1125 }
1126#endif
1127
1128
1129
1130
1131
1132
1133
1134
1135 switch (temp) {
1136 case PIPE_ISOCHRONOUS:
1137 case PIPE_INTERRUPT:
1138
1139 if (urb->interval <= 0)
1140 return -EINVAL;
1141
1142 switch (urb->dev->speed) {
1143 case USB_SPEED_HIGH:
1144
1145 if (urb->interval > (1024 * 8))
1146 urb->interval = 1024 * 8;
1147 temp = 1024 * 8;
1148 break;
1149 case USB_SPEED_FULL:
1150 case USB_SPEED_LOW:
1151 if (temp == PIPE_INTERRUPT) {
1152 if (urb->interval > 255)
1153 return -EINVAL;
1154
1155 temp = 128;
1156 } else {
1157 if (urb->interval > 1024)
1158 urb->interval = 1024;
1159
1160 temp = 1024;
1161 }
1162 break;
1163 default:
1164 return -EINVAL;
1165 }
1166
1167 while (temp > urb->interval)
1168 temp >>= 1;
1169 urb->interval = temp;
1170 }
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190 spin_lock_irqsave (&hcd_data_lock, flags);
1191 if (HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_QUIESCING) {
1192 usb_inc_dev_use (urb->dev);
1193 list_add (&urb->urb_list, &dev->urb_list);
1194 status = 0;
1195 } else {
1196 INIT_LIST_HEAD (&urb->urb_list);
1197 status = -ESHUTDOWN;
1198 }
1199 spin_unlock_irqrestore (&hcd_data_lock, flags);
1200 if (status)
1201 return status;
1202
1203
1204
1205
1206 if (urb->dev == hcd->bus->root_hub) {
1207 status = rh_urb_enqueue (hcd, urb);
1208 } else {
1209 if (usb_pipecontrol (urb->pipe))
1210 urb->setup_dma = pci_map_single (
1211 hcd->pdev,
1212 urb->setup_packet,
1213 sizeof (struct usb_ctrlrequest),
1214 PCI_DMA_TODEVICE);
1215 if (urb->transfer_buffer_length != 0)
1216 urb->transfer_dma = pci_map_single (
1217 hcd->pdev,
1218 urb->transfer_buffer,
1219 urb->transfer_buffer_length,
1220 usb_pipein (urb->pipe)
1221 ? PCI_DMA_FROMDEVICE
1222 : PCI_DMA_TODEVICE);
1223 status = hcd->driver->urb_enqueue (hcd, urb, mem_flags);
1224 }
1225 return status;
1226}
1227
1228
1229
1230
1231static int hcd_get_frame_number (struct usb_device *udev)
1232{
1233 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1234 return hcd->driver->get_frame_number (hcd);
1235}
1236
1237
1238
1239struct completion_splice {
1240
1241 struct completion done;
1242
1243
1244 void (*complete)(struct urb *);
1245 void *context;
1246};
1247
1248static void unlink_complete (struct urb *urb)
1249{
1250 struct completion_splice *splice;
1251
1252 splice = (struct completion_splice *) urb->context;
1253
1254
1255 urb->complete = splice->complete;
1256 urb->context = splice->context;
1257 urb->complete (urb);
1258
1259
1260 complete (&splice->done);
1261}
1262
1263
1264
1265
1266
1267
1268
1269static int hcd_unlink_urb (struct urb *urb)
1270{
1271 struct hcd_dev *dev;
1272 struct usb_hcd *hcd = 0;
1273 unsigned long flags;
1274 struct completion_splice splice;
1275 int retval;
1276
1277 if (!urb)
1278 return -EINVAL;
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 spin_lock_irqsave (&urb->lock, flags);
1292 spin_lock (&hcd_data_lock);
1293 if (!urb->hcpriv || urb->transfer_flags & USB_TIMEOUT_KILLED) {
1294 retval = -EINVAL;
1295 goto done;
1296 }
1297
1298 if (!urb->dev || !urb->dev->bus) {
1299 retval = -ENODEV;
1300 goto done;
1301 }
1302
1303
1304 dev = urb->dev->hcpriv;
1305 hcd = urb->dev->bus->hcpriv;
1306 if (!dev || !hcd) {
1307 retval = -ENODEV;
1308 goto done;
1309 }
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324 if (urb->status != -EINPROGRESS) {
1325 if (usb_pipetype (urb->pipe) == PIPE_INTERRUPT)
1326 retval = -EAGAIN;
1327 else
1328 retval = -EBUSY;
1329 goto done;
1330 }
1331
1332
1333 if ((urb->transfer_flags & USB_TIMEOUT_KILLED))
1334 urb->status = -ETIMEDOUT;
1335 else if (!(urb->transfer_flags & USB_ASYNC_UNLINK)) {
1336 if (in_interrupt ()) {
1337 dbg ("non-async unlink in_interrupt");
1338 retval = -EWOULDBLOCK;
1339 goto done;
1340 }
1341
1342 init_completion (&splice.done);
1343 splice.complete = urb->complete;
1344 splice.context = urb->context;
1345 urb->complete = unlink_complete;
1346 urb->context = &splice;
1347 urb->status = -ENOENT;
1348 } else {
1349
1350 urb->status = -ECONNRESET;
1351 }
1352 spin_unlock (&hcd_data_lock);
1353 spin_unlock_irqrestore (&urb->lock, flags);
1354
1355 if (urb == (struct urb *) hcd->rh_timer.data) {
1356 rh_status_dequeue (hcd, urb);
1357 retval = 0;
1358 } else {
1359 retval = hcd->driver->urb_dequeue (hcd, urb);
1360
1361if (retval && urb->status == -ENOENT) err ("whoa! retval %d", retval);
1362 }
1363
1364
1365 if (!(urb->transfer_flags & (USB_ASYNC_UNLINK|USB_TIMEOUT_KILLED))
1366 && HCD_IS_RUNNING (hcd->state)
1367 && !retval) {
1368 wait_for_completion (&splice.done);
1369 } else if ((urb->transfer_flags & USB_ASYNC_UNLINK) && retval == 0) {
1370 return -EINPROGRESS;
1371 }
1372 goto bye;
1373done:
1374 spin_unlock (&hcd_data_lock);
1375 spin_unlock_irqrestore (&urb->lock, flags);
1376bye:
1377 if (retval)
1378 dbg ("%s: hcd_unlink_urb fail %d",
1379 hcd ? hcd->bus->bus_name : "(no bus?)",
1380 retval);
1381 return retval;
1382}
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392static int hcd_free_dev (struct usb_device *udev)
1393{
1394 struct hcd_dev *dev;
1395 struct usb_hcd *hcd;
1396 unsigned long flags;
1397
1398 if (!udev || !udev->hcpriv)
1399 return -EINVAL;
1400
1401 if (!udev->bus || !udev->bus->hcpriv)
1402 return -ENODEV;
1403
1404
1405
1406 dev = udev->hcpriv;
1407 hcd = udev->bus->hcpriv;
1408
1409
1410 if (!list_empty (&dev->urb_list)) {
1411 dbg ("free busy dev, %s devnum %d (bug!)",
1412 hcd->bus->bus_name, udev->devnum);
1413 return -EINVAL;
1414 }
1415
1416 hcd->driver->free_config (hcd, udev);
1417
1418 spin_lock_irqsave (&hcd_data_lock, flags);
1419 list_del (&dev->dev_list);
1420 udev->hcpriv = NULL;
1421 spin_unlock_irqrestore (&hcd_data_lock, flags);
1422
1423 kfree (dev);
1424 return 0;
1425}
1426
1427static struct usb_operations hcd_operations = {
1428 allocate: hcd_alloc_dev,
1429 get_frame_number: hcd_get_frame_number,
1430 submit_urb: hcd_submit_urb,
1431 unlink_urb: hcd_unlink_urb,
1432 deallocate: hcd_free_dev,
1433};
1434
1435
1436
1437static void hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1438{
1439 struct usb_hcd *hcd = __hcd;
1440 int start = hcd->state;
1441
1442 if (unlikely (hcd->state == USB_STATE_HALT))
1443 return;
1444
1445 hcd->driver->irq (hcd, r);
1446 if (hcd->state != start && hcd->state == USB_STATE_HALT)
1447 hc_died (hcd);
1448}
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1475{
1476 int is_root_hub_operation;
1477
1478
1479 is_root_hub_operation = (urb->dev == hcd->bus->root_hub);
1480
1481 urb_unlink (urb);
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492 if (usb_pipecontrol (urb->pipe) && !is_root_hub_operation)
1493 pci_unmap_single (hcd->pdev, urb->setup_dma,
1494 sizeof (struct usb_ctrlrequest),
1495 PCI_DMA_TODEVICE);
1496
1497 if ((urb->transfer_buffer_length != 0) && !is_root_hub_operation)
1498 pci_unmap_single (hcd->pdev, urb->transfer_dma,
1499 urb->transfer_buffer_length,
1500 usb_pipein (urb->pipe)
1501 ? PCI_DMA_FROMDEVICE
1502 : PCI_DMA_TODEVICE);
1503
1504
1505 urb->complete (urb);
1506}
1507EXPORT_SYMBOL (usb_hcd_giveback_urb);
1508