📄 magic-key.c
字号:
/*****************************************Copyright (c)**************************************************** Guangzhou Zhiyuan Electronic Co.,LTD.** graduate school** http://www.zyinside.com****------------------------------------- File Info ------------------------------------------------------** File name: magic-key.c** Last modified Date: 2005-12-28** Last Version: 1.0** Descriptions: Driver for KEY1 on MagicARM2410.** Based on Linux 2.4.18. **------------------------------------------------------------------------------------------------------** Created by: Chenxibing** Created date: 2005-12-27** Version: 1.0** Descriptions: Preliminary version.****------------------------------------------------------------------------------------------------------** Modified by:** Modified date:** Version:** Descriptions:***********************************************************************************************************/#ifndef __KERNEL__ #define __KERNEL__#endif#ifndef MODULE #define MODULE#endif#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/poll.h>#include <linux/spinlock.h>#include <linux/irq.h>#include <linux/wait.h>//#include <linux/devfs_fs_kernel.h>//#include <linux/miscdevice.h>//#include <linux/delay.h>#include <asm/hardware.h>#define DEVICE_NAME "magic-key"#define KEY_MAJOR 231 // can be 231~239 or 240~254MODULE_LICENSE("Proprietary");MODULE_DESCRIPTION("Guangzhou Zhiyuan Electronic Co.,LTD.\ngraduate school\nhttp://www.zyinside.com");MODULE_SUPPORTED_DEVICE("Linux 2.4.18 & MagicARM2410");MODULE_AUTHOR("Chenxibing");/********************************************************************************************************** Information of KEY1.********************************************************************************************************/static struct key_info{ int irq_num; unsigned int gpio_port; int key_num;} key1[1] ={ { IRQ_EINT4, GPIO_F4, 1 },};static int key_value = 0;static int ready = 0;/********************************************************************************************************** Function name: key_irq()** Descriptions : key1's irq handler.** : for request_irq() and free_irq().** handler prototype:** : void (*handler)(int irq, void *dev_id, struct pt_regs*).** Input:** irq : irq number. ** dev_id : private data.** pt_regs : seldom use.** Output:** return : NONE.** Created by : Chenxibing** Created Date : 2005-12-27**-----------------------------------------------------------------------------------------------------** Modified by :** Modified Date: **-----------------------------------------------------------------------------------------------------********************************************************************************************************/static DECLARE_WAIT_QUEUE_HEAD(key_wait);static void key_irq_handle(int irq, void *dev_id, struct pt_regs *reg){ struct key_info *key; key = key1; key_value = key->key_num; ready = 1; wake_up_interruptible(&key_wait);}/********************************************************************************************************** Function name: magic_key_read()** Descriptions : read key1's value.** Input:** filp : pointer of file** buf : buf for save data** count : size of read f_ops : *p_ops = read point** Output:** return : read size** Created by : Chenxibing** Created Date : 2005-12-27**-----------------------------------------------------------------------------------------------------** Modified by :** Modified Date: **-----------------------------------------------------------------------------------------------------********************************************************************************************************/static ssize_t magic_key_read(struct file *filp, char * buf, size_t count, loff_t *f_pos){ static int value; wait_event_interruptible(key_wait, ready == 1);// interruptible_sleep_on(&key_wait); if(count != sizeof(key_value)) return -EINVAL; value = key_value; copy_to_user(buf, &value, sizeof(value)); ready = 0; return sizeof(value);}/********************************************************************************************************** Function name: magic_key_open()** Descriptions : open key** Input:** inode : information of device** filp : pointer of file** Output:** 0 : OK** other : not OK** Created by : Chenxibing** Created Date : 2005-12-27**-----------------------------------------------------------------------------------------------------** Modified by :** Modified Date: **-----------------------------------------------------------------------------------------------------********************************************************************************************************/static int magic_key_open(struct inode *inode, struct file *filp){ int result; struct key_info *key; ready = 0; key = key1; set_external_irq(key->irq_num, EXT_FALLING_EDGE, GPIO_PULLUP_DIS); result = request_irq(key->irq_num, key_irq_handle, SA_INTERRUPT, DEVICE_NAME, NULL); if(result) { printk(KERN_INFO DEVICE_NAME " Failed to request irq.\n"); return result; } MOD_INC_USE_COUNT; printk(KERN_INFO DEVICE_NAME ": opened.\n"); return 0;}/********************************************************************************************************** Function name: magic_key_release()** Descriptions : release KEY1** Input:** inode : information of device** filp : pointer of file** Output:** 0 : OK** other : not OK** Created by : Chenxibing** Created Date : 2005-12-27**-----------------------------------------------------------------------------------------------------** Modified by :** Modified Date: **-----------------------------------------------------------------------------------------------------********************************************************************************************************/static int magic_key_release(struct inode *inode, struct file *filp){ struct key_info *key; key = key1; free_irq(key->irq_num, NULL); MOD_DEC_USE_COUNT; printk(KERN_INFO DEVICE_NAME ": released.\n"); return 0;}/********************************************************************************************************** operations of the driver********************************************************************************************************/static struct file_operations magic_key_fops ={ owner: THIS_MODULE, read: magic_key_read, open: magic_key_open, release: magic_key_release,};/********************************************************************************************************** Function name: magic_key_init()** Descriptions : register driver** Input:** : NONE** Output:** 0 : OK** other : not OK** Created by : Chenxibing** Created Date : 2005-12-27**-----------------------------------------------------------------------------------------------------** Modified by :** Modified Date: **-----------------------------------------------------------------------------------------------------********************************************************************************************************/static devfs_handle_t devfs_handle;static int __init magic_key_init(void){// int result;// result = register_chrdev(KEY_MAJOR, DEVICE_NAME, &magic_key_fops);// if(result<0)// {// printk(KERN_INFO "Failed to register major: " DEVICE_NAME ".\n");// return result;// } devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, KEY_MAJOR, 0, S_IFCHR | S_IRUSR | S_IWUSR, &magic_key_fops, NULL); return 0;}/********************************************************************************************************** Function name: magic_key_exit()** Descriptions : unregister driver** Input:** : NONE** Output:** 0 : OK** other : not OK** Created by : Chenxibing** Created Date : 2005-12-27**-----------------------------------------------------------------------------------------------------** Modified by :** Modified Date: **-----------------------------------------------------------------------------------------------------********************************************************************************************************/static void __exit magic_key_exit(void){ devfs_unregister(devfs_handle);// unregister_chrdev(KEY_MAJOR, DEVICE_NAME);}module_init(magic_key_init);module_exit(magic_key_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -