i8k.c 19 KB

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