main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010-2011 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <inttypes.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <errno.h>
00041 #include <str_error.h>
00042 #include <bool.h>
00043 #include <getopt.h>
00044 #include <devman.h>
00045 #include <devmap.h>
00046 #include <usb/dev/hub.h>
00047 #include <usb/hc.h>
00048 
00049 #define NAME "lsusb"
00050 
00051 #define MAX_USB_ADDRESS USB11_ADDRESS_MAX
00052 #define MAX_FAILED_ATTEMPTS 10
00053 #define MAX_PATH_LENGTH 1024
00054 
00055 static void print_found_hc(size_t class_index, const char *path)
00056 {
00057         // printf(NAME ": host controller %zu is `%s'.\n", class_index, path);
00058         printf("Bus %02zu: %s\n", class_index, path);
00059 }
00060 static void print_found_dev(usb_address_t addr, const char *path)
00061 {
00062         // printf(NAME ":     device with address %d is `%s'.\n", addr, path);
00063         printf("  Device %02d: %s\n", addr, path);
00064 }
00065 
00066 static void print_hc_devices(devman_handle_t hc_handle)
00067 {
00068         int rc;
00069         usb_hc_connection_t conn;
00070 
00071         usb_hc_connection_initialize(&conn, hc_handle);
00072         rc = usb_hc_connection_open(&conn);
00073         if (rc != EOK) {
00074                 printf(NAME ": failed to connect to HC: %s.\n",
00075                     str_error(rc));
00076                 return;
00077         }
00078         usb_address_t addr;
00079         for (addr = 1; addr < MAX_USB_ADDRESS; addr++) {
00080                 devman_handle_t dev_handle;
00081                 rc = usb_hc_get_handle_by_address(&conn, addr, &dev_handle);
00082                 if (rc != EOK) {
00083                         continue;
00084                 }
00085                 char path[MAX_PATH_LENGTH];
00086                 rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH);
00087                 if (rc != EOK) {
00088                         continue;
00089                 }
00090                 print_found_dev(addr, path);
00091         }
00092         usb_hc_connection_close(&conn);
00093 }
00094 
00095 int main(int argc, char *argv[])
00096 {
00097         size_t class_index = 0;
00098         size_t failed_attempts = 0;
00099 
00100         while (failed_attempts < MAX_FAILED_ATTEMPTS) {
00101                 class_index++;
00102                 devman_handle_t hc_handle = 0;
00103                 int rc = usb_ddf_get_hc_handle_by_class(class_index, &hc_handle);
00104                 if (rc != EOK) {
00105                         failed_attempts++;
00106                         continue;
00107                 }
00108                 char path[MAX_PATH_LENGTH];
00109                 rc = devman_get_device_path(hc_handle, path, MAX_PATH_LENGTH);
00110                 if (rc != EOK) {
00111                         continue;
00112                 }
00113                 print_found_hc(class_index, path);
00114                 print_hc_devices(hc_handle);
00115         }
00116 
00117         return 0;
00118 }
00119 
00120 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7