LinuxQuestions.org
Visit Jeremy's Blog.
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 10-05-2008, 10:43 PM   #1
bapigoo9
Member
 
Registered: Aug 2008
Posts: 107

Rep: Reputation: 16
Question USB, how to R/W to port, sample C code?


Linux 2.6.17.X

Want to find some sample C code on how to Read and Write R/W to USB. I have seen something similar for the serial port.
 
Old 10-05-2008, 11:16 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 122Reputation: 122
All USB access is performed by the kernel, and device nodes are created on a per-device basis for those devices found on the USB bus (and with applicable drivers to handle them).
 
Old 10-05-2008, 11:41 PM   #3
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
If you're talking about a USB-to-serial converter (or even a serial-like interface to a USB modem), then just open /dev/ttyUSB* and treat it like any ordinary serial port. Some defective hardware doesn't support things like hardware handshaking and modem control lines though.

If you want to learn how to write a USB driver, have a look at the kernel source. The core USB driver handles all of the USB protocol, so all you have to do with your own driver is implement the various required function calls to do registration, initialization, r/w, and hardware-specific controls.
 
Old 11-24-2008, 11:11 PM   #4
bapigoo9
Member
 
Registered: Aug 2008
Posts: 107

Original Poster
Rep: Reputation: 16
Quote:
If you want to learn how to write a USB driver, have a look at the kernel source
That is an idea...I was looking for some basic example code that would read and write to the USB port, and some documentation too. The kernel code is more complex, but is a good idea to think about. Thanks.
Quote:
device nodes are created on a per-device basis
Is any of this under user or user-level code control, or is all within kernel space? And, are there any naming conventions that are adhered to, or does that change from kernel to kernel and distro to distro?
 
Old 11-24-2008, 11:37 PM   #5
kevinalm
Member
 
Registered: Oct 2003
Location: Iowa
Distribution: LFS 5.0, building 6.3, win98se, multiboot
Posts: 288

Rep: Reputation: 30
Device node creation is done by udev. It is governed by a set of conf files in /etc/udev or something like that. Very user configurable, but _complicated_ .

Last edited by kevinalm; 11-24-2008 at 11:39 PM.
 
Old 11-25-2008, 04:19 AM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Quote:
Originally Posted by bapigoo9 View Post
The kernel code is more complex, but is a good idea to think about.
The kernel code for the USB core is complex because the USB protocol specification is extremely complex. Fortunately the USB core takes care of all the horrible stuff; the rest is fairly straightforward. Have a look at the kernel code for, say, 'usbserial' (although even this simple driver may be complicated by the need to interact with the tty layer).
 
Old 11-25-2008, 09:01 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,181

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The beauty of Unix/Linux is ... "a device is a file." The various entries in /dev can (with permission...) be opened, read, and written-to as "a file."

ioctl() is a generic system-call that devices provide "to do 'everything else'," which of course may be somewhat device-specific.

When you plug-in a USB device, a system daemon that watches for such things goes through a process of recognizing the device and determining how to set it up. A single device may appear more-than-once in the /dev structure. As an application writer, though, you should not have to have any concerns with that.
 
  


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
MRG Sample Code xingkia Programming 0 05-05-2008 09:43 PM
system() and execl anyone got any sample code please? twirl Programming 2 09-08-2005 03:01 AM
Perl: What's this mean? (Code sample) Benr Programming 5 10-23-2004 08:20 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 12:16 PM
Any sample code of how to use Rand_seed() cybercop12us Programming 0 04-03-2003 08:25 AM


All times are GMT -5. The time now is 12:27 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