platform_find_device_by_driver
时间: 2023-08-05 17:06:44 浏览: 85
`platform_find_device_by_driver`是一个函数,用于在平台设备列表中查找与给定驱动程序匹配的设备。它用于在Linux内核中查找与指定驱动程序匹配的平台设备,并返回找到的设备的指针。
这个函数通常在驱动程序的probe函数中使用,用于在设备与驱动匹配成功后,查找对应的设备实例进行进一步的操作。它接受一个指向`platform_driver`结构体的指针作为参数,该结构体包含了驱动程序的名称和其他属性。
如果找到匹配的设备,函数将返回对应的设备指针;如果没有找到匹配的设备,函数将返回NULL。
相关问题
AttributeError: 'WebDriver' object has no attribute 'find_element_by_android_uiautomator'
这个错误通常是由于WebDriver对象没有找到指定的元素而引起的。在这种情况下,'find_element_by_android_uiautomator'方法可能不适用于当前的WebDriver对象。这可能是因为WebDriver对象没有正确初始化或没有正确导入所需的库。
以下是一些可能的解决方法:
1. 确保你已经正确地初始化了WebDriver对象,并且已经导入了所需的库。
2. 确保你正在使用正确的方法来查找元素。在这种情况下,你应该使用'find_element_by_android_uiautomator'方法而不是'find_elements_by_xpath'方法。
3. 确保你正在使用正确的版本的WebDriver。如果你正在使用旧版本的WebDriver,则可能会出现此错误。
4. 确保你的代码没有拼写错误或语法错误。这些错误可能会导致WebDriver对象无法正确识别元素。
以下是一个使用'find_element_by_android_uiautomator'方法查找元素的示例:
```python
from appium import webdriver
desired_caps = {
'platformName': 'Android',
'platformVersion': '8.0',
'deviceName': 'Android Emulator',
'appPackage': 'com.android.settings',
'appActivity': '.Settings'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 使用'find_element_by_android_uiautomator'方法查找元素
element = driver.find_element_by_android_uiautomator('new UiSelector().text("Display")')
# 输出元素的文本
print(element.text)
driver.quit()
```
用platform bus driver的方式来写上述程序 ,给出设备树配置
好的,以下是使用platform bus driver的方式编写Winbond SPI Flash驱动程序的示例代码,以及相应的设备树配置。
首先,创建一个名为“winbond-spi-flash”或类似的设备节点,并将其添加到设备树中。以下是一个简单的设备树配置示例:
```dts
&spi0 {
winbond_spi_flash: winbond-spi-flash@0 {
compatible = "winbond,w25q64";
reg = <0>;
spi-max-frequency = <100000000>;
};
};
```
在驱动程序中,您需要实现probe和remove函数,用于初始化和清理设备资源。以下是一个简单的驱动程序示例:
```c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#define WINBOND_CMD_READ_ID 0x9f
#define WINBOND_CMD_READ_DATA 0x03
static struct spi_device *winbond_spi_device;
static int winbond_spi_probe(struct platform_device *pdev)
{
int ret;
struct device *dev = &pdev->dev;
u8 id[3];
printk(KERN_INFO "winbond_spi: probe\n");
// get the SPI device pointer
winbond_spi_device = dev_get_drvdata(dev);
// read the Winbond Flash ID
struct spi_transfer transfer = {
.tx_buf = &WINBOND_CMD_READ_ID,
.rx_buf = id,
.len = 3,
};
struct spi_message message;
spi_message_init(&message);
spi_message_add_tail(&transfer, &message);
ret = spi_sync(winbond_spi_device, &message);
if (ret < 0) {
printk(KERN_ERR "winbond_spi: failed to read Winbond Flash ID\n");
return ret;
}
printk(KERN_INFO "winbond_spi: Winbond Flash ID: %02x %02x %02x\n",
id[0], id[1], id[2]);
return 0;
}
static int winbond_spi_remove(struct platform_device *pdev)
{
printk(KERN_INFO "winbond_spi: remove\n");
return 0;
}
static struct platform_driver winbond_spi_driver = {
.driver = {
.name = "winbond-spi-flash",
},
.probe = winbond_spi_probe,
.remove = winbond_spi_remove,
};
static int winbond_spi_probe(struct spi_device *spi)
{
return 0;
}
static int winbond_spi_remove(struct spi_device *spi)
{
return 0;
}
static struct spi_driver winbond_spi_driver = {
.driver = {
.name = "winbond-spi-flash",
},
.probe = winbond_spi_probe,
.remove = winbond_spi_remove,
};
static int __init winbond_spi_init(void)
{
int ret;
struct spi_master *master;
struct platform_device *pdev;
printk(KERN_INFO "winbond_spi: init\n");
// find the SPI master controller
master = spi_busnum_to_master(0);
if (!master) {
printk(KERN_ERR "winbond_spi: failed to find SPI master controller\n");
return -ENODEV;
}
// register the SPI driver
ret = spi_register_driver(&winbond_spi_driver);
if (ret < 0) {
printk(KERN_ERR "winbond_spi: failed to register SPI driver\n");
return ret;
}
// create the platform device
pdev = platform_device_alloc("winbond-spi-flash", 0);
if (!pdev) {
printk(KERN_ERR "winbond_spi: failed to allocate platform device\n");
return -ENOMEM;
}
platform_device_add(pdev);
return 0;
}
static void __exit winbond_spi_exit(void)
{
printk(KERN_INFO "winbond_spi: exit\n");
// remove the platform device
platform_device_unregister(&winbond_spi_device->dev);
// unregister the SPI driver
spi_unregister_driver(&winbond_spi_driver);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Winbond SPI Flash Driver");
module_init(winbond_spi_init);
module_exit(winbond_spi_exit);
```
在该驱动程序中,probe函数用于初始化SPI总线并读取Winbond Flash的ID。在remove函数中,您可以清理和释放设备资源。
需要注意的是,驱动程序中的设备树配置和平台驱动程序名称需要匹配。在本例中,设备树中的节点名称为“winbond-spi-flash”,而平台驱动程序的名称为“winbond-spi-flash”。