linux内核uart驱动,使用队列解耦原有的功能函数
时间: 2024-05-09 21:15:36 浏览: 137
linux设备模型之uart驱动架构分析
Linux内核UART驱动通常使用队列来解耦原有的功能函数,以提高代码的可读性和可维护性。这种方法的基本思想是将不同的功能分割成独立的模块,每个模块都有一个输入队列和一个输出队列。
输入队列用于接收来自UART接口的数据,处理数据并将结果放入输出队列中。输出队列中的数据可以是控制命令、状态信息或者其他需要传递给上层应用程序的数据。这种方法的优点是可以使编写驱动程序更加简单,易于维护和扩展。
以下是一个使用队列解耦原有功能函数的UART驱动程序的示例:
```c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/serial_core.h>
#define BUFFER_SIZE 1024
struct uart_device {
struct cdev cdev;
struct uart_port port;
struct mutex mutex;
struct work_struct work;
struct tasklet_struct tasklet;
spinlock_t lock;
wait_queue_head_t read_queue;
wait_queue_head_t write_queue;
char *buf;
int head;
int tail;
};
static int uart_driver_open(struct inode *inode, struct file *file)
{
struct uart_device *dev;
dev = container_of(inode->i_cdev, struct uart_device, cdev);
file->private_data = dev;
return 0;
}
static int uart_driver_release(struct inode *inode, struct file *file)
{
return 0;
}
static ssize_t uart_driver_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
struct uart_device *dev = file->private_data;
ssize_t ret;
if (count == 0)
return 0;
if (wait_event_interruptible(dev->read_queue, dev->head != dev->tail))
return -ERESTARTSYS;
mutex_lock(&dev->mutex);
if (dev->head > dev->tail) {
ret = min_t(ssize_t, count, dev->head - dev->tail);
if (copy_to_user(buf, dev->buf + dev->tail, ret)) {
ret = -EFAULT;
goto out;
}
dev->tail += ret;
} else {
ret = min_t(ssize_t, count, BUFFER_SIZE - dev->tail);
if (copy_to_user(buf, dev->buf + dev->tail, ret)) {
ret = -EFAULT;
goto out;
}
dev->tail = (dev->tail + ret) % BUFFER_SIZE;
}
out:
mutex_unlock(&dev->mutex);
return ret;
}
static ssize_t uart_driver_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
{
struct uart_device *dev = file->private_data;
ssize_t ret;
if (count == 0)
return 0;
if (wait_event_interruptible(dev->write_queue, dev->head != ((dev->tail - 1 + BUFFER_SIZE) % BUFFER_SIZE)))
return -ERESTARTSYS;
mutex_lock(&dev->mutex);
if (dev->tail > dev->head) {
ret = min_t(ssize_t, count, BUFFER_SIZE - dev->tail);
if (copy_from_user(dev->buf + dev->tail, buf, ret)) {
ret = -EFAULT;
goto out;
}
dev->tail += ret;
} else {
ret = min_t(ssize_t, count, dev->head - dev->tail);
if (copy_from_user(dev->buf + dev->tail, buf, ret)) {
ret = -EFAULT;
goto out;
}
dev->tail = (dev->tail + ret) % BUFFER_SIZE;
}
out:
mutex_unlock(&dev->mutex);
return ret;
}
static void uart_driver_work(struct work_struct *work)
{
struct uart_device *dev = container_of(work, struct uart_device, work);
struct uart_port *port = &dev->port;
unsigned char c;
int i;
mutex_lock(&dev->mutex);
while (uart_chars_avail(port)) {
c = uart_get_char(port);
if (dev->head == ((dev->tail - 1 + BUFFER_SIZE) % BUFFER_SIZE)) {
/* Buffer is full, drop the incoming character */
continue;
}
dev->buf[dev->head] = c;
dev->head = (dev->head + 1) % BUFFER_SIZE;
}
mutex_unlock(&dev->mutex);
wake_up_interruptible(&dev->read_queue);
}
static void uart_driver_tasklet(unsigned long data)
{
struct uart_device *dev = (struct uart_device *)data;
struct uart_port *port = &dev->port;
unsigned char c;
int i;
spin_lock(&dev->lock);
while (uart_chars_avail(port)) {
c = uart_get_char(port);
if (dev->head == ((dev->tail - 1 + BUFFER_SIZE) % BUFFER_SIZE)) {
/* Buffer is full, drop the incoming character */
continue;
}
dev->buf[dev->head] = c;
dev->head = (dev->head + 1) % BUFFER_SIZE;
}
spin_unlock(&dev->lock);
wake_up_interruptible(&dev->read_queue);
}
static void uart_driver_start(struct uart_port *port)
{
struct uart_device *dev = container_of(port, struct uart_device, port);
INIT_WORK(&dev->work, uart_driver_work);
tasklet_init(&dev->tasklet, uart_driver_tasklet, (unsigned long)dev);
spin_lock_init(&dev->lock);
init_waitqueue_head(&dev->read_queue);
init_waitqueue_head(&dev->write_queue);
mutex_init(&dev->mutex);
dev->buf = kzalloc(BUFFER_SIZE, GFP_KERNEL);
dev->head = 0;
dev->tail = 0;
uart_write_wakeup(port);
}
static void uart_driver_stop(struct uart_port *port)
{
struct uart_device *dev = container_of(port, struct uart_device, port);
cancel_work_sync(&dev->work);
tasklet_kill(&dev->tasklet);
spin_lock_irq(&dev->lock);
dev->head = dev->tail = 0;
spin_unlock_irq(&dev->lock);
kfree(dev->buf);
}
static struct uart_ops uart_driver_ops = {
.tx_empty = uart_tx_empty,
.set_mctrl = uart_set_mctrl,
.get_mctrl = uart_get_mctrl,
.stop_tx = uart_stop_tx,
.start_tx = uart_start_tx,
.send_xchar = uart_send_xchar,
.stop_rx = uart_stop_rx,
.enable_ms = uart_enable_ms,
.break_ctl = uart_break_ctl,
.startup = uart_driver_start,
.shutdown = uart_driver_stop,
};
static struct uart_driver uart_driver = {
.owner = THIS_MODULE,
.driver_name = "uart_driver",
.dev_name = "ttyUART",
.major = 0,
.minor = 0,
.nr = 1,
.cons = NULL,
.ops = &uart_driver_ops,
};
static int __init uart_driver_init(void)
{
dev_t devno;
int ret;
ret = alloc_chrdev_region(&devno, 0, 1, "uart_driver");
if (ret < 0)
return ret;
cdev_init(&uart_driver.cdev, &uart_driver_ops);
uart_driver.cdev.owner = THIS_MODULE;
ret = cdev_add(&uart_driver.cdev, devno, 1);
if (ret < 0) {
unregister_chrdev_region(devno, 1);
return ret;
}
uart_register_driver(&uart_driver);
return 0;
}
static void __exit uart_driver_exit(void)
{
uart_unregister_driver(&uart_driver);
cdev_del(&uart_driver.cdev);
unregister_chrdev_region(uart_driver.cdev.dev, 1);
}
module_init(uart_driver_init);
module_exit(uart_driver_exit);
MODULE_AUTHOR("Your Name");
MODULE_LICENSE("GPL");
```
在这个驱动程序中,我们使用了两个输入队列和一个输出队列。`read_queue` 用于接收来自 UART 的数据,`write_queue` 用于接收要发送到 UART 的数据,`buf` 用于存储接收到的数据。
`wait_event_interruptible` 函数用于等待数据到达队列。`mutex_lock` 和 `mutex_unlock` 函数用于保护共享数据结构。`wake_up_interruptible` 函数用于唤醒等待在队列上的进程。
`uart_driver_work` 和 `uart_driver_tasklet` 函数用于从 UART 中接收数据,并将接收到的数据放入输入队列中。
`uart_driver_start` 和 `uart_driver_stop` 函数用于初始化和清除输入队列和输出队列中的数据。`uart_driver_ops` 结构体包含了驱动程序中使用的 UART 操作。`uart_driver` 结构体包含了驱动程序的基本信息。
通过使用队列解耦原有的功能函数,我们可以将驱动程序中的不同部分分割成独立的模块,使得代码更加简洁、易于维护和扩展。
阅读全文