基于S3C2440的嵌入式Linux驱动——SPI子系统解读(三)
该系列文章将分为四个部分:
第三部分,即本篇文章,该文将对SPI设备驱动,也称protocol 驱动,进行讲解。
本文属于第三部分。
5. SPI设备驱动
在主控制器驱动中,spi_device已经注册了,在设备驱动中,首先要做的就是注册spi_driver,并提供用户层相应的API。
5.1 SPI设备驱动的注册
下列数据结构及函数位于drivers/spi/spidev.c。
- 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,
- .unlocked_ioctl = spidev_ioctl,
- .open = spidev_open,
- .release = spidev_release,
- };
- /* The main reason to have this class is to make mdev/udev create the
- * /dev/spidevB.C character device nodes exposing our userspace API.
- * It also simplifies memory management.
- */
- static struct class *spidev_class;
- 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.
- */
- };
- 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); /*注册字符设备,major=153*/
- if (status < 0)
- return status;
- spidev_class = class_create(THIS_MODULE, "spidev"); /*创建spidev类*/
- if (IS_ERR(spidev_class)) {
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
- return PTR_ERR(spidev_class);
- }
- status = spi_register_driver(&spidev_spi); /*注册spi_driver,并调用probe方法*/
- if (status < 0) {
- class_destroy(spidev_class);
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
- }
- return status;
- }
- module_init(spidev_init);
- static void __exit spidev_exit(void)
- {
- spi_unregister_driver(&spidev_spi); /*注销spi_driver*/
- class_destroy(spidev_class); /*注销类*/
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);/*注销字符设备*/
- }
- module_exit(spidev_exit);
该函数中,创建了一个字符设备以提供API给用户层,同时创建了一个spidev类,最后注册spi_driver到内核中。
在这里我们看到了SPI设备驱动是如何提供API给用户层的,那就是通过再熟悉不过的字符设备。通过字符设备,给用户层提供了5个API:open,release,write,read和ioctl。本文在后面将介绍open和close,剩余3个将在本系列的第四篇文章中介绍。
接着看下spi_register_driver函数, 该函数位于drivers/spi/spidev.c。
- /**
- * spi_register_driver - register a SPI driver
- * @sdrv: the driver to register
- * Context: can sleep
- */
- int spi_register_driver(struct spi_driver *sdrv)
- {
- sdrv->driver.bus = &spi_bus_type;
- if (sdrv->probe)
- sdrv->driver.probe = spi_drv_probe;
- if (sdrv->remove)
- sdrv->driver.remove = spi_drv_remove;
- if (sdrv->shutdown)
- sdrv->driver.shutdown = spi_drv_shutdown;
- return driver_register(&sdrv->driver);
- }
- EXPORT_SYMBOL_GPL(spi_register_driver);
在调用driver_register的过程中,将用driver.name和spi_device的modalias字段进行比较,两者相等则将该spi_driver和spi_device进行绑定。
当spi_driver注册成功以后,将调用probe方法:spidev_probe函数。
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20