Tux

...making Linux just a little more fun!

USB test module

Ben Okopnik [ben at linuxgazette.net]


Tue, 13 Jan 2009 15:13:46 -0600

I'm trying to do something rather abstruse and complex with a weird mix of software, hardware, and crazy hackery (too long to explain and it would be boring to most people if I did), but - I need a "magic bullet" and I'm hoping that somebody here can point me in the right direction, or maybe toss a bit of code at me. Here it goes: I need a module that would create a serial-USB device (/dev/ttyUSB9 by preference) and let me pipe data into it without actually plugging in any hardware.

Is this even possible? Pretty much all of my programming experience is in the userspace stuff, and beating on bare metal like that is something I've always considered black magic, so I have absolutely no idea.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


René Pfeiffer [lynx at luchs.at]


Tue, 13 Jan 2009 23:10:12 +0100

On Jan 13, 2009 at 1513 -0600, Ben Okopnik appeared and said:

> I'm trying to do something rather abstruse and complex with a weird mix
> of software, hardware, and crazy hackery (too long to explain and it
> would be boring to most people if I did), but - I need a "magic bullet"
> and I'm hoping that somebody here can point me in the right direction,
> or maybe toss a bit of code at me. Here it goes: I need a module that
> would create a serial-USB device (/dev/ttyUSB9 by preference) and let me
> pipe data into it without actually plugging in any hardware.

Hm, I think I saw something like a USB dummy driver somwhere somewhen. But there is a tutorial for coding USB drivers and even skeleton code.

http://tali.admingilde.org/linux-docbook/writing_usb_driver.pdf

Apart from that I only know about LKML and http://www.linux-usb.org/ where most of the developers hang out.=20

> Is this even possible? [...]

I think so. Maybe the driver testing tool (http://www.linux-usb.org/usbtest/#devices) has a way of faking devices and creating devices for I/O.

Does it have to be a serial-USB device?

Best, René.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 13 Jan 2009 23:53:33 -0600

On Tue, Jan 13, 2009 at 11:10:12PM +0100, René Pfeiffer wrote:

> On Jan 13, 2009 at 1513 -0600, Ben Okopnik appeared and said:
> > I'm trying to do something rather abstruse and complex with a weird mix
> > of software, hardware, and crazy hackery (too long to explain and it
> > would be boring to most people if I did), but - I need a "magic bullet"
> > and I'm hoping that somebody here can point me in the right direction,
> > or maybe toss a bit of code at me. Here it goes: I need a module that
> > would create a serial-USB device (/dev/ttyUSB9 by preference) and let me
> > pipe data into it without actually plugging in any hardware.
> 
> Hm, I think I saw something like a USB dummy driver somwhere somewhen.
> But there is a tutorial for coding USB drivers and even skeleton code.
> 
> http://tali.admingilde.org/linux-docbook/writing_usb_driver.pdf

[Laugh] Funny thing, I was just exchanging mail with Greg Kroah-Hartman yesterday. Nice guy, and very helpful, but I was only able to get just so far with his help and no farther. Mostly because the thing that I'm trying to do now is something that I figured out based on his help.

Perhaps I should just wait until I can get back home (I'm teaching a class in Milwaukee; last night, it was *over a hundred degrees colder* here than Florida was when I left!) so I can use my GPS as a data source for this, but I'm impatient. Blame the cold weather. :)

> Apart from that I only know about LKML and http://www.linux-usb.org/
> where most of the developers hang out. 

Eeep. I was hoping it wasn't that complicated - some equivalent of 'netcat' or whatever... maybe I do need to wait.

> > Is this even possible? [...]
> 
> I think so. Maybe the driver testing tool
> (http://www.linux-usb.org/usbtest/#devices) has a way of faking devices
> and creating devices for I/O.

Hmm. I just worked my way through that, and it seems that you have to have some hardware at least; none of the modules in /lib/modules/2.6.24-16-generic/kernel/drivers/usb/gadget load for me, all of them dying with a 'No such device' error.

> Does it have to be a serial-USB device?

I'd be willing to try it with a non-serial version, if that's any easier.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Wed, 14 Jan 2009 12:03:54 +0000

2009/1/14 Ben Okopnik <ben@linuxgazette.net>:

> On Tue, Jan 13, 2009 at 11:10:12PM +0100, René Pfeiffer wrote:
>> On Jan 13, 2009 at 1513 -0600, Ben Okopnik appeared and said:
>> > I'm trying to do something rather abstruse and complex with a weird mix
>> > of software, hardware, and crazy hackery (too long to explain and it
>> > would be boring to most people if I did), but - I need a "magic bullet"
>> > and I'm hoping that somebody here can point me in the right direction,
>> > or maybe toss a bit of code at me. Here it goes: I need a module that
>> > would create a serial-USB device (/dev/ttyUSB9 by preference) and let me
>> > pipe data into it without actually plugging in any hardware.
>>
>> Hm, I think I saw something like a USB dummy driver somwhere somewhen.
>> But there is a tutorial for coding USB drivers and even skeleton code.
>>
>> http://tali.admingilde.org/linux-docbook/writing_usb_driver.pdf
>
> [Laugh] Funny thing, I was just exchanging mail with Greg Kroah-Hartman
> yesterday. Nice guy, and very helpful, but I was only able to get just
> so far with his help and no farther. Mostly because the thing that I'm
> trying to do now is something that I figured out based on his help.
>
> Perhaps I should just wait until I can get back home (I'm teaching a
> class in Milwaukee; last night, it was *over a hundred degrees colder*
> here than Florida was when I left!) so I can use my GPS as a data source
> for this, but I'm impatient. Blame the cold weather. :)
>
>> Apart from that I only know about LKML and http://www.linux-usb.org/
>> where most of the developers hang out.
>
> Eeep. I was hoping it wasn't that complicated - some equivalent of
> 'netcat' or whatever... maybe I do need to wait.
>

socat ('netcat++' - http://www.dest-unreach.org/socat/) can redirect serial ports, but nothing about USB there.

>> > Is this even possible? [...]
>>
>> I think so. Maybe the driver testing tool
>> (http://www.linux-usb.org/usbtest/#devices) has a way of faking devices
>> and creating devices for I/O.
>
> Hmm. I just worked my way through that, and it seems that you have to
> have some hardware at least; none of the modules in
> /lib/modules/2.6.24-16-generic/kernel/drivers/usb/gadget load for me,
> all of them dying with a 'No such device' error.
>

The libusb documentations says: 'Before any communication can occur with a device, it needs to be found.' (Perl has Device::USB, but it's a libusb wrapper, so same thing).

>> Does it have to be a serial-USB device?
>
> I'd be willing to try it with a non-serial version, if that's any
> easier.

Well, qemu has some virtual usb devices: it can emulate a USB storage device using a directory, or a USB mouse or tablet using X's. It tends to attract a lot of experimental patches, so perhaps there's a virtual serial device floating on the net somewhere.


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Wed, 14 Jan 2009 12:05:31 +0000

2009/1/14 Jimmy O'Regan <joregan@gmail.com>:

> Well, qemu has some virtual usb devices: it can emulate a USB storage
> device using a directory, or a USB mouse or tablet using X's. It tends
> to attract a lot of experimental patches, so perhaps there's a virtual
> serial device floating on the net somewhere.

Like this: http://www.mail-archive.com/qemu-devel@nongnu.org/msg15188.html


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 14 Jan 2009 11:03:48 -0600

On Wed, Jan 14, 2009 at 12:03:54PM +0000, Jimmy O'Regan wrote:

> 2009/1/14 Ben Okopnik <ben@linuxgazette.net>:
> >
> > Perhaps I should just wait until I can get back home (I'm teaching a
> > class in Milwaukee; last night, it was *over a hundred degrees colder*

Erm, the cold obviously got to my brain. I'm in Minneapolis, not Milwaukee... anyway, one of those cities that's entirely too close to the North Pole. :)

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 14 Jan 2009 11:41:52 -0600

On Wed, Jan 14, 2009 at 12:03:54PM +0000, Jimmy O'Regan wrote:

> 2009/1/14 Ben Okopnik <ben@linuxgazette.net>:
> >
> > Eeep. I was hoping it wasn't that complicated - some equivalent of
> > 'netcat' or whatever... maybe I do need to wait.
> 
> socat ('netcat++' - http://www.dest-unreach.org/socat/) can redirect
> serial ports, but nothing about USB there.

root@Tyr:~# mknod -m 0664 /dev/ttyUSB9 c 188 9
root@Tyr:~# ls -l /dev/ttyUSB9
crw-rw-r-- 1 root root 188, 9 2009-01-14 11:37 /dev/ttyUSB9
root@Tyr:~# echo Hello|socat - /dev/ttyUSB9
2009/01/14 11:37:47 socat[23056] E open("/dev/ttyUSB9", 02002, 0666): No such device or address

Oh well - it was definitely worth trying! Certainly sounded like a good possibility...

> Well, qemu has some virtual usb devices: it can emulate a USB storage
> device using a directory, or a USB mouse or tablet using X's. It tends
> to attract a lot of experimental patches, so perhaps there's a virtual
> serial device floating on the net somewhere.

I guess my first hopes for this didn't pan out; this seems to be a Non-Trivial Task. I was hoping that there was something like "socat" (thanks, Jimmy!) for it, but - doesn't seem like it.

Maybe I should just grit my teeth and wait till the end of the week. :)

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Wed, 14 Jan 2009 19:40:12 +0000

2009/1/14 Ben Okopnik <ben@linuxgazette.net>:

> On Wed, Jan 14, 2009 at 12:03:54PM +0000, Jimmy O'Regan wrote:
>> 2009/1/14 Ben Okopnik <ben@linuxgazette.net>:
>> >
>> > Eeep. I was hoping it wasn't that complicated - some equivalent of
>> > 'netcat' or whatever... maybe I do need to wait.
>>
>> socat ('netcat++' - http://www.dest-unreach.org/socat/) can redirect
>> serial ports, but nothing about USB there.
>
> ----------------------------------------------------------------------
> root@Tyr:~# mknod -m 0664 /dev/ttyUSB9 c 188 9
> root@Tyr:~# ls -l /dev/ttyUSB9
> crw-rw-r-- 1 root root 188, 9 2009-01-14 11:37 /dev/ttyUSB9
> root@Tyr:~# echo Hello|socat - /dev/ttyUSB9
> 2009/01/14 11:37:47 socat[23056] E open("/dev/ttyUSB9", 02002, 0666): No such device or address
> ----------------------------------------------------------------------
>
> Oh well - it was definitely worth trying! Certainly sounded like a good
> possibility...
>
>> Well, qemu has some virtual usb devices: it can emulate a USB storage
>> device using a directory, or a USB mouse or tablet using X's. It tends
>> to attract a lot of experimental patches, so perhaps there's a virtual
>> serial device floating on the net somewhere.
>
> I guess my first hopes for this didn't pan out; this seems to be a
> Non-Trivial Task. I was hoping that there was something like "socat"
> (thanks, Jimmy!) for it, but - doesn't seem like it.
>
> Maybe I should just grit my teeth and wait till the end of the week. :)

FWIW, the USB Serial virtual device was integrated into qemu: perhaps you could use it with qemu's userspace emulation?


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Wed, 14 Jan 2009 20:29:13 +0000

2009/1/13 René Pfeiffer <lynx@luchs.at>:

> On Jan 13, 2009 at 1513 -0600, Ben Okopnik appeared and said:
>> I'm trying to do something rather abstruse and complex with a weird mix
>> of software, hardware, and crazy hackery (too long to explain and it
>> would be boring to most people if I did), but - I need a "magic bullet"
>> and I'm hoping that somebody here can point me in the right direction,
>> or maybe toss a bit of code at me. Here it goes: I need a module that
>> would create a serial-USB device (/dev/ttyUSB9 by preference) and let me
>> pipe data into it without actually plugging in any hardware.
>
> Hm, I think I saw something like a USB dummy driver somwhere somewhen.

This? http://www.gelato.unsw.edu.au/lxr/source/drivers/usb/gadget/dummy_hcd.c


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 14 Jan 2009 15:32:05 -0600

On Wed, Jan 14, 2009 at 08:29:13PM +0000, Jimmy O'Regan wrote:

> 2009/1/13 René Pfeiffer <lynx@luchs.at>:
> >
> > Hm, I think I saw something like a USB dummy driver somwhere somewhen.
> 
> This? http://www.gelato.unsw.edu.au/lxr/source/drivers/usb/gadget/dummy_hcd.c

That's actually in the kernel source. I tried compiling it, and it threw a bunch of compilation errors - not even a question of missing libs, just some makefile problem:

ben@Tyr:/tmp/gadget/usb_module_dir/gadget$ make
make -C /lib/modules/2.6.24-16-generic/build SUBDIRS=/tmp/gadget/usb_module_dir/gadget modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.24-16-generic'
make[2]:   No rule to make target `/tmp/gadget/usb_module_dir/gadget/dummy_hcd.c', needed by `/tmp/gadget/usb_module_dir/gadget/dummy_hcd.o'.  Stop.
make[1]:   [_module_/tmp/gadget/usb_module_dir/gadget] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.24-16-generic'
make:   [default] Error 2

That's deeper than I want to dig. :\

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back