#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#define CALL_DEV_NAME "call_dev"
#define CALL_DEV_MAJOR 240
int call_open (struct inode *inode, struct file *filp)
{
int num = MINOR (inode->i_rdev);
printk ("call open -> minor : %d\n", num);
return 0;
}
loff_t call_llseek (struct file *filp, loff_t off, int whence)
{
printk ("call llseek -> off : %08X, whence : %08X\n", off, whence);
return 0x23;
}
ssize_t call_read (struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
printk ("call read -> buf : %08X, count : %08X\n", buf ,count);
return 0x33;
}
ssize_t call_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
printk ("call write -> buf : %08X, count : %08X\n", buf, count);
return 0x43;
}
int call_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned int arg)
{
printk ("call ioctl -> cmd : %08X, arg : %08X\n", cmd, arg);
return 0x53;
}
int call_release (struct inode *inode, struct file *filp)
{
printk ("call release \n");
return 0;
}
struct file_operations call_fops = {
.owner = THIS_MODULE,
.llseek = call_llseek,
.read = call_read,
.write = call_write,
.ioctl = call_ioctl,
.open = call_open,
.release = call_release,
};
int call_init (void)
{
int result;
printk ("call call_init \n");
result = register_chrdev (CALL_DEV_MAJOR, CALL_DEV_NAME, &call_fops);
if (result < 0)
return result;
return 0;
}
void call_exit (void)
{
printk ("call call_exit \n");
unregister_chrdev (CALL_DEV_MAJOR, CALL_DEV_NAME);
}
module_init (call_init);
module_exit (call_exit);
MODULE_LICENSE ("Dual BSD/GPL");
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define DEVICE_FILENAME "/dev/call_dev"
int main()
{
int dev;
char buff [128];
int ret;
printf ("1) device file open\n");
dev = open (DEVICE_FILENAME, O_RDWR | O_NDELAY);
if (dev < 0) {
perror ("errno");
exit(1);
}
if (dev >= 0) {
printf ("2) seek function call\n");
ret = lseek (dev, 0x20, SEEK_SET);
printf ("ret = %08X\n", ret);
printf ("3) read function call \n");
ret = read (dev, 0x30, 0x31);
printf ("ret = %08X\n", ret);
printf ("4) write function call\n");
ret = write (dev, 0x40, 0x41);
printf ("ret = %08X\n", ret);
printf ("5) ioctl function call\n");
ret = ioctl (dev, 0x51, 0x52);
printf ("ret = %08X\n", ret);
printf ("6) device file close\n");
ret = close (dev);
printf ("ret = %08X\n", ret);
}
return 0;
}
obj-m := call_dev.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.ko
rm -rf *.mod.*
rm -rf .*.cmd
rm -rf *.o
beyes@linux-beyes:~/Drivers/my> make
make -C /lib/modules/2.6.27.29-0.1-pae/build SUBDIRS=/home/beyes/Drivers/my modules
make[1]: Entering directory `/usr/src/linux-2.6.27.29-0.1-obj/i386/pae'
make -C ../../../linux-2.6.27.29-0.1 O=/usr/src/linux-2.6.27.29-0.1-obj/i386/pae/. modules
CC [M] /home/beyes/Drivers/my/call_dev.o
/home/beyes/Drivers/my/call_dev.c: In function ‘call_llseek’:
/home/beyes/Drivers/my/call_dev.c:21: warning: format ‘%08X’ expects type ‘unsigned int’, but argument 2 has type ‘loff_t’
/home/beyes/Drivers/my/call_dev.c: In function ‘call_read’:
/home/beyes/Drivers/my/call_dev.c:27: warning: format ‘%08X’ expects type ‘unsigned int’, but argument 2 has type ‘char *’
/home/beyes/Drivers/my/call_dev.c: In function ‘call_write’:
/home/beyes/Drivers/my/call_dev.c:33: warning: format ‘%08X’ expects type ‘unsigned int’, but argument 2 has type ‘const char *’
/home/beyes/Drivers/my/call_dev.c: At top level:
/home/beyes/Drivers/my/call_dev.c:54: warning: initialization from incompatible pointer type
Building modules, stage 2.
MODPOST 1 modules
CC /home/beyes/Drivers/my/call_dev.mod.o
LD [M] /home/beyes/Drivers/my/call_dev.ko
make[1]: Leaving directory `/usr/src/linux-2.6.27.29-0.1-obj/i386/pae'
beyes@linux-beyes:~/Drivers/my> sudo mknod /dev/call_dev c 240 32
sudo /sbin/insmod call_dev.ko
beyes@linux-beyes:~/Drivers/my> sudo ./call_app.exe
1) device file open
2) seek function call
ret = 00000023
3) read function call
ret = 00000033
4) write function call
ret = 00000043
5) ioctl function call
ret = 00000053
6) device file close
ret = 00000000
call call_exit
call call_init
call open -> minor : 32
call llseek -> off : 00000020, whence : 00000000
call read -> buf : 00000030, count : 00000031
call write -> buf : 00000040, count : 00000041
call ioctl -> cmd : 00000051, arg : 00000052
call release
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |