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