usb.tmpl 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="Linux-USB-API">
  5. <bookinfo>
  6. <title>The Linux-USB Host Side API</title>
  7. <legalnotice>
  8. <para>
  9. This documentation is free software; you can redistribute
  10. it and/or modify it under the terms of the GNU General Public
  11. License as published by the Free Software Foundation; either
  12. version 2 of the License, or (at your option) any later
  13. version.
  14. </para>
  15. <para>
  16. This program is distributed in the hope that it will be
  17. useful, but WITHOUT ANY WARRANTY; without even the implied
  18. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. See the GNU General Public License for more details.
  20. </para>
  21. <para>
  22. You should have received a copy of the GNU General Public
  23. License along with this program; if not, write to the Free
  24. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. MA 02111-1307 USA
  26. </para>
  27. <para>
  28. For more details see the file COPYING in the source
  29. distribution of Linux.
  30. </para>
  31. </legalnotice>
  32. </bookinfo>
  33. <toc></toc>
  34. <chapter id="intro">
  35. <title>Introduction to USB on Linux</title>
  36. <para>A Universal Serial Bus (USB) is used to connect a host,
  37. such as a PC or workstation, to a number of peripheral
  38. devices. USB uses a tree structure, with the host as the
  39. root (the system's master), hubs as interior nodes, and
  40. peripherals as leaves (and slaves).
  41. Modern PCs support several such trees of USB devices, usually
  42. one USB 2.0 tree (480 Mbit/sec each) with
  43. a few USB 1.1 trees (12 Mbit/sec each) that are used when you
  44. connect a USB 1.1 device directly to the machine's "root hub".
  45. </para>
  46. <para>That master/slave asymmetry was designed-in for a number of
  47. reasons, one being ease of use. It is not physically possible to
  48. assemble (legal) USB cables incorrectly: all upstream "to the host"
  49. connectors are the rectangular type (matching the sockets on
  50. root hubs), and all downstream connectors are the squarish type
  51. (or they are built into the peripheral).
  52. Also, the host software doesn't need to deal with distributed
  53. auto-configuration since the pre-designated master node manages all that.
  54. And finally, at the electrical level, bus protocol overhead is reduced by
  55. eliminating arbitration and moving scheduling into the host software.
  56. </para>
  57. <para>USB 1.0 was announced in January 1996 and was revised
  58. as USB 1.1 (with improvements in hub specification and
  59. support for interrupt-out transfers) in September 1998.
  60. USB 2.0 was released in April 2000, adding high-speed
  61. transfers and transaction-translating hubs (used for USB 1.1
  62. and 1.0 backward compatibility).
  63. </para>
  64. <para>Kernel developers added USB support to Linux early in the 2.2 kernel
  65. series, shortly before 2.3 development forked. Updates from 2.3 were
  66. regularly folded back into 2.2 releases, which improved reliability and
  67. brought <filename>/sbin/hotplug</filename> support as well more drivers.
  68. Such improvements were continued in the 2.5 kernel series, where they added
  69. USB 2.0 support, improved performance, and made the host controller drivers
  70. (HCDs) more consistent. They also simplified the API (to make bugs less
  71. likely) and added internal "kerneldoc" documentation.
  72. </para>
  73. <para>Linux can run inside USB devices as well as on
  74. the hosts that control the devices.
  75. But USB device drivers running inside those peripherals
  76. don't do the same things as the ones running inside hosts,
  77. so they've been given a different name:
  78. <emphasis>gadget drivers</emphasis>.
  79. This document does not cover gadget drivers.
  80. </para>
  81. </chapter>
  82. <chapter id="host">
  83. <title>USB Host-Side API Model</title>
  84. <para>Host-side drivers for USB devices talk to the "usbcore" APIs.
  85. There are two. One is intended for
  86. <emphasis>general-purpose</emphasis> drivers (exposed through
  87. driver frameworks), and the other is for drivers that are
  88. <emphasis>part of the core</emphasis>.
  89. Such core drivers include the <emphasis>hub</emphasis> driver
  90. (which manages trees of USB devices) and several different kinds
  91. of <emphasis>host controller drivers</emphasis>,
  92. which control individual busses.
  93. </para>
  94. <para>The device model seen by USB drivers is relatively complex.
  95. </para>
  96. <itemizedlist>
  97. <listitem><para>USB supports four kinds of data transfers
  98. (control, bulk, interrupt, and isochronous). Two of them (control
  99. and bulk) use bandwidth as it's available,
  100. while the other two (interrupt and isochronous)
  101. are scheduled to provide guaranteed bandwidth.
  102. </para></listitem>
  103. <listitem><para>The device description model includes one or more
  104. "configurations" per device, only one of which is active at a time.
  105. Devices that are capable of high-speed operation must also support
  106. full-speed configurations, along with a way to ask about the
  107. "other speed" configurations which might be used.
  108. </para></listitem>
  109. <listitem><para>Configurations have one or more "interfaces", each
  110. of which may have "alternate settings". Interfaces may be
  111. standardized by USB "Class" specifications, or may be specific to
  112. a vendor or device.</para>
  113. <para>USB device drivers actually bind to interfaces, not devices.
  114. Think of them as "interface drivers", though you
  115. may not see many devices where the distinction is important.
  116. <emphasis>Most USB devices are simple, with only one configuration,
  117. one interface, and one alternate setting.</emphasis>
  118. </para></listitem>
  119. <listitem><para>Interfaces have one or more "endpoints", each of
  120. which supports one type and direction of data transfer such as
  121. "bulk out" or "interrupt in". The entire configuration may have
  122. up to sixteen endpoints in each direction, allocated as needed
  123. among all the interfaces.
  124. </para></listitem>
  125. <listitem><para>Data transfer on USB is packetized; each endpoint
  126. has a maximum packet size.
  127. Drivers must often be aware of conventions such as flagging the end
  128. of bulk transfers using "short" (including zero length) packets.
  129. </para></listitem>
  130. <listitem><para>The Linux USB API supports synchronous calls for
  131. control and bulk messages.
  132. It also supports asynchnous calls for all kinds of data transfer,
  133. using request structures called "URBs" (USB Request Blocks).
  134. </para></listitem>
  135. </itemizedlist>
  136. <para>Accordingly, the USB Core API exposed to device drivers
  137. covers quite a lot of territory. You'll probably need to consult
  138. the USB 2.0 specification, available online from www.usb.org at
  139. no cost, as well as class or device specifications.
  140. </para>
  141. <para>The only host-side drivers that actually touch hardware
  142. (reading/writing registers, handling IRQs, and so on) are the HCDs.
  143. In theory, all HCDs provide the same functionality through the same
  144. API. In practice, that's becoming more true on the 2.5 kernels,
  145. but there are still differences that crop up especially with
  146. fault handling. Different controllers don't necessarily report
  147. the same aspects of failures, and recovery from faults (including
  148. software-induced ones like unlinking an URB) isn't yet fully
  149. consistent.
  150. Device driver authors should make a point of doing disconnect
  151. testing (while the device is active) with each different host
  152. controller driver, to make sure drivers don't have bugs of
  153. their own as well as to make sure they aren't relying on some
  154. HCD-specific behavior.
  155. (You will need external USB 1.1 and/or
  156. USB 2.0 hubs to perform all those tests.)
  157. </para>
  158. </chapter>
  159. <chapter><title>USB-Standard Types</title>
  160. <para>In <filename>&lt;linux/usb_ch9.h&gt;</filename> you will find
  161. the USB data types defined in chapter 9 of the USB specification.
  162. These data types are used throughout USB, and in APIs including
  163. this host side API, gadget APIs, and usbfs.
  164. </para>
  165. !Iinclude/linux/usb_ch9.h
  166. </chapter>
  167. <chapter><title>Host-Side Data Types and Macros</title>
  168. <para>The host side API exposes several layers to drivers, some of
  169. which are more necessary than others.
  170. These support lifecycle models for host side drivers
  171. and devices, and support passing buffers through usbcore to
  172. some HCD that performs the I/O for the device driver.
  173. </para>
  174. !Iinclude/linux/usb.h
  175. </chapter>
  176. <chapter><title>USB Core APIs</title>
  177. <para>There are two basic I/O models in the USB API.
  178. The most elemental one is asynchronous: drivers submit requests
  179. in the form of an URB, and the URB's completion callback
  180. handle the next step.
  181. All USB transfer types support that model, although there
  182. are special cases for control URBs (which always have setup
  183. and status stages, but may not have a data stage) and
  184. isochronous URBs (which allow large packets and include
  185. per-packet fault reports).
  186. Built on top of that is synchronous API support, where a
  187. driver calls a routine that allocates one or more URBs,
  188. submits them, and waits until they complete.
  189. There are synchronous wrappers for single-buffer control
  190. and bulk transfers (which are awkward to use in some
  191. driver disconnect scenarios), and for scatterlist based
  192. streaming i/o (bulk or interrupt).
  193. </para>
  194. <para>USB drivers need to provide buffers that can be
  195. used for DMA, although they don't necessarily need to
  196. provide the DMA mapping themselves.
  197. There are APIs to use used when allocating DMA buffers,
  198. which can prevent use of bounce buffers on some systems.
  199. In some cases, drivers may be able to rely on 64bit DMA
  200. to eliminate another kind of bounce buffer.
  201. </para>
  202. !Edrivers/usb/core/urb.c
  203. !Edrivers/usb/core/message.c
  204. !Edrivers/usb/core/file.c
  205. !Edrivers/usb/core/driver.c
  206. !Edrivers/usb/core/usb.c
  207. !Edrivers/usb/core/hub.c
  208. </chapter>
  209. <chapter><title>Host Controller APIs</title>
  210. <para>These APIs are only for use by host controller drivers,
  211. most of which implement standard register interfaces such as
  212. EHCI, OHCI, or UHCI.
  213. UHCI was one of the first interfaces, designed by Intel and
  214. also used by VIA; it doesn't do much in hardware.
  215. OHCI was designed later, to have the hardware do more work
  216. (bigger transfers, tracking protocol state, and so on).
  217. EHCI was designed with USB 2.0; its design has features that
  218. resemble OHCI (hardware does much more work) as well as
  219. UHCI (some parts of ISO support, TD list processing).
  220. </para>
  221. <para>There are host controllers other than the "big three",
  222. although most PCI based controllers (and a few non-PCI based
  223. ones) use one of those interfaces.
  224. Not all host controllers use DMA; some use PIO, and there
  225. is also a simulator.
  226. </para>
  227. <para>The same basic APIs are available to drivers for all
  228. those controllers.
  229. For historical reasons they are in two layers:
  230. <structname>struct usb_bus</structname> is a rather thin
  231. layer that became available in the 2.2 kernels, while
  232. <structname>struct usb_hcd</structname> is a more featureful
  233. layer (available in later 2.4 kernels and in 2.5) that
  234. lets HCDs share common code, to shrink driver size
  235. and significantly reduce hcd-specific behaviors.
  236. </para>
  237. !Edrivers/usb/core/hcd.c
  238. !Edrivers/usb/core/hcd-pci.c
  239. !Idrivers/usb/core/buffer.c
  240. </chapter>
  241. <chapter>
  242. <title>The USB Filesystem (usbfs)</title>
  243. <para>This chapter presents the Linux <emphasis>usbfs</emphasis>.
  244. You may prefer to avoid writing new kernel code for your
  245. USB driver; that's the problem that usbfs set out to solve.
  246. User mode device drivers are usually packaged as applications
  247. or libraries, and may use usbfs through some programming library
  248. that wraps it. Such libraries include
  249. <ulink url="http://libusb.sourceforge.net">libusb</ulink>
  250. for C/C++, and
  251. <ulink url="http://jUSB.sourceforge.net">jUSB</ulink> for Java.
  252. </para>
  253. <note><title>Unfinished</title>
  254. <para>This particular documentation is incomplete,
  255. especially with respect to the asynchronous mode.
  256. As of kernel 2.5.66 the code and this (new) documentation
  257. need to be cross-reviewed.
  258. </para>
  259. </note>
  260. <para>Configure usbfs into Linux kernels by enabling the
  261. <emphasis>USB filesystem</emphasis> option (CONFIG_USB_DEVICEFS),
  262. and you get basic support for user mode USB device drivers.
  263. Until relatively recently it was often (confusingly) called
  264. <emphasis>usbdevfs</emphasis> although it wasn't solving what
  265. <emphasis>devfs</emphasis> was.
  266. Every USB device will appear in usbfs, regardless of whether or
  267. not it has a kernel driver; but only devices with kernel drivers
  268. show up in devfs.
  269. </para>
  270. <sect1>
  271. <title>What files are in "usbfs"?</title>
  272. <para>Conventionally mounted at
  273. <filename>/proc/bus/usb</filename>, usbfs
  274. features include:
  275. <itemizedlist>
  276. <listitem><para><filename>/proc/bus/usb/devices</filename>
  277. ... a text file
  278. showing each of the USB devices on known to the kernel,
  279. and their configuration descriptors.
  280. You can also poll() this to learn about new devices.
  281. </para></listitem>
  282. <listitem><para><filename>/proc/bus/usb/BBB/DDD</filename>
  283. ... magic files
  284. exposing the each device's configuration descriptors, and
  285. supporting a series of ioctls for making device requests,
  286. including I/O to devices. (Purely for access by programs.)
  287. </para></listitem>
  288. </itemizedlist>
  289. </para>
  290. <para> Each bus is given a number (BBB) based on when it was
  291. enumerated; within each bus, each device is given a similar
  292. number (DDD).
  293. Those BBB/DDD paths are not "stable" identifiers;
  294. expect them to change even if you always leave the devices
  295. plugged in to the same hub port.
  296. <emphasis>Don't even think of saving these in application
  297. configuration files.</emphasis>
  298. Stable identifiers are available, for user mode applications
  299. that want to use them. HID and networking devices expose
  300. these stable IDs, so that for example you can be sure that
  301. you told the right UPS to power down its second server.
  302. "usbfs" doesn't (yet) expose those IDs.
  303. </para>
  304. </sect1>
  305. <sect1>
  306. <title>Mounting and Access Control</title>
  307. <para>There are a number of mount options for usbfs, which will
  308. be of most interest to you if you need to override the default
  309. access control policy.
  310. That policy is that only root may read or write device files
  311. (<filename>/proc/bus/BBB/DDD</filename>) although anyone may read
  312. the <filename>devices</filename>
  313. or <filename>drivers</filename> files.
  314. I/O requests to the device also need the CAP_SYS_RAWIO capability,
  315. </para>
  316. <para>The significance of that is that by default, all user mode
  317. device drivers need super-user privileges.
  318. You can change modes or ownership in a driver setup
  319. when the device hotplugs, or maye just start the
  320. driver right then, as a privileged server (or some activity
  321. within one).
  322. That's the most secure approach for multi-user systems,
  323. but for single user systems ("trusted" by that user)
  324. it's more convenient just to grant everyone all access
  325. (using the <emphasis>devmode=0666</emphasis> option)
  326. so the driver can start whenever it's needed.
  327. </para>
  328. <para>The mount options for usbfs, usable in /etc/fstab or
  329. in command line invocations of <emphasis>mount</emphasis>, are:
  330. <variablelist>
  331. <varlistentry>
  332. <term><emphasis>busgid</emphasis>=NNNNN</term>
  333. <listitem><para>Controls the GID used for the
  334. /proc/bus/usb/BBB
  335. directories. (Default: 0)</para></listitem></varlistentry>
  336. <varlistentry><term><emphasis>busmode</emphasis>=MMM</term>
  337. <listitem><para>Controls the file mode used for the
  338. /proc/bus/usb/BBB
  339. directories. (Default: 0555)
  340. </para></listitem></varlistentry>
  341. <varlistentry><term><emphasis>busuid</emphasis>=NNNNN</term>
  342. <listitem><para>Controls the UID used for the
  343. /proc/bus/usb/BBB
  344. directories. (Default: 0)</para></listitem></varlistentry>
  345. <varlistentry><term><emphasis>devgid</emphasis>=NNNNN</term>
  346. <listitem><para>Controls the GID used for the
  347. /proc/bus/usb/BBB/DDD
  348. files. (Default: 0)</para></listitem></varlistentry>
  349. <varlistentry><term><emphasis>devmode</emphasis>=MMM</term>
  350. <listitem><para>Controls the file mode used for the
  351. /proc/bus/usb/BBB/DDD
  352. files. (Default: 0644)</para></listitem></varlistentry>
  353. <varlistentry><term><emphasis>devuid</emphasis>=NNNNN</term>
  354. <listitem><para>Controls the UID used for the
  355. /proc/bus/usb/BBB/DDD
  356. files. (Default: 0)</para></listitem></varlistentry>
  357. <varlistentry><term><emphasis>listgid</emphasis>=NNNNN</term>
  358. <listitem><para>Controls the GID used for the
  359. /proc/bus/usb/devices and drivers files.
  360. (Default: 0)</para></listitem></varlistentry>
  361. <varlistentry><term><emphasis>listmode</emphasis>=MMM</term>
  362. <listitem><para>Controls the file mode used for the
  363. /proc/bus/usb/devices and drivers files.
  364. (Default: 0444)</para></listitem></varlistentry>
  365. <varlistentry><term><emphasis>listuid</emphasis>=NNNNN</term>
  366. <listitem><para>Controls the UID used for the
  367. /proc/bus/usb/devices and drivers files.
  368. (Default: 0)</para></listitem></varlistentry>
  369. </variablelist>
  370. </para>
  371. <para>Note that many Linux distributions hard-wire the mount options
  372. for usbfs in their init scripts, such as
  373. <filename>/etc/rc.d/rc.sysinit</filename>,
  374. rather than making it easy to set this per-system
  375. policy in <filename>/etc/fstab</filename>.
  376. </para>
  377. </sect1>
  378. <sect1>
  379. <title>/proc/bus/usb/devices</title>
  380. <para>This file is handy for status viewing tools in user
  381. mode, which can scan the text format and ignore most of it.
  382. More detailed device status (including class and vendor
  383. status) is available from device-specific files.
  384. For information about the current format of this file,
  385. see the
  386. <filename>Documentation/usb/proc_usb_info.txt</filename>
  387. file in your Linux kernel sources.
  388. </para>
  389. <para>This file, in combination with the poll() system call, can
  390. also be used to detect when devices are added or removed:
  391. <programlisting>int fd;
  392. struct pollfd pfd;
  393. fd = open("/proc/bus/usb/devices", O_RDONLY);
  394. pfd = { fd, POLLIN, 0 };
  395. for (;;) {
  396. /* The first time through, this call will return immediately. */
  397. poll(&amp;pfd, 1, -1);
  398. /* To see what's changed, compare the file's previous and current
  399. contents or scan the filesystem. (Scanning is more precise.) */
  400. }</programlisting>
  401. Note that this behavior is intended to be used for informational
  402. and debug purposes. It would be more appropriate to use programs
  403. such as udev or HAL to initialize a device or start a user-mode
  404. helper program, for instance.
  405. </para>
  406. </sect1>
  407. <sect1>
  408. <title>/proc/bus/usb/BBB/DDD</title>
  409. <para>Use these files in one of these basic ways:
  410. </para>
  411. <para><emphasis>They can be read,</emphasis>
  412. producing first the device descriptor
  413. (18 bytes) and then the descriptors for the current configuration.
  414. See the USB 2.0 spec for details about those binary data formats.
  415. You'll need to convert most multibyte values from little endian
  416. format to your native host byte order, although a few of the
  417. fields in the device descriptor (both of the BCD-encoded fields,
  418. and the vendor and product IDs) will be byteswapped for you.
  419. Note that configuration descriptors include descriptors for
  420. interfaces, altsettings, endpoints, and maybe additional
  421. class descriptors.
  422. </para>
  423. <para><emphasis>Perform USB operations</emphasis> using
  424. <emphasis>ioctl()</emphasis> requests to make endpoint I/O
  425. requests (synchronously or asynchronously) or manage
  426. the device.
  427. These requests need the CAP_SYS_RAWIO capability,
  428. as well as filesystem access permissions.
  429. Only one ioctl request can be made on one of these
  430. device files at a time.
  431. This means that if you are synchronously reading an endpoint
  432. from one thread, you won't be able to write to a different
  433. endpoint from another thread until the read completes.
  434. This works for <emphasis>half duplex</emphasis> protocols,
  435. but otherwise you'd use asynchronous i/o requests.
  436. </para>
  437. </sect1>
  438. <sect1>
  439. <title>Life Cycle of User Mode Drivers</title>
  440. <para>Such a driver first needs to find a device file
  441. for a device it knows how to handle.
  442. Maybe it was told about it because a
  443. <filename>/sbin/hotplug</filename> event handling agent
  444. chose that driver to handle the new device.
  445. Or maybe it's an application that scans all the
  446. /proc/bus/usb device files, and ignores most devices.
  447. In either case, it should <function>read()</function> all
  448. the descriptors from the device file,
  449. and check them against what it knows how to handle.
  450. It might just reject everything except a particular
  451. vendor and product ID, or need a more complex policy.
  452. </para>
  453. <para>Never assume there will only be one such device
  454. on the system at a time!
  455. If your code can't handle more than one device at
  456. a time, at least detect when there's more than one, and
  457. have your users choose which device to use.
  458. </para>
  459. <para>Once your user mode driver knows what device to use,
  460. it interacts with it in either of two styles.
  461. The simple style is to make only control requests; some
  462. devices don't need more complex interactions than those.
  463. (An example might be software using vendor-specific control
  464. requests for some initialization or configuration tasks,
  465. with a kernel driver for the rest.)
  466. </para>
  467. <para>More likely, you need a more complex style driver:
  468. one using non-control endpoints, reading or writing data
  469. and claiming exclusive use of an interface.
  470. <emphasis>Bulk</emphasis> transfers are easiest to use,
  471. but only their sibling <emphasis>interrupt</emphasis> transfers
  472. work with low speed devices.
  473. Both interrupt and <emphasis>isochronous</emphasis> transfers
  474. offer service guarantees because their bandwidth is reserved.
  475. Such "periodic" transfers are awkward to use through usbfs,
  476. unless you're using the asynchronous calls. However, interrupt
  477. transfers can also be used in a synchronous "one shot" style.
  478. </para>
  479. <para>Your user-mode driver should never need to worry
  480. about cleaning up request state when the device is
  481. disconnected, although it should close its open file
  482. descriptors as soon as it starts seeing the ENODEV
  483. errors.
  484. </para>
  485. </sect1>
  486. <sect1><title>The ioctl() Requests</title>
  487. <para>To use these ioctls, you need to include the following
  488. headers in your userspace program:
  489. <programlisting>#include &lt;linux/usb.h&gt;
  490. #include &lt;linux/usbdevice_fs.h&gt;
  491. #include &lt;asm/byteorder.h&gt;</programlisting>
  492. The standard USB device model requests, from "Chapter 9" of
  493. the USB 2.0 specification, are automatically included from
  494. the <filename>&lt;linux/usb_ch9.h&gt;</filename> header.
  495. </para>
  496. <para>Unless noted otherwise, the ioctl requests
  497. described here will
  498. update the modification time on the usbfs file to which
  499. they are applied (unless they fail).
  500. A return of zero indicates success; otherwise, a
  501. standard USB error code is returned. (These are
  502. documented in
  503. <filename>Documentation/usb/error-codes.txt</filename>
  504. in your kernel sources.)
  505. </para>
  506. <para>Each of these files multiplexes access to several
  507. I/O streams, one per endpoint.
  508. Each device has one control endpoint (endpoint zero)
  509. which supports a limited RPC style RPC access.
  510. Devices are configured
  511. by khubd (in the kernel) setting a device-wide
  512. <emphasis>configuration</emphasis> that affects things
  513. like power consumption and basic functionality.
  514. The endpoints are part of USB <emphasis>interfaces</emphasis>,
  515. which may have <emphasis>altsettings</emphasis>
  516. affecting things like which endpoints are available.
  517. Many devices only have a single configuration and interface,
  518. so drivers for them will ignore configurations and altsettings.
  519. </para>
  520. <sect2>
  521. <title>Management/Status Requests</title>
  522. <para>A number of usbfs requests don't deal very directly
  523. with device I/O.
  524. They mostly relate to device management and status.
  525. These are all synchronous requests.
  526. </para>
  527. <variablelist>
  528. <varlistentry><term>USBDEVFS_CLAIMINTERFACE</term>
  529. <listitem><para>This is used to force usbfs to
  530. claim a specific interface,
  531. which has not previously been claimed by usbfs or any other
  532. kernel driver.
  533. The ioctl parameter is an integer holding the number of
  534. the interface (bInterfaceNumber from descriptor).
  535. </para><para>
  536. Note that if your driver doesn't claim an interface
  537. before trying to use one of its endpoints, and no
  538. other driver has bound to it, then the interface is
  539. automatically claimed by usbfs.
  540. </para><para>
  541. This claim will be released by a RELEASEINTERFACE ioctl,
  542. or by closing the file descriptor.
  543. File modification time is not updated by this request.
  544. </para></listitem></varlistentry>
  545. <varlistentry><term>USBDEVFS_CONNECTINFO</term>
  546. <listitem><para>Says whether the device is lowspeed.
  547. The ioctl parameter points to a structure like this:
  548. <programlisting>struct usbdevfs_connectinfo {
  549. unsigned int devnum;
  550. unsigned char slow;
  551. }; </programlisting>
  552. File modification time is not updated by this request.
  553. </para><para>
  554. <emphasis>You can't tell whether a "not slow"
  555. device is connected at high speed (480 MBit/sec)
  556. or just full speed (12 MBit/sec).</emphasis>
  557. You should know the devnum value already,
  558. it's the DDD value of the device file name.
  559. </para></listitem></varlistentry>
  560. <varlistentry><term>USBDEVFS_GETDRIVER</term>
  561. <listitem><para>Returns the name of the kernel driver
  562. bound to a given interface (a string). Parameter
  563. is a pointer to this structure, which is modified:
  564. <programlisting>struct usbdevfs_getdriver {
  565. unsigned int interface;
  566. char driver[USBDEVFS_MAXDRIVERNAME + 1];
  567. };</programlisting>
  568. File modification time is not updated by this request.
  569. </para></listitem></varlistentry>
  570. <varlistentry><term>USBDEVFS_IOCTL</term>
  571. <listitem><para>Passes a request from userspace through
  572. to a kernel driver that has an ioctl entry in the
  573. <emphasis>struct usb_driver</emphasis> it registered.
  574. <programlisting>struct usbdevfs_ioctl {
  575. int ifno;
  576. int ioctl_code;
  577. void *data;
  578. };
  579. /* user mode call looks like this.
  580. * 'request' becomes the driver->ioctl() 'code' parameter.
  581. * the size of 'param' is encoded in 'request', and that data
  582. * is copied to or from the driver->ioctl() 'buf' parameter.
  583. */
  584. static int
  585. usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
  586. {
  587. struct usbdevfs_ioctl wrapper;
  588. wrapper.ifno = ifno;
  589. wrapper.ioctl_code = request;
  590. wrapper.data = param;
  591. return ioctl (fd, USBDEVFS_IOCTL, &amp;wrapper);
  592. } </programlisting>
  593. File modification time is not updated by this request.
  594. </para><para>
  595. This request lets kernel drivers talk to user mode code
  596. through filesystem operations even when they don't create
  597. a charactor or block special device.
  598. It's also been used to do things like ask devices what
  599. device special file should be used.
  600. Two pre-defined ioctls are used
  601. to disconnect and reconnect kernel drivers, so
  602. that user mode code can completely manage binding
  603. and configuration of devices.
  604. </para></listitem></varlistentry>
  605. <varlistentry><term>USBDEVFS_RELEASEINTERFACE</term>
  606. <listitem><para>This is used to release the claim usbfs
  607. made on interface, either implicitly or because of a
  608. USBDEVFS_CLAIMINTERFACE call, before the file
  609. descriptor is closed.
  610. The ioctl parameter is an integer holding the number of
  611. the interface (bInterfaceNumber from descriptor);
  612. File modification time is not updated by this request.
  613. </para><warning><para>
  614. <emphasis>No security check is made to ensure
  615. that the task which made the claim is the one
  616. which is releasing it.
  617. This means that user mode driver may interfere
  618. other ones. </emphasis>
  619. </para></warning></listitem></varlistentry>
  620. <varlistentry><term>USBDEVFS_RESETEP</term>
  621. <listitem><para>Resets the data toggle value for an endpoint
  622. (bulk or interrupt) to DATA0.
  623. The ioctl parameter is an integer endpoint number
  624. (1 to 15, as identified in the endpoint descriptor),
  625. with USB_DIR_IN added if the device's endpoint sends
  626. data to the host.
  627. </para><warning><para>
  628. <emphasis>Avoid using this request.
  629. It should probably be removed.</emphasis>
  630. Using it typically means the device and driver will lose
  631. toggle synchronization. If you really lost synchronization,
  632. you likely need to completely handshake with the device,
  633. using a request like CLEAR_HALT
  634. or SET_INTERFACE.
  635. </para></warning></listitem></varlistentry>
  636. </variablelist>
  637. </sect2>
  638. <sect2>
  639. <title>Synchronous I/O Support</title>
  640. <para>Synchronous requests involve the kernel blocking
  641. until until the user mode request completes, either by
  642. finishing successfully or by reporting an error.
  643. In most cases this is the simplest way to use usbfs,
  644. although as noted above it does prevent performing I/O
  645. to more than one endpoint at a time.
  646. </para>
  647. <variablelist>
  648. <varlistentry><term>USBDEVFS_BULK</term>
  649. <listitem><para>Issues a bulk read or write request to the
  650. device.
  651. The ioctl parameter is a pointer to this structure:
  652. <programlisting>struct usbdevfs_bulktransfer {
  653. unsigned int ep;
  654. unsigned int len;
  655. unsigned int timeout; /* in milliseconds */
  656. void *data;
  657. };</programlisting>
  658. </para><para>The "ep" value identifies a
  659. bulk endpoint number (1 to 15, as identified in an endpoint
  660. descriptor),
  661. masked with USB_DIR_IN when referring to an endpoint which
  662. sends data to the host from the device.
  663. The length of the data buffer is identified by "len";
  664. Recent kernels support requests up to about 128KBytes.
  665. <emphasis>FIXME say how read length is returned,
  666. and how short reads are handled.</emphasis>.
  667. </para></listitem></varlistentry>
  668. <varlistentry><term>USBDEVFS_CLEAR_HALT</term>
  669. <listitem><para>Clears endpoint halt (stall) and
  670. resets the endpoint toggle. This is only
  671. meaningful for bulk or interrupt endpoints.
  672. The ioctl parameter is an integer endpoint number
  673. (1 to 15, as identified in an endpoint descriptor),
  674. masked with USB_DIR_IN when referring to an endpoint which
  675. sends data to the host from the device.
  676. </para><para>
  677. Use this on bulk or interrupt endpoints which have
  678. stalled, returning <emphasis>-EPIPE</emphasis> status
  679. to a data transfer request.
  680. Do not issue the control request directly, since
  681. that could invalidate the host's record of the
  682. data toggle.
  683. </para></listitem></varlistentry>
  684. <varlistentry><term>USBDEVFS_CONTROL</term>
  685. <listitem><para>Issues a control request to the device.
  686. The ioctl parameter points to a structure like this:
  687. <programlisting>struct usbdevfs_ctrltransfer {
  688. __u8 bRequestType;
  689. __u8 bRequest;
  690. __u16 wValue;
  691. __u16 wIndex;
  692. __u16 wLength;
  693. __u32 timeout; /* in milliseconds */
  694. void *data;
  695. };</programlisting>
  696. </para><para>
  697. The first eight bytes of this structure are the contents
  698. of the SETUP packet to be sent to the device; see the
  699. USB 2.0 specification for details.
  700. The bRequestType value is composed by combining a
  701. USB_TYPE_* value, a USB_DIR_* value, and a
  702. USB_RECIP_* value (from
  703. <emphasis>&lt;linux/usb.h&gt;</emphasis>).
  704. If wLength is nonzero, it describes the length of the data
  705. buffer, which is either written to the device
  706. (USB_DIR_OUT) or read from the device (USB_DIR_IN).
  707. </para><para>
  708. At this writing, you can't transfer more than 4 KBytes
  709. of data to or from a device; usbfs has a limit, and
  710. some host controller drivers have a limit.
  711. (That's not usually a problem.)
  712. <emphasis>Also</emphasis> there's no way to say it's
  713. not OK to get a short read back from the device.
  714. </para></listitem></varlistentry>
  715. <varlistentry><term>USBDEVFS_RESET</term>
  716. <listitem><para>Does a USB level device reset.
  717. The ioctl parameter is ignored.
  718. After the reset, this rebinds all device interfaces.
  719. File modification time is not updated by this request.
  720. </para><warning><para>
  721. <emphasis>Avoid using this call</emphasis>
  722. until some usbcore bugs get fixed,
  723. since it does not fully synchronize device, interface,
  724. and driver (not just usbfs) state.
  725. </para></warning></listitem></varlistentry>
  726. <varlistentry><term>USBDEVFS_SETINTERFACE</term>
  727. <listitem><para>Sets the alternate setting for an
  728. interface. The ioctl parameter is a pointer to a
  729. structure like this:
  730. <programlisting>struct usbdevfs_setinterface {
  731. unsigned int interface;
  732. unsigned int altsetting;
  733. }; </programlisting>
  734. File modification time is not updated by this request.
  735. </para><para>
  736. Those struct members are from some interface descriptor
  737. applying to the current configuration.
  738. The interface number is the bInterfaceNumber value, and
  739. the altsetting number is the bAlternateSetting value.
  740. (This resets each endpoint in the interface.)
  741. </para></listitem></varlistentry>
  742. <varlistentry><term>USBDEVFS_SETCONFIGURATION</term>
  743. <listitem><para>Issues the
  744. <function>usb_set_configuration</function> call
  745. for the device.
  746. The parameter is an integer holding the number of
  747. a configuration (bConfigurationValue from descriptor).
  748. File modification time is not updated by this request.
  749. </para><warning><para>
  750. <emphasis>Avoid using this call</emphasis>
  751. until some usbcore bugs get fixed,
  752. since it does not fully synchronize device, interface,
  753. and driver (not just usbfs) state.
  754. </para></warning></listitem></varlistentry>
  755. </variablelist>
  756. </sect2>
  757. <sect2>
  758. <title>Asynchronous I/O Support</title>
  759. <para>As mentioned above, there are situations where it may be
  760. important to initiate concurrent operations from user mode code.
  761. This is particularly important for periodic transfers
  762. (interrupt and isochronous), but it can be used for other
  763. kinds of USB requests too.
  764. In such cases, the asynchronous requests described here
  765. are essential. Rather than submitting one request and having
  766. the kernel block until it completes, the blocking is separate.
  767. </para>
  768. <para>These requests are packaged into a structure that
  769. resembles the URB used by kernel device drivers.
  770. (No POSIX Async I/O support here, sorry.)
  771. It identifies the endpoint type (USBDEVFS_URB_TYPE_*),
  772. endpoint (number, masked with USB_DIR_IN as appropriate),
  773. buffer and length, and a user "context" value serving to
  774. uniquely identify each request.
  775. (It's usually a pointer to per-request data.)
  776. Flags can modify requests (not as many as supported for
  777. kernel drivers).
  778. </para>
  779. <para>Each request can specify a realtime signal number
  780. (between SIGRTMIN and SIGRTMAX, inclusive) to request a
  781. signal be sent when the request completes.
  782. </para>
  783. <para>When usbfs returns these urbs, the status value
  784. is updated, and the buffer may have been modified.
  785. Except for isochronous transfers, the actual_length is
  786. updated to say how many bytes were transferred; if the
  787. USBDEVFS_URB_DISABLE_SPD flag is set
  788. ("short packets are not OK"), if fewer bytes were read
  789. than were requested then you get an error report.
  790. </para>
  791. <programlisting>struct usbdevfs_iso_packet_desc {
  792. unsigned int length;
  793. unsigned int actual_length;
  794. unsigned int status;
  795. };
  796. struct usbdevfs_urb {
  797. unsigned char type;
  798. unsigned char endpoint;
  799. int status;
  800. unsigned int flags;
  801. void *buffer;
  802. int buffer_length;
  803. int actual_length;
  804. int start_frame;
  805. int number_of_packets;
  806. int error_count;
  807. unsigned int signr;
  808. void *usercontext;
  809. struct usbdevfs_iso_packet_desc iso_frame_desc[];
  810. };</programlisting>
  811. <para> For these asynchronous requests, the file modification
  812. time reflects when the request was initiated.
  813. This contrasts with their use with the synchronous requests,
  814. where it reflects when requests complete.
  815. </para>
  816. <variablelist>
  817. <varlistentry><term>USBDEVFS_DISCARDURB</term>
  818. <listitem><para>
  819. <emphasis>TBS</emphasis>
  820. File modification time is not updated by this request.
  821. </para><para>
  822. </para></listitem></varlistentry>
  823. <varlistentry><term>USBDEVFS_DISCSIGNAL</term>
  824. <listitem><para>
  825. <emphasis>TBS</emphasis>
  826. File modification time is not updated by this request.
  827. </para><para>
  828. </para></listitem></varlistentry>
  829. <varlistentry><term>USBDEVFS_REAPURB</term>
  830. <listitem><para>
  831. <emphasis>TBS</emphasis>
  832. File modification time is not updated by this request.
  833. </para><para>
  834. </para></listitem></varlistentry>
  835. <varlistentry><term>USBDEVFS_REAPURBNDELAY</term>
  836. <listitem><para>
  837. <emphasis>TBS</emphasis>
  838. File modification time is not updated by this request.
  839. </para><para>
  840. </para></listitem></varlistentry>
  841. <varlistentry><term>USBDEVFS_SUBMITURB</term>
  842. <listitem><para>
  843. <emphasis>TBS</emphasis>
  844. </para><para>
  845. </para></listitem></varlistentry>
  846. </variablelist>
  847. </sect2>
  848. </sect1>
  849. </chapter>
  850. </book>
  851. <!-- vim:syntax=sgml:sw=4
  852. -->