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