source: mainline/uspace/drv/usbhub/usbhub.c@ 5a2c42b

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

forgotten hub port powering

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