Linux today: too much plug and play

January 07, 2011 | 7 Minute Read

This title is probably not what you’d expect. Usually people complain for the opposite reason: they buy some hardware, connect it to Linux and it does nothing, so they have to spend time in forums to see what can be done even if manufacturers don’t officially support Linux.

In my case, it’s the plug and play system itself that is causing me problems, because it incorrectly identifies a device as another type of device…

But let’s start from the beginning. I’m an embedded developer, and therefore I deal with microcontrollers. Often, the easiest way to debug, program, and communicate with a microcontroller is a serial port. Given that serial ports disappeared from computers long ago, USB to serial adapters have been introduced to solve this problem. These are all tiny chips like the FT232 or PL2303 that connect upstream as USB and downstream as serial ports.

These chips can be used either as standalone products, like the blue dongle at the left of this picture which is PL2303 based

or directly embedded into the device. For example, the Miosix board that I designed has an FT232 on board. It’s the chip at the bottom right, next to the USB connector

Now, both these devices identify as serial ports, using the default VID and PID (which are the identifiers of the USB protocol) of the manufacturer. They do so because they are serial ports, the first one is obvious to see, but also the second, since I don’t need any program to automatically start when I plug one of those, just to sit there and wait till I open screen or minicom to watch debug messages and the like.

Now, I’ve recently upgraded my computer to Fedora 14. It works very well, except for serial ports. At first I noticed that immediately after connecting a USB/serial device, trying to talk to it resulted in errors. “Device ore resource busy”, it said. Retrying after a couple of seconds fixed this, but then it looks like the bootloader of the microcontroller (the first program that starts) has been upset by something, so the first programming cycle failed, the next one went well, and all the subsequent too. At least until the USB/serial adapter is disconnected and reattached, in this case the issue presents again.

What could it be?, I thought. Maybe a bug in the kernel driver for serial ports? It turned out to be something entirely different.

To understand what was happening, first we have to talk about crappy GPS mice and GSM usb sticks. Both have someting in common: those who manufacture the GSM/GPS module are different from people who design the final product. The GSM/GPS modules, for convenience, spit out their data through a serial port. But given that PCs have USB ports only, final product designers add an USB/serial converter into the device. Now, the issue is, that they don’t care about changing the default VID and PID of the USB/serial adapter. Therefore when plugged to a PC they appear as serial ports despite their products are not serial ports. A GPS can only understand NMEA sentences (plus some weird binary protocol), while a GSM “internet key” can only understand AT commands. And what’s worse, they require plug and play, they require a program that attaches to these devices as soon as they’re plugged in.

Linux on the other side, is growing fast even as a desktop operating system. So it’s natural that it is starting to support more and more hardware, in the easiest possible way. But how can it cope with GSM/GPS USB sticks?

Simple: for GPSes there’s a program, gpsd, that when a new device is connected, its VID and PID are checked against a list of devices, and if matches it tries to send NMEA sentences. If it responds, ok, otherwise the device is discarded. The same happens for GSM devices, there’s another program: ModemManager that does the same thing with AT commands.

But given that most devices appear as a PL2303 serial port, how can these programs know if after the PL2303 there’s a GPS unit, a GSM “internet key”, or none of these? Simple, they don’t. They try for a while to talk and if the device on the other side doesn’t seem to be what they expect, they give up.

This explains why trying to connect my USB/serial adapter to the PC and immedately open screen or minicom resulted in “device or resource busy”, while retrying a couple of seconds later worked.

The issue I’m having with the bootloader, is instead caused by the fact that these little program don’t just open the serial port and listen, they open the port and talk! The bootloader of my board probably gets upset by AT commands, or NMEA sentences or both.

But then I wonder, does a GPS unit like AT commands, or conversely, does a GSM internet key like NMEA sentences? This is trouble waiting to happen. Also on Windows, I suppose. Assume one buys a cheap GSM unit that identifies as a PL2303, and a cheap internet key that identifies too as PL2303, what does the official Windows drivers that come with them do? Conflict, I guess.

The first attempt to fix my issue has been to uninstall gpsd and modem-manager. Unfortunately, on Fedora, the whole KDE depends on gpsd, so it can’t be unistalled. I therefore contacted the gpsd developers on the mailing list. They have been very helpful in finding a workaround (they even blogged about it as I’m doing now, here), which is to download the gpsd sources and issue these commands:

./configure
make udev-uninstall

which leaves gpsd installed, but removes udev rules that cause the plug and play. It’s also interesting that if I ever buy a GPS unit I can still start gpsd manually. Nice.

The fun thing is that gpsd developers were not aware that some other opensource project, ModemManager, was lurking for the same USB VID and PIDs…

I still haven’t found a way to disable modem-manager. It is a daemon, but it auto-respawns when killed, there’s no “service modem-manager stop” and trying to uninstall it would also get rid of NetworkManager.

Ofcouse, the only way to fix the whole problem is that hardware manufacturers should change the VID and PID so that a GPS, a GSM modem and a serial port can be uniquely identified…

Update:

I found a way to disable modem-manager while leaving NetworkManager:

sudo service NetworkManager stop
sudo killall modem-manager
cd /usr/sbin
sudo rm modem-manager
sudo service NetworkManager start

Ofcourse any GSM internet key would probably stop working, but for now It’s enough. I can finally connect an USB/serial adapter and nothing is trying to speak with it by default. Finally.