source: mainline/uspace/lib/usbdev/src/hub.c@ a861ccb3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a861ccb3 was ffa96c2, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Convert libusbdev away from DDF_DATA_IMPLANT.

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