Skip to content

Commit 63ae0a1

Browse files
committed
Subject: added another char driver(null-driver.c)
Description:
1 parent 84fe8c8 commit 63ae0a1

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

test_char_device/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Makefile for testCharDevice.c
2-
obj-m := testCharDevice.o
1+
# Makefile for charater devices
2+
obj-m += testCharDevice.o
3+
obj-m += null-driver.o
4+
obj-m += video-ram.o
35

46
KDIR=/usr/src/linux-headers-$(shell uname -r)
57

test_char_device/null-driver.c

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* null-driver.c */
2+
/*
3+
Usage:
4+
5+
make
6+
sudo insmod null-driver.ko
7+
echo "some-text" > /dev/mynull
8+
cat /dev/mynull
9+
sudo rmmod null-driver.ko
10+
*/
11+
#include <linux/module.h>
12+
#include <linux/version.h>
13+
#include <linux/kernel.h>
14+
#include <linux/types.h>
15+
#include <linux/kdev_t.h>
16+
#include <linux/fs.h>
17+
#include <linux/device.h>
18+
#include <linux/cdev.h>
19+
20+
static dev_t first; // Global variable for the first device number
21+
static struct cdev c_dev; // Global variable for the character device structure
22+
static struct class *cl; // Global variable for the device class
23+
static int my_open(struct inode *i, struct file *f)
24+
{
25+
printk(KERN_INFO "Driver: open()\n");
26+
return 0;
27+
}
28+
static int my_close(struct inode *i, struct file *f)
29+
{
30+
printk(KERN_INFO "Driver: close()\n");
31+
return 0;
32+
}
33+
static ssize_t my_read(struct file *f, char __user *buf, size_t
34+
len, loff_t *off)
35+
{
36+
printk(KERN_INFO "Driver: read()\n");
37+
return 0;
38+
}
39+
static ssize_t my_write(struct file *f, const char __user *buf,
40+
size_t len, loff_t *off)
41+
{
42+
printk(KERN_INFO "Driver: write()\n");
43+
return len;
44+
}
45+
static struct file_operations pugs_fops =
46+
{
47+
.owner = THIS_MODULE,
48+
.open = my_open,
49+
.release = my_close,
50+
.read = my_read,
51+
.write = my_write
52+
};
53+
54+
static int __init ofcd_init(void) /* Constructor */
55+
{
56+
printk(KERN_INFO "null-driver: ofcd registered");
57+
if (alloc_chrdev_region(&first, 0, 1, "Shweta") < 0)
58+
{
59+
return -1;
60+
}
61+
if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)
62+
{
63+
unregister_chrdev_region(first, 1);
64+
return -1;
65+
}
66+
if (device_create(cl, NULL, first, NULL, "mynull") == NULL)
67+
{
68+
class_destroy(cl);
69+
unregister_chrdev_region(first, 1);
70+
return -1;
71+
}
72+
cdev_init(&c_dev, &pugs_fops);
73+
if (cdev_add(&c_dev, first, 1) == -1)
74+
{
75+
device_destroy(cl, first);
76+
class_destroy(cl);
77+
unregister_chrdev_region(first, 1);
78+
return -1;
79+
}
80+
return 0;
81+
}
82+
83+
static void __exit ofcd_exit(void) /* Destructor */
84+
{
85+
cdev_del(&c_dev);
86+
device_destroy(cl, first);
87+
class_destroy(cl);
88+
unregister_chrdev_region(first, 1);
89+
printk(KERN_INFO "null-driver: ofcd unregistered");
90+
}
91+
92+
module_init(ofcd_init);
93+
module_exit(ofcd_exit);
94+
MODULE_LICENSE("GPL");
95+
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
96+
MODULE_DESCRIPTION("Our First Character Driver");

userapplication/userapp.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
Usage:
44
% make
5-
% ./userapp
5+
% sudo ./userapp
66
*/
77
#include<stdio.h>
88
#include<stdlib.h>
@@ -18,7 +18,7 @@ int main(int argc, char *argv[])
1818
fd = open(DEVICE, O_RDWR); /* open for read/write */
1919

2020
if(fd == -1){
21-
printf("file %s either doesnot exit, or locked by another process\n", DEVICE);
21+
printf("file %s either does not exit, or locked by another process, or you are not root!\n", DEVICE);
2222
exit(-1);
2323
}
2424
printf("r - read from device,\nw- write from device\nEnter command: ");

0 commit comments

Comments
 (0)