From ab121f379a3cff458c90e6f480ba4bb68c8733dd Mon Sep 17 00:00:00 2001 From: Javier Martinez Canillas Date: Sat, 27 Nov 2010 07:49:17 +0100 Subject: Linux Device Drivers 3 examples --- simple/Makefile | 39 +++++++++ simple/simple.c | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++ simple/simple_load | 26 ++++++ simple/simple_unload | 14 +++ 4 files changed, 314 insertions(+) create mode 100644 simple/Makefile create mode 100644 simple/simple.c create mode 100644 simple/simple_load create mode 100644 simple/simple_unload (limited to 'simple') diff --git a/simple/Makefile b/simple/Makefile new file mode 100644 index 0000000..429f743 --- /dev/null +++ b/simple/Makefile @@ -0,0 +1,39 @@ +# Comment/uncomment the following line to disable/enable debugging +#DEBUG = y + +# Add your debugging flag (or not) to CFLAGS +ifeq ($(DEBUG),y) + DEBFLAGS = -O -g # "-O" is needed to expand inlines +else + DEBFLAGS = -O2 +endif + +CFLAGS += $(DEBFLAGS) -I$(LDDINCDIR) + +ifneq ($(KERNELRELEASE),) +# call from kernel build system + +obj-m := simple.o + +else + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build +PWD := $(shell pwd) + +default: + $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINCDIR=$(PWD)/../include modules + +endif + + + +clean: + rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions + +depend .depend dep: + $(CC) $(CFLAGS) -M *.c > .depend + + +ifeq (.depend,$(wildcard .depend)) +include .depend +endif diff --git a/simple/simple.c b/simple/simple.c new file mode 100644 index 0000000..e6860c7 --- /dev/null +++ b/simple/simple.c @@ -0,0 +1,235 @@ +/* + * Simple - REALLY simple memory mapping demonstration. + * + * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet + * Copyright (C) 2001 O'Reilly & Associates + * + * The source code in this file can be freely used, adapted, + * and redistributed in source or binary form, so long as an + * acknowledgment appears in derived source files. The citation + * should list that the code comes from the book "Linux Device + * Drivers" by Alessandro Rubini and Jonathan Corbet, published + * by O'Reilly & Associates. No warranty is attached; + * we cannot take responsibility for errors or fitness for use. + * + * $Id: simple.c,v 1.12 2005/01/31 16:15:31 rubini Exp $ + */ + +#include +#include +#include +#include + +#include /* printk() */ +#include /* kmalloc() */ +#include /* everything... */ +#include /* error codes */ +#include /* size_t */ +#include +#include +#include +#include + +#include + +static int simple_major = 0; +module_param(simple_major, int, 0); +MODULE_AUTHOR("Jonathan Corbet"); +MODULE_LICENSE("Dual BSD/GPL"); + +/* + * Open the device; in fact, there's nothing to do here. + */ +static int simple_open (struct inode *inode, struct file *filp) +{ + return 0; +} + + +/* + * Closing is just as simpler. + */ +static int simple_release(struct inode *inode, struct file *filp) +{ + return 0; +} + + + +/* + * Common VMA ops. + */ + +void simple_vma_open(struct vm_area_struct *vma) +{ + printk(KERN_NOTICE "Simple VMA open, virt %lx, phys %lx\n", + vma->vm_start, vma->vm_pgoff << PAGE_SHIFT); +} + +void simple_vma_close(struct vm_area_struct *vma) +{ + printk(KERN_NOTICE "Simple VMA close.\n"); +} + + +/* + * The remap_pfn_range version of mmap. This one is heavily borrowed + * from drivers/char/mem.c. + */ + +static struct vm_operations_struct simple_remap_vm_ops = { + .open = simple_vma_open, + .close = simple_vma_close, +}; + +static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma) +{ + if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, + vma->vm_end - vma->vm_start, + vma->vm_page_prot)) + return -EAGAIN; + + vma->vm_ops = &simple_remap_vm_ops; + simple_vma_open(vma); + return 0; +} + + + +/* + * The nopage version. + */ +struct page *simple_vma_nopage(struct vm_area_struct *vma, + unsigned long address, int *type) +{ + struct page *pageptr; + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; + unsigned long physaddr = address - vma->vm_start + offset; + unsigned long pageframe = physaddr >> PAGE_SHIFT; + +// Eventually remove these printks + printk (KERN_NOTICE "---- Nopage, off %lx phys %lx\n", offset, physaddr); + printk (KERN_NOTICE "VA is %p\n", __va (physaddr)); + printk (KERN_NOTICE "Page at %p\n", virt_to_page (__va (physaddr))); + if (!pfn_valid(pageframe)) + return NOPAGE_SIGBUS; + pageptr = pfn_to_page(pageframe); + printk (KERN_NOTICE "page->index = %ld mapping %p\n", pageptr->index, pageptr->mapping); + printk (KERN_NOTICE "Page frame %ld\n", pageframe); + get_page(pageptr); + if (type) + *type = VM_FAULT_MINOR; + return pageptr; +} + +static struct vm_operations_struct simple_nopage_vm_ops = { + .open = simple_vma_open, + .close = simple_vma_close, + .nopage = simple_vma_nopage, +}; + +static int simple_nopage_mmap(struct file *filp, struct vm_area_struct *vma) +{ + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; + + if (offset >= __pa(high_memory) || (filp->f_flags & O_SYNC)) + vma->vm_flags |= VM_IO; + vma->vm_flags |= VM_RESERVED; + + vma->vm_ops = &simple_nopage_vm_ops; + simple_vma_open(vma); + return 0; +} + + +/* + * Set up the cdev structure for a device. + */ +static void simple_setup_cdev(struct cdev *dev, int minor, + struct file_operations *fops) +{ + int err, devno = MKDEV(simple_major, minor); + + cdev_init(dev, fops); + dev->owner = THIS_MODULE; + dev->ops = fops; + err = cdev_add (dev, devno, 1); + /* Fail gracefully if need be */ + if (err) + printk (KERN_NOTICE "Error %d adding simple%d", err, minor); +} + + +/* + * Our various sub-devices. + */ +/* Device 0 uses remap_pfn_range */ +static struct file_operations simple_remap_ops = { + .owner = THIS_MODULE, + .open = simple_open, + .release = simple_release, + .mmap = simple_remap_mmap, +}; + +/* Device 1 uses nopage */ +static struct file_operations simple_nopage_ops = { + .owner = THIS_MODULE, + .open = simple_open, + .release = simple_release, + .mmap = simple_nopage_mmap, +}; + +#define MAX_SIMPLE_DEV 2 + +#if 0 +static struct file_operations *simple_fops[MAX_SIMPLE_DEV] = { + &simple_remap_ops, + &simple_nopage_ops, +}; +#endif + +/* + * We export two simple devices. There's no need for us to maintain any + * special housekeeping info, so we just deal with raw cdevs. + */ +static struct cdev SimpleDevs[MAX_SIMPLE_DEV]; + +/* + * Module housekeeping. + */ +static int simple_init(void) +{ + int result; + dev_t dev = MKDEV(simple_major, 0); + + /* Figure out our device number. */ + if (simple_major) + result = register_chrdev_region(dev, 2, "simple"); + else { + result = alloc_chrdev_region(&dev, 0, 2, "simple"); + simple_major = MAJOR(dev); + } + if (result < 0) { + printk(KERN_WARNING "simple: unable to get major %d\n", simple_major); + return result; + } + if (simple_major == 0) + simple_major = result; + + /* Now set up two cdevs. */ + simple_setup_cdev(SimpleDevs, 0, &simple_remap_ops); + simple_setup_cdev(SimpleDevs + 1, 1, &simple_nopage_ops); + return 0; +} + + +static void simple_cleanup(void) +{ + cdev_del(SimpleDevs); + cdev_del(SimpleDevs + 1); + unregister_chrdev_region(MKDEV(simple_major, 0), 2); +} + + +module_init(simple_init); +module_exit(simple_cleanup); diff --git a/simple/simple_load b/simple/simple_load new file mode 100644 index 0000000..5cd004d --- /dev/null +++ b/simple/simple_load @@ -0,0 +1,26 @@ +#!/bin/sh +module="simple" +device="simple" +mode="664" + +# Group: since distributions do it differently, look for wheel or use staff +if grep '^staff:' /etc/group > /dev/null; then + group="staff" +else + group="wheel" +fi + +# invoke insmod with all arguments we got +# and use a pathname, as newer modutils don't look in . by default +/sbin/insmod -f ./$module.ko $* || exit 1 + +major=`cat /proc/devices | awk "\\$2==\"$module\" {print \\$1}"` + +# Remove stale nodes and replace them, then give gid and perms +# Usually the script is shorter, it's simple that has several devices in it. + +rm -f /dev/${device}[rn] +mknod /dev/${device}r c $major 0 +mknod /dev/${device}n c $major 1 +chgrp $group /dev/${device}[rn] +chmod $mode /dev/${device}[rn] diff --git a/simple/simple_unload b/simple/simple_unload new file mode 100644 index 0000000..dca9421 --- /dev/null +++ b/simple/simple_unload @@ -0,0 +1,14 @@ +#!/bin/sh +module="simple" +device="simple" + +# invoke rmmod with all arguments we got +/sbin/rmmod $module $* || exit 1 + +# Remove stale nodes +rm -f /dev/${device}[rn] + + + + + -- cgit v1.2.1-18-gbd029