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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c50941f was c50941f, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Unused code removal

  • Property mode set to 100644
File size: 14.5 KB
RevLine 
[e080332]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 */
[281ebae]28/** @addtogroup drvusbhub
[e080332]29 * @{
30 */
31/** @file
32 * @brief usb hub main functionality
33 */
34
[eb1a2f4]35#include <ddf/driver.h>
[e080332]36#include <bool.h>
37#include <errno.h>
[4e8e1f5]38#include <str_error.h>
[e080332]39
[71ed4849]40#include <usb_iface.h>
[357a302]41#include <usb/ddfiface.h>
[e080332]42#include <usb/descriptor.h>
[4e8e1f5]43#include <usb/recognise.h>
[d81ef61c]44#include <usb/request.h>
[e080332]45#include <usb/classes/hub.h>
[cd4b184]46#include <stdio.h>
[e080332]47
48#include "usbhub.h"
49#include "usbhub_private.h"
50#include "port_status.h"
[f40a1e2]51#include "usb/usb.h"
[d81ef61c]52#include "usb/pipes.h"
[15b0432]53#include "usb/classes/classes.h"
[e080332]54
[df3ad97]55static int usb_hub_trigger_connecting_non_removable_devices(
56 usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
[e6223239]57
[15b0432]58
[e080332]59//*********************************************
60//
61// hub driver code, initialization
62//
63//*********************************************
64
[15b0432]65/**
[09daa8b]66 * create usb_hub_info_t structure
67 *
68 * Does only basic copying of known information into new structure.
69 * @param usb_dev usb device structure
70 * @return basic usb_hub_info_t structure
[15b0432]71 */
[09daa8b]72static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
73 usb_hub_info_t * result = usb_new(usb_hub_info_t);
74 if(!result) return NULL;
75 result->usb_device = usb_dev;
76 result->status_change_pipe = usb_dev->pipes[0].pipe;
77 result->control_pipe = &usb_dev->ctrl_pipe;
78 result->is_default_address_used = false;
79 return result;
80}
81
82/**
[e6223239]83 * Load hub-specific information into hub_info structure and process if needed
[09daa8b]84 *
85 * Particularly read port count and initialize structure holding port
[e6223239]86 * information. If there are non-removable devices, start initializing them.
[09daa8b]87 * This function is hub-specific and should be run only after the hub is
88 * configured using usb_hub_set_configuration function.
[df3ad97]89 * @param hub_info hub representation
[09daa8b]90 * @return error code
91 */
[e6223239]92static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info){
[09daa8b]93 // get hub descriptor
94 usb_log_debug("creating serialized descriptor\n");
95 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
96 usb_hub_descriptor_t * descriptor;
97
98 /* this was one fix of some bug, should not be needed anymore
[e6223239]99 * these lines allow to reset hub once more, it can be used as
100 * brute-force initialization for non-removable devices
[09daa8b]101 int opResult = usb_request_set_configuration(&result->endpoints.control, 1);
102 if(opResult!=EOK){
103 usb_log_error("could not set default configuration, errno %d",opResult);
[15b0432]104 return opResult;
105 }
[09daa8b]106 */
107 size_t received_size;
108 int opResult = usb_request_get_descriptor(&hub_info->usb_device->ctrl_pipe,
109 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
110 USB_DESCTYPE_HUB,
111 0, 0, serialized_descriptor,
112 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
113
114 if (opResult != EOK) {
115 usb_log_error("failed when receiving hub descriptor, badcode = %d\n",
[b495a93]116 opResult);
[09daa8b]117 free(serialized_descriptor);
[15b0432]118 return opResult;
119 }
[09daa8b]120 usb_log_debug2("deserializing descriptor\n");
121 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
122 if(descriptor==NULL){
123 usb_log_warning("could not deserialize descriptor \n");
[206f71a]124 return opResult;
[15b0432]125 }
[fbefd0e]126 usb_log_debug("setting port count to %d\n",descriptor->ports_count);
[09daa8b]127 hub_info->port_count = descriptor->ports_count;
[578a2547]128 hub_info->ports = malloc(sizeof(usb_hub_port_t) * (hub_info->port_count+1));
129 size_t port;
130 for (port = 0; port < hub_info->port_count + 1; port++) {
131 usb_hub_port_init(&hub_info->ports[port]);
[15b0432]132 }
[df3ad97]133 //handle non-removable devices
134 usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
[09daa8b]135 usb_log_debug2("freeing data\n");
136 free(serialized_descriptor);
137 free(descriptor->devices_removable);
138 free(descriptor);
[206f71a]139 return EOK;
[15b0432]140}
141/**
[09daa8b]142 * Set configuration of hub
143 *
144 * Check whether there is at least one configuration and sets the first one.
145 * This function should be run prior to running any hub-specific action.
[df3ad97]146 * @param hub_info hub representation
147 * @return error code
[15b0432]148 */
[09daa8b]149static int usb_hub_set_configuration(usb_hub_info_t * hub_info){
[15b0432]150 //device descriptor
[625f1ba]151 usb_standard_device_descriptor_t *std_descriptor
152 = &hub_info->usb_device->descriptors.device;
[fbefd0e]153 usb_log_debug("hub has %d configurations\n",
[625f1ba]154 std_descriptor->configuration_count);
155 if(std_descriptor->configuration_count<1){
[4d0c40b]156 usb_log_error("there are no configurations available\n");
[625f1ba]157 return EINVAL;
[15b0432]158 }
159
[d70e0a3c]160 usb_standard_configuration_descriptor_t *config_descriptor
[625f1ba]161 = (usb_standard_configuration_descriptor_t *)
162 hub_info->usb_device->descriptors.configuration;
[d70e0a3c]163
164 /* Set configuration. */
[625f1ba]165 int opResult = usb_request_set_configuration(
166 &hub_info->usb_device->ctrl_pipe,
[d70e0a3c]167 config_descriptor->configuration_number);
[15b0432]168
169 if (opResult != EOK) {
[d70e0a3c]170 usb_log_error("Failed to set hub configuration: %s.\n",
171 str_error(opResult));
[15b0432]172 return opResult;
173 }
[09daa8b]174 usb_log_debug("\tused configuration %d\n",
[d70e0a3c]175 config_descriptor->configuration_number);
[625f1ba]176
[15b0432]177 return EOK;
178}
179
180/**
[09daa8b]181 * Initialize hub device driver fibril
182 *
183 * Creates hub representation and fibril that periodically checks hub`s status.
184 * Hub representation is passed to the fibril.
185 * @param usb_dev generic usb device information
186 * @return error code
[15b0432]187 */
[09daa8b]188int usb_hub_add_device(usb_device_t * usb_dev){
189 if(!usb_dev) return EINVAL;
190 usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
191 //create hc connection
192 usb_log_debug("Initializing USB wire abstraction.\n");
193 int opResult = usb_hc_connection_initialize_from_device(
194 &hub_info->connection,
195 hub_info->usb_device->ddf_dev);
[15b0432]196 if(opResult != EOK){
[09daa8b]197 usb_log_error("could not initialize connection to device, errno %d\n",
[b495a93]198 opResult);
[09daa8b]199 free(hub_info);
200 return opResult;
[332f860]201 }
[09daa8b]202
[3954a63b]203 usb_pipe_start_session(hub_info->control_pipe);
[09daa8b]204 //set hub configuration
205 opResult = usb_hub_set_configuration(hub_info);
206 if(opResult!=EOK){
207 usb_log_error("could not set hub configuration, errno %d\n",opResult);
208 free(hub_info);
[e080332]209 return opResult;
210 }
[09daa8b]211 //get port count and create attached_devs
[e6223239]212 opResult = usb_hub_process_hub_specific_info(hub_info);
[09daa8b]213 if(opResult!=EOK){
214 usb_log_error("could not set hub configuration, errno %d\n",opResult);
215 free(hub_info);
216 return opResult;
[e080332]217 }
[3954a63b]218 usb_pipe_end_session(hub_info->control_pipe);
[e080332]219
[cd4b184]220
[09daa8b]221 /// \TODO what is this?
[625f1ba]222 usb_log_debug("Creating `hub' function.\n");
[09daa8b]223 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
224 fun_exposed, "hub");
[cc34f32f]225 assert(hub_fun != NULL);
226 hub_fun->ops = NULL;
227
228 int rc = ddf_fun_bind(hub_fun);
229 assert(rc == EOK);
230 rc = ddf_fun_add_to_class(hub_fun, "hub");
231 assert(rc == EOK);
232
[578a2547]233 /*
234 * The processing will require opened control pipe and connection
235 * to the host controller.
236 * It is waste of resources but let's hope there will be less
237 * hubs than the phone limit.
238 * FIXME: with some proper locking over pipes and session
239 * auto destruction, this could work better.
240 */
241 rc = usb_pipe_start_session(&usb_dev->ctrl_pipe);
242 if (rc != EOK) {
243 usb_log_error("Failed to start session on control pipe: %s.\n",
244 str_error(rc));
245 goto leave;
246 }
247 rc = usb_hc_connection_open(&hub_info->connection);
248 if (rc != EOK) {
249 usb_pipe_end_session(&usb_dev->ctrl_pipe);
250 usb_log_error("Failed to open connection to HC: %s.\n",
251 str_error(rc));
252 goto leave;
253 }
254
255 rc = usb_device_auto_poll(hub_info->usb_device, 0,
256 hub_port_changes_callback, ((hub_info->port_count+1) / 8) + 1,
257 NULL, hub_info);
258 if (rc != EOK) {
259 usb_log_error("Failed to create polling fibril: %s.\n",
260 str_error(rc));
261 free(hub_info);
262 return rc;
[cd4b184]263 }
[625f1ba]264
265 usb_log_info("Controlling hub `%s' (%d ports).\n",
266 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
[e080332]267 return EOK;
[578a2547]268
269leave:
270 free(hub_info);
271
272 return rc;
[e080332]273}
274
275
276//*********************************************
277//
[e6223239]278// hub driver code, main loop and port handling
[e080332]279//
280//*********************************************
281
[e6223239]282/**
[df3ad97]283 * triggers actions to connect non0removable devices
[e6223239]284 *
285 * This will trigger operations leading to activated non-removable device.
286 * Control pipe of the hub must be open fo communication.
[df3ad97]287 * @param hub hub representation
[e6223239]288 * @param descriptor usb hub descriptor
289 * @return error code
290 */
[df3ad97]291static int usb_hub_trigger_connecting_non_removable_devices(usb_hub_info_t * hub,
[e6223239]292 usb_hub_descriptor_t * descriptor)
293{
294 usb_log_info("attaching non-removable devices(if any)\n");
295 usb_device_request_setup_packet_t request;
296 int opResult;
297 size_t rcvd_size;
298 usb_port_status_t status;
299 uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
300 int port;
301 for(port=1;port<=descriptor->ports_count;++port){
302 bool is_non_removable =
303 ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
304 if(is_non_removable){
305 usb_log_debug("non-removable device on port %d\n",port);
306 usb_hub_set_port_status_request(&request, port);
307 opResult = usb_pipe_control_read(
308 hub->control_pipe,
309 &request, sizeof(usb_device_request_setup_packet_t),
310 &status, 4, &rcvd_size
311 );
312 if (opResult != EOK) {
313 usb_log_error("could not get port status of port %d errno:%d\n",
314 port, opResult);
315 return opResult;
316 }
[df3ad97]317 //set the status change bit, so it will be noticed in driver loop
[e6223239]318 if(usb_port_dev_connected(&status)){
[8123695a]319 usb_hub_set_disable_port_feature_request(&request, port,
320 USB_HUB_FEATURE_PORT_CONNECTION);
321 opResult = usb_pipe_control_read(
322 hub->control_pipe,
323 &request, sizeof(usb_device_request_setup_packet_t),
324 &status, 4, &rcvd_size
325 );
326 if (opResult != EOK) {
327 usb_log_warning(
328 "could not clear port connection on port %d errno:%d\n",
329 port, opResult);
330 }
331 usb_log_debug("cleared port connection\n");
[df3ad97]332 usb_hub_set_enable_port_feature_request(&request, port,
[8123695a]333 USB_HUB_FEATURE_PORT_ENABLE);
[df3ad97]334 opResult = usb_pipe_control_read(
335 hub->control_pipe,
336 &request, sizeof(usb_device_request_setup_packet_t),
337 &status, 4, &rcvd_size
338 );
339 if (opResult != EOK) {
340 usb_log_warning(
[8123695a]341 "could not set port enabled on port %d errno:%d\n",
[df3ad97]342 port, opResult);
343 }
[8123695a]344 usb_log_debug("port set to enabled - should lead to connection change\n");
[e6223239]345 }
346 }
347 }
[a9c7c6f]348 /// \TODO this is just a debug code
349 for(port=1;port<=descriptor->ports_count;++port){
350 bool is_non_removable =
351 ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
352 if(is_non_removable){
353 usb_log_debug("port %d is non-removable\n",port);
354 usb_port_status_t status;
355 size_t rcvd_size;
356 usb_device_request_setup_packet_t request;
357 //int opResult;
358 usb_hub_set_port_status_request(&request, port);
359 //endpoint 0
360 opResult = usb_pipe_control_read(
361 hub->control_pipe,
362 &request, sizeof(usb_device_request_setup_packet_t),
363 &status, 4, &rcvd_size
364 );
365 if (opResult != EOK) {
366 usb_log_error("could not get port status %d\n",opResult);
367 }
368 if (rcvd_size != sizeof (usb_port_status_t)) {
369 usb_log_error("received status has incorrect size\n");
370 }
371 //something connected/disconnected
372 if (usb_port_connect_change(&status)) {
373 usb_log_debug("some connection changed\n");
374 }
375 usb_log_debug("status: %s\n",usb_debug_str_buffer(
376 (uint8_t *)&status,4,4));
377 }
378 }
[e6223239]379 return EOK;
380}
381
382
[a83e138]383/**
384 * release default address used by given hub
385 *
386 * Also unsets hub->is_default_address_used. Convenience wrapper function.
387 * @note hub->connection MUST be open for communication
388 * @param hub hub representation
389 * @return error code
390 */
391static int usb_hub_release_default_address(usb_hub_info_t * hub){
392 int opResult = usb_hc_release_default_address(&hub->connection);
393 if(opResult!=EOK){
[09daa8b]394 usb_log_error("could not release default address, errno %d\n",opResult);
[a83e138]395 return opResult;
396 }
397 hub->is_default_address_used = false;
398 return EOK;
399}
400
[e080332]401/**
[df3ad97]402 * routine called when a device on port has been removed
403 *
404 * If the device on port had default address, it releases default address.
405 * Otherwise does not do anything, because DDF does not allow to remove device
406 * from it`s device tree.
407 * @param hub hub representation
408 * @param port port number, starting from 1
[e080332]409 */
[578a2547]410void usb_hub_removed_device(
[6bb83c7]411 usb_hub_info_t * hub,uint16_t port) {
[e93e319]412
[09daa8b]413 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
[e93e319]414 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
415 if(opResult != EOK){
[09daa8b]416 usb_log_warning("could not clear port-change-connection flag\n");
[e93e319]417 }
[f40a1e2]418 /** \TODO remove device from device manager - not yet implemented in
419 * devide manager
420 */
[6bb83c7]421
[e080332]422 //close address
[578a2547]423 if(hub->ports[port].attached_device.address >= 0){
[42a3a57]424 /*uncomment this code to use it when DDF allows device removal
[6bb83c7]425 opResult = usb_hc_unregister_device(
426 &hub->connection, hub->attached_devs[port].address);
[e080332]427 if(opResult != EOK) {
[103a3626]428 dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
[0cfc68f0]429 "removed device: %d", opResult);
[e080332]430 }
431 hub->attached_devs[port].address = 0;
[6bb83c7]432 hub->attached_devs[port].handle = 0;
[42a3a57]433 */
[e080332]434 }else{
[09daa8b]435 usb_log_warning("this is strange, disconnected device had no address\n");
[e080332]436 //device was disconnected before it`s port was reset - return default address
[a83e138]437 usb_hub_release_default_address(hub);
[e080332]438 }
439}
440
[cd4b184]441
442/**
[b495a93]443 * Process over current condition on port.
[cd4b184]444 *
445 * Turn off the power on the port.
446 *
[df3ad97]447 * @param hub hub representation
448 * @param port port number, starting from 1
[cd4b184]449 */
[578a2547]450void usb_hub_over_current( usb_hub_info_t * hub,
[cd4b184]451 uint16_t port){
452 int opResult;
[09daa8b]453 opResult = usb_hub_clear_port_feature(hub->control_pipe,
[cd4b184]454 port, USB_HUB_FEATURE_PORT_POWER);
455 if(opResult!=EOK){
[09daa8b]456 usb_log_error("cannot power off port %d; %d\n",
[cd4b184]457 port, opResult);
458 }
459}
460
[e080332]461
462/**
463 * @}
[71ed4849]464 */
Note: See TracBrowser for help on using the repository browser.