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 | * @return Error code.
|
---|
60 | */
|
---|
61 | int usb_hc_reserve_default_address(usb_hc_connection_t *connection,
|
---|
62 | usb_speed_t speed)
|
---|
63 | {
|
---|
64 | CHECK_CONNECTION(connection);
|
---|
65 |
|
---|
66 | return async_req_2_0(connection->hc_phone,
|
---|
67 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
68 | IPC_M_USBHC_RESERVE_DEFAULT_ADDRESS, speed);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Tell host controller to release default address.
|
---|
72 | *
|
---|
73 | * @param connection Opened connection to host controller.
|
---|
74 | * @return Error code.
|
---|
75 | */
|
---|
76 | int usb_hc_release_default_address(usb_hc_connection_t *connection)
|
---|
77 | {
|
---|
78 | CHECK_CONNECTION(connection);
|
---|
79 |
|
---|
80 | return async_req_1_0(connection->hc_phone,
|
---|
81 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
82 | IPC_M_USBHC_RELEASE_DEFAULT_ADDRESS);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Ask host controller for free address assignment.
|
---|
86 | *
|
---|
87 | * @param connection Opened connection to host controller.
|
---|
88 | * @return Assigned USB address or negative error code.
|
---|
89 | */
|
---|
90 | usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
|
---|
91 | usb_speed_t speed)
|
---|
92 | {
|
---|
93 | CHECK_CONNECTION(connection);
|
---|
94 |
|
---|
95 | sysarg_t address;
|
---|
96 | int rc = async_req_2_1(connection->hc_phone,
|
---|
97 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
98 | IPC_M_USBHC_REQUEST_ADDRESS, speed,
|
---|
99 | &address);
|
---|
100 | if (rc != EOK) {
|
---|
101 | return (usb_address_t) rc;
|
---|
102 | } else {
|
---|
103 | return (usb_address_t) address;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Inform host controller about new device.
|
---|
108 | *
|
---|
109 | * @param connection Opened connection to host controller.
|
---|
110 | * @param attached_device Information about the new device.
|
---|
111 | * @return Error code.
|
---|
112 | */
|
---|
113 | int usb_hc_register_device(usb_hc_connection_t * connection,
|
---|
114 | const usb_hc_attached_device_t *attached_device)
|
---|
115 | {
|
---|
116 | CHECK_CONNECTION(connection);
|
---|
117 | if (attached_device == NULL) {
|
---|
118 | return EBADMEM;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return async_req_3_0(connection->hc_phone,
|
---|
122 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
123 | IPC_M_USBHC_BIND_ADDRESS,
|
---|
124 | attached_device->address, attached_device->handle);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Inform host controller about device removal.
|
---|
128 | *
|
---|
129 | * @param connection Opened connection to host controller.
|
---|
130 | * @param address Address of the device that is being removed.
|
---|
131 | * @return Error code.
|
---|
132 | */
|
---|
133 | int usb_hc_unregister_device(usb_hc_connection_t *connection,
|
---|
134 | usb_address_t address)
|
---|
135 | {
|
---|
136 | CHECK_CONNECTION(connection);
|
---|
137 |
|
---|
138 | return async_req_2_0(connection->hc_phone,
|
---|
139 | DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
140 | IPC_M_USBHC_RELEASE_ADDRESS, address);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /** Wrapper for registering attached device to the hub.
|
---|
145 | *
|
---|
146 | * The @p enable_port function is expected to enable singalling on given
|
---|
147 | * port.
|
---|
148 | * The two arguments to it can have arbitrary meaning
|
---|
149 | * (the @p port_no is only a suggestion)
|
---|
150 | * and are not touched at all by this function
|
---|
151 | * (they are passed as is to the @p enable_port function).
|
---|
152 | *
|
---|
153 | * If the @p enable_port fails (i.e. does not return EOK), the device
|
---|
154 | * addition is cancelled.
|
---|
155 | * The return value is then returned (it is good idea to use different
|
---|
156 | * error codes than those listed as return codes by this function itself).
|
---|
157 | *
|
---|
158 | * @param parent Parent device (i.e. the hub device).
|
---|
159 | * @param connection Opened connection to host controller.
|
---|
160 | * @param dev_speed New device speed.
|
---|
161 | * @param enable_port Function for enabling signalling through the port the
|
---|
162 | * device is attached to.
|
---|
163 | * @param port_no Port number (passed through to @p enable_port).
|
---|
164 | * @param arg Any data argument to @p enable_port.
|
---|
165 | * @param[out] assigned_address USB address of the device.
|
---|
166 | * @param[out] assigned_handle Devman handle of the new device.
|
---|
167 | * @return Error code.
|
---|
168 | * @retval ENOENT Connection to HC not opened.
|
---|
169 | * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
|
---|
170 | * @retval EBUSY Failed reserving default USB address.
|
---|
171 | * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
|
---|
172 | * @retval ESTALL Problem communication with device (either SET_ADDRESS
|
---|
173 | * request or requests for descriptors when creating match ids).
|
---|
174 | */
|
---|
175 | int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
|
---|
176 | usb_speed_t dev_speed,
|
---|
177 | int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
|
---|
178 | usb_address_t *assigned_address, devman_handle_t *assigned_handle,
|
---|
179 | ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
|
---|
180 | {
|
---|
181 | CHECK_CONNECTION(connection);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Request new address.
|
---|
185 | */
|
---|
186 | usb_address_t dev_addr = usb_hc_request_address(connection, dev_speed);
|
---|
187 | if (dev_addr < 0) {
|
---|
188 | return EADDRNOTAVAIL;
|
---|
189 | }
|
---|
190 |
|
---|
191 | int rc;
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Reserve the default address.
|
---|
195 | */
|
---|
196 | rc = usb_hc_reserve_default_address(connection, dev_speed);
|
---|
197 | if (rc != EOK) {
|
---|
198 | rc = EBUSY;
|
---|
199 | goto leave_release_free_address;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * Enable the port (i.e. allow signalling through this port).
|
---|
204 | */
|
---|
205 | rc = enable_port(port_no, arg);
|
---|
206 | if (rc != EOK) {
|
---|
207 | goto leave_release_default_address;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Change the address from default to the free one.
|
---|
212 | * We need to create a new control pipe for that.
|
---|
213 | */
|
---|
214 | usb_device_connection_t dev_conn;
|
---|
215 | rc = usb_device_connection_initialize_on_default_address(&dev_conn,
|
---|
216 | connection);
|
---|
217 | if (rc != EOK) {
|
---|
218 | rc = ENOTCONN;
|
---|
219 | goto leave_release_default_address;
|
---|
220 | }
|
---|
221 |
|
---|
222 | usb_endpoint_pipe_t ctrl_pipe;
|
---|
223 | rc = usb_endpoint_pipe_initialize_default_control(&ctrl_pipe,
|
---|
224 | &dev_conn);
|
---|
225 | if (rc != EOK) {
|
---|
226 | rc = ENOTCONN;
|
---|
227 | goto leave_release_default_address;
|
---|
228 | }
|
---|
229 |
|
---|
230 | rc = usb_endpoint_pipe_start_session(&ctrl_pipe);
|
---|
231 | if (rc != EOK) {
|
---|
232 | rc = ENOTCONN;
|
---|
233 | goto leave_release_default_address;
|
---|
234 | }
|
---|
235 |
|
---|
236 | rc = usb_request_set_address(&ctrl_pipe, dev_addr);
|
---|
237 | if (rc != EOK) {
|
---|
238 | rc = ESTALL;
|
---|
239 | goto leave_stop_session;
|
---|
240 | }
|
---|
241 |
|
---|
242 | usb_endpoint_pipe_end_session(&ctrl_pipe);
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Once the address is changed, we can return the default address.
|
---|
246 | */
|
---|
247 | usb_hc_release_default_address(connection);
|
---|
248 |
|
---|
249 | /*
|
---|
250 | * It is time to register the device with devman.
|
---|
251 | */
|
---|
252 | /* FIXME: create device_register that will get opened ctrl pipe. */
|
---|
253 | devman_handle_t child_handle;
|
---|
254 | rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
|
---|
255 | parent, &child_handle,
|
---|
256 | dev_ops, new_dev_data, new_fun);
|
---|
257 | if (rc != EOK) {
|
---|
258 | rc = ESTALL;
|
---|
259 | goto leave_release_free_address;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * And now inform the host controller about the handle.
|
---|
264 | */
|
---|
265 | usb_hc_attached_device_t new_device = {
|
---|
266 | .address = dev_addr,
|
---|
267 | .handle = child_handle
|
---|
268 | };
|
---|
269 | rc = usb_hc_register_device(connection, &new_device);
|
---|
270 | if (rc != EOK) {
|
---|
271 | rc = EDESTADDRREQ;
|
---|
272 | goto leave_release_free_address;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * And we are done.
|
---|
277 | */
|
---|
278 | if (assigned_address != NULL) {
|
---|
279 | *assigned_address = dev_addr;
|
---|
280 | }
|
---|
281 | if (assigned_handle != NULL) {
|
---|
282 | *assigned_handle = child_handle;
|
---|
283 | }
|
---|
284 |
|
---|
285 | return EOK;
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Error handling (like nested exceptions) starts here.
|
---|
291 | * Completely ignoring errors here.
|
---|
292 | */
|
---|
293 |
|
---|
294 | leave_stop_session:
|
---|
295 | usb_endpoint_pipe_end_session(&ctrl_pipe);
|
---|
296 |
|
---|
297 | leave_release_default_address:
|
---|
298 | usb_hc_release_default_address(connection);
|
---|
299 |
|
---|
300 | leave_release_free_address:
|
---|
301 | usb_hc_unregister_device(connection, dev_addr);
|
---|
302 |
|
---|
303 | return rc;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * @}
|
---|
308 | */
|
---|