- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道是否有一种方法可以在不使用 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/
我试图理解Linux 内核 中字符设备驱动程序 背后的概念。基本上我想向用户模式程序公开一个 IOCtl。我实现了一个“struct file_operations->unlocked_ioctl”和
我正在为 linux 内核编写一个简单的 misc 设备驱动程序。在我的 file_operations.write 中,我做了一些检查并将传递的值与预定义值进行比较,如果值相等,我返回字符串长度,如
我想知道是否有一种方法可以在不使用 file_operations 结构中提供的任何函数的情况下编写字符驱动程序。 我是 Linux 设备驱动程序的新手,就像任何新手一样,我开始阅读 LDD3书。我成
我正在实现一个 Linux 字符设备驱动程序。 linux/fs.h 头文件列出了不带参数名称的 file_operations。 例如 struct file_operations { st
我正在尝试制作一个支持打开、关闭、读取和写入操作的 linux 内核模块。所以我想通过 struct file_operations 注册这些函数,但是我在结构中找不到“关闭”条目。我想我应该使用'r
我正在写一个 pci driver对于 PCIe FPGA卡片。 我不确定如何实现“读取”和“写入”功能 - 如何使用参数:struct file *filp, char _ _user *buff,
我正在编写一个简单的 linux 驱动程序,它可以接受通过 CLI 传输的消息,但我收到了一个不兼容的指针类型错误。 我的读取函数和 file_operations 结构: ssize_t drive
假设我想让用户访问的内存被分成几页。我有两个相同大小的数组:一个用于逻辑地址,一个用于 dma_ptr。我想将这些连续的片段映射到一个连续的虚拟地址,供用户使用。 我真的无法理解 remap_pfn_
我正在设计一个只读取和写入字符缓冲区的设备驱动程序。然而,我的问题是关于 file_operations 结构 read 和 write 中的两个函数。我真的不明白 loff_t *offp 到底是什
我尝试导入 file_operations 的结构并得到这个错误: Variable has incomplete type 'struct file_operations' 我的导入是 #inclu
您好,我有几个问题。如果有人能回答我会很高兴:)我正在尝试为 lcd 16X2 模块编译 Linux 驱动程序。我试图在我的代码中使用 struct file_operations();1.我注意到其
我正在尝试注册一个字符设备,但我不确定在使用 register_chrdev 注册设备时使用的 file_opreation 之间有什么区别?和 device_attribute,在使用 device
我正在开发一个 Linux 内核模块,它通过系统调用 mmap 与用户应用程序共享一 block 内存。该模块在 struct file_operations 中定义的 mmap 回调的帮助下正常工作
引用 LDD-3 pg-50。据记载 struct module *owner 第一个 file_operations 字段根本不是一个操作;它是指向“拥有”该结构的模块的指针。该字段用于防止模块
我正在跟踪 open() 系统调用,以查明 struct file_operations 和 struct file 在创建文件期间何时连接. 主要路径如下: sys_open -> do_sys_o
我正在使用 ubuntu 和内核版本 5.6.3-rc3。 我在 file_operations 中没有看到任何 ioctl标题中的结构。 是否取消了支持?什么是替代品? 从 linux/fs.h 粘
struct file_operations中的unlocked_ioctl的签名是 long (*unlocked_ioctl) (struct file *, unsigned int, unsi
我是一名优秀的程序员,十分优秀!