source: mainline/uspace/lib/usbdev/src/driver.c@ 9efad54

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9efad54 was c54b898, checked in by Petr Manek <petr.manek@…>, 7 years ago

usbdev: refactoring

The device_rem driver callback was renamed to device_remove. Also,
a new device_removed callback was introduced. This callback will fire
after all device pipes (endpoints, internally) are unregistered from
the HC device after a call to device_remove. It is semantically
a better opportunity for the USB device driver to free resources and
join all fibrils, since endpoint unregistration will force abort all
active transfers, waking up their issuers.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2013 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup libusbdev
30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
35
36#include <usb/dev/driver.h>
37#include <usb/dev/device.h>
38#include <usb/debug.h>
39
40#include <assert.h>
41#include <errno.h>
42#include <str_error.h>
43#include <ddf/driver.h>
44
45static const usb_driver_t *driver = NULL;
46
47/** Callback when a new device is supposed to be controlled by this driver.
48 *
49 * This callback is a wrapper for USB specific version of @c device_add.
50 *
51 * @param gen_dev Device structure as prepared by DDF.
52 * @return Error code.
53 */
54static int generic_device_add(ddf_dev_t *gen_dev)
55{
56 assert(driver);
57 assert(driver->ops);
58 assert(driver->ops->device_add);
59
60 /* Initialize generic USB driver data. */
61 const char *err_msg = NULL;
62 int rc = usb_device_create_ddf(gen_dev, driver->endpoints, &err_msg);
63 if (rc != EOK) {
64 usb_log_error("USB device `%s' init failed (%s): %s.\n",
65 ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
66 return rc;
67 }
68
69 /* Start USB driver specific initialization. */
70 rc = driver->ops->device_add(ddf_dev_data_get(gen_dev));
71 if (rc != EOK)
72 usb_device_destroy_ddf(gen_dev);
73 return rc;
74}
75
76/** Callback when a device is supposed to be removed from the system.
77 *
78 * This callback is a wrapper for USB specific version of @c device_remove.
79 *
80 * @param gen_dev Device structure as prepared by DDF.
81 * @return Error code.
82 */
83static int generic_device_remove(ddf_dev_t *gen_dev)
84{
85 assert(driver);
86 assert(driver->ops);
87 if (driver->ops->device_remove == NULL)
88 return ENOTSUP;
89
90 /* Just tell the driver to stop whatever it is doing */
91 usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
92 int ret = driver->ops->device_remove(usb_dev);
93 if (ret != EOK)
94 return ret;
95
96 /* Notify the driver after endpoints were unregistered. */
97 usb_device_destroy_pipes(usb_dev);
98 if (driver->ops->device_removed != NULL) {
99 ret = driver->ops->device_removed(usb_dev);
100 if (ret != EOK)
101 return ret;
102 }
103
104 usb_device_destroy_ddf(gen_dev);
105 return EOK;
106}
107
108/** Callback when a device was removed from the system.
109 *
110 * This callback is a wrapper for USB specific version of @c device_gone.
111 *
112 * @param gen_dev Device structure as prepared by DDF.
113 * @return Error code.
114 */
115static int generic_device_gone(ddf_dev_t *gen_dev)
116{
117 assert(driver);
118 assert(driver->ops);
119 if (driver->ops->device_gone == NULL)
120 return ENOTSUP;
121 usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
122 const int ret = driver->ops->device_gone(usb_dev);
123 if (ret == EOK)
124 usb_device_destroy_ddf(gen_dev);
125
126 return ret;
127}
128
129/** Callback when the driver is asked to online a specific function.
130 *
131 * This callback is a wrapper for USB specific version of @c fun_online.
132 *
133 * @param gen_dev Device function structure as prepared by DDF.
134 * @return Error code.
135 */
136static int generic_function_online(ddf_fun_t *fun)
137{
138 assert(driver);
139 assert(driver->ops);
140 if (driver->ops->function_online == NULL)
141 return ENOTSUP;
142 return driver->ops->function_online(fun);
143}
144
145/** Callback when the driver is asked to offline a specific function.
146 *
147 * This callback is a wrapper for USB specific version of @c fun_offline.
148 *
149 * @param gen_dev Device function structure as prepared by DDF.
150 * @return Error code.
151 */
152static int generic_function_offline(ddf_fun_t *fun)
153{
154 assert(driver);
155 assert(driver->ops);
156 if (driver->ops->function_offline == NULL)
157 return ENOTSUP;
158 return driver->ops->function_offline(fun);
159}
160
161static driver_ops_t generic_driver_ops = {
162 .dev_add = generic_device_add,
163 .dev_remove = generic_device_remove,
164 .dev_gone = generic_device_gone,
165 .fun_online = generic_function_online,
166 .fun_offline = generic_function_offline,
167};
168static driver_t generic_driver = {
169 .driver_ops = &generic_driver_ops
170};
171
172
173/** Main routine of USB device driver.
174 *
175 * Under normal conditions, this function never returns.
176 *
177 * @param drv USB device driver structure.
178 * @return Task exit status.
179 */
180int usb_driver_main(const usb_driver_t *drv)
181{
182 assert(drv != NULL);
183
184 /* Prepare the generic driver. */
185 generic_driver.name = drv->name;
186
187 driver = drv;
188
189 return ddf_driver_main(&generic_driver);
190}
191
192/**
193 * @}
194 */
Note: See TracBrowser for help on using the repository browser.