私はLinuxカーネルモジュールプログラミングの初心者であり、ダミーデバイスの読み書きにダミーキャラクタデバイスドライバを書いています(実際には、 )。このプログラムは、1つのデバイスファイルのみで試してみるとうまく動作しますが、同じメジャー番号を持ちながら異なるマイナー番号(つまり、1
)を持つ第2のダミーデバイスファイルを作成すると問題が発生します。私は1つのファイル(たとえばdevfile0、major : 250
、minor : 0
)に書き込むときのデータは、他のファイルに書き込む(devfile1、major : 250
、minor : 1
を言う)が、私はちょうどをdevfile1しないとをdevfile0する書きたいです。出来ますか?私はおそらく何が間違っているのですか?ここで同じメジャー番号でも一意のマイナー番号を持つ複数のデバイスファイルを扱う
は、私が作成したカーネルモジュールです:
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/cdev.h>
#include<linux/fs.h>
#include<linux/semaphore.h>
#include<asm/uaccess.h>
#include<linux/kmod.h>
struct cdev *newDev;
int maj_no;
int ret;
dev_t crdev;
#define DEVICE_NAME "CryptoDevCHARDEVDRVR"
struct dummy{
char string[100];
int length;
struct semaphore sem;
}device;
int device_open(struct inode *node,struct file *fp)
{
printk(KERN_ALERT "Atempting to open device file\n");
if(down_interruptible(&device.sem) != 0)
{
printk(KERN_ALERT "%s : Unable to Lock file while open\n",DEVICE_NAME);
return -1;
}
printk(KERN_ALERT "File open operation Complete\n");
return 0;
}
ssize_t device_read(struct file* fp,char* buffer, size_t bufsize, loff_t* buffoff)
{
printk(KERN_ALERT "Reading from the device...\n");
ret = copy_to_user(buffer,device.string,bufsize);
device.length = bufsize;
return ret;
}
ssize_t device_write(struct file* fp,const char* buffer, size_t bufsize, loff_t* buffoff)
{
printk(KERN_ALERT "Writing to the device...\n");
ret = copy_from_user(device.string,buffer,bufsize);
printk(KERN_ALERT "%s\n",device.string);
printk(KERN_ALERT "Written\n");
device.length = bufsize;
return ret;
}
int device_close(struct inode* node, struct file* fp)
{
printk(KERN_ALERT "Closing Device File");
up(&device.sem);
printk(KERN_ALERT "Device Close Successfully");
return 0;
}
struct file_operations fop = {
.owner = THIS_MODULE,
.open = device_open,
.release = device_close,
.read = device_read,
.write = device_write
};
static int hello_init(void)
{
ret = alloc_chrdev_region(&crdev,0,50,DEVICE_NAME);
if(ret < 0)
{
printk(KERN_ALERT "\n%s : Unable to assign Character Device Driver Region",DEVICE_NAME);
return ret;
}
maj_no = MAJOR(crdev);
printk(KERN_ALERT "%s : Major Number:%d\n",DEVICE_NAME,maj_no);
newDev = cdev_alloc();
newDev->ops = &fop;
newDev->owner = THIS_MODULE;
ret = cdev_add(newDev,crdev,50);
if(ret < 0)
{
printk(KERN_ALERT "%s : Unable to Register Device Driver\n",DEVICE_NAME);
}
sema_init(&device.sem,1);
printk(KERN_ALERT "Successfully Initialised Device Driver\n");
printk(KERN_ALERT "Test caller");
return 0;
}
static void hello_destroy(void)
{
printk(KERN_ALERT "Killing Hello-Start.c ... Byeeee\n");
cdev_del(newDev);
unregister_chrdev_region(crdev,50);
printk(KERN_ALERT "%s : Successfully Unregistered Driver\n",DEVICE_NAME);
printk(KERN_ALERT "Done");
}
module_init(hello_init);
module_exit(hello_destroy);
ユーザ空間のアプリケーションはdevfile0への書き込みをdevfile0とwrite
システムコールを開くためにopen
システムコールを使用しています。
fp = open(DEVICE , O_RDWR);
if(fp == -1)
{
printf("%s cann't be accessed right now try after sometime\n",DEVICE);
exit(-1);
}
printf("Enter any Character String to transmit to Device:");
fgets(buff,100,stdin);
write(fp,buff,sizeof(buff));
printf("\nWritten: %s\n",buff);
したがって、割り当てるデバイスごとに1つずつ、いくつかの基本バッファを作成する必要があります。 [その質問](http://stackoverflow.com/q/36066635/3440745)は、デバイスとファイル操作の違いを説明しています。 – Tsyvarev