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

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

libusbhost: Implement endpoint management for usb iface.

  • Property mode set to 100644
File size: 16.3 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;
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/** Root hub USB interface */
219static usb_iface_t usb_iface = {
220 .get_hc_handle = get_hc_handle,
221 .get_my_address = get_my_address,
222
223 .reserve_default_address = reserve_default_address,
224 .release_default_address = release_default_address,
225 .device_enumerate = device_enumerate,
226 .device_remove = device_remove,
227 .register_endpoint = register_endpoint,
228 .unregister_endpoint = unregister_endpoint,
229};
230/** Standard USB RH options (RH interface) */
231static ddf_dev_ops_t usb_ops = {
232 .interfaces[USB_DEV_IFACE] = &usb_iface,
233};
234
235/** Standard USB HC options (HC interface) */
236static ddf_dev_ops_t hc_ops = {
237 .interfaces[USBHC_DEV_IFACE] = &hcd_iface,
238};
239
240#define GET_DEVICE_DESC(size) \
241{ \
242 .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
243 | (USB_REQUEST_TYPE_STANDARD << 5) \
244 | USB_REQUEST_RECIPIENT_DEVICE, \
245 .request = USB_DEVREQ_GET_DESCRIPTOR, \
246 .value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
247 .index = uint16_host2usb(0), \
248 .length = uint16_host2usb(size), \
249};
250
251#define SET_ADDRESS(address) \
252{ \
253 .request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
254 | (USB_REQUEST_TYPE_STANDARD << 5) \
255 | USB_REQUEST_RECIPIENT_DEVICE, \
256 .request = USB_DEVREQ_SET_ADDRESS, \
257 .value = uint16_host2usb(address), \
258 .index = uint16_host2usb(0), \
259 .length = uint16_host2usb(0), \
260};
261
262int hcd_ddf_add_usb_device(ddf_dev_t *parent,
263 usb_address_t address, usb_speed_t speed, const char *name,
264 const match_id_list_t *mids)
265{
266 assert(parent);
267 hc_dev_t *hc_dev = dev_to_hc_dev(parent);
268 devman_handle_t hc_handle = ddf_fun_get_handle(hc_dev->hc_fun);
269
270 char default_name[10] = { 0 }; /* usbxyz-ss */
271 if (!name) {
272 snprintf(default_name, sizeof(default_name) - 1,
273 "usb%u-%cs", address, usb_str_speed(speed)[0]);
274 name = default_name;
275 }
276
277 //TODO more checks
278 ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
279 if (!fun)
280 return ENOMEM;
281 usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
282 if (!info) {
283 ddf_fun_destroy(fun);
284 return ENOMEM;
285 }
286 info->address = address;
287 info->speed = speed;
288 info->hc_handle = hc_handle;
289 info->fun = fun;
290 link_initialize(&info->link);
291
292 ddf_fun_set_ops(fun, &usb_ops);
293 list_foreach(mids->ids, iter) {
294 match_id_t *mid = list_get_instance(iter, match_id_t, link);
295 ddf_fun_add_match_id(fun, mid->id, mid->score);
296 }
297
298 int ret = ddf_fun_bind(fun);
299 if (ret != EOK) {
300 ddf_fun_destroy(fun);
301 return ret;
302 }
303
304 ret = usb_device_manager_bind_address(&dev_to_hcd(parent)->dev_manager,
305 address, ddf_fun_get_handle(fun));
306 if (ret != EOK)
307 usb_log_warning("Failed to bind address: %s.\n",
308 str_error(ret));
309
310 list_append(&info->link, &hc_dev->devices);
311 return EOK;
312}
313
314#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
315do { \
316 match_id_t *mid = malloc(sizeof(match_id_t)); \
317 if (!mid) { \
318 clean_match_ids(list); \
319 return ENOMEM; \
320 } \
321 char *id = NULL; \
322 int ret = asprintf(&id, str, ##__VA_ARGS__); \
323 if (ret < 0) { \
324 clean_match_ids(list); \
325 free(mid); \
326 return ENOMEM; \
327 } \
328 mid->score = sc; \
329 mid->id = id; \
330 add_match_id(list, mid); \
331} while (0)
332
333
334/* This is a copy of lib/usbdev/src/recognise.c */
335static int create_match_ids(match_id_list_t *l,
336 usb_standard_device_descriptor_t *d)
337{
338 assert(l);
339 assert(d);
340
341 if (d->vendor_id != 0) {
342 /* First, with release number. */
343 ADD_MATCHID_OR_RETURN(l, 100,
344 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
345 d->vendor_id, d->product_id, (d->device_version >> 8),
346 (d->device_version & 0xff));
347
348 /* Next, without release number. */
349 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
350 d->vendor_id, d->product_id);
351 }
352
353 /* Class match id */
354 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
355 usb_str_class(d->device_class));
356
357 /* As a last resort, try fallback driver. */
358 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
359
360 return EOK;
361
362}
363
364int hcd_ddf_remove_device(ddf_dev_t *device, usb_address_t id)
365{
366 assert(device);
367
368 hcd_t *hcd = dev_to_hcd(device);
369 assert(hcd);
370
371 hc_dev_t *hc_dev = dev_to_hc_dev(device);
372 assert(hc_dev);
373
374 fibril_mutex_lock(&hc_dev->guard);
375
376 usb_dev_t *victim = NULL;
377
378 list_foreach(hc_dev->devices, it) {
379 victim = list_get_instance(it, usb_dev_t, link);
380 if (victim->address == id)
381 break;
382 }
383 if (victim && victim->address == id) {
384 list_remove(&victim->link);
385 fibril_mutex_unlock(&hc_dev->guard);
386 const int ret = ddf_fun_unbind(victim->fun);
387 if (ret == EOK) {
388 ddf_fun_destroy(victim->fun);
389 hcd_release_address(hcd, id);
390 } else {
391 usb_log_warning("Failed to unbind device %d: %s\n",
392 id, str_error(ret));
393 }
394 return EOK;
395 }
396 return ENOENT;
397}
398
399int hcd_ddf_new_device(ddf_dev_t *device, usb_address_t *id)
400{
401 assert(device);
402
403 hcd_t *hcd = dev_to_hcd(device);
404 assert(hcd);
405
406 usb_speed_t speed = USB_SPEED_MAX;
407
408 /* This checks whether the default address is reserved and gets speed */
409 int ret = usb_device_manager_get_info_by_address(&hcd->dev_manager,
410 USB_ADDRESS_DEFAULT, NULL, &speed);
411 if (ret != EOK) {
412 return ret;
413 }
414
415 static const usb_target_t default_target = {{
416 .address = USB_ADDRESS_DEFAULT,
417 .endpoint = 0,
418 }};
419
420 const usb_address_t address = hcd_request_address(hcd, speed);
421 if (address < 0)
422 return address;
423
424 const usb_target_t target = {{
425 .address = address,
426 .endpoint = 0,
427 }};
428
429 /* Add default pipe on default address */
430 ret = hcd_add_ep(hcd,
431 default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
432 CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE);
433
434 if (ret != EOK) {
435 hcd_release_address(hcd, address);
436 return ret;
437 }
438
439 /* Get max packet size for default pipe */
440 usb_standard_device_descriptor_t desc = { 0 };
441 static const usb_device_request_setup_packet_t get_device_desc_8 =
442 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
443
444 // TODO CALLBACKS
445 ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
446 &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
447 "read first 8 bytes of dev descriptor");
448
449 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
450 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
451 hcd_release_address(hcd, address);
452 return got < 0 ? got : EOVERFLOW;
453 }
454
455 /* Register EP on the new address */
456 ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
457 desc.max_packet_size, desc.max_packet_size);
458 if (ret != EOK) {
459 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
460 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
461 hcd_release_address(hcd, address);
462 return ret;
463 }
464
465 /* Set new address */
466 const usb_device_request_setup_packet_t set_address =
467 SET_ADDRESS(target.address);
468
469 got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
470 NULL, 0, *(uint64_t *)&set_address, "set address");
471
472 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
473
474 if (got != 0) {
475 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
476 hcd_release_address(hcd, address);
477 return got;
478 }
479
480 /* Get std device descriptor */
481 static const usb_device_request_setup_packet_t get_device_desc =
482 GET_DEVICE_DESC(sizeof(desc));
483
484 got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
485 &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
486 "read device descriptor");
487 if (ret != EOK) {
488 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
489 hcd_release_address(hcd, target.address);
490 return got < 0 ? got : EOVERFLOW;
491 }
492
493 /* Create match ids from the device descriptor */
494 match_id_list_t mids;
495 init_match_ids(&mids);
496
497 ret = create_match_ids(&mids, &desc);
498 if (ret != EOK) {
499 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
500 hcd_release_address(hcd, target.address);
501 return ret;
502 }
503
504 /* Register device */
505 ret = hcd_ddf_add_usb_device(device, address, speed, NULL, &mids);
506 clean_match_ids(&mids);
507 if (ret != EOK) {
508 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
509 hcd_release_address(hcd, target.address);
510 return ret;
511 }
512 if (ret == EOK && id)
513 *id = target.address;
514
515 return ret;
516}
517
518/** Announce root hub to the DDF
519 *
520 * @param[in] device Host controller ddf device
521 * @param[in] speed roothub communication speed
522 * @return Error code
523 */
524int hcd_ddf_setup_root_hub(ddf_dev_t *device, usb_speed_t speed)
525{
526 assert(device);
527 hcd_t *hcd = dev_to_hcd(device);
528 assert(hcd);
529
530 hcd_reserve_default_address(hcd, speed);
531 const int ret = hcd_ddf_new_device(device, NULL);
532 hcd_release_default_address(hcd);
533 return ret;
534}
535
536/** Initialize hc structures.
537 *
538 * @param[in] device DDF instance of the device to use.
539 *
540 * This function does all the ddf work for hc driver.
541 */
542int hcd_ddf_setup_device(ddf_dev_t *device, ddf_fun_t **hc_fun,
543 usb_speed_t max_speed, size_t bw, bw_count_func_t bw_count)
544{
545 if (device == NULL)
546 return EBADMEM;
547
548 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
549 if (instance == NULL) {
550 usb_log_error("Failed to allocate HCD ddf structure.\n");
551 return ENOMEM;
552 }
553 list_initialize(&instance->devices);
554 fibril_mutex_initialize(&instance->guard);
555
556#define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
557if (ret != EOK) { \
558 if (instance->hc_fun) { \
559 ddf_fun_destroy(instance->hc_fun); \
560 } \
561 usb_log_error(message); \
562 return ret; \
563} else (void)0
564
565 instance->hc_fun = ddf_fun_create(device, fun_exposed, "hc");
566 int ret = instance->hc_fun ? EOK : ENOMEM;
567 CHECK_RET_DEST_FREE_RETURN(ret,
568 "Failed to create HCD HC function: %s.\n", str_error(ret));
569 ddf_fun_set_ops(instance->hc_fun, &hc_ops);
570 hcd_t *hcd = ddf_fun_data_alloc(instance->hc_fun, sizeof(hcd_t));
571 ret = hcd ? EOK : ENOMEM;
572 CHECK_RET_DEST_FREE_RETURN(ret,
573 "Failed to allocate HCD structure: %s.\n", str_error(ret));
574
575 hcd_init(hcd, max_speed, bw, bw_count);
576
577 ret = ddf_fun_bind(instance->hc_fun);
578 CHECK_RET_DEST_FREE_RETURN(ret,
579 "Failed to bind HCD device function: %s.\n", str_error(ret));
580
581#define CHECK_RET_UNBIND_FREE_RETURN(ret, message...) \
582if (ret != EOK) { \
583 ddf_fun_unbind(instance->hc_fun); \
584 CHECK_RET_DEST_FREE_RETURN(ret, message); \
585} else (void)0
586 ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
587 CHECK_RET_UNBIND_FREE_RETURN(ret,
588 "Failed to add hc to category: %s\n", str_error(ret));
589
590 /* HC should be ok at this point (except it can't do anything) */
591 if (hc_fun)
592 *hc_fun = instance->hc_fun;
593
594 return EOK;
595}
596
597/**
598 * @}
599 */
Note: See TracBrowser for help on using the repository browser.