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