Creating custom virtual usb device using usb-vhci - linux

I am new to working on virtual USB device simulation in Linux. So far I have installed the virtual host control (vhci) libraries as per this tutorial (http://sourceforge.net/p/usb-vhci/wiki/Home/) and can see a virtual USB device being created which has some typical specifications that the library implements (Bus 05 in the image with the vendor and product IDs being "dead" and "beef" respectively).
However I want the created virtual device to have the specifications of a real device I have at hand (a mouse, for example).
So how to enumerate and initialize a virtual USB device with the same credentials as another device?

The kernel module (vhci-hcd) is only a (virtual) host controller that you can attach virtual devices to.
If you want to emulate eg a mouse you should get the libusb_vhci from the same source, and look into the examples. These are bare minimum starting points that does nothing except for the basic usb device handling. You'll have to extend this with all descriptors and protocol handling for a USB HID mouse or whatever you want to emulate.
http://www.usbmadesimple.co.uk/ums_5.htm should be a good starting point.
you can use lsusb and in particular lsusb -D to dump the descriptors of devices you have connected.

Related

How can we create a virtio platform device?

I am writing a virtio driver for a device that is seen in the original driver as a platform device, that is the parent of a character device in the device tree.
In the tutorials that I followed, The virtio drivers are always done for "regular" character devices.
I wanted to create my virtio driver for it to expose a fake platform device as a parent of a "regular" character device.
My question is this one: Should I detect my virtual device in the regular way using the probe function and then create my platform device inside of it or is there a recommended way or order to create both (the platform device and the character device (child))?
When we get the virtio device and trigger the associated probe, we can then register a platform driver and then add some platform devices correponding to your needs.
And your device will appear as a platform device instead of a virtio_device in the guest OS.

Enumerate commands available for a usb chip in fedora 24

I am learning to program a USB device (iBall 3.5g USB Dongle) using libusb.h header library.
Until now I am able to identify my device using the Vendor ID and also open the device for operation.
As a next step I would like to know the available commands (or the controls) for example : command to scan the surroundings for available GSM networks.
Obviously I will have to talk to the devices' firmware to extract the necessary information.
I tried to search for the technical datasheet for the 3g dongle, but couldn't find any.
The dongle is powered by a Qualcomm chip
Do you know any of the methods in which I can get the control commands for a usb device ?
Thanks in advance.
There is no simple procedure for figuring out what commands a USB device has. You need to use a combination of looking at the descriptors reported by the device, seeing if the device supports any particular USB device class, reading the USB specification, and maybe doing some reverse engineering using a protocol analyzer.
A good first step would be for you to use lsusb -v to print human-readable descriptions of the device's USB descriptors.

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".

Process of device driver detection in linux

Wanted to know how a device is detected in Linux? What exactly is the workflow of the device driver in device detection?
It is the Kernel's job to detect devices as it has the lowest level access to the available hardware. When the Kernel scans through all available addresses it maintains a list of Vendor and Device IDs.
To use PCI bus devices as an example, there is a Vendor ID and a Device ID associated with all PCI devices.
Device drivers are written in such a way as to identify to the Kernel what kinds of devices the driver is able to control. Drivers may advertise that they can handle more than one vendor and device type combination.
The Kernel will allocate a driver to each device based on these IDs. A similar process is in place for USB devices. Older technologies like legacy devices (serial ports, parallel, ps2 mice/keyboards) will have explicitly hardcoded methods of associating particular drivers with devices.
You can use the Linux commands lsusb and lspci to see the available devices and IDs on your system.
So in direct answer to your question - the device driver usually does nothing to detect the device, at least in the first instance. Once the driver is associated with a device (by the Kernel) the driver will likely do further interrogation of the device to ensure it contains the right firmware or is the right hardware revision, etc.

USB device detection on /dev directory on Linux

Using the lsusb command in Linux I have come to know about bus and device numbers, along with its name of newly attached USB devices.
But how can I know on which device directory (/dev/*) USB device get attached in Linux using command lines only?
It isn't a rule that every device has to show up directly under /dev/, but some device classes will be nested under sub-directories inside /dev/
USB device drivers are a bit different,
If you connect a valid USB device, USB HCI would read the VID:PID and will tell the usb-core that the device with VID:PID combination is connected.
If the usbcore detects any registered driver for the VID:PID combination, it will couple it with the device, and the device file creation would happen accordingly
The device will show in /dev/bus/usb/.., even if, the corresponding driver is not present, to indicate that the device was detected.
You need to have the device driver to have the device in action/operation.
You can verify whether a device driver is coupled to the device through
cat /sys/kernel/debug/usb/devices
Each detected USB device will have an entry here, and also shows the "Driver=" field, to show which driver is associated with your device.
Now, IFF there is a driver, that makes an entry in appropriate /dev tree, you will find the device there.
NOT every device will show up directly under /dev/ in the first level.
say, your mouse/keyboard will not show-up directly under /dev, but inside /dev/input/
Likewise, IF the connected USB device is a char/block device, it MAY show up there, that too have exceptions.
If your device is and ethernet/wifi device, the interface device will NOT show up under /dev/, cross-check with your existing eth0, wlan0, they will not appear directly under /dev/, but will in /proc/net/devices
sda/b/c shows up under /dev directly, because they are block devices and are managed by udev, as such.
Here is an example of lsusb output on my laptop:
Bus 004 Device 123: ID 2001:3c1b D-Link Corp. DWA-127 Wireless N 150 High-Gain Adapter(rev.A1) [Ralink RT3070]
It's the device 123 on the bus 004. /dev/bus/usb/004/123 is just the file for the interested device.
The path might vary on different kernels. The result above holds on kernel 3.15.2

Resources