i8k.c 12 KB

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