i8k.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * i8k.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
  3. * See http://www.debian.org/~dz/i8k/ for more information
  4. * and for latest version of this driver.
  5. *
  6. * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/init.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/dmi.h>
  24. #include <linux/capability.h>
  25. #include <linux/mutex.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/io.h>
  28. #include <linux/i8k.h>
  29. #define I8K_VERSION "1.14 21/02/2005"
  30. #define I8K_SMM_FN_STATUS 0x0025
  31. #define I8K_SMM_POWER_STATUS 0x0069
  32. #define I8K_SMM_SET_FAN 0x01a3
  33. #define I8K_SMM_GET_FAN 0x00a3
  34. #define I8K_SMM_GET_SPEED 0x02a3
  35. #define I8K_SMM_GET_TEMP 0x10a3
  36. #define I8K_SMM_GET_DELL_SIG1 0xfea3
  37. #define I8K_SMM_GET_DELL_SIG2 0xffa3
  38. #define I8K_SMM_BIOS_VERSION 0x00a6
  39. #define I8K_FAN_MULT 30
  40. #define I8K_MAX_TEMP 127
  41. #define I8K_FN_NONE 0x00
  42. #define I8K_FN_UP 0x01
  43. #define I8K_FN_DOWN 0x02
  44. #define I8K_FN_MUTE 0x04
  45. #define I8K_FN_MASK 0x07
  46. #define I8K_FN_SHIFT 8
  47. #define I8K_POWER_AC 0x05
  48. #define I8K_POWER_BATTERY 0x01
  49. #define I8K_TEMPERATURE_BUG 1
  50. static DEFINE_MUTEX(i8k_mutex);
  51. static char bios_version[4];
  52. MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
  53. MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
  54. MODULE_LICENSE("GPL");
  55. static int force;
  56. module_param(force, bool, 0);
  57. MODULE_PARM_DESC(force, "Force loading without checking for supported models");
  58. static int ignore_dmi;
  59. module_param(ignore_dmi, bool, 0);
  60. MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
  61. static int restricted;
  62. module_param(restricted, bool, 0);
  63. MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
  64. static int power_status;
  65. module_param(power_status, bool, 0600);
  66. MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
  67. static int fan_mult = I8K_FAN_MULT;
  68. module_param(fan_mult, int, 0);
  69. MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with");
  70. static int i8k_open_fs(struct inode *inode, struct file *file);
  71. static long i8k_ioctl(struct file *, unsigned int, unsigned long);
  72. static const struct file_operations i8k_fops = {
  73. .owner = THIS_MODULE,
  74. .open = i8k_open_fs,
  75. .read = seq_read,
  76. .llseek = seq_lseek,
  77. .release = single_release,
  78. .unlocked_ioctl = i8k_ioctl,
  79. };
  80. struct smm_regs {
  81. unsigned int eax;
  82. unsigned int ebx __attribute__ ((packed));
  83. unsigned int ecx __attribute__ ((packed));
  84. unsigned int edx __attribute__ ((packed));
  85. unsigned int esi __attribute__ ((packed));
  86. unsigned int edi __attribute__ ((packed));
  87. };
  88. static inline const char *i8k_get_dmi_data(int field)
  89. {
  90. const char *dmi_data = dmi_get_system_info(field);
  91. return dmi_data && *dmi_data ? dmi_data : "?";
  92. }
  93. /*
  94. * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
  95. */
  96. static int i8k_smm(struct smm_regs *regs)
  97. {
  98. int rc;
  99. int eax = regs->eax;
  100. #if defined(CONFIG_X86_64)
  101. asm volatile("pushq %%rax\n\t"
  102. "movl 0(%%rax),%%edx\n\t"
  103. "pushq %%rdx\n\t"
  104. "movl 4(%%rax),%%ebx\n\t"
  105. "movl 8(%%rax),%%ecx\n\t"
  106. "movl 12(%%rax),%%edx\n\t"
  107. "movl 16(%%rax),%%esi\n\t"
  108. "movl 20(%%rax),%%edi\n\t"
  109. "popq %%rax\n\t"
  110. "out %%al,$0xb2\n\t"
  111. "out %%al,$0x84\n\t"
  112. "xchgq %%rax,(%%rsp)\n\t"
  113. "movl %%ebx,4(%%rax)\n\t"
  114. "movl %%ecx,8(%%rax)\n\t"
  115. "movl %%edx,12(%%rax)\n\t"
  116. "movl %%esi,16(%%rax)\n\t"
  117. "movl %%edi,20(%%rax)\n\t"
  118. "popq %%rdx\n\t"
  119. "movl %%edx,0(%%rax)\n\t"
  120. "lahf\n\t"
  121. "shrl $8,%%eax\n\t"
  122. "andl $1,%%eax\n"
  123. :"=a"(rc)
  124. : "a"(regs)
  125. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  126. #else
  127. asm volatile("pushl %%eax\n\t"
  128. "movl 0(%%eax),%%edx\n\t"
  129. "push %%edx\n\t"
  130. "movl 4(%%eax),%%ebx\n\t"
  131. "movl 8(%%eax),%%ecx\n\t"
  132. "movl 12(%%eax),%%edx\n\t"
  133. "movl 16(%%eax),%%esi\n\t"
  134. "movl 20(%%eax),%%edi\n\t"
  135. "popl %%eax\n\t"
  136. "out %%al,$0xb2\n\t"
  137. "out %%al,$0x84\n\t"
  138. "xchgl %%eax,(%%esp)\n\t"
  139. "movl %%ebx,4(%%eax)\n\t"
  140. "movl %%ecx,8(%%eax)\n\t"
  141. "movl %%edx,12(%%eax)\n\t"
  142. "movl %%esi,16(%%eax)\n\t"
  143. "movl %%edi,20(%%eax)\n\t"
  144. "popl %%edx\n\t"
  145. "movl %%edx,0(%%eax)\n\t"
  146. "lahf\n\t"
  147. "shrl $8,%%eax\n\t"
  148. "andl $1,%%eax\n"
  149. :"=a"(rc)
  150. : "a"(regs)
  151. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  152. #endif
  153. if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
  154. return -EINVAL;
  155. return 0;
  156. }
  157. /*
  158. * Read the bios version. Return the version as an integer corresponding
  159. * to the ascii value, for example "A17" is returned as 0x00413137.
  160. */
  161. static int i8k_get_bios_version(void)
  162. {
  163. struct smm_regs regs = { .eax = I8K_SMM_BIOS_VERSION, };
  164. return i8k_smm(&regs) ? : regs.eax;
  165. }
  166. /*
  167. * Read the Fn key status.
  168. */
  169. static int i8k_get_fn_status(void)
  170. {
  171. struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
  172. int rc;
  173. if ((rc = i8k_smm(&regs)) < 0)
  174. return rc;
  175. switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
  176. case I8K_FN_UP:
  177. return I8K_VOL_UP;
  178. case I8K_FN_DOWN:
  179. return I8K_VOL_DOWN;
  180. case I8K_FN_MUTE:
  181. return I8K_VOL_MUTE;
  182. default:
  183. return 0;
  184. }
  185. }
  186. /*
  187. * Read the power status.
  188. */
  189. static int i8k_get_power_status(void)
  190. {
  191. struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
  192. int rc;
  193. if ((rc = i8k_smm(&regs)) < 0)
  194. return rc;
  195. return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
  196. }
  197. /*
  198. * Read the fan status.
  199. */
  200. static int i8k_get_fan_status(int fan)
  201. {
  202. struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
  203. regs.ebx = fan & 0xff;
  204. return i8k_smm(&regs) ? : regs.eax & 0xff;
  205. }
  206. /*
  207. * Read the fan speed in RPM.
  208. */
  209. static int i8k_get_fan_speed(int fan)
  210. {
  211. struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
  212. regs.ebx = fan & 0xff;
  213. return i8k_smm(&regs) ? : (regs.eax & 0xffff) * fan_mult;
  214. }
  215. /*
  216. * Set the fan speed (off, low, high). Returns the new fan status.
  217. */
  218. static int i8k_set_fan(int fan, int speed)
  219. {
  220. struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
  221. speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
  222. regs.ebx = (fan & 0xff) | (speed << 8);
  223. return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
  224. }
  225. /*
  226. * Read the cpu temperature.
  227. */
  228. static int i8k_get_temp(int sensor)
  229. {
  230. struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
  231. int rc;
  232. int temp;
  233. #ifdef I8K_TEMPERATURE_BUG
  234. static int prev;
  235. #endif
  236. regs.ebx = sensor & 0xff;
  237. if ((rc = i8k_smm(&regs)) < 0)
  238. return rc;
  239. temp = regs.eax & 0xff;
  240. #ifdef I8K_TEMPERATURE_BUG
  241. /*
  242. * Sometimes the temperature sensor returns 0x99, which is out of range.
  243. * In this case we return (once) the previous cached value. For example:
  244. # 1003655137 00000058 00005a4b
  245. # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
  246. # 1003655139 00000054 00005c52
  247. */
  248. if (temp > I8K_MAX_TEMP) {
  249. temp = prev;
  250. prev = I8K_MAX_TEMP;
  251. } else {
  252. prev = temp;
  253. }
  254. #endif
  255. return temp;
  256. }
  257. static int i8k_get_dell_signature(int req_fn)
  258. {
  259. struct smm_regs regs = { .eax = req_fn, };
  260. int rc;
  261. if ((rc = i8k_smm(&regs)) < 0)
  262. return rc;
  263. return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
  264. }
  265. static int
  266. i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
  267. {
  268. int val = 0;
  269. int speed;
  270. unsigned char buff[16];
  271. int __user *argp = (int __user *)arg;
  272. if (!argp)
  273. return -EINVAL;
  274. switch (cmd) {
  275. case I8K_BIOS_VERSION:
  276. val = i8k_get_bios_version();
  277. break;
  278. case I8K_MACHINE_ID:
  279. memset(buff, 0, 16);
  280. strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL), sizeof(buff));
  281. break;
  282. case I8K_FN_STATUS:
  283. val = i8k_get_fn_status();
  284. break;
  285. case I8K_POWER_STATUS:
  286. val = i8k_get_power_status();
  287. break;
  288. case I8K_GET_TEMP:
  289. val = i8k_get_temp(0);
  290. break;
  291. case I8K_GET_SPEED:
  292. if (copy_from_user(&val, argp, sizeof(int)))
  293. return -EFAULT;
  294. val = i8k_get_fan_speed(val);
  295. break;
  296. case I8K_GET_FAN:
  297. if (copy_from_user(&val, argp, sizeof(int)))
  298. return -EFAULT;
  299. val = i8k_get_fan_status(val);
  300. break;
  301. case I8K_SET_FAN:
  302. if (restricted && !capable(CAP_SYS_ADMIN))
  303. return -EPERM;
  304. if (copy_from_user(&val, argp, sizeof(int)))
  305. return -EFAULT;
  306. if (copy_from_user(&speed, argp + 1, sizeof(int)))
  307. return -EFAULT;
  308. val = i8k_set_fan(val, speed);
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. if (val < 0)
  314. return val;
  315. switch (cmd) {
  316. case I8K_BIOS_VERSION:
  317. if (copy_to_user(argp, &val, 4))
  318. return -EFAULT;
  319. break;
  320. case I8K_MACHINE_ID:
  321. if (copy_to_user(argp, buff, 16))
  322. return -EFAULT;
  323. break;
  324. default:
  325. if (copy_to_user(argp, &val, sizeof(int)))
  326. return -EFAULT;
  327. break;
  328. }
  329. return 0;
  330. }
  331. static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  332. {
  333. long ret;
  334. mutex_lock(&i8k_mutex);
  335. ret = i8k_ioctl_unlocked(fp, cmd, arg);
  336. mutex_unlock(&i8k_mutex);
  337. return ret;
  338. }
  339. /*
  340. * Print the information for /proc/i8k.
  341. */
  342. static int i8k_proc_show(struct seq_file *seq, void *offset)
  343. {
  344. int fn_key, cpu_temp, ac_power;
  345. int left_fan, right_fan, left_speed, right_speed;
  346. cpu_temp = i8k_get_temp(0); /* 11100 µs */
  347. left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
  348. right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
  349. left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
  350. right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
  351. fn_key = i8k_get_fn_status(); /* 750 µs */
  352. if (power_status)
  353. ac_power = i8k_get_power_status(); /* 14700 µs */
  354. else
  355. ac_power = -1;
  356. /*
  357. * Info:
  358. *
  359. * 1) Format version (this will change if format changes)
  360. * 2) BIOS version
  361. * 3) BIOS machine ID
  362. * 4) Cpu temperature
  363. * 5) Left fan status
  364. * 6) Right fan status
  365. * 7) Left fan speed
  366. * 8) Right fan speed
  367. * 9) AC power
  368. * 10) Fn Key status
  369. */
  370. return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
  371. I8K_PROC_FMT,
  372. bios_version,
  373. i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  374. cpu_temp,
  375. left_fan, right_fan, left_speed, right_speed,
  376. ac_power, fn_key);
  377. }
  378. static int i8k_open_fs(struct inode *inode, struct file *file)
  379. {
  380. return single_open(file, i8k_proc_show, NULL);
  381. }
  382. static struct dmi_system_id __initdata i8k_dmi_table[] = {
  383. {
  384. .ident = "Dell Inspiron",
  385. .matches = {
  386. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  387. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  388. },
  389. },
  390. {
  391. .ident = "Dell Latitude",
  392. .matches = {
  393. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  394. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  395. },
  396. },
  397. {
  398. .ident = "Dell Inspiron 2",
  399. .matches = {
  400. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  401. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  402. },
  403. },
  404. {
  405. .ident = "Dell Latitude 2",
  406. .matches = {
  407. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  408. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  409. },
  410. },
  411. { /* UK Inspiron 6400 */
  412. .ident = "Dell Inspiron 3",
  413. .matches = {
  414. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  415. DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
  416. },
  417. },
  418. {
  419. .ident = "Dell Inspiron 3",
  420. .matches = {
  421. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  422. DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
  423. },
  424. },
  425. {
  426. .ident = "Dell Precision",
  427. .matches = {
  428. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  429. DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
  430. },
  431. },
  432. {
  433. .ident = "Dell Vostro",
  434. .matches = {
  435. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  436. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
  437. },
  438. },
  439. { }
  440. };
  441. /*
  442. * Probe for the presence of a supported laptop.
  443. */
  444. static int __init i8k_probe(void)
  445. {
  446. char buff[4];
  447. int version;
  448. /*
  449. * Get DMI information
  450. */
  451. if (!dmi_check_system(i8k_dmi_table)) {
  452. if (!ignore_dmi && !force)
  453. return -ENODEV;
  454. printk(KERN_INFO "i8k: not running on a supported Dell system.\n");
  455. printk(KERN_INFO "i8k: vendor=%s, model=%s, version=%s\n",
  456. i8k_get_dmi_data(DMI_SYS_VENDOR),
  457. i8k_get_dmi_data(DMI_PRODUCT_NAME),
  458. i8k_get_dmi_data(DMI_BIOS_VERSION));
  459. }
  460. strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION), sizeof(bios_version));
  461. /*
  462. * Get SMM Dell signature
  463. */
  464. if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
  465. i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
  466. printk(KERN_ERR "i8k: unable to get SMM Dell signature\n");
  467. if (!force)
  468. return -ENODEV;
  469. }
  470. /*
  471. * Get SMM BIOS version.
  472. */
  473. version = i8k_get_bios_version();
  474. if (version <= 0) {
  475. printk(KERN_WARNING "i8k: unable to get SMM BIOS version\n");
  476. } else {
  477. buff[0] = (version >> 16) & 0xff;
  478. buff[1] = (version >> 8) & 0xff;
  479. buff[2] = (version) & 0xff;
  480. buff[3] = '\0';
  481. /*
  482. * If DMI BIOS version is unknown use SMM BIOS version.
  483. */
  484. if (!dmi_get_system_info(DMI_BIOS_VERSION))
  485. strlcpy(bios_version, buff, sizeof(bios_version));
  486. /*
  487. * Check if the two versions match.
  488. */
  489. if (strncmp(buff, bios_version, sizeof(bios_version)) != 0)
  490. printk(KERN_WARNING "i8k: BIOS version mismatch: %s != %s\n",
  491. buff, bios_version);
  492. }
  493. return 0;
  494. }
  495. static int __init i8k_init(void)
  496. {
  497. struct proc_dir_entry *proc_i8k;
  498. /* Are we running on an supported laptop? */
  499. if (i8k_probe())
  500. return -ENODEV;
  501. /* Register the proc entry */
  502. proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
  503. if (!proc_i8k)
  504. return -ENOENT;
  505. printk(KERN_INFO
  506. "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
  507. I8K_VERSION);
  508. return 0;
  509. }
  510. static void __exit i8k_exit(void)
  511. {
  512. remove_proc_entry("i8k", NULL);
  513. }
  514. module_init(i8k_init);
  515. module_exit(i8k_exit);