porting-clients 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Revision 6, 2005-11-20
  2. Jean Delvare <khali@linux-fr.org>
  3. Greg KH <greg@kroah.com>
  4. This is a guide on how to convert I2C chip drivers from Linux 2.4 to
  5. Linux 2.6. I have been using existing drivers (lm75, lm78) as examples.
  6. Then I converted a driver myself (lm83) and updated this document.
  7. Note that this guide is strongly oriented towards hardware monitoring
  8. drivers. Many points are still valid for other type of drivers, but
  9. others may be irrelevant.
  10. There are two sets of points below. The first set concerns technical
  11. changes. The second set concerns coding policy. Both are mandatory.
  12. Although reading this guide will help you porting drivers, I suggest
  13. you keep an eye on an already ported driver while porting your own
  14. driver. This will help you a lot understanding what this guide
  15. exactly means. Choose the chip driver that is the more similar to
  16. yours for best results.
  17. Technical changes:
  18. * [Includes] Get rid of "version.h" and <linux/i2c-proc.h>.
  19. Includes typically look like that:
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/i2c-isa.h> /* for ISA drivers */
  26. #include <linux/hwmon.h> /* for hardware monitoring drivers */
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/hwmon-vid.h> /* if you need VRM support */
  29. #include <linux/err.h> /* for class registration */
  30. #include <asm/io.h> /* if you have I/O operations */
  31. Please respect this inclusion order. Some extra headers may be
  32. required for a given driver (e.g. "lm75.h").
  33. * [Addresses] SENSORS_I2C_END becomes I2C_CLIENT_END, ISA addresses
  34. are no more handled by the i2c core. Address ranges are no more
  35. supported either, define each individual address separately.
  36. SENSORS_INSMOD_<n> becomes I2C_CLIENT_INSMOD_<n>.
  37. * [Client data] Get rid of sysctl_id. Try using standard names for
  38. register values (for example, temp_os becomes temp_max). You're
  39. still relatively free here, but you *have* to follow the standard
  40. names for sysfs files (see the Sysctl section below).
  41. * [Function prototypes] The detect functions loses its flags
  42. parameter. Sysctl (e.g. lm75_temp) and miscellaneous functions
  43. are off the list of prototypes. This usually leaves five
  44. prototypes:
  45. static int lm75_attach_adapter(struct i2c_adapter *adapter);
  46. static int lm75_detect(struct i2c_adapter *adapter, int address,
  47. int kind);
  48. static void lm75_init_client(struct i2c_client *client);
  49. static int lm75_detach_client(struct i2c_client *client);
  50. static struct lm75_data lm75_update_device(struct device *dev);
  51. * [Sysctl] All sysctl stuff is of course gone (defines, ctl_table
  52. and functions). Instead, you have to define show and set functions for
  53. each sysfs file. Only define set for writable values. Take a look at an
  54. existing 2.6 driver for details (it87 for example). Don't forget
  55. to define the attributes for each file (this is that step that
  56. links callback functions). Use the file names specified in
  57. Documentation/hwmon/sysfs-interface for the individual files. Also
  58. convert the units these files read and write to the specified ones.
  59. If you need to add a new type of file, please discuss it on the
  60. sensors mailing list <lm-sensors@lm-sensors.org> by providing a
  61. patch to the Documentation/hwmon/sysfs-interface file.
  62. * [Attach] For I2C drivers, the attach function should make sure
  63. that the adapter's class has I2C_CLASS_HWMON (or whatever class is
  64. suitable for your driver), using the following construct:
  65. if (!(adapter->class & I2C_CLASS_HWMON))
  66. return 0;
  67. ISA-only drivers of course don't need this.
  68. Call i2c_probe() instead of i2c_detect().
  69. * [Detect] As mentioned earlier, the flags parameter is gone.
  70. The type_name and client_name strings are replaced by a single
  71. name string, which will be filled with a lowercase, short string.
  72. In i2c-only drivers, drop the i2c_is_isa_adapter check, it's
  73. useless. Same for isa-only drivers, as the test would always be
  74. true. Only hybrid drivers (which are quite rare) still need it.
  75. The labels used for error paths are reduced to the number needed.
  76. It is advised that the labels are given descriptive names such as
  77. exit and exit_free. Don't forget to properly set err before
  78. jumping to error labels. By the way, labels should be left-aligned.
  79. Use kzalloc instead of kmalloc.
  80. Use i2c_set_clientdata to set the client data (as opposed to
  81. a direct access to client->data).
  82. Use strlcpy instead of strcpy or snprintf to copy the client name.
  83. Replace the sysctl directory registration by calls to
  84. device_create_file. Move the driver initialization before any
  85. sysfs file creation.
  86. Register the client with the hwmon class (using hwmon_device_register)
  87. if applicable.
  88. Drop client->id.
  89. Drop any 24RF08 corruption prevention you find, as this is now done
  90. at the i2c-core level, and doing it twice voids it.
  91. Don't add I2C_CLIENT_ALLOW_USE to client->flags, it's the default now.
  92. * [Init] Limits must not be set by the driver (can be done later in
  93. user-space). Chip should not be reset default (although a module
  94. parameter may be used to force it), and initialization should be
  95. limited to the strictly necessary steps.
  96. * [Detach] Remove the call to i2c_deregister_entry. Do not log an
  97. error message if i2c_detach_client fails, as i2c-core will now do
  98. it for you.
  99. Unregister from the hwmon class if applicable.
  100. * [Update] The function prototype changed, it is now
  101. passed a device structure, which you have to convert to a client
  102. using to_i2c_client(dev). The update function should return a
  103. pointer to the client data.
  104. Don't access client->data directly, use i2c_get_clientdata(client)
  105. instead.
  106. Use time_after() instead of direct jiffies comparison.
  107. * [Interface] Make sure there is a MODULE_LICENSE() line, at the bottom
  108. of the file (after MODULE_AUTHOR() and MODULE_DESCRIPTION(), in this
  109. order).
  110. * [Driver] The flags field of the i2c_driver structure is gone.
  111. I2C_DF_NOTIFY is now the default behavior.
  112. The i2c_driver structure has a driver member, which is itself a
  113. structure, those name member should be initialized to a driver name
  114. string. i2c_driver itself has no name member anymore.
  115. * [Driver model] Instead of shutdown or reboot notifiers, provide a
  116. shutdown() method in your driver.
  117. * [Power management] Use the driver model suspend() and resume()
  118. callbacks instead of the obsolete pm_register() calls.
  119. Coding policy:
  120. * [Copyright] Use (C), not (c), for copyright.
  121. * [Debug/log] Get rid of #ifdef DEBUG/#endif constructs whenever you
  122. can. Calls to printk for debugging purposes are replaced by calls to
  123. dev_dbg where possible, else to pr_debug. Here is an example of how
  124. to call it (taken from lm75_detect):
  125. dev_dbg(&client->dev, "Starting lm75 update\n");
  126. Replace other printk calls with the dev_info, dev_err or dev_warn
  127. function, as appropriate.
  128. * [Constants] Constants defines (registers, conversions) should be
  129. aligned. This greatly improves readability.
  130. Alignments are achieved by the means of tabs, not spaces. Remember
  131. that tabs are set to 8 in the Linux kernel code.
  132. * [Layout] Avoid extra empty lines between comments and what they
  133. comment. Respect the coding style (see Documentation/CodingStyle),
  134. in particular when it comes to placing curly braces.
  135. * [Comments] Make sure that no comment refers to a file that isn't
  136. part of the Linux source tree (typically doc/chips/<chip name>),
  137. and that remaining comments still match the code. Merging comment
  138. lines when possible is encouraged.