Accessing Linux /dev/USB as standard files to communicate with USB device
Jim, I don't think you can escape the need to write a driver and just manage to read the USB file in /dev
. Because who defines as to what should happen when you do a read()
on the USB device file? And who defines what action should be initiated when you invoke sysioctl()
? Your driver! In other words, the device files are themselves incapable of anything until they are supported by the underlying drivers. In fact, you can treat the device files to be an abstraction of the underlying driver! So, no driver, no use of device file :(
I suggest you go through the following articles about how to write a driver and also understand the USB internals-
http://www.linux-usb.org/USB-guide/c15.html
http://www.linuxjournal.com/article/4786 ( Slightly outdated )
How to enumerate USB devices *and* read/write to them?
libusb
is an ideal choice. There are also several examples for such a case as you described.
In your case, you just need to:
- Create a default context.
- Request the device list.
- Search for the device with the desired vendor/product ID.
- Open the device, and then communicate via synchronous or asynchronous I/O calls.
- Clean up the requested device list.
- When done, close off the device handle and destroy the context.
This allows you to write a userspace application/library with relative ease. I've used it with microphones, cameras, and DIY hardware (mini assembly line devices I've built) with considerable success. The devs on the mailing list are also very helpful, though one or two fellows tend to be somewhat caustic to new members if their posts aren't perfectly written.
Get access to USB device on Linux (libusb-1.0)?
On modern Linux systems, udevd
(man 7 udev
) creates the device nodes for USB devices when they're plugged in. Add a udev rule that matches your device (eg. you could match by USB Vendor and Product IDs), and sets the OWNER
/ GROUP
/ MODE
of the device node.
The best approach is probably to create a new group for users who should be able to access the device, then set that as the group owner in the udev rule. You may also need to use MODE
to ensure that it has group read/write permissions. Eg. your rule will probably look something like:
SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffee", ATTRS{idProduct}=="5a5a", MODE="0660", GROUP="foobar"
Communicate to Android Device over USB
First you have to know what USB kind of your android device is. How this device is recognized on your linux box? As a mass-storage or CDC or anything else? Then you can try to communicate with it. And you don't have to create any device in /dev/ to communicate with android via USB. Take a look at libusb library.
In your example - /dev/ttyUSBx is created because kernel knows that this is communication device class. So you also need to know what kind of USB device is this android device.
How could be notified once USB device is attached/detached in linux system and read the file from USB
I'd add udev rule first with running your custom script. Something like:
ACTION=="add", KERNEL=="sd?1", SUBSYSTEMS=="usb", RUN+="/path/to/your/script %k"
%k
is kernel parameter that is passed to your script.
From udev man:
$kernel, %k
The kernel name for this device.
In the script I'd use curl
. If you don't need some tricky logic. If so, I'd use python.
Anyway I think udev is perfect for this problem.
Related Topics
Change System Date Time in Docker Containers Without Impacting Host
Unsupported Protocol While Download Tar.Gz Package
Git - Windows and Linux Line-Endings
Searching Multiple Patterns (Words) with Ack
Git Bash Is Displaying Strange Characters on Windows 7
How to Replace Single Quotes with Another Character in Sed
What Is Kernel Section Mismatch
Error: Ld.So: Object 'Libgtk3-Nocsd.So.0' from Ld_Preload Cannot Be Preloaded
Shell Script Change Directory with Variable
Run Docker in Ubuntu Live Disk
How to "Git Pull" - Host Key Verification Failed
Install.Packages("Devtools") on R 3.0.2 Fails in Ubuntu 14.04
Copy a Directory Structure with File Names Without Content
Kill a Java Process (In Linux) by Process Name Instead of Pid