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

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

uhcirh: make sure connection to hc is opoen during device removal.

Add error message and change error code to ENOTCONN if there is no connection to the hc.

  • Property mode set to 100644
File size: 10.6 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 libusbdev
30 * @{
31 */
32/** @file
33 * Functions needed by hub drivers.
34 */
35#include <usb/dev/hub.h>
36#include <usb/dev/pipes.h>
37#include <usb/dev/request.h>
38#include <usb/dev/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#include <async.h>
45
46/** How much time to wait between attempts to register endpoint 0:0.
47 * The value is based on typical value for port reset + some overhead.
48 */
49#define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
50
51/** Check that HC connection is alright.
52 *
53 * @param conn Connection to be checked.
54 */
55#define CHECK_CONNECTION(conn) \
56 do { \
57 assert((conn)); \
58 if (!usb_hc_connection_is_opened((conn))) { \
59 usb_log_error("Connection not open.\n"); \
60 return ENOTCONN; \
61 } \
62 } while (false)
63
64/** Ask host controller for free address assignment.
65 *
66 * @param connection Opened connection to host controller.
67 * @param speed Speed of the new device (device that will be assigned
68 * the returned address).
69 * @return Assigned USB address or negative error code.
70 */
71usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
72 usb_speed_t speed)
73{
74 CHECK_CONNECTION(connection);
75
76 async_exch_t *exch = async_exchange_begin(connection->hc_sess);
77
78 sysarg_t address;
79 int rc = async_req_2_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
80 IPC_M_USBHC_REQUEST_ADDRESS, speed,
81 &address);
82
83 async_exchange_end(exch);
84
85 if (rc != EOK)
86 return (usb_address_t) rc;
87
88 return (usb_address_t) address;
89}
90
91/** Inform host controller about new device.
92 *
93 * @param connection Opened connection to host controller.
94 * @param attached_device Information about the new device.
95 * @return Error code.
96 */
97int usb_hc_register_device(usb_hc_connection_t * connection,
98 const usb_hub_attached_device_t *attached_device)
99{
100 CHECK_CONNECTION(connection);
101
102 if (attached_device == NULL)
103 return EBADMEM;
104
105 async_exch_t *exch = async_exchange_begin(connection->hc_sess);
106 int rc = async_req_3_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
107 IPC_M_USBHC_BIND_ADDRESS,
108 attached_device->address, attached_device->fun->handle);
109 async_exchange_end(exch);
110
111 return rc;
112}
113
114/** Inform host controller about device removal.
115 *
116 * @param connection Opened connection to host controller.
117 * @param address Address of the device that is being removed.
118 * @return Error code.
119 */
120int usb_hc_unregister_device(usb_hc_connection_t *connection,
121 usb_address_t address)
122{
123 CHECK_CONNECTION(connection);
124
125 async_exch_t *exch = async_exchange_begin(connection->hc_sess);
126 int rc = async_req_2_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
127 IPC_M_USBHC_RELEASE_ADDRESS, address);
128 async_exchange_end(exch);
129
130 return rc;
131}
132
133
134static void unregister_control_endpoint_on_default_address(
135 usb_hc_connection_t *connection)
136{
137 usb_device_connection_t dev_conn;
138 int rc = usb_device_connection_initialize_on_default_address(&dev_conn,
139 connection);
140 if (rc != EOK) {
141 return;
142 }
143
144 usb_pipe_t ctrl_pipe;
145 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
146 if (rc != EOK) {
147 return;
148 }
149
150 usb_pipe_unregister(&ctrl_pipe, connection);
151}
152
153
154/** Wrapper for registering attached device to the hub.
155 *
156 * The @p enable_port function is expected to enable signaling on given
157 * port.
158 * The argument can have arbitrary meaning and it is not touched at all
159 * by this function (it is passed as is to the @p enable_port function).
160 *
161 * If the @p enable_port fails (i.e. does not return EOK), the device
162 * addition is canceled.
163 * The return value is then returned (it is good idea to use different
164 * error codes than those listed as return codes by this function itself).
165 *
166 * The @p connection representing connection with host controller does not
167 * need to be started.
168 * This function duplicates the connection to allow simultaneous calls of
169 * this function (i.e. from different fibrils).
170 *
171 * @param[in] parent Parent device (i.e. the hub device).
172 * @param[in] connection Connection to host controller.
173 * @param[in] dev_speed New device speed.
174 * @param[in] enable_port Function for enabling signaling through the port the
175 * device is attached to.
176 * @param[in] arg Any data argument to @p enable_port.
177 * @param[out] assigned_address USB address of the device.
178 * @param[in] dev_ops Child device ops.
179 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
180 * as @c driver_data.
181 * @param[out] new_fun Storage where pointer to allocated child function
182 * will be written.
183 * @return Error code.
184 * @retval ENOENT Connection to HC not opened.
185 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
186 * @retval EBUSY Failed reserving default USB address.
187 * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
188 * @retval ESTALL Problem communication with device (either SET_ADDRESS
189 * request or requests for descriptors when creating match ids).
190 */
191int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
192 usb_speed_t dev_speed,
193 int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
194 ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
195{
196 assert(connection != NULL);
197 // FIXME: this is awful, we are accessing directly the structure.
198 usb_hc_connection_t hc_conn = {
199 .hc_handle = connection->hc_handle,
200 .hc_sess = NULL
201 };
202
203 int rc;
204 struct timeval start_time;
205
206 rc = gettimeofday(&start_time, NULL);
207 if (rc != EOK) {
208 return rc;
209 }
210
211 rc = usb_hc_connection_open(&hc_conn);
212 if (rc != EOK) {
213 return rc;
214 }
215
216
217 /*
218 * Request new address.
219 */
220 usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
221 if (dev_addr < 0) {
222 usb_hc_connection_close(&hc_conn);
223 return EADDRNOTAVAIL;
224 }
225
226 /*
227 * We will not register control pipe on default address.
228 * The registration might fail. That means that someone else already
229 * registered that endpoint. We will simply wait and try again.
230 * (Someone else already wants to add a new device.)
231 */
232 usb_device_connection_t dev_conn;
233 rc = usb_device_connection_initialize_on_default_address(&dev_conn,
234 &hc_conn);
235 if (rc != EOK) {
236 rc = ENOTCONN;
237 goto leave_release_free_address;
238 }
239
240 usb_pipe_t ctrl_pipe;
241 rc = usb_pipe_initialize_default_control(&ctrl_pipe,
242 &dev_conn);
243 if (rc != EOK) {
244 rc = ENOTCONN;
245 goto leave_release_free_address;
246 }
247
248 do {
249 rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
250 &hc_conn);
251 if (rc != EOK) {
252 /* Do not overheat the CPU ;-). */
253 async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
254 }
255 } while (rc != EOK);
256 struct timeval end_time;
257
258 rc = gettimeofday(&end_time, NULL);
259 if (rc != EOK) {
260 goto leave_release_default_address;
261 }
262
263 /* According to the USB spec part 9.1.2 host allows 100ms time for
264 * the insertion process to complete. According to 7.1.7.1 this is the
265 * time between attach detected and port reset. However, the setup done
266 * above might use much of this time so we should only wait to fill
267 * up the 100ms quota*/
268 suseconds_t elapsed = tv_sub(&end_time, &start_time);
269 if (elapsed < 100000) {
270 async_usleep(100000 - elapsed);
271 }
272
273 /*
274 * Endpoint is registered. We can enable the port and change
275 * device address.
276 */
277 rc = enable_port(arg);
278 if (rc != EOK) {
279 goto leave_release_default_address;
280 }
281 /* USB spec 7.1.7.1: The USB System Software guarantees a minimum of
282 * 10ms for reset recovery. Device response to any bus transactions
283 * addressed to the default device address during the reset recovery
284 * time is undefined.
285 */
286 async_usleep(10000);
287
288 rc = usb_pipe_probe_default_control(&ctrl_pipe);
289 if (rc != EOK) {
290 rc = ESTALL;
291 goto leave_release_default_address;
292 }
293
294 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
295 if (rc != EOK) {
296 rc = ESTALL;
297 goto leave_release_default_address;
298 }
299
300 /*
301 * Address changed. We can release the original endpoint, thus
302 * allowing other to access the default address.
303 */
304 unregister_control_endpoint_on_default_address(&hc_conn);
305
306 /*
307 * Time to register the new endpoint.
308 */
309 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
310 if (rc != EOK) {
311 goto leave_release_free_address;
312 }
313
314 /*
315 * It is time to register the device with devman.
316 */
317 /* FIXME: create device_register that will get opened ctrl pipe. */
318 ddf_fun_t *child_fun;
319 rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
320 parent, dev_ops, new_dev_data, &child_fun);
321 if (rc != EOK) {
322 rc = ESTALL;
323 goto leave_release_free_address;
324 }
325
326 /*
327 * And now inform the host controller about the handle.
328 */
329 usb_hub_attached_device_t new_device = {
330 .address = dev_addr,
331 .fun = child_fun,
332 };
333 rc = usb_hc_register_device(&hc_conn, &new_device);
334 if (rc != EOK) {
335 rc = EDESTADDRREQ;
336 goto leave_release_free_address;
337 }
338
339 usb_hc_connection_close(&hc_conn);
340
341 /*
342 * And we are done.
343 */
344 if (assigned_address != NULL) {
345 *assigned_address = dev_addr;
346 }
347 if (new_fun != NULL) {
348 *new_fun = child_fun;
349 }
350
351 return EOK;
352
353
354
355 /*
356 * Error handling (like nested exceptions) starts here.
357 * Completely ignoring errors here.
358 */
359leave_release_default_address:
360 usb_pipe_unregister(&ctrl_pipe, &hc_conn);
361
362leave_release_free_address:
363 usb_hc_unregister_device(&hc_conn, dev_addr);
364
365 usb_hc_connection_close(&hc_conn);
366
367 return rc;
368}
369
370/**
371 * @}
372 */
Note: See TracBrowser for help on using the repository browser.