Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/usb/usb.c |
| 3 | * |
| 4 | * (C) Copyright Linus Torvalds 1999 |
| 5 | * (C) Copyright Johannes Erdfelt 1999-2001 |
| 6 | * (C) Copyright Andreas Gal 1999 |
| 7 | * (C) Copyright Gregory P. Smith 1999 |
| 8 | * (C) Copyright Deti Fliegl 1999 (new USB architecture) |
| 9 | * (C) Copyright Randy Dunlap 2000 |
| 10 | * (C) Copyright David Brownell 2000-2004 |
| 11 | * (C) Copyright Yggdrasil Computing, Inc. 2000 |
| 12 | * (usb_device_id matching changes by Adam J. Richter) |
| 13 | * (C) Copyright Greg Kroah-Hartman 2002-2003 |
| 14 | * |
| 15 | * NOTE! This is not actually a driver at all, rather this is |
| 16 | * just a collection of helper routines that implement the |
| 17 | * generic USB things that the real drivers can use.. |
| 18 | * |
| 19 | * Think of this as a "USB library" rather than anything else. |
| 20 | * It should be considered a slave, with no callbacks. Callbacks |
| 21 | * are evil. |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/interrupt.h> /* for in_interrupt() */ |
| 29 | #include <linux/kmod.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/usb.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/io.h> |
| 38 | #include <asm/scatterlist.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/dma-mapping.h> |
| 41 | |
| 42 | #include "hcd.h" |
| 43 | #include "usb.h" |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | const char *usbcore_name = "usbcore"; |
| 47 | |
| 48 | static int nousb; /* Disable USB when built into kernel image */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /** |
| 52 | * usb_ifnum_to_if - get the interface object with a given interface number |
| 53 | * @dev: the device whose current configuration is considered |
| 54 | * @ifnum: the desired interface |
| 55 | * |
| 56 | * This walks the device descriptor for the currently active configuration |
| 57 | * and returns a pointer to the interface with that particular interface |
| 58 | * number, or null. |
| 59 | * |
| 60 | * Note that configuration descriptors are not required to assign interface |
| 61 | * numbers sequentially, so that it would be incorrect to assume that |
| 62 | * the first interface in that descriptor corresponds to interface zero. |
| 63 | * This routine helps device drivers avoid such mistakes. |
| 64 | * However, you should make sure that you do the right thing with any |
| 65 | * alternate settings available for this interfaces. |
| 66 | * |
| 67 | * Don't call this function unless you are bound to one of the interfaces |
| 68 | * on this device or you have locked the device! |
| 69 | */ |
| 70 | struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum) |
| 71 | { |
| 72 | struct usb_host_config *config = dev->actconfig; |
| 73 | int i; |
| 74 | |
| 75 | if (!config) |
| 76 | return NULL; |
| 77 | for (i = 0; i < config->desc.bNumInterfaces; i++) |
| 78 | if (config->interface[i]->altsetting[0] |
| 79 | .desc.bInterfaceNumber == ifnum) |
| 80 | return config->interface[i]; |
| 81 | |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * usb_altnum_to_altsetting - get the altsetting structure with a given |
| 87 | * alternate setting number. |
| 88 | * @intf: the interface containing the altsetting in question |
| 89 | * @altnum: the desired alternate setting number |
| 90 | * |
| 91 | * This searches the altsetting array of the specified interface for |
| 92 | * an entry with the correct bAlternateSetting value and returns a pointer |
| 93 | * to that entry, or null. |
| 94 | * |
| 95 | * Note that altsettings need not be stored sequentially by number, so |
| 96 | * it would be incorrect to assume that the first altsetting entry in |
| 97 | * the array corresponds to altsetting zero. This routine helps device |
| 98 | * drivers avoid such mistakes. |
| 99 | * |
| 100 | * Don't call this function unless you are bound to the intf interface |
| 101 | * or you have locked the device! |
| 102 | */ |
| 103 | struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf, |
| 104 | unsigned int altnum) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | for (i = 0; i < intf->num_altsetting; i++) { |
| 109 | if (intf->altsetting[i].desc.bAlternateSetting == altnum) |
| 110 | return &intf->altsetting[i]; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * usb_driver_claim_interface - bind a driver to an interface |
| 117 | * @driver: the driver to be bound |
| 118 | * @iface: the interface to which it will be bound; must be in the |
| 119 | * usb device's active configuration |
| 120 | * @priv: driver data associated with that interface |
| 121 | * |
| 122 | * This is used by usb device drivers that need to claim more than one |
| 123 | * interface on a device when probing (audio and acm are current examples). |
| 124 | * No device driver should directly modify internal usb_interface or |
| 125 | * usb_device structure members. |
| 126 | * |
| 127 | * Few drivers should need to use this routine, since the most natural |
| 128 | * way to bind to an interface is to return the private data from |
| 129 | * the driver's probe() method. |
| 130 | * |
| 131 | * Callers must own the device lock and the driver model's usb_bus_type.subsys |
| 132 | * writelock. So driver probe() entries don't need extra locking, |
| 133 | * but other call contexts may need to explicitly claim those locks. |
| 134 | */ |
| 135 | int usb_driver_claim_interface(struct usb_driver *driver, |
| 136 | struct usb_interface *iface, void* priv) |
| 137 | { |
| 138 | struct device *dev = &iface->dev; |
| 139 | |
| 140 | if (dev->driver) |
| 141 | return -EBUSY; |
| 142 | |
| 143 | dev->driver = &driver->driver; |
| 144 | usb_set_intfdata(iface, priv); |
| 145 | iface->condition = USB_INTERFACE_BOUND; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 146 | mark_active(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* if interface was already added, bind now; else let |
| 149 | * the future device_add() bind it, bypassing probe() |
| 150 | */ |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 151 | if (device_is_registered(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | device_bind_driver(dev); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * usb_driver_release_interface - unbind a driver from an interface |
| 159 | * @driver: the driver to be unbound |
| 160 | * @iface: the interface from which it will be unbound |
| 161 | * |
| 162 | * This can be used by drivers to release an interface without waiting |
| 163 | * for their disconnect() methods to be called. In typical cases this |
| 164 | * also causes the driver disconnect() method to be called. |
| 165 | * |
| 166 | * This call is synchronous, and may not be used in an interrupt context. |
| 167 | * Callers must own the device lock and the driver model's usb_bus_type.subsys |
| 168 | * writelock. So driver disconnect() entries don't need extra locking, |
| 169 | * but other call contexts may need to explicitly claim those locks. |
| 170 | */ |
| 171 | void usb_driver_release_interface(struct usb_driver *driver, |
| 172 | struct usb_interface *iface) |
| 173 | { |
| 174 | struct device *dev = &iface->dev; |
| 175 | |
| 176 | /* this should never happen, don't release something that's not ours */ |
| 177 | if (!dev->driver || dev->driver != &driver->driver) |
| 178 | return; |
| 179 | |
Alan Stern | f409661 | 2005-05-06 15:41:08 -0400 | [diff] [blame] | 180 | /* don't release from within disconnect() */ |
| 181 | if (iface->condition != USB_INTERFACE_BOUND) |
| 182 | return; |
| 183 | |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 184 | /* don't release if the interface hasn't been added yet */ |
| 185 | if (device_is_registered(dev)) { |
Alan Stern | f409661 | 2005-05-06 15:41:08 -0400 | [diff] [blame] | 186 | iface->condition = USB_INTERFACE_UNBINDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | device_release_driver(dev); |
Alan Stern | f409661 | 2005-05-06 15:41:08 -0400 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | dev->driver = NULL; |
| 191 | usb_set_intfdata(iface, NULL); |
| 192 | iface->condition = USB_INTERFACE_UNBOUND; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 193 | mark_quiesced(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Pete Zaitcev | f5691d7 | 2005-12-21 17:24:54 -0800 | [diff] [blame] | 196 | struct find_interface_arg { |
| 197 | int minor; |
| 198 | struct usb_interface *interface; |
| 199 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 201 | static int __find_interface(struct device * dev, void * data) |
| 202 | { |
Pete Zaitcev | f5691d7 | 2005-12-21 17:24:54 -0800 | [diff] [blame] | 203 | struct find_interface_arg *arg = data; |
| 204 | struct usb_interface *intf; |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 205 | |
| 206 | /* can't look at usb devices, only interfaces */ |
| 207 | if (dev->driver == &usb_generic_driver) |
| 208 | return 0; |
| 209 | |
| 210 | intf = to_usb_interface(dev); |
Pete Zaitcev | f5691d7 | 2005-12-21 17:24:54 -0800 | [diff] [blame] | 211 | if (intf->minor != -1 && intf->minor == arg->minor) { |
| 212 | arg->interface = intf; |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 213 | return 1; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /** |
| 219 | * usb_find_interface - find usb_interface pointer for driver and device |
| 220 | * @drv: the driver whose current configuration is considered |
| 221 | * @minor: the minor number of the desired device |
| 222 | * |
| 223 | * This walks the driver device list and returns a pointer to the interface |
| 224 | * with the matching minor. Note, this only works for devices that share the |
| 225 | * USB major number. |
| 226 | */ |
| 227 | struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) |
| 228 | { |
Pete Zaitcev | f5691d7 | 2005-12-21 17:24:54 -0800 | [diff] [blame] | 229 | struct find_interface_arg argb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Pete Zaitcev | f5691d7 | 2005-12-21 17:24:54 -0800 | [diff] [blame] | 231 | argb.minor = minor; |
| 232 | argb.interface = NULL; |
| 233 | driver_for_each_device(&drv->driver, NULL, &argb, __find_interface); |
| 234 | return argb.interface; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_HOTPLUG |
| 238 | |
| 239 | /* |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 240 | * This sends an uevent to userspace, typically helping to load driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * or other modules, configure the device, and more. Drivers can provide |
| 242 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. |
| 243 | * |
| 244 | * We're called either from khubd (the typical case) or from root hub |
| 245 | * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle |
| 246 | * delays in event delivery. Use sysfs (and DEVPATH) to make sure the |
| 247 | * device (and this configuration!) are still present. |
| 248 | */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 249 | static int usb_uevent(struct device *dev, char **envp, int num_envp, |
| 250 | char *buffer, int buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | struct usb_interface *intf; |
| 253 | struct usb_device *usb_dev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 254 | struct usb_host_interface *alt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | int i = 0; |
| 256 | int length = 0; |
| 257 | |
| 258 | if (!dev) |
| 259 | return -ENODEV; |
| 260 | |
| 261 | /* driver is often null here; dev_dbg() would oops */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 262 | pr_debug ("usb %s: uevent\n", dev->bus_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | /* Must check driver_data here, as on remove driver is always NULL */ |
| 265 | if ((dev->driver == &usb_generic_driver) || |
| 266 | (dev->driver_data == &usb_generic_driver_data)) |
| 267 | return 0; |
| 268 | |
| 269 | intf = to_usb_interface(dev); |
| 270 | usb_dev = interface_to_usbdev (intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 271 | alt = intf->cur_altsetting; |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (usb_dev->devnum < 0) { |
| 274 | pr_debug ("usb %s: already deleted?\n", dev->bus_id); |
| 275 | return -ENODEV; |
| 276 | } |
| 277 | if (!usb_dev->bus) { |
| 278 | pr_debug ("usb %s: bus removed?\n", dev->bus_id); |
| 279 | return -ENODEV; |
| 280 | } |
| 281 | |
| 282 | #ifdef CONFIG_USB_DEVICEFS |
| 283 | /* If this is available, userspace programs can directly read |
| 284 | * all the device descriptors we don't tell them about. Or |
| 285 | * even act as usermode drivers. |
| 286 | * |
| 287 | * FIXME reduce hardwired intelligence here |
| 288 | */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 289 | if (add_uevent_var(envp, num_envp, &i, |
| 290 | buffer, buffer_size, &length, |
| 291 | "DEVICE=/proc/bus/usb/%03d/%03d", |
| 292 | usb_dev->bus->busnum, usb_dev->devnum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return -ENOMEM; |
| 294 | #endif |
| 295 | |
| 296 | /* per-device configurations are common */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 297 | if (add_uevent_var(envp, num_envp, &i, |
| 298 | buffer, buffer_size, &length, |
| 299 | "PRODUCT=%x/%x/%x", |
| 300 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 301 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 302 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return -ENOMEM; |
| 304 | |
| 305 | /* class-based driver binding models */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 306 | if (add_uevent_var(envp, num_envp, &i, |
| 307 | buffer, buffer_size, &length, |
| 308 | "TYPE=%d/%d/%d", |
| 309 | usb_dev->descriptor.bDeviceClass, |
| 310 | usb_dev->descriptor.bDeviceSubClass, |
| 311 | usb_dev->descriptor.bDeviceProtocol)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | return -ENOMEM; |
| 313 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 314 | if (add_uevent_var(envp, num_envp, &i, |
| 315 | buffer, buffer_size, &length, |
| 316 | "INTERFACE=%d/%d/%d", |
| 317 | alt->desc.bInterfaceClass, |
| 318 | alt->desc.bInterfaceSubClass, |
| 319 | alt->desc.bInterfaceProtocol)) |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 320 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 322 | if (add_uevent_var(envp, num_envp, &i, |
| 323 | buffer, buffer_size, &length, |
| 324 | "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", |
| 325 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 326 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 327 | le16_to_cpu(usb_dev->descriptor.bcdDevice), |
| 328 | usb_dev->descriptor.bDeviceClass, |
| 329 | usb_dev->descriptor.bDeviceSubClass, |
| 330 | usb_dev->descriptor.bDeviceProtocol, |
| 331 | alt->desc.bInterfaceClass, |
| 332 | alt->desc.bInterfaceSubClass, |
| 333 | alt->desc.bInterfaceProtocol)) |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 334 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | envp[i] = NULL; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | #else |
| 342 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 343 | static int usb_uevent(struct device *dev, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | int num_envp, char *buffer, int buffer_size) |
| 345 | { |
| 346 | return -ENODEV; |
| 347 | } |
| 348 | |
| 349 | #endif /* CONFIG_HOTPLUG */ |
| 350 | |
| 351 | /** |
| 352 | * usb_release_dev - free a usb device structure when all users of it are finished. |
| 353 | * @dev: device that's been disconnected |
| 354 | * |
| 355 | * Will be called only by the device core when all users of this usb device are |
| 356 | * done. |
| 357 | */ |
| 358 | static void usb_release_dev(struct device *dev) |
| 359 | { |
| 360 | struct usb_device *udev; |
| 361 | |
| 362 | udev = to_usb_device(dev); |
| 363 | |
| 364 | usb_destroy_configuration(udev); |
| 365 | usb_bus_put(udev->bus); |
| 366 | kfree(udev->product); |
| 367 | kfree(udev->manufacturer); |
| 368 | kfree(udev->serial); |
| 369 | kfree(udev); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * usb_alloc_dev - usb device constructor (usbcore-internal) |
| 374 | * @parent: hub to which device is connected; null to allocate a root hub |
| 375 | * @bus: bus used to access the device |
| 376 | * @port1: one-based index of port; ignored for root hubs |
| 377 | * Context: !in_interrupt () |
| 378 | * |
| 379 | * Only hub drivers (including virtual root hub drivers for host |
| 380 | * controllers) should ever call this. |
| 381 | * |
| 382 | * This call may not be used in a non-sleeping context. |
| 383 | */ |
| 384 | struct usb_device * |
| 385 | usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1) |
| 386 | { |
| 387 | struct usb_device *dev; |
| 388 | |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 389 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (!dev) |
| 391 | return NULL; |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | bus = usb_bus_get(bus); |
| 394 | if (!bus) { |
| 395 | kfree(dev); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | device_initialize(&dev->dev); |
| 400 | dev->dev.bus = &usb_bus_type; |
| 401 | dev->dev.dma_mask = bus->controller->dma_mask; |
| 402 | dev->dev.driver_data = &usb_generic_driver_data; |
| 403 | dev->dev.driver = &usb_generic_driver; |
| 404 | dev->dev.release = usb_release_dev; |
| 405 | dev->state = USB_STATE_ATTACHED; |
| 406 | |
| 407 | INIT_LIST_HEAD(&dev->ep0.urb_list); |
| 408 | dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; |
| 409 | dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; |
| 410 | /* ep0 maxpacket comes later, from device descriptor */ |
| 411 | dev->ep_in[0] = dev->ep_out[0] = &dev->ep0; |
| 412 | |
| 413 | /* Save readable and stable topology id, distinguishing devices |
| 414 | * by location for diagnostics, tools, driver model, etc. The |
| 415 | * string is a path along hub ports, from the root. Each device's |
| 416 | * dev->devpath will be stable until USB is re-cabled, and hubs |
| 417 | * are often labeled with these port numbers. The bus_id isn't |
| 418 | * as stable: bus->busnum changes easily from modprobe order, |
| 419 | * cardbus or pci hotplugging, and so on. |
| 420 | */ |
| 421 | if (unlikely (!parent)) { |
| 422 | dev->devpath [0] = '0'; |
| 423 | |
| 424 | dev->dev.parent = bus->controller; |
| 425 | sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum); |
| 426 | } else { |
| 427 | /* match any labeling on the hubs; it's one-based */ |
| 428 | if (parent->devpath [0] == '0') |
| 429 | snprintf (dev->devpath, sizeof dev->devpath, |
| 430 | "%d", port1); |
| 431 | else |
| 432 | snprintf (dev->devpath, sizeof dev->devpath, |
| 433 | "%s.%d", parent->devpath, port1); |
| 434 | |
| 435 | dev->dev.parent = &parent->dev; |
| 436 | sprintf (&dev->dev.bus_id[0], "%d-%s", |
| 437 | bus->busnum, dev->devpath); |
| 438 | |
| 439 | /* hub driver sets up TT records */ |
| 440 | } |
| 441 | |
Alan Stern | 12c3da3 | 2005-11-23 12:09:52 -0500 | [diff] [blame] | 442 | dev->portnum = port1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | dev->bus = bus; |
| 444 | dev->parent = parent; |
| 445 | INIT_LIST_HEAD(&dev->filelist); |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return dev; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * usb_get_dev - increments the reference count of the usb device structure |
| 452 | * @dev: the device being referenced |
| 453 | * |
| 454 | * Each live reference to a device should be refcounted. |
| 455 | * |
| 456 | * Drivers for USB interfaces should normally record such references in |
| 457 | * their probe() methods, when they bind to an interface, and release |
| 458 | * them by calling usb_put_dev(), in their disconnect() methods. |
| 459 | * |
| 460 | * A pointer to the device with the incremented reference counter is returned. |
| 461 | */ |
| 462 | struct usb_device *usb_get_dev(struct usb_device *dev) |
| 463 | { |
| 464 | if (dev) |
| 465 | get_device(&dev->dev); |
| 466 | return dev; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * usb_put_dev - release a use of the usb device structure |
| 471 | * @dev: device that's been disconnected |
| 472 | * |
| 473 | * Must be called when a user of a device is finished with it. When the last |
| 474 | * user of the device calls this function, the memory of the device is freed. |
| 475 | */ |
| 476 | void usb_put_dev(struct usb_device *dev) |
| 477 | { |
| 478 | if (dev) |
| 479 | put_device(&dev->dev); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * usb_get_intf - increments the reference count of the usb interface structure |
| 484 | * @intf: the interface being referenced |
| 485 | * |
| 486 | * Each live reference to a interface must be refcounted. |
| 487 | * |
| 488 | * Drivers for USB interfaces should normally record such references in |
| 489 | * their probe() methods, when they bind to an interface, and release |
| 490 | * them by calling usb_put_intf(), in their disconnect() methods. |
| 491 | * |
| 492 | * A pointer to the interface with the incremented reference counter is |
| 493 | * returned. |
| 494 | */ |
| 495 | struct usb_interface *usb_get_intf(struct usb_interface *intf) |
| 496 | { |
| 497 | if (intf) |
| 498 | get_device(&intf->dev); |
| 499 | return intf; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * usb_put_intf - release a use of the usb interface structure |
| 504 | * @intf: interface that's been decremented |
| 505 | * |
| 506 | * Must be called when a user of an interface is finished with it. When the |
| 507 | * last user of the interface calls this function, the memory of the interface |
| 508 | * is freed. |
| 509 | */ |
| 510 | void usb_put_intf(struct usb_interface *intf) |
| 511 | { |
| 512 | if (intf) |
| 513 | put_device(&intf->dev); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | /* USB device locking |
| 518 | * |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 519 | * USB devices and interfaces are locked using the semaphore in their |
| 520 | * embedded struct device. The hub driver guarantees that whenever a |
| 521 | * device is connected or disconnected, drivers are called with the |
| 522 | * USB device locked as well as their particular interface. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | * |
| 524 | * Complications arise when several devices are to be locked at the same |
| 525 | * time. Only hub-aware drivers that are part of usbcore ever have to |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 526 | * do this; nobody else needs to worry about it. The rule for locking |
| 527 | * is simple: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * |
| 529 | * When locking both a device and its parent, always lock the |
| 530 | * the parent first. |
| 531 | */ |
| 532 | |
| 533 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | * usb_lock_device_for_reset - cautiously acquire the lock for a |
| 535 | * usb device structure |
| 536 | * @udev: device that's being locked |
| 537 | * @iface: interface bound to the driver making the request (optional) |
| 538 | * |
| 539 | * Attempts to acquire the device lock, but fails if the device is |
| 540 | * NOTATTACHED or SUSPENDED, or if iface is specified and the interface |
| 541 | * is neither BINDING nor BOUND. Rather than sleeping to wait for the |
| 542 | * lock, the routine polls repeatedly. This is to prevent deadlock with |
| 543 | * disconnect; in some drivers (such as usb-storage) the disconnect() |
Alan Stern | 3ea1596 | 2005-08-11 10:15:39 -0400 | [diff] [blame] | 544 | * or suspend() method will block waiting for a device reset to complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | * |
| 546 | * Returns a negative error code for failure, otherwise 1 or 0 to indicate |
| 547 | * that the device will or will not have to be unlocked. (0 can be |
| 548 | * returned when an interface is given and is BINDING, because in that |
| 549 | * case the driver already owns the device lock.) |
| 550 | */ |
| 551 | int usb_lock_device_for_reset(struct usb_device *udev, |
| 552 | struct usb_interface *iface) |
| 553 | { |
Alan Stern | 3ea1596 | 2005-08-11 10:15:39 -0400 | [diff] [blame] | 554 | unsigned long jiffies_expire = jiffies + HZ; |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (udev->state == USB_STATE_NOTATTACHED) |
| 557 | return -ENODEV; |
| 558 | if (udev->state == USB_STATE_SUSPENDED) |
| 559 | return -EHOSTUNREACH; |
| 560 | if (iface) { |
| 561 | switch (iface->condition) { |
| 562 | case USB_INTERFACE_BINDING: |
| 563 | return 0; |
| 564 | case USB_INTERFACE_BOUND: |
| 565 | break; |
| 566 | default: |
| 567 | return -EINTR; |
| 568 | } |
| 569 | } |
| 570 | |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 571 | while (usb_trylock_device(udev) != 0) { |
Alan Stern | 3ea1596 | 2005-08-11 10:15:39 -0400 | [diff] [blame] | 572 | |
| 573 | /* If we can't acquire the lock after waiting one second, |
| 574 | * we're probably deadlocked */ |
| 575 | if (time_after(jiffies, jiffies_expire)) |
| 576 | return -EBUSY; |
| 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | msleep(15); |
| 579 | if (udev->state == USB_STATE_NOTATTACHED) |
| 580 | return -ENODEV; |
| 581 | if (udev->state == USB_STATE_SUSPENDED) |
| 582 | return -EHOSTUNREACH; |
| 583 | if (iface && iface->condition != USB_INTERFACE_BOUND) |
| 584 | return -EINTR; |
| 585 | } |
| 586 | return 1; |
| 587 | } |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | static struct usb_device *match_device(struct usb_device *dev, |
| 591 | u16 vendor_id, u16 product_id) |
| 592 | { |
| 593 | struct usb_device *ret_dev = NULL; |
| 594 | int child; |
| 595 | |
| 596 | dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n", |
| 597 | le16_to_cpu(dev->descriptor.idVendor), |
| 598 | le16_to_cpu(dev->descriptor.idProduct)); |
| 599 | |
| 600 | /* see if this device matches */ |
| 601 | if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) && |
| 602 | (product_id == le16_to_cpu(dev->descriptor.idProduct))) { |
| 603 | dev_dbg (&dev->dev, "matched this device!\n"); |
| 604 | ret_dev = usb_get_dev(dev); |
| 605 | goto exit; |
| 606 | } |
| 607 | |
| 608 | /* look through all of the children of this device */ |
| 609 | for (child = 0; child < dev->maxchild; ++child) { |
| 610 | if (dev->children[child]) { |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 611 | usb_lock_device(dev->children[child]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | ret_dev = match_device(dev->children[child], |
| 613 | vendor_id, product_id); |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 614 | usb_unlock_device(dev->children[child]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (ret_dev) |
| 616 | goto exit; |
| 617 | } |
| 618 | } |
| 619 | exit: |
| 620 | return ret_dev; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * usb_find_device - find a specific usb device in the system |
| 625 | * @vendor_id: the vendor id of the device to find |
| 626 | * @product_id: the product id of the device to find |
| 627 | * |
| 628 | * Returns a pointer to a struct usb_device if such a specified usb |
| 629 | * device is present in the system currently. The usage count of the |
| 630 | * device will be incremented if a device is found. Make sure to call |
| 631 | * usb_put_dev() when the caller is finished with the device. |
| 632 | * |
| 633 | * If a device with the specified vendor and product id is not found, |
| 634 | * NULL is returned. |
| 635 | */ |
| 636 | struct usb_device *usb_find_device(u16 vendor_id, u16 product_id) |
| 637 | { |
| 638 | struct list_head *buslist; |
| 639 | struct usb_bus *bus; |
| 640 | struct usb_device *dev = NULL; |
| 641 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 642 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | for (buslist = usb_bus_list.next; |
| 644 | buslist != &usb_bus_list; |
| 645 | buslist = buslist->next) { |
| 646 | bus = container_of(buslist, struct usb_bus, bus_list); |
| 647 | if (!bus->root_hub) |
| 648 | continue; |
| 649 | usb_lock_device(bus->root_hub); |
| 650 | dev = match_device(bus->root_hub, vendor_id, product_id); |
| 651 | usb_unlock_device(bus->root_hub); |
| 652 | if (dev) |
| 653 | goto exit; |
| 654 | } |
| 655 | exit: |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 656 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return dev; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * usb_get_current_frame_number - return current bus frame number |
| 662 | * @dev: the device whose bus is being queried |
| 663 | * |
| 664 | * Returns the current frame number for the USB host controller |
| 665 | * used with the given USB device. This can be used when scheduling |
| 666 | * isochronous requests. |
| 667 | * |
| 668 | * Note that different kinds of host controller have different |
| 669 | * "scheduling horizons". While one type might support scheduling only |
| 670 | * 32 frames into the future, others could support scheduling up to |
| 671 | * 1024 frames into the future. |
| 672 | */ |
| 673 | int usb_get_current_frame_number(struct usb_device *dev) |
| 674 | { |
| 675 | return dev->bus->op->get_frame_number (dev); |
| 676 | } |
| 677 | |
| 678 | /*-------------------------------------------------------------------*/ |
| 679 | /* |
| 680 | * __usb_get_extra_descriptor() finds a descriptor of specific type in the |
| 681 | * extra field of the interface and endpoint descriptor structs. |
| 682 | */ |
| 683 | |
| 684 | int __usb_get_extra_descriptor(char *buffer, unsigned size, |
| 685 | unsigned char type, void **ptr) |
| 686 | { |
| 687 | struct usb_descriptor_header *header; |
| 688 | |
| 689 | while (size >= sizeof(struct usb_descriptor_header)) { |
| 690 | header = (struct usb_descriptor_header *)buffer; |
| 691 | |
| 692 | if (header->bLength < 2) { |
| 693 | printk(KERN_ERR |
| 694 | "%s: bogus descriptor, type %d length %d\n", |
| 695 | usbcore_name, |
| 696 | header->bDescriptorType, |
| 697 | header->bLength); |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | if (header->bDescriptorType == type) { |
| 702 | *ptr = header; |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | buffer += header->bLength; |
| 707 | size -= header->bLength; |
| 708 | } |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP |
| 714 | * @dev: device the buffer will be used with |
| 715 | * @size: requested buffer size |
| 716 | * @mem_flags: affect whether allocation may block |
| 717 | * @dma: used to return DMA address of buffer |
| 718 | * |
| 719 | * Return value is either null (indicating no buffer could be allocated), or |
| 720 | * the cpu-space pointer to a buffer that may be used to perform DMA to the |
| 721 | * specified device. Such cpu-space buffers are returned along with the DMA |
| 722 | * address (through the pointer provided). |
| 723 | * |
| 724 | * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags |
| 725 | * to avoid behaviors like using "DMA bounce buffers", or tying down I/O |
| 726 | * mapping hardware for long idle periods. The implementation varies between |
| 727 | * platforms, depending on details of how DMA will work to this device. |
| 728 | * Using these buffers also helps prevent cacheline sharing problems on |
| 729 | * architectures where CPU caches are not DMA-coherent. |
| 730 | * |
| 731 | * When the buffer is no longer used, free it with usb_buffer_free(). |
| 732 | */ |
| 733 | void *usb_buffer_alloc ( |
| 734 | struct usb_device *dev, |
| 735 | size_t size, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 736 | gfp_t mem_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | dma_addr_t *dma |
| 738 | ) |
| 739 | { |
| 740 | if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc) |
| 741 | return NULL; |
| 742 | return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma); |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * usb_buffer_free - free memory allocated with usb_buffer_alloc() |
| 747 | * @dev: device the buffer was used with |
| 748 | * @size: requested buffer size |
| 749 | * @addr: CPU address of buffer |
| 750 | * @dma: DMA address of buffer |
| 751 | * |
| 752 | * This reclaims an I/O buffer, letting it be reused. The memory must have |
| 753 | * been allocated using usb_buffer_alloc(), and the parameters must match |
| 754 | * those provided in that allocation request. |
| 755 | */ |
| 756 | void usb_buffer_free ( |
| 757 | struct usb_device *dev, |
| 758 | size_t size, |
| 759 | void *addr, |
| 760 | dma_addr_t dma |
| 761 | ) |
| 762 | { |
| 763 | if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free) |
| 764 | return; |
| 765 | dev->bus->op->buffer_free (dev->bus, size, addr, dma); |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * usb_buffer_map - create DMA mapping(s) for an urb |
| 770 | * @urb: urb whose transfer_buffer/setup_packet will be mapped |
| 771 | * |
| 772 | * Return value is either null (indicating no buffer could be mapped), or |
| 773 | * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are |
| 774 | * added to urb->transfer_flags if the operation succeeds. If the device |
| 775 | * is connected to this system through a non-DMA controller, this operation |
| 776 | * always succeeds. |
| 777 | * |
| 778 | * This call would normally be used for an urb which is reused, perhaps |
| 779 | * as the target of a large periodic transfer, with usb_buffer_dmasync() |
| 780 | * calls to synchronize memory and dma state. |
| 781 | * |
| 782 | * Reverse the effect of this call with usb_buffer_unmap(). |
| 783 | */ |
| 784 | #if 0 |
| 785 | struct urb *usb_buffer_map (struct urb *urb) |
| 786 | { |
| 787 | struct usb_bus *bus; |
| 788 | struct device *controller; |
| 789 | |
| 790 | if (!urb |
| 791 | || !urb->dev |
| 792 | || !(bus = urb->dev->bus) |
| 793 | || !(controller = bus->controller)) |
| 794 | return NULL; |
| 795 | |
| 796 | if (controller->dma_mask) { |
| 797 | urb->transfer_dma = dma_map_single (controller, |
| 798 | urb->transfer_buffer, urb->transfer_buffer_length, |
| 799 | usb_pipein (urb->pipe) |
| 800 | ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 801 | if (usb_pipecontrol (urb->pipe)) |
| 802 | urb->setup_dma = dma_map_single (controller, |
| 803 | urb->setup_packet, |
| 804 | sizeof (struct usb_ctrlrequest), |
| 805 | DMA_TO_DEVICE); |
| 806 | // FIXME generic api broken like pci, can't report errors |
| 807 | // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; |
| 808 | } else |
| 809 | urb->transfer_dma = ~0; |
| 810 | urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP |
| 811 | | URB_NO_SETUP_DMA_MAP); |
| 812 | return urb; |
| 813 | } |
| 814 | #endif /* 0 */ |
| 815 | |
| 816 | /* XXX DISABLED, no users currently. If you wish to re-enable this |
| 817 | * XXX please determine whether the sync is to transfer ownership of |
| 818 | * XXX the buffer from device to cpu or vice verse, and thusly use the |
| 819 | * XXX appropriate _for_{cpu,device}() method. -DaveM |
| 820 | */ |
| 821 | #if 0 |
| 822 | |
| 823 | /** |
| 824 | * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s) |
| 825 | * @urb: urb whose transfer_buffer/setup_packet will be synchronized |
| 826 | */ |
| 827 | void usb_buffer_dmasync (struct urb *urb) |
| 828 | { |
| 829 | struct usb_bus *bus; |
| 830 | struct device *controller; |
| 831 | |
| 832 | if (!urb |
| 833 | || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) |
| 834 | || !urb->dev |
| 835 | || !(bus = urb->dev->bus) |
| 836 | || !(controller = bus->controller)) |
| 837 | return; |
| 838 | |
| 839 | if (controller->dma_mask) { |
| 840 | dma_sync_single (controller, |
| 841 | urb->transfer_dma, urb->transfer_buffer_length, |
| 842 | usb_pipein (urb->pipe) |
| 843 | ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 844 | if (usb_pipecontrol (urb->pipe)) |
| 845 | dma_sync_single (controller, |
| 846 | urb->setup_dma, |
| 847 | sizeof (struct usb_ctrlrequest), |
| 848 | DMA_TO_DEVICE); |
| 849 | } |
| 850 | } |
| 851 | #endif |
| 852 | |
| 853 | /** |
| 854 | * usb_buffer_unmap - free DMA mapping(s) for an urb |
| 855 | * @urb: urb whose transfer_buffer will be unmapped |
| 856 | * |
| 857 | * Reverses the effect of usb_buffer_map(). |
| 858 | */ |
| 859 | #if 0 |
| 860 | void usb_buffer_unmap (struct urb *urb) |
| 861 | { |
| 862 | struct usb_bus *bus; |
| 863 | struct device *controller; |
| 864 | |
| 865 | if (!urb |
| 866 | || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) |
| 867 | || !urb->dev |
| 868 | || !(bus = urb->dev->bus) |
| 869 | || !(controller = bus->controller)) |
| 870 | return; |
| 871 | |
| 872 | if (controller->dma_mask) { |
| 873 | dma_unmap_single (controller, |
| 874 | urb->transfer_dma, urb->transfer_buffer_length, |
| 875 | usb_pipein (urb->pipe) |
| 876 | ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 877 | if (usb_pipecontrol (urb->pipe)) |
| 878 | dma_unmap_single (controller, |
| 879 | urb->setup_dma, |
| 880 | sizeof (struct usb_ctrlrequest), |
| 881 | DMA_TO_DEVICE); |
| 882 | } |
| 883 | urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP |
| 884 | | URB_NO_SETUP_DMA_MAP); |
| 885 | } |
| 886 | #endif /* 0 */ |
| 887 | |
| 888 | /** |
| 889 | * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint |
| 890 | * @dev: device to which the scatterlist will be mapped |
| 891 | * @pipe: endpoint defining the mapping direction |
| 892 | * @sg: the scatterlist to map |
| 893 | * @nents: the number of entries in the scatterlist |
| 894 | * |
| 895 | * Return value is either < 0 (indicating no buffers could be mapped), or |
| 896 | * the number of DMA mapping array entries in the scatterlist. |
| 897 | * |
| 898 | * The caller is responsible for placing the resulting DMA addresses from |
| 899 | * the scatterlist into URB transfer buffer pointers, and for setting the |
| 900 | * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs. |
| 901 | * |
| 902 | * Top I/O rates come from queuing URBs, instead of waiting for each one |
| 903 | * to complete before starting the next I/O. This is particularly easy |
| 904 | * to do with scatterlists. Just allocate and submit one URB for each DMA |
| 905 | * mapping entry returned, stopping on the first error or when all succeed. |
| 906 | * Better yet, use the usb_sg_*() calls, which do that (and more) for you. |
| 907 | * |
| 908 | * This call would normally be used when translating scatterlist requests, |
| 909 | * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it |
| 910 | * may be able to coalesce mappings for improved I/O efficiency. |
| 911 | * |
| 912 | * Reverse the effect of this call with usb_buffer_unmap_sg(). |
| 913 | */ |
| 914 | int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe, |
| 915 | struct scatterlist *sg, int nents) |
| 916 | { |
| 917 | struct usb_bus *bus; |
| 918 | struct device *controller; |
| 919 | |
| 920 | if (!dev |
| 921 | || usb_pipecontrol (pipe) |
| 922 | || !(bus = dev->bus) |
| 923 | || !(controller = bus->controller) |
| 924 | || !controller->dma_mask) |
| 925 | return -1; |
| 926 | |
| 927 | // FIXME generic api broken like pci, can't report errors |
| 928 | return dma_map_sg (controller, sg, nents, |
| 929 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 930 | } |
| 931 | |
| 932 | /* XXX DISABLED, no users currently. If you wish to re-enable this |
| 933 | * XXX please determine whether the sync is to transfer ownership of |
| 934 | * XXX the buffer from device to cpu or vice verse, and thusly use the |
| 935 | * XXX appropriate _for_{cpu,device}() method. -DaveM |
| 936 | */ |
| 937 | #if 0 |
| 938 | |
| 939 | /** |
| 940 | * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s) |
| 941 | * @dev: device to which the scatterlist will be mapped |
| 942 | * @pipe: endpoint defining the mapping direction |
| 943 | * @sg: the scatterlist to synchronize |
| 944 | * @n_hw_ents: the positive return value from usb_buffer_map_sg |
| 945 | * |
| 946 | * Use this when you are re-using a scatterlist's data buffers for |
| 947 | * another USB request. |
| 948 | */ |
| 949 | void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe, |
| 950 | struct scatterlist *sg, int n_hw_ents) |
| 951 | { |
| 952 | struct usb_bus *bus; |
| 953 | struct device *controller; |
| 954 | |
| 955 | if (!dev |
| 956 | || !(bus = dev->bus) |
| 957 | || !(controller = bus->controller) |
| 958 | || !controller->dma_mask) |
| 959 | return; |
| 960 | |
| 961 | dma_sync_sg (controller, sg, n_hw_ents, |
| 962 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 963 | } |
| 964 | #endif |
| 965 | |
| 966 | /** |
| 967 | * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist |
| 968 | * @dev: device to which the scatterlist will be mapped |
| 969 | * @pipe: endpoint defining the mapping direction |
| 970 | * @sg: the scatterlist to unmap |
| 971 | * @n_hw_ents: the positive return value from usb_buffer_map_sg |
| 972 | * |
| 973 | * Reverses the effect of usb_buffer_map_sg(). |
| 974 | */ |
| 975 | void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe, |
| 976 | struct scatterlist *sg, int n_hw_ents) |
| 977 | { |
| 978 | struct usb_bus *bus; |
| 979 | struct device *controller; |
| 980 | |
| 981 | if (!dev |
| 982 | || !(bus = dev->bus) |
| 983 | || !(controller = bus->controller) |
| 984 | || !controller->dma_mask) |
| 985 | return; |
| 986 | |
| 987 | dma_unmap_sg (controller, sg, n_hw_ents, |
| 988 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 989 | } |
| 990 | |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 991 | static int verify_suspended(struct device *dev, void *unused) |
| 992 | { |
Greg Kroah-Hartman | 0517587 | 2006-06-22 13:29:52 -0700 | [diff] [blame] | 993 | if (dev->driver == NULL) |
| 994 | return 0; |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 995 | return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0; |
| 996 | } |
| 997 | |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 998 | static int usb_generic_suspend(struct device *dev, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1000 | struct usb_interface *intf; |
| 1001 | struct usb_driver *driver; |
| 1002 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1004 | /* USB devices enter SUSPEND state through their hubs, but can be |
| 1005 | * marked for FREEZE as soon as their children are already idled. |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1006 | * But those semantics are useless, so we equate the two (sigh). |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1007 | */ |
| 1008 | if (dev->driver == &usb_generic_driver) { |
| 1009 | if (dev->power.power_state.event == message.event) |
| 1010 | return 0; |
| 1011 | /* we need to rule out bogus requests through sysfs */ |
| 1012 | status = device_for_each_child(dev, NULL, verify_suspended); |
| 1013 | if (status) |
| 1014 | return status; |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1015 | return usb_suspend_device (to_usb_device(dev)); |
| 1016 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | if ((dev->driver == NULL) || |
| 1019 | (dev->driver_data == &usb_generic_driver_data)) |
| 1020 | return 0; |
| 1021 | |
| 1022 | intf = to_usb_interface(dev); |
| 1023 | driver = to_usb_driver(dev->driver); |
| 1024 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1025 | /* with no hardware, USB interfaces only use FREEZE and ON states */ |
| 1026 | if (!is_active(intf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return 0; |
| 1028 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1029 | if (driver->suspend && driver->resume) { |
| 1030 | status = driver->suspend(intf, message); |
| 1031 | if (status) |
| 1032 | dev_err(dev, "%s error %d\n", "suspend", status); |
| 1033 | else |
| 1034 | mark_quiesced(intf); |
| 1035 | } else { |
| 1036 | // FIXME else if there's no suspend method, disconnect... |
Alan Stern | 5a9191f | 2005-12-21 14:28:11 -0800 | [diff] [blame] | 1037 | dev_warn(dev, "no suspend for driver %s?\n", driver->name); |
| 1038 | mark_quiesced(intf); |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1039 | status = 0; |
| 1040 | } |
| 1041 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static int usb_generic_resume(struct device *dev) |
| 1045 | { |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1046 | struct usb_interface *intf; |
| 1047 | struct usb_driver *driver; |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1048 | struct usb_device *udev; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1049 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1051 | if (dev->power.power_state.event == PM_EVENT_ON) |
| 1052 | return 0; |
| 1053 | |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1054 | /* mark things as "on" immediately, no matter what errors crop up */ |
| 1055 | dev->power.power_state.event = PM_EVENT_ON; |
| 1056 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1057 | /* devices resume through their hubs */ |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1058 | if (dev->driver == &usb_generic_driver) { |
| 1059 | udev = to_usb_device(dev); |
| 1060 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1061 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | return usb_resume_device (to_usb_device(dev)); |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | if ((dev->driver == NULL) || |
Alan Stern | 5a9191f | 2005-12-21 14:28:11 -0800 | [diff] [blame] | 1066 | (dev->driver_data == &usb_generic_driver_data)) { |
| 1067 | dev->power.power_state.event = PM_EVENT_FREEZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return 0; |
Alan Stern | 5a9191f | 2005-12-21 14:28:11 -0800 | [diff] [blame] | 1069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | intf = to_usb_interface(dev); |
| 1072 | driver = to_usb_driver(dev->driver); |
| 1073 | |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1074 | udev = interface_to_usbdev(intf); |
| 1075 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1076 | return 0; |
| 1077 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1078 | /* if driver was suspended, it has a resume method; |
| 1079 | * however, sysfs can wrongly mark things as suspended |
| 1080 | * (on the "no suspend method" FIXME path above) |
| 1081 | */ |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1082 | if (driver->resume) { |
| 1083 | status = driver->resume(intf); |
| 1084 | if (status) { |
| 1085 | dev_err(dev, "%s error %d\n", "resume", status); |
| 1086 | mark_quiesced(intf); |
| 1087 | } |
| 1088 | } else |
Alan Stern | 5a9191f | 2005-12-21 14:28:11 -0800 | [diff] [blame] | 1089 | dev_warn(dev, "no resume for driver %s?\n", driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | struct bus_type usb_bus_type = { |
| 1094 | .name = "usb", |
| 1095 | .match = usb_device_match, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1096 | .uevent = usb_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | .suspend = usb_generic_suspend, |
| 1098 | .resume = usb_generic_resume, |
| 1099 | }; |
| 1100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | /* format to disable USB on kernel command line is: nousb */ |
Pete Zaitcev | aafbf24 | 2005-12-20 14:15:04 -0800 | [diff] [blame] | 1102 | __module_param_call("", nousb, param_set_bool, param_get_bool, &nousb, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
| 1104 | /* |
| 1105 | * for external read access to <nousb> |
| 1106 | */ |
| 1107 | int usb_disabled(void) |
| 1108 | { |
| 1109 | return nousb; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Init |
| 1114 | */ |
| 1115 | static int __init usb_init(void) |
| 1116 | { |
| 1117 | int retval; |
| 1118 | if (nousb) { |
| 1119 | pr_info ("%s: USB support disabled\n", usbcore_name); |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | retval = bus_register(&usb_bus_type); |
| 1124 | if (retval) |
| 1125 | goto out; |
| 1126 | retval = usb_host_init(); |
| 1127 | if (retval) |
| 1128 | goto host_init_failed; |
| 1129 | retval = usb_major_init(); |
| 1130 | if (retval) |
| 1131 | goto major_init_failed; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1132 | retval = usb_register(&usbfs_driver); |
| 1133 | if (retval) |
| 1134 | goto driver_register_failed; |
| 1135 | retval = usbdev_init(); |
| 1136 | if (retval) |
| 1137 | goto usbdevice_init_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | retval = usbfs_init(); |
| 1139 | if (retval) |
| 1140 | goto fs_init_failed; |
| 1141 | retval = usb_hub_init(); |
| 1142 | if (retval) |
| 1143 | goto hub_init_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | retval = driver_register(&usb_generic_driver); |
| 1145 | if (!retval) |
| 1146 | goto out; |
| 1147 | |
| 1148 | usb_hub_cleanup(); |
| 1149 | hub_init_failed: |
| 1150 | usbfs_cleanup(); |
| 1151 | fs_init_failed: |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1152 | usbdev_cleanup(); |
| 1153 | usbdevice_init_failed: |
| 1154 | usb_deregister(&usbfs_driver); |
| 1155 | driver_register_failed: |
| 1156 | usb_major_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | major_init_failed: |
| 1158 | usb_host_cleanup(); |
| 1159 | host_init_failed: |
| 1160 | bus_unregister(&usb_bus_type); |
| 1161 | out: |
| 1162 | return retval; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Cleanup |
| 1167 | */ |
| 1168 | static void __exit usb_exit(void) |
| 1169 | { |
| 1170 | /* This will matter if shutdown/reboot does exitcalls. */ |
| 1171 | if (nousb) |
| 1172 | return; |
| 1173 | |
| 1174 | driver_unregister(&usb_generic_driver); |
| 1175 | usb_major_cleanup(); |
| 1176 | usbfs_cleanup(); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1177 | usb_deregister(&usbfs_driver); |
| 1178 | usbdev_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | usb_hub_cleanup(); |
| 1180 | usb_host_cleanup(); |
| 1181 | bus_unregister(&usb_bus_type); |
| 1182 | } |
| 1183 | |
| 1184 | subsys_initcall(usb_init); |
| 1185 | module_exit(usb_exit); |
| 1186 | |
| 1187 | /* |
| 1188 | * USB may be built into the kernel or be built as modules. |
| 1189 | * These symbols are exported for device (or host controller) |
| 1190 | * driver modules to use. |
| 1191 | */ |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | EXPORT_SYMBOL(usb_disabled); |
| 1194 | |
brian@murphy.dk | a3fdf4e | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 1195 | EXPORT_SYMBOL_GPL(usb_get_intf); |
| 1196 | EXPORT_SYMBOL_GPL(usb_put_intf); |
| 1197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | EXPORT_SYMBOL(usb_put_dev); |
| 1199 | EXPORT_SYMBOL(usb_get_dev); |
| 1200 | EXPORT_SYMBOL(usb_hub_tt_clear_buffer); |
| 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | EXPORT_SYMBOL(usb_lock_device_for_reset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | EXPORT_SYMBOL(usb_driver_claim_interface); |
| 1205 | EXPORT_SYMBOL(usb_driver_release_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | EXPORT_SYMBOL(usb_find_interface); |
| 1207 | EXPORT_SYMBOL(usb_ifnum_to_if); |
| 1208 | EXPORT_SYMBOL(usb_altnum_to_altsetting); |
| 1209 | |
| 1210 | EXPORT_SYMBOL(usb_reset_device); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 1211 | EXPORT_SYMBOL(usb_reset_composite_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | EXPORT_SYMBOL(__usb_get_extra_descriptor); |
| 1214 | |
| 1215 | EXPORT_SYMBOL(usb_find_device); |
| 1216 | EXPORT_SYMBOL(usb_get_current_frame_number); |
| 1217 | |
| 1218 | EXPORT_SYMBOL (usb_buffer_alloc); |
| 1219 | EXPORT_SYMBOL (usb_buffer_free); |
| 1220 | |
| 1221 | #if 0 |
| 1222 | EXPORT_SYMBOL (usb_buffer_map); |
| 1223 | EXPORT_SYMBOL (usb_buffer_dmasync); |
| 1224 | EXPORT_SYMBOL (usb_buffer_unmap); |
| 1225 | #endif |
| 1226 | |
| 1227 | EXPORT_SYMBOL (usb_buffer_map_sg); |
| 1228 | #if 0 |
| 1229 | EXPORT_SYMBOL (usb_buffer_dmasync_sg); |
| 1230 | #endif |
| 1231 | EXPORT_SYMBOL (usb_buffer_unmap_sg); |
| 1232 | |
| 1233 | MODULE_LICENSE("GPL"); |