source: mainline/uspace/drv/bus/usb/vhc/connhost.c@ 378bf85

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 378bf85 was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

  • Property mode set to 100644
File size: 16.5 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
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
29/** @addtogroup drvusbvhc
30 * @{
31 */
32/** @file
33 * Host controller interface implementation.
34 */
35#include <assert.h>
36#include <errno.h>
37#include <usb/usb.h>
38#include <usb/ddfiface.h>
39#include <usb/debug.h>
40#include <usbhc_iface.h>
41#include "vhcd.h"
42
43#define GET_VHC_DATA(fun) \
44 ((vhc_data_t *)ddf_dev_data_get(ddf_fun_get_dev(fun)))
45#define VHC_DATA(vhc, fun) \
46 vhc_data_t *vhc = GET_VHC_DATA(fun); assert(vhc->magic == 0xdeadbeef)
47
48#define UNSUPPORTED(methodname) \
49 usb_log_warning("Unsupported interface method `%s()' in %s:%d.\n", \
50 methodname, __FILE__, __LINE__)
51
52/** Found free USB address.
53 *
54 * @param[in] fun Device function the action was invoked on.
55 * @param[in] speed Speed of the device that will get this address.
56 * @param[out] address Non-null pointer where to store the free address.
57 * @return Error code.
58 */
59static int request_address(ddf_fun_t *fun, usb_address_t *address, bool strict,
60 usb_speed_t speed)
61{
62 VHC_DATA(vhc, fun);
63
64 assert(address);
65 return usb_device_manager_request_address(
66 &vhc->dev_manager, address, strict, speed);
67}
68
69/** Bind USB address with device devman handle.
70 *
71 * @param[in] fun Device function the action was invoked on.
72 * @param[in] address USB address of the device.
73 * @param[in] handle Devman handle of the device.
74 * @return Error code.
75 */
76static int bind_address(ddf_fun_t *fun,
77 usb_address_t address, devman_handle_t handle)
78{
79 VHC_DATA(vhc, fun);
80 usb_log_debug("Binding handle %" PRIun " to address %d.\n",
81 handle, address);
82 usb_device_manager_bind_address(&vhc->dev_manager, address, handle);
83
84 return EOK;
85}
86
87/** Find device handle by address interface function.
88 *
89 * @param[in] fun DDF function that was called.
90 * @param[in] address Address in question.
91 * @param[out] handle Where to store device handle if found.
92 * @return Error code.
93 */
94static int find_by_address(ddf_fun_t *fun, usb_address_t address,
95 devman_handle_t *handle)
96{
97 VHC_DATA(vhc, fun);
98 return usb_device_manager_get_info_by_address(
99 &vhc->dev_manager, address, handle, NULL);
100}
101
102/** Release previously requested address.
103 *
104 * @param[in] fun Device function the action was invoked on.
105 * @param[in] address USB address to be released.
106 * @return Error code.
107 */
108static int release_address(ddf_fun_t *fun, usb_address_t address)
109{
110 VHC_DATA(vhc, fun);
111 usb_log_debug("Releasing address %d...\n", address);
112 usb_device_manager_release_address(&vhc->dev_manager, address);
113
114 return ENOTSUP;
115}
116
117/** Register endpoint for bandwidth reservation.
118 *
119 * @param[in] fun Device function the action was invoked on.
120 * @param[in] address USB address of the device.
121 * @param[in] speed Endpoint speed (invalid means to use device one).
122 * @param[in] endpoint Endpoint number.
123 * @param[in] transfer_type USB transfer type.
124 * @param[in] direction Endpoint data direction.
125 * @param[in] max_packet_size Max packet size of the endpoint.
126 * @param[in] interval Polling interval.
127 * @return Error code.
128 */
129static int register_endpoint(ddf_fun_t *fun,
130 usb_address_t address, usb_endpoint_t endpoint,
131 usb_transfer_type_t transfer_type, usb_direction_t direction,
132 size_t max_packet_size, unsigned int interval)
133{
134 VHC_DATA(vhc, fun);
135
136 return usb_endpoint_manager_add_ep(&vhc->ep_manager,
137 address, endpoint, direction, transfer_type, USB_SPEED_FULL, 1, 0,
138 NULL, NULL);
139
140}
141
142/** Unregister endpoint (free some bandwidth reservation).
143 *
144 * @param[in] fun Device function the action was invoked on.
145 * @param[in] address USB address of the device.
146 * @param[in] endpoint Endpoint number.
147 * @param[in] direction Endpoint data direction.
148 * @return Error code.
149 */
150static int unregister_endpoint(ddf_fun_t *fun, usb_address_t address,
151 usb_endpoint_t endpoint, usb_direction_t direction)
152{
153 VHC_DATA(vhc, fun);
154
155 int rc = usb_endpoint_manager_remove_ep(&vhc->ep_manager,
156 address, endpoint, direction, NULL, NULL);
157
158 return rc;
159}
160#if 0
161/** Schedule interrupt out transfer.
162 *
163 * The callback is supposed to be called once the transfer (on the wire) is
164 * complete regardless of the outcome.
165 * However, the callback could be called only when this function returns
166 * with success status (i.e. returns EOK).
167 *
168 * @param[in] fun Device function the action was invoked on.
169 * @param[in] target Target pipe (address and endpoint number) specification.
170 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
171 * by the caller).
172 * @param[in] size Size of the @p data buffer in bytes.
173 * @param[in] callback Callback to be issued once the transfer is complete.
174 * @param[in] arg Pass-through argument to the callback.
175 * @return Error code.
176 */
177static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
178 void *data, size_t size,
179 usbhc_iface_transfer_out_callback_t callback, void *arg)
180{
181 VHC_DATA(vhc, fun);
182
183 vhc_transfer_t *transfer = vhc_transfer_create(target.address,
184 target.endpoint, USB_DIRECTION_OUT, USB_TRANSFER_INTERRUPT,
185 fun, arg);
186 if (transfer == NULL) {
187 return ENOMEM;
188 }
189
190 transfer->data_buffer = data;
191 transfer->data_buffer_size = size;
192 transfer->callback_out = callback;
193
194 int rc = vhc_virtdev_add_transfer(vhc, transfer);
195 if (rc != EOK) {
196 free(transfer);
197 return rc;
198 }
199
200 return EOK;
201}
202
203/** Schedule interrupt in transfer.
204 *
205 * The callback is supposed to be called once the transfer (on the wire) is
206 * complete regardless of the outcome.
207 * However, the callback could be called only when this function returns
208 * with success status (i.e. returns EOK).
209 *
210 * @param[in] fun Device function the action was invoked on.
211 * @param[in] target Target pipe (address and endpoint number) specification.
212 * @param[in] data Buffer where to store the data (in USB endianess,
213 * allocated and deallocated by the caller).
214 * @param[in] size Size of the @p data buffer in bytes.
215 * @param[in] callback Callback to be issued once the transfer is complete.
216 * @param[in] arg Pass-through argument to the callback.
217 * @return Error code.
218 */
219static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
220 void *data, size_t size,
221 usbhc_iface_transfer_in_callback_t callback, void *arg)
222{
223 VHC_DATA(vhc, fun);
224
225 vhc_transfer_t *transfer = vhc_transfer_create(target.address,
226 target.endpoint, USB_DIRECTION_IN, USB_TRANSFER_INTERRUPT,
227 fun, arg);
228 if (transfer == NULL) {
229 return ENOMEM;
230 }
231
232 transfer->data_buffer = data;
233 transfer->data_buffer_size = size;
234 transfer->callback_in = callback;
235
236 int rc = vhc_virtdev_add_transfer(vhc, transfer);
237 if (rc != EOK) {
238 free(transfer);
239 return rc;
240 }
241
242 return EOK;
243}
244
245/** Schedule bulk out transfer.
246 *
247 * The callback is supposed to be called once the transfer (on the wire) is
248 * complete regardless of the outcome.
249 * However, the callback could be called only when this function returns
250 * with success status (i.e. returns EOK).
251 *
252 * @param[in] fun Device function the action was invoked on.
253 * @param[in] target Target pipe (address and endpoint number) specification.
254 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
255 * by the caller).
256 * @param[in] size Size of the @p data buffer in bytes.
257 * @param[in] callback Callback to be issued once the transfer is complete.
258 * @param[in] arg Pass-through argument to the callback.
259 * @return Error code.
260 */
261static int bulk_out(ddf_fun_t *fun, usb_target_t target,
262 void *data, size_t size,
263 usbhc_iface_transfer_out_callback_t callback, void *arg)
264{
265 UNSUPPORTED("bulk_out");
266
267 return ENOTSUP;
268}
269
270/** Schedule bulk in transfer.
271 *
272 * The callback is supposed to be called once the transfer (on the wire) is
273 * complete regardless of the outcome.
274 * However, the callback could be called only when this function returns
275 * with success status (i.e. returns EOK).
276 *
277 * @param[in] fun Device function the action was invoked on.
278 * @param[in] target Target pipe (address and endpoint number) specification.
279 * @param[in] data Buffer where to store the data (in USB endianess,
280 * allocated and deallocated by the caller).
281 * @param[in] size Size of the @p data buffer in bytes.
282 * @param[in] callback Callback to be issued once the transfer is complete.
283 * @param[in] arg Pass-through argument to the callback.
284 * @return Error code.
285 */
286static int bulk_in(ddf_fun_t *fun, usb_target_t target,
287 void *data, size_t size,
288 usbhc_iface_transfer_in_callback_t callback, void *arg)
289{
290 UNSUPPORTED("bulk_in");
291
292 return ENOTSUP;
293}
294
295/** Schedule control write transfer.
296 *
297 * The callback is supposed to be called once the transfer (on the wire) is
298 * complete regardless of the outcome.
299 * However, the callback could be called only when this function returns
300 * with success status (i.e. returns EOK).
301 *
302 * @param[in] fun Device function the action was invoked on.
303 * @param[in] target Target pipe (address and endpoint number) specification.
304 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
305 * and deallocated by the caller).
306 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
307 * @param[in] data_buffer Data buffer (in USB endianess, allocated and
308 * deallocated by the caller).
309 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
310 * @param[in] callback Callback to be issued once the transfer is complete.
311 * @param[in] arg Pass-through argument to the callback.
312 * @return Error code.
313 */
314static int control_write(ddf_fun_t *fun, usb_target_t target,
315 void *setup_packet, size_t setup_packet_size,
316 void *data_buffer, size_t data_buffer_size,
317 usbhc_iface_transfer_out_callback_t callback, void *arg)
318{
319 VHC_DATA(vhc, fun);
320
321 vhc_transfer_t *transfer = vhc_transfer_create(target.address,
322 target.endpoint, USB_DIRECTION_OUT, USB_TRANSFER_CONTROL,
323 fun, arg);
324 if (transfer == NULL) {
325 return ENOMEM;
326 }
327
328 transfer->setup_buffer = setup_packet;
329 transfer->setup_buffer_size = setup_packet_size;
330 transfer->data_buffer = data_buffer;
331 transfer->data_buffer_size = data_buffer_size;
332 transfer->callback_out = callback;
333
334 int rc = vhc_virtdev_add_transfer(vhc, transfer);
335 if (rc != EOK) {
336 free(transfer);
337 return rc;
338 }
339
340 return EOK;
341}
342
343/** Schedule control read transfer.
344 *
345 * The callback is supposed to be called once the transfer (on the wire) is
346 * complete regardless of the outcome.
347 * However, the callback could be called only when this function returns
348 * with success status (i.e. returns EOK).
349 *
350 * @param[in] fun Device function the action was invoked on.
351 * @param[in] target Target pipe (address and endpoint number) specification.
352 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
353 * and deallocated by the caller).
354 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
355 * @param[in] data_buffer Buffer where to store the data (in USB endianess,
356 * allocated and deallocated by the caller).
357 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
358 * @param[in] callback Callback to be issued once the transfer is complete.
359 * @param[in] arg Pass-through argument to the callback.
360 * @return Error code.
361 */
362static int control_read(ddf_fun_t *fun, usb_target_t target,
363 void *setup_packet, size_t setup_packet_size,
364 void *data_buffer, size_t data_buffer_size,
365 usbhc_iface_transfer_in_callback_t callback, void *arg)
366{
367 VHC_DATA(vhc, fun);
368
369 vhc_transfer_t *transfer = vhc_transfer_create(target.address,
370 target.endpoint, USB_DIRECTION_IN, USB_TRANSFER_CONTROL,
371 fun, arg);
372 if (transfer == NULL) {
373 return ENOMEM;
374 }
375
376 transfer->setup_buffer = setup_packet;
377 transfer->setup_buffer_size = setup_packet_size;
378 transfer->data_buffer = data_buffer;
379 transfer->data_buffer_size = data_buffer_size;
380 transfer->callback_in = callback;
381
382 int rc = vhc_virtdev_add_transfer(vhc, transfer);
383 if (rc != EOK) {
384 free(transfer);
385 return rc;
386 }
387
388 return EOK;
389}
390#endif
391static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_buffer,
392 uint8_t *data_buffer, size_t data_buffer_size,
393 usbhc_iface_transfer_in_callback_t callback, void *arg)
394{
395 VHC_DATA(vhc, fun);
396
397 endpoint_t *ep = usb_endpoint_manager_find_ep(&vhc->ep_manager,
398 target.address, target.endpoint, USB_DIRECTION_IN);
399 if (ep == NULL) {
400 return ENOENT;
401 }
402 const usb_transfer_type_t transfer_type = ep->transfer_type;
403
404
405 vhc_transfer_t *transfer = vhc_transfer_create(target.address,
406 target.endpoint, USB_DIRECTION_IN, transfer_type,
407 fun, arg);
408 if (transfer == NULL) {
409 return ENOMEM;
410 }
411 if (transfer_type == USB_TRANSFER_CONTROL) {
412 transfer->setup_buffer = malloc(sizeof(uint64_t));
413 assert(transfer->setup_buffer);
414 memcpy(transfer->setup_buffer, &setup_buffer, sizeof(uint64_t));
415 transfer->setup_buffer_size = sizeof(uint64_t);
416 }
417 transfer->data_buffer = data_buffer;
418 transfer->data_buffer_size = data_buffer_size;
419 transfer->callback_in = callback;
420
421 int rc = vhc_virtdev_add_transfer(vhc, transfer);
422 if (rc != EOK) {
423 if (transfer->setup_buffer != NULL) {
424 free(transfer->setup_buffer);
425 }
426 free(transfer);
427 return rc;
428 }
429
430 return EOK;
431}
432
433static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_buffer,
434 const uint8_t *data_buffer, size_t data_buffer_size,
435 usbhc_iface_transfer_out_callback_t callback, void *arg)
436{
437 VHC_DATA(vhc, fun);
438
439 endpoint_t *ep = usb_endpoint_manager_find_ep(&vhc->ep_manager,
440 target.address, target.endpoint, USB_DIRECTION_OUT);
441 if (ep == NULL) {
442 return ENOENT;
443 }
444 const usb_transfer_type_t transfer_type = ep->transfer_type;
445
446
447 vhc_transfer_t *transfer = vhc_transfer_create(target.address,
448 target.endpoint, USB_DIRECTION_OUT, transfer_type,
449 fun, arg);
450 if (transfer == NULL) {
451 return ENOMEM;
452 }
453 if (transfer_type == USB_TRANSFER_CONTROL) {
454 transfer->setup_buffer = malloc(sizeof(uint64_t));
455 assert(transfer->setup_buffer);
456 memcpy(transfer->setup_buffer, &setup_buffer, sizeof(uint64_t));
457 transfer->setup_buffer_size = sizeof(uint64_t);
458 }
459 transfer->data_buffer = (void*)data_buffer;
460 transfer->data_buffer_size = data_buffer_size;
461 transfer->callback_out = callback;
462
463 int rc = vhc_virtdev_add_transfer(vhc, transfer);
464 if (rc != EOK) {
465 free(transfer->setup_buffer);
466 free(transfer);
467 return rc;
468 }
469
470 return EOK;
471}
472
473static int tell_address(ddf_fun_t *fun, usb_address_t *address)
474{
475 UNSUPPORTED("tell_address");
476
477 return ENOTSUP;
478}
479
480static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
481 devman_handle_t *handle)
482{
483 VHC_DATA(vhc, root_hub_fun);
484
485 *handle = ddf_fun_get_handle(vhc->hc_fun);
486
487 return EOK;
488}
489
490static int tell_address_rh(ddf_fun_t *root_hub_fun, usb_address_t *address)
491{
492 VHC_DATA(vhc, root_hub_fun);
493
494 devman_handle_t handle = ddf_fun_get_handle(root_hub_fun);
495
496 usb_log_debug("tell_address_rh(handle=%" PRIun ")\n", handle);
497 const usb_address_t addr =
498 usb_device_manager_find_address(&vhc->dev_manager, handle);
499 if (addr < 0) {
500 return addr;
501 } else {
502 *address = addr;
503 return EOK;
504 }
505}
506
507usbhc_iface_t vhc_iface = {
508 .request_address = request_address,
509 .bind_address = bind_address,
510 .get_handle = find_by_address,
511 .release_address = release_address,
512
513 .register_endpoint = register_endpoint,
514 .unregister_endpoint = unregister_endpoint,
515
516 .write = usb_write,
517 .read = usb_read,
518};
519
520usb_iface_t vhc_usb_iface = {
521 .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
522 .get_my_address = tell_address
523};
524
525usb_iface_t rh_usb_iface = {
526 .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
527 .get_my_address = tell_address_rh
528};
529
530
531/**
532 * @}
533 */
Note: See TracBrowser for help on using the repository browser.