Krisbee
Posts: 4
Joined: Sat Dec 24, 2011 3:11 pm

Raspbian in QEMU: Any USB support?

Fri Feb 28, 2014 12:29 pm

I don't have a Rpi, so followed shift's guides at http://xecdesign.com/qemu-emulating-ras ... -easy-way/ to create a qemu emulation. I wanted to test the "volumio" project, which is based on raspbian, and needed full network access, CIFS support and sound in my QEMU guest. For sound, I complied both built-in snd-usb-audio and suppport for ac97.

After building an appropriate qemu kernel, and using a bridged tap, it boots OK, but USB support is lacking for reason's I'm not clear about.

Code: Select all

 #!/bin/bash
#
# Boot the Raspberry Pi in QEMU
#
# 
#

qemu-system-arm -machine versatilepb   \
   -cpu arm1176  \
   -m 256                                          \
   -no-reboot                                    \
   -serial stdio                                  \
   -no-reboot				      \
   -kernel kernel4qemu                   \
   -append "root=/dev/sda3 panic=10 rootfstype=ext4 rw"       \
   -net nic				              \
   -net tap,ifname=tap0,script=no \
   -soundhw ac97			      \
   -hda  VolumioBeta1.1PI.img     \
   -usb 
The emulation is started by a non-root user, and a udev rule was written to give "0664" perms to the "user" group for a specific host usb device, an external USB DAC. Using the qemu console (crtl+2), you can add a usb device (usb_add host:08bb:2902 ) but nothing happens.

In the QEMU guest, "lspci" shows the expected presence of the qemu generated OCHI contoller:

Code: Select all

pi@volumio:~$ lspci
00:0c.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:0d.0 SCSI storage controller: LSI Logic / Symbios Logic 53c895a
00:0e.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
But "lsusb" returns an error:

Code: Select all

pi@volumio:~$ lsusb                                                                                                                                          
unable to initialize libusb: -99 
In the QEMU guest there's no /dev/bus/sub. Booting qemu with 2014-01-07-wheezy-raspbian.img, exhibits the same behaviour.

Code: Select all

pi@raspberrypi ~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          4.6G  2.1G  2.4G  47% /
/dev/root       4.6G  2.1G  2.4G  47% /
devtmpfs        125M     0  125M   0% /dev
tmpfs            25M  240K   25M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            50M     0   50M   0% /run/shm
/dev/sda1        56M   19M   38M  34% /boot
pi@raspberrypi ~ $ find /dev/bus/usb/ 
find: `/dev/bus/usb/': No such file or directory
pi@raspberrypi ~ $ lspci
00:0c.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:0d.0 SCSI storage controller: LSI Logic / Symbios Logic 53c895a
00:0e.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
pi@raspberrypi ~ $ lsusb
unable to initialize libusb: -99
Any ideas? I don't think I missed anything obvious in the kernel compile, but I've posted my config here:

http://pastebin.com/1UF4zSe4

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6259
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Raspbian in QEMU: Any USB support?

Fri Feb 28, 2014 2:13 pm

If you figure it out, please report back. I have never managed to get anywhere with usb and qemu arm.

Krisbee
Posts: 4
Joined: Sat Dec 24, 2011 3:11 pm

Re: Raspbian in QEMU: Any USB support?

Mon Mar 03, 2014 12:14 pm

Hi Shift,

The error was mine. I hadn't noticed that I'd got my qemu kernel configs mixed up. That was obvious when i looked at dmesg again and OHCI was missing. So USB works in this qemu model.

For example, attaching a USB DAC:

Code: Select all

pi@raspberrypi:~$ lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/3p, 12M
pi@raspberrypi:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: I82801AAICH [Intel 82801AA-ICH], device 0: Intel ICH [Intel 82801AA-ICH]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Interface [ARM AC'97 Interface], device 0: AACI AC'97 [aaci-pl041]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: CODEC [USB Audio CODEC], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
  
pi@raspberrypi:~$ aplay -v -D hw:2,0 test.wav
Playing WAVE 'test.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
aplay: set_params:1145: Unable to install hw params:
ACCESS:  RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 32
CHANNELS: 2
RATE: 44100
PERIOD_TIME: (125011 125012)
PERIOD_SIZE: 5513
PERIOD_BYTES: 22052
PERIODS: (3 4)
BUFFER_TIME: 500000
BUFFER_SIZE: 22050
BUFFER_BYTES: 88200
TICK_TIME: 0
In this case I simulated plugging in USB DAC (adpative UAC 1) after booting by using an usb_add command in the qemu monitor. See here for monitor commands: http://en.wikibooks.org/wiki/QEMU/Monitor

It doesn't function, which is a qemu limitation, but was sufficient for my test purpose. Adding a usbstick needs a EHCI controller, e.g:

With USB stick on host:

Code: Select all

Linux raspberrypi 3.10.25+ #6 Fri Feb 28 23:11:51 GMT 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Mar  2 10:09:15 2014
pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 05dc:a205 Lexar Media, Inc. 
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
pi@raspberrypi ~ $ lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/3p, 12M
    |__ Port 1: Dev 2, If 0, Class=stor., Driver=usb-storage, 12M
pi@raspberrypi ~ $ lspci
00:0c.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:0d.0 SCSI storage controller: LSI Logic / Symbios Logic 53c895a
00:0e.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
pi@raspberrypi ~ $ find /dev/bus/usb -exec ls -l {} \;
total 0
drwxr-xr-x 2 root root 80 Mar  2 10:10 001
total 0
crw-rw-r-T 1 root root 189, 0 Jan  1  1970 001
crw-rw-r-T 1 root root 189, 1 Mar  2 10:10 002
crw-rw-r-T 1 root root 189, 1 Mar  2 10:10 /dev/bus/usb/001/002
crw-rw-r-T 1 root root 189, 0 Jan  1  1970 /dev/bus/usb/001/001
pi@raspberrypi ~ $ 
from dmesg:

Code: Select all

ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
ehci-pci: EHCI PCI platform driver
ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
ohci_hcd 0000:00:0c.0: OHCI Host Controller
ohci_hcd 0000:00:0c.0: new USB bus registered, assigned bus number 1
ohci_hcd 0000:00:0c.0: irq 93, io mem 0x50002400
sd 0:0:0:0: [sda] Attached SCSI disk
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 3 ports detected
uhci_hcd: USB Universal Host Controller Interface driver
usbcore: registered new interface driver usb-storage
mousedev: PS/2 mouse device common for all mice
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver 
After usb_add in qemu monitor

Code: Select all

usb 1-1: new full-speed USB device number 2 using ohci_hcd
usb 1-1: Parent hub missing LPM exit latency info.  Power management will be impacted.
usb-storage 1-1:1.0: USB Mass Storage device detected
scsi1 : usb-storage 1-1:1.0
scsi 1:0:0:0: Direct-Access     Lexar    JumpDrive        1.00 PQ: 0 ANSI: 6
sd 1:0:0:0: [sdb] 15667200 512-byte logical blocks: (8.02 GB/7.47 GiB)
sd 1:0:0:0: [sdb] Write Protect is off
sd 1:0:0:0: [sdb] Mode Sense: 23 00 00 00
sd 1:0:0:0: [sdb] Write cache: disabled, read cache: disabled, doesn't support DPO or FUA
 sdb: sdb1 sdb2
sd 1:0:0:0: [sdb] Attached SCSI removable disk
After usb_del 0.2 in qemu monitor, see last line below:

Code: Select all

root@raspberrypi:~# tail -n 11 /var/log/messages
Mar  2 10:10:34 raspberrypi kernel: usb 1-1: new full-speed USB device number 2 using ohci_hcd
Mar  2 10:10:36 raspberrypi kernel: usb 1-1: Parent hub missing LPM exit latency info.  Power management will be impacted.
Mar  2 10:10:36 raspberrypi kernel: usb-storage 1-1:1.0: USB Mass Storage device detected
Mar  2 10:10:36 raspberrypi kernel: scsi1 : usb-storage 1-1:1.0
Mar  2 10:10:37 raspberrypi kernel: scsi 1:0:0:0: Direct-Access     Lexar    JumpDrive        1.00 PQ: 0 ANSI: 6
Mar  2 10:10:37 raspberrypi kernel: sd 1:0:0:0: [sdb] 15667200 512-byte logical blocks: (8.02 GB/7.47 GiB)
Mar  2 10:10:37 raspberrypi kernel: sd 1:0:0:0: [sdb] Write Protect is off
Mar  2 10:10:37 raspberrypi kernel: sd 1:0:0:0: [sdb] Write cache: disabled, read cache: disabled, doesn't support DPO or FUA
Mar  2 10:10:37 raspberrypi kernel: sdb: sdb1 sdb2
Mar  2 10:10:37 raspberrypi kernel: sd 1:0:0:0: [sdb] Attached SCSI removable disk
Mar  2 10:23:00 raspberrypi kernel: usb 1-1: USB disconnect, device number 2
Acces to the host USB memory stick is fully functional.

If the emulation has only a OHCI controller on the pci-bus, it's also possible to add a virtual USB device with commands like:

Code: Select all

	-drive if=none,id=usbstick,file=/path/to/img\
		-usb \
		-device usb-ehci,id=ehci \
		-device usb-storage,bus=ehci.0,drive=usbstick
The first device command above adds an EHCI controller to which you can attach a virtual USB2 usb storage device. Qemu USB support is not well documented, but these notes are helpful:

http://www.kraxel.org/cgit/qemu/tree/docs/usb2.txt

Running Qemu as a non-root user can lead to USB permission problems. You will likely have to write a udev rule to fix this. For example:

50-qemuperms.rule

Code: Select all

# Texas Instruments PCM2902 Audio Codec
#SUBSYSTEMS=="usb", ATTRS{idVendor}=="08bb", ATTRS{idProduct}=="2902", GROUP="kvm", MODE="0664"
# Lexar usb stick
#SUBSYSTEMS=="usb", ATTRS{idVendor}=="05dc", ATTRS{idProduct}=="a205", GROUP="users", MODE="0664"
My non-root user happened to be in the "kvm" group on my debian host, but being in the "users" group should suffice. To get udev to use any new rules, execute these commands on the host:

Code: Select all

udevadm control --reload-rules
udevadm trigger --attr-match=subsystem=usb
That's about it. I hope that's useful.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6259
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Raspbian in QEMU: Any USB support?

Mon Mar 03, 2014 10:53 pm

Thanks again!

spaceribs
Posts: 4
Joined: Tue Sep 16, 2014 3:20 am

Re: Raspbian in QEMU: Any USB support?

Tue Sep 16, 2014 3:30 am

I'm trying to get USB working as well with qemu, and I'm having similar issues. I see the device in "info usb" as correctly attached, but I don't see anything in /dev/bus/usb and I'm getting that same -99 error. I'm having a hard time understanding what I can do to get this working from your instructions, can you list out the steps? Do I need to compile my own custom kernel-qemu?

spaceribs
Posts: 4
Joined: Tue Sep 16, 2014 3:20 am

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 3:05 pm

Took me a few days of tinkering, but I figured it out on my own. What I suggest for any other beginners trying to get qemu with usb support, you will definitely need to compile your own linux kernel. Being on a mac, I did it in an ubuntu vagrant box. Follow the directions here but make sure to add OHCI/EHCI support as well: http://xecdesign.com/compiling-a-kernel/

You can add OHCI support enabiling the following in menuconfig:

Code: Select all

    Device Drivers->
        [*] USB Support->
            [*] Support for Host-side USB
            [*]    USB verbose debug messages
            [*]    USB announce new devices
            [*]    EHCI HCD
            [*]    OHCI HCD
                [*] OHCI support for PCI-bus USB controllers
                [*] Generic OHCI driver for a platform device
Some of these options might be unnesseccary but that did it for me, I can now see the Ralink WiPi connected to my qemu in the startup after adding these options to my qemu command:

Code: Select all

-usb \
-device usb-ehci,id=ehci \
-device usb-host,bus=ehci.0,vendorid=0x148f,productid=0x5370 \
P.S. As another aside, I also had to move my linux kernel compilation folder into my home directory from my shared /vagrant/ directory for it to compile properly. Don't compile things from shared directories apparently.

Hope this helps,
-spaceribs

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6259
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 3:34 pm

Thanks, I've been meaning to update the kernel on xecdesign, so I'll make sure to build those in when I do.

User avatar
DougieLawson
Posts: 40224
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 4:46 pm

ShiftPlusOne wrote:Thanks, I've been meaning to update the kernel on xecdesign, so I'll make sure to build those in when I do.
Is there any way to get Qemu running with 512MB rather than being restricted to 256MB?
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6259
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 5:22 pm

Not without creating a custom platform. Torlus' rpi platform can have more ram, but last time I checked, lacked USB support, so there was no network. It has been a while since I've played around with qemu.

I actually recommend usermode qemu chroot, as that pretty much removes all the silly restrictions. Maybe a tutorial is in order there.

diederik
Posts: 394
Joined: Wed Mar 26, 2014 11:17 pm

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 5:55 pm

ShiftPlusOne wrote:I actually recommend usermode qemu chroot, as that pretty much removes all the silly restrictions. Maybe a tutorial is in order there.
That would be much appreciated :)
I have very little experience with qemu, but I'd like to use it especially for RPi use/testing.

spaceribs
Posts: 4
Joined: Tue Sep 16, 2014 3:20 am

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 5:58 pm

ShiftPlusOne wrote:Not without creating a custom platform. Torlus' rpi platform can have more ram, but last time I checked, lacked USB support, so there was no network. It has been a while since I've played around with qemu.

I actually recommend usermode qemu chroot, as that pretty much removes all the silly restrictions. Maybe a tutorial is in order there.
I'm going to try using "bcmrpi_defconfig" instead of "versatile_defconfig" as in the tutorial I linked before, I'll let you know how much of the menuconfig steps it bypasses and maybe we can get an up to date tutorial of it all.

spaceribs
Posts: 4
Joined: Tue Sep 16, 2014 3:20 am

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 9:23 pm

So... it compiled, but qemu shows a black screen. I'll have to play around with some settings to see what the problem is, but for now I've got full USB support and the WiPi/Ralink USB adapter working. It's kinda cool hosting an emulated Raspberry Pi on my mac which I can connect to from my mac through the network as though it's completely separate, this is going to make designing a django application which plays with AdHoc/AP mode networking a lot easier.

Thanks so much for the tutorials shift!

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6259
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Raspbian in QEMU: Any USB support?

Sun Sep 21, 2014 9:43 pm

spaceribs wrote: Thanks so much for the tutorials shift!
No worries! Sorry that they're poorly maintained nowadays.

legreg
Posts: 2
Joined: Tue May 12, 2015 3:45 pm

Re: Raspbian in QEMU: Any USB support?

Tue May 12, 2015 3:52 pm

Hello,

Maybe some news concerning an updated tuto for newbies like me?
I'm really interested in getting usb working, because I actually have a problem with a real raspbery and an usb device, and I'm not able to know if it's a software or hardware problem actually.

Whatever my reasons, that would be greatly appreciated ;)

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6259
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Raspbian in QEMU: Any USB support?

Wed May 13, 2015 3:49 am

Won't be coming from me, since I don't do that sort of thing anymore and have limited free time.

legreg
Posts: 2
Joined: Tue May 12, 2015 3:45 pm

Re: Raspbian in QEMU: Any USB support?

Wed May 13, 2015 9:15 am

Ok, then I will try to find with the previous informations.

There is one (several) thing I don't understand concerning the use of usb.
Apparently, by default (with the -usb option), it's possible to create a virtual device.
But I'm not sure to understand the qemu options to use for a real usb device.
Could someone explain just that in details?

ulao
Posts: 16
Joined: Tue Mar 10, 2015 11:59 am

Re: Raspbian in QEMU: Any USB support?

Sat Dec 03, 2016 8:27 pm

Can't anyone help on this usb thing? Compiling kernels is not for the newbie types and dead links really are not helpful. I found a slew of command line options but none work with the emulators except -usb. When booted I have no idea where to go next.

I'm just looking to pass-true a HID game pad device in qemu when running an arm binary like Raspberry Pi. Is there no way to do this as of yet?

Return to “Raspberry Pi OS”