source: mainline/uspace/drv/bus/usb/usbhub/port.c@ dbd54f72

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

usbhub: Handle device removal on port.

  • Property mode set to 100644
File size: 14.6 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup drvusbhub
30 * @{
31 */
32/** @file
33 * Hub ports functions.
34 */
35
36#include <bool.h>
37#include <devman.h>
38#include <errno.h>
39#include <str_error.h>
40#include <inttypes.h>
41#include <fibril_synch.h>
42
43#include <usb/debug.h>
44#include <usb/dev/hub.h>
45
46#include "port.h"
47#include "usbhub.h"
48#include "status.h"
49
50/** Information for fibril for device discovery. */
51struct add_device_phase1 {
52 usb_hub_info_t *hub;
53 usb_hub_port_t *port;
54 usb_speed_t speed;
55};
56
57static void usb_hub_port_removed_device(usb_hub_port_t *port,
58 usb_hub_info_t *hub);
59static void usb_hub_port_reset_completed(usb_hub_port_t *port,
60 usb_port_status_t status);
61static int get_port_status(usb_hub_port_t *port, usb_port_status_t *status);
62static int enable_port_callback(int port_no, void *arg);
63static int add_device_phase1_worker_fibril(void *arg);
64static int create_add_device_fibril(usb_hub_port_t *port, usb_hub_info_t *hub,
65 usb_speed_t speed);
66
67/**
68 * Clear feature on hub port.
69 *
70 * @param hc Host controller telephone
71 * @param address Hub address
72 * @param port_index Port
73 * @param feature Feature selector
74 * @return Operation result
75 */
76int usb_hub_port_clear_feature(
77 usb_hub_port_t *port, usb_hub_class_feature_t feature)
78{
79 assert(port);
80 usb_device_request_setup_packet_t clear_request = {
81 .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
82 .request = USB_DEVREQ_CLEAR_FEATURE,
83 .value = feature,
84 .index = port->port_number,
85 .length = 0,
86 };
87 return usb_pipe_control_write(port->control_pipe, &clear_request,
88 sizeof(clear_request), NULL, 0);
89}
90/*----------------------------------------------------------------------------*/
91/**
92 * Clear feature on hub port.
93 *
94 * @param hc Host controller telephone
95 * @param address Hub address
96 * @param port_index Port
97 * @param feature Feature selector
98 * @return Operation result
99 */
100int usb_hub_port_set_feature(
101 usb_hub_port_t *port, usb_hub_class_feature_t feature)
102{
103 assert(port);
104 usb_device_request_setup_packet_t clear_request = {
105 .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
106 .request = USB_DEVREQ_SET_FEATURE,
107 .index = port->port_number,
108 .value = feature,
109 .length = 0,
110 };
111 return usb_pipe_control_write(port->control_pipe, &clear_request,
112 sizeof(clear_request), NULL, 0);
113}
114/*----------------------------------------------------------------------------*/
115void usb_hub_port_reset_fail(usb_hub_port_t *port)
116{
117 assert(port);
118 fibril_mutex_lock(&port->mutex);
119 port->reset_completed = true;
120 port->reset_okay = false;
121 fibril_condvar_broadcast(&port->reset_cv);
122 fibril_mutex_unlock(&port->mutex);
123}
124/*----------------------------------------------------------------------------*/
125/**
126 * Process interrupts on given hub port
127 *
128 * Accepts connection, over current and port reset change.
129 * @param hub hub representation
130 * @param port port number, starting from 1
131 */
132void usb_hub_port_process_interrupt(usb_hub_port_t *port, usb_hub_info_t *hub)
133{
134 assert(port);
135 assert(hub);
136 usb_log_debug("Interrupt at port %zu\n", port->port_number);
137
138 usb_port_status_t status;
139 const int opResult = get_port_status(port, &status);
140 if (opResult != EOK) {
141 usb_log_error("Failed to get port %zu status: %s.\n",
142 port->port_number, str_error(opResult));
143 return;
144 }
145
146 /* Connection change */
147 if (status & USB_HUB_PORT_C_STATUS_CONNECTION) {
148 const bool connected =
149 (status & USB_HUB_PORT_STATUS_CONNECTION) != 0;
150 usb_log_debug("Connection change on port %zu: device %s.\n",
151 port->port_number, connected ? "attached" : "removed");
152
153 /* ACK the change */
154 const int opResult = usb_hub_port_clear_feature(port,
155 USB_HUB_FEATURE_C_PORT_CONNECTION);
156 if (opResult != EOK) {
157 usb_log_warning("Failed to clear port-change-connection"
158 " flag: %s.\n", str_error(opResult));
159 }
160
161 if (connected) {
162 const int opResult = create_add_device_fibril(port, hub,
163 usb_port_speed(status));
164 if (opResult != EOK) {
165 usb_log_error(
166 "Cannot handle change on port %zu: %s.\n",
167 port->port_number, str_error(opResult));
168 }
169 } else {
170 /* If enabled change was reported leave the removal
171 * to that handler, it shall ACK the change too. */
172 if (!(status & USB_HUB_PORT_C_STATUS_ENABLED)) {
173 usb_hub_port_removed_device(port, hub);
174 }
175 }
176 }
177
178 /* Enable change, ports are automatically disabled on errors. */
179 if (status & USB_HUB_PORT_C_STATUS_ENABLED) {
180 usb_log_info("Port %zu, disabled because of errors.\n",
181 port->port_number);
182 usb_hub_port_removed_device(port, hub);
183 const int rc = usb_hub_port_clear_feature(port,
184 USB_HUB_FEATURE_C_PORT_ENABLE);
185 if (rc != EOK) {
186 usb_log_error(
187 "Failed to clear port %zu enable change feature: "
188 "%s.\n", port->port_number, str_error(rc));
189 }
190
191 }
192
193 /* Suspend change */
194 if (status & USB_HUB_PORT_C_STATUS_SUSPEND) {
195 usb_log_error("Port %zu went to suspend state, this should"
196 "NOT happen as we do not support suspend state!",
197 port->port_number);
198 const int rc = usb_hub_port_clear_feature(port,
199 USB_HUB_FEATURE_C_PORT_SUSPEND);
200 if (rc != EOK) {
201 usb_log_error(
202 "Failed to clear port %zu suspend change feature: "
203 "%s.\n", port->port_number, str_error(rc));
204 }
205 }
206
207 /* Over current */
208 if (status & USB_HUB_PORT_C_STATUS_OC) {
209 /* According to the USB specs:
210 * 11.13.5 Over-current Reporting and Recovery
211 * Hub device is responsible for putting port in power off
212 * mode. USB system software is responsible for powering port
213 * back on when the over-current condition is gone */
214 const int rc = usb_hub_port_clear_feature(port,
215 USB_HUB_FEATURE_C_PORT_OVER_CURRENT);
216 if (rc != EOK) {
217 usb_log_error(
218 "Failed to clear port %zu OC change feature: %s.\n",
219 port->port_number, str_error(rc));
220 }
221 if (!(status & ~USB_HUB_PORT_STATUS_OC)) {
222 const int rc = usb_hub_port_set_feature(
223 port, USB_HUB_FEATURE_PORT_POWER);
224 if (rc != EOK) {
225 usb_log_error(
226 "Failed to set port %zu power after OC:"
227 " %s.\n", port->port_number, str_error(rc));
228 }
229 }
230 }
231
232 /* Port reset, set on port reset complete. */
233 if (status & USB_HUB_PORT_C_STATUS_RESET) {
234 usb_hub_port_reset_completed(port, status);
235 }
236
237 usb_log_debug("Port %zu status 0x%08" PRIx32 "\n",
238 port->port_number, status);
239}
240
241/**
242 * routine called when a device on port has been removed
243 *
244 * If the device on port had default address, it releases default address.
245 * Otherwise does not do anything, because DDF does not allow to remove device
246 * from it`s device tree.
247 * @param hub hub representation
248 * @param port port number, starting from 1
249 */
250static void usb_hub_port_removed_device(usb_hub_port_t *port,
251 usb_hub_info_t *hub)
252{
253 assert(port);
254 assert(hub);
255 if (port->attached_device.address >= 0) {
256 fibril_mutex_lock(&port->mutex);
257 usb_log_debug("Removing device on port %zu.\n",
258 port->port_number);
259 const int ret =
260 devman_remove_function(port->attached_device.handle);
261 if (ret == EOK) {
262 const int ret =
263 usb_hc_unregister_device(&hub->connection,
264 port->attached_device.address);
265 if (ret != EOK) {
266 usb_log_error("Failed to unregister "
267 "address of removed device: %s.\n",
268 str_error(ret));
269 }
270 } else {
271 usb_log_error("Failed to remove child function on port"
272 " %zu: %s.\n", port->port_number, str_error(ret));
273 }
274 port->attached_device.address = -1;
275 port->attached_device.handle = 0;
276 fibril_mutex_unlock(&port->mutex);
277 usb_log_info("Removed device on port %zu.\n",
278 port->port_number);
279 } else {
280 usb_log_warning(
281 "Device on port %zu removed before being registered.\n",
282 port->port_number);
283
284 /*
285 * Device was removed before port reset completed.
286 * We will announce a failed port reset to unblock the
287 * port reset callback from new device wrapper.
288 */
289 usb_hub_port_reset_fail(port);
290 }
291}
292
293/**
294 * Process port reset change
295 *
296 * After this change port should be enabled, unless some problem occurred.
297 * This functions triggers second phase of enabling new device.
298 * @param hub
299 * @param port
300 * @param status
301 */
302static void usb_hub_port_reset_completed(usb_hub_port_t *port,
303 usb_port_status_t status)
304{
305 assert(port);
306 fibril_mutex_lock(&port->mutex);
307 /* Finalize device adding. */
308 port->reset_completed = true;
309 port->reset_okay = (status & USB_HUB_PORT_STATUS_ENABLED) != 0;
310
311 if (port->reset_okay) {
312 usb_log_debug("Port %zu reset complete.\n", port->port_number);
313 } else {
314 usb_log_warning(
315 "Port %zu reset complete but port not enabled.\n",
316 port->port_number);
317 }
318 fibril_condvar_broadcast(&port->reset_cv);
319 fibril_mutex_unlock(&port->mutex);
320
321 /* Clear the port reset change. */
322 int rc = usb_hub_port_clear_feature(port, USB_HUB_FEATURE_C_PORT_RESET);
323 if (rc != EOK) {
324 usb_log_error(
325 "Failed to clear port %zu reset change feature: %s.\n",
326 port->port_number, str_error(rc));
327 }
328}
329/*----------------------------------------------------------------------------*/
330/** Retrieve port status.
331 *
332 * @param[in] ctrl_pipe Control pipe to use.
333 * @param[in] port Port number (starting at 1).
334 * @param[out] status Where to store the port status.
335 * @return Error code.
336 */
337static int get_port_status(usb_hub_port_t *port, usb_port_status_t *status)
338{
339 assert(port);
340 /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
341 * Generic GET_STATUS request cannot be used because of the difference
342 * in status data size (2B vs. 4B)*/
343 const usb_device_request_setup_packet_t request = {
344 .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
345 .request = USB_HUB_REQUEST_GET_STATUS,
346 .value = 0,
347 .index = port->port_number,
348 .length = sizeof(usb_port_status_t),
349 };
350 size_t recv_size;
351 usb_port_status_t status_tmp;
352
353 const int rc = usb_pipe_control_read(port->control_pipe,
354 &request, sizeof(usb_device_request_setup_packet_t),
355 &status_tmp, sizeof(status_tmp), &recv_size);
356 if (rc != EOK) {
357 return rc;
358 }
359
360 if (recv_size != sizeof (status_tmp)) {
361 return ELIMIT;
362 }
363
364 if (status != NULL) {
365 *status = status_tmp;
366 }
367
368 return EOK;
369}
370/*----------------------------------------------------------------------------*/
371/** Callback for enabling a specific port.
372 *
373 * We wait on a CV until port is reseted.
374 * That is announced via change on interrupt pipe.
375 *
376 * @param port_no Port number (starting at 1).
377 * @param arg Custom argument, points to @c usb_hub_info_t.
378 * @return Error code.
379 */
380static int enable_port_callback(int port_no, void *arg)
381{
382 usb_hub_port_t *port = arg;
383 const int rc =
384 usb_hub_port_set_feature(port, USB_HUB_FEATURE_PORT_RESET);
385 if (rc != EOK) {
386 usb_log_warning("Port reset failed: %s.\n", str_error(rc));
387 return rc;
388 }
389
390 /*
391 * Wait until reset completes.
392 */
393 fibril_mutex_lock(&port->mutex);
394 while (!port->reset_completed) {
395 fibril_condvar_wait(&port->reset_cv, &port->mutex);
396 }
397 fibril_mutex_unlock(&port->mutex);
398
399 if (port->reset_okay) {
400 return EOK;
401 } else {
402 return ESTALL;
403 }
404}
405
406/** Fibril for adding a new device.
407 *
408 * Separate fibril is needed because the port reset completion is announced
409 * via interrupt pipe and thus we cannot block here.
410 *
411 * @param arg Pointer to struct add_device_phase1.
412 * @return 0 Always.
413 */
414static int add_device_phase1_worker_fibril(void *arg)
415{
416 struct add_device_phase1 *data = arg;
417 assert(data);
418
419 usb_address_t new_address;
420 devman_handle_t child_handle;
421
422 const int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
423 &data->hub->connection, data->speed,
424 enable_port_callback, (int) data->port->port_number,
425 data->port, &new_address, &child_handle,
426 NULL, NULL, NULL);
427
428 if (rc != EOK) {
429 usb_log_error("Failed registering device on port %zu: %s.\n",
430 data->port->port_number, str_error(rc));
431 goto leave;
432 }
433
434 fibril_mutex_lock(&data->port->mutex);
435 data->port->attached_device.handle = child_handle;
436 data->port->attached_device.address = new_address;
437 fibril_mutex_unlock(&data->port->mutex);
438
439 usb_log_info("Detected new device on `%s' (port %zu), "
440 "address %d (handle %" PRIun ").\n",
441 data->hub->usb_device->ddf_dev->name, data->port->port_number,
442 new_address, child_handle);
443
444leave:
445 fibril_mutex_lock(&data->hub->pending_ops_mutex);
446 assert(data->hub->pending_ops_count > 0);
447 --data->hub->pending_ops_count;
448 fibril_condvar_signal(&data->hub->pending_ops_cv);
449 fibril_mutex_unlock(&data->hub->pending_ops_mutex);
450
451 free(arg);
452
453 return EOK;
454}
455
456/** Start device adding when connection change is detected.
457 *
458 * This fires a new fibril to complete the device addition.
459 *
460 * @param hub Hub where the change occured.
461 * @param port Port index (starting at 1).
462 * @param speed Speed of the device.
463 * @return Error code.
464 */
465static int create_add_device_fibril(usb_hub_port_t *port, usb_hub_info_t *hub,
466 usb_speed_t speed)
467{
468 assert(hub);
469 assert(port);
470 struct add_device_phase1 *data
471 = malloc(sizeof (struct add_device_phase1));
472 if (data == NULL) {
473 return ENOMEM;
474 }
475 data->hub = hub;
476 data->port = port;
477 data->speed = speed;
478
479 fibril_mutex_lock(&port->mutex);
480 port->reset_completed = false;
481 fibril_mutex_unlock(&port->mutex);
482
483 fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
484 if (fibril == 0) {
485 free(data);
486 return ENOMEM;
487 }
488 fibril_mutex_lock(&hub->pending_ops_mutex);
489 ++hub->pending_ops_count;
490 fibril_mutex_unlock(&hub->pending_ops_mutex);
491 fibril_add_ready(fibril);
492
493 return EOK;
494}
495
496/**
497 * @}
498 */
Note: See TracBrowser for help on using the repository browser.