Hi,

I'm learning writing device driver and have found some examples in the
net. I tried one of them. But got over hundreds error messages during
the compile. Almost all the error complain the syntax problem in
Linux include files such as "fs.h", "kernel.h" and "module.h". Any help
would be appreciated!

Le

Under Mandrake 7.0 (2.2.14)
to make the following code,

# Makefile for a basic kernel module

CC=gcc
MODCFLAGS := -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLINUX

chardev.o: chardev.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c chardev.c

/* chardev.c
* Copyright (C) 1998-1999 by Ori Pomerantz
*
* Create a character device (read only)
*/

#include < linux/kernel.h >
#include < linux/module.h >
#if CONFIG_MODVERSIONS==1

#define MODVERSIONS
#include < linux/modversions.h >
#endif

#include < linux/fs.h >
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
#endif

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0)
#include < asm/uaccess.h > /* for put_user */
#endif

#define SUCCESS 0
#define DEVICE_NAME "char_dev"
#define BUF_LEN 80
static int Device_Open = 0;
static char Message[BUF_LEN];
static char *Message_Ptr;
static int device_open(struct inode *inode,
struct file *file)
{
 static int counter = 0;

#ifdef DEBUG
 printk ("device_open(%p,%p)\n", inode, file);
#endif

 printk("Device: %d.%d\n",
 inode->i_rdev >> 8, inode->i_rdev & 0xFF);

 if (Device_Open)
  return -EBUSY;

 Device_Open++;

 sprintf(Message,
  "If I told you once, I told you %d times - %s",
  counter++,
  "Hello, world\n");

 Message_Ptr = Message;

 MOD_INC_USE_COUNT;

 return SUCCESS;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
static int device_release(struct inode *inode,
   struct file *file)
#else
static void device_release(struct inode *inode,
   struct file *file)
#endif
{
#ifdef DEBUG
 printk ("device_release(%p,%p)\n", inode, file);
#endif

 Device_Open --;

 MOD_DEC_USE_COUNT;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
 return 0;
#endif
}


#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
static ssize_t device_read(struct file *file,
    char *buffer,
    size_t length,
    loff_t *offset)
#else
static int device_read(struct inode *inode,
    struct file *file,
    char *buffer,
    int length)
#endif
{
 int bytes_read = 0;

 if (*Message_Ptr == 0)
 return 0;

 while (length && *Message_Ptr) {

  put_user(*(Message_Ptr++), buffer++);

  length --;
  bytes_read ++;
 }

#ifdef DEBUG
 printk ("Read %d bytes, %d left\n",
  bytes_read, length);
#endif

 return bytes_read;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
static ssize_t device_write(struct file *file,
    const char *buffer,
    size_t length,
    loff_t *offset)
#else
static int device_write(struct inode *inode,
   struct file *file,
   const char *buffer,
   int length)
#endif
{
 return -EINVAL;
}

static int Major;

struct file_operations Fops = {
  NULL, /* seek */
  device_read,
  device_write,
  NULL, /* readdir */
  NULL, /* select */
  NULL, /* ioctl */
  NULL, /* mmap */
  device_open,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  NULL, /* flush */
#endif
  device_release /* a.k.a. close */
};


int init_module()
{
 Major = module_register_chrdev(0,
     DEVICE_NAME,
     &Fops);

 if (Major < 0) {
  printk ("%s device failed with %d\n",
   "Sorry, registering the character",
   Major);
  return Major;
 }

 printk ("%s The major device number is %d.\n",
  "Registeration is a success.",
  Major);
 printk ("If you want to talk to the device driver,\n");
 printk ("you'll have to create a device file. \n");
 printk ("We suggest you use:\n");
 printk ("mknod c %d \n", Major);
 printk ("You can try different minor numbers %s",
  "and see what happens.\n");

 return 0;
}


void cleanup_module()
{
 int ret;

 ret = module_unregister_chrdev(Major, DEVICE_NAME);

 if (ret < 0)
  printk("Error in unregister_chrdev: %d\n", ret);
}



Errors:

[root@ws46 02_chardev]# make
gcc -O6 -Wall -DCONFIG_KERNELD -DMODULE -D__KERNEL__ -DLINUX -c
chardev.c
In file included from /usr/include/linux/fs.h:270,
from chardev.c:20:
/usr/include/linux/hpfs_fs_i.h:5: parse error before `ino_t'
/usr/include/linux/hpfs_fs_i.h:5: warning: no semicolon at end of struct

or union
/usr/include/linux/hpfs_fs_i.h:12: parse error before `:'
In file included from /usr/include/linux/fs.h:272,
from chardev.c:20:
/usr/include/linux/msdos_fs_i.h:36: parse error before `off_t'
/usr/include/linux/msdos_fs_i.h:36: warning: no semicolon at end of
struct or union
In file included from /usr/include/linux/fs.h:273,
from chardev.c:20:
/usr/include/linux/umsdos_fs_i.h:62: field `msdos_info' has incomplete
type
/usr/include/linux/umsdos_fs_i.h:69: parse error before `off_t'
/usr/include/linux/umsdos_fs_i.h:69: warning: no semicolon at end of
struct or union
/usr/include/linux/umsdos_fs_i.h:73: parse error before `}'
In file included from /usr/include/linux/fs.h:274,
from chardev.c:20:
/usr/include/linux/iso_fs_i.h:11: parse error before `off_t'
/usr/include/linux/iso_fs_i.h:11: warning: no semicolon at end of struct

or union
In file included from /usr/include/linux/affs_fs_i.h:5,
from /usr/include/linux/fs.h:277,
from chardev.c:20:
/usr/include/linux/time.h:10: parse error before `time_t'
/usr/include/linux/time.h:10: warning: no semicolon at end of struct or
union
/usr/include/linux/time.h:12: parse error before `}'
/usr/include/linux/time.h: In function `timespec_to_jiffies':
/usr/include/linux/time.h:32: dereferencing pointer to incomplete type
/usr/include/linux/time.h:33: dereferencing pointer to incomplete type
/usr/include/linux/time.h: In function `jiffies_to_timespec':
/usr/include/linux/time.h:45: dereferencing pointer to incomplete type
/usr/include/linux/time.h:46: dereferencing pointer to incomplete type
/usr/include/linux/time.h: At top level:
/usr/include/linux/time.h:50: parse error before `time_t'
/usr/include/linux/time.h:50: warning: no semicolon at end of struct or
union
/usr/include/linux/time.h:51: warning: data definition has no type or
storage class
/usr/include/linux/time.h:83: field `it_interval' has incomplete type
/usr/include/linux/time.h:84: field `it_value' has incomplete type
/usr/include/linux/time.h:88: field `it_interval' has incomplete type
/usr/include/linux/time.h:89: field `it_value' has incomplete type
In file included from /usr/include/linux/fs.h:277,
from chardev.c:20:
/usr/include/linux/affs_fs_i.h:11: field `kc_lru_time' has incomplete
type
In file included from /usr/include/linux/fs.h:279,
from chardev.c:20:
....



Reply via email to