[c0e1be7] | 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 usb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief USB Host Controller Driver.
|
---|
| 34 | */
|
---|
| 35 | #ifndef LIBUSB_HCD_H_
|
---|
| 36 | #define LIBUSB_HCD_H_
|
---|
| 37 |
|
---|
[fd17ab5] | 38 | #include "usb.h"
|
---|
| 39 |
|
---|
[c0e1be7] | 40 | #include <ipc/ipc.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 |
|
---|
| 43 | /** Maximum size of transaction payload. */
|
---|
| 44 | #define USB_MAX_PAYLOAD_SIZE 1020
|
---|
| 45 |
|
---|
| 46 | /** Opaque handle of active USB transaction.
|
---|
| 47 | * This handle is when informing about transaction outcome (or status).
|
---|
| 48 | */
|
---|
| 49 | typedef ipcarg_t usb_transaction_handle_t;
|
---|
| 50 |
|
---|
| 51 |
|
---|
[34586183] | 52 | /** USB packet identifier. */
|
---|
| 53 | typedef enum {
|
---|
| 54 | #define _MAKE_PID_NIBBLE(tag, type) \
|
---|
| 55 | ((uint8_t)(((tag) << 2) | (type)))
|
---|
| 56 | #define _MAKE_PID(tag, type) \
|
---|
| 57 | ( \
|
---|
| 58 | _MAKE_PID_NIBBLE(tag, type) \
|
---|
| 59 | | ((~_MAKE_PID_NIBBLE(tag, type)) << 4) \
|
---|
| 60 | )
|
---|
| 61 | USB_PID_OUT = _MAKE_PID(0, 1),
|
---|
| 62 | USB_PID_IN = _MAKE_PID(2, 1),
|
---|
| 63 | USB_PID_SOF = _MAKE_PID(1, 1),
|
---|
| 64 | USB_PID_SETUP = _MAKE_PID(3, 1),
|
---|
| 65 |
|
---|
| 66 | USB_PID_DATA0 = _MAKE_PID(0 ,3),
|
---|
| 67 | USB_PID_DATA1 = _MAKE_PID(2 ,3),
|
---|
| 68 |
|
---|
| 69 | USB_PID_ACK = _MAKE_PID(0 ,2),
|
---|
| 70 | USB_PID_NAK = _MAKE_PID(2 ,2),
|
---|
| 71 | USB_PID_STALL = _MAKE_PID(3 ,2),
|
---|
| 72 |
|
---|
| 73 | USB_PID_PRE = _MAKE_PID(3 ,0),
|
---|
| 74 | /* USB_PID_ = _MAKE_PID( ,), */
|
---|
| 75 | #undef _MAKE_PID
|
---|
| 76 | #undef _MAKE_PID_NIBBLE
|
---|
| 77 | } usb_packet_id;
|
---|
| 78 |
|
---|
[fa2a361] | 79 | /** IPC methods for HCD.
|
---|
| 80 | *
|
---|
| 81 | * Notes for async methods:
|
---|
| 82 | *
|
---|
| 83 | * Methods for sending data to device (OUT transactions)
|
---|
| 84 | * - e.g. IPC_M_USB_HCD_INTERRUPT_OUT_ASYNC -
|
---|
| 85 | * always use the same semantics:
|
---|
| 86 | * - first, IPC call with given method is made
|
---|
| 87 | * - argument #1 is target address
|
---|
| 88 | * - argument #2 is target endpoint
|
---|
| 89 | * - argument #3 is buffer size
|
---|
| 90 | * - this call is immediately followed by IPC data write (from caller)
|
---|
| 91 | * - the initial call (and the whole transaction) is answer after the
|
---|
| 92 | * transaction is scheduled by the HC and acknowledged by the device
|
---|
| 93 | * or immediatelly after error is detected
|
---|
| 94 | * - the answer carries only the error code
|
---|
| 95 | *
|
---|
| 96 | * Methods for retrieving data from device (IN transactions)
|
---|
| 97 | * - e.g. IPC_M_USB_HCD_INTERRUPT_IN_ASYNC -
|
---|
| 98 | * also use the same semantics:
|
---|
| 99 | * - first, IPC call with given method is made
|
---|
| 100 | * - argument #1 is target address
|
---|
| 101 | * - argument #2 is target endpoint
|
---|
| 102 | * - argument #3 is buffer size
|
---|
| 103 | * - the call is not answered until the device returns some data (or until
|
---|
| 104 | * error occurs)
|
---|
| 105 | * - if the call is answered with EOK, first argument of the answer is buffer
|
---|
| 106 | * hash that could be used to retrieve the actual data
|
---|
| 107 | *
|
---|
| 108 | * Some special methods (NO-DATA transactions) do not send any data. These
|
---|
| 109 | * might behave as both OUT or IN transactions because communication parts
|
---|
| 110 | * where actual buffers are exchanged are ommitted.
|
---|
| 111 | *
|
---|
| 112 | * The mentioned data retrieval can be done any time after receiving EOK
|
---|
| 113 | * answer to IN method.
|
---|
| 114 | * This retrieval is done using the IPC_M_USB_HCD_GET_BUFFER_ASYNC where
|
---|
| 115 | * the first argument is buffer hash from call answer.
|
---|
| 116 | * This call must be immediatelly followed by data read-in and after the
|
---|
| 117 | * data are transferred, the initial call (IPC_M_USB_HCD_GET_BUFFER_ASYNC)
|
---|
| 118 | * is answered. Each buffer can be retrieved only once.
|
---|
| 119 | *
|
---|
| 120 | * For all these methods, wrap functions exists. Important rule: functions
|
---|
| 121 | * for IN transactions have (as parameters) buffers where retrieved data
|
---|
| 122 | * will be stored. These buffers must be already allocated and shall not be
|
---|
| 123 | * touch until the transaction is completed
|
---|
| 124 | * (e.g. not before calling usb_hcd_async_wait_for() with appropriate handle).
|
---|
| 125 | * OUT transactions buffers can be freed immediatelly after call is dispatched
|
---|
| 126 | * (i.e. after return from wrapping function).
|
---|
[1d1f894] | 127 | *
|
---|
[fa2a361] | 128 | */
|
---|
[c0e1be7] | 129 | typedef enum {
|
---|
| 130 | /** Tell maximum size of the transaction buffer (payload).
|
---|
| 131 | *
|
---|
| 132 | * Arguments of the call:
|
---|
| 133 | * (none)
|
---|
| 134 | *
|
---|
| 135 | * Answer:
|
---|
| 136 | * - EOK - always
|
---|
| 137 | *
|
---|
| 138 | * Arguments of the answer:
|
---|
| 139 | * - buffer size (in bytes):
|
---|
| 140 | */
|
---|
[1d1f894] | 141 | IPC_M_USB_HCD_TRANSACTION_SIZE = IPC_FIRST_USER_METHOD,
|
---|
[fa2a361] | 142 |
|
---|
| 143 | /** Asks for data buffer.
|
---|
| 144 | * See explanation at usb_hcd_method_t.
|
---|
| 145 | */
|
---|
[2185776] | 146 | IPC_M_USB_HCD_GET_BUFFER_ASYNC,
|
---|
| 147 |
|
---|
[fa2a361] | 148 |
|
---|
| 149 |
|
---|
| 150 | /** Send interrupt data to device.
|
---|
| 151 | * See explanation at usb_hcd_method_t (OUT transaction).
|
---|
| 152 | */
|
---|
[2185776] | 153 | IPC_M_USB_HCD_INTERRUPT_OUT_ASYNC,
|
---|
[fa2a361] | 154 |
|
---|
| 155 | /** Get interrupt data from device.
|
---|
| 156 | * See explanation at usb_hcd_method_t (IN transaction).
|
---|
| 157 | */
|
---|
[2185776] | 158 | IPC_M_USB_HCD_INTERRUPT_IN_ASYNC,
|
---|
| 159 |
|
---|
[fa2a361] | 160 |
|
---|
| 161 | /** Start WRITE control transfer.
|
---|
| 162 | * See explanation at usb_hcd_method_t (OUT transaction).
|
---|
| 163 | */
|
---|
[2185776] | 164 | IPC_M_USB_HCD_CONTROL_WRITE_SETUP_ASYNC,
|
---|
[fa2a361] | 165 |
|
---|
| 166 | /** Send control-transfer data to device.
|
---|
| 167 | * See explanation at usb_hcd_method_t (OUT transaction).
|
---|
| 168 | */
|
---|
[2185776] | 169 | IPC_M_USB_HCD_CONTROL_WRITE_DATA_ASYNC,
|
---|
[fa2a361] | 170 |
|
---|
| 171 | /** Terminate WRITE control transfer.
|
---|
| 172 | * See explanation at usb_hcd_method_t (NO-DATA transaction).
|
---|
| 173 | */
|
---|
[2185776] | 174 | IPC_M_USB_HCD_CONTROL_WRITE_STATUS_ASYNC,
|
---|
| 175 |
|
---|
[fa2a361] | 176 |
|
---|
| 177 |
|
---|
| 178 | /** Start READ control transfer.
|
---|
| 179 | * See explanation at usb_hcd_method_t (OUT transaction).
|
---|
| 180 | */
|
---|
[2185776] | 181 | IPC_M_USB_HCD_CONTROL_READ_SETUP_ASYNC,
|
---|
[fa2a361] | 182 |
|
---|
| 183 | /** Get control-transfer data from device.
|
---|
| 184 | * See explanation at usb_hcd_method_t (IN transaction).
|
---|
| 185 | */
|
---|
[2185776] | 186 | IPC_M_USB_HCD_CONTROL_READ_DATA_ASYNC,
|
---|
[fa2a361] | 187 |
|
---|
| 188 | /** Terminate READ control transfer.
|
---|
| 189 | * See explanation at usb_hcd_method_t (NO-DATA transaction).
|
---|
| 190 | */
|
---|
[2185776] | 191 | IPC_M_USB_HCD_CONTROL_READ_STATUS_ASYNC,
|
---|
[fa2a361] | 192 |
|
---|
| 193 |
|
---|
[34586183] | 194 | /* IPC_M_USB_HCD_ */
|
---|
[c0e1be7] | 195 | } usb_hcd_method_t;
|
---|
| 196 |
|
---|
| 197 | /** IPC methods for callbacks from HCD. */
|
---|
| 198 | typedef enum {
|
---|
| 199 | /** Confimation after data sent.
|
---|
| 200 | *
|
---|
| 201 | * Arguments of the call:
|
---|
| 202 | * - transaction handle
|
---|
| 203 | * - transaction outcome
|
---|
| 204 | */
|
---|
| 205 | IPC_M_USB_HCD_DATA_SENT = IPC_FIRST_USER_METHOD,
|
---|
| 206 |
|
---|
| 207 | /** Notification of data received.
|
---|
| 208 | * This call initiates sending a data buffer from HCD to the client.
|
---|
| 209 | * See IPC_M_USB_HCD_SEND_DATA for details for buffer transfer is
|
---|
| 210 | * done.
|
---|
| 211 | *
|
---|
| 212 | * Arguments of the call:
|
---|
| 213 | * - transaction handle
|
---|
| 214 | * - transaction outcome
|
---|
| 215 | * - actual data length
|
---|
| 216 | */
|
---|
| 217 | IPC_M_USB_HCD_DATA_RECEIVED,
|
---|
| 218 |
|
---|
| 219 | /** Notification about a serious trouble with HC.
|
---|
| 220 | */
|
---|
[34586183] | 221 | IPC_M_USB_HCD_CONTROLLER_FAILURE,
|
---|
| 222 |
|
---|
| 223 | /* IPC_M_USB_HCD_ */
|
---|
[c0e1be7] | 224 | } usb_hcd_callback_method_t;
|
---|
| 225 |
|
---|
| 226 |
|
---|
[1d1f894] | 227 | int usb_hcd_connect(const char *);
|
---|
[34586183] | 228 |
|
---|
[2185776] | 229 | int usb_hcd_async_transfer_interrupt_out(int, usb_target_t,
|
---|
| 230 | void *, size_t, usb_handle_t *);
|
---|
| 231 | int usb_hcd_async_transfer_interrupt_in(int, usb_target_t,
|
---|
| 232 | void *, size_t, size_t *, usb_handle_t *);
|
---|
| 233 |
|
---|
| 234 | int usb_hcd_async_transfer_control_write_setup(int, usb_target_t,
|
---|
| 235 | void *, size_t, usb_handle_t *);
|
---|
| 236 | int usb_hcd_async_transfer_control_write_data(int, usb_target_t,
|
---|
| 237 | void *, size_t, usb_handle_t *);
|
---|
| 238 | int usb_hcd_async_transfer_control_write_status(int, usb_target_t,
|
---|
| 239 | usb_handle_t *);
|
---|
| 240 |
|
---|
| 241 | int usb_hcd_async_transfer_control_read_setup(int, usb_target_t,
|
---|
| 242 | void *, size_t, usb_handle_t *);
|
---|
| 243 | int usb_hcd_async_transfer_control_read_data(int, usb_target_t,
|
---|
| 244 | void *, size_t, size_t *, usb_handle_t *);
|
---|
| 245 | int usb_hcd_async_transfer_control_read_status(int, usb_target_t,
|
---|
| 246 | usb_handle_t *);
|
---|
| 247 |
|
---|
| 248 | int usb_hcd_async_wait_for(usb_handle_t);
|
---|
| 249 |
|
---|
[c0e1be7] | 250 | #endif
|
---|
| 251 | /**
|
---|
| 252 | * @}
|
---|
| 253 | */
|
---|