source: mainline/uspace/lib/usbdev/src/driver.c@ 1d758fc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d758fc was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 7 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

  • 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 * 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 errno_t 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 errno_t 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.",
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 errno_t 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 const errno_t ret = driver->ops->device_remove(usb_dev);
93 if (ret != EOK)
94 return ret;
95
96 usb_device_destroy_ddf(gen_dev);
97 return EOK;
98}
99
100/** Callback when a device was removed from the system.
101 *
102 * This callback is a wrapper for USB specific version of @c device_gone.
103 *
104 * @param gen_dev Device structure as prepared by DDF.
105 * @return Error code.
106 */
107static errno_t generic_device_gone(ddf_dev_t *gen_dev)
108{
109 assert(driver);
110 assert(driver->ops);
111 if (driver->ops->device_gone == NULL)
112 return ENOTSUP;
113 usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
114 const errno_t ret = driver->ops->device_gone(usb_dev);
115 if (ret == EOK)
116 usb_device_destroy_ddf(gen_dev);
117
118 return ret;
119}
120
121/** Callback when the driver is asked to online a specific function.
122 *
123 * This callback is a wrapper for USB specific version of @c fun_online.
124 *
125 * @param gen_dev Device function structure as prepared by DDF.
126 * @return Error code.
127 */
128static int generic_function_online(ddf_fun_t *fun)
129{
130 assert(driver);
131 assert(driver->ops);
132 if (driver->ops->function_online == NULL)
133 return ENOTSUP;
134 return driver->ops->function_online(fun);
135}
136
137/** Callback when the driver is asked to offline a specific function.
138 *
139 * This callback is a wrapper for USB specific version of @c fun_offline.
140 *
141 * @param gen_dev Device function structure as prepared by DDF.
142 * @return Error code.
143 */
144static int generic_function_offline(ddf_fun_t *fun)
145{
146 assert(driver);
147 assert(driver->ops);
148 if (driver->ops->function_offline == NULL)
149 return ENOTSUP;
150 return driver->ops->function_offline(fun);
151}
152
153static driver_ops_t generic_driver_ops = {
154 .dev_add = generic_device_add,
155 .dev_remove = generic_device_remove,
156 .dev_gone = generic_device_gone,
157 .fun_online = generic_function_online,
158 .fun_offline = generic_function_offline,
159};
160static driver_t generic_driver = {
161 .driver_ops = &generic_driver_ops
162};
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.