📄 writing-clients
字号:
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 = { .owner = THIS_MODULE, .name = "Foo version 2.3 driver", .id = I2C_DRIVERID_FOO, /* usually from i2c-id.h */ .flags = I2C_DF_NOTIFY, .attach_adapter = &foo_attach_adapter, .detach_client = &foo_detach_client, .command = &foo_command /* may be NULL */ } The name can be chosen freely, and may be upto 40 characters long. Pleaseuse something descriptive here.The id should be a unique ID. The range 0xf000 to 0xffff is reserved forlocal use, and you can use one of those until you start distributing thedriver. Before you do that, contact the i2c authors to get your own ID(s).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.There use to be two additional fields in this structure, inc_use et dec_use,for module usage count, but these fields were obsoleted and removed.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 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. probe_range: insmod parameter. Initialize this list with SENSORS_I2C_END values. A list of triples. The first value is a bus number (SENSORS_ISA_BUS for the ISA bus, -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. 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 I2C address. These addresses are never probed. This parameter overrules 'normal' and 'probe', but not the 'force' lists. ignore_range: insmod parameter. Initialize this list with SENSORS_I2C_END values. A list of triples. The first value is a bus number (SENSORS_ISA_BUS for the ISA bus, -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.Also used is a list of pointers to sensors_force_data structures: force_data: insmod parameters. A list, ending with an element of which the force field is NULL. Each element contains the type of chip and 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 are automatically translated to insmod variables of the form force_foo.So we have a generic insmod variabled `force', and chip-specific variables`force_CHIPNAME'.Fortunately, as a module writer, you just have to define the `normal' and/or `normal_range' parameters, and define what chip names are used. The complete declaration could look like this: /* Scan i2c addresses 0x20 to 0x2f, 0x37, and 0x40 to 0x4f static unsigned short normal_i2c[] = {0x37,SENSORS_I2C_END};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -