User Mode Drivers
I have a regular discussion about user-mode drivers and Linux. It starts with a customer who maintains a DOS system realizing that at some point--usually sooner than later--he must upgrade his application to run under another OS. Of course I suggest Linux.
In these discussions, the customer will proceed to explain that he or she doesn't know how to write a device driver and doesn't want to spend the time to learn how to do so. The customer's code on the DOS system is console-based and uses simple (x86) I/O ports to read the adaptor card. Typically, he or she also will vent a bit about how operating systems have hardware protection. It only takes me a minute to mention that Linux supports methods of directly accessing the device with a user application. Then there's the customer's problem with the absence of ISA slots in modern PCs; the old adaptor card won't work in a new system. PCI-based cards can replace the ISA-based device, but for his or her code there's a clincher: how do you know the settings of the PCI device?
This is a short article on writing user-mode device drivers to penetrate the abstraction layer from a user application and to determine where a PCI card is located. This information is useful for those concerned with replacing their DOS machines and porting code to Linux. There are many reasons in favor of a user-mode driver. The user-mode method is very useful for validating new hardware. It's also convenient for informal accessing of the device on a register level. But, there are many simple hardware devices that don't implement interrupts, and their operation is hardly worth writing a device driver for.
Historically, x86 peripherals were I/O mapped. The x86 architecture has two memory maps: one for the memory and the other for peripherals. This separate map has unique instructions for accessing it. In Port and Out Port were used to read and write bytes, words or longs (in those days) to the hardware.
The Linux system calls ioperm(2) and iopl(2) (with a level argument of 3) are the silver-bullet commands for penetrating the abstraction layer. These functions are reserved for the root user (UID == 0), and each is slightly different. ioperm() opens up a block of the I/O space to the user application. This function requires the starting I/O base address and the block size (length) of consecutive addresses. ioperm() is useful for accessing ports located from 0x0000 through 0x03ff. On the other hand, iopl() opens up the entire I/O space for accessing. PCI devices that are I/O mapped typically are located above the space ioperm() permits, so iopl() must be used.
Once the abstraction layer is opened, there are six primary system calls for reading and writing to the I/O map. Three are for reading, and three are for writing to the I/O map. The reading triplet is inb(), inw() and inl() (for byte, word and long, respectively). The writing triplet is outb(), outw() and outl() (for byte, word and long, respectively). The header file is <sys/io.h>.
First the application needs only to call the appropriate function (ioperm or iopl), then inspect the error code returned by the system call, if any. Both ioperm and iopl return zero on success. Now you can start reading and writing--it's that simple.
Many devices are memory mapped for two reasons. First, for x86 architectures, the memory map is more expansive than the I/O map. Most other architectures only have a memory-mapped architecture (in other words, you don't get a choice); only the x86 architecture has the second address space for peripherals. Again, there are methods to penetrate the memory protection mechanism to access the hardware. The other reason is that arguably, more powerful addressing modes allow faster and more flexible methods of accessing the memory. The x86 port commands are extremely limited in features.
To access your memory-mapped hardware, the device /dev/mem is the silver bullet, and mmap() is the method to select the memory physical base address and the block size to access. To open the peripheral memory, open /dev/mem. Use the opened file descriptor in mmap(), with the appropriate address and the block size (in bytes) to open. mmap() returns an address that is mapped to the physical base address; cast it to the appropriate data type and use it like any other pointer, array, structure pointer or your favorite method.
Many hardware devices are hard-coded. Usually a good reference will describe where the device is located. For example, my employer manufactures many ISA-based products where the device address is set using jumpers. A device's address is found by comparing its jumpers and a table in a user's guide. That's easy...almost.
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Tech Tip: Really Simple HTTP Server with Python
- Home, My Backup Data Center
- New Products
- Android is Linux -- why no better inter-operation
27 min 43 sec ago
- Connecting Android device to desktop Linux via USB
56 min 13 sec ago
- Find new cell phone and tablet pc
1 hour 54 min ago
3 hours 23 min ago
- Automatically updating Guest Additions
4 hours 31 min ago
- I like your topic on android
5 hours 18 min ago
- Reply to comment | Linux Journal
5 hours 39 min ago
- This is the easiest tutorial
11 hours 53 min ago
- Ahh, the Koolaid.
17 hours 32 min ago
- git-annex assistant
23 hours 32 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.