1
2
3
4
5
6
7
8
9
10#include <linux/kallsyms.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/utsname.h>
16#include <linux/bitfield.h>
17
18#include <linux/usb/composite.h>
19#include <linux/usb/otg.h>
20#include <asm/unaligned.h>
21
22#include "u_os_desc.h"
23
24
25
26
27
28
29
30
31
32struct usb_os_string {
33 __u8 bLength;
34 __u8 bDescriptorType;
35 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
36 __u8 bMS_VendorCode;
37 __u8 bPad;
38} __packed;
39
40
41
42
43
44
45
46
47static struct usb_gadget_strings **get_containers_gs(
48 struct usb_gadget_string_container *uc)
49{
50 return (struct usb_gadget_strings **)uc->stash;
51}
52
53
54
55
56
57
58
59
60static struct usb_descriptor_header **
61function_descriptors(struct usb_function *f,
62 enum usb_device_speed speed)
63{
64 struct usb_descriptor_header **descriptors;
65
66
67
68
69
70
71 switch (speed) {
72 case USB_SPEED_SUPER_PLUS:
73 descriptors = f->ssp_descriptors;
74 if (descriptors)
75 break;
76 fallthrough;
77 case USB_SPEED_SUPER:
78 descriptors = f->ss_descriptors;
79 if (descriptors)
80 break;
81 fallthrough;
82 case USB_SPEED_HIGH:
83 descriptors = f->hs_descriptors;
84 if (descriptors)
85 break;
86 fallthrough;
87 default:
88 descriptors = f->fs_descriptors;
89 }
90
91
92
93
94
95
96 return descriptors;
97}
98
99
100
101
102
103
104
105
106
107
108
109static struct usb_descriptor_header**
110next_desc(struct usb_descriptor_header **t, u8 desc_type)
111{
112 for (; *t; t++) {
113 if ((*t)->bDescriptorType == desc_type)
114 return t;
115 }
116 return NULL;
117}
118
119
120
121
122
123
124
125
126#define for_each_desc(start, iter_desc, desc_type) \
127 for (iter_desc = next_desc(start, desc_type); \
128 iter_desc; iter_desc = next_desc(iter_desc + 1, desc_type))
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149int config_ep_by_speed_and_alt(struct usb_gadget *g,
150 struct usb_function *f,
151 struct usb_ep *_ep,
152 u8 alt)
153{
154 struct usb_endpoint_descriptor *chosen_desc = NULL;
155 struct usb_interface_descriptor *int_desc = NULL;
156 struct usb_descriptor_header **speed_desc = NULL;
157
158 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
159 int want_comp_desc = 0;
160
161 struct usb_descriptor_header **d_spd;
162
163 if (!g || !f || !_ep)
164 return -EIO;
165
166
167 switch (g->speed) {
168 case USB_SPEED_SUPER_PLUS:
169 if (gadget_is_superspeed_plus(g)) {
170 speed_desc = f->ssp_descriptors;
171 want_comp_desc = 1;
172 break;
173 }
174 fallthrough;
175 case USB_SPEED_SUPER:
176 if (gadget_is_superspeed(g)) {
177 speed_desc = f->ss_descriptors;
178 want_comp_desc = 1;
179 break;
180 }
181 fallthrough;
182 case USB_SPEED_HIGH:
183 if (gadget_is_dualspeed(g)) {
184 speed_desc = f->hs_descriptors;
185 break;
186 }
187 fallthrough;
188 default:
189 speed_desc = f->fs_descriptors;
190 }
191
192
193 for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) {
194 int_desc = (struct usb_interface_descriptor *)*d_spd;
195
196 if (int_desc->bAlternateSetting == alt) {
197 speed_desc = d_spd;
198 goto intf_found;
199 }
200 }
201 return -EIO;
202
203intf_found:
204
205 for_each_desc(speed_desc, d_spd, USB_DT_ENDPOINT) {
206 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
207 if (chosen_desc->bEndpointAddress == _ep->address)
208 goto ep_found;
209 }
210 return -EIO;
211
212ep_found:
213
214 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
215 _ep->desc = chosen_desc;
216 _ep->comp_desc = NULL;
217 _ep->maxburst = 0;
218 _ep->mult = 1;
219
220 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
221 usb_endpoint_xfer_int(_ep->desc)))
222 _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
223
224 if (!want_comp_desc)
225 return 0;
226
227
228
229
230
231 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
232 if (!comp_desc ||
233 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
234 return -EIO;
235 _ep->comp_desc = comp_desc;
236 if (g->speed >= USB_SPEED_SUPER) {
237 switch (usb_endpoint_type(_ep->desc)) {
238 case USB_ENDPOINT_XFER_ISOC:
239
240 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
241 fallthrough;
242 case USB_ENDPOINT_XFER_BULK:
243 case USB_ENDPOINT_XFER_INT:
244 _ep->maxburst = comp_desc->bMaxBurst + 1;
245 break;
246 default:
247 if (comp_desc->bMaxBurst != 0) {
248 struct usb_composite_dev *cdev;
249
250 cdev = get_gadget_data(g);
251 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
252 }
253 _ep->maxburst = 1;
254 break;
255 }
256 }
257 return 0;
258}
259EXPORT_SYMBOL_GPL(config_ep_by_speed_and_alt);
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279int config_ep_by_speed(struct usb_gadget *g,
280 struct usb_function *f,
281 struct usb_ep *_ep)
282{
283 return config_ep_by_speed_and_alt(g, f, _ep, 0);
284}
285EXPORT_SYMBOL_GPL(config_ep_by_speed);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301int usb_add_function(struct usb_configuration *config,
302 struct usb_function *function)
303{
304 int value = -EINVAL;
305
306 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
307 function->name, function,
308 config->label, config);
309
310 if (!function->set_alt || !function->disable)
311 goto done;
312
313 function->config = config;
314 list_add_tail(&function->list, &config->functions);
315
316 if (function->bind_deactivated) {
317 value = usb_function_deactivate(function);
318 if (value)
319 goto done;
320 }
321
322
323 if (function->bind) {
324 value = function->bind(config, function);
325 if (value < 0) {
326 list_del(&function->list);
327 function->config = NULL;
328 }
329 } else
330 value = 0;
331
332
333
334
335
336
337 if (!config->fullspeed && function->fs_descriptors)
338 config->fullspeed = true;
339 if (!config->highspeed && function->hs_descriptors)
340 config->highspeed = true;
341 if (!config->superspeed && function->ss_descriptors)
342 config->superspeed = true;
343 if (!config->superspeed_plus && function->ssp_descriptors)
344 config->superspeed_plus = true;
345
346done:
347 if (value)
348 DBG(config->cdev, "adding '%s'/%p --> %d\n",
349 function->name, function, value);
350 return value;
351}
352EXPORT_SYMBOL_GPL(usb_add_function);
353
354void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
355{
356 if (f->disable)
357 f->disable(f);
358
359 bitmap_zero(f->endpoints, 32);
360 list_del(&f->list);
361 if (f->unbind)
362 f->unbind(c, f);
363
364 if (f->bind_deactivated)
365 usb_function_activate(f);
366}
367EXPORT_SYMBOL_GPL(usb_remove_function);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388int usb_function_deactivate(struct usb_function *function)
389{
390 struct usb_composite_dev *cdev = function->config->cdev;
391 unsigned long flags;
392 int status = 0;
393
394 spin_lock_irqsave(&cdev->lock, flags);
395
396 if (cdev->deactivations == 0) {
397 spin_unlock_irqrestore(&cdev->lock, flags);
398 status = usb_gadget_deactivate(cdev->gadget);
399 spin_lock_irqsave(&cdev->lock, flags);
400 }
401 if (status == 0)
402 cdev->deactivations++;
403
404 spin_unlock_irqrestore(&cdev->lock, flags);
405 return status;
406}
407EXPORT_SYMBOL_GPL(usb_function_deactivate);
408
409
410
411
412
413
414
415
416
417
418
419int usb_function_activate(struct usb_function *function)
420{
421 struct usb_composite_dev *cdev = function->config->cdev;
422 unsigned long flags;
423 int status = 0;
424
425 spin_lock_irqsave(&cdev->lock, flags);
426
427 if (WARN_ON(cdev->deactivations == 0))
428 status = -EINVAL;
429 else {
430 cdev->deactivations--;
431 if (cdev->deactivations == 0) {
432 spin_unlock_irqrestore(&cdev->lock, flags);
433 status = usb_gadget_activate(cdev->gadget);
434 spin_lock_irqsave(&cdev->lock, flags);
435 }
436 }
437
438 spin_unlock_irqrestore(&cdev->lock, flags);
439 return status;
440}
441EXPORT_SYMBOL_GPL(usb_function_activate);
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466int usb_interface_id(struct usb_configuration *config,
467 struct usb_function *function)
468{
469 unsigned id = config->next_interface_id;
470
471 if (id < MAX_CONFIG_INTERFACES) {
472 config->interface[id] = function;
473 config->next_interface_id = id + 1;
474 return id;
475 }
476 return -ENODEV;
477}
478EXPORT_SYMBOL_GPL(usb_interface_id);
479
480static u8 encode_bMaxPower(enum usb_device_speed speed,
481 struct usb_configuration *c)
482{
483 unsigned val;
484
485 if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER))
486 val = c->MaxPower;
487 else
488 val = CONFIG_USB_GADGET_VBUS_DRAW;
489 if (!val)
490 return 0;
491 if (speed < USB_SPEED_SUPER)
492 return min(val, 500U) / 2;
493 else
494
495
496
497
498 return min(val, 900U) / 8;
499}
500
501static int config_buf(struct usb_configuration *config,
502 enum usb_device_speed speed, void *buf, u8 type)
503{
504 struct usb_config_descriptor *c = buf;
505 void *next = buf + USB_DT_CONFIG_SIZE;
506 int len;
507 struct usb_function *f;
508 int status;
509
510 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
511
512 c = buf;
513 c->bLength = USB_DT_CONFIG_SIZE;
514 c->bDescriptorType = type;
515
516 c->bNumInterfaces = config->next_interface_id;
517 c->bConfigurationValue = config->bConfigurationValue;
518 c->iConfiguration = config->iConfiguration;
519 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
520 c->bMaxPower = encode_bMaxPower(speed, config);
521
522
523 if (config->descriptors) {
524 status = usb_descriptor_fillbuf(next, len,
525 config->descriptors);
526 if (status < 0)
527 return status;
528 len -= status;
529 next += status;
530 }
531
532
533 list_for_each_entry(f, &config->functions, list) {
534 struct usb_descriptor_header **descriptors;
535
536 descriptors = function_descriptors(f, speed);
537 if (!descriptors)
538 continue;
539 status = usb_descriptor_fillbuf(next, len,
540 (const struct usb_descriptor_header **) descriptors);
541 if (status < 0)
542 return status;
543 len -= status;
544 next += status;
545 }
546
547 len = next - buf;
548 c->wTotalLength = cpu_to_le16(len);
549 return len;
550}
551
552static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
553{
554 struct usb_gadget *gadget = cdev->gadget;
555 struct usb_configuration *c;
556 struct list_head *pos;
557 u8 type = w_value >> 8;
558 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
559
560 if (gadget->speed >= USB_SPEED_SUPER)
561 speed = gadget->speed;
562 else if (gadget_is_dualspeed(gadget)) {
563 int hs = 0;
564 if (gadget->speed == USB_SPEED_HIGH)
565 hs = 1;
566 if (type == USB_DT_OTHER_SPEED_CONFIG)
567 hs = !hs;
568 if (hs)
569 speed = USB_SPEED_HIGH;
570
571 }
572
573
574 w_value &= 0xff;
575
576 pos = &cdev->configs;
577 c = cdev->os_desc_config;
578 if (c)
579 goto check_config;
580
581 while ((pos = pos->next) != &cdev->configs) {
582 c = list_entry(pos, typeof(*c), list);
583
584
585 if (c == cdev->os_desc_config)
586 continue;
587
588check_config:
589
590 switch (speed) {
591 case USB_SPEED_SUPER_PLUS:
592 if (!c->superspeed_plus)
593 continue;
594 break;
595 case USB_SPEED_SUPER:
596 if (!c->superspeed)
597 continue;
598 break;
599 case USB_SPEED_HIGH:
600 if (!c->highspeed)
601 continue;
602 break;
603 default:
604 if (!c->fullspeed)
605 continue;
606 }
607
608 if (w_value == 0)
609 return config_buf(c, speed, cdev->req->buf, type);
610 w_value--;
611 }
612 return -EINVAL;
613}
614
615static int count_configs(struct usb_composite_dev *cdev, unsigned type)
616{
617 struct usb_gadget *gadget = cdev->gadget;
618 struct usb_configuration *c;
619 unsigned count = 0;
620 int hs = 0;
621 int ss = 0;
622 int ssp = 0;
623
624 if (gadget_is_dualspeed(gadget)) {
625 if (gadget->speed == USB_SPEED_HIGH)
626 hs = 1;
627 if (gadget->speed == USB_SPEED_SUPER)
628 ss = 1;
629 if (gadget->speed == USB_SPEED_SUPER_PLUS)
630 ssp = 1;
631 if (type == USB_DT_DEVICE_QUALIFIER)
632 hs = !hs;
633 }
634 list_for_each_entry(c, &cdev->configs, list) {
635
636 if (ssp) {
637 if (!c->superspeed_plus)
638 continue;
639 } else if (ss) {
640 if (!c->superspeed)
641 continue;
642 } else if (hs) {
643 if (!c->highspeed)
644 continue;
645 } else {
646 if (!c->fullspeed)
647 continue;
648 }
649 count++;
650 }
651 return count;
652}
653
654
655
656
657
658
659
660
661
662
663static int bos_desc(struct usb_composite_dev *cdev)
664{
665 struct usb_ext_cap_descriptor *usb_ext;
666 struct usb_dcd_config_params dcd_config_params;
667 struct usb_bos_descriptor *bos = cdev->req->buf;
668 unsigned int besl = 0;
669
670 bos->bLength = USB_DT_BOS_SIZE;
671 bos->bDescriptorType = USB_DT_BOS;
672
673 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
674 bos->bNumDeviceCaps = 0;
675
676
677 if (cdev->gadget->ops->get_config_params) {
678 cdev->gadget->ops->get_config_params(cdev->gadget,
679 &dcd_config_params);
680 } else {
681 dcd_config_params.besl_baseline =
682 USB_DEFAULT_BESL_UNSPECIFIED;
683 dcd_config_params.besl_deep =
684 USB_DEFAULT_BESL_UNSPECIFIED;
685 dcd_config_params.bU1devExitLat =
686 USB_DEFAULT_U1_DEV_EXIT_LAT;
687 dcd_config_params.bU2DevExitLat =
688 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
689 }
690
691 if (dcd_config_params.besl_baseline != USB_DEFAULT_BESL_UNSPECIFIED)
692 besl = USB_BESL_BASELINE_VALID |
693 USB_SET_BESL_BASELINE(dcd_config_params.besl_baseline);
694
695 if (dcd_config_params.besl_deep != USB_DEFAULT_BESL_UNSPECIFIED)
696 besl |= USB_BESL_DEEP_VALID |
697 USB_SET_BESL_DEEP(dcd_config_params.besl_deep);
698
699
700
701
702
703 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
704 bos->bNumDeviceCaps++;
705 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
706 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
707 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
708 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
709 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT |
710 USB_BESL_SUPPORT | besl);
711
712
713
714
715
716 if (gadget_is_superspeed(cdev->gadget)) {
717 struct usb_ss_cap_descriptor *ss_cap;
718
719 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
720 bos->bNumDeviceCaps++;
721 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
722 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
723 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
724 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
725 ss_cap->bmAttributes = 0;
726 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
727 USB_FULL_SPEED_OPERATION |
728 USB_HIGH_SPEED_OPERATION |
729 USB_5GBPS_OPERATION);
730 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
731
732
733 if (cdev->gadget->ops->get_config_params) {
734 cdev->gadget->ops->get_config_params(cdev->gadget,
735 &dcd_config_params);
736 } else {
737 dcd_config_params.bU1devExitLat =
738 USB_DEFAULT_U1_DEV_EXIT_LAT;
739 dcd_config_params.bU2DevExitLat =
740 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
741 }
742 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
743 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
744 }
745
746
747 if (gadget_is_superspeed_plus(cdev->gadget)) {
748 struct usb_ssp_cap_descriptor *ssp_cap;
749 u8 ssac = 1;
750 u8 ssic;
751 int i;
752
753 if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x2)
754 ssac = 3;
755
756
757
758
759
760 ssic = (ssac + 1) / 2 - 1;
761
762 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
763 bos->bNumDeviceCaps++;
764
765 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(ssac));
766 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(ssac);
767 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
768 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
769 ssp_cap->bReserved = 0;
770 ssp_cap->wReserved = 0;
771
772 ssp_cap->bmAttributes =
773 cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_ATTRIBS, ssac) |
774 FIELD_PREP(USB_SSP_SUBLINK_SPEED_IDS, ssic));
775
776 ssp_cap->wFunctionalitySupport =
777 cpu_to_le16(FIELD_PREP(USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID, 0) |
778 FIELD_PREP(USB_SSP_MIN_RX_LANE_COUNT, 1) |
779 FIELD_PREP(USB_SSP_MIN_TX_LANE_COUNT, 1));
780
781
782
783
784
785
786
787
788
789
790
791
792
793 for (i = 0; i < ssac + 1; i++) {
794 u8 ssid;
795 u8 mantissa;
796 u8 type;
797
798 ssid = i >> 1;
799
800 if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x1 ||
801 cdev->gadget->max_ssp_rate == USB_SSP_GEN_UNKNOWN)
802 mantissa = 10;
803 else
804 mantissa = 5 << ssid;
805
806 if (i % 2)
807 type = USB_SSP_SUBLINK_SPEED_ST_SYM_TX;
808 else
809 type = USB_SSP_SUBLINK_SPEED_ST_SYM_RX;
810
811 ssp_cap->bmSublinkSpeedAttr[i] =
812 cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_SSID, ssid) |
813 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSE,
814 USB_SSP_SUBLINK_SPEED_LSE_GBPS) |
815 FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST, type) |
816 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LP,
817 USB_SSP_SUBLINK_SPEED_LP_SSP) |
818 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSM, mantissa));
819 }
820 }
821
822 return le16_to_cpu(bos->wTotalLength);
823}
824
825static void device_qual(struct usb_composite_dev *cdev)
826{
827 struct usb_qualifier_descriptor *qual = cdev->req->buf;
828
829 qual->bLength = sizeof(*qual);
830 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
831
832 qual->bcdUSB = cdev->desc.bcdUSB;
833 qual->bDeviceClass = cdev->desc.bDeviceClass;
834 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
835 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
836
837 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
838 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
839 qual->bRESERVED = 0;
840}
841
842
843
844static void reset_config(struct usb_composite_dev *cdev)
845{
846 struct usb_function *f;
847
848 DBG(cdev, "reset config\n");
849
850 list_for_each_entry(f, &cdev->config->functions, list) {
851 if (f->disable)
852 f->disable(f);
853
854 bitmap_zero(f->endpoints, 32);
855 }
856 cdev->config = NULL;
857 cdev->delayed_status = 0;
858}
859
860static int set_config(struct usb_composite_dev *cdev,
861 const struct usb_ctrlrequest *ctrl, unsigned number)
862{
863 struct usb_gadget *gadget = cdev->gadget;
864 struct usb_configuration *c = NULL;
865 int result = -EINVAL;
866 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
867 int tmp;
868
869 if (number) {
870 list_for_each_entry(c, &cdev->configs, list) {
871 if (c->bConfigurationValue == number) {
872
873
874
875
876
877 if (cdev->config)
878 reset_config(cdev);
879 result = 0;
880 break;
881 }
882 }
883 if (result < 0)
884 goto done;
885 } else {
886 if (cdev->config)
887 reset_config(cdev);
888 result = 0;
889 }
890
891 DBG(cdev, "%s config #%d: %s\n",
892 usb_speed_string(gadget->speed),
893 number, c ? c->label : "unconfigured");
894
895 if (!c)
896 goto done;
897
898 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
899 cdev->config = c;
900
901
902 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
903 struct usb_function *f = c->interface[tmp];
904 struct usb_descriptor_header **descriptors;
905
906 if (!f)
907 break;
908
909
910
911
912
913
914
915 descriptors = function_descriptors(f, gadget->speed);
916
917 for (; *descriptors; ++descriptors) {
918 struct usb_endpoint_descriptor *ep;
919 int addr;
920
921 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
922 continue;
923
924 ep = (struct usb_endpoint_descriptor *)*descriptors;
925 addr = ((ep->bEndpointAddress & 0x80) >> 3)
926 | (ep->bEndpointAddress & 0x0f);
927 set_bit(addr, f->endpoints);
928 }
929
930 result = f->set_alt(f, tmp, 0);
931 if (result < 0) {
932 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
933 tmp, f->name, f, result);
934
935 reset_config(cdev);
936 goto done;
937 }
938
939 if (result == USB_GADGET_DELAYED_STATUS) {
940 DBG(cdev,
941 "%s: interface %d (%s) requested delayed status\n",
942 __func__, tmp, f->name);
943 cdev->delayed_status++;
944 DBG(cdev, "delayed_status count %d\n",
945 cdev->delayed_status);
946 }
947 }
948
949
950 if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER))
951 power = c->MaxPower;
952 else
953 power = CONFIG_USB_GADGET_VBUS_DRAW;
954
955 if (gadget->speed < USB_SPEED_SUPER)
956 power = min(power, 500U);
957 else
958 power = min(power, 900U);
959done:
960 if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
961 usb_gadget_set_selfpowered(gadget);
962 else
963 usb_gadget_clear_selfpowered(gadget);
964
965 usb_gadget_vbus_draw(gadget, power);
966 if (result >= 0 && cdev->delayed_status)
967 result = USB_GADGET_DELAYED_STATUS;
968 return result;
969}
970
971int usb_add_config_only(struct usb_composite_dev *cdev,
972 struct usb_configuration *config)
973{
974 struct usb_configuration *c;
975
976 if (!config->bConfigurationValue)
977 return -EINVAL;
978
979
980 list_for_each_entry(c, &cdev->configs, list) {
981 if (c->bConfigurationValue == config->bConfigurationValue)
982 return -EBUSY;
983 }
984
985 config->cdev = cdev;
986 list_add_tail(&config->list, &cdev->configs);
987
988 INIT_LIST_HEAD(&config->functions);
989 config->next_interface_id = 0;
990 memset(config->interface, 0, sizeof(config->interface));
991
992 return 0;
993}
994EXPORT_SYMBOL_GPL(usb_add_config_only);
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011int usb_add_config(struct usb_composite_dev *cdev,
1012 struct usb_configuration *config,
1013 int (*bind)(struct usb_configuration *))
1014{
1015 int status = -EINVAL;
1016
1017 if (!bind)
1018 goto done;
1019
1020 DBG(cdev, "adding config #%u '%s'/%p\n",
1021 config->bConfigurationValue,
1022 config->label, config);
1023
1024 status = usb_add_config_only(cdev, config);
1025 if (status)
1026 goto done;
1027
1028 status = bind(config);
1029 if (status < 0) {
1030 while (!list_empty(&config->functions)) {
1031 struct usb_function *f;
1032
1033 f = list_first_entry(&config->functions,
1034 struct usb_function, list);
1035 list_del(&f->list);
1036 if (f->unbind) {
1037 DBG(cdev, "unbind function '%s'/%p\n",
1038 f->name, f);
1039 f->unbind(config, f);
1040
1041 }
1042 }
1043 list_del(&config->list);
1044 config->cdev = NULL;
1045 } else {
1046 unsigned i;
1047
1048 DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
1049 config->bConfigurationValue, config,
1050 config->superspeed_plus ? " superplus" : "",
1051 config->superspeed ? " super" : "",
1052 config->highspeed ? " high" : "",
1053 config->fullspeed
1054 ? (gadget_is_dualspeed(cdev->gadget)
1055 ? " full"
1056 : " full/low")
1057 : "");
1058
1059 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1060 struct usb_function *f = config->interface[i];
1061
1062 if (!f)
1063 continue;
1064 DBG(cdev, " interface %d = %s/%p\n",
1065 i, f->name, f);
1066 }
1067 }
1068
1069
1070 usb_ep_autoconfig_reset(cdev->gadget);
1071
1072done:
1073 if (status)
1074 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1075 config->bConfigurationValue, status);
1076 return status;
1077}
1078EXPORT_SYMBOL_GPL(usb_add_config);
1079
1080static void remove_config(struct usb_composite_dev *cdev,
1081 struct usb_configuration *config)
1082{
1083 while (!list_empty(&config->functions)) {
1084 struct usb_function *f;
1085
1086 f = list_first_entry(&config->functions,
1087 struct usb_function, list);
1088
1089 usb_remove_function(config, f);
1090 }
1091 list_del(&config->list);
1092 if (config->unbind) {
1093 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
1094 config->unbind(config);
1095
1096 }
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108void usb_remove_config(struct usb_composite_dev *cdev,
1109 struct usb_configuration *config)
1110{
1111 unsigned long flags;
1112
1113 spin_lock_irqsave(&cdev->lock, flags);
1114
1115 if (cdev->config == config)
1116 reset_config(cdev);
1117
1118 spin_unlock_irqrestore(&cdev->lock, flags);
1119
1120 remove_config(cdev, config);
1121}
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1132{
1133 const struct usb_gadget_strings *s;
1134 __le16 language;
1135 __le16 *tmp;
1136
1137 while (*sp) {
1138 s = *sp;
1139 language = cpu_to_le16(s->language);
1140 for (tmp = buf; *tmp && tmp < &buf[USB_MAX_STRING_LEN]; tmp++) {
1141 if (*tmp == language)
1142 goto repeat;
1143 }
1144 *tmp++ = language;
1145repeat:
1146 sp++;
1147 }
1148}
1149
1150static int lookup_string(
1151 struct usb_gadget_strings **sp,
1152 void *buf,
1153 u16 language,
1154 int id
1155)
1156{
1157 struct usb_gadget_strings *s;
1158 int value;
1159
1160 while (*sp) {
1161 s = *sp++;
1162 if (s->language != language)
1163 continue;
1164 value = usb_gadget_get_string(s, id, buf);
1165 if (value > 0)
1166 return value;
1167 }
1168 return -EINVAL;
1169}
1170
1171static int get_string(struct usb_composite_dev *cdev,
1172 void *buf, u16 language, int id)
1173{
1174 struct usb_composite_driver *composite = cdev->driver;
1175 struct usb_gadget_string_container *uc;
1176 struct usb_configuration *c;
1177 struct usb_function *f;
1178 int len;
1179
1180
1181
1182
1183
1184
1185
1186 if (id == 0) {
1187 struct usb_string_descriptor *s = buf;
1188 struct usb_gadget_strings **sp;
1189
1190 memset(s, 0, 256);
1191 s->bDescriptorType = USB_DT_STRING;
1192
1193 sp = composite->strings;
1194 if (sp)
1195 collect_langs(sp, s->wData);
1196
1197 list_for_each_entry(c, &cdev->configs, list) {
1198 sp = c->strings;
1199 if (sp)
1200 collect_langs(sp, s->wData);
1201
1202 list_for_each_entry(f, &c->functions, list) {
1203 sp = f->strings;
1204 if (sp)
1205 collect_langs(sp, s->wData);
1206 }
1207 }
1208 list_for_each_entry(uc, &cdev->gstrings, list) {
1209 struct usb_gadget_strings **sp;
1210
1211 sp = get_containers_gs(uc);
1212 collect_langs(sp, s->wData);
1213 }
1214
1215 for (len = 0; len <= USB_MAX_STRING_LEN && s->wData[len]; len++)
1216 continue;
1217 if (!len)
1218 return -EINVAL;
1219
1220 s->bLength = 2 * (len + 1);
1221 return s->bLength;
1222 }
1223
1224 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1225 struct usb_os_string *b = buf;
1226 b->bLength = sizeof(*b);
1227 b->bDescriptorType = USB_DT_STRING;
1228 compiletime_assert(
1229 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1230 "qwSignature size must be equal to qw_sign");
1231 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1232 b->bMS_VendorCode = cdev->b_vendor_code;
1233 b->bPad = 0;
1234 return sizeof(*b);
1235 }
1236
1237 list_for_each_entry(uc, &cdev->gstrings, list) {
1238 struct usb_gadget_strings **sp;
1239
1240 sp = get_containers_gs(uc);
1241 len = lookup_string(sp, buf, language, id);
1242 if (len > 0)
1243 return len;
1244 }
1245
1246
1247
1248
1249
1250 if (composite->strings) {
1251 len = lookup_string(composite->strings, buf, language, id);
1252 if (len > 0)
1253 return len;
1254 }
1255 list_for_each_entry(c, &cdev->configs, list) {
1256 if (c->strings) {
1257 len = lookup_string(c->strings, buf, language, id);
1258 if (len > 0)
1259 return len;
1260 }
1261 list_for_each_entry(f, &c->functions, list) {
1262 if (!f->strings)
1263 continue;
1264 len = lookup_string(f->strings, buf, language, id);
1265 if (len > 0)
1266 return len;
1267 }
1268 }
1269 return -EINVAL;
1270}
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286int usb_string_id(struct usb_composite_dev *cdev)
1287{
1288 if (cdev->next_string_id < 254) {
1289
1290
1291
1292 cdev->next_string_id++;
1293 return cdev->next_string_id;
1294 }
1295 return -ENODEV;
1296}
1297EXPORT_SYMBOL_GPL(usb_string_id);
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1316{
1317 int next = cdev->next_string_id;
1318
1319 for (; str->s; ++str) {
1320 if (unlikely(next >= 254))
1321 return -ENODEV;
1322 str->id = ++next;
1323 }
1324
1325 cdev->next_string_id = next;
1326
1327 return 0;
1328}
1329EXPORT_SYMBOL_GPL(usb_string_ids_tab);
1330
1331static struct usb_gadget_string_container *copy_gadget_strings(
1332 struct usb_gadget_strings **sp, unsigned n_gstrings,
1333 unsigned n_strings)
1334{
1335 struct usb_gadget_string_container *uc;
1336 struct usb_gadget_strings **gs_array;
1337 struct usb_gadget_strings *gs;
1338 struct usb_string *s;
1339 unsigned mem;
1340 unsigned n_gs;
1341 unsigned n_s;
1342 void *stash;
1343
1344 mem = sizeof(*uc);
1345 mem += sizeof(void *) * (n_gstrings + 1);
1346 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1347 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1348 uc = kmalloc(mem, GFP_KERNEL);
1349 if (!uc)
1350 return ERR_PTR(-ENOMEM);
1351 gs_array = get_containers_gs(uc);
1352 stash = uc->stash;
1353 stash += sizeof(void *) * (n_gstrings + 1);
1354 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1355 struct usb_string *org_s;
1356
1357 gs_array[n_gs] = stash;
1358 gs = gs_array[n_gs];
1359 stash += sizeof(struct usb_gadget_strings);
1360 gs->language = sp[n_gs]->language;
1361 gs->strings = stash;
1362 org_s = sp[n_gs]->strings;
1363
1364 for (n_s = 0; n_s < n_strings; n_s++) {
1365 s = stash;
1366 stash += sizeof(struct usb_string);
1367 if (org_s->s)
1368 s->s = org_s->s;
1369 else
1370 s->s = "";
1371 org_s++;
1372 }
1373 s = stash;
1374 s->s = NULL;
1375 stash += sizeof(struct usb_string);
1376
1377 }
1378 gs_array[n_gs] = NULL;
1379 return uc;
1380}
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1400 struct usb_gadget_strings **sp, unsigned n_strings)
1401{
1402 struct usb_gadget_string_container *uc;
1403 struct usb_gadget_strings **n_gs;
1404 unsigned n_gstrings = 0;
1405 unsigned i;
1406 int ret;
1407
1408 for (i = 0; sp[i]; i++)
1409 n_gstrings++;
1410
1411 if (!n_gstrings)
1412 return ERR_PTR(-EINVAL);
1413
1414 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1415 if (IS_ERR(uc))
1416 return ERR_CAST(uc);
1417
1418 n_gs = get_containers_gs(uc);
1419 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1420 if (ret)
1421 goto err;
1422
1423 for (i = 1; i < n_gstrings; i++) {
1424 struct usb_string *m_s;
1425 struct usb_string *s;
1426 unsigned n;
1427
1428 m_s = n_gs[0]->strings;
1429 s = n_gs[i]->strings;
1430 for (n = 0; n < n_strings; n++) {
1431 s->id = m_s->id;
1432 s++;
1433 m_s++;
1434 }
1435 }
1436 list_add_tail(&uc->list, &cdev->gstrings);
1437 return n_gs[0]->strings;
1438err:
1439 kfree(uc);
1440 return ERR_PTR(ret);
1441}
1442EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1464{
1465 unsigned next = c->next_string_id;
1466 if (unlikely(n > 254 || (unsigned)next + n > 254))
1467 return -ENODEV;
1468 c->next_string_id += n;
1469 return next + 1;
1470}
1471EXPORT_SYMBOL_GPL(usb_string_ids_n);
1472
1473
1474
1475static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1476{
1477 struct usb_composite_dev *cdev;
1478
1479 if (req->status || req->actual != req->length)
1480 DBG((struct usb_composite_dev *) ep->driver_data,
1481 "setup complete --> %d, %d/%d\n",
1482 req->status, req->actual, req->length);
1483
1484
1485
1486
1487
1488
1489
1490
1491 if (!req->context)
1492 return;
1493
1494 cdev = req->context;
1495
1496 if (cdev->req == req)
1497 cdev->setup_pending = false;
1498 else if (cdev->os_desc_req == req)
1499 cdev->os_desc_pending = false;
1500 else
1501 WARN(1, "unknown request %p\n", req);
1502}
1503
1504static int composite_ep0_queue(struct usb_composite_dev *cdev,
1505 struct usb_request *req, gfp_t gfp_flags)
1506{
1507 int ret;
1508
1509 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1510 if (ret == 0) {
1511 if (cdev->req == req)
1512 cdev->setup_pending = true;
1513 else if (cdev->os_desc_req == req)
1514 cdev->os_desc_pending = true;
1515 else
1516 WARN(1, "unknown request %p\n", req);
1517 }
1518
1519 return ret;
1520}
1521
1522static int count_ext_compat(struct usb_configuration *c)
1523{
1524 int i, res;
1525
1526 res = 0;
1527 for (i = 0; i < c->next_interface_id; ++i) {
1528 struct usb_function *f;
1529 int j;
1530
1531 f = c->interface[i];
1532 for (j = 0; j < f->os_desc_n; ++j) {
1533 struct usb_os_desc *d;
1534
1535 if (i != f->os_desc_table[j].if_id)
1536 continue;
1537 d = f->os_desc_table[j].os_desc;
1538 if (d && d->ext_compat_id)
1539 ++res;
1540 }
1541 }
1542 BUG_ON(res > 255);
1543 return res;
1544}
1545
1546static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
1547{
1548 int i, count;
1549
1550 count = 16;
1551 buf += 16;
1552 for (i = 0; i < c->next_interface_id; ++i) {
1553 struct usb_function *f;
1554 int j;
1555
1556 f = c->interface[i];
1557 for (j = 0; j < f->os_desc_n; ++j) {
1558 struct usb_os_desc *d;
1559
1560 if (i != f->os_desc_table[j].if_id)
1561 continue;
1562 d = f->os_desc_table[j].os_desc;
1563 if (d && d->ext_compat_id) {
1564 *buf++ = i;
1565 *buf++ = 0x01;
1566 memcpy(buf, d->ext_compat_id, 16);
1567 buf += 22;
1568 } else {
1569 ++buf;
1570 *buf = 0x01;
1571 buf += 23;
1572 }
1573 count += 24;
1574 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1575 return count;
1576 }
1577 }
1578
1579 return count;
1580}
1581
1582static int count_ext_prop(struct usb_configuration *c, int interface)
1583{
1584 struct usb_function *f;
1585 int j;
1586
1587 f = c->interface[interface];
1588 for (j = 0; j < f->os_desc_n; ++j) {
1589 struct usb_os_desc *d;
1590
1591 if (interface != f->os_desc_table[j].if_id)
1592 continue;
1593 d = f->os_desc_table[j].os_desc;
1594 if (d && d->ext_compat_id)
1595 return d->ext_prop_count;
1596 }
1597 return 0;
1598}
1599
1600static int len_ext_prop(struct usb_configuration *c, int interface)
1601{
1602 struct usb_function *f;
1603 struct usb_os_desc *d;
1604 int j, res;
1605
1606 res = 10;
1607 f = c->interface[interface];
1608 for (j = 0; j < f->os_desc_n; ++j) {
1609 if (interface != f->os_desc_table[j].if_id)
1610 continue;
1611 d = f->os_desc_table[j].os_desc;
1612 if (d)
1613 return min(res + d->ext_prop_len, 4096);
1614 }
1615 return res;
1616}
1617
1618static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1619{
1620 struct usb_function *f;
1621 struct usb_os_desc *d;
1622 struct usb_os_desc_ext_prop *ext_prop;
1623 int j, count, n, ret;
1624
1625 f = c->interface[interface];
1626 count = 10;
1627 buf += 10;
1628 for (j = 0; j < f->os_desc_n; ++j) {
1629 if (interface != f->os_desc_table[j].if_id)
1630 continue;
1631 d = f->os_desc_table[j].os_desc;
1632 if (d)
1633 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1634 n = ext_prop->data_len +
1635 ext_prop->name_len + 14;
1636 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1637 return count;
1638 usb_ext_prop_put_size(buf, n);
1639 usb_ext_prop_put_type(buf, ext_prop->type);
1640 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1641 ext_prop->name_len);
1642 if (ret < 0)
1643 return ret;
1644 switch (ext_prop->type) {
1645 case USB_EXT_PROP_UNICODE:
1646 case USB_EXT_PROP_UNICODE_ENV:
1647 case USB_EXT_PROP_UNICODE_LINK:
1648 usb_ext_prop_put_unicode(buf, ret,
1649 ext_prop->data,
1650 ext_prop->data_len);
1651 break;
1652 case USB_EXT_PROP_BINARY:
1653 usb_ext_prop_put_binary(buf, ret,
1654 ext_prop->data,
1655 ext_prop->data_len);
1656 break;
1657 case USB_EXT_PROP_LE32:
1658
1659 case USB_EXT_PROP_BE32:
1660
1661 default:
1662 return -EINVAL;
1663 }
1664 buf += n;
1665 count += n;
1666 }
1667 }
1668
1669 return count;
1670}
1671
1672
1673
1674
1675
1676
1677
1678
1679int
1680composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1681{
1682 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1683 struct usb_request *req = cdev->req;
1684 int value = -EOPNOTSUPP;
1685 int status = 0;
1686 u16 w_index = le16_to_cpu(ctrl->wIndex);
1687 u8 intf = w_index & 0xFF;
1688 u16 w_value = le16_to_cpu(ctrl->wValue);
1689 u16 w_length = le16_to_cpu(ctrl->wLength);
1690 struct usb_function *f = NULL;
1691 u8 endp;
1692
1693
1694
1695
1696
1697 req->zero = 0;
1698 req->context = cdev;
1699 req->complete = composite_setup_complete;
1700 req->length = 0;
1701 gadget->ep0->driver_data = cdev;
1702
1703
1704
1705
1706
1707 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1708 goto unknown;
1709
1710 switch (ctrl->bRequest) {
1711
1712
1713 case USB_REQ_GET_DESCRIPTOR:
1714 if (ctrl->bRequestType != USB_DIR_IN)
1715 goto unknown;
1716 switch (w_value >> 8) {
1717
1718 case USB_DT_DEVICE:
1719 cdev->desc.bNumConfigurations =
1720 count_configs(cdev, USB_DT_DEVICE);
1721 cdev->desc.bMaxPacketSize0 =
1722 cdev->gadget->ep0->maxpacket;
1723 if (gadget_is_superspeed(gadget)) {
1724 if (gadget->speed >= USB_SPEED_SUPER) {
1725 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
1726 cdev->desc.bMaxPacketSize0 = 9;
1727 } else {
1728 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1729 }
1730 } else {
1731 if (gadget->lpm_capable)
1732 cdev->desc.bcdUSB = cpu_to_le16(0x0201);
1733 else
1734 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
1735 }
1736
1737 value = min(w_length, (u16) sizeof cdev->desc);
1738 memcpy(req->buf, &cdev->desc, value);
1739 break;
1740 case USB_DT_DEVICE_QUALIFIER:
1741 if (!gadget_is_dualspeed(gadget) ||
1742 gadget->speed >= USB_SPEED_SUPER)
1743 break;
1744 device_qual(cdev);
1745 value = min_t(int, w_length,
1746 sizeof(struct usb_qualifier_descriptor));
1747 break;
1748 case USB_DT_OTHER_SPEED_CONFIG:
1749 if (!gadget_is_dualspeed(gadget) ||
1750 gadget->speed >= USB_SPEED_SUPER)
1751 break;
1752 fallthrough;
1753 case USB_DT_CONFIG:
1754 value = config_desc(cdev, w_value);
1755 if (value >= 0)
1756 value = min(w_length, (u16) value);
1757 break;
1758 case USB_DT_STRING:
1759 value = get_string(cdev, req->buf,
1760 w_index, w_value & 0xff);
1761 if (value >= 0)
1762 value = min(w_length, (u16) value);
1763 break;
1764 case USB_DT_BOS:
1765 if (gadget_is_superspeed(gadget) ||
1766 gadget->lpm_capable) {
1767 value = bos_desc(cdev);
1768 value = min(w_length, (u16) value);
1769 }
1770 break;
1771 case USB_DT_OTG:
1772 if (gadget_is_otg(gadget)) {
1773 struct usb_configuration *config;
1774 int otg_desc_len = 0;
1775
1776 if (cdev->config)
1777 config = cdev->config;
1778 else
1779 config = list_first_entry(
1780 &cdev->configs,
1781 struct usb_configuration, list);
1782 if (!config)
1783 goto done;
1784
1785 if (gadget->otg_caps &&
1786 (gadget->otg_caps->otg_rev >= 0x0200))
1787 otg_desc_len += sizeof(
1788 struct usb_otg20_descriptor);
1789 else
1790 otg_desc_len += sizeof(
1791 struct usb_otg_descriptor);
1792
1793 value = min_t(int, w_length, otg_desc_len);
1794 memcpy(req->buf, config->descriptors[0], value);
1795 }
1796 break;
1797 }
1798 break;
1799
1800
1801 case USB_REQ_SET_CONFIGURATION:
1802 if (ctrl->bRequestType != 0)
1803 goto unknown;
1804 if (gadget_is_otg(gadget)) {
1805 if (gadget->a_hnp_support)
1806 DBG(cdev, "HNP available\n");
1807 else if (gadget->a_alt_hnp_support)
1808 DBG(cdev, "HNP on another port\n");
1809 else
1810 VDBG(cdev, "HNP inactive\n");
1811 }
1812 spin_lock(&cdev->lock);
1813 value = set_config(cdev, ctrl, w_value);
1814 spin_unlock(&cdev->lock);
1815 break;
1816 case USB_REQ_GET_CONFIGURATION:
1817 if (ctrl->bRequestType != USB_DIR_IN)
1818 goto unknown;
1819 if (cdev->config)
1820 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1821 else
1822 *(u8 *)req->buf = 0;
1823 value = min(w_length, (u16) 1);
1824 break;
1825
1826
1827 case USB_REQ_SET_INTERFACE:
1828 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1829 goto unknown;
1830 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1831 break;
1832 f = cdev->config->interface[intf];
1833 if (!f)
1834 break;
1835
1836
1837
1838
1839
1840
1841 if (w_value && !f->get_alt)
1842 break;
1843
1844 spin_lock(&cdev->lock);
1845 value = f->set_alt(f, w_index, w_value);
1846 if (value == USB_GADGET_DELAYED_STATUS) {
1847 DBG(cdev,
1848 "%s: interface %d (%s) requested delayed status\n",
1849 __func__, intf, f->name);
1850 cdev->delayed_status++;
1851 DBG(cdev, "delayed_status count %d\n",
1852 cdev->delayed_status);
1853 }
1854 spin_unlock(&cdev->lock);
1855 break;
1856 case USB_REQ_GET_INTERFACE:
1857 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1858 goto unknown;
1859 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1860 break;
1861 f = cdev->config->interface[intf];
1862 if (!f)
1863 break;
1864
1865 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1866 if (value < 0)
1867 break;
1868 *((u8 *)req->buf) = value;
1869 value = min(w_length, (u16) 1);
1870 break;
1871 case USB_REQ_GET_STATUS:
1872 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1873 (w_index == OTG_STS_SELECTOR)) {
1874 if (ctrl->bRequestType != (USB_DIR_IN |
1875 USB_RECIP_DEVICE))
1876 goto unknown;
1877 *((u8 *)req->buf) = gadget->host_request_flag;
1878 value = 1;
1879 break;
1880 }
1881
1882
1883
1884
1885
1886
1887
1888
1889 if (!gadget_is_superspeed(gadget))
1890 goto unknown;
1891 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1892 goto unknown;
1893 value = 2;
1894 put_unaligned_le16(0, req->buf);
1895 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1896 break;
1897 f = cdev->config->interface[intf];
1898 if (!f)
1899 break;
1900 status = f->get_status ? f->get_status(f) : 0;
1901 if (status < 0)
1902 break;
1903 put_unaligned_le16(status & 0x0000ffff, req->buf);
1904 break;
1905
1906
1907
1908
1909
1910 case USB_REQ_CLEAR_FEATURE:
1911 case USB_REQ_SET_FEATURE:
1912 if (!gadget_is_superspeed(gadget))
1913 goto unknown;
1914 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1915 goto unknown;
1916 switch (w_value) {
1917 case USB_INTRF_FUNC_SUSPEND:
1918 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1919 break;
1920 f = cdev->config->interface[intf];
1921 if (!f)
1922 break;
1923 value = 0;
1924 if (f->func_suspend)
1925 value = f->func_suspend(f, w_index >> 8);
1926 if (value < 0) {
1927 ERROR(cdev,
1928 "func_suspend() returned error %d\n",
1929 value);
1930 value = 0;
1931 }
1932 break;
1933 }
1934 break;
1935 default:
1936unknown:
1937
1938
1939
1940 if (cdev->use_os_string && cdev->os_desc_config &&
1941 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
1942 ctrl->bRequest == cdev->b_vendor_code) {
1943 struct usb_configuration *os_desc_cfg;
1944 u8 *buf;
1945 int interface;
1946 int count = 0;
1947
1948 req = cdev->os_desc_req;
1949 req->context = cdev;
1950 req->complete = composite_setup_complete;
1951 buf = req->buf;
1952 os_desc_cfg = cdev->os_desc_config;
1953 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
1954 memset(buf, 0, w_length);
1955 buf[5] = 0x01;
1956 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1957 case USB_RECIP_DEVICE:
1958 if (w_index != 0x4 || (w_value >> 8))
1959 break;
1960 buf[6] = w_index;
1961
1962 count = count_ext_compat(os_desc_cfg);
1963 buf[8] = count;
1964 count *= 24;
1965 count += 16;
1966 put_unaligned_le32(count, buf);
1967 value = w_length;
1968 if (w_length > 0x10) {
1969 value = fill_ext_compat(os_desc_cfg, buf);
1970 value = min_t(u16, w_length, value);
1971 }
1972 break;
1973 case USB_RECIP_INTERFACE:
1974 if (w_index != 0x5 || (w_value >> 8))
1975 break;
1976 interface = w_value & 0xFF;
1977 buf[6] = w_index;
1978 count = count_ext_prop(os_desc_cfg,
1979 interface);
1980 put_unaligned_le16(count, buf + 8);
1981 count = len_ext_prop(os_desc_cfg,
1982 interface);
1983 put_unaligned_le32(count, buf);
1984 value = w_length;
1985 if (w_length > 0x0A) {
1986 value = fill_ext_prop(os_desc_cfg,
1987 interface, buf);
1988 if (value >= 0)
1989 value = min_t(u16, w_length, value);
1990 }
1991 break;
1992 }
1993
1994 goto check_value;
1995 }
1996
1997 VDBG(cdev,
1998 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1999 ctrl->bRequestType, ctrl->bRequest,
2000 w_value, w_index, w_length);
2001
2002
2003
2004
2005
2006 if (cdev->config) {
2007 list_for_each_entry(f, &cdev->config->functions, list)
2008 if (f->req_match &&
2009 f->req_match(f, ctrl, false))
2010 goto try_fun_setup;
2011 } else {
2012 struct usb_configuration *c;
2013 list_for_each_entry(c, &cdev->configs, list)
2014 list_for_each_entry(f, &c->functions, list)
2015 if (f->req_match &&
2016 f->req_match(f, ctrl, true))
2017 goto try_fun_setup;
2018 }
2019 f = NULL;
2020
2021 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2022 case USB_RECIP_INTERFACE:
2023 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
2024 break;
2025 f = cdev->config->interface[intf];
2026 break;
2027
2028 case USB_RECIP_ENDPOINT:
2029 if (!cdev->config)
2030 break;
2031 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2032 list_for_each_entry(f, &cdev->config->functions, list) {
2033 if (test_bit(endp, f->endpoints))
2034 break;
2035 }
2036 if (&f->list == &cdev->config->functions)
2037 f = NULL;
2038 break;
2039 }
2040try_fun_setup:
2041 if (f && f->setup)
2042 value = f->setup(f, ctrl);
2043 else {
2044 struct usb_configuration *c;
2045
2046 c = cdev->config;
2047 if (!c)
2048 goto done;
2049
2050
2051 if (c->setup) {
2052 value = c->setup(c, ctrl);
2053 goto done;
2054 }
2055
2056
2057 if (!list_is_singular(&c->functions))
2058 goto done;
2059 f = list_first_entry(&c->functions, struct usb_function,
2060 list);
2061 if (f->setup)
2062 value = f->setup(f, ctrl);
2063 }
2064
2065 goto done;
2066 }
2067
2068check_value:
2069
2070 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
2071 req->length = value;
2072 req->context = cdev;
2073 req->zero = value < w_length;
2074 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
2075 if (value < 0) {
2076 DBG(cdev, "ep_queue --> %d\n", value);
2077 req->status = 0;
2078 composite_setup_complete(gadget->ep0, req);
2079 }
2080 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2081 WARN(cdev,
2082 "%s: Delayed status not supported for w_length != 0",
2083 __func__);
2084 }
2085
2086done:
2087
2088 return value;
2089}
2090
2091static void __composite_disconnect(struct usb_gadget *gadget)
2092{
2093 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2094 unsigned long flags;
2095
2096
2097
2098
2099 spin_lock_irqsave(&cdev->lock, flags);
2100 cdev->suspended = 0;
2101 if (cdev->config)
2102 reset_config(cdev);
2103 if (cdev->driver->disconnect)
2104 cdev->driver->disconnect(cdev);
2105 spin_unlock_irqrestore(&cdev->lock, flags);
2106}
2107
2108void composite_disconnect(struct usb_gadget *gadget)
2109{
2110 usb_gadget_vbus_draw(gadget, 0);
2111 __composite_disconnect(gadget);
2112}
2113
2114void composite_reset(struct usb_gadget *gadget)
2115{
2116
2117
2118
2119
2120
2121 usb_gadget_vbus_draw(gadget, 100);
2122 __composite_disconnect(gadget);
2123}
2124
2125
2126
2127static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2128 char *buf)
2129{
2130 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2131 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2132
2133 return sprintf(buf, "%d\n", cdev->suspended);
2134}
2135static DEVICE_ATTR_RO(suspended);
2136
2137static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
2138{
2139 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2140 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2141 struct usb_string *dev_str = gstr->strings;
2142
2143
2144
2145
2146
2147
2148 WARN_ON(cdev->config);
2149
2150 while (!list_empty(&cdev->configs)) {
2151 struct usb_configuration *c;
2152 c = list_first_entry(&cdev->configs,
2153 struct usb_configuration, list);
2154 remove_config(cdev, c);
2155 }
2156 if (cdev->driver->unbind && unbind_driver)
2157 cdev->driver->unbind(cdev);
2158
2159 composite_dev_cleanup(cdev);
2160
2161 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2162 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2163
2164 kfree(cdev->def_manufacturer);
2165 kfree(cdev);
2166 set_gadget_data(gadget, NULL);
2167}
2168
2169static void composite_unbind(struct usb_gadget *gadget)
2170{
2171 __composite_unbind(gadget, true);
2172}
2173
2174static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2175 const struct usb_device_descriptor *old)
2176{
2177 __le16 idVendor;
2178 __le16 idProduct;
2179 __le16 bcdDevice;
2180 u8 iSerialNumber;
2181 u8 iManufacturer;
2182 u8 iProduct;
2183
2184
2185
2186
2187
2188 idVendor = new->idVendor;
2189 idProduct = new->idProduct;
2190 bcdDevice = new->bcdDevice;
2191 iSerialNumber = new->iSerialNumber;
2192 iManufacturer = new->iManufacturer;
2193 iProduct = new->iProduct;
2194
2195 *new = *old;
2196 if (idVendor)
2197 new->idVendor = idVendor;
2198 if (idProduct)
2199 new->idProduct = idProduct;
2200 if (bcdDevice)
2201 new->bcdDevice = bcdDevice;
2202 else
2203 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
2204 if (iSerialNumber)
2205 new->iSerialNumber = iSerialNumber;
2206 if (iManufacturer)
2207 new->iManufacturer = iManufacturer;
2208 if (iProduct)
2209 new->iProduct = iProduct;
2210}
2211
2212int composite_dev_prepare(struct usb_composite_driver *composite,
2213 struct usb_composite_dev *cdev)
2214{
2215 struct usb_gadget *gadget = cdev->gadget;
2216 int ret = -ENOMEM;
2217
2218
2219 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2220 if (!cdev->req)
2221 return -ENOMEM;
2222
2223 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2224 if (!cdev->req->buf)
2225 goto fail;
2226
2227 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2228 if (ret)
2229 goto fail_dev;
2230
2231 cdev->req->complete = composite_setup_complete;
2232 cdev->req->context = cdev;
2233 gadget->ep0->driver_data = cdev;
2234
2235 cdev->driver = composite;
2236
2237
2238
2239
2240
2241
2242 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2243 usb_gadget_set_selfpowered(gadget);
2244
2245
2246
2247
2248
2249 usb_ep_autoconfig_reset(gadget);
2250 return 0;
2251fail_dev:
2252 kfree(cdev->req->buf);
2253fail:
2254 usb_ep_free_request(gadget->ep0, cdev->req);
2255 cdev->req = NULL;
2256 return ret;
2257}
2258
2259int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2260 struct usb_ep *ep0)
2261{
2262 int ret = 0;
2263
2264 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2265 if (!cdev->os_desc_req) {
2266 ret = -ENOMEM;
2267 goto end;
2268 }
2269
2270 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2271 GFP_KERNEL);
2272 if (!cdev->os_desc_req->buf) {
2273 ret = -ENOMEM;
2274 usb_ep_free_request(ep0, cdev->os_desc_req);
2275 goto end;
2276 }
2277 cdev->os_desc_req->context = cdev;
2278 cdev->os_desc_req->complete = composite_setup_complete;
2279end:
2280 return ret;
2281}
2282
2283void composite_dev_cleanup(struct usb_composite_dev *cdev)
2284{
2285 struct usb_gadget_string_container *uc, *tmp;
2286 struct usb_ep *ep, *tmp_ep;
2287
2288 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2289 list_del(&uc->list);
2290 kfree(uc);
2291 }
2292 if (cdev->os_desc_req) {
2293 if (cdev->os_desc_pending)
2294 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2295
2296 kfree(cdev->os_desc_req->buf);
2297 cdev->os_desc_req->buf = NULL;
2298 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2299 cdev->os_desc_req = NULL;
2300 }
2301 if (cdev->req) {
2302 if (cdev->setup_pending)
2303 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2304
2305 kfree(cdev->req->buf);
2306 cdev->req->buf = NULL;
2307 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2308 cdev->req = NULL;
2309 }
2310 cdev->next_string_id = 0;
2311 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322 list_for_each_entry_safe(ep, tmp_ep,
2323 &cdev->gadget->ep_list, ep_list) {
2324 if (ep->ops->dispose)
2325 ep->ops->dispose(ep);
2326 }
2327}
2328
2329static int composite_bind(struct usb_gadget *gadget,
2330 struct usb_gadget_driver *gdriver)
2331{
2332 struct usb_composite_dev *cdev;
2333 struct usb_composite_driver *composite = to_cdriver(gdriver);
2334 int status = -ENOMEM;
2335
2336 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2337 if (!cdev)
2338 return status;
2339
2340 spin_lock_init(&cdev->lock);
2341 cdev->gadget = gadget;
2342 set_gadget_data(gadget, cdev);
2343 INIT_LIST_HEAD(&cdev->configs);
2344 INIT_LIST_HEAD(&cdev->gstrings);
2345
2346 status = composite_dev_prepare(composite, cdev);
2347 if (status)
2348 goto fail;
2349
2350
2351
2352
2353
2354 status = composite->bind(cdev);
2355 if (status < 0)
2356 goto fail;
2357
2358 if (cdev->use_os_string) {
2359 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2360 if (status)
2361 goto fail;
2362 }
2363
2364 update_unchanged_dev_desc(&cdev->desc, composite->dev);
2365
2366
2367 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2368 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2369
2370 INFO(cdev, "%s ready\n", composite->name);
2371 return 0;
2372
2373fail:
2374 __composite_unbind(gadget, false);
2375 return status;
2376}
2377
2378
2379
2380void composite_suspend(struct usb_gadget *gadget)
2381{
2382 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2383 struct usb_function *f;
2384
2385
2386
2387
2388 DBG(cdev, "suspend\n");
2389 if (cdev->config) {
2390 list_for_each_entry(f, &cdev->config->functions, list) {
2391 if (f->suspend)
2392 f->suspend(f);
2393 }
2394 }
2395 if (cdev->driver->suspend)
2396 cdev->driver->suspend(cdev);
2397
2398 cdev->suspended = 1;
2399
2400 usb_gadget_set_selfpowered(gadget);
2401 usb_gadget_vbus_draw(gadget, 2);
2402}
2403
2404void composite_resume(struct usb_gadget *gadget)
2405{
2406 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2407 struct usb_function *f;
2408 unsigned maxpower;
2409
2410
2411
2412
2413 DBG(cdev, "resume\n");
2414 if (cdev->driver->resume)
2415 cdev->driver->resume(cdev);
2416 if (cdev->config) {
2417 list_for_each_entry(f, &cdev->config->functions, list) {
2418 if (f->resume)
2419 f->resume(f);
2420 }
2421
2422 maxpower = cdev->config->MaxPower ?
2423 cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
2424 if (gadget->speed < USB_SPEED_SUPER)
2425 maxpower = min(maxpower, 500U);
2426 else
2427 maxpower = min(maxpower, 900U);
2428
2429 if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW)
2430 usb_gadget_clear_selfpowered(gadget);
2431
2432 usb_gadget_vbus_draw(gadget, maxpower);
2433 }
2434
2435 cdev->suspended = 0;
2436}
2437
2438
2439
2440static const struct usb_gadget_driver composite_driver_template = {
2441 .bind = composite_bind,
2442 .unbind = composite_unbind,
2443
2444 .setup = composite_setup,
2445 .reset = composite_reset,
2446 .disconnect = composite_disconnect,
2447
2448 .suspend = composite_suspend,
2449 .resume = composite_resume,
2450
2451 .driver = {
2452 .owner = THIS_MODULE,
2453 },
2454};
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472int usb_composite_probe(struct usb_composite_driver *driver)
2473{
2474 struct usb_gadget_driver *gadget_driver;
2475
2476 if (!driver || !driver->dev || !driver->bind)
2477 return -EINVAL;
2478
2479 if (!driver->name)
2480 driver->name = "composite";
2481
2482 driver->gadget_driver = composite_driver_template;
2483 gadget_driver = &driver->gadget_driver;
2484
2485 gadget_driver->function = (char *) driver->name;
2486 gadget_driver->driver.name = driver->name;
2487 gadget_driver->max_speed = driver->max_speed;
2488
2489 return usb_gadget_probe_driver(gadget_driver);
2490}
2491EXPORT_SYMBOL_GPL(usb_composite_probe);
2492
2493
2494
2495
2496
2497
2498
2499
2500void usb_composite_unregister(struct usb_composite_driver *driver)
2501{
2502 usb_gadget_unregister_driver(&driver->gadget_driver);
2503}
2504EXPORT_SYMBOL_GPL(usb_composite_unregister);
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2517{
2518 int value;
2519 struct usb_request *req = cdev->req;
2520 unsigned long flags;
2521
2522 DBG(cdev, "%s\n", __func__);
2523 spin_lock_irqsave(&cdev->lock, flags);
2524
2525 if (cdev->delayed_status == 0) {
2526 WARN(cdev, "%s: Unexpected call\n", __func__);
2527
2528 } else if (--cdev->delayed_status == 0) {
2529 DBG(cdev, "%s: Completing delayed status\n", __func__);
2530 req->length = 0;
2531 req->context = cdev;
2532 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
2533 if (value < 0) {
2534 DBG(cdev, "ep_queue --> %d\n", value);
2535 req->status = 0;
2536 composite_setup_complete(cdev->gadget->ep0, req);
2537 }
2538 }
2539
2540 spin_unlock_irqrestore(&cdev->lock, flags);
2541}
2542EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
2543
2544static char *composite_default_mfr(struct usb_gadget *gadget)
2545{
2546 return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
2547 init_utsname()->release, gadget->name);
2548}
2549
2550void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2551 struct usb_composite_overwrite *covr)
2552{
2553 struct usb_device_descriptor *desc = &cdev->desc;
2554 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2555 struct usb_string *dev_str = gstr->strings;
2556
2557 if (covr->idVendor)
2558 desc->idVendor = cpu_to_le16(covr->idVendor);
2559
2560 if (covr->idProduct)
2561 desc->idProduct = cpu_to_le16(covr->idProduct);
2562
2563 if (covr->bcdDevice)
2564 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
2565
2566 if (covr->serial_number) {
2567 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2568 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2569 }
2570 if (covr->manufacturer) {
2571 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2572 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
2573
2574 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2575 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2576 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2577 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
2578 }
2579
2580 if (covr->product) {
2581 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2582 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2583 }
2584}
2585EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
2586
2587MODULE_LICENSE("GPL");
2588MODULE_AUTHOR("David Brownell");
2589