iforce-serio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * $Id: iforce-serio.c,v 1.4 2002/01/28 22:45:00 jdeneux Exp $
  3. *
  4. * Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@ucw.cz>
  5. * Copyright (c) 2001 Johann Deneux <deneux@ifrance.com>
  6. *
  7. * USB/RS232 I-Force joysticks and wheels.
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Should you need to contact me, the author, you can do so either by
  25. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  26. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  27. */
  28. #include "iforce.h"
  29. void iforce_serial_xmit(struct iforce *iforce)
  30. {
  31. unsigned char cs;
  32. int i;
  33. unsigned long flags;
  34. if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
  35. set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags);
  36. return;
  37. }
  38. spin_lock_irqsave(&iforce->xmit_lock, flags);
  39. again:
  40. if (iforce->xmit.head == iforce->xmit.tail) {
  41. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  42. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  43. return;
  44. }
  45. cs = 0x2b;
  46. serio_write(iforce->serio, 0x2b);
  47. serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
  48. cs ^= iforce->xmit.buf[iforce->xmit.tail];
  49. XMIT_INC(iforce->xmit.tail, 1);
  50. for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) {
  51. serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
  52. cs ^= iforce->xmit.buf[iforce->xmit.tail];
  53. XMIT_INC(iforce->xmit.tail, 1);
  54. }
  55. serio_write(iforce->serio, cs);
  56. if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags))
  57. goto again;
  58. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  59. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  60. }
  61. static void iforce_serio_write_wakeup(struct serio *serio)
  62. {
  63. struct iforce *iforce = serio_get_drvdata(serio);
  64. iforce_serial_xmit(iforce);
  65. }
  66. static irqreturn_t iforce_serio_irq(struct serio *serio,
  67. unsigned char data, unsigned int flags, struct pt_regs *regs)
  68. {
  69. struct iforce *iforce = serio_get_drvdata(serio);
  70. if (!iforce->pkt) {
  71. if (data == 0x2b)
  72. iforce->pkt = 1;
  73. goto out;
  74. }
  75. if (!iforce->id) {
  76. if (data > 3 && data != 0xff)
  77. iforce->pkt = 0;
  78. else
  79. iforce->id = data;
  80. goto out;
  81. }
  82. if (!iforce->len) {
  83. if (data > IFORCE_MAX_LENGTH) {
  84. iforce->pkt = 0;
  85. iforce->id = 0;
  86. } else {
  87. iforce->len = data;
  88. }
  89. goto out;
  90. }
  91. if (iforce->idx < iforce->len) {
  92. iforce->csum += iforce->data[iforce->idx++] = data;
  93. goto out;
  94. }
  95. if (iforce->idx == iforce->len) {
  96. iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data, regs);
  97. iforce->pkt = 0;
  98. iforce->id = 0;
  99. iforce->len = 0;
  100. iforce->idx = 0;
  101. iforce->csum = 0;
  102. }
  103. out:
  104. return IRQ_HANDLED;
  105. }
  106. static int iforce_serio_connect(struct serio *serio, struct serio_driver *drv)
  107. {
  108. struct iforce *iforce;
  109. int err;
  110. if (!(iforce = kmalloc(sizeof(struct iforce), GFP_KERNEL)))
  111. return -ENOMEM;
  112. memset(iforce, 0, sizeof(struct iforce));
  113. iforce->bus = IFORCE_232;
  114. iforce->serio = serio;
  115. serio_set_drvdata(serio, iforce);
  116. err = serio_open(serio, drv);
  117. if (err) {
  118. serio_set_drvdata(serio, NULL);
  119. kfree(iforce);
  120. return err;
  121. }
  122. if (iforce_init_device(iforce)) {
  123. serio_close(serio);
  124. serio_set_drvdata(serio, NULL);
  125. kfree(iforce);
  126. return -ENODEV;
  127. }
  128. return 0;
  129. }
  130. static void iforce_serio_disconnect(struct serio *serio)
  131. {
  132. struct iforce *iforce = serio_get_drvdata(serio);
  133. input_unregister_device(&iforce->dev);
  134. serio_close(serio);
  135. serio_set_drvdata(serio, NULL);
  136. kfree(iforce);
  137. }
  138. static struct serio_device_id iforce_serio_ids[] = {
  139. {
  140. .type = SERIO_RS232,
  141. .proto = SERIO_IFORCE,
  142. .id = SERIO_ANY,
  143. .extra = SERIO_ANY,
  144. },
  145. { 0 }
  146. };
  147. MODULE_DEVICE_TABLE(serio, iforce_serio_ids);
  148. struct serio_driver iforce_serio_drv = {
  149. .driver = {
  150. .name = "iforce",
  151. },
  152. .description = "RS232 I-Force joysticks and wheels driver",
  153. .id_table = iforce_serio_ids,
  154. .write_wakeup = iforce_serio_write_wakeup,
  155. .interrupt = iforce_serio_irq,
  156. .connect = iforce_serio_connect,
  157. .disconnect = iforce_serio_disconnect,
  158. };