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

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

libusbdev: usb_device_register_child_in_devman takes open control pipe.

Instead of doing all the initialization again.

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