usb.tmpl 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 asynchronous 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 id="types"><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 id="hostside"><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 id="usbcore"><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 id="hcd"><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 id="usbfs">
  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.
  268. </para>
  269. <sect1 id="usbfs-files">
  270. <title>What files are in "usbfs"?</title>
  271. <para>Conventionally mounted at
  272. <filename>/proc/bus/usb</filename>, usbfs
  273. features include:
  274. <itemizedlist>
  275. <listitem><para><filename>/proc/bus/usb/devices</filename>
  276. ... a text file
  277. showing each of the USB devices on known to the kernel,
  278. and their configuration descriptors.
  279. You can also poll() this to learn about new devices.
  280. </para></listitem>
  281. <listitem><para><filename>/proc/bus/usb/BBB/DDD</filename>
  282. ... magic files
  283. exposing the each device's configuration descriptors, and
  284. supporting a series of ioctls for making device requests,
  285. including I/O to devices. (Purely for access by programs.)
  286. </para></listitem>
  287. </itemizedlist>
  288. </para>
  289. <para> Each bus is given a number (BBB) based on when it was
  290. enumerated; within each bus, each device is given a similar
  291. number (DDD).
  292. Those BBB/DDD paths are not "stable" identifiers;
  293. expect them to change even if you always leave the devices
  294. plugged in to the same hub port.
  295. <emphasis>Don't even think of saving these in application
  296. configuration files.</emphasis>
  297. Stable identifiers are available, for user mode applications
  298. that want to use them. HID and networking devices expose
  299. these stable IDs, so that for example you can be sure that
  300. you told the right UPS to power down its second server.
  301. "usbfs" doesn't (yet) expose those IDs.
  302. </para>
  303. </sect1>
  304. <sect1 id="usbfs-fstab">
  305. <title>Mounting and Access Control</title>
  306. <para>There are a number of mount options for usbfs, which will
  307. be of most interest to you if you need to override the default
  308. access control policy.
  309. That policy is that only root may read or write device files
  310. (<filename>/proc/bus/BBB/DDD</filename>) although anyone may read
  311. the <filename>devices</filename>
  312. or <filename>drivers</filename> files.
  313. I/O requests to the device also need the CAP_SYS_RAWIO capability,
  314. </para>
  315. <para>The significance of that is that by default, all user mode
  316. device drivers need super-user privileges.
  317. You can change modes or ownership in a driver setup
  318. when the device hotplugs, or maye just start the
  319. driver right then, as a privileged server (or some activity
  320. within one).
  321. That's the most secure approach for multi-user systems,
  322. but for single user systems ("trusted" by that user)
  323. it's more convenient just to grant everyone all access
  324. (using the <emphasis>devmode=0666</emphasis> option)
  325. so the driver can start whenever it's needed.
  326. </para>
  327. <para>The mount options for usbfs, usable in /etc/fstab or
  328. in command line invocations of <emphasis>mount</emphasis>, are:
  329. <variablelist>
  330. <varlistentry>
  331. <term><emphasis>busgid</emphasis>=NNNNN</term>
  332. <listitem><para>Controls the GID used for the
  333. /proc/bus/usb/BBB
  334. directories. (Default: 0)</para></listitem></varlistentry>
  335. <varlistentry><term><emphasis>busmode</emphasis>=MMM</term>
  336. <listitem><para>Controls the file mode used for the
  337. /proc/bus/usb/BBB
  338. directories. (Default: 0555)
  339. </para></listitem></varlistentry>
  340. <varlistentry><term><emphasis>busuid</emphasis>=NNNNN</term>
  341. <listitem><para>Controls the UID used for the
  342. /proc/bus/usb/BBB
  343. directories. (Default: 0)</para></listitem></varlistentry>
  344. <varlistentry><term><emphasis>devgid</emphasis>=NNNNN</term>
  345. <listitem><para>Controls the GID used for the
  346. /proc/bus/usb/BBB/DDD
  347. files. (Default: 0)</para></listitem></varlistentry>
  348. <varlistentry><term><emphasis>devmode</emphasis>=MMM</term>
  349. <listitem><para>Controls the file mode used for the
  350. /proc/bus/usb/BBB/DDD
  351. files. (Default: 0644)</para></listitem></varlistentry>
  352. <varlistentry><term><emphasis>devuid</emphasis>=NNNNN</term>
  353. <listitem><para>Controls the UID used for the
  354. /proc/bus/usb/BBB/DDD
  355. files. (Default: 0)</para></listitem></varlistentry>
  356. <varlistentry><term><emphasis>listgid</emphasis>=NNNNN</term>
  357. <listitem><para>Controls the GID used for the
  358. /proc/bus/usb/devices and drivers files.
  359. (Default: 0)</para></listitem></varlistentry>
  360. <varlistentry><term><emphasis>listmode</emphasis>=MMM</term>
  361. <listitem><para>Controls the file mode used for the
  362. /proc/bus/usb/devices and drivers files.
  363. (Default: 0444)</para></listitem></varlistentry>
  364. <varlistentry><term><emphasis>listuid</emphasis>=NNNNN</term>
  365. <listitem><para>Controls the UID used for the
  366. /proc/bus/usb/devices and drivers files.
  367. (Default: 0)</para></listitem></varlistentry>
  368. </variablelist>
  369. </para>
  370. <para>Note that many Linux distributions hard-wire the mount options
  371. for usbfs in their init scripts, such as
  372. <filename>/etc/rc.d/rc.sysinit</filename>,
  373. rather than making it easy to set this per-system
  374. policy in <filename>/etc/fstab</filename>.
  375. </para>
  376. </sect1>
  377. <sect1 id="usbfs-devices">
  378. <title>/proc/bus/usb/devices</title>
  379. <para>This file is handy for status viewing tools in user
  380. mode, which can scan the text format and ignore most of it.
  381. More detailed device status (including class and vendor
  382. status) is available from device-specific files.
  383. For information about the current format of this file,
  384. see the
  385. <filename>Documentation/usb/proc_usb_info.txt</filename>
  386. file in your Linux kernel sources.
  387. </para>
  388. <para>This file, in combination with the poll() system call, can
  389. also be used to detect when devices are added or removed:
  390. <programlisting>int fd;
  391. struct pollfd pfd;
  392. fd = open("/proc/bus/usb/devices", O_RDONLY);
  393. pfd = { fd, POLLIN, 0 };
  394. for (;;) {
  395. /* The first time through, this call will return immediately. */
  396. poll(&amp;pfd, 1, -1);
  397. /* To see what's changed, compare the file's previous and current
  398. contents or scan the filesystem. (Scanning is more precise.) */
  399. }</programlisting>
  400. Note that this behavior is intended to be used for informational
  401. and debug purposes. It would be more appropriate to use programs
  402. such as udev or HAL to initialize a device or start a user-mode
  403. helper program, for instance.
  404. </para>
  405. </sect1>
  406. <sect1 id="usbfs-bbbddd">
  407. <title>/proc/bus/usb/BBB/DDD</title>
  408. <para>Use these files in one of these basic ways:
  409. </para>
  410. <para><emphasis>They can be read,</emphasis>
  411. producing first the device descriptor
  412. (18 bytes) and then the descriptors for the current configuration.
  413. See the USB 2.0 spec for details about those binary data formats.
  414. You'll need to convert most multibyte values from little endian
  415. format to your native host byte order, although a few of the
  416. fields in the device descriptor (both of the BCD-encoded fields,
  417. and the vendor and product IDs) will be byteswapped for you.
  418. Note that configuration descriptors include descriptors for
  419. interfaces, altsettings, endpoints, and maybe additional
  420. class descriptors.
  421. </para>
  422. <para><emphasis>Perform USB operations</emphasis> using
  423. <emphasis>ioctl()</emphasis> requests to make endpoint I/O
  424. requests (synchronously or asynchronously) or manage
  425. the device.
  426. These requests need the CAP_SYS_RAWIO capability,
  427. as well as filesystem access permissions.
  428. Only one ioctl request can be made on one of these
  429. device files at a time.
  430. This means that if you are synchronously reading an endpoint
  431. from one thread, you won't be able to write to a different
  432. endpoint from another thread until the read completes.
  433. This works for <emphasis>half duplex</emphasis> protocols,
  434. but otherwise you'd use asynchronous i/o requests.
  435. </para>
  436. </sect1>
  437. <sect1 id="usbfs-lifecycle">
  438. <title>Life Cycle of User Mode Drivers</title>
  439. <para>Such a driver first needs to find a device file
  440. for a device it knows how to handle.
  441. Maybe it was told about it because a
  442. <filename>/sbin/hotplug</filename> event handling agent
  443. chose that driver to handle the new device.
  444. Or maybe it's an application that scans all the
  445. /proc/bus/usb device files, and ignores most devices.
  446. In either case, it should <function>read()</function> all
  447. the descriptors from the device file,
  448. and check them against what it knows how to handle.
  449. It might just reject everything except a particular
  450. vendor and product ID, or need a more complex policy.
  451. </para>
  452. <para>Never assume there will only be one such device
  453. on the system at a time!
  454. If your code can't handle more than one device at
  455. a time, at least detect when there's more than one, and
  456. have your users choose which device to use.
  457. </para>
  458. <para>Once your user mode driver knows what device to use,
  459. it interacts with it in either of two styles.
  460. The simple style is to make only control requests; some
  461. devices don't need more complex interactions than those.
  462. (An example might be software using vendor-specific control
  463. requests for some initialization or configuration tasks,
  464. with a kernel driver for the rest.)
  465. </para>
  466. <para>More likely, you need a more complex style driver:
  467. one using non-control endpoints, reading or writing data
  468. and claiming exclusive use of an interface.
  469. <emphasis>Bulk</emphasis> transfers are easiest to use,
  470. but only their sibling <emphasis>interrupt</emphasis> transfers
  471. work with low speed devices.
  472. Both interrupt and <emphasis>isochronous</emphasis> transfers
  473. offer service guarantees because their bandwidth is reserved.
  474. Such "periodic" transfers are awkward to use through usbfs,
  475. unless you're using the asynchronous calls. However, interrupt
  476. transfers can also be used in a synchronous "one shot" style.
  477. </para>
  478. <para>Your user-mode driver should never need to worry
  479. about cleaning up request state when the device is
  480. disconnected, although it should close its open file
  481. descriptors as soon as it starts seeing the ENODEV
  482. errors.
  483. </para>
  484. </sect1>
  485. <sect1 id="usbfs-ioctl"><title>The ioctl() Requests</title>
  486. <para>To use these ioctls, you need to include the following
  487. headers in your userspace program:
  488. <programlisting>#include &lt;linux/usb.h&gt;
  489. #include &lt;linux/usbdevice_fs.h&gt;
  490. #include &lt;asm/byteorder.h&gt;</programlisting>
  491. The standard USB device model requests, from "Chapter 9" of
  492. the USB 2.0 specification, are automatically included from
  493. the <filename>&lt;linux/usb/ch9.h&gt;</filename> header.
  494. </para>
  495. <para>Unless noted otherwise, the ioctl requests
  496. described here will
  497. update the modification time on the usbfs file to which
  498. they are applied (unless they fail).
  499. A return of zero indicates success; otherwise, a
  500. standard USB error code is returned. (These are
  501. documented in
  502. <filename>Documentation/usb/error-codes.txt</filename>
  503. in your kernel sources.)
  504. </para>
  505. <para>Each of these files multiplexes access to several
  506. I/O streams, one per endpoint.
  507. Each device has one control endpoint (endpoint zero)
  508. which supports a limited RPC style RPC access.
  509. Devices are configured
  510. by hub_wq (in the kernel) setting a device-wide
  511. <emphasis>configuration</emphasis> that affects things
  512. like power consumption and basic functionality.
  513. The endpoints are part of USB <emphasis>interfaces</emphasis>,
  514. which may have <emphasis>altsettings</emphasis>
  515. affecting things like which endpoints are available.
  516. Many devices only have a single configuration and interface,
  517. so drivers for them will ignore configurations and altsettings.
  518. </para>
  519. <sect2 id="usbfs-mgmt">
  520. <title>Management/Status Requests</title>
  521. <para>A number of usbfs requests don't deal very directly
  522. with device I/O.
  523. They mostly relate to device management and status.
  524. These are all synchronous requests.
  525. </para>
  526. <variablelist>
  527. <varlistentry><term>USBDEVFS_CLAIMINTERFACE</term>
  528. <listitem><para>This is used to force usbfs to
  529. claim a specific interface,
  530. which has not previously been claimed by usbfs or any other
  531. kernel driver.
  532. The ioctl parameter is an integer holding the number of
  533. the interface (bInterfaceNumber from descriptor).
  534. </para><para>
  535. Note that if your driver doesn't claim an interface
  536. before trying to use one of its endpoints, and no
  537. other driver has bound to it, then the interface is
  538. automatically claimed by usbfs.
  539. </para><para>
  540. This claim will be released by a RELEASEINTERFACE ioctl,
  541. or by closing the file descriptor.
  542. File modification time is not updated by this request.
  543. </para></listitem></varlistentry>
  544. <varlistentry><term>USBDEVFS_CONNECTINFO</term>
  545. <listitem><para>Says whether the device is lowspeed.
  546. The ioctl parameter points to a structure like this:
  547. <programlisting>struct usbdevfs_connectinfo {
  548. unsigned int devnum;
  549. unsigned char slow;
  550. }; </programlisting>
  551. File modification time is not updated by this request.
  552. </para><para>
  553. <emphasis>You can't tell whether a "not slow"
  554. device is connected at high speed (480 MBit/sec)
  555. or just full speed (12 MBit/sec).</emphasis>
  556. You should know the devnum value already,
  557. it's the DDD value of the device file name.
  558. </para></listitem></varlistentry>
  559. <varlistentry><term>USBDEVFS_GETDRIVER</term>
  560. <listitem><para>Returns the name of the kernel driver
  561. bound to a given interface (a string). Parameter
  562. is a pointer to this structure, which is modified:
  563. <programlisting>struct usbdevfs_getdriver {
  564. unsigned int interface;
  565. char driver[USBDEVFS_MAXDRIVERNAME + 1];
  566. };</programlisting>
  567. File modification time is not updated by this request.
  568. </para></listitem></varlistentry>
  569. <varlistentry><term>USBDEVFS_IOCTL</term>
  570. <listitem><para>Passes a request from userspace through
  571. to a kernel driver that has an ioctl entry in the
  572. <emphasis>struct usb_driver</emphasis> it registered.
  573. <programlisting>struct usbdevfs_ioctl {
  574. int ifno;
  575. int ioctl_code;
  576. void *data;
  577. };
  578. /* user mode call looks like this.
  579. * 'request' becomes the driver->ioctl() 'code' parameter.
  580. * the size of 'param' is encoded in 'request', and that data
  581. * is copied to or from the driver->ioctl() 'buf' parameter.
  582. */
  583. static int
  584. usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
  585. {
  586. struct usbdevfs_ioctl wrapper;
  587. wrapper.ifno = ifno;
  588. wrapper.ioctl_code = request;
  589. wrapper.data = param;
  590. return ioctl (fd, USBDEVFS_IOCTL, &amp;wrapper);
  591. } </programlisting>
  592. File modification time is not updated by this request.
  593. </para><para>
  594. This request lets kernel drivers talk to user mode code
  595. through filesystem operations even when they don't create
  596. a character or block special device.
  597. It's also been used to do things like ask devices what
  598. device special file should be used.
  599. Two pre-defined ioctls are used
  600. to disconnect and reconnect kernel drivers, so
  601. that user mode code can completely manage binding
  602. and configuration of devices.
  603. </para></listitem></varlistentry>
  604. <varlistentry><term>USBDEVFS_RELEASEINTERFACE</term>
  605. <listitem><para>This is used to release the claim usbfs
  606. made on interface, either implicitly or because of a
  607. USBDEVFS_CLAIMINTERFACE call, before the file
  608. descriptor is closed.
  609. The ioctl parameter is an integer holding the number of
  610. the interface (bInterfaceNumber from descriptor);
  611. File modification time is not updated by this request.
  612. </para><warning><para>
  613. <emphasis>No security check is made to ensure
  614. that the task which made the claim is the one
  615. which is releasing it.
  616. This means that user mode driver may interfere
  617. other ones. </emphasis>
  618. </para></warning></listitem></varlistentry>
  619. <varlistentry><term>USBDEVFS_RESETEP</term>
  620. <listitem><para>Resets the data toggle value for an endpoint
  621. (bulk or interrupt) to DATA0.
  622. The ioctl parameter is an integer endpoint number
  623. (1 to 15, as identified in the endpoint descriptor),
  624. with USB_DIR_IN added if the device's endpoint sends
  625. data to the host.
  626. </para><warning><para>
  627. <emphasis>Avoid using this request.
  628. It should probably be removed.</emphasis>
  629. Using it typically means the device and driver will lose
  630. toggle synchronization. If you really lost synchronization,
  631. you likely need to completely handshake with the device,
  632. using a request like CLEAR_HALT
  633. or SET_INTERFACE.
  634. </para></warning></listitem></varlistentry>
  635. </variablelist>
  636. </sect2>
  637. <sect2 id="usbfs-sync">
  638. <title>Synchronous I/O Support</title>
  639. <para>Synchronous requests involve the kernel blocking
  640. until the user mode request completes, either by
  641. finishing successfully or by reporting an error.
  642. In most cases this is the simplest way to use usbfs,
  643. although as noted above it does prevent performing I/O
  644. to more than one endpoint at a time.
  645. </para>
  646. <variablelist>
  647. <varlistentry><term>USBDEVFS_BULK</term>
  648. <listitem><para>Issues a bulk read or write request to the
  649. device.
  650. The ioctl parameter is a pointer to this structure:
  651. <programlisting>struct usbdevfs_bulktransfer {
  652. unsigned int ep;
  653. unsigned int len;
  654. unsigned int timeout; /* in milliseconds */
  655. void *data;
  656. };</programlisting>
  657. </para><para>The "ep" value identifies a
  658. bulk endpoint number (1 to 15, as identified in an endpoint
  659. descriptor),
  660. masked with USB_DIR_IN when referring to an endpoint which
  661. sends data to the host from the device.
  662. The length of the data buffer is identified by "len";
  663. Recent kernels support requests up to about 128KBytes.
  664. <emphasis>FIXME say how read length is returned,
  665. and how short reads are handled.</emphasis>.
  666. </para></listitem></varlistentry>
  667. <varlistentry><term>USBDEVFS_CLEAR_HALT</term>
  668. <listitem><para>Clears endpoint halt (stall) and
  669. resets the endpoint toggle. This is only
  670. meaningful for bulk or interrupt endpoints.
  671. The ioctl parameter is an integer endpoint number
  672. (1 to 15, as identified in an endpoint descriptor),
  673. masked with USB_DIR_IN when referring to an endpoint which
  674. sends data to the host from the device.
  675. </para><para>
  676. Use this on bulk or interrupt endpoints which have
  677. stalled, returning <emphasis>-EPIPE</emphasis> status
  678. to a data transfer request.
  679. Do not issue the control request directly, since
  680. that could invalidate the host's record of the
  681. data toggle.
  682. </para></listitem></varlistentry>
  683. <varlistentry><term>USBDEVFS_CONTROL</term>
  684. <listitem><para>Issues a control request to the device.
  685. The ioctl parameter points to a structure like this:
  686. <programlisting>struct usbdevfs_ctrltransfer {
  687. __u8 bRequestType;
  688. __u8 bRequest;
  689. __u16 wValue;
  690. __u16 wIndex;
  691. __u16 wLength;
  692. __u32 timeout; /* in milliseconds */
  693. void *data;
  694. };</programlisting>
  695. </para><para>
  696. The first eight bytes of this structure are the contents
  697. of the SETUP packet to be sent to the device; see the
  698. USB 2.0 specification for details.
  699. The bRequestType value is composed by combining a
  700. USB_TYPE_* value, a USB_DIR_* value, and a
  701. USB_RECIP_* value (from
  702. <emphasis>&lt;linux/usb.h&gt;</emphasis>).
  703. If wLength is nonzero, it describes the length of the data
  704. buffer, which is either written to the device
  705. (USB_DIR_OUT) or read from the device (USB_DIR_IN).
  706. </para><para>
  707. At this writing, you can't transfer more than 4 KBytes
  708. of data to or from a device; usbfs has a limit, and
  709. some host controller drivers have a limit.
  710. (That's not usually a problem.)
  711. <emphasis>Also</emphasis> there's no way to say it's
  712. not OK to get a short read back from the device.
  713. </para></listitem></varlistentry>
  714. <varlistentry><term>USBDEVFS_RESET</term>
  715. <listitem><para>Does a USB level device reset.
  716. The ioctl parameter is ignored.
  717. After the reset, this rebinds all device interfaces.
  718. File modification time is not updated by this request.
  719. </para><warning><para>
  720. <emphasis>Avoid using this call</emphasis>
  721. until some usbcore bugs get fixed,
  722. since it does not fully synchronize device, interface,
  723. and driver (not just usbfs) state.
  724. </para></warning></listitem></varlistentry>
  725. <varlistentry><term>USBDEVFS_SETINTERFACE</term>
  726. <listitem><para>Sets the alternate setting for an
  727. interface. The ioctl parameter is a pointer to a
  728. structure like this:
  729. <programlisting>struct usbdevfs_setinterface {
  730. unsigned int interface;
  731. unsigned int altsetting;
  732. }; </programlisting>
  733. File modification time is not updated by this request.
  734. </para><para>
  735. Those struct members are from some interface descriptor
  736. applying to the current configuration.
  737. The interface number is the bInterfaceNumber value, and
  738. the altsetting number is the bAlternateSetting value.
  739. (This resets each endpoint in the interface.)
  740. </para></listitem></varlistentry>
  741. <varlistentry><term>USBDEVFS_SETCONFIGURATION</term>
  742. <listitem><para>Issues the
  743. <function>usb_set_configuration</function> call
  744. for the device.
  745. The parameter is an integer holding the number of
  746. a configuration (bConfigurationValue from descriptor).
  747. File modification time is not updated by this request.
  748. </para><warning><para>
  749. <emphasis>Avoid using this call</emphasis>
  750. until some usbcore bugs get fixed,
  751. since it does not fully synchronize device, interface,
  752. and driver (not just usbfs) state.
  753. </para></warning></listitem></varlistentry>
  754. </variablelist>
  755. </sect2>
  756. <sect2 id="usbfs-async">
  757. <title>Asynchronous I/O Support</title>
  758. <para>As mentioned above, there are situations where it may be
  759. important to initiate concurrent operations from user mode code.
  760. This is particularly important for periodic transfers
  761. (interrupt and isochronous), but it can be used for other
  762. kinds of USB requests too.
  763. In such cases, the asynchronous requests described here
  764. are essential. Rather than submitting one request and having
  765. the kernel block until it completes, the blocking is separate.
  766. </para>
  767. <para>These requests are packaged into a structure that
  768. resembles the URB used by kernel device drivers.
  769. (No POSIX Async I/O support here, sorry.)
  770. It identifies the endpoint type (USBDEVFS_URB_TYPE_*),
  771. endpoint (number, masked with USB_DIR_IN as appropriate),
  772. buffer and length, and a user "context" value serving to
  773. uniquely identify each request.
  774. (It's usually a pointer to per-request data.)
  775. Flags can modify requests (not as many as supported for
  776. kernel drivers).
  777. </para>
  778. <para>Each request can specify a realtime signal number
  779. (between SIGRTMIN and SIGRTMAX, inclusive) to request a
  780. signal be sent when the request completes.
  781. </para>
  782. <para>When usbfs returns these urbs, the status value
  783. is updated, and the buffer may have been modified.
  784. Except for isochronous transfers, the actual_length is
  785. updated to say how many bytes were transferred; if the
  786. USBDEVFS_URB_DISABLE_SPD flag is set
  787. ("short packets are not OK"), if fewer bytes were read
  788. than were requested then you get an error report.
  789. </para>
  790. <programlisting>struct usbdevfs_iso_packet_desc {
  791. unsigned int length;
  792. unsigned int actual_length;
  793. unsigned int status;
  794. };
  795. struct usbdevfs_urb {
  796. unsigned char type;
  797. unsigned char endpoint;
  798. int status;
  799. unsigned int flags;
  800. void *buffer;
  801. int buffer_length;
  802. int actual_length;
  803. int start_frame;
  804. int number_of_packets;
  805. int error_count;
  806. unsigned int signr;
  807. void *usercontext;
  808. struct usbdevfs_iso_packet_desc iso_frame_desc[];
  809. };</programlisting>
  810. <para> For these asynchronous requests, the file modification
  811. time reflects when the request was initiated.
  812. This contrasts with their use with the synchronous requests,
  813. where it reflects when requests complete.
  814. </para>
  815. <variablelist>
  816. <varlistentry><term>USBDEVFS_DISCARDURB</term>
  817. <listitem><para>
  818. <emphasis>TBS</emphasis>
  819. File modification time is not updated by this request.
  820. </para><para>
  821. </para></listitem></varlistentry>
  822. <varlistentry><term>USBDEVFS_DISCSIGNAL</term>
  823. <listitem><para>
  824. <emphasis>TBS</emphasis>
  825. File modification time is not updated by this request.
  826. </para><para>
  827. </para></listitem></varlistentry>
  828. <varlistentry><term>USBDEVFS_REAPURB</term>
  829. <listitem><para>
  830. <emphasis>TBS</emphasis>
  831. File modification time is not updated by this request.
  832. </para><para>
  833. </para></listitem></varlistentry>
  834. <varlistentry><term>USBDEVFS_REAPURBNDELAY</term>
  835. <listitem><para>
  836. <emphasis>TBS</emphasis>
  837. File modification time is not updated by this request.
  838. </para><para>
  839. </para></listitem></varlistentry>
  840. <varlistentry><term>USBDEVFS_SUBMITURB</term>
  841. <listitem><para>
  842. <emphasis>TBS</emphasis>
  843. </para><para>
  844. </para></listitem></varlistentry>
  845. </variablelist>
  846. </sect2>
  847. </sect1>
  848. </chapter>
  849. </book>
  850. <!-- vim:syntax=sgml:sw=4
  851. -->