2  * $Id: guillemot.c,v 1.10 2002/01/22 20:28:12 vojtech Exp $
 
   4  *  Copyright (c) 2001 Vojtech Pavlik
 
   8  * Guillemot Digital Interface Protocol driver for Linux
 
  12  * This program is free software; you can redistribute it and/or modify
 
  13  * it under the terms of the GNU General Public License as published by
 
  14  * the Free Software Foundation; either version 2 of the License, or
 
  15  * (at your option) any later version.
 
  17  * This program is distributed in the hope that it will be useful,
 
  18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  20  * GNU General Public License for more details.
 
  22  * You should have received a copy of the GNU General Public License
 
  23  * along with this program; if not, write to the Free Software
 
  24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
  26  * Should you need to contact me, the author, you can do so either by
 
  27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 
  28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 
  31 #include <linux/kernel.h>
 
  32 #include <linux/slab.h>
 
  33 #include <linux/module.h>
 
  34 #include <linux/delay.h>
 
  35 #include <linux/init.h>
 
  36 #include <linux/gameport.h>
 
  37 #include <linux/input.h>
 
  39 #define DRIVER_DESC     "Guillemot Digital joystick driver"
 
  41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 
  42 MODULE_DESCRIPTION(DRIVER_DESC);
 
  43 MODULE_LICENSE("GPL");
 
  45 #define GUILLEMOT_MAX_START     600     /* 600 us */
 
  46 #define GUILLEMOT_MAX_STROBE    60      /* 60 us */
 
  47 #define GUILLEMOT_MAX_LENGTH    17      /* 17 bytes */
 
  49 static short guillemot_abs_pad[] =
 
  50         { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
 
  52 static short guillemot_btn_pad[] =
 
  53         { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
 
  58 } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
 
  60 struct guillemot_type {
 
  69         struct gameport *gameport;
 
  73         struct guillemot_type *type;
 
  78 static struct guillemot_type guillemot_type[] = {
 
  79         { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
 
  83  * guillemot_read_packet() reads Guillemot joystick data.
 
  86 static int guillemot_read_packet(struct gameport *gameport, u8 *data)
 
  93         for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
 
  97         t = gameport_time(gameport, GUILLEMOT_MAX_START);
 
  98         s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
 
 100         local_irq_save(flags);
 
 101         gameport_trigger(gameport);
 
 102         v = gameport_read(gameport);
 
 104         while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
 
 106                 u = v; v = gameport_read(gameport);
 
 108                         data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
 
 114         local_irq_restore(flags);
 
 120  * guillemot_poll() reads and analyzes Guillemot joystick data.
 
 123 static void guillemot_poll(struct gameport *gameport)
 
 125         struct guillemot *guillemot = gameport_get_drvdata(gameport);
 
 126         struct input_dev *dev = &guillemot->dev;
 
 127         u8 data[GUILLEMOT_MAX_LENGTH];
 
 132         if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
 
 133                 data[0] != 0x55 || data[16] != 0xaa) {
 
 137                 for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
 
 138                         input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
 
 140                 if (guillemot->type->hat) {
 
 141                         input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
 
 142                         input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
 
 145                 for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
 
 146                         input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
 
 153  * guillemot_open() is a callback from the input open routine.
 
 156 static int guillemot_open(struct input_dev *dev)
 
 158         struct guillemot *guillemot = dev->private;
 
 160         gameport_start_polling(guillemot->gameport);
 
 165  * guillemot_close() is a callback from the input close routine.
 
 168 static void guillemot_close(struct input_dev *dev)
 
 170         struct guillemot *guillemot = dev->private;
 
 172         gameport_stop_polling(guillemot->gameport);
 
 176  * guillemot_connect() probes for Guillemot joysticks.
 
 179 static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
 
 181         struct guillemot *guillemot;
 
 182         u8 data[GUILLEMOT_MAX_LENGTH];
 
 186         if (!(guillemot = kcalloc(1, sizeof(struct guillemot), GFP_KERNEL)))
 
 189         guillemot->gameport = gameport;
 
 191         gameport_set_drvdata(gameport, guillemot);
 
 193         err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
 
 197         i = guillemot_read_packet(gameport, data);
 
 199         if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
 
 204         for (i = 0; guillemot_type[i].name; i++)
 
 205                 if (guillemot_type[i].id == data[11])
 
 208         if (!guillemot_type[i].name) {
 
 209                 printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
 
 210                         gameport->phys, data[12], data[13], data[11], data[14], data[15]);
 
 215         gameport_set_poll_handler(gameport, guillemot_poll);
 
 216         gameport_set_poll_interval(gameport, 20);
 
 218         sprintf(guillemot->phys, "%s/input0", gameport->phys);
 
 220         guillemot->type = guillemot_type + i;
 
 222         guillemot->dev.private = guillemot;
 
 223         guillemot->dev.open = guillemot_open;
 
 224         guillemot->dev.close = guillemot_close;
 
 226         guillemot->dev.name = guillemot_type[i].name;
 
 227         guillemot->dev.phys = guillemot->phys;
 
 228         guillemot->dev.id.bustype = BUS_GAMEPORT;
 
 229         guillemot->dev.id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
 
 230         guillemot->dev.id.product = guillemot_type[i].id;
 
 231         guillemot->dev.id.version = (int)data[14] << 8 | data[15];
 
 233         guillemot->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
 
 235         for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
 
 236                 input_set_abs_params(&guillemot->dev, t, 0, 255, 0, 0);
 
 238         if (guillemot->type->hat) {
 
 239                 input_set_abs_params(&guillemot->dev, ABS_HAT0X, -1, 1, 0, 0);
 
 240                 input_set_abs_params(&guillemot->dev, ABS_HAT0Y, -1, 1, 0, 0);
 
 243         for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
 
 244                 set_bit(t, guillemot->dev.keybit);
 
 246         input_register_device(&guillemot->dev);
 
 247         printk(KERN_INFO "input: %s ver %d.%02d on %s\n",
 
 248                 guillemot->type->name, data[14], data[15], gameport->phys);
 
 252 fail2:  gameport_close(gameport);
 
 253 fail1:  gameport_set_drvdata(gameport, NULL);
 
 258 static void guillemot_disconnect(struct gameport *gameport)
 
 260         struct guillemot *guillemot = gameport_get_drvdata(gameport);
 
 262         printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
 
 263         input_unregister_device(&guillemot->dev);
 
 264         gameport_close(gameport);
 
 268 static struct gameport_driver guillemot_drv = {
 
 272         .description    = DRIVER_DESC,
 
 273         .connect        = guillemot_connect,
 
 274         .disconnect     = guillemot_disconnect,
 
 277 static int __init guillemot_init(void)
 
 279         gameport_register_driver(&guillemot_drv);
 
 283 static void __exit guillemot_exit(void)
 
 285         gameport_unregister_driver(&guillemot_drv);
 
 288 module_init(guillemot_init);
 
 289 module_exit(guillemot_exit);