source: mainline/uspace/lib/usbdev/src/hub.c@ d736fe38

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d736fe38 was 5f94a0c, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Add timeouts to the enumeration routine

  • Property mode set to 100644
File size: 11.1 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 * 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#include <usb/debug.h>
43#include <time.h>
44
45/** How much time to wait between attempts to register endpoint 0:0.
46 * The value is based on typical value for port reset + some overhead.
47 */
48#define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
49
50/** Check that HC connection is alright.
51 *
52 * @param conn Connection to be checked.
53 */
54#define CHECK_CONNECTION(conn) \
55 do { \
56 assert((conn)); \
57 if (!usb_hc_connection_is_opened((conn))) { \
58 return ENOENT; \
59 } \
60 } while (false)
61
62/** Ask host controller for free address assignment.
63 *
64 * @param connection Opened connection to host controller.
65 * @param speed Speed of the new device (device that will be assigned
66 * the returned address).
67 * @return Assigned USB address or negative error code.
68 */
69usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
70 usb_speed_t speed)
71{
72 CHECK_CONNECTION(connection);
73
74 sysarg_t address;
75 int rc = async_req_2_1(connection->hc_phone,
76 DEV_IFACE_ID(USBHC_DEV_IFACE),
77 IPC_M_USBHC_REQUEST_ADDRESS, speed,
78 &address);
79 if (rc != EOK) {
80 return (usb_address_t) rc;
81 } else {
82 return (usb_address_t) address;
83 }
84}
85
86/** Inform host controller about new device.
87 *
88 * @param connection Opened connection to host controller.
89 * @param attached_device Information about the new device.
90 * @return Error code.
91 */
92int usb_hc_register_device(usb_hc_connection_t * connection,
93 const usb_hc_attached_device_t *attached_device)
94{
95 CHECK_CONNECTION(connection);
96 if (attached_device == NULL) {
97 return EBADMEM;
98 }
99
100 return async_req_3_0(connection->hc_phone,
101 DEV_IFACE_ID(USBHC_DEV_IFACE),
102 IPC_M_USBHC_BIND_ADDRESS,
103 attached_device->address, attached_device->handle);
104}
105
106/** Inform host controller about device removal.
107 *
108 * @param connection Opened connection to host controller.
109 * @param address Address of the device that is being removed.
110 * @return Error code.
111 */
112int usb_hc_unregister_device(usb_hc_connection_t *connection,
113 usb_address_t address)
114{
115 CHECK_CONNECTION(connection);
116
117 return async_req_2_0(connection->hc_phone,
118 DEV_IFACE_ID(USBHC_DEV_IFACE),
119 IPC_M_USBHC_RELEASE_ADDRESS, address);
120}
121
122/** Get handle of USB device with given address.
123 *
124 * @param[in] connection Opened connection to host controller.
125 * @param[in] address Address of device in question.
126 * @param[out] handle Where to write the device handle.
127 * @return Error code.
128 */
129int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
130 usb_address_t address, devman_handle_t *handle)
131{
132 CHECK_CONNECTION(connection);
133
134 sysarg_t tmp;
135 int rc = async_req_2_1(connection->hc_phone,
136 DEV_IFACE_ID(USBHC_DEV_IFACE),
137 IPC_M_USBHC_GET_HANDLE_BY_ADDRESS,
138 address, &tmp);
139 if ((rc == EOK) && (handle != NULL)) {
140 *handle = tmp;
141 }
142
143 return rc;
144}
145
146static 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 */
207int 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 struct timeval start_time;
222
223 rc = gettimeofday(&start_time, NULL);
224 if (rc != EOK) {
225 return rc;
226 }
227
228 rc = usb_hc_connection_open(&hc_conn);
229 if (rc != EOK) {
230 return rc;
231 }
232
233
234 /*
235 * Request new address.
236 */
237 usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
238 if (dev_addr < 0) {
239 usb_hc_connection_close(&hc_conn);
240 return EADDRNOTAVAIL;
241 }
242
243 /*
244 * We will not register control pipe on default address.
245 * The registration might fail. That means that someone else already
246 * registered that endpoint. We will simply wait and try again.
247 * (Someone else already wants to add a new device.)
248 */
249 usb_device_connection_t dev_conn;
250 rc = usb_device_connection_initialize_on_default_address(&dev_conn,
251 &hc_conn);
252 if (rc != EOK) {
253 rc = ENOTCONN;
254 goto leave_release_free_address;
255 }
256
257 usb_pipe_t ctrl_pipe;
258 rc = usb_pipe_initialize_default_control(&ctrl_pipe,
259 &dev_conn);
260 if (rc != EOK) {
261 rc = ENOTCONN;
262 goto leave_release_free_address;
263 }
264
265 do {
266 rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
267 &hc_conn);
268 if (rc != EOK) {
269 /* Do not overheat the CPU ;-). */
270 async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
271 }
272 } while (rc != EOK);
273 struct timeval end_time;
274
275 rc = gettimeofday(&end_time, NULL);
276 if (rc != EOK) {
277 goto leave_release_default_address;
278 }
279
280 /* According to the USB spec part 9.1.2 host allows 100ms time for
281 * the insertion process to complete. According to 7.1.7.1 this is the
282 * time between attach detected and port reset. However, the setup done
283 * above might use much of this time so we should only wait to fill
284 * up the 100ms quota*/
285 suseconds_t elapsed = tv_sub(&end_time, &start_time);
286 if (elapsed < 100000) {
287 async_usleep(100000 - elapsed);
288 }
289
290 /*
291 * Endpoint is registered. We can enable the port and change
292 * device address.
293 */
294 rc = enable_port(port_no, arg);
295 if (rc != EOK) {
296 goto leave_release_default_address;
297 }
298 /* USB spec 7.1.7.1: The USB System Software guarantees a minimum of
299 * 10ms for reset recovery. Device response to any bus transactions
300 * addressed to the default device address during the reset recovery
301 * time is undefined.
302 */
303 async_usleep(10000);
304
305 rc = usb_pipe_probe_default_control(&ctrl_pipe);
306 if (rc != EOK) {
307 rc = ESTALL;
308 goto leave_release_default_address;
309 }
310
311 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
312 if (rc != EOK) {
313 rc = ESTALL;
314 goto leave_release_default_address;
315 }
316
317 /*
318 * Address changed. We can release the original endpoint, thus
319 * allowing other to access the default address.
320 */
321 unregister_control_endpoint_on_default_address(&hc_conn);
322
323 /*
324 * Time to register the new endpoint.
325 */
326 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
327 if (rc != EOK) {
328 goto leave_release_free_address;
329 }
330
331 /*
332 * It is time to register the device with devman.
333 */
334 /* FIXME: create device_register that will get opened ctrl pipe. */
335 devman_handle_t child_handle;
336 rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
337 parent, &child_handle,
338 dev_ops, new_dev_data, new_fun);
339 if (rc != EOK) {
340 rc = ESTALL;
341 goto leave_release_free_address;
342 }
343
344 /*
345 * And now inform the host controller about the handle.
346 */
347 usb_hc_attached_device_t new_device = {
348 .address = dev_addr,
349 .handle = child_handle
350 };
351 rc = usb_hc_register_device(&hc_conn, &new_device);
352 if (rc != EOK) {
353 rc = EDESTADDRREQ;
354 goto leave_release_free_address;
355 }
356
357 /*
358 * And we are done.
359 */
360 if (assigned_address != NULL) {
361 *assigned_address = dev_addr;
362 }
363 if (assigned_handle != NULL) {
364 *assigned_handle = child_handle;
365 }
366
367 return EOK;
368
369
370
371 /*
372 * Error handling (like nested exceptions) starts here.
373 * Completely ignoring errors here.
374 */
375leave_release_default_address:
376 usb_pipe_unregister(&ctrl_pipe, &hc_conn);
377
378leave_release_free_address:
379 usb_hc_unregister_device(&hc_conn, dev_addr);
380
381 usb_hc_connection_close(&hc_conn);
382
383 return rc;
384}
385
386/**
387 * @}
388 */
Note: See TracBrowser for help on using the repository browser.