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

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

Doxygen comments fixes

No change in functionality.

  • Property mode set to 100644
File size: 9.0 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
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.
59 * @param speed Speed of the device that will respond on the default address.
60 * @return Error code.
61 */
62int usb_hc_reserve_default_address(usb_hc_connection_t *connection,
63 usb_speed_t speed)
64{
65 CHECK_CONNECTION(connection);
66
67 return async_req_2_0(connection->hc_phone,
68 DEV_IFACE_ID(USBHC_DEV_IFACE),
69 IPC_M_USBHC_RESERVE_DEFAULT_ADDRESS, speed);
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.
89 * @param speed Speed of the new device (device that will be assigned
90 * the returned address).
91 * @return Assigned USB address or negative error code.
92 */
93usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
94 usb_speed_t speed)
95{
96 CHECK_CONNECTION(connection);
97
98 sysarg_t address;
99 int rc = async_req_2_1(connection->hc_phone,
100 DEV_IFACE_ID(USBHC_DEV_IFACE),
101 IPC_M_USBHC_REQUEST_ADDRESS, speed,
102 &address);
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
146
147/** Wrapper for registering attached device to the hub.
148 *
149 * The @p enable_port function is expected to enable signaling on given
150 * port.
151 * The two arguments to it can have arbitrary meaning
152 * (the @p port_no is only a suggestion)
153 * and are not touched at all by this function
154 * (they are passed as is to the @p enable_port function).
155 *
156 * If the @p enable_port fails (i.e. does not return EOK), the device
157 * addition is canceled.
158 * The return value is then returned (it is good idea to use different
159 * error codes than those listed as return codes by this function itself).
160 *
161 * @param[in] parent Parent device (i.e. the hub device).
162 * @param[in] connection Opened connection to host controller.
163 * @param[in] dev_speed New device speed.
164 * @param[in] enable_port Function for enabling signaling through the port the
165 * device is attached to.
166 * @param[in] port_no Port number (passed through to @p enable_port).
167 * @param[in] arg Any data argument to @p enable_port.
168 * @param[out] assigned_address USB address of the device.
169 * @param[out] assigned_handle Devman handle of the new device.
170 * @param[in] dev_ops Child device ops.
171 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
172 * as @c driver_data.
173 * @param[out] new_fun Storage where pointer to allocated child function
174 * will be written.
175 * @return Error code.
176 * @retval ENOENT Connection to HC not opened.
177 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
178 * @retval EBUSY Failed reserving default USB address.
179 * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
180 * @retval ESTALL Problem communication with device (either SET_ADDRESS
181 * request or requests for descriptors when creating match ids).
182 */
183int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
184 usb_speed_t dev_speed,
185 int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
186 usb_address_t *assigned_address, devman_handle_t *assigned_handle,
187 ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
188{
189 CHECK_CONNECTION(connection);
190
191 /*
192 * Request new address.
193 */
194 usb_address_t dev_addr = usb_hc_request_address(connection, dev_speed);
195 if (dev_addr < 0) {
196 return EADDRNOTAVAIL;
197 }
198
199 int rc;
200
201 /*
202 * Reserve the default address.
203 */
204 rc = usb_hc_reserve_default_address(connection, dev_speed);
205 if (rc != EOK) {
206 rc = EBUSY;
207 goto leave_release_free_address;
208 }
209
210 /*
211 * Enable the port (i.e. allow signaling through this port).
212 */
213 rc = enable_port(port_no, arg);
214 if (rc != EOK) {
215 goto leave_release_default_address;
216 }
217
218 /*
219 * Change the address from default to the free one.
220 * We need to create a new control pipe for that.
221 */
222 usb_device_connection_t dev_conn;
223 rc = usb_device_connection_initialize_on_default_address(&dev_conn,
224 connection);
225 if (rc != EOK) {
226 rc = ENOTCONN;
227 goto leave_release_default_address;
228 }
229
230 usb_endpoint_pipe_t ctrl_pipe;
231 rc = usb_endpoint_pipe_initialize_default_control(&ctrl_pipe,
232 &dev_conn);
233 if (rc != EOK) {
234 rc = ENOTCONN;
235 goto leave_release_default_address;
236 }
237
238 rc = usb_endpoint_pipe_start_session(&ctrl_pipe);
239 if (rc != EOK) {
240 rc = ENOTCONN;
241 goto leave_release_default_address;
242 }
243
244 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
245 if (rc != EOK) {
246 rc = ESTALL;
247 goto leave_stop_session;
248 }
249
250 usb_endpoint_pipe_end_session(&ctrl_pipe);
251
252 /*
253 * Once the address is changed, we can return the default address.
254 */
255 usb_hc_release_default_address(connection);
256
257 /*
258 * It is time to register the device with devman.
259 */
260 /* FIXME: create device_register that will get opened ctrl pipe. */
261 devman_handle_t child_handle;
262 rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
263 parent, &child_handle,
264 dev_ops, new_dev_data, new_fun);
265 if (rc != EOK) {
266 rc = ESTALL;
267 goto leave_release_free_address;
268 }
269
270 /*
271 * And now inform the host controller about the handle.
272 */
273 usb_hc_attached_device_t new_device = {
274 .address = dev_addr,
275 .handle = child_handle
276 };
277 rc = usb_hc_register_device(connection, &new_device);
278 if (rc != EOK) {
279 rc = EDESTADDRREQ;
280 goto leave_release_free_address;
281 }
282
283 /*
284 * And we are done.
285 */
286 if (assigned_address != NULL) {
287 *assigned_address = dev_addr;
288 }
289 if (assigned_handle != NULL) {
290 *assigned_handle = child_handle;
291 }
292
293 return EOK;
294
295
296
297 /*
298 * Error handling (like nested exceptions) starts here.
299 * Completely ignoring errors here.
300 */
301
302leave_stop_session:
303 usb_endpoint_pipe_end_session(&ctrl_pipe);
304
305leave_release_default_address:
306 usb_hc_release_default_address(connection);
307
308leave_release_free_address:
309 usb_hc_unregister_device(connection, dev_addr);
310
311 return rc;
312}
313
314/**
315 * @}
316 */
Note: See TracBrowser for help on using the repository browser.