porting-clients 7.2 KB

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