newtonkbd.c 4.5 KB

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