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 <usbhc_iface.h>
|
---|
54 |
|
---|
55 | #include "usbhub.h"
|
---|
56 | #include "status.h"
|
---|
57 |
|
---|
58 | #define HUB_FNC_NAME "hub"
|
---|
59 |
|
---|
60 | #define HUB_STATUS_CHANGE_EP(protocol) { \
|
---|
61 | .transfer_type = USB_TRANSFER_INTERRUPT, \
|
---|
62 | .direction = USB_DIRECTION_IN, \
|
---|
63 | .interface_class = USB_CLASS_HUB, \
|
---|
64 | .interface_subclass = 0, \
|
---|
65 | .interface_protocol = (protocol), \
|
---|
66 | .flags = 0 \
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Hub status-change endpoint description.
|
---|
70 | *
|
---|
71 | * For more information see section 11.15.1 of USB 1.1 specification.
|
---|
72 | */
|
---|
73 | static const usb_endpoint_description_t
|
---|
74 | status_change_single_tt_only = HUB_STATUS_CHANGE_EP(0),
|
---|
75 | status_change_mtt_available = HUB_STATUS_CHANGE_EP(1);
|
---|
76 |
|
---|
77 | const usb_endpoint_description_t *usb_hub_endpoints [] = {
|
---|
78 | &status_change_single_tt_only,
|
---|
79 | &status_change_mtt_available,
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | /** Standard get hub global status request */
|
---|
84 | static const usb_device_request_setup_packet_t get_hub_status_request = {
|
---|
85 | .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
|
---|
86 | .request = USB_HUB_REQUEST_GET_STATUS,
|
---|
87 | .index = 0,
|
---|
88 | .value = 0,
|
---|
89 | .length = sizeof(usb_hub_status_t),
|
---|
90 | };
|
---|
91 |
|
---|
92 | static int usb_set_first_configuration(usb_device_t *usb_device);
|
---|
93 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
|
---|
94 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
---|
95 | usb_hub_status_t status);
|
---|
96 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
|
---|
97 |
|
---|
98 | static bool usb_hub_polling_error_callback(usb_device_t *dev, int err_code, void *arg)
|
---|
99 | {
|
---|
100 | assert(dev);
|
---|
101 | assert(arg);
|
---|
102 |
|
---|
103 | usb_log_error("Device %s polling error: %s", usb_device_get_name(dev), str_error(err_code));
|
---|
104 |
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Initialize hub device driver structure.
|
---|
110 | *
|
---|
111 | * Creates hub representation and fibril that periodically checks hub's status.
|
---|
112 | * Hub representation is passed to the fibril.
|
---|
113 | * @param usb_dev generic usb device information
|
---|
114 | * @return error code
|
---|
115 | */
|
---|
116 | int usb_hub_device_add(usb_device_t *usb_dev)
|
---|
117 | {
|
---|
118 | assert(usb_dev);
|
---|
119 | /* Create driver soft-state structure */
|
---|
120 | usb_hub_dev_t *hub_dev =
|
---|
121 | usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
|
---|
122 | if (hub_dev == NULL) {
|
---|
123 | usb_log_error("Failed to create hub driver structure.");
|
---|
124 | return ENOMEM;
|
---|
125 | }
|
---|
126 | hub_dev->usb_device = usb_dev;
|
---|
127 | hub_dev->speed = usb_device_get_speed(usb_dev);
|
---|
128 |
|
---|
129 | fibril_mutex_initialize(&hub_dev->default_address_guard);
|
---|
130 | fibril_condvar_initialize(&hub_dev->default_address_cv);
|
---|
131 |
|
---|
132 | /* Set hub's first configuration. (There should be only one) */
|
---|
133 | int opResult = usb_set_first_configuration(usb_dev);
|
---|
134 | if (opResult != EOK) {
|
---|
135 | usb_log_error("Could not set hub configuration: %s",
|
---|
136 | str_error(opResult));
|
---|
137 | return opResult;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* Get port count and create attached_devices. */
|
---|
141 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
---|
142 | if (opResult != EOK) {
|
---|
143 | usb_log_error("Could process hub specific info, %s",
|
---|
144 | str_error(opResult));
|
---|
145 | return opResult;
|
---|
146 | }
|
---|
147 |
|
---|
148 | const usb_endpoint_description_t *status_change = hub_dev->mtt_available
|
---|
149 | ? &status_change_mtt_available
|
---|
150 | : &status_change_single_tt_only;
|
---|
151 |
|
---|
152 | usb_endpoint_mapping_t *status_change_mapping
|
---|
153 | = usb_device_get_mapped_ep_desc(hub_dev->usb_device, status_change);
|
---|
154 | if (!status_change_mapping) {
|
---|
155 | usb_log_error("Failed to map the Status Change Endpoint of a hub.");
|
---|
156 | return EIO;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* Create hub control function. */
|
---|
160 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
|
---|
161 | hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
|
---|
162 | fun_exposed, HUB_FNC_NAME);
|
---|
163 | if (hub_dev->hub_fun == NULL) {
|
---|
164 | usb_log_error("Failed to create hub function.");
|
---|
165 | return ENOMEM;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* Bind hub control function. */
|
---|
169 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
---|
170 | if (opResult != EOK) {
|
---|
171 | usb_log_error("Failed to bind hub function: %s.",
|
---|
172 | str_error(opResult));
|
---|
173 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
174 | return opResult;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* Start hub operation. */
|
---|
178 | usb_polling_t *polling = &hub_dev->polling;
|
---|
179 | opResult = usb_polling_init(polling);
|
---|
180 | if (opResult != EOK) {
|
---|
181 | /* Function is already bound */
|
---|
182 | ddf_fun_unbind(hub_dev->hub_fun);
|
---|
183 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
184 | usb_log_error("Failed to initialize polling fibril: %s.",
|
---|
185 | str_error(opResult));
|
---|
186 | return opResult;
|
---|
187 | }
|
---|
188 |
|
---|
189 | polling->device = hub_dev->usb_device;
|
---|
190 | polling->ep_mapping = status_change_mapping;
|
---|
191 | polling->request_size = ((hub_dev->port_count + 1 + 7) / 8);
|
---|
192 | polling->buffer = malloc(polling->request_size);
|
---|
193 | polling->on_data = hub_port_changes_callback;
|
---|
194 | polling->on_error = usb_hub_polling_error_callback;
|
---|
195 | polling->arg = hub_dev;
|
---|
196 |
|
---|
197 | opResult = usb_polling_start(polling);
|
---|
198 | if (opResult != EOK) {
|
---|
199 | /* Polling is already initialized. */
|
---|
200 | free(polling->buffer);
|
---|
201 | usb_polling_fini(polling);
|
---|
202 | ddf_fun_unbind(hub_dev->hub_fun);
|
---|
203 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
204 | usb_log_error("Failed to create polling fibril: %s.",
|
---|
205 | str_error(opResult));
|
---|
206 | return opResult;
|
---|
207 | }
|
---|
208 |
|
---|
209 | usb_log_info("Controlling %s-speed hub '%s' (%p: %zu ports).",
|
---|
210 | usb_str_speed(hub_dev->speed),
|
---|
211 | usb_device_get_name(hub_dev->usb_device), hub_dev,
|
---|
212 | hub_dev->port_count);
|
---|
213 |
|
---|
214 | return EOK;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static int usb_hub_cleanup(usb_hub_dev_t *hub)
|
---|
218 | {
|
---|
219 | free(hub->polling.buffer);
|
---|
220 | usb_polling_fini(&hub->polling);
|
---|
221 |
|
---|
222 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
223 | usb_port_fini(&hub->ports[port].base);
|
---|
224 | }
|
---|
225 | free(hub->ports);
|
---|
226 |
|
---|
227 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
---|
228 | if (ret != EOK) {
|
---|
229 | usb_log_error("(%p) Failed to unbind '%s' function: %s.",
|
---|
230 | hub, HUB_FNC_NAME, str_error(ret));
|
---|
231 | return ret;
|
---|
232 | }
|
---|
233 | ddf_fun_destroy(hub->hub_fun);
|
---|
234 |
|
---|
235 | usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
|
---|
236 |
|
---|
237 | /* Device data (usb_hub_dev_t) will be freed by usbdev. */
|
---|
238 | return EOK;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Turn off power to all ports.
|
---|
243 | *
|
---|
244 | * @param usb_dev generic usb device information
|
---|
245 | * @return error code
|
---|
246 | */
|
---|
247 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
---|
248 | {
|
---|
249 | assert(usb_dev);
|
---|
250 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
---|
251 | assert(hub);
|
---|
252 |
|
---|
253 | usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
|
---|
254 |
|
---|
255 | /* Join polling fibril (ignoring error code). */
|
---|
256 | usb_polling_join(&hub->polling);
|
---|
257 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
---|
258 |
|
---|
259 | /* Destroy hub. */
|
---|
260 | return usb_hub_cleanup(hub);
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Remove all attached devices
|
---|
265 | * @param usb_dev generic usb device information
|
---|
266 | * @return error code
|
---|
267 | */
|
---|
268 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
---|
269 | {
|
---|
270 | assert(usb_dev);
|
---|
271 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
---|
272 | assert(hub);
|
---|
273 |
|
---|
274 | usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
|
---|
275 |
|
---|
276 | /* Join polling fibril (ignoring error code). */
|
---|
277 | usb_polling_join(&hub->polling);
|
---|
278 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
---|
279 |
|
---|
280 | /* Destroy hub. */
|
---|
281 | return usb_hub_cleanup(hub);
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** Callback for polling hub for changes.
|
---|
285 | *
|
---|
286 | * @param dev Device where the change occured.
|
---|
287 | * @param change_bitmap Bitmap of changed ports.
|
---|
288 | * @param change_bitmap_size Size of the bitmap in bytes.
|
---|
289 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
---|
290 | * @return Whether to continue polling.
|
---|
291 | */
|
---|
292 | bool hub_port_changes_callback(usb_device_t *dev,
|
---|
293 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
---|
294 | {
|
---|
295 | usb_hub_dev_t *hub = arg;
|
---|
296 | assert(hub);
|
---|
297 |
|
---|
298 | /* It is an error condition if we didn't receive enough data */
|
---|
299 | if (change_bitmap_size == 0) {
|
---|
300 | return false;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* Lowest bit indicates global change */
|
---|
304 | const bool change = change_bitmap[0] & 1;
|
---|
305 | if (change) {
|
---|
306 | usb_hub_global_interrupt(hub);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* N + 1 bit indicates change on port N */
|
---|
310 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
311 | const size_t bit = port + 1;
|
---|
312 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
---|
313 | if (change) {
|
---|
314 | usb_hub_port_process_interrupt(&hub->ports[port]);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | return true;
|
---|
318 | }
|
---|
319 |
|
---|
320 | static void usb_hub_power_ports(usb_hub_dev_t *hub_dev)
|
---|
321 | {
|
---|
322 | if (!hub_dev->power_switched) {
|
---|
323 | usb_log_info("(%p): Power switching not supported, "
|
---|
324 | "ports always powered.", hub_dev);
|
---|
325 | return;
|
---|
326 | }
|
---|
327 |
|
---|
328 | usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
|
---|
329 | hub_dev->per_port_power ? "per port" : "ganged");
|
---|
330 |
|
---|
331 | for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
|
---|
332 | usb_log_debug("(%p): Powering port %u.", hub_dev, port + 1);
|
---|
333 | const int ret = usb_hub_set_port_feature(hub_dev, port + 1, USB_HUB_FEATURE_PORT_POWER);
|
---|
334 |
|
---|
335 | if (ret != EOK) {
|
---|
336 | usb_log_error("(%p-%u): Cannot power on port: %s.",
|
---|
337 | hub_dev, hub_dev->ports[port].port_number,
|
---|
338 | str_error(ret));
|
---|
339 | /* Continue to try at least other ports */
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Load hub-specific information into hub_dev structure and process if needed
|
---|
346 | *
|
---|
347 | * Read port count and initialize structures holding per port information.
|
---|
348 | * If there are any non-removable devices, start initializing them.
|
---|
349 | * This function is hub-specific and should be run only after the hub is
|
---|
350 | * configured using usb_set_first_configuration function.
|
---|
351 | * @param hub_dev hub representation
|
---|
352 | * @return error code
|
---|
353 | */
|
---|
354 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
---|
355 | {
|
---|
356 | assert(hub_dev);
|
---|
357 |
|
---|
358 | /* Get hub descriptor. */
|
---|
359 | usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
|
---|
360 | usb_pipe_t *control_pipe = usb_device_get_default_pipe(hub_dev->usb_device);
|
---|
361 |
|
---|
362 | usb_descriptor_type_t desc_type = hub_dev->speed >= USB_SPEED_SUPER
|
---|
363 | ? USB_DESCTYPE_SSPEED_HUB : USB_DESCTYPE_HUB;
|
---|
364 |
|
---|
365 | usb_hub_descriptor_header_t descriptor;
|
---|
366 | size_t received_size;
|
---|
367 | int opResult = usb_request_get_descriptor(control_pipe,
|
---|
368 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
369 | desc_type, 0, 0, &descriptor,
|
---|
370 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
---|
371 | if (opResult != EOK) {
|
---|
372 | usb_log_error("(%p): Failed to receive hub descriptor: %s.",
|
---|
373 | hub_dev, str_error(opResult));
|
---|
374 | return opResult;
|
---|
375 | }
|
---|
376 |
|
---|
377 | usb_log_debug("(%p): Setting port count to %d.", hub_dev,
|
---|
378 | descriptor.port_count);
|
---|
379 | hub_dev->port_count = descriptor.port_count;
|
---|
380 | hub_dev->control_pipe = control_pipe;
|
---|
381 |
|
---|
382 | usb_log_debug("(%p): Setting hub depth to %u.", hub_dev,
|
---|
383 | usb_device_get_depth(hub_dev->usb_device));
|
---|
384 | if ((opResult = usb_hub_set_depth(hub_dev))) {
|
---|
385 | usb_log_error("(%p): Failed to set hub depth: %s.",
|
---|
386 | hub_dev, str_error(opResult));
|
---|
387 | return opResult;
|
---|
388 | }
|
---|
389 |
|
---|
390 | hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
|
---|
391 | if (!hub_dev->ports) {
|
---|
392 | return ENOMEM;
|
---|
393 | }
|
---|
394 |
|
---|
395 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
---|
396 | usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
|
---|
397 | }
|
---|
398 |
|
---|
399 | hub_dev->power_switched =
|
---|
400 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
---|
401 | hub_dev->per_port_power =
|
---|
402 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
---|
403 |
|
---|
404 | const uint8_t protocol = usb_device_descriptors(hub_dev->usb_device)
|
---|
405 | ->device.device_protocol;
|
---|
406 | hub_dev->mtt_available = (protocol == 2);
|
---|
407 |
|
---|
408 | usb_hub_power_ports(hub_dev);
|
---|
409 |
|
---|
410 | return EOK;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Set configuration of and USB device
|
---|
415 | *
|
---|
416 | * Check whether there is at least one configuration and sets the first one.
|
---|
417 | * This function should be run prior to running any hub-specific action.
|
---|
418 | * @param usb_device usb device representation
|
---|
419 | * @return error code
|
---|
420 | */
|
---|
421 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
---|
422 | {
|
---|
423 | assert(usb_device);
|
---|
424 | /* Get number of possible configurations from device descriptor */
|
---|
425 | const size_t configuration_count =
|
---|
426 | usb_device_descriptors(usb_device)->device.configuration_count;
|
---|
427 | usb_log_debug("Hub has %zu configurations.", configuration_count);
|
---|
428 |
|
---|
429 | if (configuration_count < 1) {
|
---|
430 | usb_log_error("There are no configurations available");
|
---|
431 | return EINVAL;
|
---|
432 | }
|
---|
433 |
|
---|
434 | const size_t config_size =
|
---|
435 | usb_device_descriptors(usb_device)->full_config_size;
|
---|
436 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
---|
437 | usb_device_descriptors(usb_device)->full_config;
|
---|
438 |
|
---|
439 | if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
|
---|
440 | usb_log_error("Configuration descriptor is not big enough"
|
---|
441 | " to fit standard configuration descriptor.\n");
|
---|
442 | return EOVERFLOW;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /* Set configuration. Use the configuration that was in
|
---|
446 | * usb_device->descriptors.configuration i.e. The first one. */
|
---|
447 | int opResult = usb_request_set_configuration(
|
---|
448 | usb_device_get_default_pipe(usb_device),
|
---|
449 | config_descriptor->configuration_number);
|
---|
450 | if (opResult != EOK) {
|
---|
451 | usb_log_error("Failed to set hub configuration: %s.",
|
---|
452 | str_error(opResult));
|
---|
453 | } else {
|
---|
454 | usb_log_debug("\tUsed configuration %d",
|
---|
455 | config_descriptor->configuration_number);
|
---|
456 | }
|
---|
457 |
|
---|
458 | return opResult;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Process hub over current change
|
---|
463 | *
|
---|
464 | * This means either to power off the hub or power it on.
|
---|
465 | * @param hub_dev hub instance
|
---|
466 | * @param status hub status bitmask
|
---|
467 | * @return error code
|
---|
468 | */
|
---|
469 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
---|
470 | usb_hub_status_t status)
|
---|
471 | {
|
---|
472 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
---|
473 | /* Hub should remove power from all ports if it detects OC */
|
---|
474 | usb_log_warning("(%p) Detected hub over-current condition, "
|
---|
475 | "all ports should be powered off.", hub_dev);
|
---|
476 | return;
|
---|
477 | }
|
---|
478 |
|
---|
479 | /* Ports are always powered. */
|
---|
480 | if (!hub_dev->power_switched)
|
---|
481 | return;
|
---|
482 |
|
---|
483 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
---|
484 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
---|
485 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
---|
486 | if (ret != EOK) {
|
---|
487 | usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
|
---|
488 | " power on port: %s\n", hub_dev,
|
---|
489 | hub_dev->ports[port].port_number, str_error(ret));
|
---|
490 | } else {
|
---|
491 | if (!hub_dev->per_port_power)
|
---|
492 | return;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Set feature on the real hub port.
|
---|
499 | *
|
---|
500 | * @param port Port structure.
|
---|
501 | * @param feature Feature selector.
|
---|
502 | */
|
---|
503 | int usb_hub_set_depth(const usb_hub_dev_t *hub)
|
---|
504 | {
|
---|
505 | assert(hub);
|
---|
506 |
|
---|
507 | /* Slower hubs do not care about depth */
|
---|
508 | if (hub->speed < USB_SPEED_SUPER)
|
---|
509 | return EOK;
|
---|
510 |
|
---|
511 | const usb_device_request_setup_packet_t set_request = {
|
---|
512 | .request_type = USB_HUB_REQ_TYPE_SET_HUB_DEPTH,
|
---|
513 | .request = USB_HUB_REQUEST_SET_HUB_DEPTH,
|
---|
514 | .value = uint16_host2usb(usb_device_get_depth(hub->usb_device) - 1),
|
---|
515 | .index = 0,
|
---|
516 | .length = 0,
|
---|
517 | };
|
---|
518 | return usb_pipe_control_write(hub->control_pipe, &set_request,
|
---|
519 | sizeof(set_request), NULL, 0);
|
---|
520 | }
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * Set feature on the real hub port.
|
---|
524 | *
|
---|
525 | * @param port Port structure.
|
---|
526 | * @param feature Feature selector.
|
---|
527 | */
|
---|
528 | int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
---|
529 | {
|
---|
530 | assert(hub);
|
---|
531 | const usb_device_request_setup_packet_t clear_request = {
|
---|
532 | .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
|
---|
533 | .request = USB_DEVREQ_SET_FEATURE,
|
---|
534 | .index = uint16_host2usb(port_number),
|
---|
535 | .value = feature,
|
---|
536 | .length = 0,
|
---|
537 | };
|
---|
538 | return usb_pipe_control_write(hub->control_pipe, &clear_request,
|
---|
539 | sizeof(clear_request), NULL, 0);
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Clear feature on the real hub port.
|
---|
544 | *
|
---|
545 | * @param port Port structure.
|
---|
546 | * @param feature Feature selector.
|
---|
547 | */
|
---|
548 | int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
---|
549 | {
|
---|
550 | assert(hub);
|
---|
551 | const usb_device_request_setup_packet_t clear_request = {
|
---|
552 | .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
|
---|
553 | .request = USB_DEVREQ_CLEAR_FEATURE,
|
---|
554 | .value = feature,
|
---|
555 | .index = uint16_host2usb(port_number),
|
---|
556 | .length = 0,
|
---|
557 | };
|
---|
558 | return usb_pipe_control_write(hub->control_pipe,
|
---|
559 | &clear_request, sizeof(clear_request), NULL, 0);
|
---|
560 | }
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Retrieve port status.
|
---|
564 | *
|
---|
565 | * @param[in] port Port structure
|
---|
566 | * @param[out] status Where to store the port status.
|
---|
567 | * @return Error code.
|
---|
568 | */
|
---|
569 | int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number, usb_port_status_t *status)
|
---|
570 | {
|
---|
571 | assert(hub);
|
---|
572 | assert(status);
|
---|
573 |
|
---|
574 | /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
|
---|
575 | * Generic GET_STATUS request cannot be used because of the difference
|
---|
576 | * in status data size (2B vs. 4B)*/
|
---|
577 | const usb_device_request_setup_packet_t request = {
|
---|
578 | .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
|
---|
579 | .request = USB_HUB_REQUEST_GET_STATUS,
|
---|
580 | .value = 0,
|
---|
581 | .index = uint16_host2usb(port_number),
|
---|
582 | .length = sizeof(usb_port_status_t),
|
---|
583 | };
|
---|
584 | size_t recv_size;
|
---|
585 |
|
---|
586 | uint32_t buffer;
|
---|
587 | const int rc = usb_pipe_control_read(hub->control_pipe,
|
---|
588 | &request, sizeof(usb_device_request_setup_packet_t),
|
---|
589 | &buffer, sizeof(buffer), &recv_size);
|
---|
590 | if (rc != EOK)
|
---|
591 | return rc;
|
---|
592 |
|
---|
593 | if (recv_size != sizeof(*status))
|
---|
594 | return ELIMIT;
|
---|
595 |
|
---|
596 | *status = uint32_usb2host(buffer);
|
---|
597 | return EOK;
|
---|
598 | }
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Process hub interrupts.
|
---|
602 | *
|
---|
603 | * The change can be either in the over-current condition or local-power change.
|
---|
604 | * @param hub_dev hub instance
|
---|
605 | */
|
---|
606 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
---|
607 | {
|
---|
608 | assert(hub_dev);
|
---|
609 | assert(hub_dev->usb_device);
|
---|
610 | usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
|
---|
611 | usb_pipe_t *control_pipe =
|
---|
612 | usb_device_get_default_pipe(hub_dev->usb_device);
|
---|
613 |
|
---|
614 | usb_hub_status_t status;
|
---|
615 | size_t rcvd_size;
|
---|
616 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
---|
617 | * hubs reply is 4byte instead of 2 */
|
---|
618 | const int opResult = usb_pipe_control_read(control_pipe,
|
---|
619 | &get_hub_status_request, sizeof(get_hub_status_request),
|
---|
620 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
---|
621 | if (opResult != EOK) {
|
---|
622 | usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
|
---|
623 | str_error(opResult));
|
---|
624 | return;
|
---|
625 | }
|
---|
626 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
---|
627 | usb_log_error("(%p): Received status has incorrect size: "
|
---|
628 | "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
|
---|
629 | return;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /* Handle status changes */
|
---|
633 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
---|
634 | usb_hub_over_current(hub_dev, status);
|
---|
635 | /* Ack change in hub OC flag */
|
---|
636 | const int ret = usb_request_clear_feature(
|
---|
637 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
---|
638 | USB_REQUEST_RECIPIENT_DEVICE,
|
---|
639 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
---|
640 | if (ret != EOK) {
|
---|
641 | usb_log_error("(%p): Failed to clear hub over-current "
|
---|
642 | "change flag: %s.\n", hub_dev, str_error(opResult));
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
---|
647 | /* NOTE: Handling this is more complicated.
|
---|
648 | * If the transition is from bus power to local power, all
|
---|
649 | * is good and we may signal the parent hub that we don't
|
---|
650 | * need the power.
|
---|
651 | * If the transition is from local power to bus power
|
---|
652 | * the hub should turn off all the ports and devices need
|
---|
653 | * to be reinitialized taking into account the limited power
|
---|
654 | * that is now available.
|
---|
655 | * There is no support for power distribution in HelenOS,
|
---|
656 | * (or other OSes/hub devices that I've seen) so this is not
|
---|
657 | * implemented.
|
---|
658 | * Just ACK the change.
|
---|
659 | */
|
---|
660 | const int ret = usb_request_clear_feature(
|
---|
661 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
---|
662 | USB_REQUEST_RECIPIENT_DEVICE,
|
---|
663 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
---|
664 | if (opResult != EOK) {
|
---|
665 | usb_log_error("(%p): Failed to clear hub power change "
|
---|
666 | "flag: %s.\n", hub_dev, str_error(ret));
|
---|
667 | }
|
---|
668 | }
|
---|
669 | }
|
---|
670 |
|
---|
671 | static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Reserve a default address for a port across all other devices connected to
|
---|
675 | * the bus. We aggregate requests for ports to minimize delays between
|
---|
676 | * connecting multiple devices from one hub - which happens e.g. when the hub
|
---|
677 | * is connected with already attached devices.
|
---|
678 | */
|
---|
679 | int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch, usb_port_t *port)
|
---|
680 | {
|
---|
681 | assert(hub);
|
---|
682 | assert(exch);
|
---|
683 | assert(port);
|
---|
684 | assert(fibril_mutex_is_locked(&port->guard));
|
---|
685 |
|
---|
686 | fibril_mutex_lock(&hub->default_address_guard);
|
---|
687 | if (hub->default_address_requests++ == 0) {
|
---|
688 | /* We're the first to request the address, we can just do it */
|
---|
689 | fibril_mutex_unlock(&hub->default_address_guard);
|
---|
690 | int err;
|
---|
691 | while ((err = usbhc_reserve_default_address(exch)) == EAGAIN) {
|
---|
692 | // We ignore the return value here, as we cannot give up now.
|
---|
693 | usb_port_condvar_wait_timeout(port, &global_hub_default_address_cv, 500000);
|
---|
694 | }
|
---|
695 | return err;
|
---|
696 | } else {
|
---|
697 | /* Drop the port guard, we're going to wait */
|
---|
698 | fibril_mutex_unlock(&port->guard);
|
---|
699 |
|
---|
700 | /* Wait for a signal */
|
---|
701 | fibril_condvar_wait(&hub->default_address_cv, &hub->default_address_guard);
|
---|
702 |
|
---|
703 | /* Remember ABBA, first drop the hub guard */
|
---|
704 | fibril_mutex_unlock(&hub->default_address_guard);
|
---|
705 | fibril_mutex_lock(&port->guard);
|
---|
706 | return EOK;
|
---|
707 | }
|
---|
708 | }
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Release the default address from a port.
|
---|
712 | */
|
---|
713 | int usb_hub_release_default_address(usb_hub_dev_t *hub, async_exch_t *exch)
|
---|
714 | {
|
---|
715 | int ret = EOK;
|
---|
716 |
|
---|
717 | fibril_mutex_lock(&hub->default_address_guard);
|
---|
718 | if (--hub->default_address_requests == 0) {
|
---|
719 | // We must do it in critical section to prevent other fibril
|
---|
720 | // from requesting the address before we release
|
---|
721 | ret = usbhc_release_default_address(exch);
|
---|
722 | // This is optimistic optimization - it may wake one hub from polling sleep
|
---|
723 | fibril_condvar_signal(&global_hub_default_address_cv);
|
---|
724 | } else {
|
---|
725 | fibril_condvar_signal(&hub->default_address_cv);
|
---|
726 | }
|
---|
727 | fibril_mutex_unlock(&hub->default_address_guard);
|
---|
728 |
|
---|
729 | return ret;
|
---|
730 | }
|
---|
731 |
|
---|
732 | /**
|
---|
733 | * @}
|
---|
734 | */
|
---|