warrior.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1999-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * Logitech WingMan Warrior joystick driver for Linux
  7. */
  8. /*
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/input.h>
  14. #include <linux/serio.h>
  15. #define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
  16. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  17. MODULE_DESCRIPTION(DRIVER_DESC);
  18. MODULE_LICENSE("GPL");
  19. /*
  20. * Constants.
  21. */
  22. #define WARRIOR_MAX_LENGTH 16
  23. static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
  24. /*
  25. * Per-Warrior data.
  26. */
  27. struct warrior {
  28. struct input_dev *dev;
  29. int idx, len;
  30. unsigned char data[WARRIOR_MAX_LENGTH];
  31. char phys[32];
  32. };
  33. /*
  34. * warrior_process_packet() decodes packets the driver receives from the
  35. * Warrior. It updates the data accordingly.
  36. */
  37. static void warrior_process_packet(struct warrior *warrior)
  38. {
  39. struct input_dev *dev = warrior->dev;
  40. unsigned char *data = warrior->data;
  41. if (!warrior->idx) return;
  42. switch ((data[0] >> 4) & 7) {
  43. case 1: /* Button data */
  44. input_report_key(dev, BTN_TRIGGER, data[3] & 1);
  45. input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
  46. input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
  47. input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
  48. break;
  49. case 3: /* XY-axis info->data */
  50. input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
  51. input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  52. break;
  53. case 5: /* Throttle, spinner, hat info->data */
  54. input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  55. input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
  56. input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
  57. input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
  58. break;
  59. }
  60. input_sync(dev);
  61. }
  62. /*
  63. * warrior_interrupt() is called by the low level driver when characters
  64. * are ready for us. We then buffer them for further processing, or call the
  65. * packet processing routine.
  66. */
  67. static irqreturn_t warrior_interrupt(struct serio *serio,
  68. unsigned char data, unsigned int flags)
  69. {
  70. struct warrior *warrior = serio_get_drvdata(serio);
  71. if (data & 0x80) {
  72. if (warrior->idx) warrior_process_packet(warrior);
  73. warrior->idx = 0;
  74. warrior->len = warrior_lengths[(data >> 4) & 7];
  75. }
  76. if (warrior->idx < warrior->len)
  77. warrior->data[warrior->idx++] = data;
  78. if (warrior->idx == warrior->len) {
  79. if (warrior->idx) warrior_process_packet(warrior);
  80. warrior->idx = 0;
  81. warrior->len = 0;
  82. }
  83. return IRQ_HANDLED;
  84. }
  85. /*
  86. * warrior_disconnect() is the opposite of warrior_connect()
  87. */
  88. static void warrior_disconnect(struct serio *serio)
  89. {
  90. struct warrior *warrior = serio_get_drvdata(serio);
  91. serio_close(serio);
  92. serio_set_drvdata(serio, NULL);
  93. input_unregister_device(warrior->dev);
  94. kfree(warrior);
  95. }
  96. /*
  97. * warrior_connect() is the routine that is called when someone adds a
  98. * new serio device. It looks for the Warrior, and if found, registers
  99. * it as an input device.
  100. */
  101. static int warrior_connect(struct serio *serio, struct serio_driver *drv)
  102. {
  103. struct warrior *warrior;
  104. struct input_dev *input_dev;
  105. int err = -ENOMEM;
  106. warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
  107. input_dev = input_allocate_device();
  108. if (!warrior || !input_dev)
  109. goto fail1;
  110. warrior->dev = input_dev;
  111. snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
  112. input_dev->name = "Logitech WingMan Warrior";
  113. input_dev->phys = warrior->phys;
  114. input_dev->id.bustype = BUS_RS232;
  115. input_dev->id.vendor = SERIO_WARRIOR;
  116. input_dev->id.product = 0x0001;
  117. input_dev->id.version = 0x0100;
  118. input_dev->dev.parent = &serio->dev;
  119. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
  120. BIT_MASK(EV_ABS);
  121. input_dev->keybit[BIT_WORD(BTN_TRIGGER)] = BIT_MASK(BTN_TRIGGER) |
  122. BIT_MASK(BTN_THUMB) | BIT_MASK(BTN_TOP) | BIT_MASK(BTN_TOP2);
  123. input_dev->relbit[0] = BIT_MASK(REL_DIAL);
  124. input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
  125. input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
  126. input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
  127. input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
  128. input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
  129. serio_set_drvdata(serio, warrior);
  130. err = serio_open(serio, drv);
  131. if (err)
  132. goto fail2;
  133. err = input_register_device(warrior->dev);
  134. if (err)
  135. goto fail3;
  136. return 0;
  137. fail3: serio_close(serio);
  138. fail2: serio_set_drvdata(serio, NULL);
  139. fail1: input_free_device(input_dev);
  140. kfree(warrior);
  141. return err;
  142. }
  143. /*
  144. * The serio driver structure.
  145. */
  146. static const struct serio_device_id warrior_serio_ids[] = {
  147. {
  148. .type = SERIO_RS232,
  149. .proto = SERIO_WARRIOR,
  150. .id = SERIO_ANY,
  151. .extra = SERIO_ANY,
  152. },
  153. { 0 }
  154. };
  155. MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
  156. static struct serio_driver warrior_drv = {
  157. .driver = {
  158. .name = "warrior",
  159. },
  160. .description = DRIVER_DESC,
  161. .id_table = warrior_serio_ids,
  162. .interrupt = warrior_interrupt,
  163. .connect = warrior_connect,
  164. .disconnect = warrior_disconnect,
  165. };
  166. module_serio_driver(warrior_drv);