joydump.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * Should you need to contact me, the author, you can do so either by
  24. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  25. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  26. */
  27. #include <linux/module.h>
  28. #include <linux/gameport.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #define DRIVER_DESC "Gameport data dumper module"
  34. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  35. MODULE_DESCRIPTION(DRIVER_DESC);
  36. MODULE_LICENSE("GPL");
  37. #define BUF_SIZE 256
  38. struct joydump {
  39. unsigned int time;
  40. unsigned char data;
  41. };
  42. static int joydump_connect(struct gameport *gameport, struct gameport_driver *drv)
  43. {
  44. struct joydump *buf; /* all entries */
  45. struct joydump *dump, *prev; /* one entry each */
  46. int axes[4], buttons;
  47. int i, j, t, timeout;
  48. unsigned long flags;
  49. unsigned char u;
  50. printk(KERN_INFO "joydump: ,------------------ START ----------------.\n");
  51. printk(KERN_INFO "joydump: | Dumping: %30s |\n", gameport->phys);
  52. printk(KERN_INFO "joydump: | Speed: %28d kHz |\n", gameport->speed);
  53. if (gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
  54. printk(KERN_INFO "joydump: | Raw mode not available - trying cooked. |\n");
  55. if (gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
  56. printk(KERN_INFO "joydump: | Cooked not available either. Failing. |\n");
  57. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  58. return -ENODEV;
  59. }
  60. gameport_cooked_read(gameport, axes, &buttons);
  61. for (i = 0; i < 4; i++)
  62. printk(KERN_INFO "joydump: | Axis %d: %4d. |\n", i, axes[i]);
  63. printk(KERN_INFO "joydump: | Buttons %02x. |\n", buttons);
  64. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  65. }
  66. timeout = gameport_time(gameport, 10000); /* 10 ms */
  67. buf = kmalloc(BUF_SIZE * sizeof(struct joydump), GFP_KERNEL);
  68. if (!buf) {
  69. printk(KERN_INFO "joydump: no memory for testing\n");
  70. goto jd_end;
  71. }
  72. dump = buf;
  73. t = 0;
  74. i = 1;
  75. local_irq_save(flags);
  76. u = gameport_read(gameport);
  77. dump->data = u;
  78. dump->time = t;
  79. dump++;
  80. gameport_trigger(gameport);
  81. while (i < BUF_SIZE && t < timeout) {
  82. dump->data = gameport_read(gameport);
  83. if (dump->data ^ u) {
  84. u = dump->data;
  85. dump->time = t;
  86. i++;
  87. dump++;
  88. }
  89. t++;
  90. }
  91. local_irq_restore(flags);
  92. /*
  93. * Dump data.
  94. */
  95. t = i;
  96. dump = buf;
  97. prev = dump;
  98. printk(KERN_INFO "joydump: >------------------ DATA -----------------<\n");
  99. printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ", 0, 0);
  100. for (j = 7; j >= 0; j--)
  101. printk("%d", (dump->data >> j) & 1);
  102. printk(" |\n");
  103. dump++;
  104. for (i = 1; i < t; i++, dump++, prev++) {
  105. printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ",
  106. i, dump->time - prev->time);
  107. for (j = 7; j >= 0; j--)
  108. printk("%d", (dump->data >> j) & 1);
  109. printk(" |\n");
  110. }
  111. kfree(buf);
  112. jd_end:
  113. printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
  114. return 0;
  115. }
  116. static void joydump_disconnect(struct gameport *gameport)
  117. {
  118. gameport_close(gameport);
  119. }
  120. static struct gameport_driver joydump_drv = {
  121. .driver = {
  122. .name = "joydump",
  123. },
  124. .description = DRIVER_DESC,
  125. .connect = joydump_connect,
  126. .disconnect = joydump_disconnect,
  127. };
  128. static int __init joydump_init(void)
  129. {
  130. return gameport_register_driver(&joydump_drv);
  131. }
  132. static void __exit joydump_exit(void)
  133. {
  134. gameport_unregister_driver(&joydump_drv);
  135. }
  136. module_init(joydump_init);
  137. module_exit(joydump_exit);