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 libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Functions needed by hub drivers.
|
---|
34 | */
|
---|
35 | #include <usb/dev/hub.h>
|
---|
36 | #include <usb/dev/pipes.h>
|
---|
37 | #include <usb/dev/request.h>
|
---|
38 | #include <usb/dev/recognise.h>
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usbhc_iface.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <assert.h>
|
---|
43 | #include <usb/debug.h>
|
---|
44 | #include <time.h>
|
---|
45 | #include <async.h>
|
---|
46 |
|
---|
47 | /** How much time to wait between attempts to register endpoint 0:0.
|
---|
48 | * The value is based on typical value for port reset + some overhead.
|
---|
49 | */
|
---|
50 | #define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
|
---|
51 |
|
---|
52 | /** Check that HC connection is alright.
|
---|
53 | *
|
---|
54 | * @param conn Connection to be checked.
|
---|
55 | */
|
---|
56 | #define CHECK_CONNECTION(conn) \
|
---|
57 | do { \
|
---|
58 | assert((conn)); \
|
---|
59 | if (!usb_hc_connection_is_opened((conn))) { \
|
---|
60 | usb_log_error("Connection not open.\n"); \
|
---|
61 | return ENOTCONN; \
|
---|
62 | } \
|
---|
63 | } while (false)
|
---|
64 |
|
---|
65 |
|
---|
66 | /** Inform host controller about new device.
|
---|
67 | *
|
---|
68 | * @param connection Opened connection to host controller.
|
---|
69 | * @param attached_device Information about the new device.
|
---|
70 | * @return Error code.
|
---|
71 | */
|
---|
72 | int usb_hc_register_device(usb_hc_connection_t *connection,
|
---|
73 | const usb_hub_attached_device_t *attached_device)
|
---|
74 | {
|
---|
75 | // CHECK_CONNECTION(connection);
|
---|
76 | if (attached_device == NULL || attached_device->fun == NULL)
|
---|
77 | return EINVAL;
|
---|
78 |
|
---|
79 | async_exch_t *exch = async_exchange_begin(connection->hc_sess);
|
---|
80 | if (!exch)
|
---|
81 | return ENOMEM;
|
---|
82 | const int ret = usbhc_bind_address(exch,
|
---|
83 | attached_device->address, attached_device->fun->handle);
|
---|
84 | async_exchange_end(exch);
|
---|
85 |
|
---|
86 | return ret;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Inform host controller about device removal.
|
---|
90 | *
|
---|
91 | * @param connection Opened connection to host controller.
|
---|
92 | * @param address Address of the device that is being removed.
|
---|
93 | * @return Error code.
|
---|
94 | */
|
---|
95 | int usb_hc_unregister_device(usb_hc_connection_t *connection,
|
---|
96 | usb_address_t address)
|
---|
97 | {
|
---|
98 | // CHECK_CONNECTION(connection);
|
---|
99 |
|
---|
100 | async_exch_t *exch = async_exchange_begin(connection->hc_sess);
|
---|
101 | if (!exch)
|
---|
102 | return ENOMEM;
|
---|
103 | const int ret = usbhc_release_address(exch, address);
|
---|
104 | async_exchange_end(exch);
|
---|
105 |
|
---|
106 | return ret;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Change address of connected device.
|
---|
110 | * This function automatically updates the backing connection to point to
|
---|
111 | * the new address. It also unregisterrs the old endpoint and registers
|
---|
112 | * a new one.
|
---|
113 | * This creates whole bunch of problems:
|
---|
114 | * 1. All pipes using this wire are broken because they are not
|
---|
115 | * registered for new address
|
---|
116 | * 2. All other pipes for this device are using wrong address,
|
---|
117 | * possibly targeting completely different device
|
---|
118 | *
|
---|
119 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
120 | * @param new_address New USB address to be set (in native endianness).
|
---|
121 | * @return Error code.
|
---|
122 | */
|
---|
123 | static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
|
---|
124 | {
|
---|
125 | if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
|
---|
126 | return EINVAL;
|
---|
127 | }
|
---|
128 | assert(pipe);
|
---|
129 | assert(pipe->wire != NULL);
|
---|
130 |
|
---|
131 | const uint16_t addr = uint16_host2usb((uint16_t)new_address);
|
---|
132 |
|
---|
133 | int rc = usb_control_request_set(pipe,
|
---|
134 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
135 | USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0);
|
---|
136 |
|
---|
137 | if (rc != EOK) {
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* TODO: prevent others from accessing the wire now. */
|
---|
142 | if (usb_pipe_unregister(pipe) != EOK) {
|
---|
143 | usb_log_warning(
|
---|
144 | "Failed to unregister the old pipe on address change.\n");
|
---|
145 | }
|
---|
146 | /* Address changed. We can release the default, thus
|
---|
147 | * allowing other to access the default address. */
|
---|
148 | usb_hc_unregister_device(pipe->wire->hc_connection,
|
---|
149 | pipe->wire->address);
|
---|
150 |
|
---|
151 | /* The address is already changed so set it in the wire */
|
---|
152 | pipe->wire->address = new_address;
|
---|
153 | rc = usb_pipe_register(pipe, 0);
|
---|
154 | if (rc != EOK)
|
---|
155 | return EADDRNOTAVAIL;
|
---|
156 |
|
---|
157 | return EOK;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /** Wrapper for registering attached device to the hub.
|
---|
162 | *
|
---|
163 | * The @p enable_port function is expected to enable signaling on given
|
---|
164 | * port.
|
---|
165 | * The argument can have arbitrary meaning and it is not touched at all
|
---|
166 | * by this function (it is passed as is to the @p enable_port function).
|
---|
167 | *
|
---|
168 | * If the @p enable_port fails (i.e. does not return EOK), the device
|
---|
169 | * addition is canceled.
|
---|
170 | * The return value is then returned (it is good idea to use different
|
---|
171 | * error codes than those listed as return codes by this function itself).
|
---|
172 | *
|
---|
173 | * The @p connection representing connection with host controller does not
|
---|
174 | * need to be started.
|
---|
175 | * This function duplicates the connection to allow simultaneous calls of
|
---|
176 | * this function (i.e. from different fibrils).
|
---|
177 | *
|
---|
178 | * @param[in] parent Parent device (i.e. the hub device).
|
---|
179 | * @param[in] connection Connection to host controller. Must be non-null.
|
---|
180 | * @param[in] dev_speed New device speed.
|
---|
181 | * @param[in] enable_port Function for enabling signaling through the port the
|
---|
182 | * device is attached to.
|
---|
183 | * @param[in] arg Any data argument to @p enable_port.
|
---|
184 | * @param[out] assigned_address USB address of the device.
|
---|
185 | * @param[in] dev_ops Child device ops. Will use default if not provided.
|
---|
186 | * @param[in] new_dev_data Arbitrary pointer to be stored in the child
|
---|
187 | * as @c driver_data. Will allocate and assign usb_hub_attached_device_t
|
---|
188 | * structure if NULL.
|
---|
189 | * @param[out] new_fun Storage where pointer to allocated child function
|
---|
190 | * will be written. Must be non-null.
|
---|
191 | * @return Error code.
|
---|
192 | * @retval EINVAL Either connection or new_fun is a NULL pointer.
|
---|
193 | * @retval ENOENT Connection to HC not opened.
|
---|
194 | * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
|
---|
195 | * @retval EBUSY Failed reserving default USB address.
|
---|
196 | * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
|
---|
197 | * @retval ESTALL Problem communication with device (either SET_ADDRESS
|
---|
198 | * request or requests for descriptors when creating match ids).
|
---|
199 | */
|
---|
200 | int usb_hc_new_device_wrapper(ddf_dev_t *parent,
|
---|
201 | usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
|
---|
202 | int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
|
---|
203 | ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
|
---|
204 | {
|
---|
205 | if (new_fun == NULL || hc_conn == NULL)
|
---|
206 | return EINVAL;
|
---|
207 |
|
---|
208 | int rc;
|
---|
209 | struct timeval start_time;
|
---|
210 |
|
---|
211 | rc = gettimeofday(&start_time, NULL);
|
---|
212 | if (rc != EOK) {
|
---|
213 | return rc;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* We are gona do a lot of communication better open it in advance. */
|
---|
217 | rc = usb_hc_connection_open(hc_conn);
|
---|
218 | if (rc != EOK) {
|
---|
219 | return rc;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* Request a new address. */
|
---|
223 | usb_address_t dev_addr =
|
---|
224 | usb_hc_request_address(hc_conn, 0, false, dev_speed);
|
---|
225 | if (dev_addr < 0) {
|
---|
226 | rc = EADDRNOTAVAIL;
|
---|
227 | goto close_connection;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Initialize connection to device. */
|
---|
231 | usb_device_connection_t dev_conn;
|
---|
232 | rc = usb_device_connection_initialize(
|
---|
233 | &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
|
---|
234 | if (rc != EOK) {
|
---|
235 | rc = ENOTCONN;
|
---|
236 | goto leave_release_free_address;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* Initialize control pipe on default address. Don't register yet. */
|
---|
240 | usb_pipe_t ctrl_pipe;
|
---|
241 | rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
|
---|
242 | if (rc != EOK) {
|
---|
243 | rc = ENOTCONN;
|
---|
244 | goto leave_release_free_address;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * The default address request might fail.
|
---|
249 | * That means that someone else is already using that address.
|
---|
250 | * We will simply wait and try again.
|
---|
251 | * (Someone else already wants to add a new device.)
|
---|
252 | */
|
---|
253 | do {
|
---|
254 | rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
|
---|
255 | true, dev_speed);
|
---|
256 | if (rc == ENOENT) {
|
---|
257 | /* Do not overheat the CPU ;-). */
|
---|
258 | async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
|
---|
259 | }
|
---|
260 | } while (rc == ENOENT);
|
---|
261 | if (rc < 0) {
|
---|
262 | goto leave_release_free_address;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /* Register control pipe on default address. 0 means no interval. */
|
---|
266 | rc = usb_pipe_register(&ctrl_pipe, 0);
|
---|
267 | if (rc != EOK) {
|
---|
268 | rc = ENOTCONN;
|
---|
269 | goto leave_release_default_address;
|
---|
270 | }
|
---|
271 |
|
---|
272 | struct timeval end_time;
|
---|
273 | rc = gettimeofday(&end_time, NULL);
|
---|
274 | if (rc != EOK) {
|
---|
275 | goto leave_release_default_address;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* According to the USB spec part 9.1.2 host allows 100ms time for
|
---|
279 | * the insertion process to complete. According to 7.1.7.1 this is the
|
---|
280 | * time between attach detected and port reset. However, the setup done
|
---|
281 | * above might use much of this time so we should only wait to fill
|
---|
282 | * up the 100ms quota*/
|
---|
283 | const suseconds_t elapsed = tv_sub(&end_time, &start_time);
|
---|
284 | if (elapsed < 100000) {
|
---|
285 | async_usleep(100000 - elapsed);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /* Endpoint is registered. We can enable the port and change address. */
|
---|
289 | rc = enable_port(arg);
|
---|
290 | if (rc != EOK) {
|
---|
291 | goto leave_release_default_address;
|
---|
292 | }
|
---|
293 | /* USB spec 7.1.7.1: The USB System Software guarantees a minimum of
|
---|
294 | * 10ms for reset recovery. Device response to any bus transactions
|
---|
295 | * addressed to the default device address during the reset recovery
|
---|
296 | * time is undefined.
|
---|
297 | */
|
---|
298 | async_usleep(10000);
|
---|
299 |
|
---|
300 | /* Get max_packet_size value. */
|
---|
301 | rc = usb_pipe_probe_default_control(&ctrl_pipe);
|
---|
302 | if (rc != EOK) {
|
---|
303 | rc = ESTALL;
|
---|
304 | goto leave_release_default_address;
|
---|
305 | }
|
---|
306 |
|
---|
307 | rc = usb_request_set_address(&ctrl_pipe, dev_addr);
|
---|
308 | if (rc != EOK) {
|
---|
309 | rc = ESTALL;
|
---|
310 | goto leave_release_default_address;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /* Register the device with devman. */
|
---|
315 | /* FIXME: create device_register that will get opened ctrl pipe. */
|
---|
316 | ddf_fun_t *child_fun;
|
---|
317 | rc = usb_device_register_child_in_devman(&ctrl_pipe,
|
---|
318 | parent, dev_ops, new_dev_data, &child_fun);
|
---|
319 | if (rc != EOK) {
|
---|
320 | goto leave_release_free_address;
|
---|
321 | }
|
---|
322 |
|
---|
323 | const usb_hub_attached_device_t new_device = {
|
---|
324 | .address = dev_addr,
|
---|
325 | .fun = child_fun,
|
---|
326 | };
|
---|
327 |
|
---|
328 |
|
---|
329 | /* Inform the host controller about the handle. */
|
---|
330 | rc = usb_hc_register_device(hc_conn, &new_device);
|
---|
331 | if (rc != EOK) {
|
---|
332 | /* We know nothing about that data. */
|
---|
333 | if (new_dev_data)
|
---|
334 | child_fun->driver_data = NULL;
|
---|
335 | /* The child function is already created. */
|
---|
336 | ddf_fun_destroy(child_fun);
|
---|
337 | rc = EDESTADDRREQ;
|
---|
338 | goto leave_release_free_address;
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (assigned_address != NULL) {
|
---|
342 | *assigned_address = dev_addr;
|
---|
343 | }
|
---|
344 |
|
---|
345 | *new_fun = child_fun;
|
---|
346 |
|
---|
347 | rc = EOK;
|
---|
348 | goto close_connection;
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * Error handling (like nested exceptions) starts here.
|
---|
352 | * Completely ignoring errors here.
|
---|
353 | */
|
---|
354 | leave_release_default_address:
|
---|
355 | if (usb_hc_unregister_device(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
|
---|
356 | usb_log_warning("%s: Failed to unregister defaut device.\n",
|
---|
357 | __FUNCTION__);
|
---|
358 |
|
---|
359 | leave_release_free_address:
|
---|
360 | /* This might be either 0:0 or dev_addr:0 */
|
---|
361 | if (usb_pipe_unregister(&ctrl_pipe) != EOK)
|
---|
362 | usb_log_warning("%s: Failed to unregister default pipe.\n",
|
---|
363 | __FUNCTION__);
|
---|
364 |
|
---|
365 | if (usb_hc_unregister_device(hc_conn, dev_addr) != EOK)
|
---|
366 | usb_log_warning("%s: Failed to unregister device.\n",
|
---|
367 | __FUNCTION__);
|
---|
368 |
|
---|
369 | close_connection:
|
---|
370 | if (usb_hc_connection_close(hc_conn) != EOK)
|
---|
371 | usb_log_warning("%s: Failed to close hc connection.\n",
|
---|
372 | __FUNCTION__);
|
---|
373 |
|
---|
374 | return rc;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * @}
|
---|
379 | */
|
---|