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 drvusbohci
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief OHCI driver USB transaction structure
|
---|
33 | */
|
---|
34 | #include <errno.h>
|
---|
35 | #include <str_error.h>
|
---|
36 |
|
---|
37 | #include <usb/usb.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 |
|
---|
40 | #include "batch.h"
|
---|
41 | #include "utils/malloc32.h"
|
---|
42 |
|
---|
43 | static void batch_call_in_and_dispose(usb_transfer_batch_t *instance);
|
---|
44 | static void batch_call_out_and_dispose(usb_transfer_batch_t *instance);
|
---|
45 |
|
---|
46 | #define DEFAULT_ERROR_COUNT 3
|
---|
47 | usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
|
---|
48 | char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
|
---|
49 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
50 | usbhc_iface_transfer_out_callback_t func_out, void *arg)
|
---|
51 | {
|
---|
52 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
---|
53 | if (ptr == NULL) { \
|
---|
54 | usb_log_error(message); \
|
---|
55 | if (instance) { \
|
---|
56 | batch_dispose(instance); \
|
---|
57 | } \
|
---|
58 | return NULL; \
|
---|
59 | } else (void)0
|
---|
60 |
|
---|
61 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
62 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
63 | "Failed to allocate batch instance.\n");
|
---|
64 | usb_target_t target =
|
---|
65 | { .address = ep->address, .endpoint = ep->endpoint };
|
---|
66 | usb_transfer_batch_init(instance, target, ep->transfer_type, ep->speed,
|
---|
67 | ep->max_packet_size, buffer, NULL, buffer_size, NULL, setup_size,
|
---|
68 | func_in, func_out, arg, fun, ep, NULL);
|
---|
69 |
|
---|
70 | if (buffer_size > 0) {
|
---|
71 | instance->transport_buffer = malloc32(buffer_size);
|
---|
72 | CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
|
---|
73 | "Failed to allocate device accessible buffer.\n");
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (setup_size > 0) {
|
---|
77 | instance->setup_buffer = malloc32(setup_size);
|
---|
78 | CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
|
---|
79 | "Failed to allocate device accessible setup buffer.\n");
|
---|
80 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | return instance;
|
---|
85 | }
|
---|
86 | /*----------------------------------------------------------------------------*/
|
---|
87 | void batch_dispose(usb_transfer_batch_t *instance)
|
---|
88 | {
|
---|
89 | assert(instance);
|
---|
90 | free32(instance->transport_buffer);
|
---|
91 | free32(instance->setup_buffer);
|
---|
92 | free(instance);
|
---|
93 | }
|
---|
94 | /*----------------------------------------------------------------------------*/
|
---|
95 | void batch_control_write(usb_transfer_batch_t *instance)
|
---|
96 | {
|
---|
97 | assert(instance);
|
---|
98 | /* We are data out, we are supposed to provide data */
|
---|
99 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
100 | instance->buffer_size);
|
---|
101 | instance->next_step = batch_call_out_and_dispose;
|
---|
102 | /* TODO: implement */
|
---|
103 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
104 | }
|
---|
105 | /*----------------------------------------------------------------------------*/
|
---|
106 | void batch_control_read(usb_transfer_batch_t *instance)
|
---|
107 | {
|
---|
108 | assert(instance);
|
---|
109 | instance->next_step = batch_call_in_and_dispose;
|
---|
110 | /* TODO: implement */
|
---|
111 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
---|
112 | }
|
---|
113 | /*----------------------------------------------------------------------------*/
|
---|
114 | void batch_interrupt_in(usb_transfer_batch_t *instance)
|
---|
115 | {
|
---|
116 | assert(instance);
|
---|
117 | instance->direction = USB_DIRECTION_IN;
|
---|
118 | instance->next_step = batch_call_in_and_dispose;
|
---|
119 | /* TODO: implement */
|
---|
120 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
121 | }
|
---|
122 | /*----------------------------------------------------------------------------*/
|
---|
123 | void batch_interrupt_out(usb_transfer_batch_t *instance)
|
---|
124 | {
|
---|
125 | assert(instance);
|
---|
126 | instance->direction = USB_DIRECTION_OUT;
|
---|
127 | /* We are data out, we are supposed to provide data */
|
---|
128 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
129 | instance->buffer_size);
|
---|
130 | instance->next_step = batch_call_out_and_dispose;
|
---|
131 | /* TODO: implement */
|
---|
132 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
133 | }
|
---|
134 | /*----------------------------------------------------------------------------*/
|
---|
135 | void batch_bulk_in(usb_transfer_batch_t *instance)
|
---|
136 | {
|
---|
137 | assert(instance);
|
---|
138 | instance->direction = USB_DIRECTION_IN;
|
---|
139 | instance->next_step = batch_call_in_and_dispose;
|
---|
140 | /* TODO: implement */
|
---|
141 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
142 | }
|
---|
143 | /*----------------------------------------------------------------------------*/
|
---|
144 | void batch_bulk_out(usb_transfer_batch_t *instance)
|
---|
145 | {
|
---|
146 | assert(instance);
|
---|
147 | instance->direction = USB_DIRECTION_IN;
|
---|
148 | instance->next_step = batch_call_in_and_dispose;
|
---|
149 | /* TODO: implement */
|
---|
150 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
151 | }
|
---|
152 | /*----------------------------------------------------------------------------*/
|
---|
153 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
154 | *
|
---|
155 | * @param[in] instance Batch structure to use.
|
---|
156 | */
|
---|
157 | void batch_call_in_and_dispose(usb_transfer_batch_t *instance)
|
---|
158 | {
|
---|
159 | assert(instance);
|
---|
160 | usb_transfer_batch_call_in(instance);
|
---|
161 | batch_dispose(instance);
|
---|
162 | }
|
---|
163 | /*----------------------------------------------------------------------------*/
|
---|
164 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
165 | *
|
---|
166 | * @param[in] instance Batch structure to use.
|
---|
167 | */
|
---|
168 | void batch_call_out_and_dispose(usb_transfer_batch_t *instance)
|
---|
169 | {
|
---|
170 | assert(instance);
|
---|
171 | usb_transfer_batch_call_out(instance);
|
---|
172 | batch_dispose(instance);
|
---|
173 | }
|
---|
174 | /**
|
---|
175 | * @}
|
---|
176 | */
|
---|