source: mainline/uspace/lib/usb/src/devdrv.c@ bb18a59

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bb18a59 was bb18a59, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Ensure "my interface" is alway initialized

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[6105fc0]1/*
2 * Copyright (c) 2011 Vojtech Horky
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 libusb
30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
35#include <usb/devdrv.h>
36#include <usb/request.h>
37#include <usb/debug.h>
38#include <errno.h>
[69334af]39#include <str_error.h>
[6105fc0]40#include <assert.h>
41
42static int generic_add_device(ddf_dev_t *);
43
44static driver_ops_t generic_driver_ops = {
45 .add_device = generic_add_device
46};
47static driver_t generic_driver = {
48 .driver_ops = &generic_driver_ops
49};
50
51static usb_driver_t *driver = NULL;
52
[69334af]53
54/** Main routine of USB device driver.
55 *
56 * Under normal conditions, this function never returns.
57 *
58 * @param drv USB device driver structure.
59 * @return Task exit status.
60 */
[6105fc0]61int usb_driver_main(usb_driver_t *drv)
62{
[69334af]63 assert(drv != NULL);
64
[6105fc0]65 /* Prepare the generic driver. */
66 generic_driver.name = drv->name;
67
68 driver = drv;
69
70 return ddf_driver_main(&generic_driver);
71}
72
[69334af]73/** Log out of memory error on given device.
74 *
75 * @param dev Device causing the trouble.
76 */
77static void usb_log_oom(ddf_dev_t *dev)
78{
79 usb_log_error("Out of memory when adding device `%s'.\n",
80 dev->name);
81}
82
83/** Count number of pipes the driver expects.
84 *
85 * @param drv USB driver.
86 * @return Number of pipes (excluding default control pipe).
87 */
[6105fc0]88static size_t count_other_pipes(usb_driver_t *drv)
89{
90 size_t count = 0;
91 if (drv->endpoints == NULL) {
92 return 0;
93 }
94
95 while (drv->endpoints[count] != NULL) {
96 count++;
97 }
98
99 return count;
100}
101
[69334af]102/** Initialize endpoint pipes, excluding default control one.
103 *
104 * @param drv The device driver.
105 * @param dev Device to be initialized.
106 * @return Error code.
107 */
[6105fc0]108static int initialize_other_pipes(usb_driver_t *drv, usb_device_t *dev)
109{
[69334af]110 int rc;
[6105fc0]111
112 size_t pipe_count = count_other_pipes(drv);
113 dev->pipes = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
[69334af]114 if (dev->pipes == NULL) {
115 usb_log_oom(dev->ddf_dev);
116 return ENOMEM;
117 }
[6105fc0]118
119 size_t i;
[69334af]120
121 /* Initialize to NULL first for rollback purposes. */
122 for (i = 0; i < pipe_count; i++) {
123 dev->pipes[i].pipe = NULL;
124 }
125
[6105fc0]126 for (i = 0; i < pipe_count; i++) {
[a372663]127 dev->pipes[i].pipe = malloc(sizeof(usb_pipe_t));
[69334af]128 if (dev->pipes[i].pipe == NULL) {
129 usb_log_oom(dev->ddf_dev);
130 rc = ENOMEM;
131 goto rollback;
132 }
133
[6105fc0]134 dev->pipes[i].description = drv->endpoints[i];
[d71691d]135 dev->pipes[i].interface_no = dev->interface_no;
[6105fc0]136 }
137
[e484f3b]138 rc = usb_pipe_initialize_from_configuration(dev->pipes, pipe_count,
139 dev->descriptors.configuration, dev->descriptors.configuration_size,
140 &dev->wire);
[69334af]141 if (rc != EOK) {
142 usb_log_error("Failed initializing USB endpoints: %s.\n",
143 str_error(rc));
144 goto rollback;
145 }
[6105fc0]146
[5fd22d8]147 /* Register the endpoints. */
148 usb_hc_connection_t hc_conn;
149 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev->ddf_dev);
150 if (rc != EOK) {
151 usb_log_error(
152 "Failed initializing connection to host controller: %s.\n",
153 str_error(rc));
154 goto rollback;
155 }
156 rc = usb_hc_connection_open(&hc_conn);
157 if (rc != EOK) {
158 usb_log_error("Failed to connect to host controller: %s.\n",
159 str_error(rc));
160 goto rollback;
161 }
162 for (i = 0; i < pipe_count; i++) {
163 if (dev->pipes[i].present) {
[3954a63b]164 rc = usb_pipe_register(dev->pipes[i].pipe,
[5fd22d8]165 dev->pipes[i].descriptor->poll_interval,
166 &hc_conn);
167 /* Ignore error when operation not supported by HC. */
168 if ((rc != EOK) && (rc != ENOTSUP)) {
169 /* FIXME: what shall we do? */
170 dev->pipes[i].present = false;
171 free(dev->pipes[i].pipe);
172 dev->pipes[i].pipe = NULL;
173 }
174 }
175 }
176 /* Ignoring errors here. */
177 usb_hc_connection_close(&hc_conn);
178
[6105fc0]179 return EOK;
[69334af]180
181rollback:
182 for (i = 0; i < pipe_count; i++) {
183 if (dev->pipes[i].pipe != NULL) {
184 free(dev->pipes[i].pipe);
185 }
186 }
187 free(dev->pipes);
188
189 return rc;
[6105fc0]190}
191
[69334af]192/** Initialize all endpoint pipes.
193 *
194 * @param drv The driver.
195 * @param dev The device to be initialized.
196 * @return Error code.
197 */
[09daa8b]198static int initialize_pipes(usb_device_t *dev)
[69334af]199{
200 int rc;
201
202 rc = usb_device_connection_initialize_from_device(&dev->wire,
203 dev->ddf_dev);
204 if (rc != EOK) {
205 usb_log_error(
206 "Failed initializing connection on device `%s'. %s.\n",
207 dev->ddf_dev->name, str_error(rc));
208 return rc;
209 }
210
[3954a63b]211 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
[69334af]212 &dev->wire);
213 if (rc != EOK) {
214 usb_log_error("Failed to initialize default control pipe " \
215 "on device `%s': %s.\n",
216 dev->ddf_dev->name, str_error(rc));
217 return rc;
218 }
219
[3954a63b]220 rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
[206f71a]221 if (rc != EOK) {
222 usb_log_error(
223 "Probing default control pipe on device `%s' failed: %s.\n",
224 dev->ddf_dev->name, str_error(rc));
225 return rc;
226 }
227
[bb18a59]228 /* Get our interface. */
229 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
230
[69334af]231 /*
[e484f3b]232 * For further actions, we need open session on default control pipe.
[69334af]233 */
[3954a63b]234 rc = usb_pipe_start_session(&dev->ctrl_pipe);
[69334af]235 if (rc != EOK) {
236 usb_log_error("Failed to start an IPC session: %s.\n",
237 str_error(rc));
238 return rc;
239 }
240
[e484f3b]241 /* Get the device descriptor. */
242 rc = usb_request_get_device_descriptor(&dev->ctrl_pipe,
243 &dev->descriptors.device);
244 if (rc != EOK) {
245 usb_log_error("Failed to retrieve device descriptor: %s.\n",
246 str_error(rc));
247 return rc;
248 }
249
250 /* Get the full configuration descriptor. */
251 rc = usb_request_get_full_configuration_descriptor_alloc(
252 &dev->ctrl_pipe, 0, (void **) &dev->descriptors.configuration,
253 &dev->descriptors.configuration_size);
254 if (rc != EOK) {
255 usb_log_error("Failed retrieving configuration descriptor: %s.\n",
256 dev->ddf_dev->name, str_error(rc));
257 return rc;
258 }
259
[69334af]260 if (driver->endpoints != NULL) {
261 rc = initialize_other_pipes(driver, dev);
262 }
263
264 /* No checking here. */
[3954a63b]265 usb_pipe_end_session(&dev->ctrl_pipe);
[69334af]266
[e484f3b]267 /* Rollback actions. */
268 if (rc != EOK) {
269 if (dev->descriptors.configuration != NULL) {
270 free(dev->descriptors.configuration);
271 }
272 }
273
[69334af]274 return rc;
275}
276
277/** Callback when new device is supposed to be controlled by this driver.
278 *
279 * This callback is a wrapper for USB specific version of @c add_device.
280 *
281 * @param gen_dev Device structure as prepared by DDF.
282 * @return Error code.
283 */
[6105fc0]284int generic_add_device(ddf_dev_t *gen_dev)
285{
286 assert(driver);
287 assert(driver->ops);
288 assert(driver->ops->add_device);
289
290 int rc;
291
292 usb_device_t *dev = malloc(sizeof(usb_device_t));
[69334af]293 if (dev == NULL) {
294 usb_log_error("Out of memory when adding device `%s'.\n",
295 gen_dev->name);
296 return ENOMEM;
297 }
298
[6105fc0]299
300 dev->ddf_dev = gen_dev;
[69334af]301 dev->ddf_dev->driver_data = dev;
[6105fc0]302 dev->driver_data = NULL;
[e484f3b]303 dev->descriptors.configuration = NULL;
[6105fc0]304
[09daa8b]305 rc = initialize_pipes(dev);
[69334af]306 if (rc != EOK) {
307 free(dev);
308 return rc;
[6105fc0]309 }
310
311 return driver->ops->add_device(dev);
312}
313
314/**
315 * @}
316 */
Note: See TracBrowser for help on using the repository browser.