i8k.c 12 KB

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