usb.tmpl 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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/usb.c
  216. !Edrivers/usb/core/hub.c
  217. </chapter>
  218. <chapter><title>Host Controller APIs</title>
  219. <para>These APIs are only for use by host controller drivers,
  220. most of which implement standard register interfaces such as
  221. EHCI, OHCI, or UHCI.
  222. UHCI was one of the first interfaces, designed by Intel and
  223. also used by VIA; it doesn't do much in hardware.
  224. OHCI was designed later, to have the hardware do more work
  225. (bigger transfers, tracking protocol state, and so on).
  226. EHCI was designed with USB 2.0; its design has features that
  227. resemble OHCI (hardware does much more work) as well as
  228. UHCI (some parts of ISO support, TD list processing).
  229. </para>
  230. <para>There are host controllers other than the "big three",
  231. although most PCI based controllers (and a few non-PCI based
  232. ones) use one of those interfaces.
  233. Not all host controllers use DMA; some use PIO, and there
  234. is also a simulator.
  235. </para>
  236. <para>The same basic APIs are available to drivers for all
  237. those controllers.
  238. For historical reasons they are in two layers:
  239. <structname>struct usb_bus</structname> is a rather thin
  240. layer that became available in the 2.2 kernels, while
  241. <structname>struct usb_hcd</structname> is a more featureful
  242. layer (available in later 2.4 kernels and in 2.5) that
  243. lets HCDs share common code, to shrink driver size
  244. and significantly reduce hcd-specific behaviors.
  245. </para>
  246. !Edrivers/usb/core/hcd.c
  247. !Edrivers/usb/core/hcd-pci.c
  248. !Edrivers/usb/core/buffer.c
  249. </chapter>
  250. <chapter>
  251. <title>The USB Filesystem (usbfs)</title>
  252. <para>This chapter presents the Linux <emphasis>usbfs</emphasis>.
  253. You may prefer to avoid writing new kernel code for your
  254. USB driver; that's the problem that usbfs set out to solve.
  255. User mode device drivers are usually packaged as applications
  256. or libraries, and may use usbfs through some programming library
  257. that wraps it. Such libraries include
  258. <ulink url="http://libusb.sourceforge.net">libusb</ulink>
  259. for C/C++, and
  260. <ulink url="http://jUSB.sourceforge.net">jUSB</ulink> for Java.
  261. </para>
  262. <note><title>Unfinished</title>
  263. <para>This particular documentation is incomplete,
  264. especially with respect to the asynchronous mode.
  265. As of kernel 2.5.66 the code and this (new) documentation
  266. need to be cross-reviewed.
  267. </para>
  268. </note>
  269. <para>Configure usbfs into Linux kernels by enabling the
  270. <emphasis>USB filesystem</emphasis> option (CONFIG_USB_DEVICEFS),
  271. and you get basic support for user mode USB device drivers.
  272. Until relatively recently it was often (confusingly) called
  273. <emphasis>usbdevfs</emphasis> although it wasn't solving what
  274. <emphasis>devfs</emphasis> was.
  275. Every USB device will appear in usbfs, regardless of whether or
  276. not it has a kernel driver; but only devices with kernel drivers
  277. show up in devfs.
  278. </para>
  279. <sect1>
  280. <title>What files are in "usbfs"?</title>
  281. <para>Conventionally mounted at
  282. <filename>/proc/bus/usb</filename>, usbfs
  283. features include:
  284. <itemizedlist>
  285. <listitem><para><filename>/proc/bus/usb/devices</filename>
  286. ... a text file
  287. showing each of the USB devices on known to the kernel,
  288. and their configuration descriptors.
  289. You can also poll() this to learn about new devices.
  290. </para></listitem>
  291. <listitem><para><filename>/proc/bus/usb/BBB/DDD</filename>
  292. ... magic files
  293. exposing the each device's configuration descriptors, and
  294. supporting a series of ioctls for making device requests,
  295. including I/O to devices. (Purely for access by programs.)
  296. </para></listitem>
  297. </itemizedlist>
  298. </para>
  299. <para> Each bus is given a number (BBB) based on when it was
  300. enumerated; within each bus, each device is given a similar
  301. number (DDD).
  302. Those BBB/DDD paths are not "stable" identifiers;
  303. expect them to change even if you always leave the devices
  304. plugged in to the same hub port.
  305. <emphasis>Don't even think of saving these in application
  306. configuration files.</emphasis>
  307. Stable identifiers are available, for user mode applications
  308. that want to use them. HID and networking devices expose
  309. these stable IDs, so that for example you can be sure that
  310. you told the right UPS to power down its second server.
  311. "usbfs" doesn't (yet) expose those IDs.
  312. </para>
  313. </sect1>
  314. <sect1>
  315. <title>Mounting and Access Control</title>
  316. <para>There are a number of mount options for usbfs, which will
  317. be of most interest to you if you need to override the default
  318. access control policy.
  319. That policy is that only root may read or write device files
  320. (<filename>/proc/bus/BBB/DDD</filename>) although anyone may read
  321. the <filename>devices</filename>
  322. or <filename>drivers</filename> files.
  323. I/O requests to the device also need the CAP_SYS_RAWIO capability,
  324. </para>
  325. <para>The significance of that is that by default, all user mode
  326. device drivers need super-user privileges.
  327. You can change modes or ownership in a driver setup
  328. when the device hotplugs, or maye just start the
  329. driver right then, as a privileged server (or some activity
  330. within one).
  331. That's the most secure approach for multi-user systems,
  332. but for single user systems ("trusted" by that user)
  333. it's more convenient just to grant everyone all access
  334. (using the <emphasis>devmode=0666</emphasis> option)
  335. so the driver can start whenever it's needed.
  336. </para>
  337. <para>The mount options for usbfs, usable in /etc/fstab or
  338. in command line invocations of <emphasis>mount</emphasis>, are:
  339. <variablelist>
  340. <varlistentry>
  341. <term><emphasis>busgid</emphasis>=NNNNN</term>
  342. <listitem><para>Controls the GID used for the
  343. /proc/bus/usb/BBB
  344. directories. (Default: 0)</para></listitem></varlistentry>
  345. <varlistentry><term><emphasis>busmode</emphasis>=MMM</term>
  346. <listitem><para>Controls the file mode used for the
  347. /proc/bus/usb/BBB
  348. directories. (Default: 0555)
  349. </para></listitem></varlistentry>
  350. <varlistentry><term><emphasis>busuid</emphasis>=NNNNN</term>
  351. <listitem><para>Controls the UID used for the
  352. /proc/bus/usb/BBB
  353. directories. (Default: 0)</para></listitem></varlistentry>
  354. <varlistentry><term><emphasis>devgid</emphasis>=NNNNN</term>
  355. <listitem><para>Controls the GID used for the
  356. /proc/bus/usb/BBB/DDD
  357. files. (Default: 0)</para></listitem></varlistentry>
  358. <varlistentry><term><emphasis>devmode</emphasis>=MMM</term>
  359. <listitem><para>Controls the file mode used for the
  360. /proc/bus/usb/BBB/DDD
  361. files. (Default: 0644)</para></listitem></varlistentry>
  362. <varlistentry><term><emphasis>devuid</emphasis>=NNNNN</term>
  363. <listitem><para>Controls the UID used for the
  364. /proc/bus/usb/BBB/DDD
  365. files. (Default: 0)</para></listitem></varlistentry>
  366. <varlistentry><term><emphasis>listgid</emphasis>=NNNNN</term>
  367. <listitem><para>Controls the GID used for the
  368. /proc/bus/usb/devices and drivers files.
  369. (Default: 0)</para></listitem></varlistentry>
  370. <varlistentry><term><emphasis>listmode</emphasis>=MMM</term>
  371. <listitem><para>Controls the file mode used for the
  372. /proc/bus/usb/devices and drivers files.
  373. (Default: 0444)</para></listitem></varlistentry>
  374. <varlistentry><term><emphasis>listuid</emphasis>=NNNNN</term>
  375. <listitem><para>Controls the UID used for the
  376. /proc/bus/usb/devices and drivers files.
  377. (Default: 0)</para></listitem></varlistentry>
  378. </variablelist>
  379. </para>
  380. <para>Note that many Linux distributions hard-wire the mount options
  381. for usbfs in their init scripts, such as
  382. <filename>/etc/rc.d/rc.sysinit</filename>,
  383. rather than making it easy to set this per-system
  384. policy in <filename>/etc/fstab</filename>.
  385. </para>
  386. </sect1>
  387. <sect1>
  388. <title>/proc/bus/usb/devices</title>
  389. <para>This file is handy for status viewing tools in user
  390. mode, which can scan the text format and ignore most of it.
  391. More detailed device status (including class and vendor
  392. status) is available from device-specific files.
  393. For information about the current format of this file,
  394. see the
  395. <filename>Documentation/usb/proc_usb_info.txt</filename>
  396. file in your Linux kernel sources.
  397. </para>
  398. <para>Otherwise the main use for this file from programs
  399. is to poll() it to get notifications of usb devices
  400. as they're plugged or unplugged.
  401. To see what changed, you'd need to read the file and
  402. compare "before" and "after" contents, scan the filesystem,
  403. or see its hotplug event.
  404. </para>
  405. </sect1>
  406. <sect1>
  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>
  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><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 khubd (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>
  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 charactor 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>
  638. <title>Synchronous I/O Support</title>
  639. <para>Synchronous requests involve the kernel blocking
  640. until 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>
  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. -->