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 |
|
---|
40 | #include <usb_iface.h>
|
---|
41 | #include <usb/ddfiface.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/recognise.h>
|
---|
44 | #include <usb/request.h>
|
---|
45 | #include <usb/classes/hub.h>
|
---|
46 | #include <stdio.h>
|
---|
47 |
|
---|
48 | #include "usbhub.h"
|
---|
49 | #include "usbhub_private.h"
|
---|
50 | #include "port_status.h"
|
---|
51 | #include "usb/usb.h"
|
---|
52 | #include "usb/pipes.h"
|
---|
53 | #include "usb/classes/classes.h"
|
---|
54 |
|
---|
55 | static int usb_hub_trigger_connecting_non_removable_devices(
|
---|
56 | usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
|
---|
57 |
|
---|
58 |
|
---|
59 | //*********************************************
|
---|
60 | //
|
---|
61 | // hub driver code, initialization
|
---|
62 | //
|
---|
63 | //*********************************************
|
---|
64 |
|
---|
65 | /**
|
---|
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
|
---|
71 | */
|
---|
72 | static 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 | /**
|
---|
83 | * Load hub-specific information into hub_info structure and process if needed
|
---|
84 | *
|
---|
85 | * Particularly read port count and initialize structure holding port
|
---|
86 | * information. If there are non-removable devices, start initializing them.
|
---|
87 | * This function is hub-specific and should be run only after the hub is
|
---|
88 | * configured using usb_hub_set_configuration function.
|
---|
89 | * @param hub_info hub representation
|
---|
90 | * @return error code
|
---|
91 | */
|
---|
92 | static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info){
|
---|
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
|
---|
99 | * these lines allow to reset hub once more, it can be used as
|
---|
100 | * brute-force initialization for non-removable devices
|
---|
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);
|
---|
104 | return opResult;
|
---|
105 | }
|
---|
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",
|
---|
116 | opResult);
|
---|
117 | free(serialized_descriptor);
|
---|
118 | return opResult;
|
---|
119 | }
|
---|
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");
|
---|
124 | return opResult;
|
---|
125 | }
|
---|
126 | usb_log_debug("setting port count to %d\n",descriptor->ports_count);
|
---|
127 | hub_info->port_count = descriptor->ports_count;
|
---|
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]);
|
---|
132 | }
|
---|
133 | //handle non-removable devices
|
---|
134 | usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
|
---|
135 | usb_log_debug2("freeing data\n");
|
---|
136 | free(serialized_descriptor);
|
---|
137 | free(descriptor->devices_removable);
|
---|
138 | free(descriptor);
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 | /**
|
---|
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.
|
---|
146 | * @param hub_info hub representation
|
---|
147 | * @return error code
|
---|
148 | */
|
---|
149 | static int usb_hub_set_configuration(usb_hub_info_t * hub_info){
|
---|
150 | //device descriptor
|
---|
151 | usb_standard_device_descriptor_t *std_descriptor
|
---|
152 | = &hub_info->usb_device->descriptors.device;
|
---|
153 | usb_log_debug("hub has %d configurations\n",
|
---|
154 | std_descriptor->configuration_count);
|
---|
155 | if(std_descriptor->configuration_count<1){
|
---|
156 | usb_log_error("there are no configurations available\n");
|
---|
157 | return EINVAL;
|
---|
158 | }
|
---|
159 |
|
---|
160 | usb_standard_configuration_descriptor_t *config_descriptor
|
---|
161 | = (usb_standard_configuration_descriptor_t *)
|
---|
162 | hub_info->usb_device->descriptors.configuration;
|
---|
163 |
|
---|
164 | /* Set configuration. */
|
---|
165 | int opResult = usb_request_set_configuration(
|
---|
166 | &hub_info->usb_device->ctrl_pipe,
|
---|
167 | config_descriptor->configuration_number);
|
---|
168 |
|
---|
169 | if (opResult != EOK) {
|
---|
170 | usb_log_error("Failed to set hub configuration: %s.\n",
|
---|
171 | str_error(opResult));
|
---|
172 | return opResult;
|
---|
173 | }
|
---|
174 | usb_log_debug("\tused configuration %d\n",
|
---|
175 | config_descriptor->configuration_number);
|
---|
176 |
|
---|
177 | return EOK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
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
|
---|
187 | */
|
---|
188 | int 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);
|
---|
196 | if(opResult != EOK){
|
---|
197 | usb_log_error("could not initialize connection to device, errno %d\n",
|
---|
198 | opResult);
|
---|
199 | free(hub_info);
|
---|
200 | return opResult;
|
---|
201 | }
|
---|
202 |
|
---|
203 | usb_pipe_start_session(hub_info->control_pipe);
|
---|
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);
|
---|
209 | return opResult;
|
---|
210 | }
|
---|
211 | //get port count and create attached_devs
|
---|
212 | opResult = usb_hub_process_hub_specific_info(hub_info);
|
---|
213 | if(opResult!=EOK){
|
---|
214 | usb_log_error("could not set hub configuration, errno %d\n",opResult);
|
---|
215 | free(hub_info);
|
---|
216 | return opResult;
|
---|
217 | }
|
---|
218 | usb_pipe_end_session(hub_info->control_pipe);
|
---|
219 |
|
---|
220 |
|
---|
221 | /// \TODO what is this?
|
---|
222 | usb_log_debug("Creating `hub' function.\n");
|
---|
223 | ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
|
---|
224 | fun_exposed, "hub");
|
---|
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 |
|
---|
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;
|
---|
263 | }
|
---|
264 |
|
---|
265 | usb_log_info("Controlling hub `%s' (%d ports).\n",
|
---|
266 | hub_info->usb_device->ddf_dev->name, hub_info->port_count);
|
---|
267 | return EOK;
|
---|
268 |
|
---|
269 | leave:
|
---|
270 | free(hub_info);
|
---|
271 |
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | //*********************************************
|
---|
277 | //
|
---|
278 | // hub driver code, main loop and port handling
|
---|
279 | //
|
---|
280 | //*********************************************
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * triggers actions to connect non0removable devices
|
---|
284 | *
|
---|
285 | * This will trigger operations leading to activated non-removable device.
|
---|
286 | * Control pipe of the hub must be open fo communication.
|
---|
287 | * @param hub hub representation
|
---|
288 | * @param descriptor usb hub descriptor
|
---|
289 | * @return error code
|
---|
290 | */
|
---|
291 | static int usb_hub_trigger_connecting_non_removable_devices(usb_hub_info_t * hub,
|
---|
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 | }
|
---|
317 | //set the status change bit, so it will be noticed in driver loop
|
---|
318 | if(usb_port_dev_connected(&status)){
|
---|
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");
|
---|
332 | usb_hub_set_enable_port_feature_request(&request, port,
|
---|
333 | USB_HUB_FEATURE_PORT_ENABLE);
|
---|
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(
|
---|
341 | "could not set port enabled on port %d errno:%d\n",
|
---|
342 | port, opResult);
|
---|
343 | }
|
---|
344 | usb_log_debug("port set to enabled - should lead to connection change\n");
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
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 | }
|
---|
379 | return EOK;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
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 | */
|
---|
391 | static 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){
|
---|
394 | usb_log_error("could not release default address, errno %d\n",opResult);
|
---|
395 | return opResult;
|
---|
396 | }
|
---|
397 | hub->is_default_address_used = false;
|
---|
398 | return EOK;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
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
|
---|
409 | */
|
---|
410 | void usb_hub_removed_device(
|
---|
411 | usb_hub_info_t * hub,uint16_t port) {
|
---|
412 |
|
---|
413 | int opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
---|
414 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
---|
415 | if(opResult != EOK){
|
---|
416 | usb_log_warning("could not clear port-change-connection flag\n");
|
---|
417 | }
|
---|
418 | /** \TODO remove device from device manager - not yet implemented in
|
---|
419 | * devide manager
|
---|
420 | */
|
---|
421 |
|
---|
422 | //close address
|
---|
423 | if(hub->ports[port].attached_device.address >= 0){
|
---|
424 | /*uncomment this code to use it when DDF allows device removal
|
---|
425 | opResult = usb_hc_unregister_device(
|
---|
426 | &hub->connection, hub->attached_devs[port].address);
|
---|
427 | if(opResult != EOK) {
|
---|
428 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
---|
429 | "removed device: %d", opResult);
|
---|
430 | }
|
---|
431 | hub->attached_devs[port].address = 0;
|
---|
432 | hub->attached_devs[port].handle = 0;
|
---|
433 | */
|
---|
434 | }else{
|
---|
435 | usb_log_warning("this is strange, disconnected device had no address\n");
|
---|
436 | //device was disconnected before it`s port was reset - return default address
|
---|
437 | usb_hub_release_default_address(hub);
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Process over current condition on port.
|
---|
444 | *
|
---|
445 | * Turn off the power on the port.
|
---|
446 | *
|
---|
447 | * @param hub hub representation
|
---|
448 | * @param port port number, starting from 1
|
---|
449 | */
|
---|
450 | void usb_hub_over_current( usb_hub_info_t * hub,
|
---|
451 | uint16_t port){
|
---|
452 | int opResult;
|
---|
453 | opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
---|
454 | port, USB_HUB_FEATURE_PORT_POWER);
|
---|
455 | if(opResult!=EOK){
|
---|
456 | usb_log_error("cannot power off port %d; %d\n",
|
---|
457 | port, opResult);
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * @}
|
---|
464 | */
|
---|