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

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

libusbhost: remove old iface

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