static vsf_err_t versaloon_libusb_comm_init(void) { if (!usb_param_valid()) { usb_set_param(VERSALOON_VID, VERSALOON_PID, VERSALOON_INP, VERSALOON_OUTP, VERSALOON_IFACE); } versaloon_device_handle = find_usb_device(usb_param_vid(), usb_param_pid(), usb_param_interface(), VERSALOON_SERIALSTRING_INDEX, usb_param_serial(), VERSALOON_PRODUCTSTRING_INDEX, VERSALOON_PRODUCTSTRING); if (NULL == versaloon_device_handle) { if (usb_param_serial() != NULL) { LOG_ERROR("Not found vid=0x%04x,pid = 0x%04x,serial = %s.", usb_param_vid(), usb_param_pid(), usb_param_serial()); } else { LOG_ERROR("Not found vid=0x%04x,pid = 0x%04x.", usb_param_vid(), usb_param_pid()); } return VSFERR_FAIL; } return VSFERR_NONE; }
int zerocd_main(int argc, char **argv) { int i, i_vid, i_pid, res; if (argc != 3) { printf("Usage: %s [vid] [pid]\n", argv[0]); return 0; } signal(SIGPIPE, SIG_IGN); signal(SIGUSR1, SIG_IGN); signal(SIGUSR2, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGTERM, catch_sig_zerocd); daemon(0, 0); i_vid = strtol(argv[1], NULL, 16); i_pid = strtol(argv[2], NULL, 16); res = launch_usb_modeswitch(i_vid, i_pid, 1); if (res == 0) { for (i = 0; i < 6; i++) { sleep(1); if (!find_usb_device(i_vid, i_pid)) { res = 1; break; } } if (res == 0) launch_usb_modeswitch(i_vid, i_pid, 0); } return 0; }
void usb_set_config_test(uint16_t vid, uint16_t pid, uint32_t duration) { struct libusb20_device *pdev; struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; int x; int error; int failed; int exp; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 0); if (error) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } failed = 0; printf("Starting set config test for " "VID=0x%04x PID=0x%04x\n", vid, pid); for (x = 255; x > -1; x--) { error = libusb20_dev_set_config_index(pdev, x); if (error == 0) { if (x == 255) { printf("Unconfiguring USB device " "was successful\n"); } else { printf("Setting configuration %d " "was successful\n", x); } } else { failed++; } } ddesc = libusb20_dev_get_device_desc(pdev); if (ddesc != NULL) exp = ddesc->bNumConfigurations + 1; else exp = 1; printf("\n\n" "Set configuration summary\n" "Valid count: %d/%d %s\n" "Failed count: %d\n", 256 - failed, exp, (exp == (256 - failed)) ? "(expected)" : "(unexpected)", failed); libusb20_dev_free(pdev); }
int main(int argc, char *argv[]) { // Parsing args if(argc < 2 || argc > 3) print_help_and_exit(argv); int result, vid, pid; result = sscanf(argv[1], "%x:%x", &vid, &pid); if(!result) print_help_and_exit(argv); out = NULL; if(argc > 2) { // Opening log file out = fopen ( argv[2], "w" ); if(!out) { perror("Couldn't open log"); exit(-1); } setvbuf(out, NULL, _IONBF, 0); } // Dealing with libusb libusb_device_handle *dev_handle; libusb_context *ctx; result = libusb_init(&ctx); // Obtain libusb_context assert(result >= 0); libusb_set_debug(ctx, 3); libusb_device *usbdev = find_usb_device(ctx, vid, pid); assert(usbdev); bus_number = libusb_get_bus_number(usbdev); // Which bus to sniff device_number = libusb_get_device_address(usbdev); // Filter fprintf(stderr, "bus_number=%d, device_number=%d\n", bus_number, device_number); char pcap_if_name[10]; #ifdef LINUX system("modprobe usbmon"); sprintf(pcap_if_name, "usbmon%d", bus_number); #else #error "Unsupported platform" #endif printf("pcap device: %s\n", pcap_if_name); pcap_t *handle = pcap_open_live(pcap_if_name, BUFSIZ, 1, 1000, errbuf); if (handle == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", pcap_if_name, errbuf); #ifdef LINUX fprintf(stderr, "Please check for modules list.\n"); #endif return(2); } pcap_loop(handle, -1, process_packet, NULL); printf("exit\n"); pcap_close(handle); if(out) fclose(out); }
void usb_get_descriptor_test(uint16_t vid, uint16_t pid, uint32_t duration) { struct libusb20_device *pdev; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } libusb20_dev_free(pdev); }
/* initialize driver -- returns 1 on success, 0 on error */ static int dfc_init() { struct usb_device *usb_dev; int pipe_fd[2] = { -1, -1 }; LOGPRINTF(1, "initializing USB receiver"); init_rec_buffer(); usb_dev = find_usb_device(); if (usb_dev == NULL) { logprintf(LOG_ERR, "couldn't find a compatible USB device"); return 0; } /* A separate process will be forked to read data from the USB * receiver and write it to a pipe. hw.fd is set to the readable * end of this pipe. */ if (pipe(pipe_fd) != 0) { logperror(LOG_ERR, "couldn't open pipe"); return 0; } hw.fd = pipe_fd[0]; dev_handle = usb_open(usb_dev); if (dev_handle == NULL) { logperror(LOG_ERR, "couldn't open USB receiver"); goto fail; } child = fork(); if (child == -1) { logperror(LOG_ERR, "couldn't fork child process"); goto fail; } else if (child == 0) { usb_read_loop(pipe_fd[1]); } LOGPRINTF(1, "USB receiver initialized"); return 1; fail: if (dev_handle) { usb_close(dev_handle); dev_handle = NULL; } if (pipe_fd[0] >= 0) close(pipe_fd[0]); if (pipe_fd[1] >= 0) close(pipe_fd[1]); return 0; }
static struct vsllink *vsllink_usb_open(void) { usb_init(); struct usb_dev_handle *dev; dev = find_usb_device(versaloon_interface.usb_setting.vid, versaloon_interface.usb_setting.pid, versaloon_interface.usb_setting.interface, 0, NULL, 2, "Versaloon"); if (NULL == dev) return NULL; struct vsllink *result = malloc(sizeof(struct vsllink)); result->usb_handle = dev; return result; }
usb_dev_handle *canon_open_camera(void) { struct usb_device *camera; usb_dev_handle *camera_handle; /* intialize USB library */ usb_init(); usb_find_busses(); usb_find_devices(); /* search the USB bus for the appropriate camera */ fprintf(stderr, "Searching for Canon Powershot S40 camera... "); camera = find_usb_device(CANON_S40_VENDOR, CANON_S40_PRODUCT); if(camera == NULL) { fprintf(stderr, "not found.\n"); return NULL; } else fprintf(stderr, "found.\n"); /* claim the camera on the USB bus */ fprintf(stderr, "Opening connection to camera... "); camera_handle = usb_open(camera); if(camera_handle == NULL) { fprintf(stderr, "failed.\n"); return NULL; } else fprintf(stderr, "done.\n"); if(usb_claim_interface(camera_handle, 0) < 0) { fprintf(stderr, "Error: could not claim USB interface to camera.\n"); return NULL; } if(usb_set_configuration(camera_handle, 1) < 0) { fprintf(stderr, "Error: could not set configuration.\n"); return NULL; } if(usb_set_altinterface(camera_handle, 0) < 0) { fprintf(stderr, "Error: could not set altinterface.\n"); return NULL; } return camera_handle; }
void usb_control_ep_error_test(uint16_t vid, uint16_t pid) { struct LIBUSB20_CONTROL_SETUP_DECODED req; struct libusb20_device *pdev; uint8_t buffer[256]; int error; int fail = 0; int bus; int dev; int cfg; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 0); if (error) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } bus = libusb20_dev_get_bus_number(pdev); dev = libusb20_dev_get_address(pdev); for (cfg = 0; cfg != 255; cfg++) { LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); req.bmRequestType = 0x80; /* read */ req.bRequest = 0x06; /* descriptor */ req.wValue = 0x0200 | cfg; /* config descriptor */ req.wIndex = 0; req.wLength = 255; printf("Test #%d.1/3 ...\n", cfg); set_ctrl_ep_fail(-1,-1,0,0); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); if (error != 0) { printf("Last configuration index is: %d\n", cfg - 1); break; } printf("Test #%d.2/3 ...\n", cfg); set_ctrl_ep_fail(bus,dev,1,1); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); set_ctrl_ep_fail(-1,-1,0,0); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); if (error != 0) { printf("Cannot fetch descriptor (unexpected)\n"); fail++; } printf("Test #%d.3/3 ...\n", cfg); set_ctrl_ep_fail(bus,dev,0,1); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); set_ctrl_ep_fail(-1,-1,0,0); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); if (error != 0) { printf("Cannot fetch descriptor (unexpected)\n"); fail++; } } libusb20_dev_close(pdev); libusb20_dev_free(pdev); printf("Test completed detecting %d failures\nDone\n\n", fail); }
void usb_set_alt_interface_test(uint16_t vid, uint16_t pid) { struct libusb20_device *pdev; struct libusb20_config *config; int iter; int error; int errcnt; int n; int m; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } printf("Starting set alternate setting test " "for VID=0x%04x PID=0x%04x\n", vid, pid); config = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev)); if (config == NULL) { printf("Could not get configuration descriptor\n"); libusb20_dev_free(pdev); return; } iter = 0; errcnt = 0; for (n = 0; n != config->num_interface; n++) { /* detach kernel driver */ libusb20_dev_detach_kernel_driver(pdev, n); error = libusb20_dev_open(pdev, 0); if (error) printf("ERROR could not open device\n"); /* Try the alternate settings */ for (m = 0; m != config->interface[n].num_altsetting; m++) { iter++; if (libusb20_dev_set_alt_index(pdev, n, m + 1)) { printf("ERROR on interface %d alt %d\n", n, m + 1); errcnt++; } } /* Restore to default */ iter++; if (libusb20_dev_set_alt_index(pdev, n, 0)) { printf("ERROR on interface %d alt %d\n", n, 0); errcnt++; } libusb20_dev_close(pdev); } libusb20_dev_free(pdev); printf("\n" "Test summary\n" "============\n" "Interfaces tested: %d\n" "Errors: %d\n", iter, errcnt); }
void usb_set_and_clear_stall_test(uint16_t vid, uint16_t pid) { struct libusb20_device *pdev; struct libusb20_transfer *pxfer; int iter; int error; int errcnt; int ep; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 1); if (error) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } printf("Starting set and clear stall test " "for VID=0x%04x PID=0x%04x\n", vid, pid); iter = 0; errcnt = 0; for (ep = 2; ep != 32; ep++) { struct LIBUSB20_CONTROL_SETUP_DECODED setup_set_stall; struct LIBUSB20_CONTROL_SETUP_DECODED setup_get_status; uint8_t epno = ((ep / 2) | ((ep & 1) << 7)); uint8_t buf[1]; LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &setup_set_stall); setup_set_stall.bmRequestType = 0x02; /* write endpoint */ setup_set_stall.bRequest = 0x03; /* set feature */ setup_set_stall.wValue = 0x00; /* UF_ENDPOINT_HALT */ setup_set_stall.wIndex = epno; setup_set_stall.wLength = 0; LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &setup_get_status); setup_get_status.bmRequestType = 0x82; /* read endpoint */ setup_get_status.bRequest = 0x00; /* get status */ setup_get_status.wValue = 0x00; setup_get_status.wIndex = epno; setup_get_status.wLength = 1; if (libusb20_dev_check_connected(pdev) != 0) { printf("Device disconnected\n"); break; } pxfer = libusb20_tr_get_pointer(pdev, 0); error = libusb20_tr_open(pxfer, 1, 1, epno); if (error != 0) { printf("Endpoint 0x%02x does not exist " "in current setting. (%s, ignored)\n", epno, libusb20_strerror(error)); continue; } printf("Stalling endpoint 0x%02x\n", epno); /* set stall */ error = libusb20_dev_request_sync(pdev, &setup_set_stall, NULL, NULL, 250, 0); if (error != 0) { printf("Endpoint 0x%02x does not allow " "setting of stall. (%s)\n", epno, libusb20_strerror(error)); errcnt++; } /* get EP status */ buf[0] = 0; error = libusb20_dev_request_sync(pdev, &setup_get_status, buf, NULL, 250, 0); if (error != 0) { printf("Endpoint 0x%02x does not allow " "reading status. (%s)\n", epno, libusb20_strerror(error)); errcnt++; } else { if (!(buf[0] & 1)) { printf("Endpoint 0x%02x status is " "not set to stalled\n", epno); errcnt++; } } buf[0] = 0; error = libusb20_tr_bulk_intr_sync(pxfer, buf, 1, NULL, 250); if (error != LIBUSB20_TRANSFER_STALL) { printf("Endpoint 0x%02x does not appear to " "have stalled. Missing stall PID!\n", epno); errcnt++; } printf("Unstalling endpoint 0x%02x\n", epno); libusb20_tr_clear_stall_sync(pxfer); /* get EP status */ buf[0] = 0; error = libusb20_dev_request_sync(pdev, &setup_get_status, buf, NULL, 250, 0); if (error != 0) { printf("Endpoint 0x%02x does not allow " "reading status. (%s)\n", epno, libusb20_strerror(error)); errcnt++; } else { if (buf[0] & 1) { printf("Endpoint 0x%02x status is " "still stalled\n", epno); errcnt++; } } libusb20_tr_close(pxfer); iter++; } libusb20_dev_free(pdev); printf("\n" "Test summary\n" "============\n" "Endpoints tested: %d\n" "Errors: %d\n", iter, errcnt); }
void usb_suspend_resume_test(uint16_t vid, uint16_t pid, uint32_t duration) { struct timeval sub_tv; struct timeval ref_tv; struct timeval res_tv; struct libusb20_device *pdev; time_t last_sec; int iter; int error; int ptimo; int errcnt; int power_old; ptimo = 1; /* second(s) */ error = sysctlbyname("hw.usb.power_timeout", NULL, NULL, &ptimo, sizeof(ptimo)); if (error != 0) { printf("WARNING: Could not set power " "timeout to 1 (error=%d) \n", errno); } pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 0); if (error) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } power_old = libusb20_dev_get_power_mode(pdev); printf("Starting suspend and resume " "test for VID=0x%04x PID=0x%04x\n", vid, pid); iter = 0; errcnt = 0; gettimeofday(&ref_tv, 0); last_sec = ref_tv.tv_sec; while (1) { if (libusb20_dev_check_connected(pdev) != 0) { printf("Device disconnected\n"); break; } gettimeofday(&sub_tv, 0); if (last_sec != sub_tv.tv_sec) { printf("STATUS: ID=%u, ERR=%u\n", (int)iter, (int)errcnt); fflush(stdout); last_sec = sub_tv.tv_sec; } timersub(&sub_tv, &ref_tv, &res_tv); if ((res_tv.tv_sec < 0) || (res_tv.tv_sec >= (int)duration)) break; error = libusb20_dev_set_power_mode(pdev, (iter & 1) ? LIBUSB20_POWER_ON : LIBUSB20_POWER_SAVE); if (error) errcnt++; /* wait before switching power mode */ usleep(4100000 + (((uint32_t)usb_ts_rand_noise()) % 2000000U)); iter++; } /* restore default power mode */ libusb20_dev_set_power_mode(pdev, power_old); libusb20_dev_free(pdev); }
int main(int argc, char** argv) { Options opts = parse_args(argc, argv); usb_init(); usb_find_busses(); usb_find_devices(); struct usb_device* dev = find_usb_device(0x20d6, 0xcb17); int acc_x_min = 0; int acc_y_min = 0; int acc_z_min = 0; int acc_x_max = 0; int acc_y_max = 0; int acc_z_max = 0; int touch_pos_x = 0; int touch_pos_y = 0; bool finger_touching = false; bool pinch_touching = false; bool scroll_wheel = false; int wheel_distance = 0; if (dev) { std::unique_ptr<USBDevice> usbdev(new USBDevice(dev)); usbdev->print_info(); usbdev->detach_kernel_driver(0); usbdev->claim_interface(0); Evdev evdev; // init evdev if (opts.gamepad_mode) { evdev.add_abs(ABS_X, -1, 1, 0, 0); evdev.add_abs(ABS_Y, -1, 1, 0, 0); evdev.add_key(BTN_A); evdev.add_key(BTN_B); evdev.add_key(BTN_X); evdev.add_key(BTN_Y); evdev.add_key(BTN_START); evdev.add_key(BTN_SELECT); evdev.add_key(BTN_Z); } else if (opts.keyboard_mode) { evdev.add_key(KEY_LEFT); evdev.add_key(KEY_RIGHT); evdev.add_key(KEY_UP); evdev.add_key(KEY_DOWN); evdev.add_key(KEY_ENTER); evdev.add_key(KEY_SPACE); evdev.add_key(KEY_A); evdev.add_key(KEY_Z); evdev.add_key(KEY_ESC); evdev.add_key(KEY_TAB); } else if (opts.tablet_mode) { evdev.add_abs(ABS_X, 0, 1913, 0, 0); evdev.add_abs(ABS_Y, 0, 1076, 0, 0); evdev.add_abs(ABS_PRESSURE, 0, 143, 0, 0); evdev.add_key(BTN_TOUCH); evdev.add_key(BTN_TOOL_PEN); evdev.add_rel(REL_WHEEL); evdev.add_rel(REL_HWHEEL); evdev.add_rel(REL_X); evdev.add_rel(REL_Y); } else if (opts.touchpad_mode) { evdev.add_key(BTN_LEFT); evdev.add_key(BTN_RIGHT); evdev.add_key(BTN_MIDDLE); /* add_key(KEY_FORWARD); add_key(KEY_BACK); */ evdev.add_rel(REL_WHEEL); evdev.add_rel(REL_HWHEEL); evdev.add_rel(REL_X); evdev.add_rel(REL_Y); } evdev.finish(); usbdev->listen (3, [&](uint8_t* data, int size) { UDrawDecoder decoder(data, size); if (opts.keyboard_mode) { evdev.send(EV_KEY, KEY_LEFT, decoder.get_left()); evdev.send(EV_KEY, KEY_RIGHT, decoder.get_right()); evdev.send(EV_KEY, KEY_UP, decoder.get_up()); evdev.send(EV_KEY, KEY_DOWN, decoder.get_down()); evdev.send(EV_KEY, KEY_ENTER, decoder.get_cross()); evdev.send(EV_KEY, KEY_SPACE, decoder.get_circle()); evdev.send(EV_KEY, KEY_A, decoder.get_square()); evdev.send(EV_KEY, KEY_Z, decoder.get_triangle()); evdev.send(EV_KEY, KEY_ESC, decoder.get_start()); evdev.send(EV_KEY, KEY_TAB, decoder.get_select()); evdev.send(EV_SYN, SYN_REPORT, 0); } else if (opts.gamepad_mode) { evdev.send(EV_ABS, ABS_X, -1 * decoder.get_left() + 1 * decoder.get_right()); evdev.send(EV_ABS, ABS_Y, -1 * decoder.get_up() + 1 * decoder.get_down()); evdev.send(EV_KEY, BTN_A, decoder.get_cross()); evdev.send(EV_KEY, BTN_B, decoder.get_circle()); evdev.send(EV_KEY, BTN_X, decoder.get_square()); evdev.send(EV_KEY, BTN_Y, decoder.get_triangle()); evdev.send(EV_KEY, BTN_START, decoder.get_start()); evdev.send(EV_KEY, BTN_SELECT, decoder.get_select()); evdev.send(EV_KEY, BTN_Z, decoder.get_guide()); evdev.send(EV_SYN, SYN_REPORT, 0); } else if (opts.tablet_mode) { if (decoder.get_mode() == UDrawDecoder::PEN_MODE) { evdev.send(EV_ABS, ABS_X, decoder.get_x()); evdev.send(EV_ABS, ABS_Y, decoder.get_y()); evdev.send(EV_ABS, ABS_PRESSURE, decoder.get_pressure()); evdev.send(EV_KEY, BTN_TOOL_PEN, 1); if (decoder.get_pressure() > 0) { evdev.send(EV_KEY, BTN_TOUCH, 1); } else { evdev.send(EV_KEY, BTN_TOUCH, 0); } evdev.send(EV_SYN, SYN_REPORT, 0); } else { evdev.send(EV_KEY, BTN_TOOL_PEN, 0); evdev.send(EV_SYN, SYN_REPORT, 0); } } else if (opts.touchpad_mode) { evdev.send(EV_KEY, BTN_LEFT, decoder.get_right()); evdev.send(EV_KEY, BTN_RIGHT, decoder.get_left()); if (decoder.get_mode() == UDrawDecoder::FINGER_MODE) { if (!finger_touching) { touch_pos_x = decoder.get_x(); touch_pos_y = decoder.get_y(); finger_touching = true; if (touch_pos_x > 1800) { scroll_wheel = true; wheel_distance = 0; } else { scroll_wheel = false; } } if (scroll_wheel) { wheel_distance += (decoder.get_y() - touch_pos_y) / 10; int rel = wheel_distance/10; if (rel != 0) { evdev.send(EV_REL, REL_WHEEL, -rel); wheel_distance -= rel; touch_pos_x = decoder.get_x(); touch_pos_y = decoder.get_y(); //std::cout << rel << " " << wheel_distance << std::endl; } } else { evdev.send(EV_REL, REL_X, decoder.get_x() - touch_pos_x); evdev.send(EV_REL, REL_Y, decoder.get_y() - touch_pos_y); touch_pos_x = decoder.get_x(); touch_pos_y = decoder.get_y(); } } else { finger_touching = false; } evdev.send(EV_SYN, SYN_REPORT, 0); } else if (opts.accelerometer_mode) { if (size != 27) { std::cerr << "unknown read size: " << size << std::endl; } else { //data[0]; int x = data[15] * 255 + data[17]; // x - pen: 3px resolution int y = data[16] * 255 + data[18]; // y - finger: 1px resolution if (data[11] == 0x00) { std::cout << "nothing"; } else if (data[11] == 0x80) { std::cout << boost::format("finger: x: %4d y: %4d") % x % y; } else if (data[11] == 0x40) { std::cout << boost::format("pen: x: %4d y: %4d - pressure: %3d") % x % y % (int(data[13]) - 0x70); } else { std::cout << boost::format("pinch: x: %4d y: %4d distance: %4d orientation: %02x") % x % y % int(data[12]) % (int(data[11]) - 0xc0); } int acc_x = ((data[19] + data[20] * 255) - 512); int acc_y = ((data[21] + data[22] * 255) - 512); int acc_z = ((data[23] + data[24] * 255) - 512); acc_x_min = std::min(acc_x, acc_x_min); acc_y_min = std::min(acc_y, acc_y_min); acc_z_min = std::min(acc_z, acc_z_min); acc_x_max = std::max(acc_x, acc_x_max); acc_y_max = std::max(acc_y, acc_y_max); acc_z_max = std::max(acc_z, acc_z_max); // acc_min -31 -33 -33 // acc_max 31 28 29 // ~22 == 1g // accelerometer std::cout << boost::format("%4d %4d %4d - %4d %4d %4d - %4d %4d %4d") % acc_x % acc_y % acc_z % acc_x_min % acc_y_min % acc_z_min % acc_x_max % acc_y_max % acc_z_max; std::cout << std::endl; } } else { print_raw_data(std::cout, data, size); std::cout << std::endl; } if (false) { evdev.send(EV_KEY, BTN_RIGHT, decoder.get_left()); evdev.send(EV_KEY, BTN_MIDDLE, decoder.get_up()); evdev.send(EV_KEY, BTN_LEFT, decoder.get_right()); evdev.send(EV_REL, REL_WHEEL, decoder.get_triangle() ? 1 : 0); evdev.send(EV_REL, REL_WHEEL, decoder.get_cross() ? -1 : 0); evdev.send(EV_KEY, KEY_BACK, decoder.get_circle()); evdev.send(EV_KEY, KEY_FORWARD, decoder.get_square()); if (false) { // FIXME: does not work as is, needs throttling if (decoder.get_mode() == UDrawDecoder::PINCH_MODE) { if (!pinch_touching) { touch_pos_x = decoder.get_x(); touch_pos_y = decoder.get_y(); pinch_touching = true; } evdev.send(EV_REL, REL_HWHEEL, decoder.get_x() - touch_pos_x); evdev.send(EV_REL, REL_WHEEL, decoder.get_y() - touch_pos_y); touch_pos_x = decoder.get_x(); touch_pos_y = decoder.get_y(); } else { pinch_touching = false; } evdev.send(EV_SYN, SYN_REPORT, 0); } } }); } return 0; }
int usb_find(ifc_match_func callback) { return find_usb_device("/dev/bus/usb", callback); }
int main() { int busnum, devnum; char *filename; uint8_t *buf; const size_t BUFLEN = 128*1024; /* Edit these settings. TODO: Use command line args. */ int vend = 0x04a9; int prod = 0x2228; const char logname[] = "log.bin"; signal(SIGINT, sigint_handler); /* Check we're root */ if (getuid() != 0) { fprintf(stderr, "Not running as root. Exiting.\n"); return 1; } /* Look for USB device */ if (!find_usb_device(vend, prod, &busnum, &devnum)) { fprintf(stderr, "Device %04x:%04x not found.\n", vend, prod); return 1; } fprintf(stderr, "Found device: bus %d, device %d\n", busnum, devnum); /* Open usbmon */ asprintf(&filename, "/dev/usbmon%d", busnum); d = open(filename, O_RDWR); if (d < 0) { fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno)); return 1; } free(filename); /* Prepare usbmon ring-buffer */ if (ioctl(d, MON_IOCT_RING_SIZE, BUFLEN) < 0) { fprintf(stderr, "Cannot allocate ring buffer: %s\n", strerror(errno)); return 1; } buf = mmap(NULL, BUFLEN, PROT_READ, MAP_SHARED, d, 0); if (buf == MAP_FAILED) { fprintf(stderr, "Cannot mmap ring buffer: %s\n", strerror(errno)); return 1; } /* Open log file. */ logfile = fopen(logname, "w+"); if (!logfile) { fprintf(stderr, "Cannot open %s: %s\n", logname, strerror(errno)); return 1; } /* Main capture loop */ const size_t PCKCNT = 100; uint32_t offvec[PCKCNT]; int i, nflush = 0; struct usbmon_mfetch fetch; struct usbmon_packet *hdr; for (;;) { /* Fetch packets */ fetch.offvec = offvec; fetch.nfetch = PCKCNT; fetch.nflush = nflush; if (ioctl(d, MON_IOCX_MFETCH, &fetch) == -1) { fprintf(stderr, "usbmon read error: %s\n", strerror(errno)); return 1; } nflush = fetch.nfetch; ntotal += nflush; /* Process packets to/from busnum:devnum */ for (i = 0; i < nflush; i++) { hdr = (struct usbmon_packet *) &buf[offvec[i]]; if (hdr->type == '@' || hdr->devnum != devnum) continue; process_urb(hdr, ((unsigned char *)hdr) + sizeof(*hdr), logfile); } } /* Dead code */ return 0; }
/* initialize driver -- returns 1 on success, 0 on error */ static int ati_init() { struct usb_device *usb_dev; int pipe_fd[2] = { -1, -1 }; LOGPRINTF(1, "initializing USB receiver"); init_rec_buffer(); /* A separate process will be forked to read data from the USB * receiver and write it to a pipe. hw.fd is set to the readable * end of this pipe. */ if (pipe(pipe_fd) != 0) { logperror(LOG_ERR, "couldn't open pipe"); return 0; } hw.fd = pipe_fd[0]; usb_dev = find_usb_device(); if (usb_dev == NULL) { logprintf(LOG_ERR, "couldn't find a compatible USB device"); return 0; } if (!find_device_endpoints(usb_dev)) { logprintf(LOG_ERR, "couldn't find device endpoints"); return 0; } dev_handle = usb_open(usb_dev); if (dev_handle == NULL) { logperror(LOG_ERR, "couldn't open USB receiver"); goto fail; } if (usb_claim_interface(dev_handle, 0) != 0) { logperror(LOG_ERR, "couldn't claim USB interface"); goto fail; } errno = 0; if ((usb_interrupt_write(dev_handle, dev_ep_out->bEndpointAddress, init1, sizeof(init1), 100) != sizeof(init1)) || (usb_interrupt_write(dev_handle, dev_ep_out->bEndpointAddress, init2, sizeof(init2), 100) != sizeof(init2))) { logprintf(LOG_ERR, "couldn't initialize USB receiver: %s", errno ? strerror(errno) : "short write"); goto fail; } child = fork(); if (child == -1) { logperror(LOG_ERR, "couldn't fork child process"); goto fail; } else if (child == 0) { usb_read_loop(pipe_fd[1]); } LOGPRINTF(1, "USB receiver initialized"); return 1; fail: if (dev_handle) { usb_close(dev_handle); dev_handle = NULL; } if (pipe_fd[0] >= 0) close(pipe_fd[0]); if (pipe_fd[1] >= 0) close(pipe_fd[1]); return 0; }
usb_handle *usb_open(ifc_match_func callback) { return find_usb_device("/sys/bus/usb/devices", callback); }
int main(int argc, char** argv) { srand(time(NULL)); if (argc == 2 && strcmp("list", argv[1]) == 0) { usb_init(); usb_find_busses(); usb_find_devices(); list_usb_devices(); } else if ((argc == 4 || argc == 5 || argc == 6) && (strcmp("cat", argv[1]) == 0 || strcmp("read", argv[1]) == 0 || strcmp("write", argv[1]) == 0)) { uint16_t idVendor; uint16_t idProduct; int interface = 0; int endpoint = 1; if (sscanf(argv[2], "0x%hx", &idVendor) == 1 && sscanf(argv[3], "0x%hx", &idProduct) == 1) { if (argc >= 5) interface = atoi(argv[4]); if (argc == 6) endpoint = atoi(argv[5]); usb_init(); usb_find_busses(); usb_find_devices(); struct usb_device* dev = find_usb_device(idVendor, idProduct); if (!dev) { std::cout << "Error: Device (" << boost::format("idVendor: 0x%04hx, idProduct: 0x%04hx") % idVendor % idProduct << ") not found" << std::endl; } else { if (strcmp("cat", argv[1]) == 0) { std::cout << "Reading data from: " << dev << " Interface: " << interface << " Endpoint: " << endpoint << std::endl; cat_usb_device(dev, interface, endpoint); } else if (strcmp("read", argv[1]) == 0) { read_usb_device(dev, interface, endpoint); } else if (strcmp("write", argv[1]) == 0) { write_usb_device(dev, interface, endpoint); } } } else { std::cout << "Error: Expected IDVENDOR IDPRODUCT" << std::endl; } } else { std::cout << "Usage: " << argv[0] << " list\n" << " " << argv[0] << " cat IDVENDOR IDPRODUCT [INTERFACE] [ENDPOINT]" << std::endl; } }
usb_handle *usb_open(ifc_match_func callback, const char *serial) { return find_usb_device("/dev/bus/usb", callback, serial); }
void usb_get_string_desc_test(uint16_t vid, uint16_t pid) { struct libusb20_device *pdev; uint32_t x; uint32_t y; uint32_t valid; uint8_t *buf; int error; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 0); if (error) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } buf = malloc(256); if (buf == NULL) { printf("Cannot allocate memory\n"); libusb20_dev_free(pdev); return; } valid = 0; printf("Starting string descriptor test for " "VID=0x%04x PID=0x%04x\n", vid, pid); for (x = 0; x != 256; x++) { if (libusb20_dev_check_connected(pdev) != 0) { printf("Device disconnected\n"); break; } printf("%d .. ", (int)x); fflush(stdout); error = libusb20_dev_req_string_simple_sync(pdev, x, buf, 255); if (error == 0) { printf("\nINDEX=%d, STRING='%s' (Default language)\n", (int)x, buf); fflush(stdout); } else { continue; } valid = 0; for (y = 0; y != 65536; y++) { if (libusb20_dev_check_connected(pdev) != 0) { printf("Device disconnected\n"); break; } error = libusb20_dev_req_string_sync(pdev, x, y, buf, 256); if (error == 0) valid++; } printf("String at INDEX=%d responds to %d " "languages\n", (int)x, (int)valid); } printf("\nDone\n"); free(buf); libusb20_dev_free(pdev); }
usb_handle *usb_open(ifc_match_func callback) { return find_usb_device(callback); }
void usb_port_reset_test(uint16_t vid, uint16_t pid, uint32_t duration) { struct timeval sub_tv; struct timeval ref_tv; struct timeval res_tv; struct libusb20_device *pdev; int error; int iter; int errcnt; time_t last_sec; /* sysctl() - no set config */ pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 0); if (error) { libusb20_dev_free(pdev); printf("Could not open USB device\n"); return; } iter = 0; errcnt = 0; gettimeofday(&ref_tv, 0); last_sec = ref_tv.tv_sec; while (1) { gettimeofday(&sub_tv, 0); if (last_sec != sub_tv.tv_sec) { printf("STATUS: ID=%u, ERR=%u\n", (int)iter, (int)errcnt); fflush(stdout); last_sec = sub_tv.tv_sec; } timersub(&sub_tv, &ref_tv, &res_tv); if ((res_tv.tv_sec < 0) || (res_tv.tv_sec >= (int)duration)) break; if (libusb20_dev_reset(pdev)) { errcnt++; usleep(50000); } if (libusb20_dev_check_connected(pdev) != 0) { printf("Device disconnected\n"); break; } iter++; } libusb20_dev_reset(pdev); libusb20_dev_free(pdev); }
static void exec_host_modem_test(struct modem *p, uint16_t vid, uint16_t pid) { struct libusb20_device *pdev; uint8_t ntest = 0; uint8_t x; uint8_t in_ep; uint8_t out_ep; uint8_t iface; int error; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } if (p->use_vendor_specific) find_usb_endpoints(pdev, 255, 255, 255, 0, &iface, &in_ep, &out_ep, 0); else find_usb_endpoints(pdev, 2, 2, 1, 0, &iface, &in_ep, &out_ep, 1); if ((in_ep == 0) || (out_ep == 0)) { printf("Could not find USB endpoints\n"); libusb20_dev_free(pdev); return; } printf("Attaching to: %s @ iface %d\n", libusb20_dev_get_desc(pdev), iface); if (libusb20_dev_open(pdev, 2)) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } if (libusb20_dev_detach_kernel_driver(pdev, iface)) { printf("WARNING: Could not detach kernel driver\n"); } p->xfer_in = libusb20_tr_get_pointer(pdev, 0); error = libusb20_tr_open(p->xfer_in, 65536 / 4, 1, in_ep); if (error) { printf("Could not open USB endpoint %d\n", in_ep); libusb20_dev_free(pdev); return; } p->xfer_out = libusb20_tr_get_pointer(pdev, 1); error = libusb20_tr_open(p->xfer_out, 65536 / 4, 1, out_ep); if (error) { printf("Could not open USB endpoint %d\n", out_ep); libusb20_dev_free(pdev); return; } p->usb_dev = pdev; p->usb_iface = iface; p->errors = 0; if (p->control_ep_test) ntest += 7; if (p->data_stress_test) ntest += 1; if (ntest == 0) { printf("No tests selected\n"); } else { if (p->control_ep_test) { for (x = 1; x != 8; x++) { usb_modem_control_ep_test(p, (p->duration + ntest - 1) / ntest, x); } } if (p->data_stress_test) { usb_modem_data_stress_test(p, (p->duration + ntest - 1) / ntest); } } printf("\nDone\n"); libusb20_dev_free(pdev); }
Transport* usb_open(ifc_match_func callback) { std::unique_ptr<usb_handle> handle = find_usb_device(callback); return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr; }