免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 863 | 回复: 0
打印 上一主题 下一主题

字符驱动小练习(key1*4) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-19 20:10 |只看该作者 |倒序浏览


//simple key1*4
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include
#include
#include  /* printk() */
#include  /* __init __exit */
#include  /* size_t */
#include  /* file_operation */
#include  /* Error number */
#include  /* udelay */
#include  /* copy_to_user, copy_from_user */
#include
#include
#include
#include
#include
#include
#include "gpio.h"
#define BUFFER_SIZE    256
#define DRIVER_NAME "gpf"
#ifdef DEBUG
#define PRINTK(fmt, arg...)  printk(KERN_NOTICE fmt, ##arg)
#else
#define PRINTK(fmt, arg...)
#endif
/*
KERN_EMERG  用于紧急事件,一般是系统崩溃前的提示信息
KERN_ALERT  用于需要立即采取动作的场合
KERN_CRIT  临界状态,通常设计验证的硬件或软件操作失败
KERN_ERR  用于报告错误状态.设备驱动程序通常会用它报告来自硬件的问题
KERN_WARNING 就可能出现的问题提出警告.这些问题通常不会对系统造成严重破坏
KERN_NOTICE  有必要提示的正常情况.许多安全相关的情况用这个级别汇报
KERN_INFO  提示性信息.有很多驱动程序在启动时用这个级别打印相关信息
KERN_DEBUG  用于调试的信息
*/
static int gpfDriver_Major = 0;  /* Driver Major Number */
//static unsigned char gpfDriver_inMask = 0x0;
static spinlock_t buffer_lock=SPIN_LOCK_UNLOCKED;
static struct kfifo *buffer;
struct timer_list mytimer;
struct semaphore bufferlock;
/* Driver Operation Functions */
//buffer = kfifo_alloc(BUFFER_SIZE,GFP_KERNEL,&buffer_lock);
void timerhandler(unsigned long data)
{
char data_temp;
data_temp=(char)data;
kfifo_put(buffer,&data_temp,sizeof(data_temp));
up(&bufferlock);
}
void handler(int irq,void *dev_id,struct pt_regs *regs)
{

  unsigned long key;
  key= (unsigned long)(irq+1);
  mytimer.expires = jiffies+2*HZ/100;
  mytimer.data = key;
  mytimer.function = timerhandler;
  add_timer(&mytimer);
}
static int gpfDriver_open(struct inode *inode, struct file *filp)
{
buffer = kfifo_alloc(BUFFER_SIZE,GFP_KERNEL,&buffer_lock);
MOD_INC_USE_COUNT;
PRINTK("gpfDriver open called!\n");
return 0;
}
static int gpfDriver_release(struct inode *inode, struct file *filp)
{
// int Minor = MINOR(inode->i_rdev);
MOD_DEC_USE_COUNT;
PRINTK("gpfDriver release called!\n");
return 0;
}
static ssize_t gpfDriver_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
unsigned int data;
char data_temp;
data_temp = (char)data;
down(&bufferlock);
kfifo_get(buffer,&data_temp,sizeof(data_temp));
PRINTK("in gpfDriver_read data=%d\n",data_temp);
copy_to_user(buf, &data_temp, sizeof(data_temp));
PRINTK("gpfDriver read called!\n");
return sizeof(data);
}
static struct file_operations gpfDriver_fops = {
owner: THIS_MODULE,
read:  gpfDriver_read,
open: gpfDriver_open,
release: gpfDriver_release,
};
/* Module Init & Exit function */
#ifdef CONFIG_DEVFS_FS
devfs_handle_t devfs_gpfDriver_dir;
devfs_handle_t devfs_gpfDriver_raw;
#endif
static int __init s3c2410_gpf_init(void)
{
/* Module init code */
PRINTK("s3c2410_gpf_init\n");
GPFUP =0X00 ;
/* Driver register */
gpfDriver_Major = register_chrdev(0, DRIVER_NAME, &gpfDriver_fops);//
if(gpfDriver_Major  0)
{
#ifdef CONFIG_DEVFS_FS
  devfs_unregister(devfs_gpfDriver_raw);
  devfs_unregister(devfs_gpfDriver_dir);
#endif
  unregister_chrdev(gpfDriver_Major, DRIVER_NAME);
}
free_irq(0,"KEY1");
free_irq(1,"KEY2");
free_irq(2,"KEY3");
free_irq(3,"KEY4");
kfifo_free(buffer);
return;
}
MODULE_AUTHOR("*****");
MODULE_LICENSE("Dual BSD/GPL");
module_init(s3c2410_gpf_init);
module_exit(s3c2410_gpf_exit);

//=======makefile
OUTPUT = gpfDriver.o
OUTPUT_DIR = output
KERNEL = /home/zhy/kernel_2.4.18
CROSSPREFIX = /usr/local/arm/2.95.3/bin/arm-linux-
CFLAGS = -Wall -I$(KERNEL)/include -c -DDEBUG
#CFLAGS += -DEXPORT_SYMTAB -DMODVERSIONS -include $(KERNEL)/include/linux/modversions.h
DEST = $(foreach fn, $(OUTPUT), $(OUTPUT_DIR)/$(fn))
ECHO_OK = echo -e "\x1b[40G\x1b[34m[   OK   ]\x1b[0m"
ECHO_FAILED = echo -e "\x1b[40G\x1b[31m[ FAILED ]\x1b[0m"
all: $(OUTPUT_DIR) $(OUTPUT_DIR)/$(OUTPUT)
$(OUTPUT_DIR):
@mkdir $@
@chmod 777 -R $@
$(OUTPUT_DIR)/%.o: %.c
@echo -n "Compling $^..."
@if $(CROSSPREFIX)gcc $(CFLAGS) $^ -o $@; then $(ECHO_OK); else $(ECHO_FAILED); fi
clean:
@find . \( -name '*.[oas]' -o -name install.sh \) -type f -print | xargs rm -f
@echo "Cleaned!"


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/97215/showart_2032196.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP