convert_drivers_to_kernel_api.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. Converting old watchdog drivers to the watchdog framework
  2. by Wolfram Sang <w.sang@pengutronix.de>
  3. =========================================================
  4. Before the watchdog framework came into the kernel, every driver had to
  5. implement the API on its own. Now, as the framework factored out the common
  6. components, those drivers can be lightened making it a user of the framework.
  7. This document shall guide you for this task. The necessary steps are described
  8. as well as things to look out for.
  9. Remove the file_operations struct
  10. ---------------------------------
  11. Old drivers define their own file_operations for actions like open(), write(),
  12. etc... These are now handled by the framework and just call the driver when
  13. needed. So, in general, the 'file_operations' struct and assorted functions can
  14. go. Only very few driver-specific details have to be moved to other functions.
  15. Here is a overview of the functions and probably needed actions:
  16. - open: Everything dealing with resource management (file-open checks, magic
  17. close preparations) can simply go. Device specific stuff needs to go to the
  18. driver specific start-function. Note that for some drivers, the start-function
  19. also serves as the ping-function. If that is the case and you need start/stop
  20. to be balanced (clocks!), you are better off refactoring a separate start-function.
  21. - close: Same hints as for open apply.
  22. - write: Can simply go, all defined behaviour is taken care of by the framework,
  23. i.e. ping on write and magic char ('V') handling.
  24. - ioctl: While the driver is allowed to have extensions to the IOCTL interface,
  25. the most common ones are handled by the framework, supported by some assistance
  26. from the driver:
  27. WDIOC_GETSUPPORT:
  28. Returns the mandatory watchdog_info struct from the driver
  29. WDIOC_GETSTATUS:
  30. Needs the status-callback defined, otherwise returns 0
  31. WDIOC_GETBOOTSTATUS:
  32. Needs the bootstatus member properly set. Make sure it is 0 if you
  33. don't have further support!
  34. WDIOC_SETOPTIONS:
  35. No preparations needed
  36. WDIOC_KEEPALIVE:
  37. If wanted, options in watchdog_info need to have WDIOF_KEEPALIVEPING
  38. set
  39. WDIOC_SETTIMEOUT:
  40. Options in watchdog_info need to have WDIOF_SETTIMEOUT set
  41. and a set_timeout-callback has to be defined. The core will also
  42. do limit-checking, if min_timeout and max_timeout in the watchdog
  43. device are set. All is optional.
  44. WDIOC_GETTIMEOUT:
  45. No preparations needed
  46. WDIOC_GETTIMELEFT:
  47. It needs get_timeleft() callback to be defined. Otherwise it
  48. will return EOPNOTSUPP
  49. Other IOCTLs can be served using the ioctl-callback. Note that this is mainly
  50. intended for porting old drivers; new drivers should not invent private IOCTLs.
  51. Private IOCTLs are processed first. When the callback returns with
  52. -ENOIOCTLCMD, the IOCTLs of the framework will be tried, too. Any other error
  53. is directly given to the user.
  54. Example conversion:
  55. -static const struct file_operations s3c2410wdt_fops = {
  56. - .owner = THIS_MODULE,
  57. - .llseek = no_llseek,
  58. - .write = s3c2410wdt_write,
  59. - .unlocked_ioctl = s3c2410wdt_ioctl,
  60. - .open = s3c2410wdt_open,
  61. - .release = s3c2410wdt_release,
  62. -};
  63. Check the functions for device-specific stuff and keep it for later
  64. refactoring. The rest can go.
  65. Remove the miscdevice
  66. ---------------------
  67. Since the file_operations are gone now, you can also remove the 'struct
  68. miscdevice'. The framework will create it on watchdog_dev_register() called by
  69. watchdog_register_device().
  70. -static struct miscdevice s3c2410wdt_miscdev = {
  71. - .minor = WATCHDOG_MINOR,
  72. - .name = "watchdog",
  73. - .fops = &s3c2410wdt_fops,
  74. -};
  75. Remove obsolete includes and defines
  76. ------------------------------------
  77. Because of the simplifications, a few defines are probably unused now. Remove
  78. them. Includes can be removed, too. For example:
  79. - #include <linux/fs.h>
  80. - #include <linux/miscdevice.h> (if MODULE_ALIAS_MISCDEV is not used)
  81. - #include <linux/uaccess.h> (if no custom IOCTLs are used)
  82. Add the watchdog operations
  83. ---------------------------
  84. All possible callbacks are defined in 'struct watchdog_ops'. You can find it
  85. explained in 'watchdog-kernel-api.txt' in this directory. start(), stop() and
  86. owner must be set, the rest are optional. You will easily find corresponding
  87. functions in the old driver. Note that you will now get a pointer to the
  88. watchdog_device as a parameter to these functions, so you probably have to
  89. change the function header. Other changes are most likely not needed, because
  90. here simply happens the direct hardware access. If you have device-specific
  91. code left from the above steps, it should be refactored into these callbacks.
  92. Here is a simple example:
  93. +static struct watchdog_ops s3c2410wdt_ops = {
  94. + .owner = THIS_MODULE,
  95. + .start = s3c2410wdt_start,
  96. + .stop = s3c2410wdt_stop,
  97. + .ping = s3c2410wdt_keepalive,
  98. + .set_timeout = s3c2410wdt_set_heartbeat,
  99. +};
  100. A typical function-header change looks like:
  101. -static void s3c2410wdt_keepalive(void)
  102. +static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
  103. {
  104. ...
  105. +
  106. + return 0;
  107. }
  108. ...
  109. - s3c2410wdt_keepalive();
  110. + s3c2410wdt_keepalive(&s3c2410_wdd);
  111. Add the watchdog device
  112. -----------------------
  113. Now we need to create a 'struct watchdog_device' and populate it with the
  114. necessary information for the framework. The struct is also explained in detail
  115. in 'watchdog-kernel-api.txt' in this directory. We pass it the mandatory
  116. watchdog_info struct and the newly created watchdog_ops. Often, old drivers
  117. have their own record-keeping for things like bootstatus and timeout using
  118. static variables. Those have to be converted to use the members in
  119. watchdog_device. Note that the timeout values are unsigned int. Some drivers
  120. use signed int, so this has to be converted, too.
  121. Here is a simple example for a watchdog device:
  122. +static struct watchdog_device s3c2410_wdd = {
  123. + .info = &s3c2410_wdt_ident,
  124. + .ops = &s3c2410wdt_ops,
  125. +};
  126. Handle the 'nowayout' feature
  127. -----------------------------
  128. A few drivers use nowayout statically, i.e. there is no module parameter for it
  129. and only CONFIG_WATCHDOG_NOWAYOUT determines if the feature is going to be
  130. used. This needs to be converted by initializing the status variable of the
  131. watchdog_device like this:
  132. .status = WATCHDOG_NOWAYOUT_INIT_STATUS,
  133. Most drivers, however, also allow runtime configuration of nowayout, usually
  134. by adding a module parameter. The conversion for this would be something like:
  135. watchdog_set_nowayout(&s3c2410_wdd, nowayout);
  136. The module parameter itself needs to stay, everything else related to nowayout
  137. can go, though. This will likely be some code in open(), close() or write().
  138. Register the watchdog device
  139. ----------------------------
  140. Replace misc_register(&miscdev) with watchdog_register_device(&watchdog_dev).
  141. Make sure the return value gets checked and the error message, if present,
  142. still fits. Also convert the unregister case.
  143. - ret = misc_register(&s3c2410wdt_miscdev);
  144. + ret = watchdog_register_device(&s3c2410_wdd);
  145. ...
  146. - misc_deregister(&s3c2410wdt_miscdev);
  147. + watchdog_unregister_device(&s3c2410_wdd);
  148. Update the Kconfig-entry
  149. ------------------------
  150. The entry for the driver now needs to select WATCHDOG_CORE:
  151. + select WATCHDOG_CORE
  152. Create a patch and send it to upstream
  153. --------------------------------------
  154. Make sure you understood Documentation/SubmittingPatches and send your patch to
  155. linux-watchdog@vger.kernel.org. We are looking forward to it :)