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

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

Only INT and ISO transfers require bandwidth reservation

add dead code for alternate default address reservation.

  • Property mode set to 100644
File size: 17.7 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
163 endpoint_t *ep = malloc(sizeof(endpoint_t));
164 if (ep == NULL)
165 return ENOMEM;
166 endpoint_init(ep, transfer_type, speed, max_packet_size);
167
168 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
169 address, endpoint, usb_str_transfer_type(transfer_type),
170 usb_str_speed(speed), direction, size, max_packet_size, interval);
171
172 const size_t bw =
173 (transfer_type == USB_TRANSFER_INTERRUPT
174 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
175 bandwidth_count_usb11(speed, transfer_type, size, max_packet_size) :
176 0;
177
178 int ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
179 address, endpoint, direction, ep, endpoint_destroy, bw);
180 if (ret != EOK) {
181 endpoint_destroy(ep);
182 }
183 return ret;
184}
185/*----------------------------------------------------------------------------*/
186static int unregister_endpoint(
187 ddf_fun_t *fun, usb_address_t address,
188 usb_endpoint_t endpoint, usb_direction_t direction)
189{
190 hc_t *hc = fun_to_hc(fun);
191 assert(hc);
192 usb_log_debug("Unregister endpoint %d:%d %d.\n",
193 address, endpoint, direction);
194 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
195 endpoint, direction);
196}
197/*----------------------------------------------------------------------------*/
198/** Interrupt out transaction interface function
199 *
200 * @param[in] fun DDF function that was called.
201 * @param[in] target USB device to write to.
202 * @param[in] max_packet_size maximum size of data packet the device accepts
203 * @param[in] data Source of data.
204 * @param[in] size Size of data source.
205 * @param[in] callback Function to call on transaction completion
206 * @param[in] arg Additional for callback function.
207 * @return Error code.
208 */
209static int interrupt_out(
210 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
211 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
212{
213 assert(fun);
214 hc_t *hc = fun_to_hc(fun);
215 assert(hc);
216
217 usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
218 target.address, target.endpoint, size, max_packet_size);
219
220 size_t res_bw;
221 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
222 target.address, target.endpoint, USB_DIRECTION_OUT, &res_bw);
223 if (ep == NULL) {
224 usb_log_error("Endpoint(%d:%d) not registered for INT OUT.\n",
225 target.address, target.endpoint);
226 return ENOENT;
227 }
228 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
229 size, ep->max_packet_size);
230 if (res_bw < bw)
231 {
232 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
233 "but only %zu is reserved.\n",
234 target.address, target.endpoint, bw, res_bw);
235 return ENOENT;
236 }
237 assert(ep->speed ==
238 usb_device_keeper_get_speed(&hc->manager, target.address));
239 assert(ep->max_packet_size == max_packet_size);
240 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
241
242 usb_transfer_batch_t *batch =
243 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
244 ep->speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);
245 if (!batch)
246 return ENOMEM;
247 batch_interrupt_out(batch);
248 const int ret = hc_schedule(hc, batch);
249 if (ret != EOK) {
250 batch_dispose(batch);
251 }
252 return ret;
253}
254/*----------------------------------------------------------------------------*/
255/** Interrupt in transaction interface function
256 *
257 * @param[in] fun DDF function that was called.
258 * @param[in] target USB device to write to.
259 * @param[in] max_packet_size maximum size of data packet the device accepts
260 * @param[out] data Data destination.
261 * @param[in] size Size of data source.
262 * @param[in] callback Function to call on transaction completion
263 * @param[in] arg Additional for callback function.
264 * @return Error code.
265 */
266static int interrupt_in(
267 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
268 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
269{
270 assert(fun);
271 hc_t *hc = fun_to_hc(fun);
272 assert(hc);
273
274 usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
275 target.address, target.endpoint, size, max_packet_size);
276
277 size_t res_bw;
278 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
279 target.address, target.endpoint, USB_DIRECTION_IN, &res_bw);
280 if (ep == NULL) {
281 usb_log_error("Endpoint(%d:%d) not registered for INT IN.\n",
282 target.address, target.endpoint);
283 return ENOENT;
284 }
285 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
286 size, ep->max_packet_size);
287 if (res_bw < bw)
288 {
289 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
290 "but only %zu bw is reserved.\n",
291 target.address, target.endpoint, bw, res_bw);
292 return ENOENT;
293 }
294
295 assert(ep->speed ==
296 usb_device_keeper_get_speed(&hc->manager, target.address));
297 assert(ep->max_packet_size == max_packet_size);
298 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
299
300 usb_transfer_batch_t *batch =
301 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
302 ep->speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);
303 if (!batch)
304 return ENOMEM;
305 batch_interrupt_in(batch);
306 const int ret = hc_schedule(hc, batch);
307 if (ret != EOK) {
308 batch_dispose(batch);
309 }
310 return ret;
311}
312/*----------------------------------------------------------------------------*/
313/** Bulk out transaction interface function
314 *
315 * @param[in] fun DDF function that was called.
316 * @param[in] target USB device to write to.
317 * @param[in] max_packet_size maximum size of data packet the device accepts
318 * @param[in] data Source of data.
319 * @param[in] size Size of data source.
320 * @param[in] callback Function to call on transaction completion
321 * @param[in] arg Additional for callback function.
322 * @return Error code.
323 */
324static int bulk_out(
325 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
326 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
327{
328 assert(fun);
329 hc_t *hc = fun_to_hc(fun);
330 assert(hc);
331
332 usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
333 target.address, target.endpoint, size, max_packet_size);
334
335 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
336 target.address, target.endpoint, USB_DIRECTION_OUT, NULL);
337 if (ep == NULL) {
338 usb_log_error("Endpoint(%d:%d) not registered for BULK OUT.\n",
339 target.address, target.endpoint);
340 return ENOENT;
341 }
342 assert(ep->speed ==
343 usb_device_keeper_get_speed(&hc->manager, target.address));
344 assert(ep->max_packet_size == max_packet_size);
345 assert(ep->transfer_type == USB_TRANSFER_BULK);
346
347
348 usb_transfer_batch_t *batch =
349 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
350 ep->speed, data, size, NULL, 0, NULL, callback, arg,
351 &hc->manager);
352 if (!batch)
353 return ENOMEM;
354 batch_bulk_out(batch);
355 const int ret = hc_schedule(hc, batch);
356 if (ret != EOK) {
357 batch_dispose(batch);
358 }
359 return ret;
360}
361/*----------------------------------------------------------------------------*/
362/** Bulk in transaction interface function
363 *
364 * @param[in] fun DDF function that was called.
365 * @param[in] target USB device to write to.
366 * @param[in] max_packet_size maximum size of data packet the device accepts
367 * @param[out] data Data destination.
368 * @param[in] size Size of data source.
369 * @param[in] callback Function to call on transaction completion
370 * @param[in] arg Additional for callback function.
371 * @return Error code.
372 */
373static int bulk_in(
374 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
375 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
376{
377 assert(fun);
378 hc_t *hc = fun_to_hc(fun);
379 assert(hc);
380 usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
381 target.address, target.endpoint, size, max_packet_size);
382
383 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
384 target.address, target.endpoint, USB_DIRECTION_IN, NULL);
385 if (ep == NULL) {
386 usb_log_error("Endpoint(%d:%d) not registered for BULK IN.\n",
387 target.address, target.endpoint);
388 return ENOENT;
389 }
390 assert(ep->speed ==
391 usb_device_keeper_get_speed(&hc->manager, target.address));
392 assert(ep->max_packet_size == max_packet_size);
393 assert(ep->transfer_type == USB_TRANSFER_BULK);
394
395 usb_transfer_batch_t *batch =
396 batch_get(fun, target, ep->transfer_type, ep->max_packet_size, ep->speed,
397 data, size, NULL, 0, callback, NULL, arg, &hc->manager);
398 if (!batch)
399 return ENOMEM;
400 batch_bulk_in(batch);
401 const int ret = hc_schedule(hc, batch);
402 if (ret != EOK) {
403 batch_dispose(batch);
404 }
405 return ret;
406}
407/*----------------------------------------------------------------------------*/
408/** Control write transaction interface function
409 *
410 * @param[in] fun DDF function that was called.
411 * @param[in] target USB device to write to.
412 * @param[in] max_packet_size maximum size of data packet the device accepts.
413 * @param[in] setup_data Data to send with SETUP transfer.
414 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
415 * @param[in] data Source of data.
416 * @param[in] size Size of data source.
417 * @param[in] callback Function to call on transaction completion.
418 * @param[in] arg Additional for callback function.
419 * @return Error code.
420 */
421static int control_write(
422 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
423 void *setup_data, size_t setup_size, void *data, size_t size,
424 usbhc_iface_transfer_out_callback_t callback, void *arg)
425{
426 assert(fun);
427 hc_t *hc = fun_to_hc(fun);
428 assert(hc);
429 usb_speed_t speed =
430 usb_device_keeper_get_speed(&hc->manager, target.address);
431 usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
432 speed, target.address, target.endpoint, size, max_packet_size);
433 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
434 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
435 if (ep == NULL) {
436 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
437 target.address, target.endpoint);
438 }
439
440 if (setup_size != 8)
441 return EINVAL;
442
443 usb_transfer_batch_t *batch =
444 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
445 data, size, setup_data, setup_size, NULL, callback, arg,
446 &hc->manager);
447 if (!batch)
448 return ENOMEM;
449 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
450 batch_control_write(batch);
451 const int ret = hc_schedule(hc, batch);
452 if (ret != EOK) {
453 batch_dispose(batch);
454 }
455 return ret;
456}
457/*----------------------------------------------------------------------------*/
458/** Control read transaction interface function
459 *
460 * @param[in] fun DDF function that was called.
461 * @param[in] target USB device to write to.
462 * @param[in] max_packet_size maximum size of data packet the device accepts.
463 * @param[in] setup_data Data to send with SETUP packet.
464 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
465 * @param[out] data Source of data.
466 * @param[in] size Size of data source.
467 * @param[in] callback Function to call on transaction completion.
468 * @param[in] arg Additional for callback function.
469 * @return Error code.
470 */
471static int control_read(
472 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
473 void *setup_data, size_t setup_size, void *data, size_t size,
474 usbhc_iface_transfer_in_callback_t callback, void *arg)
475{
476 assert(fun);
477 hc_t *hc = fun_to_hc(fun);
478 assert(hc);
479 usb_speed_t speed =
480 usb_device_keeper_get_speed(&hc->manager, target.address);
481
482 usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
483 speed, target.address, target.endpoint, size, max_packet_size);
484 endpoint_t *ep = usb_endpoint_manager_get_ep_data(&hc->ep_manager,
485 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
486 if (ep == NULL) {
487 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
488 target.address, target.endpoint);
489 }
490 usb_transfer_batch_t *batch =
491 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
492 data, size, setup_data, setup_size, callback, NULL, arg,
493 &hc->manager);
494 if (!batch)
495 return ENOMEM;
496 batch_control_read(batch);
497 const int ret = hc_schedule(hc, batch);
498 if (ret != EOK) {
499 batch_dispose(batch);
500 }
501 return ret;
502}
503/*----------------------------------------------------------------------------*/
504usbhc_iface_t hc_iface = {
505 .reserve_default_address = reserve_default_address,
506 .release_default_address = release_default_address,
507 .request_address = request_address,
508 .bind_address = bind_address,
509 .release_address = release_address,
510
511 .register_endpoint = register_endpoint,
512 .unregister_endpoint = unregister_endpoint,
513
514 .interrupt_out = interrupt_out,
515 .interrupt_in = interrupt_in,
516
517 .bulk_out = bulk_out,
518 .bulk_in = bulk_in,
519
520 .control_write = control_write,
521 .control_read = control_read,
522};
523/**
524 * @}
525 */
Note: See TracBrowser for help on using the repository browser.