source: mainline/uspace/drv/uhci-hcd/iface.c@ 1e70157

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

Header fixes, add qh to endpoint structure, add endpoint toggle reset

  • Property mode set to 100644
File size: 17.8 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky, 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 hc interface implementation
33 */
34#include <ddf/driver.h>
35#include <errno.h>
36
37#include <usb/debug.h>
38
39#include "endpoint.h"
40#include "iface.h"
41#include "hc.h"
42
43/** Reserve default address interface function
44 *
45 * @param[in] fun DDF function that was called.
46 * @param[in] speed Speed to associate with the new default address.
47 * @return Error code.
48 */
49static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
50{
51 assert(fun);
52 hc_t *hc = fun_to_hc(fun);
53 assert(hc);
54 usb_log_debug("Default address request with speed %d.\n", speed);
55 usb_device_keeper_reserve_default_address(&hc->manager, speed);
56 return EOK;
57#if 0
58 endpoint_t *ep = malloc(sizeof(endpoint_t));
59 if (ep == NULL)
60 return ENOMEM;
61 const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64;
62 endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size);
63 int ret;
64try_retgister:
65 ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
66 USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0);
67 if (ret == EEXISTS) {
68 async_usleep(1000);
69 goto try_retgister;
70 }
71 if (ret != EOK) {
72 endpoint_destroy(ep);
73 }
74 return ret;
75#endif
76}
77/*----------------------------------------------------------------------------*/
78/** Release default address interface function
79 *
80 * @param[in] fun DDF function that was called.
81 * @return Error code.
82 */
83static int release_default_address(ddf_fun_t *fun)
84{
85 assert(fun);
86 hc_t *hc = fun_to_hc(fun);
87 assert(hc);
88 usb_log_debug("Default address release.\n");
89// return usb_endpoint_manager_unregister_ep(&hc->ep_manager,
90// USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH);
91 usb_device_keeper_release_default_address(&hc->manager);
92 return EOK;
93}
94/*----------------------------------------------------------------------------*/
95/** Request address interface function
96 *
97 * @param[in] fun DDF function that was called.
98 * @param[in] speed Speed to associate with the new default address.
99 * @param[out] address Place to write a new address.
100 * @return Error code.
101 */
102static int request_address(
103 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
104{
105 assert(fun);
106 hc_t *hc = fun_to_hc(fun);
107 assert(hc);
108 assert(address);
109
110 usb_log_debug("Address request with speed %d.\n", speed);
111 *address = device_keeper_get_free_address(&hc->manager, speed);
112 usb_log_debug("Address request with result: %d.\n", *address);
113 if (*address <= 0)
114 return *address;
115 return EOK;
116}
117/*----------------------------------------------------------------------------*/
118/** Bind address interface function
119 *
120 * @param[in] fun DDF function that was called.
121 * @param[in] address Address of the device
122 * @param[in] handle Devman handle of the device driver.
123 * @return Error code.
124 */
125static int bind_address(
126 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
127{
128 assert(fun);
129 hc_t *hc = fun_to_hc(fun);
130 assert(hc);
131 usb_log_debug("Address bind %d-%d.\n", address, handle);
132 usb_device_keeper_bind(&hc->manager, address, handle);
133 return EOK;
134}
135/*----------------------------------------------------------------------------*/
136/** Release address interface function
137 *
138 * @param[in] fun DDF function that was called.
139 * @param[in] address USB address to be released.
140 * @return Error code.
141 */
142static int release_address(ddf_fun_t *fun, usb_address_t address)
143{
144 assert(fun);
145 hc_t *hc = fun_to_hc(fun);
146 assert(hc);
147 usb_log_debug("Address release %d.\n", address);
148 usb_device_keeper_release(&hc->manager, address);
149 return EOK;
150}
151/*----------------------------------------------------------------------------*/
152static int register_endpoint(
153 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
154 usb_transfer_type_t transfer_type, usb_direction_t direction,
155 size_t max_packet_size, unsigned int interval)
156{
157 hc_t *hc = fun_to_hc(fun);
158 assert(hc);
159 const usb_speed_t speed =
160 usb_device_keeper_get_speed(&hc->manager, address);
161 const size_t size = max_packet_size;
162 int ret;
163
164 endpoint_t *ep = malloc(sizeof(endpoint_t));
165 if (ep == NULL)
166 return ENOMEM;
167 ret = endpoint_init(ep, transfer_type, speed, max_packet_size);
168 if (ret != EOK) {
169 free(ep);
170 return ret;
171 }
172
173 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
174 address, endpoint, usb_str_transfer_type(transfer_type),
175 usb_str_speed(speed), direction, size, max_packet_size, interval);
176
177 const size_t bw =
178 (transfer_type == USB_TRANSFER_INTERRUPT
179 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
180 bandwidth_count_usb11(speed, transfer_type, size, max_packet_size) :
181 0;
182
183 ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
184 address, endpoint, direction, ep, endpoint_destroy, bw);
185 if (ret != EOK) {
186 endpoint_destroy(ep);
187 } else {
188 usb_device_keeper_add_ep(&hc->manager, address, &ep->same_device_eps);
189 }
190 return ret;
191}
192/*----------------------------------------------------------------------------*/
193static int unregister_endpoint(
194 ddf_fun_t *fun, usb_address_t address,
195 usb_endpoint_t endpoint, usb_direction_t direction)
196{
197 hc_t *hc = fun_to_hc(fun);
198 assert(hc);
199 usb_log_debug("Unregister endpoint %d:%d %d.\n",
200 address, endpoint, direction);
201 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
202 endpoint, direction);
203}
204/*----------------------------------------------------------------------------*/
205/** Interrupt out transaction interface function
206 *
207 * @param[in] fun DDF function that was called.
208 * @param[in] target USB device to write to.
209 * @param[in] max_packet_size maximum size of data packet the device accepts
210 * @param[in] data Source of data.
211 * @param[in] size Size of data source.
212 * @param[in] callback Function to call on transaction completion
213 * @param[in] arg Additional for callback function.
214 * @return Error code.
215 */
216static int interrupt_out(
217 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
218 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
219{
220 assert(fun);
221 hc_t *hc = fun_to_hc(fun);
222 assert(hc);
223
224 usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
225 target.address, target.endpoint, size, max_packet_size);
226
227 size_t res_bw;
228 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
229 target.address, target.endpoint, USB_DIRECTION_OUT, &res_bw);
230 if (ep == NULL) {
231 usb_log_error("Endpoint(%d:%d) not registered for INT OUT.\n",
232 target.address, target.endpoint);
233 return ENOENT;
234 }
235 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
236 size, ep->max_packet_size);
237 if (res_bw < bw)
238 {
239 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
240 "but only %zu is reserved.\n",
241 target.address, target.endpoint, bw, res_bw);
242 return ENOENT;
243 }
244 assert(ep->speed ==
245 usb_device_keeper_get_speed(&hc->manager, target.address));
246 assert(ep->max_packet_size == max_packet_size);
247 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
248
249 usb_transfer_batch_t *batch =
250 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
251 ep->speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);
252 if (!batch)
253 return ENOMEM;
254 batch_interrupt_out(batch);
255 const int ret = hc_schedule(hc, batch);
256 if (ret != EOK) {
257 batch_dispose(batch);
258 }
259 return ret;
260}
261/*----------------------------------------------------------------------------*/
262/** Interrupt in transaction interface function
263 *
264 * @param[in] fun DDF function that was called.
265 * @param[in] target USB device to write to.
266 * @param[in] max_packet_size maximum size of data packet the device accepts
267 * @param[out] data Data destination.
268 * @param[in] size Size of data source.
269 * @param[in] callback Function to call on transaction completion
270 * @param[in] arg Additional for callback function.
271 * @return Error code.
272 */
273static int interrupt_in(
274 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
275 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
276{
277 assert(fun);
278 hc_t *hc = fun_to_hc(fun);
279 assert(hc);
280
281 usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
282 target.address, target.endpoint, size, max_packet_size);
283
284 size_t res_bw;
285 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
286 target.address, target.endpoint, USB_DIRECTION_IN, &res_bw);
287 if (ep == NULL) {
288 usb_log_error("Endpoint(%d:%d) not registered for INT IN.\n",
289 target.address, target.endpoint);
290 return ENOENT;
291 }
292 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
293 size, ep->max_packet_size);
294 if (res_bw < bw)
295 {
296 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
297 "but only %zu bw is reserved.\n",
298 target.address, target.endpoint, bw, res_bw);
299 return ENOENT;
300 }
301
302 assert(ep->speed ==
303 usb_device_keeper_get_speed(&hc->manager, target.address));
304 assert(ep->max_packet_size == max_packet_size);
305 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
306
307 usb_transfer_batch_t *batch =
308 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
309 ep->speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);
310 if (!batch)
311 return ENOMEM;
312 batch_interrupt_in(batch);
313 const int ret = hc_schedule(hc, batch);
314 if (ret != EOK) {
315 batch_dispose(batch);
316 }
317 return ret;
318}
319/*----------------------------------------------------------------------------*/
320/** Bulk out transaction interface function
321 *
322 * @param[in] fun DDF function that was called.
323 * @param[in] target USB device to write to.
324 * @param[in] max_packet_size maximum size of data packet the device accepts
325 * @param[in] data Source of data.
326 * @param[in] size Size of data source.
327 * @param[in] callback Function to call on transaction completion
328 * @param[in] arg Additional for callback function.
329 * @return Error code.
330 */
331static int bulk_out(
332 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
333 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
334{
335 assert(fun);
336 hc_t *hc = fun_to_hc(fun);
337 assert(hc);
338
339 usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
340 target.address, target.endpoint, size, max_packet_size);
341
342 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
343 target.address, target.endpoint, USB_DIRECTION_OUT, NULL);
344 if (ep == NULL) {
345 usb_log_error("Endpoint(%d:%d) not registered for BULK OUT.\n",
346 target.address, target.endpoint);
347 return ENOENT;
348 }
349 assert(ep->speed ==
350 usb_device_keeper_get_speed(&hc->manager, target.address));
351 assert(ep->max_packet_size == max_packet_size);
352 assert(ep->transfer_type == USB_TRANSFER_BULK);
353
354 usb_transfer_batch_t *batch =
355 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
356 ep->speed, data, size, NULL, 0, NULL, callback, arg,
357 &hc->manager);
358 if (!batch)
359 return ENOMEM;
360 batch_bulk_out(batch);
361 const int ret = hc_schedule(hc, batch);
362 if (ret != EOK) {
363 batch_dispose(batch);
364 }
365 return ret;
366}
367/*----------------------------------------------------------------------------*/
368/** Bulk in transaction interface function
369 *
370 * @param[in] fun DDF function that was called.
371 * @param[in] target USB device to write to.
372 * @param[in] max_packet_size maximum size of data packet the device accepts
373 * @param[out] data Data destination.
374 * @param[in] size Size of data source.
375 * @param[in] callback Function to call on transaction completion
376 * @param[in] arg Additional for callback function.
377 * @return Error code.
378 */
379static int bulk_in(
380 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
381 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
382{
383 assert(fun);
384 hc_t *hc = fun_to_hc(fun);
385 assert(hc);
386 usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
387 target.address, target.endpoint, size, max_packet_size);
388
389 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
390 target.address, target.endpoint, USB_DIRECTION_IN, NULL);
391 if (ep == NULL) {
392 usb_log_error("Endpoint(%d:%d) not registered for BULK IN.\n",
393 target.address, target.endpoint);
394 return ENOENT;
395 }
396 assert(ep->speed ==
397 usb_device_keeper_get_speed(&hc->manager, target.address));
398 assert(ep->max_packet_size == max_packet_size);
399 assert(ep->transfer_type == USB_TRANSFER_BULK);
400
401 usb_transfer_batch_t *batch =
402 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
403 ep->speed, data, size, NULL, 0, callback, NULL, arg,
404 &hc->manager);
405 if (!batch)
406 return ENOMEM;
407 batch_bulk_in(batch);
408 const int ret = hc_schedule(hc, batch);
409 if (ret != EOK) {
410 batch_dispose(batch);
411 }
412 return ret;
413}
414/*----------------------------------------------------------------------------*/
415/** Control write transaction interface function
416 *
417 * @param[in] fun DDF function that was called.
418 * @param[in] target USB device to write to.
419 * @param[in] max_packet_size maximum size of data packet the device accepts.
420 * @param[in] setup_data Data to send with SETUP transfer.
421 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
422 * @param[in] data Source of data.
423 * @param[in] size Size of data source.
424 * @param[in] callback Function to call on transaction completion.
425 * @param[in] arg Additional for callback function.
426 * @return Error code.
427 */
428static int control_write(
429 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
430 void *setup_data, size_t setup_size, void *data, size_t size,
431 usbhc_iface_transfer_out_callback_t callback, void *arg)
432{
433 assert(fun);
434 hc_t *hc = fun_to_hc(fun);
435 assert(hc);
436 usb_speed_t speed =
437 usb_device_keeper_get_speed(&hc->manager, target.address);
438 usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
439 speed, target.address, target.endpoint, size, max_packet_size);
440 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
441 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
442 if (ep == NULL) {
443 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
444 target.address, target.endpoint);
445 }
446
447 if (setup_size != 8)
448 return EINVAL;
449
450 usb_transfer_batch_t *batch =
451 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
452 data, size, setup_data, setup_size, NULL, callback, arg,
453 &hc->manager);
454 if (!batch)
455 return ENOMEM;
456 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
457 batch_control_write(batch);
458 const int ret = hc_schedule(hc, batch);
459 if (ret != EOK) {
460 batch_dispose(batch);
461 }
462 return ret;
463}
464/*----------------------------------------------------------------------------*/
465/** Control read transaction interface function
466 *
467 * @param[in] fun DDF function that was called.
468 * @param[in] target USB device to write to.
469 * @param[in] max_packet_size maximum size of data packet the device accepts.
470 * @param[in] setup_data Data to send with SETUP packet.
471 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
472 * @param[out] data Source of data.
473 * @param[in] size Size of data source.
474 * @param[in] callback Function to call on transaction completion.
475 * @param[in] arg Additional for callback function.
476 * @return Error code.
477 */
478static int control_read(
479 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
480 void *setup_data, size_t setup_size, void *data, size_t size,
481 usbhc_iface_transfer_in_callback_t callback, void *arg)
482{
483 assert(fun);
484 hc_t *hc = fun_to_hc(fun);
485 assert(hc);
486 usb_speed_t speed =
487 usb_device_keeper_get_speed(&hc->manager, target.address);
488
489 usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
490 speed, target.address, target.endpoint, size, max_packet_size);
491 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
492 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
493 if (ep == NULL) {
494 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
495 target.address, target.endpoint);
496 }
497 usb_transfer_batch_t *batch =
498 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
499 data, size, setup_data, setup_size, callback, NULL, arg,
500 &hc->manager);
501 if (!batch)
502 return ENOMEM;
503 batch_control_read(batch);
504 const int ret = hc_schedule(hc, batch);
505 if (ret != EOK) {
506 batch_dispose(batch);
507 }
508 return ret;
509}
510/*----------------------------------------------------------------------------*/
511usbhc_iface_t hc_iface = {
512 .reserve_default_address = reserve_default_address,
513 .release_default_address = release_default_address,
514 .request_address = request_address,
515 .bind_address = bind_address,
516 .release_address = release_address,
517
518 .register_endpoint = register_endpoint,
519 .unregister_endpoint = unregister_endpoint,
520
521 .interrupt_out = interrupt_out,
522 .interrupt_in = interrupt_in,
523
524 .bulk_out = bulk_out,
525 .bulk_in = bulk_in,
526
527 .control_write = control_write,
528 .control_read = control_read,
529};
530/**
531 * @}
532 */
Note: See TracBrowser for help on using the repository browser.