我正在为
Linux编写设备驱动程序.它创建了一个包含4个次要编号的设备.每当我们尝试以次要编号3写入设备时,我们都会想要杀死设备,目前除了打印它正在写入booga设备之外,它不会做任何其他事情.这是我当前的一些代码,如果有必要,我可以发布更多代码:
写方法:
static ssize_t booga_write (struct file *filp,const char *buf,size_t count,loff_t *f_pose) { printk("Attempting to write to booga device\n"); /* need to protect this with a semaphore if multiple processes will invoke this driver to prevent a race condition */ if (down_interruptible (&booga_device_stats->sem)) return (-ERESTARTSYS); booga_device_stats->num_bytes_written += count; up(&booga_device_stats->sem); return count; // pretend that count bytes were written }
如何测试:
static void run_write_test(char *device,int bufsize) { char *buf; int src; int out; src = open(device,O_WRONLY); if (src < 0) { perror("Open for write failed:"); exit(1); } buf = (char *) malloc(sizeof(char)*(bufsize+1)); fprintf(stderr,"Attempting to write to booga device\n"); out = write(src,buf,bufsize); fprintf(stderr,"Wrote %d bytes.\n",out); free(buf); close(src); }
我想知道是否有办法获得次要号码.我查看了linux / fs.h,看到文件结构有一个名为private_data的成员,但每当我试图调用它时,它会使我的系统崩溃,因为它当前设置为null.
或者,我是否应该尝试从结构文件中获取次要编号,并且应该在我第一次打开设备时尝试跟踪它?
解决方法
您可以像这样得到次要号码:
iminor(filp->f_path.dentry->d_inode)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。