looking for simple module/driver programming examples in source tree

Robert P. J. Day rpjday at crashcourse.ca
Tue May 3 07:45:54 EDT 2011


  as a followup to an earlier post, this is kind of a nebulous
proposal but i want to start collecting examples out of the kernel
source tree that demonstrate a lot of the basic or introductory kernel
programming concepts.  with some of the courseware i deal with,
there's typically a perfectly reasonable explanation for numerous
introductory concepts and topics, but none of that beats being
accompanied by an actual example right out of the source.

  for example, i can explain in detail how to write a simple,
read-only proc file, or i can just point at some terrific examples in
the fs/proc directory, such as version.c:

=== snip header files ===

static int version_proc_show(struct seq_file *m, void *v)
{
        seq_printf(m, linux_proc_banner,
                utsname()->sysname,
                utsname()->release,
                utsname()->version);
        return 0;
}

static int version_proc_open(struct inode *inode, struct file *file)
{
        return single_open(file, version_proc_show, NULL);
}

static const struct file_operations version_proc_fops = {
        .open           = version_proc_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = single_release,
};

static int __init proc_version_init(void)
{
        proc_create("version", 0, NULL, &version_proc_fops);
        return 0;
}
module_init(proc_version_init);

=== end version.c ===

  once you see that living, breathing example, what more would you
need to know about how to have your module/driver create a simple
read-only proc file?

  as another example, we all know that if your module init() routine
runs into an error, it has an obligation to carefully (and, typically,
in reverse order) undo everything it's done up to that point.
sometimes, that routine is loaded with "goto" stmts.  other times, the
module author might create a "cleanup" routine that is callable from
both the init and exit routines.  i'm currently perusing the source
for nice, uncluttered examples of exactly that (where the explanation
isn't buried under extraneous processing to the point where it's hard
to see the relevant code).

  and the list goes on and on -- there are assuredly all sorts of
source files for drivers that can be used as respectable examples of
all sorts of *introductory* concepts.

  i'm also interesting in collecting examples of documentation for
basic kernel programming concepts.  sometimes, there are excellent
examples in the source Documentation/ directory, sometimes not.  or
there might be high-quality tutorials at places like lwn.net,
developerworks, and lots of other places.  i'm interested in starting
a list of tutorials like that that explain, ideally, a single topic
really, really well for the sake of beginners.

  thoughts?

rday

-- 

========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================



More information about the Kernelnewbies mailing list