⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writing-clients

📁 I2C总线LINUX驱动程序
💻
📖 第 1 页 / 共 3 页
字号:
This is a small guide for those who want to write kernel drivers for I2Cor SMBus devices.To set up a driver, you need to do several things. Some are optional, andsome things can be done slightly or completely different. Use this as aguide, not as a rule book!General remarks===============Try to keep the kernel namespace as clean as possible. The best way todo this is to use a unique prefix for all global symbols. This is especially important for exported symbols, but it is a good idea to doit for non-exported symbols too. We will use the prefix `foo_' in thistutorial, and `FOO_' for preprocessor variables.The driver structure====================Usually, you will implement a single driver structure, and instantiateall clients from it. Remember, a driver structure contains general access routines, a client structure specific information like the actual I2Caddress.static struct i2c_driver foo_driver = {	.name		= "Foo version 2.3 driver",	.id		= I2C_DRIVERID_FOO, /* from i2c-id.h, optional */	.flags		= I2C_DF_NOTIFY,	.attach_adapter	= foo_attach_adapter,	.detach_client	= foo_detach_client,	.command	= foo_command, /* may be NULL */	.inc_use	= foo_inc_use, /* May be NULL */	.dec_use	= foo_dec_use, /* May be NULL */} The name can be chosen freely, and may be up to 31 characters long. Pleaseuse something descriptive here.If used, the id should be a unique ID. The range 0xf000 to 0xffff isreserved for local use, and you can use one of those until you startdistributing the driver, at which time you should contact the i2c authorsto get your own ID(s). Note that most of the time you don't need an IDat all so you can just omit it.Don't worry about the flags field; just put I2C_DF_NOTIFY into it. Thismeans that your driver will be notified when new adapters are found.This is almost always what you want.All other fields are for call-back functions which will be explained below.Module usage count==================If your driver can also be compiled as a module, there are moments at which the module can not be removed from memory. For example, when youare doing a lengthy transaction, or when you create a /proc directory,and some process has entered that directory.i2c-core and i2c-proc will take care of properly counting the usersof the client driver for the common cases. If however your driver hasfunctions used by other parts of the kernel, you will have to implementthe inc_use and dec_use callback functions and use them.To increase or decrease the module usage count, you can use theMOD_{INC,DEC}_USE_COUNT macros. They must be called from the modulewhich needs to get its usage count changed; that is why each drivermodule has to implement its own callback functions.static void foo_inc_use (struct i2c_client *client){#ifdef MODULE	MOD_INC_USE_COUNT;#endif}static void foo_dec_use (struct i2c_client *client){#ifdef MODULE	MOD_DEC_USE_COUNT;#endif}Do not call these callback functions directly; instead, use thefollowing functions defined in i2c.h:void i2c_inc_use_client(struct i2c_client *);void i2c_dec_use_client(struct i2c_client *);You should *not* increase the module count just because a device isdetected and a client created. This would make it impossible to removean adapter driver! Extra client data=================The client structure has a special `data' field that can point to anystructure at all. You can use this to keep client-specific data. Youdo not always need this, but especially for `sensors' drivers, it canbe very useful.An example structure is below.  struct foo_data {    struct i2c_client client;    struct semaphore lock; /* For ISA access in `sensors' drivers. */    int sysctl_id;         /* To keep the /proc directory entry for                               `sensors' drivers. */    enum chips type;       /* To keep the chips type for `sensors' drivers. */       /* Because the i2c bus is slow, it is often useful to cache the read       information of a chip for some time (for example, 1 or 2 seconds).       It depends of course on the device whether this is really worthwhile       or even sensible. */    struct semaphore update_lock; /* When we are reading lots of information,                                     another process should not update the                                     below information */    char valid;                   /* != 0 if the following fields are valid. */    unsigned long last_updated;   /* In jiffies */    /* Add the read information here too */  };Accessing the client====================Let's say we have a valid client structure. At some time, we will needto gather information from the client, or write new information to theclient. How we will export this information to user-space is less important at this moment (perhaps we do not need to do this at all forsome obscure clients). But we need generic reading and writing routines.I have found it useful to define foo_read and foo_write function for this.For some cases, it will be easier to call the i2c functions directly,but many chips have some kind of register-value idea that can easilybe encapsulated. Also, some chips have both ISA and I2C interfaces, andit useful to abstract from this (only for `sensors' drivers).The below functions are simple examples, and should not be copiedliterally.  int foo_read_value(struct i2c_client *client, u8 reg)  {    if (reg < 0x10) /* byte-sized register */      return i2c_smbus_read_byte_data(client,reg);    else /* word-sized register */      return i2c_smbus_read_word_data(client,reg);  }  int foo_write_value(struct i2c_client *client, u8 reg, u16 value)  {    if (reg == 0x10) /* Impossible to write - driver error! */ {      return -1;    else if (reg < 0x10) /* byte-sized register */      return i2c_smbus_write_byte_data(client,reg,value);    else /* word-sized register */      return i2c_smbus_write_word_data(client,reg,value);  }For sensors code, you may have to cope with ISA registers too. Somethinglike the below often works. Note the locking!   int foo_read_value(struct i2c_client *client, u8 reg)  {    int res;    if (i2c_is_isa_client(client)) {      down(&(((struct foo_data *) (client->data)) -> lock));      outb_p(reg,client->addr + FOO_ADDR_REG_OFFSET);      res = inb_p(client->addr + FOO_DATA_REG_OFFSET);      up(&(((struct foo_data *) (client->data)) -> lock));      return res;    } else      return i2c_smbus_read_byte_data(client,reg);  }Writing is done the same way.Probing and attaching=====================Most i2c devices can be present on several i2c addresses; for some thisis determined in hardware (by soldering some chip pins to Vcc or Ground),for others this can be changed in software (by writing to specific clientregisters). Some devices are usually on a specific address, but not always;and some are even more tricky. So you will probably need to scan severali2c addresses for your clients, and do some sort of detection to seewhether it is actually a device supported by your driver.To give the user a maximum of possibilities, some default module parametersare defined to help determine what addresses are scanned. Several macrosare defined in i2c.h to help you support them, as well as a genericdetection algorithm.You do not have to use this parameter interface; but don't try to usefunction i2c_probe() (or i2c_detect()) if you don't.NOTE: If you want to write a `sensors' driver, the interface is slightly      different! See below.Probing classes (i2c)---------------------All parameters are given as lists of unsigned 16-bit integers. Lists areterminated by I2C_CLIENT_END.The following lists are used internally:  normal_i2c: filled in by the module writer.      A list of I2C addresses which should normally be examined.   normal_i2c_range: filled in by the module writer.     A list of pairs of I2C addresses, each pair being an inclusive range of     addresses which should normally be examined.   probe: insmod parameter.      A list of pairs. The first value is a bus number (-1 for any I2C bus),      the second is the address. These addresses are also probed, as if they      were in the 'normal' list.   probe_range: insmod parameter.      A list of triples. The first value is a bus number (-1 for any I2C bus),      the second and third are addresses.  These form an inclusive range of      addresses that are also probed, as if they were in the 'normal' list.   ignore: insmod parameter.     A list of pairs. The first value is a bus number (-1 for any I2C bus),      the second is the I2C address. These addresses are never probed.      This parameter overrules 'normal' and 'probe', but not the 'force' lists.   ignore_range: insmod parameter.      A list of triples. The first value is a bus number (-1 for any I2C bus),      the second and third are addresses. These form an inclusive range of      I2C addresses that are never probed.     This parameter overrules 'normal' and 'probe', but not the 'force' lists.   force: insmod parameter.      A list of pairs. The first value is a bus number (-1 for any I2C bus),     the second is the I2C address. A device is blindly assumed to be on     the given address, no probing is done. Fortunately, as a module writer, you just have to define the `normal' and/or `normal_range' parameters. The complete declaration could looklike this:  /* Scan 0x20 to 0x2f, 0x37, and 0x40 to 0x4f */  static unsigned short normal_i2c[] = { 0x37,I2C_CLIENT_END };   static unsigned short normal_i2c_range[] = { 0x20, 0x2f, 0x40, 0x4f,                                                I2C_CLIENT_END };  /* Magic definition of all other variables and things */  I2C_CLIENT_INSMOD;Note that you *have* to call the two defined variables `normal_i2c' and`normal_i2c_range', without any prefix!Probing classes (sensors)-------------------------If you write a `sensors' driver, you use a slightly different interface.As well as I2C addresses, we have to cope with ISA addresses. Also, weuse a enum of chip types. Don't forget to include `sensors.h'.The following lists are used internally. They are all lists of integers.   normal_i2c: filled in by the module writer. Terminated by SENSORS_I2C_END.     A list of I2C addresses which should normally be examined.   normal_i2c_range: filled in by the module writer. Terminated by      SENSORS_I2C_END     A list of pairs of I2C addresses, each pair being an inclusive range of     addresses which should normally be examined.   normal_isa: filled in by the module writer. Terminated by SENSORS_ISA_END.     A list of ISA addresses which should normally be examined.   normal_isa_range: filled in by the module writer. Terminated by      SENSORS_ISA_END     A list of triples. The first two elements are ISA addresses, being an     range of addresses which should normally be examined. The third is the     modulo parameter: only addresses which are 0 module this value relative     to the first address of the range are actually considered.   probe: insmod parameter. Initialize this list with SENSORS_I2C_END values.     A list of pairs. The first value is a bus number (SENSORS_ISA_BUS for     the ISA bus, -1 for any I2C bus), the second is the address. These     addresses are also probed, as if they were in the 'normal' list.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -