当前位置: 技术问答>linux和unix
简单的字符设备驱动程序问题
来源: 互联网 发布时间:2016-04-04
本文导语: 我写了十分简单的module,只实现read的功能, 测试文件可以如下: int main() { printf("Scull testn"); char buf[20]; int fd=open("/dev/scull0",O_RDWR); printf("fd is %d",fd); read(fd,buf,10); printf("the output is %cn",buf[0]); close(fd); return 0...
我写了十分简单的module,只实现read的功能,
测试文件可以如下:
int main()
{
printf("Scull testn");
char buf[20];
int fd=open("/dev/scull0",O_RDWR);
printf("fd is %d",fd);
read(fd,buf,10);
printf("the output is %cn",buf[0]);
close(fd);
return 0;
}
最后没有正确的读到内容,不解,各位高手帮帮忙
以下是代码(头文件省略了)
static char buffer[]="123456789012345678901234567890";
int scull_trim(Scull_Dev *dev)
{return 0;
}
int scull_open(struct inode *inode,struct file *filp)
{return 0;
}
void scull_release(struct inode *inode,struct file *filp)
{
//MOD_DEC_USE_COUNT;
}
Scull_Dev *scull_follow(Scull_Dev *dev, int n)
{
return dev;
}
read_write_t scull_read (struct inode *inode, struct file *filp,
char *buf, count_t count)
{
copy_to_user(buf,buffer,count);
return count;
}
read_write_t scull_write (struct inode *inode, struct file *filp,const char *buf, count_t count)
{
printk("in write functionn");
return count;
}
int scull_ioctl (struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
return 0;
}
int scull_lseek (struct inode *inode, struct file *filp,
off_t off, int whence)
{
return 0;
}
struct file_operations scull_fops = {
read: scull_read,
write: scull_write,
open: scull_open,
release: scull_release,
};
int init_module(void)
{
int result, i;
SET_MODULE_OWNER(&scull_fops);
result = register_chrdev(0, "scull", &scull_fops);
if (result
测试文件可以如下:
int main()
{
printf("Scull testn");
char buf[20];
int fd=open("/dev/scull0",O_RDWR);
printf("fd is %d",fd);
read(fd,buf,10);
printf("the output is %cn",buf[0]);
close(fd);
return 0;
}
最后没有正确的读到内容,不解,各位高手帮帮忙
以下是代码(头文件省略了)
static char buffer[]="123456789012345678901234567890";
int scull_trim(Scull_Dev *dev)
{return 0;
}
int scull_open(struct inode *inode,struct file *filp)
{return 0;
}
void scull_release(struct inode *inode,struct file *filp)
{
//MOD_DEC_USE_COUNT;
}
Scull_Dev *scull_follow(Scull_Dev *dev, int n)
{
return dev;
}
read_write_t scull_read (struct inode *inode, struct file *filp,
char *buf, count_t count)
{
copy_to_user(buf,buffer,count);
return count;
}
read_write_t scull_write (struct inode *inode, struct file *filp,const char *buf, count_t count)
{
printk("in write functionn");
return count;
}
int scull_ioctl (struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
return 0;
}
int scull_lseek (struct inode *inode, struct file *filp,
off_t off, int whence)
{
return 0;
}
struct file_operations scull_fops = {
read: scull_read,
write: scull_write,
open: scull_open,
release: scull_release,
};
int init_module(void)
{
int result, i;
SET_MODULE_OWNER(&scull_fops);
result = register_chrdev(0, "scull", &scull_fops);
if (result