source: mainline/uspace/lib/usbhost/src/ddf_helpers.c@ ce33c10

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

Minor fixes.

  • Property mode set to 100644
File size: 17.6 KB
Line 
1/*
2 * Copyright (c) 2013 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
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 *
34 */
35
36#include <usb_iface.h>
37#include <usb/classes/classes.h>
38#include <usb/debug.h>
39#include <usb/descriptor.h>
40#include <usb/request.h>
41#include <errno.h>
42#include <str_error.h>
43
44#include "ddf_helpers.h"
45
46#define CTRL_PIPE_MIN_PACKET_SIZE 8
47
48typedef struct usb_dev {
49 link_t link;
50 list_t devices;
51 fibril_mutex_t guard;
52 ddf_fun_t *fun;
53 usb_address_t address;
54 usb_speed_t speed;
55 usb_address_t tt_address;
56 unsigned port;
57} usb_dev_t;
58
59typedef struct hc_dev {
60 ddf_fun_t *ctl_fun;
61 hcd_t hcd;
62 usb_dev_t *root_hub;
63} hc_dev_t;
64
65static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
66{
67 return ddf_dev_data_get(dev);
68}
69
70hcd_t *dev_to_hcd(ddf_dev_t *dev)
71{
72 hc_dev_t *hc_dev = dev_to_hc_dev(dev);
73 if (!hc_dev) {
74 usb_log_error("Invalid HCD device.\n");
75 return NULL;
76 }
77 return &hc_dev->hcd;
78}
79
80
81static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
82static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
83
84
85/* DDF INTERFACE */
86
87/** Register endpoint interface function.
88 * @param fun DDF function.
89 * @param address USB address of the device.
90 * @param endpoint USB endpoint number to be registered.
91 * @param transfer_type Endpoint's transfer type.
92 * @param direction USB communication direction the endpoint is capable of.
93 * @param max_packet_size Maximu size of packets the endpoint accepts.
94 * @param interval Preferred timeout between communication.
95 * @return Error code.
96 */
97static int register_endpoint(
98 ddf_fun_t *fun, usb_endpoint_t endpoint,
99 usb_transfer_type_t transfer_type, usb_direction_t direction,
100 size_t max_packet_size, unsigned interval)
101{
102 assert(fun);
103 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
104 usb_dev_t *dev = ddf_fun_data_get(fun);
105 assert(hcd);
106 assert(dev);
107 const size_t size = max_packet_size;
108 const usb_target_t target =
109 {{.address = dev->address, .endpoint = endpoint}};
110
111 usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
112 dev->address, endpoint, usb_str_transfer_type(transfer_type),
113 usb_str_direction(direction), max_packet_size, interval);
114
115 return hcd_add_ep(hcd, target, direction, transfer_type,
116 max_packet_size, size, dev->tt_address, dev->port);
117}
118
119/** Unregister endpoint interface function.
120 * @param fun DDF function.
121 * @param address USB address of the endpoint.
122 * @param endpoint USB endpoint number.
123 * @param direction Communication direction of the enpdoint to unregister.
124 * @return Error code.
125 */
126static int unregister_endpoint(
127 ddf_fun_t *fun, usb_endpoint_t endpoint, usb_direction_t direction)
128{
129 assert(fun);
130 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
131 usb_dev_t *dev = ddf_fun_data_get(fun);
132 assert(hcd);
133 assert(dev);
134 const usb_target_t target =
135 {{.address = dev->address, .endpoint = endpoint}};
136 usb_log_debug("Unregister endpoint %d:%d %s.\n",
137 dev->address, endpoint, usb_str_direction(direction));
138 return hcd_remove_ep(hcd, target, direction);
139}
140
141static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
142{
143 assert(fun);
144 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
145 usb_dev_t *dev = ddf_fun_data_get(fun);
146 assert(hcd);
147 assert(dev);
148
149 usb_log_debug("Device %d requested default address at %s speed\n",
150 dev->address, usb_str_speed(speed));
151 return hcd_reserve_default_address(hcd, speed);
152}
153
154static int release_default_address(ddf_fun_t *fun)
155{
156 assert(fun);
157 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
158 usb_dev_t *dev = ddf_fun_data_get(fun);
159 assert(hcd);
160 assert(dev);
161
162 usb_log_debug("Device %d released default address\n", dev->address);
163 return hcd_release_default_address(hcd);
164}
165
166static int device_enumerate(ddf_fun_t *fun, unsigned port)
167{
168 assert(fun);
169 ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
170 usb_dev_t *dev = ddf_fun_data_get(fun);
171 assert(ddf_dev);
172 assert(dev);
173 usb_log_debug("Hub %d reported a new USB device on port: %u\n",
174 dev->address, port);
175 return hcd_ddf_new_device(ddf_dev, dev, port);
176}
177
178static int device_remove(ddf_fun_t *fun, unsigned port)
179{
180 assert(fun);
181 ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
182 usb_dev_t *dev = ddf_fun_data_get(fun);
183 assert(ddf_dev);
184 assert(dev);
185 usb_log_debug("Hub `%s' reported removal of device on port %u\n",
186 ddf_fun_get_name(fun), port);
187 return hcd_ddf_remove_device(ddf_dev, dev, port);
188}
189
190/** Gets handle of the respective device.
191 *
192 * @param[in] fun Device function.
193 * @param[out] handle Place to write the handle.
194 * @return Error code.
195 */
196static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
197{
198 assert(fun);
199 if (handle)
200 *handle = ddf_fun_get_handle(fun);
201 return EOK;
202}
203
204/** Inbound communication interface function.
205 * @param fun DDF function.
206 * @param target Communication target.
207 * @param setup_data Data to use in setup stage (control transfers).
208 * @param data Pointer to data buffer.
209 * @param size Size of the data buffer.
210 * @param callback Function to call on communication end.
211 * @param arg Argument passed to the callback function.
212 * @return Error code.
213 */
214static int dev_read(ddf_fun_t *fun, usb_endpoint_t endpoint,
215 uint64_t setup_data, uint8_t *data, size_t size,
216 usbhc_iface_transfer_in_callback_t callback, void *arg)
217{
218 assert(fun);
219 usb_dev_t *usb_dev = ddf_fun_data_get(fun);
220 assert(usb_dev);
221 const usb_target_t target = {{
222 .address = usb_dev->address,
223 .endpoint = endpoint,
224 }};
225 return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
226 USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
227 "READ");
228}
229
230/** Outbound communication interface function.
231 * @param fun DDF function.
232 * @param target Communication target.
233 * @param setup_data Data to use in setup stage (control transfers).
234 * @param data Pointer to data buffer.
235 * @param size Size of the data buffer.
236 * @param callback Function to call on communication end.
237 * @param arg Argument passed to the callback function.
238 * @return Error code.
239 */
240static int dev_write(ddf_fun_t *fun, usb_endpoint_t endpoint,
241 uint64_t setup_data, const uint8_t *data, size_t size,
242 usbhc_iface_transfer_out_callback_t callback, void *arg)
243{
244 assert(fun);
245 usb_dev_t *usb_dev = ddf_fun_data_get(fun);
246 assert(usb_dev);
247 const usb_target_t target = {{
248 .address = usb_dev->address,
249 .endpoint = endpoint,
250 }};
251 return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
252 target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
253 callback, arg, "WRITE");
254}
255
256/** USB device interface */
257static usb_iface_t usb_iface = {
258 .get_my_device_handle = get_my_device_handle,
259
260 .reserve_default_address = reserve_default_address,
261 .release_default_address = release_default_address,
262
263 .device_enumerate = device_enumerate,
264 .device_remove = device_remove,
265
266 .register_endpoint = register_endpoint,
267 .unregister_endpoint = unregister_endpoint,
268
269 .read = dev_read,
270 .write = dev_write,
271};
272
273/** Standard USB device interface) */
274static ddf_dev_ops_t usb_ops = {
275 .interfaces[USB_DEV_IFACE] = &usb_iface,
276};
277
278
279/* DDF HELPERS */
280
281#define GET_DEVICE_DESC(size) \
282{ \
283 .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
284 | (USB_REQUEST_TYPE_STANDARD << 5) \
285 | USB_REQUEST_RECIPIENT_DEVICE, \
286 .request = USB_DEVREQ_GET_DESCRIPTOR, \
287 .value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
288 .index = uint16_host2usb(0), \
289 .length = uint16_host2usb(size), \
290};
291
292#define SET_ADDRESS(address) \
293{ \
294 .request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
295 | (USB_REQUEST_TYPE_STANDARD << 5) \
296 | USB_REQUEST_RECIPIENT_DEVICE, \
297 .request = USB_DEVREQ_SET_ADDRESS, \
298 .value = uint16_host2usb(address), \
299 .index = uint16_host2usb(0), \
300 .length = uint16_host2usb(0), \
301};
302
303static int hcd_ddf_add_device(ddf_dev_t *parent, usb_dev_t *hub_dev,
304 unsigned port, usb_address_t address, usb_speed_t speed, const char *name,
305 const match_id_list_t *mids)
306{
307 assert(parent);
308
309 char default_name[10] = { 0 }; /* usbxyz-ss */
310 if (!name) {
311 snprintf(default_name, sizeof(default_name) - 1,
312 "usb%u-%cs", address, usb_str_speed(speed)[0]);
313 name = default_name;
314 }
315
316 ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
317 if (!fun)
318 return ENOMEM;
319 usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
320 if (!info) {
321 ddf_fun_destroy(fun);
322 return ENOMEM;
323 }
324 info->address = address;
325 info->speed = speed;
326 info->fun = fun;
327 info->port = port;
328 info->tt_address = hub_dev ? hub_dev->tt_address : -1;
329 link_initialize(&info->link);
330 list_initialize(&info->devices);
331 fibril_mutex_initialize(&info->guard);
332
333 if (hub_dev && hub_dev->speed == USB_SPEED_HIGH && usb_speed_is_11(speed))
334 info->tt_address = hub_dev->address;
335
336 ddf_fun_set_ops(fun, &usb_ops);
337 list_foreach(mids->ids, link, const match_id_t, mid) {
338 ddf_fun_add_match_id(fun, mid->id, mid->score);
339 }
340
341 int ret = ddf_fun_bind(fun);
342 if (ret != EOK) {
343 ddf_fun_destroy(fun);
344 return ret;
345 }
346
347 if (hub_dev) {
348 fibril_mutex_lock(&hub_dev->guard);
349 list_append(&info->link, &hub_dev->devices);
350 fibril_mutex_unlock(&hub_dev->guard);
351 } else {
352 hc_dev_t *hc_dev = dev_to_hc_dev(parent);
353 assert(hc_dev->root_hub == NULL);
354 hc_dev->root_hub = info;
355 }
356 return EOK;
357}
358
359#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
360do { \
361 match_id_t *mid = malloc(sizeof(match_id_t)); \
362 if (!mid) { \
363 clean_match_ids(list); \
364 return ENOMEM; \
365 } \
366 char *id = NULL; \
367 int ret = asprintf(&id, str, ##__VA_ARGS__); \
368 if (ret < 0) { \
369 clean_match_ids(list); \
370 free(mid); \
371 return ENOMEM; \
372 } \
373 mid->score = sc; \
374 mid->id = id; \
375 add_match_id(list, mid); \
376} while (0)
377
378/* This is a copy of lib/usbdev/src/recognise.c */
379static int create_match_ids(match_id_list_t *l,
380 usb_standard_device_descriptor_t *d)
381{
382 assert(l);
383 assert(d);
384
385 if (d->vendor_id != 0) {
386 /* First, with release number. */
387 ADD_MATCHID_OR_RETURN(l, 100,
388 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
389 d->vendor_id, d->product_id, (d->device_version >> 8),
390 (d->device_version & 0xff));
391
392 /* Next, without release number. */
393 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
394 d->vendor_id, d->product_id);
395 }
396
397 /* Class match id */
398 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
399 usb_str_class(d->device_class));
400
401 /* As a last resort, try fallback driver. */
402 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
403
404 return EOK;
405
406}
407
408static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub,
409 unsigned port)
410{
411 assert(device);
412
413 hcd_t *hcd = dev_to_hcd(device);
414 assert(hcd);
415
416 hc_dev_t *hc_dev = dev_to_hc_dev(device);
417 assert(hc_dev);
418
419 fibril_mutex_lock(&hub->guard);
420
421 usb_dev_t *victim = NULL;
422
423 list_foreach(hub->devices, link, usb_dev_t, it) {
424 if (it->port == port) {
425 victim = it;
426 break;
427 }
428 }
429 if (victim && victim->port == port) {
430 list_remove(&victim->link);
431 fibril_mutex_unlock(&hub->guard);
432 const int ret = ddf_fun_unbind(victim->fun);
433 if (ret == EOK) {
434 ddf_fun_destroy(victim->fun);
435 hcd_release_address(hcd, victim->address);
436 } else {
437 usb_log_warning("Failed to unbind device `%s': %s\n",
438 ddf_fun_get_name(victim->fun), str_error(ret));
439 }
440 return EOK;
441 }
442 return ENOENT;
443}
444
445static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port)
446{
447 assert(device);
448
449 hcd_t *hcd = dev_to_hcd(device);
450 assert(hcd);
451
452 usb_speed_t speed = USB_SPEED_MAX;
453
454 /* This checks whether the default address is reserved and gets speed */
455 int ret = usb_bus_get_speed(&hcd->bus, USB_ADDRESS_DEFAULT, &speed);
456 if (ret != EOK) {
457 return ret;
458 }
459
460 static const usb_target_t default_target = {{
461 .address = USB_ADDRESS_DEFAULT,
462 .endpoint = 0,
463 }};
464
465 const usb_address_t address = hcd_request_address(hcd, speed);
466 if (address < 0)
467 return address;
468
469 const usb_target_t target = {{
470 .address = address,
471 .endpoint = 0,
472 }};
473
474 const usb_address_t tt_address = hub ? hub->tt_address : -1;
475
476 /* Add default pipe on default address */
477 ret = hcd_add_ep(hcd,
478 default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
479 CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE,
480 tt_address, port);
481
482 if (ret != EOK) {
483 hcd_release_address(hcd, address);
484 return ret;
485 }
486
487 /* Get max packet size for default pipe */
488 usb_standard_device_descriptor_t desc = { 0 };
489 static const usb_device_request_setup_packet_t get_device_desc_8 =
490 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
491
492 // TODO CALLBACKS
493 ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
494 &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
495 "read first 8 bytes of dev descriptor");
496
497 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
498 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
499 hcd_release_address(hcd, address);
500 return got < 0 ? got : EOVERFLOW;
501 }
502
503 /* Register EP on the new address */
504 ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
505 desc.max_packet_size, desc.max_packet_size, tt_address, port);
506 if (ret != EOK) {
507 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
508 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
509 hcd_release_address(hcd, address);
510 return ret;
511 }
512
513 /* Set new address */
514 const usb_device_request_setup_packet_t set_address =
515 SET_ADDRESS(target.address);
516
517 got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
518 NULL, 0, *(uint64_t *)&set_address, "set address");
519
520 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
521
522 if (got != 0) {
523 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
524 hcd_release_address(hcd, address);
525 return got;
526 }
527
528 /* Get std device descriptor */
529 static const usb_device_request_setup_packet_t get_device_desc =
530 GET_DEVICE_DESC(sizeof(desc));
531
532 got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
533 &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
534 "read device descriptor");
535 if (ret != EOK) {
536 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
537 hcd_release_address(hcd, target.address);
538 return got < 0 ? got : EOVERFLOW;
539 }
540
541 /* Create match ids from the device descriptor */
542 match_id_list_t mids;
543 init_match_ids(&mids);
544
545 ret = create_match_ids(&mids, &desc);
546 if (ret != EOK) {
547 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
548 hcd_release_address(hcd, target.address);
549 return ret;
550 }
551
552 /* Register device */
553 ret = hcd_ddf_add_device(device, hub, port, address, speed, NULL, &mids);
554 clean_match_ids(&mids);
555 if (ret != EOK) {
556 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
557 hcd_release_address(hcd, target.address);
558 }
559
560 return ret;
561}
562
563/** Announce root hub to the DDF
564 *
565 * @param[in] device Host controller ddf device
566 * @return Error code
567 */
568int hcd_ddf_setup_root_hub(ddf_dev_t *device)
569{
570 assert(device);
571 hcd_t *hcd = dev_to_hcd(device);
572 assert(hcd);
573
574 hcd_reserve_default_address(hcd, hcd->bus.max_speed);
575 const int ret = hcd_ddf_new_device(device, NULL, 0);
576 hcd_release_default_address(hcd);
577 return ret;
578}
579
580/** Initialize hc structures.
581 *
582 * @param[in] device DDF instance of the device to use.
583 * @param[in] max_speed Maximum supported USB speed.
584 * @param[in] bw available bandwidth.
585 * @param[in] bw_count Function to compute required ep bandwidth.
586 *
587 * @return Error code.
588 * This function does all the ddf work for hc driver.
589 */
590int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
591 size_t bw, bw_count_func_t bw_count)
592{
593 assert(device);
594
595 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
596 if (instance == NULL) {
597 usb_log_error("Failed to allocate HCD ddf structure.\n");
598 return ENOMEM;
599 }
600 instance->root_hub = NULL;
601 hcd_init(&instance->hcd, max_speed, bw, bw_count);
602
603 int ret = ENOMEM;
604 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
605 if (!instance->ctl_fun) {
606 usb_log_error("Failed to create HCD ddf fun.\n");
607 goto err_destroy_fun;
608 }
609
610 ret = ddf_fun_bind(instance->ctl_fun);
611 if (ret != EOK) {
612 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
613 goto err_destroy_fun;
614 }
615
616 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
617 if (ret != EOK) {
618 usb_log_error("Failed to add fun to category: %s.\n",
619 str_error(ret));
620 ddf_fun_unbind(instance->ctl_fun);
621 goto err_destroy_fun;
622 }
623
624 /* HC should be ok at this point (except it can't do anything) */
625 return EOK;
626
627err_destroy_fun:
628 ddf_fun_destroy(instance->ctl_fun);
629 instance->ctl_fun = NULL;
630 return ret;
631}
632
633void hcd_ddf_clean_hc(ddf_dev_t *device)
634{
635 assert(device);
636 hc_dev_t *hc = dev_to_hc_dev(device);
637 assert(hc);
638 const int ret = ddf_fun_unbind(hc->ctl_fun);
639 if (ret == EOK)
640 ddf_fun_destroy(hc->ctl_fun);
641}
642/**
643 * @}
644 */
Note: See TracBrowser for help on using the repository browser.