我将为Linux编写一个设备驱动程序。它创建了一个具有4个次设备号的设备。每当我们尝试在第3个次设备号上写入设备时,我们会杀死该设备,而目前除了打印正在写入booga设备外,它不应该执行任何其他操作。以下是我的一些现有代码,如果需要,我可以发布更多代码:
写入方法:
我想知道是否有获取次要编号的方法。我查看了linux/fs.h文件,发现文件结构体有一个名为private_data的成员,但每当我尝试调用它时,系统都会崩溃,因为它当前设置为null。
或者我根本不应该尝试从文件结构体中获取次要编号,而应该在第一次打开设备时尝试跟踪它?
写入方法:
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。
或者我根本不应该尝试从文件结构体中获取次要编号,而应该在第一次打开设备时尝试跟踪它?