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 at the
  39. root (the system's master), hubs as interior nodes, and
  40. peripheral devices 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 part for
  47. ease of use. It is not physically possible to assemble
  48. (legal) USB cables incorrectly: all upstream "to-the-host"
  49. connectors are the rectangular type, matching the sockets on
  50. root hubs, and the downstream type are the squarish type
  51. (or they are built in to the peripheral).
  52. Software doesn't need to deal with distributed autoconfiguration
  53. since the pre-designated master node manages all that.
  54. At the electrical level, bus protocol overhead is reduced by
  55. eliminating arbitration and moving scheduling into 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, including high speed
  61. transfers and transaction translating hubs (used for USB 1.1
  62. and 1.0 backward compatibility).
  63. </para>
  64. <para>USB support was added to Linux early in the 2.2 kernel series
  65. shortly before the 2.3 development forked off. Updates
  66. from 2.3 were regularly folded back into 2.2 releases, bringing
  67. new features such as <filename>/sbin/hotplug</filename> support,
  68. more drivers, and more robustness.
  69. The 2.5 kernel series continued such improvements, and also
  70. worked on USB 2.0 support,
  71. higher performance,
  72. better consistency between host controller drivers,
  73. API simplification (to make bugs less likely),
  74. and providing internal "kerneldoc" documentation.
  75. </para>
  76. <para>Linux can run inside USB devices as well as on
  77. the hosts that control the devices.
  78. Because the Linux 2.x USB support evolved to support mass market
  79. platforms such as Apple Macintosh or PC-compatible systems,
  80. it didn't address design concerns for those types of USB systems.
  81. So it can't be used inside mass-market PDAs, or other peripherals.
  82. USB device drivers running inside those Linux peripherals
  83. don't do the same things as the ones running inside hosts,
  84. and so they've been given a different name:
  85. they're called <emphasis>gadget drivers</emphasis>.
  86. This document does not present gadget drivers.
  87. </para>
  88. </chapter>
  89. <chapter id="host">
  90. <title>USB Host-Side API Model</title>
  91. <para>Within the kernel,
  92. host-side drivers for USB devices talk to the "usbcore" APIs.
  93. There are two types of public "usbcore" APIs, targetted at two different
  94. layers of USB driver. Those are
  95. <emphasis>general purpose</emphasis> drivers, exposed through
  96. driver frameworks such as block, character, or network devices;
  97. and drivers that are <emphasis>part of the core</emphasis>,
  98. which are involved in managing a USB bus.
  99. Such core drivers include the <emphasis>hub</emphasis> driver,
  100. which manages trees of USB devices, and several different kinds
  101. of <emphasis>host controller driver (HCD)</emphasis>,
  102. which control individual busses.
  103. </para>
  104. <para>The device model seen by USB drivers is relatively complex.
  105. </para>
  106. <itemizedlist>
  107. <listitem><para>USB supports four kinds of data transfer
  108. (control, bulk, interrupt, and isochronous). Two transfer
  109. types use bandwidth as it's available (control and bulk),
  110. while the other two types of transfer (interrupt and isochronous)
  111. are scheduled to provide guaranteed bandwidth.
  112. </para></listitem>
  113. <listitem><para>The device description model includes one or more
  114. "configurations" per device, only one of which is active at a time.
  115. Devices that are capable of high speed operation must also support
  116. full speed configurations, along with a way to ask about the
  117. "other speed" configurations that might be used.
  118. </para></listitem>
  119. <listitem><para>Configurations have one or more "interface", each
  120. of which may have "alternate settings". Interfaces may be
  121. standardized by USB "Class" specifications, or may be specific to
  122. a vendor or device.</para>
  123. <para>USB device drivers actually bind to interfaces, not devices.
  124. Think of them as "interface drivers", though you
  125. may not see many devices where the distinction is important.
  126. <emphasis>Most USB devices are simple, with only one configuration,
  127. one interface, and one alternate setting.</emphasis>
  128. </para></listitem>
  129. <listitem><para>Interfaces have one or more "endpoints", each of
  130. which supports one type and direction of data transfer such as
  131. "bulk out" or "interrupt in". The entire configuration may have
  132. up to sixteen endpoints in each direction, allocated as needed
  133. among all the interfaces.
  134. </para></listitem>
  135. <listitem><para>Data transfer on USB is packetized; each endpoint
  136. has a maximum packet size.
  137. Drivers must often be aware of conventions such as flagging the end
  138. of bulk transfers using "short" (including zero length) packets.
  139. </para></listitem>
  140. <listitem><para>The Linux USB API supports synchronous calls for
  141. control and bulk messaging.
  142. It also supports asynchnous calls for all kinds of data transfer,
  143. using request structures called "URBs" (USB Request Blocks).
  144. </para></listitem>
  145. </itemizedlist>
  146. <para>Accordingly, the USB Core API exposed to device drivers
  147. covers quite a lot of territory. You'll probably need to consult
  148. the USB 2.0 specification, available online from www.usb.org at
  149. no cost, as well as class or device specifications.
  150. </para>
  151. <para>The only host-side drivers that actually touch hardware
  152. (reading/writing registers, handling IRQs, and so on) are the HCDs.
  153. In theory, all HCDs provide the same functionality through the same
  154. API. In practice, that's becoming more true on the 2.5 kernels,
  155. but there are still differences that crop up especially with
  156. fault handling. Different controllers don't necessarily report
  157. the same aspects of failures, and recovery from faults (including
  158. software-induced ones like unlinking an URB) isn't yet fully
  159. consistent.
  160. Device driver authors should make a point of doing disconnect
  161. testing (while the device is active) with each different host
  162. controller driver, to make sure drivers don't have bugs of
  163. their own as well as to make sure they aren't relying on some
  164. HCD-specific behavior.
  165. (You will need external USB 1.1 and/or
  166. USB 2.0 hubs to perform all those tests.)
  167. </para>
  168. </chapter>
  169. <chapter><title>USB-Standard Types</title>
  170. <para>In <filename>&lt;linux/usb_ch9.h&gt;</filename> you will find
  171. the USB data types defined in chapter 9 of the USB specification.
  172. These data types are used throughout USB, and in APIs including
  173. this host side API, gadget APIs, and usbfs.
  174. </para>
  175. !Iinclude/linux/usb_ch9.h
  176. </chapter>
  177. <chapter><title>Host-Side Data Types and Macros</title>
  178. <para>The host side API exposes several layers to drivers, some of
  179. which are more necessary than others.
  180. These support lifecycle models for host side drivers
  181. and devices, and support passing buffers through usbcore to
  182. some HCD that performs the I/O for the device driver.
  183. </para>
  184. !Iinclude/linux/usb.h
  185. </chapter>
  186. <chapter><title>USB Core APIs</title>
  187. <para>There are two basic I/O models in the USB API.
  188. The most elemental one is asynchronous: drivers submit requests
  189. in the form of an URB, and the URB's completion callback
  190. handle the next step.
  191. All USB transfer types support that model, although there
  192. are special cases for control URBs (which always have setup
  193. and status stages, but may not have a data stage) and
  194. isochronous URBs (which allow large packets and include
  195. per-packet fault reports).
  196. Built on top of that is synchronous API support, where a
  197. driver calls a routine that allocates one or more URBs,
  198. submits them, and waits until they complete.
  199. There are synchronous wrappers for single-buffer control
  200. and bulk transfers (which are awkward to use in some
  201. driver disconnect scenarios), and for scatterlist based
  202. streaming i/o (bulk or interrupt).
  203. </para>
  204. <para>USB drivers need to provide buffers that can be
  205. used for DMA, although they don't necessarily need to
  206. provide the DMA mapping themselves.
  207. There are APIs to use used when allocating DMA buffers,
  208. which can prevent use of bounce buffers on some systems.
  209. In some cases, drivers may be able to rely on 64bit DMA
  210. to eliminate another kind of bounce buffer.
  211. </para>
  212. !Edrivers/usb/core/urb.c
  213. !Edrivers/usb/core/message.c
  214. !Edrivers/usb/core/file.c
  215. !Edrivers/usb/core/driver.c
  216. !Edrivers/usb/core/usb.c
  217. !Edrivers/usb/core/hub.c
  218. </chapter>
  219. <chapter><title>Host Controller APIs</title>
  220. <para>These APIs are only for use by host controller drivers,
  221. most of which implement standard register interfaces such as
  222. EHCI, OHCI, or UHCI.
  223. UHCI was one of the first interfaces, designed by Intel and
  224. also used by VIA; it doesn't do much in hardware.
  225. OHCI was designed later, to have the hardware do more work
  226. (bigger transfers, tracking protocol state, and so on).
  227. EHCI was designed with USB 2.0; its design has features that
  228. resemble OHCI (hardware does much more work) as well as
  229. UHCI (some parts of ISO support, TD list processing).
  230. </para>
  231. <para>There are host controllers other than the "big three",
  232. although most PCI based controllers (and a few non-PCI based
  233. ones) use one of those interfaces.
  234. Not all host controllers use DMA; some use PIO, and there
  235. is also a simulator.
  236. </para>
  237. <para>The same basic APIs are available to drivers for all
  238. those controllers.
  239. For historical reasons they are in two layers:
  240. <structname>struct usb_bus</structname> is a rather thin
  241. layer that became available in the 2.2 kernels, while
  242. <structname>struct usb_hcd</structname> is a more featureful
  243. layer (available in later 2.4 kernels and in 2.5) that
  244. lets HCDs share common code, to shrink driver size
  245. and significantly reduce hcd-specific behaviors.
  246. </para>
  247. !Edrivers/usb/core/hcd.c
  248. !Edrivers/usb/core/hcd-pci.c
  249. !Idrivers/usb/core/buffer.c
  250. </chapter>
  251. <chapter>
  252. <title>The USB Filesystem (usbfs)</title>
  253. <para>This chapter presents the Linux <emphasis>usbfs</emphasis>.
  254. You may prefer to avoid writing new kernel code for your
  255. USB driver; that's the problem that usbfs set out to solve.
  256. User mode device drivers are usually packaged as applications
  257. or libraries, and may use usbfs through some programming library
  258. that wraps it. Such libraries include
  259. <ulink url="http://libusb.sourceforge.net">libusb</ulink>
  260. for C/C++, and
  261. <ulink url="http://jUSB.sourceforge.net">jUSB</ulink> for Java.
  262. </para>
  263. <note><title>Unfinished</title>
  264. <para>This particular documentation is incomplete,
  265. especially with respect to the asynchronous mode.
  266. As of kernel 2.5.66 the code and this (new) documentation
  267. need to be cross-reviewed.
  268. </para>
  269. </note>
  270. <para>Configure usbfs into Linux kernels by enabling the
  271. <emphasis>USB filesystem</emphasis> option (CONFIG_USB_DEVICEFS),
  272. and you get basic support for user mode USB device drivers.
  273. Until relatively recently it was often (confusingly) called
  274. <emphasis>usbdevfs</emphasis> although it wasn't solving what
  275. <emphasis>devfs</emphasis> was.
  276. Every USB device will appear in usbfs, regardless of whether or
  277. not it has a kernel driver; but only devices with kernel drivers
  278. show up in devfs.
  279. </para>
  280. <sect1>
  281. <title>What files are in "usbfs"?</title>
  282. <para>Conventionally mounted at
  283. <filename>/proc/bus/usb</filename>, usbfs
  284. features include:
  285. <itemizedlist>
  286. <listitem><para><filename>/proc/bus/usb/devices</filename>
  287. ... a text file
  288. showing each of the USB devices on known to the kernel,
  289. and their configuration descriptors.
  290. You can also poll() this to learn about new devices.
  291. </para></listitem>
  292. <listitem><para><filename>/proc/bus/usb/BBB/DDD</filename>
  293. ... magic files
  294. exposing the each device's configuration descriptors, and
  295. supporting a series of ioctls for making device requests,
  296. including I/O to devices. (Purely for access by programs.)
  297. </para></listitem>
  298. </itemizedlist>
  299. </para>
  300. <para> Each bus is given a number (BBB) based on when it was
  301. enumerated; within each bus, each device is given a similar
  302. number (DDD).
  303. Those BBB/DDD paths are not "stable" identifiers;
  304. expect them to change even if you always leave the devices
  305. plugged in to the same hub port.
  306. <emphasis>Don't even think of saving these in application
  307. configuration files.</emphasis>
  308. Stable identifiers are available, for user mode applications
  309. that want to use them. HID and networking devices expose
  310. these stable IDs, so that for example you can be sure that
  311. you told the right UPS to power down its second server.
  312. "usbfs" doesn't (yet) expose those IDs.
  313. </para>
  314. </sect1>
  315. <sect1>
  316. <title>Mounting and Access Control</title>
  317. <para>There are a number of mount options for usbfs, which will
  318. be of most interest to you if you need to override the default
  319. access control policy.
  320. That policy is that only root may read or write device files
  321. (<filename>/proc/bus/BBB/DDD</filename>) although anyone may read
  322. the <filename>devices</filename>
  323. or <filename>drivers</filename> files.
  324. I/O requests to the device also need the CAP_SYS_RAWIO capability,
  325. </para>
  326. <para>The significance of that is that by default, all user mode
  327. device drivers need super-user privileges.
  328. You can change modes or ownership in a driver setup
  329. when the device hotplugs, or maye just start the
  330. driver right then, as a privileged server (or some activity
  331. within one).
  332. That's the most secure approach for multi-user systems,
  333. but for single user systems ("trusted" by that user)
  334. it's more convenient just to grant everyone all access
  335. (using the <emphasis>devmode=0666</emphasis> option)
  336. so the driver can start whenever it's needed.
  337. </para>
  338. <para>The mount options for usbfs, usable in /etc/fstab or
  339. in command line invocations of <emphasis>mount</emphasis>, are:
  340. <variablelist>
  341. <varlistentry>
  342. <term><emphasis>busgid</emphasis>=NNNNN</term>
  343. <listitem><para>Controls the GID used for the
  344. /proc/bus/usb/BBB
  345. directories. (Default: 0)</para></listitem></varlistentry>
  346. <varlistentry><term><emphasis>busmode</emphasis>=MMM</term>
  347. <listitem><para>Controls the file mode used for the
  348. /proc/bus/usb/BBB
  349. directories. (Default: 0555)
  350. </para></listitem></varlistentry>
  351. <varlistentry><term><emphasis>busuid</emphasis>=NNNNN</term>
  352. <listitem><para>Controls the UID used for the
  353. /proc/bus/usb/BBB
  354. directories. (Default: 0)</para></listitem></varlistentry>
  355. <varlistentry><term><emphasis>devgid</emphasis>=NNNNN</term>
  356. <listitem><para>Controls the GID used for the
  357. /proc/bus/usb/BBB/DDD
  358. files. (Default: 0)</para></listitem></varlistentry>
  359. <varlistentry><term><emphasis>devmode</emphasis>=MMM</term>
  360. <listitem><para>Controls the file mode used for the
  361. /proc/bus/usb/BBB/DDD
  362. files. (Default: 0644)</para></listitem></varlistentry>
  363. <varlistentry><term><emphasis>devuid</emphasis>=NNNNN</term>
  364. <listitem><para>Controls the UID used for the
  365. /proc/bus/usb/BBB/DDD
  366. files. (Default: 0)</para></listitem></varlistentry>
  367. <varlistentry><term><emphasis>listgid</emphasis>=NNNNN</term>
  368. <listitem><para>Controls the GID used for the
  369. /proc/bus/usb/devices and drivers files.
  370. (Default: 0)</para></listitem></varlistentry>
  371. <varlistentry><term><emphasis>listmode</emphasis>=MMM</term>
  372. <listitem><para>Controls the file mode used for the
  373. /proc/bus/usb/devices and drivers files.
  374. (Default: 0444)</para></listitem></varlistentry>
  375. <varlistentry><term><emphasis>listuid</emphasis>=NNNNN</term>
  376. <listitem><para>Controls the UID used for the
  377. /proc/bus/usb/devices and drivers files.
  378. (Default: 0)</para></listitem></varlistentry>
  379. </variablelist>
  380. </para>
  381. <para>Note that many Linux distributions hard-wire the mount options
  382. for usbfs in their init scripts, such as
  383. <filename>/etc/rc.d/rc.sysinit</filename>,
  384. rather than making it easy to set this per-system
  385. policy in <filename>/etc/fstab</filename>.
  386. </para>
  387. </sect1>
  388. <sect1>
  389. <title>/proc/bus/usb/devices</title>
  390. <para>This file is handy for status viewing tools in user
  391. mode, which can scan the text format and ignore most of it.
  392. More detailed device status (including class and vendor
  393. status) is available from device-specific files.
  394. For information about the current format of this file,
  395. see the
  396. <filename>Documentation/usb/proc_usb_info.txt</filename>
  397. file in your Linux kernel sources.
  398. </para>
  399. <para>Otherwise the main use for this file from programs
  400. is to poll() it to get notifications of usb devices
  401. as they're plugged or unplugged.
  402. To see what changed, you'd need to read the file and
  403. compare "before" and "after" contents, scan the filesystem,
  404. or see its hotplug event.
  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. -->