3. 注册SPI总线
下列函数位于drivers/spi/spi.c中。
- struct bus_type spi_bus_type = {
- .name = "spi",
- .dev_attrs = spi_dev_attrs,
- .match = spi_match_device,
- .uevent = spi_uevent,
- .suspend = spi_suspend,
- .resume = spi_resume,
- };
- EXPORT_SYMBOL_GPL(spi_bus_type);
- static struct class spi_master_class = {
- .name = "spi_master",
- .owner = THIS_MODULE,
- .dev_release = spi_master_release,
- };
- /* portable code must never pass more than 32 bytes */
- #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
- static u8 *buf;
- static int __init spi_init(void)
- {
- int status;
- buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
- if (!buf) {
- status = -ENOMEM;
- goto err0;
- }
- status = bus_register(&spi_bus_type); /*注册SPI总线*/
- if (status < 0)
- goto err1;
- status = class_register(&spi_master_class);/*注册SPI类*/
- if (status < 0)
- goto err2;
- return 0;
- err2:
- bus_unregister(&spi_bus_type);
- err1:
- kfree(buf);
- buf = NULL;
- err0:
- return status;
- }
- /* board_info is normally registered in arch_initcall(),
- * but even essential drivers wait till later
- *
- * REVISIT only boardinfo really needs static linking. the rest (device and
- * driver registration) _could_ be dynamically linked (modular) ... costs
- * include needing to have boardinfo data structures be much more public.
- */
- postcore_initcall(spi_init);
spi_init函数注册SPI总线以及SPI类到内核中。该函数在内核初始化的postcore_initcall阶段被调用。
顺便看看下总线的匹配函数。
下列函数位于drivers/spi/spi.c中。
- /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
- * and the sysfs version makes coldplug work too.
- */
- static int spi_match_device(struct device *dev, struct device_driver *drv)
- {
- const struct spi_device *spi = to_spi_device(dev);
- return strcmp(spi->modalias, drv->name) == 0;
- }
从这里我们可以看出,SPI设备和驱动的匹配是通过spi的modalias字段和driver的name字段,这两个字段相等则绑定设备和驱动。
