source: mainline/uspace/drv/usbhub/usbhub.c@ 1d7a74e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d7a74e was f401312, checked in by Lubos Slovak <lubos.slovak@…>, 15 years ago

Merged development into lelian/hidd

  • Property mode set to 100644
File size: 14.5 KB
Line 
1/*
2 * Copyright (c) 2010 Matus Dekanek
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup usb hub driver
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
39#include <usb_iface.h>
40#include <usb/usbdrv.h>
41#include <usb/descriptor.h>
42#include <usb/devreq.h>
43#include <usb/classes/hub.h>
44
45#include "usbhub.h"
46#include "usbhub_private.h"
47#include "port_status.h"
48
49static usb_iface_t hub_usb_iface = {
50 .get_hc_handle = usb_drv_find_hc
51};
52
53static device_ops_t hub_device_ops = {
54 .interfaces[USB_DEV_IFACE] = &hub_usb_iface
55};
56
57//*********************************************
58//
59// hub driver code, initialization
60//
61//*********************************************
62
63usb_hub_info_t * usb_create_hub_info(device_t * device, int hc) {
64 usb_hub_info_t* result = usb_new(usb_hub_info_t);
65 //result->device = device;
66 result->port_count = -1;
67 /// \TODO is this correct? is the device stored?
68 result->device = device;
69
70
71 //printf("[usb_hub] phone to hc = %d\n", hc);
72 if (hc < 0) {
73 return result;
74 }
75 //get some hub info
76 usb_address_t addr = usb_drv_get_my_address(hc, device);
77 dprintf(1,"[usb_hub] address of newly created hub = %d", addr);
78 /*if(addr<0){
79 //return result;
80
81 }*/
82
83 result->usb_device = usb_new(usb_hcd_attached_device_info_t);
84 result->usb_device->address = addr;
85
86 // get hub descriptor
87 usb_target_t target;
88 target.address = addr;
89 target.endpoint = 0;
90 usb_device_request_setup_packet_t request;
91 //printf("[usb_hub] creating descriptor request\n");
92 usb_hub_set_descriptor_request(&request);
93
94 //printf("[usb_hub] creating serialized descriptor\n");
95 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
96 usb_hub_descriptor_t * descriptor;
97 size_t received_size;
98 int opResult;
99 //printf("[usb_hub] starting control transaction\n");
100 opResult = usb_drv_sync_control_read(
101 hc, target, &request, serialized_descriptor,
102 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
103 if (opResult != EOK) {
104 dprintf(1,"[usb_hub] failed when receiving hub descriptor, badcode = %d",opResult);
105 free(serialized_descriptor);
106 return result;
107 }
108 //printf("[usb_hub] deserializing descriptor\n");
109 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
110 if(descriptor==NULL){
111 dprintf(1,"[usb_hub] could not deserialize descriptor ");
112 result->port_count = 1;///\TODO this code is only for debug!!!
113 return result;
114 }
115 //printf("[usb_hub] setting port count to %d\n",descriptor->ports_count);
116 result->port_count = descriptor->ports_count;
117 result->attached_devs = (usb_hub_attached_device_t*)
118 malloc((result->port_count+1) * sizeof(usb_hub_attached_device_t));
119 int i;
120 for(i=0;i<result->port_count+1;++i){
121 result->attached_devs[i].devman_handle=0;
122 result->attached_devs[i].address=0;
123 }
124 //printf("[usb_hub] freeing data\n");
125 free(serialized_descriptor);
126 free(descriptor->devices_removable);
127 free(descriptor);
128
129 //finish
130
131 dprintf(1,"[usb_hub] hub info created");
132
133 return result;
134}
135
136int usb_add_hub_device(device_t *dev) {
137 dprintf(1, "add_hub_device(handle=%d)", (int) dev->handle);
138 dprintf(1,"[usb_hub] hub device");
139
140 /*
141 * We are some (probably deeply nested) hub.
142 * Thus, assign our own operations and explore already
143 * connected devices.
144 */
145 dev->ops = &hub_device_ops;
146
147 //create the hub structure
148 //get hc connection
149 int hc = usb_drv_hc_connect_auto(dev, 0);
150 if (hc < 0) {
151 return hc;
152 }
153
154 usb_hub_info_t * hub_info = usb_create_hub_info(dev, hc);
155 int port;
156 int opResult;
157 usb_device_request_setup_packet_t request;
158 usb_target_t target;
159 target.address = hub_info->usb_device->address;
160 target.endpoint = 0;
161
162 //get configuration descriptor
163 // this is not fully correct - there are more configurations
164 // and all should be checked
165 usb_standard_device_descriptor_t std_descriptor;
166 opResult = usb_drv_req_get_device_descriptor(hc, target.address,
167 &std_descriptor);
168 if(opResult!=EOK){
169 dprintf(1,"[usb_hub] could not get device descriptor, %d",opResult);
170 return opResult;
171 }
172 dprintf(1,"[usb_hub] hub has %d configurations",std_descriptor.configuration_count);
173 if(std_descriptor.configuration_count<1){
174 dprintf(1,"[usb_hub] THERE ARE NO CONFIGURATIONS AVAILABLE");
175 }
176 /// \TODO check other configurations
177 usb_standard_configuration_descriptor_t config_descriptor;
178 opResult = usb_drv_req_get_bare_configuration_descriptor(hc,
179 target.address, 0,
180 &config_descriptor);
181 if(opResult!=EOK){
182 dprintf(1,"[usb_hub] could not get configuration descriptor, %d",opResult);
183 return opResult;
184 }
185 //set configuration
186 request.request_type = 0;
187 request.request = USB_DEVREQ_SET_CONFIGURATION;
188 request.index=0;
189 request.length=0;
190 request.value_high=0;
191 request.value_low = config_descriptor.configuration_number;
192 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
193 if (opResult != EOK) {
194 dprintf(1,"[usb_hub]something went wrong when setting hub`s configuration, %d", opResult);
195 }
196
197 for (port = 1; port < hub_info->port_count+1; ++port) {
198 usb_hub_set_power_port_request(&request, port);
199 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
200 dprintf(1,"[usb_hub] powering port %d",port);
201 if (opResult != EOK) {
202 dprintf(1,"[usb_hub]something went wrong when setting hub`s %dth port", port);
203 }
204 }
205 //ports powered, hub seems to be enabled
206
207 ipc_hangup(hc);
208
209 //add the hub to list
210 futex_down(&usb_hub_list_lock);
211 usb_lst_append(&usb_hub_list, hub_info);
212 futex_up(&usb_hub_list_lock);
213
214 dprintf(1,"[usb_hub] hub info added to list");
215 //(void)hub_info;
216 usb_hub_check_hub_changes();
217
218
219
220 dprintf(1,"[usb_hub] hub dev added");
221 dprintf(1,"\taddress %d, has %d ports ",
222 hub_info->usb_device->address,
223 hub_info->port_count);
224 dprintf(1,"\tused configuration %d",config_descriptor.configuration_number);
225
226 return EOK;
227 //return ENOTSUP;
228}
229
230
231
232//*********************************************
233//
234// hub driver code, main loop
235//
236//*********************************************
237
238/**
239 * convenience function for releasing default address and writing debug info
240 * (these few lines are used too often to be written again and again)
241 * @param hc
242 * @return
243 */
244inline static int usb_hub_release_default_address(int hc){
245 int opResult;
246 dprintf(1,"[usb_hub] releasing default address");
247 opResult = usb_drv_release_default_address(hc);
248 if (opResult != EOK) {
249 dprintf(1,"[usb_hub] failed to release default address");
250 }
251 return opResult;
252}
253
254/**
255 * reset the port with new device and reserve the default address
256 * @param hc
257 * @param port
258 * @param target
259 */
260static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) {
261 usb_device_request_setup_packet_t request;
262 int opResult;
263 dprintf(1,"[usb_hub] some connection changed");
264 //get default address
265 opResult = usb_drv_reserve_default_address(hc);
266 if (opResult != EOK) {
267 dprintf(1,"[usb_hub] cannot assign default address, it is probably used");
268 return;
269 }
270 //reset port
271 usb_hub_set_reset_port_request(&request, port);
272 opResult = usb_drv_sync_control_write(
273 hc, target,
274 &request,
275 NULL, 0
276 );
277 if (opResult != EOK) {
278 dprintf(1,"[usb_hub] something went wrong when reseting a port");
279 usb_hub_release_default_address(hc);
280 }
281}
282
283/**
284 * finalize adding new device after port reset
285 * @param hc
286 * @param port
287 * @param target
288 */
289static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
290 int hc, uint16_t port, usb_target_t target) {
291
292 int opResult;
293 dprintf(1,"[usb_hub] finalizing add device");
294 opResult = usb_hub_clear_port_feature(hc, target.address,
295 port, USB_HUB_FEATURE_C_PORT_RESET);
296 if (opResult != EOK) {
297 dprintf(1,"[usb_hub] failed to clear port reset feature");
298 usb_hub_release_default_address(hc);
299 return;
300 }
301
302 /* Request address at from host controller. */
303 usb_address_t new_device_address = usb_drv_request_address(hc);
304 if (new_device_address < 0) {
305 dprintf(1,"[usb_hub] failed to get free USB address");
306 opResult = new_device_address;
307 usb_hub_release_default_address(hc);
308 return;
309 }
310 dprintf(1,"[usb_hub] setting new address");
311 opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
312 new_device_address);
313
314 if (opResult != EOK) {
315 dprintf(1,"[usb_hub] could not set address for new device");
316 usb_hub_release_default_address(hc);
317 return;
318 }
319
320
321 opResult = usb_hub_release_default_address(hc);
322 if(opResult!=EOK){
323 return;
324 }
325
326 devman_handle_t child_handle;
327 opResult = usb_drv_register_child_in_devman(hc, hub->device,
328 new_device_address, &child_handle);
329 if (opResult != EOK) {
330 dprintf(1,"[usb_hub] could not start driver for new device");
331 return;
332 }
333 hub->attached_devs[port].devman_handle = child_handle;
334 hub->attached_devs[port].address = new_device_address;
335
336 opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
337 if (opResult != EOK) {
338 dprintf(1,"[usb_hub] could not assign address of device in hcd");
339 return;
340 }
341 dprintf(1,"[usb_hub] new device address %d, handle %zu",
342 new_device_address, child_handle);
343
344}
345
346/**
347 * unregister device address in hc
348 * @param hc
349 * @param port
350 * @param target
351 */
352static void usb_hub_removed_device(
353 usb_hub_info_t * hub, int hc, uint16_t port, usb_target_t target) {
354 //usb_device_request_setup_packet_t request;
355 int opResult;
356
357 /// \TODO remove device
358
359 hub->attached_devs[port].devman_handle=0;
360 //close address
361 if(hub->attached_devs[port].address!=0){
362 opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
363 if(opResult != EOK) {
364 dprintf(1,
365 "[usb_hub] could not release address of removed device: %d"
366 ,opResult);
367 }
368 hub->attached_devs[port].address = 0;
369 }else{
370 dprintf(1,
371 "[usb_hub] this is strange, disconnected device had no address");
372 //device was disconnected before it`s port was reset - return default address
373 usb_drv_release_default_address(hc);
374 }
375}
376
377/**
378 * process interrupts on given hub port
379 * @param hc
380 * @param port
381 * @param target
382 */
383static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc,
384 uint16_t port, usb_address_t address) {
385 dprintf(1,"[usb_hub] interrupt at port %d", port);
386 //determine type of change
387 usb_target_t target;
388 target.address=address;
389 target.endpoint=0;
390 usb_port_status_t status;
391 size_t rcvd_size;
392 usb_device_request_setup_packet_t request;
393 int opResult;
394 usb_hub_set_port_status_request(&request, port);
395 //endpoint 0
396
397 opResult = usb_drv_sync_control_read(
398 hc, target,
399 &request,
400 &status, 4, &rcvd_size
401 );
402 if (opResult != EOK) {
403 dprintf(1,"[usb_hub] ERROR: could not get port status");
404 return;
405 }
406 if (rcvd_size != sizeof (usb_port_status_t)) {
407 dprintf(1,"[usb_hub] ERROR: received status has incorrect size");
408 return;
409 }
410 //something connected/disconnected
411 if (usb_port_connect_change(&status)) {
412 opResult = usb_hub_clear_port_feature(hc, target.address,
413 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
414 // TODO: check opResult
415 if (usb_port_dev_connected(&status)) {
416 dprintf(1,"[usb_hub] some connection changed");
417 usb_hub_init_add_device(hc, port, target);
418 } else {
419 usb_hub_removed_device(hub, hc, port, target);
420 }
421 }
422 //port reset
423 if (usb_port_reset_completed(&status)) {
424 dprintf(1,"[usb_hub] port reset complete");
425 if (usb_port_enabled(&status)) {
426 usb_hub_finalize_add_device(hub, hc, port, target);
427 } else {
428 dprintf(1,"[usb_hub] ERROR: port reset, but port still not enabled");
429 }
430 }
431
432 usb_port_set_connect_change(&status, false);
433 usb_port_set_reset(&status, false);
434 usb_port_set_reset_completed(&status, false);
435 usb_port_set_dev_connected(&status, false);
436 if (status) {
437 dprintf(1,"[usb_hub]there was some unsupported change on port %d",port);
438 }
439 /// \TODO handle other changes
440 /// \TODO debug log for various situations
441
442}
443
444/* Check changes on all known hubs.
445 */
446void usb_hub_check_hub_changes(void) {
447 /*
448 * Iterate through all hubs.
449 */
450 usb_general_list_t * lst_item;
451 futex_down(&usb_hub_list_lock);
452 for (lst_item = usb_hub_list.next;
453 lst_item != &usb_hub_list;
454 lst_item = lst_item->next) {
455 futex_up(&usb_hub_list_lock);
456 usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
457 /*
458 * Check status change pipe of this hub.
459 */
460
461 usb_target_t target;
462 target.address = hub_info->usb_device->address;
463 target.endpoint = 1;/// \TODO get from endpoint descriptor
464 /*dprintf(1,"[usb_hub] checking changes for hub at addr %d",
465 target.address);*/
466
467 size_t port_count = hub_info->port_count;
468
469 /*
470 * Connect to respective HC.
471 */
472 int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
473 if (hc < 0) {
474 continue;
475 }
476
477 /// FIXME: count properly
478 size_t byte_length = ((port_count+1) / 8) + 1;
479
480 void *change_bitmap = malloc(byte_length);
481 size_t actual_size;
482 usb_handle_t handle;
483
484 /*
485 * Send the request.
486 */
487 int opResult = usb_drv_async_interrupt_in(hc, target,
488 change_bitmap, byte_length, &actual_size,
489 &handle);
490
491 usb_drv_async_wait_for(handle);
492
493 if (opResult != EOK) {
494 dprintf(1,"[usb_hub] something went wrong while getting status of hub");
495 continue;
496 }
497 unsigned int port;
498 for (port = 1; port < port_count+1; ++port) {
499 bool interrupt =
500 (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
501 if (interrupt) {
502 usb_hub_process_interrupt(
503 hub_info, hc, port, hub_info->usb_device->address);
504 }
505 }
506 free(change_bitmap);
507
508 ipc_hangup(hc);
509 futex_down(&usb_hub_list_lock);
510 }
511 futex_up(&usb_hub_list_lock);
512}
513
514
515
516
517
518/**
519 * @}
520 */
Note: See TracBrowser for help on using the repository browser.