source: mainline/uspace/lib/usb/src/hub.c@ 61727bf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61727bf was 61727bf, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Adding new devices can be done in parallel

The wrapper for adding new device duplicates the connection, thus
allowing more parallel device discovery.

  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[e40b9f00]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>
[6d8c5725]36#include <usb/pipes.h>
37#include <usb/request.h>
38#include <usb/recognise.h>
[e40b9f00]39#include <usbhc_iface.h>
40#include <errno.h>
[eb1a2f4]41#include <assert.h>
[e40b9f00]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.
[bc1c6fb]59 * @param speed Speed of the device that will respond on the default address.
[e40b9f00]60 * @return Error code.
61 */
[6427cf67]62int usb_hc_reserve_default_address(usb_hc_connection_t *connection,
[fa48ebe]63 usb_speed_t speed)
[e40b9f00]64{
65 CHECK_CONNECTION(connection);
66
[6427cf67]67 return async_req_2_0(connection->hc_phone,
[e40b9f00]68 DEV_IFACE_ID(USBHC_DEV_IFACE),
[fa48ebe]69 IPC_M_USBHC_RESERVE_DEFAULT_ADDRESS, speed);
[e40b9f00]70}
71
72/** Tell host controller to release default address.
73 *
74 * @param connection Opened connection to host controller.
75 * @return Error code.
76 */
77int 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.
[bc1c6fb]89 * @param speed Speed of the new device (device that will be assigned
90 * the returned address).
[e40b9f00]91 * @return Assigned USB address or negative error code.
92 */
[6427cf67]93usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
[fa48ebe]94 usb_speed_t speed)
[e40b9f00]95{
96 CHECK_CONNECTION(connection);
97
98 sysarg_t address;
[6427cf67]99 int rc = async_req_2_1(connection->hc_phone,
[e40b9f00]100 DEV_IFACE_ID(USBHC_DEV_IFACE),
[fa48ebe]101 IPC_M_USBHC_REQUEST_ADDRESS, speed,
[6427cf67]102 &address);
[e40b9f00]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 */
116int 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 */
136int 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
[9f9b31ad]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
[e40b9f00]165
[6d8c5725]166/** Wrapper for registering attached device to the hub.
167 *
[a6add7a]168 * The @p enable_port function is expected to enable signaling on given
[6d8c5725]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
[a6add7a]176 * addition is canceled.
[6d8c5725]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 *
[61727bf]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 *
[bc1c6fb]185 * @param[in] parent Parent device (i.e. the hub device).
[61727bf]186 * @param[in] connection Connection to host controller.
[bc1c6fb]187 * @param[in] dev_speed New device speed.
188 * @param[in] enable_port Function for enabling signaling through the port the
[6d8c5725]189 * device is attached to.
[bc1c6fb]190 * @param[in] port_no Port number (passed through to @p enable_port).
191 * @param[in] arg Any data argument to @p enable_port.
[6d8c5725]192 * @param[out] assigned_address USB address of the device.
193 * @param[out] assigned_handle Devman handle of the new device.
[bc1c6fb]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.
[6d8c5725]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 */
[eb1a2f4]207int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
[6d8c5725]208 usb_speed_t dev_speed,
209 int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
[eb1a2f4]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)
[6d8c5725]212{
[61727bf]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
[6d8c5725]227
228 /*
229 * Request new address.
230 */
[61727bf]231 usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
[6d8c5725]232 if (dev_addr < 0) {
[61727bf]233 usb_hc_connection_close(&hc_conn);
[6d8c5725]234 return EADDRNOTAVAIL;
235 }
236
237
238 /*
239 * Reserve the default address.
240 */
[61727bf]241 rc = usb_hc_reserve_default_address(&hc_conn, dev_speed);
[6d8c5725]242 if (rc != EOK) {
243 rc = EBUSY;
244 goto leave_release_free_address;
245 }
246
247 /*
[a6add7a]248 * Enable the port (i.e. allow signaling through this port).
[6d8c5725]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,
[61727bf]261 &hc_conn);
[6d8c5725]262 if (rc != EOK) {
263 rc = ENOTCONN;
264 goto leave_release_default_address;
265 }
266
[a372663]267 usb_pipe_t ctrl_pipe;
[3954a63b]268 rc = usb_pipe_initialize_default_control(&ctrl_pipe,
[6d8c5725]269 &dev_conn);
270 if (rc != EOK) {
271 rc = ENOTCONN;
272 goto leave_release_default_address;
273 }
[9f9b31ad]274
275 /* Before sending any traffic, we need to register this
276 * endpoint.
277 */
[61727bf]278 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
[9f9b31ad]279 if (rc != EOK) {
280 rc = EREFUSED;
281 goto leave_release_default_address;
282 }
[3954a63b]283 rc = usb_pipe_probe_default_control(&ctrl_pipe);
[206f71a]284 if (rc != EOK) {
285 rc = ENOTCONN;
286 goto leave_release_default_address;
287 }
[6d8c5725]288
[3954a63b]289 rc = usb_pipe_start_session(&ctrl_pipe);
[6d8c5725]290 if (rc != EOK) {
291 rc = ENOTCONN;
[9f9b31ad]292 goto leave_unregister_endpoint;
[6d8c5725]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
[3954a63b]301 usb_pipe_end_session(&ctrl_pipe);
[6d8c5725]302
[9f9b31ad]303 /*
304 * Register the control endpoint for the new device.
305 */
[61727bf]306 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
[9f9b31ad]307 if (rc != EOK) {
308 rc = EREFUSED;
309 goto leave_unregister_endpoint;
310 }
311
312 /*
313 * Release the original endpoint.
314 */
[61727bf]315 unregister_control_endpoint_on_default_address(&hc_conn);
[9f9b31ad]316
[6d8c5725]317 /*
318 * Once the address is changed, we can return the default address.
319 */
[61727bf]320 usb_hc_release_default_address(&hc_conn);
[6d8c5725]321
[9f9b31ad]322
[6d8c5725]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,
[eb1a2f4]329 parent, &child_handle,
330 dev_ops, new_dev_data, new_fun);
[6d8c5725]331 if (rc != EOK) {
332 rc = ESTALL;
333 goto leave_release_free_address;
334 }
335
[9f9b31ad]336
337
[6d8c5725]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 };
[61727bf]345 rc = usb_hc_register_device(&hc_conn, &new_device);
[6d8c5725]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
370leave_stop_session:
[3954a63b]371 usb_pipe_end_session(&ctrl_pipe);
[6d8c5725]372
[9f9b31ad]373leave_unregister_endpoint:
[61727bf]374 usb_pipe_unregister(&ctrl_pipe, &hc_conn);
[9f9b31ad]375
[6d8c5725]376leave_release_default_address:
[61727bf]377 usb_hc_release_default_address(&hc_conn);
[6d8c5725]378
379leave_release_free_address:
[61727bf]380 usb_hc_unregister_device(&hc_conn, dev_addr);
381
382 usb_hc_connection_close(&hc_conn);
[6d8c5725]383
384 return rc;
385}
386
[e40b9f00]387/**
388 * @}
389 */
Note: See TracBrowser for help on using the repository browser.