我写了一个并口 Linux 设备驱动程序,但我无法访问并口硬件
#include <linux/init.h>
#include <linux/autoconf.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <linux/ioport.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#define DRVNAME "parportleds"
#define BASEPORT 0x378
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Ganesh");
MODULE_DESCRIPTION("Parallel port LED driver.");
int parportleds_open(struct inode *inode, struct file *filp);
int parportleds_release(struct inode *inode, struct file *filp);
ssize_t parportleds_read(struct file *filp,char *buf,size_t count, loff_t *f_pos);
ssize_t parportleds_write(struct file *filp, const char *buf,size_t count, loff_t *f_pos);
void parportleds_exit(void);
int parportleds_init(void);
struct file_operations parportleds_fops = {
read: parportleds_read,
write: parportleds_write,
open: parportleds_open,
release: parportleds_release
};
int parportleds_major = 61;
int port;
module_init(parportleds_init);
module_exit(parportleds_exit);
int parportleds_init(void) {
int result;
result = register_chrdev(parportleds_major, DRVNAME,&parportleds_fops);
if (result < 0) {
printk("<1>parport: cannot obtain major number %d\n",parportleds_major);
return result;
}
port = check_region(BASEPORT, 1);
if (port) {
printk("<1>parportleds: cannot reserve 0x378 \n");
result = port;
goto fail;
}
request_region(BASEPORT, 1, DRVNAME);
printk("<1>Inserting parportled module\n");
return 0;
fail:
parportleds_exit();
return result;
}
void parportleds_exit(void) {
unregister_chrdev(parportleds_major, DRVNAME);
if (!port) {
release_region(BASEPORT,1);
}
printk("<1>Removing module parportleds\n");
}
int parportleds_open(struct inode *inode, struct file *filp) {
return 0;
}
int parportleds_release(struct inode *inode, struct file *filp) {
return 0;
}
ssize_t parportleds_read(struct file *filp, char *buf,size_t count, loff_t *f_pos) {
char parportleds_buffer;
parportleds_buffer = inb(BASEPORT);
copy_to_user(buf,&parportleds_buffer,1);
if (*f_pos == 0) {
*f_pos+=1;
return 1;
} else {
return 0;
}
}
ssize_t parportleds_write( struct file *filp, const char *buf, size_t count, loff_t *f_pos) {
char *tmp;
char parportleds_buffer;
tmp=(char *)buf+count-1;
copy_from_user(&parportleds_buffer,tmp,1);
outb(parportleds_buffer,BASEPORT);
printk("<1>parport write: %d\n",parportleds_buffer);
return 1;
}
这是我的代码,当我尝试读写时,/dev/parportleds
我在 dmesg 中获得了成功的消息
但是当我尝试使用并行端口的数据引脚echo 3 > /dev/parportleds
的电压保持不变时,并行端口的引脚电压保持不变。
谁能帮我解决这个问题。
这是我第一次尝试编写设备驱动程序。