| 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 <stdbool.h>
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 | #include <byteorder.h>
|
|---|
| 40 |
|
|---|
| 41 | /** Convert 16bit value from native (host) endianness to USB endianness. */
|
|---|
| 42 | #define uint16_host2usb(n) host2uint16_t_le((n))
|
|---|
| 43 |
|
|---|
| 44 | /** Convert 32bit value from native (host) endianness to USB endianness. */
|
|---|
| 45 | #define uint32_host2usb(n) host2uint32_t_le((n))
|
|---|
| 46 |
|
|---|
| 47 | /** Convert 16bit value from USB endianness into native (host) one. */
|
|---|
| 48 | #define uint16_usb2host(n) uint16_t_le2host((n))
|
|---|
| 49 |
|
|---|
| 50 | /** Convert 32bit value from USB endianness into native (host) one. */
|
|---|
| 51 | #define uint32_usb2host(n) uint32_t_le2host((n))
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /** USB transfer type. */
|
|---|
| 55 | typedef enum {
|
|---|
| 56 | USB_TRANSFER_CONTROL = 0,
|
|---|
| 57 | USB_TRANSFER_ISOCHRONOUS = 1,
|
|---|
| 58 | USB_TRANSFER_BULK = 2,
|
|---|
| 59 | USB_TRANSFER_INTERRUPT = 3
|
|---|
| 60 | } usb_transfer_type_t;
|
|---|
| 61 |
|
|---|
| 62 | const char * usb_str_transfer_type(usb_transfer_type_t t);
|
|---|
| 63 | const char * usb_str_transfer_type_short(usb_transfer_type_t t);
|
|---|
| 64 |
|
|---|
| 65 | /** USB data transfer direction. */
|
|---|
| 66 | typedef enum {
|
|---|
| 67 | USB_DIRECTION_IN,
|
|---|
| 68 | USB_DIRECTION_OUT,
|
|---|
| 69 | USB_DIRECTION_BOTH
|
|---|
| 70 | } usb_direction_t;
|
|---|
| 71 |
|
|---|
| 72 | const char *usb_str_direction(usb_direction_t);
|
|---|
| 73 |
|
|---|
| 74 | /** USB speeds. */
|
|---|
| 75 | typedef enum {
|
|---|
| 76 | /** USB 1.1 low speed (1.5Mbits/s). */
|
|---|
| 77 | USB_SPEED_LOW,
|
|---|
| 78 | /** USB 1.1 full speed (12Mbits/s). */
|
|---|
| 79 | USB_SPEED_FULL,
|
|---|
| 80 | /** USB 2.0 high speed (480Mbits/s). */
|
|---|
| 81 | USB_SPEED_HIGH,
|
|---|
| 82 | /** Psuedo-speed serving as a boundary. */
|
|---|
| 83 | USB_SPEED_MAX
|
|---|
| 84 | } usb_speed_t;
|
|---|
| 85 |
|
|---|
| 86 | static inline bool usb_speed_is_11(const usb_speed_t s)
|
|---|
| 87 | {
|
|---|
| 88 | return (s == USB_SPEED_FULL) || (s == USB_SPEED_LOW);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | const char *usb_str_speed(usb_speed_t);
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | /** USB request type target. */
|
|---|
| 95 | typedef enum {
|
|---|
| 96 | USB_REQUEST_TYPE_STANDARD = 0,
|
|---|
| 97 | USB_REQUEST_TYPE_CLASS = 1,
|
|---|
| 98 | USB_REQUEST_TYPE_VENDOR = 2
|
|---|
| 99 | } usb_request_type_t;
|
|---|
| 100 |
|
|---|
| 101 | /** USB request recipient. */
|
|---|
| 102 | typedef enum {
|
|---|
| 103 | USB_REQUEST_RECIPIENT_DEVICE = 0,
|
|---|
| 104 | USB_REQUEST_RECIPIENT_INTERFACE = 1,
|
|---|
| 105 | USB_REQUEST_RECIPIENT_ENDPOINT = 2,
|
|---|
| 106 | USB_REQUEST_RECIPIENT_OTHER = 3
|
|---|
| 107 | } usb_request_recipient_t;
|
|---|
| 108 |
|
|---|
| 109 | /** USB address type.
|
|---|
| 110 | * Negative values could be used to indicate error.
|
|---|
| 111 | */
|
|---|
| 112 | typedef int16_t usb_address_t;
|
|---|
| 113 |
|
|---|
| 114 | /** Default USB address. */
|
|---|
| 115 | #define USB_ADDRESS_DEFAULT 0
|
|---|
| 116 | /** Maximum address number in USB 1.1. */
|
|---|
| 117 | #define USB11_ADDRESS_MAX 127
|
|---|
| 118 | #define USB_ADDRESS_COUNT (USB11_ADDRESS_MAX + 1)
|
|---|
| 119 |
|
|---|
| 120 | /** Check USB address for allowed values.
|
|---|
| 121 | *
|
|---|
| 122 | * @param ep USB address.
|
|---|
| 123 | * @return True, if value is wihtin limits, false otherwise.
|
|---|
| 124 | */
|
|---|
| 125 | static inline bool usb_address_is_valid(usb_address_t a)
|
|---|
| 126 | {
|
|---|
| 127 | return (a >= USB_ADDRESS_DEFAULT) && (a <= USB11_ADDRESS_MAX);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** USB endpoint number type.
|
|---|
| 131 | * Negative values could be used to indicate error.
|
|---|
| 132 | */
|
|---|
| 133 | typedef int16_t usb_endpoint_t;
|
|---|
| 134 |
|
|---|
| 135 | /** Default control endpoint */
|
|---|
| 136 | #define USB_ENDPOINT_DEFAULT_CONTROL 0
|
|---|
| 137 | /** Maximum endpoint number in USB 1.1. */
|
|---|
| 138 | #define USB11_ENDPOINT_MAX 16
|
|---|
| 139 |
|
|---|
| 140 | /** Check USB endpoint for allowed values.
|
|---|
| 141 | *
|
|---|
| 142 | * @param ep USB endpoint number.
|
|---|
| 143 | * @return True, if value is wihtin limits, false otherwise.
|
|---|
| 144 | */
|
|---|
| 145 | static inline bool usb_endpoint_is_valid(usb_endpoint_t ep)
|
|---|
| 146 | {
|
|---|
| 147 | return (ep >= USB_ENDPOINT_DEFAULT_CONTROL) &&
|
|---|
| 148 | (ep < USB11_ENDPOINT_MAX);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 | /** USB complete address type.
|
|---|
| 153 | * Pair address + endpoint is identification of transaction recipient.
|
|---|
| 154 | */
|
|---|
| 155 | typedef union {
|
|---|
| 156 | struct {
|
|---|
| 157 | usb_address_t address;
|
|---|
| 158 | usb_endpoint_t endpoint;
|
|---|
| 159 | } __attribute__((packed));
|
|---|
| 160 | uint32_t packed;
|
|---|
| 161 | } usb_target_t;
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | /** Check USB target for allowed values (address and endpoint).
|
|---|
| 165 | *
|
|---|
| 166 | * @param target.
|
|---|
| 167 | * @return True, if values are wihtin limits, false otherwise.
|
|---|
| 168 | */
|
|---|
| 169 | static inline bool usb_target_is_valid(usb_target_t target)
|
|---|
| 170 | {
|
|---|
| 171 | return usb_address_is_valid(target.address) &&
|
|---|
| 172 | usb_endpoint_is_valid(target.endpoint);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** Compare USB targets (addresses and endpoints).
|
|---|
| 176 | *
|
|---|
| 177 | * @param a First target.
|
|---|
| 178 | * @param b Second target.
|
|---|
| 179 | * @return Whether @p a and @p b points to the same pipe on the same device.
|
|---|
| 180 | */
|
|---|
| 181 | static inline int usb_target_same(usb_target_t a, usb_target_t b)
|
|---|
| 182 | {
|
|---|
| 183 | return (a.address == b.address)
|
|---|
| 184 | && (a.endpoint == b.endpoint);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /** General handle type.
|
|---|
| 188 | * Used by various USB functions as opaque handle.
|
|---|
| 189 | */
|
|---|
| 190 | typedef sysarg_t usb_handle_t;
|
|---|
| 191 |
|
|---|
| 192 | /** USB packet identifier. */
|
|---|
| 193 | typedef enum {
|
|---|
| 194 | #define _MAKE_PID_NIBBLE(tag, type) \
|
|---|
| 195 | ((uint8_t)(((tag) << 2) | (type)))
|
|---|
| 196 | #define _MAKE_PID(tag, type) \
|
|---|
| 197 | ( \
|
|---|
| 198 | _MAKE_PID_NIBBLE(tag, type) \
|
|---|
| 199 | | (((~_MAKE_PID_NIBBLE(tag, type)) & 0xf) << 4) \
|
|---|
| 200 | )
|
|---|
| 201 | USB_PID_OUT = _MAKE_PID(0, 1),
|
|---|
| 202 | USB_PID_IN = _MAKE_PID(2, 1),
|
|---|
| 203 | USB_PID_SOF = _MAKE_PID(1, 1),
|
|---|
| 204 | USB_PID_SETUP = _MAKE_PID(3, 1),
|
|---|
| 205 |
|
|---|
| 206 | USB_PID_DATA0 = _MAKE_PID(0 ,3),
|
|---|
| 207 | USB_PID_DATA1 = _MAKE_PID(2 ,3),
|
|---|
| 208 |
|
|---|
| 209 | USB_PID_ACK = _MAKE_PID(0 ,2),
|
|---|
| 210 | USB_PID_NAK = _MAKE_PID(2 ,2),
|
|---|
| 211 | USB_PID_STALL = _MAKE_PID(3 ,2),
|
|---|
| 212 |
|
|---|
| 213 | USB_PID_PRE = _MAKE_PID(3 ,0),
|
|---|
| 214 | /* USB_PID_ = _MAKE_PID( ,), */
|
|---|
| 215 | #undef _MAKE_PID
|
|---|
| 216 | #undef _MAKE_PID_NIBBLE
|
|---|
| 217 | } usb_packet_id;
|
|---|
| 218 |
|
|---|
| 219 | /** Category for USB host controllers. */
|
|---|
| 220 | #define USB_HC_CATEGORY "usbhc"
|
|---|
| 221 |
|
|---|
| 222 | #endif
|
|---|
| 223 | /**
|
|---|
| 224 | * @}
|
|---|
| 225 | */
|
|---|