字符设备驱动笔记(一)

1.调用关系
            应用程序:open         read         write    
应用层    -----------------------------------------
                              c库        (swi val)
=================================================    
内核:    system call interface    
            (在异常处理函数里面,根据发生中断的原因,调用
             不同的处理函数)
        -----------------------------------------
            sys_open sys_read sys_write
            (VFS Virtual File System)
        -----------------------------------------
            驱动: led_open         led_read     led_write    
        -----------------------------------------
           硬件: led     flash ....    
        
      
2.驱动程序框架:
1)写出led_on led_read
2)通知内核
  1>构造file_operations
          .open
          .write
          ...
  2>入口函数中:register_chrdev
                       unregister_chrdev
  3>first_drv_init(自定义的入口函数)
      first_drv_exit
  4>module_init修饰入口函数
      module_exit
 
3.    
用户空间
        APP:    open("/dev/xxx")        read        write
                    属性:c(设备类型),111(主设备号)
          -----------------------------------------------------
                          C库
==============================================================
内核                    system call interface
        ------------------------------------------------------
                   VFS                  major作为索引
                ————————————————————————
           | 数组        |    |    |...    |file_operations|       |
                ————————————————————————
        ------------------------------------------------------
                 led_open                led_read    led_write
        ------------------------------------------------------
 
VFS根据打开的文件的属性:设备类型、主设备号,这两个属性就能找到
register_chrdev函数注册进内核的file_operations结构体

4.Makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

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

obj-m    += first_drv.o

5.字符设备驱动程序:

#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\n");
    /* 配置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\n");

    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)
{
   //0系统自动分配主设备号 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 */   
   //0x56000050为GPFCON的物理地址 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");

6.测试程序:

#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!\n");
    }
    if (argc != 2)
    {
        printf("Usage :\n");
        printf("%s <on|off>\n", argv[0]);
        return 0;
    }

    if (strcmp(argv[1], "on") == 0)
    {
        val  = 1;
    }
    else
    {
        val = 0;
    }
    
    write(fd, &val, 4);
    return 0;
}

 

7.运行

  1>cat /proc/devices
  //加载驱动
  2>insmod first_drv.ko
  //创建设备节点
  3>mknod /dev/xxx  c  111  0
  4>运行
 
  5>lsmod
  6>rmmod first_drv
  7>rm /dev/xxx


  8.  自动分配主设备号,自动创建设备
    1)驱动:①自动分配主设备号
               ②手工指定
    2)应用:
            open("/dev/xxx")
            1>mknod /dev/xxx c 主设备号     次设备号
            2>自动创建设备  udev  mdev(根据系统信息,创建设备节点) 
         class_create()
        class_device_create()
    3)查看系统信息
        cd /sys/class
        cd firstdrv
        cd xyz
        ls
    4)  
        /etc/init.d/rc.S
        echo /sbin/mdev > /proc/sys/kernel/hotplus

posted on 2013-09-19 23:21  屁屁侠  阅读(467)  评论(0编辑  收藏  举报

导航