i8k.c 12 KB

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