hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/usb/core/message.c
....@@ -5,6 +5,7 @@
55 * Released under the GPLv2 only.
66 */
77
8
+#include <linux/acpi.h>
89 #include <linux/pci.h> /* for scatterlist macros */
910 #include <linux/usb.h>
1011 #include <linux/module.h>
....@@ -160,6 +161,143 @@
160161 return ret;
161162 }
162163 EXPORT_SYMBOL_GPL(usb_control_msg);
164
+
165
+/**
166
+ * usb_control_msg_send - Builds a control "send" message, sends it off and waits for completion
167
+ * @dev: pointer to the usb device to send the message to
168
+ * @endpoint: endpoint to send the message to
169
+ * @request: USB message request value
170
+ * @requesttype: USB message request type value
171
+ * @value: USB message value
172
+ * @index: USB message index value
173
+ * @driver_data: pointer to the data to send
174
+ * @size: length in bytes of the data to send
175
+ * @timeout: time in msecs to wait for the message to complete before timing
176
+ * out (if 0 the wait is forever)
177
+ * @memflags: the flags for memory allocation for buffers
178
+ *
179
+ * Context: !in_interrupt ()
180
+ *
181
+ * This function sends a control message to a specified endpoint that is not
182
+ * expected to fill in a response (i.e. a "send message") and waits for the
183
+ * message to complete, or timeout.
184
+ *
185
+ * Do not use this function from within an interrupt context. If you need
186
+ * an asynchronous message, or need to send a message from within interrupt
187
+ * context, use usb_submit_urb(). If a thread in your driver uses this call,
188
+ * make sure your disconnect() method can wait for it to complete. Since you
189
+ * don't have a handle on the URB used, you can't cancel the request.
190
+ *
191
+ * The data pointer can be made to a reference on the stack, or anywhere else,
192
+ * as it will not be modified at all. This does not have the restriction that
193
+ * usb_control_msg() has where the data pointer must be to dynamically allocated
194
+ * memory (i.e. memory that can be successfully DMAed to a device).
195
+ *
196
+ * Return: If successful, 0 is returned, Otherwise, a negative error number.
197
+ */
198
+int usb_control_msg_send(struct usb_device *dev, __u8 endpoint, __u8 request,
199
+ __u8 requesttype, __u16 value, __u16 index,
200
+ const void *driver_data, __u16 size, int timeout,
201
+ gfp_t memflags)
202
+{
203
+ unsigned int pipe = usb_sndctrlpipe(dev, endpoint);
204
+ int ret;
205
+ u8 *data = NULL;
206
+
207
+ if (usb_pipe_type_check(dev, pipe))
208
+ return -EINVAL;
209
+
210
+ if (size) {
211
+ data = kmemdup(driver_data, size, memflags);
212
+ if (!data)
213
+ return -ENOMEM;
214
+ }
215
+
216
+ ret = usb_control_msg(dev, pipe, request, requesttype, value, index,
217
+ data, size, timeout);
218
+ kfree(data);
219
+
220
+ if (ret < 0)
221
+ return ret;
222
+ if (ret == size)
223
+ return 0;
224
+ return -EINVAL;
225
+}
226
+EXPORT_SYMBOL_GPL(usb_control_msg_send);
227
+
228
+/**
229
+ * usb_control_msg_recv - Builds a control "receive" message, sends it off and waits for completion
230
+ * @dev: pointer to the usb device to send the message to
231
+ * @endpoint: endpoint to send the message to
232
+ * @request: USB message request value
233
+ * @requesttype: USB message request type value
234
+ * @value: USB message value
235
+ * @index: USB message index value
236
+ * @driver_data: pointer to the data to be filled in by the message
237
+ * @size: length in bytes of the data to be received
238
+ * @timeout: time in msecs to wait for the message to complete before timing
239
+ * out (if 0 the wait is forever)
240
+ * @memflags: the flags for memory allocation for buffers
241
+ *
242
+ * Context: !in_interrupt ()
243
+ *
244
+ * This function sends a control message to a specified endpoint that is
245
+ * expected to fill in a response (i.e. a "receive message") and waits for the
246
+ * message to complete, or timeout.
247
+ *
248
+ * Do not use this function from within an interrupt context. If you need
249
+ * an asynchronous message, or need to send a message from within interrupt
250
+ * context, use usb_submit_urb(). If a thread in your driver uses this call,
251
+ * make sure your disconnect() method can wait for it to complete. Since you
252
+ * don't have a handle on the URB used, you can't cancel the request.
253
+ *
254
+ * The data pointer can be made to a reference on the stack, or anywhere else
255
+ * that can be successfully written to. This function does not have the
256
+ * restriction that usb_control_msg() has where the data pointer must be to
257
+ * dynamically allocated memory (i.e. memory that can be successfully DMAed to a
258
+ * device).
259
+ *
260
+ * The "whole" message must be properly received from the device in order for
261
+ * this function to be successful. If a device returns less than the expected
262
+ * amount of data, then the function will fail. Do not use this for messages
263
+ * where a variable amount of data might be returned.
264
+ *
265
+ * Return: If successful, 0 is returned, Otherwise, a negative error number.
266
+ */
267
+int usb_control_msg_recv(struct usb_device *dev, __u8 endpoint, __u8 request,
268
+ __u8 requesttype, __u16 value, __u16 index,
269
+ void *driver_data, __u16 size, int timeout,
270
+ gfp_t memflags)
271
+{
272
+ unsigned int pipe = usb_rcvctrlpipe(dev, endpoint);
273
+ int ret;
274
+ u8 *data;
275
+
276
+ if (!size || !driver_data || usb_pipe_type_check(dev, pipe))
277
+ return -EINVAL;
278
+
279
+ data = kmalloc(size, memflags);
280
+ if (!data)
281
+ return -ENOMEM;
282
+
283
+ ret = usb_control_msg(dev, pipe, request, requesttype, value, index,
284
+ data, size, timeout);
285
+
286
+ if (ret < 0)
287
+ goto exit;
288
+
289
+ if (ret == size) {
290
+ memcpy(driver_data, data, size);
291
+ ret = 0;
292
+ } else {
293
+ ret = -EINVAL;
294
+ }
295
+
296
+exit:
297
+ kfree(data);
298
+ return ret;
299
+}
300
+EXPORT_SYMBOL_GPL(usb_control_msg_recv);
163301
164302 /**
165303 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
....@@ -647,6 +785,9 @@
647785 int i;
648786 int result;
649787
788
+ if (size <= 0) /* No point in asking for no data */
789
+ return -EINVAL;
790
+
650791 memset(buf, 0, size); /* Make sure we parse really received data */
651792
652793 for (i = 0; i < 3; ++i) {
....@@ -694,6 +835,9 @@
694835 {
695836 int i;
696837 int result;
838
+
839
+ if (size <= 0) /* No point in asking for no data */
840
+ return -EINVAL;
697841
698842 for (i = 0; i < 3; ++i) {
699843 /* retry on length 0 or stall; some devices are flakey */
....@@ -895,39 +1039,34 @@
8951039 }
8961040
8971041 /*
898
- * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
899
- * @dev: the device whose device descriptor is being updated
900
- * @size: how much of the descriptor to read
1042
+ * usb_get_device_descriptor - read the device descriptor
1043
+ * @udev: the device whose device descriptor should be read
9011044 * Context: !in_interrupt ()
902
- *
903
- * Updates the copy of the device descriptor stored in the device structure,
904
- * which dedicates space for this purpose.
9051045 *
9061046 * Not exported, only for use by the core. If drivers really want to read
9071047 * the device descriptor directly, they can call usb_get_descriptor() with
9081048 * type = USB_DT_DEVICE and index = 0.
9091049 *
910
- * This call is synchronous, and may not be used in an interrupt context.
911
- *
912
- * Return: The number of bytes received on success, or else the status code
913
- * returned by the underlying usb_control_msg() call.
1050
+ * Returns: a pointer to a dynamically allocated usb_device_descriptor
1051
+ * structure (which the caller must deallocate), or an ERR_PTR value.
9141052 */
915
-int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
1053
+struct usb_device_descriptor *usb_get_device_descriptor(struct usb_device *udev)
9161054 {
9171055 struct usb_device_descriptor *desc;
9181056 int ret;
9191057
920
- if (size > sizeof(*desc))
921
- return -EINVAL;
9221058 desc = kmalloc(sizeof(*desc), GFP_NOIO);
9231059 if (!desc)
924
- return -ENOMEM;
1060
+ return ERR_PTR(-ENOMEM);
9251061
926
- ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
1062
+ ret = usb_get_descriptor(udev, USB_DT_DEVICE, 0, desc, sizeof(*desc));
1063
+ if (ret == sizeof(*desc))
1064
+ return desc;
1065
+
9271066 if (ret >= 0)
928
- memcpy(&dev->descriptor, desc, size);
1067
+ ret = -EMSGSIZE;
9291068 kfree(desc);
930
- return ret;
1069
+ return ERR_PTR(ret);
9311070 }
9321071
9331072 /*
....@@ -947,11 +1086,12 @@
9471086 if (dev->speed < USB_SPEED_SUPER)
9481087 return 0;
9491088
950
- return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1089
+ return usb_control_msg_send(dev, 0,
9511090 USB_REQ_SET_ISOCH_DELAY,
9521091 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
9531092 dev->hub_delay, 0, NULL, 0,
954
- USB_CTRL_SET_TIMEOUT);
1093
+ USB_CTRL_SET_TIMEOUT,
1094
+ GFP_NOIO);
9551095 }
9561096
9571097 /**
....@@ -1069,13 +1209,13 @@
10691209 * (like some ibmcam model 1 units) seem to expect hosts to make
10701210 * this request for iso endpoints, which can't halt!
10711211 */
1072
- result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1073
- USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
1074
- USB_ENDPOINT_HALT, endp, NULL, 0,
1075
- USB_CTRL_SET_TIMEOUT);
1212
+ result = usb_control_msg_send(dev, 0,
1213
+ USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
1214
+ USB_ENDPOINT_HALT, endp, NULL, 0,
1215
+ USB_CTRL_SET_TIMEOUT, GFP_NOIO);
10761216
10771217 /* don't un-halt or force to DATA0 except on success */
1078
- if (result < 0)
1218
+ if (result)
10791219 return result;
10801220
10811221 /* NOTE: seems like Microsoft and Apple don't bother verifying
....@@ -1244,8 +1384,7 @@
12441384 */
12451385 void usb_disable_device(struct usb_device *dev, int skip_ep0)
12461386 {
1247
- int i, j;
1248
- struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1387
+ int i;
12491388
12501389 /* getting rid of interfaces will disconnect
12511390 * any drivers bound to them (a key side effect)
....@@ -1269,27 +1408,6 @@
12691408 dev_dbg(&dev->dev, "unregistering interface %s\n",
12701409 dev_name(&interface->dev));
12711410 remove_intf_ep_devs(interface);
1272
-
1273
- /*
1274
- * Some special SoCs (e.g. rk322xh) USB 3.0 module
1275
- * can't handle outstanding URBs by hardware when
1276
- * when USB 3.0 device disconnect, so we need to
1277
- * cancel all URBs pending on this device here.
1278
- *
1279
- * In addition, we just reuse the hub autosuspend
1280
- * quirk but not add a new quirk for this issue.
1281
- * Because it always occurs with autosuspend issue.
1282
- */
1283
- if (hcd->self.root_hub->quirks &
1284
- USB_QUIRK_AUTO_SUSPEND) {
1285
- for (j = skip_ep0; j < 16; ++j) {
1286
- usb_hcd_flush_endpoint(dev,
1287
- dev->ep_out[j]);
1288
- usb_hcd_flush_endpoint(dev,
1289
- dev->ep_in[j]);
1290
- }
1291
- }
1292
-
12931411 device_del(&interface->dev);
12941412 }
12951413
....@@ -1459,9 +1577,11 @@
14591577 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
14601578 ret = -EPIPE;
14611579 else
1462
- ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1463
- USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1464
- alternate, interface, NULL, 0, 5000);
1580
+ ret = usb_control_msg_send(dev, 0,
1581
+ USB_REQ_SET_INTERFACE,
1582
+ USB_RECIP_INTERFACE, alternate,
1583
+ interface, NULL, 0, 5000,
1584
+ GFP_NOIO);
14651585
14661586 /* 9.4.10 says devices don't need this and are free to STALL the
14671587 * request if the interface only has one alternate setting.
....@@ -1471,7 +1591,7 @@
14711591 "manual set_interface for iface %d, alt %d\n",
14721592 interface, alternate);
14731593 manual = 1;
1474
- } else if (ret < 0) {
1594
+ } else if (ret) {
14751595 /* Re-instate the old alt setting */
14761596 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
14771597 usb_enable_lpm(dev);
....@@ -1595,11 +1715,11 @@
15951715 mutex_unlock(hcd->bandwidth_mutex);
15961716 return retval;
15971717 }
1598
- retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1599
- USB_REQ_SET_CONFIGURATION, 0,
1600
- config->desc.bConfigurationValue, 0,
1601
- NULL, 0, USB_CTRL_SET_TIMEOUT);
1602
- if (retval < 0) {
1718
+ retval = usb_control_msg_send(dev, 0, USB_REQ_SET_CONFIGURATION, 0,
1719
+ config->desc.bConfigurationValue, 0,
1720
+ NULL, 0, USB_CTRL_SET_TIMEOUT,
1721
+ GFP_NOIO);
1722
+ if (retval) {
16031723 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
16041724 usb_enable_lpm(dev);
16051725 mutex_unlock(hcd->bandwidth_mutex);
....@@ -1963,16 +2083,11 @@
19632083 intf->dev.of_node = usb_of_get_interface_node(dev,
19642084 configuration, ifnum);
19652085 }
2086
+ ACPI_COMPANION_SET(&intf->dev, ACPI_COMPANION(&dev->dev));
19662087 intf->dev.driver = NULL;
19672088 intf->dev.bus = &usb_bus_type;
19682089 intf->dev.type = &usb_if_device_type;
19692090 intf->dev.groups = usb_interface_groups;
1970
- /*
1971
- * Please refer to usb_alloc_dev() to see why we set
1972
- * dma_mask and dma_pfn_offset.
1973
- */
1974
- intf->dev.dma_mask = dev->dev.dma_mask;
1975
- intf->dev.dma_pfn_offset = dev->dev.dma_pfn_offset;
19762091 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
19772092 intf->minor = -1;
19782093 device_initialize(&intf->dev);
....@@ -1983,10 +2098,10 @@
19832098 }
19842099 kfree(new_interfaces);
19852100
1986
- ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1987
- USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1988
- NULL, 0, USB_CTRL_SET_TIMEOUT);
1989
- if (ret < 0 && cp) {
2101
+ ret = usb_control_msg_send(dev, 0, USB_REQ_SET_CONFIGURATION, 0,
2102
+ configuration, 0, NULL, 0,
2103
+ USB_CTRL_SET_TIMEOUT, GFP_NOIO);
2104
+ if (ret && cp) {
19902105 /*
19912106 * All the old state is gone, so what else can we do?
19922107 * The device is probably useless now anyway.
....@@ -2030,6 +2145,13 @@
20302145 for (i = 0; i < nintf; ++i) {
20312146 struct usb_interface *intf = cp->interface[i];
20322147
2148
+ if (intf->dev.of_node &&
2149
+ !of_device_is_available(intf->dev.of_node)) {
2150
+ dev_info(&dev->dev, "skipping disabled interface %d\n",
2151
+ intf->cur_altsetting->desc.bInterfaceNumber);
2152
+ continue;
2153
+ }
2154
+
20332155 dev_dbg(&dev->dev,
20342156 "adding %s (config #%d, interface %d)\n",
20352157 dev_name(&intf->dev), configuration,