Changeset 7ffe82f in mainline
- Timestamp:
- 2011-03-14T22:37:59Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 2ef036a
- Parents:
- c2343cc
- Location:
- uspace/app/usbinfo
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/usbinfo/info.c
rc2343cc r7ffe82f 107 107 dump_usb_descriptor((uint8_t *)&device_descriptor, sizeof(device_descriptor)); 108 108 109 rc = EOK; 110 goto leave; 111 109 112 /* 110 113 * Get first configuration descriptor and dump it. -
uspace/app/usbinfo/main.c
rc2343cc r7ffe82f 43 43 #include <devman.h> 44 44 #include <devmap.h> 45 #include <usb/usbdevice.h> 46 #include <usb/pipes.h> 45 47 #include "usbinfo.h" 46 47 enum {48 ACTION_HELP = 256,49 ACTION_DEVICE_ADDRESS,50 ACTION_HOST_CONTROLLER,51 ACTION_DEVICE,52 };53 54 static struct option long_options[] = {55 {"help", no_argument, NULL, ACTION_HELP},56 {"address", required_argument, NULL, ACTION_DEVICE_ADDRESS},57 {"host-controller", required_argument, NULL, ACTION_HOST_CONTROLLER},58 {"device", required_argument, NULL, ACTION_DEVICE},59 {0, 0, NULL, 0}60 };61 static const char *short_options = "ha:t:d:";62 48 63 49 static void print_usage(char *app_name) … … 65 51 #define INDENT " " 66 52 printf(NAME ": query USB devices for descriptors\n\n"); 67 printf("Usage: %s [options]\n", app_name); 68 printf(" -h --help\n" INDENT \ 69 "Display this help.\n"); 70 printf(" -tID --host-controller ID\n" INDENT \ 71 "Set host controller (ID can be path or class number)\n"); 72 printf(" -aADDR --address ADDR\n" INDENT \ 73 "Set device address\n"); 53 printf("Usage: %s [options] device [device [device [ ... ]]]\n", 54 app_name); 55 printf(INDENT "The device is a devman path to the device.\n"); 74 56 printf("\n"); 75 57 #undef INDENT 76 58 } 77 59 78 static int get_host_controller_handle(const char *path,79 devman_handle_t * hc_handle)60 static bool resolve_hc_handle_and_dev_addr(const char *devpath, 61 devman_handle_t *out_hc_handle, usb_address_t *out_device_address) 80 62 { 81 63 int rc; 82 64 83 if (str_cmp(path, "uhci") == 0) { 84 path = "/hw/pci0/00:01.2/uhci-hc"; 65 char *path = str_dup(devpath); 66 if (path == NULL) { 67 return ENOMEM; 85 68 } 86 69 87 devman_handle_t handle; 88 rc = devman_device_get_handle(path, &handle, 0); 89 if (rc != EOK) { 90 fprintf(stderr, 91 NAME ": failed getting handle of `devman::/%s'.\n", 92 path); 93 return rc; 94 } 95 *hc_handle = handle; 70 devman_handle_t hc = 0; 71 bool hc_found = false; 72 usb_address_t addr = 0; 73 bool addr_found = false; 96 74 97 return EOK; 98 } 75 /* Remove suffixes and hope that we will encounter device node. */ 76 while (str_length(path) > 0) { 77 /* Get device handle first. */ 78 devman_handle_t dev_handle; 79 rc = devman_device_get_handle(path, &dev_handle, 0); 80 if (rc != EOK) { 81 free(path); 82 return false; 83 } 99 84 100 static int get_device_address(const char *str_address, usb_address_t *address) 101 { 102 usb_address_t addr = (usb_address_t) strtol(str_address, NULL, 0); 103 if ((addr < 0) || (addr >= USB11_ADDRESS_MAX)) { 104 fprintf(stderr, NAME ": USB address out of range.\n"); 105 return ERANGE; 85 /* Try to find its host controller. */ 86 if (!hc_found) { 87 rc = usb_hc_find(dev_handle, &hc); 88 if (rc == EOK) { 89 hc_found = true; 90 } 91 } 92 /* Try to get its address. */ 93 if (!addr_found) { 94 addr = usb_device_get_assigned_address(dev_handle); 95 if (addr >= 0) { 96 addr_found = true; 97 } 98 } 99 100 /* Speed-up. */ 101 if (hc_found && addr_found) { 102 break; 103 } 104 105 /* Remove the last suffix. */ 106 char *slash_pos = str_rchr(path, '/'); 107 if (slash_pos != NULL) { 108 *slash_pos = 0; 109 } 106 110 } 107 111 108 *address = addr; 109 return EOK; 112 free(path); 113 114 if (hc_found && addr_found) { 115 if (out_hc_handle != NULL) { 116 *out_hc_handle = hc; 117 } 118 if (out_device_address != NULL) { 119 *out_device_address = addr; 120 } 121 return true; 122 } else { 123 return false; 124 } 110 125 } 111 112 126 113 127 int main(int argc, char *argv[]) 114 128 { 115 devman_handle_t hc_handle = (devman_handle_t) -1;116 usb_address_t device_address = (usb_address_t) -1;117 118 129 if (argc <= 1) { 119 130 print_usage(argv[0]); … … 121 132 } 122 133 134 /* 135 * Process command-line options. They determine what shall be 136 * done with the device. 137 */ 138 139 /* TODO */ 140 141 /* 142 * Go through all devices given on the command line and run the 143 * specified actions. 144 */ 123 145 int i; 124 do { 125 i = getopt_long(argc, argv, short_options, long_options, NULL); 126 switch (i) { 127 case -1: 128 break; 146 for (i = 1; i < argc; i++) { 147 char *devpath = argv[i]; 129 148 130 case '?': 131 print_usage(argv[0]); 132 return -1; 133 134 case 'h': 135 case ACTION_HELP: 136 print_usage(argv[0]); 137 return 0; 138 139 case 'a': 140 case ACTION_DEVICE_ADDRESS: { 141 int rc = get_device_address(optarg, 142 &device_address); 143 if (rc != EOK) { 144 return rc; 145 } 146 break; 147 } 148 149 case 't': 150 case ACTION_HOST_CONTROLLER: { 151 int rc = get_host_controller_handle(optarg, 152 &hc_handle); 153 if (rc != EOK) { 154 return rc; 155 } 156 break; 157 } 158 159 case 'd': 160 case ACTION_DEVICE: 161 break; 162 163 default: 164 break; 149 /* The initialization is here only to make compiler happy. */ 150 devman_handle_t hc_handle = 0; 151 usb_address_t dev_addr = 0; 152 bool found = resolve_hc_handle_and_dev_addr(devpath, 153 &hc_handle, &dev_addr); 154 if (!found) { 155 fprintf(stderr, NAME ": device `%s' not found " 156 "or not of USB kind, skipping.\n", 157 devpath); 158 continue; 165 159 } 166 160 167 } while (i != -1); 168 169 if ((hc_handle == (devman_handle_t) -1) 170 || (device_address == (usb_address_t) -1)) { 171 fprintf(stderr, NAME ": no target specified.\n"); 172 return EINVAL; 161 printf("Device `%s':\n------------\n", devpath); 162 dump_device(hc_handle, dev_addr); 173 163 } 174 175 dump_device(hc_handle, device_address);176 164 177 165 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.