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 | * The @p connection representing connection with host controller does not
|
---|
181 | * need to be started.
|
---|
182 | * This function duplicates the connection to allow simultaneous calls of
|
---|
183 | * this function (i.e. from different fibrils).
|
---|
184 | *
|
---|
185 | * @param[in] parent Parent device (i.e. the hub device).
|
---|
186 | * @param[in] connection Connection to host controller.
|
---|
187 | * @param[in] dev_speed New device speed.
|
---|
188 | * @param[in] enable_port Function for enabling signaling through the port the
|
---|
189 | * device is attached to.
|
---|
190 | * @param[in] port_no Port number (passed through to @p enable_port).
|
---|
191 | * @param[in] arg Any data argument to @p enable_port.
|
---|
192 | * @param[out] assigned_address USB address of the device.
|
---|
193 | * @param[out] assigned_handle Devman handle of the new device.
|
---|
194 | * @param[in] dev_ops Child device ops.
|
---|
195 | * @param[in] new_dev_data Arbitrary pointer to be stored in the child
|
---|
196 | * as @c driver_data.
|
---|
197 | * @param[out] new_fun Storage where pointer to allocated child function
|
---|
198 | * will be written.
|
---|
199 | * @return Error code.
|
---|
200 | * @retval ENOENT Connection to HC not opened.
|
---|
201 | * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
|
---|
202 | * @retval EBUSY Failed reserving default USB address.
|
---|
203 | * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
|
---|
204 | * @retval ESTALL Problem communication with device (either SET_ADDRESS
|
---|
205 | * request or requests for descriptors when creating match ids).
|
---|
206 | */
|
---|
207 | int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
|
---|
208 | usb_speed_t dev_speed,
|
---|
209 | int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
|
---|
210 | usb_address_t *assigned_address, devman_handle_t *assigned_handle,
|
---|
211 | ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
|
---|
212 | {
|
---|
213 | assert(connection != NULL);
|
---|
214 | // FIXME: this is awful, we are accessing directly the structure.
|
---|
215 | usb_hc_connection_t hc_conn = {
|
---|
216 | .hc_handle = connection->hc_handle,
|
---|
217 | .hc_phone = -1
|
---|
218 | };
|
---|
219 |
|
---|
220 | int rc;
|
---|
221 |
|
---|
222 | rc = usb_hc_connection_open(&hc_conn);
|
---|
223 | if (rc != EOK) {
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Request new address.
|
---|
230 | */
|
---|
231 | usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
|
---|
232 | if (dev_addr < 0) {
|
---|
233 | usb_hc_connection_close(&hc_conn);
|
---|
234 | return EADDRNOTAVAIL;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Reserve the default address.
|
---|
240 | */
|
---|
241 | rc = usb_hc_reserve_default_address(&hc_conn, dev_speed);
|
---|
242 | if (rc != EOK) {
|
---|
243 | rc = EBUSY;
|
---|
244 | goto leave_release_free_address;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Enable the port (i.e. allow signaling through this port).
|
---|
249 | */
|
---|
250 | rc = enable_port(port_no, arg);
|
---|
251 | if (rc != EOK) {
|
---|
252 | goto leave_release_default_address;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Change the address from default to the free one.
|
---|
257 | * We need to create a new control pipe for that.
|
---|
258 | */
|
---|
259 | usb_device_connection_t dev_conn;
|
---|
260 | rc = usb_device_connection_initialize_on_default_address(&dev_conn,
|
---|
261 | &hc_conn);
|
---|
262 | if (rc != EOK) {
|
---|
263 | rc = ENOTCONN;
|
---|
264 | goto leave_release_default_address;
|
---|
265 | }
|
---|
266 |
|
---|
267 | usb_pipe_t ctrl_pipe;
|
---|
268 | rc = usb_pipe_initialize_default_control(&ctrl_pipe,
|
---|
269 | &dev_conn);
|
---|
270 | if (rc != EOK) {
|
---|
271 | rc = ENOTCONN;
|
---|
272 | goto leave_release_default_address;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* Before sending any traffic, we need to register this
|
---|
276 | * endpoint.
|
---|
277 | */
|
---|
278 | rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
|
---|
279 | if (rc != EOK) {
|
---|
280 | rc = EREFUSED;
|
---|
281 | goto leave_release_default_address;
|
---|
282 | }
|
---|
283 | rc = usb_pipe_probe_default_control(&ctrl_pipe);
|
---|
284 | if (rc != EOK) {
|
---|
285 | rc = ENOTCONN;
|
---|
286 | goto leave_release_default_address;
|
---|
287 | }
|
---|
288 |
|
---|
289 | rc = usb_pipe_start_session(&ctrl_pipe);
|
---|
290 | if (rc != EOK) {
|
---|
291 | rc = ENOTCONN;
|
---|
292 | goto leave_unregister_endpoint;
|
---|
293 | }
|
---|
294 |
|
---|
295 | rc = usb_request_set_address(&ctrl_pipe, dev_addr);
|
---|
296 | if (rc != EOK) {
|
---|
297 | rc = ESTALL;
|
---|
298 | goto leave_stop_session;
|
---|
299 | }
|
---|
300 |
|
---|
301 | usb_pipe_end_session(&ctrl_pipe);
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Register the control endpoint for the new device.
|
---|
305 | */
|
---|
306 | rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
|
---|
307 | if (rc != EOK) {
|
---|
308 | rc = EREFUSED;
|
---|
309 | goto leave_unregister_endpoint;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * Release the original endpoint.
|
---|
314 | */
|
---|
315 | unregister_control_endpoint_on_default_address(&hc_conn);
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Once the address is changed, we can return the default address.
|
---|
319 | */
|
---|
320 | usb_hc_release_default_address(&hc_conn);
|
---|
321 |
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * It is time to register the device with devman.
|
---|
325 | */
|
---|
326 | /* FIXME: create device_register that will get opened ctrl pipe. */
|
---|
327 | devman_handle_t child_handle;
|
---|
328 | rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
|
---|
329 | parent, &child_handle,
|
---|
330 | dev_ops, new_dev_data, new_fun);
|
---|
331 | if (rc != EOK) {
|
---|
332 | rc = ESTALL;
|
---|
333 | goto leave_release_free_address;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * And now inform the host controller about the handle.
|
---|
340 | */
|
---|
341 | usb_hc_attached_device_t new_device = {
|
---|
342 | .address = dev_addr,
|
---|
343 | .handle = child_handle
|
---|
344 | };
|
---|
345 | rc = usb_hc_register_device(&hc_conn, &new_device);
|
---|
346 | if (rc != EOK) {
|
---|
347 | rc = EDESTADDRREQ;
|
---|
348 | goto leave_release_free_address;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*
|
---|
352 | * And we are done.
|
---|
353 | */
|
---|
354 | if (assigned_address != NULL) {
|
---|
355 | *assigned_address = dev_addr;
|
---|
356 | }
|
---|
357 | if (assigned_handle != NULL) {
|
---|
358 | *assigned_handle = child_handle;
|
---|
359 | }
|
---|
360 |
|
---|
361 | return EOK;
|
---|
362 |
|
---|
363 |
|
---|
364 |
|
---|
365 | /*
|
---|
366 | * Error handling (like nested exceptions) starts here.
|
---|
367 | * Completely ignoring errors here.
|
---|
368 | */
|
---|
369 |
|
---|
370 | leave_stop_session:
|
---|
371 | usb_pipe_end_session(&ctrl_pipe);
|
---|
372 |
|
---|
373 | leave_unregister_endpoint:
|
---|
374 | usb_pipe_unregister(&ctrl_pipe, &hc_conn);
|
---|
375 |
|
---|
376 | leave_release_default_address:
|
---|
377 | usb_hc_release_default_address(&hc_conn);
|
---|
378 |
|
---|
379 | leave_release_free_address:
|
---|
380 | usb_hc_unregister_device(&hc_conn, dev_addr);
|
---|
381 |
|
---|
382 | usb_hc_connection_close(&hc_conn);
|
---|
383 |
|
---|
384 | return rc;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * @}
|
---|
389 | */
|
---|