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