devicemodel.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 int devicemodel_remove(struct platform_device *dev)
  23. #else
  24. static void devicemodel_remove(struct platform_device *dev)
  25. #endif
  26. {
  27. pr_info("devicemodel example removed\n");
  28. /* Your device removal code */
  29. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 11, 0)
  30. return 0;
  31. #endif
  32. }
  33. static int devicemodel_suspend(struct device *dev)
  34. {
  35. pr_info("devicemodel example suspend\n");
  36. /* Your device suspend code */
  37. return 0;
  38. }
  39. static int devicemodel_resume(struct device *dev)
  40. {
  41. pr_info("devicemodel example resume\n");
  42. /* Your device resume code */
  43. return 0;
  44. }
  45. static const struct dev_pm_ops devicemodel_pm_ops = {
  46. .suspend = devicemodel_suspend,
  47. .resume = devicemodel_resume,
  48. .poweroff = devicemodel_suspend,
  49. .freeze = devicemodel_suspend,
  50. .thaw = devicemodel_resume,
  51. .restore = devicemodel_resume,
  52. };
  53. static struct platform_driver devicemodel_driver = {
  54. .driver =
  55. {
  56. .name = "devicemodel_example",
  57. .pm = &devicemodel_pm_ops,
  58. },
  59. .probe = devicemodel_probe,
  60. .remove = devicemodel_remove,
  61. };
  62. static int __init devicemodel_init(void)
  63. {
  64. int ret;
  65. pr_info("devicemodel init\n");
  66. ret = platform_driver_register(&devicemodel_driver);
  67. if (ret) {
  68. pr_err("Unable to register driver\n");
  69. return ret;
  70. }
  71. return 0;
  72. }
  73. static void __exit devicemodel_exit(void)
  74. {
  75. pr_info("devicemodel exit\n");
  76. platform_driver_unregister(&devicemodel_driver);
  77. }
  78. module_init(devicemodel_init);
  79. module_exit(devicemodel_exit);
  80. MODULE_LICENSE("GPL");
  81. MODULE_DESCRIPTION("Linux Device Model example");