fan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 ssize_t temp1_input_show(struct kobject *kobj, struct kobj_attribute *attr,
  214. char *buf)
  215. {
  216. static int count = 0;
  217. count++;
  218. return sprintf(buf, "temp1_input_show count=%d\n", count);
  219. }
  220. static ssize_t temp1_input_store(struct kobject *kobj, struct kobj_attribute *attr,
  221. const char *buf, size_t count)
  222. {
  223. printk("temp1_input_store kernel rev:%s\n", buf);
  224. return count;
  225. }
  226. static ssize_t temp2_input_show(struct kobject *kobj, struct kobj_attribute *attr,
  227. char *buf)
  228. {
  229. static int count = 0;
  230. count++;
  231. return sprintf(buf, "temp2_input_show count=%d\n", count);
  232. }
  233. static ssize_t temp2_input_store(struct kobject *kobj, struct kobj_attribute *attr,
  234. const char *buf, size_t count)
  235. {
  236. printk("temp2_input_store kernel rev:%s\n", buf);
  237. return count;
  238. }
  239. static struct kobj_attribute fan1_input =
  240. __ATTR(fan1_input, 0644, fan1_input_show, fan1_input_store);
  241. static struct kobj_attribute fan2_input =
  242. __ATTR(fan2_input, 0644, fan2_input_show, fan2_input_store);
  243. static struct kobj_attribute pwm1 =
  244. __ATTR(pwm1, 0644, pwm1_show, pwm1_store);
  245. static struct kobj_attribute pwm1_enable =
  246. __ATTR(pwm1_enable, 0644, pwm1_enable_show, pwm1_enable_store);
  247. static struct kobj_attribute pwm2 =
  248. __ATTR(pwm2, 0644, pwm2_show, pwm2_store);
  249. static struct kobj_attribute pwm2_enable =
  250. __ATTR(pwm2_enable, 0644, pwm2_enable_show, pwm2_enable_store);
  251. static struct kobj_attribute voltage_5v =
  252. __ATTR(voltage_5v, 0644, voltage_5v_show, voltage_5v_store);
  253. static struct kobj_attribute voltage_12v =
  254. __ATTR(voltage_12v, 0644, voltage_12v_show, voltage_12v_store);
  255. static struct kobj_attribute voltage_vcore =
  256. __ATTR(voltage_vcore, 0644, voltage_vcore_show, voltage_vcore_store);
  257. static struct kobj_attribute ac_power =
  258. __ATTR(ac_power, 0644, ac_power_show, ac_power_store);
  259. static struct kobj_attribute power_flag =
  260. __ATTR(power_flag, 0644, power_flag_show, power_flag_store);
  261. static struct kobj_attribute temp1_input =
  262. __ATTR(temp1_input, 0444, temp1_input_show, temp1_input_store);
  263. static struct kobj_attribute temp2_input =
  264. __ATTR(temp2_input, 0444, temp2_input_show, temp2_input_store);
  265. /* ==================== 属性组 ==================== */
  266. static struct attribute *fan_attrs[] = {
  267. &fan1_input.attr,
  268. &fan2_input.attr,
  269. &pwm1.attr,
  270. &pwm1_enable.attr,
  271. &pwm2.attr,
  272. &pwm2_enable.attr,
  273. &voltage_5v.attr,
  274. &voltage_12v.attr,
  275. &voltage_vcore.attr,
  276. &ac_power.attr,
  277. &power_flag.attr,
  278. &temp1_input.attr,
  279. &temp2_input.attr,
  280. NULL,
  281. };
  282. static struct attribute_group fan_attr_group = {
  283. .attrs = fan_attrs,
  284. };
  285. int fan_init(void)
  286. {
  287. int ret;
  288. /* 创建 /sys/kernel/vfiec/lightring */
  289. fan_kobj = kobject_create_and_add("hwmon", vfiec_kobj);
  290. if (!fan_kobj)
  291. {
  292. ret = -ENOMEM;
  293. }
  294. else
  295. {
  296. printk(KERN_INFO "Faifan to create sysfs node\n");
  297. }
  298. /* 创建属性文件 */
  299. ret = sysfs_create_group(fan_kobj, &fan_attr_group);
  300. if (ret)
  301. {
  302. pr_err("Faifan to create sysfs group: %d\n", ret);
  303. goto free_fan_kobj;
  304. }
  305. else
  306. {
  307. printk(KERN_INFO "Create sysfs group success\n");
  308. }
  309. ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
  310. if (ret < 0)
  311. {
  312. printk(KERN_ALERT "mychar: Failed to allocate device number\n");
  313. goto free_group;
  314. }
  315. major = MAJOR(dev_num);
  316. printk(KERN_INFO "mychar: Assigned major number %d\n", major);
  317. // 2. 分配设备结构体内存
  318. fan_dev_device = kzalloc(sizeof(struct fan_dev), GFP_KERNEL);
  319. if (!fan_dev_device)
  320. {
  321. printk(KERN_ALERT "mychar: Failed to allocate device memory\n");
  322. ret = -ENOMEM;
  323. goto free_chrdev;
  324. }
  325. cdev_init(&fan_dev_device->cdev, &fan_fops);
  326. fan_dev_device->cdev.owner = THIS_MODULE;
  327. ret = cdev_add(&fan_dev_device->cdev, dev_num, 1);
  328. if (ret < 0)
  329. {
  330. printk(KERN_ALERT "mychar: Failed to add cdev\n");
  331. goto free_fan_devices;
  332. }
  333. // 4. 创建设备类(用于自动创建/dev节点)
  334. fan_dev_device->class = class_create(THIS_MODULE, CLASS_NAME);
  335. if (IS_ERR(fan_dev_device->class))
  336. {
  337. printk(KERN_ALERT "mychar: Failed to create class\n");
  338. goto free_cdev_del;
  339. }
  340. // 5. 创建设备节点(会在/dev下生成)
  341. fan_dev_device->device = device_create(fan_dev_device->class, NULL, dev_num, NULL, DEVICE_NAME);
  342. if (IS_ERR(fan_dev_device->device))
  343. {
  344. printk(KERN_ALERT "mychar: Failed to create device\n");
  345. goto free_class;
  346. }
  347. return 0;
  348. free_class:
  349. class_destroy(fan_dev_device->class);
  350. free_cdev_del:
  351. cdev_del(&fan_dev_device->cdev);
  352. free_fan_devices:
  353. kfree(fan_dev_device);
  354. free_chrdev:
  355. unregister_chrdev_region(dev_num, 1);
  356. free_group:
  357. sysfs_remove_group(fan_kobj, &fan_attr_group);
  358. free_fan_kobj:
  359. kobject_put(fan_kobj);
  360. return ret;
  361. }
  362. void fan_exit(void)
  363. {
  364. device_destroy(fan_dev_device->class, dev_num);
  365. class_destroy(fan_dev_device->class);
  366. cdev_del(&fan_dev_device->cdev);
  367. kfree(fan_dev_device);
  368. unregister_chrdev_region(dev_num, 1);
  369. sysfs_remove_group(fan_kobj, &fan_attr_group);
  370. kobject_put(fan_kobj);
  371. }