| 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 (implementation).
|
|---|
| 34 | */
|
|---|
| 35 | #include "hcd.h"
|
|---|
| 36 | #include <devmap.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <fcntl.h>
|
|---|
| 39 | #include <vfs/vfs.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 |
|
|---|
| 42 | /** Information about pending transaction on HC. */
|
|---|
| 43 | typedef struct {
|
|---|
| 44 | /** Phone to host controller driver. */
|
|---|
| 45 | int phone;
|
|---|
| 46 | /** Data buffer. */
|
|---|
| 47 | void *buffer;
|
|---|
| 48 | /** Buffer size. */
|
|---|
| 49 | size_t size;
|
|---|
| 50 | /** Storage for actual number of bytes transferred. */
|
|---|
| 51 | size_t *size_transferred;
|
|---|
| 52 | /** Initial call replay data. */
|
|---|
| 53 | ipc_call_t reply;
|
|---|
| 54 | /** Initial call identifier. */
|
|---|
| 55 | aid_t request;
|
|---|
| 56 | } transfer_info_t;
|
|---|
| 57 |
|
|---|
| 58 | #define NAMESPACE "usb"
|
|---|
| 59 |
|
|---|
| 60 | /** String representation for USB transfer type. */
|
|---|
| 61 | const char * usb_str_transfer_type(usb_transfer_type_t t)
|
|---|
| 62 | {
|
|---|
| 63 | switch (t) {
|
|---|
| 64 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 65 | return "isochronous";
|
|---|
| 66 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 67 | return "interrupt";
|
|---|
| 68 | case USB_TRANSFER_CONTROL:
|
|---|
| 69 | return "control";
|
|---|
| 70 | case USB_TRANSFER_BULK:
|
|---|
| 71 | return "bulk";
|
|---|
| 72 | default:
|
|---|
| 73 | return "unknown";
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** String representation of USB transaction outcome. */
|
|---|
| 78 | const char * usb_str_transaction_outcome(usb_transaction_outcome_t o)
|
|---|
| 79 | {
|
|---|
| 80 | switch (o) {
|
|---|
| 81 | case USB_OUTCOME_OK:
|
|---|
| 82 | return "ok";
|
|---|
| 83 | case USB_OUTCOME_CRCERROR:
|
|---|
| 84 | return "CRC error";
|
|---|
| 85 | case USB_OUTCOME_BABBLE:
|
|---|
| 86 | return "babble";
|
|---|
| 87 | default:
|
|---|
| 88 | return "unknown";
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Create necessary phones for communicating with HCD.
|
|---|
| 93 | * This function wraps following calls:
|
|---|
| 94 | * -# open <code>/dev/usb/<i>hcd_path</i></code> for reading
|
|---|
| 95 | * -# access phone of file opened in previous step
|
|---|
| 96 | * -# return the (outgoing) phone
|
|---|
| 97 | *
|
|---|
| 98 | * @warning This function is wrapper for several actions and therefore
|
|---|
| 99 | * it is not possible - in case of error - to determine at which point
|
|---|
| 100 | * error occurred.
|
|---|
| 101 | *
|
|---|
| 102 | * @param hcd_path HCD identification under devfs
|
|---|
| 103 | * (without <code>/dev/usb/</code>).
|
|---|
| 104 | * @return Phone for communicating with HCD or error code from errno.h.
|
|---|
| 105 | */
|
|---|
| 106 | int usb_hcd_connect(const char * hcd_path)
|
|---|
| 107 | {
|
|---|
| 108 | char dev_path[DEVMAP_NAME_MAXLEN + 1];
|
|---|
| 109 | snprintf(dev_path, DEVMAP_NAME_MAXLEN,
|
|---|
| 110 | "/dev/%s/%s", NAMESPACE, hcd_path);
|
|---|
| 111 |
|
|---|
| 112 | int fd = open(dev_path, O_RDONLY);
|
|---|
| 113 | if (fd < 0) {
|
|---|
| 114 | return fd;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | int hcd_phone = fd_phone(fd);
|
|---|
| 118 |
|
|---|
| 119 | if (hcd_phone < 0) {
|
|---|
| 120 | return hcd_phone;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return hcd_phone;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Send data to HCD.
|
|---|
| 127 | *
|
|---|
| 128 | * @param phone Opened phone to HCD.
|
|---|
| 129 | * @param method Method used for calling.
|
|---|
| 130 | * @param target Target device.
|
|---|
| 131 | * @param buffer Data buffer (NULL to skip data transfer phase).
|
|---|
| 132 | * @param size Buffer size (must be zero when @p buffer is NULL).
|
|---|
| 133 | * @param handle Storage for transaction handle (cannot be NULL).
|
|---|
| 134 | * @return Error status.
|
|---|
| 135 | * @retval EINVAL Invalid parameter.
|
|---|
| 136 | * @retval ENOMEM Not enough memory to complete the operation.
|
|---|
| 137 | */
|
|---|
| 138 | static int async_send_buffer(int phone, int method,
|
|---|
| 139 | usb_target_t target,
|
|---|
| 140 | void *buffer, size_t size,
|
|---|
| 141 | usb_handle_t *handle)
|
|---|
| 142 | {
|
|---|
| 143 | if (phone < 0) {
|
|---|
| 144 | return EINVAL;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if ((buffer == NULL) && (size > 0)) {
|
|---|
| 148 | return EINVAL;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | if (handle == NULL) {
|
|---|
| 152 | return EINVAL;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | transfer_info_t *transfer
|
|---|
| 156 | = (transfer_info_t *) malloc(sizeof(transfer_info_t));
|
|---|
| 157 | if (transfer == NULL) {
|
|---|
| 158 | return ENOMEM;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | transfer->size_transferred = NULL;
|
|---|
| 162 | transfer->buffer = NULL;
|
|---|
| 163 | transfer->size = 0;
|
|---|
| 164 | transfer->phone = phone;
|
|---|
| 165 |
|
|---|
| 166 | int rc;
|
|---|
| 167 |
|
|---|
| 168 | transfer->request = async_send_3(phone,
|
|---|
| 169 | method,
|
|---|
| 170 | target.address, target.endpoint,
|
|---|
| 171 | size,
|
|---|
| 172 | &transfer->reply);
|
|---|
| 173 |
|
|---|
| 174 | if (size > 0) {
|
|---|
| 175 | rc = async_data_write_start(phone, buffer, size);
|
|---|
| 176 | if (rc != EOK) {
|
|---|
| 177 | async_wait_for(transfer->request, NULL);
|
|---|
| 178 | return rc;
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | *handle = (usb_handle_t) transfer;
|
|---|
| 183 |
|
|---|
| 184 | return EOK;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /** Prepare data retrieval.
|
|---|
| 188 | *
|
|---|
| 189 | * @param phone Opened phone to HCD.
|
|---|
| 190 | * @param method Method used for calling.
|
|---|
| 191 | * @param target Target device.
|
|---|
| 192 | * @param buffer Buffer where to store retrieved data
|
|---|
| 193 | * (NULL to skip data transfer phase).
|
|---|
| 194 | * @param size Buffer size (must be zero when @p buffer is NULL).
|
|---|
| 195 | * @param actual_size Storage where actual number of bytes transferred will
|
|---|
| 196 | * be stored.
|
|---|
| 197 | * @param handle Storage for transaction handle (cannot be NULL).
|
|---|
| 198 | * @return Error status.
|
|---|
| 199 | * @retval EINVAL Invalid parameter.
|
|---|
| 200 | * @retval ENOMEM Not enough memory to complete the operation.
|
|---|
| 201 | */
|
|---|
| 202 | static int async_recv_buffer(int phone, int method,
|
|---|
| 203 | usb_target_t target,
|
|---|
| 204 | void *buffer, size_t size, size_t *actual_size,
|
|---|
| 205 | usb_handle_t *handle)
|
|---|
| 206 | {
|
|---|
| 207 | if (phone < 0) {
|
|---|
| 208 | return EINVAL;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | if ((buffer == NULL) && (size > 0)) {
|
|---|
| 212 | return EINVAL;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | if (handle == NULL) {
|
|---|
| 216 | return EINVAL;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | transfer_info_t *transfer
|
|---|
| 220 | = (transfer_info_t *) malloc(sizeof(transfer_info_t));
|
|---|
| 221 | if (transfer == NULL) {
|
|---|
| 222 | return ENOMEM;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | transfer->size_transferred = actual_size;
|
|---|
| 226 | transfer->buffer = buffer;
|
|---|
| 227 | transfer->size = size;
|
|---|
| 228 | transfer->phone = phone;
|
|---|
| 229 |
|
|---|
| 230 | transfer->request = async_send_3(phone,
|
|---|
| 231 | method,
|
|---|
| 232 | target.address, target.endpoint,
|
|---|
| 233 | size,
|
|---|
| 234 | &transfer->reply);
|
|---|
| 235 |
|
|---|
| 236 | *handle = (usb_handle_t) transfer;
|
|---|
| 237 |
|
|---|
| 238 | return EOK;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /** Read buffer from HCD.
|
|---|
| 242 | *
|
|---|
| 243 | * @param phone Opened phone to HCD.
|
|---|
| 244 | * @param hash Buffer hash (obtained after completing IN transaction).
|
|---|
| 245 | * @param buffer Buffer where to store data data.
|
|---|
| 246 | * @param size Buffer size.
|
|---|
| 247 | * @param actual_size Storage where actual number of bytes transferred will
|
|---|
| 248 | * be stored.
|
|---|
| 249 | * @return Error status.
|
|---|
| 250 | */
|
|---|
| 251 | static int read_buffer_in(int phone, ipcarg_t hash,
|
|---|
| 252 | void *buffer, size_t size, size_t *actual_size)
|
|---|
| 253 | {
|
|---|
| 254 | ipc_call_t answer_data;
|
|---|
| 255 | ipcarg_t answer_rc;
|
|---|
| 256 | aid_t req;
|
|---|
| 257 | int rc;
|
|---|
| 258 |
|
|---|
| 259 | req = async_send_1(phone,
|
|---|
| 260 | IPC_M_USB_HCD_GET_BUFFER_ASYNC,
|
|---|
| 261 | hash,
|
|---|
| 262 | &answer_data);
|
|---|
| 263 |
|
|---|
| 264 | rc = async_data_read_start(phone, buffer, size);
|
|---|
| 265 | if (rc != EOK) {
|
|---|
| 266 | async_wait_for(req, NULL);
|
|---|
| 267 | return EINVAL;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | async_wait_for(req, &answer_rc);
|
|---|
| 271 | rc = (int)answer_rc;
|
|---|
| 272 |
|
|---|
| 273 | if (rc != EOK) {
|
|---|
| 274 | return rc;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | *actual_size = IPC_GET_ARG1(answer_data);
|
|---|
| 278 |
|
|---|
| 279 | return EOK;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | /** Blocks caller until given USB transaction is finished.
|
|---|
| 283 | * After the transaction is finished, the user can access all output data
|
|---|
| 284 | * given to initial call function.
|
|---|
| 285 | *
|
|---|
| 286 | * @param handle Transaction handle.
|
|---|
| 287 | * @return Error status.
|
|---|
| 288 | * @retval EOK No error.
|
|---|
| 289 | * @retval EBADMEM Invalid handle.
|
|---|
| 290 | * @retval ENOENT Data buffer associated with transaction does not exist.
|
|---|
| 291 | */
|
|---|
| 292 | int usb_hcd_async_wait_for(usb_handle_t handle)
|
|---|
| 293 | {
|
|---|
| 294 | if (handle == 0) {
|
|---|
| 295 | return EBADMEM;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | int rc = EOK;
|
|---|
| 299 |
|
|---|
| 300 | transfer_info_t *transfer = (transfer_info_t *) handle;
|
|---|
| 301 |
|
|---|
| 302 | ipcarg_t answer_rc;
|
|---|
| 303 | async_wait_for(transfer->request, &answer_rc);
|
|---|
| 304 |
|
|---|
| 305 | if (answer_rc != EOK) {
|
|---|
| 306 | rc = (int) answer_rc;
|
|---|
| 307 | goto leave;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /*
|
|---|
| 311 | * If the buffer is not NULL, we must accept some data.
|
|---|
| 312 | */
|
|---|
| 313 | if ((transfer->buffer != NULL) && (transfer->size > 0)) {
|
|---|
| 314 | /*
|
|---|
| 315 | * The buffer hash identifies the data on the server
|
|---|
| 316 | * side.
|
|---|
| 317 | * We will use it when actually reading-in the data.
|
|---|
| 318 | */
|
|---|
| 319 | ipcarg_t buffer_hash = IPC_GET_ARG1(transfer->reply);
|
|---|
| 320 | if (buffer_hash == 0) {
|
|---|
| 321 | rc = ENOENT;
|
|---|
| 322 | goto leave;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | size_t actual_size;
|
|---|
| 326 | rc = read_buffer_in(transfer->phone, buffer_hash,
|
|---|
| 327 | transfer->buffer, transfer->size, &actual_size);
|
|---|
| 328 |
|
|---|
| 329 | if (rc != EOK) {
|
|---|
| 330 | goto leave;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | if (transfer->size_transferred) {
|
|---|
| 334 | *(transfer->size_transferred) = actual_size;
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | leave:
|
|---|
| 339 | free(transfer);
|
|---|
| 340 |
|
|---|
| 341 | return rc;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | /** Send interrupt data to device. */
|
|---|
| 345 | int usb_hcd_async_transfer_interrupt_out(int hcd_phone,
|
|---|
| 346 | usb_target_t target,
|
|---|
| 347 | void *buffer, size_t size,
|
|---|
| 348 | usb_handle_t *handle)
|
|---|
| 349 | {
|
|---|
| 350 | return async_send_buffer(hcd_phone,
|
|---|
| 351 | IPC_M_USB_HCD_INTERRUPT_OUT_ASYNC,
|
|---|
| 352 | target,
|
|---|
| 353 | buffer, size,
|
|---|
| 354 | handle);
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /** Request interrupt data from device. */
|
|---|
| 358 | int usb_hcd_async_transfer_interrupt_in(int hcd_phone,
|
|---|
| 359 | usb_target_t target,
|
|---|
| 360 | void *buffer, size_t size, size_t *actual_size,
|
|---|
| 361 | usb_handle_t *handle)
|
|---|
| 362 | {
|
|---|
| 363 | return async_recv_buffer(hcd_phone,
|
|---|
| 364 | IPC_M_USB_HCD_INTERRUPT_IN_ASYNC,
|
|---|
| 365 | target,
|
|---|
| 366 | buffer, size, actual_size,
|
|---|
| 367 | handle);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /** Start WRITE control transfer. */
|
|---|
| 371 | int usb_hcd_async_transfer_control_write_setup(int hcd_phone,
|
|---|
| 372 | usb_target_t target,
|
|---|
| 373 | void *buffer, size_t size,
|
|---|
| 374 | usb_handle_t *handle)
|
|---|
| 375 | {
|
|---|
| 376 | return async_send_buffer(hcd_phone,
|
|---|
| 377 | IPC_M_USB_HCD_CONTROL_WRITE_SETUP_ASYNC,
|
|---|
| 378 | target,
|
|---|
| 379 | buffer, size,
|
|---|
| 380 | handle);
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | /** Send data during WRITE control transfer. */
|
|---|
| 384 | int usb_hcd_async_transfer_control_write_data(int hcd_phone,
|
|---|
| 385 | usb_target_t target,
|
|---|
| 386 | void *buffer, size_t size,
|
|---|
| 387 | usb_handle_t *handle)
|
|---|
| 388 | {
|
|---|
| 389 | return async_send_buffer(hcd_phone,
|
|---|
| 390 | IPC_M_USB_HCD_CONTROL_WRITE_DATA_ASYNC,
|
|---|
| 391 | target,
|
|---|
| 392 | buffer, size,
|
|---|
| 393 | handle);
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /** Terminate WRITE control transfer. */
|
|---|
| 397 | int usb_hcd_async_transfer_control_write_status(int hcd_phone,
|
|---|
| 398 | usb_target_t target,
|
|---|
| 399 | usb_handle_t *handle)
|
|---|
| 400 | {
|
|---|
| 401 | return async_recv_buffer(hcd_phone,
|
|---|
| 402 | IPC_M_USB_HCD_CONTROL_WRITE_STATUS_ASYNC,
|
|---|
| 403 | target,
|
|---|
| 404 | NULL, 0, NULL,
|
|---|
| 405 | handle);
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /** Start READ control transfer. */
|
|---|
| 409 | int usb_hcd_async_transfer_control_read_setup(int hcd_phone,
|
|---|
| 410 | usb_target_t target,
|
|---|
| 411 | void *buffer, size_t size,
|
|---|
| 412 | usb_handle_t *handle)
|
|---|
| 413 | {
|
|---|
| 414 | return async_send_buffer(hcd_phone,
|
|---|
| 415 | IPC_M_USB_HCD_CONTROL_READ_SETUP_ASYNC,
|
|---|
| 416 | target,
|
|---|
| 417 | buffer, size,
|
|---|
| 418 | handle);
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | /** Request data during READ control transfer. */
|
|---|
| 422 | int usb_hcd_async_transfer_control_read_data(int hcd_phone,
|
|---|
| 423 | usb_target_t target,
|
|---|
| 424 | void *buffer, size_t size, size_t *actual_size,
|
|---|
| 425 | usb_handle_t *handle)
|
|---|
| 426 | {
|
|---|
| 427 | return async_recv_buffer(hcd_phone,
|
|---|
| 428 | IPC_M_USB_HCD_CONTROL_READ_DATA_ASYNC,
|
|---|
| 429 | target,
|
|---|
| 430 | buffer, size, actual_size,
|
|---|
| 431 | handle);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | /** Terminate READ control transfer. */
|
|---|
| 435 | int usb_hcd_async_transfer_control_read_status(int hcd_phone,
|
|---|
| 436 | usb_target_t target,
|
|---|
| 437 | usb_handle_t *handle)
|
|---|
| 438 | {
|
|---|
| 439 | return async_send_buffer(hcd_phone,
|
|---|
| 440 | IPC_M_USB_HCD_CONTROL_READ_STATUS_ASYNC,
|
|---|
| 441 | target,
|
|---|
| 442 | NULL, 0,
|
|---|
| 443 | handle);
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /**
|
|---|
| 447 | * @}
|
|---|
| 448 | */
|
|---|