iforce-serio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@ucw.cz>
  3. * Copyright (c) 2001, 2007 Johann Deneux <johann.deneux@gmail.com>
  4. *
  5. * USB/RS232 I-Force joysticks and wheels.
  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 <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include "iforce.h"
  27. void iforce_serial_xmit(struct iforce *iforce)
  28. {
  29. unsigned char cs;
  30. int i;
  31. unsigned long flags;
  32. if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
  33. set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags);
  34. return;
  35. }
  36. spin_lock_irqsave(&iforce->xmit_lock, flags);
  37. again:
  38. if (iforce->xmit.head == iforce->xmit.tail) {
  39. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  40. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  41. return;
  42. }
  43. cs = 0x2b;
  44. serio_write(iforce->serio, 0x2b);
  45. serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
  46. cs ^= iforce->xmit.buf[iforce->xmit.tail];
  47. XMIT_INC(iforce->xmit.tail, 1);
  48. for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) {
  49. serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
  50. cs ^= iforce->xmit.buf[iforce->xmit.tail];
  51. XMIT_INC(iforce->xmit.tail, 1);
  52. }
  53. serio_write(iforce->serio, cs);
  54. if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags))
  55. goto again;
  56. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  57. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  58. }
  59. static void iforce_serio_write_wakeup(struct serio *serio)
  60. {
  61. struct iforce *iforce = serio_get_drvdata(serio);
  62. iforce_serial_xmit(iforce);
  63. }
  64. static irqreturn_t iforce_serio_irq(struct serio *serio,
  65. unsigned char data, unsigned int flags)
  66. {
  67. struct iforce *iforce = serio_get_drvdata(serio);
  68. if (!iforce->pkt) {
  69. if (data == 0x2b)
  70. iforce->pkt = 1;
  71. goto out;
  72. }
  73. if (!iforce->id) {
  74. if (data > 3 && data != 0xff)
  75. iforce->pkt = 0;
  76. else
  77. iforce->id = data;
  78. goto out;
  79. }
  80. if (!iforce->len) {
  81. if (data > IFORCE_MAX_LENGTH) {
  82. iforce->pkt = 0;
  83. iforce->id = 0;
  84. } else {
  85. iforce->len = data;
  86. }
  87. goto out;
  88. }
  89. if (iforce->idx < iforce->len) {
  90. iforce->csum += iforce->data[iforce->idx++] = data;
  91. goto out;
  92. }
  93. if (iforce->idx == iforce->len) {
  94. iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data);
  95. iforce->pkt = 0;
  96. iforce->id = 0;
  97. iforce->len = 0;
  98. iforce->idx = 0;
  99. iforce->csum = 0;
  100. }
  101. out:
  102. return IRQ_HANDLED;
  103. }
  104. static int iforce_serio_connect(struct serio *serio, struct serio_driver *drv)
  105. {
  106. struct iforce *iforce;
  107. int err;
  108. iforce = kzalloc(sizeof(struct iforce), GFP_KERNEL);
  109. if (!iforce)
  110. return -ENOMEM;
  111. iforce->bus = IFORCE_232;
  112. iforce->serio = serio;
  113. serio_set_drvdata(serio, iforce);
  114. err = serio_open(serio, drv);
  115. if (err)
  116. goto fail1;
  117. err = iforce_init_device(iforce);
  118. if (err)
  119. goto fail2;
  120. return 0;
  121. fail2: serio_close(serio);
  122. fail1: serio_set_drvdata(serio, NULL);
  123. kfree(iforce);
  124. return err;
  125. }
  126. static void iforce_serio_disconnect(struct serio *serio)
  127. {
  128. struct iforce *iforce = serio_get_drvdata(serio);
  129. input_unregister_device(iforce->dev);
  130. serio_close(serio);
  131. serio_set_drvdata(serio, NULL);
  132. kfree(iforce);
  133. }
  134. static struct serio_device_id iforce_serio_ids[] = {
  135. {
  136. .type = SERIO_RS232,
  137. .proto = SERIO_IFORCE,
  138. .id = SERIO_ANY,
  139. .extra = SERIO_ANY,
  140. },
  141. { 0 }
  142. };
  143. MODULE_DEVICE_TABLE(serio, iforce_serio_ids);
  144. struct serio_driver iforce_serio_drv = {
  145. .driver = {
  146. .name = "iforce",
  147. },
  148. .description = "RS232 I-Force joysticks and wheels driver",
  149. .id_table = iforce_serio_ids,
  150. .write_wakeup = iforce_serio_write_wakeup,
  151. .interrupt = iforce_serio_irq,
  152. .connect = iforce_serio_connect,
  153. .disconnect = iforce_serio_disconnect,
  154. };