mydriver.c
来自「在frv环境下开发板的led驱动」· C语言 代码 · 共 76 行
C
76 行
#define MODULE
#define __KERNEL__
#include<linux/kernel.h>
#include<linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <asm/smplock.h>
#include <linux/coda.h>
#include <linux/config.h>
#include <linux/sched.h>
#include <linux/devfs_fs_kernel.h>
#include <asm/ioctl.h>
#define FLED_ADDR (volatile unsigned short *)0x20000022
#define FSWR_ADDR (volatile unsigned short *)0x20000028
#define FPGA_SW_IOC_MAGIC 0xdc
#define FPGA_SWITCH_READ _IOR(FPGA_SW_IOC_MAGIC,0,unsigned short)
#define FPGA_LED _IOW(FPGA_SW_IOC_MAGIC,1,unsigned short)
#define MAJOR 240
#define DEVICE_NAME "keyled"
static int Device_Open=0;
static int keyled_open(struct inode *inode ,struct file *file)
{
printk("<1>keyled driver opened\n");
MOD_INC_USE_COUNT;
return 0;
}
static int keyled_release(struct inode *inode ,struct file *file)
{
MOD_DEC_USE_COUNT;
return 0;
}
static int keyled_ioctl(struct inode *inode,struct file *file,unsigned int cmd ,unsigned long arg)
{
unsigned short sw_val;
switch(cmd)
{
case FPGA_SWITCH_READ: sw_val=*((volatile unsigned short *)(FSWR_ADDR));
*(int*) arg=sw_val; __asm__ __volatile__("membar") ;
break;
case FPGA_LED: *((volatile unsigned short*)(FLED_ADDR))=arg; __asm__ __volatile__("membar");
break;
default: printk("No such command!\n");
return -EINVAL;
}
}
struct file_operations keyled_fops=
{
open: keyled_open,
release: keyled_release,
ioctl: keyled_ioctl,
};
static int keyled_init(void)
{
if(register_chrdev(MAJOR,DEVICE_NAME,&keyled_fops)>=0)
printk("<1>Key and Led OK\n");
return 0;
}
static void keyled_exit(void)
{
if(unregister_chrdev(MAJOR,DEVICE_NAME)>=0)
printk("<1>Programme Over\n");
}
module_init(keyled_init);
module_exit(keyled_exit);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?