source: mainline/uspace/drv/usbhub/usbhub.c@ 7ab7c7f6

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

corrected hub power change handling

  • Property mode set to 100644
File size: 14.3 KB
Line 
1/*
2 * Copyright (c) 2010 Matus Dekanek
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/** @addtogroup drvusbhub
29 * @{
30 */
31/** @file
32 * @brief usb hub main functionality
33 */
34
35#include <ddf/driver.h>
36#include <bool.h>
37#include <errno.h>
38#include <str_error.h>
39#include <inttypes.h>
40
41#include <usb_iface.h>
42#include <usb/ddfiface.h>
43#include <usb/descriptor.h>
44#include <usb/recognise.h>
45#include <usb/request.h>
46#include <usb/classes/hub.h>
47#include <usb/devpoll.h>
48#include <stdio.h>
49
50#include "usbhub.h"
51#include "usbhub_private.h"
52#include "port_status.h"
53#include "usb/usb.h"
54#include "usb/pipes.h"
55#include "usb/classes/classes.h"
56
57
58static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev);
59
60static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info);
61
62static int usb_hub_set_configuration(usb_hub_info_t * hub_info);
63
64static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info);
65
66static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
67 usb_hub_status_t status);
68
69static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
70 usb_hub_status_t status);
71
72static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info);
73
74
75//*********************************************
76//
77// hub driver code, initialization
78//
79//*********************************************
80
81/**
82 * Initialize hub device driver fibril
83 *
84 * Creates hub representation and fibril that periodically checks hub`s status.
85 * Hub representation is passed to the fibril.
86 * @param usb_dev generic usb device information
87 * @return error code
88 */
89int usb_hub_add_device(usb_device_t * usb_dev) {
90 if (!usb_dev) return EINVAL;
91 usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
92 //create hc connection
93 usb_log_debug("Initializing USB wire abstraction.\n");
94 int opResult = usb_hc_connection_initialize_from_device(
95 &hub_info->connection,
96 hub_info->usb_device->ddf_dev);
97 if (opResult != EOK) {
98 usb_log_error("could not initialize connection to device, "
99 "errno %d\n",
100 opResult);
101 free(hub_info);
102 return opResult;
103 }
104
105 //set hub configuration
106 opResult = usb_hub_set_configuration(hub_info);
107 if (opResult != EOK) {
108 usb_log_error("could not set hub configuration, errno %d\n",
109 opResult);
110 free(hub_info);
111 return opResult;
112 }
113 //get port count and create attached_devs
114 opResult = usb_hub_process_hub_specific_info(hub_info);
115 if (opResult != EOK) {
116 usb_log_error("could process hub specific info, errno %d\n",
117 opResult);
118 free(hub_info);
119 return opResult;
120 }
121
122 usb_log_debug("Creating 'hub' function in DDF.\n");
123 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
124 fun_exposed, "hub");
125 assert(hub_fun != NULL);
126 hub_fun->ops = NULL;
127
128 opResult = ddf_fun_bind(hub_fun);
129 assert(opResult == EOK);
130 opResult = ddf_fun_add_to_class(hub_fun, "hub");
131 assert(opResult == EOK);
132
133 opResult = usb_hub_start_hub_fibril(hub_info);
134 if(opResult!=EOK)
135 free(hub_info);
136 return opResult;
137}
138
139
140/** Callback for polling hub for changes.
141 *
142 * @param dev Device where the change occured.
143 * @param change_bitmap Bitmap of changed ports.
144 * @param change_bitmap_size Size of the bitmap in bytes.
145 * @param arg Custom argument, points to @c usb_hub_info_t.
146 * @return Whether to continue polling.
147 */
148bool hub_port_changes_callback(usb_device_t *dev,
149 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) {
150 usb_log_debug("hub_port_changes_callback\n");
151 usb_hub_info_t *hub = (usb_hub_info_t *) arg;
152
153 /* FIXME: check that we received enough bytes. */
154 if (change_bitmap_size == 0) {
155 goto leave;
156 }
157
158 bool change;
159 change = ((uint8_t*) change_bitmap)[0] & 1;
160 if (change) {
161 usb_hub_process_global_interrupt(hub);
162 }
163
164 size_t port;
165 for (port = 1; port < hub->port_count + 1; port++) {
166 bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
167 if (change) {
168 usb_hub_process_interrupt(hub, port);
169 }
170 }
171leave:
172 /* FIXME: proper interval. */
173 async_usleep(1000 * 250);
174
175 return true;
176}
177
178
179//*********************************************
180//
181// support functions
182//
183//*********************************************
184
185/**
186 * create usb_hub_info_t structure
187 *
188 * Does only basic copying of known information into new structure.
189 * @param usb_dev usb device structure
190 * @return basic usb_hub_info_t structure
191 */
192static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
193 usb_hub_info_t * result = malloc(sizeof(usb_hub_info_t));
194 if (!result) return NULL;
195 result->usb_device = usb_dev;
196 result->status_change_pipe = usb_dev->pipes[0].pipe;
197 result->control_pipe = &usb_dev->ctrl_pipe;
198 result->is_default_address_used = false;
199 return result;
200}
201
202/**
203 * Load hub-specific information into hub_info structure and process if needed
204 *
205 * Particularly read port count and initialize structure holding port
206 * information. If there are non-removable devices, start initializing them.
207 * This function is hub-specific and should be run only after the hub is
208 * configured using usb_hub_set_configuration function.
209 * @param hub_info hub representation
210 * @return error code
211 */
212static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
213 // get hub descriptor
214 usb_log_debug("creating serialized descriptor\n");
215 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
216 usb_hub_descriptor_t * descriptor;
217 int opResult;
218
219 size_t received_size;
220 opResult = usb_request_get_descriptor(hub_info->control_pipe,
221 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
222 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
223 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
224
225 if (opResult != EOK) {
226 usb_log_error("failed when receiving hub descriptor, "
227 "badcode = %d\n",
228 opResult);
229 free(serialized_descriptor);
230 return opResult;
231 }
232 usb_log_debug2("deserializing descriptor\n");
233 descriptor = usb_create_deserialized_hub_desriptor(
234 serialized_descriptor);
235 if (descriptor == NULL) {
236 usb_log_warning("could not deserialize descriptor \n");
237 return ENOMEM;
238 }
239 usb_log_debug("setting port count to %d\n", descriptor->ports_count);
240 hub_info->port_count = descriptor->ports_count;
241 /// \TODO this is not semantically correct
242 bool is_power_switched =
243 ((descriptor->hub_characteristics & 1) ==0);
244 bool has_individual_port_powering =
245 ((descriptor->hub_characteristics & 1) !=0);
246 hub_info->ports = malloc(
247 sizeof (usb_hub_port_t) * (hub_info->port_count + 1));
248 if(!hub_info->ports){
249 return ENOMEM;
250 }
251 size_t port;
252 for (port = 0; port < hub_info->port_count + 1; ++port) {
253 usb_hub_port_init(&hub_info->ports[port]);
254 }
255 if(is_power_switched){
256 usb_log_debug("is_power_switched\n");
257
258 if(!has_individual_port_powering){
259 usb_log_debug("!has_individual_port_powering\n");
260 opResult = usb_hub_set_feature(hub_info->control_pipe,
261 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
262 if (opResult != EOK) {
263 usb_log_error("cannot power hub: %s\n",
264 str_error(opResult));
265 }
266 }
267
268 for (port = 1; port <= hub_info->port_count; ++port) {
269 usb_log_debug("Powering port %zu.\n",port);
270 opResult = usb_hub_set_port_feature(hub_info->control_pipe,
271 port, USB_HUB_FEATURE_PORT_POWER);
272 if (opResult != EOK) {
273 usb_log_error("cannot power on port %zu: %s.\n",
274 port, str_error(opResult));
275 }
276 }
277
278 }else{
279 usb_log_debug("!is_power_switched, not going to be powered\n");
280 }
281 usb_log_debug2("freeing data\n");
282 free(descriptor);
283 return EOK;
284}
285
286/**
287 * Set configuration of hub
288 *
289 * Check whether there is at least one configuration and sets the first one.
290 * This function should be run prior to running any hub-specific action.
291 * @param hub_info hub representation
292 * @return error code
293 */
294static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
295 //device descriptor
296 usb_standard_device_descriptor_t *std_descriptor
297 = &hub_info->usb_device->descriptors.device;
298 usb_log_debug("hub has %d configurations\n",
299 std_descriptor->configuration_count);
300 if (std_descriptor->configuration_count < 1) {
301 usb_log_error("there are no configurations available\n");
302 return EINVAL;
303 }
304
305 usb_standard_configuration_descriptor_t *config_descriptor
306 = (usb_standard_configuration_descriptor_t *)
307 hub_info->usb_device->descriptors.configuration;
308
309 /* Set configuration. */
310 int opResult = usb_request_set_configuration(
311 &hub_info->usb_device->ctrl_pipe,
312 config_descriptor->configuration_number);
313
314 if (opResult != EOK) {
315 usb_log_error("Failed to set hub configuration: %s.\n",
316 str_error(opResult));
317 return opResult;
318 }
319 usb_log_debug("\tused configuration %d\n",
320 config_descriptor->configuration_number);
321
322 return EOK;
323}
324
325/**
326 * create and start fibril with hub control loop
327 *
328 * Before the fibril is started, the control pipe and host controller
329 * connection of the hub is open.
330 *
331 * @param hub_info hub representing structure
332 * @return error code
333 */
334static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info){
335 /*
336 * The processing will require opened control pipe and connection
337 * to the host controller.
338 * It is waste of resources but let's hope there will be less
339 * hubs than the phone limit.
340 * FIXME: with some proper locking over pipes and session
341 * auto destruction, this could work better.
342 */
343 int rc = usb_hc_connection_open(&hub_info->connection);
344 if (rc != EOK) {
345 //usb_pipe_end_session(hub_info->control_pipe);
346 usb_log_error("Failed to open connection to HC: %s.\n",
347 str_error(rc));
348 return rc;
349 }
350
351 rc = usb_device_auto_poll(hub_info->usb_device, 0,
352 hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
353 NULL, hub_info);
354 if (rc != EOK) {
355 usb_log_error("Failed to create polling fibril: %s.\n",
356 str_error(rc));
357 free(hub_info);
358 return rc;
359 }
360
361 usb_log_info("Controlling hub `%s' (%zu ports).\n",
362 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
363 return EOK;
364}
365
366//*********************************************
367//
368// change handling functions
369//
370//*********************************************
371
372
373/**
374 * process hub over current change
375 *
376 * This means either to power off the hub or power it on.
377 * @param hub_info hub instance
378 * @param status hub status bitmask
379 * @return error code
380 */
381static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
382 usb_hub_status_t status) {
383 int opResult;
384 if (usb_hub_is_status(status,USB_HUB_FEATURE_HUB_OVER_CURRENT)){
385 opResult = usb_hub_clear_feature(hub_info->control_pipe,
386 USB_HUB_FEATURE_HUB_LOCAL_POWER);
387 if (opResult != EOK) {
388 usb_log_error("cannot power off hub: %d\n",
389 opResult);
390 }
391 } else {
392 opResult = usb_hub_set_feature(hub_info->control_pipe,
393 USB_HUB_FEATURE_HUB_LOCAL_POWER);
394 if (opResult != EOK) {
395 usb_log_error("cannot power on hub: %d\n",
396 opResult);
397 }
398 }
399 return opResult;
400}
401
402/**
403 * process hub power change
404 *
405 * If the power has been lost, reestablish it.
406 * If it was reestablished, re-power all ports.
407 * @param hub_info hub instance
408 * @param status hub status bitmask
409 * @return error code
410 */
411static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
412 usb_hub_status_t status) {
413 int opResult = EOK;
414 if (!usb_hub_is_status(status,USB_HUB_FEATURE_HUB_LOCAL_POWER)) {
415 //restart power on hub
416 opResult = usb_hub_set_feature(hub_info->control_pipe,
417 USB_HUB_FEATURE_HUB_LOCAL_POWER);
418 if (opResult != EOK) {
419 usb_log_error("cannot power on hub: %d\n",
420 opResult);
421 }
422 } else {//power reestablished on hub- restart ports
423 size_t port;
424 for (port = 1; port <= hub_info->port_count; ++port) {
425 opResult = usb_hub_set_port_feature(
426 hub_info->control_pipe,
427 port, USB_HUB_FEATURE_PORT_POWER);
428 if (opResult != EOK) {
429 usb_log_error("Cannot power on port %zu: %s.\n",
430 port, str_error(opResult));
431 }
432 }
433 }
434 if(opResult!=EOK){
435 return opResult;//no feature clearing
436 }
437 opResult = usb_hub_clear_feature(hub_info->control_pipe,
438 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
439 if (opResult != EOK) {
440 usb_log_error("cannnot clear hub power change flag: "
441 "%d\n",
442 opResult);
443 }
444 return opResult;
445}
446
447/**
448 * process hub interrupts
449 *
450 * The change can be either in the over-current condition or
451 * local-power lost condition.
452 * @param hub_info hub instance
453 */
454static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info) {
455 usb_log_debug("global interrupt on a hub\n");
456 usb_pipe_t *pipe = hub_info->control_pipe;
457 int opResult;
458
459 usb_port_status_t status;
460 size_t rcvd_size;
461 usb_device_request_setup_packet_t request;
462 //int opResult;
463 usb_hub_set_hub_status_request(&request);
464 //endpoint 0
465
466 opResult = usb_pipe_control_read(
467 pipe,
468 &request, sizeof (usb_device_request_setup_packet_t),
469 &status, 4, &rcvd_size
470 );
471 if (opResult != EOK) {
472 usb_log_error("could not get hub status\n");
473 return;
474 }
475 if (rcvd_size != sizeof (usb_port_status_t)) {
476 usb_log_error("received status has incorrect size\n");
477 return;
478 }
479 //port reset
480 if (
481 usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
482 usb_process_hub_over_current(hub_info, status);
483 }
484 if (
485 usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
486 usb_process_hub_power_change(hub_info, status);
487 }
488}
489
490/**
491 * @}
492 */
Note: See TracBrowser for help on using the repository browser.