newtonkbd.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/serio.h>
  30. #define DRIVER_DESC "Newton keyboard driver"
  31. MODULE_AUTHOR("Justin Cormack <j.cormack@doc.ic.ac.uk>");
  32. MODULE_DESCRIPTION(DRIVER_DESC);
  33. MODULE_LICENSE("GPL");
  34. #define NKBD_KEY 0x7f
  35. #define NKBD_PRESS 0x80
  36. static unsigned char nkbd_keycode[128] = {
  37. KEY_A, KEY_S, KEY_D, KEY_F, KEY_H, KEY_G, KEY_Z, KEY_X,
  38. KEY_C, KEY_V, 0, KEY_B, KEY_Q, KEY_W, KEY_E, KEY_R,
  39. KEY_Y, KEY_T, KEY_1, KEY_2, KEY_3, KEY_4, KEY_6, KEY_5,
  40. KEY_EQUAL, KEY_9, KEY_7, KEY_MINUS, KEY_8, KEY_0, KEY_RIGHTBRACE, KEY_O,
  41. KEY_U, KEY_LEFTBRACE, KEY_I, KEY_P, KEY_ENTER, KEY_L, KEY_J, KEY_APOSTROPHE,
  42. KEY_K, KEY_SEMICOLON, KEY_BACKSLASH, KEY_COMMA, KEY_SLASH, KEY_N, KEY_M, KEY_DOT,
  43. KEY_TAB, KEY_SPACE, KEY_GRAVE, KEY_DELETE, 0, 0, 0, KEY_LEFTMETA,
  44. KEY_LEFTSHIFT, KEY_CAPSLOCK, KEY_LEFTALT, KEY_LEFTCTRL, KEY_RIGHTSHIFT, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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,
  49. KEY_LEFT, KEY_RIGHT, KEY_DOWN, KEY_UP, 0
  50. };
  51. struct nkbd {
  52. unsigned char keycode[128];
  53. struct input_dev *dev;
  54. struct serio *serio;
  55. char phys[32];
  56. };
  57. static irqreturn_t nkbd_interrupt(struct serio *serio,
  58. unsigned char data, unsigned int flags)
  59. {
  60. struct nkbd *nkbd = serio_get_drvdata(serio);
  61. /* invalid scan codes are probably the init sequence, so we ignore them */
  62. if (nkbd->keycode[data & NKBD_KEY]) {
  63. input_report_key(nkbd->dev, nkbd->keycode[data & NKBD_KEY], data & NKBD_PRESS);
  64. input_sync(nkbd->dev);
  65. }
  66. else if (data == 0xe7) /* end of init sequence */
  67. printk(KERN_INFO "input: %s on %s\n", nkbd->dev->name, serio->phys);
  68. return IRQ_HANDLED;
  69. }
  70. static int nkbd_connect(struct serio *serio, struct serio_driver *drv)
  71. {
  72. struct nkbd *nkbd;
  73. struct input_dev *input_dev;
  74. int err = -ENOMEM;
  75. int i;
  76. nkbd = kzalloc(sizeof(struct nkbd), GFP_KERNEL);
  77. input_dev = input_allocate_device();
  78. if (!nkbd || !input_dev)
  79. goto fail1;
  80. nkbd->serio = serio;
  81. nkbd->dev = input_dev;
  82. snprintf(nkbd->phys, sizeof(nkbd->phys), "%s/input0", serio->phys);
  83. memcpy(nkbd->keycode, nkbd_keycode, sizeof(nkbd->keycode));
  84. input_dev->name = "Newton Keyboard";
  85. input_dev->phys = nkbd->phys;
  86. input_dev->id.bustype = BUS_RS232;
  87. input_dev->id.vendor = SERIO_NEWTON;
  88. input_dev->id.product = 0x0001;
  89. input_dev->id.version = 0x0100;
  90. input_dev->dev.parent = &serio->dev;
  91. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  92. input_dev->keycode = nkbd->keycode;
  93. input_dev->keycodesize = sizeof(unsigned char);
  94. input_dev->keycodemax = ARRAY_SIZE(nkbd_keycode);
  95. for (i = 0; i < 128; i++)
  96. set_bit(nkbd->keycode[i], input_dev->keybit);
  97. clear_bit(0, input_dev->keybit);
  98. serio_set_drvdata(serio, nkbd);
  99. err = serio_open(serio, drv);
  100. if (err)
  101. goto fail2;
  102. err = input_register_device(nkbd->dev);
  103. if (err)
  104. goto fail3;
  105. return 0;
  106. fail3: serio_close(serio);
  107. fail2: serio_set_drvdata(serio, NULL);
  108. fail1: 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. module_serio_driver(nkbd_drv);