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 | * Functions needed by hub drivers.
|
---|
34 | */
|
---|
35 | #include <usb/hub.h>
|
---|
36 | #include <usb/pipes.h>
|
---|
37 | #include <usb/request.h>
|
---|
38 | #include <usb/recognise.h>
|
---|
39 | #include <usbhc_iface.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <assert.h>
|
---|
42 |
|
---|
43 | /** Check that HC connection is alright.
|
---|
44 | *
|
---|
45 | * @param conn Connection to be checked.
|
---|
46 | */
|
---|
47 | #define CHECK_CONNECTION(conn) \
|
---|
48 | do { \
|
---|
49 | assert((conn)); \
|
---|
50 | if (!usb_hc_connection_is_opened((conn))) { \
|
---|
51 | return ENOENT; \
|
---|
52 | } \
|
---|
53 | } while (false)
|
---|
54 |
|
---|
55 |
|
---|
56 | /** Tell host controller to reserve default address.
|
---|
57 | *
|
---|
58 | * @param connection Opened connection to host controller.
|
---|
59 | * @param speed Speed of the device that will respond on the default address.
|
---|
60 | * @return Error code.
|
---|
61 | */
|
---|
62 | int usb_hc_reserve_default_address(usb_hc_connection_t *connection,
|
---|
63 | usb_speed_t speed)
|
---|
64 | {
|
---|
65 | CHECK_CONNECTION(connection);
|
---|
66 |
|
---|
67 | return async_req_2_0(connection->hc_phone,
|
---|
68 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
69 | IPC_M_USBHC_RESERVE_DEFAULT_ADDRESS, speed);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Tell host controller to release default address.
|
---|
73 | *
|
---|
74 | * @param connection Opened connection to host controller.
|
---|
75 | * @return Error code.
|
---|
76 | */
|
---|
77 | int usb_hc_release_default_address(usb_hc_connection_t *connection)
|
---|
78 | {
|
---|
79 | CHECK_CONNECTION(connection);
|
---|
80 |
|
---|
81 | return async_req_1_0(connection->hc_phone,
|
---|
82 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
83 | IPC_M_USBHC_RELEASE_DEFAULT_ADDRESS);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Ask host controller for free address assignment.
|
---|
87 | *
|
---|
88 | * @param connection Opened connection to host controller.
|
---|
89 | * @param speed Speed of the new device (device that will be assigned
|
---|
90 | * the returned address).
|
---|
91 | * @return Assigned USB address or negative error code.
|
---|
92 | */
|
---|
93 | usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
|
---|
94 | usb_speed_t speed)
|
---|
95 | {
|
---|
96 | CHECK_CONNECTION(connection);
|
---|
97 |
|
---|
98 | sysarg_t address;
|
---|
99 | int rc = async_req_2_1(connection->hc_phone,
|
---|
100 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
101 | IPC_M_USBHC_REQUEST_ADDRESS, speed,
|
---|
102 | &address);
|
---|
103 | if (rc != EOK) {
|
---|
104 | return (usb_address_t) rc;
|
---|
105 | } else {
|
---|
106 | return (usb_address_t) address;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Inform host controller about new device.
|
---|
111 | *
|
---|
112 | * @param connection Opened connection to host controller.
|
---|
113 | * @param attached_device Information about the new device.
|
---|
114 | * @return Error code.
|
---|
115 | */
|
---|
116 | int usb_hc_register_device(usb_hc_connection_t * connection,
|
---|
117 | const usb_hc_attached_device_t *attached_device)
|
---|
118 | {
|
---|
119 | CHECK_CONNECTION(connection);
|
---|
120 | if (attached_device == NULL) {
|
---|
121 | return EBADMEM;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return async_req_3_0(connection->hc_phone,
|
---|
125 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
126 | IPC_M_USBHC_BIND_ADDRESS,
|
---|
127 | attached_device->address, attached_device->handle);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Inform host controller about device removal.
|
---|
131 | *
|
---|
132 | * @param connection Opened connection to host controller.
|
---|
133 | * @param address Address of the device that is being removed.
|
---|
134 | * @return Error code.
|
---|
135 | */
|
---|
136 | int usb_hc_unregister_device(usb_hc_connection_t *connection,
|
---|
137 | usb_address_t address)
|
---|
138 | {
|
---|
139 | CHECK_CONNECTION(connection);
|
---|
140 |
|
---|
141 | return async_req_2_0(connection->hc_phone,
|
---|
142 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
143 | IPC_M_USBHC_RELEASE_ADDRESS, address);
|
---|
144 | }
|
---|
145 |
|
---|
146 | static void unregister_control_endpoint_on_default_address(
|
---|
147 | usb_hc_connection_t *connection)
|
---|
148 | {
|
---|
149 | usb_device_connection_t dev_conn;
|
---|
150 | int rc = usb_device_connection_initialize_on_default_address(&dev_conn,
|
---|
151 | connection);
|
---|
152 | if (rc != EOK) {
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | usb_pipe_t ctrl_pipe;
|
---|
157 | rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
|
---|
158 | if (rc != EOK) {
|
---|
159 | return;
|
---|
160 | }
|
---|
161 |
|
---|
162 | usb_pipe_unregister(&ctrl_pipe, connection);
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /** Wrapper for registering attached device to the hub.
|
---|
167 | *
|
---|
168 | * The @p enable_port function is expected to enable signaling on given
|
---|
169 | * port.
|
---|
170 | * The two arguments to it can have arbitrary meaning
|
---|
171 | * (the @p port_no is only a suggestion)
|
---|
172 | * and are not touched at all by this function
|
---|
173 | * (they are passed as is to the @p enable_port function).
|
---|
174 | *
|
---|
175 | * If the @p enable_port fails (i.e. does not return EOK), the device
|
---|
176 | * addition is canceled.
|
---|
177 | * The return value is then returned (it is good idea to use different
|
---|
178 | * error codes than those listed as return codes by this function itself).
|
---|
179 | *
|
---|
180 | * @param[in] parent Parent device (i.e. the hub device).
|
---|
181 | * @param[in] connection Opened connection to host controller.
|
---|
182 | * @param[in] dev_speed New device speed.
|
---|
183 | * @param[in] enable_port Function for enabling signaling through the port the
|
---|
184 | * device is attached to.
|
---|
185 | * @param[in] port_no Port number (passed through to @p enable_port).
|
---|
186 | * @param[in] arg Any data argument to @p enable_port.
|
---|
187 | * @param[out] assigned_address USB address of the device.
|
---|
188 | * @param[out] assigned_handle Devman handle of the new device.
|
---|
189 | * @param[in] dev_ops Child device ops.
|
---|
190 | * @param[in] new_dev_data Arbitrary pointer to be stored in the child
|
---|
191 | * as @c driver_data.
|
---|
192 | * @param[out] new_fun Storage where pointer to allocated child function
|
---|
193 | * will be written.
|
---|
194 | * @return Error code.
|
---|
195 | * @retval ENOENT Connection to HC not opened.
|
---|
196 | * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
|
---|
197 | * @retval EBUSY Failed reserving default USB address.
|
---|
198 | * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
|
---|
199 | * @retval ESTALL Problem communication with device (either SET_ADDRESS
|
---|
200 | * request or requests for descriptors when creating match ids).
|
---|
201 | */
|
---|
202 | int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
|
---|
203 | usb_speed_t dev_speed,
|
---|
204 | int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
|
---|
205 | usb_address_t *assigned_address, devman_handle_t *assigned_handle,
|
---|
206 | ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
|
---|
207 | {
|
---|
208 | CHECK_CONNECTION(connection);
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Request new address.
|
---|
212 | */
|
---|
213 | usb_address_t dev_addr = usb_hc_request_address(connection, dev_speed);
|
---|
214 | if (dev_addr < 0) {
|
---|
215 | return EADDRNOTAVAIL;
|
---|
216 | }
|
---|
217 |
|
---|
218 | int rc;
|
---|
219 |
|
---|
220 | /*
|
---|
221 | * Reserve the default address.
|
---|
222 | */
|
---|
223 | rc = usb_hc_reserve_default_address(connection, dev_speed);
|
---|
224 | if (rc != EOK) {
|
---|
225 | rc = EBUSY;
|
---|
226 | goto leave_release_free_address;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Enable the port (i.e. allow signaling through this port).
|
---|
231 | */
|
---|
232 | rc = enable_port(port_no, arg);
|
---|
233 | if (rc != EOK) {
|
---|
234 | goto leave_release_default_address;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Change the address from default to the free one.
|
---|
239 | * We need to create a new control pipe for that.
|
---|
240 | */
|
---|
241 | usb_device_connection_t dev_conn;
|
---|
242 | rc = usb_device_connection_initialize_on_default_address(&dev_conn,
|
---|
243 | connection);
|
---|
244 | if (rc != EOK) {
|
---|
245 | rc = ENOTCONN;
|
---|
246 | goto leave_release_default_address;
|
---|
247 | }
|
---|
248 |
|
---|
249 | usb_pipe_t ctrl_pipe;
|
---|
250 | rc = usb_pipe_initialize_default_control(&ctrl_pipe,
|
---|
251 | &dev_conn);
|
---|
252 | if (rc != EOK) {
|
---|
253 | rc = ENOTCONN;
|
---|
254 | goto leave_release_default_address;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* Before sending any traffic, we need to register this
|
---|
258 | * endpoint.
|
---|
259 | */
|
---|
260 | rc = usb_pipe_register(&ctrl_pipe, 0, connection);
|
---|
261 | if (rc != EOK) {
|
---|
262 | rc = EREFUSED;
|
---|
263 | goto leave_release_default_address;
|
---|
264 | }
|
---|
265 | rc = usb_pipe_probe_default_control(&ctrl_pipe);
|
---|
266 | if (rc != EOK) {
|
---|
267 | rc = ENOTCONN;
|
---|
268 | goto leave_release_default_address;
|
---|
269 | }
|
---|
270 |
|
---|
271 | rc = usb_pipe_start_session(&ctrl_pipe);
|
---|
272 | if (rc != EOK) {
|
---|
273 | rc = ENOTCONN;
|
---|
274 | goto leave_unregister_endpoint;
|
---|
275 | }
|
---|
276 |
|
---|
277 | rc = usb_request_set_address(&ctrl_pipe, dev_addr);
|
---|
278 | if (rc != EOK) {
|
---|
279 | rc = ESTALL;
|
---|
280 | goto leave_stop_session;
|
---|
281 | }
|
---|
282 |
|
---|
283 | usb_pipe_end_session(&ctrl_pipe);
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Register the control endpoint for the new device.
|
---|
287 | */
|
---|
288 | rc = usb_pipe_register(&ctrl_pipe, 0, connection);
|
---|
289 | if (rc != EOK) {
|
---|
290 | rc = EREFUSED;
|
---|
291 | goto leave_unregister_endpoint;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * Release the original endpoint.
|
---|
296 | */
|
---|
297 | unregister_control_endpoint_on_default_address(connection);
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Once the address is changed, we can return the default address.
|
---|
301 | */
|
---|
302 | usb_hc_release_default_address(connection);
|
---|
303 |
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * It is time to register the device with devman.
|
---|
307 | */
|
---|
308 | /* FIXME: create device_register that will get opened ctrl pipe. */
|
---|
309 | devman_handle_t child_handle;
|
---|
310 | rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
|
---|
311 | parent, &child_handle,
|
---|
312 | dev_ops, new_dev_data, new_fun);
|
---|
313 | if (rc != EOK) {
|
---|
314 | rc = ESTALL;
|
---|
315 | goto leave_release_free_address;
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * And now inform the host controller about the handle.
|
---|
322 | */
|
---|
323 | usb_hc_attached_device_t new_device = {
|
---|
324 | .address = dev_addr,
|
---|
325 | .handle = child_handle
|
---|
326 | };
|
---|
327 | rc = usb_hc_register_device(connection, &new_device);
|
---|
328 | if (rc != EOK) {
|
---|
329 | rc = EDESTADDRREQ;
|
---|
330 | goto leave_release_free_address;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * And we are done.
|
---|
335 | */
|
---|
336 | if (assigned_address != NULL) {
|
---|
337 | *assigned_address = dev_addr;
|
---|
338 | }
|
---|
339 | if (assigned_handle != NULL) {
|
---|
340 | *assigned_handle = child_handle;
|
---|
341 | }
|
---|
342 |
|
---|
343 | return EOK;
|
---|
344 |
|
---|
345 |
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * Error handling (like nested exceptions) starts here.
|
---|
349 | * Completely ignoring errors here.
|
---|
350 | */
|
---|
351 |
|
---|
352 | leave_stop_session:
|
---|
353 | usb_pipe_end_session(&ctrl_pipe);
|
---|
354 |
|
---|
355 | leave_unregister_endpoint:
|
---|
356 | usb_pipe_unregister(&ctrl_pipe, connection);
|
---|
357 |
|
---|
358 | leave_release_default_address:
|
---|
359 | usb_hc_release_default_address(connection);
|
---|
360 |
|
---|
361 | leave_release_free_address:
|
---|
362 | usb_hc_unregister_device(connection, dev_addr);
|
---|
363 |
|
---|
364 | return rc;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * @}
|
---|
369 | */
|
---|