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

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

usb: add support for packing target_t type into uint32_t

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