linux内核的__uart_start函数
时间: 2024-05-29 21:11:18 浏览: 151
__uart_start函数是Linux内核中用于启动串口传输的函数。该函数位于drivers/tty/serial/serial_core.c文件中,其作用是启动与串口相关的数据传输。
具体来说,__uart_start函数会检查串口的状态,如果发现串口处于空闲状态,则将串口的数据传输状态设置为“已启动”。此后,数据将通过串口进行传输,直到传输完成或者被中断。
在Linux内核中,__uart_start函数通常由其他串口驱动程序调用,以便在需要时启动数据传输。该函数的实现依赖于操作系统底层的硬件控制器和驱动程序,因此在不同的硬件平台和操作系统版本中可能存在差异。
相关问题
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` 结构体包含了驱动程序的基本信息。
通过使用队列解耦原有的功能函数,我们可以将驱动程序中的不同部分分割成独立的模块,使得代码更加简洁、易于维护和扩展。
基于Linux内核,写一个使用中断方式发送和接收的UART驱动
好的,以下是一个基于Linux内核的使用中断方式发送和接收的UART驱动的示例代码。
```c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/serial_reg.h>
#include <linux/serial.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
// 定义串口寄存器地址
#define PORT_BASE 0x3f8
#define UART_RX 0
#define UART_TX 0
#define UART_IER 1
#define UART_IIR 2
#define UART_FCR 2
#define UART_LCR 3
#define UART_MCR 4
#define UART_LSR 5
// 定义串口中断号
#define UART_IRQ 4
// 定义读写缓冲区大小
#define BUFFER_SIZE 1024
// 定义串口设备结构体
struct uart_device {
spinlock_t lock; // 自旋锁
struct platform_device *pdev; // 平台设备指针
struct tty_port port; // tty端口
unsigned char *rx_buffer; // 接收缓冲区
unsigned char *tx_buffer; // 发送缓冲区
int rx_buffer_size; // 接收缓冲区大小
int tx_buffer_size; // 发送缓冲区大小
int tx_head; // 发送缓冲区头指针
int tx_tail; // 发送缓冲区尾指针
};
// 定义串口设备结构体全局变量
static struct uart_device *uart_dev;
// 定义串口中断处理函数
static irqreturn_t uart_interrupt(int irq, void *dev_id)
{
unsigned char status;
unsigned char c;
// 获取中断状态寄存器值
status = inb(PORT_BASE + UART_IIR);
// 如果是接收中断
if ((status & UART_IIR_NO_INT) == 0) {
// 不断读取接收寄存器中的数据,直到接收缓冲区满或没有数据为止
while (inb(PORT_BASE + UART_LSR) & UART_LSR_DR) {
c = inb(PORT_BASE + UART_RX);
// 将接收到的字符存入接收缓冲区
tty_insert_flip_char(&uart_dev->port, c, TTY_NORMAL);
}
// 告诉tty层有数据到来
tty_flip_buffer_push(&uart_dev->port);
}
// 如果是发送中断
if (status & UART_IIR_THRI) {
// 不断将发送缓冲区中的数据写入发送寄存器,直到发送缓冲区为空或发送寄存器已满
while (uart_dev->tx_head != uart_dev->tx_tail &&
inb(PORT_BASE + UART_LSR) & UART_LSR_THRE) {
c = uart_dev->tx_buffer[uart_dev->tx_tail];
outb(c, PORT_BASE + UART_TX);
uart_dev->tx_tail = (uart_dev->tx_tail + 1) % uart_dev->tx_buffer_size;
}
// 如果发送缓冲区已空,关闭发送中断
if (uart_dev->tx_head == uart_dev->tx_tail) {
outb(0x00, PORT_BASE + UART_IER);
}
}
return IRQ_HANDLED;
}
// 定义串口设备文件操作函数
static int uart_open(struct tty_struct *tty, struct file *file)
{
return 0;
}
static void uart_close(struct tty_struct *tty, struct file *file)
{
}
static int uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
int i;
// 获取自旋锁
spin_lock_irq(&uart_dev->lock);
// 将要发送的数据存入发送缓冲区
for (i = 0; i < count; i++) {
uart_dev->tx_buffer[uart_dev->tx_head] = buf[i];
uart_dev->tx_head = (uart_dev->tx_head + 1) % uart_dev->tx_buffer_size;
}
// 打开发送中断
outb(UART_IER_THRI, PORT_BASE + UART_IER);
// 释放自旋锁
spin_unlock_irq(&uart_dev->lock);
return count;
}
static int uart_write_room(struct tty_struct *tty)
{
return uart_dev->tx_buffer_size;
}
static const struct tty_operations uart_ops = {
.open = uart_open,
.close = uart_close,
.write = uart_write,
.write_room = uart_write_room,
};
// 定义串口设备初始化函数
static int uart_probe(struct platform_device *pdev)
{
int ret = 0;
unsigned char lcr;
// 初始化串口设备结构体
uart_dev = kzalloc(sizeof(struct uart_device), GFP_KERNEL);
if (!uart_dev) {
return -ENOMEM;
}
// 初始化自旋锁
spin_lock_init(&uart_dev->lock);
// 初始化tty端口
tty_port_init(&uart_dev->port);
uart_dev->port.ops = &uart_ops;
uart_dev->port.dev = &pdev->dev;
// 初始化接收缓冲区和发送缓冲区
uart_dev->rx_buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
uart_dev->tx_buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
uart_dev->rx_buffer_size = BUFFER_SIZE;
uart_dev->tx_buffer_size = BUFFER_SIZE;
uart_dev->tx_head = 0;
uart_dev->tx_tail = 0;
// 初始化串口
lcr = inb(PORT_BASE + UART_LCR);
outb(lcr | UART_LCR_DLAB, PORT_BASE + UART_LCR);
outb(115200 / 9600, PORT_BASE + UART_DLL);
outb(0x00, PORT_BASE + UART_DLM);
outb(lcr, PORT_BASE + UART_LCR);
outb(UART_FCR_ENABLE_FIFO, PORT_BASE + UART_FCR);
// 注册串口中断处理函数
ret = request_irq(UART_IRQ, uart_interrupt, IRQF_SHARED, "uart_interrupt", uart_dev);
if (ret) {
goto failed_irq;
}
// 注册tty设备
tty_port_register_device(&uart_dev->port, tty_register_device(tty_driver, 0, &pdev->dev));
// 保存平台设备指针
uart_dev->pdev = pdev;
return 0;
failed_irq:
kfree(uart_dev->rx_buffer);
kfree(uart_dev->tx_buffer);
kfree(uart_dev);
return ret;
}
static int uart_remove(struct platform_device *pdev)
{
// 注销tty设备
tty_unregister_device(tty_driver, 0);
// 注销串口中断处理函数
free_irq(UART_IRQ, uart_dev);
// 释放接收缓冲区和发送缓冲区
kfree(uart_dev->rx_buffer);
kfree(uart_dev->tx_buffer);
// 释放串口设备结构体
kfree(uart_dev);
return 0;
}
// 定义平台设备结构体
static struct platform_driver uart_platform_driver = {
.probe = uart_probe,
.remove = uart_remove,
.driver = {
.name = "uart",
.owner = THIS_MODULE,
},
};
// 初始化模块
static int __init uart_init(void)
{
int ret = 0;
// 注册平台设备
ret = platform_driver_register(&uart_platform_driver);
if (ret) {
printk(KERN_ERR "uart: failed to register platform driver\n");
return ret;
}
// 注册tty设备驱动
tty_driver = alloc_tty_driver(1);
if (!tty_driver) {
printk(KERN_ERR "uart: failed to allocate tty driver\n");
platform_driver_unregister(&uart_platform_driver);
return -ENOMEM;
}
tty_driver->driver_name = "uart";
tty_driver->name = "ttyUART";
tty_driver->major = 0;
tty_driver->minor_start = 0;
tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
tty_driver->subtype = SERIAL_TYPE_NORMAL;
tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
tty_driver->init_termios = tty_std_termios;
tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
tty_driver->init_termios.c_lflag = 0;
tty_driver->init_termios.c_iflag = 0;
tty_driver->init_termios.c_oflag = 0;
tty_driver->driver_state = NULL;
tty_driver->chars_in_buffer = 0;
tty_driver->port = &uart_dev->port;
ret = tty_register_driver(tty_driver);
if (ret) {
printk(KERN_ERR "uart: failed to register tty driver\n");
put_tty_driver(tty_driver);
platform_driver_unregister(&uart_platform_driver);
return ret;
}
return 0;
}
// 清理模块
static void __exit uart_exit(void)
{
// 注销tty设备驱动
tty_unregister_driver(tty_driver);
put_tty_driver(tty_driver);
// 注销平台设备
platform_driver_unregister(&uart_platform_driver);
}
module_init(uart_init);
module_exit(uart_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple UART driver for Linux");
```
这段代码中,我们首先定义了串口寄存器地址和中断号,然后定义了一个表示串口设备的结构体,并在其中定义了接收缓冲区、发送缓冲区、缓冲区大小和指针等。接着,我们定义了一个串口中断处理函数,在其中处理接收和发送中断。然后,我们定义了一个表示tty设备的结构体,并在其中定义了打开、关闭和写操作函数。最后,我们定义了一个平台设备结构体和一个tty设备驱动结构体,并在模块初始化函数中注册平台设备和tty设备驱动。
这个驱动程序中的数据传输过程是通过中断来完成的,每当有数据到来或发送完毕时,就会触发中断处理函数。在中断处理函数中,我们不断读取或写入数据,直到接收缓冲区满或发送缓冲区空为止。在写操作函数中,我们将要发送的数据存入发送缓冲区,并开启发送中断。这样,当有数据到来或发送完毕时,就会触发中断处理函数,从而完成数据的传输。
阅读全文