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

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

Doxygen and some minor code-style changes

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