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 <usb/debug.h>
|
---|
35 |
|
---|
36 | #include "transfer_descriptor.h"
|
---|
37 |
|
---|
38 | void transfer_descriptor_init(transfer_descriptor_t *instance,
|
---|
39 | int error_count, size_t size, bool isochronous, usb_target_t target,
|
---|
40 | int pid, void *buffer)
|
---|
41 | {
|
---|
42 | assert(instance);
|
---|
43 |
|
---|
44 | instance->next =
|
---|
45 | 0 | LINK_POINTER_TERMINATE_FLAG;
|
---|
46 |
|
---|
47 |
|
---|
48 | assert(size < 1024);
|
---|
49 | instance->status = 0
|
---|
50 | | ((error_count & TD_STATUS_ERROR_COUNT_MASK) << TD_STATUS_ERROR_COUNT_POS)
|
---|
51 | | TD_STATUS_ERROR_ACTIVE;
|
---|
52 |
|
---|
53 | instance->device = 0
|
---|
54 | | (((size - 1) & TD_DEVICE_MAXLEN_MASK) << TD_DEVICE_MAXLEN_POS)
|
---|
55 | | ((target.address & TD_DEVICE_ADDRESS_MASK) << TD_DEVICE_ADDRESS_POS)
|
---|
56 | | ((target.endpoint & TD_DEVICE_ENDPOINT_MASK) << TD_DEVICE_ENDPOINT_POS)
|
---|
57 | | ((pid & TD_DEVICE_PID_MASK) << TD_DEVICE_PID_POS);
|
---|
58 |
|
---|
59 | instance->buffer_ptr = 0;
|
---|
60 |
|
---|
61 | instance->next_va = NULL;
|
---|
62 | instance->callback = NULL;
|
---|
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 | static inline usb_transaction_outcome_t convert_outcome(uint32_t status)
|
---|
86 | {
|
---|
87 | /*TODO: refactor into something sane */
|
---|
88 | /*TODO: add additional usb_errors to usb_outcome_t */
|
---|
89 |
|
---|
90 | if (status & TD_STATUS_ERROR_STALLED)
|
---|
91 | return USB_OUTCOME_CRCERROR;
|
---|
92 |
|
---|
93 | if (status & TD_STATUS_ERROR_BUFFER)
|
---|
94 | return USB_OUTCOME_CRCERROR;
|
---|
95 |
|
---|
96 | if (status & TD_STATUS_ERROR_BABBLE)
|
---|
97 | return USB_OUTCOME_BABBLE;
|
---|
98 |
|
---|
99 | if (status & TD_STATUS_ERROR_NAK)
|
---|
100 | return USB_OUTCOME_CRCERROR;
|
---|
101 |
|
---|
102 | if (status & TD_STATUS_ERROR_CRC)
|
---|
103 | return USB_OUTCOME_CRCERROR;
|
---|
104 |
|
---|
105 | if (status & TD_STATUS_ERROR_BIT_STUFF)
|
---|
106 | return USB_OUTCOME_CRCERROR;
|
---|
107 |
|
---|
108 | assert((((status >> TD_STATUS_ERROR_POS) & TD_STATUS_ERROR_MASK)
|
---|
109 | | TD_STATUS_ERROR_RESERVED) == TD_STATUS_ERROR_RESERVED);
|
---|
110 | return USB_OUTCOME_OK;
|
---|
111 | }
|
---|
112 |
|
---|
113 | void transfer_descriptor_fini(transfer_descriptor_t *instance)
|
---|
114 | {
|
---|
115 | assert(instance);
|
---|
116 | callback_run(instance->callback,
|
---|
117 | convert_outcome(instance->status),
|
---|
118 | ((instance->status >> TD_STATUS_ACTLEN_POS) + 1) & TD_STATUS_ACTLEN_MASK
|
---|
119 | );
|
---|
120 | }
|
---|
121 | /**
|
---|
122 | * @}
|
---|
123 | */
|
---|