1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_USB_H 3 #define __LINUX_USB_H 4 5 #include <linux/mod_devicetable.h> 6 #include <linux/usb/ch9.h> 7 8 #define USB_MAJOR 180 9 #define USB_DEVICE_MAJOR 189 10 11 12 #ifdef __KERNEL__ 13 14 #include <linux/errno.h> /* for -ENODEV */ 15 #include <linux/delay.h> /* for mdelay() */ 16 #include <linux/interrupt.h> /* for in_interrupt() */ 17 #include <linux/list.h> /* for struct list_head */ 18 #include <linux/kref.h> /* for struct kref */ 19 #include <linux/device.h> /* for struct device */ 20 #include <linux/fs.h> /* for struct file_operations */ 21 #include <linux/completion.h> /* for struct completion */ 22 #include <linux/sched.h> /* for current && schedule_timeout */ 23 #include <linux/mutex.h> /* for struct mutex */ 24 #include <linux/pm_runtime.h> /* for runtime PM */ 25 26 struct usb_device; 27 struct usb_driver; 28 struct wusb_dev; 29 30 /*-------------------------------------------------------------------------*/ 31 32 /* 33 * Host-side wrappers for standard USB descriptors ... these are parsed 34 * from the data provided by devices. Parsing turns them from a flat 35 * sequence of descriptors into a hierarchy: 36 * 37 * - devices have one (usually) or more configs; 38 * - configs have one (often) or more interfaces; 39 * - interfaces have one (usually) or more settings; 40 * - each interface setting has zero or (usually) more endpoints. 41 * - a SuperSpeed endpoint has a companion descriptor 42 * 43 * And there might be other descriptors mixed in with those. 44 * 45 * Devices may also have class-specific or vendor-specific descriptors. 46 */ 47 48 struct ep_device; 49 50 /** 51 * struct usb_host_endpoint - host-side endpoint descriptor and queue 52 * @desc: descriptor for this endpoint, wMaxPacketSize in native byteorder 53 * @ss_ep_comp: SuperSpeed companion descriptor for this endpoint 54 * @ssp_isoc_ep_comp: SuperSpeedPlus isoc companion descriptor for this endpoint 55 * @urb_list: urbs queued to this endpoint; maintained by usbcore 56 * @hcpriv: for use by HCD; typically holds hardware dma queue head (QH) 57 * with one or more transfer descriptors (TDs) per urb 58 * @ep_dev: ep_device for sysfs info 59 * @extra: descriptors following this endpoint in the configuration 60 * @extralen: how many bytes of "extra" are valid 61 * @enabled: URBs may be submitted to this endpoint 62 * @streams: number of USB-3 streams allocated on the endpoint 63 * 64 * USB requests are always queued to a given endpoint, identified by a 65 * descriptor within an active interface in a given USB configuration. 66 */ 67 struct usb_host_endpoint { 68 struct usb_endpoint_descriptor desc; 69 struct usb_ss_ep_comp_descriptor ss_ep_comp; 70 struct usb_ssp_isoc_ep_comp_descriptor ssp_isoc_ep_comp; 71 struct list_head urb_list; 72 void *hcpriv; 73 struct ep_device *ep_dev; /* For sysfs info */ 74 75 unsigned char *extra; /* Extra descriptors */ 76 int extralen; 77 int enabled; 78 int streams; 79 }; 80 81 /* host-side wrapper for one interface setting's parsed descriptors */ 82 struct usb_host_interface { 83 struct usb_interface_descriptor desc; 84 85 int extralen; 86 unsigned char *extra; /* Extra descriptors */ 87 88 /* array of desc.bNumEndpoints endpoints associated with this 89 * interface setting. these will be in no particular order. 90 */ 91 struct usb_host_endpoint *endpoint; 92 93 char *string; /* iInterface string, if present */ 94 }; 95 96 enum usb_interface_condition { 97 USB_INTERFACE_UNBOUND = 0, 98 USB_INTERFACE_BINDING, 99 USB_INTERFACE_BOUND, 100 USB_INTERFACE_UNBINDING, 101 }; 102 103 int __must_check 104 usb_find_common_endpoints(struct usb_host_interface *alt, 105 struct usb_endpoint_descriptor **bulk_in, 106 struct usb_endpoint_descriptor **bulk_out, 107 struct usb_endpoint_descriptor **int_in, 108 struct usb_endpoint_descriptor **int_out); 109 110 int __must_check 111 usb_find_common_endpoints_reverse(struct usb_host_interface *alt, 112 struct usb_endpoint_descriptor **bulk_in, 113 struct usb_endpoint_descriptor **bulk_out, 114 struct usb_endpoint_descriptor **int_in, 115 struct usb_endpoint_descriptor **int_out); 116 117 static inline int __must_check 118 usb_find_bulk_in_endpoint(struct usb_host_interface *alt, 119 struct usb_endpoint_descriptor **bulk_in) 120 { 121 return usb_find_common_endpoints(alt, bulk_in, NULL, NULL, NULL); 122 } 123 124 static inline int __must_check 125 usb_find_bulk_out_endpoint(struct usb_host_interface *alt, 126 struct usb_endpoint_descriptor **bulk_out) 127 { 128 return usb_find_common_endpoints(alt, NULL, bulk_out, NULL, NULL); 129 } 130 131 static inline int __must_check 132 usb_find_int_in_endpoint(struct usb_host_interface *alt, 133 struct usb_endpoint_descriptor **int_in) 134 { 135 return usb_find_common_endpoints(alt, NULL, NULL, int_in, NULL); 136 } 137 138 static inline int __must_check 139 usb_find_int_out_endpoint(struct usb_host_interface *alt, 140 struct usb_endpoint_descriptor **int_out) 141 { 142 return usb_find_common_endpoints(alt, NULL, NULL, NULL, int_out); 143 } 144 145 static inline int __must_check 146 usb_find_last_bulk_in_endpoint(struct usb_host_interface *alt, 147 struct usb_endpoint_descriptor **bulk_in) 148 { 149 return usb_find_common_endpoints_reverse(alt, bulk_in, NULL, NULL, NULL); 150 } 151 152 static inline int __must_check 153 usb_find_last_bulk_out_endpoint(struct usb_host_interface *alt, 154 struct usb_endpoint_descriptor **bulk_out) 155 { 156 return usb_find_common_endpoints_reverse(alt, NULL, bulk_out, NULL, NULL); 157 } 158 159 static inline int __must_check 160 usb_find_last_int_in_endpoint(struct usb_host_interface *alt, 161 struct usb_endpoint_descriptor **int_in) 162 { 163 return usb_find_common_endpoints_reverse(alt, NULL, NULL, int_in, NULL); 164 } 165 166 static inline int __must_check 167 usb_find_last_int_out_endpoint(struct usb_host_interface *alt, 168 struct usb_endpoint_descriptor **int_out) 169 { 170 return usb_find_common_endpoints_reverse(alt, NULL, NULL, NULL, int_out); 171 } 172 173 enum usb_wireless_status { 174 USB_WIRELESS_STATUS_NA = 0, 175 USB_WIRELESS_STATUS_DISCONNECTED, 176 USB_WIRELESS_STATUS_CONNECTED, 177 }; 178 179 /** 180 * struct usb_interface - what usb device drivers talk to 181 * @altsetting: array of interface structures, one for each alternate 182 * setting that may be selected. Each one includes a set of 183 * endpoint configurations. They will be in no particular order. 184 * @cur_altsetting: the current altsetting. 185 * @num_altsetting: number of altsettings defined. 186 * @intf_assoc: interface association descriptor 187 * @minor: the minor number assigned to this interface, if this 188 * interface is bound to a driver that uses the USB major number. 189 * If this interface does not use the USB major, this field should 190 * be unused. The driver should set this value in the probe() 191 * function of the driver, after it has been assigned a minor 192 * number from the USB core by calling usb_register_dev(). 193 * @condition: binding state of the interface: not bound, binding 194 * (in probe()), bound to a driver, or unbinding (in disconnect()) 195 * @sysfs_files_created: sysfs attributes exist 196 * @ep_devs_created: endpoint child pseudo-devices exist 197 * @unregistering: flag set when the interface is being unregistered 198 * @needs_remote_wakeup: flag set when the driver requires remote-wakeup 199 * capability during autosuspend. 200 * @needs_altsetting0: flag set when a set-interface request for altsetting 0 201 * has been deferred. 202 * @needs_binding: flag set when the driver should be re-probed or unbound 203 * following a reset or suspend operation it doesn't support. 204 * @authorized: This allows to (de)authorize individual interfaces instead 205 * a whole device in contrast to the device authorization. 206 * @dev: driver model's view of this device 207 * @usb_dev: if an interface is bound to the USB major, this will point 208 * to the sysfs representation for that device. 209 * @reset_ws: Used for scheduling resets from atomic context. 210 * @resetting_device: USB core reset the device, so use alt setting 0 as 211 * current; needs bandwidth alloc after reset. 212 * @wireless_status: if the USB device uses a receiver/emitter combo, whether 213 * the emitter is connected. 214 * 215 * USB device drivers attach to interfaces on a physical device. Each 216 * interface encapsulates a single high level function, such as feeding 217 * an audio stream to a speaker or reporting a change in a volume control. 218 * Many USB devices only have one interface. The protocol used to talk to 219 * an interface's endpoints can be defined in a usb "class" specification, 220 * or by a product's vendor. The (default) control endpoint is part of 221 * every interface, but is never listed among the interface's descriptors. 222 * 223 * The driver that is bound to the interface can use standard driver model 224 * calls such as dev_get_drvdata() on the dev member of this structure. 225 * 226 * Each interface may have alternate settings. The initial configuration 227 * of a device sets altsetting 0, but the device driver can change 228 * that setting using usb_set_interface(). Alternate settings are often 229 * used to control the use of periodic endpoints, such as by having 230 * different endpoints use different amounts of reserved USB bandwidth. 231 * All standards-conformant USB devices that use isochronous endpoints 232 * will use them in non-default settings. 233 * 234 * The USB specification says that alternate setting numbers must run from 235 * 0 to one less than the total number of alternate settings. But some 236 * devices manage to mess this up, and the structures aren't necessarily 237 * stored in numerical order anyhow. Use usb_altnum_to_altsetting() to 238 * look up an alternate setting in the altsetting array based on its number. 239 */ 240 struct usb_interface { 241 /* array of alternate settings for this interface, 242 * stored in no particular order */ 243 struct usb_host_interface *altsetting; 244 245 struct usb_host_interface *cur_altsetting; /* the currently 246 * active alternate setting */ 247 unsigned num_altsetting; /* number of alternate settings */ 248 249 /* If there is an interface association descriptor then it will list 250 * the associated interfaces */ 251 struct usb_interface_assoc_descriptor *intf_assoc; 252 253 int minor; /* minor number this interface is 254 * bound to */ 255 enum usb_interface_condition condition; /* state of binding */ 256 unsigned sysfs_files_created:1; /* the sysfs attributes exist */ 257 unsigned ep_devs_created:1; /* endpoint "devices" exist */ 258 unsigned unregistering:1; /* unregistration is in progress */ 259 unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */ 260 unsigned needs_altsetting0:1; /* switch to altsetting 0 is pending */ 261 unsigned needs_binding:1; /* needs delayed unbind/rebind */ 262 unsigned resetting_device:1; /* true: bandwidth alloc after reset */ 263 unsigned authorized:1; /* used for interface authorization */ 264 enum usb_wireless_status wireless_status; 265 266 struct device dev; /* interface specific device info */ 267 struct device *usb_dev; 268 struct work_struct reset_ws; /* for resets in atomic context */ 269 }; 270 271 #define to_usb_interface(__dev) container_of_const(__dev, struct usb_interface, dev) 272 273 static inline void *usb_get_intfdata(struct usb_interface *intf) 274 { 275 return dev_get_drvdata(&intf->dev); 276 } 277 278 /** 279 * usb_set_intfdata() - associate driver-specific data with an interface 280 * @intf: USB interface 281 * @data: driver data 282 * 283 * Drivers can use this function in their probe() callbacks to associate 284 * driver-specific data with an interface. 285 * 286 * Note that there is generally no need to clear the driver-data pointer even 287 * if some drivers do so for historical or implementation-specific reasons. 288 */ 289 static inline void usb_set_intfdata(struct usb_interface *intf, void *data) 290 { 291 dev_set_drvdata(&intf->dev, data); 292 } 293 294 struct usb_interface *usb_get_intf(struct usb_interface *intf); 295 void usb_put_intf(struct usb_interface *intf); 296 297 /* Hard limit */ 298 #define USB_MAXENDPOINTS 30 299 /* this maximum is arbitrary */ 300 #define USB_MAXINTERFACES 32 301 #define USB_MAXIADS (USB_MAXINTERFACES/2) 302 303 /* 304 * USB Resume Timer: Every Host controller driver should drive the resume 305 * signalling on the bus for the amount of time defined by this macro. 306 * 307 * That way we will have a 'stable' behavior among all HCDs supported by Linux. 308 * 309 * Note that the USB Specification states we should drive resume for *at least* 310 * 20 ms, but it doesn't give an upper bound. This creates two possible 311 * situations which we want to avoid: 312 * 313 * (a) sometimes an msleep(20) might expire slightly before 20 ms, which causes 314 * us to fail USB Electrical Tests, thus failing Certification 315 * 316 * (b) Some (many) devices actually need more than 20 ms of resume signalling, 317 * and while we can argue that's against the USB Specification, we don't have 318 * control over which devices a certification laboratory will be using for 319 * certification. If CertLab uses a device which was tested against Windows and 320 * that happens to have relaxed resume signalling rules, we might fall into 321 * situations where we fail interoperability and electrical tests. 322 * 323 * In order to avoid both conditions, we're using a 40 ms resume timeout, which 324 * should cope with both LPJ calibration errors and devices not following every 325 * detail of the USB Specification. 326 */ 327 #define USB_RESUME_TIMEOUT 40 /* ms */ 328 329 /** 330 * struct usb_interface_cache - long-term representation of a device interface 331 * @num_altsetting: number of altsettings defined. 332 * @ref: reference counter. 333 * @altsetting: variable-length array of interface structures, one for 334 * each alternate setting that may be selected. Each one includes a 335 * set of endpoint configurations. They will be in no particular order. 336 * 337 * These structures persist for the lifetime of a usb_device, unlike 338 * struct usb_interface (which persists only as long as its configuration 339 * is installed). The altsetting arrays can be accessed through these 340 * structures at any time, permitting comparison of configurations and 341 * providing support for the /sys/kernel/debug/usb/devices pseudo-file. 342 */ 343 struct usb_interface_cache { 344 unsigned num_altsetting; /* number of alternate settings */ 345 struct kref ref; /* reference counter */ 346 347 /* variable-length array of alternate settings for this interface, 348 * stored in no particular order */ 349 struct usb_host_interface altsetting[]; 350 }; 351 #define ref_to_usb_interface_cache(r) \ 352 container_of(r, struct usb_interface_cache, ref) 353 #define altsetting_to_usb_interface_cache(a) \ 354 container_of(a, struct usb_interface_cache, altsetting[0]) 355 356 /** 357 * struct usb_host_config - representation of a device's configuration 358 * @desc: the device's configuration descriptor. 359 * @string: pointer to the cached version of the iConfiguration string, if 360 * present for this configuration. 361 * @intf_assoc: list of any interface association descriptors in this config 362 * @interface: array of pointers to usb_interface structures, one for each 363 * interface in the configuration. The number of interfaces is stored 364 * in desc.bNumInterfaces. These pointers are valid only while the 365 * configuration is active. 366 * @intf_cache: array of pointers to usb_interface_cache structures, one 367 * for each interface in the configuration. These structures exist 368 * for the entire life of the device. 369 * @extra: pointer to buffer containing all extra descriptors associated 370 * with this configuration (those preceding the first interface 371 * descriptor). 372 * @extralen: length of the extra descriptors buffer. 373 * 374 * USB devices may have multiple configurations, but only one can be active 375 * at any time. Each encapsulates a different operational environment; 376 * for example, a dual-speed device would have separate configurations for 377 * full-speed and high-speed operation. The number of configurations 378 * available is stored in the device descriptor as bNumConfigurations. 379 * 380 * A configuration can contain multiple interfaces. Each corresponds to 381 * a different function of the USB device, and all are available whenever 382 * the configuration is active. The USB standard says that interfaces 383 * are supposed to be numbered from 0 to desc.bNumInterfaces-1, but a lot 384 * of devices get this wrong. In addition, the interface array is not 385 * guaranteed to be sorted in numerical order. Use usb_ifnum_to_if() to 386 * look up an interface entry based on its number. 387 * 388 * Device drivers should not attempt to activate configurations. The choice 389 * of which configuration to install is a policy decision based on such 390 * considerations as available power, functionality provided, and the user's 391 * desires (expressed through userspace tools). However, drivers can call 392 * usb_reset_configuration() to reinitialize the current configuration and 393 * all its interfaces. 394 */ 395 struct usb_host_config { 396 struct usb_config_descriptor desc; 397 398 char *string; /* iConfiguration string, if present */ 399 400 /* List of any Interface Association Descriptors in this 401 * configuration. */ 402 struct usb_interface_assoc_descriptor *intf_assoc[USB_MAXIADS]; 403 404 /* the interfaces associated with this configuration, 405 * stored in no particular order */ 406 struct usb_interface *interface[USB_MAXINTERFACES]; 407 408 /* Interface information available even when this is not the 409 * active configuration */ 410 struct usb_interface_cache *intf_cache[USB_MAXINTERFACES]; 411 412 unsigned char *extra; /* Extra descriptors */ 413 int extralen; 414 }; 415 416 /* USB2.0 and USB3.0 device BOS descriptor set */ 417 struct usb_host_bos { 418 struct usb_bos_descriptor *desc; 419 420 /* wireless cap descriptor is handled by wusb */ 421 struct usb_ext_cap_descriptor *ext_cap; 422 struct usb_ss_cap_descriptor *ss_cap; 423 struct usb_ssp_cap_descriptor *ssp_cap; 424 struct usb_ss_container_id_descriptor *ss_id; 425 struct usb_ptm_cap_descriptor *ptm_cap; 426 }; 427 428 int __usb_get_extra_descriptor(char *buffer, unsigned size, 429 unsigned char type, void **ptr, size_t min); 430 #define usb_get_extra_descriptor(ifpoint, type, ptr) \ 431 __usb_get_extra_descriptor((ifpoint)->extra, \ 432 (ifpoint)->extralen, \ 433 type, (void **)ptr, sizeof(**(ptr))) 434 435 /* ----------------------------------------------------------------------- */ 436 437 /* USB device number allocation bitmap */ 438 struct usb_devmap { 439 unsigned long devicemap[128 / (8*sizeof(unsigned long))]; 440 }; 441 442 /* 443 * Allocated per bus (tree of devices) we have: 444 */ 445 struct usb_bus { 446 struct device *controller; /* host side hardware */ 447 struct device *sysdev; /* as seen from firmware or bus */ 448 int busnum; /* Bus number (in order of reg) */ 449 const char *bus_name; /* stable id (PCI slot_name etc) */ 450 u8 uses_pio_for_control; /* 451 * Does the host controller use PIO 452 * for control transfers? 453 */ 454 u8 otg_port; /* 0, or number of OTG/HNP port */ 455 unsigned is_b_host:1; /* true during some HNP roleswitches */ 456 unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */ 457 unsigned no_stop_on_short:1; /* 458 * Quirk: some controllers don't stop 459 * the ep queue on a short transfer 460 * with the URB_SHORT_NOT_OK flag set. 461 */ 462 unsigned no_sg_constraint:1; /* no sg constraint */ 463 unsigned sg_tablesize; /* 0 or largest number of sg list entries */ 464 465 int devnum_next; /* Next open device number in 466 * round-robin allocation */ 467 struct mutex devnum_next_mutex; /* devnum_next mutex */ 468 469 struct usb_devmap devmap; /* device address allocation map */ 470 struct usb_device *root_hub; /* Root hub */ 471 struct usb_bus *hs_companion; /* Companion EHCI bus, if any */ 472 473 int bandwidth_allocated; /* on this bus: how much of the time 474 * reserved for periodic (intr/iso) 475 * requests is used, on average? 476 * Units: microseconds/frame. 477 * Limits: Full/low speed reserve 90%, 478 * while high speed reserves 80%. 479 */ 480 int bandwidth_int_reqs; /* number of Interrupt requests */ 481 int bandwidth_isoc_reqs; /* number of Isoc. requests */ 482 483 unsigned resuming_ports; /* bit array: resuming root-hub ports */ 484 485 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE) 486 struct mon_bus *mon_bus; /* non-null when associated */ 487 int monitored; /* non-zero when monitored */ 488 #endif 489 }; 490 491 struct usb_dev_state; 492 493 /* ----------------------------------------------------------------------- */ 494 495 struct usb_tt; 496 497 enum usb_port_connect_type { 498 USB_PORT_CONNECT_TYPE_UNKNOWN = 0, 499 USB_PORT_CONNECT_TYPE_HOT_PLUG, 500 USB_PORT_CONNECT_TYPE_HARD_WIRED, 501 USB_PORT_NOT_USED, 502 }; 503 504 /* 505 * USB port quirks. 506 */ 507 508 /* For the given port, prefer the old (faster) enumeration scheme. */ 509 #define USB_PORT_QUIRK_OLD_SCHEME BIT(0) 510 511 /* Decrease TRSTRCY to 10ms during device enumeration. */ 512 #define USB_PORT_QUIRK_FAST_ENUM BIT(1) 513 514 /* 515 * USB 2.0 Link Power Management (LPM) parameters. 516 */ 517 struct usb2_lpm_parameters { 518 /* Best effort service latency indicate how long the host will drive 519 * resume on an exit from L1. 520 */ 521 unsigned int besl; 522 523 /* Timeout value in microseconds for the L1 inactivity (LPM) timer. 524 * When the timer counts to zero, the parent hub will initiate a LPM 525 * transition to L1. 526 */ 527 int timeout; 528 }; 529 530 /* 531 * USB 3.0 Link Power Management (LPM) parameters. 532 * 533 * PEL and SEL are USB 3.0 Link PM latencies for device-initiated LPM exit. 534 * MEL is the USB 3.0 Link PM latency for host-initiated LPM exit. 535 * All three are stored in nanoseconds. 536 */ 537 struct usb3_lpm_parameters { 538 /* 539 * Maximum exit latency (MEL) for the host to send a packet to the 540 * device (either a Ping for isoc endpoints, or a data packet for 541 * interrupt endpoints), the hubs to decode the packet, and for all hubs 542 * in the path to transition the links to U0. 543 */ 544 unsigned int mel; 545 /* 546 * Maximum exit latency for a device-initiated LPM transition to bring 547 * all links into U0. Abbreviated as "PEL" in section 9.4.12 of the USB 548 * 3.0 spec, with no explanation of what "P" stands for. "Path"? 549 */ 550 unsigned int pel; 551 552 /* 553 * The System Exit Latency (SEL) includes PEL, and three other 554 * latencies. After a device initiates a U0 transition, it will take 555 * some time from when the device sends the ERDY to when it will finally 556 * receive the data packet. Basically, SEL should be the worse-case 557 * latency from when a device starts initiating a U0 transition to when 558 * it will get data. 559 */ 560 unsigned int sel; 561 /* 562 * The idle timeout value that is currently programmed into the parent 563 * hub for this device. When the timer counts to zero, the parent hub 564 * will initiate an LPM transition to either U1 or U2. 565 */ 566 int timeout; 567 }; 568 569 /** 570 * struct usb_device - kernel's representation of a USB device 571 * @devnum: device number; address on a USB bus 572 * @devpath: device ID string for use in messages (e.g., /port/...) 573 * @route: tree topology hex string for use with xHCI 574 * @state: device state: configured, not attached, etc. 575 * @speed: device speed: high/full/low (or error) 576 * @rx_lanes: number of rx lanes in use, USB 3.2 adds dual-lane support 577 * @tx_lanes: number of tx lanes in use, USB 3.2 adds dual-lane support 578 * @ssp_rate: SuperSpeed Plus phy signaling rate and lane count 579 * @tt: Transaction Translator info; used with low/full speed dev, highspeed hub 580 * @ttport: device port on that tt hub 581 * @toggle: one bit for each endpoint, with ([0] = IN, [1] = OUT) endpoints 582 * @parent: our hub, unless we're the root 583 * @bus: bus we're part of 584 * @ep0: endpoint 0 data (default control pipe) 585 * @dev: generic device interface 586 * @descriptor: USB device descriptor 587 * @bos: USB device BOS descriptor set 588 * @config: all of the device's configs 589 * @actconfig: the active configuration 590 * @ep_in: array of IN endpoints 591 * @ep_out: array of OUT endpoints 592 * @rawdescriptors: raw descriptors for each config 593 * @bus_mA: Current available from the bus 594 * @portnum: parent port number (origin 1) 595 * @level: number of USB hub ancestors 596 * @devaddr: device address, XHCI: assigned by HW, others: same as devnum 597 * @can_submit: URBs may be submitted 598 * @persist_enabled: USB_PERSIST enabled for this device 599 * @reset_in_progress: the device is being reset 600 * @have_langid: whether string_langid is valid 601 * @authorized: policy has said we can use it; 602 * (user space) policy determines if we authorize this device to be 603 * used or not. By default, wired USB devices are authorized. 604 * WUSB devices are not, until we authorize them from user space. 605 * FIXME -- complete doc 606 * @authenticated: Crypto authentication passed 607 * @wusb: device is Wireless USB 608 * @lpm_capable: device supports LPM 609 * @lpm_devinit_allow: Allow USB3 device initiated LPM, exit latency is in range 610 * @usb2_hw_lpm_capable: device can perform USB2 hardware LPM 611 * @usb2_hw_lpm_besl_capable: device can perform USB2 hardware BESL LPM 612 * @usb2_hw_lpm_enabled: USB2 hardware LPM is enabled 613 * @usb2_hw_lpm_allowed: Userspace allows USB 2.0 LPM to be enabled 614 * @usb3_lpm_u1_enabled: USB3 hardware U1 LPM enabled 615 * @usb3_lpm_u2_enabled: USB3 hardware U2 LPM enabled 616 * @string_langid: language ID for strings 617 * @product: iProduct string, if present (static) 618 * @manufacturer: iManufacturer string, if present (static) 619 * @serial: iSerialNumber string, if present (static) 620 * @filelist: usbfs files that are open to this device 621 * @maxchild: number of ports if hub 622 * @quirks: quirks of the whole device 623 * @urbnum: number of URBs submitted for the whole device 624 * @active_duration: total time device is not suspended 625 * @connect_time: time device was first connected 626 * @do_remote_wakeup: remote wakeup should be enabled 627 * @reset_resume: needs reset instead of resume 628 * @port_is_suspended: the upstream port is suspended (L2 or U3) 629 * @wusb_dev: if this is a Wireless USB device, link to the WUSB 630 * specific data for the device. 631 * @slot_id: Slot ID assigned by xHCI 632 * @removable: Device can be physically removed from this port 633 * @l1_params: best effor service latency for USB2 L1 LPM state, and L1 timeout. 634 * @u1_params: exit latencies for USB3 U1 LPM state, and hub-initiated timeout. 635 * @u2_params: exit latencies for USB3 U2 LPM state, and hub-initiated timeout. 636 * @lpm_disable_count: Ref count used by usb_disable_lpm() and usb_enable_lpm() 637 * to keep track of the number of functions that require USB 3.0 Link Power 638 * Management to be disabled for this usb_device. This count should only 639 * be manipulated by those functions, with the bandwidth_mutex is held. 640 * @hub_delay: cached value consisting of: 641 * parent->hub_delay + wHubDelay + tTPTransmissionDelay (40ns) 642 * Will be used as wValue for SetIsochDelay requests. 643 * @use_generic_driver: ask driver core to reprobe using the generic driver. 644 * 645 * Notes: 646 * Usbcore drivers should not set usbdev->state directly. Instead use 647 * usb_set_device_state(). 648 */ 649 struct usb_device { 650 int devnum; 651 char devpath[16]; 652 u32 route; 653 enum usb_device_state state; 654 enum usb_device_speed speed; 655 unsigned int rx_lanes; 656 unsigned int tx_lanes; 657 enum usb_ssp_rate ssp_rate; 658 659 struct usb_tt *tt; 660 int ttport; 661 662 unsigned int toggle[2]; 663 664 struct usb_device *parent; 665 struct usb_bus *bus; 666 struct usb_host_endpoint ep0; 667 668 struct device dev; 669 670 struct usb_device_descriptor descriptor; 671 struct usb_host_bos *bos; 672 struct usb_host_config *config; 673 674 struct usb_host_config *actconfig; 675 struct usb_host_endpoint *ep_in[16]; 676 struct usb_host_endpoint *ep_out[16]; 677 678 char **rawdescriptors; 679 680 unsigned short bus_mA; 681 u8 portnum; 682 u8 level; 683 u8 devaddr; 684 685 unsigned can_submit:1; 686 unsigned persist_enabled:1; 687 unsigned reset_in_progress:1; 688 unsigned have_langid:1; 689 unsigned authorized:1; 690 unsigned authenticated:1; 691 unsigned wusb:1; 692 unsigned lpm_capable:1; 693 unsigned lpm_devinit_allow:1; 694 unsigned usb2_hw_lpm_capable:1; 695 unsigned usb2_hw_lpm_besl_capable:1; 696 unsigned usb2_hw_lpm_enabled:1; 697 unsigned usb2_hw_lpm_allowed:1; 698 unsigned usb3_lpm_u1_enabled:1; 699 unsigned usb3_lpm_u2_enabled:1; 700 int string_langid; 701 702 /* static strings from the device */ 703 char *product; 704 char *manufacturer; 705 char *serial; 706 707 struct list_head filelist; 708 709 int maxchild; 710 711 u32 quirks; 712 atomic_t urbnum; 713 714 unsigned long active_duration; 715 716 #ifdef CONFIG_PM 717 unsigned long connect_time; 718 719 unsigned do_remote_wakeup:1; 720 unsigned reset_resume:1; 721 unsigned port_is_suspended:1; 722 #endif 723 struct wusb_dev *wusb_dev; 724 int slot_id; 725 struct usb2_lpm_parameters l1_params; 726 struct usb3_lpm_parameters u1_params; 727 struct usb3_lpm_parameters u2_params; 728 unsigned lpm_disable_count; 729 730 u16 hub_delay; 731 unsigned use_generic_driver:1; 732 }; 733 734 #define to_usb_device(__dev) container_of_const(__dev, struct usb_device, dev) 735 736 static inline struct usb_device *__intf_to_usbdev(struct usb_interface *intf) 737 { 738 return to_usb_device(intf->dev.parent); 739 } 740 static inline const struct usb_device *__intf_to_usbdev_const(const struct usb_interface *intf) 741 { 742 return to_usb_device((const struct device *)intf->dev.parent); 743 } 744 745 #define interface_to_usbdev(intf) \ 746 _Generic((intf), \ 747 const struct usb_interface *: __intf_to_usbdev_const, \ 748 struct usb_interface *: __intf_to_usbdev)(intf) 749 750 extern struct usb_device *usb_get_dev(struct usb_device *dev); 751 extern void usb_put_dev(struct usb_device *dev); 752 extern struct usb_device *usb_hub_find_child(struct usb_device *hdev, 753 int port1); 754 755 /** 756 * usb_hub_for_each_child - iterate over all child devices on the hub 757 * @hdev: USB device belonging to the usb hub 758 * @port1: portnum associated with child device 759 * @child: child device pointer 760 */ 761 #define usb_hub_for_each_child(hdev, port1, child) \ 762 for (port1 = 1, child = usb_hub_find_child(hdev, port1); \ 763 port1 <= hdev->maxchild; \ 764 child = usb_hub_find_child(hdev, ++port1)) \ 765 if (!child) continue; else 766 767 /* USB device locking */ 768 #define usb_lock_device(udev) device_lock(&(udev)->dev) 769 #define usb_unlock_device(udev) device_unlock(&(udev)->dev) 770 #define usb_lock_device_interruptible(udev) device_lock_interruptible(&(udev)->dev) 771 #define usb_trylock_device(udev) device_trylock(&(udev)->dev) 772 extern int usb_lock_device_for_reset(struct usb_device *udev, 773 const struct usb_interface *iface); 774 775 /* USB port reset for device reinitialization */ 776 extern int usb_reset_device(struct usb_device *dev); 777 extern void usb_queue_reset_device(struct usb_interface *dev); 778 779 extern struct device *usb_intf_get_dma_device(struct usb_interface *intf); 780 781 #ifdef CONFIG_ACPI 782 extern int usb_acpi_set_power_state(struct usb_device *hdev, int index, 783 bool enable); 784 extern bool usb_acpi_power_manageable(struct usb_device *hdev, int index); 785 extern int usb_acpi_port_lpm_incapable(struct usb_device *hdev, int index); 786 #else 787 static inline int usb_acpi_set_power_state(struct usb_device *hdev, int index, 788 bool enable) { return 0; } 789 static inline bool usb_acpi_power_manageable(struct usb_device *hdev, int index) 790 { return true; } 791 static inline int usb_acpi_port_lpm_incapable(struct usb_device *hdev, int index) 792 { return 0; } 793 #endif 794 795 /* USB autosuspend and autoresume */ 796 #ifdef CONFIG_PM 797 extern void usb_enable_autosuspend(struct usb_device *udev); 798 extern void usb_disable_autosuspend(struct usb_device *udev); 799 800 extern int usb_autopm_get_interface(struct usb_interface *intf); 801 extern void usb_autopm_put_interface(struct usb_interface *intf); 802 extern int usb_autopm_get_interface_async(struct usb_interface *intf); 803 extern void usb_autopm_put_interface_async(struct usb_interface *intf); 804 extern void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 805 extern void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 806 807 static inline void usb_mark_last_busy(struct usb_device *udev) 808 { 809 pm_runtime_mark_last_busy(&udev->dev); 810 } 811 812 #else 813 814 static inline int usb_enable_autosuspend(struct usb_device *udev) 815 { return 0; } 816 static inline int usb_disable_autosuspend(struct usb_device *udev) 817 { return 0; } 818 819 static inline int usb_autopm_get_interface(struct usb_interface *intf) 820 { return 0; } 821 static inline int usb_autopm_get_interface_async(struct usb_interface *intf) 822 { return 0; } 823 824 static inline void usb_autopm_put_interface(struct usb_interface *intf) 825 { } 826 static inline void usb_autopm_put_interface_async(struct usb_interface *intf) 827 { } 828 static inline void usb_autopm_get_interface_no_resume( 829 struct usb_interface *intf) 830 { } 831 static inline void usb_autopm_put_interface_no_suspend( 832 struct usb_interface *intf) 833 { } 834 static inline void usb_mark_last_busy(struct usb_device *udev) 835 { } 836 #endif 837 838 extern int usb_disable_lpm(struct usb_device *udev); 839 extern void usb_enable_lpm(struct usb_device *udev); 840 /* Same as above, but these functions lock/unlock the bandwidth_mutex. */ 841 extern int usb_unlocked_disable_lpm(struct usb_device *udev); 842 extern void usb_unlocked_enable_lpm(struct usb_device *udev); 843 844 extern int usb_disable_ltm(struct usb_device *udev); 845 extern void usb_enable_ltm(struct usb_device *udev); 846 847 static inline bool usb_device_supports_ltm(struct usb_device *udev) 848 { 849 if (udev->speed < USB_SPEED_SUPER || !udev->bos || !udev->bos->ss_cap) 850 return false; 851 return udev->bos->ss_cap->bmAttributes & USB_LTM_SUPPORT; 852 } 853 854 static inline bool usb_device_no_sg_constraint(struct usb_device *udev) 855 { 856 return udev && udev->bus && udev->bus->no_sg_constraint; 857 } 858 859 860 /*-------------------------------------------------------------------------*/ 861 862 /* for drivers using iso endpoints */ 863 extern int usb_get_current_frame_number(struct usb_device *usb_dev); 864 865 /* Sets up a group of bulk endpoints to support multiple stream IDs. */ 866 extern int usb_alloc_streams(struct usb_interface *interface, 867 struct usb_host_endpoint **eps, unsigned int num_eps, 868 unsigned int num_streams, gfp_t mem_flags); 869 870 /* Reverts a group of bulk endpoints back to not using stream IDs. */ 871 extern int usb_free_streams(struct usb_interface *interface, 872 struct usb_host_endpoint **eps, unsigned int num_eps, 873 gfp_t mem_flags); 874 875 /* used these for multi-interface device registration */ 876 extern int usb_driver_claim_interface(struct usb_driver *driver, 877 struct usb_interface *iface, void *data); 878 879 /** 880 * usb_interface_claimed - returns true iff an interface is claimed 881 * @iface: the interface being checked 882 * 883 * Return: %true (nonzero) iff the interface is claimed, else %false 884 * (zero). 885 * 886 * Note: 887 * Callers must own the driver model's usb bus readlock. So driver 888 * probe() entries don't need extra locking, but other call contexts 889 * may need to explicitly claim that lock. 890 * 891 */ 892 static inline int usb_interface_claimed(struct usb_interface *iface) 893 { 894 return (iface->dev.driver != NULL); 895 } 896 897 extern void usb_driver_release_interface(struct usb_driver *driver, 898 struct usb_interface *iface); 899 const struct usb_device_id *usb_match_id(struct usb_interface *interface, 900 const struct usb_device_id *id); 901 extern int usb_match_one_id(struct usb_interface *interface, 902 const struct usb_device_id *id); 903 904 extern int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *)); 905 extern struct usb_interface *usb_find_interface(struct usb_driver *drv, 906 int minor); 907 extern struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev, 908 unsigned ifnum); 909 extern struct usb_host_interface *usb_altnum_to_altsetting( 910 const struct usb_interface *intf, unsigned int altnum); 911 extern struct usb_host_interface *usb_find_alt_setting( 912 struct usb_host_config *config, 913 unsigned int iface_num, 914 unsigned int alt_num); 915 916 /* port claiming functions */ 917 int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 918 struct usb_dev_state *owner); 919 int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 920 struct usb_dev_state *owner); 921 922 /** 923 * usb_make_path - returns stable device path in the usb tree 924 * @dev: the device whose path is being constructed 925 * @buf: where to put the string 926 * @size: how big is "buf"? 927 * 928 * Return: Length of the string (> 0) or negative if size was too small. 929 * 930 * Note: 931 * This identifier is intended to be "stable", reflecting physical paths in 932 * hardware such as physical bus addresses for host controllers or ports on 933 * USB hubs. That makes it stay the same until systems are physically 934 * reconfigured, by re-cabling a tree of USB devices or by moving USB host 935 * controllers. Adding and removing devices, including virtual root hubs 936 * in host controller driver modules, does not change these path identifiers; 937 * neither does rebooting or re-enumerating. These are more useful identifiers 938 * than changeable ("unstable") ones like bus numbers or device addresses. 939 * 940 * With a partial exception for devices connected to USB 2.0 root hubs, these 941 * identifiers are also predictable. So long as the device tree isn't changed, 942 * plugging any USB device into a given hub port always gives it the same path. 943 * Because of the use of "companion" controllers, devices connected to ports on 944 * USB 2.0 root hubs (EHCI host controllers) will get one path ID if they are 945 * high speed, and a different one if they are full or low speed. 946 */ 947 static inline int usb_make_path(struct usb_device *dev, char *buf, size_t size) 948 { 949 int actual; 950 actual = snprintf(buf, size, "usb-%s-%s", dev->bus->bus_name, 951 dev->devpath); 952 return (actual >= (int)size) ? -1 : actual; 953 } 954 955 /*-------------------------------------------------------------------------*/ 956 957 #define USB_DEVICE_ID_MATCH_DEVICE \ 958 (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT) 959 #define USB_DEVICE_ID_MATCH_DEV_RANGE \ 960 (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI) 961 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \ 962 (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE) 963 #define USB_DEVICE_ID_MATCH_DEV_INFO \ 964 (USB_DEVICE_ID_MATCH_DEV_CLASS | \ 965 USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \ 966 USB_DEVICE_ID_MATCH_DEV_PROTOCOL) 967 #define USB_DEVICE_ID_MATCH_INT_INFO \ 968 (USB_DEVICE_ID_MATCH_INT_CLASS | \ 969 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 970 USB_DEVICE_ID_MATCH_INT_PROTOCOL) 971 972 /** 973 * USB_DEVICE - macro used to describe a specific usb device 974 * @vend: the 16 bit USB Vendor ID 975 * @prod: the 16 bit USB Product ID 976 * 977 * This macro is used to create a struct usb_device_id that matches a 978 * specific device. 979 */ 980 #define USB_DEVICE(vend, prod) \ 981 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \ 982 .idVendor = (vend), \ 983 .idProduct = (prod) 984 /** 985 * USB_DEVICE_VER - describe a specific usb device with a version range 986 * @vend: the 16 bit USB Vendor ID 987 * @prod: the 16 bit USB Product ID 988 * @lo: the bcdDevice_lo value 989 * @hi: the bcdDevice_hi value 990 * 991 * This macro is used to create a struct usb_device_id that matches a 992 * specific device, with a version range. 993 */ 994 #define USB_DEVICE_VER(vend, prod, lo, hi) \ 995 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \ 996 .idVendor = (vend), \ 997 .idProduct = (prod), \ 998 .bcdDevice_lo = (lo), \ 999 .bcdDevice_hi = (hi) 1000 1001 /** 1002 * USB_DEVICE_INTERFACE_CLASS - describe a usb device with a specific interface class 1003 * @vend: the 16 bit USB Vendor ID 1004 * @prod: the 16 bit USB Product ID 1005 * @cl: bInterfaceClass value 1006 * 1007 * This macro is used to create a struct usb_device_id that matches a 1008 * specific interface class of devices. 1009 */ 1010 #define USB_DEVICE_INTERFACE_CLASS(vend, prod, cl) \ 1011 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 1012 USB_DEVICE_ID_MATCH_INT_CLASS, \ 1013 .idVendor = (vend), \ 1014 .idProduct = (prod), \ 1015 .bInterfaceClass = (cl) 1016 1017 /** 1018 * USB_DEVICE_INTERFACE_PROTOCOL - describe a usb device with a specific interface protocol 1019 * @vend: the 16 bit USB Vendor ID 1020 * @prod: the 16 bit USB Product ID 1021 * @pr: bInterfaceProtocol value 1022 * 1023 * This macro is used to create a struct usb_device_id that matches a 1024 * specific interface protocol of devices. 1025 */ 1026 #define USB_DEVICE_INTERFACE_PROTOCOL(vend, prod, pr) \ 1027 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 1028 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ 1029 .idVendor = (vend), \ 1030 .idProduct = (prod), \ 1031 .bInterfaceProtocol = (pr) 1032 1033 /** 1034 * USB_DEVICE_INTERFACE_NUMBER - describe a usb device with a specific interface number 1035 * @vend: the 16 bit USB Vendor ID 1036 * @prod: the 16 bit USB Product ID 1037 * @num: bInterfaceNumber value 1038 * 1039 * This macro is used to create a struct usb_device_id that matches a 1040 * specific interface number of devices. 1041 */ 1042 #define USB_DEVICE_INTERFACE_NUMBER(vend, prod, num) \ 1043 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 1044 USB_DEVICE_ID_MATCH_INT_NUMBER, \ 1045 .idVendor = (vend), \ 1046 .idProduct = (prod), \ 1047 .bInterfaceNumber = (num) 1048 1049 /** 1050 * USB_DEVICE_INFO - macro used to describe a class of usb devices 1051 * @cl: bDeviceClass value 1052 * @sc: bDeviceSubClass value 1053 * @pr: bDeviceProtocol value 1054 * 1055 * This macro is used to create a struct usb_device_id that matches a 1056 * specific class of devices. 1057 */ 1058 #define USB_DEVICE_INFO(cl, sc, pr) \ 1059 .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, \ 1060 .bDeviceClass = (cl), \ 1061 .bDeviceSubClass = (sc), \ 1062 .bDeviceProtocol = (pr) 1063 1064 /** 1065 * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces 1066 * @cl: bInterfaceClass value 1067 * @sc: bInterfaceSubClass value 1068 * @pr: bInterfaceProtocol value 1069 * 1070 * This macro is used to create a struct usb_device_id that matches a 1071 * specific class of interfaces. 1072 */ 1073 #define USB_INTERFACE_INFO(cl, sc, pr) \ 1074 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, \ 1075 .bInterfaceClass = (cl), \ 1076 .bInterfaceSubClass = (sc), \ 1077 .bInterfaceProtocol = (pr) 1078 1079 /** 1080 * USB_DEVICE_AND_INTERFACE_INFO - describe a specific usb device with a class of usb interfaces 1081 * @vend: the 16 bit USB Vendor ID 1082 * @prod: the 16 bit USB Product ID 1083 * @cl: bInterfaceClass value 1084 * @sc: bInterfaceSubClass value 1085 * @pr: bInterfaceProtocol value 1086 * 1087 * This macro is used to create a struct usb_device_id that matches a 1088 * specific device with a specific class of interfaces. 1089 * 1090 * This is especially useful when explicitly matching devices that have 1091 * vendor specific bDeviceClass values, but standards-compliant interfaces. 1092 */ 1093 #define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, cl, sc, pr) \ 1094 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ 1095 | USB_DEVICE_ID_MATCH_DEVICE, \ 1096 .idVendor = (vend), \ 1097 .idProduct = (prod), \ 1098 .bInterfaceClass = (cl), \ 1099 .bInterfaceSubClass = (sc), \ 1100 .bInterfaceProtocol = (pr) 1101 1102 /** 1103 * USB_VENDOR_AND_INTERFACE_INFO - describe a specific usb vendor with a class of usb interfaces 1104 * @vend: the 16 bit USB Vendor ID 1105 * @cl: bInterfaceClass value 1106 * @sc: bInterfaceSubClass value 1107 * @pr: bInterfaceProtocol value 1108 * 1109 * This macro is used to create a struct usb_device_id that matches a 1110 * specific vendor with a specific class of interfaces. 1111 * 1112 * This is especially useful when explicitly matching devices that have 1113 * vendor specific bDeviceClass values, but standards-compliant interfaces. 1114 */ 1115 #define USB_VENDOR_AND_INTERFACE_INFO(vend, cl, sc, pr) \ 1116 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ 1117 | USB_DEVICE_ID_MATCH_VENDOR, \ 1118 .idVendor = (vend), \ 1119 .bInterfaceClass = (cl), \ 1120 .bInterfaceSubClass = (sc), \ 1121 .bInterfaceProtocol = (pr) 1122 1123 /* ----------------------------------------------------------------------- */ 1124 1125 /* Stuff for dynamic usb ids */ 1126 struct usb_dynids { 1127 spinlock_t lock; 1128 struct list_head list; 1129 }; 1130 1131 struct usb_dynid { 1132 struct list_head node; 1133 struct usb_device_id id; 1134 }; 1135 1136 extern ssize_t usb_store_new_id(struct usb_dynids *dynids, 1137 const struct usb_device_id *id_table, 1138 struct device_driver *driver, 1139 const char *buf, size_t count); 1140 1141 extern ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf); 1142 1143 /** 1144 * struct usbdrv_wrap - wrapper for driver-model structure 1145 * @driver: The driver-model core driver structure. 1146 * @for_devices: Non-zero for device drivers, 0 for interface drivers. 1147 */ 1148 struct usbdrv_wrap { 1149 struct device_driver driver; 1150 int for_devices; 1151 }; 1152 1153 /** 1154 * struct usb_driver - identifies USB interface driver to usbcore 1155 * @name: The driver name should be unique among USB drivers, 1156 * and should normally be the same as the module name. 1157 * @probe: Called to see if the driver is willing to manage a particular 1158 * interface on a device. If it is, probe returns zero and uses 1159 * usb_set_intfdata() to associate driver-specific data with the 1160 * interface. It may also use usb_set_interface() to specify the 1161 * appropriate altsetting. If unwilling to manage the interface, 1162 * return -ENODEV, if genuine IO errors occurred, an appropriate 1163 * negative errno value. 1164 * @disconnect: Called when the interface is no longer accessible, usually 1165 * because its device has been (or is being) disconnected or the 1166 * driver module is being unloaded. 1167 * @unlocked_ioctl: Used for drivers that want to talk to userspace through 1168 * the "usbfs" filesystem. This lets devices provide ways to 1169 * expose information to user space regardless of where they 1170 * do (or don't) show up otherwise in the filesystem. 1171 * @suspend: Called when the device is going to be suspended by the 1172 * system either from system sleep or runtime suspend context. The 1173 * return value will be ignored in system sleep context, so do NOT 1174 * try to continue using the device if suspend fails in this case. 1175 * Instead, let the resume or reset-resume routine recover from 1176 * the failure. 1177 * @resume: Called when the device is being resumed by the system. 1178 * @reset_resume: Called when the suspended device has been reset instead 1179 * of being resumed. 1180 * @pre_reset: Called by usb_reset_device() when the device is about to be 1181 * reset. This routine must not return until the driver has no active 1182 * URBs for the device, and no more URBs may be submitted until the 1183 * post_reset method is called. 1184 * @post_reset: Called by usb_reset_device() after the device 1185 * has been reset 1186 * @id_table: USB drivers use ID table to support hotplugging. 1187 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set 1188 * or your driver's probe function will never get called. 1189 * @dev_groups: Attributes attached to the device that will be created once it 1190 * is bound to the driver. 1191 * @dynids: used internally to hold the list of dynamically added device 1192 * ids for this driver. 1193 * @drvwrap: Driver-model core structure wrapper. 1194 * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be 1195 * added to this driver by preventing the sysfs file from being created. 1196 * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend 1197 * for interfaces bound to this driver. 1198 * @soft_unbind: if set to 1, the USB core will not kill URBs and disable 1199 * endpoints before calling the driver's disconnect method. 1200 * @disable_hub_initiated_lpm: if set to 1, the USB core will not allow hubs 1201 * to initiate lower power link state transitions when an idle timeout 1202 * occurs. Device-initiated USB 3.0 link PM will still be allowed. 1203 * 1204 * USB interface drivers must provide a name, probe() and disconnect() 1205 * methods, and an id_table. Other driver fields are optional. 1206 * 1207 * The id_table is used in hotplugging. It holds a set of descriptors, 1208 * and specialized data may be associated with each entry. That table 1209 * is used by both user and kernel mode hotplugging support. 1210 * 1211 * The probe() and disconnect() methods are called in a context where 1212 * they can sleep, but they should avoid abusing the privilege. Most 1213 * work to connect to a device should be done when the device is opened, 1214 * and undone at the last close. The disconnect code needs to address 1215 * concurrency issues with respect to open() and close() methods, as 1216 * well as forcing all pending I/O requests to complete (by unlinking 1217 * them as necessary, and blocking until the unlinks complete). 1218 */ 1219 struct usb_driver { 1220 const char *name; 1221 1222 int (*probe) (struct usb_interface *intf, 1223 const struct usb_device_id *id); 1224 1225 void (*disconnect) (struct usb_interface *intf); 1226 1227 int (*unlocked_ioctl) (struct usb_interface *intf, unsigned int code, 1228 void *buf); 1229 1230 int (*suspend) (struct usb_interface *intf, pm_message_t message); 1231 int (*resume) (struct usb_interface *intf); 1232 int (*reset_resume)(struct usb_interface *intf); 1233 1234 int (*pre_reset)(struct usb_interface *intf); 1235 int (*post_reset)(struct usb_interface *intf); 1236 1237 const struct usb_device_id *id_table; 1238 const struct attribute_group **dev_groups; 1239 1240 struct usb_dynids dynids; 1241 struct usbdrv_wrap drvwrap; 1242 unsigned int no_dynamic_id:1; 1243 unsigned int supports_autosuspend:1; 1244 unsigned int disable_hub_initiated_lpm:1; 1245 unsigned int soft_unbind:1; 1246 }; 1247 #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver) 1248 1249 /** 1250 * struct usb_device_driver - identifies USB device driver to usbcore 1251 * @name: The driver name should be unique among USB drivers, 1252 * and should normally be the same as the module name. 1253 * @match: If set, used for better device/driver matching. 1254 * @probe: Called to see if the driver is willing to manage a particular 1255 * device. If it is, probe returns zero and uses dev_set_drvdata() 1256 * to associate driver-specific data with the device. If unwilling 1257 * to manage the device, return a negative errno value. 1258 * @disconnect: Called when the device is no longer accessible, usually 1259 * because it has been (or is being) disconnected or the driver's 1260 * module is being unloaded. 1261 * @suspend: Called when the device is going to be suspended by the system. 1262 * @resume: Called when the device is being resumed by the system. 1263 * @dev_groups: Attributes attached to the device that will be created once it 1264 * is bound to the driver. 1265 * @drvwrap: Driver-model core structure wrapper. 1266 * @id_table: used with @match() to select better matching driver at 1267 * probe() time. 1268 * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend 1269 * for devices bound to this driver. 1270 * @generic_subclass: if set to 1, the generic USB driver's probe, disconnect, 1271 * resume and suspend functions will be called in addition to the driver's 1272 * own, so this part of the setup does not need to be replicated. 1273 * 1274 * USB drivers must provide all the fields listed above except drvwrap, 1275 * match, and id_table. 1276 */ 1277 struct usb_device_driver { 1278 const char *name; 1279 1280 bool (*match) (struct usb_device *udev); 1281 int (*probe) (struct usb_device *udev); 1282 void (*disconnect) (struct usb_device *udev); 1283 1284 int (*suspend) (struct usb_device *udev, pm_message_t message); 1285 int (*resume) (struct usb_device *udev, pm_message_t message); 1286 const struct attribute_group **dev_groups; 1287 struct usbdrv_wrap drvwrap; 1288 const struct usb_device_id *id_table; 1289 unsigned int supports_autosuspend:1; 1290 unsigned int generic_subclass:1; 1291 }; 1292 #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ 1293 drvwrap.driver) 1294 1295 /** 1296 * struct usb_class_driver - identifies a USB driver that wants to use the USB major number 1297 * @name: the usb class device name for this driver. Will show up in sysfs. 1298 * @devnode: Callback to provide a naming hint for a possible 1299 * device node to create. 1300 * @fops: pointer to the struct file_operations of this driver. 1301 * @minor_base: the start of the minor range for this driver. 1302 * 1303 * This structure is used for the usb_register_dev() and 1304 * usb_deregister_dev() functions, to consolidate a number of the 1305 * parameters used for them. 1306 */ 1307 struct usb_class_driver { 1308 char *name; 1309 char *(*devnode)(const struct device *dev, umode_t *mode); 1310 const struct file_operations *fops; 1311 int minor_base; 1312 }; 1313 1314 /* 1315 * use these in module_init()/module_exit() 1316 * and don't forget MODULE_DEVICE_TABLE(usb, ...) 1317 */ 1318 extern int usb_register_driver(struct usb_driver *, struct module *, 1319 const char *); 1320 1321 /* use a define to avoid include chaining to get THIS_MODULE & friends */ 1322 #define usb_register(driver) \ 1323 usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME) 1324 1325 extern void usb_deregister(struct usb_driver *); 1326 1327 /** 1328 * module_usb_driver() - Helper macro for registering a USB driver 1329 * @__usb_driver: usb_driver struct 1330 * 1331 * Helper macro for USB drivers which do not do anything special in module 1332 * init/exit. This eliminates a lot of boilerplate. Each module may only 1333 * use this macro once, and calling it replaces module_init() and module_exit() 1334 */ 1335 #define module_usb_driver(__usb_driver) \ 1336 module_driver(__usb_driver, usb_register, \ 1337 usb_deregister) 1338 1339 extern int usb_register_device_driver(struct usb_device_driver *, 1340 struct module *); 1341 extern void usb_deregister_device_driver(struct usb_device_driver *); 1342 1343 extern int usb_register_dev(struct usb_interface *intf, 1344 struct usb_class_driver *class_driver); 1345 extern void usb_deregister_dev(struct usb_interface *intf, 1346 struct usb_class_driver *class_driver); 1347 1348 extern int usb_disabled(void); 1349 1350 /* ----------------------------------------------------------------------- */ 1351 1352 /* 1353 * URB support, for asynchronous request completions 1354 */ 1355 1356 /* 1357 * urb->transfer_flags: 1358 * 1359 * Note: URB_DIR_IN/OUT is automatically set in usb_submit_urb(). 1360 */ 1361 #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */ 1362 #define URB_ISO_ASAP 0x0002 /* iso-only; use the first unexpired 1363 * slot in the schedule */ 1364 #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */ 1365 #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */ 1366 #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt 1367 * needed */ 1368 #define URB_FREE_BUFFER 0x0100 /* Free transfer buffer with the URB */ 1369 1370 /* The following flags are used internally by usbcore and HCDs */ 1371 #define URB_DIR_IN 0x0200 /* Transfer from device to host */ 1372 #define URB_DIR_OUT 0 1373 #define URB_DIR_MASK URB_DIR_IN 1374 1375 #define URB_DMA_MAP_SINGLE 0x00010000 /* Non-scatter-gather mapping */ 1376 #define URB_DMA_MAP_PAGE 0x00020000 /* HCD-unsupported S-G */ 1377 #define URB_DMA_MAP_SG 0x00040000 /* HCD-supported S-G */ 1378 #define URB_MAP_LOCAL 0x00080000 /* HCD-local-memory mapping */ 1379 #define URB_SETUP_MAP_SINGLE 0x00100000 /* Setup packet DMA mapped */ 1380 #define URB_SETUP_MAP_LOCAL 0x00200000 /* HCD-local setup packet */ 1381 #define URB_DMA_SG_COMBINED 0x00400000 /* S-G entries were combined */ 1382 #define URB_ALIGNED_TEMP_BUFFER 0x00800000 /* Temp buffer was alloc'd */ 1383 1384 struct usb_iso_packet_descriptor { 1385 unsigned int offset; 1386 unsigned int length; /* expected length */ 1387 unsigned int actual_length; 1388 int status; 1389 }; 1390 1391 struct urb; 1392 1393 struct usb_anchor { 1394 struct list_head urb_list; 1395 wait_queue_head_t wait; 1396 spinlock_t lock; 1397 atomic_t suspend_wakeups; 1398 unsigned int poisoned:1; 1399 }; 1400 1401 static inline void init_usb_anchor(struct usb_anchor *anchor) 1402 { 1403 memset(anchor, 0, sizeof(*anchor)); 1404 INIT_LIST_HEAD(&anchor->urb_list); 1405 init_waitqueue_head(&anchor->wait); 1406 spin_lock_init(&anchor->lock); 1407 } 1408 1409 typedef void (*usb_complete_t)(struct urb *); 1410 1411 /** 1412 * struct urb - USB Request Block 1413 * @urb_list: For use by current owner of the URB. 1414 * @anchor_list: membership in the list of an anchor 1415 * @anchor: to anchor URBs to a common mooring 1416 * @ep: Points to the endpoint's data structure. Will eventually 1417 * replace @pipe. 1418 * @pipe: Holds endpoint number, direction, type, and more. 1419 * Create these values with the eight macros available; 1420 * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl" 1421 * (control), "bulk", "int" (interrupt), or "iso" (isochronous). 1422 * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint 1423 * numbers range from zero to fifteen. Note that "in" endpoint two 1424 * is a different endpoint (and pipe) from "out" endpoint two. 1425 * The current configuration controls the existence, type, and 1426 * maximum packet size of any given endpoint. 1427 * @stream_id: the endpoint's stream ID for bulk streams 1428 * @dev: Identifies the USB device to perform the request. 1429 * @status: This is read in non-iso completion functions to get the 1430 * status of the particular request. ISO requests only use it 1431 * to tell whether the URB was unlinked; detailed status for 1432 * each frame is in the fields of the iso_frame-desc. 1433 * @transfer_flags: A variety of flags may be used to affect how URB 1434 * submission, unlinking, or operation are handled. Different 1435 * kinds of URB can use different flags. 1436 * @transfer_buffer: This identifies the buffer to (or from) which the I/O 1437 * request will be performed unless URB_NO_TRANSFER_DMA_MAP is set 1438 * (however, do not leave garbage in transfer_buffer even then). 1439 * This buffer must be suitable for DMA; allocate it with 1440 * kmalloc() or equivalent. For transfers to "in" endpoints, contents 1441 * of this buffer will be modified. This buffer is used for the data 1442 * stage of control transfers. 1443 * @transfer_dma: When transfer_flags includes URB_NO_TRANSFER_DMA_MAP, 1444 * the device driver is saying that it provided this DMA address, 1445 * which the host controller driver should use in preference to the 1446 * transfer_buffer. 1447 * @sg: scatter gather buffer list, the buffer size of each element in 1448 * the list (except the last) must be divisible by the endpoint's 1449 * max packet size if no_sg_constraint isn't set in 'struct usb_bus' 1450 * @num_mapped_sgs: (internal) number of mapped sg entries 1451 * @num_sgs: number of entries in the sg list 1452 * @transfer_buffer_length: How big is transfer_buffer. The transfer may 1453 * be broken up into chunks according to the current maximum packet 1454 * size for the endpoint, which is a function of the configuration 1455 * and is encoded in the pipe. When the length is zero, neither 1456 * transfer_buffer nor transfer_dma is used. 1457 * @actual_length: This is read in non-iso completion functions, and 1458 * it tells how many bytes (out of transfer_buffer_length) were 1459 * transferred. It will normally be the same as requested, unless 1460 * either an error was reported or a short read was performed. 1461 * The URB_SHORT_NOT_OK transfer flag may be used to make such 1462 * short reads be reported as errors. 1463 * @setup_packet: Only used for control transfers, this points to eight bytes 1464 * of setup data. Control transfers always start by sending this data 1465 * to the device. Then transfer_buffer is read or written, if needed. 1466 * @setup_dma: DMA pointer for the setup packet. The caller must not use 1467 * this field; setup_packet must point to a valid buffer. 1468 * @start_frame: Returns the initial frame for isochronous transfers. 1469 * @number_of_packets: Lists the number of ISO transfer buffers. 1470 * @interval: Specifies the polling interval for interrupt or isochronous 1471 * transfers. The units are frames (milliseconds) for full and low 1472 * speed devices, and microframes (1/8 millisecond) for highspeed 1473 * and SuperSpeed devices. 1474 * @error_count: Returns the number of ISO transfers that reported errors. 1475 * @context: For use in completion functions. This normally points to 1476 * request-specific driver context. 1477 * @complete: Completion handler. This URB is passed as the parameter to the 1478 * completion function. The completion function may then do what 1479 * it likes with the URB, including resubmitting or freeing it. 1480 * @iso_frame_desc: Used to provide arrays of ISO transfer buffers and to 1481 * collect the transfer status for each buffer. 1482 * 1483 * This structure identifies USB transfer requests. URBs must be allocated by 1484 * calling usb_alloc_urb() and freed with a call to usb_free_urb(). 1485 * Initialization may be done using various usb_fill_*_urb() functions. URBs 1486 * are submitted using usb_submit_urb(), and pending requests may be canceled 1487 * using usb_unlink_urb() or usb_kill_urb(). 1488 * 1489 * Data Transfer Buffers: 1490 * 1491 * Normally drivers provide I/O buffers allocated with kmalloc() or otherwise 1492 * taken from the general page pool. That is provided by transfer_buffer 1493 * (control requests also use setup_packet), and host controller drivers 1494 * perform a dma mapping (and unmapping) for each buffer transferred. Those 1495 * mapping operations can be expensive on some platforms (perhaps using a dma 1496 * bounce buffer or talking to an IOMMU), 1497 * although they're cheap on commodity x86 and ppc hardware. 1498 * 1499 * Alternatively, drivers may pass the URB_NO_TRANSFER_DMA_MAP transfer flag, 1500 * which tells the host controller driver that no such mapping is needed for 1501 * the transfer_buffer since 1502 * the device driver is DMA-aware. For example, a device driver might 1503 * allocate a DMA buffer with usb_alloc_coherent() or call usb_buffer_map(). 1504 * When this transfer flag is provided, host controller drivers will 1505 * attempt to use the dma address found in the transfer_dma 1506 * field rather than determining a dma address themselves. 1507 * 1508 * Note that transfer_buffer must still be set if the controller 1509 * does not support DMA (as indicated by hcd_uses_dma()) and when talking 1510 * to root hub. If you have to transfer between highmem zone and the device 1511 * on such controller, create a bounce buffer or bail out with an error. 1512 * If transfer_buffer cannot be set (is in highmem) and the controller is DMA 1513 * capable, assign NULL to it, so that usbmon knows not to use the value. 1514 * The setup_packet must always be set, so it cannot be located in highmem. 1515 * 1516 * Initialization: 1517 * 1518 * All URBs submitted must initialize the dev, pipe, transfer_flags (may be 1519 * zero), and complete fields. All URBs must also initialize 1520 * transfer_buffer and transfer_buffer_length. They may provide the 1521 * URB_SHORT_NOT_OK transfer flag, indicating that short reads are 1522 * to be treated as errors; that flag is invalid for write requests. 1523 * 1524 * Bulk URBs may 1525 * use the URB_ZERO_PACKET transfer flag, indicating that bulk OUT transfers 1526 * should always terminate with a short packet, even if it means adding an 1527 * extra zero length packet. 1528 * 1529 * Control URBs must provide a valid pointer in the setup_packet field. 1530 * Unlike the transfer_buffer, the setup_packet may not be mapped for DMA 1531 * beforehand. 1532 * 1533 * Interrupt URBs must provide an interval, saying how often (in milliseconds 1534 * or, for highspeed devices, 125 microsecond units) 1535 * to poll for transfers. After the URB has been submitted, the interval 1536 * field reflects how the transfer was actually scheduled. 1537 * The polling interval may be more frequent than requested. 1538 * For example, some controllers have a maximum interval of 32 milliseconds, 1539 * while others support intervals of up to 1024 milliseconds. 1540 * Isochronous URBs also have transfer intervals. (Note that for isochronous 1541 * endpoints, as well as high speed interrupt endpoints, the encoding of 1542 * the transfer interval in the endpoint descriptor is logarithmic. 1543 * Device drivers must convert that value to linear units themselves.) 1544 * 1545 * If an isochronous endpoint queue isn't already running, the host 1546 * controller will schedule a new URB to start as soon as bandwidth 1547 * utilization allows. If the queue is running then a new URB will be 1548 * scheduled to start in the first transfer slot following the end of the 1549 * preceding URB, if that slot has not already expired. If the slot has 1550 * expired (which can happen when IRQ delivery is delayed for a long time), 1551 * the scheduling behavior depends on the URB_ISO_ASAP flag. If the flag 1552 * is clear then the URB will be scheduled to start in the expired slot, 1553 * implying that some of its packets will not be transferred; if the flag 1554 * is set then the URB will be scheduled in the first unexpired slot, 1555 * breaking the queue's synchronization. Upon URB completion, the 1556 * start_frame field will be set to the (micro)frame number in which the 1557 * transfer was scheduled. Ranges for frame counter values are HC-specific 1558 * and can go from as low as 256 to as high as 65536 frames. 1559 * 1560 * Isochronous URBs have a different data transfer model, in part because 1561 * the quality of service is only "best effort". Callers provide specially 1562 * allocated URBs, with number_of_packets worth of iso_frame_desc structures 1563 * at the end. Each such packet is an individual ISO transfer. Isochronous 1564 * URBs are normally queued, submitted by drivers to arrange that 1565 * transfers are at least double buffered, and then explicitly resubmitted 1566 * in completion handlers, so 1567 * that data (such as audio or video) streams at as constant a rate as the 1568 * host controller scheduler can support. 1569 * 1570 * Completion Callbacks: 1571 * 1572 * The completion callback is made in_interrupt(), and one of the first 1573 * things that a completion handler should do is check the status field. 1574 * The status field is provided for all URBs. It is used to report 1575 * unlinked URBs, and status for all non-ISO transfers. It should not 1576 * be examined before the URB is returned to the completion handler. 1577 * 1578 * The context field is normally used to link URBs back to the relevant 1579 * driver or request state. 1580 * 1581 * When the completion callback is invoked for non-isochronous URBs, the 1582 * actual_length field tells how many bytes were transferred. This field 1583 * is updated even when the URB terminated with an error or was unlinked. 1584 * 1585 * ISO transfer status is reported in the status and actual_length fields 1586 * of the iso_frame_desc array, and the number of errors is reported in 1587 * error_count. Completion callbacks for ISO transfers will normally 1588 * (re)submit URBs to ensure a constant transfer rate. 1589 * 1590 * Note that even fields marked "public" should not be touched by the driver 1591 * when the urb is owned by the hcd, that is, since the call to 1592 * usb_submit_urb() till the entry into the completion routine. 1593 */ 1594 struct urb { 1595 /* private: usb core and host controller only fields in the urb */ 1596 struct kref kref; /* reference count of the URB */ 1597 int unlinked; /* unlink error code */ 1598 void *hcpriv; /* private data for host controller */ 1599 atomic_t use_count; /* concurrent submissions counter */ 1600 atomic_t reject; /* submissions will fail */ 1601 1602 /* public: documented fields in the urb that can be used by drivers */ 1603 struct list_head urb_list; /* list head for use by the urb's 1604 * current owner */ 1605 struct list_head anchor_list; /* the URB may be anchored */ 1606 struct usb_anchor *anchor; 1607 struct usb_device *dev; /* (in) pointer to associated device */ 1608 struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */ 1609 unsigned int pipe; /* (in) pipe information */ 1610 unsigned int stream_id; /* (in) stream ID */ 1611 int status; /* (return) non-ISO status */ 1612 unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/ 1613 void *transfer_buffer; /* (in) associated data buffer */ 1614 dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */ 1615 struct scatterlist *sg; /* (in) scatter gather buffer list */ 1616 int num_mapped_sgs; /* (internal) mapped sg entries */ 1617 int num_sgs; /* (in) number of entries in the sg list */ 1618 u32 transfer_buffer_length; /* (in) data buffer length */ 1619 u32 actual_length; /* (return) actual transfer length */ 1620 unsigned char *setup_packet; /* (in) setup packet (control only) */ 1621 dma_addr_t setup_dma; /* (in) dma addr for setup_packet */ 1622 int start_frame; /* (modify) start frame (ISO) */ 1623 int number_of_packets; /* (in) number of ISO packets */ 1624 int interval; /* (modify) transfer interval 1625 * (INT/ISO) */ 1626 int error_count; /* (return) number of ISO errors */ 1627 void *context; /* (in) context for completion */ 1628 usb_complete_t complete; /* (in) completion routine */ 1629 struct usb_iso_packet_descriptor iso_frame_desc[]; 1630 /* (in) ISO ONLY */ 1631 }; 1632 1633 /* ----------------------------------------------------------------------- */ 1634 1635 /** 1636 * usb_fill_control_urb - initializes a control urb 1637 * @urb: pointer to the urb to initialize. 1638 * @dev: pointer to the struct usb_device for this urb. 1639 * @pipe: the endpoint pipe 1640 * @setup_packet: pointer to the setup_packet buffer. The buffer must be 1641 * suitable for DMA. 1642 * @transfer_buffer: pointer to the transfer buffer. The buffer must be 1643 * suitable for DMA. 1644 * @buffer_length: length of the transfer buffer 1645 * @complete_fn: pointer to the usb_complete_t function 1646 * @context: what to set the urb context to. 1647 * 1648 * Initializes a control urb with the proper information needed to submit 1649 * it to a device. 1650 * 1651 * The transfer buffer and the setup_packet buffer will most likely be filled 1652 * or read via DMA. The simplest way to get a buffer that can be DMAed to is 1653 * allocating it via kmalloc() or equivalent, even for very small buffers. 1654 * If the buffers are embedded in a bigger structure, there is a risk that 1655 * the buffer itself, the previous fields and/or the next fields are corrupted 1656 * due to cache incoherencies; or slowed down if they are evicted from the 1657 * cache. For more information, check &struct urb. 1658 * 1659 */ 1660 static inline void usb_fill_control_urb(struct urb *urb, 1661 struct usb_device *dev, 1662 unsigned int pipe, 1663 unsigned char *setup_packet, 1664 void *transfer_buffer, 1665 int buffer_length, 1666 usb_complete_t complete_fn, 1667 void *context) 1668 { 1669 urb->dev = dev; 1670 urb->pipe = pipe; 1671 urb->setup_packet = setup_packet; 1672 urb->transfer_buffer = transfer_buffer; 1673 urb->transfer_buffer_length = buffer_length; 1674 urb->complete = complete_fn; 1675 urb->context = context; 1676 } 1677 1678 /** 1679 * usb_fill_bulk_urb - macro to help initialize a bulk urb 1680 * @urb: pointer to the urb to initialize. 1681 * @dev: pointer to the struct usb_device for this urb. 1682 * @pipe: the endpoint pipe 1683 * @transfer_buffer: pointer to the transfer buffer. The buffer must be 1684 * suitable for DMA. 1685 * @buffer_length: length of the transfer buffer 1686 * @complete_fn: pointer to the usb_complete_t function 1687 * @context: what to set the urb context to. 1688 * 1689 * Initializes a bulk urb with the proper information needed to submit it 1690 * to a device. 1691 * 1692 * Refer to usb_fill_control_urb() for a description of the requirements for 1693 * transfer_buffer. 1694 */ 1695 static inline void usb_fill_bulk_urb(struct urb *urb, 1696 struct usb_device *dev, 1697 unsigned int pipe, 1698 void *transfer_buffer, 1699 int buffer_length, 1700 usb_complete_t complete_fn, 1701 void *context) 1702 { 1703 urb->dev = dev; 1704 urb->pipe = pipe; 1705 urb->transfer_buffer = transfer_buffer; 1706 urb->transfer_buffer_length = buffer_length; 1707 urb->complete = complete_fn; 1708 urb->context = context; 1709 } 1710 1711 /** 1712 * usb_fill_int_urb - macro to help initialize a interrupt urb 1713 * @urb: pointer to the urb to initialize. 1714 * @dev: pointer to the struct usb_device for this urb. 1715 * @pipe: the endpoint pipe 1716 * @transfer_buffer: pointer to the transfer buffer. The buffer must be 1717 * suitable for DMA. 1718 * @buffer_length: length of the transfer buffer 1719 * @complete_fn: pointer to the usb_complete_t function 1720 * @context: what to set the urb context to. 1721 * @interval: what to set the urb interval to, encoded like 1722 * the endpoint descriptor's bInterval value. 1723 * 1724 * Initializes a interrupt urb with the proper information needed to submit 1725 * it to a device. 1726 * 1727 * Refer to usb_fill_control_urb() for a description of the requirements for 1728 * transfer_buffer. 1729 * 1730 * Note that High Speed and SuperSpeed(+) interrupt endpoints use a logarithmic 1731 * encoding of the endpoint interval, and express polling intervals in 1732 * microframes (eight per millisecond) rather than in frames (one per 1733 * millisecond). 1734 * 1735 * Wireless USB also uses the logarithmic encoding, but specifies it in units of 1736 * 128us instead of 125us. For Wireless USB devices, the interval is passed 1737 * through to the host controller, rather than being translated into microframe 1738 * units. 1739 */ 1740 static inline void usb_fill_int_urb(struct urb *urb, 1741 struct usb_device *dev, 1742 unsigned int pipe, 1743 void *transfer_buffer, 1744 int buffer_length, 1745 usb_complete_t complete_fn, 1746 void *context, 1747 int interval) 1748 { 1749 urb->dev = dev; 1750 urb->pipe = pipe; 1751 urb->transfer_buffer = transfer_buffer; 1752 urb->transfer_buffer_length = buffer_length; 1753 urb->complete = complete_fn; 1754 urb->context = context; 1755 1756 if (dev->speed == USB_SPEED_HIGH || dev->speed >= USB_SPEED_SUPER) { 1757 /* make sure interval is within allowed range */ 1758 interval = clamp(interval, 1, 16); 1759 1760 urb->interval = 1 << (interval - 1); 1761 } else { 1762 urb->interval = interval; 1763 } 1764 1765 urb->start_frame = -1; 1766 } 1767 1768 extern void usb_init_urb(struct urb *urb); 1769 extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags); 1770 extern void usb_free_urb(struct urb *urb); 1771 #define usb_put_urb usb_free_urb 1772 extern struct urb *usb_get_urb(struct urb *urb); 1773 extern int usb_submit_urb(struct urb *urb, gfp_t mem_flags); 1774 extern int usb_unlink_urb(struct urb *urb); 1775 extern void usb_kill_urb(struct urb *urb); 1776 extern void usb_poison_urb(struct urb *urb); 1777 extern void usb_unpoison_urb(struct urb *urb); 1778 extern void usb_block_urb(struct urb *urb); 1779 extern void usb_kill_anchored_urbs(struct usb_anchor *anchor); 1780 extern void usb_poison_anchored_urbs(struct usb_anchor *anchor); 1781 extern void usb_unpoison_anchored_urbs(struct usb_anchor *anchor); 1782 extern void usb_unlink_anchored_urbs(struct usb_anchor *anchor); 1783 extern void usb_anchor_suspend_wakeups(struct usb_anchor *anchor); 1784 extern void usb_anchor_resume_wakeups(struct usb_anchor *anchor); 1785 extern void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor); 1786 extern void usb_unanchor_urb(struct urb *urb); 1787 extern int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor, 1788 unsigned int timeout); 1789 extern struct urb *usb_get_from_anchor(struct usb_anchor *anchor); 1790 extern void usb_scuttle_anchored_urbs(struct usb_anchor *anchor); 1791 extern int usb_anchor_empty(struct usb_anchor *anchor); 1792 1793 #define usb_unblock_urb usb_unpoison_urb 1794 1795 /** 1796 * usb_urb_dir_in - check if an URB describes an IN transfer 1797 * @urb: URB to be checked 1798 * 1799 * Return: 1 if @urb describes an IN transfer (device-to-host), 1800 * otherwise 0. 1801 */ 1802 static inline int usb_urb_dir_in(struct urb *urb) 1803 { 1804 return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN; 1805 } 1806 1807 /** 1808 * usb_urb_dir_out - check if an URB describes an OUT transfer 1809 * @urb: URB to be checked 1810 * 1811 * Return: 1 if @urb describes an OUT transfer (host-to-device), 1812 * otherwise 0. 1813 */ 1814 static inline int usb_urb_dir_out(struct urb *urb) 1815 { 1816 return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_OUT; 1817 } 1818 1819 int usb_pipe_type_check(struct usb_device *dev, unsigned int pipe); 1820 int usb_urb_ep_type_check(const struct urb *urb); 1821 1822 void *usb_alloc_coherent(struct usb_device *dev, size_t size, 1823 gfp_t mem_flags, dma_addr_t *dma); 1824 void usb_free_coherent(struct usb_device *dev, size_t size, 1825 void *addr, dma_addr_t dma); 1826 1827 #if 0 1828 struct urb *usb_buffer_map(struct urb *urb); 1829 void usb_buffer_dmasync(struct urb *urb); 1830 void usb_buffer_unmap(struct urb *urb); 1831 #endif 1832 1833 struct scatterlist; 1834 int usb_buffer_map_sg(const struct usb_device *dev, int is_in, 1835 struct scatterlist *sg, int nents); 1836 #if 0 1837 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in, 1838 struct scatterlist *sg, int n_hw_ents); 1839 #endif 1840 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in, 1841 struct scatterlist *sg, int n_hw_ents); 1842 1843 /*-------------------------------------------------------------------* 1844 * SYNCHRONOUS CALL SUPPORT * 1845 *-------------------------------------------------------------------*/ 1846 1847 extern int usb_control_msg(struct usb_device *dev, unsigned int pipe, 1848 __u8 request, __u8 requesttype, __u16 value, __u16 index, 1849 void *data, __u16 size, int timeout); 1850 extern int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe, 1851 void *data, int len, int *actual_length, int timeout); 1852 extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 1853 void *data, int len, int *actual_length, 1854 int timeout); 1855 1856 /* wrappers around usb_control_msg() for the most common standard requests */ 1857 int usb_control_msg_send(struct usb_device *dev, __u8 endpoint, __u8 request, 1858 __u8 requesttype, __u16 value, __u16 index, 1859 const void *data, __u16 size, int timeout, 1860 gfp_t memflags); 1861 int usb_control_msg_recv(struct usb_device *dev, __u8 endpoint, __u8 request, 1862 __u8 requesttype, __u16 value, __u16 index, 1863 void *data, __u16 size, int timeout, 1864 gfp_t memflags); 1865 extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype, 1866 unsigned char descindex, void *buf, int size); 1867 extern int usb_get_status(struct usb_device *dev, 1868 int recip, int type, int target, void *data); 1869 1870 static inline int usb_get_std_status(struct usb_device *dev, 1871 int recip, int target, void *data) 1872 { 1873 return usb_get_status(dev, recip, USB_STATUS_TYPE_STANDARD, target, 1874 data); 1875 } 1876 1877 static inline int usb_get_ptm_status(struct usb_device *dev, void *data) 1878 { 1879 return usb_get_status(dev, USB_RECIP_DEVICE, USB_STATUS_TYPE_PTM, 1880 0, data); 1881 } 1882 1883 extern int usb_string(struct usb_device *dev, int index, 1884 char *buf, size_t size); 1885 extern char *usb_cache_string(struct usb_device *udev, int index); 1886 1887 /* wrappers that also update important state inside usbcore */ 1888 extern int usb_clear_halt(struct usb_device *dev, int pipe); 1889 extern int usb_reset_configuration(struct usb_device *dev); 1890 extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate); 1891 extern void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr); 1892 1893 /* this request isn't really synchronous, but it belongs with the others */ 1894 extern int usb_driver_set_configuration(struct usb_device *udev, int config); 1895 1896 /* choose and set configuration for device */ 1897 extern int usb_choose_configuration(struct usb_device *udev); 1898 extern int usb_set_configuration(struct usb_device *dev, int configuration); 1899 1900 /* 1901 * timeouts, in milliseconds, used for sending/receiving control messages 1902 * they typically complete within a few frames (msec) after they're issued 1903 * USB identifies 5 second timeouts, maybe more in a few cases, and a few 1904 * slow devices (like some MGE Ellipse UPSes) actually push that limit. 1905 */ 1906 #define USB_CTRL_GET_TIMEOUT 5000 1907 #define USB_CTRL_SET_TIMEOUT 5000 1908 1909 1910 /** 1911 * struct usb_sg_request - support for scatter/gather I/O 1912 * @status: zero indicates success, else negative errno 1913 * @bytes: counts bytes transferred. 1914 * 1915 * These requests are initialized using usb_sg_init(), and then are used 1916 * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most 1917 * members of the request object aren't for driver access. 1918 * 1919 * The status and bytecount values are valid only after usb_sg_wait() 1920 * returns. If the status is zero, then the bytecount matches the total 1921 * from the request. 1922 * 1923 * After an error completion, drivers may need to clear a halt condition 1924 * on the endpoint. 1925 */ 1926 struct usb_sg_request { 1927 int status; 1928 size_t bytes; 1929 1930 /* private: 1931 * members below are private to usbcore, 1932 * and are not provided for driver access! 1933 */ 1934 spinlock_t lock; 1935 1936 struct usb_device *dev; 1937 int pipe; 1938 1939 int entries; 1940 struct urb **urbs; 1941 1942 int count; 1943 struct completion complete; 1944 }; 1945 1946 int usb_sg_init( 1947 struct usb_sg_request *io, 1948 struct usb_device *dev, 1949 unsigned pipe, 1950 unsigned period, 1951 struct scatterlist *sg, 1952 int nents, 1953 size_t length, 1954 gfp_t mem_flags 1955 ); 1956 void usb_sg_cancel(struct usb_sg_request *io); 1957 void usb_sg_wait(struct usb_sg_request *io); 1958 1959 1960 /* ----------------------------------------------------------------------- */ 1961 1962 /* 1963 * For various legacy reasons, Linux has a small cookie that's paired with 1964 * a struct usb_device to identify an endpoint queue. Queue characteristics 1965 * are defined by the endpoint's descriptor. This cookie is called a "pipe", 1966 * an unsigned int encoded as: 1967 * 1968 * - direction: bit 7 (0 = Host-to-Device [Out], 1969 * 1 = Device-to-Host [In] ... 1970 * like endpoint bEndpointAddress) 1971 * - device address: bits 8-14 ... bit positions known to uhci-hcd 1972 * - endpoint: bits 15-18 ... bit positions known to uhci-hcd 1973 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 1974 * 10 = control, 11 = bulk) 1975 * 1976 * Given the device address and endpoint descriptor, pipes are redundant. 1977 */ 1978 1979 /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */ 1980 /* (yet ... they're the values used by usbfs) */ 1981 #define PIPE_ISOCHRONOUS 0 1982 #define PIPE_INTERRUPT 1 1983 #define PIPE_CONTROL 2 1984 #define PIPE_BULK 3 1985 1986 #define usb_pipein(pipe) ((pipe) & USB_DIR_IN) 1987 #define usb_pipeout(pipe) (!usb_pipein(pipe)) 1988 1989 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f) 1990 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf) 1991 1992 #define usb_pipetype(pipe) (((pipe) >> 30) & 3) 1993 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS) 1994 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT) 1995 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL) 1996 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK) 1997 1998 static inline unsigned int __create_pipe(struct usb_device *dev, 1999 unsigned int endpoint) 2000 { 2001 return (dev->devnum << 8) | (endpoint << 15); 2002 } 2003 2004 /* Create various pipes... */ 2005 #define usb_sndctrlpipe(dev, endpoint) \ 2006 ((PIPE_CONTROL << 30) | __create_pipe(dev, endpoint)) 2007 #define usb_rcvctrlpipe(dev, endpoint) \ 2008 ((PIPE_CONTROL << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 2009 #define usb_sndisocpipe(dev, endpoint) \ 2010 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev, endpoint)) 2011 #define usb_rcvisocpipe(dev, endpoint) \ 2012 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 2013 #define usb_sndbulkpipe(dev, endpoint) \ 2014 ((PIPE_BULK << 30) | __create_pipe(dev, endpoint)) 2015 #define usb_rcvbulkpipe(dev, endpoint) \ 2016 ((PIPE_BULK << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 2017 #define usb_sndintpipe(dev, endpoint) \ 2018 ((PIPE_INTERRUPT << 30) | __create_pipe(dev, endpoint)) 2019 #define usb_rcvintpipe(dev, endpoint) \ 2020 ((PIPE_INTERRUPT << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 2021 2022 static inline struct usb_host_endpoint * 2023 usb_pipe_endpoint(struct usb_device *dev, unsigned int pipe) 2024 { 2025 struct usb_host_endpoint **eps; 2026 eps = usb_pipein(pipe) ? dev->ep_in : dev->ep_out; 2027 return eps[usb_pipeendpoint(pipe)]; 2028 } 2029 2030 static inline u16 usb_maxpacket(struct usb_device *udev, int pipe) 2031 { 2032 struct usb_host_endpoint *ep = usb_pipe_endpoint(udev, pipe); 2033 2034 if (!ep) 2035 return 0; 2036 2037 /* NOTE: only 0x07ff bits are for packet size... */ 2038 return usb_endpoint_maxp(&ep->desc); 2039 } 2040 2041 /* translate USB error codes to codes user space understands */ 2042 static inline int usb_translate_errors(int error_code) 2043 { 2044 switch (error_code) { 2045 case 0: 2046 case -ENOMEM: 2047 case -ENODEV: 2048 case -EOPNOTSUPP: 2049 return error_code; 2050 default: 2051 return -EIO; 2052 } 2053 } 2054 2055 /* Events from the usb core */ 2056 #define USB_DEVICE_ADD 0x0001 2057 #define USB_DEVICE_REMOVE 0x0002 2058 #define USB_BUS_ADD 0x0003 2059 #define USB_BUS_REMOVE 0x0004 2060 extern void usb_register_notify(struct notifier_block *nb); 2061 extern void usb_unregister_notify(struct notifier_block *nb); 2062 2063 /* debugfs stuff */ 2064 extern struct dentry *usb_debug_root; 2065 2066 /* LED triggers */ 2067 enum usb_led_event { 2068 USB_LED_EVENT_HOST = 0, 2069 USB_LED_EVENT_GADGET = 1, 2070 }; 2071 2072 #ifdef CONFIG_USB_LED_TRIG 2073 extern void usb_led_activity(enum usb_led_event ev); 2074 #else 2075 static inline void usb_led_activity(enum usb_led_event ev) {} 2076 #endif 2077 2078 #endif /* __KERNEL__ */ 2079 2080 #endif 2081