joydump.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1996-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * This is just a very simple driver that can dump the data
  7. * out of the joystick port into the syslog ...
  8. */
  9. /*
  10. */
  11. #include <linux/module.h>
  12. #include <linux/gameport.h>
  13. #include <linux/kernel.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #define DRIVER_DESC "Gameport data dumper module"
  17. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  18. MODULE_DESCRIPTION(DRIVER_DESC);
  19. MODULE_LICENSE("GPL");
  20. #define BUF_SIZE 256
  21. struct joydump {
  22. unsigned int time;
  23. unsigned char data;
  24. };
  25. static int joydump_connect(struct gameport *gameport, struct gameport_driver *drv)
  26. {
  27. struct joydump *buf; /* all entries */
  28. struct joydump *dump, *prev; /* one entry each */
  29. int axes[4], buttons;
  30. int i, j, t, timeout;
  31. unsigned long flags;
  32. unsigned char u;
  33. printk(KERN_INFO "joydump: ,------------------ START ----------------.\n");
  34. printk(KERN_INFO "joydump: | Dumping: %30s |\n", gameport->phys);
  35. printk(KERN_INFO "joydump: | Speed: %28d kHz |\n", gameport->speed);
  36. if (gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
  37. printk(KERN_INFO "joydump: | Raw mode not available - trying cooked. |\n");
  38. if (gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
  39. printk(KERN_INFO "joydump: | Cooked not available either. Failing. |\n");
  40. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  41. return -ENODEV;
  42. }
  43. gameport_cooked_read(gameport, axes, &buttons);
  44. for (i = 0; i < 4; i++)
  45. printk(KERN_INFO "joydump: | Axis %d: %4d. |\n", i, axes[i]);
  46. printk(KERN_INFO "joydump: | Buttons %02x. |\n", buttons);
  47. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  48. }
  49. timeout = gameport_time(gameport, 10000); /* 10 ms */
  50. buf = kmalloc_array(BUF_SIZE, sizeof(struct joydump), GFP_KERNEL);
  51. if (!buf) {
  52. printk(KERN_INFO "joydump: no memory for testing\n");
  53. goto jd_end;
  54. }
  55. dump = buf;
  56. t = 0;
  57. i = 1;
  58. local_irq_save(flags);
  59. u = gameport_read(gameport);
  60. dump->data = u;
  61. dump->time = t;
  62. dump++;
  63. gameport_trigger(gameport);
  64. while (i < BUF_SIZE && t < timeout) {
  65. dump->data = gameport_read(gameport);
  66. if (dump->data ^ u) {
  67. u = dump->data;
  68. dump->time = t;
  69. i++;
  70. dump++;
  71. }
  72. t++;
  73. }
  74. local_irq_restore(flags);
  75. /*
  76. * Dump data.
  77. */
  78. t = i;
  79. dump = buf;
  80. prev = dump;
  81. printk(KERN_INFO "joydump: >------------------ DATA -----------------<\n");
  82. printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ", 0, 0);
  83. for (j = 7; j >= 0; j--)
  84. printk("%d", (dump->data >> j) & 1);
  85. printk(" |\n");
  86. dump++;
  87. for (i = 1; i < t; i++, dump++, prev++) {
  88. printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ",
  89. i, dump->time - prev->time);
  90. for (j = 7; j >= 0; j--)
  91. printk("%d", (dump->data >> j) & 1);
  92. printk(" |\n");
  93. }
  94. kfree(buf);
  95. jd_end:
  96. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  97. return 0;
  98. }
  99. static void joydump_disconnect(struct gameport *gameport)
  100. {
  101. gameport_close(gameport);
  102. }
  103. static struct gameport_driver joydump_drv = {
  104. .driver = {
  105. .name = "joydump",
  106. },
  107. .description = DRIVER_DESC,
  108. .connect = joydump_connect,
  109. .disconnect = joydump_disconnect,
  110. };
  111. module_gameport_driver(joydump_drv);