warrior.c 5.1 KB

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