/*****************************Copyright (c)**************************
** FREE
**
**--------------File Info-----------------------------------------
** File Name: test.c
** Last modified Date: 2006-9-9
** Last Version: 1.0
** Descriptions: User Configurable File
**
**------------------------------------------------------------------------------------------------------
** Created By: ZLG CHENMINGJI
** Created date: 2006-9-9
** Version: 1.0
** Descriptions: First version
**
**------------------------------------------------------------------------------------------------------
** Modified by:MAMAJINCO
** Modified date:2006-9-9
** Version:1.0
** Descriptions:在此忠心感谢ZLG的模版 我的高质量编程意识起源于此
**
********************************************************************************************************/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include "key.h"
/********************************************************************************************************
function announce
********************************************************************************************************/
//以下是关键函数的声明
static int key_open(struct inode *inode, struct file *filp);
//打开设备时用的 linux把设备当作文件管理 设备最好在用的时候再打开 尽量不要提前
static int key_release(struct inode *inode, struct file *filp);
static int key_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
unsigned long param);
//控制函数 这里用于控制LED亮与灭
static int __init key_init(void);//注册时用的 注意 模块注册的越早越好
static void __exit key_cleanup(void);//卸载时用的
/*********************************************************************************************************
** "全局和静态变量在这里定义"
** global variables and static variables define here
********************************************************************************************************/
//static int major = LED_MAJOR_NR;
/********************************************************************************************************/
static struct file_operations key_fops = /* driver info */
{
owner: THIS_MODULE,
ioctl: key_ioctl,
open: key_open,
release: key_release,
};
/*********************************************************************************************************
** Function name: led_open
** Descriptions: open device
** Input:inode: information of device
** filp: pointer of file
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2006-9-9
**-------------------------------------------------------------------------------------------------------
** Modified by:mamajinco
** Modified Date: 2006-9-9
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static int key_open(struct inode *inode, struct file *filp)
{
//初始化放在OPEN里
outw(inw(S3C44B0X_PCONG)&0x00ff,S3C44B0X_PCONG); //GP4-GP7设置为输入
outb(inb(S3C44B0X_PUPG)&0x0f,S3C44B0X_PUPG); //高四为口上拉
MOD_INC_USE_COUNT;
return 0; /* success */
}
/*********************************************************************************************************
** Function name: led_release
** Descriptions: release device
** Input:inode: information of device
** filp: pointer of file
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2006-9-9
**-------------------------------------------------------------------------------------------------------
** Modified by: mamajinco
** Modified Date: 2006-9-9
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static int key_release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
return(0);
}
/*********************************************************************************************************
** Function name: led_ioctl
** Descriptions: IO control function
** Input:inode: information of device
** filp: pointer of file
** cmd: command
** arg: additive parameter
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2006-9-9
**-------------------------------------------------------------------------------------------------------
** Modified by: mamajinco
** Modified Date: 2006-9-9
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static int key_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
unsigned char temp;
switch(cmd)
{
case 0:
temp=inb(S3C44B0X_PDATG); //读出io端口的数据
put_user((temp&0xf0)>>4,(unsigned char*)arg);
break;
//case 1:
// break;
default:
return -1;
break;
}
return 0;
}
/*********************************************************************************************************
** Function name: led_init
** Descriptions: init driver
** Input:none
** Output 0: OK
** other: not OK
** Created by: Chenmingji
** Created Date: 2006-9-9
**-------------------------------------------------------------------------------------------------------
** Modified by: mamajinco
** Modified Date: 2006-9-9
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static int __init key_init(void)
{
int result;
result = register_chrdev(232,"key",&key_fops);
/*关键语句 用于注册
注意!这是传统的注册方法 在2.4以上的linux版本中 加入了devfs设备文件系统 使注册更容易 但为了与大部分资料相同 大家看的方便 这里仍然使用老方法*/
if (result < 0)
{
printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n", KEY_MAJOR_NR ); //printk用于向内核输出信息
return(result);
}
printk(KERN_INFO DEVICE_NAME ": init OK\n");
return(0);
}
/*********************************************************************************************************
** Function name: led_cleanup
** Descriptions: exit driver
** Input:none
** Output none
** Created by: Chenmingji
** Created Date: 2006-9-9
**-------------------------------------------------------------------------------------------------------
** Modified by: mamajinco
** Modified Date: 2006-9-9
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static void __exit key_cleanup(void)
{
unregister_chrdev(232, "key"); //与register_chrdev配对使用
printk(KERN_INFO DEVICE_NAME ": exit OK\n");
}
module_init(key_init);
module_exit(key_cleanup);
/*********************************************************************************************************
** End Of File
********************************************************************************************************/