sunkbd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * $Id: sunkbd.c,v 1.14 2001/09/25 10:12:07 vojtech Exp $
  3. *
  4. * Copyright (c) 1999-2001 Vojtech Pavlik
  5. */
  6. /*
  7. * Sun keyboard driver for Linux
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Should you need to contact me, the author, you can do so either by
  25. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  26. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/init.h>
  33. #include <linux/input.h>
  34. #include <linux/serio.h>
  35. #include <linux/workqueue.h>
  36. #define DRIVER_DESC "Sun keyboard driver"
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. static unsigned char sunkbd_keycode[128] = {
  41. 0,128,114,129,115, 59, 60, 68, 61, 87, 62, 88, 63,100, 64,112,
  42. 65, 66, 67, 56,103,119, 99, 70,105,130,131,108,106, 1, 2, 3,
  43. 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 41, 14,110,113, 98, 55,
  44. 116,132, 83,133,102, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  45. 26, 27,111,127, 71, 72, 73, 74,134,135,107, 0, 29, 30, 31, 32,
  46. 33, 34, 35, 36, 37, 38, 39, 40, 43, 28, 96, 75, 76, 77, 82,136,
  47. 104,137, 69, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,101,
  48. 79, 80, 81, 0, 0, 0,138, 58,125, 57,126,109, 86, 78
  49. };
  50. #define SUNKBD_CMD_RESET 0x1
  51. #define SUNKBD_CMD_BELLON 0x2
  52. #define SUNKBD_CMD_BELLOFF 0x3
  53. #define SUNKBD_CMD_CLICK 0xa
  54. #define SUNKBD_CMD_NOCLICK 0xb
  55. #define SUNKBD_CMD_SETLED 0xe
  56. #define SUNKBD_CMD_LAYOUT 0xf
  57. #define SUNKBD_RET_RESET 0xff
  58. #define SUNKBD_RET_ALLUP 0x7f
  59. #define SUNKBD_RET_LAYOUT 0xfe
  60. #define SUNKBD_LAYOUT_5_MASK 0x20
  61. #define SUNKBD_RELEASE 0x80
  62. #define SUNKBD_KEY 0x7f
  63. /*
  64. * Per-keyboard data.
  65. */
  66. struct sunkbd {
  67. unsigned char keycode[128];
  68. struct input_dev *dev;
  69. struct serio *serio;
  70. struct work_struct tq;
  71. wait_queue_head_t wait;
  72. char name[64];
  73. char phys[32];
  74. char type;
  75. unsigned char enabled;
  76. volatile s8 reset;
  77. volatile s8 layout;
  78. };
  79. /*
  80. * sunkbd_interrupt() is called by the low level driver when a character
  81. * is received.
  82. */
  83. static irqreturn_t sunkbd_interrupt(struct serio *serio,
  84. unsigned char data, unsigned int flags, struct pt_regs *regs)
  85. {
  86. struct sunkbd* sunkbd = serio_get_drvdata(serio);
  87. if (sunkbd->reset <= -1) { /* If cp[i] is 0xff, sunkbd->reset will stay -1. */
  88. sunkbd->reset = data; /* The keyboard sends 0xff 0xff 0xID on powerup */
  89. wake_up_interruptible(&sunkbd->wait);
  90. goto out;
  91. }
  92. if (sunkbd->layout == -1) {
  93. sunkbd->layout = data;
  94. wake_up_interruptible(&sunkbd->wait);
  95. goto out;
  96. }
  97. switch (data) {
  98. case SUNKBD_RET_RESET:
  99. schedule_work(&sunkbd->tq);
  100. sunkbd->reset = -1;
  101. break;
  102. case SUNKBD_RET_LAYOUT:
  103. sunkbd->layout = -1;
  104. break;
  105. case SUNKBD_RET_ALLUP: /* All keys released */
  106. break;
  107. default:
  108. if (!sunkbd->enabled)
  109. break;
  110. if (sunkbd->keycode[data & SUNKBD_KEY]) {
  111. input_regs(sunkbd->dev, regs);
  112. input_report_key(sunkbd->dev, sunkbd->keycode[data & SUNKBD_KEY], !(data & SUNKBD_RELEASE));
  113. input_sync(sunkbd->dev);
  114. } else {
  115. printk(KERN_WARNING "sunkbd.c: Unknown key (scancode %#x) %s.\n",
  116. data & SUNKBD_KEY, data & SUNKBD_RELEASE ? "released" : "pressed");
  117. }
  118. }
  119. out:
  120. return IRQ_HANDLED;
  121. }
  122. /*
  123. * sunkbd_event() handles events from the input module.
  124. */
  125. static int sunkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  126. {
  127. struct sunkbd *sunkbd = dev->private;
  128. switch (type) {
  129. case EV_LED:
  130. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_SETLED);
  131. sunkbd->serio->write(sunkbd->serio,
  132. (!!test_bit(LED_CAPSL, dev->led) << 3) | (!!test_bit(LED_SCROLLL, dev->led) << 2) |
  133. (!!test_bit(LED_COMPOSE, dev->led) << 1) | !!test_bit(LED_NUML, dev->led));
  134. return 0;
  135. case EV_SND:
  136. switch (code) {
  137. case SND_CLICK:
  138. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_NOCLICK - value);
  139. return 0;
  140. case SND_BELL:
  141. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_BELLOFF - value);
  142. return 0;
  143. }
  144. break;
  145. }
  146. return -1;
  147. }
  148. /*
  149. * sunkbd_initialize() checks for a Sun keyboard attached, and determines
  150. * its type.
  151. */
  152. static int sunkbd_initialize(struct sunkbd *sunkbd)
  153. {
  154. sunkbd->reset = -2;
  155. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_RESET);
  156. wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
  157. if (sunkbd->reset < 0)
  158. return -1;
  159. sunkbd->type = sunkbd->reset;
  160. if (sunkbd->type == 4) { /* Type 4 keyboard */
  161. sunkbd->layout = -2;
  162. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_LAYOUT);
  163. wait_event_interruptible_timeout(sunkbd->wait, sunkbd->layout >= 0, HZ/4);
  164. if (sunkbd->layout < 0) return -1;
  165. if (sunkbd->layout & SUNKBD_LAYOUT_5_MASK) sunkbd->type = 5;
  166. }
  167. return 0;
  168. }
  169. /*
  170. * sunkbd_reinit() sets leds and beeps to a state the computer remembers they
  171. * were in.
  172. */
  173. static void sunkbd_reinit(void *data)
  174. {
  175. struct sunkbd *sunkbd = data;
  176. wait_event_interruptible_timeout(sunkbd->wait, sunkbd->reset >= 0, HZ);
  177. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_SETLED);
  178. sunkbd->serio->write(sunkbd->serio,
  179. (!!test_bit(LED_CAPSL, sunkbd->dev->led) << 3) | (!!test_bit(LED_SCROLLL, sunkbd->dev->led) << 2) |
  180. (!!test_bit(LED_COMPOSE, sunkbd->dev->led) << 1) | !!test_bit(LED_NUML, sunkbd->dev->led));
  181. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_NOCLICK - !!test_bit(SND_CLICK, sunkbd->dev->snd));
  182. sunkbd->serio->write(sunkbd->serio, SUNKBD_CMD_BELLOFF - !!test_bit(SND_BELL, sunkbd->dev->snd));
  183. }
  184. static void sunkbd_enable(struct sunkbd *sunkbd, int enable)
  185. {
  186. serio_pause_rx(sunkbd->serio);
  187. sunkbd->enabled = 1;
  188. serio_continue_rx(sunkbd->serio);
  189. }
  190. /*
  191. * sunkbd_connect() probes for a Sun keyboard and fills the necessary structures.
  192. */
  193. static int sunkbd_connect(struct serio *serio, struct serio_driver *drv)
  194. {
  195. struct sunkbd *sunkbd;
  196. struct input_dev *input_dev;
  197. int err = -ENOMEM;
  198. int i;
  199. sunkbd = kzalloc(sizeof(struct sunkbd), GFP_KERNEL);
  200. input_dev = input_allocate_device();
  201. if (!sunkbd || !input_dev)
  202. goto fail;
  203. sunkbd->serio = serio;
  204. sunkbd->dev = input_dev;
  205. init_waitqueue_head(&sunkbd->wait);
  206. INIT_WORK(&sunkbd->tq, sunkbd_reinit, sunkbd);
  207. snprintf(sunkbd->phys, sizeof(sunkbd->phys), "%s/input0", serio->phys);
  208. serio_set_drvdata(serio, sunkbd);
  209. err = serio_open(serio, drv);
  210. if (err)
  211. goto fail;
  212. if (sunkbd_initialize(sunkbd) < 0) {
  213. serio_close(serio);
  214. goto fail;
  215. }
  216. sprintf(sunkbd->name, "Sun Type %d keyboard", sunkbd->type);
  217. memcpy(sunkbd->keycode, sunkbd_keycode, sizeof(sunkbd->keycode));
  218. input_dev->name = sunkbd->name;
  219. input_dev->phys = sunkbd->phys;
  220. input_dev->id.bustype = BUS_RS232;
  221. input_dev->id.vendor = SERIO_SUNKBD;
  222. input_dev->id.product = sunkbd->type;
  223. input_dev->id.version = 0x0100;
  224. input_dev->cdev.dev = &serio->dev;
  225. input_dev->private = sunkbd;
  226. input_dev->event = sunkbd_event;
  227. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_SND) | BIT(EV_REP);
  228. input_dev->ledbit[0] = BIT(LED_CAPSL) | BIT(LED_COMPOSE) | BIT(LED_SCROLLL) | BIT(LED_NUML);
  229. input_dev->sndbit[0] = BIT(SND_CLICK) | BIT(SND_BELL);
  230. input_dev->keycode = sunkbd->keycode;
  231. input_dev->keycodesize = sizeof(unsigned char);
  232. input_dev->keycodemax = ARRAY_SIZE(sunkbd_keycode);
  233. for (i = 0; i < 128; i++)
  234. set_bit(sunkbd->keycode[i], input_dev->keybit);
  235. clear_bit(0, input_dev->keybit);
  236. sunkbd_enable(sunkbd, 1);
  237. input_register_device(sunkbd->dev);
  238. return 0;
  239. fail: serio_set_drvdata(serio, NULL);
  240. input_free_device(input_dev);
  241. kfree(sunkbd);
  242. return err;
  243. }
  244. /*
  245. * sunkbd_disconnect() unregisters and closes behind us.
  246. */
  247. static void sunkbd_disconnect(struct serio *serio)
  248. {
  249. struct sunkbd *sunkbd = serio_get_drvdata(serio);
  250. sunkbd_enable(sunkbd, 0);
  251. input_unregister_device(sunkbd->dev);
  252. serio_close(serio);
  253. serio_set_drvdata(serio, NULL);
  254. kfree(sunkbd);
  255. }
  256. static struct serio_device_id sunkbd_serio_ids[] = {
  257. {
  258. .type = SERIO_RS232,
  259. .proto = SERIO_SUNKBD,
  260. .id = SERIO_ANY,
  261. .extra = SERIO_ANY,
  262. },
  263. {
  264. .type = SERIO_RS232,
  265. .proto = SERIO_UNKNOWN, /* sunkbd does probe */
  266. .id = SERIO_ANY,
  267. .extra = SERIO_ANY,
  268. },
  269. { 0 }
  270. };
  271. MODULE_DEVICE_TABLE(serio, sunkbd_serio_ids);
  272. static struct serio_driver sunkbd_drv = {
  273. .driver = {
  274. .name = "sunkbd",
  275. },
  276. .description = DRIVER_DESC,
  277. .id_table = sunkbd_serio_ids,
  278. .interrupt = sunkbd_interrupt,
  279. .connect = sunkbd_connect,
  280. .disconnect = sunkbd_disconnect,
  281. };
  282. /*
  283. * The functions for insering/removing us as a module.
  284. */
  285. static int __init sunkbd_init(void)
  286. {
  287. serio_register_driver(&sunkbd_drv);
  288. return 0;
  289. }
  290. static void __exit sunkbd_exit(void)
  291. {
  292. serio_unregister_driver(&sunkbd_drv);
  293. }
  294. module_init(sunkbd_init);
  295. module_exit(sunkbd_exit);