lkkbd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
  3. */
  4. /*
  5. * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik
  6. */
  7. /*
  8. * DEC LK201 and LK401 keyboard driver for Linux (primary for DECstations
  9. * and VAXstations, but can also be used on any standard RS232 with an
  10. * adaptor).
  11. *
  12. * DISCLAIMER: This works for _me_. If you break anything by using the
  13. * information given below, I will _not_ be liable!
  14. *
  15. * RJ10 pinout: To DE9: Or DB25:
  16. * 1 - RxD <----> Pin 3 (TxD) <-> Pin 2 (TxD)
  17. * 2 - GND <----> Pin 5 (GND) <-> Pin 7 (GND)
  18. * 4 - TxD <----> Pin 2 (RxD) <-> Pin 3 (RxD)
  19. * 3 - +12V (from HDD drive connector), DON'T connect to DE9 or DB25!!!
  20. *
  21. * Pin numbers for DE9 and DB25 are noted on the plug (quite small:). For
  22. * RJ10, it's like this:
  23. *
  24. * __=__ Hold the plug in front of you, cable downwards,
  25. * /___/| nose is hidden behind the plug. Now, pin 1 is at
  26. * |1234|| the left side, pin 4 at the right and 2 and 3 are
  27. * |IIII|| in between, of course:)
  28. * | ||
  29. * |____|/
  30. * || So the adaptor consists of three connected cables
  31. * || for data transmission (RxD and TxD) and signal ground.
  32. * Additionally, you have to get +12V from somewhere.
  33. * Most easily, you'll get that from a floppy or HDD power connector.
  34. * It's the yellow cable there (black is ground and red is +5V).
  35. *
  36. * The keyboard and all the commands it understands are documented in
  37. * "VCB02 Video Subsystem - Technical Manual", EK-104AA-TM-001. This
  38. * document is LK201 specific, but LK401 is mostly compatible. It comes
  39. * up in LK201 mode and doesn't report any of the additional keys it
  40. * has. These need to be switched on with the LK_CMD_ENABLE_LK401
  41. * command. You'll find this document (scanned .pdf file) on MANX,
  42. * a search engine specific to DEC documentation. Try
  43. * http://www.vt100.net/manx/details?pn=EK-104AA-TM-001;id=21;cp=1
  44. */
  45. /*
  46. * This program is free software; you can redistribute it and/or modify
  47. * it under the terms of the GNU General Public License as published by
  48. * the Free Software Foundation; either version 2 of the License, or
  49. * (at your option) any later version.
  50. *
  51. * This program is distributed in the hope that it will be useful,
  52. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  54. * GNU General Public License for more details.
  55. *
  56. * You should have received a copy of the GNU General Public License
  57. * along with this program; if not, write to the Free Software
  58. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  59. *
  60. * Should you need to contact me, the author, you can do so either by
  61. * email or by paper mail:
  62. * Jan-Benedict Glaw, Lilienstraße 16, 33790 Hörste (near Halle/Westf.),
  63. * Germany.
  64. */
  65. #include <linux/delay.h>
  66. #include <linux/slab.h>
  67. #include <linux/module.h>
  68. #include <linux/moduleparam.h>
  69. #include <linux/interrupt.h>
  70. #include <linux/init.h>
  71. #include <linux/input.h>
  72. #include <linux/serio.h>
  73. #include <linux/workqueue.h>
  74. #define DRIVER_DESC "LK keyboard driver"
  75. MODULE_AUTHOR ("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  76. MODULE_DESCRIPTION (DRIVER_DESC);
  77. MODULE_LICENSE ("GPL");
  78. /*
  79. * Known parameters:
  80. * bell_volume
  81. * keyclick_volume
  82. * ctrlclick_volume
  83. *
  84. * Please notice that there's not yet an API to set these at runtime.
  85. */
  86. static int bell_volume = 100; /* % */
  87. module_param (bell_volume, int, 0);
  88. MODULE_PARM_DESC (bell_volume, "Bell volume (in %). default is 100%");
  89. static int keyclick_volume = 100; /* % */
  90. module_param (keyclick_volume, int, 0);
  91. MODULE_PARM_DESC (keyclick_volume, "Keyclick volume (in %), default is 100%");
  92. static int ctrlclick_volume = 100; /* % */
  93. module_param (ctrlclick_volume, int, 0);
  94. MODULE_PARM_DESC (ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
  95. static int lk201_compose_is_alt = 0;
  96. module_param (lk201_compose_is_alt, int, 0);
  97. MODULE_PARM_DESC (lk201_compose_is_alt, "If set non-zero, LK201' Compose key "
  98. "will act as an Alt key");
  99. #undef LKKBD_DEBUG
  100. #ifdef LKKBD_DEBUG
  101. #define DBG(x...) printk (x)
  102. #else
  103. #define DBG(x...) do {} while (0)
  104. #endif
  105. /* LED control */
  106. #define LK_LED_WAIT 0x81
  107. #define LK_LED_COMPOSE 0x82
  108. #define LK_LED_SHIFTLOCK 0x84
  109. #define LK_LED_SCROLLLOCK 0x88
  110. #define LK_CMD_LED_ON 0x13
  111. #define LK_CMD_LED_OFF 0x11
  112. /* Mode control */
  113. #define LK_MODE_DOWN 0x80
  114. #define LK_MODE_AUTODOWN 0x82
  115. #define LK_MODE_UPDOWN 0x86
  116. #define LK_CMD_SET_MODE(mode,div) ((mode) | ((div) << 3))
  117. /* Misc commands */
  118. #define LK_CMD_ENABLE_KEYCLICK 0x1b
  119. #define LK_CMD_DISABLE_KEYCLICK 0x99
  120. #define LK_CMD_DISABLE_BELL 0xa1
  121. #define LK_CMD_SOUND_BELL 0xa7
  122. #define LK_CMD_ENABLE_BELL 0x23
  123. #define LK_CMD_DISABLE_CTRCLICK 0xb9
  124. #define LK_CMD_ENABLE_CTRCLICK 0xbb
  125. #define LK_CMD_SET_DEFAULTS 0xd3
  126. #define LK_CMD_POWERCYCLE_RESET 0xfd
  127. #define LK_CMD_ENABLE_LK401 0xe9
  128. #define LK_CMD_REQUEST_ID 0xab
  129. /* Misc responses from keyboard */
  130. #define LK_STUCK_KEY 0x3d
  131. #define LK_SELFTEST_FAILED 0x3e
  132. #define LK_ALL_KEYS_UP 0xb3
  133. #define LK_METRONOME 0xb4
  134. #define LK_OUTPUT_ERROR 0xb5
  135. #define LK_INPUT_ERROR 0xb6
  136. #define LK_KBD_LOCKED 0xb7
  137. #define LK_KBD_TEST_MODE_ACK 0xb8
  138. #define LK_PREFIX_KEY_DOWN 0xb9
  139. #define LK_MODE_CHANGE_ACK 0xba
  140. #define LK_RESPONSE_RESERVED 0xbb
  141. #define LK_NUM_KEYCODES 256
  142. #define LK_NUM_IGNORE_BYTES 6
  143. typedef u_int16_t lk_keycode_t;
  144. static lk_keycode_t lkkbd_keycode[LK_NUM_KEYCODES] = {
  145. [0x56] = KEY_F1,
  146. [0x57] = KEY_F2,
  147. [0x58] = KEY_F3,
  148. [0x59] = KEY_F4,
  149. [0x5a] = KEY_F5,
  150. [0x64] = KEY_F6,
  151. [0x65] = KEY_F7,
  152. [0x66] = KEY_F8,
  153. [0x67] = KEY_F9,
  154. [0x68] = KEY_F10,
  155. [0x71] = KEY_F11,
  156. [0x72] = KEY_F12,
  157. [0x73] = KEY_F13,
  158. [0x74] = KEY_F14,
  159. [0x7c] = KEY_F15,
  160. [0x7d] = KEY_F16,
  161. [0x80] = KEY_F17,
  162. [0x81] = KEY_F18,
  163. [0x82] = KEY_F19,
  164. [0x83] = KEY_F20,
  165. [0x8a] = KEY_FIND,
  166. [0x8b] = KEY_INSERT,
  167. [0x8c] = KEY_DELETE,
  168. [0x8d] = KEY_SELECT,
  169. [0x8e] = KEY_PAGEUP,
  170. [0x8f] = KEY_PAGEDOWN,
  171. [0x92] = KEY_KP0,
  172. [0x94] = KEY_KPDOT,
  173. [0x95] = KEY_KPENTER,
  174. [0x96] = KEY_KP1,
  175. [0x97] = KEY_KP2,
  176. [0x98] = KEY_KP3,
  177. [0x99] = KEY_KP4,
  178. [0x9a] = KEY_KP5,
  179. [0x9b] = KEY_KP6,
  180. [0x9c] = KEY_KPCOMMA,
  181. [0x9d] = KEY_KP7,
  182. [0x9e] = KEY_KP8,
  183. [0x9f] = KEY_KP9,
  184. [0xa0] = KEY_KPMINUS,
  185. [0xa1] = KEY_PROG1,
  186. [0xa2] = KEY_PROG2,
  187. [0xa3] = KEY_PROG3,
  188. [0xa4] = KEY_PROG4,
  189. [0xa7] = KEY_LEFT,
  190. [0xa8] = KEY_RIGHT,
  191. [0xa9] = KEY_DOWN,
  192. [0xaa] = KEY_UP,
  193. [0xab] = KEY_RIGHTSHIFT,
  194. [0xac] = KEY_LEFTALT,
  195. [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
  196. [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
  197. [0xaf] = KEY_LEFTCTRL,
  198. [0xb0] = KEY_CAPSLOCK,
  199. [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
  200. [0xb2] = KEY_RIGHTALT,
  201. [0xbc] = KEY_BACKSPACE,
  202. [0xbd] = KEY_ENTER,
  203. [0xbe] = KEY_TAB,
  204. [0xbf] = KEY_ESC,
  205. [0xc0] = KEY_1,
  206. [0xc1] = KEY_Q,
  207. [0xc2] = KEY_A,
  208. [0xc3] = KEY_Z,
  209. [0xc5] = KEY_2,
  210. [0xc6] = KEY_W,
  211. [0xc7] = KEY_S,
  212. [0xc8] = KEY_X,
  213. [0xc9] = KEY_102ND,
  214. [0xcb] = KEY_3,
  215. [0xcc] = KEY_E,
  216. [0xcd] = KEY_D,
  217. [0xce] = KEY_C,
  218. [0xd0] = KEY_4,
  219. [0xd1] = KEY_R,
  220. [0xd2] = KEY_F,
  221. [0xd3] = KEY_V,
  222. [0xd4] = KEY_SPACE,
  223. [0xd6] = KEY_5,
  224. [0xd7] = KEY_T,
  225. [0xd8] = KEY_G,
  226. [0xd9] = KEY_B,
  227. [0xdb] = KEY_6,
  228. [0xdc] = KEY_Y,
  229. [0xdd] = KEY_H,
  230. [0xde] = KEY_N,
  231. [0xe0] = KEY_7,
  232. [0xe1] = KEY_U,
  233. [0xe2] = KEY_J,
  234. [0xe3] = KEY_M,
  235. [0xe5] = KEY_8,
  236. [0xe6] = KEY_I,
  237. [0xe7] = KEY_K,
  238. [0xe8] = KEY_COMMA,
  239. [0xea] = KEY_9,
  240. [0xeb] = KEY_O,
  241. [0xec] = KEY_L,
  242. [0xed] = KEY_DOT,
  243. [0xef] = KEY_0,
  244. [0xf0] = KEY_P,
  245. [0xf2] = KEY_SEMICOLON,
  246. [0xf3] = KEY_SLASH,
  247. [0xf5] = KEY_EQUAL,
  248. [0xf6] = KEY_RIGHTBRACE,
  249. [0xf7] = KEY_BACKSLASH,
  250. [0xf9] = KEY_MINUS,
  251. [0xfa] = KEY_LEFTBRACE,
  252. [0xfb] = KEY_APOSTROPHE,
  253. };
  254. #define CHECK_LED(LED, BITS) do { \
  255. if (test_bit (LED, lk->dev.led)) \
  256. leds_on |= BITS; \
  257. else \
  258. leds_off |= BITS; \
  259. } while (0)
  260. /*
  261. * Per-keyboard data
  262. */
  263. struct lkkbd {
  264. lk_keycode_t keycode[LK_NUM_KEYCODES];
  265. int ignore_bytes;
  266. unsigned char id[LK_NUM_IGNORE_BYTES];
  267. struct input_dev dev;
  268. struct serio *serio;
  269. struct work_struct tq;
  270. char name[64];
  271. char phys[32];
  272. char type;
  273. int bell_volume;
  274. int keyclick_volume;
  275. int ctrlclick_volume;
  276. };
  277. /*
  278. * Calculate volume parameter byte for a given volume.
  279. */
  280. static unsigned char
  281. volume_to_hw (int volume_percent)
  282. {
  283. unsigned char ret = 0;
  284. if (volume_percent < 0)
  285. volume_percent = 0;
  286. if (volume_percent > 100)
  287. volume_percent = 100;
  288. if (volume_percent >= 0)
  289. ret = 7;
  290. if (volume_percent >= 13) /* 12.5 */
  291. ret = 6;
  292. if (volume_percent >= 25)
  293. ret = 5;
  294. if (volume_percent >= 38) /* 37.5 */
  295. ret = 4;
  296. if (volume_percent >= 50)
  297. ret = 3;
  298. if (volume_percent >= 63) /* 62.5 */
  299. ret = 2; /* This is the default volume */
  300. if (volume_percent >= 75)
  301. ret = 1;
  302. if (volume_percent >= 88) /* 87.5 */
  303. ret = 0;
  304. ret |= 0x80;
  305. return ret;
  306. }
  307. static void
  308. lkkbd_detection_done (struct lkkbd *lk)
  309. {
  310. int i;
  311. /*
  312. * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
  313. */
  314. lk->keycode[0xb1] = KEY_COMPOSE;
  315. /*
  316. * Print keyboard name and modify Compose=Alt on user's request.
  317. */
  318. switch (lk->id[4]) {
  319. case 1:
  320. sprintf (lk->name, "DEC LK201 keyboard");
  321. if (lk201_compose_is_alt)
  322. lk->keycode[0xb1] = KEY_LEFTALT;
  323. break;
  324. case 2:
  325. sprintf (lk->name, "DEC LK401 keyboard");
  326. break;
  327. default:
  328. sprintf (lk->name, "Unknown DEC keyboard");
  329. printk (KERN_ERR "lkkbd: keyboard on %s is unknown, "
  330. "please report to Jan-Benedict Glaw "
  331. "<jbglaw@lug-owl.de>\n", lk->phys);
  332. printk (KERN_ERR "lkkbd: keyboard ID'ed as:");
  333. for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
  334. printk (" 0x%02x", lk->id[i]);
  335. printk ("\n");
  336. break;
  337. }
  338. printk (KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
  339. lk->phys, lk->name);
  340. /*
  341. * Report errors during keyboard boot-up.
  342. */
  343. switch (lk->id[2]) {
  344. case 0x00:
  345. /* All okay */
  346. break;
  347. case LK_STUCK_KEY:
  348. printk (KERN_ERR "lkkbd: Stuck key on keyboard at "
  349. "%s\n", lk->phys);
  350. break;
  351. case LK_SELFTEST_FAILED:
  352. printk (KERN_ERR "lkkbd: Selftest failed on keyboard "
  353. "at %s, keyboard may not work "
  354. "properly\n", lk->phys);
  355. break;
  356. default:
  357. printk (KERN_ERR "lkkbd: Unknown error %02x on "
  358. "keyboard at %s\n", lk->id[2],
  359. lk->phys);
  360. break;
  361. }
  362. /*
  363. * Try to hint user if there's a stuck key.
  364. */
  365. if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
  366. printk (KERN_ERR "Scancode of stuck key is 0x%02x, keycode "
  367. "is 0x%04x\n", lk->id[3],
  368. lk->keycode[lk->id[3]]);
  369. return;
  370. }
  371. /*
  372. * lkkbd_interrupt() is called by the low level driver when a character
  373. * is received.
  374. */
  375. static irqreturn_t
  376. lkkbd_interrupt (struct serio *serio, unsigned char data, unsigned int flags,
  377. struct pt_regs *regs)
  378. {
  379. struct lkkbd *lk = serio_get_drvdata (serio);
  380. int i;
  381. DBG (KERN_INFO "Got byte 0x%02x\n", data);
  382. if (lk->ignore_bytes > 0) {
  383. DBG (KERN_INFO "Ignoring a byte on %s\n",
  384. lk->name);
  385. lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
  386. if (lk->ignore_bytes == 0)
  387. lkkbd_detection_done (lk);
  388. return IRQ_HANDLED;
  389. }
  390. switch (data) {
  391. case LK_ALL_KEYS_UP:
  392. input_regs (&lk->dev, regs);
  393. for (i = 0; i < ARRAY_SIZE (lkkbd_keycode); i++)
  394. if (lk->keycode[i] != KEY_RESERVED)
  395. input_report_key (&lk->dev, lk->keycode[i], 0);
  396. input_sync (&lk->dev);
  397. break;
  398. case LK_METRONOME:
  399. DBG (KERN_INFO "Got LK_METRONOME and don't "
  400. "know how to handle...\n");
  401. break;
  402. case LK_OUTPUT_ERROR:
  403. DBG (KERN_INFO "Got LK_OUTPUT_ERROR and don't "
  404. "know how to handle...\n");
  405. break;
  406. case LK_INPUT_ERROR:
  407. DBG (KERN_INFO "Got LK_INPUT_ERROR and don't "
  408. "know how to handle...\n");
  409. break;
  410. case LK_KBD_LOCKED:
  411. DBG (KERN_INFO "Got LK_KBD_LOCKED and don't "
  412. "know how to handle...\n");
  413. break;
  414. case LK_KBD_TEST_MODE_ACK:
  415. DBG (KERN_INFO "Got LK_KBD_TEST_MODE_ACK and don't "
  416. "know how to handle...\n");
  417. break;
  418. case LK_PREFIX_KEY_DOWN:
  419. DBG (KERN_INFO "Got LK_PREFIX_KEY_DOWN and don't "
  420. "know how to handle...\n");
  421. break;
  422. case LK_MODE_CHANGE_ACK:
  423. DBG (KERN_INFO "Got LK_MODE_CHANGE_ACK and ignored "
  424. "it properly...\n");
  425. break;
  426. case LK_RESPONSE_RESERVED:
  427. DBG (KERN_INFO "Got LK_RESPONSE_RESERVED and don't "
  428. "know how to handle...\n");
  429. break;
  430. case 0x01:
  431. DBG (KERN_INFO "Got 0x01, scheduling re-initialization\n");
  432. lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
  433. lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
  434. schedule_work (&lk->tq);
  435. break;
  436. default:
  437. if (lk->keycode[data] != KEY_RESERVED) {
  438. input_regs (&lk->dev, regs);
  439. if (!test_bit (lk->keycode[data], lk->dev.key))
  440. input_report_key (&lk->dev, lk->keycode[data], 1);
  441. else
  442. input_report_key (&lk->dev, lk->keycode[data], 0);
  443. input_sync (&lk->dev);
  444. } else
  445. printk (KERN_WARNING "%s: Unknown key with "
  446. "scancode 0x%02x on %s.\n",
  447. __FILE__, data, lk->name);
  448. }
  449. return IRQ_HANDLED;
  450. }
  451. /*
  452. * lkkbd_event() handles events from the input module.
  453. */
  454. static int
  455. lkkbd_event (struct input_dev *dev, unsigned int type, unsigned int code,
  456. int value)
  457. {
  458. struct lkkbd *lk = dev->private;
  459. unsigned char leds_on = 0;
  460. unsigned char leds_off = 0;
  461. switch (type) {
  462. case EV_LED:
  463. CHECK_LED (LED_CAPSL, LK_LED_SHIFTLOCK);
  464. CHECK_LED (LED_COMPOSE, LK_LED_COMPOSE);
  465. CHECK_LED (LED_SCROLLL, LK_LED_SCROLLLOCK);
  466. CHECK_LED (LED_SLEEP, LK_LED_WAIT);
  467. if (leds_on != 0) {
  468. lk->serio->write (lk->serio, LK_CMD_LED_ON);
  469. lk->serio->write (lk->serio, leds_on);
  470. }
  471. if (leds_off != 0) {
  472. lk->serio->write (lk->serio, LK_CMD_LED_OFF);
  473. lk->serio->write (lk->serio, leds_off);
  474. }
  475. return 0;
  476. case EV_SND:
  477. switch (code) {
  478. case SND_CLICK:
  479. if (value == 0) {
  480. DBG ("%s: Deactivating key clicks\n", __FUNCTION__);
  481. lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK);
  482. lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK);
  483. } else {
  484. DBG ("%s: Activating key clicks\n", __FUNCTION__);
  485. lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK);
  486. lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume));
  487. lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK);
  488. lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume));
  489. }
  490. return 0;
  491. case SND_BELL:
  492. if (value != 0)
  493. lk->serio->write (lk->serio, LK_CMD_SOUND_BELL);
  494. return 0;
  495. }
  496. break;
  497. default:
  498. printk (KERN_ERR "%s (): Got unknown type %d, code %d, value %d\n",
  499. __FUNCTION__, type, code, value);
  500. }
  501. return -1;
  502. }
  503. /*
  504. * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
  505. * were in.
  506. */
  507. static void
  508. lkkbd_reinit (void *data)
  509. {
  510. struct lkkbd *lk = data;
  511. int division;
  512. unsigned char leds_on = 0;
  513. unsigned char leds_off = 0;
  514. /* Ask for ID */
  515. lk->serio->write (lk->serio, LK_CMD_REQUEST_ID);
  516. /* Reset parameters */
  517. lk->serio->write (lk->serio, LK_CMD_SET_DEFAULTS);
  518. /* Set LEDs */
  519. CHECK_LED (LED_CAPSL, LK_LED_SHIFTLOCK);
  520. CHECK_LED (LED_COMPOSE, LK_LED_COMPOSE);
  521. CHECK_LED (LED_SCROLLL, LK_LED_SCROLLLOCK);
  522. CHECK_LED (LED_SLEEP, LK_LED_WAIT);
  523. if (leds_on != 0) {
  524. lk->serio->write (lk->serio, LK_CMD_LED_ON);
  525. lk->serio->write (lk->serio, leds_on);
  526. }
  527. if (leds_off != 0) {
  528. lk->serio->write (lk->serio, LK_CMD_LED_OFF);
  529. lk->serio->write (lk->serio, leds_off);
  530. }
  531. /*
  532. * Try to activate extended LK401 mode. This command will
  533. * only work with a LK401 keyboard and grants access to
  534. * LAlt, RAlt, RCompose and RShift.
  535. */
  536. lk->serio->write (lk->serio, LK_CMD_ENABLE_LK401);
  537. /* Set all keys to UPDOWN mode */
  538. for (division = 1; division <= 14; division++)
  539. lk->serio->write (lk->serio, LK_CMD_SET_MODE (LK_MODE_UPDOWN,
  540. division));
  541. /* Enable bell and set volume */
  542. lk->serio->write (lk->serio, LK_CMD_ENABLE_BELL);
  543. lk->serio->write (lk->serio, volume_to_hw (lk->bell_volume));
  544. /* Enable/disable keyclick (and possibly set volume) */
  545. if (test_bit (SND_CLICK, lk->dev.snd)) {
  546. lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK);
  547. lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume));
  548. lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK);
  549. lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume));
  550. } else {
  551. lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK);
  552. lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK);
  553. }
  554. /* Sound the bell if needed */
  555. if (test_bit (SND_BELL, lk->dev.snd))
  556. lk->serio->write (lk->serio, LK_CMD_SOUND_BELL);
  557. }
  558. /*
  559. * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
  560. */
  561. static int
  562. lkkbd_connect (struct serio *serio, struct serio_driver *drv)
  563. {
  564. struct lkkbd *lk;
  565. int i;
  566. int err;
  567. if (!(lk = kmalloc (sizeof (struct lkkbd), GFP_KERNEL)))
  568. return -ENOMEM;
  569. memset (lk, 0, sizeof (struct lkkbd));
  570. init_input_dev (&lk->dev);
  571. set_bit (EV_KEY, lk->dev.evbit);
  572. set_bit (EV_LED, lk->dev.evbit);
  573. set_bit (EV_SND, lk->dev.evbit);
  574. set_bit (EV_REP, lk->dev.evbit);
  575. set_bit (LED_CAPSL, lk->dev.ledbit);
  576. set_bit (LED_SLEEP, lk->dev.ledbit);
  577. set_bit (LED_COMPOSE, lk->dev.ledbit);
  578. set_bit (LED_SCROLLL, lk->dev.ledbit);
  579. set_bit (SND_BELL, lk->dev.sndbit);
  580. set_bit (SND_CLICK, lk->dev.sndbit);
  581. lk->serio = serio;
  582. INIT_WORK (&lk->tq, lkkbd_reinit, lk);
  583. lk->bell_volume = bell_volume;
  584. lk->keyclick_volume = keyclick_volume;
  585. lk->ctrlclick_volume = ctrlclick_volume;
  586. lk->dev.keycode = lk->keycode;
  587. lk->dev.keycodesize = sizeof (lk_keycode_t);
  588. lk->dev.keycodemax = LK_NUM_KEYCODES;
  589. lk->dev.event = lkkbd_event;
  590. lk->dev.private = lk;
  591. serio_set_drvdata (serio, lk);
  592. err = serio_open (serio, drv);
  593. if (err) {
  594. serio_set_drvdata (serio, NULL);
  595. kfree (lk);
  596. return err;
  597. }
  598. sprintf (lk->name, "DEC LK keyboard");
  599. sprintf (lk->phys, "%s/input0", serio->phys);
  600. memcpy (lk->keycode, lkkbd_keycode, sizeof (lk_keycode_t) * LK_NUM_KEYCODES);
  601. for (i = 0; i < LK_NUM_KEYCODES; i++)
  602. set_bit (lk->keycode[i], lk->dev.keybit);
  603. lk->dev.name = lk->name;
  604. lk->dev.phys = lk->phys;
  605. lk->dev.id.bustype = BUS_RS232;
  606. lk->dev.id.vendor = SERIO_LKKBD;
  607. lk->dev.id.product = 0;
  608. lk->dev.id.version = 0x0100;
  609. lk->dev.dev = &serio->dev;
  610. input_register_device (&lk->dev);
  611. printk (KERN_INFO "input: %s on %s, initiating reset\n", lk->name, serio->phys);
  612. lk->serio->write (lk->serio, LK_CMD_POWERCYCLE_RESET);
  613. return 0;
  614. }
  615. /*
  616. * lkkbd_disconnect() unregisters and closes behind us.
  617. */
  618. static void
  619. lkkbd_disconnect (struct serio *serio)
  620. {
  621. struct lkkbd *lk = serio_get_drvdata (serio);
  622. input_unregister_device (&lk->dev);
  623. serio_close (serio);
  624. serio_set_drvdata (serio, NULL);
  625. kfree (lk);
  626. }
  627. static struct serio_device_id lkkbd_serio_ids[] = {
  628. {
  629. .type = SERIO_RS232,
  630. .proto = SERIO_LKKBD,
  631. .id = SERIO_ANY,
  632. .extra = SERIO_ANY,
  633. },
  634. { 0 }
  635. };
  636. MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
  637. static struct serio_driver lkkbd_drv = {
  638. .driver = {
  639. .name = "lkkbd",
  640. },
  641. .description = DRIVER_DESC,
  642. .id_table = lkkbd_serio_ids,
  643. .connect = lkkbd_connect,
  644. .disconnect = lkkbd_disconnect,
  645. .interrupt = lkkbd_interrupt,
  646. };
  647. /*
  648. * The functions for insering/removing us as a module.
  649. */
  650. static int __init
  651. lkkbd_init (void)
  652. {
  653. serio_register_driver(&lkkbd_drv);
  654. return 0;
  655. }
  656. static void __exit
  657. lkkbd_exit (void)
  658. {
  659. serio_unregister_driver(&lkkbd_drv);
  660. }
  661. module_init (lkkbd_init);
  662. module_exit (lkkbd_exit);