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 drvusbmid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Helper functions.
|
---|
35 | */
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <usb_iface.h>
|
---|
40 | #include <usb/ddfiface.h>
|
---|
41 | #include <usb/pipes.h>
|
---|
42 | #include <usb/classes/classes.h>
|
---|
43 | #include <usb/recognise.h>
|
---|
44 | #include "usbmid.h"
|
---|
45 |
|
---|
46 | /** Callback for DDF USB interface. */
|
---|
47 | static int usb_iface_get_address_impl(ddf_fun_t *fun, devman_handle_t handle,
|
---|
48 | usb_address_t *address)
|
---|
49 | {
|
---|
50 | return usb_iface_get_address_hub_impl(fun, handle, address);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** Callback for DDF USB interface. */
|
---|
54 | static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
|
---|
55 | int *iface_no)
|
---|
56 | {
|
---|
57 | assert(fun);
|
---|
58 |
|
---|
59 | usbmid_interface_t *iface = fun->driver_data;
|
---|
60 | assert(iface);
|
---|
61 |
|
---|
62 | if (iface_no != NULL) {
|
---|
63 | *iface_no = iface->interface_no;
|
---|
64 | }
|
---|
65 |
|
---|
66 | return EOK;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** DDF interface of the child - interface function. */
|
---|
70 | static usb_iface_t child_usb_iface = {
|
---|
71 | .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
|
---|
72 | .get_address = usb_iface_get_address_impl,
|
---|
73 | .get_interface = usb_iface_get_interface_impl
|
---|
74 | };
|
---|
75 |
|
---|
76 | /** Operations for children - interface functions. */
|
---|
77 | static ddf_dev_ops_t child_device_ops = {
|
---|
78 | .interfaces[USB_DEV_IFACE] = &child_usb_iface
|
---|
79 | };
|
---|
80 |
|
---|
81 | /** Operations of the device itself. */
|
---|
82 | static ddf_dev_ops_t mid_device_ops = {
|
---|
83 | .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
|
---|
84 | };
|
---|
85 |
|
---|
86 | /** Create new USB multi interface device.
|
---|
87 | *
|
---|
88 | * @param dev Backing generic DDF device.
|
---|
89 | * @return New USB MID device.
|
---|
90 | * @retval NULL Error occured.
|
---|
91 | */
|
---|
92 | usbmid_device_t *usbmid_device_create(ddf_dev_t *dev)
|
---|
93 | {
|
---|
94 | usbmid_device_t *mid = malloc(sizeof(usbmid_device_t));
|
---|
95 | if (mid == NULL) {
|
---|
96 | usb_log_error("Out of memory (wanted %zu bytes).\n",
|
---|
97 | sizeof(usbmid_device_t));
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | int rc;
|
---|
102 | rc = usb_device_connection_initialize_from_device(&mid->wire, dev);
|
---|
103 | if (rc != EOK) {
|
---|
104 | usb_log_error("Failed to initialize `USB wire': %s.\n",
|
---|
105 | str_error(rc));
|
---|
106 | free(mid);
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | rc = usb_pipe_initialize_default_control(&mid->ctrl_pipe,
|
---|
111 | &mid->wire);
|
---|
112 | if (rc != EOK) {
|
---|
113 | usb_log_error("Failed to initialize control pipe: %s.\n",
|
---|
114 | str_error(rc));
|
---|
115 | free(mid);
|
---|
116 | return NULL;
|
---|
117 | }
|
---|
118 | rc = usb_pipe_probe_default_control(&mid->ctrl_pipe);
|
---|
119 | if (rc != EOK) {
|
---|
120 | usb_log_error("Probing default control pipe failed: %s.\n",
|
---|
121 | str_error(rc));
|
---|
122 | free(mid);
|
---|
123 | return NULL;
|
---|
124 | }
|
---|
125 |
|
---|
126 | mid->dev = dev;
|
---|
127 | (void) &mid_device_ops;
|
---|
128 |
|
---|
129 | return mid;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** Create new interface for USB MID device.
|
---|
133 | *
|
---|
134 | * @param fun Backing generic DDF device function (representing interface).
|
---|
135 | * @param iface_no Interface number.
|
---|
136 | * @return New interface.
|
---|
137 | * @retval NULL Error occured.
|
---|
138 | */
|
---|
139 | usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)
|
---|
140 | {
|
---|
141 | usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
|
---|
142 | if (iface == NULL) {
|
---|
143 | usb_log_error("Out of memory (wanted %zuB).\n",
|
---|
144 | sizeof(usbmid_interface_t));
|
---|
145 | return NULL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | iface->fun = fun;
|
---|
149 | iface->interface_no = iface_no;
|
---|
150 |
|
---|
151 | return iface;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /** Spawn new child device from one interface.
|
---|
156 | *
|
---|
157 | * @param parent Parent MID device.
|
---|
158 | * @param device_descriptor Device descriptor.
|
---|
159 | * @param interface_descriptor Interface descriptor.
|
---|
160 | * @return Error code.
|
---|
161 | */
|
---|
162 | int usbmid_spawn_interface_child(usbmid_device_t *parent,
|
---|
163 | const usb_standard_device_descriptor_t *device_descriptor,
|
---|
164 | const usb_standard_interface_descriptor_t *interface_descriptor)
|
---|
165 | {
|
---|
166 | ddf_fun_t *child = NULL;
|
---|
167 | char *child_name = NULL;
|
---|
168 | usbmid_interface_t *child_as_interface = NULL;
|
---|
169 | int rc;
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Name is class name followed by interface number.
|
---|
173 | * The interface number shall provide uniqueness while the
|
---|
174 | * class name something humanly understandable.
|
---|
175 | */
|
---|
176 | rc = asprintf(&child_name, "%s%d",
|
---|
177 | usb_str_class(interface_descriptor->interface_class),
|
---|
178 | (int) interface_descriptor->interface_number);
|
---|
179 | if (rc < 0) {
|
---|
180 | goto error_leave;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* Create the device. */
|
---|
184 | child = ddf_fun_create(parent->dev, fun_inner, child_name);
|
---|
185 | if (child == NULL) {
|
---|
186 | rc = ENOMEM;
|
---|
187 | goto error_leave;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 |
|
---|
192 | child_as_interface = usbmid_interface_create(child,
|
---|
193 | (int) interface_descriptor->interface_number);
|
---|
194 | if (child_as_interface == NULL) {
|
---|
195 | rc = ENOMEM;
|
---|
196 | goto error_leave;
|
---|
197 | }
|
---|
198 |
|
---|
199 | child->driver_data = child_as_interface;
|
---|
200 | child->ops = &child_device_ops;
|
---|
201 |
|
---|
202 | rc = usb_device_create_match_ids_from_interface(device_descriptor,
|
---|
203 | interface_descriptor,
|
---|
204 | &child->match_ids);
|
---|
205 | if (rc != EOK) {
|
---|
206 | goto error_leave;
|
---|
207 | }
|
---|
208 |
|
---|
209 | rc = ddf_fun_bind(child);
|
---|
210 | if (rc != EOK) {
|
---|
211 | goto error_leave;
|
---|
212 | }
|
---|
213 |
|
---|
214 | return EOK;
|
---|
215 |
|
---|
216 | error_leave:
|
---|
217 | if (child != NULL) {
|
---|
218 | child->name = NULL;
|
---|
219 | /* This takes care of match_id deallocation as well. */
|
---|
220 | ddf_fun_destroy(child);
|
---|
221 | }
|
---|
222 | if (child_name != NULL) {
|
---|
223 | free(child_name);
|
---|
224 | }
|
---|
225 | if (child_as_interface != NULL) {
|
---|
226 | free(child_as_interface);
|
---|
227 | }
|
---|
228 |
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * @}
|
---|
234 | */
|
---|