Changes in uspace/app/usbinfo/main.c [03e02248:c377bc50] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/usbinfo/main.c
r03e02248 rc377bc50 1 1 /* 2 * Copyright (c) 2010 Vojtech Horky2 * Copyright (c) 2010-2011 Vojtech Horky 3 3 * All rights reserved. 4 4 * … … 40 40 #include <str_error.h> 41 41 #include <bool.h> 42 #include <getopt.h> 42 43 #include <devman.h> 44 #include <devmap.h> 43 45 #include <usb/usbdrv.h> 44 46 #include "usbinfo.h" 45 47 46 #define DEFAULT_HOST_CONTROLLER_PATH "/virt/usbhc" 48 enum { 49 ACTION_HELP = 256, 50 ACTION_DEVICE_ADDRESS, 51 ACTION_HOST_CONTROLLER, 52 ACTION_DEVICE, 53 }; 54 55 static struct option long_options[] = { 56 {"help", no_argument, NULL, ACTION_HELP}, 57 {"address", required_argument, NULL, ACTION_DEVICE_ADDRESS}, 58 {"host-controller", required_argument, NULL, ACTION_HOST_CONTROLLER}, 59 {"device", required_argument, NULL, ACTION_DEVICE}, 60 {0, 0, NULL, 0} 61 }; 62 static const char *short_options = "ha:t:d:"; 47 63 48 64 static void print_usage(char *app_name) 49 65 { 66 #define INDENT " " 50 67 printf(NAME ": query USB devices for descriptors\n\n"); 51 printf("Usage: %s /path/to/hc usb-address\n where\n", app_name); 52 printf(" /path/to/hc Devman path to USB host controller " \ 53 "(use `-' for\n"); 54 printf(" default HC at `%s').\n", 55 DEFAULT_HOST_CONTROLLER_PATH); 56 printf(" usb-address USB address of device to be queried\n"); 68 printf("Usage: %s [options]\n", app_name); 69 printf(" -h --help\n" INDENT \ 70 "Display this help.\n"); 71 printf(" -tID --host-controller ID\n" INDENT \ 72 "Set host controller (ID can be path or class number)\n"); 73 printf(" -aADDR --address ADDR\n" INDENT \ 74 "Set device address\n"); 57 75 printf("\n"); 58 } 59 60 static int connect_to_hc(const char *path) 76 #undef INDENT 77 } 78 79 static int set_new_host_controller(int *phone, const char *path) 61 80 { 62 81 int rc; 63 devman_handle_t handle; 64 65 rc = devman_device_get_handle(path, &handle, 0); 66 if (rc != EOK) { 67 return rc; 68 } 69 70 int phone = devman_device_connect(handle, 0); 71 72 return phone; 73 } 74 75 int main(int argc, char *argv[]) 76 { 77 if (argc != 3) { 78 print_usage(argv[0]); 79 return EINVAL; 80 } 81 82 char *hc_path = argv[1]; 83 long int address_long = strtol(argv[2], NULL, 0); 84 85 /* 86 * Connect to given host controller driver. 87 */ 88 if (str_cmp(hc_path, "-") == 0) { 89 hc_path = (char *) DEFAULT_HOST_CONTROLLER_PATH; 90 } 91 int hc_phone = connect_to_hc(hc_path); 92 if (hc_phone < 0) { 93 fprintf(stderr, 94 NAME ": unable to connect to HC at `%s': %s.\n", 95 hc_path, str_error(hc_phone)); 96 return hc_phone; 97 } 98 99 /* 100 * Verify address is okay. 101 */ 102 usb_address_t address = (usb_address_t) address_long; 82 int tmp_phone; 83 84 if (path[0] != '/') { 85 int hc_class_index = (int) strtol(path, NULL, 10); 86 char *dev_path; 87 rc = asprintf(&dev_path, "class/usbhc\\%d", hc_class_index); 88 if (rc < 0) { 89 internal_error(rc); 90 return rc; 91 } 92 devmap_handle_t handle; 93 rc = devmap_device_get_handle(dev_path, &handle, 0); 94 if (rc < 0) { 95 fprintf(stderr, 96 NAME ": failed getting handle of `devman://%s'.\n", 97 dev_path); 98 free(dev_path); 99 return rc; 100 } 101 tmp_phone = devmap_device_connect(handle, 0); 102 if (tmp_phone < 0) { 103 fprintf(stderr, 104 NAME ": could not connect to `%s'.\n", 105 dev_path); 106 free(dev_path); 107 return tmp_phone; 108 } 109 free(dev_path); 110 } else { 111 devman_handle_t handle; 112 rc = devman_device_get_handle(path, &handle, 0); 113 if (rc != EOK) { 114 fprintf(stderr, 115 NAME ": failed getting handle of `devmap::/%s'.\n", 116 path); 117 return rc; 118 } 119 tmp_phone = devman_device_connect(handle, 0); 120 if (tmp_phone < 0) { 121 fprintf(stderr, 122 NAME ": could not connect to `%s'.\n", 123 path); 124 return tmp_phone; 125 } 126 } 127 128 *phone = tmp_phone; 129 130 return EOK; 131 } 132 133 static int connect_with_address(int hc_phone, const char *str_address) 134 { 135 usb_address_t address = (usb_address_t) strtol(str_address, NULL, 0); 103 136 if ((address < 0) || (address >= USB11_ADDRESS_MAX)) { 104 137 fprintf(stderr, NAME ": USB address out of range.\n"); … … 106 139 } 107 140 108 /* 109 * Now, learn information about the device. 110 */ 111 int rc; 112 113 /* 114 * Dump information about possible match ids. 115 */ 116 match_id_list_t match_id_list; 117 init_match_ids(&match_id_list); 118 rc = usb_drv_create_device_match_ids(hc_phone, &match_id_list, address); 119 if (rc != EOK) { 120 fprintf(stderr, 121 NAME ": failed to fetch match ids of the device: %s.\n", 122 str_error(rc)); 123 return rc; 124 } 125 dump_match_ids(&match_id_list); 126 127 /* 128 * Get device descriptor and dump it. 129 */ 130 usb_standard_device_descriptor_t device_descriptor; 131 usb_dprintf(NAME, 1, 132 "usb_drv_req_get_device_descriptor(%d, %d, %p)\n", 133 hc_phone, (int) address, &device_descriptor); 134 135 rc = usb_drv_req_get_device_descriptor(hc_phone, address, 136 &device_descriptor); 137 if (rc != EOK) { 138 fprintf(stderr, 139 NAME ": failed to fetch standard device descriptor: %s.\n", 140 str_error(rc)); 141 return rc; 142 } 143 dump_standard_device_descriptor(&device_descriptor); 144 145 /* 146 * Get first configuration descriptor and dump it. 147 */ 148 usb_standard_configuration_descriptor_t config_descriptor; 149 int config_index = 0; 150 usb_dprintf(NAME, 1, 151 "usb_drv_req_get_bare_configuration_descriptor(%d, %d, %d, %p)\n", 152 hc_phone, (int) address, config_index, &config_descriptor); 153 154 rc = usb_drv_req_get_bare_configuration_descriptor(hc_phone, address, 155 config_index, &config_descriptor ); 156 if (rc != EOK) { 157 fprintf(stderr, 158 NAME ": failed to fetch standard configuration descriptor: %s.\n", 159 str_error(rc)); 160 return rc; 161 } 162 dump_standard_configuration_descriptor(config_index, 163 &config_descriptor); 164 165 void *full_config_descriptor = malloc(config_descriptor.total_length); 166 usb_dprintf(NAME, 1, 167 "usb_drv_req_get_full_configuration_descriptor(%d, %d, %d, %p, %zu)\n", 168 hc_phone, (int) address, config_index, 169 full_config_descriptor, config_descriptor.total_length); 170 171 rc = usb_drv_req_get_full_configuration_descriptor(hc_phone, address, 172 config_index, 173 full_config_descriptor, config_descriptor.total_length, NULL); 174 if (rc != EOK) { 175 fprintf(stderr, 176 NAME ": failed to fetch full configuration descriptor: %s.\n", 177 str_error(rc)); 178 return rc; 179 } 180 dump_buffer("Full configuration descriptor:", 181 full_config_descriptor, config_descriptor.total_length); 182 183 return EOK; 141 if (hc_phone < 0) { 142 fprintf(stderr, NAME ": no active host controller.\n"); 143 return ENOENT; 144 } 145 146 return dump_device(hc_phone, address); 147 } 148 149 150 int main(int argc, char *argv[]) 151 { 152 int hc_phone = -1; 153 154 if (argc <= 1) { 155 print_usage(argv[0]); 156 return -1; 157 } 158 159 int i; 160 do { 161 i = getopt_long(argc, argv, short_options, long_options, NULL); 162 switch (i) { 163 case -1: 164 break; 165 166 case '?': 167 print_usage(argv[0]); 168 return -1; 169 170 case 'h': 171 case ACTION_HELP: 172 print_usage(argv[0]); 173 return 0; 174 175 case 'a': 176 case ACTION_DEVICE_ADDRESS: { 177 int rc = connect_with_address(hc_phone, optarg); 178 if (rc != EOK) { 179 return rc; 180 } 181 break; 182 } 183 184 case 't': 185 case ACTION_HOST_CONTROLLER: { 186 int rc = set_new_host_controller(&hc_phone, 187 optarg); 188 if (rc != EOK) { 189 return rc; 190 } 191 break; 192 } 193 194 case 'd': 195 case ACTION_DEVICE: 196 break; 197 198 default: 199 break; 200 } 201 202 } while (i != -1); 203 204 return 0; 184 205 } 185 206
Note:
See TracChangeset
for help on using the changeset viewer.