newtonkbd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. struct nkbd {
  53. unsigned char keycode[128];
  54. struct input_dev *dev;
  55. struct serio *serio;
  56. char phys[32];
  57. };
  58. static irqreturn_t nkbd_interrupt(struct serio *serio,
  59. unsigned char data, unsigned int flags, struct pt_regs *regs)
  60. {
  61. struct nkbd *nkbd = serio_get_drvdata(serio);
  62. /* invalid scan codes are probably the init sequence, so we ignore them */
  63. if (nkbd->keycode[data & NKBD_KEY]) {
  64. input_regs(nkbd->dev, regs);
  65. input_report_key(nkbd->dev, nkbd->keycode[data & NKBD_KEY], data & NKBD_PRESS);
  66. input_sync(nkbd->dev);
  67. }
  68. else if (data == 0xe7) /* end of init sequence */
  69. printk(KERN_INFO "input: %s on %s\n", nkbd->dev->name, serio->phys);
  70. return IRQ_HANDLED;
  71. }
  72. static int nkbd_connect(struct serio *serio, struct serio_driver *drv)
  73. {
  74. struct nkbd *nkbd;
  75. struct input_dev *input_dev;
  76. int err = -ENOMEM;
  77. int i;
  78. nkbd = kzalloc(sizeof(struct nkbd), GFP_KERNEL);
  79. input_dev = input_allocate_device();
  80. if (!nkbd || !input_dev)
  81. goto fail;
  82. nkbd->serio = serio;
  83. nkbd->dev = input_dev;
  84. sprintf(nkbd->phys, "%s/input0", serio->phys);
  85. memcpy(nkbd->keycode, nkbd_keycode, sizeof(nkbd->keycode));
  86. input_dev->name = "Newton Keyboard";
  87. input_dev->phys = nkbd->phys;
  88. input_dev->id.bustype = BUS_RS232;
  89. input_dev->id.vendor = SERIO_NEWTON;
  90. input_dev->id.product = 0x0001;
  91. input_dev->id.version = 0x0100;
  92. input_dev->cdev.dev = &serio->dev;
  93. input_dev->private = nkbd;
  94. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  95. input_dev->keycode = nkbd->keycode;
  96. input_dev->keycodesize = sizeof(unsigned char);
  97. input_dev->keycodemax = ARRAY_SIZE(nkbd_keycode);
  98. for (i = 0; i < 128; i++)
  99. set_bit(nkbd->keycode[i], input_dev->keybit);
  100. clear_bit(0, input_dev->keybit);
  101. serio_set_drvdata(serio, nkbd);
  102. err = serio_open(serio, drv);
  103. if (err)
  104. goto fail;
  105. input_register_device(nkbd->dev);
  106. return 0;
  107. fail: serio_set_drvdata(serio, NULL);
  108. input_free_device(input_dev);
  109. kfree(nkbd);
  110. return err;
  111. }
  112. static void nkbd_disconnect(struct serio *serio)
  113. {
  114. struct nkbd *nkbd = serio_get_drvdata(serio);
  115. serio_close(serio);
  116. serio_set_drvdata(serio, NULL);
  117. input_unregister_device(nkbd->dev);
  118. kfree(nkbd);
  119. }
  120. static struct serio_device_id nkbd_serio_ids[] = {
  121. {
  122. .type = SERIO_RS232,
  123. .proto = SERIO_NEWTON,
  124. .id = SERIO_ANY,
  125. .extra = SERIO_ANY,
  126. },
  127. { 0 }
  128. };
  129. MODULE_DEVICE_TABLE(serio, nkbd_serio_ids);
  130. static struct serio_driver nkbd_drv = {
  131. .driver = {
  132. .name = "newtonkbd",
  133. },
  134. .description = DRIVER_DESC,
  135. .id_table = nkbd_serio_ids,
  136. .interrupt = nkbd_interrupt,
  137. .connect = nkbd_connect,
  138. .disconnect = nkbd_disconnect,
  139. };
  140. static int __init nkbd_init(void)
  141. {
  142. serio_register_driver(&nkbd_drv);
  143. return 0;
  144. }
  145. static void __exit nkbd_exit(void)
  146. {
  147. serio_unregister_driver(&nkbd_drv);
  148. }
  149. module_init(nkbd_init);
  150. module_exit(nkbd_exit);