00001
00041 #ifndef USB_PROTOCOL_H_INCLUDED
00042 #define USB_PROTOCOL_H_INCLUDED
00043
00044 #include <types.h>
00045
00062 enum usb_device_state {
00064 USB_STATE_ATTACHED = 0,
00066 USB_STATE_POWERED,
00068 USB_STATE_DEFAULT,
00073 USB_STATE_ADDRESS,
00075 USB_STATE_CONFIGURED,
00077 USB_STATE_SUSPENDED = 0x80,
00078 USB_STATE_POWERED_SUSPENDED = USB_STATE_POWERED | USB_STATE_SUSPENDED,
00079 USB_STATE_DEFAULT_SUSPENDED = USB_STATE_DEFAULT | USB_STATE_SUSPENDED,
00080 USB_STATE_ADDRESS_SUSPENDED = USB_STATE_ADDRESS | USB_STATE_SUSPENDED,
00081 USB_STATE_CONFIGURED_SUSPENDED = (USB_STATE_CONFIGURED
00082 | USB_STATE_SUSPENDED),
00083 };
00084
00092 enum usb_device_speed {
00093 USB_SPEED_UNKNOWN = 0,
00094 USB_SPEED_LOW,
00095 USB_SPEED_FULL,
00096 USB_SPEED_HIGH,
00097 };
00098
00102 enum usb_reqtype {
00103 USB_REQTYPE_STANDARD = 0,
00104 USB_REQTYPE_CLASS = 1,
00105 USB_REQTYPE_VENDOR = 2,
00106 };
00107
00108 #define USB_REQTYPE_STANDARD_MASK (USB_REQTYPE_STANDARD << 5)
00109 #define USB_REQTYPE_CLASS_MASK (USB_REQTYPE_CLASS << 5)
00110 #define USB_REQTYPE_VENDOR_MASK (USB_REQTYPE_VENDOR << 5)
00111
00115 enum usb_recipient {
00116 USB_RECIP_DEVICE = 0,
00117 USB_RECIP_INTERFACE = 1,
00118 USB_RECIP_ENDPOINT = 2,
00119 USB_RECIP_OTHER = 3,
00120 };
00121
00122 #define USB_RECIP_DEVICE_MASK (USB_RECIP_DEVICE << 0)
00123 #define USB_RECIP_INTERFACE_MASK (USB_RECIP_INTERFACE << 0)
00124 #define USB_RECIP_ENDPOINT_MASK (USB_RECIP_ENDPOINT << 0)
00125 #define USB_RECIP_OTHER_MASK (USB_RECIP_OTHER << 0)
00126
00130 enum usb_reqid {
00131 USB_REQ_GET_STATUS = 0,
00132 USB_REQ_CLEAR_FEATURE = 1,
00133 USB_REQ_SET_FEATURE = 3,
00134 USB_REQ_SET_ADDRESS = 5,
00135 USB_REQ_GET_DESCRIPTOR = 6,
00136 USB_REQ_SET_DESCRIPTOR = 7,
00137 USB_REQ_GET_CONFIGURATION = 8,
00138 USB_REQ_SET_CONFIGURATION = 9,
00139 USB_REQ_GET_INTERFACE = 10,
00140 USB_REQ_SET_INTERFACE = 11,
00141 USB_REQ_SYNCH_FRAME = 12,
00142 };
00143
00154 enum usb_device_feature {
00155 USB_DEV_STATUS_SELF_POWERED = 0,
00156 USB_DEV_FEATURE_REMOTE_WAKEUP = 1,
00157 USB_DEV_FEATURE_TEST_MODE = 2,
00158 };
00159
00163 enum usb_endpoint_feature {
00164 USB_EP_FEATURE_HALT = 0,
00165 };
00166
00170 enum usb_test_mode_selector {
00171 USB_TEST_J = 0x01,
00172 USB_TEST_K = 0x02,
00173 USB_TEST_SE0_NAK = 0x03,
00174 USB_TEST_PACKET = 0x04,
00175 USB_TEST_FORCE_ENABLE = 0x05,
00176 };
00177
00181 enum usb_descriptor_type {
00182 USB_DT_DEVICE = 1,
00183 USB_DT_CONFIGURATION = 2,
00184 USB_DT_STRING = 3,
00185 USB_DT_INTERFACE = 4,
00186 USB_DT_ENDPOINT = 5,
00187 USB_DT_DEVICE_QUALIFIER = 6,
00188 USB_DT_OTHER_SPEED_CONFIGURATION = 7,
00189 USB_DT_INTERFACE_POWER = 8,
00190 };
00191
00195 enum usb_ep_xfer_type {
00196 USB_EP_XFER_CONTROL = 0x00,
00197 USB_EP_XFER_ISOCHRONOUS = 0x01,
00198 USB_EP_XFER_BULK = 0x02,
00199 USB_EP_XFER_INTERRUPT = 0x03,
00200 };
00201
00205 enum usb_langid {
00206 USB_LANGID_EN_US = 0x0406,
00207 };
00208
00212 #define USB_EP_ADDR_MASK 0x0f
00213
00216 #define USB_DIR_IN 0x80
00217
00220 #define USB_DIR_OUT 0x00
00221
00228 #define USB_MAX_DESC_LEN 255
00229
00230
00231
00232
00233
00234 COMPILER_PACK_SET(2);
00235
00241 struct usb_setup_req {
00242 uint8_t bmRequestType;
00243 uint8_t bRequest;
00244 le16_t wValue;
00245 le16_t wIndex;
00246 le16_t wLength;
00247 };
00248
00250 #define usb_setup_is_in(req) (!!((req)->bmRequestType & USB_DIR_IN))
00251
00252 #define usb_setup_is_out(req) (!((req)->bmRequestType & USB_DIR_IN))
00253
00254 #define usb_setup_type(req) (((req)->bmRequestType >> 5) & 3)
00255
00256 #define usb_setup_recipient(req) ((req)->bmRequestType & 0x1f)
00257
00261 struct usb_device_descriptor {
00262 uint8_t bLength;
00263 uint8_t bDescriptorType;
00264 le16_t bcdUSB;
00265 uint8_t bDeviceClass;
00266 uint8_t bDeviceSubClass;
00267 uint8_t bDeviceProtocol;
00268 uint8_t bMaxPacketSize0;
00269 le16_t idVendor;
00270 le16_t idProduct;
00271 le16_t bcdDevice;
00272 uint8_t iManufacturer;
00273 uint8_t iProduct;
00274 uint8_t iSerialNumber;
00275 uint8_t bNumConfigurations;
00276 };
00277
00286 struct usb_device_qualifier_descriptor {
00287 uint8_t bLength;
00288 uint8_t bDescriptorType;
00289 le16_t bcdUSB;
00290 uint8_t bDeviceClass;
00291 uint8_t bDeviceSubClass;
00292 uint8_t bDeviceProtocol;
00293 uint8_t bMaxPacketSize0;
00294 uint8_t bNumConfigurations;
00295 uint8_t bReserved;
00296 };
00297
00298
00299 COMPILER_PACK_SET(1);
00300
00304 struct usb_configuration_descriptor {
00305 uint8_t bLength;
00306 uint8_t bDescriptorType;
00307 le16_t wTotalLength;
00308 uint8_t bNumInterfaces;
00309 uint8_t bConfigurationValue;
00310 uint8_t iConfiguration;
00311 uint8_t bmAttributes;
00312 uint8_t bMaxPower;
00313 };
00314
00315 #define USB_CONFIG_ATTR_MUST_SET (1U << 7) //!< Must always be set
00316 #define USB_CONFIG_ATTR_SELF_POWERED (1U << 6) //!< Self-powered
00317 #define USB_CONFIG_ATTR_REMOTE_WAKEUP (1U << 5) //!< Supports remote wakeup
00318 #define USB_CONFIG_MAX_POWER(ma) (((ma) + 1) / 2) //!< Max power in mA
00319
00323 struct usb_interface_descriptor {
00324 uint8_t bLength;
00325 uint8_t bDescriptorType;
00326 uint8_t bInterfaceNumber;
00327 uint8_t bAlternateSetting;
00328 uint8_t bNumEndpoints;
00329 uint8_t bInterfaceClass;
00330 uint8_t bInterfaceSubClass;
00331 uint8_t bInterfaceProtocol;
00332 uint8_t iInterface;
00333 };
00334
00338 struct usb_endpoint_descriptor {
00339 uint8_t bLength;
00340 uint8_t bDescriptorType;
00341 uint8_t bEndpointAddress;
00342 uint8_t bmAttributes;
00343 le16_t wMaxPacketSize;
00344 uint8_t bInterval;
00345 };
00346
00348 #define usb_ep_index(desc) ((desc)->bEndpointAddress & USB_EP_ADDR_MASK)
00349
00350 #define usb_ep_is_in(desc) (!!((desc)->bEndpointAddress & USB_DIR_IN))
00351
00352 #define usb_ep_is_out(desc) (!((desc)->bEndpointAddress & USB_DIR_IN))
00353
00354 #define usb_ep_xfer(desc) ((desc)->bmAttributes & 0x03)
00355
00356
00357 COMPILER_PACK_SET(2);
00358
00362 struct usb_string_descriptor {
00363 uint8_t bLength;
00364 uint8_t bDescriptorType;
00365 le16_t bString[0];
00366 };
00367
00368 COMPILER_PACK_RESET();
00369
00371
00372 #endif