By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,967 Members | 2,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,967 IT Pros & Developers. It's quick & easy.

USB programming in Linux

P: 8
I am trying to open a file to a USB(pendrive) device through library functions using C program.I used libusb to write the code.
I can able to open the device trough libusb functions.but i am not getting how to open a file using the functions.

Please provide some inputs/help

Dec 14 '10 #1
Share this Question
Share on Google+
4 Replies

P: 256
If I were trying to write to a file on a usb pendrive I'd mount the drive (using mount command) and let Linux handle all the low level details.

Your program only needs to know where the thing is mounted and just write to a file (as you would any other filesystem).

You only need libusb if you're trying to communicate to some device for which there is no higher order driver. (or if you simply like to experiment I guess)
Dec 15 '10 #2

P: 8
thanks for the reply.

If we know the mount point we can give that path in the code and we can open a file.but if i don't know the mount point how i can open the file in that pen drive.Any possibility to open with the file using libusb functions.

if (!(handle = usb_open(dev))) {
printf("Could not open USB device\n");
return NULL;

after this function i tried to open a file but i am not getting how to provide the path to that.
please help
Dec 16 '10 #3

P: 256
You don't need to be working at the usb level, just file system level.

There are various ways to know the mount point. If linux mounts for you then you can ask the user for input (select file location or something), or inspect /etc/fstab or /proc/mounts (various other methods exist I'm sure).

The thing you need to work out is how do you know which device your program is going to write to. What if there's 3 USB drives plugged in? I think you're best served by asking the user for input. This is what every program that saves a file does.
Dec 16 '10 #4

P: 8
Any libusb function is there to open a file dynamically without knowing the mount point?
can we find any function in USB stack to open a file file like that(without knowing mount point).

1 more questions is i need to read the data from usb mouse.while reading the data it is giving interrupt read error -7.can u tell me what might be the problem.
here is my code:

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>

#define VERSION "0.1.0"
#define VENDOR_ID 0x046d
#define PRODUCT_ID 0xc016

// HID Class-Specific Requests values. See section 7.2 of the HID specifications
#define HID_GET_REPORT 0x01
#define HID_GET_IDLE 0x02
#define HID_GET_PROTOCOL 0x03
#define HID_SET_REPORT 0x09
#define HID_SET_IDLE 0x0A

//#define LINUX
const static int PACKET_CTRL_LEN=2;
const static int PACKET_INT_LEN=2;
const static int INTERFACE=0;
const static int ENDPOINT_INT_IN=0x81; /* endpoint 0x81 address for IN */
const static int ENDPOINT_INT_OUT=0x01; /* endpoint 1 address for OUT */
const static int TIMEOUT=5000; /* timeout in ms */

void bad(const char *why) {
fprintf(stderr,"Fatal error> %s\n",why);

static struct libusb_device_handle *devh = NULL;

static int find_lvr_hidusb(void)
devh = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);
return devh ? 0 : -EIO;

static int test_interrupt_transfer(void)
int r,i;
int transferred;

unsigned char buf[5];

r = libusb_interrupt_transfer(devh, ENDPOINT_INT_IN, buf,sizeof(buf),
&transferred, TIMEOUT);
// printf("bytes read %d\n",r);
if (r < 0) {
fprintf(stderr, "Interrupt read error %d\n", r);
return r;

if (transferred < PACKET_INT_LEN) {
fprintf(stderr, "Interrupt transfer short read (%d)\n", r);
return -1;

for(i = 0;i < 5; i++) {
if(i%8 == 0)
printf("%02x ",buf[i]);


return 0;
int main(void)
int r = 1,j;
unsigned char buf[64];

r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "Failed to initialise libusb\n");

r = find_lvr_hidusb();
if (r < 0) {
fprintf(stderr, "Could not find/open LVR Generic HID device\n");
goto out;
printf("Successfully find the LVR Generic HID device\n");

#ifdef LINUX
libusb_detach_kernel_driver(devh, 0);

r = libusb_set_configuration(devh, 1);
if (r < 0) {
fprintf(stderr, "libusb_set_configuration error %d\n", r);
goto out;
printf("Successfully set usb configuration 1\n");
r = libusb_claim_interface(devh, 0);
if (r < 0) {
fprintf(stderr, "libusb_claim_interface error %d\n", r);
goto out;
printf("Successfully claimed interface\n");


libusb_release_interface(devh, 0);
// libusb_reset_device(devh);

return r >= 0 ? r : -r;
Dec 17 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.