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