hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/usb/core/usb.c
....@@ -19,9 +19,8 @@
1919 * just a collection of helper routines that implement the
2020 * generic USB things that the real drivers can use..
2121 *
22
- * Think of this as a "USB library" rather than anything else.
23
- * It should be considered a slave, with no callbacks. Callbacks
24
- * are evil.
22
+ * Think of this as a "USB library" rather than anything else,
23
+ * with no callbacks. Callbacks are evil.
2524 */
2625
2726 #include <linux/module.h>
....@@ -46,8 +45,7 @@
4645 #include <linux/mm.h>
4746 #include <linux/dma-mapping.h>
4847
49
-#include "usb.h"
50
-
48
+#include "hub.h"
5149
5250 const char *usbcore_name = "usbcore";
5351
....@@ -65,8 +63,8 @@
6563 EXPORT_SYMBOL_GPL(usb_disabled);
6664
6765 #ifdef CONFIG_PM
68
-static int usb_autosuspend_delay = 2; /* Default delay value,
69
- * in seconds */
66
+/* Default delay value, in seconds */
67
+static int usb_autosuspend_delay = CONFIG_USB_AUTOSUSPEND_DELAY;
7068 module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
7169 MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
7270
....@@ -210,6 +208,82 @@
210208 EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse);
211209
212210 /**
211
+ * usb_find_endpoint() - Given an endpoint address, search for the endpoint's
212
+ * usb_host_endpoint structure in an interface's current altsetting.
213
+ * @intf: the interface whose current altsetting should be searched
214
+ * @ep_addr: the endpoint address (number and direction) to find
215
+ *
216
+ * Search the altsetting's list of endpoints for one with the specified address.
217
+ *
218
+ * Return: Pointer to the usb_host_endpoint if found, %NULL otherwise.
219
+ */
220
+static const struct usb_host_endpoint *usb_find_endpoint(
221
+ const struct usb_interface *intf, unsigned int ep_addr)
222
+{
223
+ int n;
224
+ const struct usb_host_endpoint *ep;
225
+
226
+ n = intf->cur_altsetting->desc.bNumEndpoints;
227
+ ep = intf->cur_altsetting->endpoint;
228
+ for (; n > 0; (--n, ++ep)) {
229
+ if (ep->desc.bEndpointAddress == ep_addr)
230
+ return ep;
231
+ }
232
+ return NULL;
233
+}
234
+
235
+/**
236
+ * usb_check_bulk_endpoints - Check whether an interface's current altsetting
237
+ * contains a set of bulk endpoints with the given addresses.
238
+ * @intf: the interface whose current altsetting should be searched
239
+ * @ep_addrs: 0-terminated array of the endpoint addresses (number and
240
+ * direction) to look for
241
+ *
242
+ * Search for endpoints with the specified addresses and check their types.
243
+ *
244
+ * Return: %true if all the endpoints are found and are bulk, %false otherwise.
245
+ */
246
+bool usb_check_bulk_endpoints(
247
+ const struct usb_interface *intf, const u8 *ep_addrs)
248
+{
249
+ const struct usb_host_endpoint *ep;
250
+
251
+ for (; *ep_addrs; ++ep_addrs) {
252
+ ep = usb_find_endpoint(intf, *ep_addrs);
253
+ if (!ep || !usb_endpoint_xfer_bulk(&ep->desc))
254
+ return false;
255
+ }
256
+ return true;
257
+}
258
+EXPORT_SYMBOL_GPL(usb_check_bulk_endpoints);
259
+
260
+/**
261
+ * usb_check_int_endpoints - Check whether an interface's current altsetting
262
+ * contains a set of interrupt endpoints with the given addresses.
263
+ * @intf: the interface whose current altsetting should be searched
264
+ * @ep_addrs: 0-terminated array of the endpoint addresses (number and
265
+ * direction) to look for
266
+ *
267
+ * Search for endpoints with the specified addresses and check their types.
268
+ *
269
+ * Return: %true if all the endpoints are found and are interrupt,
270
+ * %false otherwise.
271
+ */
272
+bool usb_check_int_endpoints(
273
+ const struct usb_interface *intf, const u8 *ep_addrs)
274
+{
275
+ const struct usb_host_endpoint *ep;
276
+
277
+ for (; *ep_addrs; ++ep_addrs) {
278
+ ep = usb_find_endpoint(intf, *ep_addrs);
279
+ if (!ep || !usb_endpoint_xfer_int(&ep->desc))
280
+ return false;
281
+ }
282
+ return true;
283
+}
284
+EXPORT_SYMBOL_GPL(usb_check_int_endpoints);
285
+
286
+/**
213287 * usb_find_alt_setting() - Given a configuration, find the alternate setting
214288 * for the given interface.
215289 * @config: the configuration to search (not necessarily the current config).
....@@ -326,9 +400,9 @@
326400 struct device_driver *drv;
327401 };
328402
329
-static int __find_interface(struct device *dev, void *data)
403
+static int __find_interface(struct device *dev, const void *data)
330404 {
331
- struct find_interface_arg *arg = data;
405
+ const struct find_interface_arg *arg = data;
332406 struct usb_interface *intf;
333407
334408 if (!is_usb_interface(dev))
....@@ -536,6 +610,27 @@
536610 return hcd->wireless;
537611 }
538612
613
+static bool usb_dev_authorized(struct usb_device *dev, struct usb_hcd *hcd)
614
+{
615
+ struct usb_hub *hub;
616
+
617
+ if (!dev->parent)
618
+ return true; /* Root hub always ok [and always wired] */
619
+
620
+ switch (hcd->dev_policy) {
621
+ case USB_DEVICE_AUTHORIZE_NONE:
622
+ default:
623
+ return false;
624
+
625
+ case USB_DEVICE_AUTHORIZE_ALL:
626
+ return true;
627
+
628
+ case USB_DEVICE_AUTHORIZE_INTERNAL:
629
+ hub = usb_hub_to_struct_hub(dev->parent);
630
+ return hub->ports[dev->portnum - 1]->connect_type ==
631
+ USB_PORT_CONNECT_TYPE_HARD_WIRED;
632
+ }
633
+}
539634
540635 /**
541636 * usb_alloc_dev - usb device constructor (usbcore-internal)
....@@ -580,18 +675,6 @@
580675 dev->dev.bus = &usb_bus_type;
581676 dev->dev.type = &usb_device_type;
582677 dev->dev.groups = usb_device_groups;
583
- /*
584
- * Fake a dma_mask/offset for the USB device:
585
- * We cannot really use the dma-mapping API (dma_alloc_* and
586
- * dma_map_*) for USB devices but instead need to use
587
- * usb_alloc_coherent and pass data in 'urb's, but some subsystems
588
- * manually look into the mask/offset pair to determine whether
589
- * they need bounce buffers.
590
- * Note: calling dma_set_mask() on a USB device would set the
591
- * mask for the entire HCD, so don't do that.
592
- */
593
- dev->dev.dma_mask = bus->sysdev->dma_mask;
594
- dev->dev.dma_pfn_offset = bus->sysdev->dma_pfn_offset;
595678 set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
596679 dev->state = USB_STATE_ATTACHED;
597680 dev->lpm_disable_count = 1;
....@@ -663,12 +746,11 @@
663746 dev->connect_time = jiffies;
664747 dev->active_duration = -jiffies;
665748 #endif
666
- if (root_hub) /* Root hub always ok [and always wired] */
667
- dev->authorized = 1;
668
- else {
669
- dev->authorized = !!HCD_DEV_AUTHORIZED(usb_hcd);
749
+
750
+ dev->authorized = usb_dev_authorized(dev, usb_hcd);
751
+ if (!root_hub)
670752 dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
671
- }
753
+
672754 return dev;
673755 }
674756 EXPORT_SYMBOL_GPL(usb_alloc_dev);
....@@ -741,6 +823,38 @@
741823 put_device(&intf->dev);
742824 }
743825 EXPORT_SYMBOL_GPL(usb_put_intf);
826
+
827
+/**
828
+ * usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint
829
+ * @intf: the usb interface
830
+ *
831
+ * While a USB device cannot perform DMA operations by itself, many USB
832
+ * controllers can. A call to usb_intf_get_dma_device() returns the DMA endpoint
833
+ * for the given USB interface, if any. The returned device structure must be
834
+ * released with put_device().
835
+ *
836
+ * See also usb_get_dma_device().
837
+ *
838
+ * Returns: A reference to the usb interface's DMA endpoint; or NULL if none
839
+ * exists.
840
+ */
841
+struct device *usb_intf_get_dma_device(struct usb_interface *intf)
842
+{
843
+ struct usb_device *udev = interface_to_usbdev(intf);
844
+ struct device *dmadev;
845
+
846
+ if (!udev->bus)
847
+ return NULL;
848
+
849
+ dmadev = get_device(udev->bus->sysdev);
850
+ if (!dmadev || !dmadev->dma_mask) {
851
+ put_device(dmadev);
852
+ return NULL;
853
+ }
854
+
855
+ return dmadev;
856
+}
857
+EXPORT_SYMBOL_GPL(usb_intf_get_dma_device);
744858
745859 /* USB device locking
746860 *
....@@ -824,63 +938,6 @@
824938 return usb_hcd_get_frame_number(dev);
825939 }
826940 EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
827
-
828
-int usb_sec_event_ring_setup(struct usb_device *dev,
829
- unsigned int intr_num)
830
-{
831
- if (dev->state == USB_STATE_NOTATTACHED)
832
- return 0;
833
-
834
- return usb_hcd_sec_event_ring_setup(dev, intr_num);
835
-}
836
-EXPORT_SYMBOL(usb_sec_event_ring_setup);
837
-
838
-int usb_sec_event_ring_cleanup(struct usb_device *dev,
839
- unsigned int intr_num)
840
-{
841
- return usb_hcd_sec_event_ring_cleanup(dev, intr_num);
842
-}
843
-EXPORT_SYMBOL(usb_sec_event_ring_cleanup);
844
-
845
-phys_addr_t
846
-usb_get_sec_event_ring_phys_addr(struct usb_device *dev,
847
- unsigned int intr_num, dma_addr_t *dma)
848
-{
849
- if (dev->state == USB_STATE_NOTATTACHED)
850
- return 0;
851
-
852
- return usb_hcd_get_sec_event_ring_phys_addr(dev, intr_num, dma);
853
-}
854
-EXPORT_SYMBOL_GPL(usb_get_sec_event_ring_phys_addr);
855
-
856
-phys_addr_t usb_get_xfer_ring_phys_addr(struct usb_device *dev,
857
- struct usb_host_endpoint *ep, dma_addr_t *dma)
858
-{
859
- if (dev->state == USB_STATE_NOTATTACHED)
860
- return 0;
861
-
862
- return usb_hcd_get_xfer_ring_phys_addr(dev, ep, dma);
863
-}
864
-EXPORT_SYMBOL_GPL(usb_get_xfer_ring_phys_addr);
865
-
866
-/**
867
- * usb_get_controller_id - returns the host controller id.
868
- * @dev: the device whose host controller id is being queried.
869
- */
870
-int usb_get_controller_id(struct usb_device *dev)
871
-{
872
- if (dev->state == USB_STATE_NOTATTACHED)
873
- return -EINVAL;
874
-
875
- return usb_hcd_get_controller_id(dev);
876
-}
877
-EXPORT_SYMBOL_GPL(usb_get_controller_id);
878
-
879
-int usb_stop_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
880
-{
881
- return usb_hcd_stop_endpoint(dev, ep);
882
-}
883
-EXPORT_SYMBOL_GPL(usb_stop_endpoint);
884941
885942 /*-------------------------------------------------------------------*/
886943 /*
....@@ -971,228 +1028,6 @@
9711028 }
9721029 EXPORT_SYMBOL_GPL(usb_free_coherent);
9731030
974
-/**
975
- * usb_buffer_map - create DMA mapping(s) for an urb
976
- * @urb: urb whose transfer_buffer/setup_packet will be mapped
977
- *
978
- * URB_NO_TRANSFER_DMA_MAP is added to urb->transfer_flags if the operation
979
- * succeeds. If the device is connected to this system through a non-DMA
980
- * controller, this operation always succeeds.
981
- *
982
- * This call would normally be used for an urb which is reused, perhaps
983
- * as the target of a large periodic transfer, with usb_buffer_dmasync()
984
- * calls to synchronize memory and dma state.
985
- *
986
- * Reverse the effect of this call with usb_buffer_unmap().
987
- *
988
- * Return: Either %NULL (indicating no buffer could be mapped), or @urb.
989
- *
990
- */
991
-#if 0
992
-struct urb *usb_buffer_map(struct urb *urb)
993
-{
994
- struct usb_bus *bus;
995
- struct device *controller;
996
-
997
- if (!urb
998
- || !urb->dev
999
- || !(bus = urb->dev->bus)
1000
- || !(controller = bus->sysdev))
1001
- return NULL;
1002
-
1003
- if (controller->dma_mask) {
1004
- urb->transfer_dma = dma_map_single(controller,
1005
- urb->transfer_buffer, urb->transfer_buffer_length,
1006
- usb_pipein(urb->pipe)
1007
- ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1008
- /* FIXME generic api broken like pci, can't report errors */
1009
- /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
1010
- } else
1011
- urb->transfer_dma = ~0;
1012
- urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1013
- return urb;
1014
-}
1015
-EXPORT_SYMBOL_GPL(usb_buffer_map);
1016
-#endif /* 0 */
1017
-
1018
-/* XXX DISABLED, no users currently. If you wish to re-enable this
1019
- * XXX please determine whether the sync is to transfer ownership of
1020
- * XXX the buffer from device to cpu or vice verse, and thusly use the
1021
- * XXX appropriate _for_{cpu,device}() method. -DaveM
1022
- */
1023
-#if 0
1024
-
1025
-/**
1026
- * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1027
- * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1028
- */
1029
-void usb_buffer_dmasync(struct urb *urb)
1030
-{
1031
- struct usb_bus *bus;
1032
- struct device *controller;
1033
-
1034
- if (!urb
1035
- || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1036
- || !urb->dev
1037
- || !(bus = urb->dev->bus)
1038
- || !(controller = bus->sysdev))
1039
- return;
1040
-
1041
- if (controller->dma_mask) {
1042
- dma_sync_single_for_cpu(controller,
1043
- urb->transfer_dma, urb->transfer_buffer_length,
1044
- usb_pipein(urb->pipe)
1045
- ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1046
- if (usb_pipecontrol(urb->pipe))
1047
- dma_sync_single_for_cpu(controller,
1048
- urb->setup_dma,
1049
- sizeof(struct usb_ctrlrequest),
1050
- DMA_TO_DEVICE);
1051
- }
1052
-}
1053
-EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
1054
-#endif
1055
-
1056
-/**
1057
- * usb_buffer_unmap - free DMA mapping(s) for an urb
1058
- * @urb: urb whose transfer_buffer will be unmapped
1059
- *
1060
- * Reverses the effect of usb_buffer_map().
1061
- */
1062
-#if 0
1063
-void usb_buffer_unmap(struct urb *urb)
1064
-{
1065
- struct usb_bus *bus;
1066
- struct device *controller;
1067
-
1068
- if (!urb
1069
- || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1070
- || !urb->dev
1071
- || !(bus = urb->dev->bus)
1072
- || !(controller = bus->sysdev))
1073
- return;
1074
-
1075
- if (controller->dma_mask) {
1076
- dma_unmap_single(controller,
1077
- urb->transfer_dma, urb->transfer_buffer_length,
1078
- usb_pipein(urb->pipe)
1079
- ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1080
- }
1081
- urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
1082
-}
1083
-EXPORT_SYMBOL_GPL(usb_buffer_unmap);
1084
-#endif /* 0 */
1085
-
1086
-#if 0
1087
-/**
1088
- * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1089
- * @dev: device to which the scatterlist will be mapped
1090
- * @is_in: mapping transfer direction
1091
- * @sg: the scatterlist to map
1092
- * @nents: the number of entries in the scatterlist
1093
- *
1094
- * Return: Either < 0 (indicating no buffers could be mapped), or the
1095
- * number of DMA mapping array entries in the scatterlist.
1096
- *
1097
- * Note:
1098
- * The caller is responsible for placing the resulting DMA addresses from
1099
- * the scatterlist into URB transfer buffer pointers, and for setting the
1100
- * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1101
- *
1102
- * Top I/O rates come from queuing URBs, instead of waiting for each one
1103
- * to complete before starting the next I/O. This is particularly easy
1104
- * to do with scatterlists. Just allocate and submit one URB for each DMA
1105
- * mapping entry returned, stopping on the first error or when all succeed.
1106
- * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1107
- *
1108
- * This call would normally be used when translating scatterlist requests,
1109
- * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1110
- * may be able to coalesce mappings for improved I/O efficiency.
1111
- *
1112
- * Reverse the effect of this call with usb_buffer_unmap_sg().
1113
- */
1114
-int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
1115
- struct scatterlist *sg, int nents)
1116
-{
1117
- struct usb_bus *bus;
1118
- struct device *controller;
1119
-
1120
- if (!dev
1121
- || !(bus = dev->bus)
1122
- || !(controller = bus->sysdev)
1123
- || !controller->dma_mask)
1124
- return -EINVAL;
1125
-
1126
- /* FIXME generic api broken like pci, can't report errors */
1127
- return dma_map_sg(controller, sg, nents,
1128
- is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE) ? : -ENOMEM;
1129
-}
1130
-EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
1131
-#endif
1132
-
1133
-/* XXX DISABLED, no users currently. If you wish to re-enable this
1134
- * XXX please determine whether the sync is to transfer ownership of
1135
- * XXX the buffer from device to cpu or vice verse, and thusly use the
1136
- * XXX appropriate _for_{cpu,device}() method. -DaveM
1137
- */
1138
-#if 0
1139
-
1140
-/**
1141
- * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1142
- * @dev: device to which the scatterlist will be mapped
1143
- * @is_in: mapping transfer direction
1144
- * @sg: the scatterlist to synchronize
1145
- * @n_hw_ents: the positive return value from usb_buffer_map_sg
1146
- *
1147
- * Use this when you are re-using a scatterlist's data buffers for
1148
- * another USB request.
1149
- */
1150
-void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
1151
- struct scatterlist *sg, int n_hw_ents)
1152
-{
1153
- struct usb_bus *bus;
1154
- struct device *controller;
1155
-
1156
- if (!dev
1157
- || !(bus = dev->bus)
1158
- || !(controller = bus->sysdev)
1159
- || !controller->dma_mask)
1160
- return;
1161
-
1162
- dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
1163
- is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1164
-}
1165
-EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
1166
-#endif
1167
-
1168
-#if 0
1169
-/**
1170
- * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1171
- * @dev: device to which the scatterlist will be mapped
1172
- * @is_in: mapping transfer direction
1173
- * @sg: the scatterlist to unmap
1174
- * @n_hw_ents: the positive return value from usb_buffer_map_sg
1175
- *
1176
- * Reverses the effect of usb_buffer_map_sg().
1177
- */
1178
-void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
1179
- struct scatterlist *sg, int n_hw_ents)
1180
-{
1181
- struct usb_bus *bus;
1182
- struct device *controller;
1183
-
1184
- if (!dev
1185
- || !(bus = dev->bus)
1186
- || !(controller = bus->sysdev)
1187
- || !controller->dma_mask)
1188
- return;
1189
-
1190
- dma_unmap_sg(controller, sg, n_hw_ents,
1191
- is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1192
-}
1193
-EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
1194
-#endif
1195
-
11961031 /*
11971032 * Notifications of device and interface registration
11981033 */
....@@ -1223,19 +1058,17 @@
12231058 .notifier_call = usb_bus_notify,
12241059 };
12251060
1226
-struct dentry *usb_debug_root;
1227
-EXPORT_SYMBOL_GPL(usb_debug_root);
1061
+static struct dentry *usb_devices_root;
12281062
12291063 static void usb_debugfs_init(void)
12301064 {
1231
- usb_debug_root = debugfs_create_dir("usb", NULL);
1232
- debugfs_create_file("devices", 0444, usb_debug_root, NULL,
1233
- &usbfs_devices_fops);
1065
+ usb_devices_root = debugfs_create_file("devices", 0444, usb_debug_root,
1066
+ NULL, &usbfs_devices_fops);
12341067 }
12351068
12361069 static void usb_debugfs_cleanup(void)
12371070 {
1238
- debugfs_remove_recursive(usb_debug_root);
1071
+ debugfs_remove(usb_devices_root);
12391072 }
12401073
12411074 /*