gpt4 book ai didi

c - 处理具有相同主编号但唯一次编号的多个设备文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:12:40 24 4
gpt4 key购买 nike

我是 Linux 内核模块编程的新手,并且编写了一个虚拟字符设备驱动程序来读取和写入虚拟设备(它实际上是其文档中给出的示例程序)。当我只使用一个设备文件尝试时程序运行良好,当我创建第二个具有相同主编号但不同次要编号(按顺序即 1 )的虚拟设备文件时,问题就出现了。当我写入一个文件(比如 devfile0、major : 250minor : 0)时,数据也会写入另一个文件(比如 devfile1、major : 250minor : 1),但我只想写入 devfile0 不给 devfile1。是否可以?我可能做错了什么?

这是我创建的内核模块:

#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);

用户空间应用程序使用 open系统调用打开 devfile0 和 write写入 devfile0 的系统调用。
这是相关代码:

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);

最佳答案

The major number tells you which driver is used to access the hardware. Each driver is assigned a unique major number; all device files with the same major number are controlled by the same driver. All the above major numbers are 3, because they're all controlled by the same driver.

The minor number is used by the driver to distinguish between the various hardware it controls. Returning to the example above, although all three devices are handled by the same driver they have unique minor numbers because the driver sees them as being different pieces of hardware.

来自 The Linux Kernel Module Programming Guide ,3.1.6.1。大号和小号

关于c - 处理具有相同主编号但唯一次编号的多个设备文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com