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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 567a3e2 was 0edf7c7, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

HC communication moved into libusb

Although communication with HC looked like a thing common only
to devices, any utility wanting to communicate with the device may
try to get some information about it from the HC.

Thus the usb_hc_connection* and some other generic functions were
moved into libusb.

Also merged dev/hc.h and host.h into hc.h.

  • Property mode set to 100644
File size: 10.5 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>
[e40b9f00]39#include <usbhc_iface.h>
40#include <errno.h>
[eb1a2f4]41#include <assert.h>
[b6c9e1e]42#include <usb/debug.h>
[5f94a0c]43#include <time.h>
[e40b9f00]44
[1c258d1]45/** How much time to wait between attempts to register endpoint 0:0.
46 * The value is based on typical value for port reset + some overhead.
47 */
48#define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
49
[e40b9f00]50/** Check that HC connection is alright.
51 *
52 * @param conn Connection to be checked.
53 */
54#define CHECK_CONNECTION(conn) \
55 do { \
56 assert((conn)); \
57 if (!usb_hc_connection_is_opened((conn))) { \
58 return ENOENT; \
59 } \
60 } while (false)
61
62/** Ask host controller for free address assignment.
63 *
64 * @param connection Opened connection to host controller.
[bc1c6fb]65 * @param speed Speed of the new device (device that will be assigned
66 * the returned address).
[e40b9f00]67 * @return Assigned USB address or negative error code.
68 */
[6427cf67]69usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
[fa48ebe]70 usb_speed_t speed)
[e40b9f00]71{
72 CHECK_CONNECTION(connection);
73
74 sysarg_t address;
[6427cf67]75 int rc = async_req_2_1(connection->hc_phone,
[e40b9f00]76 DEV_IFACE_ID(USBHC_DEV_IFACE),
[fa48ebe]77 IPC_M_USBHC_REQUEST_ADDRESS, speed,
[6427cf67]78 &address);
[e40b9f00]79 if (rc != EOK) {
80 return (usb_address_t) rc;
81 } else {
82 return (usb_address_t) address;
83 }
84}
85
86/** Inform host controller about new device.
87 *
88 * @param connection Opened connection to host controller.
89 * @param attached_device Information about the new device.
90 * @return Error code.
91 */
92int usb_hc_register_device(usb_hc_connection_t * connection,
93 const usb_hc_attached_device_t *attached_device)
94{
95 CHECK_CONNECTION(connection);
96 if (attached_device == NULL) {
97 return EBADMEM;
98 }
99
100 return async_req_3_0(connection->hc_phone,
101 DEV_IFACE_ID(USBHC_DEV_IFACE),
102 IPC_M_USBHC_BIND_ADDRESS,
103 attached_device->address, attached_device->handle);
104}
105
106/** Inform host controller about device removal.
107 *
108 * @param connection Opened connection to host controller.
109 * @param address Address of the device that is being removed.
110 * @return Error code.
111 */
112int usb_hc_unregister_device(usb_hc_connection_t *connection,
113 usb_address_t address)
114{
115 CHECK_CONNECTION(connection);
116
117 return async_req_2_0(connection->hc_phone,
118 DEV_IFACE_ID(USBHC_DEV_IFACE),
119 IPC_M_USBHC_RELEASE_ADDRESS, address);
120}
121
[eb2f7dd]122
[9f9b31ad]123static void unregister_control_endpoint_on_default_address(
124 usb_hc_connection_t *connection)
125{
126 usb_device_connection_t dev_conn;
127 int rc = usb_device_connection_initialize_on_default_address(&dev_conn,
128 connection);
129 if (rc != EOK) {
130 return;
131 }
132
133 usb_pipe_t ctrl_pipe;
134 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
135 if (rc != EOK) {
136 return;
137 }
138
139 usb_pipe_unregister(&ctrl_pipe, connection);
140}
141
[e40b9f00]142
[6d8c5725]143/** Wrapper for registering attached device to the hub.
144 *
[a6add7a]145 * The @p enable_port function is expected to enable signaling on given
[6d8c5725]146 * port.
147 * The two arguments to it can have arbitrary meaning
148 * (the @p port_no is only a suggestion)
149 * and are not touched at all by this function
150 * (they are passed as is to the @p enable_port function).
151 *
152 * If the @p enable_port fails (i.e. does not return EOK), the device
[a6add7a]153 * addition is canceled.
[6d8c5725]154 * The return value is then returned (it is good idea to use different
155 * error codes than those listed as return codes by this function itself).
156 *
[61727bf]157 * The @p connection representing connection with host controller does not
158 * need to be started.
159 * This function duplicates the connection to allow simultaneous calls of
160 * this function (i.e. from different fibrils).
161 *
[bc1c6fb]162 * @param[in] parent Parent device (i.e. the hub device).
[61727bf]163 * @param[in] connection Connection to host controller.
[bc1c6fb]164 * @param[in] dev_speed New device speed.
165 * @param[in] enable_port Function for enabling signaling through the port the
[6d8c5725]166 * device is attached to.
[bc1c6fb]167 * @param[in] port_no Port number (passed through to @p enable_port).
168 * @param[in] arg Any data argument to @p enable_port.
[6d8c5725]169 * @param[out] assigned_address USB address of the device.
170 * @param[out] assigned_handle Devman handle of the new device.
[bc1c6fb]171 * @param[in] dev_ops Child device ops.
172 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
173 * as @c driver_data.
174 * @param[out] new_fun Storage where pointer to allocated child function
175 * will be written.
[6d8c5725]176 * @return Error code.
177 * @retval ENOENT Connection to HC not opened.
178 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
179 * @retval EBUSY Failed reserving default USB address.
180 * @retval ENOTCONN Problem connecting to the host controller via USB pipe.
181 * @retval ESTALL Problem communication with device (either SET_ADDRESS
182 * request or requests for descriptors when creating match ids).
183 */
[eb1a2f4]184int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
[6d8c5725]185 usb_speed_t dev_speed,
186 int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
[eb1a2f4]187 usb_address_t *assigned_address, devman_handle_t *assigned_handle,
188 ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
[6d8c5725]189{
[61727bf]190 assert(connection != NULL);
191 // FIXME: this is awful, we are accessing directly the structure.
192 usb_hc_connection_t hc_conn = {
193 .hc_handle = connection->hc_handle,
194 .hc_phone = -1
195 };
196
197 int rc;
[5f94a0c]198 struct timeval start_time;
199
200 rc = gettimeofday(&start_time, NULL);
201 if (rc != EOK) {
202 return rc;
203 }
[61727bf]204
205 rc = usb_hc_connection_open(&hc_conn);
206 if (rc != EOK) {
207 return rc;
208 }
209
[6d8c5725]210
211 /*
212 * Request new address.
213 */
[61727bf]214 usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
[6d8c5725]215 if (dev_addr < 0) {
[61727bf]216 usb_hc_connection_close(&hc_conn);
[6d8c5725]217 return EADDRNOTAVAIL;
218 }
219
220 /*
[98064795]221 * We will not register control pipe on default address.
222 * The registration might fail. That means that someone else already
223 * registered that endpoint. We will simply wait and try again.
224 * (Someone else already wants to add a new device.)
[6d8c5725]225 */
226 usb_device_connection_t dev_conn;
227 rc = usb_device_connection_initialize_on_default_address(&dev_conn,
[61727bf]228 &hc_conn);
[6d8c5725]229 if (rc != EOK) {
230 rc = ENOTCONN;
[98064795]231 goto leave_release_free_address;
[6d8c5725]232 }
233
[a372663]234 usb_pipe_t ctrl_pipe;
[3954a63b]235 rc = usb_pipe_initialize_default_control(&ctrl_pipe,
[6d8c5725]236 &dev_conn);
237 if (rc != EOK) {
238 rc = ENOTCONN;
[98064795]239 goto leave_release_free_address;
[6d8c5725]240 }
[9f9b31ad]241
[98064795]242 do {
243 rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
244 &hc_conn);
245 if (rc != EOK) {
246 /* Do not overheat the CPU ;-). */
[1c258d1]247 async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
[98064795]248 }
249 } while (rc != EOK);
[5f94a0c]250 struct timeval end_time;
251
252 rc = gettimeofday(&end_time, NULL);
253 if (rc != EOK) {
254 goto leave_release_default_address;
255 }
256
257 /* According to the USB spec part 9.1.2 host allows 100ms time for
258 * the insertion process to complete. According to 7.1.7.1 this is the
259 * time between attach detected and port reset. However, the setup done
260 * above might use much of this time so we should only wait to fill
261 * up the 100ms quota*/
262 suseconds_t elapsed = tv_sub(&end_time, &start_time);
263 if (elapsed < 100000) {
264 async_usleep(100000 - elapsed);
265 }
[98064795]266
267 /*
268 * Endpoint is registered. We can enable the port and change
269 * device address.
[9f9b31ad]270 */
[98064795]271 rc = enable_port(port_no, arg);
[9f9b31ad]272 if (rc != EOK) {
273 goto leave_release_default_address;
274 }
[5f94a0c]275 /* USB spec 7.1.7.1: The USB System Software guarantees a minimum of
276 * 10ms for reset recovery. Device response to any bus transactions
277 * addressed to the default device address during the reset recovery
278 * time is undefined.
279 */
280 async_usleep(10000);
[98064795]281
[3954a63b]282 rc = usb_pipe_probe_default_control(&ctrl_pipe);
[206f71a]283 if (rc != EOK) {
[98064795]284 rc = ESTALL;
[206f71a]285 goto leave_release_default_address;
286 }
[6d8c5725]287
288 rc = usb_request_set_address(&ctrl_pipe, dev_addr);
289 if (rc != EOK) {
290 rc = ESTALL;
[c6394aa]291 goto leave_release_default_address;
[6d8c5725]292 }
293
[9f9b31ad]294 /*
[98064795]295 * Address changed. We can release the original endpoint, thus
296 * allowing other to access the default address.
[9f9b31ad]297 */
[61727bf]298 unregister_control_endpoint_on_default_address(&hc_conn);
[9f9b31ad]299
[6d8c5725]300 /*
[98064795]301 * Time to register the new endpoint.
[6d8c5725]302 */
[98064795]303 rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
304 if (rc != EOK) {
305 goto leave_release_free_address;
306 }
[9f9b31ad]307
[6d8c5725]308 /*
309 * It is time to register the device with devman.
310 */
311 /* FIXME: create device_register that will get opened ctrl pipe. */
312 devman_handle_t child_handle;
313 rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
[eb1a2f4]314 parent, &child_handle,
315 dev_ops, new_dev_data, new_fun);
[6d8c5725]316 if (rc != EOK) {
317 rc = ESTALL;
318 goto leave_release_free_address;
319 }
320
321 /*
322 * And now inform the host controller about the handle.
323 */
324 usb_hc_attached_device_t new_device = {
325 .address = dev_addr,
326 .handle = child_handle
327 };
[61727bf]328 rc = usb_hc_register_device(&hc_conn, &new_device);
[6d8c5725]329 if (rc != EOK) {
330 rc = EDESTADDRREQ;
331 goto leave_release_free_address;
332 }
333
334 /*
335 * And we are done.
336 */
337 if (assigned_address != NULL) {
338 *assigned_address = dev_addr;
339 }
340 if (assigned_handle != NULL) {
341 *assigned_handle = child_handle;
342 }
343
344 return EOK;
345
346
347
348 /*
349 * Error handling (like nested exceptions) starts here.
350 * Completely ignoring errors here.
351 */
352leave_release_default_address:
[98064795]353 usb_pipe_unregister(&ctrl_pipe, &hc_conn);
[6d8c5725]354
355leave_release_free_address:
[61727bf]356 usb_hc_unregister_device(&hc_conn, dev_addr);
357
358 usb_hc_connection_close(&hc_conn);
[6d8c5725]359
360 return rc;
361}
362
[e40b9f00]363/**
364 * @}
365 */
Note: See TracBrowser for help on using the repository browser.