source: mainline/uspace/drv/usbhub/usbhub.c@ 3dba1ca

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

some more comments

  • Property mode set to 100644
File size: 28.8 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]55
[6c399765]56static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
[195890b]57 usb_speed_t speed);
[e6223239]58
[df3ad97]59static int usb_hub_trigger_connecting_non_removable_devices(
[195890b]60 usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
[e6223239]61
[df3ad97]62/**
63 * control loop running in hub`s fibril
64 *
65 * Hub`s fibril periodically asks for changes on hub and if needded calls
66 * change handling routine.
67 * @warning currently hub driver asks for changes once a second
68 * @param hub_info_param hub representation pointer
69 * @return zero
70 */
[195890b]71int usb_hub_control_loop(void * hub_info_param) {
72 usb_hub_info_t * hub_info = (usb_hub_info_t*) hub_info_param;
[42a3a57]73 int errorCode = EOK;
74
[195890b]75 while (errorCode == EOK) {
76 async_usleep(1000 * 1000 * 10); /// \TODO proper number once
[42a3a57]77 errorCode = usb_hub_check_hub_changes(hub_info);
[cd4b184]78 }
[195890b]79 usb_log_error("something in ctrl loop went wrong, errno %d\n",
80 errorCode);
[b495a93]81
[cd4b184]82 return 0;
83}
84
[040ab02]85/// \TODO set_port_feature use
[3dba1ca]86/// \TODO unmess code
[15b0432]87
[e080332]88//*********************************************
89//
90// hub driver code, initialization
91//
92//*********************************************
93
[15b0432]94/**
[09daa8b]95 * create usb_hub_info_t structure
96 *
97 * Does only basic copying of known information into new structure.
98 * @param usb_dev usb device structure
99 * @return basic usb_hub_info_t structure
[15b0432]100 */
[09daa8b]101static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
102 usb_hub_info_t * result = usb_new(usb_hub_info_t);
[195890b]103 if (!result) return NULL;
[09daa8b]104 result->usb_device = usb_dev;
105 result->status_change_pipe = usb_dev->pipes[0].pipe;
106 result->control_pipe = &usb_dev->ctrl_pipe;
107 result->is_default_address_used = false;
108 return result;
109}
110
111/**
[e6223239]112 * Load hub-specific information into hub_info structure and process if needed
[09daa8b]113 *
114 * Particularly read port count and initialize structure holding port
[e6223239]115 * information. If there are non-removable devices, start initializing them.
[09daa8b]116 * This function is hub-specific and should be run only after the hub is
117 * configured using usb_hub_set_configuration function.
[df3ad97]118 * @param hub_info hub representation
[09daa8b]119 * @return error code
120 */
[195890b]121static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
[09daa8b]122 // get hub descriptor
123 usb_log_debug("creating serialized descriptor\n");
124 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
125 usb_hub_descriptor_t * descriptor;
[040ab02]126 int opResult;
[09daa8b]127
128 /* this was one fix of some bug, should not be needed anymore
[e6223239]129 * these lines allow to reset hub once more, it can be used as
130 * brute-force initialization for non-removable devices
[040ab02]131 *
132 opResult = usb_request_set_configuration(hub_info->control_pipe,
[195890b]133 1);
134 if (opResult != EOK) {
135 usb_log_error("could not set default configuration, errno %d",
136 opResult);
[15b0432]137 return opResult;
[040ab02]138 }*/
[195890b]139
140
[09daa8b]141 size_t received_size;
[6c399765]142 opResult = usb_request_get_descriptor(&hub_info->usb_device->ctrl_pipe,
[195890b]143 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
144 USB_DESCTYPE_HUB,
145 0, 0, serialized_descriptor,
146 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
[09daa8b]147
148 if (opResult != EOK) {
[195890b]149 usb_log_error("failed when receiving hub descriptor, "
150 "badcode = %d\n",
151 opResult);
[09daa8b]152 free(serialized_descriptor);
[15b0432]153 return opResult;
154 }
[09daa8b]155 usb_log_debug2("deserializing descriptor\n");
156 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
[195890b]157 if (descriptor == NULL) {
[09daa8b]158 usb_log_warning("could not deserialize descriptor \n");
[206f71a]159 return opResult;
[15b0432]160 }
[195890b]161 usb_log_debug("setting port count to %d\n", descriptor->ports_count);
[09daa8b]162 hub_info->port_count = descriptor->ports_count;
163 hub_info->attached_devs = (usb_hc_attached_device_t*)
[195890b]164 malloc((hub_info->port_count + 1) *
165 sizeof (usb_hc_attached_device_t)
166 );
[09daa8b]167 int i;
[195890b]168 for (i = 0; i < hub_info->port_count + 1; ++i) {
169 hub_info->attached_devs[i].handle = 0;
170 hub_info->attached_devs[i].address = 0;
[15b0432]171 }
[df3ad97]172 //handle non-removable devices
173 usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
[09daa8b]174 usb_log_debug2("freeing data\n");
175 free(serialized_descriptor);
[6c399765]176 hub_info->descriptor = descriptor;
177 hub_info->not_initialized_non_removables =
[195890b]178 (uint8_t*) malloc((hub_info->port_count + 8) / 8);
[6c399765]179 memcpy(hub_info->not_initialized_non_removables,
180 descriptor->devices_removable,
[195890b]181 (hub_info->port_count + 8) / 8
[6c399765]182 );
183
184 //free(descriptor->devices_removable);
185 //free(descriptor);
[206f71a]186 return EOK;
[15b0432]187}
[195890b]188
[15b0432]189/**
[09daa8b]190 * Set configuration of hub
191 *
192 * Check whether there is at least one configuration and sets the first one.
193 * This function should be run prior to running any hub-specific action.
[df3ad97]194 * @param hub_info hub representation
195 * @return error code
[15b0432]196 */
[195890b]197static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
[15b0432]198 //device descriptor
[625f1ba]199 usb_standard_device_descriptor_t *std_descriptor
[195890b]200 = &hub_info->usb_device->descriptors.device;
[fbefd0e]201 usb_log_debug("hub has %d configurations\n",
[195890b]202 std_descriptor->configuration_count);
203 if (std_descriptor->configuration_count < 1) {
[4d0c40b]204 usb_log_error("there are no configurations available\n");
[625f1ba]205 return EINVAL;
[15b0432]206 }
207
[d70e0a3c]208 usb_standard_configuration_descriptor_t *config_descriptor
[195890b]209 = (usb_standard_configuration_descriptor_t *)
210 hub_info->usb_device->descriptors.configuration;
[d70e0a3c]211
212 /* Set configuration. */
[625f1ba]213 int opResult = usb_request_set_configuration(
[195890b]214 &hub_info->usb_device->ctrl_pipe,
215 config_descriptor->configuration_number);
[15b0432]216
217 if (opResult != EOK) {
[d70e0a3c]218 usb_log_error("Failed to set hub configuration: %s.\n",
[195890b]219 str_error(opResult));
[15b0432]220 return opResult;
221 }
[09daa8b]222 usb_log_debug("\tused configuration %d\n",
[195890b]223 config_descriptor->configuration_number);
[625f1ba]224
[15b0432]225 return EOK;
226}
227
228/**
[09daa8b]229 * Initialize hub device driver fibril
230 *
231 * Creates hub representation and fibril that periodically checks hub`s status.
232 * Hub representation is passed to the fibril.
233 * @param usb_dev generic usb device information
234 * @return error code
[15b0432]235 */
[195890b]236int usb_hub_add_device(usb_device_t * usb_dev) {
237 if (!usb_dev) return EINVAL;
[09daa8b]238 usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
239 //create hc connection
240 usb_log_debug("Initializing USB wire abstraction.\n");
241 int opResult = usb_hc_connection_initialize_from_device(
[195890b]242 &hub_info->connection,
243 hub_info->usb_device->ddf_dev);
244 if (opResult != EOK) {
245 usb_log_error("could not initialize connection to device, "
246 "errno %d\n",
247 opResult);
[09daa8b]248 free(hub_info);
249 return opResult;
[332f860]250 }
[195890b]251
[3954a63b]252 usb_pipe_start_session(hub_info->control_pipe);
[09daa8b]253 //set hub configuration
254 opResult = usb_hub_set_configuration(hub_info);
[195890b]255 if (opResult != EOK) {
256 usb_log_error("could not set hub configuration, errno %d\n",
257 opResult);
[09daa8b]258 free(hub_info);
[e080332]259 return opResult;
260 }
[09daa8b]261 //get port count and create attached_devs
[e6223239]262 opResult = usb_hub_process_hub_specific_info(hub_info);
[195890b]263 if (opResult != EOK) {
264 usb_log_error("could not set hub configuration, errno %d\n",
265 opResult);
[09daa8b]266 free(hub_info);
267 return opResult;
[e080332]268 }
[3954a63b]269 usb_pipe_end_session(hub_info->control_pipe);
[e080332]270
[cd4b184]271
[09daa8b]272 /// \TODO what is this?
[625f1ba]273 usb_log_debug("Creating `hub' function.\n");
[09daa8b]274 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
[195890b]275 fun_exposed, "hub");
[cc34f32f]276 assert(hub_fun != NULL);
277 hub_fun->ops = NULL;
278
279 int rc = ddf_fun_bind(hub_fun);
280 assert(rc == EOK);
281 rc = ddf_fun_add_to_class(hub_fun, "hub");
282 assert(rc == EOK);
283
[09daa8b]284 //create fibril for the hub control loop
[cd4b184]285 fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
286 if (fid == 0) {
[195890b]287 usb_log_error("failed to start monitoring fibril for new"
288 " hub.\n");
[cd4b184]289 return ENOMEM;
290 }
291 fibril_add_ready(fid);
[625f1ba]292 usb_log_debug("Hub fibril created.\n");
293
294 usb_log_info("Controlling hub `%s' (%d ports).\n",
[195890b]295 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
[e080332]296 return EOK;
297}
298
299
300//*********************************************
301//
[e6223239]302// hub driver code, main loop and port handling
[e080332]303//
304//*********************************************
305
[e6223239]306/**
[df3ad97]307 * triggers actions to connect non0removable devices
[e6223239]308 *
309 * This will trigger operations leading to activated non-removable device.
310 * Control pipe of the hub must be open fo communication.
[df3ad97]311 * @param hub hub representation
[e6223239]312 * @param descriptor usb hub descriptor
313 * @return error code
314 */
[6c399765]315static int usb_hub_trigger_connecting_non_removable_devices(
[195890b]316 usb_hub_info_t * hub,
317 usb_hub_descriptor_t * descriptor) {
[e6223239]318 usb_log_info("attaching non-removable devices(if any)\n");
[6c399765]319 //usb_device_request_setup_packet_t request;
[e6223239]320 int opResult;
[6c399765]321 //size_t rcvd_size;
322 //usb_port_status_t status;
[e6223239]323 uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
324 int port;
[6c399765]325
326 opResult = usb_request_set_configuration(hub->control_pipe,
[195890b]327 1);
328 if (opResult != EOK) {
329 usb_log_error("could not set default configuration, errno %d",
330 opResult);
[6c399765]331 return opResult;
332 }
333#if 0
[195890b]334 for (port = 1; port <= descriptor->ports_count; ++port) {
[e6223239]335 bool is_non_removable =
[195890b]336 ((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
337 if (is_non_removable) {
338 usb_log_debug("non-removable device on port %d\n", port);
[e6223239]339 usb_hub_set_port_status_request(&request, port);
340 opResult = usb_pipe_control_read(
[195890b]341 hub->control_pipe,
342 &request,
343 sizeof (usb_device_request_setup_packet_t),
344 &status, 4, &rcvd_size
345 );
[e6223239]346 if (opResult != EOK) {
[195890b]347 usb_log_error("could not get port status of "
348 "port %d errno:%d\n",
349 port, opResult);
[e6223239]350 return opResult;
351 }
[6c399765]352 //try to reset port
[195890b]353 if (usb_port_dev_connected(&status) || true) {
354 usb_hub_set_enable_port_feature_request(
355 &request, port,
356 USB_HUB_FEATURE_PORT_RESET);
[6c399765]357 opResult = usb_pipe_control_read(
[195890b]358 hub->control_pipe,
359 &request,
360 sizeof (usb_device_request_setup_packet_t),
361 &status, 4, &rcvd_size
362 );
[6c399765]363 if (opResult != EOK) {
364 usb_log_warning(
[195890b]365 "could not reset port %d "
366 "errno:%d\n",
367 port, opResult);
[6c399765]368 }
[195890b]369 usb_log_debug("port reset, should look like "
370 "%d,x%x\n",
371 (1 << USB_HUB_FEATURE_PORT_RESET),
372 (1 << USB_HUB_FEATURE_PORT_RESET)
373 );
[4fbcd2a]374 }
[195890b]375 //set the status change bit, so it will be noticed
376 //in driver loop
377 if (usb_port_dev_connected(&status) && false) {
378 usb_hub_set_disable_port_feature_request(
379 &request, port,
380 USB_HUB_FEATURE_PORT_CONNECTION);
[8123695a]381 opResult = usb_pipe_control_read(
[195890b]382 hub->control_pipe,
383 &request,
384 sizeof (usb_device_request_setup_packet_t),
385 &status, 4, &rcvd_size
386 );
[8123695a]387 if (opResult != EOK) {
388 usb_log_warning(
[195890b]389 "could not clear port "
390 "connection on port %d "
391 "errno:%d\n",
392 port, opResult);
[8123695a]393 }
394 usb_log_debug("cleared port connection\n");
[195890b]395 usb_hub_set_enable_port_feature_request(&request,
396 port,
397 USB_HUB_FEATURE_PORT_ENABLE);
[df3ad97]398 opResult = usb_pipe_control_read(
[195890b]399 hub->control_pipe,
400 &request,
401 sizeof (usb_device_request_setup_packet_t),
402 &status, 4, &rcvd_size
403 );
[df3ad97]404 if (opResult != EOK) {
405 usb_log_warning(
[195890b]406 "could not set port enabled "
407 "on port %d errno:%d\n",
408 port, opResult);
[df3ad97]409 }
[195890b]410 usb_log_debug("port set to enabled - "
411 "should lead to connection change\n");
[6c399765]412 }
[e6223239]413 }
414 }
[6c399765]415#endif
416
[a9c7c6f]417 /// \TODO this is just a debug code
[195890b]418 for (port = 1; port <= descriptor->ports_count; ++port) {
[a9c7c6f]419 bool is_non_removable =
[195890b]420 ((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
421 if (is_non_removable) {
422 usb_log_debug("CHECKING port %d is non-removable\n",
423 port);
[a9c7c6f]424 usb_port_status_t status;
425 size_t rcvd_size;
426 usb_device_request_setup_packet_t request;
427 //int opResult;
428 usb_hub_set_port_status_request(&request, port);
429 //endpoint 0
430 opResult = usb_pipe_control_read(
[195890b]431 hub->control_pipe,
432 &request,
433 sizeof (usb_device_request_setup_packet_t),
434 &status, 4, &rcvd_size
435 );
[a9c7c6f]436 if (opResult != EOK) {
[195890b]437 usb_log_error("could not get port status %d\n",
438 opResult);
[a9c7c6f]439 }
440 if (rcvd_size != sizeof (usb_port_status_t)) {
[195890b]441 usb_log_error("received status has incorrect"
442 " size\n");
[a9c7c6f]443 }
444 //something connected/disconnected
445 if (usb_port_connect_change(&status)) {
446 usb_log_debug("some connection changed\n");
447 }
[195890b]448 usb_log_debug("status: %s\n", usb_debug_str_buffer(
449 (uint8_t *) & status, 4, 4));
[a9c7c6f]450 }
451 }
[6c399765]452
[e6223239]453 return EOK;
454}
455
[a83e138]456/**
457 * release default address used by given hub
458 *
459 * Also unsets hub->is_default_address_used. Convenience wrapper function.
460 * @note hub->connection MUST be open for communication
461 * @param hub hub representation
462 * @return error code
463 */
[195890b]464static int usb_hub_release_default_address(usb_hub_info_t * hub) {
[a83e138]465 int opResult = usb_hc_release_default_address(&hub->connection);
[195890b]466 if (opResult != EOK) {
467 usb_log_error("could not release default address, errno %d\n",
468 opResult);
[a83e138]469 return opResult;
470 }
471 hub->is_default_address_used = false;
472 return EOK;
473}
474
[e080332]475/**
[f40a1e2]476 * Reset the port with new device and reserve the default address.
[df3ad97]477 * @param hub hub representation
478 * @param port port number, starting from 1
479 * @param speed transfer speed of attached device, one of low, full or high
[6c399765]480 * @return error code
[e080332]481 */
[6c399765]482static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
[195890b]483 usb_speed_t speed) {
[a83e138]484 //if this hub already uses default address, it cannot request it once more
[195890b]485 if (hub->is_default_address_used) return EREFUSED;
[fbefd0e]486 usb_log_debug("some connection changed\n");
[09daa8b]487 assert(hub->control_pipe->hc_phone);
488 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
[195890b]489 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
490 if (opResult != EOK) {
[09daa8b]491 usb_log_warning("could not clear port-change-connection flag\n");
[e93e319]492 }
[e080332]493 usb_device_request_setup_packet_t request;
[195890b]494
[e080332]495 //get default address
[42a3a57]496 opResult = usb_hc_reserve_default_address(&hub->connection, speed);
[195890b]497
[e080332]498 if (opResult != EOK) {
[195890b]499 usb_log_warning("cannot assign default address, it is probably "
500 "used %d\n",
501 opResult);
[6c399765]502 return opResult;
[e080332]503 }
[a83e138]504 hub->is_default_address_used = true;
[e080332]505 //reset port
506 usb_hub_set_reset_port_request(&request, port);
[3954a63b]507 opResult = usb_pipe_control_write(
[195890b]508 hub->control_pipe,
509 &request, sizeof (usb_device_request_setup_packet_t),
510 NULL, 0
511 );
[e080332]512 if (opResult != EOK) {
[195890b]513 usb_log_error("something went wrong when reseting a port %d\n",
514 opResult);
[a83e138]515 usb_hub_release_default_address(hub);
[e080332]516 }
[6c399765]517 return opResult;
[e080332]518}
519
520/**
[f40a1e2]521 * Finalize adding new device after port reset
[df3ad97]522 *
523 * Set device`s address and start it`s driver.
524 * @param hub hub representation
525 * @param port port number, starting from 1
526 * @param speed transfer speed of attached device, one of low, full or high
[e080332]527 */
[195890b]528static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
529 uint16_t port, usb_speed_t speed) {
[e080332]530
531 int opResult;
[fbefd0e]532 usb_log_debug("finalizing add device\n");
[09daa8b]533 opResult = usb_hub_clear_port_feature(hub->control_pipe,
[195890b]534 port, USB_HUB_FEATURE_C_PORT_RESET);
[6bb83c7]535
[e080332]536 if (opResult != EOK) {
[09daa8b]537 usb_log_error("failed to clear port reset feature\n");
[a83e138]538 usb_hub_release_default_address(hub);
[e080332]539 return;
540 }
[6bb83c7]541 //create connection to device
[a372663]542 usb_pipe_t new_device_pipe;
[6bb83c7]543 usb_device_connection_t new_device_connection;
544 usb_device_connection_initialize_on_default_address(
[195890b]545 &new_device_connection,
546 &hub->connection
547 );
[3954a63b]548 usb_pipe_initialize_default_control(
[195890b]549 &new_device_pipe,
550 &new_device_connection);
[3954a63b]551 usb_pipe_probe_default_control(&new_device_pipe);
[6bb83c7]552
553 /* Request address from host controller. */
554 usb_address_t new_device_address = usb_hc_request_address(
[195890b]555 &hub->connection,
556 speed
557 );
[e080332]558 if (new_device_address < 0) {
[09daa8b]559 usb_log_error("failed to get free USB address\n");
[e080332]560 opResult = new_device_address;
[a83e138]561 usb_hub_release_default_address(hub);
[e080332]562 return;
563 }
[195890b]564 usb_log_debug("setting new address %d\n", new_device_address);
[6bb83c7]565 //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
566 // new_device_address);
[3954a63b]567 usb_pipe_start_session(&new_device_pipe);
[195890b]568 opResult = usb_request_set_address(&new_device_pipe,
569 new_device_address);
[3954a63b]570 usb_pipe_end_session(&new_device_pipe);
[e080332]571 if (opResult != EOK) {
[195890b]572 usb_log_error("could not set address for new device %d\n",
573 opResult);
[a83e138]574 usb_hub_release_default_address(hub);
[e080332]575 return;
576 }
577
[6bb83c7]578 //opResult = usb_hub_release_default_address(hc);
[a83e138]579 opResult = usb_hub_release_default_address(hub);
[195890b]580 if (opResult != EOK) {
[e080332]581 return;
582 }
583
584 devman_handle_t child_handle;
[6bb83c7]585 //??
[195890b]586 opResult = usb_device_register_child_in_devman(new_device_address,
587 hub->connection.hc_handle, hub->usb_device->ddf_dev,
588 &child_handle,
589 NULL, NULL, NULL);
[6bb83c7]590
[e080332]591 if (opResult != EOK) {
[195890b]592 usb_log_error("could not start driver for new device %d\n",
593 opResult);
[e080332]594 return;
595 }
[6bb83c7]596 hub->attached_devs[port].handle = child_handle;
[e080332]597 hub->attached_devs[port].address = new_device_address;
598
[6bb83c7]599 //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
600 opResult = usb_hc_register_device(
[195890b]601 &hub->connection,
602 &hub->attached_devs[port]);
[e080332]603 if (opResult != EOK) {
[195890b]604 usb_log_error("could not assign address of device in hcd %d\n",
605 opResult);
[e080332]606 return;
607 }
[fbefd0e]608 usb_log_info("Detected new device on `%s' (port %d), " \
609 "address %d (handle %llu).\n",
[195890b]610 hub->usb_device->ddf_dev->name, (int) port,
611 new_device_address, child_handle);
[e080332]612}
613
614/**
[df3ad97]615 * routine called when a device on port has been removed
616 *
617 * If the device on port had default address, it releases default address.
618 * Otherwise does not do anything, because DDF does not allow to remove device
619 * from it`s device tree.
620 * @param hub hub representation
621 * @param port port number, starting from 1
[e080332]622 */
623static void usb_hub_removed_device(
[195890b]624 usb_hub_info_t * hub, uint16_t port) {
[e93e319]625
[09daa8b]626 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
[195890b]627 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
628 if (opResult != EOK) {
[09daa8b]629 usb_log_warning("could not clear port-change-connection flag\n");
[e93e319]630 }
[f40a1e2]631 /** \TODO remove device from device manager - not yet implemented in
632 * devide manager
633 */
[195890b]634
[e080332]635 //close address
[195890b]636 if (hub->attached_devs[port].address != 0) {
[42a3a57]637 /*uncomment this code to use it when DDF allows device removal
[6bb83c7]638 opResult = usb_hc_unregister_device(
[195890b]639 &hub->connection,
640 hub->attached_devs[port].address);
[e080332]641 if(opResult != EOK) {
[195890b]642 dprintf(USB_LOG_LEVEL_WARNING, "could not release "
643 "address of "
[0cfc68f0]644 "removed device: %d", opResult);
[e080332]645 }
646 hub->attached_devs[port].address = 0;
[6bb83c7]647 hub->attached_devs[port].handle = 0;
[42a3a57]648 */
[195890b]649 } else {
650 usb_log_warning("this is strange, disconnected device had "
651 "no address\n");
652 //device was disconnected before it`s port was reset -
653 //return default address
[a83e138]654 usb_hub_release_default_address(hub);
[e080332]655 }
656}
657
[cd4b184]658/**
[b495a93]659 * Process over current condition on port.
[195890b]660 *
[cd4b184]661 * Turn off the power on the port.
662 *
[df3ad97]663 * @param hub hub representation
664 * @param port port number, starting from 1
[cd4b184]665 */
[040ab02]666static void usb_hub_port_over_current(usb_hub_info_t * hub,
667 uint16_t port, uint32_t status) {
668 /// \todo no, this is not proper sollution
669 /// get affected ports
670 /// power them off
671 /// wait until there over-current is cleared
672 /// power them on
673
[cd4b184]674 int opResult;
[040ab02]675 if(usb_port_over_current(&status)){
676 opResult = usb_hub_clear_port_feature(hub->control_pipe,
677 port, USB_HUB_FEATURE_PORT_POWER);
678 if (opResult != EOK) {
679 usb_log_error("cannot power off port %d; %d\n",
680 port, opResult);
681 }
682 }else{
683 opResult = usb_hub_set_port_feature(hub->control_pipe,
684 port, USB_HUB_FEATURE_PORT_POWER);
685 if (opResult != EOK) {
686 usb_log_error("cannot power on port %d; %d\n",
687 port, opResult);
688 }
[cd4b184]689 }
690}
691
[e080332]692/**
[f40a1e2]693 * Process interrupts on given hub port
[df3ad97]694 *
695 * Accepts connection, over current and port reset change.
696 * @param hub hub representation
697 * @param port port number, starting from 1
[e080332]698 */
[195890b]699static void usb_hub_process_interrupt(usb_hub_info_t * hub,
700 uint16_t port) {
[09daa8b]701 usb_log_debug("interrupt at port %d\n", port);
[e080332]702 //determine type of change
[a372663]703 usb_pipe_t *pipe = hub->control_pipe;
[195890b]704
[cd4b184]705 int opResult;
[d81ef61c]706
[e080332]707 usb_port_status_t status;
708 size_t rcvd_size;
709 usb_device_request_setup_packet_t request;
[d81ef61c]710 //int opResult;
[e080332]711 usb_hub_set_port_status_request(&request, port);
712 //endpoint 0
713
[3954a63b]714 opResult = usb_pipe_control_read(
[195890b]715 pipe,
716 &request, sizeof (usb_device_request_setup_packet_t),
717 &status, 4, &rcvd_size
718 );
[e080332]719 if (opResult != EOK) {
[09daa8b]720 usb_log_error("could not get port status\n");
[e080332]721 return;
722 }
723 if (rcvd_size != sizeof (usb_port_status_t)) {
[09daa8b]724 usb_log_error("received status has incorrect size\n");
[e080332]725 return;
726 }
727 //something connected/disconnected
728 if (usb_port_connect_change(&status)) {
[8123695a]729 usb_log_debug("connection change on port\n");
[e080332]730 if (usb_port_dev_connected(&status)) {
[fbefd0e]731 usb_log_debug("some connection changed\n");
[195890b]732 usb_hub_init_add_device(hub, port,
733 usb_port_speed(&status));
[e080332]734 } else {
[d81ef61c]735 usb_hub_removed_device(hub, port);
[e080332]736 }
737 }
[cd4b184]738 //over current
739 if (usb_port_overcurrent_change(&status)) {
740 //check if it was not auto-resolved
[8123695a]741 usb_log_debug("overcurrent change on port\n");
[040ab02]742 usb_hub_port_over_current(hub, port, status);
[cd4b184]743 }
[e080332]744 //port reset
745 if (usb_port_reset_completed(&status)) {
[fbefd0e]746 usb_log_debug("port reset complete\n");
[e080332]747 if (usb_port_enabled(&status)) {
[195890b]748 usb_hub_finalize_add_device(hub, port,
749 usb_port_speed(&status));
[e080332]750 } else {
[195890b]751 usb_log_warning("port reset, but port still not "
752 "enabled\n");
[e080332]753 }
754 }
[195890b]755 usb_log_debug("status x%x : %d\n ", status, status);
[e080332]756
757 usb_port_set_connect_change(&status, false);
758 usb_port_set_reset(&status, false);
759 usb_port_set_reset_completed(&status, false);
760 usb_port_set_dev_connected(&status, false);
[195890b]761 if (status >> 16) {
[09daa8b]762 usb_log_info("there was some unsupported change on port %d: %X\n",
[195890b]763 port, status);
[f40a1e2]764
[e080332]765 }
766}
767
[3dba1ca]768/**
769 * process hub over current change
770 *
771 * This means either to power off the hub or power it on.
772 * @param hub_info hub instance
773 * @param status hub status bitmask
774 * @return error code
775 */
[040ab02]776static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
777 usb_hub_status_t status)
778{
779 int opResult;
780 if(usb_hub_over_current(&status)){
781 opResult = usb_hub_clear_feature(hub_info->control_pipe,
782 USB_HUB_FEATURE_PORT_POWER);
783 if (opResult != EOK) {
784 usb_log_error("cannot power off hub: %d\n",
785 opResult);
786 }
787 }else{
788 opResult = usb_hub_set_feature(hub_info->control_pipe,
789 USB_HUB_FEATURE_PORT_POWER);
790 if (opResult != EOK) {
791 usb_log_error("cannot power on hub: %d\n",
792 opResult);
793 }
794 }
795 return opResult;
796}
797
[3dba1ca]798/**
799 * process hub power change
800 *
801 * If the power has been lost, reestablish it.
802 * If it was reestablished, re-power all ports.
803 * @param hub_info hub instance
804 * @param status hub status bitmask
805 * @return error code
806 */
[040ab02]807static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
808 usb_hub_status_t status)
809{
810 int opResult;
811 if(usb_hub_local_power_lost(&status)){
812 //restart power on hub
813 opResult = usb_hub_set_feature(hub_info->control_pipe,
814 USB_HUB_FEATURE_PORT_POWER);
815 if (opResult != EOK) {
816 usb_log_error("cannot power on hub: %d\n",
817 opResult);
818 }
819 }else{//power reestablished on hub- restart ports
820 int port;
821 for(port=0;port<hub_info->port_count;++port){
822 opResult = usb_hub_set_port_feature(
823 hub_info->control_pipe,
824 port, USB_HUB_FEATURE_PORT_POWER);
825 if (opResult != EOK) {
826 usb_log_error("cannot power on port %d; %d\n",
827 port, opResult);
828 }
829 }
830 }
831 return opResult;
832}
833
[3dba1ca]834/**
835 * process hub interrupts
836 *
837 * The change can be either in the over-current condition or
838 * local-power lost condition.
839 * @param hub_info hub instance
840 */
[040ab02]841static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info){
842 usb_log_debug("global interrupt on a hub\n");
843 usb_pipe_t *pipe = hub_info->control_pipe;
844 int opResult;
845
846 usb_port_status_t status;
847 size_t rcvd_size;
848 usb_device_request_setup_packet_t request;
849 //int opResult;
850 usb_hub_set_hub_status_request(&request);
851 //endpoint 0
852
853 opResult = usb_pipe_control_read(
854 pipe,
855 &request, sizeof (usb_device_request_setup_packet_t),
856 &status, 4, &rcvd_size
857 );
858 if (opResult != EOK) {
859 usb_log_error("could not get hub status\n");
860 return;
861 }
862 if (rcvd_size != sizeof (usb_port_status_t)) {
863 usb_log_error("received status has incorrect size\n");
864 return;
865 }
866 //port reset
867 if (usb_hub_over_current_change(&status)) {
868 usb_process_hub_over_current(hub_info,status);
869 }
870 if (usb_hub_local_power_change(&status)) {
871 usb_process_hub_power_change(hub_info,status);
872 }
873}
874
[3dba1ca]875/**
876 * this is an attempt to initialize non-removable devices in the hub
877 *
878 * @param hub_info hub instance
879 * @param port port number, counting from 1
880 * @return error code
881 */
[6c399765]882static int initialize_non_removable(usb_hub_info_t * hub_info,
[195890b]883 unsigned int port) {
[6c399765]884 int opResult;
885 usb_log_debug("there is not pluged in non-removable device on "
[195890b]886 "port %d\n", port
[6c399765]887 );
888 //usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
889 usb_port_status_t status;
890 size_t rcvd_size;
891 usb_device_request_setup_packet_t request;
892 //int opResult;
893 usb_hub_set_port_status_request(&request, port);
894 //endpoint 0
895
896 opResult = usb_pipe_control_read(
[195890b]897 hub_info->control_pipe,
898 &request, sizeof (usb_device_request_setup_packet_t),
899 &status, 4, &rcvd_size
900 );
[6c399765]901 if (opResult != EOK) {
[195890b]902 usb_log_error("could not get port status %d\n", opResult);
[6c399765]903 return opResult;
904 }
905 if (rcvd_size != sizeof (usb_port_status_t)) {
906 usb_log_error("received status has incorrect size\n");
907 return opResult;
908 }
[195890b]909 usb_log_debug("port status %d, x%x\n", status, status);
910 if (usb_port_dev_connected(&status)) {
[6c399765]911 usb_log_debug("there is connected device on this port\n");
912 }
[195890b]913 if (!hub_info->is_default_address_used)
914 usb_hub_init_add_device(hub_info, port,
915 usb_port_speed(&status));
[6c399765]916 return opResult;
917}
918
[f40a1e2]919/**
[df3ad97]920 * check changes on hub
921 *
922 * Handles changes on each port with a status change.
923 * @param hub_info hub representation
924 * @return error code
[e080332]925 */
[195890b]926int usb_hub_check_hub_changes(usb_hub_info_t * hub_info) {
[cd4b184]927 int opResult;
[3954a63b]928 opResult = usb_pipe_start_session(
[195890b]929 hub_info->status_change_pipe);
930 //this might not be necessary - if all non-removables are ok, it is
931 //not needed here
[6c399765]932 opResult = usb_pipe_start_session(hub_info->control_pipe);
[195890b]933 if (opResult != EOK) {
[09daa8b]934 usb_log_error("could not initialize communication for hub; %d\n",
[195890b]935 opResult);
[42a3a57]936 return opResult;
[cd4b184]937 }
938
939 size_t port_count = hub_info->port_count;
[6c399765]940 //first check non-removable devices
941 {
[195890b]942 unsigned int port;
943 for (port = 1; port <= port_count; ++port) {
944 bool is_non_removable =
945 hub_info->not_initialized_non_removables[port/8]
946 & (1 << (port % 8));
947 if (is_non_removable) {
948 opResult = initialize_non_removable(hub_info,
949 port);
950 }
[6c399765]951 }
952 }
953
[e080332]954
[cd4b184]955 /// FIXME: count properly
[195890b]956 size_t byte_length = ((port_count + 1) / 8) + 1;
957 void *change_bitmap = malloc(byte_length);
[cd4b184]958 size_t actual_size;
[e080332]959
[cd4b184]960 /*
961 * Send the request.
962 */
[3954a63b]963 opResult = usb_pipe_read(
[195890b]964 hub_info->status_change_pipe,
965 change_bitmap, byte_length, &actual_size
966 );
[e080332]967
[cd4b184]968 if (opResult != EOK) {
[5097bed4]969 free(change_bitmap);
[195890b]970 usb_log_warning("something went wrong while getting the"
971 "status of hub\n");
[3954a63b]972 usb_pipe_end_session(hub_info->status_change_pipe);
[42a3a57]973 return opResult;
[cd4b184]974 }
975 unsigned int port;
[195890b]976
977 if (opResult != EOK) {
978 usb_log_error("could not start control pipe session %d\n",
979 opResult);
[3954a63b]980 usb_pipe_end_session(hub_info->status_change_pipe);
[42a3a57]981 return opResult;
[dff940f8]982 }
983 opResult = usb_hc_connection_open(&hub_info->connection);
[195890b]984 if (opResult != EOK) {
[09daa8b]985 usb_log_error("could not start host controller session %d\n",
[195890b]986 opResult);
[3954a63b]987 usb_pipe_end_session(hub_info->control_pipe);
988 usb_pipe_end_session(hub_info->status_change_pipe);
[42a3a57]989 return opResult;
[dff940f8]990 }
[cd4b184]991
992 ///todo, opresult check, pre obe konekce
[040ab02]993 bool interrupt;
994 interrupt = ((uint8_t*)change_bitmap)[0] & 1;
995 if(interrupt){
996 usb_hub_process_global_interrupt(hub_info);
997 }
[195890b]998 for (port = 1; port < port_count + 1; ++port) {
[040ab02]999 interrupt =
1000 ((uint8_t*) change_bitmap)[port / 8] & (1<<(port % 8));
[cd4b184]1001 if (interrupt) {
1002 usb_hub_process_interrupt(
[195890b]1003 hub_info, port);
[cd4b184]1004 }
1005 }
[040ab02]1006 /// \todo check hub status
[cd4b184]1007 usb_hc_connection_close(&hub_info->connection);
[3954a63b]1008 usb_pipe_end_session(hub_info->control_pipe);
1009 usb_pipe_end_session(hub_info->status_change_pipe);
[cd4b184]1010 free(change_bitmap);
[42a3a57]1011 return EOK;
[cd4b184]1012}
[e080332]1013
1014
1015
1016/**
1017 * @}
[71ed4849]1018 */
Note: See TracBrowser for help on using the repository browser.