source: mainline/uspace/lib/usbdev/src/driver.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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