Path: blob/master/drivers/input/joystick/guillemot.c
15111 views
/*1* Copyright (c) 2001 Vojtech Pavlik2*/34/*5* Guillemot Digital Interface Protocol driver for Linux6*/78/*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation; either version 2 of the License, or12* (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17* GNU General Public License for more details.18*19* You should have received a copy of the GNU General Public License20* along with this program; if not, write to the Free Software21* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA22*23* Should you need to contact me, the author, you can do so either by24* e-mail - mail your message to <[email protected]>, or by paper mail:25* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic26*/2728#include <linux/kernel.h>29#include <linux/slab.h>30#include <linux/module.h>31#include <linux/delay.h>32#include <linux/init.h>33#include <linux/gameport.h>34#include <linux/input.h>35#include <linux/jiffies.h>3637#define DRIVER_DESC "Guillemot Digital joystick driver"3839MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");40MODULE_DESCRIPTION(DRIVER_DESC);41MODULE_LICENSE("GPL");4243#define GUILLEMOT_MAX_START 600 /* 600 us */44#define GUILLEMOT_MAX_STROBE 60 /* 60 us */45#define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */4647static short guillemot_abs_pad[] =48{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };4950static 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 };5253static struct {54int x;55int y;56} guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};5758struct guillemot_type {59unsigned char id;60short *abs;61short *btn;62int hat;63char *name;64};6566struct guillemot {67struct gameport *gameport;68struct input_dev *dev;69int bads;70int reads;71struct guillemot_type *type;72unsigned char length;73char phys[32];74};7576static struct guillemot_type guillemot_type[] = {77{ 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },78{ 0 }};7980/*81* guillemot_read_packet() reads Guillemot joystick data.82*/8384static int guillemot_read_packet(struct gameport *gameport, u8 *data)85{86unsigned long flags;87unsigned char u, v;88unsigned int t, s;89int i;9091for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)92data[i] = 0;9394i = 0;95t = gameport_time(gameport, GUILLEMOT_MAX_START);96s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);9798local_irq_save(flags);99gameport_trigger(gameport);100v = gameport_read(gameport);101102while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {103t--;104u = v; v = gameport_read(gameport);105if (v & ~u & 0x10) {106data[i >> 3] |= ((v >> 5) & 1) << (i & 7);107i++;108t = s;109}110}111112local_irq_restore(flags);113114return i;115}116117/*118* guillemot_poll() reads and analyzes Guillemot joystick data.119*/120121static void guillemot_poll(struct gameport *gameport)122{123struct guillemot *guillemot = gameport_get_drvdata(gameport);124struct input_dev *dev = guillemot->dev;125u8 data[GUILLEMOT_MAX_LENGTH];126int i;127128guillemot->reads++;129130if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||131data[0] != 0x55 || data[16] != 0xaa) {132guillemot->bads++;133} else {134135for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)136input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);137138if (guillemot->type->hat) {139input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);140input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);141}142143for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)144input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);145}146147input_sync(dev);148}149150/*151* guillemot_open() is a callback from the input open routine.152*/153154static int guillemot_open(struct input_dev *dev)155{156struct guillemot *guillemot = input_get_drvdata(dev);157158gameport_start_polling(guillemot->gameport);159return 0;160}161162/*163* guillemot_close() is a callback from the input close routine.164*/165166static void guillemot_close(struct input_dev *dev)167{168struct guillemot *guillemot = input_get_drvdata(dev);169170gameport_stop_polling(guillemot->gameport);171}172173/*174* guillemot_connect() probes for Guillemot joysticks.175*/176177static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)178{179struct guillemot *guillemot;180struct input_dev *input_dev;181u8 data[GUILLEMOT_MAX_LENGTH];182int i, t;183int err;184185guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);186input_dev = input_allocate_device();187if (!guillemot || !input_dev) {188err = -ENOMEM;189goto fail1;190}191192guillemot->gameport = gameport;193guillemot->dev = input_dev;194195gameport_set_drvdata(gameport, guillemot);196197err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);198if (err)199goto fail1;200201i = guillemot_read_packet(gameport, data);202203if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {204err = -ENODEV;205goto fail2;206}207208for (i = 0; guillemot_type[i].name; i++)209if (guillemot_type[i].id == data[11])210break;211212if (!guillemot_type[i].name) {213printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",214gameport->phys, data[12], data[13], data[11], data[14], data[15]);215err = -ENODEV;216goto fail2;217}218219gameport_set_poll_handler(gameport, guillemot_poll);220gameport_set_poll_interval(gameport, 20);221222snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);223guillemot->type = guillemot_type + i;224225input_dev->name = guillemot_type[i].name;226input_dev->phys = guillemot->phys;227input_dev->id.bustype = BUS_GAMEPORT;228input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;229input_dev->id.product = guillemot_type[i].id;230input_dev->id.version = (int)data[14] << 8 | data[15];231input_dev->dev.parent = &gameport->dev;232233input_set_drvdata(input_dev, guillemot);234235input_dev->open = guillemot_open;236input_dev->close = guillemot_close;237238input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);239240for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)241input_set_abs_params(input_dev, t, 0, 255, 0, 0);242243if (guillemot->type->hat) {244input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);245input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);246}247248for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)249set_bit(t, input_dev->keybit);250251err = input_register_device(guillemot->dev);252if (err)253goto fail2;254255return 0;256257fail2: gameport_close(gameport);258fail1: gameport_set_drvdata(gameport, NULL);259input_free_device(input_dev);260kfree(guillemot);261return err;262}263264static void guillemot_disconnect(struct gameport *gameport)265{266struct guillemot *guillemot = gameport_get_drvdata(gameport);267268printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);269input_unregister_device(guillemot->dev);270gameport_close(gameport);271kfree(guillemot);272}273274static struct gameport_driver guillemot_drv = {275.driver = {276.name = "guillemot",277},278.description = DRIVER_DESC,279.connect = guillemot_connect,280.disconnect = guillemot_disconnect,281};282283static int __init guillemot_init(void)284{285return gameport_register_driver(&guillemot_drv);286}287288static void __exit guillemot_exit(void)289{290gameport_unregister_driver(&guillemot_drv);291}292293module_init(guillemot_init);294module_exit(guillemot_exit);295296297