您的位置:首页 > 其它

led驱动_自动创建设备节点

2014-07-10 20:03 197 查看
原文链接: http://liu1227787871.blog.163.com/blog/static/20536319720128901736417/
上篇文章大概说明了驱动编写的格式, 但是我们每次都要去先insmod驱动程序, 然后cat /proc/devices 来查看主设备号, 然后 mknod 创建设备节点. 这一篇实现自动创建设备节点功能.

Q: struct class *class_create(struct module *owner, const char *name)

A: owner 表示类的所有者是谁, 一般都是 THIS_MODULE, 即当前模块. name表示类名

Q: class_device *class_device_create(struct class *cls, struct class_device *parent, dev_t devt, struct device *device, const char *fmt, ...);

A: 创建一个类设备, 并且用sysfs注册它. cls 表示所创建的设备所从属的类(一般为上边class_create返回的变量), 所创建设备的父设备没有就是NULL, devt为主/次设备号(MKDEV(major,minor)), device指向和本设备有关的结构设备(一般为NULL), fmt为设备名称,

Q: MKDEV(MAJOR, MINOR);

A: 获取主/次设备号

注意在exit时要取消注册 / 销毁类 / 取消内存映射

class_device_unregister();

class_destroy();

unioremap();

驱动代码:

本程序使用copy_from_user来获取应用层的数据, 通过该数据来打开/关闭 led

#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)
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核

<span style="white-space:pre">	</span>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"); // 卸载

<span style="white-space:pre">	</span>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");

驱动程序的makefile:

KERN_DIR = /home/cxh/arm/kernel/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


测试程序 test.c

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

int main(int argc, char **argv)
{
  int fd, val = 0;
  
  fd = open("dev/xyz", O_RDWR);
  if (fd < 0)
  {
    printf ("can't open /dev/xyz\n");
    return fd;
  }
  
  if (argc != 2)
  {
    printf ("usage: led <off | on>\n");
  }
  
  if (strcmp(argc[1], "on") == 0)
  {
    val = 1;
  }
  else
  {
    val = 0;
  }
  
  write(fd, &val, 4);
  
  return 0;
}


测试方法及现象:

将驱动make之后的 first.ko拷贝到 /usr/src/rootnfs/ 下, 键入 insmod, 将test.c 编译之后拷贝到 /usr/src/rootnfs/ 下执行. led on 或者 led off 可以打开或者关闭led
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: