inexio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * iNexio serial touchscreen driver
  3. *
  4. * Copyright (c) 2008 Richard Lemon
  5. * Based on the mtouch driver (c) Vojtech Pavlik and Dan Streetman
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. */
  13. /*
  14. * 2008/06/19 Richard Lemon <richard@codelemon.com>
  15. * Copied mtouch.c and edited for iNexio protocol
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/input.h>
  22. #include <linux/serio.h>
  23. #define DRIVER_DESC "iNexio serial touchscreen driver"
  24. MODULE_AUTHOR("Richard Lemon <richard@codelemon.com>");
  25. MODULE_DESCRIPTION(DRIVER_DESC);
  26. MODULE_LICENSE("GPL");
  27. /*
  28. * Definitions & global arrays.
  29. */
  30. #define INEXIO_FORMAT_TOUCH_BIT 0x01
  31. #define INEXIO_FORMAT_LENGTH 5
  32. #define INEXIO_RESPONSE_BEGIN_BYTE 0x80
  33. /* todo: check specs for max length of all responses */
  34. #define INEXIO_MAX_LENGTH 16
  35. #define INEXIO_MIN_XC 0
  36. #define INEXIO_MAX_XC 0x3fff
  37. #define INEXIO_MIN_YC 0
  38. #define INEXIO_MAX_YC 0x3fff
  39. #define INEXIO_GET_XC(data) (((data[1])<<7) | data[2])
  40. #define INEXIO_GET_YC(data) (((data[3])<<7) | data[4])
  41. #define INEXIO_GET_TOUCHED(data) (INEXIO_FORMAT_TOUCH_BIT & data[0])
  42. /*
  43. * Per-touchscreen data.
  44. */
  45. struct inexio {
  46. struct input_dev *dev;
  47. struct serio *serio;
  48. int idx;
  49. unsigned char data[INEXIO_MAX_LENGTH];
  50. char phys[32];
  51. };
  52. static void inexio_process_data(struct inexio *pinexio)
  53. {
  54. struct input_dev *dev = pinexio->dev;
  55. if (INEXIO_FORMAT_LENGTH == ++pinexio->idx) {
  56. input_report_abs(dev, ABS_X, INEXIO_GET_XC(pinexio->data));
  57. input_report_abs(dev, ABS_Y, INEXIO_GET_YC(pinexio->data));
  58. input_report_key(dev, BTN_TOUCH, INEXIO_GET_TOUCHED(pinexio->data));
  59. input_sync(dev);
  60. pinexio->idx = 0;
  61. }
  62. }
  63. static irqreturn_t inexio_interrupt(struct serio *serio,
  64. unsigned char data, unsigned int flags)
  65. {
  66. struct inexio* pinexio = serio_get_drvdata(serio);
  67. pinexio->data[pinexio->idx] = data;
  68. if (INEXIO_RESPONSE_BEGIN_BYTE&pinexio->data[0])
  69. inexio_process_data(pinexio);
  70. else
  71. printk(KERN_DEBUG "inexio.c: unknown/unsynchronized data from device, byte %x\n",pinexio->data[0]);
  72. return IRQ_HANDLED;
  73. }
  74. /*
  75. * inexio_disconnect() is the opposite of inexio_connect()
  76. */
  77. static void inexio_disconnect(struct serio *serio)
  78. {
  79. struct inexio* pinexio = serio_get_drvdata(serio);
  80. input_get_device(pinexio->dev);
  81. input_unregister_device(pinexio->dev);
  82. serio_close(serio);
  83. serio_set_drvdata(serio, NULL);
  84. input_put_device(pinexio->dev);
  85. kfree(pinexio);
  86. }
  87. /*
  88. * inexio_connect() is the routine that is called when someone adds a
  89. * new serio device that supports iNexio protocol and registers it as
  90. * an input device. This is usually accomplished using inputattach.
  91. */
  92. static int inexio_connect(struct serio *serio, struct serio_driver *drv)
  93. {
  94. struct inexio *pinexio;
  95. struct input_dev *input_dev;
  96. int err;
  97. pinexio = kzalloc(sizeof(struct inexio), GFP_KERNEL);
  98. input_dev = input_allocate_device();
  99. if (!pinexio || !input_dev) {
  100. err = -ENOMEM;
  101. goto fail1;
  102. }
  103. pinexio->serio = serio;
  104. pinexio->dev = input_dev;
  105. snprintf(pinexio->phys, sizeof(pinexio->phys), "%s/input0", serio->phys);
  106. input_dev->name = "iNexio Serial TouchScreen";
  107. input_dev->phys = pinexio->phys;
  108. input_dev->id.bustype = BUS_RS232;
  109. input_dev->id.vendor = SERIO_INEXIO;
  110. input_dev->id.product = 0;
  111. input_dev->id.version = 0x0001;
  112. input_dev->dev.parent = &serio->dev;
  113. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  114. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  115. input_set_abs_params(pinexio->dev, ABS_X, INEXIO_MIN_XC, INEXIO_MAX_XC, 0, 0);
  116. input_set_abs_params(pinexio->dev, ABS_Y, INEXIO_MIN_YC, INEXIO_MAX_YC, 0, 0);
  117. serio_set_drvdata(serio, pinexio);
  118. err = serio_open(serio, drv);
  119. if (err)
  120. goto fail2;
  121. err = input_register_device(pinexio->dev);
  122. if (err)
  123. goto fail3;
  124. return 0;
  125. fail3: serio_close(serio);
  126. fail2: serio_set_drvdata(serio, NULL);
  127. fail1: input_free_device(input_dev);
  128. kfree(pinexio);
  129. return err;
  130. }
  131. /*
  132. * The serio driver structure.
  133. */
  134. static struct serio_device_id inexio_serio_ids[] = {
  135. {
  136. .type = SERIO_RS232,
  137. .proto = SERIO_INEXIO,
  138. .id = SERIO_ANY,
  139. .extra = SERIO_ANY,
  140. },
  141. { 0 }
  142. };
  143. MODULE_DEVICE_TABLE(serio, inexio_serio_ids);
  144. static struct serio_driver inexio_drv = {
  145. .driver = {
  146. .name = "inexio",
  147. },
  148. .description = DRIVER_DESC,
  149. .id_table = inexio_serio_ids,
  150. .interrupt = inexio_interrupt,
  151. .connect = inexio_connect,
  152. .disconnect = inexio_disconnect,
  153. };
  154. module_serio_driver(inexio_drv);