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