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

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

rename get_device_handle ⇒ get_my_device_handle

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