source: mainline/uspace/drv/bus/usb/uhci/batch.c@ 27873be

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

UHCI: rename function

  • Property mode set to 100644
File size: 14.6 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 "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
46static void batch_control_write(usb_transfer_batch_t *instance);
47static void batch_control_read(usb_transfer_batch_t *instance);
48
49static void batch_interrupt_in(usb_transfer_batch_t *instance);
50static void batch_interrupt_out(usb_transfer_batch_t *instance);
51
52static void batch_bulk_in(usb_transfer_batch_t *instance);
53static void batch_bulk_out(usb_transfer_batch_t *instance);
54
55static void batch_setup_control(usb_transfer_batch_t *batch)
56{
57 // TODO Find a better way to do this
58 if (batch->setup_buffer[0] & (1 << 7))
59 batch_control_read(batch);
60 else
61 batch_control_write(batch);
62}
63
64void (*batch_setup[4][3])(usb_transfer_batch_t*) =
65{
66 { NULL, NULL, batch_setup_control },
67 { NULL, NULL, NULL },
68 { batch_bulk_in, batch_bulk_out, NULL },
69 { batch_interrupt_in, batch_interrupt_out, NULL },
70};
71 // */
72/** UHCI specific data required for USB transfer */
73typedef struct uhci_transfer_batch {
74 /** Queue head
75 * This QH is used to maintain UHCI schedule structure and the element
76 * pointer points to the first TD of this batch.
77 */
78 qh_t *qh;
79 /** List of TDs needed for the transfer */
80 td_t *tds;
81 /** Number of TDs used by the transfer */
82 size_t td_count;
83 /** Data buffer, must be accessible by the UHCI hw */
84 void *device_buffer;
85} uhci_transfer_batch_t;
86/*----------------------------------------------------------------------------*/
87static void batch_control(usb_transfer_batch_t *instance,
88 usb_packet_id data_stage, usb_packet_id status_stage);
89static void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid);
90/*----------------------------------------------------------------------------*/
91/** Safely destructs uhci_transfer_batch_t structure
92 *
93 * @param[in] uhci_batch Instance to destroy.
94 */
95static void uhci_transfer_batch_dispose(void *uhci_batch)
96{
97 uhci_transfer_batch_t *instance = uhci_batch;
98 assert(instance);
99 free32(instance->device_buffer);
100 free(instance);
101}
102/*----------------------------------------------------------------------------*/
103/** Allocate memory and initialize internal data structure.
104 *
105 * @param[in] fun DDF function to pass to callback.
106 * @param[in] ep Communication target
107 * @param[in] buffer Data source/destination.
108 * @param[in] buffer_size Size of the buffer.
109 * @param[in] setup_buffer Setup data source (if not NULL)
110 * @param[in] setup_size Size of setup_buffer (should be always 8)
111 * @param[in] func_in function to call on inbound transfer completion
112 * @param[in] func_out function to call on outbound transfer completion
113 * @param[in] arg additional parameter to func_in or func_out
114 * @return Valid pointer if all structures were successfully created,
115 * NULL otherwise.
116 *
117 * Determines the number of needed transfer descriptors (TDs).
118 * Prepares a transport buffer (that is accessible by the hardware).
119 * Initializes parameters needed for the transfer and callback.
120 */
121int batch_init_uhci(usb_transfer_batch_t *batch)
122{
123#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
124 if (ptr == NULL) { \
125 usb_log_error(message); \
126 if (uhci_data) { \
127 uhci_transfer_batch_dispose(uhci_data); \
128 } \
129 return ENOMEM; \
130 } else (void)0
131
132 uhci_transfer_batch_t *uhci_data =
133 calloc(1, sizeof(uhci_transfer_batch_t));
134 CHECK_NULL_DISPOSE_RETURN(uhci_data,
135 "Failed to allocate UHCI batch.\n");
136
137 uhci_data->td_count =
138 (batch->buffer_size + batch->ep->max_packet_size - 1)
139 / batch->ep->max_packet_size;
140 if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
141 uhci_data->td_count += 2;
142 }
143
144 assert((sizeof(td_t) % 16) == 0);
145 const size_t total_size = (sizeof(td_t) * uhci_data->td_count)
146 + sizeof(qh_t) + batch->setup_size + batch->buffer_size;
147 uhci_data->device_buffer = malloc32(total_size);
148 CHECK_NULL_DISPOSE_RETURN(uhci_data->device_buffer,
149 "Failed to allocate UHCI buffer.\n");
150 bzero(uhci_data->device_buffer, total_size);
151
152 uhci_data->tds = uhci_data->device_buffer;
153 uhci_data->qh =
154 (uhci_data->device_buffer + (sizeof(td_t) * uhci_data->td_count));
155
156 qh_init(uhci_data->qh);
157 qh_set_element_td(uhci_data->qh, uhci_data->tds);
158
159 void *setup =
160 uhci_data->device_buffer + (sizeof(td_t) * uhci_data->td_count)
161 + sizeof(qh_t);
162 /* Copy SETUP packet data to device buffer */
163 memcpy(setup, batch->setup_buffer, batch->setup_size);
164 /* Set generic data buffer pointer */
165 batch->data_buffer = setup + batch->setup_size;
166 batch->private_data_dtor = uhci_transfer_batch_dispose;
167 batch->private_data = uhci_data;
168 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
169 " memory structures ready.\n", batch,
170 USB_TRANSFER_BATCH_ARGS(*batch));
171 assert(batch_setup[batch->ep->transfer_type][batch->ep->direction]);
172 batch_setup[batch->ep->transfer_type][batch->ep->direction](batch);
173
174 return EOK;
175}
176/*----------------------------------------------------------------------------*/
177/** Check batch TDs for activity.
178 *
179 * @param[in] instance Batch structure to use.
180 * @return False, if there is an active TD, true otherwise.
181 *
182 * Walk all TDs. Stop with false if there is an active one (it is to be
183 * processed). Stop with true if an error is found. Return true if the last TD
184 * is reached.
185 */
186bool batch_is_complete(usb_transfer_batch_t *instance)
187{
188 assert(instance);
189 uhci_transfer_batch_t *data = instance->private_data;
190 assert(data);
191
192 usb_log_debug2("Batch(%p) checking %zu transfer(s) for completion.\n",
193 instance, data->td_count);
194 instance->transfered_size = 0;
195 size_t i = 0;
196 for (;i < data->td_count; ++i) {
197 if (td_is_active(&data->tds[i])) {
198 return false;
199 }
200
201 instance->error = td_status(&data->tds[i]);
202 if (instance->error != EOK) {
203 usb_log_debug("Batch(%p) found error TD(%zu):%"
204 PRIx32 ".\n", instance, i, data->tds[i].status);
205 td_print_status(&data->tds[i]);
206
207 assert(instance->ep != NULL);
208 endpoint_toggle_set(instance->ep,
209 td_toggle(&data->tds[i]));
210 if (i > 0)
211 goto substract_ret;
212 return true;
213 }
214
215 instance->transfered_size += td_act_size(&data->tds[i]);
216 if (td_is_short(&data->tds[i]))
217 goto substract_ret;
218 }
219substract_ret:
220 instance->transfered_size -= instance->setup_size;
221 return true;
222}
223
224#define LOG_BATCH_INITIALIZED(batch, name) \
225 usb_log_debug2("Batch %p %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
226 (batch), (name), USB_TRANSFER_BATCH_ARGS(*(batch)))
227
228/*----------------------------------------------------------------------------*/
229/** Prepares control write transfer.
230 *
231 * @param[in] instance Batch structure to use.
232 *
233 * Uses generic control function with pids OUT and IN.
234 */
235static void batch_control_write(usb_transfer_batch_t *instance)
236{
237 assert(instance);
238 /* We are data out, we are supposed to provide data */
239 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
240 batch_control(instance, USB_PID_OUT, USB_PID_IN);
241 instance->next_step = usb_transfer_batch_call_out_and_dispose;
242 LOG_BATCH_INITIALIZED(instance, "control write");
243}
244/*----------------------------------------------------------------------------*/
245/** Prepares control read transfer.
246 *
247 * @param[in] instance Batch structure to use.
248 *
249 * Uses generic control with pids IN and OUT.
250 */
251static void batch_control_read(usb_transfer_batch_t *instance)
252{
253 assert(instance);
254 batch_control(instance, USB_PID_IN, USB_PID_OUT);
255 instance->next_step = usb_transfer_batch_call_in_and_dispose;
256 LOG_BATCH_INITIALIZED(instance, "control read");
257}
258/*----------------------------------------------------------------------------*/
259/** Prepare interrupt in transfer.
260 *
261 * @param[in] instance Batch structure to use.
262 *
263 * Data transfer with PID_IN.
264 */
265static void batch_interrupt_in(usb_transfer_batch_t *instance)
266{
267 assert(instance);
268 batch_data(instance, USB_PID_IN);
269 instance->next_step = usb_transfer_batch_call_in_and_dispose;
270 LOG_BATCH_INITIALIZED(instance, "interrupt in");
271}
272/*----------------------------------------------------------------------------*/
273/** Prepare interrupt out transfer.
274 *
275 * @param[in] instance Batch structure to use.
276 *
277 * Data transfer with PID_OUT.
278 */
279static void batch_interrupt_out(usb_transfer_batch_t *instance)
280{
281 assert(instance);
282 /* We are data out, we are supposed to provide data */
283 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
284 batch_data(instance, USB_PID_OUT);
285 instance->next_step = usb_transfer_batch_call_out_and_dispose;
286 LOG_BATCH_INITIALIZED(instance, "interrupt out");
287}
288/*----------------------------------------------------------------------------*/
289/** Prepare bulk in transfer.
290 *
291 * @param[in] instance Batch structure to use.
292 *
293 * Data transfer with PID_IN.
294 */
295static void batch_bulk_in(usb_transfer_batch_t *instance)
296{
297 assert(instance);
298 batch_data(instance, USB_PID_IN);
299 instance->next_step = usb_transfer_batch_call_in_and_dispose;
300 LOG_BATCH_INITIALIZED(instance, "bulk in");
301}
302/*----------------------------------------------------------------------------*/
303/** Prepare bulk out transfer.
304 *
305 * @param[in] instance Batch structure to use.
306 *
307 * Data transfer with PID_OUT.
308 */
309static void batch_bulk_out(usb_transfer_batch_t *instance)
310{
311 assert(instance);
312 /* We are data out, we are supposed to provide data */
313 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
314 batch_data(instance, USB_PID_OUT);
315 instance->next_step = usb_transfer_batch_call_out_and_dispose;
316 LOG_BATCH_INITIALIZED(instance, "bulk out");
317}
318/*----------------------------------------------------------------------------*/
319/** Prepare generic data transfer
320 *
321 * @param[in] instance Batch structure to use.
322 * @param[in] pid Pid to use for data transactions.
323 *
324 * Transactions with alternating toggle bit and supplied pid value.
325 * The last transfer is marked with IOC flag.
326 */
327static void batch_data(usb_transfer_batch_t *instance, usb_packet_id pid)
328{
329 assert(instance);
330 uhci_transfer_batch_t *data = instance->private_data;
331 assert(data);
332
333 const bool low_speed = instance->ep->speed == USB_SPEED_LOW;
334 int toggle = endpoint_toggle_get(instance->ep);
335 assert(toggle == 0 || toggle == 1);
336
337 size_t td = 0;
338 size_t remain_size = instance->buffer_size;
339 char *buffer = instance->data_buffer;
340 while (remain_size > 0) {
341 const size_t packet_size =
342 (instance->ep->max_packet_size > remain_size) ?
343 remain_size : instance->ep->max_packet_size;
344
345 td_t *next_td = (td + 1 < data->td_count)
346 ? &data->tds[td + 1] : NULL;
347
348
349 usb_target_t target =
350 { instance->ep->address, instance->ep->endpoint };
351
352 assert(td < data->td_count);
353 td_init(
354 &data->tds[td], DEFAULT_ERROR_COUNT, packet_size,
355 toggle, false, low_speed, target, pid, buffer, next_td);
356
357 ++td;
358 toggle = 1 - toggle;
359 buffer += packet_size;
360 assert(packet_size <= remain_size);
361 remain_size -= packet_size;
362 }
363 td_set_ioc(&data->tds[td - 1]);
364 endpoint_toggle_set(instance->ep, toggle);
365}
366/*----------------------------------------------------------------------------*/
367/** Prepare generic control transfer
368 *
369 * @param[in] instance Batch structure to use.
370 * @param[in] data_stage Pid to use for data tds.
371 * @param[in] status_stage Pid to use for data tds.
372 *
373 * Setup stage with toggle 0 and USB_PID_SETUP.
374 * Data stage with alternating toggle and pid supplied by parameter.
375 * Status stage with toggle 1 and pid supplied by parameter.
376 * The last transfer is marked with IOC.
377 */
378static void batch_control(usb_transfer_batch_t *instance,
379 usb_packet_id data_stage, usb_packet_id status_stage)
380{
381 assert(instance);
382 uhci_transfer_batch_t *data = instance->private_data;
383 assert(data);
384 assert(data->td_count >= 2);
385
386 const bool low_speed = instance->ep->speed == USB_SPEED_LOW;
387 const usb_target_t target =
388 { instance->ep->address, instance->ep->endpoint };
389
390 /* setup stage */
391 td_init(
392 data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, 0, false,
393 low_speed, target, USB_PID_SETUP, instance->setup_buffer,
394 &data->tds[1]);
395
396 /* data stage */
397 size_t td = 1;
398 unsigned toggle = 1;
399 size_t remain_size = instance->buffer_size;
400 char *buffer = instance->data_buffer;
401 while (remain_size > 0) {
402 const size_t packet_size =
403 (instance->ep->max_packet_size > remain_size) ?
404 remain_size : instance->ep->max_packet_size;
405
406 td_init(
407 &data->tds[td], DEFAULT_ERROR_COUNT, packet_size,
408 toggle, false, low_speed, target, data_stage,
409 buffer, &data->tds[td + 1]);
410
411 ++td;
412 toggle = 1 - toggle;
413 buffer += packet_size;
414 assert(td < data->td_count);
415 assert(packet_size <= remain_size);
416 remain_size -= packet_size;
417 }
418
419 /* status stage */
420 assert(td == data->td_count - 1);
421
422 td_init(
423 &data->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
424 target, status_stage, NULL, NULL);
425 td_set_ioc(&data->tds[td]);
426
427 usb_log_debug2("Control last TD status: %x.\n",
428 data->tds[td].status);
429}
430/*----------------------------------------------------------------------------*/
431/** Provides access to QH data structure.
432 *
433 * @param[in] instance Batch pointer to use.
434 * @return Pointer to the QH used by the batch.
435 */
436qh_t * batch_qh(usb_transfer_batch_t *instance)
437{
438 assert(instance);
439 uhci_transfer_batch_t *data = instance->private_data;
440 assert(data);
441 return data->qh;
442}
443/**
444 * @}
445 */
Note: See TracBrowser for help on using the repository browser.