📄 keyboard.c
字号:
#ifndef __KERNEL__#define __KERNEL__#endif#ifndef MODULE#define MODULE#endif#include <linux/kernel.h>#include <linux/module.h>#include <asm/uaccess.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/ioport.h>#include <asm/io.h>#include <asm/8xx_immap.h>/** Header files for the charcater device */#include <linux/fs.h>#include <linux/wrapper.h>//#define DEBUG#define SUCCESS 0 /*function successfully return */#define DEVICE_NAME "keyboard" /*define lcd device name */#define LCD_MAJOR 189 /*define the lcd device major number *//**Set led control */#define LED_ADDR 0x8000001b#define LED_OFF 0xff#define LED_ON 0x80#define KEYBOARD_BASE 0x80000000#define KEYBOARD_ROW_ADDR 0x8000001d#define KEYBOARD_COL_ADDR 0x80000017/**Define bit-swap operations*/#define S_B7(x) (((x) & 0x80)>>7)#define S_B6(x) (((x) & 0x40)>>5)#define S_B5(x) (((x) & 0x20)>>3)#define S_B4(x) (((x) & 0x10)>>1)#define S_B3(x) (((x) & 0x08)<<1)#define S_B2(x) (((x) & 0x04)<<3)#define S_B1(x) (((x) & 0x02)<<5)#define S_B0(x) (((x) & 0x01)<<7)#define BIT_SWAP(x) (S_B7(x) | S_B6(x)|S_B5(x) | S_B4(x)| S_B3(x) | S_B2(x)| S_B1(x)| S_B0(x))static int device_open_num = 0; /*record numbers oflcd device opening by usr */static int Major; /*the lcd device major number *//**Standard device_driver function prototypes declaratioins*/static int device_open (struct inode *, struct file *);static int device_release (struct inode *, struct file *);static ssize_t device_read (struct file *, const char *, size_t, loff_t *);/**Standard moudle functions prototypes declarations*/int init_module (void);void cleanup_module (void);struct file_operations lcd_ops = {read:device_read,open:device_open,release:device_release,};/**Initialize the driver -Register the character device*/static intkeyboard_init (void){ int ret; // init_mem (); /* *Register the character device -lcd */ ret = register_chrdev (keyboard_MAJOR, DEVICE_NAME, &lcd_ops); /* register lcd device faild */ if (ret < 0) { iounmap ((void *) virt_base); printk ("Lcd init_module register failed with %d\n major number\n", Major); return Major; } /*register successed */ Major = LCD_MAJOR; printk ("Lcd register major number :%d successed\n", Major); /* *Lcd function initial */ writeb (LED_OFF, LED_ADDR); /*turn off led */ return SUCCESS;}}/*******************************************************Standard driver operation functions*************************************************************//**Called whenever a process attempt to open the lcd device*/static intdevice_open (struct inode *inode, struct file *lcd_device_file){#ifdef DEBUG // printk (DEVICE_NAME "%d.%d open", inode->i_rdev >> 8, inode->iredv & 0xff);#endif if (device_open_num) /*ensure open lcd device exclusively */ { return -EBUSY; /*lcd device has been open by other process */ } /* *Make sure that this module isn't removed while *the lcd is open by increasing the lcd usage count */ if (check_region (LCD_BASE, 0x1ff)) { printk ("%s memory already in use\n", DEVICE_NAME); return -EBUSY; } device_open_num++; MOD_INC_USE_COUNT; lcd_set (DISPLAY_CLEAR); lcd_set (INPUT_MODE_SET); return SUCCESS;}/**Called whenever a process closes the lcd device*/static intdevice_release (struct inode *inode, struct file *lcd_device_file){#ifdef DEBUG // printk (DEVICE_NAME "%d.%d closed", inode->i_rdev >> 8, // inode->iredv & 0xff);#endif /* *Make sure that this module isn't removed while *the lcd is open by increasing the lcd usage count */ device_open_num--; MOD_DEC_USE_COUNT; return SUCCESS;}/**The Lcd device driver write function*/static ssize_tdevice_write (struct file *lcd_device_file, const char *buffer, /*buffer for data outputed to lcd */ size_t length, /*length of buffer */ loff_t * offset) /*file pointer offset */{ return SUCCESS;}/*************************************** Linux Module functions********************************************/#ifdef MOUDLE/**Initialize the lcd device driver module*/intinit_module (void){ return keyboard_init ();}/**Cleanup the lcd device driver moudle*/voidcleanup_module (){ int ret;#ifdef DEBUG printk ("I realse myself from kernel\n");#endif /*unmap memory*/ iounmap ((void *) virt_base); /* Unregister the device */ ret = unregister_chrdev (Major, DEVICE_NAME); if (ret < 0) { printk ("unregister char_dev :error%d\n", ret); } lcd_set(DISPLAY_OFF); lcd_set (DISPLAY_CLEAR); writeb (LED_ON, LED_ADDR); /*turn on led */}#endif /*MODULE*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -