lkkbd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. #include <linux/delay.h>
  61. #include <linux/slab.h>
  62. #include <linux/module.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/input.h>
  65. #include <linux/serio.h>
  66. #include <linux/workqueue.h>
  67. #define DRIVER_DESC "LK keyboard driver"
  68. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  69. MODULE_DESCRIPTION(DRIVER_DESC);
  70. MODULE_LICENSE("GPL");
  71. /*
  72. * Known parameters:
  73. * bell_volume
  74. * keyclick_volume
  75. * ctrlclick_volume
  76. *
  77. * Please notice that there's not yet an API to set these at runtime.
  78. */
  79. static int bell_volume = 100; /* % */
  80. module_param(bell_volume, int, 0);
  81. MODULE_PARM_DESC(bell_volume, "Bell volume (in %). default is 100%");
  82. static int keyclick_volume = 100; /* % */
  83. module_param(keyclick_volume, int, 0);
  84. MODULE_PARM_DESC(keyclick_volume, "Keyclick volume (in %), default is 100%");
  85. static int ctrlclick_volume = 100; /* % */
  86. module_param(ctrlclick_volume, int, 0);
  87. MODULE_PARM_DESC(ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
  88. static int lk201_compose_is_alt;
  89. module_param(lk201_compose_is_alt, int, 0);
  90. MODULE_PARM_DESC(lk201_compose_is_alt,
  91. "If set non-zero, LK201' Compose key will act as an Alt key");
  92. #undef LKKBD_DEBUG
  93. #ifdef LKKBD_DEBUG
  94. #define DBG(x...) printk(x)
  95. #else
  96. #define DBG(x...) do {} while (0)
  97. #endif
  98. /* LED control */
  99. #define LK_LED_WAIT 0x81
  100. #define LK_LED_COMPOSE 0x82
  101. #define LK_LED_SHIFTLOCK 0x84
  102. #define LK_LED_SCROLLLOCK 0x88
  103. #define LK_CMD_LED_ON 0x13
  104. #define LK_CMD_LED_OFF 0x11
  105. /* Mode control */
  106. #define LK_MODE_DOWN 0x80
  107. #define LK_MODE_AUTODOWN 0x82
  108. #define LK_MODE_UPDOWN 0x86
  109. #define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
  110. /* Misc commands */
  111. #define LK_CMD_ENABLE_KEYCLICK 0x1b
  112. #define LK_CMD_DISABLE_KEYCLICK 0x99
  113. #define LK_CMD_DISABLE_BELL 0xa1
  114. #define LK_CMD_SOUND_BELL 0xa7
  115. #define LK_CMD_ENABLE_BELL 0x23
  116. #define LK_CMD_DISABLE_CTRCLICK 0xb9
  117. #define LK_CMD_ENABLE_CTRCLICK 0xbb
  118. #define LK_CMD_SET_DEFAULTS 0xd3
  119. #define LK_CMD_POWERCYCLE_RESET 0xfd
  120. #define LK_CMD_ENABLE_LK401 0xe9
  121. #define LK_CMD_REQUEST_ID 0xab
  122. /* Misc responses from keyboard */
  123. #define LK_STUCK_KEY 0x3d
  124. #define LK_SELFTEST_FAILED 0x3e
  125. #define LK_ALL_KEYS_UP 0xb3
  126. #define LK_METRONOME 0xb4
  127. #define LK_OUTPUT_ERROR 0xb5
  128. #define LK_INPUT_ERROR 0xb6
  129. #define LK_KBD_LOCKED 0xb7
  130. #define LK_KBD_TEST_MODE_ACK 0xb8
  131. #define LK_PREFIX_KEY_DOWN 0xb9
  132. #define LK_MODE_CHANGE_ACK 0xba
  133. #define LK_RESPONSE_RESERVED 0xbb
  134. #define LK_NUM_KEYCODES 256
  135. #define LK_NUM_IGNORE_BYTES 6
  136. static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
  137. [0x56] = KEY_F1,
  138. [0x57] = KEY_F2,
  139. [0x58] = KEY_F3,
  140. [0x59] = KEY_F4,
  141. [0x5a] = KEY_F5,
  142. [0x64] = KEY_F6,
  143. [0x65] = KEY_F7,
  144. [0x66] = KEY_F8,
  145. [0x67] = KEY_F9,
  146. [0x68] = KEY_F10,
  147. [0x71] = KEY_F11,
  148. [0x72] = KEY_F12,
  149. [0x73] = KEY_F13,
  150. [0x74] = KEY_F14,
  151. [0x7c] = KEY_F15,
  152. [0x7d] = KEY_F16,
  153. [0x80] = KEY_F17,
  154. [0x81] = KEY_F18,
  155. [0x82] = KEY_F19,
  156. [0x83] = KEY_F20,
  157. [0x8a] = KEY_FIND,
  158. [0x8b] = KEY_INSERT,
  159. [0x8c] = KEY_DELETE,
  160. [0x8d] = KEY_SELECT,
  161. [0x8e] = KEY_PAGEUP,
  162. [0x8f] = KEY_PAGEDOWN,
  163. [0x92] = KEY_KP0,
  164. [0x94] = KEY_KPDOT,
  165. [0x95] = KEY_KPENTER,
  166. [0x96] = KEY_KP1,
  167. [0x97] = KEY_KP2,
  168. [0x98] = KEY_KP3,
  169. [0x99] = KEY_KP4,
  170. [0x9a] = KEY_KP5,
  171. [0x9b] = KEY_KP6,
  172. [0x9c] = KEY_KPCOMMA,
  173. [0x9d] = KEY_KP7,
  174. [0x9e] = KEY_KP8,
  175. [0x9f] = KEY_KP9,
  176. [0xa0] = KEY_KPMINUS,
  177. [0xa1] = KEY_PROG1,
  178. [0xa2] = KEY_PROG2,
  179. [0xa3] = KEY_PROG3,
  180. [0xa4] = KEY_PROG4,
  181. [0xa7] = KEY_LEFT,
  182. [0xa8] = KEY_RIGHT,
  183. [0xa9] = KEY_DOWN,
  184. [0xaa] = KEY_UP,
  185. [0xab] = KEY_RIGHTSHIFT,
  186. [0xac] = KEY_LEFTALT,
  187. [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
  188. [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
  189. [0xaf] = KEY_LEFTCTRL,
  190. [0xb0] = KEY_CAPSLOCK,
  191. [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
  192. [0xb2] = KEY_RIGHTALT,
  193. [0xbc] = KEY_BACKSPACE,
  194. [0xbd] = KEY_ENTER,
  195. [0xbe] = KEY_TAB,
  196. [0xbf] = KEY_ESC,
  197. [0xc0] = KEY_1,
  198. [0xc1] = KEY_Q,
  199. [0xc2] = KEY_A,
  200. [0xc3] = KEY_Z,
  201. [0xc5] = KEY_2,
  202. [0xc6] = KEY_W,
  203. [0xc7] = KEY_S,
  204. [0xc8] = KEY_X,
  205. [0xc9] = KEY_102ND,
  206. [0xcb] = KEY_3,
  207. [0xcc] = KEY_E,
  208. [0xcd] = KEY_D,
  209. [0xce] = KEY_C,
  210. [0xd0] = KEY_4,
  211. [0xd1] = KEY_R,
  212. [0xd2] = KEY_F,
  213. [0xd3] = KEY_V,
  214. [0xd4] = KEY_SPACE,
  215. [0xd6] = KEY_5,
  216. [0xd7] = KEY_T,
  217. [0xd8] = KEY_G,
  218. [0xd9] = KEY_B,
  219. [0xdb] = KEY_6,
  220. [0xdc] = KEY_Y,
  221. [0xdd] = KEY_H,
  222. [0xde] = KEY_N,
  223. [0xe0] = KEY_7,
  224. [0xe1] = KEY_U,
  225. [0xe2] = KEY_J,
  226. [0xe3] = KEY_M,
  227. [0xe5] = KEY_8,
  228. [0xe6] = KEY_I,
  229. [0xe7] = KEY_K,
  230. [0xe8] = KEY_COMMA,
  231. [0xea] = KEY_9,
  232. [0xeb] = KEY_O,
  233. [0xec] = KEY_L,
  234. [0xed] = KEY_DOT,
  235. [0xef] = KEY_0,
  236. [0xf0] = KEY_P,
  237. [0xf2] = KEY_SEMICOLON,
  238. [0xf3] = KEY_SLASH,
  239. [0xf5] = KEY_EQUAL,
  240. [0xf6] = KEY_RIGHTBRACE,
  241. [0xf7] = KEY_BACKSLASH,
  242. [0xf9] = KEY_MINUS,
  243. [0xfa] = KEY_LEFTBRACE,
  244. [0xfb] = KEY_APOSTROPHE,
  245. };
  246. #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
  247. if (test_bit(LED, (LK)->dev->led)) \
  248. VAR_ON |= BITS; \
  249. else \
  250. VAR_OFF |= BITS; \
  251. } while (0)
  252. /*
  253. * Per-keyboard data
  254. */
  255. struct lkkbd {
  256. unsigned short keycode[LK_NUM_KEYCODES];
  257. int ignore_bytes;
  258. unsigned char id[LK_NUM_IGNORE_BYTES];
  259. struct input_dev *dev;
  260. struct serio *serio;
  261. struct work_struct tq;
  262. char name[64];
  263. char phys[32];
  264. char type;
  265. int bell_volume;
  266. int keyclick_volume;
  267. int ctrlclick_volume;
  268. };
  269. #ifdef LKKBD_DEBUG
  270. /*
  271. * Responses from the keyboard and mapping back to their names.
  272. */
  273. static struct {
  274. unsigned char value;
  275. unsigned char *name;
  276. } lk_response[] = {
  277. #define RESPONSE(x) { .value = (x), .name = #x, }
  278. RESPONSE(LK_STUCK_KEY),
  279. RESPONSE(LK_SELFTEST_FAILED),
  280. RESPONSE(LK_ALL_KEYS_UP),
  281. RESPONSE(LK_METRONOME),
  282. RESPONSE(LK_OUTPUT_ERROR),
  283. RESPONSE(LK_INPUT_ERROR),
  284. RESPONSE(LK_KBD_LOCKED),
  285. RESPONSE(LK_KBD_TEST_MODE_ACK),
  286. RESPONSE(LK_PREFIX_KEY_DOWN),
  287. RESPONSE(LK_MODE_CHANGE_ACK),
  288. RESPONSE(LK_RESPONSE_RESERVED),
  289. #undef RESPONSE
  290. };
  291. static unsigned char *response_name(unsigned char value)
  292. {
  293. int i;
  294. for (i = 0; i < ARRAY_SIZE(lk_response); i++)
  295. if (lk_response[i].value == value)
  296. return lk_response[i].name;
  297. return "<unknown>";
  298. }
  299. #endif /* LKKBD_DEBUG */
  300. /*
  301. * Calculate volume parameter byte for a given volume.
  302. */
  303. static unsigned char volume_to_hw(int volume_percent)
  304. {
  305. unsigned char ret = 0;
  306. if (volume_percent < 0)
  307. volume_percent = 0;
  308. if (volume_percent > 100)
  309. volume_percent = 100;
  310. if (volume_percent >= 0)
  311. ret = 7;
  312. if (volume_percent >= 13) /* 12.5 */
  313. ret = 6;
  314. if (volume_percent >= 25)
  315. ret = 5;
  316. if (volume_percent >= 38) /* 37.5 */
  317. ret = 4;
  318. if (volume_percent >= 50)
  319. ret = 3;
  320. if (volume_percent >= 63) /* 62.5 */
  321. ret = 2; /* This is the default volume */
  322. if (volume_percent >= 75)
  323. ret = 1;
  324. if (volume_percent >= 88) /* 87.5 */
  325. ret = 0;
  326. ret |= 0x80;
  327. return ret;
  328. }
  329. static void lkkbd_detection_done(struct lkkbd *lk)
  330. {
  331. int i;
  332. /*
  333. * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
  334. */
  335. lk->keycode[0xb1] = KEY_COMPOSE;
  336. /*
  337. * Print keyboard name and modify Compose=Alt on user's request.
  338. */
  339. switch (lk->id[4]) {
  340. case 1:
  341. strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
  342. if (lk201_compose_is_alt)
  343. lk->keycode[0xb1] = KEY_LEFTALT;
  344. break;
  345. case 2:
  346. strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
  347. break;
  348. default:
  349. strlcpy(lk->name, "Unknown DEC keyboard", sizeof(lk->name));
  350. printk(KERN_ERR
  351. "lkkbd: keyboard on %s is unknown, please report to "
  352. "Jan-Benedict Glaw <jbglaw@lug-owl.de>\n", lk->phys);
  353. printk(KERN_ERR "lkkbd: keyboard ID'ed as:");
  354. for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
  355. printk(" 0x%02x", lk->id[i]);
  356. printk("\n");
  357. break;
  358. }
  359. printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
  360. lk->phys, lk->name);
  361. /*
  362. * Report errors during keyboard boot-up.
  363. */
  364. switch (lk->id[2]) {
  365. case 0x00:
  366. /* All okay */
  367. break;
  368. case LK_STUCK_KEY:
  369. printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
  370. lk->phys);
  371. break;
  372. case LK_SELFTEST_FAILED:
  373. printk(KERN_ERR
  374. "lkkbd: Selftest failed on keyboard at %s, "
  375. "keyboard may not work properly\n", lk->phys);
  376. break;
  377. default:
  378. printk(KERN_ERR
  379. "lkkbd: Unknown error %02x on keyboard at %s\n",
  380. lk->id[2], lk->phys);
  381. break;
  382. }
  383. /*
  384. * Try to hint user if there's a stuck key.
  385. */
  386. if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
  387. printk(KERN_ERR
  388. "Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
  389. lk->id[3], lk->keycode[lk->id[3]]);
  390. }
  391. /*
  392. * lkkbd_interrupt() is called by the low level driver when a character
  393. * is received.
  394. */
  395. static irqreturn_t lkkbd_interrupt(struct serio *serio,
  396. unsigned char data, unsigned int flags)
  397. {
  398. struct lkkbd *lk = serio_get_drvdata(serio);
  399. struct input_dev *input_dev = lk->dev;
  400. unsigned int keycode;
  401. int i;
  402. DBG(KERN_INFO "Got byte 0x%02x\n", data);
  403. if (lk->ignore_bytes > 0) {
  404. DBG(KERN_INFO "Ignoring a byte on %s\n", lk->name);
  405. lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
  406. if (lk->ignore_bytes == 0)
  407. lkkbd_detection_done(lk);
  408. return IRQ_HANDLED;
  409. }
  410. switch (data) {
  411. case LK_ALL_KEYS_UP:
  412. for (i = 0; i < ARRAY_SIZE(lkkbd_keycode); i++)
  413. input_report_key(input_dev, lk->keycode[i], 0);
  414. input_sync(input_dev);
  415. break;
  416. case 0x01:
  417. DBG(KERN_INFO "Got 0x01, scheduling re-initialization\n");
  418. lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
  419. lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
  420. schedule_work(&lk->tq);
  421. break;
  422. case LK_METRONOME:
  423. case LK_OUTPUT_ERROR:
  424. case LK_INPUT_ERROR:
  425. case LK_KBD_LOCKED:
  426. case LK_KBD_TEST_MODE_ACK:
  427. case LK_PREFIX_KEY_DOWN:
  428. case LK_MODE_CHANGE_ACK:
  429. case LK_RESPONSE_RESERVED:
  430. DBG(KERN_INFO "Got %s and don't know how to handle...\n",
  431. response_name(data));
  432. break;
  433. default:
  434. keycode = lk->keycode[data];
  435. if (keycode != KEY_RESERVED) {
  436. input_report_key(input_dev, keycode,
  437. !test_bit(keycode, input_dev->key));
  438. input_sync(input_dev);
  439. } else {
  440. printk(KERN_WARNING
  441. "%s: Unknown key with scancode 0x%02x on %s.\n",
  442. __FILE__, data, lk->name);
  443. }
  444. }
  445. return IRQ_HANDLED;
  446. }
  447. static void lkkbd_toggle_leds(struct lkkbd *lk)
  448. {
  449. struct serio *serio = lk->serio;
  450. unsigned char leds_on = 0;
  451. unsigned char leds_off = 0;
  452. CHECK_LED(lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK);
  453. CHECK_LED(lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE);
  454. CHECK_LED(lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK);
  455. CHECK_LED(lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT);
  456. if (leds_on != 0) {
  457. serio_write(serio, LK_CMD_LED_ON);
  458. serio_write(serio, leds_on);
  459. }
  460. if (leds_off != 0) {
  461. serio_write(serio, LK_CMD_LED_OFF);
  462. serio_write(serio, leds_off);
  463. }
  464. }
  465. static void lkkbd_toggle_keyclick(struct lkkbd *lk, bool on)
  466. {
  467. struct serio *serio = lk->serio;
  468. if (on) {
  469. DBG("%s: Activating key clicks\n", __func__);
  470. serio_write(serio, LK_CMD_ENABLE_KEYCLICK);
  471. serio_write(serio, volume_to_hw(lk->keyclick_volume));
  472. serio_write(serio, LK_CMD_ENABLE_CTRCLICK);
  473. serio_write(serio, volume_to_hw(lk->ctrlclick_volume));
  474. } else {
  475. DBG("%s: Deactivating key clicks\n", __func__);
  476. serio_write(serio, LK_CMD_DISABLE_KEYCLICK);
  477. serio_write(serio, LK_CMD_DISABLE_CTRCLICK);
  478. }
  479. }
  480. /*
  481. * lkkbd_event() handles events from the input module.
  482. */
  483. static int lkkbd_event(struct input_dev *dev,
  484. unsigned int type, unsigned int code, int value)
  485. {
  486. struct lkkbd *lk = input_get_drvdata(dev);
  487. switch (type) {
  488. case EV_LED:
  489. lkkbd_toggle_leds(lk);
  490. return 0;
  491. case EV_SND:
  492. switch (code) {
  493. case SND_CLICK:
  494. lkkbd_toggle_keyclick(lk, value);
  495. return 0;
  496. case SND_BELL:
  497. if (value != 0)
  498. serio_write(lk->serio, LK_CMD_SOUND_BELL);
  499. return 0;
  500. }
  501. break;
  502. default:
  503. printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
  504. __func__, type, code, value);
  505. }
  506. return -1;
  507. }
  508. /*
  509. * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
  510. * were in.
  511. */
  512. static void lkkbd_reinit(struct work_struct *work)
  513. {
  514. struct lkkbd *lk = container_of(work, struct lkkbd, tq);
  515. int division;
  516. /* Ask for ID */
  517. serio_write(lk->serio, LK_CMD_REQUEST_ID);
  518. /* Reset parameters */
  519. serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
  520. /* Set LEDs */
  521. lkkbd_toggle_leds(lk);
  522. /*
  523. * Try to activate extended LK401 mode. This command will
  524. * only work with a LK401 keyboard and grants access to
  525. * LAlt, RAlt, RCompose and RShift.
  526. */
  527. serio_write(lk->serio, LK_CMD_ENABLE_LK401);
  528. /* Set all keys to UPDOWN mode */
  529. for (division = 1; division <= 14; division++)
  530. serio_write(lk->serio,
  531. LK_CMD_SET_MODE(LK_MODE_UPDOWN, division));
  532. /* Enable bell and set volume */
  533. serio_write(lk->serio, LK_CMD_ENABLE_BELL);
  534. serio_write(lk->serio, volume_to_hw(lk->bell_volume));
  535. /* Enable/disable keyclick (and possibly set volume) */
  536. lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
  537. /* Sound the bell if needed */
  538. if (test_bit(SND_BELL, lk->dev->snd))
  539. serio_write(lk->serio, LK_CMD_SOUND_BELL);
  540. }
  541. /*
  542. * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
  543. */
  544. static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
  545. {
  546. struct lkkbd *lk;
  547. struct input_dev *input_dev;
  548. int i;
  549. int err;
  550. lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
  551. input_dev = input_allocate_device();
  552. if (!lk || !input_dev) {
  553. err = -ENOMEM;
  554. goto fail1;
  555. }
  556. lk->serio = serio;
  557. lk->dev = input_dev;
  558. INIT_WORK(&lk->tq, lkkbd_reinit);
  559. lk->bell_volume = bell_volume;
  560. lk->keyclick_volume = keyclick_volume;
  561. lk->ctrlclick_volume = ctrlclick_volume;
  562. memcpy(lk->keycode, lkkbd_keycode, sizeof(lk->keycode));
  563. strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
  564. snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
  565. input_dev->name = lk->name;
  566. input_dev->phys = lk->phys;
  567. input_dev->id.bustype = BUS_RS232;
  568. input_dev->id.vendor = SERIO_LKKBD;
  569. input_dev->id.product = 0;
  570. input_dev->id.version = 0x0100;
  571. input_dev->dev.parent = &serio->dev;
  572. input_dev->event = lkkbd_event;
  573. input_set_drvdata(input_dev, lk);
  574. __set_bit(EV_KEY, input_dev->evbit);
  575. __set_bit(EV_LED, input_dev->evbit);
  576. __set_bit(EV_SND, input_dev->evbit);
  577. __set_bit(EV_REP, input_dev->evbit);
  578. __set_bit(LED_CAPSL, input_dev->ledbit);
  579. __set_bit(LED_SLEEP, input_dev->ledbit);
  580. __set_bit(LED_COMPOSE, input_dev->ledbit);
  581. __set_bit(LED_SCROLLL, input_dev->ledbit);
  582. __set_bit(SND_BELL, input_dev->sndbit);
  583. __set_bit(SND_CLICK, input_dev->sndbit);
  584. input_dev->keycode = lk->keycode;
  585. input_dev->keycodesize = sizeof(lk->keycode[0]);
  586. input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
  587. for (i = 0; i < LK_NUM_KEYCODES; i++)
  588. __set_bit(lk->keycode[i], input_dev->keybit);
  589. __clear_bit(KEY_RESERVED, input_dev->keybit);
  590. serio_set_drvdata(serio, lk);
  591. err = serio_open(serio, drv);
  592. if (err)
  593. goto fail2;
  594. err = input_register_device(lk->dev);
  595. if (err)
  596. goto fail3;
  597. serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
  598. return 0;
  599. fail3: serio_close(serio);
  600. fail2: serio_set_drvdata(serio, NULL);
  601. fail1: input_free_device(input_dev);
  602. kfree(lk);
  603. return err;
  604. }
  605. /*
  606. * lkkbd_disconnect() unregisters and closes behind us.
  607. */
  608. static void lkkbd_disconnect(struct serio *serio)
  609. {
  610. struct lkkbd *lk = serio_get_drvdata(serio);
  611. input_get_device(lk->dev);
  612. input_unregister_device(lk->dev);
  613. serio_close(serio);
  614. serio_set_drvdata(serio, NULL);
  615. input_put_device(lk->dev);
  616. kfree(lk);
  617. }
  618. static struct serio_device_id lkkbd_serio_ids[] = {
  619. {
  620. .type = SERIO_RS232,
  621. .proto = SERIO_LKKBD,
  622. .id = SERIO_ANY,
  623. .extra = SERIO_ANY,
  624. },
  625. { 0 }
  626. };
  627. MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
  628. static struct serio_driver lkkbd_drv = {
  629. .driver = {
  630. .name = "lkkbd",
  631. },
  632. .description = DRIVER_DESC,
  633. .id_table = lkkbd_serio_ids,
  634. .connect = lkkbd_connect,
  635. .disconnect = lkkbd_disconnect,
  636. .interrupt = lkkbd_interrupt,
  637. };
  638. module_serio_driver(lkkbd_drv);