Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK Beginning Linux Programming, 3rd Edition
This is the forum to discuss the Wrox book Beginning Linux Programming, 2nd Edition by Richard Stones, Neil Matthew, Alan Cox; ISBN: 9780764543739
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Beginning Linux Programming, 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2005, 12:30 PM
Registered User
 
Join Date: Sep 2005
Location: Castrovillari, , Italy.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default porting ch18 sample code to new kernels

Hi,
I'm posting these notes in the hope that they could be useful
to other readers.

While trying out sample code from Chapter 18 (Device
Drivers) I realized it doesn't work with 2.6+ kernels and
probably neither with 2.5.

Even if the code compiles, the resulting module cannot be
loaded into the kernel. This is because these more recent
kernel versions introduced many changes while the examples
were written at the time the 2.4 kernel series was in use.

Searching google I found this site which is an excellent
source of information about porting device drivers to the
2.6 kernel:

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

E.g., here is the "hello kernel" program on page 727
rewritten for kernel 2.5 and above:

/* hello.c */
/*
 * Hello world module.
 */
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
        printk(KERN_DEBUG "Hello, kernel!\n");
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_DEBUG "Good-bye, kernel!\n");
}

/* This is to avoid warnings when loading the module. */
MODULE_LICENSE("GPL");

Please note that the kernel source dir
/usr/src/linux-version or whatever you choose must be
configured using "make menuconfig" before compiling this
module. Also the kernel build mechanism changed and you
need a new Makefile:

# Makefile
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /usr/src/linux-$(shell uname -r)
PWD := $(shell pwd)

all:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
        rm -f *.mod.c *.ko *.o *~ core
endif

Note: if you cut & paste this ensure there is a tab before
every rule - see page 368.

Alternately you can use a simpler Makefile:

# Makefile
obj-m := hello.o

clean:
        rm -f *.mod.c *.ko *.o *~ core

but you will need a more elaborate command to build the
module instead of a simple "make":

$ make -C /usr/src/linux-version SUBDIRS=$PWD modules

Modules are now special kernel object ending with .ko in
place of .o:

# insmod hello.ko

PS:
I'd like to port subsequent examples if the book authors
haven't already done it.
Of course If I succeed I will post them here under GPL :-)

Should I run into problems will you - Neil, Rick or somebody
else - please help me, answering my specific questions or
pointing me out to other documentation?

antonio


 
Old February 12th, 2007, 12:50 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 <linux/modversions.h>
#define MODVERSIONS
#endif

#define _KERNEL_
#define MODULE

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

//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?




Similar Threads
Thread Thread Starter Forum Replies Last Post
Iam having problem running ch18 sample code wanes98 Wrox Book Feedback 1 June 7th, 2007 07:19 AM
want sample code mohiddin52 Access VBA 2 December 16th, 2006 04:54 AM
Sample Code mirouvor Java Databases 1 January 2nd, 2006 02:28 PM
Porting simple code from Borland to VC++ david_2138 Visual C++ 1 December 18th, 2004 08:43 PM
Where the sample code? rbtcx BOOK: Professional C#, 2nd and 3rd Editions 5 August 21st, 2003 02:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.