| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 | /** @addtogroup libusb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * Common USB types and functions.
|
|---|
| 33 | */
|
|---|
| 34 | #ifndef LIBUSB_USB_H_
|
|---|
| 35 | #define LIBUSB_USB_H_
|
|---|
| 36 |
|
|---|
| 37 | #include <byteorder.h>
|
|---|
| 38 | #include <stdbool.h>
|
|---|
| 39 | #include <stdint.h>
|
|---|
| 40 | #include <types/common.h>
|
|---|
| 41 |
|
|---|
| 42 | /** Convert 16bit value from native (host) endianness to USB endianness. */
|
|---|
| 43 | #define uint16_host2usb(n) host2uint16_t_le((n))
|
|---|
| 44 |
|
|---|
| 45 | /** Convert 32bit value from native (host) endianness to USB endianness. */
|
|---|
| 46 | #define uint32_host2usb(n) host2uint32_t_le((n))
|
|---|
| 47 |
|
|---|
| 48 | /** Convert 16bit value from USB endianness into native (host) one. */
|
|---|
| 49 | #define uint16_usb2host(n) uint16_t_le2host((n))
|
|---|
| 50 |
|
|---|
| 51 | /** Convert 32bit value from USB endianness into native (host) one. */
|
|---|
| 52 | #define uint32_usb2host(n) uint32_t_le2host((n))
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | /** USB transfer type. */
|
|---|
| 56 | typedef enum {
|
|---|
| 57 | USB_TRANSFER_CONTROL = 0,
|
|---|
| 58 | USB_TRANSFER_ISOCHRONOUS = 1,
|
|---|
| 59 | USB_TRANSFER_BULK = 2,
|
|---|
| 60 | USB_TRANSFER_INTERRUPT = 3
|
|---|
| 61 | } usb_transfer_type_t;
|
|---|
| 62 |
|
|---|
| 63 | const char * usb_str_transfer_type(usb_transfer_type_t t);
|
|---|
| 64 | const char * usb_str_transfer_type_short(usb_transfer_type_t t);
|
|---|
| 65 |
|
|---|
| 66 | /** USB data transfer direction. */
|
|---|
| 67 | typedef enum {
|
|---|
| 68 | USB_DIRECTION_IN,
|
|---|
| 69 | USB_DIRECTION_OUT,
|
|---|
| 70 | USB_DIRECTION_BOTH
|
|---|
| 71 | } usb_direction_t;
|
|---|
| 72 |
|
|---|
| 73 | const char *usb_str_direction(usb_direction_t);
|
|---|
| 74 |
|
|---|
| 75 | /** USB speeds. */
|
|---|
| 76 | typedef enum {
|
|---|
| 77 | /** USB 1.1 low speed (1.5Mbits/s). */
|
|---|
| 78 | USB_SPEED_LOW,
|
|---|
| 79 | /** USB 1.1 full speed (12Mbits/s). */
|
|---|
| 80 | USB_SPEED_FULL,
|
|---|
| 81 | /** USB 2.0 high speed (480Mbits/s). */
|
|---|
| 82 | USB_SPEED_HIGH,
|
|---|
| 83 | /** Psuedo-speed serving as a boundary. */
|
|---|
| 84 | USB_SPEED_MAX
|
|---|
| 85 | } usb_speed_t;
|
|---|
| 86 |
|
|---|
| 87 | static inline bool usb_speed_is_11(const usb_speed_t s)
|
|---|
| 88 | {
|
|---|
| 89 | return (s == USB_SPEED_FULL) || (s == USB_SPEED_LOW);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | const char *usb_str_speed(usb_speed_t);
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | /** USB request type target. */
|
|---|
| 96 | typedef enum {
|
|---|
| 97 | USB_REQUEST_TYPE_STANDARD = 0,
|
|---|
| 98 | USB_REQUEST_TYPE_CLASS = 1,
|
|---|
| 99 | USB_REQUEST_TYPE_VENDOR = 2
|
|---|
| 100 | } usb_request_type_t;
|
|---|
| 101 |
|
|---|
| 102 | /** USB request recipient. */
|
|---|
| 103 | typedef enum {
|
|---|
| 104 | USB_REQUEST_RECIPIENT_DEVICE = 0,
|
|---|
| 105 | USB_REQUEST_RECIPIENT_INTERFACE = 1,
|
|---|
| 106 | USB_REQUEST_RECIPIENT_ENDPOINT = 2,
|
|---|
| 107 | USB_REQUEST_RECIPIENT_OTHER = 3
|
|---|
| 108 | } usb_request_recipient_t;
|
|---|
| 109 |
|
|---|
| 110 | /** USB address type.
|
|---|
| 111 | * Negative values could be used to indicate error.
|
|---|
| 112 | */
|
|---|
| 113 | typedef int16_t usb_address_t;
|
|---|
| 114 |
|
|---|
| 115 | /** Default USB address. */
|
|---|
| 116 | #define USB_ADDRESS_DEFAULT 0
|
|---|
| 117 |
|
|---|
| 118 | /** Maximum address number in USB 1.1. */
|
|---|
| 119 | #define USB11_ADDRESS_MAX 127
|
|---|
| 120 | #define USB_ADDRESS_COUNT (USB11_ADDRESS_MAX + 1)
|
|---|
| 121 |
|
|---|
| 122 | /** Check USB address for allowed values.
|
|---|
| 123 | *
|
|---|
| 124 | * @param ep USB address.
|
|---|
| 125 | *
|
|---|
| 126 | * @return True, if value is wihtin limits, false otherwise.
|
|---|
| 127 | *
|
|---|
| 128 | */
|
|---|
| 129 | static inline bool usb_address_is_valid(usb_address_t a)
|
|---|
| 130 | {
|
|---|
| 131 | return (a >= USB_ADDRESS_DEFAULT) && (a <= USB11_ADDRESS_MAX);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /** USB endpoint number type.
|
|---|
| 135 | * Negative values could be used to indicate error.
|
|---|
| 136 | */
|
|---|
| 137 | typedef int16_t usb_endpoint_t;
|
|---|
| 138 |
|
|---|
| 139 | /** Default control endpoint */
|
|---|
| 140 | #define USB_ENDPOINT_DEFAULT_CONTROL 0
|
|---|
| 141 |
|
|---|
| 142 | /** Maximum endpoint number in USB 1.1. */
|
|---|
| 143 | #define USB11_ENDPOINT_MAX 16
|
|---|
| 144 |
|
|---|
| 145 | /** Check USB endpoint for allowed values.
|
|---|
| 146 | *
|
|---|
| 147 | * @param ep USB endpoint number.
|
|---|
| 148 | *
|
|---|
| 149 | * @return True, if value is wihtin limits, false otherwise.
|
|---|
| 150 | *
|
|---|
| 151 | */
|
|---|
| 152 | static inline bool usb_endpoint_is_valid(usb_endpoint_t ep)
|
|---|
| 153 | {
|
|---|
| 154 | return (ep >= USB_ENDPOINT_DEFAULT_CONTROL) &&
|
|---|
| 155 | (ep < USB11_ENDPOINT_MAX);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | /** USB complete address type.
|
|---|
| 160 | * Pair address + endpoint is identification of transaction recipient.
|
|---|
| 161 | */
|
|---|
| 162 | typedef union {
|
|---|
| 163 | struct {
|
|---|
| 164 | usb_address_t address;
|
|---|
| 165 | usb_endpoint_t endpoint;
|
|---|
| 166 | } __attribute__((packed));
|
|---|
| 167 | uint32_t packed;
|
|---|
| 168 | } usb_target_t;
|
|---|
| 169 |
|
|---|
| 170 | /** Check USB target for allowed values (address and endpoint).
|
|---|
| 171 | *
|
|---|
| 172 | * @param target.
|
|---|
| 173 | * @return True, if values are wihtin limits, false otherwise.
|
|---|
| 174 | */
|
|---|
| 175 | static inline bool usb_target_is_valid(usb_target_t target)
|
|---|
| 176 | {
|
|---|
| 177 | return usb_address_is_valid(target.address) &&
|
|---|
| 178 | usb_endpoint_is_valid(target.endpoint);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /** Compare USB targets (addresses and endpoints).
|
|---|
| 182 | *
|
|---|
| 183 | * @param a First target.
|
|---|
| 184 | * @param b Second target.
|
|---|
| 185 | * @return Whether @p a and @p b points to the same pipe on the same device.
|
|---|
| 186 | */
|
|---|
| 187 | static inline int usb_target_same(usb_target_t a, usb_target_t b)
|
|---|
| 188 | {
|
|---|
| 189 | return (a.address == b.address)
|
|---|
| 190 | && (a.endpoint == b.endpoint);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /** General handle type.
|
|---|
| 194 | * Used by various USB functions as opaque handle.
|
|---|
| 195 | */
|
|---|
| 196 | typedef sysarg_t usb_handle_t;
|
|---|
| 197 |
|
|---|
| 198 | /** USB packet identifier. */
|
|---|
| 199 | typedef enum {
|
|---|
| 200 | #define _MAKE_PID_NIBBLE(tag, type) \
|
|---|
| 201 | ((uint8_t)(((tag) << 2) | (type)))
|
|---|
| 202 | #define _MAKE_PID(tag, type) \
|
|---|
| 203 | ( \
|
|---|
| 204 | _MAKE_PID_NIBBLE(tag, type) \
|
|---|
| 205 | | (((~_MAKE_PID_NIBBLE(tag, type)) & 0xf) << 4) \
|
|---|
| 206 | )
|
|---|
| 207 | USB_PID_OUT = _MAKE_PID(0, 1),
|
|---|
| 208 | USB_PID_IN = _MAKE_PID(2, 1),
|
|---|
| 209 | USB_PID_SOF = _MAKE_PID(1, 1),
|
|---|
| 210 | USB_PID_SETUP = _MAKE_PID(3, 1),
|
|---|
| 211 |
|
|---|
| 212 | USB_PID_DATA0 = _MAKE_PID(0 ,3),
|
|---|
| 213 | USB_PID_DATA1 = _MAKE_PID(2 ,3),
|
|---|
| 214 |
|
|---|
| 215 | USB_PID_ACK = _MAKE_PID(0 ,2),
|
|---|
| 216 | USB_PID_NAK = _MAKE_PID(2 ,2),
|
|---|
| 217 | USB_PID_STALL = _MAKE_PID(3 ,2),
|
|---|
| 218 |
|
|---|
| 219 | USB_PID_PRE = _MAKE_PID(3 ,0),
|
|---|
| 220 | /* USB_PID_ = _MAKE_PID( ,), */
|
|---|
| 221 | #undef _MAKE_PID
|
|---|
| 222 | #undef _MAKE_PID_NIBBLE
|
|---|
| 223 | } usb_packet_id;
|
|---|
| 224 |
|
|---|
| 225 | /** Category for USB host controllers. */
|
|---|
| 226 | #define USB_HC_CATEGORY "usbhc"
|
|---|
| 227 |
|
|---|
| 228 | #endif
|
|---|
| 229 | /**
|
|---|
| 230 | * @}
|
|---|
| 231 | */
|
|---|