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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b71f3e was 2489353, checked in by Petr Manek <petr.manek@…>, 7 years ago

usbdev: driver can destroy endpoint mappings

  • Property mode set to 100644
File size: 16.0 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
30/** @addtogroup libusbdev
31 * @{
32 */
33/** @file
34 * USB device driver framework.
35 */
36
37#include <usb_iface.h>
38#include <usb/dev/alternate_ifaces.h>
39#include <usb/dev/device.h>
40#include <usb/dev/pipes.h>
41#include <usb/dev/request.h>
42#include <usb/debug.h>
43#include <usb/descriptor.h>
44#include <usb/usb.h>
45
46#include <assert.h>
47#include <async.h>
48#include <devman.h>
49#include <errno.h>
50#include <str_error.h>
51#include <stdlib.h>
52
53#include <ddf/driver.h>
54
55/** USB device structure. */
56struct usb_device {
57 /** Connection to device on USB bus */
58 usb_dev_session_t *bus_session;
59
60 /** devman handle */
61 devman_handle_t handle;
62
63 /** The default control pipe. */
64 usb_pipe_t ctrl_pipe;
65
66 /** Other endpoint pipes.
67 *
68 * This is an array of other endpoint pipes in the same order as
69 * in usb_driver_t.
70 */
71 usb_endpoint_mapping_t *pipes;
72
73 /** Number of other endpoint pipes. */
74 size_t pipes_count;
75
76 /** Current interface.
77 *
78 * Usually, drivers operate on single interface only.
79 * This item contains the value of the interface or -1 for any.
80 */
81 int interface_no;
82
83 /** Alternative interfaces. */
84 usb_alternate_interfaces_t alternate_interfaces;
85
86 /** Some useful descriptors for USB device. */
87 usb_device_descriptors_t descriptors;
88
89 /** Generic DDF device backing this one. DO NOT TOUCH! */
90 ddf_dev_t *ddf_dev;
91
92 /** Custom driver data.
93 *
94 * Do not use the entry in generic device, that is already used
95 * by the framework.
96 */
97 void *driver_data;
98};
99
100/** Count number of pipes the driver expects.
101 *
102 * @param drv USB driver.
103 * @return Number of pipes (excluding default control pipe).
104 */
105static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
106{
107 size_t count;
108 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
109 return count;
110}
111
112/** Change interface setting of a device.
113 * This function selects new alternate setting of an interface by issuing
114 * proper USB command to the device and also creates new USB pipes
115 * under @c dev->pipes.
116 *
117 * @warning This function is intended for drivers working at interface level.
118 * For drivers controlling the whole device, you need to change interface
119 * manually using usb_request_set_interface() and creating new pipes
120 * with usb_pipe_initialize_from_configuration().
121 *
122 * @warning This is a wrapper function that does several operations that
123 * can fail and that cannot be rollbacked easily. That means that a failure
124 * during the SET_INTERFACE request would result in having a device with
125 * no pipes at all (except the default control one). That is because the old
126 * pipes needs to be unregistered at HC first and the new ones could not
127 * be created.
128 *
129 * @param dev USB device.
130 * @param alternate_setting Alternate setting to choose.
131 * @param endpoints New endpoint descriptions.
132 * @return Error code.
133 */
134int usb_device_select_interface(usb_device_t *usb_dev,
135 uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
136{
137 assert(usb_dev);
138
139 if (usb_dev->interface_no < 0) {
140 return EINVAL;
141 }
142
143 /* Change the interface itself. */
144 int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
145 usb_dev->interface_no, alternate_setting);
146 if (rc != EOK) {
147 return rc;
148 }
149
150 /* Change current alternative */
151 usb_dev->alternate_interfaces.current = alternate_setting;
152
153 /* Destroy existing pipes. */
154 usb_device_destroy_pipes(usb_dev);
155
156 /* Create new pipes. */
157 rc = usb_device_create_pipes(usb_dev, endpoints);
158
159 return rc;
160}
161
162/** Retrieve basic descriptors from the device.
163 *
164 * @param[in] ctrl_pipe Control endpoint pipe.
165 * @param[out] descriptors Where to store the descriptors.
166 * @return Error code.
167 */
168static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
169{
170 assert(usb_dev);
171 assert(usb_dev->descriptors.full_config == NULL);
172
173 /* Get the device descriptor. */
174 int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
175 &usb_dev->descriptors.device);
176 if (rc != EOK) {
177 return rc;
178 }
179
180 /* Get the full configuration descriptor. */
181 rc = usb_request_get_full_configuration_descriptor_alloc(
182 &usb_dev->ctrl_pipe, 0,
183 &usb_dev->descriptors.full_config,
184 &usb_dev->descriptors.full_config_size);
185
186
187 return rc;
188}
189
190/** Cleanup structure initialized via usb_device_retrieve_descriptors.
191 *
192 * @param[in] descriptors Where to store the descriptors.
193 */
194static void usb_device_release_descriptors(usb_device_t *usb_dev)
195{
196 assert(usb_dev);
197 free(usb_dev->descriptors.full_config);
198 usb_dev->descriptors.full_config = NULL;
199 usb_dev->descriptors.full_config_size = 0;
200}
201
202/** Create pipes for a device.
203 *
204 * This is more or less a wrapper that does following actions:
205 * - allocate and initialize pipes
206 * - map endpoints to the pipes based on the descriptions
207 * - registers endpoints with the host controller
208 *
209 * @param[in] endpoints Endpoints description, NULL terminated.
210 * @param[in] config_descr Configuration descriptor of active configuration.
211 * @param[in] config_descr_size Size of @p config_descr in bytes.
212 * @param[in] interface_no Interface to map from.
213 * @param[in] interface_setting Interface setting (default is usually 0).
214 * @param[out] pipes_ptr Where to store array of created pipes
215 * (not NULL terminated).
216 * @param[out] pipes_count_ptr Where to store number of pipes
217 * (set to NULL if you wish to ignore the count).
218 * @return Error code.
219 */
220int usb_device_create_pipes(usb_device_t *usb_dev,
221 const usb_endpoint_description_t **endpoints)
222{
223 assert(usb_dev);
224 assert(usb_dev->descriptors.full_config);
225 assert(usb_dev->pipes == NULL);
226 assert(usb_dev->pipes_count == 0);
227
228 size_t pipe_count = count_pipes(endpoints);
229 if (pipe_count == 0) {
230 return EOK;
231 }
232
233 usb_endpoint_mapping_t *pipes =
234 calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
235 if (pipes == NULL) {
236 return ENOMEM;
237 }
238
239 /* Now initialize. */
240 for (size_t i = 0; i < pipe_count; i++) {
241 pipes[i].description = endpoints[i];
242 pipes[i].interface_no = usb_dev->interface_no;
243 pipes[i].interface_setting =
244 usb_dev->alternate_interfaces.current;
245 }
246
247 /* Find the mapping from configuration descriptor. */
248 int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
249 usb_dev->descriptors.full_config,
250 usb_dev->descriptors.full_config_size,
251 usb_dev->bus_session);
252 if (rc != EOK) {
253 free(pipes);
254 return rc;
255 }
256
257 /* Register created pipes. */
258 unsigned pipes_registered = 0;
259 for (size_t i = 0; i < pipe_count; i++) {
260 if (pipes[i].present) {
261 rc = usb_pipe_register(&pipes[i].pipe, pipes[i].descriptor, pipes[i].companion_descriptor);
262 if (rc != EOK) {
263 goto rollback_unregister_endpoints;
264 }
265 }
266 pipes_registered++;
267 }
268
269 usb_dev->pipes = pipes;
270 usb_dev->pipes_count = pipe_count;
271
272 return EOK;
273
274 /*
275 * Jump here if something went wrong after endpoints have
276 * been registered.
277 * This is also the target when the registration of
278 * endpoints fails.
279 */
280rollback_unregister_endpoints:
281 for (size_t i = 0; i < pipes_registered; i++) {
282 if (pipes[i].present) {
283 usb_pipe_unregister(&pipes[i].pipe);
284 }
285 }
286
287 free(pipes);
288 return rc;
289}
290
291/** Destroy pipes previously created by usb_device_create_pipes.
292 *
293 * @param[in] usb_dev USB device.
294 *
295 */
296void usb_device_destroy_pipes(usb_device_t *usb_dev)
297{
298 assert(usb_dev);
299 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
300
301 /* Destroy the pipes. */
302 int rc;
303 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
304 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
305 i, usb_dev->pipes[i].present ? "" : "not ");
306
307 rc = usb_device_unmap_ep(usb_dev->pipes + i);
308 if (rc != EOK && rc != ENOENT)
309 usb_log_warning("Unregistering pipe %zu failed: %s", i, str_error(rc));
310 }
311
312 free(usb_dev->pipes);
313 usb_dev->pipes = NULL;
314 usb_dev->pipes_count = 0;
315}
316
317usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
318{
319 assert(usb_dev);
320 return &usb_dev->ctrl_pipe;
321}
322
323usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
324 const usb_endpoint_description_t *desc)
325{
326 assert(usb_dev);
327 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
328 if (usb_dev->pipes[i].description == desc)
329 return &usb_dev->pipes[i];
330 }
331 return NULL;
332}
333
334usb_endpoint_mapping_t * usb_device_get_mapped_ep(
335 usb_device_t *usb_dev, usb_endpoint_t ep)
336{
337 assert(usb_dev);
338 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
339 if (usb_dev->pipes[i].pipe.desc.endpoint_no == ep)
340 return &usb_dev->pipes[i];
341 }
342 return NULL;
343}
344
345int usb_device_unmap_ep(usb_endpoint_mapping_t *epm)
346{
347 assert(epm);
348
349 if (!epm->present)
350 return ENOENT;
351
352 const int rc = usb_pipe_unregister(&epm->pipe);
353 if (rc != EOK)
354 return rc;
355
356 epm->present = false;
357 return EOK;
358}
359
360int usb_device_get_iface_number(usb_device_t *usb_dev)
361{
362 assert(usb_dev);
363 return usb_dev->interface_no;
364}
365
366devman_handle_t usb_device_get_devman_handle(usb_device_t *usb_dev)
367{
368 assert(usb_dev);
369 return usb_dev->handle;
370}
371
372const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
373{
374 assert(usb_dev);
375 return &usb_dev->descriptors;
376}
377
378const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
379 usb_device_t *usb_dev)
380{
381 assert(usb_dev);
382 return &usb_dev->alternate_interfaces;
383}
384
385/** Clean instance of a USB device.
386 *
387 * @param dev Device to be de-initialized.
388 *
389 * Does not free/destroy supplied pointer.
390 */
391static void usb_device_fini(usb_device_t *usb_dev)
392{
393 if (usb_dev) {
394 /* Destroy existing pipes. */
395 usb_device_destroy_pipes(usb_dev);
396 /* Ignore errors and hope for the best. */
397 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
398 usb_device_release_descriptors(usb_dev);
399 free(usb_dev->driver_data);
400 usb_dev->driver_data = NULL;
401 usb_dev_disconnect(usb_dev->bus_session);
402 usb_dev->bus_session = NULL;
403 }
404}
405
406/** Initialize new instance of USB device.
407 *
408 * @param[in] usb_dev Pointer to the new device.
409 * @param[in] ddf_dev Generic DDF device backing the USB one.
410 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
411 * @param[out] errstr_ptr Where to store description of context
412 * (in case error occurs).
413 * @return Error code.
414 */
415static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
416 const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
417 devman_handle_t handle, int interface_no)
418{
419 assert(usb_dev != NULL);
420 assert(errstr_ptr);
421
422 *errstr_ptr = NULL;
423
424 usb_dev->ddf_dev = ddf_dev;
425 usb_dev->handle = handle;
426 usb_dev->interface_no = interface_no;
427 usb_dev->driver_data = NULL;
428 usb_dev->descriptors.full_config = NULL;
429 usb_dev->descriptors.full_config_size = 0;
430 usb_dev->pipes_count = 0;
431 usb_dev->pipes = NULL;
432
433 usb_dev->bus_session = usb_dev_connect(handle);
434
435 if (!usb_dev->bus_session) {
436 *errstr_ptr = "device bus session create";
437 return ENOMEM;
438 }
439
440 /* This pipe was registered by the hub driver,
441 * during device initialization. */
442 int rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe, usb_dev->bus_session);
443 if (rc != EOK) {
444 usb_dev_disconnect(usb_dev->bus_session);
445 *errstr_ptr = "default control pipe initialization";
446 return rc;
447 }
448
449 /* Retrieve standard descriptors. */
450 rc = usb_device_retrieve_descriptors(usb_dev);
451 if (rc != EOK) {
452 *errstr_ptr = "descriptor retrieval";
453 usb_dev_disconnect(usb_dev->bus_session);
454 return rc;
455 }
456
457 /* Create alternate interfaces. We will silently ignore failure.
458 * We might either control one interface or an entire device,
459 * it makes no sense to speak about alternate interfaces when
460 * controlling a device. */
461 usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
462 usb_dev->descriptors.full_config,
463 usb_dev->descriptors.full_config_size, usb_dev->interface_no);
464
465 if (endpoints) {
466 /* Create and register other pipes than default control (EP 0)*/
467 rc = usb_device_create_pipes(usb_dev, endpoints);
468 if (rc != EOK) {
469 usb_device_fini(usb_dev);
470 *errstr_ptr = "pipes initialization";
471 return rc;
472 }
473 }
474
475 return EOK;
476}
477
478static int usb_device_get_info(async_sess_t *sess, devman_handle_t *handle,
479 int *iface_no)
480{
481 assert(handle);
482 assert(iface_no);
483
484 async_exch_t *exch = async_exchange_begin(sess);
485 if (!exch)
486 return EPARTY;
487
488 int ret = usb_get_my_device_handle(exch, handle);
489 if (ret == EOK) {
490 ret = usb_get_my_interface(exch, iface_no);
491 if (ret == ENOTSUP) {
492 *iface_no = -1;
493 ret = EOK;
494 }
495 }
496
497 async_exchange_end(exch);
498 return ret;
499}
500
501int usb_device_create_ddf(ddf_dev_t *ddf_dev,
502 const usb_endpoint_description_t **desc, const char **err)
503{
504 assert(ddf_dev);
505 assert(err);
506
507 devman_handle_t h = 0;
508 int iface_no = -1;
509
510 async_sess_t *sess = ddf_dev_parent_sess_get(ddf_dev);
511 if (sess == NULL)
512 return ENOMEM;
513 const int ret = usb_device_get_info(sess, &h, &iface_no);
514 if (ret != EOK)
515 return ret;
516
517 usb_device_t *usb_dev =
518 ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
519 if (usb_dev == NULL) {
520 *err = "DDF data alloc";
521 return ENOMEM;
522 }
523
524 return usb_device_init(usb_dev, ddf_dev, desc, err, h, iface_no);
525}
526
527void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
528{
529 assert(ddf_dev);
530 usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
531 assert(usb_dev);
532 usb_device_fini(usb_dev);
533 return;
534}
535
536usb_device_t * usb_device_create(devman_handle_t handle)
537{
538 devman_handle_t h = 0;
539 int iface_no = -1;
540
541 async_sess_t *sess = devman_device_connect(handle, IPC_FLAG_BLOCKING);
542 int ret = usb_device_get_info(sess, &h, &iface_no);
543 if (sess)
544 async_hangup(sess);
545 if (ret != EOK)
546 return NULL;
547
548 usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
549 if (!usb_dev)
550 return NULL;
551
552 const char* dummy = NULL;
553 ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle, iface_no);
554 if (ret != EOK) {
555 free(usb_dev);
556 usb_dev = NULL;
557 }
558 return usb_dev;
559}
560
561void usb_device_destroy(usb_device_t *usb_dev)
562{
563 if (usb_dev) {
564 usb_device_fini(usb_dev);
565 free(usb_dev);
566 }
567}
568
569const char *usb_device_get_name(usb_device_t *usb_dev)
570{
571 assert(usb_dev);
572 if (usb_dev->ddf_dev)
573 return ddf_dev_get_name(usb_dev->ddf_dev);
574 return NULL;
575}
576
577ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
578 const char* name)
579{
580 assert(usb_dev);
581 if (usb_dev->ddf_dev)
582 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
583 return NULL;
584}
585
586async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
587{
588 assert(usb_dev);
589 return async_exchange_begin(usb_dev->bus_session);
590}
591
592void usb_device_bus_exchange_end(async_exch_t *exch)
593{
594 async_exchange_end(exch);
595}
596
597/** Allocate driver specific data.
598 * @param usb_dev usb_device structure.
599 * @param size requested data size.
600 * @return Pointer to the newly allocated space, NULL on failure.
601 */
602void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
603{
604 assert(usb_dev);
605 assert(usb_dev->driver_data == NULL);
606 return usb_dev->driver_data = calloc(1, size);
607
608}
609
610void * usb_device_data_get(usb_device_t *usb_dev)
611{
612 assert(usb_dev);
613 return usb_dev->driver_data;
614}
615
616/**
617 * @}
618 */
Note: See TracBrowser for help on using the repository browser.