usb.h 46 KB

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