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

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

libusbhost: Move interrupt enabling to library.

Reduce code duplication.

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