newtonkbd.c 4.9 KB

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