i8k.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 Guenter Roeck <linux@roeck-us.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/init.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/dmi.h>
  27. #include <linux/capability.h>
  28. #include <linux/mutex.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/io.h>
  33. #include <linux/sched.h>
  34. #include <linux/i8k.h>
  35. #define I8K_SMM_FN_STATUS 0x0025
  36. #define I8K_SMM_POWER_STATUS 0x0069
  37. #define I8K_SMM_SET_FAN 0x01a3
  38. #define I8K_SMM_GET_FAN 0x00a3
  39. #define I8K_SMM_GET_SPEED 0x02a3
  40. #define I8K_SMM_GET_TEMP 0x10a3
  41. #define I8K_SMM_GET_DELL_SIG1 0xfea3
  42. #define I8K_SMM_GET_DELL_SIG2 0xffa3
  43. #define I8K_FAN_MULT 30
  44. #define I8K_MAX_TEMP 127
  45. #define I8K_FN_NONE 0x00
  46. #define I8K_FN_UP 0x01
  47. #define I8K_FN_DOWN 0x02
  48. #define I8K_FN_MUTE 0x04
  49. #define I8K_FN_MASK 0x07
  50. #define I8K_FN_SHIFT 8
  51. #define I8K_POWER_AC 0x05
  52. #define I8K_POWER_BATTERY 0x01
  53. #define I8K_TEMPERATURE_BUG 1
  54. static DEFINE_MUTEX(i8k_mutex);
  55. static char bios_version[4];
  56. static struct device *i8k_hwmon_dev;
  57. static u32 i8k_hwmon_flags;
  58. static int i8k_fan_mult;
  59. static int i8k_pwm_mult;
  60. static int i8k_fan_max = I8K_FAN_HIGH;
  61. #define I8K_HWMON_HAVE_TEMP1 (1 << 0)
  62. #define I8K_HWMON_HAVE_TEMP2 (1 << 1)
  63. #define I8K_HWMON_HAVE_TEMP3 (1 << 2)
  64. #define I8K_HWMON_HAVE_TEMP4 (1 << 3)
  65. #define I8K_HWMON_HAVE_FAN1 (1 << 4)
  66. #define I8K_HWMON_HAVE_FAN2 (1 << 5)
  67. MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
  68. MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
  69. MODULE_LICENSE("GPL");
  70. static bool force;
  71. module_param(force, bool, 0);
  72. MODULE_PARM_DESC(force, "Force loading without checking for supported models");
  73. static bool ignore_dmi;
  74. module_param(ignore_dmi, bool, 0);
  75. MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
  76. static bool restricted;
  77. module_param(restricted, bool, 0);
  78. MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
  79. static bool power_status;
  80. module_param(power_status, bool, 0600);
  81. MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
  82. static int fan_mult = I8K_FAN_MULT;
  83. module_param(fan_mult, int, 0);
  84. MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with");
  85. static int fan_max = I8K_FAN_HIGH;
  86. module_param(fan_max, int, 0);
  87. MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed");
  88. static int i8k_open_fs(struct inode *inode, struct file *file);
  89. static long i8k_ioctl(struct file *, unsigned int, unsigned long);
  90. static const struct file_operations i8k_fops = {
  91. .owner = THIS_MODULE,
  92. .open = i8k_open_fs,
  93. .read = seq_read,
  94. .llseek = seq_lseek,
  95. .release = single_release,
  96. .unlocked_ioctl = i8k_ioctl,
  97. };
  98. struct smm_regs {
  99. unsigned int eax;
  100. unsigned int ebx __packed;
  101. unsigned int ecx __packed;
  102. unsigned int edx __packed;
  103. unsigned int esi __packed;
  104. unsigned int edi __packed;
  105. };
  106. static inline const char *i8k_get_dmi_data(int field)
  107. {
  108. const char *dmi_data = dmi_get_system_info(field);
  109. return dmi_data && *dmi_data ? dmi_data : "?";
  110. }
  111. /*
  112. * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
  113. */
  114. static int i8k_smm(struct smm_regs *regs)
  115. {
  116. int rc;
  117. int eax = regs->eax;
  118. cpumask_var_t old_mask;
  119. /* SMM requires CPU 0 */
  120. if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
  121. return -ENOMEM;
  122. cpumask_copy(old_mask, &current->cpus_allowed);
  123. rc = set_cpus_allowed_ptr(current, cpumask_of(0));
  124. if (rc)
  125. goto out;
  126. if (smp_processor_id() != 0) {
  127. rc = -EBUSY;
  128. goto out;
  129. }
  130. #if defined(CONFIG_X86_64)
  131. asm volatile("pushq %%rax\n\t"
  132. "movl 0(%%rax),%%edx\n\t"
  133. "pushq %%rdx\n\t"
  134. "movl 4(%%rax),%%ebx\n\t"
  135. "movl 8(%%rax),%%ecx\n\t"
  136. "movl 12(%%rax),%%edx\n\t"
  137. "movl 16(%%rax),%%esi\n\t"
  138. "movl 20(%%rax),%%edi\n\t"
  139. "popq %%rax\n\t"
  140. "out %%al,$0xb2\n\t"
  141. "out %%al,$0x84\n\t"
  142. "xchgq %%rax,(%%rsp)\n\t"
  143. "movl %%ebx,4(%%rax)\n\t"
  144. "movl %%ecx,8(%%rax)\n\t"
  145. "movl %%edx,12(%%rax)\n\t"
  146. "movl %%esi,16(%%rax)\n\t"
  147. "movl %%edi,20(%%rax)\n\t"
  148. "popq %%rdx\n\t"
  149. "movl %%edx,0(%%rax)\n\t"
  150. "pushfq\n\t"
  151. "popq %%rax\n\t"
  152. "andl $1,%%eax\n"
  153. : "=a"(rc)
  154. : "a"(regs)
  155. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  156. #else
  157. asm volatile("pushl %%eax\n\t"
  158. "movl 0(%%eax),%%edx\n\t"
  159. "push %%edx\n\t"
  160. "movl 4(%%eax),%%ebx\n\t"
  161. "movl 8(%%eax),%%ecx\n\t"
  162. "movl 12(%%eax),%%edx\n\t"
  163. "movl 16(%%eax),%%esi\n\t"
  164. "movl 20(%%eax),%%edi\n\t"
  165. "popl %%eax\n\t"
  166. "out %%al,$0xb2\n\t"
  167. "out %%al,$0x84\n\t"
  168. "xchgl %%eax,(%%esp)\n\t"
  169. "movl %%ebx,4(%%eax)\n\t"
  170. "movl %%ecx,8(%%eax)\n\t"
  171. "movl %%edx,12(%%eax)\n\t"
  172. "movl %%esi,16(%%eax)\n\t"
  173. "movl %%edi,20(%%eax)\n\t"
  174. "popl %%edx\n\t"
  175. "movl %%edx,0(%%eax)\n\t"
  176. "lahf\n\t"
  177. "shrl $8,%%eax\n\t"
  178. "andl $1,%%eax\n"
  179. : "=a"(rc)
  180. : "a"(regs)
  181. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  182. #endif
  183. if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
  184. rc = -EINVAL;
  185. out:
  186. set_cpus_allowed_ptr(current, old_mask);
  187. free_cpumask_var(old_mask);
  188. return rc;
  189. }
  190. /*
  191. * Read the Fn key status.
  192. */
  193. static int i8k_get_fn_status(void)
  194. {
  195. struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
  196. int rc;
  197. rc = i8k_smm(&regs);
  198. if (rc < 0)
  199. return rc;
  200. switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
  201. case I8K_FN_UP:
  202. return I8K_VOL_UP;
  203. case I8K_FN_DOWN:
  204. return I8K_VOL_DOWN;
  205. case I8K_FN_MUTE:
  206. return I8K_VOL_MUTE;
  207. default:
  208. return 0;
  209. }
  210. }
  211. /*
  212. * Read the power status.
  213. */
  214. static int i8k_get_power_status(void)
  215. {
  216. struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
  217. int rc;
  218. rc = i8k_smm(&regs);
  219. if (rc < 0)
  220. return rc;
  221. return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
  222. }
  223. /*
  224. * Read the fan status.
  225. */
  226. static int i8k_get_fan_status(int fan)
  227. {
  228. struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
  229. regs.ebx = fan & 0xff;
  230. return i8k_smm(&regs) ? : regs.eax & 0xff;
  231. }
  232. /*
  233. * Read the fan speed in RPM.
  234. */
  235. static int i8k_get_fan_speed(int fan)
  236. {
  237. struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
  238. regs.ebx = fan & 0xff;
  239. return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
  240. }
  241. /*
  242. * Set the fan speed (off, low, high). Returns the new fan status.
  243. */
  244. static int i8k_set_fan(int fan, int speed)
  245. {
  246. struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
  247. speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
  248. regs.ebx = (fan & 0xff) | (speed << 8);
  249. return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
  250. }
  251. /*
  252. * Read the cpu temperature.
  253. */
  254. static int i8k_get_temp(int sensor)
  255. {
  256. struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
  257. int rc;
  258. int temp;
  259. #ifdef I8K_TEMPERATURE_BUG
  260. static int prev[4];
  261. #endif
  262. regs.ebx = sensor & 0xff;
  263. rc = i8k_smm(&regs);
  264. if (rc < 0)
  265. return rc;
  266. temp = regs.eax & 0xff;
  267. #ifdef I8K_TEMPERATURE_BUG
  268. /*
  269. * Sometimes the temperature sensor returns 0x99, which is out of range.
  270. * In this case we return (once) the previous cached value. For example:
  271. # 1003655137 00000058 00005a4b
  272. # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
  273. # 1003655139 00000054 00005c52
  274. */
  275. if (temp > I8K_MAX_TEMP) {
  276. temp = prev[sensor];
  277. prev[sensor] = I8K_MAX_TEMP;
  278. } else {
  279. prev[sensor] = temp;
  280. }
  281. #endif
  282. return temp;
  283. }
  284. static int i8k_get_dell_signature(int req_fn)
  285. {
  286. struct smm_regs regs = { .eax = req_fn, };
  287. int rc;
  288. rc = i8k_smm(&regs);
  289. if (rc < 0)
  290. return rc;
  291. return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
  292. }
  293. static int
  294. i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
  295. {
  296. int val = 0;
  297. int speed;
  298. unsigned char buff[16];
  299. int __user *argp = (int __user *)arg;
  300. if (!argp)
  301. return -EINVAL;
  302. switch (cmd) {
  303. case I8K_BIOS_VERSION:
  304. val = (bios_version[0] << 16) |
  305. (bios_version[1] << 8) | bios_version[2];
  306. break;
  307. case I8K_MACHINE_ID:
  308. memset(buff, 0, 16);
  309. strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  310. sizeof(buff));
  311. break;
  312. case I8K_FN_STATUS:
  313. val = i8k_get_fn_status();
  314. break;
  315. case I8K_POWER_STATUS:
  316. val = i8k_get_power_status();
  317. break;
  318. case I8K_GET_TEMP:
  319. val = i8k_get_temp(0);
  320. break;
  321. case I8K_GET_SPEED:
  322. if (copy_from_user(&val, argp, sizeof(int)))
  323. return -EFAULT;
  324. val = i8k_get_fan_speed(val);
  325. break;
  326. case I8K_GET_FAN:
  327. if (copy_from_user(&val, argp, sizeof(int)))
  328. return -EFAULT;
  329. val = i8k_get_fan_status(val);
  330. break;
  331. case I8K_SET_FAN:
  332. if (restricted && !capable(CAP_SYS_ADMIN))
  333. return -EPERM;
  334. if (copy_from_user(&val, argp, sizeof(int)))
  335. return -EFAULT;
  336. if (copy_from_user(&speed, argp + 1, sizeof(int)))
  337. return -EFAULT;
  338. val = i8k_set_fan(val, speed);
  339. break;
  340. default:
  341. return -EINVAL;
  342. }
  343. if (val < 0)
  344. return val;
  345. switch (cmd) {
  346. case I8K_BIOS_VERSION:
  347. if (copy_to_user(argp, &val, 4))
  348. return -EFAULT;
  349. break;
  350. case I8K_MACHINE_ID:
  351. if (copy_to_user(argp, buff, 16))
  352. return -EFAULT;
  353. break;
  354. default:
  355. if (copy_to_user(argp, &val, sizeof(int)))
  356. return -EFAULT;
  357. break;
  358. }
  359. return 0;
  360. }
  361. static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  362. {
  363. long ret;
  364. mutex_lock(&i8k_mutex);
  365. ret = i8k_ioctl_unlocked(fp, cmd, arg);
  366. mutex_unlock(&i8k_mutex);
  367. return ret;
  368. }
  369. /*
  370. * Print the information for /proc/i8k.
  371. */
  372. static int i8k_proc_show(struct seq_file *seq, void *offset)
  373. {
  374. int fn_key, cpu_temp, ac_power;
  375. int left_fan, right_fan, left_speed, right_speed;
  376. cpu_temp = i8k_get_temp(0); /* 11100 µs */
  377. left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
  378. right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
  379. left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
  380. right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
  381. fn_key = i8k_get_fn_status(); /* 750 µs */
  382. if (power_status)
  383. ac_power = i8k_get_power_status(); /* 14700 µs */
  384. else
  385. ac_power = -1;
  386. /*
  387. * Info:
  388. *
  389. * 1) Format version (this will change if format changes)
  390. * 2) BIOS version
  391. * 3) BIOS machine ID
  392. * 4) Cpu temperature
  393. * 5) Left fan status
  394. * 6) Right fan status
  395. * 7) Left fan speed
  396. * 8) Right fan speed
  397. * 9) AC power
  398. * 10) Fn Key status
  399. */
  400. return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
  401. I8K_PROC_FMT,
  402. bios_version,
  403. i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  404. cpu_temp,
  405. left_fan, right_fan, left_speed, right_speed,
  406. ac_power, fn_key);
  407. }
  408. static int i8k_open_fs(struct inode *inode, struct file *file)
  409. {
  410. return single_open(file, i8k_proc_show, NULL);
  411. }
  412. /*
  413. * Hwmon interface
  414. */
  415. static ssize_t i8k_hwmon_show_temp(struct device *dev,
  416. struct device_attribute *devattr,
  417. char *buf)
  418. {
  419. int index = to_sensor_dev_attr(devattr)->index;
  420. int temp;
  421. temp = i8k_get_temp(index);
  422. if (temp < 0)
  423. return temp;
  424. return sprintf(buf, "%d\n", temp * 1000);
  425. }
  426. static ssize_t i8k_hwmon_show_fan(struct device *dev,
  427. struct device_attribute *devattr,
  428. char *buf)
  429. {
  430. int index = to_sensor_dev_attr(devattr)->index;
  431. int fan_speed;
  432. fan_speed = i8k_get_fan_speed(index);
  433. if (fan_speed < 0)
  434. return fan_speed;
  435. return sprintf(buf, "%d\n", fan_speed);
  436. }
  437. static ssize_t i8k_hwmon_show_pwm(struct device *dev,
  438. struct device_attribute *devattr,
  439. char *buf)
  440. {
  441. int index = to_sensor_dev_attr(devattr)->index;
  442. int status;
  443. status = i8k_get_fan_status(index);
  444. if (status < 0)
  445. return -EIO;
  446. return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
  447. }
  448. static ssize_t i8k_hwmon_set_pwm(struct device *dev,
  449. struct device_attribute *attr,
  450. const char *buf, size_t count)
  451. {
  452. int index = to_sensor_dev_attr(attr)->index;
  453. unsigned long val;
  454. int err;
  455. err = kstrtoul(buf, 10, &val);
  456. if (err)
  457. return err;
  458. val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
  459. mutex_lock(&i8k_mutex);
  460. err = i8k_set_fan(index, val);
  461. mutex_unlock(&i8k_mutex);
  462. return err < 0 ? -EIO : count;
  463. }
  464. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
  465. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
  466. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
  467. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
  468. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  469. I8K_FAN_LEFT);
  470. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  471. i8k_hwmon_set_pwm, I8K_FAN_LEFT);
  472. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  473. I8K_FAN_RIGHT);
  474. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  475. i8k_hwmon_set_pwm, I8K_FAN_RIGHT);
  476. static struct attribute *i8k_attrs[] = {
  477. &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
  478. &sensor_dev_attr_temp2_input.dev_attr.attr, /* 1 */
  479. &sensor_dev_attr_temp3_input.dev_attr.attr, /* 2 */
  480. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 3 */
  481. &sensor_dev_attr_fan1_input.dev_attr.attr, /* 4 */
  482. &sensor_dev_attr_pwm1.dev_attr.attr, /* 5 */
  483. &sensor_dev_attr_fan2_input.dev_attr.attr, /* 6 */
  484. &sensor_dev_attr_pwm2.dev_attr.attr, /* 7 */
  485. NULL
  486. };
  487. static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
  488. int index)
  489. {
  490. if (index == 0 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
  491. return 0;
  492. if (index == 1 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
  493. return 0;
  494. if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
  495. return 0;
  496. if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
  497. return 0;
  498. if (index >= 4 && index <= 5 &&
  499. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
  500. return 0;
  501. if (index >= 6 && index <= 7 &&
  502. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
  503. return 0;
  504. return attr->mode;
  505. }
  506. static const struct attribute_group i8k_group = {
  507. .attrs = i8k_attrs,
  508. .is_visible = i8k_is_visible,
  509. };
  510. __ATTRIBUTE_GROUPS(i8k);
  511. static int __init i8k_init_hwmon(void)
  512. {
  513. int err;
  514. i8k_hwmon_flags = 0;
  515. /* CPU temperature attributes, if temperature reading is OK */
  516. err = i8k_get_temp(0);
  517. if (err >= 0)
  518. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
  519. /* check for additional temperature sensors */
  520. err = i8k_get_temp(1);
  521. if (err >= 0)
  522. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
  523. err = i8k_get_temp(2);
  524. if (err >= 0)
  525. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
  526. err = i8k_get_temp(3);
  527. if (err >= 0)
  528. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
  529. /* Left fan attributes, if left fan is present */
  530. err = i8k_get_fan_status(I8K_FAN_LEFT);
  531. if (err >= 0)
  532. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
  533. /* Right fan attributes, if right fan is present */
  534. err = i8k_get_fan_status(I8K_FAN_RIGHT);
  535. if (err >= 0)
  536. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
  537. i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
  538. i8k_groups);
  539. if (IS_ERR(i8k_hwmon_dev)) {
  540. err = PTR_ERR(i8k_hwmon_dev);
  541. i8k_hwmon_dev = NULL;
  542. pr_err("hwmon registration failed (%d)\n", err);
  543. return err;
  544. }
  545. return 0;
  546. }
  547. struct i8k_config_data {
  548. int fan_mult;
  549. int fan_max;
  550. };
  551. enum i8k_configs {
  552. DELL_LATITUDE_D520,
  553. DELL_LATITUDE_E6540,
  554. DELL_PRECISION_490,
  555. DELL_STUDIO,
  556. DELL_XPS_M140,
  557. };
  558. static const struct i8k_config_data i8k_config_data[] = {
  559. [DELL_LATITUDE_D520] = {
  560. .fan_mult = 1,
  561. .fan_max = I8K_FAN_TURBO,
  562. },
  563. [DELL_LATITUDE_E6540] = {
  564. .fan_mult = 1,
  565. .fan_max = I8K_FAN_HIGH,
  566. },
  567. [DELL_PRECISION_490] = {
  568. .fan_mult = 1,
  569. .fan_max = I8K_FAN_TURBO,
  570. },
  571. [DELL_STUDIO] = {
  572. .fan_mult = 1,
  573. .fan_max = I8K_FAN_HIGH,
  574. },
  575. [DELL_XPS_M140] = {
  576. .fan_mult = 1,
  577. .fan_max = I8K_FAN_HIGH,
  578. },
  579. };
  580. static struct dmi_system_id i8k_dmi_table[] __initdata = {
  581. {
  582. .ident = "Dell Inspiron",
  583. .matches = {
  584. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  585. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  586. },
  587. },
  588. {
  589. .ident = "Dell Latitude",
  590. .matches = {
  591. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  592. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  593. },
  594. },
  595. {
  596. .ident = "Dell Inspiron 2",
  597. .matches = {
  598. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  599. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  600. },
  601. },
  602. {
  603. .ident = "Dell Latitude D520",
  604. .matches = {
  605. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  606. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
  607. },
  608. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
  609. },
  610. {
  611. .ident = "Dell Latitude E6540",
  612. .matches = {
  613. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  614. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6540"),
  615. },
  616. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
  617. },
  618. {
  619. .ident = "Dell Latitude 2",
  620. .matches = {
  621. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  622. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  623. },
  624. },
  625. { /* UK Inspiron 6400 */
  626. .ident = "Dell Inspiron 3",
  627. .matches = {
  628. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  629. DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
  630. },
  631. },
  632. {
  633. .ident = "Dell Inspiron 3",
  634. .matches = {
  635. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  636. DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
  637. },
  638. },
  639. {
  640. .ident = "Dell Precision 490",
  641. .matches = {
  642. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  643. DMI_MATCH(DMI_PRODUCT_NAME,
  644. "Precision WorkStation 490"),
  645. },
  646. .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
  647. },
  648. {
  649. .ident = "Dell Precision",
  650. .matches = {
  651. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  652. DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
  653. },
  654. },
  655. {
  656. .ident = "Dell Vostro",
  657. .matches = {
  658. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  659. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
  660. },
  661. },
  662. {
  663. .ident = "Dell XPS421",
  664. .matches = {
  665. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  666. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
  667. },
  668. },
  669. {
  670. .ident = "Dell Studio",
  671. .matches = {
  672. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  673. DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
  674. },
  675. .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
  676. },
  677. {
  678. .ident = "Dell XPS M140",
  679. .matches = {
  680. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  681. DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
  682. },
  683. .driver_data = (void *)&i8k_config_data[DELL_XPS_M140],
  684. },
  685. { }
  686. };
  687. /*
  688. * Probe for the presence of a supported laptop.
  689. */
  690. static int __init i8k_probe(void)
  691. {
  692. const struct dmi_system_id *id;
  693. /*
  694. * Get DMI information
  695. */
  696. if (!dmi_check_system(i8k_dmi_table)) {
  697. if (!ignore_dmi && !force)
  698. return -ENODEV;
  699. pr_info("not running on a supported Dell system.\n");
  700. pr_info("vendor=%s, model=%s, version=%s\n",
  701. i8k_get_dmi_data(DMI_SYS_VENDOR),
  702. i8k_get_dmi_data(DMI_PRODUCT_NAME),
  703. i8k_get_dmi_data(DMI_BIOS_VERSION));
  704. }
  705. strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
  706. sizeof(bios_version));
  707. /*
  708. * Get SMM Dell signature
  709. */
  710. if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
  711. i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
  712. pr_err("unable to get SMM Dell signature\n");
  713. if (!force)
  714. return -ENODEV;
  715. }
  716. i8k_fan_mult = fan_mult;
  717. i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
  718. id = dmi_first_match(i8k_dmi_table);
  719. if (id && id->driver_data) {
  720. const struct i8k_config_data *conf = id->driver_data;
  721. if (fan_mult == I8K_FAN_MULT && conf->fan_mult)
  722. i8k_fan_mult = conf->fan_mult;
  723. if (fan_max == I8K_FAN_HIGH && conf->fan_max)
  724. i8k_fan_max = conf->fan_max;
  725. }
  726. i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
  727. return 0;
  728. }
  729. static int __init i8k_init(void)
  730. {
  731. struct proc_dir_entry *proc_i8k;
  732. int err;
  733. /* Are we running on an supported laptop? */
  734. if (i8k_probe())
  735. return -ENODEV;
  736. /* Register the proc entry */
  737. proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
  738. if (!proc_i8k)
  739. return -ENOENT;
  740. err = i8k_init_hwmon();
  741. if (err)
  742. goto exit_remove_proc;
  743. return 0;
  744. exit_remove_proc:
  745. remove_proc_entry("i8k", NULL);
  746. return err;
  747. }
  748. static void __exit i8k_exit(void)
  749. {
  750. hwmon_device_unregister(i8k_hwmon_dev);
  751. remove_proc_entry("i8k", NULL);
  752. }
  753. module_init(i8k_init);
  754. module_exit(i8k_exit);