source: mainline/uspace/drv/uhci-hcd/batch.c@ f58154c5

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

Cleanup, add normal and doxygen comments

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