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 <driver.h>
|
---|
36 | #include <bool.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 |
|
---|
40 | #include <usb_iface.h>
|
---|
41 | #include <usb/usbdrv.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/recognise.h>
|
---|
44 | #include <usb/devreq.h>
|
---|
45 | #include <usb/request.h>
|
---|
46 | #include <usb/classes/hub.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 |
|
---|
54 | static int iface_get_hc_handle(device_t *device, devman_handle_t *handle)
|
---|
55 | {
|
---|
56 | return usb_hc_find(device->handle, handle);
|
---|
57 | }
|
---|
58 |
|
---|
59 | static usb_iface_t hub_usb_iface = {
|
---|
60 | .get_hc_handle = iface_get_hc_handle
|
---|
61 | };
|
---|
62 |
|
---|
63 | static device_ops_t hub_device_ops = {
|
---|
64 | .interfaces[USB_DEV_IFACE] = &hub_usb_iface
|
---|
65 | };
|
---|
66 |
|
---|
67 | //*********************************************
|
---|
68 | //
|
---|
69 | // hub driver code, initialization
|
---|
70 | //
|
---|
71 | //*********************************************
|
---|
72 |
|
---|
73 | usb_hub_info_t * usb_create_hub_info(device_t * device) {
|
---|
74 | usb_hub_info_t* result = usb_new(usb_hub_info_t);
|
---|
75 | usb_device_connection_initialize_from_device(&result->device_connection,
|
---|
76 | device);
|
---|
77 | usb_hc_connection_initialize_from_device(&result->connection,
|
---|
78 | device);
|
---|
79 | usb_endpoint_pipe_initialize_default_control(&result->endpoints.control,
|
---|
80 | &result->device_connection);
|
---|
81 |
|
---|
82 |
|
---|
83 | //result->device = device;
|
---|
84 | result->port_count = -1;
|
---|
85 | /// \TODO is this correct? is the device stored?
|
---|
86 | result->device = device;
|
---|
87 |
|
---|
88 | //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
|
---|
89 |
|
---|
90 | // get hub descriptor
|
---|
91 |
|
---|
92 | dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
|
---|
93 | void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
|
---|
94 | usb_hub_descriptor_t * descriptor;
|
---|
95 | size_t received_size;
|
---|
96 | int opResult;
|
---|
97 | dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
|
---|
98 | usb_endpoint_pipe_start_session(&result->endpoints.control);
|
---|
99 | opResult = usb_request_get_descriptor(&result->endpoints.control,
|
---|
100 | USB_REQUEST_TYPE_CLASS,
|
---|
101 | USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
|
---|
102 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
---|
103 | usb_endpoint_pipe_end_session(&result->endpoints.control);
|
---|
104 |
|
---|
105 | /* Initialize the interrupt endpoint.
|
---|
106 | usb_endpoint_pipe_initalize(
|
---|
107 | &hub_data->endpoints->status_change,
|
---|
108 | &endpiont_descriptor, &hub_data->connection);
|
---|
109 |
|
---|
110 | */ /// \TODO add this call
|
---|
111 |
|
---|
112 | if (opResult != EOK) {
|
---|
113 | dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
|
---|
114 | free(serialized_descriptor);
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 | dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
|
---|
118 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
---|
119 | if(descriptor==NULL){
|
---|
120 | dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
|
---|
121 | result->port_count = 1;///\TODO this code is only for debug!!!
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 | dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
|
---|
125 | result->port_count = descriptor->ports_count;
|
---|
126 | result->attached_devs = (usb_hc_attached_device_t*)
|
---|
127 | malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
|
---|
128 | int i;
|
---|
129 | for(i=0;i<result->port_count+1;++i){
|
---|
130 | result->attached_devs[i].handle=0;
|
---|
131 | result->attached_devs[i].address=0;
|
---|
132 | }
|
---|
133 | dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
|
---|
134 | free(serialized_descriptor);
|
---|
135 | free(descriptor->devices_removable);
|
---|
136 | free(descriptor);
|
---|
137 |
|
---|
138 | //finish
|
---|
139 |
|
---|
140 | dprintf(USB_LOG_LEVEL_INFO, "hub info created");
|
---|
141 |
|
---|
142 | return result;
|
---|
143 | }
|
---|
144 |
|
---|
145 | int usb_add_hub_device(device_t *dev) {
|
---|
146 | dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * We are some (probably deeply nested) hub.
|
---|
150 | * Thus, assign our own operations and explore already
|
---|
151 | * connected devices.
|
---|
152 | */
|
---|
153 | dev->ops = &hub_device_ops;
|
---|
154 |
|
---|
155 |
|
---|
156 | usb_hub_info_t * hub_info = usb_create_hub_info(dev);
|
---|
157 | usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
---|
158 |
|
---|
159 | int port;
|
---|
160 | int opResult;
|
---|
161 | //usb_target_t target;
|
---|
162 | //target.address = hub_info->usb_device->address;
|
---|
163 | //target.endpoint = 0;
|
---|
164 |
|
---|
165 | //get configuration descriptor
|
---|
166 | // this is not fully correct - there are more configurations
|
---|
167 | // and all should be checked
|
---|
168 | usb_standard_device_descriptor_t std_descriptor;
|
---|
169 | opResult = usb_request_get_device_descriptor(&hub_info->endpoints.control,
|
---|
170 | &std_descriptor);
|
---|
171 | if(opResult!=EOK){
|
---|
172 | dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
|
---|
173 | return opResult;
|
---|
174 | }
|
---|
175 | dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",std_descriptor.configuration_count);
|
---|
176 | if(std_descriptor.configuration_count<1){
|
---|
177 | dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
|
---|
178 | //shouldn`t I return?
|
---|
179 | }
|
---|
180 | /// \TODO check other configurations
|
---|
181 | usb_standard_configuration_descriptor_t config_descriptor;
|
---|
182 | opResult = usb_request_get_bare_configuration_descriptor(
|
---|
183 | &hub_info->endpoints.control, 0,
|
---|
184 | &config_descriptor);
|
---|
185 | if(opResult!=EOK){
|
---|
186 | dprintf(USB_LOG_LEVEL_ERROR, "could not get configuration descriptor, %d",opResult);
|
---|
187 | return opResult;
|
---|
188 | }
|
---|
189 | //set configuration
|
---|
190 | opResult = usb_request_set_configuration(&hub_info->endpoints.control,
|
---|
191 | config_descriptor.configuration_number);
|
---|
192 |
|
---|
193 | if (opResult != EOK) {
|
---|
194 | dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when setting hub`s configuration, %d", opResult);
|
---|
195 | }
|
---|
196 |
|
---|
197 | usb_device_request_setup_packet_t request;
|
---|
198 | for (port = 1; port < hub_info->port_count+1; ++port) {
|
---|
199 | usb_hub_set_power_port_request(&request, port);
|
---|
200 | opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
|
---|
201 | &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
|
---|
202 | dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
|
---|
203 | if (opResult != EOK) {
|
---|
204 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | //ports powered, hub seems to be enabled
|
---|
208 |
|
---|
209 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
---|
210 | //async_hangup(hc);
|
---|
211 |
|
---|
212 | //add the hub to list
|
---|
213 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
214 | usb_lst_append(&usb_hub_list, hub_info);
|
---|
215 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
216 |
|
---|
217 | dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
|
---|
218 | //(void)hub_info;
|
---|
219 | usb_hub_check_hub_changes();
|
---|
220 |
|
---|
221 |
|
---|
222 |
|
---|
223 | dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
|
---|
224 | //address is lost...
|
---|
225 | dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
|
---|
226 | //hub_info->endpoints.control.,
|
---|
227 | hub_info->port_count);
|
---|
228 | dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",config_descriptor.configuration_number);
|
---|
229 |
|
---|
230 | return EOK;
|
---|
231 | //return ENOTSUP;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | //*********************************************
|
---|
236 | //
|
---|
237 | // hub driver code, main loop
|
---|
238 | //
|
---|
239 | //*********************************************
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Convenience function for releasing default address and writing debug info
|
---|
243 | * (these few lines are used too often to be written again and again).
|
---|
244 | * @param hc
|
---|
245 | * @return
|
---|
246 | */
|
---|
247 | inline static int usb_hub_release_default_address(int hc){
|
---|
248 | int opResult;
|
---|
249 | dprintf(USB_LOG_LEVEL_INFO, "releasing default address");
|
---|
250 | opResult = usb_drv_release_default_address(hc);
|
---|
251 | if (opResult != EOK) {
|
---|
252 | dprintf(USB_LOG_LEVEL_WARNING, "failed to release default address");
|
---|
253 | }
|
---|
254 | return opResult;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Reset the port with new device and reserve the default address.
|
---|
259 | * @param hc
|
---|
260 | * @param port
|
---|
261 | * @param target
|
---|
262 | */
|
---|
263 | static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
|
---|
264 | usb_device_request_setup_packet_t request;
|
---|
265 | int opResult;
|
---|
266 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
---|
267 | assert(hub->endpoints.control.hc_phone);
|
---|
268 | //get default address
|
---|
269 | //opResult = usb_drv_reserve_default_address(hc);
|
---|
270 | opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
|
---|
271 | if (opResult != EOK) {
|
---|
272 | dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
|
---|
273 | return;
|
---|
274 | }
|
---|
275 | //reset port
|
---|
276 | usb_hub_set_reset_port_request(&request, port);
|
---|
277 | opResult = usb_endpoint_pipe_control_write(
|
---|
278 | &hub->endpoints.control,
|
---|
279 | &request,sizeof(usb_device_request_setup_packet_t),
|
---|
280 | NULL, 0
|
---|
281 | );
|
---|
282 | if (opResult != EOK) {
|
---|
283 | dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
|
---|
284 | //usb_hub_release_default_address(hc);
|
---|
285 | usb_hc_release_default_address(&hub->connection);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Finalize adding new device after port reset
|
---|
291 | * @param hc
|
---|
292 | * @param port
|
---|
293 | * @param target
|
---|
294 | */
|
---|
295 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
---|
296 | uint16_t port) {
|
---|
297 |
|
---|
298 | int opResult;
|
---|
299 | dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
|
---|
300 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
---|
301 | port, USB_HUB_FEATURE_C_PORT_RESET);
|
---|
302 |
|
---|
303 | if (opResult != EOK) {
|
---|
304 | dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
|
---|
305 | usb_hc_release_default_address(&hub->connection);
|
---|
306 | return;
|
---|
307 | }
|
---|
308 | //create connection to device
|
---|
309 | usb_endpoint_pipe_t new_device_pipe;
|
---|
310 | usb_device_connection_t new_device_connection;
|
---|
311 | usb_device_connection_initialize_on_default_address(
|
---|
312 | &new_device_connection,
|
---|
313 | &hub->connection
|
---|
314 | );
|
---|
315 | usb_endpoint_pipe_initialize_default_control(
|
---|
316 | &new_device_pipe,
|
---|
317 | &new_device_connection);
|
---|
318 | /// \TODO get highspeed info
|
---|
319 |
|
---|
320 |
|
---|
321 |
|
---|
322 |
|
---|
323 |
|
---|
324 | /* Request address from host controller. */
|
---|
325 | usb_address_t new_device_address = usb_hc_request_address(
|
---|
326 | &hub->connection,
|
---|
327 | USB_SPEED_LOW/// \TODO fullspeed??
|
---|
328 | );
|
---|
329 | if (new_device_address < 0) {
|
---|
330 | dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
|
---|
331 | opResult = new_device_address;
|
---|
332 | usb_hc_release_default_address(&hub->connection);
|
---|
333 | return;
|
---|
334 | }
|
---|
335 | dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
|
---|
336 | //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
|
---|
337 | // new_device_address);
|
---|
338 | opResult = usb_request_set_address(&new_device_pipe,new_device_address);
|
---|
339 |
|
---|
340 | if (opResult != EOK) {
|
---|
341 | dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
|
---|
342 | usb_hc_release_default_address(&hub->connection);
|
---|
343 | return;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | //opResult = usb_hub_release_default_address(hc);
|
---|
348 | opResult = usb_hc_release_default_address(&hub->connection);
|
---|
349 | if(opResult!=EOK){
|
---|
350 | return;
|
---|
351 | }
|
---|
352 |
|
---|
353 | devman_handle_t child_handle;
|
---|
354 | //??
|
---|
355 | opResult = usb_device_register_child_in_devman(new_device_address,
|
---|
356 | hub->connection.hc_handle, hub->device, &child_handle);
|
---|
357 |
|
---|
358 | if (opResult != EOK) {
|
---|
359 | dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
|
---|
360 | return;
|
---|
361 | }
|
---|
362 | hub->attached_devs[port].handle = child_handle;
|
---|
363 | hub->attached_devs[port].address = new_device_address;
|
---|
364 |
|
---|
365 | //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
|
---|
366 | opResult = usb_hc_register_device(
|
---|
367 | &hub->connection,
|
---|
368 | &hub->attached_devs[port]);
|
---|
369 | if (opResult != EOK) {
|
---|
370 | dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
|
---|
371 | return;
|
---|
372 | }
|
---|
373 | dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
|
---|
374 | new_device_address, child_handle);
|
---|
375 |
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Unregister device address in hc
|
---|
380 | * @param hc
|
---|
381 | * @param port
|
---|
382 | * @param target
|
---|
383 | */
|
---|
384 | static void usb_hub_removed_device(
|
---|
385 | usb_hub_info_t * hub,uint16_t port) {
|
---|
386 | //usb_device_request_setup_packet_t request;
|
---|
387 | int opResult;
|
---|
388 |
|
---|
389 | /** \TODO remove device from device manager - not yet implemented in
|
---|
390 | * devide manager
|
---|
391 | */
|
---|
392 |
|
---|
393 | //close address
|
---|
394 | if(hub->attached_devs[port].address!=0){
|
---|
395 | //opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
|
---|
396 | opResult = usb_hc_unregister_device(
|
---|
397 | &hub->connection, hub->attached_devs[port].address);
|
---|
398 | if(opResult != EOK) {
|
---|
399 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
---|
400 | "removed device: %d", opResult);
|
---|
401 | }
|
---|
402 | hub->attached_devs[port].address = 0;
|
---|
403 | hub->attached_devs[port].handle = 0;
|
---|
404 | }else{
|
---|
405 | dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
|
---|
406 | //device was disconnected before it`s port was reset - return default address
|
---|
407 | //usb_drv_release_default_address(hc);
|
---|
408 | usb_hc_release_default_address(&hub->connection);
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | /**
|
---|
413 | * Process interrupts on given hub port
|
---|
414 | * @param hc
|
---|
415 | * @param port
|
---|
416 | * @param target
|
---|
417 | */
|
---|
418 | static void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
---|
419 | uint16_t port) {
|
---|
420 | dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
|
---|
421 | //determine type of change
|
---|
422 | usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
|
---|
423 | int opResult = usb_endpoint_pipe_start_session(pipe);
|
---|
424 |
|
---|
425 | if(opResult != EOK){
|
---|
426 | dprintf(USB_LOG_LEVEL_ERROR, "cannot open pipe %d", opResult);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /*
|
---|
430 | usb_target_t target;
|
---|
431 | target.address=address;
|
---|
432 | target.endpoint=0;
|
---|
433 | */
|
---|
434 |
|
---|
435 | usb_port_status_t status;
|
---|
436 | size_t rcvd_size;
|
---|
437 | usb_device_request_setup_packet_t request;
|
---|
438 | //int opResult;
|
---|
439 | usb_hub_set_port_status_request(&request, port);
|
---|
440 | //endpoint 0
|
---|
441 |
|
---|
442 | opResult = usb_endpoint_pipe_control_read(
|
---|
443 | pipe,
|
---|
444 | &request, sizeof(usb_device_request_setup_packet_t),
|
---|
445 | &status, 4, &rcvd_size
|
---|
446 | );
|
---|
447 | if (opResult != EOK) {
|
---|
448 | dprintf(USB_LOG_LEVEL_ERROR, "ERROR: could not get port status");
|
---|
449 | return;
|
---|
450 | }
|
---|
451 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
---|
452 | dprintf(USB_LOG_LEVEL_ERROR, "ERROR: received status has incorrect size");
|
---|
453 | return;
|
---|
454 | }
|
---|
455 | //something connected/disconnected
|
---|
456 | if (usb_port_connect_change(&status)) {
|
---|
457 | opResult = usb_hub_clear_port_feature(pipe,
|
---|
458 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
---|
459 | // TODO: check opResult
|
---|
460 | if (usb_port_dev_connected(&status)) {
|
---|
461 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
---|
462 | usb_hub_init_add_device(hub, port);
|
---|
463 | } else {
|
---|
464 | usb_hub_removed_device(hub, port);
|
---|
465 | }
|
---|
466 | }
|
---|
467 | //port reset
|
---|
468 | if (usb_port_reset_completed(&status)) {
|
---|
469 | dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
|
---|
470 | if (usb_port_enabled(&status)) {
|
---|
471 | usb_hub_finalize_add_device(hub, port);
|
---|
472 | } else {
|
---|
473 | dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | usb_port_set_connect_change(&status, false);
|
---|
478 | usb_port_set_reset(&status, false);
|
---|
479 | usb_port_set_reset_completed(&status, false);
|
---|
480 | usb_port_set_dev_connected(&status, false);
|
---|
481 | if (status>>16) {
|
---|
482 | dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
|
---|
483 |
|
---|
484 | }
|
---|
485 | /// \TODO handle other changes
|
---|
486 | /// \TODO debug log for various situations
|
---|
487 | usb_endpoint_pipe_end_session(pipe);
|
---|
488 |
|
---|
489 |
|
---|
490 | }
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Check changes on all known hubs.
|
---|
494 | */
|
---|
495 | void usb_hub_check_hub_changes(void) {
|
---|
496 | /*
|
---|
497 | * Iterate through all hubs.
|
---|
498 | */
|
---|
499 | usb_general_list_t * lst_item;
|
---|
500 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
501 | for (lst_item = usb_hub_list.next;
|
---|
502 | lst_item != &usb_hub_list;
|
---|
503 | lst_item = lst_item->next) {
|
---|
504 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
505 | usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
|
---|
506 | int opResult;
|
---|
507 |
|
---|
508 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
|
---|
509 | if(opResult != EOK){
|
---|
510 | continue;
|
---|
511 | }
|
---|
512 | /*
|
---|
513 | * Check status change pipe of this hub.
|
---|
514 | */
|
---|
515 | /*
|
---|
516 | usb_target_t target;
|
---|
517 | target.address = hub_info->address;
|
---|
518 | target.endpoint = 1;/// \TODO get from endpoint descriptor
|
---|
519 | dprintf(USB_LOG_LEVEL_INFO, "checking changes for hub at addr %d",
|
---|
520 | target.address);
|
---|
521 | */
|
---|
522 | size_t port_count = hub_info->port_count;
|
---|
523 |
|
---|
524 | /*
|
---|
525 | * Connect to respective HC.
|
---|
526 | *
|
---|
527 | int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
|
---|
528 | if (hc < 0) {
|
---|
529 | continue;
|
---|
530 | }*/
|
---|
531 |
|
---|
532 | /// FIXME: count properly
|
---|
533 | size_t byte_length = ((port_count+1) / 8) + 1;
|
---|
534 |
|
---|
535 | void *change_bitmap = malloc(byte_length);
|
---|
536 | size_t actual_size;
|
---|
537 | //usb_handle_t handle;
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Send the request.
|
---|
541 | */
|
---|
542 | opResult = usb_endpoint_pipe_read(
|
---|
543 | &hub_info->endpoints.status_change,
|
---|
544 | change_bitmap, byte_length, &actual_size
|
---|
545 | );
|
---|
546 |
|
---|
547 | //usb_drv_async_wait_for(handle);
|
---|
548 |
|
---|
549 | if (opResult != EOK) {
|
---|
550 | free(change_bitmap);
|
---|
551 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
|
---|
552 | continue;
|
---|
553 | }
|
---|
554 | unsigned int port;
|
---|
555 | for (port = 1; port < port_count+1; ++port) {
|
---|
556 | bool interrupt =
|
---|
557 | (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
|
---|
558 | if (interrupt) {
|
---|
559 | usb_hub_process_interrupt(
|
---|
560 | hub_info, port);
|
---|
561 | }
|
---|
562 | }
|
---|
563 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
---|
564 | free(change_bitmap);
|
---|
565 |
|
---|
566 |
|
---|
567 | //async_hangup(hc);
|
---|
568 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
569 | }
|
---|
570 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 |
|
---|
575 |
|
---|
576 |
|
---|
577 | /**
|
---|
578 | * @}
|
---|
579 | */
|
---|