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

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

uhci: simplify uhci_batch initialization

remove per type/direction functions and use a generic one,
nice array will provide suitable PIDs

  • Property mode set to 100644
File size: 12.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 "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] instance 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#define LOG_BATCH_INITIALIZED(batch, name, dir) \
206 usb_log_debug2("Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
207 (batch), (name), (dir), USB_TRANSFER_BATCH_ARGS(*(batch)))
208
209/*----------------------------------------------------------------------------*/
210/** Prepare generic data transfer
211 *
212 * @param[in] instance Batch structure to use.
213 * @param[in] pid Pid to use for data transactions.
214 *
215 * Transactions with alternating toggle bit and supplied pid value.
216 * The last transfer is marked with IOC flag.
217 */
218static void batch_data(uhci_transfer_batch_t *uhci_batch)
219{
220 assert(uhci_batch);
221 assert(uhci_batch->usb_batch);
222 static const usb_packet_id pids[] = {
223 [USB_DIRECTION_IN] = USB_PID_IN,
224 [USB_DIRECTION_OUT] = USB_PID_OUT,
225 };
226 assert(uhci_batch->usb_batch->ep->direction == USB_DIRECTION_OUT ||
227 uhci_batch->usb_batch->ep->direction == USB_DIRECTION_IN);
228
229 const usb_packet_id pid = pids[uhci_batch->usb_batch->ep->direction];
230 const bool low_speed =
231 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
232 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
233 const usb_target_t target = {
234 uhci_batch->usb_batch->ep->address,
235 uhci_batch->usb_batch->ep->endpoint };
236
237 int toggle = endpoint_toggle_get(uhci_batch->usb_batch->ep);
238 assert(toggle == 0 || toggle == 1);
239
240 size_t td = 0;
241 size_t remain_size = uhci_batch->usb_batch->buffer_size;
242 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
243
244 while (remain_size > 0) {
245 const size_t packet_size =
246 (remain_size < mps) ? remain_size : mps;
247
248 const td_t *next_td = (td + 1 < uhci_batch->td_count)
249 ? &uhci_batch->tds[td + 1] : NULL;
250
251 assert(td < uhci_batch->td_count);
252 td_init(
253 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
254 toggle, false, low_speed, target, pid, buffer, next_td);
255
256 ++td;
257 toggle = 1 - toggle;
258 buffer += packet_size;
259 remain_size -= packet_size;
260 }
261 td_set_ioc(&uhci_batch->tds[td - 1]);
262 endpoint_toggle_set(uhci_batch->usb_batch->ep, toggle);
263 LOG_BATCH_INITIALIZED(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}
267/*----------------------------------------------------------------------------*/
268/** Prepare generic control transfer
269 *
270 * @param[in] instance Batch structure to use.
271 * @param[in] data_stage Pid to use for data tds.
272 * @param[in] status_stage Pid to use for data tds.
273 *
274 * Setup stage with toggle 0 and USB_PID_SETUP.
275 * Data stage with alternating toggle and pid supplied by parameter.
276 * Status stage with toggle 1 and pid supplied by parameter.
277 * The last transfer is marked with IOC.
278 */
279static void batch_control(uhci_transfer_batch_t *uhci_batch,
280 usb_packet_id data_stage_pid, usb_packet_id status_stage_pid)
281{
282 assert(uhci_batch);
283 assert(uhci_batch->usb_batch);
284 assert(uhci_batch->usb_batch->ep);
285 assert(uhci_batch->td_count >= 2);
286
287 const bool low_speed =
288 uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
289 const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
290 const usb_target_t target = {
291 uhci_batch->usb_batch->ep->address,
292 uhci_batch->usb_batch->ep->endpoint };
293
294 /* setup stage */
295 td_init(
296 &uhci_batch->tds[0], DEFAULT_ERROR_COUNT,
297 uhci_batch->usb_batch->setup_size, 0, false,
298 low_speed, target, USB_PID_SETUP,
299 uhci_transfer_batch_setup_buffer(uhci_batch), &uhci_batch->tds[1]);
300
301 /* data stage */
302 size_t td = 1;
303 unsigned toggle = 1;
304 size_t remain_size = uhci_batch->usb_batch->buffer_size;
305 char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
306
307 while (remain_size > 0) {
308 const size_t packet_size =
309 (remain_size < mps) ? remain_size : mps;
310
311 td_init(
312 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
313 toggle, false, low_speed, target, data_stage_pid,
314 buffer, &uhci_batch->tds[td + 1]);
315
316 ++td;
317 toggle = 1 - toggle;
318 buffer += packet_size;
319 remain_size -= packet_size;
320 assert(td < uhci_batch->td_count);
321 }
322
323 /* status stage */
324 assert(td == uhci_batch->td_count - 1);
325
326 td_init(
327 &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
328 target, status_stage_pid, NULL, NULL);
329 td_set_ioc(&uhci_batch->tds[td]);
330
331 usb_log_debug2("Control last TD status: %x.\n",
332 uhci_batch->tds[td].status);
333}
334/*----------------------------------------------------------------------------*/
335static void batch_setup_control(uhci_transfer_batch_t *uhci_batch)
336{
337 assert(uhci_batch);
338 assert(uhci_batch->usb_batch);
339 assert(uhci_batch->usb_batch->setup_buffer);
340 // TODO Find a better way to do this
341 /* Check first bit of the first setup request byte
342 * (it signals hc-> dev or dev->hc communication) */
343 if (uhci_batch->usb_batch->setup_buffer[0] & (1 << 7)) {
344 batch_control(uhci_batch, USB_PID_IN, USB_PID_OUT);
345 LOG_BATCH_INITIALIZED(uhci_batch->usb_batch, "control", "read");
346 } else {
347 batch_control(uhci_batch, USB_PID_OUT, USB_PID_IN);
348 LOG_BATCH_INITIALIZED(uhci_batch->usb_batch, "control", "write");
349 }
350}
351/*----------------------------------------------------------------------------*/
352static void (* const batch_setup[4][3])(uhci_transfer_batch_t*) =
353{
354 { NULL, NULL, batch_setup_control },
355 { NULL, NULL, NULL },
356 { batch_data, batch_data, NULL },
357 { batch_data, batch_data, NULL },
358};
359/**
360 * @}
361 */
Note: See TracBrowser for help on using the repository browser.