libusb for USB target user space driver - linux

I would like to implement a USB device driver in user space by means of libusb. I'm using a Linux machine supporting a USB OTG controller which is switched to device mode. The USB host is another machine which needs to communicate with my Linux machine by means of a USB vendor specific interface with a bulk in/out interface.
I would like to know if it is possible to use libusb to communicate with the USB host on the other side. Or, if libusb could be used only for host side functionalities.
If libusb can't be used is there any other way to implement the device driver in user space?
Thank you.

As of my knowledge this is not possible. (Vanilla) libusb is only for host usage because the whole process of how to use libusb can only provide this mode.
You can find an answer in the FAQ of the old libusb (before libusbx, the abandoning of the old libusb and the rename of libusbx to the new libusb):
libusb FAQ
Also there is a question on SO to this topic with some suggestions:
How to communicate with the USB Host from a Linux USB Client

Related

Connecting to Serial USB device via WebUSB

I have hardware which is connected to the mac or windows via USB Serial Mode. Currently, I have a nodejs application which sends and receive messages to this hardware using "serial" node module.
I am trying to connect to the hardware from WebPage. I used WebUSB serial.js. I am able to see the device and guess it is connecting. But when I try to send/receive the message - it says unable to claim the interface.
I would like to know can I use the WebUSB for USB with serial support?
The claimInterface() call is failing because your operating system has already loaded the USB serial class driver for this device (or perhaps a vendor-specific one such as FTDI). USB interfaces can only be claimed by one thing at a time. The option to change the device firmware so that the interface is no longer recognized by the driver (modify the interface class or product IDs) then you will be able to claim the interface and use code like the Arduino WebUSB library's serial.js that you found.
The other option is to wait for Chrome to finish implementing the Serial API. Status on that can be tracked here.
What worked for me was to run sudo modprobe -r ftdi_sio in the console.
That way, it unloads the USB from the OS and makes it available for WebUSB to use it and claim the interface.

How to detect when a usb cable is connected/disconnected on the device side in Linux 2.6.37?

I have a embedded device that runs linux 2.6.37.
I want my application to know when the USB is connected.
Currently I can achieve this by pooling
/sys/devices/platform/musb/musb-hdrc.0/vbus.
However this approach does not distinguish between a USB charger or a USB host.
I found this udev approach but I don't think it's available in my version of the kernel. because I did not find any USB related nodes in my /dev. This discussing also shows that it might not be feasible, ether.
I also found linux hotplug and tried the netlink example, but I didn't see any output running the example when I connect/disconnect the USB cable.
What I want to do is to detect connection type on the device, when USB is connected, and prepare (unmount file system) and switch to g_file_storage if device is connected to a host, and do nothing if device is connect to a charger.
How shall I achieve this?
To achieve that, you can use the inotify(7) feature, available in all linux kernels to be awaken as soon as some device node gets created in /sys.
To know what type of device you have, you have to read the usb info from proper usb ioctl call (or if you are not a kernel interface expert, using the libusb interface) to get the device vendor, device id and device class fields coming from the device. Normally, the hotplug software gets informed on these clase of events (via a special socket). The most probably reason you don't get the device properly initialized is some misconfiguration in the config files for udev system, which normally has one entry for each possible device vendor/device id pair and allows it to load the appropiate device driver to control it. The process continues with the device driver module creating dynamically the actual devices, and they'll appear in the /dev/ filesystem as a consequence of some other kernel event to udevd.
Read apropiate documents in <linux_src>/Documentation (this directory directory belongs to the linux kernel source code, so you'll probably need to install it), and udevd(8) man pages to be able to add a new usb.
On 2.6.37 kernel, this could be done by polling
/sys/devices/platform/musb-omap2430.0/musb-hdrc.0/mode
If handshake with host is successful then it will read as "peripheral", if fail it'll be "idle".

Tracing traffic in Linux-based usb gadget (CDC/NCM)

I have a linux platform* that is connected as a usb device to an automotive device which acts as the USB host. The two devices should communicate over CDC/NCM, but the linux platform is not recognised by the automotive device and therefore the connection is not established. Surprisingly a connection to my computer is established correctly.
I now need to create a trace of that USB connection in order to check if there is an error in the USB handshake that can't be handled by the automotive device. As I cannot access the USB host, I need to create the trace from the gadget side.
I tried using usbmon and tcpdump, but this seems to work only for USB controllers configured as hosts on the tracing platform, not for ones configured as devices.
How can I configure usbmon to work also on devices?
If that is not possible are there any other possibilities to achieve this? (preferrably without hacking any drivers...)
Or do I have to use a Hardware USB sniffer?
BTW, all required modules (esp. g_ncm) are correctly loaded.
Thank you for your help!
stefan
*custom distribution on a freescale iMX6 processor (ARM), Kernel Version 3.0.35

Accessing Linux /dev/USB as standard files to communicate with USB device

I'm researching ways to communicate with a USB device in Linux and would prefer to not write a Linux Kernel driver. I understand that libusb exists and is a user-land library that would work, but our embedded device doesn't support usbfs ( and would be really a pain to change kernels to add the support ).
So my question is basically this: Is it possible / advisable to communicate with a USB device by directly reading and writing to the /dev/USB or the udev file corresponding to the USB device thus bypassing the need for a custom Linux Driver and usbfs?
I'm hoping it's possible to communicate using the USB devices protocol just by reading / writing protocol packets directly through file-type read/write commands once the /dev/USB or udev device file is open.
Thoughts and suggestions please.
FOLLOW UP:
Since the USB device I needed to talk to is a USB HID class device, I was able to use libudev and the standard Linux USB HID RAW driver by reading / writing directly to /dev/hidraw0 ( or the appropriate /dev/hidraw device ). It wasn't necessary to write a custom driver for a simple USB HID 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 )

Read a Device in GNU/Linux or FreeBSD

I am wondering, do you need a specific device driver to read a usb device in Linux, or should it just be able to be read. If I connect my cell phone or iPod touch to my linux box, it is not found is /proc/partitions and thus is not a mountable device by fdisks standards, though gnomes nautilus does in fact mount the iPod but not the windows mobile touch pro cell phone.
So I am interested, If I just wanted to read a device(iPod touch) in linux, how can I do so. How can I get a hold of a descriptor of a set usb device so I can read it.
Thanks all.
You can access raw USB endpoints under /dev/usbdev. There is user-space libusb that makes it easier.
Unfortunately there is no simple concept of "just read it" for USB devices (I am assuming that you are not referring to reading and writing the data on the USB bus that make up the USB protocol). In short, you always need a device driver for accessing a USB device and it is up to the driver to implement "the abstraction" of the device used by the system (disk, serial device, etc).

Resources