| .. | .. |
|---|
| 5 | 5 | * Released under the GPLv2 only. |
|---|
| 6 | 6 | */ |
|---|
| 7 | 7 | |
|---|
| 8 | +#include <linux/acpi.h> |
|---|
| 8 | 9 | #include <linux/pci.h> /* for scatterlist macros */ |
|---|
| 9 | 10 | #include <linux/usb.h> |
|---|
| 10 | 11 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 160 | 161 | return ret; |
|---|
| 161 | 162 | } |
|---|
| 162 | 163 | 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); |
|---|
| 163 | 301 | |
|---|
| 164 | 302 | /** |
|---|
| 165 | 303 | * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion |
|---|
| .. | .. |
|---|
| 647 | 785 | int i; |
|---|
| 648 | 786 | int result; |
|---|
| 649 | 787 | |
|---|
| 788 | + if (size <= 0) /* No point in asking for no data */ |
|---|
| 789 | + return -EINVAL; |
|---|
| 790 | + |
|---|
| 650 | 791 | memset(buf, 0, size); /* Make sure we parse really received data */ |
|---|
| 651 | 792 | |
|---|
| 652 | 793 | for (i = 0; i < 3; ++i) { |
|---|
| .. | .. |
|---|
| 694 | 835 | { |
|---|
| 695 | 836 | int i; |
|---|
| 696 | 837 | int result; |
|---|
| 838 | + |
|---|
| 839 | + if (size <= 0) /* No point in asking for no data */ |
|---|
| 840 | + return -EINVAL; |
|---|
| 697 | 841 | |
|---|
| 698 | 842 | for (i = 0; i < 3; ++i) { |
|---|
| 699 | 843 | /* retry on length 0 or stall; some devices are flakey */ |
|---|
| .. | .. |
|---|
| 895 | 1039 | } |
|---|
| 896 | 1040 | |
|---|
| 897 | 1041 | /* |
|---|
| 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 |
|---|
| 901 | 1044 | * Context: !in_interrupt () |
|---|
| 902 | | - * |
|---|
| 903 | | - * Updates the copy of the device descriptor stored in the device structure, |
|---|
| 904 | | - * which dedicates space for this purpose. |
|---|
| 905 | 1045 | * |
|---|
| 906 | 1046 | * Not exported, only for use by the core. If drivers really want to read |
|---|
| 907 | 1047 | * the device descriptor directly, they can call usb_get_descriptor() with |
|---|
| 908 | 1048 | * type = USB_DT_DEVICE and index = 0. |
|---|
| 909 | 1049 | * |
|---|
| 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. |
|---|
| 914 | 1052 | */ |
|---|
| 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) |
|---|
| 916 | 1054 | { |
|---|
| 917 | 1055 | struct usb_device_descriptor *desc; |
|---|
| 918 | 1056 | int ret; |
|---|
| 919 | 1057 | |
|---|
| 920 | | - if (size > sizeof(*desc)) |
|---|
| 921 | | - return -EINVAL; |
|---|
| 922 | 1058 | desc = kmalloc(sizeof(*desc), GFP_NOIO); |
|---|
| 923 | 1059 | if (!desc) |
|---|
| 924 | | - return -ENOMEM; |
|---|
| 1060 | + return ERR_PTR(-ENOMEM); |
|---|
| 925 | 1061 | |
|---|
| 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 | + |
|---|
| 927 | 1066 | if (ret >= 0) |
|---|
| 928 | | - memcpy(&dev->descriptor, desc, size); |
|---|
| 1067 | + ret = -EMSGSIZE; |
|---|
| 929 | 1068 | kfree(desc); |
|---|
| 930 | | - return ret; |
|---|
| 1069 | + return ERR_PTR(ret); |
|---|
| 931 | 1070 | } |
|---|
| 932 | 1071 | |
|---|
| 933 | 1072 | /* |
|---|
| .. | .. |
|---|
| 947 | 1086 | if (dev->speed < USB_SPEED_SUPER) |
|---|
| 948 | 1087 | return 0; |
|---|
| 949 | 1088 | |
|---|
| 950 | | - return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
|---|
| 1089 | + return usb_control_msg_send(dev, 0, |
|---|
| 951 | 1090 | USB_REQ_SET_ISOCH_DELAY, |
|---|
| 952 | 1091 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
|---|
| 953 | 1092 | dev->hub_delay, 0, NULL, 0, |
|---|
| 954 | | - USB_CTRL_SET_TIMEOUT); |
|---|
| 1093 | + USB_CTRL_SET_TIMEOUT, |
|---|
| 1094 | + GFP_NOIO); |
|---|
| 955 | 1095 | } |
|---|
| 956 | 1096 | |
|---|
| 957 | 1097 | /** |
|---|
| .. | .. |
|---|
| 1069 | 1209 | * (like some ibmcam model 1 units) seem to expect hosts to make |
|---|
| 1070 | 1210 | * this request for iso endpoints, which can't halt! |
|---|
| 1071 | 1211 | */ |
|---|
| 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); |
|---|
| 1076 | 1216 | |
|---|
| 1077 | 1217 | /* don't un-halt or force to DATA0 except on success */ |
|---|
| 1078 | | - if (result < 0) |
|---|
| 1218 | + if (result) |
|---|
| 1079 | 1219 | return result; |
|---|
| 1080 | 1220 | |
|---|
| 1081 | 1221 | /* NOTE: seems like Microsoft and Apple don't bother verifying |
|---|
| .. | .. |
|---|
| 1244 | 1384 | */ |
|---|
| 1245 | 1385 | void usb_disable_device(struct usb_device *dev, int skip_ep0) |
|---|
| 1246 | 1386 | { |
|---|
| 1247 | | - int i, j; |
|---|
| 1248 | | - struct usb_hcd *hcd = bus_to_hcd(dev->bus); |
|---|
| 1387 | + int i; |
|---|
| 1249 | 1388 | |
|---|
| 1250 | 1389 | /* getting rid of interfaces will disconnect |
|---|
| 1251 | 1390 | * any drivers bound to them (a key side effect) |
|---|
| .. | .. |
|---|
| 1269 | 1408 | dev_dbg(&dev->dev, "unregistering interface %s\n", |
|---|
| 1270 | 1409 | dev_name(&interface->dev)); |
|---|
| 1271 | 1410 | 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 | | - |
|---|
| 1293 | 1411 | device_del(&interface->dev); |
|---|
| 1294 | 1412 | } |
|---|
| 1295 | 1413 | |
|---|
| .. | .. |
|---|
| 1459 | 1577 | if (dev->quirks & USB_QUIRK_NO_SET_INTF) |
|---|
| 1460 | 1578 | ret = -EPIPE; |
|---|
| 1461 | 1579 | 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); |
|---|
| 1465 | 1585 | |
|---|
| 1466 | 1586 | /* 9.4.10 says devices don't need this and are free to STALL the |
|---|
| 1467 | 1587 | * request if the interface only has one alternate setting. |
|---|
| .. | .. |
|---|
| 1471 | 1591 | "manual set_interface for iface %d, alt %d\n", |
|---|
| 1472 | 1592 | interface, alternate); |
|---|
| 1473 | 1593 | manual = 1; |
|---|
| 1474 | | - } else if (ret < 0) { |
|---|
| 1594 | + } else if (ret) { |
|---|
| 1475 | 1595 | /* Re-instate the old alt setting */ |
|---|
| 1476 | 1596 | usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting); |
|---|
| 1477 | 1597 | usb_enable_lpm(dev); |
|---|
| .. | .. |
|---|
| 1595 | 1715 | mutex_unlock(hcd->bandwidth_mutex); |
|---|
| 1596 | 1716 | return retval; |
|---|
| 1597 | 1717 | } |
|---|
| 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) { |
|---|
| 1603 | 1723 | usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL); |
|---|
| 1604 | 1724 | usb_enable_lpm(dev); |
|---|
| 1605 | 1725 | mutex_unlock(hcd->bandwidth_mutex); |
|---|
| .. | .. |
|---|
| 1963 | 2083 | intf->dev.of_node = usb_of_get_interface_node(dev, |
|---|
| 1964 | 2084 | configuration, ifnum); |
|---|
| 1965 | 2085 | } |
|---|
| 2086 | + ACPI_COMPANION_SET(&intf->dev, ACPI_COMPANION(&dev->dev)); |
|---|
| 1966 | 2087 | intf->dev.driver = NULL; |
|---|
| 1967 | 2088 | intf->dev.bus = &usb_bus_type; |
|---|
| 1968 | 2089 | intf->dev.type = &usb_if_device_type; |
|---|
| 1969 | 2090 | 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; |
|---|
| 1976 | 2091 | INIT_WORK(&intf->reset_ws, __usb_queue_reset_device); |
|---|
| 1977 | 2092 | intf->minor = -1; |
|---|
| 1978 | 2093 | device_initialize(&intf->dev); |
|---|
| .. | .. |
|---|
| 1983 | 2098 | } |
|---|
| 1984 | 2099 | kfree(new_interfaces); |
|---|
| 1985 | 2100 | |
|---|
| 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) { |
|---|
| 1990 | 2105 | /* |
|---|
| 1991 | 2106 | * All the old state is gone, so what else can we do? |
|---|
| 1992 | 2107 | * The device is probably useless now anyway. |
|---|
| .. | .. |
|---|
| 2030 | 2145 | for (i = 0; i < nintf; ++i) { |
|---|
| 2031 | 2146 | struct usb_interface *intf = cp->interface[i]; |
|---|
| 2032 | 2147 | |
|---|
| 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 | + |
|---|
| 2033 | 2155 | dev_dbg(&dev->dev, |
|---|
| 2034 | 2156 | "adding %s (config #%d, interface %d)\n", |
|---|
| 2035 | 2157 | dev_name(&intf->dev), configuration, |
|---|