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

Last change on this file since 34b2f54d was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

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