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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c901632 was c901632, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usbdev: polling interval and interval is the same thing

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