i8k.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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] = { I8K_MAX_TEMP+1, I8K_MAX_TEMP+1, I8K_MAX_TEMP+1, I8K_MAX_TEMP+1 };
  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+1;
  278. } else {
  279. prev[sensor] = temp;
  280. }
  281. if (temp > I8K_MAX_TEMP)
  282. return -ERANGE;
  283. #endif
  284. return temp;
  285. }
  286. static int i8k_get_dell_signature(int req_fn)
  287. {
  288. struct smm_regs regs = { .eax = req_fn, };
  289. int rc;
  290. rc = i8k_smm(&regs);
  291. if (rc < 0)
  292. return rc;
  293. return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
  294. }
  295. static int
  296. i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
  297. {
  298. int val = 0;
  299. int speed;
  300. unsigned char buff[16];
  301. int __user *argp = (int __user *)arg;
  302. if (!argp)
  303. return -EINVAL;
  304. switch (cmd) {
  305. case I8K_BIOS_VERSION:
  306. val = (bios_version[0] << 16) |
  307. (bios_version[1] << 8) | bios_version[2];
  308. break;
  309. case I8K_MACHINE_ID:
  310. memset(buff, 0, 16);
  311. strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  312. sizeof(buff));
  313. break;
  314. case I8K_FN_STATUS:
  315. val = i8k_get_fn_status();
  316. break;
  317. case I8K_POWER_STATUS:
  318. val = i8k_get_power_status();
  319. break;
  320. case I8K_GET_TEMP:
  321. val = i8k_get_temp(0);
  322. break;
  323. case I8K_GET_SPEED:
  324. if (copy_from_user(&val, argp, sizeof(int)))
  325. return -EFAULT;
  326. val = i8k_get_fan_speed(val);
  327. break;
  328. case I8K_GET_FAN:
  329. if (copy_from_user(&val, argp, sizeof(int)))
  330. return -EFAULT;
  331. val = i8k_get_fan_status(val);
  332. break;
  333. case I8K_SET_FAN:
  334. if (restricted && !capable(CAP_SYS_ADMIN))
  335. return -EPERM;
  336. if (copy_from_user(&val, argp, sizeof(int)))
  337. return -EFAULT;
  338. if (copy_from_user(&speed, argp + 1, sizeof(int)))
  339. return -EFAULT;
  340. val = i8k_set_fan(val, speed);
  341. break;
  342. default:
  343. return -EINVAL;
  344. }
  345. if (val < 0)
  346. return val;
  347. switch (cmd) {
  348. case I8K_BIOS_VERSION:
  349. if (copy_to_user(argp, &val, 4))
  350. return -EFAULT;
  351. break;
  352. case I8K_MACHINE_ID:
  353. if (copy_to_user(argp, buff, 16))
  354. return -EFAULT;
  355. break;
  356. default:
  357. if (copy_to_user(argp, &val, sizeof(int)))
  358. return -EFAULT;
  359. break;
  360. }
  361. return 0;
  362. }
  363. static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  364. {
  365. long ret;
  366. mutex_lock(&i8k_mutex);
  367. ret = i8k_ioctl_unlocked(fp, cmd, arg);
  368. mutex_unlock(&i8k_mutex);
  369. return ret;
  370. }
  371. /*
  372. * Print the information for /proc/i8k.
  373. */
  374. static int i8k_proc_show(struct seq_file *seq, void *offset)
  375. {
  376. int fn_key, cpu_temp, ac_power;
  377. int left_fan, right_fan, left_speed, right_speed;
  378. cpu_temp = i8k_get_temp(0); /* 11100 µs */
  379. left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
  380. right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
  381. left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
  382. right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
  383. fn_key = i8k_get_fn_status(); /* 750 µs */
  384. if (power_status)
  385. ac_power = i8k_get_power_status(); /* 14700 µs */
  386. else
  387. ac_power = -1;
  388. /*
  389. * Info:
  390. *
  391. * 1) Format version (this will change if format changes)
  392. * 2) BIOS version
  393. * 3) BIOS machine ID
  394. * 4) Cpu temperature
  395. * 5) Left fan status
  396. * 6) Right fan status
  397. * 7) Left fan speed
  398. * 8) Right fan speed
  399. * 9) AC power
  400. * 10) Fn Key status
  401. */
  402. return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
  403. I8K_PROC_FMT,
  404. bios_version,
  405. i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  406. cpu_temp,
  407. left_fan, right_fan, left_speed, right_speed,
  408. ac_power, fn_key);
  409. }
  410. static int i8k_open_fs(struct inode *inode, struct file *file)
  411. {
  412. return single_open(file, i8k_proc_show, NULL);
  413. }
  414. /*
  415. * Hwmon interface
  416. */
  417. static ssize_t i8k_hwmon_show_temp(struct device *dev,
  418. struct device_attribute *devattr,
  419. char *buf)
  420. {
  421. int index = to_sensor_dev_attr(devattr)->index;
  422. int temp;
  423. temp = i8k_get_temp(index);
  424. if (temp == -ERANGE)
  425. return -EINVAL;
  426. if (temp < 0)
  427. return temp;
  428. return sprintf(buf, "%d\n", temp * 1000);
  429. }
  430. static ssize_t i8k_hwmon_show_fan(struct device *dev,
  431. struct device_attribute *devattr,
  432. char *buf)
  433. {
  434. int index = to_sensor_dev_attr(devattr)->index;
  435. int fan_speed;
  436. fan_speed = i8k_get_fan_speed(index);
  437. if (fan_speed < 0)
  438. return fan_speed;
  439. return sprintf(buf, "%d\n", fan_speed);
  440. }
  441. static ssize_t i8k_hwmon_show_pwm(struct device *dev,
  442. struct device_attribute *devattr,
  443. char *buf)
  444. {
  445. int index = to_sensor_dev_attr(devattr)->index;
  446. int status;
  447. status = i8k_get_fan_status(index);
  448. if (status < 0)
  449. return -EIO;
  450. return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
  451. }
  452. static ssize_t i8k_hwmon_set_pwm(struct device *dev,
  453. struct device_attribute *attr,
  454. const char *buf, size_t count)
  455. {
  456. int index = to_sensor_dev_attr(attr)->index;
  457. unsigned long val;
  458. int err;
  459. err = kstrtoul(buf, 10, &val);
  460. if (err)
  461. return err;
  462. val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
  463. mutex_lock(&i8k_mutex);
  464. err = i8k_set_fan(index, val);
  465. mutex_unlock(&i8k_mutex);
  466. return err < 0 ? -EIO : count;
  467. }
  468. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
  469. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
  470. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
  471. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
  472. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  473. I8K_FAN_LEFT);
  474. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  475. i8k_hwmon_set_pwm, I8K_FAN_LEFT);
  476. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  477. I8K_FAN_RIGHT);
  478. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  479. i8k_hwmon_set_pwm, I8K_FAN_RIGHT);
  480. static struct attribute *i8k_attrs[] = {
  481. &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
  482. &sensor_dev_attr_temp2_input.dev_attr.attr, /* 1 */
  483. &sensor_dev_attr_temp3_input.dev_attr.attr, /* 2 */
  484. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 3 */
  485. &sensor_dev_attr_fan1_input.dev_attr.attr, /* 4 */
  486. &sensor_dev_attr_pwm1.dev_attr.attr, /* 5 */
  487. &sensor_dev_attr_fan2_input.dev_attr.attr, /* 6 */
  488. &sensor_dev_attr_pwm2.dev_attr.attr, /* 7 */
  489. NULL
  490. };
  491. static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
  492. int index)
  493. {
  494. if (index == 0 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
  495. return 0;
  496. if (index == 1 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
  497. return 0;
  498. if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
  499. return 0;
  500. if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
  501. return 0;
  502. if (index >= 4 && index <= 5 &&
  503. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
  504. return 0;
  505. if (index >= 6 && index <= 7 &&
  506. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
  507. return 0;
  508. return attr->mode;
  509. }
  510. static const struct attribute_group i8k_group = {
  511. .attrs = i8k_attrs,
  512. .is_visible = i8k_is_visible,
  513. };
  514. __ATTRIBUTE_GROUPS(i8k);
  515. static int __init i8k_init_hwmon(void)
  516. {
  517. int err;
  518. i8k_hwmon_flags = 0;
  519. /* CPU temperature attributes, if temperature reading is OK */
  520. err = i8k_get_temp(0);
  521. if (err >= 0 || err == -ERANGE)
  522. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
  523. /* check for additional temperature sensors */
  524. err = i8k_get_temp(1);
  525. if (err >= 0 || err == -ERANGE)
  526. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
  527. err = i8k_get_temp(2);
  528. if (err >= 0 || err == -ERANGE)
  529. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
  530. err = i8k_get_temp(3);
  531. if (err >= 0 || err == -ERANGE)
  532. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
  533. /* Left fan attributes, if left fan is present */
  534. err = i8k_get_fan_status(I8K_FAN_LEFT);
  535. if (err >= 0)
  536. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
  537. /* Right fan attributes, if right fan is present */
  538. err = i8k_get_fan_status(I8K_FAN_RIGHT);
  539. if (err >= 0)
  540. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
  541. i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
  542. i8k_groups);
  543. if (IS_ERR(i8k_hwmon_dev)) {
  544. err = PTR_ERR(i8k_hwmon_dev);
  545. i8k_hwmon_dev = NULL;
  546. pr_err("hwmon registration failed (%d)\n", err);
  547. return err;
  548. }
  549. return 0;
  550. }
  551. struct i8k_config_data {
  552. int fan_mult;
  553. int fan_max;
  554. };
  555. enum i8k_configs {
  556. DELL_LATITUDE_D520,
  557. DELL_LATITUDE_E6540,
  558. DELL_PRECISION_490,
  559. DELL_STUDIO,
  560. DELL_XPS_M140,
  561. };
  562. static const struct i8k_config_data i8k_config_data[] = {
  563. [DELL_LATITUDE_D520] = {
  564. .fan_mult = 1,
  565. .fan_max = I8K_FAN_TURBO,
  566. },
  567. [DELL_LATITUDE_E6540] = {
  568. .fan_mult = 1,
  569. .fan_max = I8K_FAN_HIGH,
  570. },
  571. [DELL_PRECISION_490] = {
  572. .fan_mult = 1,
  573. .fan_max = I8K_FAN_TURBO,
  574. },
  575. [DELL_STUDIO] = {
  576. .fan_mult = 1,
  577. .fan_max = I8K_FAN_HIGH,
  578. },
  579. [DELL_XPS_M140] = {
  580. .fan_mult = 1,
  581. .fan_max = I8K_FAN_HIGH,
  582. },
  583. };
  584. static struct dmi_system_id i8k_dmi_table[] __initdata = {
  585. {
  586. .ident = "Dell Inspiron",
  587. .matches = {
  588. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  589. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  590. },
  591. },
  592. {
  593. .ident = "Dell Latitude",
  594. .matches = {
  595. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  596. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  597. },
  598. },
  599. {
  600. .ident = "Dell Inspiron 2",
  601. .matches = {
  602. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  603. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  604. },
  605. },
  606. {
  607. .ident = "Dell Latitude D520",
  608. .matches = {
  609. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  610. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
  611. },
  612. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
  613. },
  614. {
  615. .ident = "Dell Latitude E6440",
  616. .matches = {
  617. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  618. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
  619. },
  620. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
  621. },
  622. {
  623. .ident = "Dell Latitude E6540",
  624. .matches = {
  625. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  626. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6540"),
  627. },
  628. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
  629. },
  630. {
  631. .ident = "Dell Latitude 2",
  632. .matches = {
  633. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  634. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  635. },
  636. },
  637. { /* UK Inspiron 6400 */
  638. .ident = "Dell Inspiron 3",
  639. .matches = {
  640. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  641. DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
  642. },
  643. },
  644. {
  645. .ident = "Dell Inspiron 3",
  646. .matches = {
  647. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  648. DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
  649. },
  650. },
  651. {
  652. .ident = "Dell Precision 490",
  653. .matches = {
  654. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  655. DMI_MATCH(DMI_PRODUCT_NAME,
  656. "Precision WorkStation 490"),
  657. },
  658. .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
  659. },
  660. {
  661. .ident = "Dell Precision",
  662. .matches = {
  663. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  664. DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
  665. },
  666. },
  667. {
  668. .ident = "Dell Vostro",
  669. .matches = {
  670. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  671. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
  672. },
  673. },
  674. {
  675. .ident = "Dell XPS421",
  676. .matches = {
  677. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  678. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
  679. },
  680. },
  681. {
  682. .ident = "Dell Studio",
  683. .matches = {
  684. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  685. DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
  686. },
  687. .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
  688. },
  689. {
  690. .ident = "Dell XPS M140",
  691. .matches = {
  692. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  693. DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
  694. },
  695. .driver_data = (void *)&i8k_config_data[DELL_XPS_M140],
  696. },
  697. { }
  698. };
  699. MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
  700. /*
  701. * Probe for the presence of a supported laptop.
  702. */
  703. static int __init i8k_probe(void)
  704. {
  705. const struct dmi_system_id *id;
  706. /*
  707. * Get DMI information
  708. */
  709. if (!dmi_check_system(i8k_dmi_table)) {
  710. if (!ignore_dmi && !force)
  711. return -ENODEV;
  712. pr_info("not running on a supported Dell system.\n");
  713. pr_info("vendor=%s, model=%s, version=%s\n",
  714. i8k_get_dmi_data(DMI_SYS_VENDOR),
  715. i8k_get_dmi_data(DMI_PRODUCT_NAME),
  716. i8k_get_dmi_data(DMI_BIOS_VERSION));
  717. }
  718. strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
  719. sizeof(bios_version));
  720. /*
  721. * Get SMM Dell signature
  722. */
  723. if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
  724. i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
  725. pr_err("unable to get SMM Dell signature\n");
  726. if (!force)
  727. return -ENODEV;
  728. }
  729. i8k_fan_mult = fan_mult;
  730. i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
  731. id = dmi_first_match(i8k_dmi_table);
  732. if (id && id->driver_data) {
  733. const struct i8k_config_data *conf = id->driver_data;
  734. if (fan_mult == I8K_FAN_MULT && conf->fan_mult)
  735. i8k_fan_mult = conf->fan_mult;
  736. if (fan_max == I8K_FAN_HIGH && conf->fan_max)
  737. i8k_fan_max = conf->fan_max;
  738. }
  739. i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
  740. return 0;
  741. }
  742. static int __init i8k_init(void)
  743. {
  744. struct proc_dir_entry *proc_i8k;
  745. int err;
  746. /* Are we running on an supported laptop? */
  747. if (i8k_probe())
  748. return -ENODEV;
  749. /* Register the proc entry */
  750. proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
  751. if (!proc_i8k)
  752. return -ENOENT;
  753. err = i8k_init_hwmon();
  754. if (err)
  755. goto exit_remove_proc;
  756. return 0;
  757. exit_remove_proc:
  758. remove_proc_entry("i8k", NULL);
  759. return err;
  760. }
  761. static void __exit i8k_exit(void)
  762. {
  763. hwmon_device_unregister(i8k_hwmon_dev);
  764. remove_proc_entry("i8k", NULL);
  765. }
  766. module_init(i8k_init);
  767. module_exit(i8k_exit);