Wednesday, 2 September 2015

Char Device Driver

int register_chrdev_region(dev_t first, unsigned int count, char *name);
return 0 if suceeds    int MKDEV((250,0),4,"ANIL");

int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int cnt, char *name);

Unregistering the Device Driver

void unregister_chrdev_region(dev_t first,unsigned int count);
Header <linux/fs.h>


root@beaglebone:~# mknod chandrashekhar c 250 0

cat chandrashekhar
root@beaglebone:~# cat chandrashekhar
cat: chandrashekhar: No such device

Layer called VFS
register with VFS driver with device file with Major Minor Range

I am a driver who service 250,0 .... 250,3

open is a system call passes through VFS and to Device Driver
read is a system call

7 tyes of file tyes in Linux


        Major number and minor number

prw-r----- 1 root adm            0 Apr 23 22:22 xconsole
crw-rw-rw- 1 root root      1,   5 Apr 23 21:22 zero
lrwxrwxrwx 1 root root           8 Apr 23 21:22 shm -> /run/shm
drwxr-xr-x 2 root root          60 Jan  1  1970 snd
crw-r--r-- 1 root root  250, 0 Apr 23 22:48 chandrashekhar


d for Directory
c for character driver
s for socket driver
l for link to a file
b for block device file
p for proc window

root@beaglebone:~# cat /proc/modules
usb_f_acm 5166 1 - Live 0xbf033000
u_serial 9640 1 usb_f_acm, Live 0xbf02c000
g_multi 32469 0 - Live 0xbf01e000
u_rndis 8723 1 g_multi, Live 0xbf017000
u_ether 12283 1 g_multi, Live 0xbf010000
libcomposite 34172 2 usb_f_acm,g_multi, Live 0xbf000000



root@beaglebone:~# cat /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  7 vcs
 10 misc
 13 input
 14 sound
 29 fb
 81 video4linux
 89 i2c
 90 mtd
116 alsa
128 ptm
136 pts
153 spi
180 usb
188 ttyUSB
189 usb_device
212 DVB
226 drm
244 ttyGS
245 hidraw
246 ttySDIO
247 usbmon
248 uio
249 ttyO
250 bsg
251 iio
252 watchdog
253 media
254 rtc

Block devices:
  1 ramdisk
259 blkext
  7 loop
  8 sd
 31 mtdblock
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc




Makefile
 # If called directly from the command line, invoke the kernel build system.
ifeq ($(KERNELRELEASE),)

    KERNEL_SOURCE := ../../Kernel/linux-3.12.9
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) modules

clean:
    $(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) clean

# Otherwise KERNELRELEASE is defined; we've been invoked from the
# kernel build system and can use its language.
else

    src-m := $(notdir $(wildcard ${SUBDIRS}/*.c))
    src-mod := $(notdir $(wildcard ${SUBDIRS}/*.mod.c))
    src-m := $(filter-out ${src-mod}, ${src-m})
    obj-m := $(src-m:.c=.o)

endif


 File.c

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>

static dev_t first; // Global variable for the first device number

static int __init ofcd_init(void) /* Constructor */
{
    int ret;

    printk(KERN_INFO "Namaskar: ofd registered");
    if ((ret = alloc_chrdev_region(&first, 0, 3, "Shweta")) < 0)
    {
        return ret;
    }
    printk(KERN_INFO "<Major, Minor>: <%d, %d>\n", MAJOR(first), MINOR(first));
    return 0;
}

static void __exit ofcd_exit(void) /* Destructor */
{
    unregister_chrdev_region(first, 3);
    printk(KERN_INFO "Alvida: ofd unregistered");
}

module_init(ofcd_init);
module_exit(ofcd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <anil@sysplay.in>");
MODULE_DESCRIPTION("Our First Character Driver");