fan.c_ybak 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/slab.h>
  9. #include <linux/pci.h>
  10. #include <linux/i2c.h>
  11. #include <linux/acpi.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched.h>
  15. #include <linux/poll.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include "light_ring.h"
  19. #define DEVICE_NAME "fan"
  20. #define CLASS_NAME "fan_class"
  21. #define DRIVER_NAME "fan_driver"
  22. struct fan_dev
  23. {
  24. struct cdev cdev;
  25. dev_t devt;
  26. struct class *class;
  27. struct device *device;
  28. };
  29. extern struct kobject *vfiec_kobj;
  30. static struct kobject *fan_kobj = NULL;
  31. static int major = 0; // 主设备号(0表示动态分配)
  32. static struct fan_dev *fan_dev_device = NULL;
  33. static dev_t dev_num;
  34. // 文件打开
  35. static int fan_open(struct inode *inode, struct file *filp)
  36. {
  37. // 从inode获取cdev,再获取包含cdev的结构体
  38. struct fan_dev *dev = container_of(inode->i_cdev, struct fan_dev, cdev);
  39. filp->private_data = dev; // 保存到file结构供其他函数使用
  40. printk(KERN_INFO "fan: device opened\n");
  41. return 0;
  42. }
  43. // 文件释放
  44. static int fan_release(struct inode *inode, struct file *filp)
  45. {
  46. printk(KERN_INFO "fan: device closed\n");
  47. return 0;
  48. }
  49. // 读操作
  50. static ssize_t fan_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  51. {
  52. struct fan_dev *dev = filp->private_data;
  53. return count;
  54. }
  55. // 写操作
  56. static ssize_t fan_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
  57. {
  58. struct fan_dev *dev = filp->private_data;
  59. printk(KERN_INFO "fan: wrote %zu bytes\n", count);
  60. return count;
  61. }
  62. // 文件操作结构体
  63. static struct file_operations fan_fops = {
  64. .owner = THIS_MODULE,
  65. .open = fan_open,
  66. .release = fan_release,
  67. .read = fan_read,
  68. .write = fan_write,
  69. };
  70. static ssize_t fan1_input_show(struct kobject *kobj, struct kobj_attribute *attr,
  71. char *buf)
  72. {
  73. static int count = 0;
  74. count++;
  75. return sprintf(buf, "fan1_input_show count=%d\n", count);
  76. }
  77. static ssize_t fan1_input_store(struct kobject *kobj, struct kobj_attribute *attr,
  78. const char *buf, size_t count)
  79. {
  80. printk("fan1_input_store kernel rev:%s\n", buf);
  81. return count;
  82. }
  83. static ssize_t fan2_input_show(struct kobject *kobj, struct kobj_attribute *attr,
  84. char *buf)
  85. {
  86. static int count = 0;
  87. count++;
  88. return sprintf(buf, "fan2_input_show count=%d\n", count);
  89. }
  90. static ssize_t fan2_input_store(struct kobject *kobj, struct kobj_attribute *attr,
  91. const char *buf, size_t count)
  92. {
  93. printk("fan2_input_store kernel rev:%s\n", buf);
  94. return count;
  95. }
  96. static ssize_t pwm1_show(struct kobject *kobj, struct kobj_attribute *attr,
  97. char *buf)
  98. {
  99. static int count = 0;
  100. count++;
  101. return sprintf(buf, "pwm1_show count=%d\n", count);
  102. }
  103. static ssize_t pwm1_store(struct kobject *kobj, struct kobj_attribute *attr,
  104. const char *buf, size_t count)
  105. {
  106. printk("pwm1_store kernel rev:%s\n", buf);
  107. return count;
  108. }
  109. static ssize_t pwm1_enable_show(struct kobject *kobj, struct kobj_attribute *attr,
  110. char *buf)
  111. {
  112. static int count = 0;
  113. count++;
  114. return sprintf(buf, "pwm1_enable_show count=%d\n", count);
  115. }
  116. static ssize_t pwm1_enable_store(struct kobject *kobj, struct kobj_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. printk("pwm1_enable_store kernel rev:%s\n", buf);
  120. return count;
  121. }
  122. static ssize_t pwm2_show(struct kobject *kobj, struct kobj_attribute *attr,
  123. char *buf)
  124. {
  125. static int count = 0;
  126. count++;
  127. return sprintf(buf, "pwm2_show count=%d\n", count);
  128. }
  129. static ssize_t pwm2_store(struct kobject *kobj, struct kobj_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. printk("pwm2_store kernel rev:%s\n", buf);
  133. return count;
  134. }
  135. static ssize_t pwm2_enable_show(struct kobject *kobj, struct kobj_attribute *attr,
  136. char *buf)
  137. {
  138. static int count = 0;
  139. count++;
  140. return sprintf(buf, "pwm2_enable_show count=%d\n", count);
  141. }
  142. static ssize_t pwm2_enable_store(struct kobject *kobj, struct kobj_attribute *attr,
  143. const char *buf, size_t count)
  144. {
  145. printk("pwm2_enable_store kernel rev:%s\n", buf);
  146. return count;
  147. }
  148. static struct kobj_attribute fan1_input =
  149. __ATTR(fan1_input, 0644, fan1_input_show, fan1_input_store);
  150. static struct kobj_attribute fan2_input =
  151. __ATTR(fan2_input, 0644, fan2_input_show, fan2_input_store);
  152. static struct kobj_attribute pwm1 =
  153. __ATTR(pwm1, 0644, pwm1_show, pwm1_store);
  154. static struct kobj_attribute pwm1_enable =
  155. __ATTR(pwm1_enable, 0644, pwm1_enable_show, pwm1_enable_store);
  156. static struct kobj_attribute pwm2 =
  157. __ATTR(pwm2, 0644, pwm2_show, pwm2_store);
  158. static struct kobj_attribute pwm2_enable =
  159. __ATTR(pwm2_enable, 0644, pwm2_enable_show, pwm2_enable_store);
  160. /* ==================== 属性组 ==================== */
  161. static struct attribute *fan_attrs[] = {
  162. &fan1_input.attr,
  163. &fan2_input.attr,
  164. &pwm1.attr,
  165. &pwm1_enable.attr,
  166. &pwm2.attr,
  167. &pwm2_enable.attr,
  168. NULL,
  169. };
  170. static struct attribute_group fan_attr_group = {
  171. .attrs = fan_attrs,
  172. };
  173. int fan_init(void)
  174. {
  175. int ret;
  176. /* 创建 /sys/kernel/vfiec/lightring */
  177. fan_kobj = kobject_create_and_add("hwmon", vfiec_kobj);
  178. if (!fan_kobj)
  179. {
  180. ret = -ENOMEM;
  181. }
  182. else
  183. {
  184. printk(KERN_INFO "Faifan to create sysfs node\n");
  185. }
  186. /* 创建属性文件 */
  187. ret = sysfs_create_group(fan_kobj, &fan_attr_group);
  188. if (ret)
  189. {
  190. pr_err("Faifan to create sysfs group: %d\n", ret);
  191. goto free_fan_kobj;
  192. }
  193. else
  194. {
  195. printk(KERN_INFO "Create sysfs group success\n");
  196. }
  197. ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
  198. if (ret < 0)
  199. {
  200. printk(KERN_ALERT "mychar: Failed to allocate device number\n");
  201. goto free_group;
  202. }
  203. major = MAJOR(dev_num);
  204. printk(KERN_INFO "mychar: Assigned major number %d\n", major);
  205. // 2. 分配设备结构体内存
  206. fan_dev_device = kzalloc(sizeof(struct fan_dev), GFP_KERNEL);
  207. if (!fan_dev_device)
  208. {
  209. printk(KERN_ALERT "mychar: Failed to allocate device memory\n");
  210. ret = -ENOMEM;
  211. goto free_chrdev;
  212. }
  213. cdev_init(&fan_dev_device->cdev, &fan_fops);
  214. fan_dev_device->cdev.owner = THIS_MODULE;
  215. ret = cdev_add(&fan_dev_device->cdev, dev_num, 1);
  216. if (ret < 0)
  217. {
  218. printk(KERN_ALERT "mychar: Failed to add cdev\n");
  219. goto free_fan_devices;
  220. }
  221. // 4. 创建设备类(用于自动创建/dev节点)
  222. fan_dev_device->class = class_create(THIS_MODULE, CLASS_NAME);
  223. if (IS_ERR(fan_dev_device->class))
  224. {
  225. printk(KERN_ALERT "mychar: Failed to create class\n");
  226. goto free_cdev_del;
  227. }
  228. // 5. 创建设备节点(会在/dev下生成)
  229. fan_dev_device->device = device_create(fan_dev_device->class, NULL, dev_num, NULL, DEVICE_NAME);
  230. if (IS_ERR(fan_dev_device->device))
  231. {
  232. printk(KERN_ALERT "mychar: Failed to create device\n");
  233. goto free_class;
  234. }
  235. return 0;
  236. free_class:
  237. class_destroy(fan_dev_device->class);
  238. free_cdev_del:
  239. cdev_del(&fan_dev_device->cdev);
  240. free_fan_devices:
  241. kfree(fan_dev_device);
  242. free_chrdev:
  243. unregister_chrdev_region(dev_num, 1);
  244. free_group:
  245. sysfs_remove_group(fan_kobj, &fan_attr_group);
  246. free_fan_kobj:
  247. kobject_put(fan_kobj);
  248. return ret;
  249. }
  250. void fan_exit(void)
  251. {
  252. device_destroy(fan_dev_device->class, dev_num);
  253. class_destroy(fan_dev_device->class);
  254. cdev_del(&fan_dev_device->cdev);
  255. kfree(fan_dev_device);
  256. unregister_chrdev_region(dev_num, 1);
  257. sysfs_remove_group(fan_kobj, &fan_attr_group);
  258. kobject_put(fan_kobj);
  259. }