gameport-programming.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. Programming gameport drivers
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. A basic classic gameport
  5. ~~~~~~~~~~~~~~~~~~~~~~~~
  6. If the gameport doesn't provide more than the inb()/outb() functionality,
  7. the code needed to register it with the joystick drivers is simple::
  8. struct gameport gameport;
  9. gameport.io = MY_IO_ADDRESS;
  10. gameport_register_port(&gameport);
  11. Make sure struct gameport is initialized to 0 in all other fields. The
  12. gameport generic code will take care of the rest.
  13. If your hardware supports more than one io address, and your driver can
  14. choose which one to program the hardware to, starting from the more exotic
  15. addresses is preferred, because the likelihood of clashing with the standard
  16. 0x201 address is smaller.
  17. Eg. if your driver supports addresses 0x200, 0x208, 0x210 and 0x218, then
  18. 0x218 would be the address of first choice.
  19. If your hardware supports a gameport address that is not mapped to ISA io
  20. space (is above 0x1000), use that one, and don't map the ISA mirror.
  21. Also, always request_region() on the whole io space occupied by the
  22. gameport. Although only one ioport is really used, the gameport usually
  23. occupies from one to sixteen addresses in the io space.
  24. Please also consider enabling the gameport on the card in the ->open()
  25. callback if the io is mapped to ISA space - this way it'll occupy the io
  26. space only when something really is using it. Disable it again in the
  27. ->close() callback. You also can select the io address in the ->open()
  28. callback, so that it doesn't fail if some of the possible addresses are
  29. already occupied by other gameports.
  30. Memory mapped gameport
  31. ~~~~~~~~~~~~~~~~~~~~~~
  32. When a gameport can be accessed through MMIO, this way is preferred, because
  33. it is faster, allowing more reads per second. Registering such a gameport
  34. isn't as easy as a basic IO one, but not so much complex::
  35. struct gameport gameport;
  36. void my_trigger(struct gameport *gameport)
  37. {
  38. my_mmio = 0xff;
  39. }
  40. unsigned char my_read(struct gameport *gameport)
  41. {
  42. return my_mmio;
  43. }
  44. gameport.read = my_read;
  45. gameport.trigger = my_trigger;
  46. gameport_register_port(&gameport);
  47. .. _gameport_pgm_cooked_mode:
  48. Cooked mode gameport
  49. ~~~~~~~~~~~~~~~~~~~~
  50. There are gameports that can report the axis values as numbers, that means
  51. the driver doesn't have to measure them the old way - an ADC is built into
  52. the gameport. To register a cooked gameport::
  53. struct gameport gameport;
  54. int my_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  55. {
  56. int i;
  57. for (i = 0; i < 4; i++)
  58. axes[i] = my_mmio[i];
  59. buttons[i] = my_mmio[4];
  60. }
  61. int my_open(struct gameport *gameport, int mode)
  62. {
  63. return -(mode != GAMEPORT_MODE_COOKED);
  64. }
  65. gameport.cooked_read = my_cooked_read;
  66. gameport.open = my_open;
  67. gameport.fuzz = 8;
  68. gameport_register_port(&gameport);
  69. The only confusing thing here is the fuzz value. Best determined by
  70. experimentation, it is the amount of noise in the ADC data. Perfect
  71. gameports can set this to zero, most common have fuzz between 8 and 32.
  72. See analog.c and input.c for handling of fuzz - the fuzz value determines
  73. the size of a gaussian filter window that is used to eliminate the noise
  74. in the data.
  75. More complex gameports
  76. ~~~~~~~~~~~~~~~~~~~~~~
  77. Gameports can support both raw and cooked modes. In that case combine either
  78. examples 1+2 or 1+3. Gameports can support internal calibration - see below,
  79. and also lightning.c and analog.c on how that works. If your driver supports
  80. more than one gameport instance simultaneously, use the ->private member of
  81. the gameport struct to point to your data.
  82. Unregistering a gameport
  83. ~~~~~~~~~~~~~~~~~~~~~~~~
  84. Simple::
  85. gameport_unregister_port(&gameport);
  86. The gameport structure
  87. ~~~~~~~~~~~~~~~~~~~~~~
  88. .. note::
  89. This section is outdated. There are several fields here that don't
  90. match what's there at include/linux/gameport.h.
  91. ::
  92. struct gameport {
  93. void *private;
  94. A private pointer for free use in the gameport driver. (Not the joystick
  95. driver!)
  96. ::
  97. int number;
  98. Number assigned to the gameport when registered. Informational purpose only.
  99. ::
  100. int io;
  101. I/O address for use with raw mode. You have to either set this, or ->read()
  102. to some value if your gameport supports raw mode.
  103. ::
  104. int speed;
  105. Raw mode speed of the gameport reads in thousands of reads per second.
  106. ::
  107. int fuzz;
  108. If the gameport supports cooked mode, this should be set to a value that
  109. represents the amount of noise in the data. See
  110. :ref:`gameport_pgm_cooked_mode`.
  111. ::
  112. void (*trigger)(struct gameport *);
  113. Trigger. This function should trigger the ns558 oneshots. If set to NULL,
  114. outb(0xff, io) will be used.
  115. ::
  116. unsigned char (*read)(struct gameport *);
  117. Read the buttons and ns558 oneshot bits. If set to NULL, inb(io) will be
  118. used instead.
  119. ::
  120. int (*cooked_read)(struct gameport *, int *axes, int *buttons);
  121. If the gameport supports cooked mode, it should point this to its cooked
  122. read function. It should fill axes[0..3] with four values of the joystick axes
  123. and buttons[0] with four bits representing the buttons.
  124. ::
  125. int (*calibrate)(struct gameport *, int *axes, int *max);
  126. Function for calibrating the ADC hardware. When called, axes[0..3] should be
  127. pre-filled by cooked data by the caller, max[0..3] should be pre-filled with
  128. expected maximums for each axis. The calibrate() function should set the
  129. sensitivity of the ADC hardware so that the maximums fit in its range and
  130. recompute the axes[] values to match the new sensitivity or re-read them from
  131. the hardware so that they give valid values.
  132. ::
  133. int (*open)(struct gameport *, int mode);
  134. Open() serves two purposes. First a driver either opens the port in raw or
  135. in cooked mode, the open() callback can decide which modes are supported.
  136. Second, resource allocation can happen here. The port can also be enabled
  137. here. Prior to this call, other fields of the gameport struct (namely the io
  138. member) need not to be valid.
  139. ::
  140. void (*close)(struct gameport *);
  141. Close() should free the resources allocated by open, possibly disabling the
  142. gameport.
  143. ::
  144. struct gameport_dev *dev;
  145. struct gameport *next;
  146. For internal use by the gameport layer.
  147. ::
  148. };
  149. Enjoy!