i8k.c 23 KB

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