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

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

Rename usb_endpoint_pipe_*() ⇒ usb_pipe_*()

No change in functionality.

  • Property mode set to 100644
File size: 9.1 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
146
[6d8c5725]147/** Wrapper for registering attached device to the hub.
148 *
[a6add7a]149 * The @p enable_port function is expected to enable signaling on given
[6d8c5725]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
[a6add7a]157 * addition is canceled.
[6d8c5725]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 *
[bc1c6fb]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
[6d8c5725]165 * device is attached to.
[bc1c6fb]166 * @param[in] port_no Port number (passed through to @p enable_port).
167 * @param[in] arg Any data argument to @p enable_port.
[6d8c5725]168 * @param[out] assigned_address USB address of the device.
169 * @param[out] assigned_handle Devman handle of the new device.
[bc1c6fb]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.
[6d8c5725]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 */
[eb1a2f4]183int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
[6d8c5725]184 usb_speed_t dev_speed,
185 int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
[eb1a2f4]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)
[6d8c5725]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 /*
[a6add7a]211 * Enable the port (i.e. allow signaling through this port).
[6d8c5725]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
[a372663]230 usb_pipe_t ctrl_pipe;
[3954a63b]231 rc = usb_pipe_initialize_default_control(&ctrl_pipe,
[6d8c5725]232 &dev_conn);
233 if (rc != EOK) {
234 rc = ENOTCONN;
235 goto leave_release_default_address;
236 }
[3954a63b]237 rc = usb_pipe_probe_default_control(&ctrl_pipe);
[206f71a]238 if (rc != EOK) {
239 rc = ENOTCONN;
240 goto leave_release_default_address;
241 }
[6d8c5725]242
[3954a63b]243 rc = usb_pipe_start_session(&ctrl_pipe);
[6d8c5725]244 if (rc != EOK) {
245 rc = ENOTCONN;
246 goto leave_release_default_address;
247 }
248
249 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
250 if (rc != EOK) {
251 rc = ESTALL;
252 goto leave_stop_session;
253 }
254
[3954a63b]255 usb_pipe_end_session(&ctrl_pipe);
[6d8c5725]256
257 /*
258 * Once the address is changed, we can return the default address.
259 */
260 usb_hc_release_default_address(connection);
261
262 /*
263 * It is time to register the device with devman.
264 */
265 /* FIXME: create device_register that will get opened ctrl pipe. */
266 devman_handle_t child_handle;
267 rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
[eb1a2f4]268 parent, &child_handle,
269 dev_ops, new_dev_data, new_fun);
[6d8c5725]270 if (rc != EOK) {
271 rc = ESTALL;
272 goto leave_release_free_address;
273 }
274
275 /*
276 * And now inform the host controller about the handle.
277 */
278 usb_hc_attached_device_t new_device = {
279 .address = dev_addr,
280 .handle = child_handle
281 };
282 rc = usb_hc_register_device(connection, &new_device);
283 if (rc != EOK) {
284 rc = EDESTADDRREQ;
285 goto leave_release_free_address;
286 }
287
288 /*
289 * And we are done.
290 */
291 if (assigned_address != NULL) {
292 *assigned_address = dev_addr;
293 }
294 if (assigned_handle != NULL) {
295 *assigned_handle = child_handle;
296 }
297
298 return EOK;
299
300
301
302 /*
303 * Error handling (like nested exceptions) starts here.
304 * Completely ignoring errors here.
305 */
306
307leave_stop_session:
[3954a63b]308 usb_pipe_end_session(&ctrl_pipe);
[6d8c5725]309
310leave_release_default_address:
311 usb_hc_release_default_address(connection);
312
313leave_release_free_address:
314 usb_hc_unregister_device(connection, dev_addr);
315
316 return rc;
317}
318
[e40b9f00]319/**
320 * @}
321 */
Note: See TracBrowser for help on using the repository browser.