source: mainline/uspace/drv/bus/usb/uhci/uhci_batch.c@ 790318e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 790318e was 790318e, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

uhci,ohci: Remove direction from initialization table.

  • Property mode set to 100644
File size: 12.0 KB
Line 
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 drvusbuhcihc
29 * @{
30 */
31/** @file
32 * @brief UHCI driver USB transfer 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 "uhci_batch.h"
41#include "transfer_list.h"
42#include "hw_struct/transfer_descriptor.h"
43#include "utils/malloc32.h"
44
45#define DEFAULT_ERROR_COUNT 3
46
47static void uhci_transfer_batch_dispose(uhci_transfer_batch_t *uhci_batch)
48{
49 if (uhci_batch) {
50 usb_transfer_batch_dispose(uhci_batch->usb_batch);
51 free32(uhci_batch->device_buffer);
52 free(uhci_batch);
53 }
54}
55/*----------------------------------------------------------------------------*/
56/** Safely destructs uhci_transfer_batch_t structure
57 *
58 * @param[in] uhci_batch Instance to destroy.
59 */
60void uhci_transfer_batch_call_dispose(uhci_transfer_batch_t *uhci_batch)
61{
62 assert(uhci_batch);
63 assert(uhci_batch->usb_batch);
64 usb_transfer_batch_finish(uhci_batch->usb_batch,
65 uhci_transfer_batch_data_buffer(uhci_batch),
66 uhci_batch->usb_batch->buffer_size);
67 uhci_transfer_batch_dispose(uhci_batch);
68}
69/*----------------------------------------------------------------------------*/
70static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t);
71/*----------------------------------------------------------------------------*/
72/** Allocate memory and initialize internal data structure.
73 *
74 * @param[in] fun DDF function to pass to callback.
75 * @param[in] ep Communication target
76 * @param[in] buffer Data source/destination.
77 * @param[in] buffer_size Size of the buffer.
78 * @param[in] setup_buffer Setup data source (if not NULL)
79 * @param[in] setup_size Size of setup_buffer (should be always 8)
80 * @param[in] func_in function to call on inbound transfer completion
81 * @param[in] func_out function to call on outbound transfer completion
82 * @param[in] arg additional parameter to func_in or func_out
83 * @return Valid pointer if all structures were successfully created,
84 * NULL otherwise.
85 *
86 * Determines the number of needed transfer descriptors (TDs).
87 * Prepares a transport buffer (that is accessible by the hardware).
88 * Initializes parameters needed for the transfer and callback.
89 */
90uhci_transfer_batch_t * uhci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
91{
92 assert((sizeof(td_t) % 16) == 0);
93#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
94 if (ptr == NULL) { \
95 usb_log_error(message); \
96 uhci_transfer_batch_dispose(uhci_batch); \
97 return NULL; \
98 } else (void)0
99
100 uhci_transfer_batch_t *uhci_batch =
101 calloc(1, sizeof(uhci_transfer_batch_t));
102 CHECK_NULL_DISPOSE_RETURN(uhci_batch,
103 "Failed to allocate UHCI batch.\n");
104 link_initialize(&uhci_batch->link);
105 uhci_batch->td_count =
106 (usb_batch->buffer_size + usb_batch->ep->max_packet_size - 1)
107 / usb_batch->ep->max_packet_size;
108 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
109 uhci_batch->td_count += 2;
110 }
111
112 const size_t total_size = (sizeof(td_t) * uhci_batch->td_count)
113 + sizeof(qh_t) + usb_batch->setup_size + usb_batch->buffer_size;
114 uhci_batch->device_buffer = malloc32(total_size);
115 CHECK_NULL_DISPOSE_RETURN(uhci_batch->device_buffer,
116 "Failed to allocate UHCI buffer.\n");
117 bzero(uhci_batch->device_buffer, total_size);
118
119 uhci_batch->tds = uhci_batch->device_buffer;
120 uhci_batch->qh =
121 (uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count));
122
123 qh_init(uhci_batch->qh);
124 qh_set_element_td(uhci_batch->qh, &uhci_batch->tds[0]);
125
126 void *dest =
127 uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count)
128 + sizeof(qh_t);
129 /* Copy SETUP packet data to the device buffer */
130 memcpy(dest, usb_batch->setup_buffer, usb_batch->setup_size);
131 dest += usb_batch->setup_size;
132 /* Copy generic data unless they are provided by the device */
133 if (usb_batch->ep->direction != USB_DIRECTION_IN) {
134 memcpy(dest, usb_batch->buffer, usb_batch->buffer_size);
135 }
136 uhci_batch->usb_batch = usb_batch;
137 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
138 " memory structures ready.\n", usb_batch,
139 USB_TRANSFER_BATCH_ARGS(*usb_batch));
140
141 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
142
143 assert(batch_setup[usb_batch->ep->transfer_type]);
144 batch_setup[usb_batch->ep->transfer_type](uhci_batch, dir);
145
146 return uhci_batch;
147}
148/*----------------------------------------------------------------------------*/
149/** Check batch TDs for activity.
150 *
151 * @param[in] uhci_batch Batch structure to use.
152 * @return False, if there is an active TD, true otherwise.
153 *
154 * Walk all TDs. Stop with false if there is an active one (it is to be
155 * processed). Stop with true if an error is found. Return true if the last TD
156 * is reached.
157 */
158bool uhci_transfer_batch_is_complete(uhci_transfer_batch_t *uhci_batch)
159{
160 assert(uhci_batch);
161 assert(uhci_batch->usb_batch);
162
163 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
164 " checking %zu transfer(s) for completion.\n",
165 uhci_batch->usb_batch,
166 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
167 uhci_batch->td_count);
168 uhci_batch->usb_batch->transfered_size = 0;
169 size_t i = 0;
170 for (;i < uhci_batch->td_count; ++i) {
171 if (td_is_active(&uhci_batch->tds[i])) {
172 return false;
173 }
174
175 uhci_batch->usb_batch->error = td_status(&uhci_batch->tds[i]);
176 if (uhci_batch->usb_batch->error != EOK) {
177 assert(uhci_batch->usb_batch->ep != NULL);
178
179 usb_log_debug("Batch(%p) found error TD(%zu):%"
180 PRIx32 ".\n", uhci_batch->usb_batch, i,
181 uhci_batch->tds[i].status);
182 td_print_status(&uhci_batch->tds[i]);
183
184 endpoint_toggle_set(uhci_batch->usb_batch->ep,
185 td_toggle(&uhci_batch->tds[i]));
186 if (i > 0)
187 goto substract_ret;
188 return true;
189 }
190
191 uhci_batch->usb_batch->transfered_size
192 += td_act_size(&uhci_batch->tds[i]);
193 if (td_is_short(&uhci_batch->tds[i]))
194 goto substract_ret;
195 }
196substract_ret:
197 uhci_batch->usb_batch->transfered_size
198 -= uhci_batch->usb_batch->setup_size;
199 return true;
200}
201/*----------------------------------------------------------------------------*/
202static const usb_packet_id direction_pids[] = {
203 [USB_DIRECTION_IN] = USB_PID_IN,
204 [USB_DIRECTION_OUT] = USB_PID_OUT,
205};
206/*----------------------------------------------------------------------------*/
207/** Prepare generic data transfer
208 *
209 * @param[in] uhci_batch Batch structure to use.
210 * @param[in] dir Communication direction.
211 *
212 * Transactions with alternating toggle bit and supplied pid value.
213 * The last transfer is marked with IOC flag.
214 */
215static void batch_data(uhci_transfer_batch_t *uhci_batch, usb_direction_t dir)
216{
217 assert(uhci_batch);
218 assert(uhci_batch->usb_batch);
219 assert(uhci_batch->usb_batch->ep);
220 assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
221
222
223 const usb_packet_id pid = direction_pids[dir];
224 const bool low_speed =
225 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
226 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
227 const usb_target_t target = {{
228 uhci_batch->usb_batch->ep->address,
229 uhci_batch->usb_batch->ep->endpoint }};
230
231 int toggle = endpoint_toggle_get(uhci_batch->usb_batch->ep);
232 assert(toggle == 0 || toggle == 1);
233
234 size_t td = 0;
235 size_t remain_size = uhci_batch->usb_batch->buffer_size;
236 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
237
238 while (remain_size > 0) {
239 const size_t packet_size =
240 (remain_size < mps) ? remain_size : mps;
241
242 const td_t *next_td = (td + 1 < uhci_batch->td_count)
243 ? &uhci_batch->tds[td + 1] : NULL;
244
245 assert(td < uhci_batch->td_count);
246 td_init(
247 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
248 toggle, false, low_speed, target, pid, buffer, next_td);
249
250 ++td;
251 toggle = 1 - toggle;
252 buffer += packet_size;
253 remain_size -= packet_size;
254 }
255 td_set_ioc(&uhci_batch->tds[td - 1]);
256 endpoint_toggle_set(uhci_batch->usb_batch->ep, toggle);
257 usb_log_debug2(
258 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
259 uhci_batch->usb_batch,
260 usb_str_transfer_type(uhci_batch->usb_batch->ep->transfer_type),
261 usb_str_direction(uhci_batch->usb_batch->ep->direction),
262 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch));
263}
264/*----------------------------------------------------------------------------*/
265/** Prepare generic control transfer
266 *
267 * @param[in] uhci_batch Batch structure to use.
268 * @param[in] dir Communication direction.
269 *
270 * Setup stage with toggle 0 and USB_PID_SETUP.
271 * Data stage with alternating toggle and pid determined by the communication
272 * direction.
273 * Status stage with toggle 1 and pid determined by the communication direction.
274 * The last transfer is marked with IOC.
275 */
276static void batch_control(uhci_transfer_batch_t *uhci_batch, usb_direction_t dir)
277{
278 assert(uhci_batch);
279 assert(uhci_batch->usb_batch);
280 assert(uhci_batch->usb_batch->ep);
281 assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
282 assert(uhci_batch->td_count >= 2);
283 static const usb_packet_id status_stage_pids[] = {
284 [USB_DIRECTION_IN] = USB_PID_OUT,
285 [USB_DIRECTION_OUT] = USB_PID_IN,
286 };
287
288 const usb_packet_id data_stage_pid = direction_pids[dir];
289 const usb_packet_id status_stage_pid = status_stage_pids[dir];
290 const bool low_speed =
291 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
292 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
293 const usb_target_t target = {{
294 uhci_batch->usb_batch->ep->address,
295 uhci_batch->usb_batch->ep->endpoint }};
296
297 /* setup stage */
298 td_init(
299 &uhci_batch->tds[0], DEFAULT_ERROR_COUNT,
300 uhci_batch->usb_batch->setup_size, 0, false,
301 low_speed, target, USB_PID_SETUP,
302 uhci_transfer_batch_setup_buffer(uhci_batch), &uhci_batch->tds[1]);
303
304 /* data stage */
305 size_t td = 1;
306 unsigned toggle = 1;
307 size_t remain_size = uhci_batch->usb_batch->buffer_size;
308 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
309
310 while (remain_size > 0) {
311 const size_t packet_size =
312 (remain_size < mps) ? remain_size : mps;
313
314 td_init(
315 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
316 toggle, false, low_speed, target, data_stage_pid,
317 buffer, &uhci_batch->tds[td + 1]);
318
319 ++td;
320 toggle = 1 - toggle;
321 buffer += packet_size;
322 remain_size -= packet_size;
323 assert(td < uhci_batch->td_count);
324 }
325
326 /* status stage */
327 assert(td == uhci_batch->td_count - 1);
328
329 td_init(
330 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
331 target, status_stage_pid, NULL, NULL);
332 td_set_ioc(&uhci_batch->tds[td]);
333
334 usb_log_debug2("Control last TD status: %x.\n",
335 uhci_batch->tds[td].status);
336}
337/*----------------------------------------------------------------------------*/
338static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t) =
339{
340 [USB_TRANSFER_CONTROL] = batch_control,
341 [USB_TRANSFER_BULK] = batch_data,
342 [USB_TRANSFER_INTERRUPT] = batch_data,
343 [USB_TRANSFER_ISOCHRONOUS] = NULL,
344};
345/**
346 * @}
347 */
Note: See TracBrowser for help on using the repository browser.