Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#320158 - 09/03/2009 12:52 Locating a USB-CDC device by VID & PID on linux
sn00p
addict

Registered: 24/07/2002
Posts: 618
Loc: South London
Hi,

I'm just in the process of finishing up firmware/sdk/documentation on a new product we've developed that uses the USB CDC-ACM class. The Product will be available in both USB & RS232 configurations, so using the CDC class saves headaches both device and host side.

We are providing an SDK for the device which wraps up the serial protocol into nice easy to use API calls, so far I have it running on Linux, OS X, Windows and Windows CE.

Under windows I've provided a call to easily open a USB device, it's fairly trivial to find a device by VID & PID and get the COM port number which can then be opened as normal, this does make the USB version completely plug and play (barring the fact that Microsofts class driver for CDC *requires* an .inf file).

I cannot seem to find a solution under linux though, I'm sure there's an easy solution, but I can't find the correct combination of VID/PID/USB/CDC/OPEN to pass to google to find the answer, hoping somebody here might have the solution or might be able to point me off in the right direction.

What I want to achieve is something along the lines of:

Code:

char *deviceName;
int fd;

deviceName = find_usb_device(0x1F1E, 0x0005);

if (!deviceName)
    return;

fd = open(deviceName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
    
if (fd<0)
    return;

....



Thanks

Adrian

Top
#320170 - 09/03/2009 14:39 Re: Locating a USB-CDC device by VID & PID on linux [Re: sn00p]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
You'll be wanting to build this on top of the Linux libusb stack (well, more like a thin veneer than a stack).

Here's some sample code I wrote for the VFD display that came in the Antec Fusion PC case that houses my Myth box.

http://rtr.ca/vfd_updater/

The file of interest to you is vfd_usb.c, which does all of the Right Things(tm) to detach the gizmo from any other driver before opening it for use. And so on.

EDIT: This is all original code by me, and you are free to use it as you please. No strings attached, no warranties given, no lawyers sacrificed (darn!).

Cheers

Mark


Edited by mlord (09/03/2009 14:46)

Top
#320182 - 09/03/2009 16:30 Re: Locating a USB-CDC device by VID & PID on linux [Re: mlord]
sn00p
addict

Registered: 24/07/2002
Posts: 618
Loc: South London
Thank you very much Mark! laugh

A quick view of your code shows me that the "usb_device" struct looks like it contains the information I need!

I'm busy producing a demo at the moment so I might end up bringing a board home tomorrow night and have a play.

I'd used libusb under windows when I was testing a "custom" driver, I didn't fancy spending 2 months in the car crash that is the windows driver model.

Will let you know how I get on!

Adrian

Top
#324027 - 08/07/2009 04:11 Re: Locating a USB-CDC device by VID & PID on linux [Re: sn00p]
sn00p
addict

Registered: 24/07/2002
Posts: 618
Loc: South London
Digging up and old thread.

I've been working on the configuration software which is written in Qt and I'm back now looking for the "correct" way of detecting USB hotplug events under Linux.

I'm slightly confused as to the best or indeed correct way of going about this. After much scouring and reading of the internet I settled on libhal, using the DeviceAdded and DeviceRemoved events, my application quite happily receives these and they contain the information I need to maintain a list of our connected devices.

But, I still have a nagging doubt in my mind that using libhal is a good thing, searching the web it appears to have been deprecated (or is in the process of) and I find many posts saying that using it is not a good thing, but with no mention of how what I should be using. Furthermore, when my configuration software finds one of our devices, it opens it (It's appears to the computer as a serial port (CDC/ACM)) and keeps it open until you either manually close the device from a menu or unplug it.

Unplugging the device while open seems to send HAL into chaos, if I look in device manager I see multiple entries for the TTY driver, sometimes with different dev locations (/dev/ttyUSB0 - /dev/ttyUSB1) and sometimes with the same ones, it's like something somewhere in the chain is not cleaning up after itself. Obviously my software reports phantom devices too.

Any thoughts on these anybody?

Thanks

Adrian

Top
#324058 - 09/07/2009 00:37 Re: Locating a USB-CDC device by VID & PID on linux [Re: sn00p]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
Heh.. Hopefully someone else will pitch in here, as the programming I do rarely, if ever, uses libraries other than standard libc. smile

But for hotplug events, the kernel can feed them to userspace using whatever command is echoed into /proc/sys/kernel/hotplug (that's the old method) and/or via a netlink interface.

The advantage of netlink is that the events will generally arrive in time sequence, which cannot be said of the other method.

Libraries like libhal, and daemons like udevd, presumably sit on top of one or both of those.

Cheers

Top
#324061 - 09/07/2009 04:25 Re: Locating a USB-CDC device by VID & PID on linux [Re: mlord]
sn00p
addict

Registered: 24/07/2002
Posts: 618
Loc: South London
Once again Mark, you come up with the goods. smile

Having spent most of yesterday searching for various combinations of hotplug, linux, kernel and usb, I must have just overlooked netlink.

I will have a quick play today and see what I can come up with.

Thanks!

Top