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 |
|
---|
45 | static 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 | */
|
---|
54 | static 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 | */
|
---|
83 | static int generic_device_remove(ddf_dev_t *gen_dev)
|
---|
84 | {
|
---|
85 | assert(driver);
|
---|
86 | assert(driver->ops);
|
---|
87 | if (driver->ops->device_rem == NULL)
|
---|
88 | return ENOTSUP;
|
---|
89 | /* Just tell the driver to stop whatever it is doing */
|
---|
90 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
91 | const int ret = driver->ops->device_rem(usb_dev);
|
---|
92 | if (ret != EOK)
|
---|
93 | return ret;
|
---|
94 | usb_device_destroy_ddf(gen_dev);
|
---|
95 | return EOK;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Callback when a device was removed from the system.
|
---|
99 | *
|
---|
100 | * This callback is a wrapper for USB specific version of @c device_gone.
|
---|
101 | *
|
---|
102 | * @param gen_dev Device structure as prepared by DDF.
|
---|
103 | * @return Error code.
|
---|
104 | */
|
---|
105 | static int generic_device_gone(ddf_dev_t *gen_dev)
|
---|
106 | {
|
---|
107 | assert(driver);
|
---|
108 | assert(driver->ops);
|
---|
109 | if (driver->ops->device_gone == NULL)
|
---|
110 | return ENOTSUP;
|
---|
111 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
112 | const int ret = driver->ops->device_gone(usb_dev);
|
---|
113 | if (ret == EOK)
|
---|
114 | usb_device_destroy_ddf(gen_dev);
|
---|
115 |
|
---|
116 | return ret;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Callback when the driver is asked to online a specific function.
|
---|
120 | *
|
---|
121 | * This callback is a wrapper for USB specific version of @c fun_online.
|
---|
122 | *
|
---|
123 | * @param gen_dev Device function structure as prepared by DDF.
|
---|
124 | * @return Error code.
|
---|
125 | */
|
---|
126 | static int generic_function_online(ddf_fun_t *fun)
|
---|
127 | {
|
---|
128 | assert(driver);
|
---|
129 | assert(driver->ops);
|
---|
130 | if (driver->ops->function_online == NULL)
|
---|
131 | return ENOTSUP;
|
---|
132 | return driver->ops->function_online(fun);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Callback when the driver is asked to offline a specific function.
|
---|
136 | *
|
---|
137 | * This callback is a wrapper for USB specific version of @c fun_offline.
|
---|
138 | *
|
---|
139 | * @param gen_dev Device function structure as prepared by DDF.
|
---|
140 | * @return Error code.
|
---|
141 | */
|
---|
142 | static int generic_function_offline(ddf_fun_t *fun)
|
---|
143 | {
|
---|
144 | assert(driver);
|
---|
145 | assert(driver->ops);
|
---|
146 | if (driver->ops->function_offline == NULL)
|
---|
147 | return ENOTSUP;
|
---|
148 | return driver->ops->function_offline(fun);
|
---|
149 | }
|
---|
150 |
|
---|
151 | static driver_ops_t generic_driver_ops = {
|
---|
152 | .dev_add = generic_device_add,
|
---|
153 | .dev_remove = generic_device_remove,
|
---|
154 | .dev_gone = generic_device_gone,
|
---|
155 | .fun_online = generic_function_online,
|
---|
156 | .fun_offline = generic_function_offline,
|
---|
157 | };
|
---|
158 | static driver_t generic_driver = {
|
---|
159 | .driver_ops = &generic_driver_ops
|
---|
160 | };
|
---|
161 |
|
---|
162 |
|
---|
163 | /** Main routine of USB device driver.
|
---|
164 | *
|
---|
165 | * Under normal conditions, this function never returns.
|
---|
166 | *
|
---|
167 | * @param drv USB device driver structure.
|
---|
168 | * @return Task exit status.
|
---|
169 | */
|
---|
170 | int usb_driver_main(const usb_driver_t *drv)
|
---|
171 | {
|
---|
172 | assert(drv != NULL);
|
---|
173 |
|
---|
174 | /* Prepare the generic driver. */
|
---|
175 | generic_driver.name = drv->name;
|
---|
176 |
|
---|
177 | driver = drv;
|
---|
178 |
|
---|
179 | return ddf_driver_main(&generic_driver);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * @}
|
---|
184 | */
|
---|