wistron_btns.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Wistron laptop button driver
  3. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  4. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  5. *
  6. * You can redistribute and/or modify this program under the terms of the
  7. * GNU General Public License version 2 as published by the Free Software
  8. * Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. * Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <asm/io.h>
  20. #include <linux/dmi.h>
  21. #include <linux/init.h>
  22. #include <linux/input.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mc146818rtc.h>
  26. #include <linux/module.h>
  27. #include <linux/preempt.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/types.h>
  31. /*
  32. * Number of attempts to read data from queue per poll;
  33. * the queue can hold up to 31 entries
  34. */
  35. #define MAX_POLL_ITERATIONS 64
  36. #define POLL_FREQUENCY 10 /* Number of polls per second */
  37. #if POLL_FREQUENCY > HZ
  38. #error "POLL_FREQUENCY too high"
  39. #endif
  40. /* BIOS subsystem IDs */
  41. #define WIFI 0x35
  42. #define BLUETOOTH 0x34
  43. MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
  44. MODULE_DESCRIPTION("Wistron laptop button driver");
  45. MODULE_LICENSE("GPL v2");
  46. MODULE_VERSION("0.1");
  47. static int force; /* = 0; */
  48. module_param(force, bool, 0);
  49. MODULE_PARM_DESC(force, "Load even if computer is not in database");
  50. static char *keymap_name; /* = NULL; */
  51. module_param_named(keymap, keymap_name, charp, 0);
  52. MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
  53. /* BIOS interface implementation */
  54. static void __iomem *bios_entry_point; /* BIOS routine entry point */
  55. static void __iomem *bios_code_map_base;
  56. static void __iomem *bios_data_map_base;
  57. static u8 cmos_address;
  58. struct regs {
  59. u32 eax, ebx, ecx;
  60. };
  61. static void call_bios(struct regs *regs)
  62. {
  63. unsigned long flags;
  64. preempt_disable();
  65. local_irq_save(flags);
  66. asm volatile ("pushl %%ebp;"
  67. "movl %7, %%ebp;"
  68. "call *%6;"
  69. "popl %%ebp"
  70. : "=a" (regs->eax), "=b" (regs->ebx), "=c" (regs->ecx)
  71. : "0" (regs->eax), "1" (regs->ebx), "2" (regs->ecx),
  72. "m" (bios_entry_point), "m" (bios_data_map_base)
  73. : "edx", "edi", "esi", "memory");
  74. local_irq_restore(flags);
  75. preempt_enable();
  76. }
  77. static size_t __init locate_wistron_bios(void __iomem *base)
  78. {
  79. static const unsigned char __initdata signature[] =
  80. { 0x42, 0x21, 0x55, 0x30 };
  81. size_t offset;
  82. for (offset = 0; offset < 0x10000; offset += 0x10) {
  83. if (check_signature(base + offset, signature,
  84. sizeof(signature)) != 0)
  85. return offset;
  86. }
  87. return -1;
  88. }
  89. static int __init map_bios(void)
  90. {
  91. void __iomem *base;
  92. size_t offset;
  93. u32 entry_point;
  94. base = ioremap(0xF0000, 0x10000); /* Can't fail */
  95. offset = locate_wistron_bios(base);
  96. if (offset < 0) {
  97. printk(KERN_ERR "wistron_btns: BIOS entry point not found\n");
  98. iounmap(base);
  99. return -ENODEV;
  100. }
  101. entry_point = readl(base + offset + 5);
  102. printk(KERN_DEBUG
  103. "wistron_btns: BIOS signature found at %p, entry point %08X\n",
  104. base + offset, entry_point);
  105. if (entry_point >= 0xF0000) {
  106. bios_code_map_base = base;
  107. bios_entry_point = bios_code_map_base + (entry_point & 0xFFFF);
  108. } else {
  109. iounmap(base);
  110. bios_code_map_base = ioremap(entry_point & ~0x3FFF, 0x4000);
  111. if (bios_code_map_base == NULL) {
  112. printk(KERN_ERR
  113. "wistron_btns: Can't map BIOS code at %08X\n",
  114. entry_point & ~0x3FFF);
  115. goto err;
  116. }
  117. bios_entry_point = bios_code_map_base + (entry_point & 0x3FFF);
  118. }
  119. /* The Windows driver maps 0x10000 bytes, we keep only one page... */
  120. bios_data_map_base = ioremap(0x400, 0xc00);
  121. if (bios_data_map_base == NULL) {
  122. printk(KERN_ERR "wistron_btns: Can't map BIOS data\n");
  123. goto err_code;
  124. }
  125. return 0;
  126. err_code:
  127. iounmap(bios_code_map_base);
  128. err:
  129. return -ENOMEM;
  130. }
  131. static void __exit unmap_bios(void)
  132. {
  133. iounmap(bios_code_map_base);
  134. iounmap(bios_data_map_base);
  135. }
  136. /* BIOS calls */
  137. static u16 bios_pop_queue(void)
  138. {
  139. struct regs regs;
  140. memset(&regs, 0, sizeof (regs));
  141. regs.eax = 0x9610;
  142. regs.ebx = 0x061C;
  143. regs.ecx = 0x0000;
  144. call_bios(&regs);
  145. return regs.eax;
  146. }
  147. static void __init bios_attach(void)
  148. {
  149. struct regs regs;
  150. memset(&regs, 0, sizeof (regs));
  151. regs.eax = 0x9610;
  152. regs.ebx = 0x012E;
  153. call_bios(&regs);
  154. }
  155. static void __exit bios_detach(void)
  156. {
  157. struct regs regs;
  158. memset(&regs, 0, sizeof (regs));
  159. regs.eax = 0x9610;
  160. regs.ebx = 0x002E;
  161. call_bios(&regs);
  162. }
  163. static u8 __init bios_get_cmos_address(void)
  164. {
  165. struct regs regs;
  166. memset(&regs, 0, sizeof (regs));
  167. regs.eax = 0x9610;
  168. regs.ebx = 0x051C;
  169. call_bios(&regs);
  170. return regs.ecx;
  171. }
  172. static u16 __init bios_get_default_setting(u8 subsys)
  173. {
  174. struct regs regs;
  175. memset(&regs, 0, sizeof (regs));
  176. regs.eax = 0x9610;
  177. regs.ebx = 0x0200 | subsys;
  178. call_bios(&regs);
  179. return regs.eax;
  180. }
  181. static void bios_set_state(u8 subsys, int enable)
  182. {
  183. struct regs regs;
  184. memset(&regs, 0, sizeof (regs));
  185. regs.eax = 0x9610;
  186. regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
  187. call_bios(&regs);
  188. }
  189. /* Hardware database */
  190. struct key_entry {
  191. char type; /* See KE_* below */
  192. u8 code;
  193. unsigned keycode; /* For KE_KEY */
  194. };
  195. enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
  196. static const struct key_entry *keymap; /* = NULL; Current key map */
  197. static int have_wifi;
  198. static int have_bluetooth;
  199. static int __init dmi_matched(struct dmi_system_id *dmi)
  200. {
  201. const struct key_entry *key;
  202. keymap = dmi->driver_data;
  203. for (key = keymap; key->type != KE_END; key++) {
  204. if (key->type == KE_WIFI) {
  205. have_wifi = 1;
  206. break;
  207. } else if (key->type == KE_BLUETOOTH) {
  208. have_bluetooth = 1;
  209. break;
  210. }
  211. }
  212. return 1;
  213. }
  214. static struct key_entry keymap_empty[] = {
  215. { KE_END, 0 }
  216. };
  217. static struct key_entry keymap_fs_amilo_pro_v2000[] = {
  218. { KE_KEY, 0x01, KEY_HELP },
  219. { KE_KEY, 0x11, KEY_PROG1 },
  220. { KE_KEY, 0x12, KEY_PROG2 },
  221. { KE_WIFI, 0x30, 0 },
  222. { KE_KEY, 0x31, KEY_MAIL },
  223. { KE_KEY, 0x36, KEY_WWW },
  224. { KE_END, 0 }
  225. };
  226. static struct key_entry keymap_wistron_ms2141[] = {
  227. { KE_KEY, 0x11, KEY_PROG1 },
  228. { KE_KEY, 0x12, KEY_PROG2 },
  229. { KE_WIFI, 0x30, 0 },
  230. { KE_KEY, 0x22, KEY_REWIND },
  231. { KE_KEY, 0x23, KEY_FORWARD },
  232. { KE_KEY, 0x24, KEY_PLAYPAUSE },
  233. { KE_KEY, 0x25, KEY_STOPCD },
  234. { KE_KEY, 0x31, KEY_MAIL },
  235. { KE_KEY, 0x36, KEY_WWW },
  236. { KE_END, 0 }
  237. };
  238. static struct key_entry keymap_acer_aspire_1500[] = {
  239. { KE_KEY, 0x11, KEY_PROG1 },
  240. { KE_KEY, 0x12, KEY_PROG2 },
  241. { KE_WIFI, 0x30, 0 },
  242. { KE_KEY, 0x31, KEY_MAIL },
  243. { KE_KEY, 0x36, KEY_WWW },
  244. { KE_BLUETOOTH, 0x44, 0 },
  245. { KE_END, 0 }
  246. };
  247. /*
  248. * If your machine is not here (which is currently rather likely), please send
  249. * a list of buttons and their key codes (reported when loading this module
  250. * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
  251. */
  252. static struct dmi_system_id dmi_ids[] = {
  253. {
  254. .callback = dmi_matched,
  255. .ident = "Fujitsu-Siemens Amilo Pro V2000",
  256. .matches = {
  257. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  258. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
  259. },
  260. .driver_data = keymap_fs_amilo_pro_v2000
  261. },
  262. {
  263. .callback = dmi_matched,
  264. .ident = "Acer Aspire 1500",
  265. .matches = {
  266. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  267. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
  268. },
  269. .driver_data = keymap_acer_aspire_1500
  270. },
  271. { 0, }
  272. };
  273. static int __init select_keymap(void)
  274. {
  275. if (keymap_name != NULL) {
  276. if (strcmp (keymap_name, "1557/MS2141") == 0)
  277. keymap = keymap_wistron_ms2141;
  278. else {
  279. printk(KERN_ERR "wistron_btns: Keymap unknown\n");
  280. return -EINVAL;
  281. }
  282. }
  283. dmi_check_system(dmi_ids);
  284. if (keymap == NULL) {
  285. if (!force) {
  286. printk(KERN_ERR "wistron_btns: System unknown\n");
  287. return -ENODEV;
  288. }
  289. keymap = keymap_empty;
  290. }
  291. return 0;
  292. }
  293. /* Input layer interface */
  294. static struct input_dev input_dev = {
  295. .name = "Wistron laptop buttons",
  296. };
  297. static void __init setup_input_dev(void)
  298. {
  299. const struct key_entry *key;
  300. for (key = keymap; key->type != KE_END; key++) {
  301. if (key->type == KE_KEY) {
  302. input_dev.evbit[LONG(EV_KEY)] = BIT(EV_KEY);
  303. input_dev.keybit[LONG(key->keycode)]
  304. |= BIT(key->keycode);
  305. }
  306. }
  307. input_register_device(&input_dev);
  308. }
  309. static void report_key(unsigned keycode)
  310. {
  311. input_report_key(&input_dev, keycode, 1);
  312. input_sync(&input_dev);
  313. input_report_key(&input_dev, keycode, 0);
  314. input_sync(&input_dev);
  315. }
  316. /* Driver core */
  317. static int wifi_enabled;
  318. static int bluetooth_enabled;
  319. static void poll_bios(unsigned long);
  320. static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
  321. static void handle_key(u8 code)
  322. {
  323. const struct key_entry *key;
  324. for (key = keymap; key->type != KE_END; key++) {
  325. if (code == key->code) {
  326. switch (key->type) {
  327. case KE_KEY:
  328. report_key(key->keycode);
  329. break;
  330. case KE_WIFI:
  331. if (have_wifi) {
  332. wifi_enabled = !wifi_enabled;
  333. bios_set_state(WIFI, wifi_enabled);
  334. }
  335. break;
  336. case KE_BLUETOOTH:
  337. if (have_bluetooth) {
  338. bluetooth_enabled = !bluetooth_enabled;
  339. bios_set_state(BLUETOOTH, bluetooth_enabled);
  340. }
  341. break;
  342. case KE_END:
  343. default:
  344. BUG();
  345. }
  346. return;
  347. }
  348. }
  349. printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
  350. }
  351. static void poll_bios(unsigned long discard)
  352. {
  353. u8 qlen;
  354. u16 val;
  355. for (;;) {
  356. qlen = CMOS_READ(cmos_address);
  357. if (qlen == 0)
  358. break;
  359. val = bios_pop_queue();
  360. if (val != 0 && !discard)
  361. handle_key((u8)val);
  362. }
  363. mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
  364. }
  365. static int __init wb_module_init(void)
  366. {
  367. int err;
  368. err = select_keymap();
  369. if (err)
  370. return err;
  371. err = map_bios();
  372. if (err)
  373. return err;
  374. bios_attach();
  375. cmos_address = bios_get_cmos_address();
  376. if (have_wifi) {
  377. u16 wifi = bios_get_default_setting(WIFI);
  378. if (wifi & 1)
  379. wifi_enabled = (wifi & 2) ? 1 : 0;
  380. else
  381. have_wifi = 0;
  382. if (have_wifi)
  383. bios_set_state(WIFI, wifi_enabled);
  384. }
  385. if (have_bluetooth) {
  386. u16 bt = bios_get_default_setting(BLUETOOTH);
  387. if (bt & 1)
  388. bluetooth_enabled = (bt & 2) ? 1 : 0;
  389. else
  390. have_bluetooth = 0;
  391. if (have_bluetooth)
  392. bios_set_state(BLUETOOTH, bluetooth_enabled);
  393. }
  394. setup_input_dev();
  395. poll_bios(1); /* Flush stale event queue and arm timer */
  396. return 0;
  397. }
  398. static void __exit wb_module_exit(void)
  399. {
  400. del_timer_sync(&poll_timer);
  401. input_unregister_device(&input_dev);
  402. bios_detach();
  403. unmap_bios();
  404. }
  405. module_init(wb_module_init);
  406. module_exit(wb_module_exit);