?? chardrv.c
字號:
/*****************************************************************************
版 權:網域時代
部 門:研發部
創建日期:2007年12月25日
簡 述:簡單字符型驅動程序,測試gpio
文 件 名:charDrv.c
創 建 者:劉明
******************************************************************************/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <asm/io.h>
#define BUFFER_MAX (10)
#define LED_ENABLE (0)
#define LED_DISABLE (1)
#define OK (0)
#define ERROR (-1)
#define GPBCON (0x56000010)
#define GPBDAT (0x56000014)
#define GPBUP (0x56000018)
#define GPB0 (0x1)
#define GPSIZE (0x4)
struct cdev *gDev;
struct file_operations *gFile;
dev_t devNum;
unsigned int subDevNum = 1;
int reg_major = 237;
int reg_minor = 0;
unsigned long led_time = 1;/* 閃燈的時間間隔,最小為1秒(默認) */
int testOpen(struct inode *p, struct file *f)
{
volatile unsigned long *tmp;
tmp = (volatile unsigned long *)ioremap(GPBUP,GPSIZE);
*tmp |= GPB0;
tmp = (volatile unsigned long *)ioremap(GPBCON,GPSIZE);
*tmp |= GPB0;
printk(KERN_EMERG"testOpen\r\n");
return OK;
}
int testWrite(struct file *f, const char __user *u, size_t s, loff_t *l)
{ volatile unsigned long *tmp;
tmp = (volatile unsigned long *)ioremap(GPBDAT,GPSIZE);
*tmp |= GPB0;
return OK;
}
int testRead(struct file *f, char __user *u, size_t s, loff_t *l)
{ volatile unsigned long *tmp;
tmp = (volatile unsigned long *)ioremap(GPBDAT,GPSIZE);
*tmp &= ~GPB0;
return OK;
}
int charDrvInit(void)
{
devNum = MKDEV(reg_major, reg_minor);
printk(KERN_EMERG"devNum is 0x%x\r\n", devNum);
if(OK == register_chrdev_region(devNum, subDevNum, "testchar"))
{
printk(KERN_EMERG"register_chrdev_region ok\r\n");
}
else
{
printk(KERN_EMERG"register_chrdev_region error\r\n");
return ERROR;
}
gDev = kzalloc(sizeof(struct cdev), 0);
gFile = kzalloc(sizeof(struct file_operations), 0);
gFile->open = testOpen;
gFile->read = testRead;
gFile->write = testWrite;
gDev->owner = THIS_MODULE;
cdev_init(gDev, gFile);
cdev_add(gDev, devNum, 3);
return OK;
}
void __exit charDrvExit(void)
{
unregister_chrdev_region(devNum, 1);
return;
}
module_init(charDrvInit);
module_exit(charDrvExit);
MODULE_LICENSE("GPL");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -