forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/usb/gadget/composite.c
....@@ -13,6 +13,7 @@
1313 #include <linux/module.h>
1414 #include <linux/device.h>
1515 #include <linux/utsname.h>
16
+#include <linux/bitfield.h>
1617
1718 #include <linux/usb/composite.h>
1819 #include <linux/usb/otg.h>
....@@ -72,17 +73,17 @@
7273 descriptors = f->ssp_descriptors;
7374 if (descriptors)
7475 break;
75
- /* FALLTHROUGH */
76
+ fallthrough;
7677 case USB_SPEED_SUPER:
7778 descriptors = f->ss_descriptors;
7879 if (descriptors)
7980 break;
80
- /* FALLTHROUGH */
81
+ fallthrough;
8182 case USB_SPEED_HIGH:
8283 descriptors = f->hs_descriptors;
8384 if (descriptors)
8485 break;
85
- /* FALLTHROUGH */
86
+ fallthrough;
8687 default:
8788 descriptors = f->fs_descriptors;
8889 }
....@@ -158,6 +159,8 @@
158159 int want_comp_desc = 0;
159160
160161 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
162
+ struct usb_composite_dev *cdev;
163
+ bool incomplete_desc = false;
161164
162165 if (!g || !f || !_ep)
163166 return -EIO;
....@@ -166,27 +169,42 @@
166169 switch (g->speed) {
167170 case USB_SPEED_SUPER_PLUS:
168171 if (gadget_is_superspeed_plus(g)) {
169
- speed_desc = f->ssp_descriptors;
170
- want_comp_desc = 1;
171
- break;
172
+ if (f->ssp_descriptors) {
173
+ speed_desc = f->ssp_descriptors;
174
+ want_comp_desc = 1;
175
+ break;
176
+ }
177
+ incomplete_desc = true;
172178 }
173
- /* fall through */
179
+ fallthrough;
174180 case USB_SPEED_SUPER:
175181 if (gadget_is_superspeed(g)) {
176
- speed_desc = f->ss_descriptors;
177
- want_comp_desc = 1;
178
- break;
182
+ if (f->ss_descriptors) {
183
+ speed_desc = f->ss_descriptors;
184
+ want_comp_desc = 1;
185
+ break;
186
+ }
187
+ incomplete_desc = true;
179188 }
180
- /* fall through */
189
+ fallthrough;
181190 case USB_SPEED_HIGH:
182191 if (gadget_is_dualspeed(g)) {
183
- speed_desc = f->hs_descriptors;
184
- break;
192
+ if (f->hs_descriptors) {
193
+ speed_desc = f->hs_descriptors;
194
+ break;
195
+ }
196
+ incomplete_desc = true;
185197 }
186
- /* fall through */
198
+ fallthrough;
187199 default:
188200 speed_desc = f->fs_descriptors;
189201 }
202
+
203
+ cdev = get_gadget_data(g);
204
+ if (incomplete_desc)
205
+ WARNING(cdev,
206
+ "%s doesn't hold the descriptors for current speed\n",
207
+ f->name);
190208
191209 /* find correct alternate setting descriptor */
192210 for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) {
....@@ -237,18 +255,14 @@
237255 case USB_ENDPOINT_XFER_ISOC:
238256 /* mult: bits 1:0 of bmAttributes */
239257 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
240
- /* fall through */
258
+ fallthrough;
241259 case USB_ENDPOINT_XFER_BULK:
242260 case USB_ENDPOINT_XFER_INT:
243261 _ep->maxburst = comp_desc->bMaxBurst + 1;
244262 break;
245263 default:
246
- if (comp_desc->bMaxBurst != 0) {
247
- struct usb_composite_dev *cdev;
248
-
249
- cdev = get_gadget_data(g);
264
+ if (comp_desc->bMaxBurst != 0)
250265 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
251
- }
252266 _ep->maxburst = 1;
253267 break;
254268 }
....@@ -476,122 +490,6 @@
476490 }
477491 EXPORT_SYMBOL_GPL(usb_interface_id);
478492
479
-static int usb_func_wakeup_int(struct usb_function *func)
480
-{
481
- int ret;
482
- struct usb_gadget *gadget;
483
-
484
- if (!func || !func->config || !func->config->cdev ||
485
- !func->config->cdev->gadget)
486
- return -EINVAL;
487
-
488
- pr_debug("%s - %s function wakeup\n",
489
- __func__, func->name ? func->name : "");
490
-
491
- gadget = func->config->cdev->gadget;
492
- if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
493
- DBG(func->config->cdev,
494
- "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
495
- gadget->speed,
496
- func->func_wakeup_allowed);
497
-
498
- return -ENOTSUPP;
499
- }
500
-
501
- ret = usb_gadget_func_wakeup(gadget, func->intf_id);
502
-
503
- return ret;
504
-}
505
-
506
-/**
507
- * usb_func_wakeup - wakes up a composite device function.
508
- * @func: composite device function to wake up.
509
- *
510
- * Returns 0 on success or a negative error value.
511
- */
512
-int usb_func_wakeup(struct usb_function *func)
513
-{
514
- int ret;
515
- unsigned long flags;
516
-
517
- if (!func || !func->config || !func->config->cdev)
518
- return -EINVAL;
519
-
520
- pr_debug("%s function wakeup\n",
521
- func->name ? func->name : "");
522
-
523
- spin_lock_irqsave(&func->config->cdev->lock, flags);
524
- ret = usb_func_wakeup_int(func);
525
- if (ret == -EAGAIN) {
526
- DBG(func->config->cdev,
527
- "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
528
- func->name ? func->name : "");
529
- ret = 0;
530
- } else if (ret < 0 && ret != -ENOTSUPP) {
531
- ERROR(func->config->cdev,
532
- "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
533
- func->name ? func->name : "", ret);
534
- }
535
-
536
- spin_unlock_irqrestore(&func->config->cdev->lock, flags);
537
- return ret;
538
-}
539
-EXPORT_SYMBOL_GPL(usb_func_wakeup);
540
-
541
-/**
542
- * usb_func_ep_queue - queues (submits) an I/O request to a function endpoint.
543
- * This function is similar to the usb_ep_queue function, but in addition it
544
- * also checks whether the function is in Super Speed USB Function Suspend
545
- * state, and if so a Function Wake notification is sent to the host
546
- * (USB 3.0 spec, section 9.2.5.2).
547
- * @func: the function which issues the USB I/O request.
548
- * @ep:the endpoint associated with the request
549
- * @req:the request being submitted
550
- * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
551
- * pre-allocate all necessary memory with the request.
552
- */
553
-int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
554
- struct usb_request *req, gfp_t gfp_flags)
555
-{
556
- int ret;
557
- struct usb_gadget *gadget;
558
-
559
- if (!func || !func->config || !func->config->cdev ||
560
- !func->config->cdev->gadget || !ep || !req) {
561
- ret = -EINVAL;
562
- goto done;
563
- }
564
-
565
- pr_debug("Function %s queueing new data into ep %u\n",
566
- func->name ? func->name : "", ep->address);
567
-
568
- gadget = func->config->cdev->gadget;
569
- if (func->func_is_suspended && func->func_wakeup_allowed) {
570
- ret = usb_gadget_func_wakeup(gadget, func->intf_id);
571
- if (ret == -EAGAIN) {
572
- pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
573
- func->name ? func->name : "");
574
- } else if (ret < 0 && ret != -ENOTSUPP) {
575
- pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
576
- func->name ? func->name : "", ret);
577
- }
578
- goto done;
579
- }
580
-
581
- if (!func->func_is_suspended)
582
- ret = 0;
583
-
584
- if (func->func_is_suspended && !func->func_wakeup_allowed) {
585
- ret = -ENOTSUPP;
586
- goto done;
587
- }
588
-
589
- ret = usb_ep_queue(ep, req, gfp_flags);
590
-done:
591
- return ret;
592
-}
593
-EXPORT_SYMBOL_GPL(usb_func_ep_queue);
594
-
595493 static u8 encode_bMaxPower(enum usb_device_speed speed,
596494 struct usb_configuration *c)
597495 {
....@@ -780,12 +678,36 @@
780678 struct usb_ext_cap_descriptor *usb_ext;
781679 struct usb_dcd_config_params dcd_config_params;
782680 struct usb_bos_descriptor *bos = cdev->req->buf;
681
+ unsigned int besl = 0;
783682
784683 bos->bLength = USB_DT_BOS_SIZE;
785684 bos->bDescriptorType = USB_DT_BOS;
786685
787686 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
788687 bos->bNumDeviceCaps = 0;
688
+
689
+ /* Get Controller configuration */
690
+ if (cdev->gadget->ops->get_config_params) {
691
+ cdev->gadget->ops->get_config_params(cdev->gadget,
692
+ &dcd_config_params);
693
+ } else {
694
+ dcd_config_params.besl_baseline =
695
+ USB_DEFAULT_BESL_UNSPECIFIED;
696
+ dcd_config_params.besl_deep =
697
+ USB_DEFAULT_BESL_UNSPECIFIED;
698
+ dcd_config_params.bU1devExitLat =
699
+ USB_DEFAULT_U1_DEV_EXIT_LAT;
700
+ dcd_config_params.bU2DevExitLat =
701
+ cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
702
+ }
703
+
704
+ if (dcd_config_params.besl_baseline != USB_DEFAULT_BESL_UNSPECIFIED)
705
+ besl = USB_BESL_BASELINE_VALID |
706
+ USB_SET_BESL_BASELINE(dcd_config_params.besl_baseline);
707
+
708
+ if (dcd_config_params.besl_deep != USB_DEFAULT_BESL_UNSPECIFIED)
709
+ besl |= USB_BESL_DEEP_VALID |
710
+ USB_SET_BESL_DEEP(dcd_config_params.besl_deep);
789711
790712 /*
791713 * A SuperSpeed device shall include the USB2.0 extension descriptor
....@@ -797,7 +719,8 @@
797719 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
798720 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
799721 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
800
- usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
722
+ usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT |
723
+ USB_BESL_SUPPORT | besl);
801724
802725 /*
803726 * The Superspeed USB Capability descriptor shall be implemented by all
....@@ -818,17 +741,6 @@
818741 USB_HIGH_SPEED_OPERATION |
819742 USB_5GBPS_OPERATION);
820743 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
821
-
822
- /* Get Controller configuration */
823
- if (cdev->gadget->ops->get_config_params) {
824
- cdev->gadget->ops->get_config_params(
825
- &dcd_config_params);
826
- } else {
827
- dcd_config_params.bU1devExitLat =
828
- USB_DEFAULT_U1_DEV_EXIT_LAT;
829
- dcd_config_params.bU2DevExitLat =
830
- cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
831
- }
832744 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
833745 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
834746 }
....@@ -836,47 +748,77 @@
836748 /* The SuperSpeedPlus USB Device Capability descriptor */
837749 if (gadget_is_superspeed_plus(cdev->gadget)) {
838750 struct usb_ssp_cap_descriptor *ssp_cap;
751
+ u8 ssac = 1;
752
+ u8 ssic;
753
+ int i;
754
+
755
+ if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x2)
756
+ ssac = 3;
757
+
758
+ /*
759
+ * Paired RX and TX sublink speed attributes share
760
+ * the same SSID.
761
+ */
762
+ ssic = (ssac + 1) / 2 - 1;
839763
840764 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
841765 bos->bNumDeviceCaps++;
842766
843
- /*
844
- * Report typical values.
845
- */
846
-
847
- le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
848
- ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
767
+ le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(ssac));
768
+ ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(ssac);
849769 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
850770 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
851771 ssp_cap->bReserved = 0;
852772 ssp_cap->wReserved = 0;
853773
854
- /* SSAC = 1 (2 attributes) */
855
- ssp_cap->bmAttributes = cpu_to_le32(1);
774
+ ssp_cap->bmAttributes =
775
+ cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_ATTRIBS, ssac) |
776
+ FIELD_PREP(USB_SSP_SUBLINK_SPEED_IDS, ssic));
856777
857
- /* Min RX/TX Lane Count = 1 */
858778 ssp_cap->wFunctionalitySupport =
859
- cpu_to_le16((1 << 8) | (1 << 12));
779
+ cpu_to_le16(FIELD_PREP(USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID, 0) |
780
+ FIELD_PREP(USB_SSP_MIN_RX_LANE_COUNT, 1) |
781
+ FIELD_PREP(USB_SSP_MIN_TX_LANE_COUNT, 1));
860782
861783 /*
862
- * bmSublinkSpeedAttr[0]:
863
- * ST = Symmetric, RX
864
- * LSE = 3 (Gbps)
865
- * LP = 1 (SuperSpeedPlus)
866
- * LSM = 10 (10 Gbps)
784
+ * Use 1 SSID if the gadget supports up to gen2x1 or not
785
+ * specified:
786
+ * - SSID 0 for symmetric RX/TX sublink speed of 10 Gbps.
787
+ *
788
+ * Use 1 SSID if the gadget supports up to gen1x2:
789
+ * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps.
790
+ *
791
+ * Use 2 SSIDs if the gadget supports up to gen2x2:
792
+ * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps.
793
+ * - SSID 1 for symmetric RX/TX sublink speed of 10 Gbps.
867794 */
868
- ssp_cap->bmSublinkSpeedAttr[0] =
869
- cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
870
- /*
871
- * bmSublinkSpeedAttr[1] =
872
- * ST = Symmetric, TX
873
- * LSE = 3 (Gbps)
874
- * LP = 1 (SuperSpeedPlus)
875
- * LSM = 10 (10 Gbps)
876
- */
877
- ssp_cap->bmSublinkSpeedAttr[1] =
878
- cpu_to_le32((3 << 4) | (1 << 14) |
879
- (0xa << 16) | (1 << 7));
795
+ for (i = 0; i < ssac + 1; i++) {
796
+ u8 ssid;
797
+ u8 mantissa;
798
+ u8 type;
799
+
800
+ ssid = i >> 1;
801
+
802
+ if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x1 ||
803
+ cdev->gadget->max_ssp_rate == USB_SSP_GEN_UNKNOWN)
804
+ mantissa = 10;
805
+ else
806
+ mantissa = 5 << ssid;
807
+
808
+ if (i % 2)
809
+ type = USB_SSP_SUBLINK_SPEED_ST_SYM_TX;
810
+ else
811
+ type = USB_SSP_SUBLINK_SPEED_ST_SYM_RX;
812
+
813
+ ssp_cap->bmSublinkSpeedAttr[i] =
814
+ cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_SSID, ssid) |
815
+ FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSE,
816
+ USB_SSP_SUBLINK_SPEED_LSE_GBPS) |
817
+ FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST, type) |
818
+ FIELD_PREP(USB_SSP_SUBLINK_SPEED_LP,
819
+ USB_SSP_SUBLINK_SPEED_LP_SSP) |
820
+ FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSM, mantissa));
821
+ }
880822 }
881823
882824 return le16_to_cpu(bos->wTotalLength);
....@@ -948,9 +890,9 @@
948890 result = 0;
949891 }
950892
951
- INFO(cdev, "%s config #%d: %s\n",
952
- usb_speed_string(gadget->speed),
953
- number, c ? c->label : "unconfigured");
893
+ DBG(cdev, "%s config #%d: %s\n",
894
+ usb_speed_string(gadget->speed),
895
+ number, c ? c->label : "unconfigured");
954896
955897 if (!c)
956898 goto done;
....@@ -1357,7 +1299,7 @@
13571299 EXPORT_SYMBOL_GPL(usb_string_id);
13581300
13591301 /**
1360
- * usb_string_ids() - allocate unused string IDs in batch
1302
+ * usb_string_ids_tab() - allocate unused string IDs in batch
13611303 * @cdev: the device whose string descriptor IDs are being allocated
13621304 * @str: an array of usb_string objects to assign numbers to
13631305 * Context: single threaded during gadget setup
....@@ -1821,7 +1763,7 @@
18211763 if (!gadget_is_dualspeed(gadget) ||
18221764 gadget->speed >= USB_SPEED_SUPER)
18231765 break;
1824
- /* FALLTHROUGH */
1766
+ fallthrough;
18251767 case USB_DT_CONFIG:
18261768 value = config_desc(cdev, w_value);
18271769 if (value >= 0)
....@@ -2163,7 +2105,7 @@
21632105 return value;
21642106 }
21652107
2166
-void composite_disconnect(struct usb_gadget *gadget)
2108
+static void __composite_disconnect(struct usb_gadget *gadget)
21672109 {
21682110 struct usb_composite_dev *cdev = get_gadget_data(gadget);
21692111 unsigned long flags;
....@@ -2178,6 +2120,23 @@
21782120 if (cdev->driver->disconnect)
21792121 cdev->driver->disconnect(cdev);
21802122 spin_unlock_irqrestore(&cdev->lock, flags);
2123
+}
2124
+
2125
+void composite_disconnect(struct usb_gadget *gadget)
2126
+{
2127
+ usb_gadget_vbus_draw(gadget, 0);
2128
+ __composite_disconnect(gadget);
2129
+}
2130
+
2131
+void composite_reset(struct usb_gadget *gadget)
2132
+{
2133
+ /*
2134
+ * Section 1.4.13 Standard Downstream Port of the USB battery charging
2135
+ * specification v1.2 states that a device connected on a SDP shall only
2136
+ * draw at max 100mA while in a connected, but unconfigured state.
2137
+ */
2138
+ usb_gadget_vbus_draw(gadget, 100);
2139
+ __composite_disconnect(gadget);
21812140 }
21822141
21832142 /*-------------------------------------------------------------------------*/
....@@ -2488,6 +2447,10 @@
24882447 usb_gadget_clear_selfpowered(gadget);
24892448
24902449 usb_gadget_vbus_draw(gadget, maxpower);
2450
+ } else {
2451
+ maxpower = CONFIG_USB_GADGET_VBUS_DRAW;
2452
+ maxpower = min(maxpower, 100U);
2453
+ usb_gadget_vbus_draw(gadget, maxpower);
24912454 }
24922455
24932456 cdev->suspended = 0;
....@@ -2500,7 +2463,7 @@
25002463 .unbind = composite_unbind,
25012464
25022465 .setup = composite_setup,
2503
- .reset = composite_disconnect,
2466
+ .reset = composite_reset,
25042467 .disconnect = composite_disconnect,
25052468
25062469 .suspend = composite_suspend,