High-Level Device-Driver Programming

Tagged with Regular Expressions

In September, "Regular Expressions" described several ways kernel programming has become more manageable: instead of being restricted to a narrow sliver of elite programmers, it's now feasible for nearly anyone who maintains his or her own Linux host.

Much the same has happened with work on device drivers. While authorship of professional-caliber device drivers remains a specialized pursuit, it's now possible to be productive with device drivers recreationally, or at least on a basis other than full-time devotion.

Software for Easier Device Drivers

Most of the changes easing this work are familiar from larger trends in kernel programming mentioned earlier: standards for loadable modules, rationalization of device driver interfaces, the availability of virtual machines for testing, less expensive hardware, and so on. This week, we look at two general techniques that have proven more successful with device drivers than you might realize: programming in userland and with high-level languages.

Traditionally, device drivers have connected deep inside an operating system's kernel. In recent years, progressively better-defined interfaces have narrowed the ties that a device driver must have to a kernel, to the point that now it's even feasible to write device drivers "in userland," that is, without kernel rebinding, just like more normal applications.

FUSD, from the Center for Embedded Networking Sensing (CENS), is the best example of this. "...CENS... is a major research enterprise focused on... applying this revolutionary technology to critical scientific and societal pursuits", according to the profile on its own Web site. "[T]his revolutionary technology" involves miniaturization and networking of sensors, to "foster ... a deeper understanding of the natural and built environments." CENS looks at all sorts of things in the world as opportunities for digital sensing; it's a group whose experience with device drivers is deep.

FUSD, reminiscent in several regards of the FUSE that appeared in our September column, is the technical basis for several of these device drivers. As its manual describes, "FUSD is a Linux framework for proxying device file callbacks into user-space, allowing device files to be implemented by daemons instead of kernel code." Although this limits the performance of device drivers implemented with FUSD, its original co-author Jeremy Elson emphasizes that a user-space device driver has more capabilities than a conventional kernel-level driver, including long-running computations, branching into other device drivers, event-based processing, and retrieval of data from the filesystem.

Most crucially for CENS, development of a device driver costs less. A FUSD device can't crash the host as is all-to-common with a kernel-level driver, "and is generally easier to code and debug, especially for people who are application developers," as the other FUSD co-author, Lewis Girod, wrote us. The CENS team can focus more on the science of, for example, the Acoustic ENSBox, and less on fussing with its enabling software.

FUSD's advantages manifest in several other specific benefits. Girod illustrates,

"An example of what FUSD is good for is a 'GPS driver.' ... [T]he 'GPSd' package ... is a user space daemon that acts as a gateway between a GPS unit connected by RS232 and a sockets protocol. Using FUSD you could implement the same RS232 GPS-reading protocol in user-space, but provide access to the features of the GPS via character devices. The advantage is that these devices are browseable and accessible from the shell, rather than using telnet or other special programs to talk to the socket. Another alternative would be to write the GPS driver in the kernel, but this is difficult because it isn't possible to access the device interface for the serial port from inside the kernel.

...Since FUSD devices are just device files, you can use the usual Unix permissions to control access.

...Another advantage of FUSD is the ability to customize the semantics of the character device, for example to allow notification of events via select() and to provide reads aligned along message boundaries without framing. This can simplify client code considerably, relative to stream socket protocols."

The result is that FUSD saved CENS considerable time in connecting a variety of microcontroller boards to Linux hosts.

Our explanation here neglects subtleties involved in getting FUSD's implementation right. Elson emphasized when he wrote us that, as straightforward as the idea of FUSD sounds, rendering it in "industrial-strength code ... free of race conditions" came only after "many revisions". The continuing inspirations during this rework were "Linux's /proc filesystem" as a window to the status of the kernel, and "the QNX operation system," as a model for inter-process communication.

Unexpected Languages for Device Drivers

All this work brings still more benefits to FUSD's users. CENS open-sourced FUSD along with many of its other software assets. Among the small community of users outside CENS, at least a few develop in Java and Python. While a conventional kernel-level device driver essentially must be written in C (or perhaps assembler or C++), a FUSD device driver "can be made to work with anything that has access to the POSIX interface," as Girod observes. All the usual advantages of high-level languages immediately apply to work with device drivers.

Brian Warner accomplished so much with a Python binding, in fact, that he contributed it back to CENS, where it's now maintained in the same CVS tree.

FUSD isn't the only basis for "higher-level" device-driver development, though. Another documented example is Pat Thoyt's tclftd2xx. Future Technology Devices International Ltd. [FTDI] are British "specialists in converting legacy peripherals to Universal Serial Bus (USB)." Even though FTDI has done the hard work of packaging libraries for the hardware it supports, it still requires considerable expertise to make good use of those libraries. Thoyts needed to provide for "a general user [to] update the firmware on a device using the FTDI USB chipset." After a little experimentation, he settled on a graphical user interface (GUI) and API coded mostly in Tcl/Tk. As Thoyts wrote us, "In many ways this is a classic use of extensions in Tcl: I've added just enough [C code in a channel driver] to the Tcl interpreter to permit general use of the device."

With that minimal extension, though, supported FTDI devices are now just as programmable at a high level in Tcl as FTDI made them in C.

Is it worth the effort? Here we run into one of "Regular Expressions'" recurring themes: some of the best development stories can be publicized partially or not at all. Our own practice has yielded enough spectacular successes for scripting interfaces (one in particular was very much like tclftd2xx) to encourage us to write this column. In many specific cases, however, the details and especially measurements on the advantages of high-level programming must remain proprietary. What's certain in the public record is that Thoyts provides an open-source interface free for all of us to study and use for our own benefit.

Summary

The expressiveness and productivity of high-level languages can be available even for work in development of device drivers, generally regarded as one of the most specialized and demanding areas of programming.

Kathryn and Cameron run their own consultancy, Phaseit, Inc., specializing in high-reliability and high-performance applications managed by high-level languages. They write about high-level languages and related topics in their "Regular Expressions" columns.

 

3
Average: 3 (1 vote)
Don't forget about CUSE.
Submitted by oiaohm1 on Tue, 12/02/2008 - 03:19.

http://lwn.net/Articles/296388/ Is a direct alteration to fuse itself ok it only gives char devices not block.

Even with fusd there is still a class of devices missing a interface. Network cards.

Really the way Linux kernel is going is pure hybrid. Part Micro Kernel and Part monolithic. I really do wonder if the day will ever come that when building a Linux kernel you can choose between Userspace Module or built in when building kernel source drivers.

Could solve all the driver back porting problems.

FUSD and related variations
Submitted by re_authors on Sun, 12/07/2008 - 06:31.

First, our apology, oiaohm1, for taking so long to reply. Holiday season, weather, medical stuff, ... well, the usual. We do appreciate you joining in, and, in general, we'll be more prompt about following up.

Which "driver back porting problems" do you have in mind? Are you thinking of the challenge of writing drivers for new devices that work for legacy installations of Linux? There *are* techniques that help with that, but Cameron doubts that the particular difficulty matters enough to motivate the change in architecture that would be required. Certainly device drivers have been a lot more consequential than *we* expected in the early '90s, and Linux has made huge, huge strides in enabling a wide range to be developed and maintained more-or-less sanely.

Thanks for the reference to CUSE. Our own experience with device drivers is sufficiently shallow that it's pointless for us to try to predict how they'll look in Linux's future. What we can contribute, though, is to urge readers to keep their minds open. When any of you has a device driver problem, ask around for help; in particular, don't assume that solutions are limited to the approaches or architectures you learned when you first encountered device drivers. Userland device drivers can be great, and there are plenty of other aspects that improve with each release of Linux.

Few different things.
Submitted by oiaohm1 on Mon, 12/08/2008 - 03:28.

Current issue is quite simple. Back porting kernel drivers on Linux is a living nightmare due to changes that happen in the internal kernel ABI. Particularly worse for closed source driver developers. Same with forwards porting. Same problems are hitting both directions.

Even if things like Cuse and Fusd require old kernels to be patched so they worked at least all the other drivers that sit on them would not have to be patched either. CUSE and Fusd both have the option of being cross platform. Yes single driver for BSD's Solarias and Linux. Even more platforms if the driver is coded in java or some other byte code.

Items like Cuse and Fusd will go forwards because there is an Kernel Developer need. Problem is what do you do with a no longer maintained driver. Remove it users of it will kill you. Keep it in kernel space means having to update it ever ABI change. Someone has to do it. User-space provides a nice halfway out. Once driver is pushed to user space it can be removed from the main kernel so no more maintaining issues. If there is a user still with that device they are not stuck either. Everyone kinda happy. User might be upset at a little be lower performance than it had before. There are quite a few drivers that fall into this issue.

Closed source kernel mode binary drivers are most likely dead. Reason ksplice project kreplace. Yes this is another solution to the driver back porting problem that should hopefully appear in 2.6.29 kernel. While kernel is running complete replacement of the complete kernel. So 2.6.29 may be able to turn into 2.6.30 without user having to stop anything. One major nightmare for an closed source kernel driver. Kreplace is all about don't back port the driver just update the kernel without needing a reboot.

Closed Source kernel mode binary driver makers the think they complain about the kernel mode abi of Linux being unstable between Linux kernel versions now happens at runtime.

Nvidia and Vmware are about to have a nightmare.

Firmware will be the other option not that closed source driver developers like since firmware pushes them way from directly interfacing with kernel.

Development of these user space solutions should speed up as the kernel space driver door closes. Before people say but performance will sux. There is even a solution around that called kernel mode linux that allows a user-space program to run in kernel space avoiding context switches also avoids running foul of kreplace.

Don't worry about the lateness of reply. I have my nose in a lot of mailing list watching what is going on. Lot of it highly interesting for 2009. After 2009 everything should go back to status normal. No more major driver changes for a long time.