#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 <arch\arm\mach-s3c2410\include\mach\regs-gpio.h>
//#include <asm/hardware.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/gpio.h>
#include <mach/hardware.h>
#include <linux/device.h>
static struct class *firstdrv_class;
static struct device *firstdrv_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<<(7*2)) | (0x3<<(5*2)) |(0x3<<(6*2)) );
*gpfcon |= ((0x1<<(7*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;
copy_from_user(&val, buf, count);//用户控件向内核空间传递数据
if (val == 1)
{
*gpfdat |=(1<<7) | (1<<5) | (1<<6);//电灯
}
else
{
*gpfdat &=~(1<<7) | (1<<5) | (1<<6);//灭灯
}
//printk("first_drv_write\n");
return 0;
}
static struct file_operations first_drv_fops =
{
.owner = THIS_MODULE,
.open = first_drv_open,
.write = first_drv_write,
};
int major;
int first_drv_init(void) //驱动的入口函数
{
major = register_chrdev(0,"first_drv", &first_drv_fops);//注册 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");//先创建一个类
//if (IS_ERR(firstdrv_class))
// return PTR_ERR(firstdrv_class);
firstdrv_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz");//在这个类下面在创建一个设备
//if (unlikely(IS_ERR(firstdrv_dev)))
// return PTR_ERR(firstdrv_dev);
gpfcon = (volatile unsigned long *)ioremap(0x56000010, 16);
gpfdat = gpfcon + 1;
return 0;
}
void first_drv_exit(void) //驱动的入口函数
{
unregister_chrdev(major, "first_drv");
device_unregister(firstdrv_dev);
class_destroy(firstdrv_class);
iounmap(gpfcon);
}
module_init(first_drv_init);//修饰
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
用得是mini2440的开发板
结果是创建了设备节点 但是
# ls -l dev/xyz
brw-rw---- 1 0 0 251, 0 Jan 1 09:30 dev/xyz
这样我的测试程序就无法打开设备了 就还是只能手动mknod来创建了 谁知道这个是为什么