source: mainline/uspace/lib/usb/src/devdrv.c@ 6105fc0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6105fc0 was 6105fc0, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Start work on ticket #85 (USB framework)

Only base skeleton is ready, error checks are replaced with assertions.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusb
30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
35#include <usb/devdrv.h>
36#include <usb/request.h>
37#include <usb/debug.h>
38#include <errno.h>
39#include <assert.h>
40
41static int generic_add_device(ddf_dev_t *);
42
43static driver_ops_t generic_driver_ops = {
44 .add_device = generic_add_device
45};
46static driver_t generic_driver = {
47 .driver_ops = &generic_driver_ops
48};
49
50static usb_driver_t *driver = NULL;
51
52int usb_driver_main(usb_driver_t *drv)
53{
54 /* Prepare the generic driver. */
55 generic_driver.name = drv->name;
56
57 driver = drv;
58
59 return ddf_driver_main(&generic_driver);
60}
61
62static size_t count_other_pipes(usb_driver_t *drv)
63{
64 size_t count = 0;
65 if (drv->endpoints == NULL) {
66 return 0;
67 }
68
69 while (drv->endpoints[count] != NULL) {
70 count++;
71 }
72
73 return count;
74}
75
76static int initialize_other_pipes(usb_driver_t *drv, usb_device_t *dev)
77{
78 int my_interface = usb_device_get_assigned_interface(dev->ddf_dev);
79
80 size_t pipe_count = count_other_pipes(drv);
81 dev->pipes = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
82 assert(dev->pipes != NULL);
83
84 size_t i;
85 for (i = 0; i < pipe_count; i++) {
86 dev->pipes[i].pipe = malloc(sizeof(usb_endpoint_pipe_t));
87 assert(dev->pipes[i].pipe != NULL);
88 dev->pipes[i].description = drv->endpoints[i];
89 dev->pipes[i].interface_no = my_interface;
90 }
91
92 void *config_descriptor;
93 size_t config_descriptor_size;
94 int rc = usb_request_get_full_configuration_descriptor_alloc(
95 &dev->ctrl_pipe, 0, &config_descriptor, &config_descriptor_size);
96 assert(rc == EOK);
97
98 rc = usb_endpoint_pipe_initialize_from_configuration(dev->pipes,
99 pipe_count, config_descriptor, config_descriptor_size, &dev->wire);
100 assert(rc == EOK);
101
102
103 return EOK;
104}
105
106int generic_add_device(ddf_dev_t *gen_dev)
107{
108 assert(driver);
109 assert(driver->ops);
110 assert(driver->ops->add_device);
111
112 int rc;
113
114 usb_device_t *dev = malloc(sizeof(usb_device_t));
115 assert(dev);
116
117 dev->ddf_dev = gen_dev;
118 dev->driver_data = NULL;
119
120 /* Initialize the backing wire abstraction. */
121 rc = usb_device_connection_initialize_from_device(&dev->wire, gen_dev);
122 assert(rc == EOK);
123
124 /* Initialize the default control pipe. */
125 rc = usb_endpoint_pipe_initialize_default_control(&dev->ctrl_pipe,
126 &dev->wire);
127 assert(rc == EOK);
128
129 rc = usb_endpoint_pipe_start_session(&dev->ctrl_pipe);
130 assert(rc == EOK);
131
132 /* Initialize remaining pipes. */
133 if (driver->endpoints != NULL) {
134 initialize_other_pipes(driver, dev);
135 }
136
137 rc = usb_endpoint_pipe_end_session(&dev->ctrl_pipe);
138 assert(rc == EOK);
139
140 return driver->ops->add_device(dev);
141}
142
143/**
144 * @}
145 */
Note: See TracBrowser for help on using the repository browser.