xref: /linux/drivers/input/joystick/turbografx.c (revision 3e51108c72e8adbcf3180ed40527a2a9d2d0123b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 1998-2001 Vojtech Pavlik
4  *
5  *  Based on the work of:
6  *	Steffen Schwenke
7  */
8 
9 /*
10  * TurboGraFX parallel port interface driver for Linux.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/parport.h>
15 #include <linux/input.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 
21 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
22 MODULE_DESCRIPTION("TurboGraFX parallel port interface driver");
23 MODULE_LICENSE("GPL");
24 
25 #define TGFX_MAX_PORTS		3
26 #define TGFX_MAX_DEVICES	7
27 
28 struct tgfx_config {
29 	int args[TGFX_MAX_DEVICES + 1];
30 	unsigned int nargs;
31 };
32 
33 static struct tgfx_config tgfx_cfg[TGFX_MAX_PORTS];
34 
35 module_param_array_named(map, tgfx_cfg[0].args, int, &tgfx_cfg[0].nargs, 0);
36 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<js1>,<js2>,..<js7>");
37 module_param_array_named(map2, tgfx_cfg[1].args, int, &tgfx_cfg[1].nargs, 0);
38 MODULE_PARM_DESC(map2, "Describes second set of devices");
39 module_param_array_named(map3, tgfx_cfg[2].args, int, &tgfx_cfg[2].nargs, 0);
40 MODULE_PARM_DESC(map3, "Describes third set of devices");
41 
42 #define TGFX_REFRESH_TIME	HZ/100	/* 10 ms */
43 
44 #define TGFX_TRIGGER		0x08
45 #define TGFX_UP			0x10
46 #define TGFX_DOWN		0x20
47 #define TGFX_LEFT		0x40
48 #define TGFX_RIGHT		0x80
49 
50 #define TGFX_THUMB		0x02
51 #define TGFX_THUMB2		0x04
52 #define TGFX_TOP		0x01
53 #define TGFX_TOP2		0x08
54 
55 static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };
56 
57 static struct tgfx {
58 	struct pardevice *pd;
59 	struct timer_list timer;
60 	struct input_dev *dev[TGFX_MAX_DEVICES];
61 	char name[TGFX_MAX_DEVICES][64];
62 	char phys[TGFX_MAX_DEVICES][32];
63 	int sticks;
64 	int used;
65 	int parportno;
66 	struct mutex sem;
67 } *tgfx_base[TGFX_MAX_PORTS];
68 
69 /*
70  * tgfx_timer() reads and analyzes TurboGraFX joystick data.
71  */
72 
tgfx_timer(struct timer_list * t)73 static void tgfx_timer(struct timer_list *t)
74 {
75 	struct tgfx *tgfx = from_timer(tgfx, t, timer);
76 	struct input_dev *dev;
77 	int data1, data2, i;
78 
79 	for (i = 0; i < 7; i++)
80 		if (tgfx->sticks & (1 << i)) {
81 
82 			dev = tgfx->dev[i];
83 
84 			parport_write_data(tgfx->pd->port, ~(1 << i));
85 			data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;
86 			data2 = parport_read_control(tgfx->pd->port) ^ 0x04;	/* CAVEAT parport */
87 
88 			input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));
89 			input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP  ));
90 
91 			input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));
92 			input_report_key(dev, BTN_THUMB,   (data2 & TGFX_THUMB  ));
93 			input_report_key(dev, BTN_THUMB2,  (data2 & TGFX_THUMB2 ));
94 			input_report_key(dev, BTN_TOP,     (data2 & TGFX_TOP    ));
95 			input_report_key(dev, BTN_TOP2,    (data2 & TGFX_TOP2   ));
96 
97 			input_sync(dev);
98 		}
99 
100 	mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
101 }
102 
tgfx_open(struct input_dev * dev)103 static int tgfx_open(struct input_dev *dev)
104 {
105 	struct tgfx *tgfx = input_get_drvdata(dev);
106 
107 	scoped_guard(mutex_intr, &tgfx->sem) {
108 		if (!tgfx->used++) {
109 			parport_claim(tgfx->pd);
110 			parport_write_control(tgfx->pd->port, 0x04);
111 			mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
112 		}
113 
114 		return 0;
115 	}
116 
117 	return -EINTR;
118 }
119 
tgfx_close(struct input_dev * dev)120 static void tgfx_close(struct input_dev *dev)
121 {
122 	struct tgfx *tgfx = input_get_drvdata(dev);
123 
124 	guard(mutex)(&tgfx->sem);
125 
126 	if (!--tgfx->used) {
127 		del_timer_sync(&tgfx->timer);
128 		parport_write_control(tgfx->pd->port, 0x00);
129 		parport_release(tgfx->pd);
130 	}
131 }
132 
133 
134 
135 /*
136  * tgfx_probe() probes for tg gamepads.
137  */
138 
tgfx_attach(struct parport * pp)139 static void tgfx_attach(struct parport *pp)
140 {
141 	struct tgfx *tgfx;
142 	struct input_dev *input_dev;
143 	struct pardevice *pd;
144 	int i, j, port_idx;
145 	int *n_buttons, n_devs;
146 	struct pardev_cb tgfx_parport_cb;
147 
148 	for (port_idx = 0; port_idx < TGFX_MAX_PORTS; port_idx++) {
149 		if (tgfx_cfg[port_idx].nargs == 0 ||
150 		    tgfx_cfg[port_idx].args[0] < 0)
151 			continue;
152 		if (tgfx_cfg[port_idx].args[0] == pp->number)
153 			break;
154 	}
155 
156 	if (port_idx == TGFX_MAX_PORTS) {
157 		pr_debug("Not using parport%d.\n", pp->number);
158 		return;
159 	}
160 	n_buttons = tgfx_cfg[port_idx].args + 1;
161 	n_devs = tgfx_cfg[port_idx].nargs - 1;
162 
163 	memset(&tgfx_parport_cb, 0, sizeof(tgfx_parport_cb));
164 	tgfx_parport_cb.flags = PARPORT_FLAG_EXCL;
165 
166 	pd = parport_register_dev_model(pp, "turbografx", &tgfx_parport_cb,
167 					port_idx);
168 	if (!pd) {
169 		pr_err("parport busy already - lp.o loaded?\n");
170 		return;
171 	}
172 
173 	tgfx = kzalloc(sizeof(*tgfx), GFP_KERNEL);
174 	if (!tgfx) {
175 		printk(KERN_ERR "turbografx.c: Not enough memory\n");
176 		goto err_unreg_pardev;
177 	}
178 
179 	mutex_init(&tgfx->sem);
180 	tgfx->pd = pd;
181 	tgfx->parportno = pp->number;
182 	timer_setup(&tgfx->timer, tgfx_timer, 0);
183 
184 	for (i = 0; i < n_devs; i++) {
185 		if (n_buttons[i] < 1)
186 			continue;
187 
188 		if (n_buttons[i] > ARRAY_SIZE(tgfx_buttons)) {
189 			printk(KERN_ERR "turbografx.c: Invalid number of buttons %d\n", n_buttons[i]);
190 			goto err_unreg_devs;
191 		}
192 
193 		tgfx->dev[i] = input_dev = input_allocate_device();
194 		if (!input_dev) {
195 			printk(KERN_ERR "turbografx.c: Not enough memory for input device\n");
196 			goto err_unreg_devs;
197 		}
198 
199 		tgfx->sticks |= (1 << i);
200 		snprintf(tgfx->name[i], sizeof(tgfx->name[i]),
201 			 "TurboGraFX %d-button Multisystem joystick", n_buttons[i]);
202 		snprintf(tgfx->phys[i], sizeof(tgfx->phys[i]),
203 			 "%s/input%d", tgfx->pd->port->name, i);
204 
205 		input_dev->name = tgfx->name[i];
206 		input_dev->phys = tgfx->phys[i];
207 		input_dev->id.bustype = BUS_PARPORT;
208 		input_dev->id.vendor = 0x0003;
209 		input_dev->id.product = n_buttons[i];
210 		input_dev->id.version = 0x0100;
211 
212 		input_set_drvdata(input_dev, tgfx);
213 
214 		input_dev->open = tgfx_open;
215 		input_dev->close = tgfx_close;
216 
217 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
218 		input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
219 		input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
220 
221 		for (j = 0; j < n_buttons[i]; j++)
222 			set_bit(tgfx_buttons[j], input_dev->keybit);
223 
224 		if (input_register_device(tgfx->dev[i]))
225 			goto err_free_dev;
226 	}
227 
228         if (!tgfx->sticks) {
229 		printk(KERN_ERR "turbografx.c: No valid devices specified\n");
230 		goto err_free_tgfx;
231         }
232 
233 	tgfx_base[port_idx] = tgfx;
234 	return;
235 
236  err_free_dev:
237 	input_free_device(tgfx->dev[i]);
238  err_unreg_devs:
239 	while (--i >= 0)
240 		if (tgfx->dev[i])
241 			input_unregister_device(tgfx->dev[i]);
242  err_free_tgfx:
243 	kfree(tgfx);
244  err_unreg_pardev:
245 	parport_unregister_device(pd);
246 }
247 
tgfx_detach(struct parport * port)248 static void tgfx_detach(struct parport *port)
249 {
250 	int i;
251 	struct tgfx *tgfx;
252 
253 	for (i = 0; i < TGFX_MAX_PORTS; i++) {
254 		if (tgfx_base[i] && tgfx_base[i]->parportno == port->number)
255 			break;
256 	}
257 
258 	if (i == TGFX_MAX_PORTS)
259 		return;
260 
261 	tgfx = tgfx_base[i];
262 	tgfx_base[i] = NULL;
263 
264 	for (i = 0; i < TGFX_MAX_DEVICES; i++)
265 		if (tgfx->dev[i])
266 			input_unregister_device(tgfx->dev[i]);
267 	parport_unregister_device(tgfx->pd);
268 	kfree(tgfx);
269 }
270 
271 static struct parport_driver tgfx_parport_driver = {
272 	.name = "turbografx",
273 	.match_port = tgfx_attach,
274 	.detach = tgfx_detach,
275 };
276 
tgfx_init(void)277 static int __init tgfx_init(void)
278 {
279 	int i;
280 	int have_dev = 0;
281 
282 	for (i = 0; i < TGFX_MAX_PORTS; i++) {
283 		if (tgfx_cfg[i].nargs == 0 || tgfx_cfg[i].args[0] < 0)
284 			continue;
285 
286 		if (tgfx_cfg[i].nargs < 2) {
287 			printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");
288 			return -EINVAL;
289 		}
290 
291 		have_dev = 1;
292 	}
293 
294 	if (!have_dev)
295 		return -ENODEV;
296 
297 	return parport_register_driver(&tgfx_parport_driver);
298 }
299 
tgfx_exit(void)300 static void __exit tgfx_exit(void)
301 {
302 	parport_unregister_driver(&tgfx_parport_driver);
303 }
304 
305 module_init(tgfx_init);
306 module_exit(tgfx_exit);
307