📄 sw2410.c
字号:
//#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <linux/fs.h>
#include <linux/wrapper.h>
#define DRAM_MEM_MAJOR 103
#define DEVICE_NAME "io_sw"
#define SUCCESS 0
/*
#define LLSB(x) ((x)&0xff)
#define LNLSB(x) (((x)>>8) & 0xff)
#define LNMSB(x) (((x)>>16) & 0xff)
#define LMSB(x) (((x)>>24)&0xff)
#define LONGSWAP(x) ((LLSB(x)<<24) | (LNLSB(x) <<16) | (LNMSB(x) <<8)|(LMSB(x)))
*/
static int Device_Open = 0;
int io_pb_init(void);
static int device_open(struct inode *,struct file *);
static int device_release(struct inode *,struct file *);
static ssize_t device_read(struct file *,char *,size_t,loff_t *);
static ssize_t device_write(struct file *,const char *,size_t,loff_t *);
static loff_t device_seek(struct file *,loff_t,int);
int init_module(void);
void cleanup_module(void);
struct file_operations io_pb_ops =
{
llseek: device_seek,
read: device_read,
write: device_write,
open: device_open,
release: device_release
};
static int Major;
unsigned long gpf_con,gpf_dat,gpf_up;
int io_pb_init(void)
{
gpf_up= (unsigned long)ioremap((unsigned long)0x56000058,4);
*(unsigned long *)gpf_up &= 0xffffffe1;
gpf_con = (unsigned long)ioremap((unsigned long)0x56000050,4);
*(unsigned long *)gpf_con &= 0xfffffc03;
gpf_dat= (unsigned long)ioremap((unsigned long)0x56000054,4);
Major = register_chrdev(DRAM_MEM_MAJOR,DEVICE_NAME,&io_pb_ops);
if(Major <0)
{
iounmap((void *)gpf_con);
iounmap((void *)gpf_dat);
iounmap((void *)gpf_up);
printk("DRAM_MEM init_module:failed with %d\n",Major);
return Major;
}
Major = DRAM_MEM_MAJOR;
printk("DRAM_MEM_MAJOR registred: Major = %d\n",Major);
printk("%#X\n",*(unsigned long *)gpf_dat);
return 0;
}
static int device_open(struct inode * inode,struct file *file)
{
if(Device_Open)
{
return -EBUSY;
}
Device_Open++;
MOD_INC_USE_COUNT;
return SUCCESS;
}
static int device_release(struct inode * inode,struct file *file)
{
Device_Open --;
MOD_DEC_USE_COUNT;
return 0;
}
static ssize_t device_read(struct file *file,
char * buffer,
size_t length,
loff_t * offset)
{
*buffer=((*(unsigned char *)gpf_dat)>>1)&0x0f;
return 0;
}
static ssize_t device_write(struct file *file,
const char * buffer,
size_t length,
loff_t * offset)
{
//volatile unsigned int intx;
unsigned long len;
//unsigned long * address = (unsigned long *)virt_addr0;
//unsigned long * address1 = (unsigned long *)virt_addr1;
len = 0;
return(len);
}
static loff_t device_seek(struct file *filp,loff_t off,int whence)
{
loff_t newpos;
switch(whence)
{
case 0:
newpos = off;
break;
case 1:
newpos = filp->f_pos +off;
break;
case 2:
return -EINVAL;
break;
default:
return -EINVAL;
}
if((unsigned long)newpos <0) return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
#ifdef MODULE
int init_module()
{
return io_pb_init();
}
void cleanup_module()
{
int ret;
iounmap((void *)gpf_con);
iounmap((void *)gpf_dat);
iounmap((void *)gpf_up);
ret = unregister_chrdev(Major,DEVICE_NAME);
if(ret < 0)
{
printk("unregister_chrdev:error %d\n",ret);
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -