toshiba.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* toshiba.c -- Linux driver for accessing the SMM on Toshiba laptops
  2. *
  3. * Copyright (c) 1996-2001 Jonathan A. Buzzard (jonathan@buzzard.org.uk)
  4. *
  5. * Valuable assistance and patches from:
  6. * Tom May <tom@you-bastards.com>
  7. * Rob Napier <rnapier@employees.org>
  8. *
  9. * Fn status port numbers for machine ID's courtesy of
  10. * 0xfc02: Scott Eisert <scott.e@sky-eye.com>
  11. * 0xfc04: Steve VanDevender <stevev@efn.org>
  12. * 0xfc08: Garth Berry <garth@itsbruce.net>
  13. * 0xfc0a: Egbert Eich <eich@xfree86.org>
  14. * 0xfc10: Andrew Lofthouse <Andrew.Lofthouse@robins.af.mil>
  15. * 0xfc11: Spencer Olson <solson@novell.com>
  16. * 0xfc13: Claudius Frankewitz <kryp@gmx.de>
  17. * 0xfc15: Tom May <tom@you-bastards.com>
  18. * 0xfc17: Dave Konrad <konrad@xenia.it>
  19. * 0xfc1a: George Betzos <betzos@engr.colostate.edu>
  20. * 0xfc1b: Munemasa Wada <munemasa@jnovel.co.jp>
  21. * 0xfc1d: Arthur Liu <armie@slap.mine.nu>
  22. * 0xfc5a: Jacques L'helgoualc'h <lhh@free.fr>
  23. * 0xfcd1: Mr. Dave Konrad <konrad@xenia.it>
  24. *
  25. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  26. *
  27. * This code is covered by the GNU GPL and you are free to make any
  28. * changes you wish to it under the terms of the license. However the
  29. * code has the potential to render your computer and/or someone else's
  30. * unusable. Please proceed with care when modifying the code.
  31. *
  32. * Note: Unfortunately the laptop hardware can close the System Configuration
  33. * Interface on it's own accord. It is therefore necessary for *all*
  34. * programs using this driver to be aware that *any* SCI call can fail at
  35. * *any* time. It is up to any program to be aware of this eventuality
  36. * and take appropriate steps.
  37. *
  38. * This program is free software; you can redistribute it and/or modify it
  39. * under the terms of the GNU General Public License as published by the
  40. * Free Software Foundation; either version 2, or (at your option) any
  41. * later version.
  42. *
  43. * This program is distributed in the hope that it will be useful, but
  44. * WITHOUT ANY WARRANTY; without even the implied warranty of
  45. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  46. * General Public License for more details.
  47. *
  48. * The information used to write this driver has been obtained by reverse
  49. * engineering the software supplied by Toshiba for their portable computers in
  50. * strict accordance with the European Council Directive 92/250/EEC on the legal
  51. * protection of computer programs, and it's implementation into English Law by
  52. * the Copyright (Computer Programs) Regulations 1992 (S.I. 1992 No.3233).
  53. *
  54. */
  55. #define TOSH_VERSION "1.11 26/9/2001"
  56. #define TOSH_DEBUG 0
  57. #include <linux/module.h>
  58. #include <linux/kernel.h>
  59. #include <linux/types.h>
  60. #include <linux/fcntl.h>
  61. #include <linux/miscdevice.h>
  62. #include <linux/ioport.h>
  63. #include <asm/io.h>
  64. #include <asm/uaccess.h>
  65. #include <linux/init.h>
  66. #include <linux/stat.h>
  67. #include <linux/proc_fs.h>
  68. #include <linux/seq_file.h>
  69. #include <linux/mutex.h>
  70. #include <linux/toshiba.h>
  71. #define TOSH_MINOR_DEV 181
  72. MODULE_LICENSE("GPL");
  73. MODULE_AUTHOR("Jonathan Buzzard <jonathan@buzzard.org.uk>");
  74. MODULE_DESCRIPTION("Toshiba laptop SMM driver");
  75. MODULE_SUPPORTED_DEVICE("toshiba");
  76. static DEFINE_MUTEX(tosh_mutex);
  77. static int tosh_fn;
  78. module_param_named(fn, tosh_fn, int, 0);
  79. MODULE_PARM_DESC(fn, "User specified Fn key detection port");
  80. static int tosh_id;
  81. static int tosh_bios;
  82. static int tosh_date;
  83. static int tosh_sci;
  84. static int tosh_fan;
  85. static long tosh_ioctl(struct file *, unsigned int,
  86. unsigned long);
  87. static const struct file_operations tosh_fops = {
  88. .owner = THIS_MODULE,
  89. .unlocked_ioctl = tosh_ioctl,
  90. .llseek = noop_llseek,
  91. };
  92. static struct miscdevice tosh_device = {
  93. TOSH_MINOR_DEV,
  94. "toshiba",
  95. &tosh_fops
  96. };
  97. /*
  98. * Read the Fn key status
  99. */
  100. #ifdef CONFIG_PROC_FS
  101. static int tosh_fn_status(void)
  102. {
  103. unsigned char scan;
  104. unsigned long flags;
  105. if (tosh_fn!=0) {
  106. scan = inb(tosh_fn);
  107. } else {
  108. local_irq_save(flags);
  109. outb(0x8e, 0xe4);
  110. scan = inb(0xe5);
  111. local_irq_restore(flags);
  112. }
  113. return (int) scan;
  114. }
  115. #endif
  116. /*
  117. * For the Portage 610CT and the Tecra 700CS/700CDT emulate the HCI fan function
  118. */
  119. static int tosh_emulate_fan(SMMRegisters *regs)
  120. {
  121. unsigned long eax,ecx,flags;
  122. unsigned char al;
  123. eax = regs->eax & 0xff00;
  124. ecx = regs->ecx & 0xffff;
  125. /* Portage 610CT */
  126. if (tosh_id==0xfccb) {
  127. if (eax==0xfe00) {
  128. /* fan status */
  129. local_irq_save(flags);
  130. outb(0xbe, 0xe4);
  131. al = inb(0xe5);
  132. local_irq_restore(flags);
  133. regs->eax = 0x00;
  134. regs->ecx = (unsigned int) (al & 0x01);
  135. }
  136. if ((eax==0xff00) && (ecx==0x0000)) {
  137. /* fan off */
  138. local_irq_save(flags);
  139. outb(0xbe, 0xe4);
  140. al = inb(0xe5);
  141. outb(0xbe, 0xe4);
  142. outb (al | 0x01, 0xe5);
  143. local_irq_restore(flags);
  144. regs->eax = 0x00;
  145. regs->ecx = 0x00;
  146. }
  147. if ((eax==0xff00) && (ecx==0x0001)) {
  148. /* fan on */
  149. local_irq_save(flags);
  150. outb(0xbe, 0xe4);
  151. al = inb(0xe5);
  152. outb(0xbe, 0xe4);
  153. outb(al & 0xfe, 0xe5);
  154. local_irq_restore(flags);
  155. regs->eax = 0x00;
  156. regs->ecx = 0x01;
  157. }
  158. }
  159. /* Tecra 700CS/CDT */
  160. if (tosh_id==0xfccc) {
  161. if (eax==0xfe00) {
  162. /* fan status */
  163. local_irq_save(flags);
  164. outb(0xe0, 0xe4);
  165. al = inb(0xe5);
  166. local_irq_restore(flags);
  167. regs->eax = 0x00;
  168. regs->ecx = al & 0x01;
  169. }
  170. if ((eax==0xff00) && (ecx==0x0000)) {
  171. /* fan off */
  172. local_irq_save(flags);
  173. outb(0xe0, 0xe4);
  174. al = inb(0xe5);
  175. outw(0xe0 | ((al & 0xfe) << 8), 0xe4);
  176. local_irq_restore(flags);
  177. regs->eax = 0x00;
  178. regs->ecx = 0x00;
  179. }
  180. if ((eax==0xff00) && (ecx==0x0001)) {
  181. /* fan on */
  182. local_irq_save(flags);
  183. outb(0xe0, 0xe4);
  184. al = inb(0xe5);
  185. outw(0xe0 | ((al | 0x01) << 8), 0xe4);
  186. local_irq_restore(flags);
  187. regs->eax = 0x00;
  188. regs->ecx = 0x01;
  189. }
  190. }
  191. return 0;
  192. }
  193. /*
  194. * Put the laptop into System Management Mode
  195. */
  196. int tosh_smm(SMMRegisters *regs)
  197. {
  198. int eax;
  199. asm ("# load the values into the registers\n\t" \
  200. "pushl %%eax\n\t" \
  201. "movl 0(%%eax),%%edx\n\t" \
  202. "push %%edx\n\t" \
  203. "movl 4(%%eax),%%ebx\n\t" \
  204. "movl 8(%%eax),%%ecx\n\t" \
  205. "movl 12(%%eax),%%edx\n\t" \
  206. "movl 16(%%eax),%%esi\n\t" \
  207. "movl 20(%%eax),%%edi\n\t" \
  208. "popl %%eax\n\t" \
  209. "# call the System Management mode\n\t" \
  210. "inb $0xb2,%%al\n\t"
  211. "# fill out the memory with the values in the registers\n\t" \
  212. "xchgl %%eax,(%%esp)\n\t"
  213. "movl %%ebx,4(%%eax)\n\t" \
  214. "movl %%ecx,8(%%eax)\n\t" \
  215. "movl %%edx,12(%%eax)\n\t" \
  216. "movl %%esi,16(%%eax)\n\t" \
  217. "movl %%edi,20(%%eax)\n\t" \
  218. "popl %%edx\n\t" \
  219. "movl %%edx,0(%%eax)\n\t" \
  220. "# setup the return value to the carry flag\n\t" \
  221. "lahf\n\t" \
  222. "shrl $8,%%eax\n\t" \
  223. "andl $1,%%eax\n" \
  224. : "=a" (eax)
  225. : "a" (regs)
  226. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  227. return eax;
  228. }
  229. EXPORT_SYMBOL(tosh_smm);
  230. static long tosh_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  231. {
  232. SMMRegisters regs;
  233. SMMRegisters __user *argp = (SMMRegisters __user *)arg;
  234. unsigned short ax,bx;
  235. int err;
  236. if (!argp)
  237. return -EINVAL;
  238. if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
  239. return -EFAULT;
  240. switch (cmd) {
  241. case TOSH_SMM:
  242. ax = regs.eax & 0xff00;
  243. bx = regs.ebx & 0xffff;
  244. /* block HCI calls to read/write memory & PCI devices */
  245. if (((ax==0xff00) || (ax==0xfe00)) && (bx>0x0069))
  246. return -EINVAL;
  247. /* do we need to emulate the fan ? */
  248. mutex_lock(&tosh_mutex);
  249. if (tosh_fan==1) {
  250. if (((ax==0xf300) || (ax==0xf400)) && (bx==0x0004)) {
  251. err = tosh_emulate_fan(&regs);
  252. mutex_unlock(&tosh_mutex);
  253. break;
  254. }
  255. }
  256. err = tosh_smm(&regs);
  257. mutex_unlock(&tosh_mutex);
  258. break;
  259. default:
  260. return -EINVAL;
  261. }
  262. if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
  263. return -EFAULT;
  264. return (err==0) ? 0:-EINVAL;
  265. }
  266. /*
  267. * Print the information for /proc/toshiba
  268. */
  269. #ifdef CONFIG_PROC_FS
  270. static int proc_toshiba_show(struct seq_file *m, void *v)
  271. {
  272. int key;
  273. key = tosh_fn_status();
  274. /* Arguments
  275. 0) Linux driver version (this will change if format changes)
  276. 1) Machine ID
  277. 2) SCI version
  278. 3) BIOS version (major, minor)
  279. 4) BIOS date (in SCI date format)
  280. 5) Fn Key status
  281. */
  282. seq_printf(m, "1.1 0x%04x %d.%d %d.%d 0x%04x 0x%02x\n",
  283. tosh_id,
  284. (tosh_sci & 0xff00)>>8,
  285. tosh_sci & 0xff,
  286. (tosh_bios & 0xff00)>>8,
  287. tosh_bios & 0xff,
  288. tosh_date,
  289. key);
  290. return 0;
  291. }
  292. static int proc_toshiba_open(struct inode *inode, struct file *file)
  293. {
  294. return single_open(file, proc_toshiba_show, NULL);
  295. }
  296. static const struct file_operations proc_toshiba_fops = {
  297. .owner = THIS_MODULE,
  298. .open = proc_toshiba_open,
  299. .read = seq_read,
  300. .llseek = seq_lseek,
  301. .release = single_release,
  302. };
  303. #endif
  304. /*
  305. * Determine which port to use for the Fn key status
  306. */
  307. static void tosh_set_fn_port(void)
  308. {
  309. switch (tosh_id) {
  310. case 0xfc02: case 0xfc04: case 0xfc09: case 0xfc0a: case 0xfc10:
  311. case 0xfc11: case 0xfc13: case 0xfc15: case 0xfc1a: case 0xfc1b:
  312. case 0xfc5a:
  313. tosh_fn = 0x62;
  314. break;
  315. case 0xfc08: case 0xfc17: case 0xfc1d: case 0xfcd1: case 0xfce0:
  316. case 0xfce2:
  317. tosh_fn = 0x68;
  318. break;
  319. default:
  320. tosh_fn = 0x00;
  321. break;
  322. }
  323. return;
  324. }
  325. /*
  326. * Get the machine identification number of the current model
  327. */
  328. static int tosh_get_machine_id(void __iomem *bios)
  329. {
  330. int id;
  331. SMMRegisters regs;
  332. unsigned short bx,cx;
  333. unsigned long address;
  334. id = (0x100*(int) readb(bios+0xfffe))+((int) readb(bios+0xfffa));
  335. /* do we have a SCTTable machine identication number on our hands */
  336. if (id==0xfc2f) {
  337. /* start by getting a pointer into the BIOS */
  338. regs.eax = 0xc000;
  339. regs.ebx = 0x0000;
  340. regs.ecx = 0x0000;
  341. tosh_smm(&regs);
  342. bx = (unsigned short) (regs.ebx & 0xffff);
  343. /* At this point in the Toshiba routines under MS Windows
  344. the bx register holds 0xe6f5. However my code is producing
  345. a different value! For the time being I will just fudge the
  346. value. This has been verified on a Satellite Pro 430CDT,
  347. Tecra 750CDT, Tecra 780DVD and Satellite 310CDT. */
  348. #if TOSH_DEBUG
  349. printk("toshiba: debugging ID ebx=0x%04x\n", regs.ebx);
  350. #endif
  351. bx = 0xe6f5;
  352. /* now twiddle with our pointer a bit */
  353. address = bx;
  354. cx = readw(bios + address);
  355. address = 9+bx+cx;
  356. cx = readw(bios + address);
  357. address = 0xa+cx;
  358. cx = readw(bios + address);
  359. /* now construct our machine identification number */
  360. id = ((cx & 0xff)<<8)+((cx & 0xff00)>>8);
  361. }
  362. return id;
  363. }
  364. /*
  365. * Probe for the presence of a Toshiba laptop
  366. *
  367. * returns and non-zero if unable to detect the presence of a Toshiba
  368. * laptop, otherwise zero and determines the Machine ID, BIOS version and
  369. * date, and SCI version.
  370. */
  371. static int tosh_probe(void)
  372. {
  373. int i,major,minor,day,year,month,flag;
  374. unsigned char signature[7] = { 0x54,0x4f,0x53,0x48,0x49,0x42,0x41 };
  375. SMMRegisters regs;
  376. void __iomem *bios = ioremap_cache(0xf0000, 0x10000);
  377. if (!bios)
  378. return -ENOMEM;
  379. /* extra sanity check for the string "TOSHIBA" in the BIOS because
  380. some machines that are not Toshiba's pass the next test */
  381. for (i=0;i<7;i++) {
  382. if (readb(bios+0xe010+i)!=signature[i]) {
  383. printk("toshiba: not a supported Toshiba laptop\n");
  384. iounmap(bios);
  385. return -ENODEV;
  386. }
  387. }
  388. /* call the Toshiba SCI support check routine */
  389. regs.eax = 0xf0f0;
  390. regs.ebx = 0x0000;
  391. regs.ecx = 0x0000;
  392. flag = tosh_smm(&regs);
  393. /* if this is not a Toshiba laptop carry flag is set and ah=0x86 */
  394. if ((flag==1) || ((regs.eax & 0xff00)==0x8600)) {
  395. printk("toshiba: not a supported Toshiba laptop\n");
  396. iounmap(bios);
  397. return -ENODEV;
  398. }
  399. /* if we get this far then we are running on a Toshiba (probably)! */
  400. tosh_sci = regs.edx & 0xffff;
  401. /* next get the machine ID of the current laptop */
  402. tosh_id = tosh_get_machine_id(bios);
  403. /* get the BIOS version */
  404. major = readb(bios+0xe009)-'0';
  405. minor = ((readb(bios+0xe00b)-'0')*10)+(readb(bios+0xe00c)-'0');
  406. tosh_bios = (major*0x100)+minor;
  407. /* get the BIOS date */
  408. day = ((readb(bios+0xfff5)-'0')*10)+(readb(bios+0xfff6)-'0');
  409. month = ((readb(bios+0xfff8)-'0')*10)+(readb(bios+0xfff9)-'0');
  410. year = ((readb(bios+0xfffb)-'0')*10)+(readb(bios+0xfffc)-'0');
  411. tosh_date = (((year-90) & 0x1f)<<10) | ((month & 0xf)<<6)
  412. | ((day & 0x1f)<<1);
  413. /* in theory we should check the ports we are going to use for the
  414. fn key detection (and the fan on the Portage 610/Tecra700), and
  415. then request them to stop other drivers using them. However as
  416. the keyboard driver grabs 0x60-0x6f and the pic driver grabs
  417. 0xa0-0xbf we can't. We just have to live dangerously and use the
  418. ports anyway, oh boy! */
  419. /* do we need to emulate the fan? */
  420. if ((tosh_id==0xfccb) || (tosh_id==0xfccc))
  421. tosh_fan = 1;
  422. iounmap(bios);
  423. return 0;
  424. }
  425. static int __init toshiba_init(void)
  426. {
  427. int retval;
  428. /* are we running on a Toshiba laptop */
  429. if (tosh_probe())
  430. return -ENODEV;
  431. printk(KERN_INFO "Toshiba System Management Mode driver v" TOSH_VERSION "\n");
  432. /* set the port to use for Fn status if not specified as a parameter */
  433. if (tosh_fn==0x00)
  434. tosh_set_fn_port();
  435. /* register the device file */
  436. retval = misc_register(&tosh_device);
  437. if (retval < 0)
  438. return retval;
  439. #ifdef CONFIG_PROC_FS
  440. {
  441. struct proc_dir_entry *pde;
  442. pde = proc_create("toshiba", 0, NULL, &proc_toshiba_fops);
  443. if (!pde) {
  444. misc_deregister(&tosh_device);
  445. return -ENOMEM;
  446. }
  447. }
  448. #endif
  449. return 0;
  450. }
  451. static void __exit toshiba_exit(void)
  452. {
  453. remove_proc_entry("toshiba", NULL);
  454. misc_deregister(&tosh_device);
  455. }
  456. module_init(toshiba_init);
  457. module_exit(toshiba_exit);