usb.h 72 KB

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