usb.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. #ifdef __KERNEL__
  7. #include <linux/config.h>
  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. struct usb_device;
  18. struct usb_driver;
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * Host-side wrappers for standard USB descriptors ... these are parsed
  22. * from the data provided by devices. Parsing turns them from a flat
  23. * sequence of descriptors into a hierarchy:
  24. *
  25. * - devices have one (usually) or more configs;
  26. * - configs have one (often) or more interfaces;
  27. * - interfaces have one (usually) or more settings;
  28. * - each interface setting has zero or (usually) more endpoints.
  29. *
  30. * And there might be other descriptors mixed in with those.
  31. *
  32. * Devices may also have class-specific or vendor-specific descriptors.
  33. */
  34. /**
  35. * struct usb_host_endpoint - host-side endpoint descriptor and queue
  36. * @desc: descriptor for this endpoint, wMaxPacketSize in native byteorder
  37. * @urb_list: urbs queued to this endpoint; maintained by usbcore
  38. * @hcpriv: for use by HCD; typically holds hardware dma queue head (QH)
  39. * with one or more transfer descriptors (TDs) per urb
  40. * @extra: descriptors following this endpoint in the configuration
  41. * @extralen: how many bytes of "extra" are valid
  42. *
  43. * USB requests are always queued to a given endpoint, identified by a
  44. * descriptor within an active interface in a given USB configuration.
  45. */
  46. struct usb_host_endpoint {
  47. struct usb_endpoint_descriptor desc;
  48. struct list_head urb_list;
  49. void *hcpriv;
  50. unsigned char *extra; /* Extra descriptors */
  51. int extralen;
  52. };
  53. /* host-side wrapper for one interface setting's parsed descriptors */
  54. struct usb_host_interface {
  55. struct usb_interface_descriptor desc;
  56. /* array of desc.bNumEndpoint endpoints associated with this
  57. * interface setting. these will be in no particular order.
  58. */
  59. struct usb_host_endpoint *endpoint;
  60. char *string; /* iInterface string, if present */
  61. unsigned char *extra; /* Extra descriptors */
  62. int extralen;
  63. };
  64. enum usb_interface_condition {
  65. USB_INTERFACE_UNBOUND = 0,
  66. USB_INTERFACE_BINDING,
  67. USB_INTERFACE_BOUND,
  68. USB_INTERFACE_UNBINDING,
  69. };
  70. /**
  71. * struct usb_interface - what usb device drivers talk to
  72. * @altsetting: array of interface structures, one for each alternate
  73. * setting that may be selected. Each one includes a set of
  74. * endpoint configurations. They will be in no particular order.
  75. * @num_altsetting: number of altsettings defined.
  76. * @cur_altsetting: the current altsetting.
  77. * @driver: the USB driver that is bound to this interface.
  78. * @minor: the minor number assigned to this interface, if this
  79. * interface is bound to a driver that uses the USB major number.
  80. * If this interface does not use the USB major, this field should
  81. * be unused. The driver should set this value in the probe()
  82. * function of the driver, after it has been assigned a minor
  83. * number from the USB core by calling usb_register_dev().
  84. * @condition: binding state of the interface: not bound, binding
  85. * (in probe()), bound to a driver, or unbinding (in disconnect())
  86. * @dev: driver model's view of this device
  87. * @class_dev: driver model's class view of this device.
  88. *
  89. * USB device drivers attach to interfaces on a physical device. Each
  90. * interface encapsulates a single high level function, such as feeding
  91. * an audio stream to a speaker or reporting a change in a volume control.
  92. * Many USB devices only have one interface. The protocol used to talk to
  93. * an interface's endpoints can be defined in a usb "class" specification,
  94. * or by a product's vendor. The (default) control endpoint is part of
  95. * every interface, but is never listed among the interface's descriptors.
  96. *
  97. * The driver that is bound to the interface can use standard driver model
  98. * calls such as dev_get_drvdata() on the dev member of this structure.
  99. *
  100. * Each interface may have alternate settings. The initial configuration
  101. * of a device sets altsetting 0, but the device driver can change
  102. * that setting using usb_set_interface(). Alternate settings are often
  103. * used to control the the use of periodic endpoints, such as by having
  104. * different endpoints use different amounts of reserved USB bandwidth.
  105. * All standards-conformant USB devices that use isochronous endpoints
  106. * will use them in non-default settings.
  107. *
  108. * The USB specification says that alternate setting numbers must run from
  109. * 0 to one less than the total number of alternate settings. But some
  110. * devices manage to mess this up, and the structures aren't necessarily
  111. * stored in numerical order anyhow. Use usb_altnum_to_altsetting() to
  112. * look up an alternate setting in the altsetting array based on its number.
  113. */
  114. struct usb_interface {
  115. /* array of alternate settings for this interface,
  116. * stored in no particular order */
  117. struct usb_host_interface *altsetting;
  118. struct usb_host_interface *cur_altsetting; /* the currently
  119. * active alternate setting */
  120. unsigned num_altsetting; /* number of alternate settings */
  121. int minor; /* minor number this interface is bound to */
  122. enum usb_interface_condition condition; /* state of binding */
  123. struct device dev; /* interface specific device info */
  124. struct class_device *class_dev;
  125. };
  126. #define to_usb_interface(d) container_of(d, struct usb_interface, dev)
  127. #define interface_to_usbdev(intf) \
  128. container_of(intf->dev.parent, struct usb_device, dev)
  129. static inline void *usb_get_intfdata (struct usb_interface *intf)
  130. {
  131. return dev_get_drvdata (&intf->dev);
  132. }
  133. static inline void usb_set_intfdata (struct usb_interface *intf, void *data)
  134. {
  135. dev_set_drvdata(&intf->dev, data);
  136. }
  137. struct usb_interface *usb_get_intf(struct usb_interface *intf);
  138. void usb_put_intf(struct usb_interface *intf);
  139. /* this maximum is arbitrary */
  140. #define USB_MAXINTERFACES 32
  141. /**
  142. * struct usb_interface_cache - long-term representation of a device interface
  143. * @num_altsetting: number of altsettings defined.
  144. * @ref: reference counter.
  145. * @altsetting: variable-length array of interface structures, one for
  146. * each alternate setting that may be selected. Each one includes a
  147. * set of endpoint configurations. They will be in no particular order.
  148. *
  149. * These structures persist for the lifetime of a usb_device, unlike
  150. * struct usb_interface (which persists only as long as its configuration
  151. * is installed). The altsetting arrays can be accessed through these
  152. * structures at any time, permitting comparison of configurations and
  153. * providing support for the /proc/bus/usb/devices pseudo-file.
  154. */
  155. struct usb_interface_cache {
  156. unsigned num_altsetting; /* number of alternate settings */
  157. struct kref ref; /* reference counter */
  158. /* variable-length array of alternate settings for this interface,
  159. * stored in no particular order */
  160. struct usb_host_interface altsetting[0];
  161. };
  162. #define ref_to_usb_interface_cache(r) \
  163. container_of(r, struct usb_interface_cache, ref)
  164. #define altsetting_to_usb_interface_cache(a) \
  165. container_of(a, struct usb_interface_cache, altsetting[0])
  166. /**
  167. * struct usb_host_config - representation of a device's configuration
  168. * @desc: the device's configuration descriptor.
  169. * @string: pointer to the cached version of the iConfiguration string, if
  170. * present for this configuration.
  171. * @interface: array of pointers to usb_interface structures, one for each
  172. * interface in the configuration. The number of interfaces is stored
  173. * in desc.bNumInterfaces. These pointers are valid only while the
  174. * the configuration is active.
  175. * @intf_cache: array of pointers to usb_interface_cache structures, one
  176. * for each interface in the configuration. These structures exist
  177. * for the entire life of the device.
  178. * @extra: pointer to buffer containing all extra descriptors associated
  179. * with this configuration (those preceding the first interface
  180. * descriptor).
  181. * @extralen: length of the extra descriptors buffer.
  182. *
  183. * USB devices may have multiple configurations, but only one can be active
  184. * at any time. Each encapsulates a different operational environment;
  185. * for example, a dual-speed device would have separate configurations for
  186. * full-speed and high-speed operation. The number of configurations
  187. * available is stored in the device descriptor as bNumConfigurations.
  188. *
  189. * A configuration can contain multiple interfaces. Each corresponds to
  190. * a different function of the USB device, and all are available whenever
  191. * the configuration is active. The USB standard says that interfaces
  192. * are supposed to be numbered from 0 to desc.bNumInterfaces-1, but a lot
  193. * of devices get this wrong. In addition, the interface array is not
  194. * guaranteed to be sorted in numerical order. Use usb_ifnum_to_if() to
  195. * look up an interface entry based on its number.
  196. *
  197. * Device drivers should not attempt to activate configurations. The choice
  198. * of which configuration to install is a policy decision based on such
  199. * considerations as available power, functionality provided, and the user's
  200. * desires (expressed through hotplug scripts). However, drivers can call
  201. * usb_reset_configuration() to reinitialize the current configuration and
  202. * all its interfaces.
  203. */
  204. struct usb_host_config {
  205. struct usb_config_descriptor desc;
  206. char *string;
  207. /* the interfaces associated with this configuration,
  208. * stored in no particular order */
  209. struct usb_interface *interface[USB_MAXINTERFACES];
  210. /* Interface information available even when this is not the
  211. * active configuration */
  212. struct usb_interface_cache *intf_cache[USB_MAXINTERFACES];
  213. unsigned char *extra; /* Extra descriptors */
  214. int extralen;
  215. };
  216. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  217. unsigned char type, void **ptr);
  218. #define usb_get_extra_descriptor(ifpoint,type,ptr)\
  219. __usb_get_extra_descriptor((ifpoint)->extra,(ifpoint)->extralen,\
  220. type,(void**)ptr)
  221. /* -------------------------------------------------------------------------- */
  222. struct usb_operations;
  223. /* USB device number allocation bitmap */
  224. struct usb_devmap {
  225. unsigned long devicemap[128 / (8*sizeof(unsigned long))];
  226. };
  227. /*
  228. * Allocated per bus (tree of devices) we have:
  229. */
  230. struct usb_bus {
  231. struct device *controller; /* host/master side hardware */
  232. int busnum; /* Bus number (in order of reg) */
  233. char *bus_name; /* stable id (PCI slot_name etc) */
  234. u8 otg_port; /* 0, or number of OTG/HNP port */
  235. unsigned is_b_host:1; /* true during some HNP roleswitches */
  236. unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
  237. int devnum_next; /* Next open device number in round-robin allocation */
  238. struct usb_devmap devmap; /* device address allocation map */
  239. struct usb_operations *op; /* Operations (specific to the HC) */
  240. struct usb_device *root_hub; /* Root hub */
  241. struct list_head bus_list; /* list of busses */
  242. void *hcpriv; /* Host Controller private data */
  243. int bandwidth_allocated; /* on this bus: how much of the time
  244. * reserved for periodic (intr/iso)
  245. * requests is used, on average?
  246. * Units: microseconds/frame.
  247. * Limits: Full/low speed reserve 90%,
  248. * while high speed reserves 80%.
  249. */
  250. int bandwidth_int_reqs; /* number of Interrupt requests */
  251. int bandwidth_isoc_reqs; /* number of Isoc. requests */
  252. struct dentry *usbfs_dentry; /* usbfs dentry entry for the bus */
  253. struct class_device *class_dev; /* class device for this bus */
  254. struct kref kref; /* handles reference counting this bus */
  255. void (*release)(struct usb_bus *bus); /* function to destroy this bus's memory */
  256. #if defined(CONFIG_USB_MON)
  257. struct mon_bus *mon_bus; /* non-null when associated */
  258. int monitored; /* non-zero when monitored */
  259. #endif
  260. };
  261. /* -------------------------------------------------------------------------- */
  262. /* This is arbitrary.
  263. * From USB 2.0 spec Table 11-13, offset 7, a hub can
  264. * have up to 255 ports. The most yet reported is 10.
  265. */
  266. #define USB_MAXCHILDREN (16)
  267. struct usb_tt;
  268. /*
  269. * struct usb_device - kernel's representation of a USB device
  270. *
  271. * FIXME: Write the kerneldoc!
  272. *
  273. * Usbcore drivers should not set usbdev->state directly. Instead use
  274. * usb_set_device_state().
  275. */
  276. struct usb_device {
  277. int devnum; /* Address on USB bus */
  278. char devpath [16]; /* Use in messages: /port/port/... */
  279. enum usb_device_state state; /* configured, not attached, etc */
  280. enum usb_device_speed speed; /* high/full/low (or error) */
  281. struct usb_tt *tt; /* low/full speed dev, highspeed hub */
  282. int ttport; /* device port on that tt hub */
  283. struct semaphore serialize;
  284. unsigned int toggle[2]; /* one bit for each endpoint ([0] = IN, [1] = OUT) */
  285. struct usb_device *parent; /* our hub, unless we're the root */
  286. struct usb_bus *bus; /* Bus we're part of */
  287. struct usb_host_endpoint ep0;
  288. struct device dev; /* Generic device interface */
  289. struct usb_device_descriptor descriptor;/* Descriptor */
  290. struct usb_host_config *config; /* All of the configs */
  291. struct usb_host_config *actconfig;/* the active configuration */
  292. struct usb_host_endpoint *ep_in[16];
  293. struct usb_host_endpoint *ep_out[16];
  294. char **rawdescriptors; /* Raw descriptors for each config */
  295. int have_langid; /* whether string_langid is valid yet */
  296. int string_langid; /* language ID for strings */
  297. char *product;
  298. char *manufacturer;
  299. char *serial; /* static strings from the device */
  300. struct list_head filelist;
  301. struct dentry *usbfs_dentry; /* usbfs dentry entry for the device */
  302. /*
  303. * Child devices - these can be either new devices
  304. * (if this is a hub device), or different instances
  305. * of this same device.
  306. *
  307. * Each instance needs its own set of data structures.
  308. */
  309. int maxchild; /* Number of ports if hub */
  310. struct usb_device *children[USB_MAXCHILDREN];
  311. };
  312. #define to_usb_device(d) container_of(d, struct usb_device, dev)
  313. extern struct usb_device *usb_get_dev(struct usb_device *dev);
  314. extern void usb_put_dev(struct usb_device *dev);
  315. extern void usb_lock_device(struct usb_device *udev);
  316. extern int usb_trylock_device(struct usb_device *udev);
  317. extern int usb_lock_device_for_reset(struct usb_device *udev,
  318. struct usb_interface *iface);
  319. extern void usb_unlock_device(struct usb_device *udev);
  320. /* USB port reset for device reinitialization */
  321. extern int usb_reset_device(struct usb_device *dev);
  322. extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id);
  323. /*-------------------------------------------------------------------------*/
  324. /* for drivers using iso endpoints */
  325. extern int usb_get_current_frame_number (struct usb_device *usb_dev);
  326. /* used these for multi-interface device registration */
  327. extern int usb_driver_claim_interface(struct usb_driver *driver,
  328. struct usb_interface *iface, void* priv);
  329. /**
  330. * usb_interface_claimed - returns true iff an interface is claimed
  331. * @iface: the interface being checked
  332. *
  333. * Returns true (nonzero) iff the interface is claimed, else false (zero).
  334. * Callers must own the driver model's usb bus readlock. So driver
  335. * probe() entries don't need extra locking, but other call contexts
  336. * may need to explicitly claim that lock.
  337. *
  338. */
  339. static inline int usb_interface_claimed(struct usb_interface *iface) {
  340. return (iface->dev.driver != NULL);
  341. }
  342. extern void usb_driver_release_interface(struct usb_driver *driver,
  343. struct usb_interface *iface);
  344. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  345. const struct usb_device_id *id);
  346. extern struct usb_interface *usb_find_interface(struct usb_driver *drv,
  347. int minor);
  348. extern struct usb_interface *usb_ifnum_to_if(struct usb_device *dev,
  349. unsigned ifnum);
  350. extern struct usb_host_interface *usb_altnum_to_altsetting(
  351. struct usb_interface *intf, unsigned int altnum);
  352. /**
  353. * usb_make_path - returns stable device path in the usb tree
  354. * @dev: the device whose path is being constructed
  355. * @buf: where to put the string
  356. * @size: how big is "buf"?
  357. *
  358. * Returns length of the string (> 0) or negative if size was too small.
  359. *
  360. * This identifier is intended to be "stable", reflecting physical paths in
  361. * hardware such as physical bus addresses for host controllers or ports on
  362. * USB hubs. That makes it stay the same until systems are physically
  363. * reconfigured, by re-cabling a tree of USB devices or by moving USB host
  364. * controllers. Adding and removing devices, including virtual root hubs
  365. * in host controller driver modules, does not change these path identifers;
  366. * neither does rebooting or re-enumerating. These are more useful identifiers
  367. * than changeable ("unstable") ones like bus numbers or device addresses.
  368. *
  369. * With a partial exception for devices connected to USB 2.0 root hubs, these
  370. * identifiers are also predictable. So long as the device tree isn't changed,
  371. * plugging any USB device into a given hub port always gives it the same path.
  372. * Because of the use of "companion" controllers, devices connected to ports on
  373. * USB 2.0 root hubs (EHCI host controllers) will get one path ID if they are
  374. * high speed, and a different one if they are full or low speed.
  375. */
  376. static inline int usb_make_path (struct usb_device *dev, char *buf, size_t size)
  377. {
  378. int actual;
  379. actual = snprintf (buf, size, "usb-%s-%s", dev->bus->bus_name, dev->devpath);
  380. return (actual >= (int)size) ? -1 : actual;
  381. }
  382. /*-------------------------------------------------------------------------*/
  383. #define USB_DEVICE_ID_MATCH_DEVICE (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
  384. #define USB_DEVICE_ID_MATCH_DEV_RANGE (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI)
  385. #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE)
  386. #define USB_DEVICE_ID_MATCH_DEV_INFO \
  387. (USB_DEVICE_ID_MATCH_DEV_CLASS | USB_DEVICE_ID_MATCH_DEV_SUBCLASS | USB_DEVICE_ID_MATCH_DEV_PROTOCOL)
  388. #define USB_DEVICE_ID_MATCH_INT_INFO \
  389. (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS | USB_DEVICE_ID_MATCH_INT_PROTOCOL)
  390. /**
  391. * USB_DEVICE - macro used to describe a specific usb device
  392. * @vend: the 16 bit USB Vendor ID
  393. * @prod: the 16 bit USB Product ID
  394. *
  395. * This macro is used to create a struct usb_device_id that matches a
  396. * specific device.
  397. */
  398. #define USB_DEVICE(vend,prod) \
  399. .match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), .idProduct = (prod)
  400. /**
  401. * USB_DEVICE_VER - macro used to describe a specific usb device with a version range
  402. * @vend: the 16 bit USB Vendor ID
  403. * @prod: the 16 bit USB Product ID
  404. * @lo: the bcdDevice_lo value
  405. * @hi: the bcdDevice_hi value
  406. *
  407. * This macro is used to create a struct usb_device_id that matches a
  408. * specific device, with a version range.
  409. */
  410. #define USB_DEVICE_VER(vend,prod,lo,hi) \
  411. .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, .idVendor = (vend), .idProduct = (prod), .bcdDevice_lo = (lo), .bcdDevice_hi = (hi)
  412. /**
  413. * USB_DEVICE_INFO - macro used to describe a class of usb devices
  414. * @cl: bDeviceClass value
  415. * @sc: bDeviceSubClass value
  416. * @pr: bDeviceProtocol value
  417. *
  418. * This macro is used to create a struct usb_device_id that matches a
  419. * specific class of devices.
  420. */
  421. #define USB_DEVICE_INFO(cl,sc,pr) \
  422. .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, .bDeviceClass = (cl), .bDeviceSubClass = (sc), .bDeviceProtocol = (pr)
  423. /**
  424. * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces
  425. * @cl: bInterfaceClass value
  426. * @sc: bInterfaceSubClass value
  427. * @pr: bInterfaceProtocol value
  428. *
  429. * This macro is used to create a struct usb_device_id that matches a
  430. * specific class of interfaces.
  431. */
  432. #define USB_INTERFACE_INFO(cl,sc,pr) \
  433. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, .bInterfaceClass = (cl), .bInterfaceSubClass = (sc), .bInterfaceProtocol = (pr)
  434. /* -------------------------------------------------------------------------- */
  435. /**
  436. * struct usb_driver - identifies USB driver to usbcore
  437. * @owner: Pointer to the module owner of this driver; initialize
  438. * it using THIS_MODULE.
  439. * @name: The driver name should be unique among USB drivers,
  440. * and should normally be the same as the module name.
  441. * @probe: Called to see if the driver is willing to manage a particular
  442. * interface on a device. If it is, probe returns zero and uses
  443. * dev_set_drvdata() to associate driver-specific data with the
  444. * interface. It may also use usb_set_interface() to specify the
  445. * appropriate altsetting. If unwilling to manage the interface,
  446. * return a negative errno value.
  447. * @disconnect: Called when the interface is no longer accessible, usually
  448. * because its device has been (or is being) disconnected or the
  449. * driver module is being unloaded.
  450. * @ioctl: Used for drivers that want to talk to userspace through
  451. * the "usbfs" filesystem. This lets devices provide ways to
  452. * expose information to user space regardless of where they
  453. * do (or don't) show up otherwise in the filesystem.
  454. * @suspend: Called when the device is going to be suspended by the system.
  455. * @resume: Called when the device is being resumed by the system.
  456. * @id_table: USB drivers use ID table to support hotplugging.
  457. * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set
  458. * or your driver's probe function will never get called.
  459. * @driver: the driver model core driver structure.
  460. *
  461. * USB drivers must provide a name, probe() and disconnect() methods,
  462. * and an id_table. Other driver fields are optional.
  463. *
  464. * The id_table is used in hotplugging. It holds a set of descriptors,
  465. * and specialized data may be associated with each entry. That table
  466. * is used by both user and kernel mode hotplugging support.
  467. *
  468. * The probe() and disconnect() methods are called in a context where
  469. * they can sleep, but they should avoid abusing the privilege. Most
  470. * work to connect to a device should be done when the device is opened,
  471. * and undone at the last close. The disconnect code needs to address
  472. * concurrency issues with respect to open() and close() methods, as
  473. * well as forcing all pending I/O requests to complete (by unlinking
  474. * them as necessary, and blocking until the unlinks complete).
  475. */
  476. struct usb_driver {
  477. struct module *owner;
  478. const char *name;
  479. int (*probe) (struct usb_interface *intf,
  480. const struct usb_device_id *id);
  481. void (*disconnect) (struct usb_interface *intf);
  482. int (*ioctl) (struct usb_interface *intf, unsigned int code, void *buf);
  483. int (*suspend) (struct usb_interface *intf, pm_message_t message);
  484. int (*resume) (struct usb_interface *intf);
  485. const struct usb_device_id *id_table;
  486. struct device_driver driver;
  487. };
  488. #define to_usb_driver(d) container_of(d, struct usb_driver, driver)
  489. extern struct bus_type usb_bus_type;
  490. /**
  491. * struct usb_class_driver - identifies a USB driver that wants to use the USB major number
  492. * @name: devfs name for this driver. Will also be used by the driver
  493. * class code to create a usb class device.
  494. * @fops: pointer to the struct file_operations of this driver.
  495. * @mode: the mode for the devfs file to be created for this driver.
  496. * @minor_base: the start of the minor range for this driver.
  497. *
  498. * This structure is used for the usb_register_dev() and
  499. * usb_unregister_dev() functions, to consolidate a number of the
  500. * parameters used for them.
  501. */
  502. struct usb_class_driver {
  503. char *name;
  504. struct file_operations *fops;
  505. mode_t mode;
  506. int minor_base;
  507. };
  508. /*
  509. * use these in module_init()/module_exit()
  510. * and don't forget MODULE_DEVICE_TABLE(usb, ...)
  511. */
  512. extern int usb_register(struct usb_driver *);
  513. extern void usb_deregister(struct usb_driver *);
  514. extern int usb_register_dev(struct usb_interface *intf,
  515. struct usb_class_driver *class_driver);
  516. extern void usb_deregister_dev(struct usb_interface *intf,
  517. struct usb_class_driver *class_driver);
  518. extern int usb_disabled(void);
  519. /* -------------------------------------------------------------------------- */
  520. /*
  521. * URB support, for asynchronous request completions
  522. */
  523. /*
  524. * urb->transfer_flags:
  525. */
  526. #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
  527. #define URB_ISO_ASAP 0x0002 /* iso-only, urb->start_frame ignored */
  528. #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */
  529. #define URB_NO_SETUP_DMA_MAP 0x0008 /* urb->setup_dma valid on submit */
  530. #define URB_ASYNC_UNLINK 0x0010 /* usb_unlink_urb() returns asap */
  531. #define URB_NO_FSBR 0x0020 /* UHCI-specific */
  532. #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUTs with short packet */
  533. #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt needed */
  534. struct usb_iso_packet_descriptor {
  535. unsigned int offset;
  536. unsigned int length; /* expected length */
  537. unsigned int actual_length;
  538. unsigned int status;
  539. };
  540. struct urb;
  541. struct pt_regs;
  542. typedef void (*usb_complete_t)(struct urb *, struct pt_regs *);
  543. /**
  544. * struct urb - USB Request Block
  545. * @urb_list: For use by current owner of the URB.
  546. * @pipe: Holds endpoint number, direction, type, and more.
  547. * Create these values with the eight macros available;
  548. * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl"
  549. * (control), "bulk", "int" (interrupt), or "iso" (isochronous).
  550. * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint
  551. * numbers range from zero to fifteen. Note that "in" endpoint two
  552. * is a different endpoint (and pipe) from "out" endpoint two.
  553. * The current configuration controls the existence, type, and
  554. * maximum packet size of any given endpoint.
  555. * @dev: Identifies the USB device to perform the request.
  556. * @status: This is read in non-iso completion functions to get the
  557. * status of the particular request. ISO requests only use it
  558. * to tell whether the URB was unlinked; detailed status for
  559. * each frame is in the fields of the iso_frame-desc.
  560. * @transfer_flags: A variety of flags may be used to affect how URB
  561. * submission, unlinking, or operation are handled. Different
  562. * kinds of URB can use different flags.
  563. * @transfer_buffer: This identifies the buffer to (or from) which
  564. * the I/O request will be performed (unless URB_NO_TRANSFER_DMA_MAP
  565. * is set). This buffer must be suitable for DMA; allocate it with
  566. * kmalloc() or equivalent. For transfers to "in" endpoints, contents
  567. * of this buffer will be modified. This buffer is used for the data
  568. * stage of control transfers.
  569. * @transfer_dma: When transfer_flags includes URB_NO_TRANSFER_DMA_MAP,
  570. * the device driver is saying that it provided this DMA address,
  571. * which the host controller driver should use in preference to the
  572. * transfer_buffer.
  573. * @transfer_buffer_length: How big is transfer_buffer. The transfer may
  574. * be broken up into chunks according to the current maximum packet
  575. * size for the endpoint, which is a function of the configuration
  576. * and is encoded in the pipe. When the length is zero, neither
  577. * transfer_buffer nor transfer_dma is used.
  578. * @actual_length: This is read in non-iso completion functions, and
  579. * it tells how many bytes (out of transfer_buffer_length) were
  580. * transferred. It will normally be the same as requested, unless
  581. * either an error was reported or a short read was performed.
  582. * The URB_SHORT_NOT_OK transfer flag may be used to make such
  583. * short reads be reported as errors.
  584. * @setup_packet: Only used for control transfers, this points to eight bytes
  585. * of setup data. Control transfers always start by sending this data
  586. * to the device. Then transfer_buffer is read or written, if needed.
  587. * @setup_dma: For control transfers with URB_NO_SETUP_DMA_MAP set, the
  588. * device driver has provided this DMA address for the setup packet.
  589. * The host controller driver should use this in preference to
  590. * setup_packet.
  591. * @start_frame: Returns the initial frame for isochronous transfers.
  592. * @number_of_packets: Lists the number of ISO transfer buffers.
  593. * @interval: Specifies the polling interval for interrupt or isochronous
  594. * transfers. The units are frames (milliseconds) for for full and low
  595. * speed devices, and microframes (1/8 millisecond) for highspeed ones.
  596. * @error_count: Returns the number of ISO transfers that reported errors.
  597. * @context: For use in completion functions. This normally points to
  598. * request-specific driver context.
  599. * @complete: Completion handler. This URB is passed as the parameter to the
  600. * completion function. The completion function may then do what
  601. * it likes with the URB, including resubmitting or freeing it.
  602. * @iso_frame_desc: Used to provide arrays of ISO transfer buffers and to
  603. * collect the transfer status for each buffer.
  604. *
  605. * This structure identifies USB transfer requests. URBs must be allocated by
  606. * calling usb_alloc_urb() and freed with a call to usb_free_urb().
  607. * Initialization may be done using various usb_fill_*_urb() functions. URBs
  608. * are submitted using usb_submit_urb(), and pending requests may be canceled
  609. * using usb_unlink_urb() or usb_kill_urb().
  610. *
  611. * Data Transfer Buffers:
  612. *
  613. * Normally drivers provide I/O buffers allocated with kmalloc() or otherwise
  614. * taken from the general page pool. That is provided by transfer_buffer
  615. * (control requests also use setup_packet), and host controller drivers
  616. * perform a dma mapping (and unmapping) for each buffer transferred. Those
  617. * mapping operations can be expensive on some platforms (perhaps using a dma
  618. * bounce buffer or talking to an IOMMU),
  619. * although they're cheap on commodity x86 and ppc hardware.
  620. *
  621. * Alternatively, drivers may pass the URB_NO_xxx_DMA_MAP transfer flags,
  622. * which tell the host controller driver that no such mapping is needed since
  623. * the device driver is DMA-aware. For example, a device driver might
  624. * allocate a DMA buffer with usb_buffer_alloc() or call usb_buffer_map().
  625. * When these transfer flags are provided, host controller drivers will
  626. * attempt to use the dma addresses found in the transfer_dma and/or
  627. * setup_dma fields rather than determining a dma address themselves. (Note
  628. * that transfer_buffer and setup_packet must still be set because not all
  629. * host controllers use DMA, nor do virtual root hubs).
  630. *
  631. * Initialization:
  632. *
  633. * All URBs submitted must initialize the dev, pipe, transfer_flags (may be
  634. * zero), and complete fields.
  635. * The URB_ASYNC_UNLINK transfer flag affects later invocations of
  636. * the usb_unlink_urb() routine. Note: Failure to set URB_ASYNC_UNLINK
  637. * with usb_unlink_urb() is deprecated. For synchronous unlinks use
  638. * usb_kill_urb() instead.
  639. *
  640. * All URBs must also initialize
  641. * transfer_buffer and transfer_buffer_length. They may provide the
  642. * URB_SHORT_NOT_OK transfer flag, indicating that short reads are
  643. * to be treated as errors; that flag is invalid for write requests.
  644. *
  645. * Bulk URBs may
  646. * use the URB_ZERO_PACKET transfer flag, indicating that bulk OUT transfers
  647. * should always terminate with a short packet, even if it means adding an
  648. * extra zero length packet.
  649. *
  650. * Control URBs must provide a setup_packet. The setup_packet and
  651. * transfer_buffer may each be mapped for DMA or not, independently of
  652. * the other. The transfer_flags bits URB_NO_TRANSFER_DMA_MAP and
  653. * URB_NO_SETUP_DMA_MAP indicate which buffers have already been mapped.
  654. * URB_NO_SETUP_DMA_MAP is ignored for non-control URBs.
  655. *
  656. * Interrupt URBs must provide an interval, saying how often (in milliseconds
  657. * or, for highspeed devices, 125 microsecond units)
  658. * to poll for transfers. After the URB has been submitted, the interval
  659. * field reflects how the transfer was actually scheduled.
  660. * The polling interval may be more frequent than requested.
  661. * For example, some controllers have a maximum interval of 32 milliseconds,
  662. * while others support intervals of up to 1024 milliseconds.
  663. * Isochronous URBs also have transfer intervals. (Note that for isochronous
  664. * endpoints, as well as high speed interrupt endpoints, the encoding of
  665. * the transfer interval in the endpoint descriptor is logarithmic.
  666. * Device drivers must convert that value to linear units themselves.)
  667. *
  668. * Isochronous URBs normally use the URB_ISO_ASAP transfer flag, telling
  669. * the host controller to schedule the transfer as soon as bandwidth
  670. * utilization allows, and then set start_frame to reflect the actual frame
  671. * selected during submission. Otherwise drivers must specify the start_frame
  672. * and handle the case where the transfer can't begin then. However, drivers
  673. * won't know how bandwidth is currently allocated, and while they can
  674. * find the current frame using usb_get_current_frame_number () they can't
  675. * know the range for that frame number. (Ranges for frame counter values
  676. * are HC-specific, and can go from 256 to 65536 frames from "now".)
  677. *
  678. * Isochronous URBs have a different data transfer model, in part because
  679. * the quality of service is only "best effort". Callers provide specially
  680. * allocated URBs, with number_of_packets worth of iso_frame_desc structures
  681. * at the end. Each such packet is an individual ISO transfer. Isochronous
  682. * URBs are normally queued, submitted by drivers to arrange that
  683. * transfers are at least double buffered, and then explicitly resubmitted
  684. * in completion handlers, so
  685. * that data (such as audio or video) streams at as constant a rate as the
  686. * host controller scheduler can support.
  687. *
  688. * Completion Callbacks:
  689. *
  690. * The completion callback is made in_interrupt(), and one of the first
  691. * things that a completion handler should do is check the status field.
  692. * The status field is provided for all URBs. It is used to report
  693. * unlinked URBs, and status for all non-ISO transfers. It should not
  694. * be examined before the URB is returned to the completion handler.
  695. *
  696. * The context field is normally used to link URBs back to the relevant
  697. * driver or request state.
  698. *
  699. * When the completion callback is invoked for non-isochronous URBs, the
  700. * actual_length field tells how many bytes were transferred. This field
  701. * is updated even when the URB terminated with an error or was unlinked.
  702. *
  703. * ISO transfer status is reported in the status and actual_length fields
  704. * of the iso_frame_desc array, and the number of errors is reported in
  705. * error_count. Completion callbacks for ISO transfers will normally
  706. * (re)submit URBs to ensure a constant transfer rate.
  707. *
  708. * Note that even fields marked "public" should not be touched by the driver
  709. * when the urb is owned by the hcd, that is, since the call to
  710. * usb_submit_urb() till the entry into the completion routine.
  711. */
  712. struct urb
  713. {
  714. /* private, usb core and host controller only fields in the urb */
  715. struct kref kref; /* reference count of the URB */
  716. spinlock_t lock; /* lock for the URB */
  717. void *hcpriv; /* private data for host controller */
  718. int bandwidth; /* bandwidth for INT/ISO request */
  719. atomic_t use_count; /* concurrent submissions counter */
  720. u8 reject; /* submissions will fail */
  721. /* public, documented fields in the urb that can be used by drivers */
  722. struct list_head urb_list; /* list head for use by the urb owner */
  723. struct usb_device *dev; /* (in) pointer to associated device */
  724. unsigned int pipe; /* (in) pipe information */
  725. int status; /* (return) non-ISO status */
  726. unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
  727. void *transfer_buffer; /* (in) associated data buffer */
  728. dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
  729. int transfer_buffer_length; /* (in) data buffer length */
  730. int actual_length; /* (return) actual transfer length */
  731. unsigned char *setup_packet; /* (in) setup packet (control only) */
  732. dma_addr_t setup_dma; /* (in) dma addr for setup_packet */
  733. int start_frame; /* (modify) start frame (ISO) */
  734. int number_of_packets; /* (in) number of ISO packets */
  735. int interval; /* (modify) transfer interval (INT/ISO) */
  736. int error_count; /* (return) number of ISO errors */
  737. void *context; /* (in) context for completion */
  738. usb_complete_t complete; /* (in) completion routine */
  739. struct usb_iso_packet_descriptor iso_frame_desc[0]; /* (in) ISO ONLY */
  740. };
  741. /* -------------------------------------------------------------------------- */
  742. /**
  743. * usb_fill_control_urb - initializes a control urb
  744. * @urb: pointer to the urb to initialize.
  745. * @dev: pointer to the struct usb_device for this urb.
  746. * @pipe: the endpoint pipe
  747. * @setup_packet: pointer to the setup_packet buffer
  748. * @transfer_buffer: pointer to the transfer buffer
  749. * @buffer_length: length of the transfer buffer
  750. * @complete: pointer to the usb_complete_t function
  751. * @context: what to set the urb context to.
  752. *
  753. * Initializes a control urb with the proper information needed to submit
  754. * it to a device.
  755. */
  756. static inline void usb_fill_control_urb (struct urb *urb,
  757. struct usb_device *dev,
  758. unsigned int pipe,
  759. unsigned char *setup_packet,
  760. void *transfer_buffer,
  761. int buffer_length,
  762. usb_complete_t complete,
  763. void *context)
  764. {
  765. spin_lock_init(&urb->lock);
  766. urb->dev = dev;
  767. urb->pipe = pipe;
  768. urb->setup_packet = setup_packet;
  769. urb->transfer_buffer = transfer_buffer;
  770. urb->transfer_buffer_length = buffer_length;
  771. urb->complete = complete;
  772. urb->context = context;
  773. }
  774. /**
  775. * usb_fill_bulk_urb - macro to help initialize a bulk urb
  776. * @urb: pointer to the urb to initialize.
  777. * @dev: pointer to the struct usb_device for this urb.
  778. * @pipe: the endpoint pipe
  779. * @transfer_buffer: pointer to the transfer buffer
  780. * @buffer_length: length of the transfer buffer
  781. * @complete: pointer to the usb_complete_t function
  782. * @context: what to set the urb context to.
  783. *
  784. * Initializes a bulk urb with the proper information needed to submit it
  785. * to a device.
  786. */
  787. static inline void usb_fill_bulk_urb (struct urb *urb,
  788. struct usb_device *dev,
  789. unsigned int pipe,
  790. void *transfer_buffer,
  791. int buffer_length,
  792. usb_complete_t complete,
  793. void *context)
  794. {
  795. spin_lock_init(&urb->lock);
  796. urb->dev = dev;
  797. urb->pipe = pipe;
  798. urb->transfer_buffer = transfer_buffer;
  799. urb->transfer_buffer_length = buffer_length;
  800. urb->complete = complete;
  801. urb->context = context;
  802. }
  803. /**
  804. * usb_fill_int_urb - macro to help initialize a interrupt urb
  805. * @urb: pointer to the urb to initialize.
  806. * @dev: pointer to the struct usb_device for this urb.
  807. * @pipe: the endpoint pipe
  808. * @transfer_buffer: pointer to the transfer buffer
  809. * @buffer_length: length of the transfer buffer
  810. * @complete: pointer to the usb_complete_t function
  811. * @context: what to set the urb context to.
  812. * @interval: what to set the urb interval to, encoded like
  813. * the endpoint descriptor's bInterval value.
  814. *
  815. * Initializes a interrupt urb with the proper information needed to submit
  816. * it to a device.
  817. * Note that high speed interrupt endpoints use a logarithmic encoding of
  818. * the endpoint interval, and express polling intervals in microframes
  819. * (eight per millisecond) rather than in frames (one per millisecond).
  820. */
  821. static inline void usb_fill_int_urb (struct urb *urb,
  822. struct usb_device *dev,
  823. unsigned int pipe,
  824. void *transfer_buffer,
  825. int buffer_length,
  826. usb_complete_t complete,
  827. void *context,
  828. int interval)
  829. {
  830. spin_lock_init(&urb->lock);
  831. urb->dev = dev;
  832. urb->pipe = pipe;
  833. urb->transfer_buffer = transfer_buffer;
  834. urb->transfer_buffer_length = buffer_length;
  835. urb->complete = complete;
  836. urb->context = context;
  837. if (dev->speed == USB_SPEED_HIGH)
  838. urb->interval = 1 << (interval - 1);
  839. else
  840. urb->interval = interval;
  841. urb->start_frame = -1;
  842. }
  843. extern void usb_init_urb(struct urb *urb);
  844. extern struct urb *usb_alloc_urb(int iso_packets, int mem_flags);
  845. extern void usb_free_urb(struct urb *urb);
  846. #define usb_put_urb usb_free_urb
  847. extern struct urb *usb_get_urb(struct urb *urb);
  848. extern int usb_submit_urb(struct urb *urb, int mem_flags);
  849. extern int usb_unlink_urb(struct urb *urb);
  850. extern void usb_kill_urb(struct urb *urb);
  851. #define HAVE_USB_BUFFERS
  852. void *usb_buffer_alloc (struct usb_device *dev, size_t size,
  853. int mem_flags, dma_addr_t *dma);
  854. void usb_buffer_free (struct usb_device *dev, size_t size,
  855. void *addr, dma_addr_t dma);
  856. #if 0
  857. struct urb *usb_buffer_map (struct urb *urb);
  858. void usb_buffer_dmasync (struct urb *urb);
  859. void usb_buffer_unmap (struct urb *urb);
  860. #endif
  861. struct scatterlist;
  862. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  863. struct scatterlist *sg, int nents);
  864. #if 0
  865. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  866. struct scatterlist *sg, int n_hw_ents);
  867. #endif
  868. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  869. struct scatterlist *sg, int n_hw_ents);
  870. /*-------------------------------------------------------------------*
  871. * SYNCHRONOUS CALL SUPPORT *
  872. *-------------------------------------------------------------------*/
  873. extern int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  874. __u8 request, __u8 requesttype, __u16 value, __u16 index,
  875. void *data, __u16 size, int timeout);
  876. extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
  877. void *data, int len, int *actual_length,
  878. int timeout);
  879. /* selective suspend/resume */
  880. extern int usb_suspend_device(struct usb_device *dev, pm_message_t message);
  881. extern int usb_resume_device(struct usb_device *dev);
  882. /* wrappers around usb_control_msg() for the most common standard requests */
  883. extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype,
  884. unsigned char descindex, void *buf, int size);
  885. extern int usb_get_status(struct usb_device *dev,
  886. int type, int target, void *data);
  887. extern int usb_get_string(struct usb_device *dev,
  888. unsigned short langid, unsigned char index, void *buf, int size);
  889. extern int usb_string(struct usb_device *dev, int index,
  890. char *buf, size_t size);
  891. /* wrappers that also update important state inside usbcore */
  892. extern int usb_clear_halt(struct usb_device *dev, int pipe);
  893. extern int usb_reset_configuration(struct usb_device *dev);
  894. extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate);
  895. /*
  896. * timeouts, in milliseconds, used for sending/receiving control messages
  897. * they typically complete within a few frames (msec) after they're issued
  898. * USB identifies 5 second timeouts, maybe more in a few cases, and a few
  899. * slow devices (like some MGE Ellipse UPSes) actually push that limit.
  900. */
  901. #define USB_CTRL_GET_TIMEOUT 5000
  902. #define USB_CTRL_SET_TIMEOUT 5000
  903. /**
  904. * struct usb_sg_request - support for scatter/gather I/O
  905. * @status: zero indicates success, else negative errno
  906. * @bytes: counts bytes transferred.
  907. *
  908. * These requests are initialized using usb_sg_init(), and then are used
  909. * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most
  910. * members of the request object aren't for driver access.
  911. *
  912. * The status and bytecount values are valid only after usb_sg_wait()
  913. * returns. If the status is zero, then the bytecount matches the total
  914. * from the request.
  915. *
  916. * After an error completion, drivers may need to clear a halt condition
  917. * on the endpoint.
  918. */
  919. struct usb_sg_request {
  920. int status;
  921. size_t bytes;
  922. /*
  923. * members below are private to usbcore,
  924. * and are not provided for driver access!
  925. */
  926. spinlock_t lock;
  927. struct usb_device *dev;
  928. int pipe;
  929. struct scatterlist *sg;
  930. int nents;
  931. int entries;
  932. struct urb **urbs;
  933. int count;
  934. struct completion complete;
  935. };
  936. int usb_sg_init (
  937. struct usb_sg_request *io,
  938. struct usb_device *dev,
  939. unsigned pipe,
  940. unsigned period,
  941. struct scatterlist *sg,
  942. int nents,
  943. size_t length,
  944. int mem_flags
  945. );
  946. void usb_sg_cancel (struct usb_sg_request *io);
  947. void usb_sg_wait (struct usb_sg_request *io);
  948. /* -------------------------------------------------------------------------- */
  949. /*
  950. * For various legacy reasons, Linux has a small cookie that's paired with
  951. * a struct usb_device to identify an endpoint queue. Queue characteristics
  952. * are defined by the endpoint's descriptor. This cookie is called a "pipe",
  953. * an unsigned int encoded as:
  954. *
  955. * - direction: bit 7 (0 = Host-to-Device [Out],
  956. * 1 = Device-to-Host [In] ...
  957. * like endpoint bEndpointAddress)
  958. * - device address: bits 8-14 ... bit positions known to uhci-hcd
  959. * - endpoint: bits 15-18 ... bit positions known to uhci-hcd
  960. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  961. * 10 = control, 11 = bulk)
  962. *
  963. * Given the device address and endpoint descriptor, pipes are redundant.
  964. */
  965. /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */
  966. /* (yet ... they're the values used by usbfs) */
  967. #define PIPE_ISOCHRONOUS 0
  968. #define PIPE_INTERRUPT 1
  969. #define PIPE_CONTROL 2
  970. #define PIPE_BULK 3
  971. #define usb_pipein(pipe) ((pipe) & USB_DIR_IN)
  972. #define usb_pipeout(pipe) (!usb_pipein(pipe))
  973. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  974. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  975. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  976. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  977. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  978. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  979. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  980. /* The D0/D1 toggle bits ... USE WITH CAUTION (they're almost hcd-internal) */
  981. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> (ep)) & 1)
  982. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << (ep)))
  983. #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << (ep))) | ((bit) << (ep)))
  984. static inline unsigned int __create_pipe(struct usb_device *dev, unsigned int endpoint)
  985. {
  986. return (dev->devnum << 8) | (endpoint << 15);
  987. }
  988. /* Create various pipes... */
  989. #define usb_sndctrlpipe(dev,endpoint) ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint))
  990. #define usb_rcvctrlpipe(dev,endpoint) ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  991. #define usb_sndisocpipe(dev,endpoint) ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint))
  992. #define usb_rcvisocpipe(dev,endpoint) ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  993. #define usb_sndbulkpipe(dev,endpoint) ((PIPE_BULK << 30) | __create_pipe(dev,endpoint))
  994. #define usb_rcvbulkpipe(dev,endpoint) ((PIPE_BULK << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  995. #define usb_sndintpipe(dev,endpoint) ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint))
  996. #define usb_rcvintpipe(dev,endpoint) ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  997. /*-------------------------------------------------------------------------*/
  998. static inline __u16
  999. usb_maxpacket(struct usb_device *udev, int pipe, int is_out)
  1000. {
  1001. struct usb_host_endpoint *ep;
  1002. unsigned epnum = usb_pipeendpoint(pipe);
  1003. if (is_out) {
  1004. WARN_ON(usb_pipein(pipe));
  1005. ep = udev->ep_out[epnum];
  1006. } else {
  1007. WARN_ON(usb_pipeout(pipe));
  1008. ep = udev->ep_in[epnum];
  1009. }
  1010. if (!ep)
  1011. return 0;
  1012. /* NOTE: only 0x07ff bits are for packet size... */
  1013. return le16_to_cpu(ep->desc.wMaxPacketSize);
  1014. }
  1015. /* -------------------------------------------------------------------------- */
  1016. #ifdef DEBUG
  1017. #define dbg(format, arg...) printk(KERN_DEBUG "%s: " format "\n" , __FILE__ , ## arg)
  1018. #else
  1019. #define dbg(format, arg...) do {} while (0)
  1020. #endif
  1021. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n" , __FILE__ , ## arg)
  1022. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n" , __FILE__ , ## arg)
  1023. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n" , __FILE__ , ## arg)
  1024. #endif /* __KERNEL__ */
  1025. #endif