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

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

libusbhost: release address on device removal

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