usb.h 47 KB

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