sunkbd.c 8.7 KB

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