dell-smm-hwmon.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
  3. *
  4. * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
  5. *
  6. * Hwmon integration:
  7. * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
  8. * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
  9. * Copyright (C) 2014, 2015 Pali Rohár <pali.rohar@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2, or (at your option) any
  14. * later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/cpu.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/init.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/dmi.h>
  30. #include <linux/capability.h>
  31. #include <linux/mutex.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/io.h>
  36. #include <linux/sched.h>
  37. #include <linux/ctype.h>
  38. #include <linux/smp.h>
  39. #include <linux/i8k.h>
  40. #define I8K_SMM_FN_STATUS 0x0025
  41. #define I8K_SMM_POWER_STATUS 0x0069
  42. #define I8K_SMM_SET_FAN 0x01a3
  43. #define I8K_SMM_GET_FAN 0x00a3
  44. #define I8K_SMM_GET_SPEED 0x02a3
  45. #define I8K_SMM_GET_FAN_TYPE 0x03a3
  46. #define I8K_SMM_GET_NOM_SPEED 0x04a3
  47. #define I8K_SMM_GET_TEMP 0x10a3
  48. #define I8K_SMM_GET_TEMP_TYPE 0x11a3
  49. #define I8K_SMM_GET_DELL_SIG1 0xfea3
  50. #define I8K_SMM_GET_DELL_SIG2 0xffa3
  51. #define I8K_FAN_MULT 30
  52. #define I8K_FAN_MAX_RPM 30000
  53. #define I8K_MAX_TEMP 127
  54. #define I8K_FN_NONE 0x00
  55. #define I8K_FN_UP 0x01
  56. #define I8K_FN_DOWN 0x02
  57. #define I8K_FN_MUTE 0x04
  58. #define I8K_FN_MASK 0x07
  59. #define I8K_FN_SHIFT 8
  60. #define I8K_POWER_AC 0x05
  61. #define I8K_POWER_BATTERY 0x01
  62. static DEFINE_MUTEX(i8k_mutex);
  63. static char bios_version[4];
  64. static char bios_machineid[16];
  65. static struct device *i8k_hwmon_dev;
  66. static u32 i8k_hwmon_flags;
  67. static uint i8k_fan_mult = I8K_FAN_MULT;
  68. static uint i8k_pwm_mult;
  69. static uint i8k_fan_max = I8K_FAN_HIGH;
  70. static bool disallow_fan_type_call;
  71. #define I8K_HWMON_HAVE_TEMP1 (1 << 0)
  72. #define I8K_HWMON_HAVE_TEMP2 (1 << 1)
  73. #define I8K_HWMON_HAVE_TEMP3 (1 << 2)
  74. #define I8K_HWMON_HAVE_TEMP4 (1 << 3)
  75. #define I8K_HWMON_HAVE_FAN1 (1 << 4)
  76. #define I8K_HWMON_HAVE_FAN2 (1 << 5)
  77. #define I8K_HWMON_HAVE_FAN3 (1 << 6)
  78. MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
  79. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  80. MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
  81. MODULE_LICENSE("GPL");
  82. MODULE_ALIAS("i8k");
  83. static bool force;
  84. module_param(force, bool, 0);
  85. MODULE_PARM_DESC(force, "Force loading without checking for supported models");
  86. static bool ignore_dmi;
  87. module_param(ignore_dmi, bool, 0);
  88. MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
  89. #if IS_ENABLED(CONFIG_I8K)
  90. static bool restricted = true;
  91. module_param(restricted, bool, 0);
  92. MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
  93. static bool power_status;
  94. module_param(power_status, bool, 0600);
  95. MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
  96. #endif
  97. static uint fan_mult;
  98. module_param(fan_mult, uint, 0);
  99. MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
  100. static uint fan_max;
  101. module_param(fan_max, uint, 0);
  102. MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
  103. struct smm_regs {
  104. unsigned int eax;
  105. unsigned int ebx __packed;
  106. unsigned int ecx __packed;
  107. unsigned int edx __packed;
  108. unsigned int esi __packed;
  109. unsigned int edi __packed;
  110. };
  111. static inline const char *i8k_get_dmi_data(int field)
  112. {
  113. const char *dmi_data = dmi_get_system_info(field);
  114. return dmi_data && *dmi_data ? dmi_data : "?";
  115. }
  116. /*
  117. * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
  118. */
  119. static int i8k_smm_func(void *par)
  120. {
  121. int rc;
  122. struct smm_regs *regs = par;
  123. int eax = regs->eax;
  124. #ifdef DEBUG
  125. int ebx = regs->ebx;
  126. unsigned long duration;
  127. ktime_t calltime, delta, rettime;
  128. calltime = ktime_get();
  129. #endif
  130. /* SMM requires CPU 0 */
  131. if (smp_processor_id() != 0)
  132. return -EBUSY;
  133. #if defined(CONFIG_X86_64)
  134. asm volatile("pushq %%rax\n\t"
  135. "movl 0(%%rax),%%edx\n\t"
  136. "pushq %%rdx\n\t"
  137. "movl 4(%%rax),%%ebx\n\t"
  138. "movl 8(%%rax),%%ecx\n\t"
  139. "movl 12(%%rax),%%edx\n\t"
  140. "movl 16(%%rax),%%esi\n\t"
  141. "movl 20(%%rax),%%edi\n\t"
  142. "popq %%rax\n\t"
  143. "out %%al,$0xb2\n\t"
  144. "out %%al,$0x84\n\t"
  145. "xchgq %%rax,(%%rsp)\n\t"
  146. "movl %%ebx,4(%%rax)\n\t"
  147. "movl %%ecx,8(%%rax)\n\t"
  148. "movl %%edx,12(%%rax)\n\t"
  149. "movl %%esi,16(%%rax)\n\t"
  150. "movl %%edi,20(%%rax)\n\t"
  151. "popq %%rdx\n\t"
  152. "movl %%edx,0(%%rax)\n\t"
  153. "pushfq\n\t"
  154. "popq %%rax\n\t"
  155. "andl $1,%%eax\n"
  156. : "=a"(rc)
  157. : "a"(regs)
  158. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  159. #else
  160. asm volatile("pushl %%eax\n\t"
  161. "movl 0(%%eax),%%edx\n\t"
  162. "push %%edx\n\t"
  163. "movl 4(%%eax),%%ebx\n\t"
  164. "movl 8(%%eax),%%ecx\n\t"
  165. "movl 12(%%eax),%%edx\n\t"
  166. "movl 16(%%eax),%%esi\n\t"
  167. "movl 20(%%eax),%%edi\n\t"
  168. "popl %%eax\n\t"
  169. "out %%al,$0xb2\n\t"
  170. "out %%al,$0x84\n\t"
  171. "xchgl %%eax,(%%esp)\n\t"
  172. "movl %%ebx,4(%%eax)\n\t"
  173. "movl %%ecx,8(%%eax)\n\t"
  174. "movl %%edx,12(%%eax)\n\t"
  175. "movl %%esi,16(%%eax)\n\t"
  176. "movl %%edi,20(%%eax)\n\t"
  177. "popl %%edx\n\t"
  178. "movl %%edx,0(%%eax)\n\t"
  179. "lahf\n\t"
  180. "shrl $8,%%eax\n\t"
  181. "andl $1,%%eax\n"
  182. : "=a"(rc)
  183. : "a"(regs)
  184. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  185. #endif
  186. if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
  187. rc = -EINVAL;
  188. #ifdef DEBUG
  189. rettime = ktime_get();
  190. delta = ktime_sub(rettime, calltime);
  191. duration = ktime_to_ns(delta) >> 10;
  192. pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x (took %7lu usecs)\n", eax, ebx,
  193. (rc ? 0xffff : regs->eax & 0xffff), duration);
  194. #endif
  195. return rc;
  196. }
  197. /*
  198. * Call the System Management Mode BIOS.
  199. */
  200. static int i8k_smm(struct smm_regs *regs)
  201. {
  202. int ret;
  203. get_online_cpus();
  204. ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
  205. put_online_cpus();
  206. return ret;
  207. }
  208. /*
  209. * Read the fan status.
  210. */
  211. static int i8k_get_fan_status(int fan)
  212. {
  213. struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
  214. regs.ebx = fan & 0xff;
  215. return i8k_smm(&regs) ? : regs.eax & 0xff;
  216. }
  217. /*
  218. * Read the fan speed in RPM.
  219. */
  220. static int i8k_get_fan_speed(int fan)
  221. {
  222. struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
  223. regs.ebx = fan & 0xff;
  224. return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
  225. }
  226. /*
  227. * Read the fan type.
  228. */
  229. static int _i8k_get_fan_type(int fan)
  230. {
  231. struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
  232. if (disallow_fan_type_call)
  233. return -EINVAL;
  234. regs.ebx = fan & 0xff;
  235. return i8k_smm(&regs) ? : regs.eax & 0xff;
  236. }
  237. static int i8k_get_fan_type(int fan)
  238. {
  239. /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
  240. static int types[3] = { INT_MIN, INT_MIN, INT_MIN };
  241. if (types[fan] == INT_MIN)
  242. types[fan] = _i8k_get_fan_type(fan);
  243. return types[fan];
  244. }
  245. /*
  246. * Read the fan nominal rpm for specific fan speed.
  247. */
  248. static int i8k_get_fan_nominal_speed(int fan, int speed)
  249. {
  250. struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
  251. regs.ebx = (fan & 0xff) | (speed << 8);
  252. return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
  253. }
  254. /*
  255. * Set the fan speed (off, low, high). Returns the new fan status.
  256. */
  257. static int i8k_set_fan(int fan, int speed)
  258. {
  259. struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
  260. speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
  261. regs.ebx = (fan & 0xff) | (speed << 8);
  262. return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
  263. }
  264. static int i8k_get_temp_type(int sensor)
  265. {
  266. struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
  267. regs.ebx = sensor & 0xff;
  268. return i8k_smm(&regs) ? : regs.eax & 0xff;
  269. }
  270. /*
  271. * Read the cpu temperature.
  272. */
  273. static int _i8k_get_temp(int sensor)
  274. {
  275. struct smm_regs regs = {
  276. .eax = I8K_SMM_GET_TEMP,
  277. .ebx = sensor & 0xff,
  278. };
  279. return i8k_smm(&regs) ? : regs.eax & 0xff;
  280. }
  281. static int i8k_get_temp(int sensor)
  282. {
  283. int temp = _i8k_get_temp(sensor);
  284. /*
  285. * Sometimes the temperature sensor returns 0x99, which is out of range.
  286. * In this case we retry (once) before returning an error.
  287. # 1003655137 00000058 00005a4b
  288. # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
  289. # 1003655139 00000054 00005c52
  290. */
  291. if (temp == 0x99) {
  292. msleep(100);
  293. temp = _i8k_get_temp(sensor);
  294. }
  295. /*
  296. * Return -ENODATA for all invalid temperatures.
  297. *
  298. * Known instances are the 0x99 value as seen above as well as
  299. * 0xc1 (193), which may be returned when trying to read the GPU
  300. * temperature if the system supports a GPU and it is currently
  301. * turned off.
  302. */
  303. if (temp > I8K_MAX_TEMP)
  304. return -ENODATA;
  305. return temp;
  306. }
  307. static int i8k_get_dell_signature(int req_fn)
  308. {
  309. struct smm_regs regs = { .eax = req_fn, };
  310. int rc;
  311. rc = i8k_smm(&regs);
  312. if (rc < 0)
  313. return rc;
  314. return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
  315. }
  316. #if IS_ENABLED(CONFIG_I8K)
  317. /*
  318. * Read the Fn key status.
  319. */
  320. static int i8k_get_fn_status(void)
  321. {
  322. struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
  323. int rc;
  324. rc = i8k_smm(&regs);
  325. if (rc < 0)
  326. return rc;
  327. switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
  328. case I8K_FN_UP:
  329. return I8K_VOL_UP;
  330. case I8K_FN_DOWN:
  331. return I8K_VOL_DOWN;
  332. case I8K_FN_MUTE:
  333. return I8K_VOL_MUTE;
  334. default:
  335. return 0;
  336. }
  337. }
  338. /*
  339. * Read the power status.
  340. */
  341. static int i8k_get_power_status(void)
  342. {
  343. struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
  344. int rc;
  345. rc = i8k_smm(&regs);
  346. if (rc < 0)
  347. return rc;
  348. return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
  349. }
  350. /*
  351. * Procfs interface
  352. */
  353. static int
  354. i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
  355. {
  356. int val = 0;
  357. int speed;
  358. unsigned char buff[16];
  359. int __user *argp = (int __user *)arg;
  360. if (!argp)
  361. return -EINVAL;
  362. switch (cmd) {
  363. case I8K_BIOS_VERSION:
  364. if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
  365. !isdigit(bios_version[2]))
  366. return -EINVAL;
  367. val = (bios_version[0] << 16) |
  368. (bios_version[1] << 8) | bios_version[2];
  369. break;
  370. case I8K_MACHINE_ID:
  371. if (restricted && !capable(CAP_SYS_ADMIN))
  372. return -EPERM;
  373. memset(buff, 0, sizeof(buff));
  374. strlcpy(buff, bios_machineid, sizeof(buff));
  375. break;
  376. case I8K_FN_STATUS:
  377. val = i8k_get_fn_status();
  378. break;
  379. case I8K_POWER_STATUS:
  380. val = i8k_get_power_status();
  381. break;
  382. case I8K_GET_TEMP:
  383. val = i8k_get_temp(0);
  384. break;
  385. case I8K_GET_SPEED:
  386. if (copy_from_user(&val, argp, sizeof(int)))
  387. return -EFAULT;
  388. val = i8k_get_fan_speed(val);
  389. break;
  390. case I8K_GET_FAN:
  391. if (copy_from_user(&val, argp, sizeof(int)))
  392. return -EFAULT;
  393. val = i8k_get_fan_status(val);
  394. break;
  395. case I8K_SET_FAN:
  396. if (restricted && !capable(CAP_SYS_ADMIN))
  397. return -EPERM;
  398. if (copy_from_user(&val, argp, sizeof(int)))
  399. return -EFAULT;
  400. if (copy_from_user(&speed, argp + 1, sizeof(int)))
  401. return -EFAULT;
  402. val = i8k_set_fan(val, speed);
  403. break;
  404. default:
  405. return -EINVAL;
  406. }
  407. if (val < 0)
  408. return val;
  409. switch (cmd) {
  410. case I8K_BIOS_VERSION:
  411. if (copy_to_user(argp, &val, 4))
  412. return -EFAULT;
  413. break;
  414. case I8K_MACHINE_ID:
  415. if (copy_to_user(argp, buff, 16))
  416. return -EFAULT;
  417. break;
  418. default:
  419. if (copy_to_user(argp, &val, sizeof(int)))
  420. return -EFAULT;
  421. break;
  422. }
  423. return 0;
  424. }
  425. static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  426. {
  427. long ret;
  428. mutex_lock(&i8k_mutex);
  429. ret = i8k_ioctl_unlocked(fp, cmd, arg);
  430. mutex_unlock(&i8k_mutex);
  431. return ret;
  432. }
  433. /*
  434. * Print the information for /proc/i8k.
  435. */
  436. static int i8k_proc_show(struct seq_file *seq, void *offset)
  437. {
  438. int fn_key, cpu_temp, ac_power;
  439. int left_fan, right_fan, left_speed, right_speed;
  440. cpu_temp = i8k_get_temp(0); /* 11100 µs */
  441. left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
  442. right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
  443. left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
  444. right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
  445. fn_key = i8k_get_fn_status(); /* 750 µs */
  446. if (power_status)
  447. ac_power = i8k_get_power_status(); /* 14700 µs */
  448. else
  449. ac_power = -1;
  450. /*
  451. * Info:
  452. *
  453. * 1) Format version (this will change if format changes)
  454. * 2) BIOS version
  455. * 3) BIOS machine ID
  456. * 4) Cpu temperature
  457. * 5) Left fan status
  458. * 6) Right fan status
  459. * 7) Left fan speed
  460. * 8) Right fan speed
  461. * 9) AC power
  462. * 10) Fn Key status
  463. */
  464. seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
  465. I8K_PROC_FMT,
  466. bios_version,
  467. (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
  468. cpu_temp,
  469. left_fan, right_fan, left_speed, right_speed,
  470. ac_power, fn_key);
  471. return 0;
  472. }
  473. static int i8k_open_fs(struct inode *inode, struct file *file)
  474. {
  475. return single_open(file, i8k_proc_show, NULL);
  476. }
  477. static const struct file_operations i8k_fops = {
  478. .owner = THIS_MODULE,
  479. .open = i8k_open_fs,
  480. .read = seq_read,
  481. .llseek = seq_lseek,
  482. .release = single_release,
  483. .unlocked_ioctl = i8k_ioctl,
  484. };
  485. static void __init i8k_init_procfs(void)
  486. {
  487. /* Register the proc entry */
  488. proc_create("i8k", 0, NULL, &i8k_fops);
  489. }
  490. static void __exit i8k_exit_procfs(void)
  491. {
  492. remove_proc_entry("i8k", NULL);
  493. }
  494. #else
  495. static inline void __init i8k_init_procfs(void)
  496. {
  497. }
  498. static inline void __exit i8k_exit_procfs(void)
  499. {
  500. }
  501. #endif
  502. /*
  503. * Hwmon interface
  504. */
  505. static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
  506. struct device_attribute *devattr,
  507. char *buf)
  508. {
  509. static const char * const labels[] = {
  510. "CPU",
  511. "GPU",
  512. "SODIMM",
  513. "Other",
  514. "Ambient",
  515. "Other",
  516. };
  517. int index = to_sensor_dev_attr(devattr)->index;
  518. int type;
  519. type = i8k_get_temp_type(index);
  520. if (type < 0)
  521. return type;
  522. if (type >= ARRAY_SIZE(labels))
  523. type = ARRAY_SIZE(labels) - 1;
  524. return sprintf(buf, "%s\n", labels[type]);
  525. }
  526. static ssize_t i8k_hwmon_show_temp(struct device *dev,
  527. struct device_attribute *devattr,
  528. char *buf)
  529. {
  530. int index = to_sensor_dev_attr(devattr)->index;
  531. int temp;
  532. temp = i8k_get_temp(index);
  533. if (temp < 0)
  534. return temp;
  535. return sprintf(buf, "%d\n", temp * 1000);
  536. }
  537. static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
  538. struct device_attribute *devattr,
  539. char *buf)
  540. {
  541. static const char * const labels[] = {
  542. "Processor Fan",
  543. "Motherboard Fan",
  544. "Video Fan",
  545. "Power Supply Fan",
  546. "Chipset Fan",
  547. "Other Fan",
  548. };
  549. int index = to_sensor_dev_attr(devattr)->index;
  550. bool dock = false;
  551. int type;
  552. type = i8k_get_fan_type(index);
  553. if (type < 0)
  554. return type;
  555. if (type & 0x10) {
  556. dock = true;
  557. type &= 0x0F;
  558. }
  559. if (type >= ARRAY_SIZE(labels))
  560. type = (ARRAY_SIZE(labels) - 1);
  561. return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
  562. }
  563. static ssize_t i8k_hwmon_show_fan(struct device *dev,
  564. struct device_attribute *devattr,
  565. char *buf)
  566. {
  567. int index = to_sensor_dev_attr(devattr)->index;
  568. int fan_speed;
  569. fan_speed = i8k_get_fan_speed(index);
  570. if (fan_speed < 0)
  571. return fan_speed;
  572. return sprintf(buf, "%d\n", fan_speed);
  573. }
  574. static ssize_t i8k_hwmon_show_pwm(struct device *dev,
  575. struct device_attribute *devattr,
  576. char *buf)
  577. {
  578. int index = to_sensor_dev_attr(devattr)->index;
  579. int status;
  580. status = i8k_get_fan_status(index);
  581. if (status < 0)
  582. return -EIO;
  583. return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
  584. }
  585. static ssize_t i8k_hwmon_set_pwm(struct device *dev,
  586. struct device_attribute *attr,
  587. const char *buf, size_t count)
  588. {
  589. int index = to_sensor_dev_attr(attr)->index;
  590. unsigned long val;
  591. int err;
  592. err = kstrtoul(buf, 10, &val);
  593. if (err)
  594. return err;
  595. val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
  596. mutex_lock(&i8k_mutex);
  597. err = i8k_set_fan(index, val);
  598. mutex_unlock(&i8k_mutex);
  599. return err < 0 ? -EIO : count;
  600. }
  601. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
  602. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  603. 0);
  604. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
  605. static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  606. 1);
  607. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
  608. static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  609. 2);
  610. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
  611. static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  612. 3);
  613. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
  614. static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
  615. 0);
  616. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  617. i8k_hwmon_set_pwm, 0);
  618. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  619. 1);
  620. static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
  621. 1);
  622. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  623. i8k_hwmon_set_pwm, 1);
  624. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  625. 2);
  626. static SENSOR_DEVICE_ATTR(fan3_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
  627. 2);
  628. static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  629. i8k_hwmon_set_pwm, 2);
  630. static struct attribute *i8k_attrs[] = {
  631. &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
  632. &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
  633. &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
  634. &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
  635. &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
  636. &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
  637. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
  638. &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
  639. &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
  640. &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
  641. &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
  642. &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
  643. &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
  644. &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
  645. &sensor_dev_attr_fan3_input.dev_attr.attr, /* 14 */
  646. &sensor_dev_attr_fan3_label.dev_attr.attr, /* 15 */
  647. &sensor_dev_attr_pwm3.dev_attr.attr, /* 16 */
  648. NULL
  649. };
  650. static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
  651. int index)
  652. {
  653. if (disallow_fan_type_call &&
  654. (index == 9 || index == 12 || index == 15))
  655. return 0;
  656. if (index >= 0 && index <= 1 &&
  657. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
  658. return 0;
  659. if (index >= 2 && index <= 3 &&
  660. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
  661. return 0;
  662. if (index >= 4 && index <= 5 &&
  663. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
  664. return 0;
  665. if (index >= 6 && index <= 7 &&
  666. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
  667. return 0;
  668. if (index >= 8 && index <= 10 &&
  669. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
  670. return 0;
  671. if (index >= 11 && index <= 13 &&
  672. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
  673. return 0;
  674. if (index >= 14 && index <= 16 &&
  675. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3))
  676. return 0;
  677. return attr->mode;
  678. }
  679. static const struct attribute_group i8k_group = {
  680. .attrs = i8k_attrs,
  681. .is_visible = i8k_is_visible,
  682. };
  683. __ATTRIBUTE_GROUPS(i8k);
  684. static int __init i8k_init_hwmon(void)
  685. {
  686. int err;
  687. i8k_hwmon_flags = 0;
  688. /* CPU temperature attributes, if temperature type is OK */
  689. err = i8k_get_temp_type(0);
  690. if (err >= 0)
  691. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
  692. /* check for additional temperature sensors */
  693. err = i8k_get_temp_type(1);
  694. if (err >= 0)
  695. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
  696. err = i8k_get_temp_type(2);
  697. if (err >= 0)
  698. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
  699. err = i8k_get_temp_type(3);
  700. if (err >= 0)
  701. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
  702. /* First fan attributes, if fan status or type is OK */
  703. err = i8k_get_fan_status(0);
  704. if (err < 0)
  705. err = i8k_get_fan_type(0);
  706. if (err >= 0)
  707. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
  708. /* Second fan attributes, if fan status or type is OK */
  709. err = i8k_get_fan_status(1);
  710. if (err < 0)
  711. err = i8k_get_fan_type(1);
  712. if (err >= 0)
  713. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
  714. /* Third fan attributes, if fan status or type is OK */
  715. err = i8k_get_fan_status(2);
  716. if (err < 0)
  717. err = i8k_get_fan_type(2);
  718. if (err >= 0)
  719. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3;
  720. i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
  721. NULL, i8k_groups);
  722. if (IS_ERR(i8k_hwmon_dev)) {
  723. err = PTR_ERR(i8k_hwmon_dev);
  724. i8k_hwmon_dev = NULL;
  725. pr_err("hwmon registration failed (%d)\n", err);
  726. return err;
  727. }
  728. return 0;
  729. }
  730. struct i8k_config_data {
  731. uint fan_mult;
  732. uint fan_max;
  733. };
  734. enum i8k_configs {
  735. DELL_LATITUDE_D520,
  736. DELL_PRECISION_490,
  737. DELL_STUDIO,
  738. DELL_XPS,
  739. };
  740. static const struct i8k_config_data i8k_config_data[] = {
  741. [DELL_LATITUDE_D520] = {
  742. .fan_mult = 1,
  743. .fan_max = I8K_FAN_TURBO,
  744. },
  745. [DELL_PRECISION_490] = {
  746. .fan_mult = 1,
  747. .fan_max = I8K_FAN_TURBO,
  748. },
  749. [DELL_STUDIO] = {
  750. .fan_mult = 1,
  751. .fan_max = I8K_FAN_HIGH,
  752. },
  753. [DELL_XPS] = {
  754. .fan_mult = 1,
  755. .fan_max = I8K_FAN_HIGH,
  756. },
  757. };
  758. static struct dmi_system_id i8k_dmi_table[] __initdata = {
  759. {
  760. .ident = "Dell Inspiron",
  761. .matches = {
  762. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  763. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  764. },
  765. },
  766. {
  767. .ident = "Dell Latitude",
  768. .matches = {
  769. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  770. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  771. },
  772. },
  773. {
  774. .ident = "Dell Inspiron 2",
  775. .matches = {
  776. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  777. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  778. },
  779. },
  780. {
  781. .ident = "Dell Latitude D520",
  782. .matches = {
  783. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  784. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
  785. },
  786. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
  787. },
  788. {
  789. .ident = "Dell Latitude 2",
  790. .matches = {
  791. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  792. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  793. },
  794. },
  795. { /* UK Inspiron 6400 */
  796. .ident = "Dell Inspiron 3",
  797. .matches = {
  798. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  799. DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
  800. },
  801. },
  802. {
  803. .ident = "Dell Inspiron 3",
  804. .matches = {
  805. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  806. DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
  807. },
  808. },
  809. {
  810. .ident = "Dell Precision 490",
  811. .matches = {
  812. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  813. DMI_MATCH(DMI_PRODUCT_NAME,
  814. "Precision WorkStation 490"),
  815. },
  816. .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
  817. },
  818. {
  819. .ident = "Dell Precision",
  820. .matches = {
  821. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  822. DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
  823. },
  824. },
  825. {
  826. .ident = "Dell Vostro",
  827. .matches = {
  828. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  829. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
  830. },
  831. },
  832. {
  833. .ident = "Dell XPS421",
  834. .matches = {
  835. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  836. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
  837. },
  838. },
  839. {
  840. .ident = "Dell Studio",
  841. .matches = {
  842. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  843. DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
  844. },
  845. .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
  846. },
  847. {
  848. .ident = "Dell XPS 13",
  849. .matches = {
  850. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  851. DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
  852. },
  853. .driver_data = (void *)&i8k_config_data[DELL_XPS],
  854. },
  855. {
  856. .ident = "Dell XPS M140",
  857. .matches = {
  858. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  859. DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
  860. },
  861. .driver_data = (void *)&i8k_config_data[DELL_XPS],
  862. },
  863. { }
  864. };
  865. MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
  866. /*
  867. * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
  868. * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
  869. * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
  870. * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
  871. */
  872. static struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initdata = {
  873. {
  874. .ident = "Dell Studio XPS 8000",
  875. .matches = {
  876. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  877. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
  878. },
  879. },
  880. {
  881. .ident = "Dell Studio XPS 8100",
  882. .matches = {
  883. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  884. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
  885. },
  886. },
  887. {
  888. .ident = "Dell Inspiron 580",
  889. .matches = {
  890. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  891. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
  892. },
  893. },
  894. { }
  895. };
  896. /*
  897. * Probe for the presence of a supported laptop.
  898. */
  899. static int __init i8k_probe(void)
  900. {
  901. const struct dmi_system_id *id;
  902. int fan, ret;
  903. /*
  904. * Get DMI information
  905. */
  906. if (!dmi_check_system(i8k_dmi_table)) {
  907. if (!ignore_dmi && !force)
  908. return -ENODEV;
  909. pr_info("not running on a supported Dell system.\n");
  910. pr_info("vendor=%s, model=%s, version=%s\n",
  911. i8k_get_dmi_data(DMI_SYS_VENDOR),
  912. i8k_get_dmi_data(DMI_PRODUCT_NAME),
  913. i8k_get_dmi_data(DMI_BIOS_VERSION));
  914. }
  915. if (dmi_check_system(i8k_blacklist_fan_type_dmi_table))
  916. disallow_fan_type_call = true;
  917. strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
  918. sizeof(bios_version));
  919. strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  920. sizeof(bios_machineid));
  921. /*
  922. * Get SMM Dell signature
  923. */
  924. if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
  925. i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
  926. pr_err("unable to get SMM Dell signature\n");
  927. if (!force)
  928. return -ENODEV;
  929. }
  930. /*
  931. * Set fan multiplier and maximal fan speed from dmi config
  932. * Values specified in module parameters override values from dmi
  933. */
  934. id = dmi_first_match(i8k_dmi_table);
  935. if (id && id->driver_data) {
  936. const struct i8k_config_data *conf = id->driver_data;
  937. if (!fan_mult && conf->fan_mult)
  938. fan_mult = conf->fan_mult;
  939. if (!fan_max && conf->fan_max)
  940. fan_max = conf->fan_max;
  941. }
  942. i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
  943. i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
  944. if (!fan_mult) {
  945. /*
  946. * Autodetect fan multiplier based on nominal rpm
  947. * If fan reports rpm value too high then set multiplier to 1
  948. */
  949. for (fan = 0; fan < 2; ++fan) {
  950. ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
  951. if (ret < 0)
  952. continue;
  953. if (ret > I8K_FAN_MAX_RPM)
  954. i8k_fan_mult = 1;
  955. break;
  956. }
  957. } else {
  958. /* Fan multiplier was specified in module param or in dmi */
  959. i8k_fan_mult = fan_mult;
  960. }
  961. return 0;
  962. }
  963. static int __init i8k_init(void)
  964. {
  965. int err;
  966. /* Are we running on an supported laptop? */
  967. if (i8k_probe())
  968. return -ENODEV;
  969. err = i8k_init_hwmon();
  970. if (err)
  971. return err;
  972. i8k_init_procfs();
  973. return 0;
  974. }
  975. static void __exit i8k_exit(void)
  976. {
  977. hwmon_device_unregister(i8k_hwmon_dev);
  978. i8k_exit_procfs();
  979. }
  980. module_init(i8k_init);
  981. module_exit(i8k_exit);