usb.h 75 KB

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