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

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

Implement and use usb_get_device_handle.

Enables object instantiation on usb interface functions.
avoids forwarding in usbmid driver.

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