devicemodel.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * devicemodel.c
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/version.h>
  8. struct devicemodel_data {
  9. char *greeting;
  10. int number;
  11. };
  12. static int devicemodel_probe(struct platform_device *dev)
  13. {
  14. struct devicemodel_data *pd =
  15. (struct devicemodel_data *)(dev->dev.platform_data);
  16. pr_info("devicemodel probe\n");
  17. pr_info("devicemodel greeting: %s; %d\n", pd->greeting, pd->number);
  18. /* Your device initialization code */
  19. return 0;
  20. }
  21. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
  22. static void devicemodel_remove(struct platform_device *dev)
  23. {
  24. pr_info("devicemodel example removed\n");
  25. /* Your device removal code */
  26. }
  27. #else
  28. static int devicemodel_remove(struct platform_device *dev)
  29. {
  30. pr_info("devicemodel example removed\n");
  31. /* Your device removal code */
  32. return 0;
  33. }
  34. #endif
  35. static int devicemodel_suspend(struct device *dev)
  36. {
  37. pr_info("devicemodel example suspend\n");
  38. /* Your device suspend code */
  39. return 0;
  40. }
  41. static int devicemodel_resume(struct device *dev)
  42. {
  43. pr_info("devicemodel example resume\n");
  44. /* Your device resume code */
  45. return 0;
  46. }
  47. static const struct dev_pm_ops devicemodel_pm_ops = {
  48. .suspend = devicemodel_suspend,
  49. .resume = devicemodel_resume,
  50. .poweroff = devicemodel_suspend,
  51. .freeze = devicemodel_suspend,
  52. .thaw = devicemodel_resume,
  53. .restore = devicemodel_resume,
  54. };
  55. static struct platform_driver devicemodel_driver = {
  56. .driver =
  57. {
  58. .name = "devicemodel_example",
  59. .pm = &devicemodel_pm_ops,
  60. },
  61. .probe = devicemodel_probe,
  62. .remove = devicemodel_remove,
  63. };
  64. static int __init devicemodel_init(void)
  65. {
  66. int ret;
  67. pr_info("devicemodel init\n");
  68. ret = platform_driver_register(&devicemodel_driver);
  69. if (ret) {
  70. pr_err("Unable to register driver\n");
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. static void __exit devicemodel_exit(void)
  76. {
  77. pr_info("devicemodel exit\n");
  78. platform_driver_unregister(&devicemodel_driver);
  79. }
  80. module_init(devicemodel_init);
  81. module_exit(devicemodel_exit);
  82. MODULE_LICENSE("GPL");
  83. MODULE_DESCRIPTION("Linux Device Model example");