Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Copyright (c) 2001 Vojtech Pavlik
  4 */
  5
  6/*
  7 * Guillemot Digital Interface Protocol driver for Linux
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/slab.h>
 12#include <linux/module.h>
 13#include <linux/delay.h>
 14#include <linux/gameport.h>
 15#include <linux/input.h>
 16#include <linux/jiffies.h>
 17
 18#define DRIVER_DESC	"Guillemot Digital joystick driver"
 19
 20MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 21MODULE_DESCRIPTION(DRIVER_DESC);
 22MODULE_LICENSE("GPL");
 23
 24#define GUILLEMOT_MAX_START	600	/* 600 us */
 25#define GUILLEMOT_MAX_STROBE	60	/* 60 us */
 26#define GUILLEMOT_MAX_LENGTH	17	/* 17 bytes */
 27
 28static short guillemot_abs_pad[] =
 29	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
 30
 31static short guillemot_btn_pad[] =
 32	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
 33
 34static struct {
 35        int x;
 36        int y;
 37} guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
 38
 39struct guillemot_type {
 40	unsigned char id;
 41	short *abs;
 42	short *btn;
 43	int hat;
 44	char *name;
 45};
 46
 47struct guillemot {
 48	struct gameport *gameport;
 49	struct input_dev *dev;
 50	int bads;
 51	int reads;
 52	struct guillemot_type *type;
 53	unsigned char length;
 54	char phys[32];
 55};
 56
 57static struct guillemot_type guillemot_type[] = {
 58	{ 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
 59	{ 0 }};
 60
 61/*
 62 * guillemot_read_packet() reads Guillemot joystick data.
 63 */
 64
 65static int guillemot_read_packet(struct gameport *gameport, u8 *data)
 66{
 67	unsigned long flags;
 68	unsigned char u, v;
 69	unsigned int t, s;
 70	int i;
 71
 72	for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
 73		data[i] = 0;
 74
 75	i = 0;
 76	t = gameport_time(gameport, GUILLEMOT_MAX_START);
 77	s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
 78
 79	local_irq_save(flags);
 80	gameport_trigger(gameport);
 81	v = gameport_read(gameport);
 82
 83	while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
 84		t--;
 85		u = v; v = gameport_read(gameport);
 86		if (v & ~u & 0x10) {
 87			data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
 88			i++;
 89			t = s;
 90		}
 91	}
 92
 93	local_irq_restore(flags);
 94
 95	return i;
 96}
 97
 98/*
 99 * guillemot_poll() reads and analyzes Guillemot joystick data.
100 */
101
102static void guillemot_poll(struct gameport *gameport)
103{
104	struct guillemot *guillemot = gameport_get_drvdata(gameport);
105	struct input_dev *dev = guillemot->dev;
106	u8 data[GUILLEMOT_MAX_LENGTH];
107	int i;
108
109	guillemot->reads++;
110
111	if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
112		data[0] != 0x55 || data[16] != 0xaa) {
113		guillemot->bads++;
114	} else {
115
116		for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
117			input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
118
119		if (guillemot->type->hat) {
120			input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
121			input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
122		}
123
124		for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
125			input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
126	}
127
128	input_sync(dev);
129}
130
131/*
132 * guillemot_open() is a callback from the input open routine.
133 */
134
135static int guillemot_open(struct input_dev *dev)
136{
137	struct guillemot *guillemot = input_get_drvdata(dev);
138
139	gameport_start_polling(guillemot->gameport);
140	return 0;
141}
142
143/*
144 * guillemot_close() is a callback from the input close routine.
145 */
146
147static void guillemot_close(struct input_dev *dev)
148{
149	struct guillemot *guillemot = input_get_drvdata(dev);
150
151	gameport_stop_polling(guillemot->gameport);
152}
153
154/*
155 * guillemot_connect() probes for Guillemot joysticks.
156 */
157
158static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
159{
160	struct guillemot *guillemot;
161	struct input_dev *input_dev;
162	u8 data[GUILLEMOT_MAX_LENGTH];
163	int i, t;
164	int err;
165
166	guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
167	input_dev = input_allocate_device();
168	if (!guillemot || !input_dev) {
169		err = -ENOMEM;
170		goto fail1;
171	}
172
173	guillemot->gameport = gameport;
174	guillemot->dev = input_dev;
175
176	gameport_set_drvdata(gameport, guillemot);
177
178	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
179	if (err)
180		goto fail1;
181
182	i = guillemot_read_packet(gameport, data);
183
184	if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
185		err = -ENODEV;
186		goto fail2;
187	}
188
189	for (i = 0; guillemot_type[i].name; i++)
190		if (guillemot_type[i].id == data[11])
191			break;
192
193	if (!guillemot_type[i].name) {
194		printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
195			gameport->phys, data[12], data[13], data[11], data[14], data[15]);
196		err = -ENODEV;
197		goto fail2;
198	}
199
200	gameport_set_poll_handler(gameport, guillemot_poll);
201	gameport_set_poll_interval(gameport, 20);
202
203	snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
204	guillemot->type = guillemot_type + i;
205
206	input_dev->name = guillemot_type[i].name;
207	input_dev->phys = guillemot->phys;
208	input_dev->id.bustype = BUS_GAMEPORT;
209	input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
210	input_dev->id.product = guillemot_type[i].id;
211	input_dev->id.version = (int)data[14] << 8 | data[15];
212	input_dev->dev.parent = &gameport->dev;
213
214	input_set_drvdata(input_dev, guillemot);
215
216	input_dev->open = guillemot_open;
217	input_dev->close = guillemot_close;
218
219	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
220
221	for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
222		input_set_abs_params(input_dev, t, 0, 255, 0, 0);
223
224	if (guillemot->type->hat) {
225		input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
226		input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
227	}
228
229	for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
230		set_bit(t, input_dev->keybit);
231
232	err = input_register_device(guillemot->dev);
233	if (err)
234		goto fail2;
235
236	return 0;
237
238fail2:	gameport_close(gameport);
239fail1:  gameport_set_drvdata(gameport, NULL);
240	input_free_device(input_dev);
241	kfree(guillemot);
242	return err;
243}
244
245static void guillemot_disconnect(struct gameport *gameport)
246{
247	struct guillemot *guillemot = gameport_get_drvdata(gameport);
248
249	printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
250	input_unregister_device(guillemot->dev);
251	gameport_close(gameport);
252	kfree(guillemot);
253}
254
255static struct gameport_driver guillemot_drv = {
256	.driver		= {
257		.name	= "guillemot",
258	},
259	.description	= DRIVER_DESC,
260	.connect	= guillemot_connect,
261	.disconnect	= guillemot_disconnect,
262};
263
264module_gameport_driver(guillemot_drv);
v4.6
 
  1/*
  2 *  Copyright (c) 2001 Vojtech Pavlik
  3 */
  4
  5/*
  6 * Guillemot Digital Interface Protocol driver for Linux
  7 */
  8
  9/*
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 23 *
 24 * Should you need to contact me, the author, you can do so either by
 25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 27 */
 28
 29#include <linux/kernel.h>
 30#include <linux/slab.h>
 31#include <linux/module.h>
 32#include <linux/delay.h>
 33#include <linux/gameport.h>
 34#include <linux/input.h>
 35#include <linux/jiffies.h>
 36
 37#define DRIVER_DESC	"Guillemot Digital joystick driver"
 38
 39MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 40MODULE_DESCRIPTION(DRIVER_DESC);
 41MODULE_LICENSE("GPL");
 42
 43#define GUILLEMOT_MAX_START	600	/* 600 us */
 44#define GUILLEMOT_MAX_STROBE	60	/* 60 us */
 45#define GUILLEMOT_MAX_LENGTH	17	/* 17 bytes */
 46
 47static short guillemot_abs_pad[] =
 48	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
 49
 50static short guillemot_btn_pad[] =
 51	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
 52
 53static struct {
 54        int x;
 55        int y;
 56} guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
 57
 58struct guillemot_type {
 59	unsigned char id;
 60	short *abs;
 61	short *btn;
 62	int hat;
 63	char *name;
 64};
 65
 66struct guillemot {
 67	struct gameport *gameport;
 68	struct input_dev *dev;
 69	int bads;
 70	int reads;
 71	struct guillemot_type *type;
 72	unsigned char length;
 73	char phys[32];
 74};
 75
 76static struct guillemot_type guillemot_type[] = {
 77	{ 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
 78	{ 0 }};
 79
 80/*
 81 * guillemot_read_packet() reads Guillemot joystick data.
 82 */
 83
 84static int guillemot_read_packet(struct gameport *gameport, u8 *data)
 85{
 86	unsigned long flags;
 87	unsigned char u, v;
 88	unsigned int t, s;
 89	int i;
 90
 91	for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
 92		data[i] = 0;
 93
 94	i = 0;
 95	t = gameport_time(gameport, GUILLEMOT_MAX_START);
 96	s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
 97
 98	local_irq_save(flags);
 99	gameport_trigger(gameport);
100	v = gameport_read(gameport);
101
102	while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
103		t--;
104		u = v; v = gameport_read(gameport);
105		if (v & ~u & 0x10) {
106			data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
107			i++;
108			t = s;
109		}
110	}
111
112	local_irq_restore(flags);
113
114	return i;
115}
116
117/*
118 * guillemot_poll() reads and analyzes Guillemot joystick data.
119 */
120
121static void guillemot_poll(struct gameport *gameport)
122{
123	struct guillemot *guillemot = gameport_get_drvdata(gameport);
124	struct input_dev *dev = guillemot->dev;
125	u8 data[GUILLEMOT_MAX_LENGTH];
126	int i;
127
128	guillemot->reads++;
129
130	if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
131		data[0] != 0x55 || data[16] != 0xaa) {
132		guillemot->bads++;
133	} else {
134
135		for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
136			input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
137
138		if (guillemot->type->hat) {
139			input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
140			input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
141		}
142
143		for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
144			input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
145	}
146
147	input_sync(dev);
148}
149
150/*
151 * guillemot_open() is a callback from the input open routine.
152 */
153
154static int guillemot_open(struct input_dev *dev)
155{
156	struct guillemot *guillemot = input_get_drvdata(dev);
157
158	gameport_start_polling(guillemot->gameport);
159	return 0;
160}
161
162/*
163 * guillemot_close() is a callback from the input close routine.
164 */
165
166static void guillemot_close(struct input_dev *dev)
167{
168	struct guillemot *guillemot = input_get_drvdata(dev);
169
170	gameport_stop_polling(guillemot->gameport);
171}
172
173/*
174 * guillemot_connect() probes for Guillemot joysticks.
175 */
176
177static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
178{
179	struct guillemot *guillemot;
180	struct input_dev *input_dev;
181	u8 data[GUILLEMOT_MAX_LENGTH];
182	int i, t;
183	int err;
184
185	guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
186	input_dev = input_allocate_device();
187	if (!guillemot || !input_dev) {
188		err = -ENOMEM;
189		goto fail1;
190	}
191
192	guillemot->gameport = gameport;
193	guillemot->dev = input_dev;
194
195	gameport_set_drvdata(gameport, guillemot);
196
197	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
198	if (err)
199		goto fail1;
200
201	i = guillemot_read_packet(gameport, data);
202
203	if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
204		err = -ENODEV;
205		goto fail2;
206	}
207
208	for (i = 0; guillemot_type[i].name; i++)
209		if (guillemot_type[i].id == data[11])
210			break;
211
212	if (!guillemot_type[i].name) {
213		printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
214			gameport->phys, data[12], data[13], data[11], data[14], data[15]);
215		err = -ENODEV;
216		goto fail2;
217	}
218
219	gameport_set_poll_handler(gameport, guillemot_poll);
220	gameport_set_poll_interval(gameport, 20);
221
222	snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
223	guillemot->type = guillemot_type + i;
224
225	input_dev->name = guillemot_type[i].name;
226	input_dev->phys = guillemot->phys;
227	input_dev->id.bustype = BUS_GAMEPORT;
228	input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
229	input_dev->id.product = guillemot_type[i].id;
230	input_dev->id.version = (int)data[14] << 8 | data[15];
231	input_dev->dev.parent = &gameport->dev;
232
233	input_set_drvdata(input_dev, guillemot);
234
235	input_dev->open = guillemot_open;
236	input_dev->close = guillemot_close;
237
238	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
239
240	for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
241		input_set_abs_params(input_dev, t, 0, 255, 0, 0);
242
243	if (guillemot->type->hat) {
244		input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
245		input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
246	}
247
248	for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
249		set_bit(t, input_dev->keybit);
250
251	err = input_register_device(guillemot->dev);
252	if (err)
253		goto fail2;
254
255	return 0;
256
257fail2:	gameport_close(gameport);
258fail1:  gameport_set_drvdata(gameport, NULL);
259	input_free_device(input_dev);
260	kfree(guillemot);
261	return err;
262}
263
264static void guillemot_disconnect(struct gameport *gameport)
265{
266	struct guillemot *guillemot = gameport_get_drvdata(gameport);
267
268	printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
269	input_unregister_device(guillemot->dev);
270	gameport_close(gameport);
271	kfree(guillemot);
272}
273
274static struct gameport_driver guillemot_drv = {
275	.driver		= {
276		.name	= "guillemot",
277	},
278	.description	= DRIVER_DESC,
279	.connect	= guillemot_connect,
280	.disconnect	= guillemot_disconnect,
281};
282
283module_gameport_driver(guillemot_drv);