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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 83e69b9 was 1558d85, checked in by Jakub Jermar <jakub@…>, 9 years ago

Remove duplicate includes

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