如何写一个简单的Linux设备驱动程序? [英] How to write a simple Linux device driver?

查看:262
本文介绍了如何写一个简单的Linux设备驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写从零开始OMAP4一个SPI Linux字符设备驱动程序。
我知道编写设备驱动程序的一些基础知识。但是,我不知道如何从头开始编写特定于平台的设备驱动程序。

I need to write an SPI Linux character device driver for omap4 from scratch. I know some basics of writing device drivers. But, I don't know how to start writing platform specific device driver from scratch.

我已经写了一些基本的字符驱动,我还以为写SPI设备驱动程序将类似于它。字符驱动具有结构的file_operations 包含驱动程序实现的功能。

I've written some basic char drivers, and I thought writing SPI device driver would be similar to it. Char drivers have a structure file_operations which contains the functions implemented in the driver.

struct file_operations Fops = {
    .read = device_read,
    .write = device_write,
    .ioctl = device_ioctl,
    .open = device_open,
    .release = device_release,  /* a.k.a. close */
};

现在,我要通过 SPI-omap2- mcspi.c code作为参照得到一个想法从头开始开发SPI驱动。

Now, I am going through spi-omap2-mcspi.c code as reference to get an idea to start developing SPI driver from scratch.

不过,我没有看到功能,如打开,读,写等。
不知道从程序开始的地方。

But, I don't see functions such as open, read, write etc. Don't know from where the program starts.

请为我提供一些见解写的Linux设备驱动程序。

Kindly provide me with some insight into writing linux device drivers.

推荐答案

首先开始通过编写一个通用的内核模块。有多个地方查找信息,但我发现此链接是很有用。您通过指定那里所有的例子走了之后,你可以开始编写自己的Linux驱动程序模块。

First start by writing a generic kernel module. There are multiple places to look up for information but I found this link to be very useful. After you have gone through all examples specified there you can start writing your own Linux Driver Module.

请注意,你不会只是复制粘贴的例子code脱身,并希望它会工作,没有。内核API有时可以改变和示例将无法正常工作。只要有例子应作为指导如何做来看待。根据不同的内核版本使用的是已修改例如为了工作。

Please note, that you will not get away with just copy-pasting the example code and hope it will work, no. Kernel API can sometimes change and examples will not work. Examples provided there should be looked at as a guide how to do something. Depending on the kernel version you are using you have to modify the example in order to work.

考虑使用尽可能多的,你可以TI平台提供的功能,因为这真的可以做了很多工作,为你,就像要求,使需要的时钟,公共汽车和电源。如果我没有记错,您可以使用函数来获取直接访问寄存器内存映射地址范围。我不得不提,我有不好的经验,TI提供的功能,因为他们没有正确地释放/清理所有获取的资源,所以对于一些资源,我不得不调用其他内核服务模块卸载过程中释放他们。

Consider using TI platform provided functions as much as you can, because that can really do a lot work for you, like requesting and enabling needed clocks, buses and power supplies. If I recall correctly you can use the functions to acquire memory mapped address ranges for direct access to registers. I have to mention that I have bad experience with TI provided functions because they do not properly release/clean-up all acquired resources, so for some resources I had to call other kernel services to release them during module unload.

编辑1:

我不完全熟悉Linux SPI实现,但我会在司机看omap2_mcspi_probe()函数/ SPI / SPI-OMAP2-mcspi.c文件开始。正如你可以看到,它注册它的使用此API方法,以Linux主SPI驱动:Linux的/在include / linux / SPI / spi.h中。在对比字符驱动的主要功能在这里是* _transfer()函数。在进一步的细节spi.h中文件结构的描述抬头。另外,看看的替代设备驱动程序的API了。

I'm not entirely familiar with Linux SPI implementation but I would start by looking at omap2_mcspi_probe() function in drivers/spi/spi-omap2-mcspi.c file. As you can see there, it registers it's methods to Linux master SPI driver using this API: Linux/include/linux/spi/spi.h. In contrast to char driver the main functions here are *_transfer() functions. Look up at the struct descriptions in spi.h file for further details. Also, have a look at this alternative device driver API, too.

这篇关于如何写一个简单的Linux设备驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆