usb.h 70 KB

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