i2c驱动看下来感觉就像一张找不到头的网,繁复错杂,千丝万缕,很难理清。这还只是一个简单的子系统,还有复杂的USB总线,更别提内核的复杂了,哎,路漫漫其修远兮...anyway,加油!

函数指针和链表在驱动中随处可见,这些也是C语言的精华所在,看着都觉费力,什么时候才能设计出这种高水平的代码来...

1 结构体内容

1.1 struct i2c_adapter

i2c适配器的描述结构体。
/** i2c_adapter is the structure used to identify a physical i2c bus along* with the access algorithms necessary to access it.*/
struct i2c_adapter {struct module *owner;         /*该结构体所属模块*/unsigned int id __deprecated;unsigned int class;        /* classes to allow probing for */const struct i2c_algorithm *algo; /* the algorithm to access the bus */void *algo_data;/* data fields that are valid for all devices    */struct rt_mutex bus_lock;   /*控制并发访问的自旋锁*/int timeout;              /* in jiffies */int retries;            /*重试次数*/struct device dev;          /* the adapter device */int nr;char name[48];           /*适配器的名称*/struct completion dev_released;   /*完成量,用于同步*/struct mutex userspace_clients_lock;struct list_head userspace_clients;  /*client链表头*/
};

1.2 struct i2c_algorithm

i2c适配器的通信接口结构体
/** The following structs are for those who like to implement new bus drivers:* i2c_algorithm is the interface to a class of hardware solutions which can* be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584* to name two of the most common.*/
struct i2c_algorithm {/* If an adapter algorithm can't do I2C-level access, set master_xferto NULL. If an adapter algorithm can do SMBus access, setsmbus_xfer. If set to NULL, the SMBus protocol is simulatedusing common I2C messages *//* master_xfer should return the number of messages successfullyprocessed, or a negative value on error */int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,int num);                       /*i2c传输函数指针*/int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,unsigned short flags, char read_write,u8 command, int size, union i2c_smbus_data *data); /*smbus传输函数指针*//* To determine what the adapter supports */u32 (*functionality) (struct i2c_adapter *);             /*适配器支持的功能*/
};

1.3 struct i2c_driver

/*** struct i2c_driver - represent an I2C device driver* @class: What kind of i2c device we instantiate (for detect)* @attach_adapter: Callback for bus addition (for legacy drivers)* @detach_adapter: Callback for bus removal (for legacy drivers)* @probe: Callback for device binding* @remove: Callback for device unbinding* @shutdown: Callback for device shutdown* @suspend: Callback for device suspend* @resume: Callback for device resume* @alert: Alert callback, for example for the SMBus alert protocol* @command: Callback for bus-wide signaling (optional)* @driver: Device driver model driver* @id_table: List of I2C devices supported by this driver* @detect: Callback for device detection* @address_list: The I2C addresses to probe (for detect)* @clients: List of detected clients we created (for i2c-core use only)** The driver.owner field should be set to the module owner of this driver.* The driver.name field should be set to the name of this driver.** For automatic device detection, both @detect and @address_data must* be defined. @class should also be set, otherwise only devices forced* with module parameters will be created. The detect function must* fill at least the name field of the i2c_board_info structure it is* handed upon successful detection, and possibly also the flags field.** If @detect is missing, the driver will still work fine for enumerated* devices. Detected devices simply won't be supported. This is expected* for the many I2C/SMBus devices which can't be detected reliably, and* the ones which can always be enumerated in practice.** The i2c_client structure which is handed to the @detect callback is* not a real i2c_client. It is initialized just enough so that you can* call i2c_smbus_read_byte_data and friends on it. Don't do anything* else with it. In particular, calling dev_dbg and friends on it is* not allowed.*/
struct i2c_driver {unsigned int class;/* Notifies the driver that a new bus has appeared or is about to be* removed. You should avoid using this if you can, it will probably* be removed in a near future.*/int (*attach_adapter)(struct i2c_adapter *);int (*detach_adapter)(struct i2c_adapter *);/* Standard driver model interfaces */int (*probe)(struct i2c_client *, const struct i2c_device_id *);int (*remove)(struct i2c_client *);/* driver model interfaces that don't relate to enumeration  */void (*shutdown)(struct i2c_client *);int (*suspend)(struct i2c_client *, pm_message_t mesg);int (*resume)(struct i2c_client *);/* Alert callback, for example for the SMBus alert protocol.* The format and meaning of the data value depends on the protocol.* For the SMBus alert protocol, there is a single bit of data passed* as the alert response's low bit ("event flag").*/void (*alert)(struct i2c_client *, unsigned int data);/* a ioctl like command that can be used to perform specific functions* with the device.*/int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);struct device_driver driver;const struct i2c_device_id *id_table;/* Device detection callback for automatic device creation */int (*detect)(struct i2c_client *, struct i2c_board_info *);const unsigned short *address_list;struct list_head clients;
};

1.4 struct i2c_client

/*** struct i2c_client - represent an I2C slave device* @flags: I2C_CLIENT_TEN indicates the device uses a ten bit chip address;* I2C_CLIENT_PEC indicates it uses SMBus Packet Error Checking* @addr: Address used on the I2C bus connected to the parent adapter.* @name: Indicates the type of the device, usually a chip name that's*  generic enough to hide second-sourcing and compatible revisions.* @adapter: manages the bus segment hosting this I2C device* @driver: device's driver, hence pointer to access routines* @dev: Driver model device node for the slave.* @irq: indicates the IRQ generated by this device (if any)* @detected: member of an i2c_driver.clients list or i2c-core's*    userspace_devices list** An i2c_client identifies a single device (i.e. chip) connected to an* i2c bus. The behaviour exposed to Linux is defined by the driver* managing the device.*/
struct i2c_client {unsigned short flags;        /* div., see below      */unsigned short addr;      /* chip address - NOTE: 7bit    *//* addresses are stored in the    *//* _LOWER_ 7 bits     */char name[I2C_NAME_SIZE];struct i2c_adapter *adapter; /* the adapter we sit on    */struct i2c_driver *driver;    /* and our access routines  */struct device dev;        /* the device structure     */int irq;          /* irq issued by device     */struct list_head detected;
};

2 结构体之间的关系

2.1 struct i2c_adapter与struct i2c_algorithm

i2c_adapter对应物理上的一个适配器,适配器都有相应的通信函数以搭建最底层的数据传输及控制通道,i2c_algorithm就实现了这种功能,可以在i2c_adapter中找到i2c_algorithm结构体指针,该指针就指向适配器对应的通信实现函数(主要是master_xfer)。
传输函数以struct i2c_msg结构体作为传输单位,i2c的传输就是要填充该结构体的成员。结构体如下:
struct i2c_msg {__u16 addr;    /* slave address            */__u16 flags;
#define I2C_M_TEN       0x0010  /* this is a ten bit chip address */
#define I2C_M_RD        0x0001  /* read data, from slave to master */
#define I2C_M_NOSTART       0x4000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR  0x2000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK    0x1000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK     0x0800  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_RECV_LEN      0x0400  /* length will be first received byte */__u16 len;      /* msg length               */__u8 *buf;        /* pointer to msg data          */
};

2.2 struct i2c_client与struct i2c_driver

i2c_client对应一个物理的i2c设备,i2c_driver则是i2c设备对应的驱动。在i2c_driver注册时,attach_adapter()函数运行并探测设备,若找到client,会将i2c_client结构体中的adapter指针指向对应的i2c_adapter,driver指针指向该i2c_driver,并调用i2c_adapter中的client_register()函数。

2.3 struct i2c_adapter与struct i2c_client

i2c_client依附于i2c_adapter,一个适配器可以连接多个i2c设备,所以在i2c_adapter结构体中定义了依附于它的i2c_client链表。

linux i2c驱动相关结构体相关推荐

  1. Linux下网络相关结构体 struct servent

    Linux下网络相关结构体 struct servent 参考书籍:<UNIX环境高级编程> 参考链接: http://www.cnblogs.com/benxintuzi/p/45898 ...

  2. Linux下网络相关结构体 struct addrinfo

    参考书籍:<UNIX环境高级编程> 参考连接: http://www.cnblogs.com/benxintuzi/p/4589819.html 一.简介 结构体定义如下: struct ...

  3. linux I2C 驱动

    原文地址: http://hello2mao.github.io/2015/12/02/Linux_I2C_driver.html 目录 一.LinuxI2C驱动--概述 1.1 写在前面 1.2 I ...

  4. 【正点原子MP157连载】第四十章 Linux I2C驱动实验-摘自【正点原子】STM32MP1嵌入式Linux驱动开发指南V1.7

    1)实验平台:正点原子STM32MP157开发板 2)购买链接:https://item.taobao.com/item.htm?&id=629270721801 3)全套实验源码+手册+视频 ...

  5. 《linux设备驱动开发详解》笔记——15 linux i2c驱动

    <linux设备驱动开发详解>笔记--15 linux i2c驱动 15.1 总体结构 如下图,i2c驱动分为如下几个重要模块 核心层core,完成i2c总线.设备.驱动模型,对用户提供s ...

  6. 【正点原子Linux连载】第六十一章 Linux I2C驱动实验 -摘自【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.0

    1)实验平台:正点原子阿尔法Linux开发板 2)平台购买地址:https://item.taobao.com/item.htm?id=603672744434 2)全套实验源码+手册+视频下载地址: ...

  7. STM32MP157驱动开发——Linux I2C驱动

    相关文章:正点原子教程第四十章--Linux I2C驱动实验 0.前言   为了简化笔记的编写以及降低工作量,本节开始相关的基础知识部分通过引入原子哥的教材链接来完成,有兴趣的可以进入学习.   上一 ...

  8. linux I2C驱动架构解析

    I2C 概述 I2C是philips提出的外设总线. I2C只有两条线,一条串行数据线:SDA,一条是时钟线SCL ,使用SCL,SDA这两根信号线就实现了设备之间的数据交互,它方便了工程师的布线. ...

  9. Linux I2C驱动

    介绍:I2C是常见的一个串行通信接口,用于连接各种外设.传感器等.I2C 总线仅仅使用 SCL.SDA 这两根信号线就实现了设备之间的数据交互. I2C的体系架构:由I2C核心,I2C总线驱动,I2C ...

最新文章

  1. 多线程、并发及线程的基础问题
  2. MPB:西农焦硕组-微生物生物地理学研究方法
  3. Javascript中的prototype
  4. 台湾高校首创气体灭火数位实境教育馆
  5. 《C++编程风格(修订版)》——2.5 动态内存的一致性
  6. 【STM32】系统配置控制器相关函数和类型
  7. Python学习笔记(随机数)
  8. laravel没有route.php,Laravel中的RouteCollection.php中的NotFoundHttpException
  9. Numpy and Theano broadcasting
  10. byte[]与Image Image与 byte[] 之间的转换
  11. 在win10系统下安装ubuntu17.10以及基本配置
  12. 3.sf2 核心目录及文件结构
  13. asp.net中如何解决4M以上文件的上传
  14. 根据key找到JSON字符串中指定的value值(Java实现)
  15. android toast防重_安卓Toast自定义及防止重复显示
  16. 常用开关稳压电源(LM2596、MC3406)
  17. Unix/Linux环境C编程入门教程(14) Mandriva LinuxCCPP开发环境搭建
  18. 聚类算法Kmens和密度峰值聚类
  19. 如何通过数据驱动业务发展
  20. 关闭 mysql redo_MySQL redo log总结

热门文章

  1. Echarts——legend前面的小圆点
  2. 面试题:SQL语句的执行顺序
  3. IT知识免费学习视频地址大全
  4. 【语音编码】基于matlab ADPCM编解码(Matlab代码实现)
  5. 【洞见研报】研报速读:东兴证券——CPU生态价值与机遇研究(CPU,构架,国产替代)
  6. 【数学建模-某肿瘤疾病诊疗的经济学分析】数据分析
  7. 图片搜索淘宝商品接口
  8. 关于MEMS的技术简介
  9. java mock 数据库_Java-Mock简化单元测试
  10. 社区团购,究竟是砸人饭碗,还是提升效率?