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/device.h>
14#include <linux/scatterlist.h>
15#include <linux/usb/quirks.h>
16#include <asm/byteorder.h>
17
18#include "hcd.h"
19#include "usb.h"
20
21struct api_context {
22 struct completion done;
23 int status;
24};
25
26static void usb_api_blocking_completion(struct urb *urb)
27{
28 struct api_context *ctx = urb->context;
29
30 ctx->status = urb->status;
31 complete(&ctx->done);
32}
33
34
35
36
37
38
39
40
41static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
42{
43 struct api_context ctx;
44 unsigned long expire;
45 int retval;
46
47 init_completion(&ctx.done);
48 urb->context = &ctx;
49 urb->actual_length = 0;
50 retval = usb_submit_urb(urb, GFP_NOIO);
51 if (unlikely(retval))
52 goto out;
53
54 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
55 if (!wait_for_completion_timeout(&ctx.done, expire)) {
56 usb_kill_urb(urb);
57 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
58
59 dev_dbg(&urb->dev->dev,
60 "%s timed out on ep%d%s len=%d/%d\n",
61 current->comm,
62 usb_endpoint_num(&urb->ep->desc),
63 usb_urb_dir_in(urb) ? "in" : "out",
64 urb->actual_length,
65 urb->transfer_buffer_length);
66 } else
67 retval = ctx.status;
68out:
69 if (actual_length)
70 *actual_length = urb->actual_length;
71
72 usb_free_urb(urb);
73 return retval;
74}
75
76
77
78static int usb_internal_control_msg(struct usb_device *usb_dev,
79 unsigned int pipe,
80 struct usb_ctrlrequest *cmd,
81 void *data, int len, int timeout)
82{
83 struct urb *urb;
84 int retv;
85 int length;
86
87 urb = usb_alloc_urb(0, GFP_NOIO);
88 if (!urb)
89 return -ENOMEM;
90
91 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
92 len, usb_api_blocking_completion, NULL);
93
94 retv = usb_start_wait_urb(urb, timeout, &length);
95 if (retv < 0)
96 return retv;
97 else
98 return length;
99}
100
101
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
129int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
130 __u8 requesttype, __u16 value, __u16 index, void *data,
131 __u16 size, int timeout)
132{
133 struct usb_ctrlrequest *dr;
134 int ret;
135
136 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
137 if (!dr)
138 return -ENOMEM;
139
140 dr->bRequestType = requesttype;
141 dr->bRequest = request;
142 dr->wValue = cpu_to_le16p(&value);
143 dr->wIndex = cpu_to_le16p(&index);
144 dr->wLength = cpu_to_le16p(&size);
145
146
147
148 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
149
150 kfree(dr);
151
152 return ret;
153}
154EXPORT_SYMBOL_GPL(usb_control_msg);
155
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
182int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
183 void *data, int len, int *actual_length, int timeout)
184{
185 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
186}
187EXPORT_SYMBOL_GPL(usb_interrupt_msg);
188
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
220int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
221 void *data, int len, int *actual_length, int timeout)
222{
223 struct urb *urb;
224 struct usb_host_endpoint *ep;
225
226 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
227 [usb_pipeendpoint(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 if (io->dev->dev.dma_mask != NULL)
260 usb_buffer_unmap_sg(io->dev, usb_pipein(io->pipe),
261 io->sg, io->nents);
262 io->dev = NULL;
263}
264
265static void sg_complete(struct urb *urb)
266{
267 struct usb_sg_request *io = urb->context;
268 int status = urb->status;
269
270 spin_lock(&io->lock);
271
272
273
274
275
276
277
278
279
280
281
282 if (io->status
283 && (io->status != -ECONNRESET
284 || status != -ECONNRESET)
285 && urb->actual_length) {
286 dev_err(io->dev->bus->controller,
287 "dev %s ep%d%s scatterlist error %d/%d\n",
288 io->dev->devpath,
289 usb_endpoint_num(&urb->ep->desc),
290 usb_urb_dir_in(urb) ? "in" : "out",
291 status, io->status);
292
293 }
294
295 if (io->status == 0 && status && status != -ECONNRESET) {
296 int i, found, retval;
297
298 io->status = status;
299
300
301
302
303
304 spin_unlock(&io->lock);
305 for (i = 0, found = 0; i < io->entries; i++) {
306 if (!io->urbs [i] || !io->urbs [i]->dev)
307 continue;
308 if (found) {
309 retval = usb_unlink_urb(io->urbs [i]);
310 if (retval != -EINPROGRESS &&
311 retval != -ENODEV &&
312 retval != -EBUSY)
313 dev_err(&io->dev->dev,
314 "%s, unlink --> %d\n",
315 __func__, retval);
316 } else if (urb == io->urbs [i])
317 found = 1;
318 }
319 spin_lock(&io->lock);
320 }
321 urb->dev = NULL;
322
323
324 io->bytes += urb->actual_length;
325 io->count--;
326 if (!io->count)
327 complete(&io->complete);
328
329 spin_unlock(&io->lock);
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
355
356
357
358int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
359 unsigned pipe, unsigned period, struct scatterlist *sg,
360 int nents, size_t length, gfp_t mem_flags)
361{
362 int i;
363 int urb_flags;
364 int dma;
365
366 if (!io || !dev || !sg
367 || usb_pipecontrol(pipe)
368 || usb_pipeisoc(pipe)
369 || nents <= 0)
370 return -EINVAL;
371
372 spin_lock_init(&io->lock);
373 io->dev = dev;
374 io->pipe = pipe;
375 io->sg = sg;
376 io->nents = nents;
377
378
379
380
381 dma = (dev->dev.dma_mask != NULL);
382 if (dma)
383 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),
384 sg, nents);
385 else
386 io->entries = nents;
387
388
389 if (io->entries <= 0)
390 return io->entries;
391
392 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
393 if (!io->urbs)
394 goto nomem;
395
396 urb_flags = URB_NO_INTERRUPT;
397 if (dma)
398 urb_flags |= URB_NO_TRANSFER_DMA_MAP;
399 if (usb_pipein(pipe))
400 urb_flags |= URB_SHORT_NOT_OK;
401
402 for_each_sg(sg, sg, io->entries, i) {
403 unsigned len;
404
405 io->urbs[i] = usb_alloc_urb(0, mem_flags);
406 if (!io->urbs[i]) {
407 io->entries = i;
408 goto nomem;
409 }
410
411 io->urbs[i]->dev = NULL;
412 io->urbs[i]->pipe = pipe;
413 io->urbs[i]->interval = period;
414 io->urbs[i]->transfer_flags = urb_flags;
415
416 io->urbs[i]->complete = sg_complete;
417 io->urbs[i]->context = io;
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435 if (dma) {
436 io->urbs[i]->transfer_dma = sg_dma_address(sg);
437 len = sg_dma_len(sg);
438#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
439 io->urbs[i]->transfer_buffer = NULL;
440#else
441 io->urbs[i]->transfer_buffer = sg_virt(sg);
442#endif
443 } else {
444
445 io->urbs[i]->transfer_buffer = sg_virt(sg);
446 len = sg->length;
447 }
448
449 if (length) {
450 len = min_t(unsigned, len, length);
451 length -= len;
452 if (length == 0)
453 io->entries = i + 1;
454 }
455 io->urbs[i]->transfer_buffer_length = len;
456 }
457 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
458
459
460 io->count = io->entries;
461 io->status = 0;
462 io->bytes = 0;
463 init_completion(&io->complete);
464 return 0;
465
466nomem:
467 sg_clean(io);
468 return -ENOMEM;
469}
470EXPORT_SYMBOL_GPL(usb_sg_init);
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
502
503
504
505
506
507
508
509
510
511void usb_sg_wait(struct usb_sg_request *io)
512{
513 int i;
514 int entries = io->entries;
515
516
517 spin_lock_irq(&io->lock);
518 i = 0;
519 while (i < entries && !io->status) {
520 int retval;
521
522 io->urbs[i]->dev = io->dev;
523 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
524
525
526
527
528 spin_unlock_irq(&io->lock);
529 switch (retval) {
530
531 case -ENXIO:
532 case -EAGAIN:
533 case -ENOMEM:
534 io->urbs[i]->dev = NULL;
535 retval = 0;
536 yield();
537 break;
538
539
540
541
542
543
544
545 case 0:
546 ++i;
547 cpu_relax();
548 break;
549
550
551 default:
552 io->urbs[i]->dev = NULL;
553 io->urbs[i]->status = retval;
554 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
555 __func__, retval);
556 usb_sg_cancel(io);
557 }
558 spin_lock_irq(&io->lock);
559 if (retval && (io->status == 0 || io->status == -ECONNRESET))
560 io->status = retval;
561 }
562 io->count -= entries - i;
563 if (io->count == 0)
564 complete(&io->complete);
565 spin_unlock_irq(&io->lock);
566
567
568
569
570
571 wait_for_completion(&io->complete);
572
573 sg_clean(io);
574}
575EXPORT_SYMBOL_GPL(usb_sg_wait);
576
577
578
579
580
581
582
583
584
585void usb_sg_cancel(struct usb_sg_request *io)
586{
587 unsigned long flags;
588
589 spin_lock_irqsave(&io->lock, flags);
590
591
592 if (!io->status) {
593 int i;
594
595 io->status = -ECONNRESET;
596 spin_unlock(&io->lock);
597 for (i = 0; i < io->entries; i++) {
598 int retval;
599
600 if (!io->urbs [i]->dev)
601 continue;
602 retval = usb_unlink_urb(io->urbs [i]);
603 if (retval != -EINPROGRESS && retval != -EBUSY)
604 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
605 __func__, retval);
606 }
607 spin_lock(&io->lock);
608 }
609 spin_unlock_irqrestore(&io->lock, flags);
610}
611EXPORT_SYMBOL_GPL(usb_sg_cancel);
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637int usb_get_descriptor(struct usb_device *dev, unsigned char type,
638 unsigned char index, void *buf, int size)
639{
640 int i;
641 int result;
642
643 memset(buf, 0, size);
644
645 for (i = 0; i < 3; ++i) {
646
647 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
648 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
649 (type << 8) + index, 0, buf, size,
650 USB_CTRL_GET_TIMEOUT);
651 if (result <= 0 && result != -ETIMEDOUT)
652 continue;
653 if (result > 1 && ((u8 *)buf)[1] != type) {
654 result = -EPROTO;
655 continue;
656 }
657 break;
658 }
659 return result;
660}
661EXPORT_SYMBOL_GPL(usb_get_descriptor);
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685static int usb_get_string(struct usb_device *dev, unsigned short langid,
686 unsigned char index, void *buf, int size)
687{
688 int i;
689 int result;
690
691 for (i = 0; i < 3; ++i) {
692
693 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
694 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
695 (USB_DT_STRING << 8) + index, langid, buf, size,
696 USB_CTRL_GET_TIMEOUT);
697 if (!(result == 0 || result == -EPIPE))
698 break;
699 }
700 return result;
701}
702
703static void usb_try_string_workarounds(unsigned char *buf, int *length)
704{
705 int newlength, oldlength = *length;
706
707 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
708 if (!isprint(buf[newlength]) || buf[newlength + 1])
709 break;
710
711 if (newlength > 2) {
712 buf[0] = newlength;
713 *length = newlength;
714 }
715}
716
717static int usb_string_sub(struct usb_device *dev, unsigned int langid,
718 unsigned int index, unsigned char *buf)
719{
720 int rc;
721
722
723
724 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
725 rc = -EIO;
726 else
727 rc = usb_get_string(dev, langid, index, buf, 255);
728
729
730
731 if (rc < 2) {
732 rc = usb_get_string(dev, langid, index, buf, 2);
733 if (rc == 2)
734 rc = usb_get_string(dev, langid, index, buf, buf[0]);
735 }
736
737 if (rc >= 2) {
738 if (!buf[0] && !buf[1])
739 usb_try_string_workarounds(buf, &rc);
740
741
742 if (buf[0] < rc)
743 rc = buf[0];
744
745 rc = rc - (rc & 1);
746 }
747
748 if (rc < 2)
749 rc = (rc < 0 ? rc : -EINVAL);
750
751 return rc;
752}
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
779{
780 unsigned char *tbuf;
781 int err;
782 unsigned int u, idx;
783
784 if (dev->state == USB_STATE_SUSPENDED)
785 return -EHOSTUNREACH;
786 if (size <= 0 || !buf || !index)
787 return -EINVAL;
788 buf[0] = 0;
789 tbuf = kmalloc(256, GFP_NOIO);
790 if (!tbuf)
791 return -ENOMEM;
792
793
794 if (!dev->have_langid) {
795 err = usb_string_sub(dev, 0, 0, tbuf);
796 if (err < 0) {
797 dev_err(&dev->dev,
798 "string descriptor 0 read error: %d\n",
799 err);
800 goto errout;
801 } else if (err < 4) {
802 dev_err(&dev->dev, "string descriptor 0 too short\n");
803 err = -EINVAL;
804 goto errout;
805 } else {
806 dev->have_langid = 1;
807 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
808
809 dev_dbg(&dev->dev, "default language 0x%04x\n",
810 dev->string_langid);
811 }
812 }
813
814 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
815 if (err < 0)
816 goto errout;
817
818 size--;
819 for (idx = 0, u = 2; u < err; u += 2) {
820 if (idx >= size)
821 break;
822 if (tbuf[u+1])
823 buf[idx++] = '?';
824 else
825 buf[idx++] = tbuf[u];
826 }
827 buf[idx] = 0;
828 err = idx;
829
830 if (tbuf[1] != USB_DT_STRING)
831 dev_dbg(&dev->dev,
832 "wrong descriptor type %02x for string %d (\"%s\")\n",
833 tbuf[1], index, buf);
834
835 errout:
836 kfree(tbuf);
837 return err;
838}
839EXPORT_SYMBOL_GPL(usb_string);
840
841
842
843
844
845
846
847
848
849char *usb_cache_string(struct usb_device *udev, int index)
850{
851 char *buf;
852 char *smallbuf = NULL;
853 int len;
854
855 if (index <= 0)
856 return NULL;
857
858 buf = kmalloc(256, GFP_KERNEL);
859 if (buf) {
860 len = usb_string(udev, index, buf, 256);
861 if (len > 0) {
862 smallbuf = kmalloc(++len, GFP_KERNEL);
863 if (!smallbuf)
864 return buf;
865 memcpy(smallbuf, buf, len);
866 }
867 kfree(buf);
868 }
869 return smallbuf;
870}
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
891{
892 struct usb_device_descriptor *desc;
893 int ret;
894
895 if (size > sizeof(*desc))
896 return -EINVAL;
897 desc = kmalloc(sizeof(*desc), GFP_NOIO);
898 if (!desc)
899 return -ENOMEM;
900
901 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
902 if (ret >= 0)
903 memcpy(&dev->descriptor, desc, size);
904 kfree(desc);
905 return ret;
906}
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930int usb_get_status(struct usb_device *dev, int type, int target, void *data)
931{
932 int ret;
933 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
934
935 if (!status)
936 return -ENOMEM;
937
938 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
939 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
940 sizeof(*status), USB_CTRL_GET_TIMEOUT);
941
942 *(u16 *)data = *status;
943 kfree(status);
944 return ret;
945}
946EXPORT_SYMBOL_GPL(usb_get_status);
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971int usb_clear_halt(struct usb_device *dev, int pipe)
972{
973 int result;
974 int endp = usb_pipeendpoint(pipe);
975
976 if (usb_pipein(pipe))
977 endp |= USB_DIR_IN;
978
979
980
981
982
983 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
984 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
985 USB_ENDPOINT_HALT, endp, NULL, 0,
986 USB_CTRL_SET_TIMEOUT);
987
988
989 if (result < 0)
990 return result;
991
992
993
994
995
996
997
998
999
1000
1001 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
1002
1003 return 0;
1004}
1005EXPORT_SYMBOL_GPL(usb_clear_halt);
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
1020{
1021 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1022 struct usb_host_endpoint *ep;
1023
1024 if (!dev)
1025 return;
1026
1027 if (usb_endpoint_out(epaddr)) {
1028 ep = dev->ep_out[epnum];
1029 dev->ep_out[epnum] = NULL;
1030 } else {
1031 ep = dev->ep_in[epnum];
1032 dev->ep_in[epnum] = NULL;
1033 }
1034 if (ep) {
1035 ep->enabled = 0;
1036 usb_hcd_flush_endpoint(dev, ep);
1037 usb_hcd_disable_endpoint(dev, ep);
1038 }
1039}
1040
1041
1042
1043
1044
1045
1046
1047
1048void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1049{
1050 struct usb_host_interface *alt = intf->cur_altsetting;
1051 int i;
1052
1053 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1054 usb_disable_endpoint(dev,
1055 alt->endpoint[i].desc.bEndpointAddress);
1056 }
1057}
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069void usb_disable_device(struct usb_device *dev, int skip_ep0)
1070{
1071 int i;
1072
1073 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
1074 skip_ep0 ? "non-ep0" : "all");
1075 for (i = skip_ep0; i < 16; ++i) {
1076 usb_disable_endpoint(dev, i);
1077 usb_disable_endpoint(dev, i + USB_DIR_IN);
1078 }
1079 dev->toggle[0] = dev->toggle[1] = 0;
1080
1081
1082
1083
1084 if (dev->actconfig) {
1085 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1086 struct usb_interface *interface;
1087
1088
1089 interface = dev->actconfig->interface[i];
1090 if (!device_is_registered(&interface->dev))
1091 continue;
1092 dev_dbg(&dev->dev, "unregistering interface %s\n",
1093 dev_name(&interface->dev));
1094 interface->unregistering = 1;
1095 usb_remove_sysfs_intf_files(interface);
1096 device_del(&interface->dev);
1097 }
1098
1099
1100
1101
1102 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1103 put_device(&dev->actconfig->interface[i]->dev);
1104 dev->actconfig->interface[i] = NULL;
1105 }
1106 dev->actconfig = NULL;
1107 if (dev->state == USB_STATE_CONFIGURED)
1108 usb_set_device_state(dev, USB_STATE_ADDRESS);
1109 }
1110}
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
1121{
1122 int epnum = usb_endpoint_num(&ep->desc);
1123 int is_out = usb_endpoint_dir_out(&ep->desc);
1124 int is_control = usb_endpoint_xfer_control(&ep->desc);
1125
1126 if (is_out || is_control) {
1127 usb_settoggle(dev, epnum, 1, 0);
1128 dev->ep_out[epnum] = ep;
1129 }
1130 if (!is_out || is_control) {
1131 usb_settoggle(dev, epnum, 0, 0);
1132 dev->ep_in[epnum] = ep;
1133 }
1134 ep->enabled = 1;
1135}
1136
1137
1138
1139
1140
1141
1142
1143
1144static void usb_enable_interface(struct usb_device *dev,
1145 struct usb_interface *intf)
1146{
1147 struct usb_host_interface *alt = intf->cur_altsetting;
1148 int i;
1149
1150 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1151 usb_enable_endpoint(dev, &alt->endpoint[i]);
1152}
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1188{
1189 struct usb_interface *iface;
1190 struct usb_host_interface *alt;
1191 int ret;
1192 int manual = 0;
1193 unsigned int epaddr;
1194 unsigned int pipe;
1195
1196 if (dev->state == USB_STATE_SUSPENDED)
1197 return -EHOSTUNREACH;
1198
1199 iface = usb_ifnum_to_if(dev, interface);
1200 if (!iface) {
1201 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1202 interface);
1203 return -EINVAL;
1204 }
1205
1206 alt = usb_altnum_to_altsetting(iface, alternate);
1207 if (!alt) {
1208 dev_warn(&dev->dev, "selecting invalid altsetting %d",
1209 alternate);
1210 return -EINVAL;
1211 }
1212
1213 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1214 ret = -EPIPE;
1215 else
1216 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1217 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1218 alternate, interface, NULL, 0, 5000);
1219
1220
1221
1222
1223 if (ret == -EPIPE && iface->num_altsetting == 1) {
1224 dev_dbg(&dev->dev,
1225 "manual set_interface for iface %d, alt %d\n",
1226 interface, alternate);
1227 manual = 1;
1228 } else if (ret < 0)
1229 return ret;
1230
1231
1232
1233
1234
1235
1236
1237
1238 if (iface->cur_altsetting != alt)
1239 usb_remove_sysfs_intf_files(iface);
1240 usb_disable_interface(dev, iface);
1241
1242 iface->cur_altsetting = alt;
1243
1244
1245
1246
1247
1248
1249 if (manual) {
1250 int i;
1251
1252 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1253 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1254 pipe = __create_pipe(dev,
1255 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1256 (usb_endpoint_out(epaddr) ?
1257 USB_DIR_OUT : USB_DIR_IN);
1258
1259 usb_clear_halt(dev, pipe);
1260 }
1261 }
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274 usb_enable_interface(dev, iface);
1275 if (device_is_registered(&iface->dev))
1276 usb_create_sysfs_intf_files(iface);
1277
1278 return 0;
1279}
1280EXPORT_SYMBOL_GPL(usb_set_interface);
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304int usb_reset_configuration(struct usb_device *dev)
1305{
1306 int i, retval;
1307 struct usb_host_config *config;
1308
1309 if (dev->state == USB_STATE_SUSPENDED)
1310 return -EHOSTUNREACH;
1311
1312
1313
1314
1315
1316
1317 for (i = 1; i < 16; ++i) {
1318 usb_disable_endpoint(dev, i);
1319 usb_disable_endpoint(dev, i + USB_DIR_IN);
1320 }
1321
1322 config = dev->actconfig;
1323 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1324 USB_REQ_SET_CONFIGURATION, 0,
1325 config->desc.bConfigurationValue, 0,
1326 NULL, 0, USB_CTRL_SET_TIMEOUT);
1327 if (retval < 0)
1328 return retval;
1329
1330 dev->toggle[0] = dev->toggle[1] = 0;
1331
1332
1333 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1334 struct usb_interface *intf = config->interface[i];
1335 struct usb_host_interface *alt;
1336
1337 usb_remove_sysfs_intf_files(intf);
1338 alt = usb_altnum_to_altsetting(intf, 0);
1339
1340
1341
1342
1343
1344
1345 if (!alt)
1346 alt = &intf->altsetting[0];
1347
1348 intf->cur_altsetting = alt;
1349 usb_enable_interface(dev, intf);
1350 if (device_is_registered(&intf->dev))
1351 usb_create_sysfs_intf_files(intf);
1352 }
1353 return 0;
1354}
1355EXPORT_SYMBOL_GPL(usb_reset_configuration);
1356
1357static void usb_release_interface(struct device *dev)
1358{
1359 struct usb_interface *intf = to_usb_interface(dev);
1360 struct usb_interface_cache *intfc =
1361 altsetting_to_usb_interface_cache(intf->altsetting);
1362
1363 kref_put(&intfc->ref, usb_release_interface_cache);
1364 kfree(intf);
1365}
1366
1367#ifdef CONFIG_HOTPLUG
1368static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1369{
1370 struct usb_device *usb_dev;
1371 struct usb_interface *intf;
1372 struct usb_host_interface *alt;
1373
1374 intf = to_usb_interface(dev);
1375 usb_dev = interface_to_usbdev(intf);
1376 alt = intf->cur_altsetting;
1377
1378 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
1379 alt->desc.bInterfaceClass,
1380 alt->desc.bInterfaceSubClass,
1381 alt->desc.bInterfaceProtocol))
1382 return -ENOMEM;
1383
1384 if (add_uevent_var(env,
1385 "MODALIAS=usb:"
1386 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1387 le16_to_cpu(usb_dev->descriptor.idVendor),
1388 le16_to_cpu(usb_dev->descriptor.idProduct),
1389 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1390 usb_dev->descriptor.bDeviceClass,
1391 usb_dev->descriptor.bDeviceSubClass,
1392 usb_dev->descriptor.bDeviceProtocol,
1393 alt->desc.bInterfaceClass,
1394 alt->desc.bInterfaceSubClass,
1395 alt->desc.bInterfaceProtocol))
1396 return -ENOMEM;
1397
1398 return 0;
1399}
1400
1401#else
1402
1403static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1404{
1405 return -ENODEV;
1406}
1407#endif
1408
1409struct device_type usb_if_device_type = {
1410 .name = "usb_interface",
1411 .release = usb_release_interface,
1412 .uevent = usb_if_uevent,
1413};
1414
1415static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1416 struct usb_host_config *config,
1417 u8 inum)
1418{
1419 struct usb_interface_assoc_descriptor *retval = NULL;
1420 struct usb_interface_assoc_descriptor *intf_assoc;
1421 int first_intf;
1422 int last_intf;
1423 int i;
1424
1425 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1426 intf_assoc = config->intf_assoc[i];
1427 if (intf_assoc->bInterfaceCount == 0)
1428 continue;
1429
1430 first_intf = intf_assoc->bFirstInterface;
1431 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1432 if (inum >= first_intf && inum <= last_intf) {
1433 if (!retval)
1434 retval = intf_assoc;
1435 else
1436 dev_err(&dev->dev, "Interface #%d referenced"
1437 " by multiple IADs\n", inum);
1438 }
1439 }
1440
1441 return retval;
1442}
1443
1444
1445
1446
1447
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
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489int usb_set_configuration(struct usb_device *dev, int configuration)
1490{
1491 int i, ret;
1492 struct usb_host_config *cp = NULL;
1493 struct usb_interface **new_interfaces = NULL;
1494 int n, nintf;
1495
1496 if (dev->authorized == 0 || configuration == -1)
1497 configuration = 0;
1498 else {
1499 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1500 if (dev->config[i].desc.bConfigurationValue ==
1501 configuration) {
1502 cp = &dev->config[i];
1503 break;
1504 }
1505 }
1506 }
1507 if ((!cp && configuration != 0))
1508 return -EINVAL;
1509
1510
1511
1512
1513
1514
1515 if (cp && configuration == 0)
1516 dev_warn(&dev->dev, "config 0 descriptor??\n");
1517
1518
1519
1520 n = nintf = 0;
1521 if (cp) {
1522 nintf = cp->desc.bNumInterfaces;
1523 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1524 GFP_KERNEL);
1525 if (!new_interfaces) {
1526 dev_err(&dev->dev, "Out of memory\n");
1527 return -ENOMEM;
1528 }
1529
1530 for (; n < nintf; ++n) {
1531 new_interfaces[n] = kzalloc(
1532 sizeof(struct usb_interface),
1533 GFP_KERNEL);
1534 if (!new_interfaces[n]) {
1535 dev_err(&dev->dev, "Out of memory\n");
1536 ret = -ENOMEM;
1537free_interfaces:
1538 while (--n >= 0)
1539 kfree(new_interfaces[n]);
1540 kfree(new_interfaces);
1541 return ret;
1542 }
1543 }
1544
1545 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1546 if (i < 0)
1547 dev_warn(&dev->dev, "new config #%d exceeds power "
1548 "limit by %dmA\n",
1549 configuration, -i);
1550 }
1551
1552
1553 ret = usb_autoresume_device(dev);
1554 if (ret)
1555 goto free_interfaces;
1556
1557
1558
1559
1560 if (dev->state != USB_STATE_ADDRESS)
1561 usb_disable_device(dev, 1);
1562
1563 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1564 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1565 NULL, 0, USB_CTRL_SET_TIMEOUT);
1566 if (ret < 0) {
1567
1568
1569
1570 cp = NULL;
1571 }
1572
1573 dev->actconfig = cp;
1574 if (!cp) {
1575 usb_set_device_state(dev, USB_STATE_ADDRESS);
1576 usb_autosuspend_device(dev);
1577 goto free_interfaces;
1578 }
1579 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1580
1581
1582
1583
1584 for (i = 0; i < nintf; ++i) {
1585 struct usb_interface_cache *intfc;
1586 struct usb_interface *intf;
1587 struct usb_host_interface *alt;
1588
1589 cp->interface[i] = intf = new_interfaces[i];
1590 intfc = cp->intf_cache[i];
1591 intf->altsetting = intfc->altsetting;
1592 intf->num_altsetting = intfc->num_altsetting;
1593 intf->intf_assoc = find_iad(dev, cp, i);
1594 kref_get(&intfc->ref);
1595
1596 alt = usb_altnum_to_altsetting(intf, 0);
1597
1598
1599
1600
1601
1602
1603 if (!alt)
1604 alt = &intf->altsetting[0];
1605
1606 intf->cur_altsetting = alt;
1607 usb_enable_interface(dev, intf);
1608 intf->dev.parent = &dev->dev;
1609 intf->dev.driver = NULL;
1610 intf->dev.bus = &usb_bus_type;
1611 intf->dev.type = &usb_if_device_type;
1612 intf->dev.groups = usb_interface_groups;
1613 intf->dev.dma_mask = dev->dev.dma_mask;
1614 device_initialize(&intf->dev);
1615 mark_quiesced(intf);
1616 dev_set_name(&intf->dev, "%d-%s:%d.%d",
1617 dev->bus->busnum, dev->devpath,
1618 configuration, alt->desc.bInterfaceNumber);
1619 }
1620 kfree(new_interfaces);
1621
1622 if (cp->string == NULL)
1623 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1624
1625
1626
1627
1628
1629
1630
1631 for (i = 0; i < nintf; ++i) {
1632 struct usb_interface *intf = cp->interface[i];
1633
1634 dev_dbg(&dev->dev,
1635 "adding %s (config #%d, interface %d)\n",
1636 dev_name(&intf->dev), configuration,
1637 intf->cur_altsetting->desc.bInterfaceNumber);
1638 ret = device_add(&intf->dev);
1639 if (ret != 0) {
1640 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1641 dev_name(&intf->dev), ret);
1642 continue;
1643 }
1644 usb_create_sysfs_intf_files(intf);
1645 }
1646
1647 usb_autosuspend_device(dev);
1648 return 0;
1649}
1650
1651struct set_config_request {
1652 struct usb_device *udev;
1653 int config;
1654 struct work_struct work;
1655};
1656
1657
1658static void driver_set_config_work(struct work_struct *work)
1659{
1660 struct set_config_request *req =
1661 container_of(work, struct set_config_request, work);
1662
1663 usb_lock_device(req->udev);
1664 usb_set_configuration(req->udev, req->config);
1665 usb_unlock_device(req->udev);
1666 usb_put_dev(req->udev);
1667 kfree(req);
1668}
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690int usb_driver_set_configuration(struct usb_device *udev, int config)
1691{
1692 struct set_config_request *req;
1693
1694 req = kmalloc(sizeof(*req), GFP_KERNEL);
1695 if (!req)
1696 return -ENOMEM;
1697 req->udev = udev;
1698 req->config = config;
1699 INIT_WORK(&req->work, driver_set_config_work);
1700
1701 usb_get_dev(udev);
1702 schedule_work(&req->work);
1703 return 0;
1704}
1705EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
1706