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

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

Use sane key management in usb_endpoint_manager

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