Monday, March 11, 2013

Device Driver - initial stuff

Makefile1

 ifneq (${KERNELRELEASE},)
    obj-m := mynull.o
else
    KERNEL_SOURCE := /usr/src/linux-headers-$(shell uname -r)/
    PWD := $(shell pwd)
default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
clean:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif


Makefile2

 obj-m+=ofcd.o
all:
    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
install:
    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules_install


ofd.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>

static int __init ofd_init(void)
{
    printk(KERN_INFO "Namaskar: ofd registered\n");
    return 0;
}

static void __exit ofd_exit(void)
{
    printk(KERN_INFO "Alvida: ofd unregistered\n");
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("vinos");
MODULE_DESCRIPTION("Our first driver");


ofcd.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>

#include <linux/types.h>  /* For dev_t */
#include <linux/kdev_t.h> /* For MAJOR, MINOR, MKDEV */
#include <linux/fs.h>     /* For register_chrdev_region/alloc_chrdev_region */

static dev_t first;

static int __init ofcd_init(void)
{
    printk(KERN_INFO "Namaskar: ofcd registered\n");
    if (alloc_chrdev_region(&first, 0, 3, "Achuz") < 0) {
        return -1;
    }
    printk(KERN_INFO " : <%d , %d>\n", MAJOR(first), MINOR(first));
    return 0;
}

static void __exit ofcd_exit(void)
{
    unregister_chrdev_region(first, 3);
    printk(KERN_INFO "Alvida: ofcd unregistered\n");
}

module_init(ofcd_init);
module_exit(ofcd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("vinos");
MODULE_DESCRIPTION("Our First Character Driver");
 


mynull.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>

#include <linux/types.h>  /* dev_t */
#include <linux/kdev_t.h> /* MAJOR, MINOR, MKDEV */
#include <linux/fs.h>     /* register_chrdev_region/alloc_chrdev_region */

#include <linux/device.h> /* class_create, device_create */
#include <linux/cdev.h>   /* cdev_add, cdev_init */

static dev_t first; /* device number */
static struct cdev c_dev; /* Character device structure */
static struct class *cl; /* device class */

static int my_open(struct inode *i, struct file *f)
{
    printk(KERN_INFO "Driver: open()\n");
    return 0;
}
static int my_close(struct inode *i, struct file *f)
{
    printk(KERN_INFO "Driver: close()\n");
    return 0;
}
static ssize_t my_read(struct file *f, char __user *buf,size_t len, loff_t *off)
{
    printk(KERN_INFO "Driver: read()\n");
    return 0;
}
static ssize_t my_write(struct file *f, const char __user *buf, size_t len, loff_t *off)
{
    printk(KERN_INFO "Driver: write()\n");
    return len;
}

static struct file_operations fops = {
    .owner = THIS_MODULE,
    .open = my_open,
    .release = my_close,
    .read = my_read,
    .write = my_write
};

static int __init null_init(void)
{
    printk(KERN_INFO "Namaskar: Null registered\n");
    if (alloc_chrdev_region(&first, 0, 1, "Achuz") < 0) {
        return -1;
    }
    if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL) {
        unregister_chrdev_region(first, 1);
        return -1;
    }
    if (device_create(cl, NULL, first, NULL, "mynull") == NULL) {
        class_destroy(cl);
        unregister_chrdev_region(first, 1);
        return -1;
    }
    cdev_init(&c_dev, &fops);
    if (cdev_add(&c_dev, first, 1) == -1) {
        device_destroy(cl, first);
        class_destroy(cl);
        unregister_chrdev_region(first, 1);
        return -1;
    }
    return 0;
}

static void __exit null_exit(void)
{
    cdev_del(&c_dev);
    device_destroy(cl, first);
    class_destroy(cl);
    unregister_chrdev_region(first, 1);
    printk(KERN_INFO "Alvida: Null unregistered\n");
}

module_init(null_init);
module_exit(null_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("vinos");
MODULE_DESCRIPTION("My Null Device Driver");