4412 杂项设备

一、杂项设备

为什么用杂项设备
杂项设备可以说是对一部分字符设备的封装,还有一部分不好归类驱动也归到杂项设备。杂项设备是字符设备的封装
为什么会引入杂项设备?
第一、可以节省主设备号
如果所有的驱动都是用字符设备,那么所有的设备号很快就用完了,总共就255个主设备号。
第二、驱动写起来相对简单
如果直接使用封装好的杂项设备,那么就可以减少一步注册主设备号的过程

杂项设备初始化部分源文件drivers/char/ misc.c,这一部分通过Makefile可知,是强制编译的。而且是Linux官方(不是三星官方)出来的时候就带的,为了一些简单的驱动更容易实现。
这部分了解即可,里面的内容也比较简单,就是给字符驱动做一个简单的封装。

 

在driver/char/目录下就有misc.c文件,而且此目录下的Makfile第三行就有,说明是强制编译的

obj-y               += misc.o

二、注册文件

杂项设备注册头文件
include/linux/miscdevice.h
结构体miscdevice以及注册函数如下所示

struct miscdevice  {
    int minor;
    const char *name;
    const struct file_operations *fops;
    struct list_head list;
    struct device *parent;
    struct device *this_device;
    const char *nodename;
    mode_t mode;
};

extern int misc_register(struct miscdevice * misc);
extern int misc_deregister(struct miscdevice *misc);

注册文件

 

常用的参数
.minor设备号      一般让系统分配
.name生成设备节点的名称
.fops指向一个设备节点文件

 

内核文件的结构体

Linux中一切皆文件,上层调用底层也是通过读取文件的方式
注册设备节点,本质也是新建一个特殊的文件,包含文件名,打开、关闭、操作等函数
包含文件结构体的头文件是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 *);
/* remove by cym 20130408 support for MT660.ko */
#if 0
//#ifdef CONFIG_SMM6260_MODEM
#if 1// liang, Pixtree also need to use ioctl interface...
    int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
#endif
/* end remove */
    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 *, 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);
file_operations

文件的结构体file_operations参数很多,根据需求选择。
必选的是参数是
.owner一般是THIS_MODULE,
.open打开文件函数
.release关闭文件函数
这里在必选之外使用参数(为了介绍接下来的GPIO的操作)
.unlocked_ioctlGPIO的操作,应用向底层驱动传值

驱动代码,在probe_linux_module基础上写devicenode_linux_module驱动

写代码的时候,注意一下函数调用顺序
编译,在开发板上加载驱动生成设备节点
/dev中查看是否生成了设备节点

#include <linux/module.h>
#include <linux/init.h>

/* device register header file, include device and driver struct
 * register and remove function */
#include <linux/platform_device.h>
/* register misc device header file */
#include <linux/miscdevice.h>
/* register deivce node file operations struct */
#include <linux/fs.h>

#define DRIVER_NAME "hello_ctl"
#define DEVICE_NAME "hello_ctl123"

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static int hello_open(struct inode *inode, struct file *file)
{
        printk(KERN_EMERG "hello open\n");
        return 0;
}

static int hello_release(struct inode *inode, struct file *file)
{
        printk(KERN_EMERG "hello release\n");
        return 0;
}

static long hello_ioctl(struct file *file, unsigned int cmd ,unsigned long arg)
{
        printk(KERN_EMERG "cmd is %u, arg is %lu\n", cmd, arg);
        return 0;
}

static struct file_operations  hello_ops = {
        .owner   = THIS_MODULE,
        .open    = hello_open,
        .release = hello_release,
        .unlocked_ioctl = hello_ioctl,
};

static struct miscdevice hello_dev = {
        .minor = MISC_DYNAMIC_MINOR,
        .name  = DEVICE_NAME,
        .fops  = &hello_ops,
};

static int hello_probe(struct platform_device *pdv)
{
        printk(KERN_EMERG "\tinitialized\n");
        misc_register(&hello_dev);
        return 0;
}

static int hello_remove(struct platform_device *pdv)
{
        printk(KERN_EMERG "\tremove\n");
        misc_deregister(&hello_dev);
        return 0;
}

static void hello_shutdown(struct platform_device *pdv)
{

}

static int hello_suspend(struct platform_device *pdv, pm_message_t state)
{
        return 0;
}

static int hello_resume(struct platform_device *pdv)
{
        return 0;
}

struct platform_driver hello_driver = {
        .probe    = hello_probe,
        .remove   = hello_remove,
        .shutdown = hello_shutdown,
        .suspend  = hello_suspend,
        .resume   = hello_resume,
        .driver = {
                .name  = DRIVER_NAME,
                .owner = THIS_MODULE,
        }
};

static int hello_init(void)
{
        int DriverState;

    printk(KERN_EMERG "Hello world enter!\n");
        DriverState = platform_driver_register(&hello_driver);

        printk(KERN_EMERG "\tDriverState is %d\n", DriverState);
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_EMERG "Hello world exit!\n");
        platform_driver_unregister(&hello_driver);
}

module_init(hello_init);
module_exit(hello_exit);
misc device

加载后显示:

[root@iTOP-4412]# insmod devicenode_linux_module.ko                                                                
[15524.506581] Hello world enter!
[15524.508471]  initialized
[15524.529368]  DriverState is 0

[root@iTOP-4412]# ls /dev/hello_ctl123 -l                                                                          
crw-rw----    1 root     0          10,  46 Aug  8 06:52 /dev/hello_ctl123

[root@iTOP-4412]# rmmod devicenode_linux_module                                                                    
[15533.155158] Hello world exit!
[15533.156749]  remove

 

三、编写简单应用调用驱动

调用HELLO_CTL123设备节点

头文件

打印头文件
include <stdio.h>调用打印函数printf
应用中调用文件需要的头文件
#include <sys/types.h>基本系统数据类型。系统的基本数据类型在 32 编译环境中保持为 32 位值,并会在 64 编译环境中增长为 64 位值。
#include <sys/stat.h>系统调用函数头文件。可以调用普通文件,目录,管道,socket,字符,块的属性
#include <fcntl.h>定义了open函数
#include <unistd.h>定义了close函数
#include <sys/ioctl.h>定义了ioctl函数

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

int main(void)
{
        int fd;
        char *hello_node = "/dev/hello_ctl123";

        /* O_RDWR只读打开, O_NDELAY非阻塞方式 */
        fd = open(hello_node, O_RDWR|O_NDELAY);
        if(fd < 0) {
                printf("APP open %s failed\n", hello_node);
                exit(EXIT_FAILURE);
        } else {
                printf("APP open %s success\n", hello_node);
                ioctl(fd, 1, 6);
        }

        close(fd);
}
invok_hello


调用的头文件是和编译器放在一起的
这里使用arm2009q3编译器,编译器使用arm-none-linux-gnueabi-gcc
在编译器目录下使用查找命令找到该头文件
例如#find ./ -name types.h
调用的函数
open函数是返回文件描述符
ioctl函数是应用向驱动传值
close函数是关闭打开的文件
编写应用程序的代码,编译

arm-none-linux-gnueabi-gcc -o invoke_hello invoke_hello.c -static

开发板中加载devicenode_linux_module驱动,运行应用

 测试结果:

[root@iTOP-4412]# ./invok_hello                                                                            
[ 2550.203458] hello open
[ 2550.204809] cmd is 1, arg is 6
[ 2550.207503] hello release
APP open /dev/hello_ctl123 success
[root@iTOP-4412]

 

posted @ 2018-08-08 14:06  习惯就好233  阅读(531)  评论(0编辑  收藏  举报