关于字符设备驱动程序的一个样例

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>

#define HELLO_MAJOR 248
#define HELLO_MINOR 1

#define HELLO_SIZE 4096
#define DEVICE_NAME "hello"

static unsigned char hello_count = 0;

static int hello_major = HELLO_MAJOR;
static int hello_minor = HELLO_MINOR;

static struct cdev *pdev = NULL;

int hello_open(struct inode *inode, struct file *file)
{
	if(hello_count > 0)
		return -1;

	file->private_data = kmalloc(HELLO_SIZE, GFP_KERNEL);
	if(file->private_data == NULL)
		return -1;
	memset(file->private_data, 0, HELLO_SIZE);

	printk(KERN_INFO "hello_open succeed: %d: %d\n", MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
	hello_count ++;
	return 0;
}

int hello_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	switch(cmd)
	{
		case 0x01:
			printk(KERN_INFO "hello_ioctl succeed: %d\n", cmd);
			break;
		default:
			printk(KERN_ERR "hello_ioctl failed: %d\n", cmd);
			break;
	}

	return 0;
}

ssize_t hello_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
{
	if(length < HELLO_SIZE || copy_to_user(buffer, file->private_data, length) != 0)
	{
		printk(KERN_ERR "hello_read failed!\n");
		return -1;
	}

	printk(KERN_INFO "hello_read succeed\n");
	return 0;
}

ssize_t hello_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
{
	if(length > HELLO_SIZE || copy_from_user(file->private_data, buffer, length) != 0)
	{
		printk(KERN_ERR "hello_write failed!\n");
		return -1;
	}

	printk(KERN_INFO "hello_write succeed\n");
	return 0;
}

int hello_close(struct inode *inode, struct file *file)
{
	if(file->private_data)
		kfree(file->private_data);

	hello_count --;
	return 0;
}

static struct file_operations hello_fops = {
    .owner   =   THIS_MODULE,
    .open    =   hello_open,
    .ioctl   =   hello_ioctl,
    .read    =   hello_read,
    .write   =   hello_write,
    .release =   hello_close,
};

static int __init hello_init(void)
{
	int ret = 0;
	dev_t dev_num = MKDEV(hello_major, hello_minor);

	ret = register_chrdev_region(dev_num, 1, DEVICE_NAME);
	if(ret != 0)
	{
		ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
		if(ret != 0)
			return ret;
		
		hello_major = MAJOR(dev_num);
		hello_minor = MINOR(dev_num);
	}

	pdev = kmalloc(sizeof(struct cdev), GFP_KERNEL);
	if(pdev == NULL)
		goto ErrP;
	memset(pdev, 0, sizeof(struct cdev));

	cdev_init(pdev, &hello_fops);
	pdev->owner = THIS_MODULE;
	pdev->ops = &hello_fops;

	printk(KERN_INFO "Hello, World\n");
	return cdev_add(pdev, dev_num, 1);
ErrP:
	unregister_chrdev_region(dev_num, 1);
	printk(KERN_ERR "hello_init failed!\n");
	return -1;
}

static void __exit hello_exit(void)
{
	if(pdev)
	{
		cdev_del(pdev);
		kfree(pdev);
	}

	unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);
	printk(KERN_INFO "Goodbye, World\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("ljk@xdja.com");
MODULE_DESCRIPTION("LiJinKui");
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值