目录
一 前言
二 驱动开发步骤分析
1驱动模块都有两个函数:
2实现初始化函数和退出函数;
3初始化函数分析
3.1通过函数register_chrdev()注册如下结构体
3.2通过设备类函数class_register()注册
3.3调用spi_register_driver()
4退出函数分析注销SPI驱动、注销SPI设备类、注销SPI字符设备
5具体底层函数实现
三 总结
分析Linux驱动代码结构,SPI设备驱动举例
module_init(spidev_init);
module_exit(spidev_exit);
static int __init spidev_init(void)
{
int status;
/* Claim our 256 reserved device numbers. Then register a class
* that will key udev/mdev to add/remove /dev nodes. Last, register
* the driver which manages those device numbers.
*/
BUILD_BUG_ON(N_SPI_MINORS > 256);
status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
if (status < 0)
return status;
status = class_register(&spidev_class);
if (status < 0) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
return status;
}
status = spi_register_driver(&spidev_spi);
if (status < 0) {
class_unregister(&spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
}
return status;
}
static void __exit spidev_exit(void)
{
spi_unregister_driver(&spidev_spi);
class_unregister(&spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
}
static struct file_operations spidev_fops = {
.owner = THIS_MODULE,
/* REVISIT switch to aio primitives, so that userspace
* gets more complete API coverage. It'll simplify things
* too, except for the locking.
*/
.write = spidev_write,
.read = spidev_read,
.ioctl = spidev_ioctl,
.open = spidev_open,
.release = spidev_release,
};
static struct class spidev_class = {
.name = "spidev",
.owner = THIS_MODULE,
.dev_release = spidev_classdev_release,
};
static struct spi_driver spidev_spi = {
.driver = {
.name = "spidev",
.owner = THIS_MODULE,
},
.probe = spidev_probe,
.remove = __devexit_p(spidev_remove),
/* NOTE: suspend/resume methods are not necessary here.
* We don't do anything except pass the requests to/from
* the underlying controller. The refrigerator handles
* most issues; the controller driver handles the rest.
*/
};
spi_unregister_driver(&spidev_spi);
class_unregister(&spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
spidev_write,
spidev_read,
spidev_ioctl,
spidev_open,
spidev_release,
了解驱动设备实现流程,调用关系;