linux kernel poll_wait
时间: 2024-06-21 08:01:59 浏览: 225
Linux内核的poll_wait是用于实现异步IO的函数之一。它主要用于向内核注册文件描述符的等待队列,以便在等待队列上有事件发生时,唤醒等待队列上的进程。该函数需要一个等待队列头指针和一个等待队列项结构体指针作为参数。当内核准备好处理文件描述符相关的事件时,它将调用poll_wait函数将进程添加到等待队列中。当该文件描述符相关的事件发生时,内核会唤醒等待队列上的进程,以便进程可以继续执行操作。
相关问题
lan8720 linux驱动
LAN8720是一款常用的以太网PHY芯片,它可以通过RMII或MII接口与Linux系统连接。下面是一个可能的LAN8720 Linux驱动程序的示例代码:
```
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/of.h>
#include <linux/of_device.h>
/* LAN8720 PHY registers */
#define LAN8720_REG_BCR 0x00 /* Basic Control Register */
#define LAN8720_REG_BSR 0x01 /* Basic Status Register */
#define LAN8720_REG_PHYID1 0x02 /* PHY Identifier 1 */
#define LAN8720_REG_PHYID2 0x03 /* PHY Identifier 2 */
#define LAN8720_REG_ANAR 0x04 /* Auto-Negotiation Advertisement Register */
#define LAN8720_REG_ANLPAR 0x05 /* Auto-Negotiation Link Partner Ability Register */
#define LAN8720_REG_ANER 0x06 /* Auto-Negotiation Expansion Register */
#define LAN8720_REG_DSCR 0x10 /* PCS/TX Descriptor Register */
#define LAN8720_REG_DSCSR 0x11 /* PCS/TX Descriptor and Status Register */
#define LAN8720_REG_PHYCR 0x19 /* PHY Control Register */
#define LAN8720_PHY_RESET_DELAY_MS 100
struct lan8720_priv {
struct device *dev;
struct net_device *netdev;
void __iomem *regs;
};
static int lan8720_mdio_read(struct mii_bus *bus, int phy_addr, int reg_addr)
{
struct lan8720_priv *priv = bus->priv;
void __iomem *regs = priv->regs;
int val;
writel(0x80000000 | (phy_addr << 23) | (reg_addr << 18), regs + LAN8720_REG_DSCR);
while (!(readl(regs + LAN8720_REG_DSCSR) & 0x80000000));
val = readl(regs + LAN8720_REG_DSCR) & 0xffff;
return val;
}
static int lan8720_mdio_write(struct mii_bus *bus, int phy_addr, int reg_addr, u16 val)
{
struct lan8720_priv *priv = bus->priv;
void __iomem *regs = priv->regs;
writel(0xc0000000 | (phy_addr << 23) | (reg_addr << 18) | val, regs + LAN8720_REG_DSCR);
while (!(readl(regs + LAN8720_REG_DSCSR) & 0x80000000));
return 0;
}
static int lan8720_phy_reset(struct lan8720_priv *priv)
{
u16 val;
/* Reset PHY */
lan8720_mdio_write(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR, 0x8000);
msleep(LAN8720_PHY_RESET_DELAY_MS);
/* Wait for PHY to come out of reset */
val = lan8720_mdio_read(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR);
if (val & 0x8000)
return -EBUSY;
return 0;
}
static int lan8720_phy_init(struct lan8720_priv *priv)
{
u16 val;
/* Reset PHY */
if (lan8720_phy_reset(priv))
return -EBUSY;
/* Enable auto-negotiation */
lan8720_mdio_write(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR, 0x1000);
/* Wait for auto-negotiation to complete */
do {
val = lan8720_mdio_read(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BSR);
} while (!(val & 0x0020));
/* Enable RX/TX */
lan8720_mdio_write(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR, 0x2000);
return 0;
}
static int lan8720_probe(struct platform_device *pdev)
{
struct lan8720_priv *priv;
struct net_device *netdev;
struct device_node *np = pdev->dev.of_node;
struct resource *res;
int ret;
/* Allocate private data */
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
/* Allocate netdev */
netdev = alloc_etherdev(sizeof(*priv));
if (!netdev)
return -ENOMEM;
/* Set netdev MAC address */
of_property_read_u8_array(np, "local-mac-address", netdev->dev_addr, ETH_ALEN);
/* Initialize netdev */
netdev->netdev_ops = &lan8720_netdev_ops;
netdev->ethtool_ops = &lan8720_ethtool_ops;
netdev->needs_free_netdev = true;
netif_napi_add(netdev, &priv->napi, lan8720_rx_napi, NAPI_POLL_WEIGHT);
/* Allocate MDIO bus */
netdev->phydev = mdiobus_alloc();
if (!netdev->phydev) {
free_netdev(netdev);
return -ENOMEM;
}
/* Set MDIO bus parameters */
netdev->phydev->bus = priv->netdev->mdio_bus;
netdev->phydev->mdio_read = lan8720_mdio_read;
netdev->phydev->mdio_write = lan8720_mdio_write;
netdev->phydev->priv = priv;
/* Initialize PHY */
ret = lan8720_phy_init(priv);
if (ret) {
mdiobus_free(netdev->phydev->bus);
free_netdev(netdev);
return ret;
}
/* Set netdev device and add it to network interface */
priv->dev = &pdev->dev;
priv->netdev = netdev;
platform_set_drvdata(pdev, priv);
ret = register_netdev(netdev);
if (ret) {
mdiobus_free(netdev->phydev->bus);
free_netdev(netdev);
return ret;
}
return 0;
}
static int lan8720_remove(struct platform_device *pdev)
{
struct lan8720_priv *priv = platform_get_drvdata(pdev);
unregister_netdev(priv->netdev);
mdiobus_free(priv->netdev->phydev->bus);
free_netdev(priv->netdev);
return 0;
}
static const struct of_device_id lan8720_of_match[] = {
{ .compatible = "smsc,lan8720" },
{ },
};
MODULE_DEVICE_TABLE(of, lan8720_of_match);
static struct platform_driver lan8720_driver = {
.probe = lan8720_probe,
.remove = lan8720_remove,
.driver = {
.name = "lan8720",
.of_match_table = lan8720_of_match,
},
};
module_platform_driver(lan8720_driver);
MODULE_AUTHOR("Your Name Here");
MODULE_DESCRIPTION("LAN8720 driver");
MODULE_LICENSE("GPL");
```
需要注意的是,以上示例代码只是一个框架,实际的驱动程序需要根据硬件的具体特性进行实现。另外,该驱动程序还包含了一个NAPI驱动程序,以便在高速网络环境下进行数据接收。
linux异步io驱动 DEMO
我可以为您提供一个简单的基于Linux异步IO的驱动程序范例。这个例子演示了如何使用异步IO驱动程序在Linux内核中使用I/O, 并且可以说是一个非常简单的实现。但是需要注意的是,这个例子并不是用来在实际应用中使用的,而是用来理解和学习异步IO在Linux中的使用方法。以下是基于Linux异步IO的驱动程序范例代码:
```c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/version.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/async.h>
#include <linux/ioctl.h>
#define DEVICE_NAME "my_device" // 设备名称
#define MSG_BUFFER_LEN 2048 // 消息缓冲区长度
#define MAJOR_NUM 101 // 设备主设备号
#define MSG_START_CMD 0x00 // 消息开始命令
#define MSG_STOP_CMD 0x01 // 消息停止命令
static int major_num; // 设备主设备号
static char *msg_buffer;
static struct class *my_class;
static struct device *my_device;
static struct cdev my_cdev;
static DECLARE_WAIT_QUEUE_HEAD(read_wq); // 定义等待队列 read_wq
static struct async_struct my_async;
typedef struct {
unsigned char command;
unsigned int data_len;
void * data; // 存储数据
} command_t;
// 驱动程序打开函数
static int my_device_open(struct inode *inode, struct file *file)
{
return 0;
}
// 驱动程序关闭函数
static int my_device_release(struct inode *inode, struct file *file)
{
return 0;
}
// 驱动程序读函数
static ssize_t my_device_read(struct file *filp, char __user *buf,
size_t len, loff_t *off)
{
int size;
if( async_scheduled(&my_async) ) // 如果异步请求完成
{
size = async_error(&my_async); // 获取异步请求结果
if(size < 0) // 如果请求错误
return size;
// 如果请求成功
copy_to_user(buf, my_async.buf, size); // 将缓冲区中的数据传递给用户空间
async_done(&my_async, size); // 完成异步请求
return size;
}
if(wait_event_interruptible(read_wq, async_scheduled(&my_async))) // 等待异步请求
return -ERESTARTSYS; // 如果被中断则返回错误
size = async_error(&my_async); // 获取异步请求结果
if(size < 0) // 如果请求错误
return size;
// 如果请求成功
copy_to_user(buf, my_async.buf, size); // 将缓冲区中的数据传递给用户空间
async_done(&my_async, size); // 完成异步请求
return size;
}
// 驱动程序写函数
static ssize_t my_device_write(struct file *filp, const char __user *buf,
size_t len, loff_t *off)
{
int size;
unsigned int data_len;
command_t cmd;
cmd.data = kmalloc(MSG_BUFFER_LEN, GFP_KERNEL);
if (copy_from_user(&cmd, buf, sizeof(cmd))) // 从用户空间中拷贝数据
goto ERROR_SYS;
if (cmd.data_len > MSG_BUFFER_LEN) // 请求的数据长度过长
goto ERROR_CMD;
if (copy_from_user(cmd.data, buf + sizeof(cmd), cmd.data_len)) // 从用户空间中拷贝数据
goto ERROR_SYS;
size = async_read(my_async.q, cmd.command, cmd.data, cmd.data_len, 0); // 发送异步请求
if(size < 0) // 如果请求错误
goto ERROR_SYS;
kfree(cmd.data);
return cmd.data_len + sizeof(unsigned char) + sizeof(unsigned int);
ERROR_CMD:
kfree(cmd.data);
return -EINVAL; // 命令错误
ERROR_SYS:
kfree(cmd.data);
return -EFAULT; // 拷贝数据错误
}
// 驱动程序控制命令函数
static long my_device_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
if (cmd == MSG_STOP_CMD) // 发送停止异步请求命令
{
async_stop(&my_async.q);
return 0;
}
return -ENOTTY;
}
// 驱动程序操作函数
static struct file_operations my_device_fops = {
.owner = THIS_MODULE,
.open = my_device_open,
.release = my_device_release,
.read = my_device_read,
.write = my_device_write,
.unlocked_ioctl = my_device_ioctl,
.compat_ioctl = my_device_ioctl,
};
// 驱动程序初始化函数,注册设备
static int __init my_device_init(void)
{
int res;
dev_t dev;
// 申请设备号
res = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
if (res < 0)
{
printk(KERN_ERR "Failed to allocate char device region\n");
return res;
}
// 获得设备主设备号
major_num = MAJOR(dev);
msg_buffer = kmalloc(MSG_BUFFER_LEN, GFP_KERNEL);
if (!msg_buffer) // 申请内核缓冲区失败
goto ERR_KMALLOC;
async_init(&my_async, msg_buffer, MSG_BUFFER_LEN, GFP_KERNEL); // 初始化异步请求结构体
cdev_init(&my_cdev, &my_device_fops); // 初始化设备
res = cdev_add(&my_cdev, MKDEV(major_num, 0), 1); // 加入驱动程序
if (res < 0)
{
printk(KERN_ERR "Couldn't add the device to the system\n");
goto ERR_CDEV_ADD;
}
my_class = class_create(THIS_MODULE, DEVICE_NAME); // 创建设备类
if (IS_ERR(my_class)) // 如果创建设备类失败
goto ERR_CLASS_CREATE;
my_device = device_create(my_class, NULL, MKDEV(major_num, 0), NULL, DEVICE_NAME); // 创建设备
if (IS_ERR(my_device)) // 如果创建设备失败
goto ERR_DEVICE_CREATE;
printk(KERN_INFO "Device registered with major number %d\n", MAJOR(dev));
return 0;
ERR_DEVICE_CREATE:
class_destroy(my_class);
ERR_CLASS_CREATE:
cdev_del(&my_cdev);
ERR_CDEV_ADD:
kfree(msg_buffer);
ERR_KMALLOC:
unregister_chrdev_region(MKDEV(major_num, 0), 1);
return res;
}
// 驱动程序卸载函数,注销设备
static void __exit my_device_exit(void)
{
device_destroy(my_class, MKDEV(major_num, 0));
class_unregister(my_class);
class_destroy(my_class);
cdev_del(&my_cdev);
kfree(msg_buffer);
unregister_chrdev_region(MKDEV(major_num, 0), 1);
async_release(&my_async.q); // 释放异步请求队列资源
printk(KERN_INFO "Device unregistered\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("CSDN");
MODULE_DESCRIPTION("Driver for my device");
module_init(my_device_init);
module_exit(my_device_exit);
```
这是一个非常简单的基于Linux异步IO的驱动程序范例,该范例可以与用户空间应用程序交互来实现I/O操作。由于涉及异步IO操作,需要相应的理解和学习才能正确使用。
阅读全文