i8k.c 19 KB

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