⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver488.c

📁 linux环境arm2440上的dma驱动程序开发源代码。
💻 C
字号:
#include <linux/autoconf.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
#include <asm/smplock.h>
#include <asm/arch/hardware.h>
#include <asm/arch/bitfield.h>
#include <asm/io.h>
                                                                                                                           
#define  driver488_name  "driver488"
#define  DRIVER488_MAJOR  89

static int write_488(struct  file *filp, char *buf, size_t count);
static int read_488(struct  file *filp, char *buf, size_t count);
static int open_488(struct inode *inode,struct file *file);
static int close_488(struct inode *inode,struct file *file);
static void delay(unsigned int x);

static struct file_operations fops_488 = {
        owner:          THIS_MODULE,
        open:           open_488,
        read:           read_488,
        write:          write_488,
        release:        close_488,
};
static struct driver488
        {
        	unsigned char data[300]; 
        	unsigned long addr[300];     
        	unsigned int len;
	};   
                           
static int write_488(struct file *filp, char *buf, size_t count)
{
	unsigned int i=0;
	unsigned long temp1=0;
	unsigned long temp2=0;	
	unsigned long *addr[count];
	struct driver488 *tmp;

	tmp = kmalloc(sizeof(struct driver488),GFP_KERNEL);	
	copy_from_user(tmp,buf,count);

	
//	for(i=0;i<tmp->len;i++)
//	{
//		printk("<1>**********************%x\n",tmp->addr[i]);
//		printk("<1>**********************%x\n",tmp->data[i]);
//	}

	for(i=0;i<tmp->len;i++)
	{
		addr[i]=(unsigned long)ioremap(tmp->addr[i],4);
	}
	
	for(i=0;i<tmp->len;i++)
	{
		outb(tmp->data[i],addr[i]);
		delay(5);
	}			
			
	for(i=0;i<tmp->len;i++)
	{
		iounmap((void *)addr[i]);
	}
	kfree(tmp);
	return 0;	
}

static int open_488(struct inode *inode,struct file *file)
{     
	printk("driver(open_488)    *********open_488********\n");
	return 0;   
}

static int read_488(struct  file *filp, char *buf, size_t count)
{  
	unsigned int i=0;
	unsigned long temp1=0;
	unsigned long temp2=0;	
	unsigned long *addr[count];
	struct driver488 *tmp;
	
	tmp = kmalloc(sizeof(struct driver488),GFP_KERNEL);	
	copy_from_user(tmp,buf,count);
	
	for(i=0;i<tmp->len;i++)
	{
		addr[i]=(unsigned long)ioremap(tmp->addr[i],4);
	}
	
	for(i=0;i<tmp->len;i++)
	{
		tmp->data[i]=inb(addr[i]);
		delay(5);
	}			

	copy_to_user(buf,tmp,count);
			
	for(i=0;i<tmp->len;i++)
	{
		iounmap((void *)addr[i]);
	}
	kfree(tmp);
	return 0;	
}
////////////////////delay 100ns////////////////
void delay(unsigned int x)
{
	unsigned int y;
	unsigned int z;
	for(y=0;y<x;y++)
	{
		for(z=0;z<2;z++)
		{
			z++;
			z--;
		}
	}
}

int init_488(void)
{
        int retv;
        retv = register_chrdev(89,driver488,&fops_488);
        if(retv < 0)
        {
                printk("<1> AC488 register fail!\n");
                return retv;
        }

        printk("driver(init_488)    init_488 success!\n");
        return  0;
}

static int close_488(struct inode *inode,struct file *file)
{
	printk("<0>driver(close_488)    close_488 success!\n");
	return 0;
}

static int cleanup_488(void)
{
        int retv;
        retv = unregister_chrdev(89,driver488);
        if(retv < 0)
        {
                printk("<1>Unregister AC488 Fail\n");
        }
        else
        printk("<1>AC488 drive removed!\n");
        printk("<1>driver(cleanup_488)    cleanup_488 success!\n");
	return 0;
}

__initcall(init_488);

#ifdef MODULE
module_init(init_488);
module_exit(cleanup_488);
MODULE_LICENSE("Dual MPL/GPL");
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -