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

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

libusbhost: Remove usb_device_manager.

Functions merged to usb_endpoint_manager.

  • Property mode set to 100644
File size: 16.1 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/** 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 list_append(&info->link, &hc_dev->devices);
305 return EOK;
306}
307
308#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
309do { \
310 match_id_t *mid = malloc(sizeof(match_id_t)); \
311 if (!mid) { \
312 clean_match_ids(list); \
313 return ENOMEM; \
314 } \
315 char *id = NULL; \
316 int ret = asprintf(&id, str, ##__VA_ARGS__); \
317 if (ret < 0) { \
318 clean_match_ids(list); \
319 free(mid); \
320 return ENOMEM; \
321 } \
322 mid->score = sc; \
323 mid->id = id; \
324 add_match_id(list, mid); \
325} while (0)
326
327
328/* This is a copy of lib/usbdev/src/recognise.c */
329static int create_match_ids(match_id_list_t *l,
330 usb_standard_device_descriptor_t *d)
331{
332 assert(l);
333 assert(d);
334
335 if (d->vendor_id != 0) {
336 /* First, with release number. */
337 ADD_MATCHID_OR_RETURN(l, 100,
338 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
339 d->vendor_id, d->product_id, (d->device_version >> 8),
340 (d->device_version & 0xff));
341
342 /* Next, without release number. */
343 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
344 d->vendor_id, d->product_id);
345 }
346
347 /* Class match id */
348 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
349 usb_str_class(d->device_class));
350
351 /* As a last resort, try fallback driver. */
352 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
353
354 return EOK;
355
356}
357
358int hcd_ddf_remove_device(ddf_dev_t *device, usb_address_t id)
359{
360 assert(device);
361
362 hcd_t *hcd = dev_to_hcd(device);
363 assert(hcd);
364
365 hc_dev_t *hc_dev = dev_to_hc_dev(device);
366 assert(hc_dev);
367
368 fibril_mutex_lock(&hc_dev->guard);
369
370 usb_dev_t *victim = NULL;
371
372 list_foreach(hc_dev->devices, it) {
373 victim = list_get_instance(it, usb_dev_t, link);
374 if (victim->address == id)
375 break;
376 }
377 if (victim && victim->address == id) {
378 list_remove(&victim->link);
379 fibril_mutex_unlock(&hc_dev->guard);
380 const int ret = ddf_fun_unbind(victim->fun);
381 if (ret == EOK) {
382 ddf_fun_destroy(victim->fun);
383 hcd_release_address(hcd, id);
384 } else {
385 usb_log_warning("Failed to unbind device %d: %s\n",
386 id, str_error(ret));
387 }
388 return EOK;
389 }
390 return ENOENT;
391}
392
393int hcd_ddf_new_device(ddf_dev_t *device, usb_address_t *id)
394{
395 assert(device);
396
397 hcd_t *hcd = dev_to_hcd(device);
398 assert(hcd);
399
400 usb_speed_t speed = USB_SPEED_MAX;
401
402 /* This checks whether the default address is reserved and gets speed */
403 int ret = usb_endpoint_manager_get_info_by_address(&hcd->ep_manager,
404 USB_ADDRESS_DEFAULT, &speed);
405 if (ret != EOK) {
406 return ret;
407 }
408
409 static const usb_target_t default_target = {{
410 .address = USB_ADDRESS_DEFAULT,
411 .endpoint = 0,
412 }};
413
414 const usb_address_t address = hcd_request_address(hcd, speed);
415 if (address < 0)
416 return address;
417
418 const usb_target_t target = {{
419 .address = address,
420 .endpoint = 0,
421 }};
422
423 /* Add default pipe on default address */
424 ret = hcd_add_ep(hcd,
425 default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
426 CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE);
427
428 if (ret != EOK) {
429 hcd_release_address(hcd, address);
430 return ret;
431 }
432
433 /* Get max packet size for default pipe */
434 usb_standard_device_descriptor_t desc = { 0 };
435 static const usb_device_request_setup_packet_t get_device_desc_8 =
436 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
437
438 // TODO CALLBACKS
439 ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
440 &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
441 "read first 8 bytes of dev descriptor");
442
443 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
444 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
445 hcd_release_address(hcd, address);
446 return got < 0 ? got : EOVERFLOW;
447 }
448
449 /* Register EP on the new address */
450 ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
451 desc.max_packet_size, desc.max_packet_size);
452 if (ret != EOK) {
453 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
454 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
455 hcd_release_address(hcd, address);
456 return ret;
457 }
458
459 /* Set new address */
460 const usb_device_request_setup_packet_t set_address =
461 SET_ADDRESS(target.address);
462
463 got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
464 NULL, 0, *(uint64_t *)&set_address, "set address");
465
466 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
467
468 if (got != 0) {
469 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
470 hcd_release_address(hcd, address);
471 return got;
472 }
473
474 /* Get std device descriptor */
475 static const usb_device_request_setup_packet_t get_device_desc =
476 GET_DEVICE_DESC(sizeof(desc));
477
478 got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
479 &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
480 "read device descriptor");
481 if (ret != EOK) {
482 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
483 hcd_release_address(hcd, target.address);
484 return got < 0 ? got : EOVERFLOW;
485 }
486
487 /* Create match ids from the device descriptor */
488 match_id_list_t mids;
489 init_match_ids(&mids);
490
491 ret = create_match_ids(&mids, &desc);
492 if (ret != EOK) {
493 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
494 hcd_release_address(hcd, target.address);
495 return ret;
496 }
497
498 /* Register device */
499 ret = hcd_ddf_add_usb_device(device, address, speed, NULL, &mids);
500 clean_match_ids(&mids);
501 if (ret != EOK) {
502 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
503 hcd_release_address(hcd, target.address);
504 return ret;
505 }
506 if (ret == EOK && id)
507 *id = target.address;
508
509 return ret;
510}
511
512/** Announce root hub to the DDF
513 *
514 * @param[in] device Host controller ddf device
515 * @param[in] speed roothub communication speed
516 * @return Error code
517 */
518int hcd_ddf_setup_root_hub(ddf_dev_t *device, usb_speed_t speed)
519{
520 assert(device);
521 hcd_t *hcd = dev_to_hcd(device);
522 assert(hcd);
523
524 hcd_reserve_default_address(hcd, speed);
525 const int ret = hcd_ddf_new_device(device, NULL);
526 hcd_release_default_address(hcd);
527 return ret;
528}
529
530/** Initialize hc structures.
531 *
532 * @param[in] device DDF instance of the device to use.
533 *
534 * This function does all the ddf work for hc driver.
535 */
536int hcd_ddf_setup_device(ddf_dev_t *device, ddf_fun_t **hc_fun,
537 usb_speed_t max_speed, size_t bw, bw_count_func_t bw_count)
538{
539 if (device == NULL)
540 return EBADMEM;
541
542 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
543 if (instance == NULL) {
544 usb_log_error("Failed to allocate HCD ddf structure.\n");
545 return ENOMEM;
546 }
547 list_initialize(&instance->devices);
548 fibril_mutex_initialize(&instance->guard);
549
550#define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
551if (ret != EOK) { \
552 if (instance->hc_fun) { \
553 ddf_fun_destroy(instance->hc_fun); \
554 } \
555 usb_log_error(message); \
556 return ret; \
557} else (void)0
558
559 instance->hc_fun = ddf_fun_create(device, fun_exposed, "hc");
560 int ret = instance->hc_fun ? EOK : ENOMEM;
561 CHECK_RET_DEST_FREE_RETURN(ret,
562 "Failed to create HCD HC function: %s.\n", str_error(ret));
563 ddf_fun_set_ops(instance->hc_fun, &hc_ops);
564 hcd_t *hcd = ddf_fun_data_alloc(instance->hc_fun, sizeof(hcd_t));
565 ret = hcd ? EOK : ENOMEM;
566 CHECK_RET_DEST_FREE_RETURN(ret,
567 "Failed to allocate HCD structure: %s.\n", str_error(ret));
568
569 hcd_init(hcd, max_speed, bw, bw_count);
570
571 ret = ddf_fun_bind(instance->hc_fun);
572 CHECK_RET_DEST_FREE_RETURN(ret,
573 "Failed to bind HCD device function: %s.\n", str_error(ret));
574
575#define CHECK_RET_UNBIND_FREE_RETURN(ret, message...) \
576if (ret != EOK) { \
577 ddf_fun_unbind(instance->hc_fun); \
578 CHECK_RET_DEST_FREE_RETURN(ret, message); \
579} else (void)0
580 ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
581 CHECK_RET_UNBIND_FREE_RETURN(ret,
582 "Failed to add hc to category: %s\n", str_error(ret));
583
584 /* HC should be ok at this point (except it can't do anything) */
585 if (hc_fun)
586 *hc_fun = instance->hc_fun;
587
588 return EOK;
589}
590
591/**
592 * @}
593 */
Note: See TracBrowser for help on using the repository browser.