joydump.c 3.2 KB

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