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

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

Merge development/ changes

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