joydump.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 1996-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * This is just a very simple driver that can dump the data
  6. * out of the joystick port into the syslog ...
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/gameport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #define DRIVER_DESC "Gameport data dumper module"
  29. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  30. MODULE_DESCRIPTION(DRIVER_DESC);
  31. MODULE_LICENSE("GPL");
  32. #define BUF_SIZE 256
  33. struct joydump {
  34. unsigned int time;
  35. unsigned char data;
  36. };
  37. static int joydump_connect(struct gameport *gameport, struct gameport_driver *drv)
  38. {
  39. struct joydump *buf; /* all entries */
  40. struct joydump *dump, *prev; /* one entry each */
  41. int axes[4], buttons;
  42. int i, j, t, timeout;
  43. unsigned long flags;
  44. unsigned char u;
  45. printk(KERN_INFO "joydump: ,------------------ START ----------------.\n");
  46. printk(KERN_INFO "joydump: | Dumping: %30s |\n", gameport->phys);
  47. printk(KERN_INFO "joydump: | Speed: %28d kHz |\n", gameport->speed);
  48. if (gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
  49. printk(KERN_INFO "joydump: | Raw mode not available - trying cooked. |\n");
  50. if (gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
  51. printk(KERN_INFO "joydump: | Cooked not available either. Failing. |\n");
  52. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  53. return -ENODEV;
  54. }
  55. gameport_cooked_read(gameport, axes, &buttons);
  56. for (i = 0; i < 4; i++)
  57. printk(KERN_INFO "joydump: | Axis %d: %4d. |\n", i, axes[i]);
  58. printk(KERN_INFO "joydump: | Buttons %02x. |\n", buttons);
  59. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  60. }
  61. timeout = gameport_time(gameport, 10000); /* 10 ms */
  62. buf = kmalloc_array(BUF_SIZE, sizeof(struct joydump), GFP_KERNEL);
  63. if (!buf) {
  64. printk(KERN_INFO "joydump: no memory for testing\n");
  65. goto jd_end;
  66. }
  67. dump = buf;
  68. t = 0;
  69. i = 1;
  70. local_irq_save(flags);
  71. u = gameport_read(gameport);
  72. dump->data = u;
  73. dump->time = t;
  74. dump++;
  75. gameport_trigger(gameport);
  76. while (i < BUF_SIZE && t < timeout) {
  77. dump->data = gameport_read(gameport);
  78. if (dump->data ^ u) {
  79. u = dump->data;
  80. dump->time = t;
  81. i++;
  82. dump++;
  83. }
  84. t++;
  85. }
  86. local_irq_restore(flags);
  87. /*
  88. * Dump data.
  89. */
  90. t = i;
  91. dump = buf;
  92. prev = dump;
  93. printk(KERN_INFO "joydump: >------------------ DATA -----------------<\n");
  94. printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ", 0, 0);
  95. for (j = 7; j >= 0; j--)
  96. printk("%d", (dump->data >> j) & 1);
  97. printk(" |\n");
  98. dump++;
  99. for (i = 1; i < t; i++, dump++, prev++) {
  100. printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ",
  101. i, dump->time - prev->time);
  102. for (j = 7; j >= 0; j--)
  103. printk("%d", (dump->data >> j) & 1);
  104. printk(" |\n");
  105. }
  106. kfree(buf);
  107. jd_end:
  108. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  109. return 0;
  110. }
  111. static void joydump_disconnect(struct gameport *gameport)
  112. {
  113. gameport_close(gameport);
  114. }
  115. static struct gameport_driver joydump_drv = {
  116. .driver = {
  117. .name = "joydump",
  118. },
  119. .description = DRIVER_DESC,
  120. .connect = joydump_connect,
  121. .disconnect = joydump_disconnect,
  122. };
  123. module_gameport_driver(joydump_drv);