第2课.字符设备驱动程序的开发

1.前言

Linux内核就是由各种驱动组成的
Linux操作系统将所有的设备都看成文件,以操作文件的方式访问设备。
应用程序不能直接操作硬件,而是通过使用统一的接口函数调用硬件驱动程序。

2.字符设备驱动程序中重要的数据结构和函数

重要的数据结构和函数

可以在glibc的fcntl.h、unistd.h、sys/ioctl.h等文件中看到(应用程序中使用的接口)

对于上述每个系统调用,驱动程序中国都有一个与之对应的函数对于字符设备驱动程序,这些函数集合在一个file_operations类型的数据结构中。file_operations结构在内核的include/linux/fs.h中定义

file_operations

struct file_operations {
	struct module *owner;
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	int (*readdir) (struct file *, void *, filldir_t);
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);
	int (*open) (struct inode *, struct file *);
	int (*flush) (struct file *, fl_owner_t id);
	int (*release) (struct inode *, struct file *);
	int (*fsync) (struct file *, loff_t, loff_t, int datasync);
	int (*aio_fsync) (struct kiocb *, int datasync);
	int (*fasync) (int, struct file *, int);
	int (*lock) (struct file *, int, struct file_lock *);
	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
	int (*check_flags)(int);
	int (*flock) (struct file *, int, struct file_lock *);
	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
	int (*setlease)(struct file *, long, struct file_lock **);
	long (*fallocate)(struct file *file, int mode, loff_t offset,
			  loff_t len);
};

当应用程序使用open函数打开某个设备时,设备驱动程序的file_operations结构中的open成员函数会被调用。从这个角度来说,编写字符设备驱动程序就是为硬件的file_operations结构编写各个函数。

copy_from_user()和copy_to_user()

static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
从用户空间得到数据
eg:
    write传入的参数需要被copy_from_user从用户空间取出后才能使用
static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
把数据传递给用户空间
eg:
    同上read读取的参数,需要copy_to_user从内核中传出后才能read到

ioremap和iounmap

linux驱动直接操作不了内核,需要使用虚拟地址去映射,使用函数

static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
phys_addr_t offset:物理地址
unsigned long size:虚拟地址

static inline void iounmap(void __iomem *addr)

module_init、module_exit和MODULE_LICENSE("GPL")

module_init、module_exit:指明驱动程序的初始化函数和卸载函数
MODULE_LICENSE("GPL"):遵循的协议

主次设备号

在PC上执行ls /dev/ttyS0 -l可以看到

crw-rw---- 1 root dialout 4, 64 3月  11 16:10 /dev/ttyS0
c表示/dev/ttyS0是一个字符设备。它的主设备号是4,次设备号是64。如果开头是b则为块设备

3.代码解析

Makefile

KERN_DIR = /work/system/linux-2.6.22.6
#KERN_DIR 内核的目录,需要先编译

all:
	make -C $(KERN_DIR) M=`pwd` modules 
#-C:进入KERN_DIR目录中去执行make

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= first_drv.o
#产生一个first_drv.o的模块

first_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

static struct class *firstdrv_class;
static struct class_device	*firstdrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;


static int first_drv_open(struct inode *inode, struct file *file)
{
	//printk("first_drv_open
");
	/* 配置GPF4,5,6为输出 */
	*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
	*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
	return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int val;

	//printk("first_drv_write
");
    //write传入的参数需要使用copy_from_user把其从用户空间中取到内核
	copy_from_user(&val, buf, count); //	copy_to_user();

	if (val == 1)
	{
		// 点灯
		*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
	}
	else
	{
		// 灭灯
		*gpfdat |= (1<<4) | (1<<5) | (1<<6);
	}
	
	return 0;
}

static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,     
	.write	=	first_drv_write,	   
};


int major;
static int first_drv_init(void)
{
	major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
    
    //创建设备文件
	firstdrv_class = class_create(THIS_MODULE, "firstdrv");

	firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

	return 0;
}

static void first_drv_exit(void)
{
	unregister_chrdev(major, "first_drv"); // 卸载

	class_device_unregister(firstdrv_class_dev);
	class_destroy(firstdrv_class);
	iounmap(gpfcon);
}

module_init(first_drv_init);
module_exit(first_drv_exit);


MODULE_LICENSE("GPL");

firstdrvtest.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
	int fd;
	int val = 1;
	fd = open("/dev/xyz", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!
");
	}
	if (argc != 2)
	{
		printf("Usage :
");
		printf("%s <on|off>
", argv[0]);
		return 0;
	}

	if (strcmp(argv[1], "on") == 0)
	{
		val  = 1;
	}
	else
	{
		val = 0;
	}
	
	write(fd, &val, 4);
	return 0;
}
原文地址:https://www.cnblogs.com/huangdengtao/p/12449999.html