source: mainline/uspace/lib/usbdev/src/devdrv.c@ 8b68bdf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b68bdf was 35bc430, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Use internal bus_session to get device info.

  • Property mode set to 100644
File size: 14.3 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 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#include <usb/dev/driver.h>
36#include <usb/dev/request.h>
37#include <usb/debug.h>
38#include <usb/dev.h>
39#include <errno.h>
40#include <str_error.h>
41#include <assert.h>
42
43/** Count number of pipes the driver expects.
44 *
45 * @param drv USB driver.
46 * @return Number of pipes (excluding default control pipe).
47 */
48static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
49{
50 size_t count;
51 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
52 return count;
53}
54
55/** Change interface setting of a device.
56 * This function selects new alternate setting of an interface by issuing
57 * proper USB command to the device and also creates new USB pipes
58 * under @c dev->pipes.
59 *
60 * @warning This function is intended for drivers working at interface level.
61 * For drivers controlling the whole device, you need to change interface
62 * manually using usb_request_set_interface() and creating new pipes
63 * with usb_pipe_initialize_from_configuration().
64 *
65 * @warning This is a wrapper function that does several operations that
66 * can fail and that cannot be rollbacked easily. That means that a failure
67 * during the SET_INTERFACE request would result in having a device with
68 * no pipes at all (except the default control one). That is because the old
69 * pipes needs to be unregistered at HC first and the new ones could not
70 * be created.
71 *
72 * @param dev USB device.
73 * @param alternate_setting Alternate setting to choose.
74 * @param endpoints New endpoint descriptions.
75 * @return Error code.
76 */
77int usb_device_select_interface(usb_device_t *usb_dev,
78 uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
79{
80 assert(usb_dev);
81
82 if (usb_dev->interface_no < 0) {
83 return EINVAL;
84 }
85
86 /* Change the interface itself. */
87 int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
88 usb_dev->interface_no, alternate_setting);
89 if (rc != EOK) {
90 return rc;
91 }
92
93 /* Change current alternative */
94 usb_dev->alternate_interfaces.current = alternate_setting;
95
96 /* Destroy existing pipes. */
97 usb_device_destroy_pipes(usb_dev);
98
99 /* Create new pipes. */
100 rc = usb_device_create_pipes(usb_dev, endpoints);
101
102 return rc;
103}
104
105/** Retrieve basic descriptors from the device.
106 *
107 * @param[in] ctrl_pipe Control endpoint pipe.
108 * @param[out] descriptors Where to store the descriptors.
109 * @return Error code.
110 */
111static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
112{
113 assert(usb_dev);
114 assert(usb_dev->descriptors.configuration == NULL);
115
116 /* It is worth to start a long transfer. */
117 usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
118
119 /* Get the device descriptor. */
120 int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
121 &usb_dev->descriptors.device);
122 if (rc != EOK) {
123 goto leave;
124 }
125
126 /* Get the full configuration descriptor. */
127 rc = usb_request_get_full_configuration_descriptor_alloc(
128 &usb_dev->ctrl_pipe, 0,
129 (void **) &usb_dev->descriptors.configuration,
130 &usb_dev->descriptors.configuration_size);
131
132leave:
133 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
134
135 return rc;
136}
137
138/** Cleanup structure initialized via usb_device_retrieve_descriptors.
139 *
140 * @param[in] descriptors Where to store the descriptors.
141 */
142static void usb_device_release_descriptors(usb_device_t *usb_dev)
143{
144 assert(usb_dev);
145 free(usb_dev->descriptors.configuration);
146 usb_dev->descriptors.configuration = NULL;
147 usb_dev->descriptors.configuration_size = 0;
148}
149
150/** Create pipes for a device.
151 *
152 * This is more or less a wrapper that does following actions:
153 * - allocate and initialize pipes
154 * - map endpoints to the pipes based on the descriptions
155 * - registers endpoints with the host controller
156 *
157 * @param[in] wire Initialized backing connection to the host controller.
158 * @param[in] endpoints Endpoints description, NULL terminated.
159 * @param[in] config_descr Configuration descriptor of active configuration.
160 * @param[in] config_descr_size Size of @p config_descr in bytes.
161 * @param[in] interface_no Interface to map from.
162 * @param[in] interface_setting Interface setting (default is usually 0).
163 * @param[out] pipes_ptr Where to store array of created pipes
164 * (not NULL terminated).
165 * @param[out] pipes_count_ptr Where to store number of pipes
166 * (set to NULL if you wish to ignore the count).
167 * @return Error code.
168 */
169int usb_device_create_pipes(usb_device_t *usb_dev,
170 const usb_endpoint_description_t **endpoints)
171{
172 assert(usb_dev);
173 assert(usb_dev->descriptors.configuration);
174 assert(usb_dev->pipes == NULL);
175 assert(usb_dev->pipes_count == 0);
176
177 size_t pipe_count = count_pipes(endpoints);
178 if (pipe_count == 0) {
179 return EOK;
180 }
181
182 usb_endpoint_mapping_t *pipes =
183 calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
184 if (pipes == NULL) {
185 return ENOMEM;
186 }
187
188 /* Now initialize. */
189 for (size_t i = 0; i < pipe_count; i++) {
190 pipes[i].description = endpoints[i];
191 pipes[i].interface_no = usb_dev->interface_no;
192 pipes[i].interface_setting =
193 usb_dev->alternate_interfaces.current;
194 }
195
196 /* Find the mapping from configuration descriptor. */
197 int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
198 usb_dev->descriptors.configuration,
199 usb_dev->descriptors.configuration_size, &usb_dev->wire);
200 if (rc != EOK) {
201 free(pipes);
202 return rc;
203 }
204
205 /* Register created pipes. */
206 for (size_t i = 0; i < pipe_count; i++) {
207 if (pipes[i].present) {
208 rc = usb_pipe_register(&pipes[i].pipe,
209 pipes[i].descriptor->poll_interval);
210 if (rc != EOK) {
211 goto rollback_unregister_endpoints;
212 }
213 }
214 }
215
216 usb_dev->pipes = pipes;
217 usb_dev->pipes_count = pipe_count;
218
219 return EOK;
220
221 /*
222 * Jump here if something went wrong after endpoints have
223 * been registered.
224 * This is also the target when the registration of
225 * endpoints fails.
226 */
227rollback_unregister_endpoints:
228 for (size_t i = 0; i < pipe_count; i++) {
229 if (pipes[i].present) {
230 usb_pipe_unregister(&pipes[i].pipe);
231 }
232 }
233
234 free(pipes);
235 return rc;
236}
237
238/** Destroy pipes previously created by usb_device_create_pipes.
239 *
240 * @param[in] usb_dev USB device.
241 */
242void usb_device_destroy_pipes(usb_device_t *usb_dev)
243{
244 assert(usb_dev);
245 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
246 /* Destroy the pipes. */
247 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
248 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
249 i, usb_dev->pipes[i].present ? "" : "not ");
250 if (usb_dev->pipes[i].present)
251 usb_pipe_unregister(&usb_dev->pipes[i].pipe);
252 }
253 free(usb_dev->pipes);
254 usb_dev->pipes = NULL;
255 usb_dev->pipes_count = 0;
256}
257
258usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
259{
260 assert(usb_dev);
261 return &usb_dev->ctrl_pipe;
262}
263
264usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
265 const usb_endpoint_description_t *desc)
266{
267 assert(usb_dev);
268 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
269 if (usb_dev->pipes[i].description == desc)
270 return &usb_dev->pipes[i];
271 }
272 return NULL;
273}
274
275usb_endpoint_mapping_t * usb_device_get_mapped_ep(
276 usb_device_t *usb_dev, usb_endpoint_t ep)
277{
278 assert(usb_dev);
279 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
280 if (usb_dev->pipes[i].pipe.endpoint_no == ep)
281 return &usb_dev->pipes[i];
282 }
283 return NULL;
284}
285
286int usb_device_get_iface_number(usb_device_t *usb_dev)
287{
288 assert(usb_dev);
289 return usb_dev->interface_no;
290}
291
292const usb_standard_device_descriptor_t *
293usb_device_get_device_descriptor(usb_device_t *usb_dev)
294{
295 assert(usb_dev);
296 return &usb_dev->descriptors.device;
297}
298
299const void * usb_device_get_configuration_descriptor(
300 usb_device_t *usb_dev, size_t *size)
301{
302 assert(usb_dev);
303 if (size)
304 *size = usb_dev->descriptors.configuration_size;
305 return usb_dev->descriptors.configuration;
306}
307
308const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
309 usb_device_t *usb_dev)
310{
311 assert(usb_dev);
312 return &usb_dev->alternate_interfaces;
313}
314
315static int usb_dev_get_info(usb_device_t *usb_dev, devman_handle_t *handle,
316 usb_address_t *address, int *iface_no)
317{
318 assert(usb_dev);
319
320 int ret = EOK;
321 async_exch_t *exch = async_exchange_begin(usb_dev->bus_session);
322 if (!exch)
323 ret = ENOMEM;
324
325 if (ret == EOK && address)
326 ret = usb_get_my_address(exch, address);
327
328 if (ret == EOK && handle)
329 ret = usb_get_hc_handle(exch, handle);
330
331 if (ret == EOK && iface_no) {
332 ret = usb_get_my_interface(exch, iface_no);
333 if (ret == ENOTSUP) {
334 ret = EOK;
335 *iface_no = -1;
336 }
337 }
338
339 async_exchange_end(exch);
340 return ret;
341}
342
343/** Initialize new instance of USB device.
344 *
345 * @param[in] usb_dev Pointer to the new device.
346 * @param[in] ddf_dev Generic DDF device backing the USB one.
347 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
348 * @param[out] errstr_ptr Where to store description of context
349 * (in case error occurs).
350 * @return Error code.
351 */
352int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
353 const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
354{
355 assert(usb_dev != NULL);
356 assert(ddf_dev != NULL);
357
358 *errstr_ptr = NULL;
359
360 usb_dev->ddf_dev = ddf_dev;
361 usb_dev->driver_data = NULL;
362 usb_dev->descriptors.configuration = NULL;
363 usb_dev->pipes_count = 0;
364 usb_dev->pipes = NULL;
365
366 usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
367 if (!usb_dev->bus_session) {
368 *errstr_ptr = "device bus session create";
369 return ENOMEM;
370 }
371
372 /* Get assigned params */
373 devman_handle_t hc_handle;
374 usb_address_t address;
375
376 int rc = usb_dev_get_info(usb_dev,
377 &hc_handle, &address, &usb_dev->interface_no);
378 if (rc != EOK) {
379 *errstr_ptr = "device parameters retrieval";
380 return rc;
381 }
382
383 /* Initialize hc connection. */
384 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
385
386 /* Initialize backing wire and control pipe. */
387 rc = usb_device_connection_initialize(
388 &usb_dev->wire, &usb_dev->hc_conn, address);
389 if (rc != EOK) {
390 *errstr_ptr = "device connection initialization";
391 return rc;
392 }
393
394 /* This pipe was registered by the hub driver,
395 * during device initialization. */
396 rc = usb_pipe_initialize_default_control(
397 &usb_dev->ctrl_pipe, &usb_dev->wire);
398 if (rc != EOK) {
399 *errstr_ptr = "default control pipe initialization";
400 return rc;
401 }
402
403 /* Open hc connection for pipe registration. */
404 rc = usb_hc_connection_open(&usb_dev->hc_conn);
405 if (rc != EOK) {
406 *errstr_ptr = "hc connection open";
407 return rc;
408 }
409
410 /* Retrieve standard descriptors. */
411 rc = usb_device_retrieve_descriptors(usb_dev);
412 if (rc != EOK) {
413 *errstr_ptr = "descriptor retrieval";
414 usb_hc_connection_close(&usb_dev->hc_conn);
415 return rc;
416 }
417
418 /* Create alternate interfaces. We will silently ignore failure.
419 * We might either control one interface or an entire device,
420 * it makes no sense to speak about alternate interfaces when
421 * controlling a device. */
422 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
423 usb_dev->descriptors.configuration,
424 usb_dev->descriptors.configuration_size, usb_dev->interface_no);
425
426 /* Create and register other pipes than default control (EP 0) */
427 rc = usb_device_create_pipes(usb_dev, endpoints);
428 if (rc != EOK) {
429 usb_hc_connection_close(&usb_dev->hc_conn);
430 /* Full configuration descriptor is allocated. */
431 usb_device_release_descriptors(usb_dev);
432 /* Alternate interfaces may be allocated */
433 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
434 *errstr_ptr = "pipes initialization";
435 return rc;
436 }
437
438 usb_hc_connection_close(&usb_dev->hc_conn);
439 return EOK;
440}
441
442/** Clean instance of a USB device.
443 *
444 * @param dev Device to be de-initialized.
445 *
446 * Does not free/destroy supplied pointer.
447 */
448void usb_device_deinit(usb_device_t *dev)
449{
450 if (dev) {
451 usb_dev_session_close(dev->bus_session);
452 /* Destroy existing pipes. */
453 usb_device_destroy_pipes(dev);
454 /* Ignore errors and hope for the best. */
455 usb_hc_connection_deinitialize(&dev->hc_conn);
456 usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
457 usb_device_release_descriptors(dev);
458 free(dev->driver_data);
459 dev->driver_data = NULL;
460 }
461}
462
463const char *usb_device_get_name(usb_device_t *usb_dev)
464{
465 assert(usb_dev);
466 if (usb_dev->ddf_dev)
467 return ddf_dev_get_name(usb_dev->ddf_dev);
468 return NULL;
469}
470
471ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
472 const char* name)
473{
474 assert(usb_dev);
475 if (usb_dev->ddf_dev)
476 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
477 return NULL;
478}
479
480async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
481{
482 assert(usb_dev);
483 return async_exchange_begin(usb_dev->bus_session);
484}
485
486void usb_device_bus_exchange_end(async_exch_t *exch)
487{
488 async_exchange_end(exch);
489}
490
491/** Allocate driver specific data.
492 * @param usb_dev usb_device structure.
493 * @param size requested data size.
494 * @return Pointer to the newly allocated space, NULL on failure.
495 */
496void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
497{
498 assert(usb_dev);
499 assert(usb_dev->driver_data == NULL);
500 return usb_dev->driver_data = calloc(1, size);
501
502}
503
504void * usb_device_data_get(usb_device_t *usb_dev)
505{
506 assert(usb_dev);
507 return usb_dev->driver_data;
508}
509/**
510 * @}
511 */
Note: See TracBrowser for help on using the repository browser.