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

📄 phyaddr_rw.c

📁 gpio+physaddr驱动例子
💻 C
字号:
/* driver/char/phyaddr_drv.c 
 * 
 * This file provide IO reading and writing from user space.
 * Pls create some device file in diretory /dev/ppcflash whose major is 218.
 * This driver support 0-255 devices. In user space user can read and write
 * IO through open and ioctl function provided by glibc.
 * 
 * Any problem pls contact support@hhcn.com
 */
#include <linux/fs.h>
#include <linux/iobuf.h>
#include <linux/major.h>
#include <linux/blkdev.h>
#include <linux/capability.h>
#include <linux/smp_lock.h>
#include <asm/uaccess.h>
#include <asm/hardware.h>
#include <asm/arch/cpu_s3c2410.h>
#include <asm/io.h>
#include <linux/vmalloc.h>
#include <linux/module.h> //wpq insmod phyaddr

#define PHYADDR_MAJOR 226
#define PHYADDR_ADDR 0x08000300

typedef char phyaddr_device_t;
devfs_handle_t phyaddr_devfs_dir;

static phyaddr_device_t phyaddr_devices[256];

int	phyaddr_open(struct inode *, struct file *);
int	phyaddr_release(struct inode *, struct file *);
int	phyaddr_ctl_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
ssize_t phyaddr_read(struct file *, char * , size_t );
ssize_t phyaddr_write(struct file *, const char * , size_t ,loff_t *);


//#################################################################
static struct file_operations phyaddr_ctl_fops = {
	open:		phyaddr_open,
	ioctl:		phyaddr_ctl_ioctl,
	read:		phyaddr_read,
	write:		phyaddr_write,
	release:	phyaddr_release,
};

int phyaddr_open(struct inode *inode, struct file *filp)
{
	int minor;

	minor = MINOR(inode->i_rdev);
	printk("open ok\n");
	phyaddr_devices[minor]++;
	return 0;

}
ssize_t phyaddr_read(struct file *fp, char *buf, size_t size)
{
   char val;
   int v_addr=0;
   printk("phyaddr_read\n");
   v_addr=ioremap(PHYADDR_ADDR,0x400);
   val=(*(volatile unsigned short *)(v_addr+0))&0x00ff;
   //put_user(val,buf);
   copy_to_user(buf,&val,2);
   printk("val = %x\n",val);
   return 1;
}
static ssize_t phyaddr_write(struct file * fp, const char * buf,
		        size_t size, loff_t *ppos)
//ssize_t phyaddr_write(struct file *fp, char *buf, size_t size)
{
	unsigned short val;
   	int v_addr=0;
	printk("phyaddr_write...\n");
        
        if(copy_from_user(&val,(void *)buf,sizeof(val)))
		return -EFAULT;

//	if (get_user(val, buf))
//		return -EFAULT;
	printk("val=%x\n",val);
	v_addr=ioremap(PHYADDR_ADDR,0x400);
	*(volatile unsigned short *)(v_addr+0)=val&0xff;
	return 1;
}

int phyaddr_release(struct inode *inode, struct file *filp)
{
	int minor;
	
	minor = MINOR(inode->i_rdev);
	if (phyaddr_devices[minor])
		phyaddr_devices[minor]--;
	printk("release ok\n");
	return 0;
}

int phyaddr_ctl_ioctl(struct inode *inode, 
		  struct file *flip,
		  unsigned int command, 
		  unsigned long arg)
{
	int err = 0;
	int minor = MINOR(inode->i_rdev);
#if 0
	switch (command) {
	case IOWRITE:
//		*(volatile unsigned int *)(port_addr+1)|=0x00000080;
		return 0;
	case IOCLEAR:
//		*(volatile unsigned int *)(port_addr+1)&=~0x00000080;
		printk("clear ok\n");
		return 0;
	default:
		err = -EINVAL;
	}
#endif
	return err;
}
//##########################################################
//hhtech add it by wpq ,2005 01 28
//
static int __init
phyaddr_init (void)
{
//	register_chrdev(PHYADDR_MAJOR, "phyaddrtest", &phyaddr_ctl_fops);
//############################################################################
//
//hhtech add it by wpq
//date:20050128
//The" devfs"is very important!!!register file command
//note:devfs and register ;but ,devfs= register+mknod
//Here isn't use :mknod  /dev/phyaddrtest c 220 0
        phyaddr_devfs_dir= devfs_register(NULL,"phyaddrtest",DEVFS_FL_DEFAULT,PHYADDR_MAJOR,
                             0, S_IFCHR |S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP,
                             &phyaddr_ctl_fops, NULL);
	
        printk ("phyaddr driver installed OK\n");
        printk ("Test is starting!\n");
        return 0;
}


static void __exit
phyaddr_exit(void)
{
        devfs_unregister(phyaddr_devfs_dir);
        printk("phyaddr driver uninstalled OK\n");
}



module_init(phyaddr_init);

module_exit(phyaddr_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("hn");
MODULE_DESCRIPTION("phyaddr Driver for HHARM9-EDU");


⌨️ 快捷键说明

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