LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-04-2016, 06:11 PM   #1
david_8274
Member
 
Registered: Jun 2013
Location: California
Distribution: Ubuntu, Fedora
Posts: 75

Rep: Reputation: Disabled
sample user space code for sending/receiving through USB


Hi,

I am working on an embedded Linux project. Currently I am trying to implement a user space code that send/receive through the USB interface. Can someone point me to a sample code so I can get a rough idea on what I need to do?

Thanks,
Wei Xu
 
Old 01-05-2016, 10:52 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
You'll likely need a kernel module to communicate with your USB device (unless the manufacturer of your USB device already provides one), then your user space code would communicate with the kernel module. USB is a complicated protocol, it's not like serial where you can just open up the device and start dumping bytes (unless the manufacturer of the USB device you're talking to provides a kernel module that allows that, eg: FTDI). If you provide more info on the USB device you'll be communicating with it would help.

Last edited by suicidaleggroll; 01-05-2016 at 10:53 AM.
 
Old 01-05-2016, 11:26 AM   #3
david_8274
Member
 
Registered: Jun 2013
Location: California
Distribution: Ubuntu, Fedora
Posts: 75

Original Poster
Rep: Reputation: Disabled
Hi,

Thank you for the reply. To ensure I understand you correctly, is this code structure correct?
>>We have the existing USB protocol stack in kernel space.
>>We develop a device driver on top of the USB protocol stack.
>>In user space, we create a device node in /dev that use the device driver that we create. We then access the device with open, read, write and ioctl. In other words, in user space, the device would appear to be a specific device, not a USB device?

Thanks,
Wei Xu



Quote:
Originally Posted by suicidaleggroll View Post
You'll likely need a kernel module to communicate with your USB device (unless the manufacturer of your USB device already provides one), then your user space code would communicate with the kernel module. USB is a complicated protocol, it's not like serial where you can just open up the device and start dumping bytes (unless the manufacturer of the USB device you're talking to provides a kernel module that allows that, eg: FTDI). If you provide more info on the USB device you'll be communicating with it would help.
 
Old 01-05-2016, 11:57 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
That is one way of going about it, yes. Though it's the kernel module that creates the entry in /dev, not the user-space program.

There might be simpler ways though, depending on the USB device you want to interface with.
 
Old 01-05-2016, 12:13 PM   #5
david_8274
Member
 
Registered: Jun 2013
Location: California
Distribution: Ubuntu, Fedora
Posts: 75

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
That is one way of going about it, yes. Though it's the kernel module that creates the entry in /dev, not the user-space program.

There might be simpler ways though, depending on the USB device you want to interface with.
Hi,

Thank you again for the reply!
Could you provides an example code structure for the "simpler ways" so I can get a rough idea?

Thanks,
Wei Xu
 
Old 01-05-2016, 12:37 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
It all depends on the USB device.
 
Old 01-08-2016, 01:34 PM   #7
greenleaf
Member
 
Registered: Feb 2004
Location: Chester, UK
Distribution: Linux From Scratch. 64 bit. Kernel 4.16.7. Fluxbox.
Posts: 48

Rep: Reputation: 19
Smile Generic access via libusb

You can get generic access to USB devices using libusb. It is user-mode, and, as such, does not require access to the kernel. Just search on libusb and you will find the web site. It includes pages on the application programmer interface (API).

You will probably need the command set of whatever USB device you are interested in, and may find it advantageous to wrap that in your own code - particularly if you are writing object oriented code.

Some years ago I went through a similar exercise with a USB device. To be honest, it was quite helpful to monitor the USB traffic in Windows in order to find out what commands it needed, particularly for initialisation. Once I got hold of the full command set (with help from the device manufacturer), things became much easier, although it was necessary to write a lot of code before I could be sure of controlling every known aspect of its behaviour. Once that was done, I could move on to using it in a larger project, taking it as a know quantity and utility.

Good luck with libusb if you decide to try that route.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending data from kernel to user space ratbat Linux - Newbie 3 04-02-2013 02:33 AM
how to call socket prog code written in user space from kernel space???HELP kurt2 Programming 2 07-15-2009 09:56 PM
USB, how to R/W to port, sample C code? bapigoo9 Linux - Newbie 6 11-25-2008 08:01 AM
Sending, receiving through the USB interface The_Nerd Programming 5 02-13-2005 02:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration