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

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

USB device structure remembers interface

  • Property mode set to 100644
File size: 7.4 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;
[d71691d]111 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
[6105fc0]112
113 size_t pipe_count = count_other_pipes(drv);
114 dev->pipes = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
[69334af]115 if (dev->pipes == NULL) {
116 usb_log_oom(dev->ddf_dev);
117 return ENOMEM;
118 }
[6105fc0]119
120 size_t i;
[69334af]121
122 /* Initialize to NULL first for rollback purposes. */
123 for (i = 0; i < pipe_count; i++) {
124 dev->pipes[i].pipe = NULL;
125 }
126
[6105fc0]127 for (i = 0; i < pipe_count; i++) {
128 dev->pipes[i].pipe = malloc(sizeof(usb_endpoint_pipe_t));
[69334af]129 if (dev->pipes[i].pipe == NULL) {
130 usb_log_oom(dev->ddf_dev);
131 rc = ENOMEM;
132 goto rollback;
133 }
134
[6105fc0]135 dev->pipes[i].description = drv->endpoints[i];
[d71691d]136 dev->pipes[i].interface_no = dev->interface_no;
[6105fc0]137 }
138
139 void *config_descriptor;
140 size_t config_descriptor_size;
[69334af]141 rc = usb_request_get_full_configuration_descriptor_alloc(
142 &dev->ctrl_pipe, 0, &config_descriptor, &config_descriptor_size);
143 if (rc != EOK) {
144 usb_log_error("Failed retrieving configuration of `%s': %s.\n",
145 dev->ddf_dev->name, str_error(rc));
146 goto rollback;
147 }
[6105fc0]148
149 rc = usb_endpoint_pipe_initialize_from_configuration(dev->pipes,
150 pipe_count, config_descriptor, config_descriptor_size, &dev->wire);
[69334af]151 if (rc != EOK) {
152 usb_log_error("Failed initializing USB endpoints: %s.\n",
153 str_error(rc));
154 goto rollback;
155 }
[6105fc0]156
[5fd22d8]157 /* Register the endpoints. */
158 usb_hc_connection_t hc_conn;
159 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev->ddf_dev);
160 if (rc != EOK) {
161 usb_log_error(
162 "Failed initializing connection to host controller: %s.\n",
163 str_error(rc));
164 goto rollback;
165 }
166 rc = usb_hc_connection_open(&hc_conn);
167 if (rc != EOK) {
168 usb_log_error("Failed to connect to host controller: %s.\n",
169 str_error(rc));
170 goto rollback;
171 }
172 for (i = 0; i < pipe_count; i++) {
173 if (dev->pipes[i].present) {
174 rc = usb_endpoint_pipe_register(dev->pipes[i].pipe,
175 dev->pipes[i].descriptor->poll_interval,
176 &hc_conn);
177 /* Ignore error when operation not supported by HC. */
178 if ((rc != EOK) && (rc != ENOTSUP)) {
179 /* FIXME: what shall we do? */
180 dev->pipes[i].present = false;
181 free(dev->pipes[i].pipe);
182 dev->pipes[i].pipe = NULL;
183 }
184 }
185 }
186 /* Ignoring errors here. */
187 usb_hc_connection_close(&hc_conn);
188
[6105fc0]189 return EOK;
[69334af]190
191rollback:
192 for (i = 0; i < pipe_count; i++) {
193 if (dev->pipes[i].pipe != NULL) {
194 free(dev->pipes[i].pipe);
195 }
196 }
197 free(dev->pipes);
198
199 return rc;
[6105fc0]200}
201
[69334af]202/** Initialize all endpoint pipes.
203 *
204 * @param drv The driver.
205 * @param dev The device to be initialized.
206 * @return Error code.
207 */
208static int initialize_pipes(usb_driver_t *drv, usb_device_t *dev)
209{
210 int rc;
211
212 rc = usb_device_connection_initialize_from_device(&dev->wire,
213 dev->ddf_dev);
214 if (rc != EOK) {
215 usb_log_error(
216 "Failed initializing connection on device `%s'. %s.\n",
217 dev->ddf_dev->name, str_error(rc));
218 return rc;
219 }
220
221 rc = usb_endpoint_pipe_initialize_default_control(&dev->ctrl_pipe,
222 &dev->wire);
223 if (rc != EOK) {
224 usb_log_error("Failed to initialize default control pipe " \
225 "on device `%s': %s.\n",
226 dev->ddf_dev->name, str_error(rc));
227 return rc;
228 }
229
[206f71a]230 rc = usb_endpoint_pipe_probe_default_control(&dev->ctrl_pipe);
231 if (rc != EOK) {
232 usb_log_error(
233 "Probing default control pipe on device `%s' failed: %s.\n",
234 dev->ddf_dev->name, str_error(rc));
235 return rc;
236 }
237
[69334af]238 /*
239 * Initialization of other pipes requires open session on
240 * default control pipe.
241 */
242 rc = usb_endpoint_pipe_start_session(&dev->ctrl_pipe);
243 if (rc != EOK) {
244 usb_log_error("Failed to start an IPC session: %s.\n",
245 str_error(rc));
246 return rc;
247 }
248
249 if (driver->endpoints != NULL) {
250 rc = initialize_other_pipes(driver, dev);
251 }
252
253 /* No checking here. */
254 usb_endpoint_pipe_end_session(&dev->ctrl_pipe);
255
256 return rc;
257}
258
259/** Callback when new device is supposed to be controlled by this driver.
260 *
261 * This callback is a wrapper for USB specific version of @c add_device.
262 *
263 * @param gen_dev Device structure as prepared by DDF.
264 * @return Error code.
265 */
[6105fc0]266int generic_add_device(ddf_dev_t *gen_dev)
267{
268 assert(driver);
269 assert(driver->ops);
270 assert(driver->ops->add_device);
271
272 int rc;
273
274 usb_device_t *dev = malloc(sizeof(usb_device_t));
[69334af]275 if (dev == NULL) {
276 usb_log_error("Out of memory when adding device `%s'.\n",
277 gen_dev->name);
278 return ENOMEM;
279 }
280
[6105fc0]281
282 dev->ddf_dev = gen_dev;
[69334af]283 dev->ddf_dev->driver_data = dev;
[6105fc0]284 dev->driver_data = NULL;
285
[69334af]286 rc = initialize_pipes(driver, dev);
287 if (rc != EOK) {
288 free(dev);
289 return rc;
[6105fc0]290 }
291
292 return driver->ops->add_device(dev);
293}
294
295/**
296 * @}
297 */
Note: See TracBrowser for help on using the repository browser.