gameport-programming.txt 5.8 KB

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