source: mainline/uspace/drv/usbhub/usbhub.c@ ff76509

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

nothing important

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