i8k.c 12 KB

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