porting-clients 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Revision 4, 2004-03-30
  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. There are two sets of points below. The first set concerns technical
  8. changes. The second set concerns coding policy. Both are mandatory.
  9. Although reading this guide will help you porting drivers, I suggest
  10. you keep an eye on an already ported driver while porting your own
  11. driver. This will help you a lot understanding what this guide
  12. exactly means. Choose the chip driver that is the more similar to
  13. yours for best results.
  14. Technical changes:
  15. * [Includes] Get rid of "version.h". Replace <linux/i2c-proc.h> with
  16. <linux/i2c-sensor.h>. Includes typically look like that:
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-sensor.h>
  22. #include <linux/i2c-vid.h> /* if you need VRM support */
  23. #include <asm/io.h> /* if you have I/O operations */
  24. Please respect this inclusion order. Some extra headers may be
  25. required for a given driver (e.g. "lm75.h").
  26. * [Addresses] SENSORS_I2C_END becomes I2C_CLIENT_END, SENSORS_ISA_END
  27. becomes I2C_CLIENT_ISA_END.
  28. * [Client data] Get rid of sysctl_id. Try using standard names for
  29. register values (for example, temp_os becomes temp_max). You're
  30. still relatively free here, but you *have* to follow the standard
  31. names for sysfs files (see the Sysctl section below).
  32. * [Function prototypes] The detect functions loses its flags
  33. parameter. Sysctl (e.g. lm75_temp) and miscellaneous functions
  34. are off the list of prototypes. This usually leaves five
  35. prototypes:
  36. static int lm75_attach_adapter(struct i2c_adapter *adapter);
  37. static int lm75_detect(struct i2c_adapter *adapter, int address,
  38. int kind);
  39. static void lm75_init_client(struct i2c_client *client);
  40. static int lm75_detach_client(struct i2c_client *client);
  41. static void lm75_update_client(struct i2c_client *client);
  42. * [Sysctl] All sysctl stuff is of course gone (defines, ctl_table
  43. and functions). Instead, you have to define show and set functions for
  44. each sysfs file. Only define set for writable values. Take a look at an
  45. existing 2.6 driver for details (lm78 for example). Don't forget
  46. to define the attributes for each file (this is that step that
  47. links callback functions). Use the file names specified in
  48. Documentation/i2c/sysfs-interface for the individual files. Also
  49. convert the units these files read and write to the specified ones.
  50. If you need to add a new type of file, please discuss it on the
  51. sensors mailing list <lm-sensors@lm-sensors.org> by providing a
  52. patch to the Documentation/i2c/sysfs-interface file.
  53. * [Attach] For I2C drivers, the attach function should make sure
  54. that the adapter's class has I2C_CLASS_HWMON, using the
  55. following construct:
  56. if (!(adapter->class & I2C_CLASS_HWMON))
  57. return 0;
  58. ISA-only drivers of course don't need this.
  59. * [Detect] As mentioned earlier, the flags parameter is gone.
  60. The type_name and client_name strings are replaced by a single
  61. name string, which will be filled with a lowercase, short string
  62. (typically the driver name, e.g. "lm75").
  63. In i2c-only drivers, drop the i2c_is_isa_adapter check, it's
  64. useless.
  65. The errorN labels are reduced to the number needed. If that number
  66. is 2 (i2c-only drivers), it is advised that the labels are named
  67. exit and exit_free. For i2c+isa drivers, labels should be named
  68. ERROR0, ERROR1 and ERROR2. Don't forget to properly set err before
  69. jumping to error labels. By the way, labels should be left-aligned.
  70. Use memset to fill the client and data area with 0x00.
  71. Use i2c_set_clientdata to set the client data (as opposed to
  72. a direct access to client->data).
  73. Use strlcpy instead of strcpy to copy the client name.
  74. Replace the sysctl directory registration by calls to
  75. device_create_file. Move the driver initialization before any
  76. sysfs file creation.
  77. Drop client->id.
  78. * [Init] Limits must not be set by the driver (can be done later in
  79. user-space). Chip should not be reset default (although a module
  80. parameter may be used to force is), and initialization should be
  81. limited to the strictly necessary steps.
  82. * [Detach] Get rid of data, remove the call to
  83. i2c_deregister_entry.
  84. * [Update] Don't access client->data directly, use
  85. i2c_get_clientdata(client) instead.
  86. * [Interface] Init function should not print anything. Make sure
  87. there is a MODULE_LICENSE() line, at the bottom of the file
  88. (after MODULE_AUTHOR() and MODULE_DESCRIPTION(), in this order).
  89. Coding policy:
  90. * [Copyright] Use (C), not (c), for copyright.
  91. * [Debug/log] Get rid of #ifdef DEBUG/#endif constructs whenever you
  92. can. Calls to printk/pr_debug for debugging purposes are replaced
  93. by calls to dev_dbg. Here is an example on how to call it (taken
  94. from lm75_detect):
  95. dev_dbg(&client->dev, "Starting lm75 update\n");
  96. Replace other printk calls with the dev_info, dev_err or dev_warn
  97. function, as appropriate.
  98. * [Constants] Constants defines (registers, conversions, initial
  99. values) should be aligned. This greatly improves readability.
  100. Same goes for variables declarations. Alignments are achieved by the
  101. means of tabs, not spaces. Remember that tabs are set to 8 in the
  102. Linux kernel code.
  103. * [Structure definition] The name field should be standardized. All
  104. lowercase and as simple as the driver name itself (e.g. "lm75").
  105. * [Layout] Avoid extra empty lines between comments and what they
  106. comment. Respect the coding style (see Documentation/CodingStyle),
  107. in particular when it comes to placing curly braces.
  108. * [Comments] Make sure that no comment refers to a file that isn't
  109. part of the Linux source tree (typically doc/chips/<chip name>),
  110. and that remaining comments still match the code. Merging comment
  111. lines when possible is encouraged.