| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 usb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <usb/debug.h>
|
|---|
| 36 |
|
|---|
| 37 | #include "transfer_descriptor.h"
|
|---|
| 38 | #include "utils/malloc32.h"
|
|---|
| 39 |
|
|---|
| 40 | void transfer_descriptor_init(transfer_descriptor_t *instance,
|
|---|
| 41 | int error_count, size_t size, bool toggle, bool isochronous,
|
|---|
| 42 | usb_target_t target, int pid, void *buffer, transfer_descriptor_t *next)
|
|---|
| 43 | {
|
|---|
| 44 | assert(instance);
|
|---|
| 45 |
|
|---|
| 46 | instance->next = 0
|
|---|
| 47 | | LINK_POINTER_VERTICAL_FLAG
|
|---|
| 48 | | ((next != NULL) ? addr_to_phys(next) : LINK_POINTER_TERMINATE_FLAG);
|
|---|
| 49 |
|
|---|
| 50 | instance->status = 0
|
|---|
| 51 | | ((error_count & TD_STATUS_ERROR_COUNT_MASK) << TD_STATUS_ERROR_COUNT_POS)
|
|---|
| 52 | | TD_STATUS_ERROR_ACTIVE;
|
|---|
| 53 |
|
|---|
| 54 | assert(size < 1024);
|
|---|
| 55 | instance->device = 0
|
|---|
| 56 | | (((size - 1) & TD_DEVICE_MAXLEN_MASK) << TD_DEVICE_MAXLEN_POS)
|
|---|
| 57 | | (toggle ? TD_DEVICE_DATA_TOGGLE_ONE_FLAG : 0)
|
|---|
| 58 | | ((target.address & TD_DEVICE_ADDRESS_MASK) << TD_DEVICE_ADDRESS_POS)
|
|---|
| 59 | | ((target.endpoint & TD_DEVICE_ENDPOINT_MASK) << TD_DEVICE_ENDPOINT_POS)
|
|---|
| 60 | | ((pid & TD_DEVICE_PID_MASK) << TD_DEVICE_PID_POS);
|
|---|
| 61 |
|
|---|
| 62 | instance->buffer_ptr = 0;
|
|---|
| 63 |
|
|---|
| 64 | if (size) {
|
|---|
| 65 | instance->buffer_ptr = (uintptr_t)addr_to_phys(buffer);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | usb_log_info("Created TD: %X:%X:%X:%X(%p).\n",
|
|---|
| 69 | instance->next, instance->status, instance->device,
|
|---|
| 70 | instance->buffer_ptr, buffer);
|
|---|
| 71 | #if 0
|
|---|
| 72 | if (size) {
|
|---|
| 73 | unsigned char * buff = buffer;
|
|---|
| 74 | uhci_print_verbose("TD Buffer dump(%p-%dB): ", buffer, size);
|
|---|
| 75 | unsigned i = 0;
|
|---|
| 76 | /* TODO: Verbose? */
|
|---|
| 77 | for (; i < size; ++i) {
|
|---|
| 78 | printf((i & 1) ? "%x " : "%x", buff[i]);
|
|---|
| 79 | }
|
|---|
| 80 | printf("\n");
|
|---|
| 81 | }
|
|---|
| 82 | #endif
|
|---|
| 83 | }
|
|---|
| 84 | /*----------------------------------------------------------------------------*/
|
|---|
| 85 | int transfer_descriptor_status(transfer_descriptor_t *instance)
|
|---|
| 86 | {
|
|---|
| 87 | assert(instance);
|
|---|
| 88 |
|
|---|
| 89 | if ((instance->status & TD_STATUS_ERROR_STALLED) != 0)
|
|---|
| 90 | return EIO;
|
|---|
| 91 |
|
|---|
| 92 | if ((instance->status & TD_STATUS_ERROR_CRC) != 0)
|
|---|
| 93 | return EAGAIN;
|
|---|
| 94 |
|
|---|
| 95 | if ((instance->status & TD_STATUS_ERROR_BUFFER) != 0)
|
|---|
| 96 | return EAGAIN;
|
|---|
| 97 |
|
|---|
| 98 | if ((instance->status & TD_STATUS_ERROR_BABBLE) != 0)
|
|---|
| 99 | return EIO;
|
|---|
| 100 |
|
|---|
| 101 | if ((instance->status & TD_STATUS_ERROR_NAK) != 0)
|
|---|
| 102 | return EAGAIN;
|
|---|
| 103 |
|
|---|
| 104 | if ((instance->status & TD_STATUS_ERROR_BIT_STUFF) != 0)
|
|---|
| 105 | return EAGAIN;
|
|---|
| 106 |
|
|---|
| 107 | return EOK;
|
|---|
| 108 | }
|
|---|
| 109 | /**
|
|---|
| 110 | * @}
|
|---|
| 111 | */
|
|---|