upgrading-clients 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. },
  60. .attach_adapter = example_attach_adapter,
  61. .detach_client = example_detach,
  62. .suspend = example_suspend,
  63. .resume = example_resume,
  64. };
  65. Updating the client
  66. -------------------
  67. The new style binding model will check against a list of supported
  68. devices and their associated address supplied by the code registering
  69. the busses. This means that the driver .attach_adapter and
  70. .detach_client methods can be removed, along with the addr_data,
  71. as follows:
  72. - static struct i2c_driver example_driver;
  73. - static unsigned short ignore[] = { I2C_CLIENT_END };
  74. - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
  75. - I2C_CLIENT_INSMOD;
  76. - static int example_attach_adapter(struct i2c_adapter *adap)
  77. - {
  78. - return i2c_probe(adap, &addr_data, example_attach);
  79. - }
  80. static struct i2c_driver example_driver = {
  81. - .attach_adapter = example_attach_adapter,
  82. - .detach_client = example_detach,
  83. }
  84. Add the probe and remove methods to the i2c_driver, as so:
  85. static struct i2c_driver example_driver = {
  86. + .probe = example_probe,
  87. + .remove = example_remove,
  88. }
  89. Change the example_attach method to accept the new parameters
  90. which include the i2c_client that it will be working with:
  91. - static int example_attach(struct i2c_adapter *adap, int addr, int kind)
  92. + static int example_probe(struct i2c_client *client,
  93. + const struct i2c_device_id *id)
  94. Change the name of example_attach to example_probe to align it with the
  95. i2c_driver entry names. The rest of the probe routine will now need to be
  96. changed as the i2c_client has already been setup for use.
  97. The necessary client fields have already been setup before
  98. the probe function is called, so the following client setup
  99. can be removed:
  100. - example->client.addr = addr;
  101. - example->client.flags = 0;
  102. - example->client.adapter = adap;
  103. -
  104. - strlcpy(client->i2c_client.name, "example", I2C_NAME_SIZE);
  105. The i2c_set_clientdata is now:
  106. - i2c_set_clientdata(&state->client, state);
  107. + i2c_set_clientdata(client, state);
  108. The call to i2c_attach_client is no longer needed, if the probe
  109. routine exits successfully, then the driver will be automatically
  110. attached by the core. Change the probe routine as so:
  111. - ret = i2c_attach_client(&state->i2c_client);
  112. - if (ret < 0) {
  113. - dev_err(dev, "failed to attach client\n");
  114. - kfree(state);
  115. - return ret;
  116. - }
  117. Remove the storage of 'struct i2c_client' from the 'struct example_state'
  118. as we are provided with the i2c_client in our example_probe. Instead we
  119. store a pointer to it for when it is needed.
  120. struct example_state {
  121. - struct i2c_client client;
  122. + struct i2c_client *client;
  123. the new i2c client as so:
  124. - struct device *dev = &adap->dev; /* to use for dev_ reports */
  125. + struct device *dev = &i2c_client->dev; /* to use for dev_ reports */
  126. And remove the change after our client is attached, as the driver no
  127. longer needs to register a new client structure with the core:
  128. - dev = &state->i2c_client.dev;
  129. In the probe routine, ensure that the new state has the client stored
  130. in it:
  131. static int example_probe(struct i2c_client *i2c_client,
  132. const struct i2c_device_id *id)
  133. {
  134. struct example_state *state;
  135. struct device *dev = &i2c_client->dev;
  136. int ret;
  137. state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
  138. if (state == NULL) {
  139. dev_err(dev, "failed to create our state\n");
  140. return -ENOMEM;
  141. }
  142. + state->client = i2c_client;
  143. Update the detach method, by changing the name to _remove and
  144. to delete the i2c_detach_client call. It is possible that you
  145. can also remove the ret variable as it is not not needed for
  146. any of the core functions.
  147. - static int example_detach(struct i2c_client *client)
  148. + static int example_remove(struct i2c_client *client)
  149. {
  150. struct example_state *state = i2c_get_clientdata(client);
  151. - i2c_detach_client(client);
  152. And finally ensure that we have the correct ID table for the i2c-core
  153. and other utilities:
  154. + struct i2c_device_id example_idtable[] = {
  155. + { "example", 0 },
  156. + { }
  157. +};
  158. +
  159. +MODULE_DEVICE_TABLE(i2c, example_idtable);
  160. static struct i2c_driver example_driver = {
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = "example",
  164. },
  165. + .id_table = example_ids,
  166. Our driver should now look like this:
  167. struct example_state {
  168. struct i2c_client *client;
  169. ....
  170. };
  171. static int example_probe(struct i2c_client *client,
  172. const struct i2c_device_id *id)
  173. {
  174. struct example_state *state;
  175. struct device *dev = &client->dev;
  176. state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
  177. if (state == NULL) {
  178. dev_err(dev, "failed to create our state\n");
  179. return -ENOMEM;
  180. }
  181. state->client = client;
  182. i2c_set_clientdata(client, state);
  183. /* rest of the initialisation goes here. */
  184. dev_info(dev, "example client created\n");
  185. return 0;
  186. }
  187. static int example_remove(struct i2c_client *client)
  188. {
  189. struct example_state *state = i2c_get_clientdata(client);
  190. kfree(state);
  191. return 0;
  192. }
  193. static struct i2c_device_id example_idtable[] = {
  194. { "example", 0 },
  195. { }
  196. };
  197. MODULE_DEVICE_TABLE(i2c, example_idtable);
  198. static struct i2c_driver example_driver = {
  199. .driver = {
  200. .owner = THIS_MODULE,
  201. .name = "example",
  202. },
  203. .id_table = example_idtable,
  204. .probe = example_probe,
  205. .remove = example_remove,
  206. .suspend = example_suspend,
  207. .resume = example_resume,
  208. };