upgrading-clients 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. Upgrading I2C Drivers to the new 2.6 Driver Model
  2. =================================================
  3. Ben Dooks <ben-linux@fluff.org>
  4. Introduction
  5. ------------
  6. This guide outlines how to alter existing Linux 2.6 client drivers from
  7. the old to the new new binding methods.
  8. Example old-style driver
  9. ------------------------
  10. struct example_state {
  11. struct i2c_client client;
  12. ....
  13. };
  14. static struct i2c_driver example_driver;
  15. static unsigned short ignore[] = { I2C_CLIENT_END };
  16. static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
  17. I2C_CLIENT_INSMOD;
  18. static int example_attach(struct i2c_adapter *adap, int addr, int kind)
  19. {
  20. struct example_state *state;
  21. struct device *dev = &adap->dev; /* to use for dev_ reports */
  22. int ret;
  23. state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
  24. if (state == NULL) {
  25. dev_err(dev, "failed to create our state\n");
  26. return -ENOMEM;
  27. }
  28. example->client.addr = addr;
  29. example->client.flags = 0;
  30. example->client.adapter = adap;
  31. i2c_set_clientdata(&state->i2c_client, state);
  32. strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE);
  33. ret = i2c_attach_client(&state->i2c_client);
  34. if (ret < 0) {
  35. dev_err(dev, "failed to attach client\n");
  36. kfree(state);
  37. return ret;
  38. }
  39. dev = &state->i2c_client.dev;
  40. /* rest of the initialisation goes here. */
  41. dev_info(dev, "example client created\n");
  42. return 0;
  43. }
  44. static int example_detach(struct i2c_client *client)
  45. {
  46. struct example_state *state = i2c_get_clientdata(client);
  47. i2c_detach_client(client);
  48. kfree(state);
  49. return 0;
  50. }
  51. static int example_attach_adapter(struct i2c_adapter *adap)
  52. {
  53. return i2c_probe(adap, &addr_data, example_attach);
  54. }
  55. static struct i2c_driver example_driver = {
  56. .driver = {
  57. .owner = THIS_MODULE,
  58. .name = "example",
  59. .pm = &example_pm_ops,
  60. },
  61. .attach_adapter = example_attach_adapter,
  62. .detach_client = example_detach,
  63. };
  64. Updating the client
  65. -------------------
  66. The new style binding model will check against a list of supported
  67. devices and their associated address supplied by the code registering
  68. the busses. This means that the driver .attach_adapter and
  69. .detach_client methods can be removed, along with the addr_data,
  70. as follows:
  71. - static struct i2c_driver example_driver;
  72. - static unsigned short ignore[] = { I2C_CLIENT_END };
  73. - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
  74. - I2C_CLIENT_INSMOD;
  75. - static int example_attach_adapter(struct i2c_adapter *adap)
  76. - {
  77. - return i2c_probe(adap, &addr_data, example_attach);
  78. - }
  79. static struct i2c_driver example_driver = {
  80. - .attach_adapter = example_attach_adapter,
  81. - .detach_client = example_detach,
  82. }
  83. Add the probe and remove methods to the i2c_driver, as so:
  84. static struct i2c_driver example_driver = {
  85. + .probe = example_probe,
  86. + .remove = example_remove,
  87. }
  88. Change the example_attach method to accept the new parameters
  89. which include the i2c_client that it will be working with:
  90. - static int example_attach(struct i2c_adapter *adap, int addr, int kind)
  91. + static int example_probe(struct i2c_client *client,
  92. + const struct i2c_device_id *id)
  93. Change the name of example_attach to example_probe to align it with the
  94. i2c_driver entry names. The rest of the probe routine will now need to be
  95. changed as the i2c_client has already been setup for use.
  96. The necessary client fields have already been setup before
  97. the probe function is called, so the following client setup
  98. can be removed:
  99. - example->client.addr = addr;
  100. - example->client.flags = 0;
  101. - example->client.adapter = adap;
  102. -
  103. - strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE);
  104. The i2c_set_clientdata is now:
  105. - i2c_set_clientdata(&state->client, state);
  106. + i2c_set_clientdata(client, state);
  107. The call to i2c_attach_client is no longer needed, if the probe
  108. routine exits successfully, then the driver will be automatically
  109. attached by the core. Change the probe routine as so:
  110. - ret = i2c_attach_client(&state->i2c_client);
  111. - if (ret < 0) {
  112. - dev_err(dev, "failed to attach client\n");
  113. - kfree(state);
  114. - return ret;
  115. - }
  116. Remove the storage of 'struct i2c_client' from the 'struct example_state'
  117. as we are provided with the i2c_client in our example_probe. Instead we
  118. store a pointer to it for when it is needed.
  119. struct example_state {
  120. - struct i2c_client client;
  121. + struct i2c_client *client;
  122. the new i2c client as so:
  123. - struct device *dev = &adap->dev; /* to use for dev_ reports */
  124. + struct device *dev = &i2c_client->dev; /* to use for dev_ reports */
  125. And remove the change after our client is attached, as the driver no
  126. longer needs to register a new client structure with the core:
  127. - dev = &state->i2c_client.dev;
  128. In the probe routine, ensure that the new state has the client stored
  129. in it:
  130. static int example_probe(struct i2c_client *i2c_client,
  131. const struct i2c_device_id *id)
  132. {
  133. struct example_state *state;
  134. struct device *dev = &i2c_client->dev;
  135. int ret;
  136. state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
  137. if (state == NULL) {
  138. dev_err(dev, "failed to create our state\n");
  139. return -ENOMEM;
  140. }
  141. + state->client = i2c_client;
  142. Update the detach method, by changing the name to _remove and
  143. to delete the i2c_detach_client call. It is possible that you
  144. can also remove the ret variable as it is not needed for any
  145. of the core functions.
  146. - static int example_detach(struct i2c_client *client)
  147. + static int example_remove(struct i2c_client *client)
  148. {
  149. struct example_state *state = i2c_get_clientdata(client);
  150. - i2c_detach_client(client);
  151. And finally ensure that we have the correct ID table for the i2c-core
  152. and other utilities:
  153. + struct i2c_device_id example_idtable[] = {
  154. + { "example", 0 },
  155. + { }
  156. +};
  157. +
  158. +MODULE_DEVICE_TABLE(i2c, example_idtable);
  159. static struct i2c_driver example_driver = {
  160. .driver = {
  161. .owner = THIS_MODULE,
  162. .name = "example",
  163. },
  164. + .id_table = example_ids,
  165. Our driver should now look like this:
  166. struct example_state {
  167. struct i2c_client *client;
  168. ....
  169. };
  170. static int example_probe(struct i2c_client *client,
  171. const struct i2c_device_id *id)
  172. {
  173. struct example_state *state;
  174. struct device *dev = &client->dev;
  175. state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
  176. if (state == NULL) {
  177. dev_err(dev, "failed to create our state\n");
  178. return -ENOMEM;
  179. }
  180. state->client = client;
  181. i2c_set_clientdata(client, state);
  182. /* rest of the initialisation goes here. */
  183. dev_info(dev, "example client created\n");
  184. return 0;
  185. }
  186. static int example_remove(struct i2c_client *client)
  187. {
  188. struct example_state *state = i2c_get_clientdata(client);
  189. kfree(state);
  190. return 0;
  191. }
  192. static struct i2c_device_id example_idtable[] = {
  193. { "example", 0 },
  194. { }
  195. };
  196. MODULE_DEVICE_TABLE(i2c, example_idtable);
  197. static struct i2c_driver example_driver = {
  198. .driver = {
  199. .owner = THIS_MODULE,
  200. .name = "example",
  201. .pm = &example_pm_ops,
  202. },
  203. .id_table = example_idtable,
  204. .probe = example_probe,
  205. .remove = example_remove,
  206. };