fan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 ssize_t voltage_5v_show(struct kobject *kobj, struct kobj_attribute *attr,
  149. char *buf)
  150. {
  151. static int count = 0;
  152. count++;
  153. return sprintf(buf, "voltage_5v_show count=%d\n", count);
  154. }
  155. static ssize_t voltage_5v_store(struct kobject *kobj, struct kobj_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. printk("voltage_5v_store kernel rev:%s\n", buf);
  159. return count;
  160. }
  161. static ssize_t voltage_12v_show(struct kobject *kobj, struct kobj_attribute *attr,
  162. char *buf)
  163. {
  164. static int count = 0;
  165. count++;
  166. return sprintf(buf, "voltage_12v_show count=%d\n", count);
  167. }
  168. static ssize_t voltage_12v_store(struct kobject *kobj, struct kobj_attribute *attr,
  169. const char *buf, size_t count)
  170. {
  171. printk("voltage_12v_store kernel rev:%s\n", buf);
  172. return count;
  173. }
  174. static ssize_t voltage_vcore_show(struct kobject *kobj, struct kobj_attribute *attr,
  175. char *buf)
  176. {
  177. static int count = 0;
  178. count++;
  179. return sprintf(buf, "voltage_vcore_show count=%d\n", count);
  180. }
  181. static ssize_t voltage_vcore_store(struct kobject *kobj, struct kobj_attribute *attr,
  182. const char *buf, size_t count)
  183. {
  184. printk("voltage_vcore_store kernel rev:%s\n", buf);
  185. return count;
  186. }
  187. static ssize_t ac_power_show(struct kobject *kobj, struct kobj_attribute *attr,
  188. char *buf)
  189. {
  190. static int count = 0;
  191. count++;
  192. return sprintf(buf, "ac_power_show count=%d\n", count);
  193. }
  194. static ssize_t ac_power_store(struct kobject *kobj, struct kobj_attribute *attr,
  195. const char *buf, size_t count)
  196. {
  197. printk("ac_power_store kernel rev:%s\n", buf);
  198. return count;
  199. }
  200. static ssize_t power_flag_show(struct kobject *kobj, struct kobj_attribute *attr,
  201. char *buf)
  202. {
  203. static int count = 0;
  204. count++;
  205. return sprintf(buf, "power_flag_show count=%d\n", count);
  206. }
  207. static ssize_t power_flag_store(struct kobject *kobj, struct kobj_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. printk("power_flag_store kernel rev:%s\n", buf);
  211. return count;
  212. }
  213. static struct kobj_attribute fan1_input =
  214. __ATTR(fan1_input, 0644, fan1_input_show, fan1_input_store);
  215. static struct kobj_attribute fan2_input =
  216. __ATTR(fan2_input, 0644, fan2_input_show, fan2_input_store);
  217. static struct kobj_attribute pwm1 =
  218. __ATTR(pwm1, 0644, pwm1_show, pwm1_store);
  219. static struct kobj_attribute pwm1_enable =
  220. __ATTR(pwm1_enable, 0644, pwm1_enable_show, pwm1_enable_store);
  221. static struct kobj_attribute pwm2 =
  222. __ATTR(pwm2, 0644, pwm2_show, pwm2_store);
  223. static struct kobj_attribute pwm2_enable =
  224. __ATTR(pwm2_enable, 0644, pwm2_enable_show, pwm2_enable_store);
  225. static struct kobj_attribute voltage_5v =
  226. __ATTR(voltage_5v, 0644, voltage_5v_show, voltage_5v_store);
  227. static struct kobj_attribute voltage_12v =
  228. __ATTR(voltage_12v, 0644, voltage_12v_show, voltage_12v_store);
  229. static struct kobj_attribute voltage_vcore =
  230. __ATTR(voltage_vcore, 0644, voltage_vcore_show, voltage_vcore_store);
  231. static struct kobj_attribute ac_power =
  232. __ATTR(ac_power, 0644, ac_power_show, ac_power_store);
  233. static struct kobj_attribute power_flag =
  234. __ATTR(power_flag, 0644, power_flag_show, power_flag_store);
  235. /* ==================== 属性组 ==================== */
  236. static struct attribute *fan_attrs[] = {
  237. &fan1_input.attr,
  238. &fan2_input.attr,
  239. &pwm1.attr,
  240. &pwm1_enable.attr,
  241. &pwm2.attr,
  242. &pwm2_enable.attr,
  243. &voltage_5v.attr,
  244. &voltage_12v.attr,
  245. &voltage_vcore.attr,
  246. &ac_power.attr,
  247. &power_flag.attr,
  248. NULL,
  249. };
  250. static struct attribute_group fan_attr_group = {
  251. .attrs = fan_attrs,
  252. };
  253. int fan_init(void)
  254. {
  255. int ret;
  256. /* 创建 /sys/kernel/vfiec/lightring */
  257. fan_kobj = kobject_create_and_add("hwmon", vfiec_kobj);
  258. if (!fan_kobj)
  259. {
  260. ret = -ENOMEM;
  261. }
  262. else
  263. {
  264. printk(KERN_INFO "Faifan to create sysfs node\n");
  265. }
  266. /* 创建属性文件 */
  267. ret = sysfs_create_group(fan_kobj, &fan_attr_group);
  268. if (ret)
  269. {
  270. pr_err("Faifan to create sysfs group: %d\n", ret);
  271. goto free_fan_kobj;
  272. }
  273. else
  274. {
  275. printk(KERN_INFO "Create sysfs group success\n");
  276. }
  277. ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
  278. if (ret < 0)
  279. {
  280. printk(KERN_ALERT "mychar: Failed to allocate device number\n");
  281. goto free_group;
  282. }
  283. major = MAJOR(dev_num);
  284. printk(KERN_INFO "mychar: Assigned major number %d\n", major);
  285. // 2. 分配设备结构体内存
  286. fan_dev_device = kzalloc(sizeof(struct fan_dev), GFP_KERNEL);
  287. if (!fan_dev_device)
  288. {
  289. printk(KERN_ALERT "mychar: Failed to allocate device memory\n");
  290. ret = -ENOMEM;
  291. goto free_chrdev;
  292. }
  293. cdev_init(&fan_dev_device->cdev, &fan_fops);
  294. fan_dev_device->cdev.owner = THIS_MODULE;
  295. ret = cdev_add(&fan_dev_device->cdev, dev_num, 1);
  296. if (ret < 0)
  297. {
  298. printk(KERN_ALERT "mychar: Failed to add cdev\n");
  299. goto free_fan_devices;
  300. }
  301. // 4. 创建设备类(用于自动创建/dev节点)
  302. fan_dev_device->class = class_create(THIS_MODULE, CLASS_NAME);
  303. if (IS_ERR(fan_dev_device->class))
  304. {
  305. printk(KERN_ALERT "mychar: Failed to create class\n");
  306. goto free_cdev_del;
  307. }
  308. // 5. 创建设备节点(会在/dev下生成)
  309. fan_dev_device->device = device_create(fan_dev_device->class, NULL, dev_num, NULL, DEVICE_NAME);
  310. if (IS_ERR(fan_dev_device->device))
  311. {
  312. printk(KERN_ALERT "mychar: Failed to create device\n");
  313. goto free_class;
  314. }
  315. return 0;
  316. free_class:
  317. class_destroy(fan_dev_device->class);
  318. free_cdev_del:
  319. cdev_del(&fan_dev_device->cdev);
  320. free_fan_devices:
  321. kfree(fan_dev_device);
  322. free_chrdev:
  323. unregister_chrdev_region(dev_num, 1);
  324. free_group:
  325. sysfs_remove_group(fan_kobj, &fan_attr_group);
  326. free_fan_kobj:
  327. kobject_put(fan_kobj);
  328. return ret;
  329. }
  330. void fan_exit(void)
  331. {
  332. device_destroy(fan_dev_device->class, dev_num);
  333. class_destroy(fan_dev_device->class);
  334. cdev_del(&fan_dev_device->cdev);
  335. kfree(fan_dev_device);
  336. unregister_chrdev_region(dev_num, 1);
  337. sysfs_remove_group(fan_kobj, &fan_attr_group);
  338. kobject_put(fan_kobj);
  339. }