ledByMySelf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <linux/module.h>
  2. #include <linux/platform_device.h>
  3. #include <linux/of.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/uaccess.h> // for copy_to_user() and copy_from_user()
  7. const char *TAG = "MYNEWNODE";\
  8. static int major_number = 0; // 默认自动分配主设备号
  9. static struct cdev my_cdev;
  10. static struct class *my_class;
  11. // 测试提交
  12. // 打开字符设备
  13. static int gpio_led_open(struct inode *inode, struct file *file)
  14. {
  15. printk("%s:open!\n", TAG);
  16. return 0;
  17. }
  18. // 关闭字符设备
  19. static int gpio_led_release(struct inode *inode, struct file *file)
  20. {
  21. printk("%s:release!\n", TAG);
  22. return 0;
  23. }
  24. // 读取数据(此示例没有实际硬件操作)
  25. static ssize_t gpio_led_read(struct file *file, char __user *buf, size_t len, loff_t *offset)
  26. {
  27. printk("%s:read!\n", TAG);
  28. // 这里可以填充实际的读取逻辑,如果需要从硬件读取数据
  29. return 0;
  30. }
  31. // 写入数据(此示例没有实际硬件操作)
  32. static ssize_t gpio_led_write(struct file *file, const char __user *buf, size_t len, loff_t *offset)
  33. {
  34. printk("%s:write!\n", TAG);
  35. // 这里可以填充实际的写入逻辑,如果需要控制硬件
  36. return len; // 返回实际写入的字节数
  37. }
  38. // 字符设备操作结构体
  39. static const struct file_operations gpio_led_fops = {
  40. .owner = THIS_MODULE,
  41. .open = gpio_led_open,
  42. .release = gpio_led_release,
  43. .read = gpio_led_read,
  44. .write = gpio_led_write,
  45. };
  46. static int gpio_led_probe(struct platform_device *pdev)
  47. { int ret;
  48. // 这里可以添加初始化代码
  49. printk("%s:probe!\n",TAG);
  50. ret = alloc_chrdev_region(&major_number,0,1,"gpio_led");
  51. if(ret < 0){
  52. printk("%s:alloc_chrdev_region failed!\n", TAG);
  53. return ret;
  54. }
  55. // 注册字符设备
  56. cdev_init(&my_cdev, &gpio_led_fops);
  57. my_cdev.owner = THIS_MODULE;
  58. ret = cdev_add(&my_cdev, major_number, 1);
  59. if (ret) {
  60. printk("%s:cdev_add failed!\n", TAG);
  61. unregister_chrdev_region(major_number, 1);
  62. return ret;
  63. }
  64. // 创建设备文件
  65. my_class = class_create(THIS_MODULE, "gpio_led_class");
  66. if (IS_ERR(my_class)) {
  67. ret = PTR_ERR(my_class);
  68. printk("%s:class_create failed!\n", TAG);
  69. cdev_del(&my_cdev);
  70. unregister_chrdev_region(major_number, 1);
  71. return ret;
  72. }
  73. if (IS_ERR(device_create(my_class, NULL, major_number, NULL, "gpio_led"))) {
  74. printk("%s:device_create failed!\n", TAG);
  75. class_destroy(my_class);
  76. cdev_del(&my_cdev);
  77. unregister_chrdev_region(major_number, 1);
  78. return -ENOMEM;
  79. }
  80. return 0; // 返回 0 表示成功
  81. }
  82. // 设备remove函数
  83. static int gpio_led_remove(struct platform_device *pdev)
  84. {
  85. printk("%s:remove!\n", TAG);
  86. // 注销字符设备
  87. device_destroy(my_class, major_number);
  88. class_destroy(my_class);
  89. cdev_del(&my_cdev);
  90. unregister_chrdev_region(major_number, 1);
  91. return 0;
  92. }
  93. static void gpio_led_shutdown(struct platform_device *pdev)
  94. {
  95. printk("%s:shutdown!\n", TAG);
  96. }
  97. static const struct of_device_id of_gpio_leds_match[] = {
  98. { .compatible = "led-user", },
  99. {}
  100. };
  101. MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
  102. static struct platform_driver gpio_led_driver = {
  103. .probe = gpio_led_probe,
  104. .remove = gpio_led_remove,
  105. .shutdown = gpio_led_shutdown,
  106. .driver = {
  107. .name = "led-user",
  108. .of_match_table = of_gpio_leds_match,
  109. },
  110. };
  111. module_platform_driver(gpio_led_driver);
  112. MODULE_LICENSE("GPL");
  113. MODULE_AUTHOR("Your Name");
  114. MODULE_DESCRIPTION("Simple GPIO LEDs driver");