newtonkbd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2000 Justin Cormack
  3. */
  4. /*
  5. * Newton 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. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <j.cormack@doc.ic.ac.uk>, or by paper mail:
  24. * Justin Cormack, 68 Dartmouth Park Road, London NW5 1SN, UK.
  25. */
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/init.h>
  30. #include <linux/serio.h>
  31. #define DRIVER_DESC "Newton keyboard driver"
  32. MODULE_AUTHOR("Justin Cormack <j.cormack@doc.ic.ac.uk>");
  33. MODULE_DESCRIPTION(DRIVER_DESC);
  34. MODULE_LICENSE("GPL");
  35. #define NKBD_KEY 0x7f
  36. #define NKBD_PRESS 0x80
  37. static unsigned char nkbd_keycode[128] = {
  38. KEY_A, KEY_S, KEY_D, KEY_F, KEY_H, KEY_G, KEY_Z, KEY_X,
  39. KEY_C, KEY_V, 0, KEY_B, KEY_Q, KEY_W, KEY_E, KEY_R,
  40. KEY_Y, KEY_T, KEY_1, KEY_2, KEY_3, KEY_4, KEY_6, KEY_5,
  41. KEY_EQUAL, KEY_9, KEY_7, KEY_MINUS, KEY_8, KEY_0, KEY_RIGHTBRACE, KEY_O,
  42. KEY_U, KEY_LEFTBRACE, KEY_I, KEY_P, KEY_ENTER, KEY_L, KEY_J, KEY_APOSTROPHE,
  43. KEY_K, KEY_SEMICOLON, KEY_BACKSLASH, KEY_COMMA, KEY_SLASH, KEY_N, KEY_M, KEY_DOT,
  44. KEY_TAB, KEY_SPACE, KEY_GRAVE, KEY_DELETE, 0, 0, 0, KEY_LEFTMETA,
  45. KEY_LEFTSHIFT, KEY_CAPSLOCK, KEY_LEFTALT, KEY_LEFTCTRL, KEY_RIGHTSHIFT, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. KEY_LEFT, KEY_RIGHT, KEY_DOWN, KEY_UP, 0
  51. };
  52. static char *nkbd_name = "Newton Keyboard";
  53. struct nkbd {
  54. unsigned char keycode[128];
  55. struct input_dev dev;
  56. struct serio *serio;
  57. char phys[32];
  58. };
  59. static irqreturn_t nkbd_interrupt(struct serio *serio,
  60. unsigned char data, unsigned int flags, struct pt_regs *regs)
  61. {
  62. struct nkbd *nkbd = serio_get_drvdata(serio);
  63. /* invalid scan codes are probably the init sequence, so we ignore them */
  64. if (nkbd->keycode[data & NKBD_KEY]) {
  65. input_regs(&nkbd->dev, regs);
  66. input_report_key(&nkbd->dev, nkbd->keycode[data & NKBD_KEY], data & NKBD_PRESS);
  67. input_sync(&nkbd->dev);
  68. }
  69. else if (data == 0xe7) /* end of init sequence */
  70. printk(KERN_INFO "input: %s on %s\n", nkbd_name, serio->phys);
  71. return IRQ_HANDLED;
  72. }
  73. static int nkbd_connect(struct serio *serio, struct serio_driver *drv)
  74. {
  75. struct nkbd *nkbd;
  76. int i;
  77. int err;
  78. if (!(nkbd = kmalloc(sizeof(struct nkbd), GFP_KERNEL)))
  79. return -ENOMEM;
  80. memset(nkbd, 0, sizeof(struct nkbd));
  81. nkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  82. nkbd->serio = serio;
  83. init_input_dev(&nkbd->dev);
  84. nkbd->dev.keycode = nkbd->keycode;
  85. nkbd->dev.keycodesize = sizeof(unsigned char);
  86. nkbd->dev.keycodemax = ARRAY_SIZE(nkbd_keycode);
  87. nkbd->dev.private = nkbd;
  88. serio_set_drvdata(serio, nkbd);
  89. err = serio_open(serio, drv);
  90. if (err) {
  91. serio_set_drvdata(serio, NULL);
  92. kfree(nkbd);
  93. return err;
  94. }
  95. memcpy(nkbd->keycode, nkbd_keycode, sizeof(nkbd->keycode));
  96. for (i = 0; i < 128; i++)
  97. set_bit(nkbd->keycode[i], nkbd->dev.keybit);
  98. clear_bit(0, nkbd->dev.keybit);
  99. sprintf(nkbd->phys, "%s/input0", serio->phys);
  100. nkbd->dev.name = nkbd_name;
  101. nkbd->dev.phys = nkbd->phys;
  102. nkbd->dev.id.bustype = BUS_RS232;
  103. nkbd->dev.id.vendor = SERIO_NEWTON;
  104. nkbd->dev.id.product = 0x0001;
  105. nkbd->dev.id.version = 0x0100;
  106. nkbd->dev.dev = &serio->dev;
  107. input_register_device(&nkbd->dev);
  108. printk(KERN_INFO "input: %s on %s\n", nkbd_name, serio->phys);
  109. return 0;
  110. }
  111. static void nkbd_disconnect(struct serio *serio)
  112. {
  113. struct nkbd *nkbd = serio_get_drvdata(serio);
  114. input_unregister_device(&nkbd->dev);
  115. serio_close(serio);
  116. serio_set_drvdata(serio, NULL);
  117. kfree(nkbd);
  118. }
  119. static struct serio_device_id nkbd_serio_ids[] = {
  120. {
  121. .type = SERIO_RS232,
  122. .proto = SERIO_NEWTON,
  123. .id = SERIO_ANY,
  124. .extra = SERIO_ANY,
  125. },
  126. { 0 }
  127. };
  128. MODULE_DEVICE_TABLE(serio, nkbd_serio_ids);
  129. static struct serio_driver nkbd_drv = {
  130. .driver = {
  131. .name = "newtonkbd",
  132. },
  133. .description = DRIVER_DESC,
  134. .id_table = nkbd_serio_ids,
  135. .interrupt = nkbd_interrupt,
  136. .connect = nkbd_connect,
  137. .disconnect = nkbd_disconnect,
  138. };
  139. static int __init nkbd_init(void)
  140. {
  141. serio_register_driver(&nkbd_drv);
  142. return 0;
  143. }
  144. static void __exit nkbd_exit(void)
  145. {
  146. serio_unregister_driver(&nkbd_drv);
  147. }
  148. module_init(nkbd_init);
  149. module_exit(nkbd_exit);