usb.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. #ifndef __LINUX_USB_H
  2. #define __LINUX_USB_H
  3. #include <linux/mod_devicetable.h>
  4. #include <linux/usb_ch9.h>
  5. #define USB_MAJOR 180
  6. #define USB_DEVICE_MAJOR 189
  7. #ifdef __KERNEL__
  8. #include <linux/errno.h> /* for -ENODEV */
  9. #include <linux/delay.h> /* for mdelay() */
  10. #include <linux/interrupt.h> /* for in_interrupt() */
  11. #include <linux/list.h> /* for struct list_head */
  12. #include <linux/kref.h> /* for struct kref */
  13. #include <linux/device.h> /* for struct device */
  14. #include <linux/fs.h> /* for struct file_operations */
  15. #include <linux/completion.h> /* for struct completion */
  16. #include <linux/sched.h> /* for current && schedule_timeout */
  17. struct usb_device;
  18. struct usb_driver;
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * Host-side wrappers for standard USB descriptors ... these are parsed
  22. * from the data provided by devices. Parsing turns them from a flat
  23. * sequence of descriptors into a hierarchy:
  24. *
  25. * - devices have one (usually) or more configs;
  26. * - configs have one (often) or more interfaces;
  27. * - interfaces have one (usually) or more settings;
  28. * - each interface setting has zero or (usually) more endpoints.
  29. *
  30. * And there might be other descriptors mixed in with those.
  31. *
  32. * Devices may also have class-specific or vendor-specific descriptors.
  33. */
  34. struct ep_device;
  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. * @ep_dev: ep_device 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 ep_device *ep_dev; /* 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 int usb_reset_composite_device(struct usb_device *dev,
  333. struct usb_interface *iface);
  334. extern struct usb_device *usb_find_device(u16 vendor_id, u16 product_id);
  335. /*-------------------------------------------------------------------------*/
  336. /* for drivers using iso endpoints */
  337. extern int usb_get_current_frame_number (struct usb_device *usb_dev);
  338. /* used these for multi-interface device registration */
  339. extern int usb_driver_claim_interface(struct usb_driver *driver,
  340. struct usb_interface *iface, void* priv);
  341. /**
  342. * usb_interface_claimed - returns true iff an interface is claimed
  343. * @iface: the interface being checked
  344. *
  345. * Returns true (nonzero) iff the interface is claimed, else false (zero).
  346. * Callers must own the driver model's usb bus readlock. So driver
  347. * probe() entries don't need extra locking, but other call contexts
  348. * may need to explicitly claim that lock.
  349. *
  350. */
  351. static inline int usb_interface_claimed(struct usb_interface *iface) {
  352. return (iface->dev.driver != NULL);
  353. }
  354. extern void usb_driver_release_interface(struct usb_driver *driver,
  355. struct usb_interface *iface);
  356. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  357. const struct usb_device_id *id);
  358. extern struct usb_interface *usb_find_interface(struct usb_driver *drv,
  359. int minor);
  360. extern struct usb_interface *usb_ifnum_to_if(struct usb_device *dev,
  361. unsigned ifnum);
  362. extern struct usb_host_interface *usb_altnum_to_altsetting(
  363. struct usb_interface *intf, unsigned int altnum);
  364. /**
  365. * usb_make_path - returns stable device path in the usb tree
  366. * @dev: the device whose path is being constructed
  367. * @buf: where to put the string
  368. * @size: how big is "buf"?
  369. *
  370. * Returns length of the string (> 0) or negative if size was too small.
  371. *
  372. * This identifier is intended to be "stable", reflecting physical paths in
  373. * hardware such as physical bus addresses for host controllers or ports on
  374. * USB hubs. That makes it stay the same until systems are physically
  375. * reconfigured, by re-cabling a tree of USB devices or by moving USB host
  376. * controllers. Adding and removing devices, including virtual root hubs
  377. * in host controller driver modules, does not change these path identifers;
  378. * neither does rebooting or re-enumerating. These are more useful identifiers
  379. * than changeable ("unstable") ones like bus numbers or device addresses.
  380. *
  381. * With a partial exception for devices connected to USB 2.0 root hubs, these
  382. * identifiers are also predictable. So long as the device tree isn't changed,
  383. * plugging any USB device into a given hub port always gives it the same path.
  384. * Because of the use of "companion" controllers, devices connected to ports on
  385. * USB 2.0 root hubs (EHCI host controllers) will get one path ID if they are
  386. * high speed, and a different one if they are full or low speed.
  387. */
  388. static inline int usb_make_path (struct usb_device *dev, char *buf,
  389. size_t size)
  390. {
  391. int actual;
  392. actual = snprintf (buf, size, "usb-%s-%s", dev->bus->bus_name,
  393. dev->devpath);
  394. return (actual >= (int)size) ? -1 : actual;
  395. }
  396. /*-------------------------------------------------------------------------*/
  397. #define USB_DEVICE_ID_MATCH_DEVICE \
  398. (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
  399. #define USB_DEVICE_ID_MATCH_DEV_RANGE \
  400. (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI)
  401. #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \
  402. (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE)
  403. #define USB_DEVICE_ID_MATCH_DEV_INFO \
  404. (USB_DEVICE_ID_MATCH_DEV_CLASS | \
  405. USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \
  406. USB_DEVICE_ID_MATCH_DEV_PROTOCOL)
  407. #define USB_DEVICE_ID_MATCH_INT_INFO \
  408. (USB_DEVICE_ID_MATCH_INT_CLASS | \
  409. USB_DEVICE_ID_MATCH_INT_SUBCLASS | \
  410. USB_DEVICE_ID_MATCH_INT_PROTOCOL)
  411. /**
  412. * USB_DEVICE - macro used to describe a specific usb device
  413. * @vend: the 16 bit USB Vendor ID
  414. * @prod: the 16 bit USB Product ID
  415. *
  416. * This macro is used to create a struct usb_device_id that matches a
  417. * specific device.
  418. */
  419. #define USB_DEVICE(vend,prod) \
  420. .match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \
  421. .idProduct = (prod)
  422. /**
  423. * USB_DEVICE_VER - macro used to describe a specific usb device with a
  424. * version range
  425. * @vend: the 16 bit USB Vendor ID
  426. * @prod: the 16 bit USB Product ID
  427. * @lo: the bcdDevice_lo value
  428. * @hi: the bcdDevice_hi value
  429. *
  430. * This macro is used to create a struct usb_device_id that matches a
  431. * specific device, with a version range.
  432. */
  433. #define USB_DEVICE_VER(vend,prod,lo,hi) \
  434. .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \
  435. .idVendor = (vend), .idProduct = (prod), \
  436. .bcdDevice_lo = (lo), .bcdDevice_hi = (hi)
  437. /**
  438. * USB_DEVICE_INFO - macro used to describe a class of usb devices
  439. * @cl: bDeviceClass value
  440. * @sc: bDeviceSubClass value
  441. * @pr: bDeviceProtocol value
  442. *
  443. * This macro is used to create a struct usb_device_id that matches a
  444. * specific class of devices.
  445. */
  446. #define USB_DEVICE_INFO(cl,sc,pr) \
  447. .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, .bDeviceClass = (cl), \
  448. .bDeviceSubClass = (sc), .bDeviceProtocol = (pr)
  449. /**
  450. * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces
  451. * @cl: bInterfaceClass value
  452. * @sc: bInterfaceSubClass value
  453. * @pr: bInterfaceProtocol value
  454. *
  455. * This macro is used to create a struct usb_device_id that matches a
  456. * specific class of interfaces.
  457. */
  458. #define USB_INTERFACE_INFO(cl,sc,pr) \
  459. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, .bInterfaceClass = (cl), \
  460. .bInterfaceSubClass = (sc), .bInterfaceProtocol = (pr)
  461. /* ----------------------------------------------------------------------- */
  462. struct usb_dynids {
  463. spinlock_t lock;
  464. struct list_head list;
  465. };
  466. /**
  467. * struct usbdrv_wrap - wrapper for driver-model structure
  468. * @driver: The driver-model core driver structure.
  469. * @for_devices: Non-zero for device drivers, 0 for interface drivers.
  470. */
  471. struct usbdrv_wrap {
  472. struct device_driver driver;
  473. int for_devices;
  474. };
  475. /**
  476. * struct usb_driver - identifies USB interface driver to usbcore
  477. * @name: The driver name should be unique among USB drivers,
  478. * and should normally be the same as the module name.
  479. * @probe: Called to see if the driver is willing to manage a particular
  480. * interface on a device. If it is, probe returns zero and uses
  481. * dev_set_drvdata() to associate driver-specific data with the
  482. * interface. It may also use usb_set_interface() to specify the
  483. * appropriate altsetting. If unwilling to manage the interface,
  484. * return a negative errno value.
  485. * @disconnect: Called when the interface is no longer accessible, usually
  486. * because its device has been (or is being) disconnected or the
  487. * driver module is being unloaded.
  488. * @ioctl: Used for drivers that want to talk to userspace through
  489. * the "usbfs" filesystem. This lets devices provide ways to
  490. * expose information to user space regardless of where they
  491. * do (or don't) show up otherwise in the filesystem.
  492. * @suspend: Called when the device is going to be suspended by the system.
  493. * @resume: Called when the device is being resumed by the system.
  494. * @pre_reset: Called by usb_reset_composite_device() when the device
  495. * is about to be reset.
  496. * @post_reset: Called by usb_reset_composite_device() after the device
  497. * has been reset.
  498. * @id_table: USB drivers use ID table to support hotplugging.
  499. * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set
  500. * or your driver's probe function will never get called.
  501. * @dynids: used internally to hold the list of dynamically added device
  502. * ids for this driver.
  503. * @drvwrap: Driver-model core structure wrapper.
  504. * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be
  505. * added to this driver by preventing the sysfs file from being created.
  506. *
  507. * USB interface drivers must provide a name, probe() and disconnect()
  508. * methods, and an id_table. Other driver fields are optional.
  509. *
  510. * The id_table is used in hotplugging. It holds a set of descriptors,
  511. * and specialized data may be associated with each entry. That table
  512. * is used by both user and kernel mode hotplugging support.
  513. *
  514. * The probe() and disconnect() methods are called in a context where
  515. * they can sleep, but they should avoid abusing the privilege. Most
  516. * work to connect to a device should be done when the device is opened,
  517. * and undone at the last close. The disconnect code needs to address
  518. * concurrency issues with respect to open() and close() methods, as
  519. * well as forcing all pending I/O requests to complete (by unlinking
  520. * them as necessary, and blocking until the unlinks complete).
  521. */
  522. struct usb_driver {
  523. const char *name;
  524. int (*probe) (struct usb_interface *intf,
  525. const struct usb_device_id *id);
  526. void (*disconnect) (struct usb_interface *intf);
  527. int (*ioctl) (struct usb_interface *intf, unsigned int code,
  528. void *buf);
  529. int (*suspend) (struct usb_interface *intf, pm_message_t message);
  530. int (*resume) (struct usb_interface *intf);
  531. void (*pre_reset) (struct usb_interface *intf);
  532. void (*post_reset) (struct usb_interface *intf);
  533. const struct usb_device_id *id_table;
  534. struct usb_dynids dynids;
  535. struct usbdrv_wrap drvwrap;
  536. unsigned int no_dynamic_id:1;
  537. };
  538. #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver)
  539. /**
  540. * struct usb_device_driver - identifies USB device driver to usbcore
  541. * @name: The driver name should be unique among USB drivers,
  542. * and should normally be the same as the module name.
  543. * @probe: Called to see if the driver is willing to manage a particular
  544. * device. If it is, probe returns zero and uses dev_set_drvdata()
  545. * to associate driver-specific data with the device. If unwilling
  546. * to manage the device, return a negative errno value.
  547. * @disconnect: Called when the device is no longer accessible, usually
  548. * because it has been (or is being) disconnected or the driver's
  549. * module is being unloaded.
  550. * @suspend: Called when the device is going to be suspended by the system.
  551. * @resume: Called when the device is being resumed by the system.
  552. * @drvwrap: Driver-model core structure wrapper.
  553. *
  554. * USB drivers must provide all the fields listed above except drvwrap.
  555. */
  556. struct usb_device_driver {
  557. const char *name;
  558. int (*probe) (struct usb_device *udev);
  559. void (*disconnect) (struct usb_device *udev);
  560. int (*suspend) (struct usb_device *udev, pm_message_t message);
  561. int (*resume) (struct usb_device *udev);
  562. struct usbdrv_wrap drvwrap;
  563. };
  564. #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \
  565. drvwrap.driver)
  566. extern struct bus_type usb_bus_type;
  567. /**
  568. * struct usb_class_driver - identifies a USB driver that wants to use the USB major number
  569. * @name: the usb class device name for this driver. Will show up in sysfs.
  570. * @fops: pointer to the struct file_operations of this driver.
  571. * @minor_base: the start of the minor range for this driver.
  572. *
  573. * This structure is used for the usb_register_dev() and
  574. * usb_unregister_dev() functions, to consolidate a number of the
  575. * parameters used for them.
  576. */
  577. struct usb_class_driver {
  578. char *name;
  579. const struct file_operations *fops;
  580. int minor_base;
  581. };
  582. /*
  583. * use these in module_init()/module_exit()
  584. * and don't forget MODULE_DEVICE_TABLE(usb, ...)
  585. */
  586. extern int usb_register_driver(struct usb_driver *, struct module *);
  587. static inline int usb_register(struct usb_driver *driver)
  588. {
  589. return usb_register_driver(driver, THIS_MODULE);
  590. }
  591. extern void usb_deregister(struct usb_driver *);
  592. extern int usb_register_device_driver(struct usb_device_driver *,
  593. struct module *);
  594. extern void usb_deregister_device_driver(struct usb_device_driver *);
  595. extern int usb_register_dev(struct usb_interface *intf,
  596. struct usb_class_driver *class_driver);
  597. extern void usb_deregister_dev(struct usb_interface *intf,
  598. struct usb_class_driver *class_driver);
  599. extern int usb_disabled(void);
  600. /* ----------------------------------------------------------------------- */
  601. /*
  602. * URB support, for asynchronous request completions
  603. */
  604. /*
  605. * urb->transfer_flags:
  606. */
  607. #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
  608. #define URB_ISO_ASAP 0x0002 /* iso-only, urb->start_frame
  609. * ignored */
  610. #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */
  611. #define URB_NO_SETUP_DMA_MAP 0x0008 /* urb->setup_dma valid on submit */
  612. #define URB_NO_FSBR 0x0020 /* UHCI-specific */
  613. #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
  614. #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt
  615. * needed */
  616. struct usb_iso_packet_descriptor {
  617. unsigned int offset;
  618. unsigned int length; /* expected length */
  619. unsigned int actual_length;
  620. unsigned int status;
  621. };
  622. struct urb;
  623. struct pt_regs;
  624. typedef void (*usb_complete_t)(struct urb *, struct pt_regs *);
  625. /**
  626. * struct urb - USB Request Block
  627. * @urb_list: For use by current owner of the URB.
  628. * @pipe: Holds endpoint number, direction, type, and more.
  629. * Create these values with the eight macros available;
  630. * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl"
  631. * (control), "bulk", "int" (interrupt), or "iso" (isochronous).
  632. * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint
  633. * numbers range from zero to fifteen. Note that "in" endpoint two
  634. * is a different endpoint (and pipe) from "out" endpoint two.
  635. * The current configuration controls the existence, type, and
  636. * maximum packet size of any given endpoint.
  637. * @dev: Identifies the USB device to perform the request.
  638. * @status: This is read in non-iso completion functions to get the
  639. * status of the particular request. ISO requests only use it
  640. * to tell whether the URB was unlinked; detailed status for
  641. * each frame is in the fields of the iso_frame-desc.
  642. * @transfer_flags: A variety of flags may be used to affect how URB
  643. * submission, unlinking, or operation are handled. Different
  644. * kinds of URB can use different flags.
  645. * @transfer_buffer: This identifies the buffer to (or from) which
  646. * the I/O request will be performed (unless URB_NO_TRANSFER_DMA_MAP
  647. * is set). This buffer must be suitable for DMA; allocate it with
  648. * kmalloc() or equivalent. For transfers to "in" endpoints, contents
  649. * of this buffer will be modified. This buffer is used for the data
  650. * stage of control transfers.
  651. * @transfer_dma: When transfer_flags includes URB_NO_TRANSFER_DMA_MAP,
  652. * the device driver is saying that it provided this DMA address,
  653. * which the host controller driver should use in preference to the
  654. * transfer_buffer.
  655. * @transfer_buffer_length: How big is transfer_buffer. The transfer may
  656. * be broken up into chunks according to the current maximum packet
  657. * size for the endpoint, which is a function of the configuration
  658. * and is encoded in the pipe. When the length is zero, neither
  659. * transfer_buffer nor transfer_dma is used.
  660. * @actual_length: This is read in non-iso completion functions, and
  661. * it tells how many bytes (out of transfer_buffer_length) were
  662. * transferred. It will normally be the same as requested, unless
  663. * either an error was reported or a short read was performed.
  664. * The URB_SHORT_NOT_OK transfer flag may be used to make such
  665. * short reads be reported as errors.
  666. * @setup_packet: Only used for control transfers, this points to eight bytes
  667. * of setup data. Control transfers always start by sending this data
  668. * to the device. Then transfer_buffer is read or written, if needed.
  669. * @setup_dma: For control transfers with URB_NO_SETUP_DMA_MAP set, the
  670. * device driver has provided this DMA address for the setup packet.
  671. * The host controller driver should use this in preference to
  672. * setup_packet.
  673. * @start_frame: Returns the initial frame for isochronous transfers.
  674. * @number_of_packets: Lists the number of ISO transfer buffers.
  675. * @interval: Specifies the polling interval for interrupt or isochronous
  676. * transfers. The units are frames (milliseconds) for for full and low
  677. * speed devices, and microframes (1/8 millisecond) for highspeed ones.
  678. * @error_count: Returns the number of ISO transfers that reported errors.
  679. * @context: For use in completion functions. This normally points to
  680. * request-specific driver context.
  681. * @complete: Completion handler. This URB is passed as the parameter to the
  682. * completion function. The completion function may then do what
  683. * it likes with the URB, including resubmitting or freeing it.
  684. * @iso_frame_desc: Used to provide arrays of ISO transfer buffers and to
  685. * collect the transfer status for each buffer.
  686. *
  687. * This structure identifies USB transfer requests. URBs must be allocated by
  688. * calling usb_alloc_urb() and freed with a call to usb_free_urb().
  689. * Initialization may be done using various usb_fill_*_urb() functions. URBs
  690. * are submitted using usb_submit_urb(), and pending requests may be canceled
  691. * using usb_unlink_urb() or usb_kill_urb().
  692. *
  693. * Data Transfer Buffers:
  694. *
  695. * Normally drivers provide I/O buffers allocated with kmalloc() or otherwise
  696. * taken from the general page pool. That is provided by transfer_buffer
  697. * (control requests also use setup_packet), and host controller drivers
  698. * perform a dma mapping (and unmapping) for each buffer transferred. Those
  699. * mapping operations can be expensive on some platforms (perhaps using a dma
  700. * bounce buffer or talking to an IOMMU),
  701. * although they're cheap on commodity x86 and ppc hardware.
  702. *
  703. * Alternatively, drivers may pass the URB_NO_xxx_DMA_MAP transfer flags,
  704. * which tell the host controller driver that no such mapping is needed since
  705. * the device driver is DMA-aware. For example, a device driver might
  706. * allocate a DMA buffer with usb_buffer_alloc() or call usb_buffer_map().
  707. * When these transfer flags are provided, host controller drivers will
  708. * attempt to use the dma addresses found in the transfer_dma and/or
  709. * setup_dma fields rather than determining a dma address themselves. (Note
  710. * that transfer_buffer and setup_packet must still be set because not all
  711. * host controllers use DMA, nor do virtual root hubs).
  712. *
  713. * Initialization:
  714. *
  715. * All URBs submitted must initialize the dev, pipe, transfer_flags (may be
  716. * zero), and complete fields. All URBs must also initialize
  717. * transfer_buffer and transfer_buffer_length. They may provide the
  718. * URB_SHORT_NOT_OK transfer flag, indicating that short reads are
  719. * to be treated as errors; that flag is invalid for write requests.
  720. *
  721. * Bulk URBs may
  722. * use the URB_ZERO_PACKET transfer flag, indicating that bulk OUT transfers
  723. * should always terminate with a short packet, even if it means adding an
  724. * extra zero length packet.
  725. *
  726. * Control URBs must provide a setup_packet. The setup_packet and
  727. * transfer_buffer may each be mapped for DMA or not, independently of
  728. * the other. The transfer_flags bits URB_NO_TRANSFER_DMA_MAP and
  729. * URB_NO_SETUP_DMA_MAP indicate which buffers have already been mapped.
  730. * URB_NO_SETUP_DMA_MAP is ignored for non-control URBs.
  731. *
  732. * Interrupt URBs must provide an interval, saying how often (in milliseconds
  733. * or, for highspeed devices, 125 microsecond units)
  734. * to poll for transfers. After the URB has been submitted, the interval
  735. * field reflects how the transfer was actually scheduled.
  736. * The polling interval may be more frequent than requested.
  737. * For example, some controllers have a maximum interval of 32 milliseconds,
  738. * while others support intervals of up to 1024 milliseconds.
  739. * Isochronous URBs also have transfer intervals. (Note that for isochronous
  740. * endpoints, as well as high speed interrupt endpoints, the encoding of
  741. * the transfer interval in the endpoint descriptor is logarithmic.
  742. * Device drivers must convert that value to linear units themselves.)
  743. *
  744. * Isochronous URBs normally use the URB_ISO_ASAP transfer flag, telling
  745. * the host controller to schedule the transfer as soon as bandwidth
  746. * utilization allows, and then set start_frame to reflect the actual frame
  747. * selected during submission. Otherwise drivers must specify the start_frame
  748. * and handle the case where the transfer can't begin then. However, drivers
  749. * won't know how bandwidth is currently allocated, and while they can
  750. * find the current frame using usb_get_current_frame_number () they can't
  751. * know the range for that frame number. (Ranges for frame counter values
  752. * are HC-specific, and can go from 256 to 65536 frames from "now".)
  753. *
  754. * Isochronous URBs have a different data transfer model, in part because
  755. * the quality of service is only "best effort". Callers provide specially
  756. * allocated URBs, with number_of_packets worth of iso_frame_desc structures
  757. * at the end. Each such packet is an individual ISO transfer. Isochronous
  758. * URBs are normally queued, submitted by drivers to arrange that
  759. * transfers are at least double buffered, and then explicitly resubmitted
  760. * in completion handlers, so
  761. * that data (such as audio or video) streams at as constant a rate as the
  762. * host controller scheduler can support.
  763. *
  764. * Completion Callbacks:
  765. *
  766. * The completion callback is made in_interrupt(), and one of the first
  767. * things that a completion handler should do is check the status field.
  768. * The status field is provided for all URBs. It is used to report
  769. * unlinked URBs, and status for all non-ISO transfers. It should not
  770. * be examined before the URB is returned to the completion handler.
  771. *
  772. * The context field is normally used to link URBs back to the relevant
  773. * driver or request state.
  774. *
  775. * When the completion callback is invoked for non-isochronous URBs, the
  776. * actual_length field tells how many bytes were transferred. This field
  777. * is updated even when the URB terminated with an error or was unlinked.
  778. *
  779. * ISO transfer status is reported in the status and actual_length fields
  780. * of the iso_frame_desc array, and the number of errors is reported in
  781. * error_count. Completion callbacks for ISO transfers will normally
  782. * (re)submit URBs to ensure a constant transfer rate.
  783. *
  784. * Note that even fields marked "public" should not be touched by the driver
  785. * when the urb is owned by the hcd, that is, since the call to
  786. * usb_submit_urb() till the entry into the completion routine.
  787. */
  788. struct urb
  789. {
  790. /* private: usb core and host controller only fields in the urb */
  791. struct kref kref; /* reference count of the URB */
  792. spinlock_t lock; /* lock for the URB */
  793. void *hcpriv; /* private data for host controller */
  794. int bandwidth; /* bandwidth for INT/ISO request */
  795. atomic_t use_count; /* concurrent submissions counter */
  796. u8 reject; /* submissions will fail */
  797. /* public: documented fields in the urb that can be used by drivers */
  798. struct list_head urb_list; /* list head for use by the urb's
  799. * current owner */
  800. struct usb_device *dev; /* (in) pointer to associated device */
  801. unsigned int pipe; /* (in) pipe information */
  802. int status; /* (return) non-ISO status */
  803. unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
  804. void *transfer_buffer; /* (in) associated data buffer */
  805. dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
  806. int transfer_buffer_length; /* (in) data buffer length */
  807. int actual_length; /* (return) actual transfer length */
  808. unsigned char *setup_packet; /* (in) setup packet (control only) */
  809. dma_addr_t setup_dma; /* (in) dma addr for setup_packet */
  810. int start_frame; /* (modify) start frame (ISO) */
  811. int number_of_packets; /* (in) number of ISO packets */
  812. int interval; /* (modify) transfer interval
  813. * (INT/ISO) */
  814. int error_count; /* (return) number of ISO errors */
  815. void *context; /* (in) context for completion */
  816. usb_complete_t complete; /* (in) completion routine */
  817. struct usb_iso_packet_descriptor iso_frame_desc[0];
  818. /* (in) ISO ONLY */
  819. };
  820. /* ----------------------------------------------------------------------- */
  821. /**
  822. * usb_fill_control_urb - initializes a control urb
  823. * @urb: pointer to the urb to initialize.
  824. * @dev: pointer to the struct usb_device for this urb.
  825. * @pipe: the endpoint pipe
  826. * @setup_packet: pointer to the setup_packet buffer
  827. * @transfer_buffer: pointer to the transfer buffer
  828. * @buffer_length: length of the transfer buffer
  829. * @complete: pointer to the usb_complete_t function
  830. * @context: what to set the urb context to.
  831. *
  832. * Initializes a control urb with the proper information needed to submit
  833. * it to a device.
  834. */
  835. static inline void usb_fill_control_urb (struct urb *urb,
  836. struct usb_device *dev,
  837. unsigned int pipe,
  838. unsigned char *setup_packet,
  839. void *transfer_buffer,
  840. int buffer_length,
  841. usb_complete_t complete,
  842. void *context)
  843. {
  844. spin_lock_init(&urb->lock);
  845. urb->dev = dev;
  846. urb->pipe = pipe;
  847. urb->setup_packet = setup_packet;
  848. urb->transfer_buffer = transfer_buffer;
  849. urb->transfer_buffer_length = buffer_length;
  850. urb->complete = complete;
  851. urb->context = context;
  852. }
  853. /**
  854. * usb_fill_bulk_urb - macro to help initialize a bulk urb
  855. * @urb: pointer to the urb to initialize.
  856. * @dev: pointer to the struct usb_device for this urb.
  857. * @pipe: the endpoint pipe
  858. * @transfer_buffer: pointer to the transfer buffer
  859. * @buffer_length: length of the transfer buffer
  860. * @complete: pointer to the usb_complete_t function
  861. * @context: what to set the urb context to.
  862. *
  863. * Initializes a bulk urb with the proper information needed to submit it
  864. * to a device.
  865. */
  866. static inline void usb_fill_bulk_urb (struct urb *urb,
  867. struct usb_device *dev,
  868. unsigned int pipe,
  869. void *transfer_buffer,
  870. int buffer_length,
  871. usb_complete_t complete,
  872. void *context)
  873. {
  874. spin_lock_init(&urb->lock);
  875. urb->dev = dev;
  876. urb->pipe = pipe;
  877. urb->transfer_buffer = transfer_buffer;
  878. urb->transfer_buffer_length = buffer_length;
  879. urb->complete = complete;
  880. urb->context = context;
  881. }
  882. /**
  883. * usb_fill_int_urb - macro to help initialize a interrupt urb
  884. * @urb: pointer to the urb to initialize.
  885. * @dev: pointer to the struct usb_device for this urb.
  886. * @pipe: the endpoint pipe
  887. * @transfer_buffer: pointer to the transfer buffer
  888. * @buffer_length: length of the transfer buffer
  889. * @complete: pointer to the usb_complete_t function
  890. * @context: what to set the urb context to.
  891. * @interval: what to set the urb interval to, encoded like
  892. * the endpoint descriptor's bInterval value.
  893. *
  894. * Initializes a interrupt urb with the proper information needed to submit
  895. * it to a device.
  896. * Note that high speed interrupt endpoints use a logarithmic encoding of
  897. * the endpoint interval, and express polling intervals in microframes
  898. * (eight per millisecond) rather than in frames (one per millisecond).
  899. */
  900. static inline void usb_fill_int_urb (struct urb *urb,
  901. struct usb_device *dev,
  902. unsigned int pipe,
  903. void *transfer_buffer,
  904. int buffer_length,
  905. usb_complete_t complete,
  906. void *context,
  907. int interval)
  908. {
  909. spin_lock_init(&urb->lock);
  910. urb->dev = dev;
  911. urb->pipe = pipe;
  912. urb->transfer_buffer = transfer_buffer;
  913. urb->transfer_buffer_length = buffer_length;
  914. urb->complete = complete;
  915. urb->context = context;
  916. if (dev->speed == USB_SPEED_HIGH)
  917. urb->interval = 1 << (interval - 1);
  918. else
  919. urb->interval = interval;
  920. urb->start_frame = -1;
  921. }
  922. extern void usb_init_urb(struct urb *urb);
  923. extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags);
  924. extern void usb_free_urb(struct urb *urb);
  925. #define usb_put_urb usb_free_urb
  926. extern struct urb *usb_get_urb(struct urb *urb);
  927. extern int usb_submit_urb(struct urb *urb, gfp_t mem_flags);
  928. extern int usb_unlink_urb(struct urb *urb);
  929. extern void usb_kill_urb(struct urb *urb);
  930. #define HAVE_USB_BUFFERS
  931. void *usb_buffer_alloc (struct usb_device *dev, size_t size,
  932. gfp_t mem_flags, dma_addr_t *dma);
  933. void usb_buffer_free (struct usb_device *dev, size_t size,
  934. void *addr, dma_addr_t dma);
  935. #if 0
  936. struct urb *usb_buffer_map (struct urb *urb);
  937. void usb_buffer_dmasync (struct urb *urb);
  938. void usb_buffer_unmap (struct urb *urb);
  939. #endif
  940. struct scatterlist;
  941. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  942. struct scatterlist *sg, int nents);
  943. #if 0
  944. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  945. struct scatterlist *sg, int n_hw_ents);
  946. #endif
  947. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  948. struct scatterlist *sg, int n_hw_ents);
  949. /*-------------------------------------------------------------------*
  950. * SYNCHRONOUS CALL SUPPORT *
  951. *-------------------------------------------------------------------*/
  952. extern int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  953. __u8 request, __u8 requesttype, __u16 value, __u16 index,
  954. void *data, __u16 size, int timeout);
  955. extern int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
  956. void *data, int len, int *actual_length, int timeout);
  957. extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
  958. void *data, int len, int *actual_length,
  959. int timeout);
  960. /* wrappers around usb_control_msg() for the most common standard requests */
  961. extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype,
  962. unsigned char descindex, void *buf, int size);
  963. extern int usb_get_status(struct usb_device *dev,
  964. int type, int target, void *data);
  965. extern int usb_string(struct usb_device *dev, int index,
  966. char *buf, size_t size);
  967. /* wrappers that also update important state inside usbcore */
  968. extern int usb_clear_halt(struct usb_device *dev, int pipe);
  969. extern int usb_reset_configuration(struct usb_device *dev);
  970. extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate);
  971. /*
  972. * timeouts, in milliseconds, used for sending/receiving control messages
  973. * they typically complete within a few frames (msec) after they're issued
  974. * USB identifies 5 second timeouts, maybe more in a few cases, and a few
  975. * slow devices (like some MGE Ellipse UPSes) actually push that limit.
  976. */
  977. #define USB_CTRL_GET_TIMEOUT 5000
  978. #define USB_CTRL_SET_TIMEOUT 5000
  979. /**
  980. * struct usb_sg_request - support for scatter/gather I/O
  981. * @status: zero indicates success, else negative errno
  982. * @bytes: counts bytes transferred.
  983. *
  984. * These requests are initialized using usb_sg_init(), and then are used
  985. * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most
  986. * members of the request object aren't for driver access.
  987. *
  988. * The status and bytecount values are valid only after usb_sg_wait()
  989. * returns. If the status is zero, then the bytecount matches the total
  990. * from the request.
  991. *
  992. * After an error completion, drivers may need to clear a halt condition
  993. * on the endpoint.
  994. */
  995. struct usb_sg_request {
  996. int status;
  997. size_t bytes;
  998. /*
  999. * members below are private: to usbcore,
  1000. * and are not provided for driver access!
  1001. */
  1002. spinlock_t lock;
  1003. struct usb_device *dev;
  1004. int pipe;
  1005. struct scatterlist *sg;
  1006. int nents;
  1007. int entries;
  1008. struct urb **urbs;
  1009. int count;
  1010. struct completion complete;
  1011. };
  1012. int usb_sg_init (
  1013. struct usb_sg_request *io,
  1014. struct usb_device *dev,
  1015. unsigned pipe,
  1016. unsigned period,
  1017. struct scatterlist *sg,
  1018. int nents,
  1019. size_t length,
  1020. gfp_t mem_flags
  1021. );
  1022. void usb_sg_cancel (struct usb_sg_request *io);
  1023. void usb_sg_wait (struct usb_sg_request *io);
  1024. /* ----------------------------------------------------------------------- */
  1025. /*
  1026. * For various legacy reasons, Linux has a small cookie that's paired with
  1027. * a struct usb_device to identify an endpoint queue. Queue characteristics
  1028. * are defined by the endpoint's descriptor. This cookie is called a "pipe",
  1029. * an unsigned int encoded as:
  1030. *
  1031. * - direction: bit 7 (0 = Host-to-Device [Out],
  1032. * 1 = Device-to-Host [In] ...
  1033. * like endpoint bEndpointAddress)
  1034. * - device address: bits 8-14 ... bit positions known to uhci-hcd
  1035. * - endpoint: bits 15-18 ... bit positions known to uhci-hcd
  1036. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  1037. * 10 = control, 11 = bulk)
  1038. *
  1039. * Given the device address and endpoint descriptor, pipes are redundant.
  1040. */
  1041. /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */
  1042. /* (yet ... they're the values used by usbfs) */
  1043. #define PIPE_ISOCHRONOUS 0
  1044. #define PIPE_INTERRUPT 1
  1045. #define PIPE_CONTROL 2
  1046. #define PIPE_BULK 3
  1047. #define usb_pipein(pipe) ((pipe) & USB_DIR_IN)
  1048. #define usb_pipeout(pipe) (!usb_pipein(pipe))
  1049. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  1050. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  1051. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  1052. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  1053. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  1054. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  1055. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  1056. /* The D0/D1 toggle bits ... USE WITH CAUTION (they're almost hcd-internal) */
  1057. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> (ep)) & 1)
  1058. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << (ep)))
  1059. #define usb_settoggle(dev, ep, out, bit) \
  1060. ((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << (ep))) | \
  1061. ((bit) << (ep)))
  1062. static inline unsigned int __create_pipe(struct usb_device *dev,
  1063. unsigned int endpoint)
  1064. {
  1065. return (dev->devnum << 8) | (endpoint << 15);
  1066. }
  1067. /* Create various pipes... */
  1068. #define usb_sndctrlpipe(dev,endpoint) \
  1069. ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint))
  1070. #define usb_rcvctrlpipe(dev,endpoint) \
  1071. ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  1072. #define usb_sndisocpipe(dev,endpoint) \
  1073. ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint))
  1074. #define usb_rcvisocpipe(dev,endpoint) \
  1075. ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  1076. #define usb_sndbulkpipe(dev,endpoint) \
  1077. ((PIPE_BULK << 30) | __create_pipe(dev,endpoint))
  1078. #define usb_rcvbulkpipe(dev,endpoint) \
  1079. ((PIPE_BULK << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  1080. #define usb_sndintpipe(dev,endpoint) \
  1081. ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint))
  1082. #define usb_rcvintpipe(dev,endpoint) \
  1083. ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)
  1084. /*-------------------------------------------------------------------------*/
  1085. static inline __u16
  1086. usb_maxpacket(struct usb_device *udev, int pipe, int is_out)
  1087. {
  1088. struct usb_host_endpoint *ep;
  1089. unsigned epnum = usb_pipeendpoint(pipe);
  1090. if (is_out) {
  1091. WARN_ON(usb_pipein(pipe));
  1092. ep = udev->ep_out[epnum];
  1093. } else {
  1094. WARN_ON(usb_pipeout(pipe));
  1095. ep = udev->ep_in[epnum];
  1096. }
  1097. if (!ep)
  1098. return 0;
  1099. /* NOTE: only 0x07ff bits are for packet size... */
  1100. return le16_to_cpu(ep->desc.wMaxPacketSize);
  1101. }
  1102. /* ----------------------------------------------------------------------- */
  1103. /* Events from the usb core */
  1104. #define USB_DEVICE_ADD 0x0001
  1105. #define USB_DEVICE_REMOVE 0x0002
  1106. #define USB_BUS_ADD 0x0003
  1107. #define USB_BUS_REMOVE 0x0004
  1108. extern void usb_register_notify(struct notifier_block *nb);
  1109. extern void usb_unregister_notify(struct notifier_block *nb);
  1110. #ifdef DEBUG
  1111. #define dbg(format, arg...) printk(KERN_DEBUG "%s: " format "\n" , \
  1112. __FILE__ , ## arg)
  1113. #else
  1114. #define dbg(format, arg...) do {} while (0)
  1115. #endif
  1116. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n" , \
  1117. __FILE__ , ## arg)
  1118. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n" , \
  1119. __FILE__ , ## arg)
  1120. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n" , \
  1121. __FILE__ , ## arg)
  1122. #endif /* __KERNEL__ */
  1123. #endif