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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 54cbda2 was 6e3c005, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbdev: Doxygen and minor cleanup/renaming.

  • Property mode set to 100644
File size: 10.7 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
[160b75e]29/** @addtogroup libusbdev
[e40b9f00]30 * @{
31 */
32/** @file
33 * Functions needed by hub drivers.
34 */
[7d521e24]35#include <usb/dev/hub.h>
36#include <usb/dev/pipes.h>
37#include <usb/dev/request.h>
38#include <usb/dev/recognise.h>
[fb422312]39#include <usb/debug.h>
[e40b9f00]40#include <errno.h>
[eb1a2f4]41#include <assert.h>
[b6c9e1e]42#include <usb/debug.h>
[5f94a0c]43#include <time.h>
[79ae36dd]44#include <async.h>
[e40b9f00]45
[6e3c005]46/** How much time to wait between attempts to get the default address.
[1c258d1]47 * The value is based on typical value for port reset + some overhead.
48 */
[6e3c005]49#define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
[e40b9f00]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 */
[6e3c005]57int usb_hub_register_device(usb_hc_connection_t *connection,
[4501e207]58 const usb_hub_attached_device_t *attached_device)
[e40b9f00]59{
[6e3c005]60 assert(connection);
[02fc5c4]61 if (attached_device == NULL || attached_device->fun == NULL)
[6e3c005]62 return EBADMEM;
63 return usb_hc_bind_address(connection,
[90994fa]64 attached_device->address, attached_device->fun->handle);
[e40b9f00]65}
66
[3238506]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 */
[7267fa1]81static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
[9f9b31ad]82{
[3238506]83 if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
84 return EINVAL;
[9f9b31ad]85 }
[3238506]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);
[9f9b31ad]94
95 if (rc != EOK) {
[3238506]96 return rc;
97 }
98
99 /* TODO: prevent others from accessing the wire now. */
[bd575647]100 if (usb_pipe_unregister(pipe) != EOK) {
[3238506]101 usb_log_warning(
102 "Failed to unregister the old pipe on address change.\n");
[9f9b31ad]103 }
[6e3c005]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);
[7267fa1]107
[3238506]108 /* The address is already changed so set it in the wire */
109 pipe->wire->address = new_address;
[bd575647]110 rc = usb_pipe_register(pipe, 0);
[3238506]111 if (rc != EOK)
112 return EADDRNOTAVAIL;
[9f9b31ad]113
[3238506]114 return EOK;
[9f9b31ad]115}
116
[e40b9f00]117
[6d8c5725]118/** Wrapper for registering attached device to the hub.
119 *
[a6add7a]120 * The @p enable_port function is expected to enable signaling on given
[6d8c5725]121 * port.
[32ec5671]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).
[6d8c5725]124 *
125 * If the @p enable_port fails (i.e. does not return EOK), the device
[a6add7a]126 * addition is canceled.
[6d8c5725]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 *
[61727bf]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 *
[bc1c6fb]135 * @param[in] parent Parent device (i.e. the hub device).
[90d7033]136 * @param[in] connection Connection to host controller. Must be non-null.
[bc1c6fb]137 * @param[in] dev_speed New device speed.
138 * @param[in] enable_port Function for enabling signaling through the port the
[6d8c5725]139 * device is attached to.
[bc1c6fb]140 * @param[in] arg Any data argument to @p enable_port.
[6d8c5725]141 * @param[out] assigned_address USB address of the device.
[10059a68]142 * @param[in] dev_ops Child device ops. Will use default if not provided.
[bc1c6fb]143 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
[59c163c]144 * as @c driver_data. Will allocate and assign usb_hub_attached_device_t
145 * structure if NULL.
[bc1c6fb]146 * @param[out] new_fun Storage where pointer to allocated child function
[90d7033]147 * will be written. Must be non-null.
[6d8c5725]148 * @return Error code.
[90d7033]149 * @retval EINVAL Either connection or new_fun is a NULL pointer.
[6d8c5725]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 */
[9dbfd288]157int usb_hc_new_device_wrapper(ddf_dev_t *parent,
[7267fa1]158 usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
[32ec5671]159 int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
[eb1a2f4]160 ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
[6d8c5725]161{
[7267fa1]162 if (new_fun == NULL || hc_conn == NULL)
[90d7033]163 return EINVAL;
164
[61727bf]165 int rc;
[5f94a0c]166 struct timeval start_time;
167
168 rc = gettimeofday(&start_time, NULL);
169 if (rc != EOK) {
170 return rc;
171 }
[61727bf]172
[7267fa1]173 /* We are gona do a lot of communication better open it in advance. */
174 rc = usb_hc_connection_open(hc_conn);
[61727bf]175 if (rc != EOK) {
176 return rc;
177 }
178
[7267fa1]179 /* Request a new address. */
[67f55e7b]180 usb_address_t dev_addr =
[7267fa1]181 usb_hc_request_address(hc_conn, 0, false, dev_speed);
[6d8c5725]182 if (dev_addr < 0) {
[fb422312]183 rc = EADDRNOTAVAIL;
184 goto close_connection;
[6d8c5725]185 }
186
[7267fa1]187 /* Initialize connection to device. */
[6d8c5725]188 usb_device_connection_t dev_conn;
[3538b0e]189 rc = usb_device_connection_initialize(
190 &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
[6d8c5725]191 if (rc != EOK) {
192 rc = ENOTCONN;
[98064795]193 goto leave_release_free_address;
[6d8c5725]194 }
195
[3538b0e]196 /* Initialize control pipe on default address. Don't register yet. */
[a372663]197 usb_pipe_t ctrl_pipe;
[3238506]198 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
[6d8c5725]199 if (rc != EOK) {
200 rc = ENOTCONN;
[98064795]201 goto leave_release_free_address;
[6d8c5725]202 }
[9f9b31ad]203
[3538b0e]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 */
[98064795]210 do {
[7267fa1]211 rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
[062165f]212 true, dev_speed);
[3238506]213 if (rc == ENOENT) {
[98064795]214 /* Do not overheat the CPU ;-). */
[6e3c005]215 async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
[98064795]216 }
[062165f]217 } while (rc == ENOENT);
[3238506]218 if (rc < 0) {
[062165f]219 goto leave_release_free_address;
220 }
221
[3538b0e]222 /* Register control pipe on default address. 0 means no interval. */
[bd575647]223 rc = usb_pipe_register(&ctrl_pipe, 0);
[062165f]224 if (rc != EOK) {
225 rc = ENOTCONN;
226 goto leave_release_default_address;
227 }
228
[5f94a0c]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*/
[3238506]240 const suseconds_t elapsed = tv_sub(&end_time, &start_time);
[5f94a0c]241 if (elapsed < 100000) {
242 async_usleep(100000 - elapsed);
243 }
[98064795]244
[3238506]245 /* Endpoint is registered. We can enable the port and change address. */
[32ec5671]246 rc = enable_port(arg);
[9f9b31ad]247 if (rc != EOK) {
248 goto leave_release_default_address;
249 }
[5f94a0c]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);
[98064795]256
[3238506]257 /* Get max_packet_size value. */
[3954a63b]258 rc = usb_pipe_probe_default_control(&ctrl_pipe);
[206f71a]259 if (rc != EOK) {
[98064795]260 rc = ESTALL;
[206f71a]261 goto leave_release_default_address;
262 }
[6d8c5725]263
[7267fa1]264 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
[6d8c5725]265 if (rc != EOK) {
266 rc = ESTALL;
[c6394aa]267 goto leave_release_default_address;
[6d8c5725]268 }
269
[9f9b31ad]270
[3238506]271 /* Register the device with devman. */
[6d8c5725]272 /* FIXME: create device_register that will get opened ctrl pipe. */
[90994fa]273 ddf_fun_t *child_fun;
[2179cf95]274 rc = usb_device_register_child_in_devman(&ctrl_pipe,
[162726b]275 parent, dev_ops, new_dev_data, &child_fun);
[6d8c5725]276 if (rc != EOK) {
277 goto leave_release_free_address;
278 }
279
[90d7033]280 const usb_hub_attached_device_t new_device = {
[6d8c5725]281 .address = dev_addr,
[90994fa]282 .fun = child_fun,
[6d8c5725]283 };
[59c163c]284
285
[3238506]286 /* Inform the host controller about the handle. */
[6e3c005]287 rc = usb_hub_register_device(hc_conn, &new_device);
[6d8c5725]288 if (rc != EOK) {
[59c163c]289 /* We know nothing about that data. */
290 if (new_dev_data)
291 child_fun->driver_data = NULL;
[90d7033]292 /* The child function is already created. */
293 ddf_fun_destroy(child_fun);
[6d8c5725]294 rc = EDESTADDRREQ;
295 goto leave_release_free_address;
296 }
[fb422312]297
[6d8c5725]298 if (assigned_address != NULL) {
299 *assigned_address = dev_addr;
300 }
[3238506]301
302 *new_fun = child_fun;
[6d8c5725]303
[fb422312]304 rc = EOK;
305 goto close_connection;
[6d8c5725]306
307 /*
308 * Error handling (like nested exceptions) starts here.
309 * Completely ignoring errors here.
310 */
311leave_release_default_address:
[6e3c005]312 if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
313 usb_log_warning("%s: Failed to release defaut address.\n",
[3538b0e]314 __FUNCTION__);
[6d8c5725]315
316leave_release_free_address:
[07a7a97d]317 /* This might be either 0:0 or dev_addr:0 */
[bd575647]318 if (usb_pipe_unregister(&ctrl_pipe) != EOK)
[07a7a97d]319 usb_log_warning("%s: Failed to unregister default pipe.\n",
320 __FUNCTION__);
[61727bf]321
[6e3c005]322 if (usb_hc_release_address(hc_conn, dev_addr) != EOK)
323 usb_log_warning("%s: Failed to release address: %d.\n",
324 __FUNCTION__, dev_addr);
[3238506]325
[fb422312]326close_connection:
[7267fa1]327 if (usb_hc_connection_close(hc_conn) != EOK)
[3238506]328 usb_log_warning("%s: Failed to close hc connection.\n",
[10059a68]329 __FUNCTION__);
[6d8c5725]330
331 return rc;
332}
333
[e40b9f00]334/**
335 * @}
336 */
Note: See TracBrowser for help on using the repository browser.