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

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

Remove duplicate includes

  • Property mode set to 100644
File size: 10.3 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 */
[77ad86c]35
[7d521e24]36#include <usb/dev/hub.h>
37#include <usb/dev/pipes.h>
38#include <usb/dev/request.h>
39#include <usb/dev/recognise.h>
[fb422312]40#include <usb/debug.h>
[e40b9f00]41#include <errno.h>
[eb1a2f4]42#include <assert.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,
[56fd7cf]64 attached_device->address, ddf_fun_get_handle(attached_device->fun));
[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
[6d8c5725]117/** Wrapper for registering attached device to the hub.
118 *
[a6add7a]119 * The @p enable_port function is expected to enable signaling on given
[6d8c5725]120 * port.
[32ec5671]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).
[6d8c5725]123 *
124 * If the @p enable_port fails (i.e. does not return EOK), the device
[a6add7a]125 * addition is canceled.
[6d8c5725]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 *
[61727bf]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 *
[bc1c6fb]134 * @param[in] parent Parent device (i.e. the hub device).
[90d7033]135 * @param[in] connection Connection to host controller. Must be non-null.
[bc1c6fb]136 * @param[in] dev_speed New device speed.
137 * @param[in] enable_port Function for enabling signaling through the port the
[6d8c5725]138 * device is attached to.
[bc1c6fb]139 * @param[in] arg Any data argument to @p enable_port.
[6d8c5725]140 * @param[out] assigned_address USB address of the device.
[10059a68]141 * @param[in] dev_ops Child device ops. Will use default if not provided.
[bc1c6fb]142 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
[59c163c]143 * as @c driver_data. Will allocate and assign usb_hub_attached_device_t
144 * structure if NULL.
[bc1c6fb]145 * @param[out] new_fun Storage where pointer to allocated child function
[90d7033]146 * will be written. Must be non-null.
[6d8c5725]147 * @return Error code.
[90d7033]148 * @retval EINVAL Either connection or new_fun is a NULL pointer.
[6d8c5725]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.
[fab2746]152 * @retval ENXIO Problem connecting to the host controller via USB pipe.
[6d8c5725]153 * @retval ESTALL Problem communication with device (either SET_ADDRESS
154 * request or requests for descriptors when creating match ids).
155 */
[ffa96c2]156int usb_hc_new_device_wrapper(ddf_dev_t *parent, ddf_fun_t *fun,
[7267fa1]157 usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
[32ec5671]158 int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
[ffa96c2]159 ddf_dev_ops_t *dev_ops)
[6d8c5725]160{
[ffa96c2]161 if (hc_conn == NULL)
[90d7033]162 return EINVAL;
[1ab8539]163
[5f94a0c]164 struct timeval start_time;
[1ab8539]165 gettimeofday(&start_time, NULL);
166
[7267fa1]167 /* We are gona do a lot of communication better open it in advance. */
[1ab8539]168 int rc = usb_hc_connection_open(hc_conn);
169 if (rc != EOK)
[61727bf]170 return rc;
[1ab8539]171
[7267fa1]172 /* Request a new address. */
[67f55e7b]173 usb_address_t dev_addr =
[7267fa1]174 usb_hc_request_address(hc_conn, 0, false, dev_speed);
[6d8c5725]175 if (dev_addr < 0) {
[fb422312]176 rc = EADDRNOTAVAIL;
177 goto close_connection;
[6d8c5725]178 }
179
[7267fa1]180 /* Initialize connection to device. */
[6d8c5725]181 usb_device_connection_t dev_conn;
[3538b0e]182 rc = usb_device_connection_initialize(
183 &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
[6d8c5725]184 if (rc != EOK) {
[fab2746]185 rc = ENXIO;
[98064795]186 goto leave_release_free_address;
[6d8c5725]187 }
188
[3538b0e]189 /* Initialize control pipe on default address. Don't register yet. */
[a372663]190 usb_pipe_t ctrl_pipe;
[3238506]191 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
[6d8c5725]192 if (rc != EOK) {
[fab2746]193 rc = ENXIO;
[98064795]194 goto leave_release_free_address;
[6d8c5725]195 }
[9f9b31ad]196
[3538b0e]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 */
[98064795]203 do {
[7267fa1]204 rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
[062165f]205 true, dev_speed);
[3238506]206 if (rc == ENOENT) {
[98064795]207 /* Do not overheat the CPU ;-). */
[6e3c005]208 async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
[98064795]209 }
[062165f]210 } while (rc == ENOENT);
[3238506]211 if (rc < 0) {
[062165f]212 goto leave_release_free_address;
213 }
214
[3538b0e]215 /* Register control pipe on default address. 0 means no interval. */
[bd575647]216 rc = usb_pipe_register(&ctrl_pipe, 0);
[062165f]217 if (rc != EOK) {
[fab2746]218 rc = ENXIO;
[062165f]219 goto leave_release_default_address;
220 }
[1ab8539]221
[5f94a0c]222 struct timeval end_time;
[1ab8539]223 gettimeofday(&end_time, NULL);
224
[5f94a0c]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*/
[7f9d97f3]230 const suseconds_t elapsed = tv_sub_diff(&end_time, &start_time);
[5f94a0c]231 if (elapsed < 100000) {
232 async_usleep(100000 - elapsed);
233 }
[98064795]234
[3238506]235 /* Endpoint is registered. We can enable the port and change address. */
[32ec5671]236 rc = enable_port(arg);
[9f9b31ad]237 if (rc != EOK) {
238 goto leave_release_default_address;
239 }
[5f94a0c]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);
[98064795]246
[3238506]247 /* Get max_packet_size value. */
[3954a63b]248 rc = usb_pipe_probe_default_control(&ctrl_pipe);
[206f71a]249 if (rc != EOK) {
[98064795]250 rc = ESTALL;
[206f71a]251 goto leave_release_default_address;
252 }
[6d8c5725]253
[7267fa1]254 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
[6d8c5725]255 if (rc != EOK) {
256 rc = ESTALL;
[c6394aa]257 goto leave_release_default_address;
[6d8c5725]258 }
259
[9f9b31ad]260
[3238506]261 /* Register the device with devman. */
[6d8c5725]262 /* FIXME: create device_register that will get opened ctrl pipe. */
[2179cf95]263 rc = usb_device_register_child_in_devman(&ctrl_pipe,
[ffa96c2]264 parent, fun, dev_ops);
[6d8c5725]265 if (rc != EOK) {
266 goto leave_release_free_address;
267 }
268
[90d7033]269 const usb_hub_attached_device_t new_device = {
[6d8c5725]270 .address = dev_addr,
[ffa96c2]271 .fun = fun,
[6d8c5725]272 };
[59c163c]273
274
[3238506]275 /* Inform the host controller about the handle. */
[6e3c005]276 rc = usb_hub_register_device(hc_conn, &new_device);
[6d8c5725]277 if (rc != EOK) {
[90d7033]278 /* The child function is already created. */
[fab2746]279 rc = EIO;
[6d8c5725]280 goto leave_release_free_address;
281 }
[fb422312]282
[6d8c5725]283 if (assigned_address != NULL) {
284 *assigned_address = dev_addr;
285 }
[3238506]286
[fb422312]287 rc = EOK;
288 goto close_connection;
[6d8c5725]289
290 /*
291 * Error handling (like nested exceptions) starts here.
292 * Completely ignoring errors here.
293 */
294leave_release_default_address:
[6e3c005]295 if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
296 usb_log_warning("%s: Failed to release defaut address.\n",
[3538b0e]297 __FUNCTION__);
[6d8c5725]298
299leave_release_free_address:
[07a7a97d]300 /* This might be either 0:0 or dev_addr:0 */
[bd575647]301 if (usb_pipe_unregister(&ctrl_pipe) != EOK)
[07a7a97d]302 usb_log_warning("%s: Failed to unregister default pipe.\n",
303 __FUNCTION__);
[61727bf]304
[6e3c005]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);
[3238506]308
[fb422312]309close_connection:
[7267fa1]310 if (usb_hc_connection_close(hc_conn) != EOK)
[3238506]311 usb_log_warning("%s: Failed to close hc connection.\n",
[10059a68]312 __FUNCTION__);
[6d8c5725]313
314 return rc;
315}
316
[e40b9f00]317/**
318 * @}
319 */
Note: See TracBrowser for help on using the repository browser.