There are two choices for how to handle USB devices in a virtual platform. A USB device can be modeled using C/C++ programming, or a physical USB device can be plugged into a computer and attached to the simulator. The Xilinx QEMU for Zynq uses physical USB devices. The Cadence SystemC Virtual Platform for Zynq uses either technique.
One of the drawbacks I learned when using physical USB devices is the need for root permission to access the Linux device node from user space. This is required to open the driver and perform operations to control the USB device from an application. There are a couple of techniques I found when considering the best solution:
- Run the simulator as root
- Change owner of /proc/bus/usb
All of this assumes you have root permission (or sudo capability) on your machine.
First, I found that running QEMU as root is not too difficult; just put a sudo in front of the command used to start qemu-system-arm. For example, for Zynq QEMU, I added the sudo xterm -e in front of the command so it runs as root in a new terminal and then I added the USB device I want to attach to the simulator using -usbdevice.
Doing the same for the Cadence Virtual Platform for Zynq is not as easy. The SystemC simulator spawns multiple programs and just putting sudo in front of nscs_run or irun doesn't work very well because the permission doesn't always flow down to all of the child programs that get launched. Running the simulator as root is also undesirable because logfiles will be created with root permission. This makes it hard to read them using a normal user account. Because of all this a better solution is needed.
The second solution of changing permission on /proc/bus/usb didn't help me much because the kernel on my machine didn't have usbfs enabled and thus /proc/bus/usb didn't even show up. Newer Linux kernels don't use this method anymore to identify and export information about USB devices. It is possible to enable it by building a new kernel, but it's not something I was interested in doing.
The best solution I found to connect a physical USB device to the Zynq SystemC simulation involves using udev to assign the permissions when the USB device is plugged in. For reference, this was tested on Ubuntu 12.04.
First, create a new file in /etc/udev/rules.d
The first USB device I worked with was a Kingston memory stick so I created the file named 80-kingston.rules
The name of the file is not too important as long as it ends in .rules. The files are read in lexical order so the files starting with numbers are read first then the files with letters are read. The other files on my system in this directory started with lower numbers so I just picked 80.
Here are the contents of the file; it contains the vendor id and the product id for the USB device as given by the lsusb command. These are important as they are used to identify which USB device to connect to a virtual platform simulation. QEMU uses the same vendor id and product id scheme.
I made the group adm, but this can be any group of which you are a member. Use the groups command to see what groups you are in. This will enable permission for the users in the group to use the USB device from user space and have it work in the simulation. There are other ways to grant specific user permission or all users permission, but this is one way that works and makes sense to me.
Now restart udev and plug in the USB device.
$ sudo restart udev
Now the simulation has permission to use the USB device and attach it to the simulated hardware. There is no need to run as root anymore!
To use the USB device in a Cadence virtual platform simulation, edit the hardware configuration file (.cfg extension) and add the product id and vendor id to the USB device description. More details are available in the Virtual Platform for Xilinx Zynq-7000 EPP User Guide.
Here is a screenshot of the USB memory stick contents as seen from my host machine.
Here is a screenshot of the same USB memory stick when mounted by the Linux running on the Cadence virtual platform for Zynq:
Hopefully, this background on how to work with physical USB devices helps solve the common first question about using physical USB devices attached to virtual platforms and how to obtain permission to attach the USB device to a simulation running on a Linux host machine. There are great benefits obtained by using a physical USB device, but as you can see it requires some special handling and may not be suitable if you are running on a server in another part of the world.