1 | /*
|
---|
2 | * Copyright (c) 2010 Matus Dekanek
|
---|
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 |
|
---|
30 | /** @addtogroup drvusbhub
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * @brief usb hub main functionality
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <ddf/driver.h>
|
---|
38 | #include <stdbool.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <inttypes.h>
|
---|
42 | #include <stdio.h>
|
---|
43 |
|
---|
44 | #include <usb/usb.h>
|
---|
45 | #include <usb/debug.h>
|
---|
46 | #include <usb/dev/pipes.h>
|
---|
47 | #include <usb/classes/classes.h>
|
---|
48 | #include <usb/descriptor.h>
|
---|
49 | #include <usb/dev/recognise.h>
|
---|
50 | #include <usb/dev/request.h>
|
---|
51 | #include <usb/classes/hub.h>
|
---|
52 | #include <usb/dev/poll.h>
|
---|
53 | #include <usb_iface.h>
|
---|
54 |
|
---|
55 | #include "usbhub.h"
|
---|
56 | #include "status.h"
|
---|
57 |
|
---|
58 | #define HUB_FNC_NAME "hub"
|
---|
59 |
|
---|
60 | /** Hub status-change endpoint description.
|
---|
61 | *
|
---|
62 | * For more information see section 11.15.1 of USB 1.1 specification.
|
---|
63 | */
|
---|
64 | const usb_endpoint_description_t hub_status_change_endpoint_description =
|
---|
65 | {
|
---|
66 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
67 | .direction = USB_DIRECTION_IN,
|
---|
68 | .interface_class = USB_CLASS_HUB,
|
---|
69 | .interface_subclass = 0,
|
---|
70 | .interface_protocol = 0,
|
---|
71 | .flags = 0
|
---|
72 | };
|
---|
73 |
|
---|
74 | /** Standard get hub global status request */
|
---|
75 | static const usb_device_request_setup_packet_t get_hub_status_request = {
|
---|
76 | .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
|
---|
77 | .request = USB_HUB_REQUEST_GET_STATUS,
|
---|
78 | .index = 0,
|
---|
79 | .value = 0,
|
---|
80 | .length = sizeof(usb_hub_status_t),
|
---|
81 | };
|
---|
82 |
|
---|
83 | static int usb_set_first_configuration(usb_device_t *usb_device);
|
---|
84 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
|
---|
85 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
---|
86 | usb_hub_status_t status);
|
---|
87 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
|
---|
88 | static void usb_hub_polling_terminated_callback(usb_device_t *device,
|
---|
89 | bool was_error, void *data);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Initialize hub device driver structure.
|
---|
93 | *
|
---|
94 | * Creates hub representation and fibril that periodically checks hub's status.
|
---|
95 | * Hub representation is passed to the fibril.
|
---|
96 | * @param usb_dev generic usb device information
|
---|
97 | * @return error code
|
---|
98 | */
|
---|
99 | int usb_hub_device_add(usb_device_t *usb_dev)
|
---|
100 | {
|
---|
101 | assert(usb_dev);
|
---|
102 | /* Create driver soft-state structure */
|
---|
103 | usb_hub_dev_t *hub_dev =
|
---|
104 | usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
|
---|
105 | if (hub_dev == NULL) {
|
---|
106 | usb_log_error("Failed to create hub driver structure.\n");
|
---|
107 | return ENOMEM;
|
---|
108 | }
|
---|
109 | hub_dev->usb_device = usb_dev;
|
---|
110 | hub_dev->pending_ops_count = 0;
|
---|
111 | hub_dev->running = false;
|
---|
112 | fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
|
---|
113 | fibril_condvar_initialize(&hub_dev->pending_ops_cv);
|
---|
114 |
|
---|
115 | /* Set hub's first configuration. (There should be only one) */
|
---|
116 | int opResult = usb_set_first_configuration(usb_dev);
|
---|
117 | if (opResult != EOK) {
|
---|
118 | usb_log_error("Could not set hub configuration: %s\n",
|
---|
119 | str_error(opResult));
|
---|
120 | return opResult;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* Get port count and create attached_devices. */
|
---|
124 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
---|
125 | if (opResult != EOK) {
|
---|
126 | usb_log_error("Could process hub specific info, %s\n",
|
---|
127 | str_error(opResult));
|
---|
128 | return opResult;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* Create hub control function. */
|
---|
132 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
|
---|
133 | hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
|
---|
134 | fun_exposed, HUB_FNC_NAME);
|
---|
135 | if (hub_dev->hub_fun == NULL) {
|
---|
136 | usb_log_error("Failed to create hub function.\n");
|
---|
137 | return ENOMEM;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* Bind hub control function. */
|
---|
141 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
---|
142 | if (opResult != EOK) {
|
---|
143 | usb_log_error("Failed to bind hub function: %s.\n",
|
---|
144 | str_error(opResult));
|
---|
145 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
146 | return opResult;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Start hub operation. */
|
---|
150 | opResult = usb_device_auto_poll_desc(hub_dev->usb_device,
|
---|
151 | &hub_status_change_endpoint_description,
|
---|
152 | hub_port_changes_callback, ((hub_dev->port_count + 1 + 7) / 8),
|
---|
153 | -1, usb_hub_polling_terminated_callback, hub_dev);
|
---|
154 | if (opResult != EOK) {
|
---|
155 | /* Function is already bound */
|
---|
156 | ddf_fun_unbind(hub_dev->hub_fun);
|
---|
157 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
158 | usb_log_error("Failed to create polling fibril: %s.\n",
|
---|
159 | str_error(opResult));
|
---|
160 | return opResult;
|
---|
161 | }
|
---|
162 | hub_dev->running = true;
|
---|
163 | usb_log_info("Controlling hub '%s' (%p: %zu ports).\n",
|
---|
164 | usb_device_get_name(hub_dev->usb_device), hub_dev,
|
---|
165 | hub_dev->port_count);
|
---|
166 |
|
---|
167 | return EOK;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Turn off power to all ports.
|
---|
172 | *
|
---|
173 | * @param usb_dev generic usb device information
|
---|
174 | * @return error code
|
---|
175 | */
|
---|
176 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
---|
177 | {
|
---|
178 | return ENOTSUP;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Remove all attached devices
|
---|
183 | * @param usb_dev generic usb device information
|
---|
184 | * @return error code
|
---|
185 | */
|
---|
186 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
---|
187 | {
|
---|
188 | assert(usb_dev);
|
---|
189 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
---|
190 | assert(hub);
|
---|
191 | unsigned tries = 10;
|
---|
192 | while (hub->running) {
|
---|
193 | async_usleep(100000);
|
---|
194 | if (!tries--) {
|
---|
195 | usb_log_error("(%p): Can't remove hub, still running.",
|
---|
196 | hub);
|
---|
197 | return EBUSY;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | assert(!hub->running);
|
---|
202 |
|
---|
203 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
204 | const int ret = usb_hub_port_fini(&hub->ports[port], hub);
|
---|
205 | if (ret != EOK)
|
---|
206 | return ret;
|
---|
207 | }
|
---|
208 | free(hub->ports);
|
---|
209 |
|
---|
210 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
---|
211 | if (ret != EOK) {
|
---|
212 | usb_log_error("(%p) Failed to unbind '%s' function: %s.",
|
---|
213 | hub, HUB_FNC_NAME, str_error(ret));
|
---|
214 | return ret;
|
---|
215 | }
|
---|
216 | ddf_fun_destroy(hub->hub_fun);
|
---|
217 |
|
---|
218 | usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
|
---|
219 | return EOK;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /** Callback for polling hub for changes.
|
---|
223 | *
|
---|
224 | * @param dev Device where the change occured.
|
---|
225 | * @param change_bitmap Bitmap of changed ports.
|
---|
226 | * @param change_bitmap_size Size of the bitmap in bytes.
|
---|
227 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
---|
228 | * @return Whether to continue polling.
|
---|
229 | */
|
---|
230 | bool hub_port_changes_callback(usb_device_t *dev,
|
---|
231 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
---|
232 | {
|
---|
233 | usb_hub_dev_t *hub = arg;
|
---|
234 | assert(hub);
|
---|
235 |
|
---|
236 | /* It is an error condition if we didn't receive enough data */
|
---|
237 | if (change_bitmap_size == 0) {
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Lowest bit indicates global change */
|
---|
242 | const bool change = change_bitmap[0] & 1;
|
---|
243 | if (change) {
|
---|
244 | usb_hub_global_interrupt(hub);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* N + 1 bit indicates change on port N */
|
---|
248 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
249 | const size_t bit = port + 1;
|
---|
250 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
---|
251 | if (change) {
|
---|
252 | usb_hub_port_process_interrupt(&hub->ports[port], hub);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return true;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Load hub-specific information into hub_dev structure and process if needed
|
---|
260 | *
|
---|
261 | * Read port count and initialize structures holding per port information.
|
---|
262 | * If there are any non-removable devices, start initializing them.
|
---|
263 | * This function is hub-specific and should be run only after the hub is
|
---|
264 | * configured using usb_set_first_configuration function.
|
---|
265 | * @param hub_dev hub representation
|
---|
266 | * @return error code
|
---|
267 | */
|
---|
268 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
---|
269 | {
|
---|
270 | assert(hub_dev);
|
---|
271 |
|
---|
272 | /* Get hub descriptor. */
|
---|
273 | usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
|
---|
274 | usb_pipe_t *control_pipe =
|
---|
275 | usb_device_get_default_pipe(hub_dev->usb_device);
|
---|
276 |
|
---|
277 | usb_hub_descriptor_header_t descriptor;
|
---|
278 | size_t received_size;
|
---|
279 | int opResult = usb_request_get_descriptor(control_pipe,
|
---|
280 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
281 | USB_DESCTYPE_HUB, 0, 0, &descriptor,
|
---|
282 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
---|
283 | if (opResult != EOK) {
|
---|
284 | usb_log_error("(%p): Failed to receive hub descriptor: %s.\n",
|
---|
285 | hub_dev, str_error(opResult));
|
---|
286 | return opResult;
|
---|
287 | }
|
---|
288 |
|
---|
289 | usb_log_debug("(%p): Setting port count to %d.\n", hub_dev,
|
---|
290 | descriptor.port_count);
|
---|
291 | hub_dev->port_count = descriptor.port_count;
|
---|
292 |
|
---|
293 | hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
|
---|
294 | if (!hub_dev->ports) {
|
---|
295 | return ENOMEM;
|
---|
296 | }
|
---|
297 |
|
---|
298 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
---|
299 | usb_hub_port_init(
|
---|
300 | &hub_dev->ports[port], port + 1, control_pipe);
|
---|
301 | }
|
---|
302 |
|
---|
303 | hub_dev->power_switched =
|
---|
304 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
---|
305 | hub_dev->per_port_power =
|
---|
306 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
---|
307 |
|
---|
308 | if (!hub_dev->power_switched) {
|
---|
309 | usb_log_info("(%p): Power switching not supported, "
|
---|
310 | "ports always powered.", hub_dev);
|
---|
311 | return EOK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | usb_log_info("(%p): Hub port power switching enabled (%s).\n", hub_dev,
|
---|
315 | hub_dev->per_port_power ? "per port" : "ganged");
|
---|
316 |
|
---|
317 | for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
|
---|
318 | usb_log_debug("(%p): Powering port %u.", hub_dev, port);
|
---|
319 | const int ret = usb_hub_port_set_feature(
|
---|
320 | &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
|
---|
321 |
|
---|
322 | if (ret != EOK) {
|
---|
323 | usb_log_error("(%p-%u): Cannot power on port: %s.\n",
|
---|
324 | hub_dev, hub_dev->ports[port].port_number,
|
---|
325 | str_error(ret));
|
---|
326 | } else {
|
---|
327 | if (!hub_dev->per_port_power) {
|
---|
328 | usb_log_debug("(%p) Ganged power switching, "
|
---|
329 | "one port is enough.", hub_dev);
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 | return EOK;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Set configuration of and USB device
|
---|
339 | *
|
---|
340 | * Check whether there is at least one configuration and sets the first one.
|
---|
341 | * This function should be run prior to running any hub-specific action.
|
---|
342 | * @param usb_device usb device representation
|
---|
343 | * @return error code
|
---|
344 | */
|
---|
345 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
---|
346 | {
|
---|
347 | assert(usb_device);
|
---|
348 | /* Get number of possible configurations from device descriptor */
|
---|
349 | const size_t configuration_count =
|
---|
350 | usb_device_descriptors(usb_device)->device.configuration_count;
|
---|
351 | usb_log_debug("Hub has %zu configurations.\n", configuration_count);
|
---|
352 |
|
---|
353 | if (configuration_count < 1) {
|
---|
354 | usb_log_error("There are no configurations available\n");
|
---|
355 | return EINVAL;
|
---|
356 | }
|
---|
357 |
|
---|
358 | const size_t config_size =
|
---|
359 | usb_device_descriptors(usb_device)->full_config_size;
|
---|
360 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
---|
361 | usb_device_descriptors(usb_device)->full_config;
|
---|
362 |
|
---|
363 | if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
|
---|
364 | usb_log_error("Configuration descriptor is not big enough"
|
---|
365 | " to fit standard configuration descriptor.\n");
|
---|
366 | return EOVERFLOW;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /* Set configuration. Use the configuration that was in
|
---|
370 | * usb_device->descriptors.configuration i.e. The first one. */
|
---|
371 | const int opResult = usb_request_set_configuration(
|
---|
372 | usb_device_get_default_pipe(usb_device),
|
---|
373 | config_descriptor->configuration_number);
|
---|
374 | if (opResult != EOK) {
|
---|
375 | usb_log_error("Failed to set hub configuration: %s.\n",
|
---|
376 | str_error(opResult));
|
---|
377 | } else {
|
---|
378 | usb_log_debug("\tUsed configuration %d\n",
|
---|
379 | config_descriptor->configuration_number);
|
---|
380 | }
|
---|
381 | return opResult;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Process hub over current change
|
---|
386 | *
|
---|
387 | * This means either to power off the hub or power it on.
|
---|
388 | * @param hub_dev hub instance
|
---|
389 | * @param status hub status bitmask
|
---|
390 | * @return error code
|
---|
391 | */
|
---|
392 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
---|
393 | usb_hub_status_t status)
|
---|
394 | {
|
---|
395 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
---|
396 | /* Hub should remove power from all ports if it detects OC */
|
---|
397 | usb_log_warning("(%p) Detected hub over-current condition, "
|
---|
398 | "all ports should be powered off.", hub_dev);
|
---|
399 | return;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /* Ports are always powered. */
|
---|
403 | if (!hub_dev->power_switched)
|
---|
404 | return;
|
---|
405 |
|
---|
406 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
---|
407 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
---|
408 | const int ret = usb_hub_port_set_feature(
|
---|
409 | &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
|
---|
410 | if (ret != EOK) {
|
---|
411 | usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
|
---|
412 | " power on port: %s\n", hub_dev,
|
---|
413 | hub_dev->ports[port].port_number, str_error(ret));
|
---|
414 | } else {
|
---|
415 | if (!hub_dev->per_port_power)
|
---|
416 | return;
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Process hub interrupts.
|
---|
424 | *
|
---|
425 | * The change can be either in the over-current condition or local-power change.
|
---|
426 | * @param hub_dev hub instance
|
---|
427 | */
|
---|
428 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
---|
429 | {
|
---|
430 | assert(hub_dev);
|
---|
431 | assert(hub_dev->usb_device);
|
---|
432 | usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
|
---|
433 | usb_pipe_t *control_pipe =
|
---|
434 | usb_device_get_default_pipe(hub_dev->usb_device);
|
---|
435 |
|
---|
436 | usb_hub_status_t status;
|
---|
437 | size_t rcvd_size;
|
---|
438 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
---|
439 | * hubs reply is 4byte instead of 2 */
|
---|
440 | const int opResult = usb_pipe_control_read(control_pipe,
|
---|
441 | &get_hub_status_request, sizeof(get_hub_status_request),
|
---|
442 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
---|
443 | if (opResult != EOK) {
|
---|
444 | usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
|
---|
445 | str_error(opResult));
|
---|
446 | return;
|
---|
447 | }
|
---|
448 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
---|
449 | usb_log_error("(%p): Received status has incorrect size: "
|
---|
450 | "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
|
---|
451 | return;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /* Handle status changes */
|
---|
455 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
---|
456 | usb_hub_over_current(hub_dev, status);
|
---|
457 | /* Ack change in hub OC flag */
|
---|
458 | const int ret = usb_request_clear_feature(
|
---|
459 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
---|
460 | USB_REQUEST_RECIPIENT_DEVICE,
|
---|
461 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
---|
462 | if (ret != EOK) {
|
---|
463 | usb_log_error("(%p): Failed to clear hub over-current "
|
---|
464 | "change flag: %s.\n", hub_dev, str_error(opResult));
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
---|
469 | /* NOTE: Handling this is more complicated.
|
---|
470 | * If the transition is from bus power to local power, all
|
---|
471 | * is good and we may signal the parent hub that we don't
|
---|
472 | * need the power.
|
---|
473 | * If the transition is from local power to bus power
|
---|
474 | * the hub should turn off all the ports and devices need
|
---|
475 | * to be reinitialized taking into account the limited power
|
---|
476 | * that is now available.
|
---|
477 | * There is no support for power distribution in HelenOS,
|
---|
478 | * (or other OSes/hub devices that I've seen) so this is not
|
---|
479 | * implemented.
|
---|
480 | * Just ACK the change.
|
---|
481 | */
|
---|
482 | const int ret = usb_request_clear_feature(
|
---|
483 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
---|
484 | USB_REQUEST_RECIPIENT_DEVICE,
|
---|
485 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
---|
486 | if (opResult != EOK) {
|
---|
487 | usb_log_error("(%p): Failed to clear hub power change "
|
---|
488 | "flag: %s.\n", hub_dev, str_error(ret));
|
---|
489 | }
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * callback called from hub polling fibril when the fibril terminates
|
---|
495 | *
|
---|
496 | * Does not perform cleanup, just marks the hub as not running.
|
---|
497 | * @param device usb device afected
|
---|
498 | * @param was_error indicates that the fibril is stoped due to an error
|
---|
499 | * @param data pointer to usb_hub_dev_t structure
|
---|
500 | */
|
---|
501 | static void usb_hub_polling_terminated_callback(usb_device_t *device,
|
---|
502 | bool was_error, void *data)
|
---|
503 | {
|
---|
504 | usb_hub_dev_t *hub = data;
|
---|
505 | assert(hub);
|
---|
506 |
|
---|
507 | fibril_mutex_lock(&hub->pending_ops_mutex);
|
---|
508 |
|
---|
509 | /* The device is dead. However there might be some pending operations
|
---|
510 | * that we need to wait for.
|
---|
511 | * One of them is device adding in progress.
|
---|
512 | * The respective fibril is probably waiting for status change
|
---|
513 | * in port reset (port enable) callback.
|
---|
514 | * Such change would never come (otherwise we would not be here).
|
---|
515 | * Thus, we would flush all pending port resets.
|
---|
516 | */
|
---|
517 | if (hub->pending_ops_count > 0) {
|
---|
518 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
519 | usb_hub_port_reset_fail(&hub->ports[port]);
|
---|
520 | }
|
---|
521 | }
|
---|
522 | /* And now wait for them. */
|
---|
523 | while (hub->pending_ops_count > 0) {
|
---|
524 | fibril_condvar_wait(&hub->pending_ops_cv,
|
---|
525 | &hub->pending_ops_mutex);
|
---|
526 | }
|
---|
527 | fibril_mutex_unlock(&hub->pending_ops_mutex);
|
---|
528 | hub->running = false;
|
---|
529 | }
|
---|
530 | /**
|
---|
531 | * @}
|
---|
532 | */
|
---|