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

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

Getting default address less active

Now the fibril sleeps for at least the time needed for a
typical reset.

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