Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Michael Sevakis | 3dcb4ff | 2008-04-18 17:24:19 +0000 | [diff] [blame] | 8 | * $Id$ |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) by Linux Kernel Developers |
| 11 | * |
| 12 | * Based on code from the Linux Kernel |
| 13 | * available at http://www.kernel.org |
| 14 | * Original file: <kernel>/include/linux/usb/ch9.h |
| 15 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * as published by the Free Software Foundation; either version 2 |
| 19 | * of the License, or (at your option) any later version. |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 20 | * |
| 21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 22 | * KIND, either express or implied. |
| 23 | * |
| 24 | ****************************************************************************/ |
| 25 | |
Christian Gmeiner | fec406c | 2007-10-28 14:02:51 +0000 | [diff] [blame] | 26 | /* |
| 27 | * This file holds USB constants and structures that are needed for |
| 28 | * USB device APIs. These are used by the USB device model, which is |
| 29 | * defined in chapter 9 of the USB 2.0 specification and in the |
| 30 | * Wireless USB 1.0 (spread around). |
| 31 | * |
| 32 | * USB 2.0 adds an additional "On The Go" (OTG) mode, which lets systems |
| 33 | * act either as a USB master/host or as a USB slave/device. That means |
| 34 | * the master and slave side APIs benefit from working well together. |
| 35 | * |
| 36 | * There's also "Wireless USB", using low power short range radios for |
| 37 | * peripheral interconnection but otherwise building on the USB framework. |
| 38 | * |
| 39 | * Note all descriptors are declared '__attribute__((packed))' so that: |
| 40 | * |
| 41 | * [a] they never get padded, either internally (USB spec writers |
| 42 | * probably handled that) or externally; |
| 43 | * |
| 44 | * [b] so that accessing bigger-than-a-bytes fields will never |
| 45 | * generate bus errors on any platform, even when the location of |
| 46 | * its descriptor inside a bundle isn't "naturally aligned", and |
| 47 | * |
| 48 | * [c] for consistency, removing all doubt even when it appears to |
| 49 | * someone that the two other points are non-issues for that |
| 50 | * particular descriptor type. |
| 51 | */ |
| 52 | |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 53 | #ifndef _CH9_H_ |
| 54 | #define _CH9_H_ |
| 55 | |
| 56 | #include <inttypes.h> |
| 57 | |
| 58 | /* |
| 59 | * USB directions |
| 60 | * |
| 61 | * This bit flag is used in endpoint descriptors' bEndpointAddress field. |
| 62 | * It's also one of three fields in control requests bRequestType. |
| 63 | */ |
| 64 | #define USB_DIR_OUT 0 /* to device */ |
| 65 | #define USB_DIR_IN 0x80 /* to host */ |
| 66 | |
| 67 | /* |
| 68 | * USB types, the second of three bRequestType fields |
| 69 | */ |
| 70 | #define USB_TYPE_MASK (0x03 << 5) |
| 71 | #define USB_TYPE_STANDARD (0x00 << 5) |
| 72 | #define USB_TYPE_CLASS (0x01 << 5) |
| 73 | #define USB_TYPE_VENDOR (0x02 << 5) |
| 74 | #define USB_TYPE_RESERVED (0x03 << 5) |
| 75 | |
| 76 | /* |
| 77 | * USB recipients, the third of three bRequestType fields |
| 78 | */ |
| 79 | #define USB_RECIP_MASK 0x1f |
| 80 | #define USB_RECIP_DEVICE 0x00 |
| 81 | #define USB_RECIP_INTERFACE 0x01 |
| 82 | #define USB_RECIP_ENDPOINT 0x02 |
| 83 | #define USB_RECIP_OTHER 0x03 |
| 84 | |
| 85 | /*-------------------------------------------------------------------------*/ |
| 86 | |
| 87 | /** |
| 88 | * struct usb_ctrlrequest - SETUP data for a USB device control request |
| 89 | * @bRequestType: matches the USB bmRequestType field |
| 90 | * @bRequest: matches the USB bRequest field |
| 91 | * @wValue: matches the USB wValue field (le16 byte order) |
| 92 | * @wIndex: matches the USB wIndex field (le16 byte order) |
| 93 | * @wLength: matches the USB wLength field (le16 byte order) |
| 94 | */ |
| 95 | struct usb_ctrlrequest { |
| 96 | uint8_t bRequestType; |
| 97 | uint8_t bRequest; |
| 98 | uint16_t wValue; |
| 99 | uint16_t wIndex; |
| 100 | uint16_t wLength; |
| 101 | } __attribute__ ((packed)); |
| 102 | |
| 103 | /* |
| 104 | * Standard requests, for the bRequest field of a SETUP packet. |
| 105 | * |
| 106 | * These are qualified by the bRequestType field, so that for example |
| 107 | * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved |
| 108 | * by a GET_STATUS request. |
| 109 | */ |
| 110 | #define USB_REQ_GET_STATUS 0x00 |
| 111 | #define USB_REQ_CLEAR_FEATURE 0x01 |
| 112 | #define USB_REQ_SET_FEATURE 0x03 |
| 113 | #define USB_REQ_SET_ADDRESS 0x05 |
| 114 | #define USB_REQ_GET_DESCRIPTOR 0x06 |
| 115 | #define USB_REQ_SET_DESCRIPTOR 0x07 |
| 116 | #define USB_REQ_GET_CONFIGURATION 0x08 |
| 117 | #define USB_REQ_SET_CONFIGURATION 0x09 |
| 118 | #define USB_REQ_GET_INTERFACE 0x0A |
| 119 | #define USB_REQ_SET_INTERFACE 0x0B |
| 120 | #define USB_REQ_SYNCH_FRAME 0x0C |
| 121 | |
| 122 | /*-------------------------------------------------------------------------*/ |
| 123 | |
| 124 | /* |
| 125 | * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or |
| 126 | * (rarely) accepted by SET_DESCRIPTOR. |
| 127 | * |
| 128 | * Note that all multi-byte values here are encoded in little endian |
| 129 | * byte order "on the wire". But when exposed through Linux-USB APIs, |
| 130 | * they've been converted to cpu byte order. |
| 131 | */ |
| 132 | |
| 133 | /* |
| 134 | * Descriptor types ... USB 2.0 spec table 9.5 |
| 135 | */ |
| 136 | #define USB_DT_DEVICE 0x01 |
| 137 | #define USB_DT_CONFIG 0x02 |
| 138 | #define USB_DT_STRING 0x03 |
| 139 | #define USB_DT_INTERFACE 0x04 |
| 140 | #define USB_DT_ENDPOINT 0x05 |
| 141 | #define USB_DT_DEVICE_QUALIFIER 0x06 |
| 142 | #define USB_DT_OTHER_SPEED_CONFIG 0x07 |
| 143 | #define USB_DT_INTERFACE_POWER 0x08 |
| 144 | /* these are from a minor usb 2.0 revision (ECN) */ |
| 145 | #define USB_DT_OTG 0x09 |
| 146 | #define USB_DT_DEBUG 0x0a |
| 147 | #define USB_DT_INTERFACE_ASSOCIATION 0x0b |
| 148 | /* these are from the Wireless USB spec */ |
| 149 | #define USB_DT_SECURITY 0x0c |
| 150 | #define USB_DT_KEY 0x0d |
| 151 | #define USB_DT_ENCRYPTION_TYPE 0x0e |
| 152 | #define USB_DT_BOS 0x0f |
| 153 | #define USB_DT_DEVICE_CAPABILITY 0x10 |
| 154 | #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 |
| 155 | #define USB_DT_WIRE_ADAPTER 0x21 |
| 156 | #define USB_DT_RPIPE 0x22 |
| 157 | |
| 158 | /* Conventional codes for class-specific descriptors. The convention is |
| 159 | * defined in the USB "Common Class" Spec (3.11). Individual class specs |
| 160 | * are authoritative for their usage, not the "common class" writeup. |
| 161 | */ |
| 162 | #define USB_DT_CS_DEVICE (USB_TYPE_CLASS | USB_DT_DEVICE) |
| 163 | #define USB_DT_CS_CONFIG (USB_TYPE_CLASS | USB_DT_CONFIG) |
| 164 | #define USB_DT_CS_STRING (USB_TYPE_CLASS | USB_DT_STRING) |
| 165 | #define USB_DT_CS_INTERFACE (USB_TYPE_CLASS | USB_DT_INTERFACE) |
| 166 | #define USB_DT_CS_ENDPOINT (USB_TYPE_CLASS | USB_DT_ENDPOINT) |
| 167 | |
| 168 | /*-------------------------------------------------------------------------*/ |
| 169 | |
| 170 | /* USB_DT_DEVICE: Device descriptor */ |
| 171 | struct usb_device_descriptor { |
| 172 | uint8_t bLength; |
| 173 | uint8_t bDescriptorType; |
| 174 | uint16_t bcdUSB; |
| 175 | uint8_t bDeviceClass; |
| 176 | uint8_t bDeviceSubClass; |
| 177 | uint8_t bDeviceProtocol; |
| 178 | uint8_t bMaxPacketSize0; |
| 179 | uint16_t idVendor; |
| 180 | uint16_t idProduct; |
| 181 | uint16_t bcdDevice; |
| 182 | uint8_t iManufacturer; |
| 183 | uint8_t iProduct; |
| 184 | uint8_t iSerialNumber; |
| 185 | uint8_t bNumConfigurations; |
| 186 | } __attribute__ ((packed)); |
| 187 | |
| 188 | #define USB_DT_DEVICE_SIZE 18 |
| 189 | |
| 190 | /* |
| 191 | * Device and/or Interface Class codes |
| 192 | * as found in bDeviceClass or bInterfaceClass |
| 193 | * and defined by www.usb.org documents |
| 194 | */ |
| 195 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ |
| 196 | #define USB_CLASS_AUDIO 1 |
| 197 | #define USB_CLASS_COMM 2 |
| 198 | #define USB_CLASS_HID 3 |
| 199 | #define USB_CLASS_PHYSICAL 5 |
| 200 | #define USB_CLASS_STILL_IMAGE 6 |
| 201 | #define USB_CLASS_PRINTER 7 |
| 202 | #define USB_CLASS_MASS_STORAGE 8 |
| 203 | #define USB_CLASS_HUB 9 |
| 204 | #define USB_CLASS_CDC_DATA 0x0a |
| 205 | #define USB_CLASS_CSCID 0x0b /* chip+ smart card */ |
| 206 | #define USB_CLASS_CONTENT_SEC 0x0d /* content security */ |
| 207 | #define USB_CLASS_VIDEO 0x0e |
| 208 | #define USB_CLASS_WIRELESS_CONTROLLER 0xe0 |
| 209 | #define USB_CLASS_MISC 0xef |
| 210 | #define USB_CLASS_APP_SPEC 0xfe |
| 211 | #define USB_CLASS_VENDOR_SPEC 0xff |
| 212 | |
| 213 | /*-------------------------------------------------------------------------*/ |
| 214 | |
| 215 | /* USB_DT_CONFIG: Configuration descriptor information. |
| 216 | * |
| 217 | * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the |
| 218 | * descriptor type is different. Highspeed-capable devices can look |
| 219 | * different depending on what speed they're currently running. Only |
| 220 | * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG |
| 221 | * descriptors. |
| 222 | */ |
| 223 | struct usb_config_descriptor { |
| 224 | uint8_t bLength; |
| 225 | uint8_t bDescriptorType; |
| 226 | uint16_t wTotalLength; |
| 227 | uint8_t bNumInterfaces; |
| 228 | uint8_t bConfigurationValue; |
| 229 | uint8_t iConfiguration; |
| 230 | uint8_t bmAttributes; |
| 231 | uint8_t bMaxPower; |
Frank Gevaerts | f712c7f | 2008-02-29 18:33:54 +0000 | [diff] [blame] | 232 | } __attribute__ ((packed)); |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 233 | |
| 234 | #define USB_DT_CONFIG_SIZE 9 |
| 235 | |
| 236 | /* from config descriptor bmAttributes */ |
| 237 | #define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */ |
| 238 | #define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */ |
| 239 | #define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */ |
| 240 | #define USB_CONFIG_ATT_BATTERY (1 << 4) /* battery powered */ |
| 241 | |
| 242 | /*-------------------------------------------------------------------------*/ |
| 243 | |
| 244 | /* USB_DT_STRING: String descriptor */ |
| 245 | struct usb_string_descriptor { |
| 246 | uint8_t bLength; |
| 247 | uint8_t bDescriptorType; |
| 248 | |
Björn Stenberg | 2f7cffa | 2008-02-11 14:26:25 +0000 | [diff] [blame] | 249 | uint16_t wString[]; /* UTF-16LE encoded */ |
Frank Gevaerts | f712c7f | 2008-02-29 18:33:54 +0000 | [diff] [blame] | 250 | } __attribute__ ((packed)); |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 251 | |
| 252 | /* note that "string" zero is special, it holds language codes that |
| 253 | * the device supports, not Unicode characters. |
| 254 | */ |
| 255 | |
| 256 | /*-------------------------------------------------------------------------*/ |
| 257 | |
| 258 | /* USB_DT_INTERFACE: Interface descriptor */ |
| 259 | struct usb_interface_descriptor { |
| 260 | uint8_t bLength; |
| 261 | uint8_t bDescriptorType; |
| 262 | |
| 263 | uint8_t bInterfaceNumber; |
| 264 | uint8_t bAlternateSetting; |
| 265 | uint8_t bNumEndpoints; |
| 266 | uint8_t bInterfaceClass; |
| 267 | uint8_t bInterfaceSubClass; |
| 268 | uint8_t bInterfaceProtocol; |
| 269 | uint8_t iInterface; |
| 270 | } __attribute__ ((packed)); |
| 271 | |
| 272 | #define USB_DT_INTERFACE_SIZE 9 |
| 273 | |
| 274 | /*-------------------------------------------------------------------------*/ |
| 275 | |
| 276 | /* USB_DT_ENDPOINT: Endpoint descriptor */ |
| 277 | struct usb_endpoint_descriptor { |
| 278 | uint8_t bLength; |
| 279 | uint8_t bDescriptorType; |
| 280 | |
| 281 | uint8_t bEndpointAddress; |
| 282 | uint8_t bmAttributes; |
| 283 | uint16_t wMaxPacketSize; |
| 284 | uint8_t bInterval; |
| 285 | |
| 286 | /* NOTE: these two are _only_ in audio endpoints. */ |
| 287 | /* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */ |
| 288 | //uint8_t bRefresh; |
| 289 | //uint8_t bSynchAddress; |
Frank Gevaerts | f712c7f | 2008-02-29 18:33:54 +0000 | [diff] [blame] | 290 | } __attribute__ ((packed)); |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 291 | |
| 292 | #define USB_DT_ENDPOINT_SIZE 7 |
| 293 | #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ |
| 294 | |
| 295 | /*-------------------------------------------------------------------------*/ |
| 296 | |
| 297 | /* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */ |
| 298 | struct usb_qualifier_descriptor { |
| 299 | uint8_t bLength; |
| 300 | uint8_t bDescriptorType; |
| 301 | |
| 302 | uint16_t bcdUSB; |
| 303 | uint8_t bDeviceClass; |
| 304 | uint8_t bDeviceSubClass; |
| 305 | uint8_t bDeviceProtocol; |
| 306 | uint8_t bMaxPacketSize0; |
| 307 | uint8_t bNumConfigurations; |
| 308 | uint8_t bRESERVED; |
Frank Gevaerts | f712c7f | 2008-02-29 18:33:54 +0000 | [diff] [blame] | 309 | } __attribute__ ((packed)); |
Christian Gmeiner | 8181a0c | 2007-08-27 16:04:32 +0000 | [diff] [blame] | 310 | |
| 311 | /*-------------------------------------------------------------------------*/ |
| 312 | |
| 313 | /* USB_DT_OTG (from OTG 1.0a supplement) */ |
| 314 | struct usb_otg_descriptor { |
| 315 | uint8_t bLength; |
| 316 | uint8_t bDescriptorType; |
| 317 | |
| 318 | uint8_t bmAttributes; /* support for HNP, SRP, etc */ |
| 319 | } __attribute__ ((packed)); |
| 320 | |
| 321 | /* from usb_otg_descriptor.bmAttributes */ |
| 322 | #define USB_OTG_SRP (1 << 0) |
| 323 | #define USB_OTG_HNP (1 << 1) /* swap host/device roles */ |
| 324 | |
| 325 | /*-------------------------------------------------------------------------*/ |
| 326 | |
| 327 | /* USB_DT_DEBUG: for special highspeed devices, replacing serial console */ |
| 328 | struct usb_debug_descriptor { |
| 329 | uint8_t bLength; |
| 330 | uint8_t bDescriptorType; |
| 331 | |
| 332 | /* bulk endpoints with 8 byte maxpacket */ |
| 333 | uint8_t bDebugInEndpoint; |
| 334 | uint8_t bDebugOutEndpoint; |
| 335 | }; |
| 336 | |
| 337 | /*-------------------------------------------------------------------------*/ |
| 338 | |
| 339 | /* |
| 340 | * Endpoints |
| 341 | */ |
| 342 | #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ |
| 343 | #define USB_ENDPOINT_XFER_CONTROL 0 |
| 344 | #define USB_ENDPOINT_XFER_ISOC 1 |
| 345 | #define USB_ENDPOINT_XFER_BULK 2 |
| 346 | #define USB_ENDPOINT_XFER_INT 3 |
| 347 | |
| 348 | enum usb_device_speed { |
| 349 | USB_SPEED_UNKNOWN = 0, /* enumerating */ |
| 350 | USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */ |
| 351 | USB_SPEED_HIGH, /* usb 2.0 */ |
| 352 | USB_SPEED_VARIABLE, /* wireless (usb 2.5) */ |
| 353 | }; |
| 354 | |
| 355 | enum usb_device_state { |
| 356 | /* NOTATTACHED isn't in the USB spec, and this state acts |
| 357 | * the same as ATTACHED ... but it's clearer this way. |
| 358 | */ |
| 359 | USB_STATE_NOTATTACHED = 0, |
| 360 | |
| 361 | /* chapter 9 and authentication (wireless) device states */ |
| 362 | USB_STATE_ATTACHED, |
| 363 | USB_STATE_POWERED, /* wired */ |
| 364 | USB_STATE_UNAUTHENTICATED, /* auth */ |
| 365 | USB_STATE_RECONNECTING, /* auth */ |
| 366 | USB_STATE_DEFAULT, /* limited function */ |
| 367 | USB_STATE_ADDRESS, |
| 368 | USB_STATE_CONFIGURED, /* most functions */ |
| 369 | |
| 370 | USB_STATE_SUSPENDED |
| 371 | |
| 372 | /* NOTE: there are actually four different SUSPENDED |
| 373 | * states, returning to POWERED, DEFAULT, ADDRESS, or |
| 374 | * CONFIGURED respectively when SOF tokens flow again. |
| 375 | */ |
| 376 | }; |
| 377 | |
| 378 | /* All standard descriptors have these 2 fields at the beginning */ |
| 379 | struct usb_descriptor_header { |
| 380 | uint8_t bLength; |
| 381 | uint8_t bDescriptorType; |
| 382 | } __attribute__ ((packed)); |
| 383 | |
| 384 | /** |
| 385 | * struct usb_string - wraps a C string and its USB id |
| 386 | * @id:the (nonzero) ID for this string |
| 387 | * @s:the string, in UTF-8 encoding |
| 388 | * |
| 389 | * If you're using usb_gadget_get_string(), use this to wrap a string |
| 390 | * together with its ID. |
| 391 | */ |
| 392 | struct usb_string { |
| 393 | uint8_t id; |
| 394 | const char* s; |
| 395 | }; |
| 396 | |
| 397 | /** |
| 398 | * struct usb_gadget_strings - a set of USB strings in a given language |
| 399 | * @language:identifies the strings' language (0x0409 for en-us) |
| 400 | * @strings:array of strings with their ids |
| 401 | * |
| 402 | * If you're using usb_gadget_get_string(), use this to wrap all the |
| 403 | * strings for a given language. |
| 404 | */ |
| 405 | struct usb_gadget_strings { |
| 406 | uint16_t language; /* 0x0409 for en-us */ |
| 407 | struct usb_string* strings; |
| 408 | }; |
| 409 | |
| 410 | #endif /*_CH9_H_*/ |