📄 pio-drv.c
字号:
/*
pio driver for LPT2NET project
author: zou jian guo <zounix@126.com>
date: 2004-10-18
update: 2005-01-6
pio_read:
read count bytes data to user,but if no enough data to read ,
then return the actual count bytes to user, the EF sigh
is the driver to bethought, the user needn't take account.
pio_write:
user write the count bytes to pio ,the FF sigh need to be
careful deal with;
pio_ioctl: number 1 is to reset pio
*/
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/init.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/mm.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/poll.h> /* COPY_TO_USER */
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/delay.h> /* udelay */
#include <linux/interrupt.h>
#include <linux/tqueue.h>
#include <asm/io.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/arch/AT91RM9200.h> /* cli(), *_flags */
#include <asm/arch/hardware.h>
#define DEVICE_NAME "pio"
#define pio_MAJOR 254
#define pio_MINOR 5
#define RS485_TX_ENABLE (1<<21) //PA21 RTS0
static AT91PS_SYS AT91_SYS=((AT91PS_SYS)AT91C_VA_BASE_SYS);
//extern AT91PS_SYS AT91_SYS;
static void AT91_PIO_INIT(void)
{
//pio clock enable
AT91_SYS->PMC_PCER = (1<<AT91C_ID_PIOA);
//PIO ENABLE
AT91_SYS->PIOA_PDR = AT91C_PA17_TXD0 | AT91C_PA18_RXD0;
AT91_SYS->PIOA_PER = AT91C_PA21_RTS0; //USED TO CTROL DE /RE OF THE MAX3485
//SET IO OUTPUT ENABLE
AT91_SYS->PIOA_OER = AT91C_PA21_RTS0;
AT91_SYS->PIOA_ODR = ~( AT91C_PA21_RTS0);
AT91_SYS->PIOA_OWER = AT91C_PA21_RTS0;
AT91_SYS->PIOA_OWDR = ~(AT91C_PA21_RTS0);
//pio interrupt disable
// AT91_SYS->PIOA_IDR = AT91C_PA21_RTS0;
//Input Filter Enable Register
AT91_SYS->PIOA_IFER = AT91C_PA21_RTS0;
//PULL UP ENABLE
AT91_SYS->PIOA_PPUER= AT91C_PA21_RTS0;
}
/*********************************************************************/
static ssize_t pio_write(struct file *filp,char *buffer, size_t count)
{
return count;
}
/*********************************************************************/
static ssize_t pio_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
}
/*********************************************************************/
static int pio_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
switch(cmd){
case 0: AT91_SYS->PIOA_SODR = ~AT91C_PA21_RTS0;
AT91_SYS->PIOA_CODR = AT91C_PA21_RTS0;
printk("RS485 TX DISABLED\n");
break;
case 1: AT91_SYS->PIOA_SODR = AT91C_PA21_RTS0;
AT91_SYS->PIOA_CODR = ~AT91C_PA21_RTS0;
printk("RS485 TX ENABLED\n");
break;
default:
printk("error cmd number\n");break;
}
return 0;
}
/*********************************************************************/
static int pio_open(struct inode *inode, struct file *file)
{
MOD_INC_USE_COUNT;
AT91_PIO_INIT();
printk("device open sucess!\n");
return 0;
}
/*********************************************************************/
static int pio_release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
printk("device release\n");
return 0;
}
/*********************************************************************/
static struct file_operations pio_fops = {
owner: THIS_MODULE,
write: pio_write,
read: pio_read,
ioctl: pio_ioctl,
open: pio_open,
release: pio_release,
};
/*********************************************************************/
#ifdef CONFIG_DEVFS_FS
static devfs_handle_t devfs_pio_dir, devfs_pioraw;
#endif
/*********************************************************************/
static int __init pio_init(void)
{
#ifdef CONFIG_DEVFS_FS
devfs_pio_dir = devfs_mk_dir(NULL, "pio", NULL);
devfs_pioraw = devfs_register(devfs_pio_dir, "0", DEVFS_FL_DEFAULT,
pio_MAJOR, pio_MINOR, S_IFCHR | S_IRUSR | S_IWUSR,
&pio_fops, NULL);
#else
int result;
SET_MODULE_OWNER(&pio_fops);
result = register_chrdev(pio_MAJOR, "pio", &pio_fops);
if (result < 0) return result;
// if (pio_MAJOR == 0) pio_MAJOR = result; /* dynamic */
#endif
printk(DEVICE_NAME " initialized\n");
return 0;
}
/*********************************************************************/
static void __exit pio_exit(void)
{
#ifdef CONFIG_DEVFS_FS
devfs_unregister(devfs_pioraw);
devfs_unregister(devfs_pio_dir);
#else
unregister_chrdev(pio_MAJOR, "pio");
#endif
//kfree(pio_devices);
printk(DEVICE_NAME " unloaded\n");
}
/*********************************************************************/
module_init(pio_init);
module_exit(pio_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -