1
2
3
4
5#include <linux/pci.h>
6#include <linux/usb.h>
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/init.h>
10#include <linux/mm.h>
11#include <linux/timer.h>
12#include <linux/ctype.h>
13#include <linux/nls.h>
14#include <linux/device.h>
15#include <linux/scatterlist.h>
16#include <linux/usb/quirks.h>
17#include <linux/usb/hcd.h>
18#include <asm/byteorder.h>
19
20#include "usb.h"
21
22static void cancel_async_set_config(struct usb_device *udev);
23
24struct api_context {
25 struct completion done;
26 int status;
27};
28
29static void usb_api_blocking_completion(struct urb *urb)
30{
31 struct api_context *ctx = urb->context;
32
33 ctx->status = urb->status;
34 complete(&ctx->done);
35}
36
37
38
39
40
41
42
43
44static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
45{
46 struct api_context ctx;
47 unsigned long expire;
48 int retval;
49
50 init_completion(&ctx.done);
51 urb->context = &ctx;
52 urb->actual_length = 0;
53 retval = usb_submit_urb(urb, GFP_NOIO);
54 if (unlikely(retval))
55 goto out;
56
57 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
58 if (!wait_for_completion_timeout(&ctx.done, expire)) {
59 usb_kill_urb(urb);
60 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
61
62 dev_dbg(&urb->dev->dev,
63 "%s timed out on ep%d%s len=%u/%u\n",
64 current->comm,
65 usb_endpoint_num(&urb->ep->desc),
66 usb_urb_dir_in(urb) ? "in" : "out",
67 urb->actual_length,
68 urb->transfer_buffer_length);
69 } else
70 retval = ctx.status;
71out:
72 if (actual_length)
73 *actual_length = urb->actual_length;
74
75 usb_free_urb(urb);
76 return retval;
77}
78
79
80
81static int usb_internal_control_msg(struct usb_device *usb_dev,
82 unsigned int pipe,
83 struct usb_ctrlrequest *cmd,
84 void *data, int len, int timeout)
85{
86 struct urb *urb;
87 int retv;
88 int length;
89
90 urb = usb_alloc_urb(0, GFP_NOIO);
91 if (!urb)
92 return -ENOMEM;
93
94 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
95 len, usb_api_blocking_completion, NULL);
96
97 retv = usb_start_wait_urb(urb, timeout, &length);
98 if (retv < 0)
99 return retv;
100 else
101 return length;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
133 __u8 requesttype, __u16 value, __u16 index, void *data,
134 __u16 size, int timeout)
135{
136 struct usb_ctrlrequest *dr;
137 int ret;
138
139 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
140 if (!dr)
141 return -ENOMEM;
142
143 dr->bRequestType = requesttype;
144 dr->bRequest = request;
145 dr->wValue = cpu_to_le16(value);
146 dr->wIndex = cpu_to_le16(index);
147 dr->wLength = cpu_to_le16(size);
148
149 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
150
151 kfree(dr);
152
153 return ret;
154}
155EXPORT_SYMBOL_GPL(usb_control_msg);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
184 void *data, int len, int *actual_length, int timeout)
185{
186 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
187}
188EXPORT_SYMBOL_GPL(usb_interrupt_msg);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
222 void *data, int len, int *actual_length, int timeout)
223{
224 struct urb *urb;
225 struct usb_host_endpoint *ep;
226
227 ep = usb_pipe_endpoint(usb_dev, pipe);
228 if (!ep || len < 0)
229 return -EINVAL;
230
231 urb = usb_alloc_urb(0, GFP_KERNEL);
232 if (!urb)
233 return -ENOMEM;
234
235 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
236 USB_ENDPOINT_XFER_INT) {
237 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
238 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
239 usb_api_blocking_completion, NULL,
240 ep->desc.bInterval);
241 } else
242 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
243 usb_api_blocking_completion, NULL);
244
245 return usb_start_wait_urb(urb, timeout, actual_length);
246}
247EXPORT_SYMBOL_GPL(usb_bulk_msg);
248
249
250
251static void sg_clean(struct usb_sg_request *io)
252{
253 if (io->urbs) {
254 while (io->entries--)
255 usb_free_urb(io->urbs [io->entries]);
256 kfree(io->urbs);
257 io->urbs = NULL;
258 }
259 io->dev = NULL;
260}
261
262static void sg_complete(struct urb *urb)
263{
264 struct usb_sg_request *io = urb->context;
265 int status = urb->status;
266
267 spin_lock(&io->lock);
268
269
270
271
272
273
274
275
276
277
278
279 if (io->status
280 && (io->status != -ECONNRESET
281 || status != -ECONNRESET)
282 && urb->actual_length) {
283 dev_err(io->dev->bus->controller,
284 "dev %s ep%d%s scatterlist error %d/%d\n",
285 io->dev->devpath,
286 usb_endpoint_num(&urb->ep->desc),
287 usb_urb_dir_in(urb) ? "in" : "out",
288 status, io->status);
289
290 }
291
292 if (io->status == 0 && status && status != -ECONNRESET) {
293 int i, found, retval;
294
295 io->status = status;
296
297
298
299
300
301 spin_unlock(&io->lock);
302 for (i = 0, found = 0; i < io->entries; i++) {
303 if (!io->urbs [i] || !io->urbs [i]->dev)
304 continue;
305 if (found) {
306 retval = usb_unlink_urb(io->urbs [i]);
307 if (retval != -EINPROGRESS &&
308 retval != -ENODEV &&
309 retval != -EBUSY &&
310 retval != -EIDRM)
311 dev_err(&io->dev->dev,
312 "%s, unlink --> %d\n",
313 __func__, retval);
314 } else if (urb == io->urbs [i])
315 found = 1;
316 }
317 spin_lock(&io->lock);
318 }
319
320
321 io->bytes += urb->actual_length;
322 io->count--;
323 if (!io->count)
324 complete(&io->complete);
325
326 spin_unlock(&io->lock);
327}
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
356 unsigned pipe, unsigned period, struct scatterlist *sg,
357 int nents, size_t length, gfp_t mem_flags)
358{
359 int i;
360 int urb_flags;
361 int use_sg;
362
363 if (!io || !dev || !sg
364 || usb_pipecontrol(pipe)
365 || usb_pipeisoc(pipe)
366 || nents <= 0)
367 return -EINVAL;
368
369 spin_lock_init(&io->lock);
370 io->dev = dev;
371 io->pipe = pipe;
372
373 if (dev->bus->sg_tablesize > 0) {
374 use_sg = true;
375 io->entries = 1;
376 } else {
377 use_sg = false;
378 io->entries = nents;
379 }
380
381
382 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
383 if (!io->urbs)
384 goto nomem;
385
386 urb_flags = URB_NO_INTERRUPT;
387 if (usb_pipein(pipe))
388 urb_flags |= URB_SHORT_NOT_OK;
389
390 for_each_sg(sg, sg, io->entries, i) {
391 struct urb *urb;
392 unsigned len;
393
394 urb = usb_alloc_urb(0, mem_flags);
395 if (!urb) {
396 io->entries = i;
397 goto nomem;
398 }
399 io->urbs[i] = urb;
400
401 urb->dev = NULL;
402 urb->pipe = pipe;
403 urb->interval = period;
404 urb->transfer_flags = urb_flags;
405 urb->complete = sg_complete;
406 urb->context = io;
407 urb->sg = sg;
408
409 if (use_sg) {
410
411 urb->transfer_buffer = NULL;
412 urb->num_sgs = nents;
413
414
415 len = length;
416 if (len == 0) {
417 struct scatterlist *sg2;
418 int j;
419
420 for_each_sg(sg, sg2, nents, j)
421 len += sg2->length;
422 }
423 } else {
424
425
426
427
428
429 if (!PageHighMem(sg_page(sg)))
430 urb->transfer_buffer = sg_virt(sg);
431 else
432 urb->transfer_buffer = NULL;
433
434 len = sg->length;
435 if (length) {
436 len = min_t(size_t, len, length);
437 length -= len;
438 if (length == 0)
439 io->entries = i + 1;
440 }
441 }
442 urb->transfer_buffer_length = len;
443 }
444 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
445
446
447 io->count = io->entries;
448 io->status = 0;
449 io->bytes = 0;
450 init_completion(&io->complete);
451 return 0;
452
453nomem:
454 sg_clean(io);
455 return -ENOMEM;
456}
457EXPORT_SYMBOL_GPL(usb_sg_init);
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502void usb_sg_wait(struct usb_sg_request *io)
503{
504 int i;
505 int entries = io->entries;
506
507
508 spin_lock_irq(&io->lock);
509 i = 0;
510 while (i < entries && !io->status) {
511 int retval;
512
513 io->urbs[i]->dev = io->dev;
514 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
515
516
517
518
519 spin_unlock_irq(&io->lock);
520 switch (retval) {
521
522 case -ENXIO:
523 case -EAGAIN:
524 case -ENOMEM:
525 retval = 0;
526 yield();
527 break;
528
529
530
531
532
533
534
535 case 0:
536 ++i;
537 cpu_relax();
538 break;
539
540
541 default:
542 io->urbs[i]->status = retval;
543 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
544 __func__, retval);
545 usb_sg_cancel(io);
546 }
547 spin_lock_irq(&io->lock);
548 if (retval && (io->status == 0 || io->status == -ECONNRESET))
549 io->status = retval;
550 }
551 io->count -= entries - i;
552 if (io->count == 0)
553 complete(&io->complete);
554 spin_unlock_irq(&io->lock);
555
556
557
558
559
560 wait_for_completion(&io->complete);
561
562 sg_clean(io);
563}
564EXPORT_SYMBOL_GPL(usb_sg_wait);
565
566
567
568
569
570
571
572
573
574void usb_sg_cancel(struct usb_sg_request *io)
575{
576 unsigned long flags;
577
578 spin_lock_irqsave(&io->lock, flags);
579
580
581 if (!io->status) {
582 int i;
583
584 io->status = -ECONNRESET;
585 spin_unlock(&io->lock);
586 for (i = 0; i < io->entries; i++) {
587 int retval;
588
589 if (!io->urbs [i]->dev)
590 continue;
591 retval = usb_unlink_urb(io->urbs [i]);
592 if (retval != -EINPROGRESS
593 && retval != -ENODEV
594 && retval != -EBUSY
595 && retval != -EIDRM)
596 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
597 __func__, retval);
598 }
599 spin_lock(&io->lock);
600 }
601 spin_unlock_irqrestore(&io->lock, flags);
602}
603EXPORT_SYMBOL_GPL(usb_sg_cancel);
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629int usb_get_descriptor(struct usb_device *dev, unsigned char type,
630 unsigned char index, void *buf, int size)
631{
632 int i;
633 int result;
634
635 memset(buf, 0, size);
636
637 for (i = 0; i < 3; ++i) {
638
639 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
640 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
641 (type << 8) + index, 0, buf, size,
642 USB_CTRL_GET_TIMEOUT);
643 if (result <= 0 && result != -ETIMEDOUT)
644 continue;
645 if (result > 1 && ((u8 *)buf)[1] != type) {
646 result = -ENODATA;
647 continue;
648 }
649 break;
650 }
651 return result;
652}
653EXPORT_SYMBOL_GPL(usb_get_descriptor);
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677static int usb_get_string(struct usb_device *dev, unsigned short langid,
678 unsigned char index, void *buf, int size)
679{
680 int i;
681 int result;
682
683 for (i = 0; i < 3; ++i) {
684
685 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
686 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
687 (USB_DT_STRING << 8) + index, langid, buf, size,
688 USB_CTRL_GET_TIMEOUT);
689 if (result == 0 || result == -EPIPE)
690 continue;
691 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
692 result = -ENODATA;
693 continue;
694 }
695 break;
696 }
697 return result;
698}
699
700static void usb_try_string_workarounds(unsigned char *buf, int *length)
701{
702 int newlength, oldlength = *length;
703
704 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
705 if (!isprint(buf[newlength]) || buf[newlength + 1])
706 break;
707
708 if (newlength > 2) {
709 buf[0] = newlength;
710 *length = newlength;
711 }
712}
713
714static int usb_string_sub(struct usb_device *dev, unsigned int langid,
715 unsigned int index, unsigned char *buf)
716{
717 int rc;
718
719
720
721 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
722 rc = -EIO;
723 else
724 rc = usb_get_string(dev, langid, index, buf, 255);
725
726
727
728 if (rc < 2) {
729 rc = usb_get_string(dev, langid, index, buf, 2);
730 if (rc == 2)
731 rc = usb_get_string(dev, langid, index, buf, buf[0]);
732 }
733
734 if (rc >= 2) {
735 if (!buf[0] && !buf[1])
736 usb_try_string_workarounds(buf, &rc);
737
738
739 if (buf[0] < rc)
740 rc = buf[0];
741
742 rc = rc - (rc & 1);
743 }
744
745 if (rc < 2)
746 rc = (rc < 0 ? rc : -EINVAL);
747
748 return rc;
749}
750
751static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
752{
753 int err;
754
755 if (dev->have_langid)
756 return 0;
757
758 if (dev->string_langid < 0)
759 return -EPIPE;
760
761 err = usb_string_sub(dev, 0, 0, tbuf);
762
763
764
765 if (err == -ENODATA || (err > 0 && err < 4)) {
766 dev->string_langid = 0x0409;
767 dev->have_langid = 1;
768 dev_err(&dev->dev,
769 "string descriptor 0 malformed (err = %d), "
770 "defaulting to 0x%04x\n",
771 err, dev->string_langid);
772 return 0;
773 }
774
775
776
777
778 if (err < 0) {
779 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
780 err);
781 dev->string_langid = -1;
782 return -EPIPE;
783 }
784
785
786 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
787 dev->have_langid = 1;
788 dev_dbg(&dev->dev, "default language 0x%04x\n",
789 dev->string_langid);
790 return 0;
791}
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
811{
812 unsigned char *tbuf;
813 int err;
814
815 if (dev->state == USB_STATE_SUSPENDED)
816 return -EHOSTUNREACH;
817 if (size <= 0 || !buf || !index)
818 return -EINVAL;
819 buf[0] = 0;
820 tbuf = kmalloc(256, GFP_NOIO);
821 if (!tbuf)
822 return -ENOMEM;
823
824 err = usb_get_langid(dev, tbuf);
825 if (err < 0)
826 goto errout;
827
828 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
829 if (err < 0)
830 goto errout;
831
832 size--;
833 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
834 UTF16_LITTLE_ENDIAN, buf, size);
835 buf[err] = 0;
836
837 if (tbuf[1] != USB_DT_STRING)
838 dev_dbg(&dev->dev,
839 "wrong descriptor type %02x for string %d (\"%s\")\n",
840 tbuf[1], index, buf);
841
842 errout:
843 kfree(tbuf);
844 return err;
845}
846EXPORT_SYMBOL_GPL(usb_string);
847
848
849#define MAX_USB_STRING_SIZE (127 * 3 + 1)
850
851
852
853
854
855
856
857
858
859char *usb_cache_string(struct usb_device *udev, int index)
860{
861 char *buf;
862 char *smallbuf = NULL;
863 int len;
864
865 if (index <= 0)
866 return NULL;
867
868 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
869 if (buf) {
870 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
871 if (len > 0) {
872 smallbuf = kmalloc(++len, GFP_NOIO);
873 if (!smallbuf)
874 return buf;
875 memcpy(smallbuf, buf, len);
876 }
877 kfree(buf);
878 }
879 return smallbuf;
880}
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
901{
902 struct usb_device_descriptor *desc;
903 int ret;
904
905 if (size > sizeof(*desc))
906 return -EINVAL;
907 desc = kmalloc(sizeof(*desc), GFP_NOIO);
908 if (!desc)
909 return -ENOMEM;
910
911 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
912 if (ret >= 0)
913 memcpy(&dev->descriptor, desc, size);
914 kfree(desc);
915 return ret;
916}
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940int usb_get_status(struct usb_device *dev, int type, int target, void *data)
941{
942 int ret;
943 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
944
945 if (!status)
946 return -ENOMEM;
947
948 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
949 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
950 sizeof(*status), USB_CTRL_GET_TIMEOUT);
951
952 *(u16 *)data = *status;
953 kfree(status);
954 return ret;
955}
956EXPORT_SYMBOL_GPL(usb_get_status);
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981int usb_clear_halt(struct usb_device *dev, int pipe)
982{
983 int result;
984 int endp = usb_pipeendpoint(pipe);
985
986 if (usb_pipein(pipe))
987 endp |= USB_DIR_IN;
988
989
990
991
992
993 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
994 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
995 USB_ENDPOINT_HALT, endp, NULL, 0,
996 USB_CTRL_SET_TIMEOUT);
997
998
999 if (result < 0)
1000 return result;
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 usb_reset_endpoint(dev, endp);
1011
1012 return 0;
1013}
1014EXPORT_SYMBOL_GPL(usb_clear_halt);
1015
1016static int create_intf_ep_devs(struct usb_interface *intf)
1017{
1018 struct usb_device *udev = interface_to_usbdev(intf);
1019 struct usb_host_interface *alt = intf->cur_altsetting;
1020 int i;
1021
1022 if (intf->ep_devs_created || intf->unregistering)
1023 return 0;
1024
1025 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1026 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1027 intf->ep_devs_created = 1;
1028 return 0;
1029}
1030
1031static void remove_intf_ep_devs(struct usb_interface *intf)
1032{
1033 struct usb_host_interface *alt = intf->cur_altsetting;
1034 int i;
1035
1036 if (!intf->ep_devs_created)
1037 return;
1038
1039 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1040 usb_remove_ep_devs(&alt->endpoint[i]);
1041 intf->ep_devs_created = 0;
1042}
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1057 bool reset_hardware)
1058{
1059 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1060 struct usb_host_endpoint *ep;
1061
1062 if (!dev)
1063 return;
1064
1065 if (usb_endpoint_out(epaddr)) {
1066 ep = dev->ep_out[epnum];
1067 if (reset_hardware)
1068 dev->ep_out[epnum] = NULL;
1069 } else {
1070 ep = dev->ep_in[epnum];
1071 if (reset_hardware)
1072 dev->ep_in[epnum] = NULL;
1073 }
1074 if (ep) {
1075 ep->enabled = 0;
1076 usb_hcd_flush_endpoint(dev, ep);
1077 if (reset_hardware)
1078 usb_hcd_disable_endpoint(dev, ep);
1079 }
1080}
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1092{
1093 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1094 struct usb_host_endpoint *ep;
1095
1096 if (usb_endpoint_out(epaddr))
1097 ep = dev->ep_out[epnum];
1098 else
1099 ep = dev->ep_in[epnum];
1100 if (ep)
1101 usb_hcd_reset_endpoint(dev, ep);
1102}
1103EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1116 bool reset_hardware)
1117{
1118 struct usb_host_interface *alt = intf->cur_altsetting;
1119 int i;
1120
1121 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1122 usb_disable_endpoint(dev,
1123 alt->endpoint[i].desc.bEndpointAddress,
1124 reset_hardware);
1125 }
1126}
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138void usb_disable_device(struct usb_device *dev, int skip_ep0)
1139{
1140 int i;
1141 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1142
1143
1144
1145
1146 if (dev->actconfig) {
1147
1148
1149
1150
1151
1152 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
1153 dev->actconfig->interface[i]->unregistering = 1;
1154
1155 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1156 struct usb_interface *interface;
1157
1158
1159 interface = dev->actconfig->interface[i];
1160 if (!device_is_registered(&interface->dev))
1161 continue;
1162 dev_dbg(&dev->dev, "unregistering interface %s\n",
1163 dev_name(&interface->dev));
1164 remove_intf_ep_devs(interface);
1165 device_del(&interface->dev);
1166 }
1167
1168
1169
1170
1171 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1172 put_device(&dev->actconfig->interface[i]->dev);
1173 dev->actconfig->interface[i] = NULL;
1174 }
1175 usb_unlocked_disable_lpm(dev);
1176 usb_disable_ltm(dev);
1177 dev->actconfig = NULL;
1178 if (dev->state == USB_STATE_CONFIGURED)
1179 usb_set_device_state(dev, USB_STATE_ADDRESS);
1180 }
1181
1182 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
1183 skip_ep0 ? "non-ep0" : "all");
1184 if (hcd->driver->check_bandwidth) {
1185
1186 for (i = skip_ep0; i < 16; ++i) {
1187 usb_disable_endpoint(dev, i, false);
1188 usb_disable_endpoint(dev, i + USB_DIR_IN, false);
1189 }
1190
1191 mutex_lock(hcd->bandwidth_mutex);
1192 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
1193 mutex_unlock(hcd->bandwidth_mutex);
1194
1195 }
1196 for (i = skip_ep0; i < 16; ++i) {
1197 usb_disable_endpoint(dev, i, true);
1198 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
1199 }
1200}
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
1212 bool reset_ep)
1213{
1214 int epnum = usb_endpoint_num(&ep->desc);
1215 int is_out = usb_endpoint_dir_out(&ep->desc);
1216 int is_control = usb_endpoint_xfer_control(&ep->desc);
1217
1218 if (reset_ep)
1219 usb_hcd_reset_endpoint(dev, ep);
1220 if (is_out || is_control)
1221 dev->ep_out[epnum] = ep;
1222 if (!is_out || is_control)
1223 dev->ep_in[epnum] = ep;
1224 ep->enabled = 1;
1225}
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235void usb_enable_interface(struct usb_device *dev,
1236 struct usb_interface *intf, bool reset_eps)
1237{
1238 struct usb_host_interface *alt = intf->cur_altsetting;
1239 int i;
1240
1241 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1242 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
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
1271
1272
1273
1274
1275
1276
1277
1278int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1279{
1280 struct usb_interface *iface;
1281 struct usb_host_interface *alt;
1282 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1283 int ret;
1284 int manual = 0;
1285 unsigned int epaddr;
1286 unsigned int pipe;
1287
1288 if (dev->state == USB_STATE_SUSPENDED)
1289 return -EHOSTUNREACH;
1290
1291 iface = usb_ifnum_to_if(dev, interface);
1292 if (!iface) {
1293 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1294 interface);
1295 return -EINVAL;
1296 }
1297 if (iface->unregistering)
1298 return -ENODEV;
1299
1300 alt = usb_altnum_to_altsetting(iface, alternate);
1301 if (!alt) {
1302 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
1303 alternate);
1304 return -EINVAL;
1305 }
1306
1307
1308
1309
1310 mutex_lock(hcd->bandwidth_mutex);
1311
1312
1313
1314 if (usb_disable_lpm(dev)) {
1315 dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
1316 mutex_unlock(hcd->bandwidth_mutex);
1317 return -ENOMEM;
1318 }
1319 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1320 if (ret < 0) {
1321 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1322 alternate);
1323 usb_enable_lpm(dev);
1324 mutex_unlock(hcd->bandwidth_mutex);
1325 return ret;
1326 }
1327
1328 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1329 ret = -EPIPE;
1330 else
1331 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1332 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1333 alternate, interface, NULL, 0, 5000);
1334
1335
1336
1337
1338 if (ret == -EPIPE && iface->num_altsetting == 1) {
1339 dev_dbg(&dev->dev,
1340 "manual set_interface for iface %d, alt %d\n",
1341 interface, alternate);
1342 manual = 1;
1343 } else if (ret < 0) {
1344
1345 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
1346 usb_enable_lpm(dev);
1347 mutex_unlock(hcd->bandwidth_mutex);
1348 return ret;
1349 }
1350 mutex_unlock(hcd->bandwidth_mutex);
1351
1352
1353
1354
1355
1356
1357
1358
1359 if (iface->cur_altsetting != alt) {
1360 remove_intf_ep_devs(iface);
1361 usb_remove_sysfs_intf_files(iface);
1362 }
1363 usb_disable_interface(dev, iface, true);
1364
1365 iface->cur_altsetting = alt;
1366
1367
1368 usb_unlocked_enable_lpm(dev);
1369
1370
1371
1372
1373
1374
1375 if (manual) {
1376 int i;
1377
1378 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1379 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1380 pipe = __create_pipe(dev,
1381 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1382 (usb_endpoint_out(epaddr) ?
1383 USB_DIR_OUT : USB_DIR_IN);
1384
1385 usb_clear_halt(dev, pipe);
1386 }
1387 }
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400 usb_enable_interface(dev, iface, true);
1401 if (device_is_registered(&iface->dev)) {
1402 usb_create_sysfs_intf_files(iface);
1403 create_intf_ep_devs(iface);
1404 }
1405 return 0;
1406}
1407EXPORT_SYMBOL_GPL(usb_set_interface);
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431int usb_reset_configuration(struct usb_device *dev)
1432{
1433 int i, retval;
1434 struct usb_host_config *config;
1435 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1436
1437 if (dev->state == USB_STATE_SUSPENDED)
1438 return -EHOSTUNREACH;
1439
1440
1441
1442
1443
1444
1445 for (i = 1; i < 16; ++i) {
1446 usb_disable_endpoint(dev, i, true);
1447 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
1448 }
1449
1450 config = dev->actconfig;
1451 retval = 0;
1452 mutex_lock(hcd->bandwidth_mutex);
1453
1454
1455
1456 if (usb_disable_lpm(dev)) {
1457 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1458 mutex_unlock(hcd->bandwidth_mutex);
1459 return -ENOMEM;
1460 }
1461
1462 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1463 struct usb_interface *intf = config->interface[i];
1464 struct usb_host_interface *alt;
1465
1466 alt = usb_altnum_to_altsetting(intf, 0);
1467 if (!alt)
1468 alt = &intf->altsetting[0];
1469 if (alt != intf->cur_altsetting)
1470 retval = usb_hcd_alloc_bandwidth(dev, NULL,
1471 intf->cur_altsetting, alt);
1472 if (retval < 0)
1473 break;
1474 }
1475
1476 if (retval < 0) {
1477reset_old_alts:
1478 for (i--; i >= 0; i--) {
1479 struct usb_interface *intf = config->interface[i];
1480 struct usb_host_interface *alt;
1481
1482 alt = usb_altnum_to_altsetting(intf, 0);
1483 if (!alt)
1484 alt = &intf->altsetting[0];
1485 if (alt != intf->cur_altsetting)
1486 usb_hcd_alloc_bandwidth(dev, NULL,
1487 alt, intf->cur_altsetting);
1488 }
1489 usb_enable_lpm(dev);
1490 mutex_unlock(hcd->bandwidth_mutex);
1491 return retval;
1492 }
1493 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1494 USB_REQ_SET_CONFIGURATION, 0,
1495 config->desc.bConfigurationValue, 0,
1496 NULL, 0, USB_CTRL_SET_TIMEOUT);
1497 if (retval < 0)
1498 goto reset_old_alts;
1499 mutex_unlock(hcd->bandwidth_mutex);
1500
1501
1502 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1503 struct usb_interface *intf = config->interface[i];
1504 struct usb_host_interface *alt;
1505
1506 alt = usb_altnum_to_altsetting(intf, 0);
1507
1508
1509
1510
1511
1512
1513 if (!alt)
1514 alt = &intf->altsetting[0];
1515
1516 if (alt != intf->cur_altsetting) {
1517 remove_intf_ep_devs(intf);
1518 usb_remove_sysfs_intf_files(intf);
1519 }
1520 intf->cur_altsetting = alt;
1521 usb_enable_interface(dev, intf, true);
1522 if (device_is_registered(&intf->dev)) {
1523 usb_create_sysfs_intf_files(intf);
1524 create_intf_ep_devs(intf);
1525 }
1526 }
1527
1528 usb_unlocked_enable_lpm(dev);
1529 return 0;
1530}
1531EXPORT_SYMBOL_GPL(usb_reset_configuration);
1532
1533static void usb_release_interface(struct device *dev)
1534{
1535 struct usb_interface *intf = to_usb_interface(dev);
1536 struct usb_interface_cache *intfc =
1537 altsetting_to_usb_interface_cache(intf->altsetting);
1538
1539 kref_put(&intfc->ref, usb_release_interface_cache);
1540 kfree(intf);
1541}
1542
1543static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1544{
1545 struct usb_device *usb_dev;
1546 struct usb_interface *intf;
1547 struct usb_host_interface *alt;
1548
1549 intf = to_usb_interface(dev);
1550 usb_dev = interface_to_usbdev(intf);
1551 alt = intf->cur_altsetting;
1552
1553 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
1554 alt->desc.bInterfaceClass,
1555 alt->desc.bInterfaceSubClass,
1556 alt->desc.bInterfaceProtocol))
1557 return -ENOMEM;
1558
1559 if (add_uevent_var(env,
1560 "MODALIAS=usb:"
1561 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
1562 le16_to_cpu(usb_dev->descriptor.idVendor),
1563 le16_to_cpu(usb_dev->descriptor.idProduct),
1564 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1565 usb_dev->descriptor.bDeviceClass,
1566 usb_dev->descriptor.bDeviceSubClass,
1567 usb_dev->descriptor.bDeviceProtocol,
1568 alt->desc.bInterfaceClass,
1569 alt->desc.bInterfaceSubClass,
1570 alt->desc.bInterfaceProtocol,
1571 alt->desc.bInterfaceNumber))
1572 return -ENOMEM;
1573
1574 return 0;
1575}
1576
1577struct device_type usb_if_device_type = {
1578 .name = "usb_interface",
1579 .release = usb_release_interface,
1580 .uevent = usb_if_uevent,
1581};
1582
1583static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1584 struct usb_host_config *config,
1585 u8 inum)
1586{
1587 struct usb_interface_assoc_descriptor *retval = NULL;
1588 struct usb_interface_assoc_descriptor *intf_assoc;
1589 int first_intf;
1590 int last_intf;
1591 int i;
1592
1593 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1594 intf_assoc = config->intf_assoc[i];
1595 if (intf_assoc->bInterfaceCount == 0)
1596 continue;
1597
1598 first_intf = intf_assoc->bFirstInterface;
1599 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1600 if (inum >= first_intf && inum <= last_intf) {
1601 if (!retval)
1602 retval = intf_assoc;
1603 else
1604 dev_err(&dev->dev, "Interface #%d referenced"
1605 " by multiple IADs\n", inum);
1606 }
1607 }
1608
1609 return retval;
1610}
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635static void __usb_queue_reset_device(struct work_struct *ws)
1636{
1637 int rc;
1638 struct usb_interface *iface =
1639 container_of(ws, struct usb_interface, reset_ws);
1640 struct usb_device *udev = interface_to_usbdev(iface);
1641
1642 rc = usb_lock_device_for_reset(udev, iface);
1643 if (rc >= 0) {
1644 iface->reset_running = 1;
1645 usb_reset_device(udev);
1646 iface->reset_running = 0;
1647 usb_unlock_device(udev);
1648 }
1649}
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697int usb_set_configuration(struct usb_device *dev, int configuration)
1698{
1699 int i, ret;
1700 struct usb_host_config *cp = NULL;
1701 struct usb_interface **new_interfaces = NULL;
1702 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1703 int n, nintf;
1704
1705 if (dev->authorized == 0 || configuration == -1)
1706 configuration = 0;
1707 else {
1708 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1709 if (dev->config[i].desc.bConfigurationValue ==
1710 configuration) {
1711 cp = &dev->config[i];
1712 break;
1713 }
1714 }
1715 }
1716 if ((!cp && configuration != 0))
1717 return -EINVAL;
1718
1719
1720
1721
1722
1723
1724 if (cp && configuration == 0)
1725 dev_warn(&dev->dev, "config 0 descriptor??\n");
1726
1727
1728
1729 n = nintf = 0;
1730 if (cp) {
1731 nintf = cp->desc.bNumInterfaces;
1732 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1733 GFP_NOIO);
1734 if (!new_interfaces) {
1735 dev_err(&dev->dev, "Out of memory\n");
1736 return -ENOMEM;
1737 }
1738
1739 for (; n < nintf; ++n) {
1740 new_interfaces[n] = kzalloc(
1741 sizeof(struct usb_interface),
1742 GFP_NOIO);
1743 if (!new_interfaces[n]) {
1744 dev_err(&dev->dev, "Out of memory\n");
1745 ret = -ENOMEM;
1746free_interfaces:
1747 while (--n >= 0)
1748 kfree(new_interfaces[n]);
1749 kfree(new_interfaces);
1750 return ret;
1751 }
1752 }
1753
1754 i = dev->bus_mA - usb_get_max_power(dev, cp);
1755 if (i < 0)
1756 dev_warn(&dev->dev, "new config #%d exceeds power "
1757 "limit by %dmA\n",
1758 configuration, -i);
1759 }
1760
1761
1762 ret = usb_autoresume_device(dev);
1763 if (ret)
1764 goto free_interfaces;
1765
1766
1767
1768
1769 if (dev->state != USB_STATE_ADDRESS)
1770 usb_disable_device(dev, 1);
1771
1772
1773 cancel_async_set_config(dev);
1774
1775
1776
1777
1778
1779
1780
1781 mutex_lock(hcd->bandwidth_mutex);
1782
1783
1784
1785
1786 if (dev->actconfig && usb_disable_lpm(dev)) {
1787 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1788 mutex_unlock(hcd->bandwidth_mutex);
1789 ret = -ENOMEM;
1790 goto free_interfaces;
1791 }
1792 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
1793 if (ret < 0) {
1794 if (dev->actconfig)
1795 usb_enable_lpm(dev);
1796 mutex_unlock(hcd->bandwidth_mutex);
1797 usb_autosuspend_device(dev);
1798 goto free_interfaces;
1799 }
1800
1801
1802
1803
1804
1805 for (i = 0; i < nintf; ++i) {
1806 struct usb_interface_cache *intfc;
1807 struct usb_interface *intf;
1808 struct usb_host_interface *alt;
1809
1810 cp->interface[i] = intf = new_interfaces[i];
1811 intfc = cp->intf_cache[i];
1812 intf->altsetting = intfc->altsetting;
1813 intf->num_altsetting = intfc->num_altsetting;
1814 kref_get(&intfc->ref);
1815
1816 alt = usb_altnum_to_altsetting(intf, 0);
1817
1818
1819
1820
1821
1822
1823 if (!alt)
1824 alt = &intf->altsetting[0];
1825
1826 intf->intf_assoc =
1827 find_iad(dev, cp, alt->desc.bInterfaceNumber);
1828 intf->cur_altsetting = alt;
1829 usb_enable_interface(dev, intf, true);
1830 intf->dev.parent = &dev->dev;
1831 intf->dev.driver = NULL;
1832 intf->dev.bus = &usb_bus_type;
1833 intf->dev.type = &usb_if_device_type;
1834 intf->dev.groups = usb_interface_groups;
1835 intf->dev.dma_mask = dev->dev.dma_mask;
1836 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
1837 intf->minor = -1;
1838 device_initialize(&intf->dev);
1839 pm_runtime_no_callbacks(&intf->dev);
1840 dev_set_name(&intf->dev, "%d-%s:%d.%d",
1841 dev->bus->busnum, dev->devpath,
1842 configuration, alt->desc.bInterfaceNumber);
1843 }
1844 kfree(new_interfaces);
1845
1846 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1847 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1848 NULL, 0, USB_CTRL_SET_TIMEOUT);
1849 if (ret < 0 && cp) {
1850
1851
1852
1853
1854 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
1855 for (i = 0; i < nintf; ++i) {
1856 usb_disable_interface(dev, cp->interface[i], true);
1857 put_device(&cp->interface[i]->dev);
1858 cp->interface[i] = NULL;
1859 }
1860 cp = NULL;
1861 }
1862
1863 dev->actconfig = cp;
1864 mutex_unlock(hcd->bandwidth_mutex);
1865
1866 if (!cp) {
1867 usb_set_device_state(dev, USB_STATE_ADDRESS);
1868
1869
1870 usb_autosuspend_device(dev);
1871 return ret;
1872 }
1873 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1874
1875 if (cp->string == NULL &&
1876 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1877 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1878
1879
1880 usb_unlocked_enable_lpm(dev);
1881
1882 usb_enable_ltm(dev);
1883
1884
1885
1886
1887
1888
1889
1890 for (i = 0; i < nintf; ++i) {
1891 struct usb_interface *intf = cp->interface[i];
1892
1893 dev_dbg(&dev->dev,
1894 "adding %s (config #%d, interface %d)\n",
1895 dev_name(&intf->dev), configuration,
1896 intf->cur_altsetting->desc.bInterfaceNumber);
1897 device_enable_async_suspend(&intf->dev);
1898 ret = device_add(&intf->dev);
1899 if (ret != 0) {
1900 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1901 dev_name(&intf->dev), ret);
1902 continue;
1903 }
1904 create_intf_ep_devs(intf);
1905 }
1906
1907 usb_autosuspend_device(dev);
1908 return 0;
1909}
1910
1911static LIST_HEAD(set_config_list);
1912static DEFINE_SPINLOCK(set_config_lock);
1913
1914struct set_config_request {
1915 struct usb_device *udev;
1916 int config;
1917 struct work_struct work;
1918 struct list_head node;
1919};
1920
1921
1922static void driver_set_config_work(struct work_struct *work)
1923{
1924 struct set_config_request *req =
1925 container_of(work, struct set_config_request, work);
1926 struct usb_device *udev = req->udev;
1927
1928 usb_lock_device(udev);
1929 spin_lock(&set_config_lock);
1930 list_del(&req->node);
1931 spin_unlock(&set_config_lock);
1932
1933 if (req->config >= -1)
1934 usb_set_configuration(udev, req->config);
1935 usb_unlock_device(udev);
1936 usb_put_dev(udev);
1937 kfree(req);
1938}
1939
1940
1941
1942
1943static void cancel_async_set_config(struct usb_device *udev)
1944{
1945 struct set_config_request *req;
1946
1947 spin_lock(&set_config_lock);
1948 list_for_each_entry(req, &set_config_list, node) {
1949 if (req->udev == udev)
1950 req->config = -999;
1951 }
1952 spin_unlock(&set_config_lock);
1953}
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975int usb_driver_set_configuration(struct usb_device *udev, int config)
1976{
1977 struct set_config_request *req;
1978
1979 req = kmalloc(sizeof(*req), GFP_KERNEL);
1980 if (!req)
1981 return -ENOMEM;
1982 req->udev = udev;
1983 req->config = config;
1984 INIT_WORK(&req->work, driver_set_config_work);
1985
1986 spin_lock(&set_config_lock);
1987 list_add(&req->node, &set_config_list);
1988 spin_unlock(&set_config_lock);
1989
1990 usb_get_dev(udev);
1991 schedule_work(&req->work);
1992 return 0;
1993}
1994EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
1995