1.写出leds_open,leds_write函数
2.1告诉内核这几个函数的存在?定义一个结构体file_operations
2.2把这个结构体告诉内核?用register_chrdev(major,name,file_operations)
    //将主设备号与file_operations结构一起向内核注册
    //major=register_chrdev(0,name,file_operations)表示major是由系统自动分配的;
3.谁来调用register_chrdev?有驱动的入口函数first_drv_init
4.怎么知道入口函数就是first_drv_init?使用module_init函数来修饰入口函数,\
    内核启动时,先寻找module_init这个结构体。eg:module_init(first_drv_init)
5.当然,也会存在注销相应函数的操作;
    eg:unregister_chrdev(major, "first_drv")
    module_exit(first_drv_exit)
6.仿照其他程序加一些必要的头文件
7.如果要使驱动程序可以支持自动创建/dev/xxx,则驱动程序中需支持mdev机制。
    7.1由mdev根据系统信息创建设备节点(sys/class/firstdev),需要定义两个结构体
        static struct class *firstdrv_class;
        static struct class_device    *firstdrv_class_dev;
    7.2在first_drv_init内加入以下两条代码
        firstdrv_class = class_create(THIS_MODULE, "firstdrv");
        firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
    同理,在first_drv_exit内加入以下两条代码
        class_device_unregister(firstdrv_class_dev);
        class_destroy(firstdrv_class);
/*猜测:将firstdrv放在firstdrv_class这个结构体里面,然后根据这个使用class_device_create创建设备节点*/
8.由于驱动程序不能直接操作物理地址,需要操作虚拟地址,则需要一个物理地址到虚拟地址的映射。
查看2440手册,得出相应的物理地址,然后使用iorema函数完成映射,结束时使用iounmap撤销;
eg:gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);//用volatile防止编译器优化,必须每一次都来检测。
        iounmap(gpfcon);
9.最后加上MODULE_LICENSE("GPL");//务必在ko驱动中追加此声明,否则insmod驱动时将不能与/proc/kallsyms中的符号正常连接
    可以modinfo xxx.ko查看其依赖的模块,可知其中licens依赖于GPL
10.修改makefile的最后一行为:obj-m    += first_drv.o
     且把first_drv放在makefile对应的文件位置,执行make,得到first_drv.ko文件,使用insmod、rmmod、lsmod、modinfo实现对其操作;
11.测试驱动程序
    arm-linux-gcc -o firstdrvtest firstdrvtest.c
    根据测试程序进行操作即可;
12.因为使用的是2.6.22.6的内核,要使用3.4.5的gcc版本来交叉编译,否则无法运行。

 #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 *ptFirstdrvClass;
static struct class_device *ptFirstdrvClassDev; volatile unsigned long *pulgpfcon = NULL;
volatile unsigned long *pulgpfdat = NULL; static int firstdrv_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/* 配置GPF4,5,6为输出 */
*pulgpfcon &= ~((0x3<<(*)) | (0x3<<(*)) | (0x3<<(*)));
*pulgpfcon |= ((0x1<<(*)) | (0x1<<(*)) | (0x1<<(*)));
return ;
} static ssize_t firstdrv_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 == )
{
// 点灯
*pulgpfdat &= ~((<<) | (<<) | (<<));
}
else
{
// 灭灯
*pulgpfdat |= (<<) | (<<) | (<<);
} return ;
} static struct file_operations firstdrv_ops ={
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = firstdrv_open,
.write = firstdrv_write, }; int g_iMajor;
static int firstdrv_init(void)
{
g_iMajor = register_chrdev(,"first_drv",&firstdrv_ops);
ptFirstdrvClass = class_create(THIS_MODULE, "firstdrv");
ptFirstdrvClassDev = class_device_create(ptFirstdrvClass, NULL, MKDEV(g_iMajor, ), NULL, "xyz"); /* /dev/xyz */ pulgpfcon = (volatile unsigned long *)ioremap(0x56000050, );
pulgpfdat = pulgpfcon + ; return ; } static int firstdrv_exit(void)
{
unregister_chrdev(g_iMajor, "first_drv");
class_device_unregister(ptFirstdrvClassDev);
class_destroy(ptFirstdrvClass); iounmap(pulgpfcon); return ; } module_init(firstdrv_init); module_exit(firstdrv_exit); MODULE_LICENSE("GPL");

最新文章

  1. Linux解压命令(tar)
  2. linux驱动学习之tasklet分析
  3. 用java实现Simsimi小黄鸡接口
  4. 设计模式--委托模式C++实现
  5. css3属性选择器总结
  6. windbg Symbol file path
  7. ABP core学习之二 IIS部署.NET CORE
  8. python安装提示No module named setuptools,wget提示ERROR 403: SSL is required
  9. spring整合mybatis在使用.properties文件时候遇到的问题
  10. 使用自定义端口连接SQL Server的方法(转载)
  11. 【 Gym 101116K 】Mixing Bowls(dfs)
  12. headfirst python 01~02
  13. Delphi下让窗口不显示在任务栏的另类方法
  14. QT 截取屏幕的实现
  15. python 的math模块
  16. vbs获取当前主机IP
  17. 【会装】kylin的安装(填坑)和简单使用
  18. multi-paradigm
  19. IOS7下,AVAudioRecorder需要注意的一点
  20. PDO中捕获SQL语句中的错误

热门文章

  1. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
  2. UWP开发之Mvvmlight实践八:为什么事件注销处理要写在OnNavigatingFrom中
  3. ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0
  4. bzoj1901--树状数组套主席树
  5. jQuery遮罩层登录对话框
  6. 深入理解DOM节点操作
  7. 【iOS10 SpeechRecognition】语音识别 现说现译的最佳实践
  8. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager
  9. 让 ASP.NET vNext 在 Mac OS 中飞呀飞。。。
  10. Linux学习日记(二)