Changeset e765ccb in mainline
- Timestamp:
- 2011-05-20T10:44:53Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- e7079cf
- Parents:
- d1fb591
- Location:
- uspace
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/mkbd/Makefile
rd1fb591 re765ccb 33 33 $(LIBUSBDEV_PREFIX)/libusbdev.a \ 34 34 $(LIBUSB_PREFIX)/libusb.a \ 35 $(LIBDRV_PREFIX)/libdrv.a 35 $(LIBDRV_PREFIX)/libdrv.a \ 36 $(LIBUSBHID_PREFIX)/libusbhid.a 36 37 EXTRA_CFLAGS = \ 37 38 -I$(LIBUSB_PREFIX)/include \ 38 39 -I$(LIBUSBDEV_PREFIX)/include \ 39 -I$(LIBDRV_PREFIX)/include 40 -I$(LIBDRV_PREFIX)/include \ 41 -I$(LIBUSBHID_PREFIX)/include 40 42 41 43 SOURCES = \ -
uspace/app/mkbd/main.c
rd1fb591 re765ccb 47 47 #include <usb/host.h> 48 48 #include <usb/driver.h> 49 #include <usb/hid/iface.h> 49 50 #include <usb/dev/pipes.h> 50 51 … … 93 94 //} 94 95 95 static bool try_parse_class_and_address(const char *path,96 devman_handle_t *out_hc_handle, usb_address_t *out_device_address)97 {98 size_t class_index;99 size_t address;100 int rc;101 char *ptr;102 103 rc = str_size_t(path, &ptr, 10, false, &class_index);104 if (rc != EOK) {105 return false;106 }107 if ((*ptr == ':') || (*ptr == '.')) {108 ptr++;109 } else {110 return false;111 }112 rc = str_size_t(ptr, NULL, 10, true, &address);113 if (rc != EOK) {114 return false;115 }116 rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle);117 if (rc != EOK) {118 return false;119 }120 if (out_device_address != NULL) {121 *out_device_address = (usb_address_t) address;122 }123 return true;124 }125 126 static bool resolve_hc_handle_and_dev_addr(const char *devpath,127 devman_handle_t *out_hc_handle, usb_address_t *out_device_address)128 {129 int rc;130 131 /* Hack for QEMU to save-up on typing ;-). */132 if (str_cmp(devpath, "qemu") == 0) {133 devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";134 }135 136 /* Hack for virtual keyboard. */137 if (str_cmp(devpath, "virt") == 0) {138 devpath = "/virt/usbhc/usb00_a1/usb00_a2";139 }140 141 if (try_parse_class_and_address(devpath,142 out_hc_handle, out_device_address)) {143 return true;144 }145 146 char *path = str_dup(devpath);147 if (path == NULL) {148 return ENOMEM;149 }150 151 devman_handle_t hc = 0;152 bool hc_found = false;153 usb_address_t addr = 0;154 bool addr_found = false;155 156 /* Remove suffixes and hope that we will encounter device node. */157 while (str_length(path) > 0) {158 /* Get device handle first. */159 devman_handle_t dev_handle;160 rc = devman_device_get_handle(path, &dev_handle, 0);161 if (rc != EOK) {162 free(path);163 return false;164 }165 166 /* Try to find its host controller. */167 if (!hc_found) {168 rc = usb_hc_find(dev_handle, &hc);169 if (rc == EOK) {170 hc_found = true;171 }172 }173 /* Try to get its address. */174 if (!addr_found) {175 addr = usb_device_get_assigned_address(dev_handle);176 if (addr >= 0) {177 addr_found = true;178 }179 }180 181 /* Speed-up. */182 if (hc_found && addr_found) {183 break;184 }185 186 /* Remove the last suffix. */187 char *slash_pos = str_rchr(path, '/');188 if (slash_pos != NULL) {189 *slash_pos = 0;190 }191 }192 193 free(path);194 195 if (hc_found && addr_found) {196 if (out_hc_handle != NULL) {197 *out_hc_handle = hc;198 }199 if (out_device_address != NULL) {200 *out_device_address = addr;201 }202 return true;203 } else {204 return false;205 }206 }96 //static bool try_parse_class_and_address(const char *path, 97 // devman_handle_t *out_hc_handle, usb_address_t *out_device_address) 98 //{ 99 // size_t class_index; 100 // size_t address; 101 // int rc; 102 // char *ptr; 103 104 // rc = str_size_t(path, &ptr, 10, false, &class_index); 105 // if (rc != EOK) { 106 // return false; 107 // } 108 // if ((*ptr == ':') || (*ptr == '.')) { 109 // ptr++; 110 // } else { 111 // return false; 112 // } 113 // rc = str_size_t(ptr, NULL, 10, true, &address); 114 // if (rc != EOK) { 115 // return false; 116 // } 117 // rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle); 118 // if (rc != EOK) { 119 // return false; 120 // } 121 // if (out_device_address != NULL) { 122 // *out_device_address = (usb_address_t) address; 123 // } 124 // return true; 125 //} 126 127 //static bool resolve_hc_handle_and_dev_addr(const char *devpath, 128 // devman_handle_t *out_hc_handle, usb_address_t *out_device_address) 129 //{ 130 // int rc; 131 132 // /* Hack for QEMU to save-up on typing ;-). */ 133 // if (str_cmp(devpath, "qemu") == 0) { 134 // devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1"; 135 // } 136 137 // /* Hack for virtual keyboard. */ 138 // if (str_cmp(devpath, "virt") == 0) { 139 // devpath = "/virt/usbhc/usb00_a1/usb00_a2"; 140 // } 141 142 // if (try_parse_class_and_address(devpath, 143 // out_hc_handle, out_device_address)) { 144 // return true; 145 // } 146 147 // char *path = str_dup(devpath); 148 // if (path == NULL) { 149 // return ENOMEM; 150 // } 151 152 // devman_handle_t hc = 0; 153 // bool hc_found = false; 154 // usb_address_t addr = 0; 155 // bool addr_found = false; 156 157 // /* Remove suffixes and hope that we will encounter device node. */ 158 // while (str_length(path) > 0) { 159 // /* Get device handle first. */ 160 // devman_handle_t dev_handle; 161 // rc = devman_device_get_handle(path, &dev_handle, 0); 162 // if (rc != EOK) { 163 // free(path); 164 // return false; 165 // } 166 167 // /* Try to find its host controller. */ 168 // if (!hc_found) { 169 // rc = usb_hc_find(dev_handle, &hc); 170 // if (rc == EOK) { 171 // hc_found = true; 172 // } 173 // } 174 // /* Try to get its address. */ 175 // if (!addr_found) { 176 // addr = usb_device_get_assigned_address(dev_handle); 177 // if (addr >= 0) { 178 // addr_found = true; 179 // } 180 // } 181 182 // /* Speed-up. */ 183 // if (hc_found && addr_found) { 184 // break; 185 // } 186 187 // /* Remove the last suffix. */ 188 // char *slash_pos = str_rchr(path, '/'); 189 // if (slash_pos != NULL) { 190 // *slash_pos = 0; 191 // } 192 // } 193 194 // free(path); 195 196 // if (hc_found && addr_found) { 197 // if (out_hc_handle != NULL) { 198 // *out_hc_handle = hc; 199 // } 200 // if (out_device_address != NULL) { 201 // *out_device_address = addr; 202 // } 203 // return true; 204 // } else { 205 // return false; 206 // } 207 //} 207 208 208 209 static void print_usage(char *app_name) … … 218 219 } 219 220 221 #define MAX_PATH_LENGTH 1024 222 220 223 int main(int argc, char *argv[]) 221 224 { … … 228 231 char *devpath = argv[1]; 229 232 230 /* The initialization is here only to make compiler happy. */ 231 devman_handle_t hc_handle = 0; 232 usb_address_t dev_addr = 0; 233 bool found = resolve_hc_handle_and_dev_addr(devpath, 234 &hc_handle, &dev_addr); 235 if (!found) { 236 fprintf(stderr, NAME ": device `%s' not found " 237 "or not of USB kind. Exiting.\n", devpath); 238 return -1; 239 } 233 // /* The initialization is here only to make compiler happy. */ 234 // devman_handle_t hc_handle = 0; 235 // usb_address_t dev_addr = 0; 236 // bool found = resolve_hc_handle_and_dev_addr(devpath, 237 // &hc_handle, &dev_addr); 238 // if (!found) { 239 // fprintf(stderr, NAME ": device `%s' not found " 240 // "or not of USB kind. Exiting.\n", devpath); 241 // return -1; 242 // } 243 244 // int rc; 245 // usb_hc_connection_t conn; 246 247 // usb_hc_connection_initialize(&conn, hc_handle); 248 // rc = usb_hc_connection_open(&conn); 249 // if (rc != EOK) { 250 // printf(NAME ": failed to connect to HC: %s.\n", 251 // str_error(rc)); 252 // return -1; 253 // } 254 255 // devman_handle_t dev_handle; 256 // rc = usb_hc_get_handle_by_address(&conn, dev_addr, &dev_handle); 257 // if (rc != EOK) { 258 // printf(NAME ": failed getting handle to the device: %s.\n", 259 // str_error(rc)); 260 // return -1; 261 // } 262 263 // usb_hc_connection_close(&conn); 240 264 241 265 int rc; 242 usb_hc_connection_t conn; 243 244 usb_hc_connection_initialize(&conn, hc_handle); 245 rc = usb_hc_connection_open(&conn); 266 267 devman_handle_t dev_handle = 0; 268 rc = devman_device_get_handle(devpath, &dev_handle, 0); 246 269 if (rc != EOK) { 247 printf( NAME ": failed to connect to HC: %s.\n",270 printf("Failed to get handle from devman: %s.\n", 248 271 str_error(rc)); 249 return -1; 250 } 251 usb_address_t addr = 0; 252 253 devman_handle_t dev_handle; 254 rc = usb_hc_get_handle_by_address(&conn, addr, &dev_handle); 255 if (rc != EOK) { 256 printf(NAME ": failed getting handle to the device: %s.\n", 257 str_error(rc)); 258 return -1; 259 } 260 261 usb_hc_connection_close(&conn); 272 return rc; 273 } 262 274 263 275 rc = devman_device_connect(dev_handle, 0); 264 276 if (rc < 0) { 265 printf(NAME ": failed to connect to the device : %s.\n",266 str_error(rc));267 return -1;277 printf(NAME ": failed to connect to the device (handle %" 278 PRIun "): %s.\n", dev_handle, str_error(rc)); 279 return rc; 268 280 } 269 281 … … 271 283 printf("Got phone to the device: %d\n", dev_phone); 272 284 273 274 // size_t class_index = 0; 275 // size_t failed_attempts = 0; 276 277 // while (failed_attempts < MAX_FAILED_ATTEMPTS) { 278 // class_index++; 279 // devman_handle_t hc_handle = 0; 280 // int rc = usb_ddf_get_hc_handle_by_class(class_index, &hc_handle); 281 // if (rc != EOK) { 282 // failed_attempts++; 283 // continue; 284 // } 285 // char path[MAX_PATH_LENGTH]; 286 // rc = devman_get_device_path(hc_handle, path, MAX_PATH_LENGTH); 287 // if (rc != EOK) { 288 // continue; 289 // } 290 // print_found_hc(class_index, path); 291 // print_hc_devices(hc_handle); 292 // } 285 char path[MAX_PATH_LENGTH]; 286 rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH); 287 if (rc != EOK) { 288 return ENOMEM; 289 } 290 291 printf("Device path: %s\n", path); 292 293 size_t size; 294 rc = usbhid_dev_get_event_length(dev_phone, &size); 295 if (rc != EOK) { 296 printf("Failed to get event length: %s.\n", 297 str_error(rc)); 298 return rc; 299 } 300 301 printf("Event length: %zu\n", size); 302 int32_t *event = (int32_t *)malloc(size); 303 if (event == NULL) { 304 // hangup phone? 305 return ENOMEM; 306 } 307 308 printf("Event length: %zu\n", size); 309 310 size_t actual_size; 311 312 while (1) { 313 // get event from the driver 314 printf("Getting event from the driver.\n"); 315 rc = usbhid_dev_get_event(dev_phone, event, size, &actual_size, 316 0); 317 if (rc != EOK) { 318 // hangup phone? 319 printf("Error in getting event from the HID driver:" 320 "%s.\n", str_error(rc)); 321 break; 322 } 323 324 printf("Got buffer: %p, size: %zu\n", event, actual_size); 325 } 293 326 294 327 return 0; -
uspace/lib/drv/generic/remote_usbhid.c
rd1fb591 re765ccb 71 71 } 72 72 73 int len = hid_iface->get_event_length(fun);73 size_t len = hid_iface->get_event_length(fun); 74 74 if (len == 0) { 75 75 len = EEMPTY; … … 100 100 return; 101 101 } 102 /* Check that length is even number. Truncate otherwise. */103 if ((len % 2) == 1) {104 len--;105 }102 // /* Check that length is even number. Truncate otherwise. */ 103 // if ((len % 2) == 1) { 104 // len--; 105 // } 106 106 if (len == 0) { 107 107 async_answer_0(data_callid, EINVAL); … … 111 111 int rc; 112 112 113 size_t items = len / 2; 114 uint16_t *usage_pages_and_usages = malloc(sizeof(uint16_t) * len); 115 if (usage_pages_and_usages == NULL) { 113 int32_t *data = malloc(len); 114 if (data == NULL) { 116 115 async_answer_0(data_callid, ENOMEM); 117 116 async_answer_0(callid, ENOMEM); 118 117 } 119 118 120 size_t act_items; 121 int rc = hid_iface->get_event(fun, usage_pages_and_usages, 122 usage_pages_and_usages + items, items, &act_items, flags); 119 size_t act_length; 120 rc = hid_iface->get_event(fun, data, len, &act_length, flags); 123 121 if (rc != EOK) { 124 free( usage_pages_and_usages);122 free(data); 125 123 async_answer_0(data_callid, rc); 126 124 async_answer_0(callid, rc); 127 125 } 128 if (act_ items >= items) {126 if (act_length >= len) { 129 127 /* This shall not happen. */ 130 128 // FIXME: how about an assert here? 131 act_ items = items;129 act_length = len; 132 130 } 133 131 134 async_data_read_finalize(data_callid, usage_pages_and_usages, 135 act_items * 2 * sizeof(uint16_t)); 132 async_data_read_finalize(data_callid, data, act_length); 136 133 137 free( usage_pages_and_usages);134 free(data); 138 135 139 136 async_answer_0(callid, EOK); -
uspace/lib/usbhid/include/usb/hid/iface.h
rd1fb591 re765ccb 38 38 #include <sys/types.h> 39 39 40 int usbhid_dev_get_event_length(int );41 int usbhid_dev_get_event(int, uint16_t *, uint16_t *, size_t, size_t *,40 int usbhid_dev_get_event_length(int, size_t *); 41 int usbhid_dev_get_event(int, int32_t *, size_t, size_t *, 42 42 unsigned int); 43 43 -
uspace/lib/usbhid/src/hidiface.c
rd1fb591 re765ccb 46 46 * @return Number of usages returned or negative error code. 47 47 */ 48 int usbhid_dev_get_event_length(int dev_phone )48 int usbhid_dev_get_event_length(int dev_phone, size_t *size) 49 49 { 50 50 if (dev_phone < 0) { … … 56 56 IPC_M_USBHID_GET_EVENT_LENGTH, &len); 57 57 if (rc == EOK) { 58 return (int) len; 59 } else { 60 return rc; 58 *size = (size_t) len; 61 59 } 60 61 return rc; 62 62 } 63 63 … … 74 74 * @return Error code. 75 75 */ 76 int usbhid_dev_get_event(int dev_phone, uint16_t *usage_pages, uint16_t *usages,77 size_t usage_count, size_t *actual_usage_count, unsigned int flags)76 int usbhid_dev_get_event(int dev_phone, int32_t *buf, 77 size_t size, size_t *actual_size, unsigned int flags) 78 78 { 79 79 if (dev_phone < 0) { 80 80 return EINVAL; 81 81 } 82 if (( usage_pages == NULL) || (usages== NULL)) {82 if ((buf == NULL)) { 83 83 return ENOMEM; 84 84 } 85 if ( usage_count== 0) {85 if (size == 0) { 86 86 return EINVAL; 87 87 } 88 88 89 size_t buffer_size = sizeof(uint16_t) * usage_count * 2;89 size_t buffer_size = size; 90 90 uint16_t *buffer = malloc(buffer_size); 91 91 if (buffer == NULL) { … … 128 128 } 129 129 130 size_t actual_size = IPC_GET_ARG2(data_request_call); 131 size_t items = actual_size / 2; 130 size_t act_size = IPC_GET_ARG2(data_request_call); 132 131 133 132 /* Copy the individual items. */ 134 memcpy( usage_pages, buffer, items * sizeof(uint16_t));135 memcpy(usages, buffer + items, items * sizeof(uint16_t));133 memcpy(buf, buffer, act_size); 134 // memcpy(usages, buffer + items, items * sizeof(int32_t)); 136 135 137 if (actual_ usage_count!= NULL) {138 *actual_ usage_count = items;136 if (actual_size != NULL) { 137 *actual_size = act_size; 139 138 } 140 139
Note:
See TracChangeset
for help on using the changeset viewer.