source: mainline/uspace/drv/usbhub/ports.c@ 3476be8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3476be8 was 192ba25, checked in by Matus Dekanek <smekideki@…>, 14 years ago

usbhub: warning if there are still some unhandled changes on port

  • Property mode set to 100644
File size: 12.9 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 drvusbhub
30 * @{
31 */
32/** @file
33 * Hub ports functions.
34 */
35
36#include <bool.h>
37#include <errno.h>
38#include <str_error.h>
39#include <inttypes.h>
40#include <fibril_synch.h>
41
42#include <usb/debug.h>
43
44#include "ports.h"
45#include "usbhub.h"
46#include "usbhub_private.h"
47#include "port_status.h"
48
49
50/** Information for fibril for device discovery. */
51struct add_device_phase1 {
52 usb_hub_info_t *hub;
53 size_t port;
54 usb_speed_t speed;
55};
56
57/**
58 * count of port status changes that are not explicitly handled by
59 * any function here and must be cleared by hand
60 */
61static const unsigned int non_handled_changes_count = 2;
62
63/**
64 * port status changes that are not explicitly handled by
65 * any function here and must be cleared by hand
66 */
67static const int non_handled_changes[] = {
68 USB_HUB_FEATURE_C_PORT_ENABLE,
69 USB_HUB_FEATURE_C_PORT_SUSPEND
70};
71
72static void usb_hub_removed_device(
73 usb_hub_info_t * hub, uint16_t port);
74
75static void usb_hub_port_reset_completed(usb_hub_info_t * hub,
76 uint16_t port, uint32_t status);
77
78static void usb_hub_port_over_current(usb_hub_info_t * hub,
79 uint16_t port, uint32_t status);
80
81static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
82 usb_port_status_t *status);
83
84static int enable_port_callback(int port_no, void *arg);
85
86static int add_device_phase1_worker_fibril(void *arg);
87
88static int create_add_device_fibril(usb_hub_info_t *hub, size_t port,
89 usb_speed_t speed);
90
91/**
92 * Process interrupts on given hub port
93 *
94 * Accepts connection, over current and port reset change.
95 * @param hub hub representation
96 * @param port port number, starting from 1
97 */
98void usb_hub_process_interrupt(usb_hub_info_t * hub,
99 uint16_t port) {
100 usb_log_debug("interrupt at port %zu\n", (size_t) port);
101 //determine type of change
102 //usb_pipe_t *pipe = hub->control_pipe;
103
104 int opResult;
105
106 usb_port_status_t status;
107 opResult = get_port_status(&hub->usb_device->ctrl_pipe, port, &status);
108 if (opResult != EOK) {
109 usb_log_error("Failed to get port %zu status: %s.\n",
110 (size_t) port, str_error(opResult));
111 return;
112 }
113 //connection change
114 if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_CONNECTION)) {
115 bool device_connected = usb_port_is_status(status,
116 USB_HUB_FEATURE_PORT_CONNECTION);
117 usb_log_debug("Connection change on port %zu: %s.\n",
118 (size_t) port,
119 device_connected ? "device attached" : "device removed");
120
121 if (device_connected) {
122 opResult = create_add_device_fibril(hub, port,
123 usb_port_speed(status));
124 if (opResult != EOK) {
125 usb_log_error(
126 "Cannot handle change on port %zu: %s.\n",
127 (size_t) port, str_error(opResult));
128 }
129 } else {
130 usb_hub_removed_device(hub, port);
131 }
132 }
133 //over current
134 if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_OVER_CURRENT)) {
135 //check if it was not auto-resolved
136 usb_log_debug("overcurrent change on port\n");
137 usb_hub_port_over_current(hub, port, status);
138 }
139 //port reset
140 if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_RESET)) {
141 usb_hub_port_reset_completed(hub, port, status);
142 }
143 usb_log_debug("status x%x : %d\n ", status, status);
144
145 usb_port_status_set_bit(
146 &status, USB_HUB_FEATURE_C_PORT_CONNECTION,false);
147 usb_port_status_set_bit(
148 &status, USB_HUB_FEATURE_C_PORT_RESET,false);
149 usb_port_status_set_bit(
150 &status, USB_HUB_FEATURE_C_PORT_OVER_CURRENT,false);
151
152 //clearing not yet handled changes
153 unsigned int feature_idx;
154 for(feature_idx = 0;feature_idx<non_handled_changes_count;
155 ++feature_idx){
156 unsigned int bit_idx = non_handled_changes[feature_idx];
157 if(status & (1<<bit_idx)){
158 usb_log_info(
159 "there was not yet handled change on port %d: %d"
160 ";clearing it\n",
161 port, bit_idx);
162 int opResult = usb_hub_clear_port_feature(
163 hub->control_pipe,
164 port, bit_idx);
165 if (opResult != EOK) {
166 usb_log_warning(
167 "could not clear port flag %d: %d\n",
168 bit_idx, opResult
169 );
170 }
171 usb_port_status_set_bit(
172 &status, bit_idx,false);
173 }
174 }
175 if(status>>16){
176 usb_log_info("there is still some unhandled change %X\n",
177 status);
178 }
179}
180
181
182/**
183 * routine called when a device on port has been removed
184 *
185 * If the device on port had default address, it releases default address.
186 * Otherwise does not do anything, because DDF does not allow to remove device
187 * from it`s device tree.
188 * @param hub hub representation
189 * @param port port number, starting from 1
190 */
191static void usb_hub_removed_device(
192 usb_hub_info_t * hub, uint16_t port) {
193
194 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
195 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
196 if (opResult != EOK) {
197 usb_log_warning("could not clear port-change-connection flag\n");
198 }
199 /** \TODO remove device from device manager - not yet implemented in
200 * devide manager
201 */
202
203 //close address
204 if(hub->ports[port].attached_device.address >= 0){
205 /*uncomment this code to use it when DDF allows device removal
206 opResult = usb_hc_unregister_device(
207 &hub->connection,
208 hub->attached_devs[port].address);
209 if(opResult != EOK) {
210 dprintf(USB_LOG_LEVEL_WARNING, "could not release "
211 "address of "
212 "removed device: %d", opResult);
213 }
214 hub->attached_devs[port].address = 0;
215 hub->attached_devs[port].handle = 0;
216 */
217 } else {
218 usb_log_warning("Device removed before being registered.\n");
219
220 /*
221 * Device was removed before port reset completed.
222 * We will announce a failed port reset to unblock the
223 * port reset callback from new device wrapper.
224 */
225 usb_hub_port_t *the_port = hub->ports + port;
226 fibril_mutex_lock(&the_port->reset_mutex);
227 the_port->reset_completed = true;
228 the_port->reset_okay = false;
229 fibril_condvar_broadcast(&the_port->reset_cv);
230 fibril_mutex_unlock(&the_port->reset_mutex);
231 }
232}
233
234
235/**
236 * Process port reset change
237 *
238 * After this change port should be enabled, unless some problem occured.
239 * This functions triggers second phase of enabling new device.
240 * @param hub
241 * @param port
242 * @param status
243 */
244static void usb_hub_port_reset_completed(usb_hub_info_t * hub,
245 uint16_t port, uint32_t status){
246 usb_log_debug("Port %zu reset complete.\n", (size_t) port);
247 if (usb_port_is_status(status, USB_HUB_FEATURE_PORT_ENABLE)) {
248 /* Finalize device adding. */
249 usb_hub_port_t *the_port = hub->ports + port;
250 fibril_mutex_lock(&the_port->reset_mutex);
251 the_port->reset_completed = true;
252 the_port->reset_okay = true;
253 fibril_condvar_broadcast(&the_port->reset_cv);
254 fibril_mutex_unlock(&the_port->reset_mutex);
255 } else {
256 usb_log_warning(
257 "Port %zu reset complete but port not enabled.\n",
258 (size_t) port);
259 }
260 /* Clear the port reset change. */
261 int rc = usb_hub_clear_port_feature(hub->control_pipe,
262 port, USB_HUB_FEATURE_C_PORT_RESET);
263 if (rc != EOK) {
264 usb_log_error("Failed to clear port %d reset feature: %s.\n",
265 port, str_error(rc));
266 }
267}
268
269/**
270 * Process over current condition on port.
271 *
272 * Turn off the power on the port.
273 *
274 * @param hub hub representation
275 * @param port port number, starting from 1
276 */
277static void usb_hub_port_over_current(usb_hub_info_t * hub,
278 uint16_t port, uint32_t status) {
279 int opResult;
280 if(usb_port_is_status(status, USB_HUB_FEATURE_PORT_OVER_CURRENT)){
281 opResult = usb_hub_clear_port_feature(hub->control_pipe,
282 port, USB_HUB_FEATURE_PORT_POWER);
283 if (opResult != EOK) {
284 usb_log_error("cannot power off port %d; %d\n",
285 port, opResult);
286 }
287 }else{
288 opResult = usb_hub_set_port_feature(hub->control_pipe,
289 port, USB_HUB_FEATURE_PORT_POWER);
290 if (opResult != EOK) {
291 usb_log_error("cannot power on port %d; %d\n",
292 port, opResult);
293 }
294 }
295}
296
297/** Retrieve port status.
298 *
299 * @param[in] ctrl_pipe Control pipe to use.
300 * @param[in] port Port number (starting at 1).
301 * @param[out] status Where to store the port status.
302 * @return Error code.
303 */
304static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
305 usb_port_status_t *status)
306{
307 size_t recv_size;
308 usb_device_request_setup_packet_t request;
309 usb_port_status_t status_tmp;
310
311 usb_hub_set_port_status_request(&request, port);
312 int rc = usb_pipe_control_read(ctrl_pipe,
313 &request, sizeof(usb_device_request_setup_packet_t),
314 &status_tmp, sizeof(status_tmp), &recv_size);
315 if (rc != EOK) {
316 return rc;
317 }
318
319 if (recv_size != sizeof (status_tmp)) {
320 return ELIMIT;
321 }
322
323 if (status != NULL) {
324 *status = status_tmp;
325 }
326
327 return EOK;
328}
329
330/** Callback for enabling a specific port.
331 *
332 * We wait on a CV until port is reseted.
333 * That is announced via change on interrupt pipe.
334 *
335 * @param port_no Port number (starting at 1).
336 * @param arg Custom argument, points to @c usb_hub_info_t.
337 * @return Error code.
338 */
339static int enable_port_callback(int port_no, void *arg)
340{
341 usb_hub_info_t *hub = arg;
342 int rc;
343 usb_device_request_setup_packet_t request;
344 usb_hub_port_t *my_port = hub->ports + port_no;
345
346 usb_hub_set_reset_port_request(&request, port_no);
347 rc = usb_pipe_control_write(hub->control_pipe,
348 &request, sizeof(request), NULL, 0);
349 if (rc != EOK) {
350 usb_log_warning("Port reset failed: %s.\n", str_error(rc));
351 return rc;
352 }
353
354 /*
355 * Wait until reset completes.
356 */
357 fibril_mutex_lock(&my_port->reset_mutex);
358 while (!my_port->reset_completed) {
359 fibril_condvar_wait(&my_port->reset_cv, &my_port->reset_mutex);
360 }
361 fibril_mutex_unlock(&my_port->reset_mutex);
362
363 if (my_port->reset_okay) {
364 return EOK;
365 } else {
366 return ESTALL;
367 }
368}
369
370/** Fibril for adding a new device.
371 *
372 * Separate fibril is needed because the port reset completion is announced
373 * via interrupt pipe and thus we cannot block here.
374 *
375 * @param arg Pointer to struct add_device_phase1.
376 * @return 0 Always.
377 */
378static int add_device_phase1_worker_fibril(void *arg)
379{
380 struct add_device_phase1 *data
381 = (struct add_device_phase1 *) arg;
382
383 usb_address_t new_address;
384 devman_handle_t child_handle;
385
386 int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
387 &data->hub->connection, data->speed,
388 enable_port_callback, (int) data->port, data->hub,
389 &new_address, &child_handle,
390 NULL, NULL, NULL);
391
392 if (rc != EOK) {
393 usb_log_error("Failed registering device on port %zu: %s.\n",
394 data->port, str_error(rc));
395 goto leave;
396 }
397
398 data->hub->ports[data->port].attached_device.handle = child_handle;
399 data->hub->ports[data->port].attached_device.address = new_address;
400
401 usb_log_info("Detected new device on `%s' (port %zu), "
402 "address %d (handle %" PRIun ").\n",
403 data->hub->usb_device->ddf_dev->name, data->port,
404 new_address, child_handle);
405
406leave:
407 free(arg);
408
409 return EOK;
410}
411
412
413/** Start device adding when connection change is detected.
414 *
415 * This fires a new fibril to complete the device addition.
416 *
417 * @param hub Hub where the change occured.
418 * @param port Port index (starting at 1).
419 * @param speed Speed of the device.
420 * @return Error code.
421 */
422static int create_add_device_fibril(usb_hub_info_t *hub, size_t port,
423 usb_speed_t speed)
424{
425 struct add_device_phase1 *data
426 = malloc(sizeof(struct add_device_phase1));
427 if (data == NULL) {
428 return ENOMEM;
429 }
430 data->hub = hub;
431 data->port = port;
432 data->speed = speed;
433
434 usb_hub_port_t *the_port = hub->ports + port;
435
436 fibril_mutex_lock(&the_port->reset_mutex);
437 the_port->reset_completed = false;
438 fibril_mutex_unlock(&the_port->reset_mutex);
439
440 int rc = usb_hub_clear_port_feature(hub->control_pipe, port,
441 USB_HUB_FEATURE_C_PORT_CONNECTION);
442 if (rc != EOK) {
443 free(data);
444 usb_log_warning("Failed to clear port change flag: %s.\n",
445 str_error(rc));
446 return rc;
447 }
448
449 fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
450 if (fibril == 0) {
451 free(data);
452 return ENOMEM;
453 }
454 fibril_add_ready(fibril);
455
456 return EOK;
457}
458
459/**
460 * @}
461 */
Note: See TracBrowser for help on using the repository browser.