1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2001 Vojtech Pavlik
7 * Guillemot Digital Interface Protocol driver for Linux
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/gameport.h>
18 #include <linux/input.h>
19 #include <linux/jiffies.h>
21 #define DRIVER_DESC "Guillemot Digital joystick driver"
23 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
24 MODULE_DESCRIPTION(DRIVER_DESC
);
25 MODULE_LICENSE("GPL");
27 #define GUILLEMOT_MAX_START 600 /* 600 us */
28 #define GUILLEMOT_MAX_STROBE 60 /* 60 us */
29 #define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */
31 static short guillemot_abs_pad
[] =
32 { ABS_X
, ABS_Y
, ABS_THROTTLE
, ABS_RUDDER
, -1 };
34 static short guillemot_btn_pad
[] =
35 { BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
, BTN_TL
, BTN_TR
, BTN_MODE
, BTN_SELECT
, -1 };
40 } guillemot_hat_to_axis
[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
42 struct guillemot_type
{
51 struct gameport
*gameport
;
52 struct input_dev
*dev
;
55 struct guillemot_type
*type
;
60 static struct guillemot_type guillemot_type
[] = {
61 { 0x00, guillemot_abs_pad
, guillemot_btn_pad
, 1, "Guillemot Pad" },
65 * guillemot_read_packet() reads Guillemot joystick data.
68 static int guillemot_read_packet(struct gameport
*gameport
, u8
*data
)
75 for (i
= 0; i
< GUILLEMOT_MAX_LENGTH
; i
++)
79 t
= gameport_time(gameport
, GUILLEMOT_MAX_START
);
80 s
= gameport_time(gameport
, GUILLEMOT_MAX_STROBE
);
82 local_irq_save(flags
);
83 gameport_trigger(gameport
);
84 v
= gameport_read(gameport
);
86 while (t
> 0 && i
< GUILLEMOT_MAX_LENGTH
* 8) {
88 u
= v
; v
= gameport_read(gameport
);
90 data
[i
>> 3] |= ((v
>> 5) & 1) << (i
& 7);
96 local_irq_restore(flags
);
102 * guillemot_poll() reads and analyzes Guillemot joystick data.
105 static void guillemot_poll(struct gameport
*gameport
)
107 struct guillemot
*guillemot
= gameport_get_drvdata(gameport
);
108 struct input_dev
*dev
= guillemot
->dev
;
109 u8 data
[GUILLEMOT_MAX_LENGTH
];
114 if (guillemot_read_packet(guillemot
->gameport
, data
) != GUILLEMOT_MAX_LENGTH
* 8 ||
115 data
[0] != 0x55 || data
[16] != 0xaa) {
119 for (i
= 0; i
< 6 && guillemot
->type
->abs
[i
] >= 0; i
++)
120 input_report_abs(dev
, guillemot
->type
->abs
[i
], data
[i
+ 5]);
122 if (guillemot
->type
->hat
) {
123 input_report_abs(dev
, ABS_HAT0X
, guillemot_hat_to_axis
[data
[4] >> 4].x
);
124 input_report_abs(dev
, ABS_HAT0Y
, guillemot_hat_to_axis
[data
[4] >> 4].y
);
127 for (i
= 0; i
< 16 && guillemot
->type
->btn
[i
] >= 0; i
++)
128 input_report_key(dev
, guillemot
->type
->btn
[i
], (data
[2 + (i
>> 3)] >> (i
& 7)) & 1);
135 * guillemot_open() is a callback from the input open routine.
138 static int guillemot_open(struct input_dev
*dev
)
140 struct guillemot
*guillemot
= input_get_drvdata(dev
);
142 gameport_start_polling(guillemot
->gameport
);
147 * guillemot_close() is a callback from the input close routine.
150 static void guillemot_close(struct input_dev
*dev
)
152 struct guillemot
*guillemot
= input_get_drvdata(dev
);
154 gameport_stop_polling(guillemot
->gameport
);
158 * guillemot_connect() probes for Guillemot joysticks.
161 static int guillemot_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
163 struct guillemot
*guillemot
;
164 struct input_dev
*input_dev
;
165 u8 data
[GUILLEMOT_MAX_LENGTH
];
169 guillemot
= kzalloc(sizeof(struct guillemot
), GFP_KERNEL
);
170 input_dev
= input_allocate_device();
171 if (!guillemot
|| !input_dev
) {
176 guillemot
->gameport
= gameport
;
177 guillemot
->dev
= input_dev
;
179 gameport_set_drvdata(gameport
, guillemot
);
181 err
= gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
185 i
= guillemot_read_packet(gameport
, data
);
187 if (i
!= GUILLEMOT_MAX_LENGTH
* 8 || data
[0] != 0x55 || data
[16] != 0xaa) {
192 for (i
= 0; guillemot_type
[i
].name
; i
++)
193 if (guillemot_type
[i
].id
== data
[11])
196 if (!guillemot_type
[i
].name
) {
197 printk(KERN_WARNING
"guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
198 gameport
->phys
, data
[12], data
[13], data
[11], data
[14], data
[15]);
203 gameport_set_poll_handler(gameport
, guillemot_poll
);
204 gameport_set_poll_interval(gameport
, 20);
206 snprintf(guillemot
->phys
, sizeof(guillemot
->phys
), "%s/input0", gameport
->phys
);
207 guillemot
->type
= guillemot_type
+ i
;
209 input_dev
->name
= guillemot_type
[i
].name
;
210 input_dev
->phys
= guillemot
->phys
;
211 input_dev
->id
.bustype
= BUS_GAMEPORT
;
212 input_dev
->id
.vendor
= GAMEPORT_ID_VENDOR_GUILLEMOT
;
213 input_dev
->id
.product
= guillemot_type
[i
].id
;
214 input_dev
->id
.version
= (int)data
[14] << 8 | data
[15];
215 input_dev
->dev
.parent
= &gameport
->dev
;
217 input_set_drvdata(input_dev
, guillemot
);
219 input_dev
->open
= guillemot_open
;
220 input_dev
->close
= guillemot_close
;
222 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
224 for (i
= 0; (t
= guillemot
->type
->abs
[i
]) >= 0; i
++)
225 input_set_abs_params(input_dev
, t
, 0, 255, 0, 0);
227 if (guillemot
->type
->hat
) {
228 input_set_abs_params(input_dev
, ABS_HAT0X
, -1, 1, 0, 0);
229 input_set_abs_params(input_dev
, ABS_HAT0Y
, -1, 1, 0, 0);
232 for (i
= 0; (t
= guillemot
->type
->btn
[i
]) >= 0; i
++)
233 set_bit(t
, input_dev
->keybit
);
235 err
= input_register_device(guillemot
->dev
);
241 fail2
: gameport_close(gameport
);
242 fail1
: gameport_set_drvdata(gameport
, NULL
);
243 input_free_device(input_dev
);
248 static void guillemot_disconnect(struct gameport
*gameport
)
250 struct guillemot
*guillemot
= gameport_get_drvdata(gameport
);
252 printk(KERN_INFO
"guillemot.c: Failed %d reads out of %d on %s\n", guillemot
->reads
, guillemot
->bads
, guillemot
->phys
);
253 input_unregister_device(guillemot
->dev
);
254 gameport_close(gameport
);
258 static struct gameport_driver guillemot_drv
= {
262 .description
= DRIVER_DESC
,
263 .connect
= guillemot_connect
,
264 .disconnect
= guillemot_disconnect
,
267 module_gameport_driver(guillemot_drv
);