Wednesday, June 20, 2007

LWN: Porting device drivers to the 2.6 kernel

http://lwn.net/Articles/driver-porting/

Porting device drivers to the 2.6 kernel

[Posted February 11, 2003 by corbet]

The 2.6 kernel contains a long list of changes which affect device driver writers. As part of the task of porting the Linux Device Drivers sample code to 2.6, your humble LWN Kernel Page author is producing a set of articles describing the changes which must be made. The articles are Kernel Page as they are written; they will also be collected here. With luck, this page will be a useful reference for those who must port drivers to the new kernel.

The creation of these articles is funded by LWN.net subscribers. If you find this material useful, please consider subscribing to LWN to help ensure that more of it gets written.

Except when otherwise specified, all of the articles below are written by LWN editor Jonathan Corbet. The date and kernel version attached to each article notes when the article was last updated.

Recent changes

The most recent changes to this series are:

  • (April 28, 2004) The Workqueue Interface updated to include create_singlethread_workqueue(), which was merged in 2.6.6.

  • (January 6, 2004) Supporting mmap() and Dealing with interrupts have been updated to reflect API changes in 2.6.1.

  • (November 25, 2003) The entire set of articles has been updated to reflect the 2.6.0-test10 kernel.

Getting started

Porting 'hello world' (February, 2003); which covers the changes required to update the simplest possible module to the 2.5 kernel.

Compiling external modules (November, 2003; 2.6.0-test9); how to build modules with the new module loader and kernel build scheme.

More module changes (November, 2003, 2.6.0-test9) covers other changes to the module loading subsystem, including module parameters, use count management, exporting symbols, and more.

Miscellaneous changes is a collection point for changes which are too small to justify their own article. Currently covered topics include kdev_t, designated initializers, and min() and max(). It was last updated on November 3, 2003 (2.6.0-test9).

Support interfaces

Char drivers and large dev_t (November 2003, 2.6.0-test9); registration and management of char drivers in the new, large dev_t environment.

The seq_file interface (September 2003; 2.6.0-test6); the easy way to implement virtual files correctly. A standalone example module is provided to demonstrate the use of this interface.

Low-level memory allocation (November, 2003; 2.6.0-test9); changes to functions for allocating chunks of memory and pages, and a description of the new mempool interface.

Per-CPU variables (November, 2003; 2.6.0-test9); the 2.6 interface for maintaining per-CPU data structures.

Timekeeping changes (November, 2003; 2.6.0-test9); changes to how the kernel manages time and time-related events.

The workqueue interface (April, 2004; 2.6.6-rc3); a description of the new deferred execution mechanism which replaces task queues (and bottom halves in general).

Creating virtual filesystems with libfs (November, 2003; 2.6.0-test9). This article, which looks at how a kernel module can create its own virtual filesystem, predates the driver porting series but fits in well with it.

DMA Changes (November, 2003, 2.6.0-test9); changes to the DMA support layer. There is also a quick reference page for the new generic DMA API.

Sleeping and mutual exclusion

Mutual exclusion with seqlocks (November, 2003, 2.6.0-test9); a description of how to use the seqlock (formerly frlock) capability which was merged into 2.5.60.

The preemptible kernel (November, 2003; 2.6.0-test9); a look at how kernel preemption affects driver code and what can be done to work safely in the preemptible environment.

Sleeping and waking up (November, 2003; 2.6.0-test9); new ways of putting processes to sleep with better performance and without race conditions.

Completion events (November, 2003; 2.6.0-test9); documentation for the completion event mechanism.

Using read-copy-update (November, 2003; 2.6.0-test9); working with the read-copy-update mutual exclusion scheme.

Advanced driver tasks

Dealing with interrupts (January, 2004; 2.6.1-rc2); interrupt handling changes which are visible to device drivers.

Supporting asynchronous I/O (November, 2003; 2.6.0-test9); how to write drivers which support the 2.6 asynchronous I/O interface.

Network drivers (November 2003, 2.6.0-test9); porting network drivers, with an emphasis on the new dynamic net_device allocation functions and NAPI support.

USB driver API changes (July 2003; 2.5.75); how USB drivers have changed in the 2.5 development series. This article was contributed by USB maintainer Greg Kroah-Hartman.

Block drivers

Block layer overview (November, 2003; 2.6.0-test9). The block layer has seen extensive changes in the 2.5 development series; this article gives an overview of what has been done while deferring the details for subsequent articles.

A simple block driver (November, 2003; 2.6.0-test9); this article presents the simplest possible block driver (a basic ramdisk implementation) with discussion of how the basic block interfaces have changed in 2.6. Full source to a working driver is included.

The gendisk interface (November, 2003; 2.6.0-test9); how to work with the new generic disk interface, which takes on a rather larger role in 2.6.

The BIO structure (November, 2003; 2.6.0-test9); the new low-level structure representing block I/O operations.

Request queues I (November, 2003; 2.6.0-test9); the basics of block request queues in 2.6, including request processing, request preparation control, and DMA support.

Request queues II (November, 2003, 2.6.0-test9); advanced request queue topics, including command preparation, tagged command queueing, and the "make request" mode of operation.

Memory management

Supporting mmap() (January, 2004 - 2.6.1-rc2); changes in how device drivers support the mmap() system call.

Zero-copy user-space access (November, 2003 - 2.6.0-test9); how to get direct-access to user space to perform zero-copy I/O. If you used the kiobuf interface for this purpose in 2.4, you'll want to look here for the 2.6 equivalent.

Atomic kmaps (November, 2003; 2.6.0-test9); quick access to high-memory via kmap_atomic().

Device model

A device model overview (November, 2003; 2.6.0-test10); an introductory look at the Linux device model and sysfs, with definitions of some commonly encountered terms.

The zen of kobjects (October, 2003; 2.6.0-test6); an attempt to demystify the kobject abstraction and its use in the kernel.

kobjects and sysfs (October, 2003; 2.6.0-test8); a description of the interaction between the kobject type and its representation in sysfs.

kobjects and hotplug events (October, 2003; 2.6.0-test6); an explanation of the kset hotplug operations and how they can be used to control how hotplug events are reported to user space. This article was written by Greg Kroah-Hartman.

Examining a kobject hierarchy (October, 2003; 2.6.0-test9); a visual exploration of the device model data structures behind /sys/block.

Device classes (November, 2003; 2.6.0-test10); how the device class mechanism works.


(Log in to post comments)

Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 1:55 UTC (Wed) by subscriber fdesloges [Link]

Wow!

This is highly valuable stuff. Are you certain you want to give this away
for free after only a few days ?

This alone would be a very good reason to subscribe. And as reference
stuff it will still be valuable many months down the road.

Maybe it could fit in a "available to non-subsciber 4 months later"
category ? Or make only a few articles available (and the index of course
to hook subscribers) ?

Whatever you do, thanks!

FD

Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 9:58 UTC (Wed) by subscriber KotH [Link]

I wouldnt do that. LWN has a very good reputation as information source
around the open source community. A "closed for all but subscribed ppl"
policy would surely destroy that reputation.

Yes, i know that LWN doesnt have as much subscribers as it should have :(
but this is IMHO not the right way to get more.

Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 11:00 UTC (Wed) by subscriber bruno [Link]

Do you work for nothing? Do you give all your time to other in exchange
of nothing? You can't pretend that other people do that, the people have
childrens, wives, mortgages... and work in exchange of money to pay their
bills. Sometimes you have a bussiness plan that lets you to give away
your work and get money from other sources, sometimes you need to put a
price or close the shop.

Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 14:14 UTC (Wed) by subscriber Webexcess [Link]

Do you work for nothing? Do you give all your time to other in exchange of nothing?

I think you're oversimplifying a bit. The excellent writing at lwn.net is both its product and its advertising. How will potential subscribers be enticed if they can't see what lwn.net has to offer?

Also, the target audience largely consists of enthusiasts, many of whom are students and/or live in parts of the world where american dollars are very expensive. These are the same people that helped to build Linux into what it is today -- are you suggesting that they should be excluded?

Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 15:27 UTC (Wed) by subscriber bruno [Link]

The excellent writing at lwn.net is only possible if there are someone that
works in it full time. I think is wonderfull if someone can (and want) to
work full time in something and give it away for nothing, but I don't think
that you can say to someone "You MUST give your work for free", at the end
of the day, with your work you have the right to make whatever you want:
Give if for free, sell, rent, lease or burn it, is your work and your
decision.

If KotH really thinks that is important that this information be free, he can
study the linux kernel, write a book and put it on the web for free, instead
of criticise the actitude of lwn.net

Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 17:17 UTC (Wed) by subscriber rknop [Link]

"You MUST give your work for free"... If KotH really thinks that is important that this information be free,

Be fair. That's not what KotH said. He said he thought it was in LWN.net's best interest to do what they've done, because of their good reputation in the community. He's not insisting that they must do what they've done, he's just congratulating them and saying he understands why they might think it's a good idea to do that.

-Rob

Re: Porting device drivers to the 2.5 kernel
Posted Feb 12, 2003 19:52 UTC (Wed) by subscriber Ross [Link]

Bruno,

I think you are mischaracterizing what other people are saying. Using the device driver documentation as an advertisement was only a suggestion. We all recognize that LWN can decide what to release and when to release it. You're point is also only a suggestion. If Jonathan felt he needed to say something, I'm sure he would. You don't need to speak for him.

Porting device drivers to the 2.5 kernel
Posted May 8, 2003 17:05 UTC (Thu) by guest LogicG8 [Link]

I would just like to say that the release of this material prompted
me to purchase a subscription. It is a delicate balance providing
open content and providing incentive to readers to subscribe. I think
that LWN has done a great job and will continue to do so in the future.

Releasing quality content for free has nabbed at least one
subscriber.

Porting device drivers to the 2.5 kernel
Posted Dec 14, 2004 14:06 UTC (Tue) by subscriber arafel [Link]

Make that two...

On whether this stuff should be free
Posted Feb 12, 2003 20:07 UTC (Wed) by subscriber corbet [Link]

Just as a response to all the comments here... I appreciate the input, and certainly do not feel criticized by any of the comments.

For what it's worth, I did consider keeping this material non-free for longer than the usual period. It is different from the usual news, and it has a slightly longer useful life. In the end, I decided against such a move; I would like these articles to be generally useful, and to serve as a contribution to the kernel project. Maintaining the same access policy also lets me fold some of the articles into the Kernel Page, which could use it - development news tends to slow down a lot during feature freezes.

So the driver porting articles go free after a week. We may yet do things differently for similar material in the future.

jon

On whether this stuff should be free
Posted Dec 24, 2003 15:19 UTC (Wed) by subscriber nhasan [Link]

How about releasing it as a book? It should provide additional revenue to LWN and give us a hard copy reference.

An idea that works elsewhere
Posted Feb 27, 2003 15:20 UTC (Thu) by guest materlik [Link]

You might think about adding a little, polite blurb asking for a small donation to the end of each article, similar to what tidbits (a free Macintosh newsletter) is doing with PayBits. Maybe one only visible to non-subscribers?
Their experiences so far have not been overwhelming, but there is some money being donated because of the constant reminders on the value of filtered, edited information.

Porting device drivers to the 2.5 kernel
Posted Feb 28, 2003 20:09 UTC (Fri) by subscriber vonbrand [Link]

I think this deserves a place in the head of the page (perhaps replacing Old Site or Weekly edition). It is not a "Recent feature" anymore...

One minor gripe is that the articles don't default to a printable page format (yes, I do promise to buy the next book of yours regardless ;-)

Porting device drivers to the 2.6 kernel
Posted Jun 14, 2004 22:31 UTC (Mon) by subscriber Marsclic [Link]

This is the material that made me subscribe to this site. Keep it coming Jonnathan!

Porting drivers from kernel 2.4 to kernel 2.6,where can I find the documents
Posted Oct 29, 2004 3:56 UTC (Fri) by guest dkotian [Link]

Porting drivers from kernel 2.4 to kernel 2.6,where can I find the documents

Porting drivers from kernel 2.4 to kernel 2.6,where can I find the documents
Posted Nov 8, 2004 18:49 UTC (Mon) by guest gXyZOd [Link]

I'm looking for some information too.
Seeing that devfs is obsolent I would have to port a module that implements a "Utime Calibration" (taken from the Kansas University Real Time patch) from the 2.4.18 to the 2.6.8/9 kernel.
Not using devfs any more in the 2.6 kernel the devfs_[register/unregister]_chrev functions are useless.

I'm a n00b and am trying hard to learn.

Thanks for any response.

Porting device drivers to the 2.6 kernel
Posted Nov 26, 2004 3:30 UTC (Fri) by guest prakash07 [Link]

I am using the function
alloc_chrdev_region(dev_t *dev,61,1,"prakash");
in module but while compilation it is giving the error.

***parse error before dev_t;

i also added the .
please any one help me .

char device drivers on the 2.6 kernel
Posted May 6, 2005 21:56 UTC (Fri) by guest prashy [Link]

I am new to Linux Kernel Programming. Trying to build a basic char driver (register, open, release, unregister). When I compile the source I get the following errors, (even on kernel - 2.4). I try to compile the char drivers provided with the source and even get errors and warnings with them, here is the error dump,

prcdd.c:6:1: warning: "__KERNEL__" redefined
prcdd.c:1:1: warning: this is the location of the previous definition
prcdd.c:7:1: warning: "MODULE" redefined
prcdd.c:1:1: warning: this is the location of the previous definition
In file included from /usr/include/linux/fs.h:23,
from prcdd.c:11:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland!
prcdd.c:12:25: asm/uaccess.h: No such file or directory
prcdd.c:17: warning: `struct file' declared inside parameter list
prcdd.c:17: warning: its scope is only this definition or declaration, which is probably not what you want
prcdd.c:17: warning: `struct inode' declared inside parameter list
prcdd.c:18: warning: `struct file' declared inside parameter list
prcdd.c:18: warning: `struct inode' declared inside parameter list
prcdd.c:26: variable `fops' has initializer but incomplete type
prcdd.c:27: unknown field `owner' specified in initializer
prcdd.c:27: warning: excess elements in struct initializer
prcdd.c:27: warning: (near initialization for `fops')
prcdd.c:28: unknown field `open' specified in initializer
prcdd.c:28: warning: excess elements in struct initializer
prcdd.c:28: warning: (near initialization for `fops')
prcdd.c:29: unknown field `release' specified in initializer
prcdd.c:29: warning: excess elements in struct initializer
prcdd.c:29: warning: (near initialization for `fops')
prcdd.c: In function `prcdd_init':
prcdd.c:34: warning: implicit declaration of function `register_chrdev'
prcdd.c:38: warning: implicit declaration of function `printk'
prcdd.c: In function `prcdd_exit':
prcdd.c:48: warning: implicit declaration of function `unregister_chrdev'
prcdd.c: At top level:
prcdd.c:55: warning: `struct file' declared inside parameter list
prcdd.c:55: warning: `struct inode' declared inside parameter list
prcdd.c:56: conflicting types for `prcdd_open'
prcdd.c:17: previous declaration of `prcdd_open'
prcdd.c: In function `prcdd_open':
prcdd.c:59: `EBUSY' undeclared (first use in this function)
prcdd.c:59: (Each undeclared identifier is reported only once
prcdd.c:59: for each function it appears in.)
prcdd.c:63: warning: implicit declaration of function `atomic_inc'
prcdd.c:63: union has no member named `usecount'
prcdd.c: At top level:
prcdd.c:67: warning: `struct file' declared inside parameter list
prcdd.c:67: warning: `struct inode' declared inside parameter list
prcdd.c:68: conflicting types for `prcdd_release'
prcdd.c:18: previous declaration of `prcdd_release'
prcdd.c: In function `prcdd_release':
prcdd.c:71: warning: implicit declaration of function `atomic_dec'
prcdd.c:71: union has no member named `usecount'
prcdd.c: At top level:
prcdd.c:26: storage size of `fops' isn't known

This is the source code,

#if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
#include
#define MODVERSIONS
#endif

#define _KERNEL_
#define MODULE

#include
#include
#include
#include

//static int module_init(void);
//static void module_exit(void);
static int prcdd_open(struct inode *,struct file *);
static int prcdd_release(struct inode *,struct file *);

#define SUCCESS 0
#define DEVICE_NAME "prcdd"

static int Major;
static int Device_open = 0;

static struct file_operations fops = {
.owner = THIS_MODULE,
.open = prcdd_open,
.release = prcdd_release,
};

static int prcdd_init(void)
{
Major = register_chrdev(0,DEVICE_NAME,&fops);
if (Major < 0)
{
printk("Registering the character device failed with %d \n",Major);
return Major;
}

printk("<1>The assigned major number is %d \n",Major);
return 0;
}

static void prcdd_exit(void)
{
int ret = unregister_chrdev(Major,DEVICE_NAME);
if (ret < 0)
{
printk("Error in unregister_chrdev: %d \n",ret);
}
}

static int prcdd_open(struct inode *inode, struct file *file)
{
if (Device_open)
{
return -EBUSY;
}
Device_open++;

MOD_INC_USE_COUNT;
return SUCCESS;
}

static int prcdd_release(struct inode *inode, struct file *file)
{
Device_open--;

MOD_DEC_USE_COUNT;
return 0;
}

module_init(prcdd_init);
module_exit(prcdd_exit);

Any pointers on how to proceed from here?

char device drivers on the 2.6 kernel
Posted Apr 23, 2007 4:15 UTC (Mon) by guest psh2001 [Link]

Hi,

Any one has any solutions for this ?
even i am facing the same issue ...

Thanks,
Shiv

char device drivers on the 2.6 kernel
Posted Jun 1, 2007 8:09 UTC (Fri) by guest hy71 [Link]

i am facing the same problem...
could anyone give some suggestion?

thanks a lot!

swearing in the linux kernel
Posted May 13, 2005 14:35 UTC (Fri) by guest tomek [Link]

graphs over swearing in the linux kernel: http://www.vidarholen.net/contents/wordcount/ Tom

Porting PCTel modem driver to the 2.6 kernel
Posted Jun 15, 2005 16:59 UTC (Wed) by guest DMXMusic [Link]

Here is our situation - we have a working 2.4 driver for the PCTel PCT789T chip/modem. I have been looking high and low for a 2.6 version of this driver, but nobody seems to have taken on the task of porting the driver. I'm wondering if anyone can give me a rough time estimate for porting this driver - (it's basically a serial port driver, the dsp code is in a proprietary lib file) - I have done a couple simple Unix / Linux drivers, and worked on windows display drivers for a couple years, although I have not worked on serial port drivers. Any help is very much appreciated!

Our company has something like 5000 of these modems on our devices in the field, and we need to support our customers.

Thanks - Phil Brooks, Philip.Brooks@DMXMusic.com

Porting modem drivers to the 2.6 kernel
Posted Aug 11, 2005 4:41 UTC (Thu) by guest dabenavidesd [Link]

Hi Phil.
A time ago, i tried to install a driver of my Intel 537 EP that is instaled on my ASUS board for AMD.
The driver was made for working on SuSE 9.1. However when trying to install in this OS, and compiling, following the specified steps, I've got 9 warnings.
The modem works perfectly under Windows XP (another company delivers the driver on CD besides the finished Intel chipset and other applications like “Classic Phone Tools” http://www.bvrp.com).
The driver is still hung in the Intel´s site, and this release supports 2.4.x and 2.6.x kernels.
Attached is my web site where is a historical record of the conversation between me and a support programmer from Intel Support Service; over all, we work in the makefile and correcting the files and dependencies of the used packages, although the trouble is still there.
Im interested in continue working about this kind of troubles. So let me know how can i help.
http://es.geocities.com/dabenavidesd/
email: dabenavidesd@unal.edu.co

Porting device drivers to the 2.6 kernel
Posted Aug 17, 2005 3:55 UTC (Wed) by guest johnlin [Link]

Hello,

I got a problem proting device driver from 2.4 to 2.6, i.e. the asm/smplocak.h seems not in 2.6 kernel/include/asm, so could anyone help ?

Thanks

John

Porting device drivers to the 2.6 kernel
Posted Mar 14, 2006 10:43 UTC (Tue) by guest Pype.Clicker [Link]

it looks like the "Big Kernel Lock" headers have been moved from to between 2.4 and 2.6 ... patching the #include directive did it for me, i guess it will do for you aswell.

Porting device drivers to the 2.6 kernel
Posted Jan 22, 2006 11:39 UTC (Sun) by guest LSM [Link]

This is the kind of stuff that made me subscribe to this community. Thanks Jonnathan!

LSM Networks Blog

Porting device drivers to the 2.6 kernel
Posted Feb 21, 2006 2:33 UTC (Tue) by guest York [Link]

Dear Jonnathan:
where are the differences between 2.4 and 2.6 about pci driver program?
How can I get the way to port a pci driver in 2.6?

Thank you!

Porting device drivers to the 2.6 kernel
Posted Feb 23, 2006 15:05 UTC (Thu) by guest agllno [Link]

Hi all,
I want a small information about interruptible_sleep_on .I want it to make compatible for kernel version 2.6 from 2.2.So What modification i have to do or any altenatives are there. Please reply and give some details
Thanks in advance
Agnello

Porting device drivers to the 2.6 kernel
Posted Mar 15, 2006 10:29 UTC (Wed) by guest vikramkg82 [Link]

hi

i am new to device driver can any body tell me how to access the device file...exactly i need to access the harddisk inforamation at block level..or else can u tell where i can get the information about this plz

Porting device drivers to the 2.6 kernel
Posted May 3, 2006 17:37 UTC (Wed) by subscriber vonbrand [Link]

Some random comments...

  • Have you thought of updating the articles (perhaps letting others do the (re)writing)? Some have comments indicating that updates are in order.
  • What about a cleanup of the comments? Most ones I see here aren't really about the subject matter, and are definitely useless for "longer term reference"...
Many thanks!

No comments:

如何发掘出更多退休的钱?

如何发掘出更多退休的钱? http://bbs.wenxuecity.com/bbs/tzlc/1328415.html 按照常规的说法,退休的收入必须得有退休前的80%,或者是4% withdrawal rule,而且每年还得要加2-3%对付通胀,这是一个很大...