📄 dsp.c
字号:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/modversions.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/param.h>
#include <linux/fs.h>
#include <linux/config.h>
#include <asm/delay.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <linux/malloc.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <asm/page.h>
#include <asm/mmu.h>
#include <linux/init.h>
#define DEVICE_NAME "/dev/dsp"
#define P_U32 unsigned long *
#define HPIC 0xD0000000
#define HPIDI 0xD0000004
#define HPIA 0xD0000008
#define HPID 0xD000000C
#define DEVICE_RESET 100
/*Control Register*/
#define HPIC1stWr 0 /*Write first byte */
#define HPIC2ndWr 1 /*Write second byte*/
#define HPIC1stRd 2 /*Read first byte*/
#define HPIC2ndRd 3 /*Read second byte*/
/*Data transfer with Post increment after read and preincrement before write.*/
#define HPI1stWr 4 /*Write first byte */
#define HPI2ndWr 5 /*Write second byte*/
#define HPI1stRd 6 /*Read first byte*/
#define HPI2ndRd 7 /*Read second byte*/
/*Address Register*/
#define HPIA1stWr 8 /*Write first byte */
#define HPIA2ndWr 9 /*Write second byte*/
#define HPIA1stRd 10 /*Read first byte*/
#define HPIA2ndRd 11 /*Read second byte*/
/*Data Latches*/
#define HPIDL1stWr 12 /*Write first byte */
#define HPIDL2ndWr 13 /*Write second byte*/
#define HPIDL1stRd 14 /*Read first byte*/
#define HPIDL2ndRd 15 /*Read second byte*/
static short Major;
static ssize_t aaa_read(struct file *file, char *buf,size_t len, loff_t *off)
{
int err;
char *kbuf;
int i;
kbuf=kmalloc(len,GFP_KERNEL);
if(kbuf==NULL)return -1;
for(i=0;i<len/2;i++){
kbuf[2*i]=*(unsigned char *)(0xD0000000+HPI1stRd);
kbuf[2*i+1]=*(unsigned char *)(0xD0000000+HPI2ndRd);
}
err=copy_to_user(&buf[0],kbuf,len);
kfree(kbuf);
if(err) return -1;
else return len;
}
static ssize_t aaa_write(struct file *file, const char *buf,size_t len, loff_t *off)
{
int err;
char *kbuf;
int i;
kbuf=kmalloc(len,GFP_KERNEL);
if(kbuf==NULL)return -1;
err=copy_from_user(kbuf,&buf[0],len);
if(err){
kfree(kbuf);
return -1;
}
for(i=0;i<len/2;i++){
*(unsigned char *)(0xD0000000+HPI1stWr)=kbuf[i*2];
*(unsigned char *)(0xD0000000+HPI2ndWr)=kbuf[i*2+1];
}
kfree(kbuf);
return len;
}
static int aaa_ioctl(struct inode *inodp,struct file *filp,unsigned int command,unsigned long arg)
{
int err;
unsigned long para;
err=copy_from_user((void *)¶,(void*)(arg),sizeof(para));
if(err){
printk("wrong\n");
return err;
}
if(command==DEVICE_RESET){
*(volatile P_U32)(0xf021c308) |=0x30000000; //set bits in OCR2_D
*(volatile P_U32)(0xf021c31c) &=0xBFFFFFFF; //write to bit of DR_D
*(volatile P_U32)(0xf021c300) |=0x40000000; //Set bit in DDIRD
udelay(70);
*(volatile P_U32)(0xf021c31c) |=0x40000000; //write to bit of DR_D
*(volatile P_U32)(0xf021c300) |=0x40000000; //Set bit in DDIRD
return 0;
}
if(command>=0 && command<16){
switch(command % 4){
case 0:
case 1:
*(unsigned char *)(0xD0000000+command)=para;
break;
case 2:
case 3:
para=*(unsigned char *)(0xD0000000+command);
break;
}
}
else return -1;
err=copy_to_user((void*)(arg),(void *)¶,sizeof(para));
if(err){
printk("error\n");
return err;
}
return 0;
}
static int aaa_open(struct inode * inodp,struct file* fp)
{
return 0;
}
static int aaa_release(struct inode * inodp,struct file *filp)
{
return 0;
}
/**************************KERNEL RELATIVE************************/
static struct file_operations aaa_fops ={
NULL,
NULL,//llseek
aaa_read,
aaa_write,
NULL,//readdir
NULL,//poll
aaa_ioctl,
NULL,//mmap,
aaa_open,
NULL,//flush,
aaa_release,
NULL,// fsync
NULL,//fasync
NULL,//lock
NULL,//readv
NULL,//writev
NULL,//sendpage
NULL//get_unmapped_area
};
int init_module(void)
{
Major=register_chrdev(0,DEVICE_NAME,&aaa_fops);
if(Major<0)
{
printk("<1>sorry,cannot open device %s,your device number is %d\n",DEVICE_NAME,Major);
return Major;
}
printk("<1>succed in allocating a device number,which is %d\n",Major);
return 0;
}
void cleanup_module(void)
{
int ret;
ret=unregister_chrdev(Major,DEVICE_NAME);
ret<0 ? printk("Error in unregistering %s\n",DEVICE_NAME):printk("Success in unregistering %s\n",DEVICE_NAME);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -