#define __KERNEL__ /* kernel code */ #define MODULE /* always as a module */ #include #include /* for file_operations */ #define DEFAULT_MAJOR 121 MODULE_AUTHOR("Guto"); MODULE_DESCRIPTION("Just another dummy module to play with"); /* MODULE PARAMETERS */ static int dev = DEFAULT_MAJOR; MODULE_PARM(dev,"i"); MODULE_PARM_DESC(dev,"Major device number"); /* PROTOTYPES */ static int init_module(void); static void cleanup_module(void); static int dummy_open(struct inode * inode, struct file * filp); static int dummy_close(struct file * filp); static ssize_t dummy_read(struct file * filp, char * buf, size_t count, loff_t * offset); /* FILE OPERATIONS */ static struct file_operations dummy_fops = { owner: NULL, llseek: NULL, read: &dummy_read, write: NULL, readdir: NULL, poll: NULL, ioctl: NULL, mmap: NULL, open: &dummy_open, flush: &dummy_close, release: NULL, fsync: NULL, fasync: NULL, lock: NULL, readv: NULL, writev: NULL, sendpage: NULL, get_unmapped_area: NULL }; int init_module(void) { printk("Loading module dummy with major=%d\n", dev); /* Set field owner of file_operations to this module */ SET_MODULE_OWNER(&dummy_fops); /* Register the device driver */ return register_chrdev(dev, "dummy", &dummy_fops); } void cleanup_module(void) { printk("Unloading module dummy\n"); /* Unregister the device driver */ unregister_chrdev(dev, "dummy"); } int dummy_open(struct inode * inode, struct file * filp) { int unit; /* Get device's minor number */ unit = MINOR(inode->i_rdev); printk("dummy_open(unit=%d)\n", unit); return 0; } int dummy_close(struct file * filp) { int unit; /* Get device's minor number */ unit = MINOR(filp->f_dentry->d_inode->i_rdev); printk("dummy_close(unit=%d)\n", unit); return 0; } ssize_t dummy_read(struct file * filp, char * buf, size_t count, loff_t * offset) { int unit, i; /* Get device's minor number */ unit = MINOR(filp->f_dentry->d_inode->i_rdev); printk("dummy_read(unit=%d,buf=%p,cnt=%d,off=%d)\n", unit, buf, count, *offset); /* Produces a dummy output */ for(i = 0; i < count; i++) buf[i] = 'D'; return count; }