tst-driver.h
#ifndef __TST_DRIVER_H__ #define __TST_DRIVER_H__
#define TSTDRV_MAGIC 0xd0
#define GPIO_IN 0 #define GPIO_OUT 1//_IO(TSTDRV_MAGIC, 1)
#define GPIO_SET_BIT 2//_IO(TSTDRV_MAGIC, 2)
#define GPIO_CLR_BIT 3//_IO(TSTDRV_MAGIC, 3)
#define TSTDRV_MAXNR 4
#endif //#ifndef __TST_DRIVER_H__
tst-driver.c
#ifndef __KERNEL__ #define __KERNEL__ #endif #ifndef MODULE #define MODULE #endif
#include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h> /* printk() */ #include <linux/init.h> /* __init __exit */
#include <linux/types.h> /* size_t */ #include <linux/fs.h> /* file_operation */ //#include /* Error number */
//#include /* udelay */
#include <asm/uaccess.h> /* copy_to_user, copy_from_user */ #include <asm/hardware.h> #include <asm-arm/arch-s3c2410/S3C2410.h> #include "tst-driver.h"
#define DRIVER_NAME "myDriver"
#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 用于调试的信息 */
#undef CONFIG_DEVFS_FS static int myDriver_Major = 0; /* Driver Major Number */ static unsigned char gpio_mask=0x0ff; /* 16bit, 1: out 0: in */ /* Driver Operation Functions */
// gpio_mask记录gpio口输入输出状态, static int myDriver_open(struct inode *inode, struct file *filp) { // int Minor = MINOR(inode->i_rdev);
// filp->private_data = 0;
MOD_INC_USE_COUNT; GPFCON=0x55555555; GPFUP=0x0000; GPFDAT=0xffff; PRINTK("myDriver open called!\n"); return 0; }
static int myDriver_release(struct inode *inode, struct file *filp) { // int Minor = MINOR(inode->i_rdev);
MOD_DEC_USE_COUNT; PRINTK("myDriver release called!\n"); return 0; }
static ssize_t myDriver_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { unsigned int gpio; PRINTK("myDriver read called!\n"); gpio=GPFDAT; gpio&=(~gpio_mask); copy_to_user(buf, &gpio,sizeof(gpio)); return 0; }
static ssize_t myDriver_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos) { char gpio; PRINTK("myDriver write called!\n"); copy_from_user(&gpio, buf,sizeof(buf)); gpio&=gpio_mask; GPFDAT=gpio; return 0; }
static int myDriver_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { int mask=0x01; int i=0; PRINTK("myDriver ioctl called(%d)!\n", cmd); switch(cmd) { case GPIO_IN: for(i=0;i<8;i++) { if( (arg&mask)!=0 ) { GPFCON&=~(3<<i*2);//2410gpiocon操作 对应的2位先置0 GPFUP=0x0; gpio_mask&=( ~(1<<i) ); } mask=(mask<<1); } mask=0x01; break; case GPIO_OUT: for(i=0;i<8;i++) { if( (arg&mask)!=0 ) { GPFCON&=~(3<<i*2););//2410gpiocon操作 对应的2位先置0 GPFCON|=(1<<i*2); GPFUP=0x0; gpio_mask|=(1<<i); } mask=(mask<<1); } mask=0x01; break; case GPIO_SET_BIT: if( ( gpio_mask& (1<<arg) )==0 ) { PRINTK(" bit %ld only read allow \n",arg); break; } else GPFDAT|= (1<<arg); break; case GPIO_CLR_BIT: if( ( gpio_mask& (1<<arg) )==0 ) { PRINTK(" bit %ld only read allow \n",arg); break; } else GPFDAT&=(~( 1<<arg)); break; } return 0; }
/* Driver Operation structure */ static struct file_operations myDriver_fops = { owner: THIS_MODULE, write: myDriver_write, read: myDriver_read, ioctl: myDriver_ioctl, open: myDriver_open, release: myDriver_release, };
/* Module Init & Exit function */ #ifdef CONFIG_DEVFS_FS devfs_handle_t devfs_myDriver_dir; devfs_handle_t devfs_myDriver_raw; #endif static int __init myModule_init(void) { /* Module init code */ PRINTK("myModule_init\n"); /* Driver register */ myDriver_Major = register_chrdev(0, DRIVER_NAME, &myDriver_fops); if(myDriver_Major < 0) { PRINTK("register char device fail!\n"); return myDriver_Major; } PRINTK("register myDriver OK! Major = %d\n", myDriver_Major); #ifdef CONFIG_DEVFS_FS devfs_myDriver_dir = devfs_mk_dir(NULL, "myDriver", NULL); devfs_myDriver_raw = devfs_register(devfs_myDriver_dir, "raw0", DEVFS_FL_DEFAULT, myDriver_Major, 0, S_IFCHR | S_IRUSR | S_IWUSR, &myDriver_fops, NULL); PRINTK("add dev file to devfs OK!\n"); #endif PRINTK("init GPF OK!\n"); GPFCON=0x55555555; GPFUP=0x0000; GPFDAT=0xffff; return 0; }
static void __exit myModule_exit(void) { /* Module exit code */ PRINTK("myModule_exit\n"); /* Driver unregister */ if(myDriver_Major > 0) { #ifdef CONFIG_DEVFS_FS devfs_unregister(devfs_myDriver_raw); devfs_unregister(devfs_myDriver_dir); #endif unregister_chrdev(myDriver_Major, DRIVER_NAME); } return; }
MODULE_AUTHOR("tastesweet@yeah.net"); MODULE_LICENSE("Dual BSD/GPL"); module_init(myModule_init); module_exit(myModule_exit);
|