dell-smm-hwmon.c 25 KB

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