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

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

uhci,ohci: remove logging macro

  • 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 if 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 assert(
145 batch_setup[usb_batch->ep->transfer_type][usb_batch->ep->direction]);
146 batch_setup[usb_batch->ep->transfer_type][usb_batch->ep->direction](
147 uhci_batch);
148
149 return uhci_batch;
150}
151/*----------------------------------------------------------------------------*/
152/** Check batch TDs for activity.
153 *
154 * @param[in] uhci_batch Batch structure to use.
155 * @return False, if there is an active TD, true otherwise.
156 *
157 * Walk all TDs. Stop with false if there is an active one (it is to be
158 * processed). Stop with true if an error is found. Return true if the last TD
159 * is reached.
160 */
161bool uhci_transfer_batch_is_complete(uhci_transfer_batch_t *uhci_batch)
162{
163 assert(uhci_batch);
164 assert(uhci_batch->usb_batch);
165
166 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
167 " checking %zu transfer(s) for completion.\n",
168 uhci_batch->usb_batch,
169 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
170 uhci_batch->td_count);
171 uhci_batch->usb_batch->transfered_size = 0;
172 size_t i = 0;
173 for (;i < uhci_batch->td_count; ++i) {
174 if (td_is_active(&uhci_batch->tds[i])) {
175 return false;
176 }
177
178 uhci_batch->usb_batch->error = td_status(&uhci_batch->tds[i]);
179 if (uhci_batch->usb_batch->error != EOK) {
180 assert(uhci_batch->usb_batch->ep != NULL);
181
182 usb_log_debug("Batch(%p) found error TD(%zu):%"
183 PRIx32 ".\n", uhci_batch->usb_batch, i,
184 uhci_batch->tds[i].status);
185 td_print_status(&uhci_batch->tds[i]);
186
187 endpoint_toggle_set(uhci_batch->usb_batch->ep,
188 td_toggle(&uhci_batch->tds[i]));
189 if (i > 0)
190 goto substract_ret;
191 return true;
192 }
193
194 uhci_batch->usb_batch->transfered_size
195 += td_act_size(&uhci_batch->tds[i]);
196 if (td_is_short(&uhci_batch->tds[i]))
197 goto substract_ret;
198 }
199substract_ret:
200 uhci_batch->usb_batch->transfered_size
201 -= uhci_batch->usb_batch->setup_size;
202 return true;
203}
204/*----------------------------------------------------------------------------*/
205/** Prepare generic data transfer
206 *
207 * @param[in] uhci_batch Batch structure to use.
208 * @param[in] pid Pid to use for data transactions.
209 *
210 * Transactions with alternating toggle bit and supplied pid value.
211 * The last transfer is marked with IOC flag.
212 */
213static void batch_data(uhci_transfer_batch_t *uhci_batch)
214{
215 assert(uhci_batch);
216 assert(uhci_batch->usb_batch);
217 static const usb_packet_id pids[] = {
218 [USB_DIRECTION_IN] = USB_PID_IN,
219 [USB_DIRECTION_OUT] = USB_PID_OUT,
220 };
221 assert(uhci_batch->usb_batch->ep->direction == USB_DIRECTION_OUT ||
222 uhci_batch->usb_batch->ep->direction == USB_DIRECTION_IN);
223
224 const usb_packet_id pid = pids[uhci_batch->usb_batch->ep->direction];
225 const bool low_speed =
226 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
227 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
228 const usb_target_t target = {
229 uhci_batch->usb_batch->ep->address,
230 uhci_batch->usb_batch->ep->endpoint };
231
232 int toggle = endpoint_toggle_get(uhci_batch->usb_batch->ep);
233 assert(toggle == 0 || toggle == 1);
234
235 size_t td = 0;
236 size_t remain_size = uhci_batch->usb_batch->buffer_size;
237 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
238
239 while (remain_size > 0) {
240 const size_t packet_size =
241 (remain_size < mps) ? remain_size : mps;
242
243 const td_t *next_td = (td + 1 < uhci_batch->td_count)
244 ? &uhci_batch->tds[td + 1] : NULL;
245
246 assert(td < uhci_batch->td_count);
247 td_init(
248 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
249 toggle, false, low_speed, target, pid, buffer, next_td);
250
251 ++td;
252 toggle = 1 - toggle;
253 buffer += packet_size;
254 remain_size -= packet_size;
255 }
256 td_set_ioc(&uhci_batch->tds[td - 1]);
257 endpoint_toggle_set(uhci_batch->usb_batch->ep, toggle);
258 usb_log_debug2(
259 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
260 uhci_batch->usb_batch,
261 usb_str_transfer_type(uhci_batch->usb_batch->ep->transfer_type),
262 usb_str_direction(uhci_batch->usb_batch->ep->direction),
263 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch));
264}
265/*----------------------------------------------------------------------------*/
266/** Prepare generic control transfer
267 *
268 * @param[in] uhci_batch Batch structure to use.
269 * @param[in] data_stage Pid to use for data tds.
270 * @param[in] status_stage Pid to use for data tds.
271 *
272 * Setup stage with toggle 0 and USB_PID_SETUP.
273 * Data stage with alternating toggle and pid supplied by parameter.
274 * Status stage with toggle 1 and pid supplied by parameter.
275 * The last transfer is marked with IOC.
276 */
277static void batch_control(uhci_transfer_batch_t *uhci_batch,
278 usb_packet_id data_stage_pid, usb_packet_id status_stage_pid)
279{
280 assert(uhci_batch);
281 assert(uhci_batch->usb_batch);
282 assert(uhci_batch->usb_batch->ep);
283 assert(uhci_batch->td_count >= 2);
284
285 const bool low_speed =
286 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
287 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
288 const usb_target_t target = {
289 uhci_batch->usb_batch->ep->address,
290 uhci_batch->usb_batch->ep->endpoint };
291
292 /* setup stage */
293 td_init(
294 &uhci_batch->tds[0], DEFAULT_ERROR_COUNT,
295 uhci_batch->usb_batch->setup_size, 0, false,
296 low_speed, target, USB_PID_SETUP,
297 uhci_transfer_batch_setup_buffer(uhci_batch), &uhci_batch->tds[1]);
298
299 /* data stage */
300 size_t td = 1;
301 unsigned toggle = 1;
302 size_t remain_size = uhci_batch->usb_batch->buffer_size;
303 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
304
305 while (remain_size > 0) {
306 const size_t packet_size =
307 (remain_size < mps) ? remain_size : mps;
308
309 td_init(
310 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
311 toggle, false, low_speed, target, data_stage_pid,
312 buffer, &uhci_batch->tds[td + 1]);
313
314 ++td;
315 toggle = 1 - toggle;
316 buffer += packet_size;
317 remain_size -= packet_size;
318 assert(td < uhci_batch->td_count);
319 }
320
321 /* status stage */
322 assert(td == uhci_batch->td_count - 1);
323
324 td_init(
325 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
326 target, status_stage_pid, NULL, NULL);
327 td_set_ioc(&uhci_batch->tds[td]);
328
329 usb_log_debug2("Control last TD status: %x.\n",
330 uhci_batch->tds[td].status);
331}
332/*----------------------------------------------------------------------------*/
333static void batch_setup_control(uhci_transfer_batch_t *uhci_batch)
334{
335 assert(uhci_batch);
336 assert(uhci_batch->usb_batch);
337 assert(uhci_batch->usb_batch->setup_buffer);
338 // TODO Find a better way to do this
339 /* Check first bit of the first setup request byte
340 * (it signals hc-> dev or dev->hc communication) */
341 const char *direction = NULL;
342 if (uhci_batch->usb_batch->setup_buffer[0] & (1 << 7)) {
343 batch_control(uhci_batch, USB_PID_IN, USB_PID_OUT);
344 direction = "read";
345 } else {
346 batch_control(uhci_batch, USB_PID_OUT, USB_PID_IN);
347 direction = "write";
348 }
349 usb_log_debug2(
350 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
351 uhci_batch->usb_batch,
352 usb_str_transfer_type(uhci_batch->usb_batch->ep->transfer_type),
353 direction, USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch));
354}
355/*----------------------------------------------------------------------------*/
356static void (* const batch_setup[4][3])(uhci_transfer_batch_t*) =
357{
358 { NULL, NULL, batch_setup_control },
359 { NULL, NULL, NULL },
360 { batch_data, batch_data, NULL },
361 { batch_data, batch_data, NULL },
362};
363/**
364 * @}
365 */
Note: See TracBrowser for help on using the repository browser.