| 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 |
|
|---|
| 29 | /** @addtogroup libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Base USB types.
|
|---|
| 34 | */
|
|---|
| 35 | #ifndef LIBUSB_USB_H_
|
|---|
| 36 | #define LIBUSB_USB_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 | #include <byteorder.h>
|
|---|
| 40 | #include <ipc/ipc.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 |
|
|---|
| 65 | /** USB data transfer direction. */
|
|---|
| 66 | typedef enum {
|
|---|
| 67 | USB_DIRECTION_IN,
|
|---|
| 68 | USB_DIRECTION_OUT
|
|---|
| 69 | } usb_direction_t;
|
|---|
| 70 |
|
|---|
| 71 | /** USB request type target. */
|
|---|
| 72 | typedef enum {
|
|---|
| 73 | USB_REQUEST_TYPE_STANDARD = 0,
|
|---|
| 74 | USB_REQUEST_TYPE_CLASS = 1,
|
|---|
| 75 | USB_REQUEST_TYPE_VENDOR = 2
|
|---|
| 76 | } usb_request_type_t;
|
|---|
| 77 |
|
|---|
| 78 | /** USB request recipient. */
|
|---|
| 79 | typedef enum {
|
|---|
| 80 | USB_REQUEST_RECIPIENT_DEVICE = 0,
|
|---|
| 81 | USB_REQUEST_RECIPIENT_INTERFACE = 1,
|
|---|
| 82 | USB_REQUEST_RECIPIENT_ENDPOINT = 2
|
|---|
| 83 | } usb_request_recipient_t;
|
|---|
| 84 |
|
|---|
| 85 | /** USB transaction outcome. */
|
|---|
| 86 | typedef enum {
|
|---|
| 87 | USB_OUTCOME_OK,
|
|---|
| 88 | USB_OUTCOME_CRCERROR,
|
|---|
| 89 | USB_OUTCOME_BABBLE
|
|---|
| 90 | } usb_transaction_outcome_t;
|
|---|
| 91 |
|
|---|
| 92 | const char * usb_str_transaction_outcome(usb_transaction_outcome_t o);
|
|---|
| 93 |
|
|---|
| 94 | /** USB address type.
|
|---|
| 95 | * Negative values could be used to indicate error.
|
|---|
| 96 | */
|
|---|
| 97 | typedef int usb_address_t;
|
|---|
| 98 |
|
|---|
| 99 | /** Default USB address. */
|
|---|
| 100 | #define USB_ADDRESS_DEFAULT 0
|
|---|
| 101 | /** Maximum address number in USB 1.1. */
|
|---|
| 102 | #define USB11_ADDRESS_MAX 128
|
|---|
| 103 |
|
|---|
| 104 | /** USB endpoint number type.
|
|---|
| 105 | * Negative values could be used to indicate error.
|
|---|
| 106 | */
|
|---|
| 107 | typedef int usb_endpoint_t;
|
|---|
| 108 |
|
|---|
| 109 | /** Maximum endpoint number in USB 1.1.
|
|---|
| 110 | */
|
|---|
| 111 | #define USB11_ENDPOINT_MAX 16
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | /** USB complete address type.
|
|---|
| 115 | * Pair address + endpoint is identification of transaction recipient.
|
|---|
| 116 | */
|
|---|
| 117 | typedef struct {
|
|---|
| 118 | usb_address_t address;
|
|---|
| 119 | usb_endpoint_t endpoint;
|
|---|
| 120 | } usb_target_t;
|
|---|
| 121 |
|
|---|
| 122 | static inline int usb_target_same(usb_target_t a, usb_target_t b)
|
|---|
| 123 | {
|
|---|
| 124 | return (a.address == b.address)
|
|---|
| 125 | && (a.endpoint == b.endpoint);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** General handle type.
|
|---|
| 129 | * Used by various USB functions as opaque handle.
|
|---|
| 130 | */
|
|---|
| 131 | typedef sysarg_t usb_handle_t;
|
|---|
| 132 |
|
|---|
| 133 | /** USB packet identifier. */
|
|---|
| 134 | typedef enum {
|
|---|
| 135 | #define _MAKE_PID_NIBBLE(tag, type) \
|
|---|
| 136 | ((uint8_t)(((tag) << 2) | (type)))
|
|---|
| 137 | #define _MAKE_PID(tag, type) \
|
|---|
| 138 | ( \
|
|---|
| 139 | _MAKE_PID_NIBBLE(tag, type) \
|
|---|
| 140 | | ((~_MAKE_PID_NIBBLE(tag, type)) << 4) \
|
|---|
| 141 | )
|
|---|
| 142 | USB_PID_OUT = _MAKE_PID(0, 1),
|
|---|
| 143 | USB_PID_IN = _MAKE_PID(2, 1),
|
|---|
| 144 | USB_PID_SOF = _MAKE_PID(1, 1),
|
|---|
| 145 | USB_PID_SETUP = _MAKE_PID(3, 1),
|
|---|
| 146 |
|
|---|
| 147 | USB_PID_DATA0 = _MAKE_PID(0 ,3),
|
|---|
| 148 | USB_PID_DATA1 = _MAKE_PID(2 ,3),
|
|---|
| 149 |
|
|---|
| 150 | USB_PID_ACK = _MAKE_PID(0 ,2),
|
|---|
| 151 | USB_PID_NAK = _MAKE_PID(2 ,2),
|
|---|
| 152 | USB_PID_STALL = _MAKE_PID(3 ,2),
|
|---|
| 153 |
|
|---|
| 154 | USB_PID_PRE = _MAKE_PID(3 ,0),
|
|---|
| 155 | /* USB_PID_ = _MAKE_PID( ,), */
|
|---|
| 156 | #undef _MAKE_PID
|
|---|
| 157 | #undef _MAKE_PID_NIBBLE
|
|---|
| 158 | } usb_packet_id;
|
|---|
| 159 |
|
|---|
| 160 | #endif
|
|---|
| 161 | /**
|
|---|
| 162 | * @}
|
|---|
| 163 | */
|
|---|