sunkbd.c 8.8 KB

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