473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

USB programming in Linux

Hi,
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

Thanks,
mahesh.
Dec 14 '10 #1
4 9474
mac11
256 100+
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
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
mac11
256 100+
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
hi,
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>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.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 HID_SET_PROTOCOL 0x0B
#define HID_REPORT_TYPE_INPUT 0x01
#define HID_REPORT_TYPE_OUTPUT 0x02
#define HID_REPORT_TYPE_FEATURE 0x03

#define CTRL_IN LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUS B_RECIPIENT_INTERFACE
#define CTRL_OUT LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBU SB_RECIPIENT_INTERFACE
//#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);
exit(17);
}

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("\n");
printf("%02x ",buf[i]);

}
printf("\n");

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");
exit(1);
}

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);
#endif

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");
test_interrupt_transfer();

printf("\n");

libusb_release_interface(devh, 0);
out:
// libusb_reset_device(devh);
libusb_close(devh);

libusb_exit(NULL);
return r >= 0 ? r : -r;
}
Dec 17 '10 #5

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

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
2
by: beliavsky | last post by:
Linux Journal annually polls its readers on questions such as their favorite programming language. In the 2005 poll, Python is 2nd, its highest ranking ever. Below are the results by year. I wish...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
10
by: stylecomputers | last post by:
Hey guys, I am absolutely new to Linux programming, with no w######s programming experience except a small amount of C++ console apps. Reasonably new to Linux, BSD etc, got good sound networking...
17
by: Nirjhar Oberoi | last post by:
Hi, I am new to Linux and wanted to know how to use GCC to Compile the Code written in C? I dont want to use EMacs or VI for my editor. Can you suggest a good IDE for linux for C Programming.. ...
12
by: Magesh | last post by:
Can anyone suggest me a good book to learn C/C++ programming under Linux platform?
12
by: Sophie000 | last post by:
I am implementing a peer-to-peer program: When a computer A receives “get file XXX” from stdio, it broadcasts (floods) a request packet to ask others. Both Computer B and C have the file so they...
1
by: Concepts Systems | last post by:
Hello All, Kernel Programming is the key to the digital city of device drivers: If you know the language for drivers you can get a device to do almost anything you want. Learn how to write...
4
by: kid joe | last post by:
Hello I've got interested in learning some basic sound programming bits in C... mainly I want to know how to go about accessing the sound devices - reading from them mainly - in windows and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.