gpt4 book ai didi

c - 是否可以在不使用 file_operations 结构的情况下编写字符驱动程序?

转载 作者:太空狗 更新时间:2023-10-29 12:32:25 26 4
gpt4 key购买 nike

我想知道是否有一种方法可以在不使用 file_operations 结构中提供的任何函数的情况下编写字符驱动程序。

我是 Linux 设备驱动程序的新手,就像任何新手一样,我开始阅读 LDD3书。我成功地编写了一个简单的字符驱动程序。

include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/semaphore.h>
#include<linux/uaccess.h>

int chardev_init(void);
void chardev_exit(void);
static int device_open(struct inode *, struct file *);
static int device_close(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t , loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
static loff_t device_lseek(struct file *filp, loff_t offset, int orig);

#define BUFFER_SIZE 1024
#define DEVICE_NAME "readWrite"

static char deviceBuffer[BUFFER_SIZE];
dev_t devNum; /* device number allocated by the kernel */
struct cdev *mcdev; /* name of the char driver that will be registered */
struct semaphore sem;
int majorNum;
int minorNum;
int ret;

static int device_open(struct inode *inode, struct file *filp)
{
if(down_interruptible(&sem) != 0)
{
printk(KERN_ALERT "%s: device has been opened by some other device, unable to open lock\n", DEVICE_NAME);
return -1;
}
printk(KERN_INFO "%s: device opened successfully\n", DEVICE_NAME);
return 0;
}

static ssize_t device_read(struct file *fp, char *buff, size_t length, loff_t *ppos)
{
int maxbytes; /* maximum bytes that can be read from ppos to BUFFER_SIZE */
int bytes_to_read; /* gives the number of bytes to read */
int bytes_read; /* number of bytes actually read */

maxbytes = BUFFER_SIZE - *ppos;
if(maxbytes > length)
bytes_to_read = length;
else
bytes_to_read = maxbytes;

if(bytes_to_read == 0)
printk(KERN_INFO "%s: reached the end of the device\n", DEVICE_NAME);
bytes_read = bytes_to_read - copy_to_user(buff, deviceBuffer + *ppos, bytes_to_read);
printk(KERN_INFO "%s: device has been read %d bytes\n", DEVICE_NAME, bytes_read);
*ppos += bytes_read;
printk(KERN_INFO "%s: device has been read\n", DEVICE_NAME);
return bytes_read;
}

static ssize_t device_write(struct file *fp, const char * buff, size_t length, loff_t *ppos)
{
int maxbytes; /* maximum bytes that can be written */
int bytes_to_write;
int bytes_written;

maxbytes = BUFFER_SIZE - *ppos;
if(maxbytes < length)
bytes_to_write = maxbytes;
else
bytes_to_write = length;
bytes_written = bytes_to_write - copy_from_user(deviceBuffer + *ppos, buff, bytes_to_write);
printk(KERN_INFO "%s: device has been written %d bytes\n", DEVICE_NAME, bytes_written);
*ppos += bytes_written;
printk(KERN_INFO "%s: device has been written %d\n", DEVICE_NAME, bytes_written);
return bytes_written;
}

static loff_t device_lseek(struct file *filp, loff_t offset, int orig)
{
loff_t new_pos = 0;
printk(KERN_INFO "%s: lseek function in work\n", DEVICE_NAME);

switch(orig)
{
case 0: /* seek set */
new_pos = offset;
break;
case 1: /* seek cur */
new_pos = filp->f_pos + offset;
break;
case 2: /* seek end */
new_pos = BUFFER_SIZE - offset;
break;
}
if(new_pos > BUFFER_SIZE)
new_pos = BUFFER_SIZE;
if(new_pos < 0)
new_pos = 0;
filp->f_pos = new_pos;
return new_pos;
}

static int device_close(struct inode *inode, struct file *filp)
{
up(&sem);
printk(KERN_INFO "%s: device has been closed\n", DEVICE_NAME);
return 0;
}

struct file_operations fops =
{
.owner = THIS_MODULE,
.read = device_read,
.write = device_write,
.llseek = device_lseek,
.release = device_close,
.open = device_open
};

int chardev_init(void)
{
/* get the major number dynamically */
ret = alloc_chrdev_region(&devNum, 0, 1, DEVICE_NAME);
if(ret < 0)
{
printk(KERN_ALERT "%s: failed to allocate major number\n", DEVICE_NAME);
return ret;
}
else
printk(KERN_INFO "%s: major number allocation successful\n", DEVICE_NAME);
majorNum = MAJOR(devNum);
minorNum = MINOR(devNum);
printk(KERN_INFO "%s: major number of our device is %d\n", DEVICE_NAME, majorNum);
printk(KERN_INFO "%s: minor number of our device is %d\n", DEVICE_NAME, minorNum);
printk(KERN_INFO "%s: to use mknod /dev/%s c %d 0\n", DEVICE_NAME, DEVICE_NAME, majorNum);

mcdev = cdev_alloc(); /* create, allocate and initialize our cdev structure */
mcdev->ops = &fops;
mcdev->owner = THIS_MODULE;

/* after creating and initializing our cdev structure, we need to add it to the kernel */
ret = cdev_add(mcdev, devNum, 1);
if(ret < 0)
{
printk(KERN_ALERT "%s: adding device to the kernel failed\n",DEVICE_NAME);
return ret;
}
else
printk(KERN_INFO "%s: adding device to the kernel successful\n", DEVICE_NAME);
sema_init(&sem, 1); /* initial value to 1 */
return 0;
}

void chardev_exit(void)
{
cdev_del(mcdev); /* removing the mcdev structure */
printk(KERN_INFO "%s: removed the mcdev from kernel\n", DEVICE_NAME);

unregister_chrdev_region(devNum,1);
printk(KERN_INFO "%s: unregistered the device numbers\n", DEVICE_NAME);
printk(KERN_ALERT "%s: character driver is exiting\n", DEVICE_NAME);
}

MODULE_AUTHOR("SJ");
MODULE_DESCRIPTION("read write char driver");
MODULE_LICENSE("GPL");

module_init(chardev_init);
module_exit(chardev_exit);

现在我开始了解更复杂的驱动程序,例如 this one ,但我完全不解。因为没有 file_operations 结构,所以没有 module_init() 或 module_exit() 函数。

我有很多问题,比如,

code 中分配主要编号的位置?

cdev结构在哪里?

如何处理读写操作?

有没有Linux嵌入式设备驱动编写经验的,帮我解答一下。

谢谢!

最佳答案

character 驱动程序是创建character 设备的驱动程序,即 /dev/ 中的某个设备节点,允许您访问通过open/read/write等系统调用驱动程序

对于以其他方式访问的设备,还有许多其他驱动程序类型。对于 SPI 驱动程序,内核 SPI 框架处理大部分设备管理; spi-omap2-mcspi 驱动程序注册自己的 spi_master 结构,该结构等同于通用 cdev/fops结构。

关于c - 是否可以在不使用 file_operations 结构的情况下编写字符驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836997/

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