Skip to content

Instantly share code, notes, and snippets.

@Julio-Guerra
Created June 1, 2016 11:19
Show Gist options
  • Save Julio-Guerra/24dab7ea3ecb95f54a7c41eba662d957 to your computer and use it in GitHub Desktop.
Save Julio-Guerra/24dab7ea3ecb95f54a7c41eba662d957 to your computer and use it in GitHub Desktop.
USB Control Message to flush a FTDI device without detaching the ftdi_sio kernel driver Raw
// gcc -I/usr/include/libftdi1 -I/usr/include/libusb-1.0 usb.c
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <asm/byteorder.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>
#include <ftdi.h>
void usb_control_message(int fd, struct usbdevfs_ctrltransfer* urb) {
int rc = ioctl(fd, USBDEVFS_CONTROL, &urb);
if (rc < 0) {
perror(NULL);
exit(-1);
}
}
int main(void)
{
int fd = open("/dev/bus/usb/002/040", O_RDWR);
if (fd == -1) {
perror(NULL);
return -1;
}
struct usbdevfs_ctrltransfer urb;
memset(&urb, 0, sizeof (urb));
// Flush RX
urb.bRequestType = FTDI_DEVICE_OUT_REQTYPE;
urb.bRequest = SIO_RESET_REQUEST;
urb.wValue = SIO_RESET_PURGE_RX;
urb.wIndex = 1;
urb.data = NULL;
urb.wLength = 0;
urb.timeout = 5000; /* in milliseconds */
usb_control_message(fd, &urb);
// Flush TX
urb.wValue = SIO_RESET_PURGE_TX;
usb_control_message(fd, &urb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment