Path: blob/master/drivers/input/joystick/magellan.c
15112 views
/*1* Copyright (c) 1999-2001 Vojtech Pavlik2*/34/*5* Magellan and Space Mouse 6dof controller 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/module.h>30#include <linux/slab.h>31#include <linux/input.h>32#include <linux/serio.h>33#include <linux/init.h>3435#define DRIVER_DESC "Magellan and SpaceMouse 6dof controller driver"3637MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");38MODULE_DESCRIPTION(DRIVER_DESC);39MODULE_LICENSE("GPL");4041/*42* Definitions & global arrays.43*/4445#define MAGELLAN_MAX_LENGTH 324647static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };48static int magellan_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };4950/*51* Per-Magellan data.52*/5354struct magellan {55struct input_dev *dev;56int idx;57unsigned char data[MAGELLAN_MAX_LENGTH];58char phys[32];59};6061/*62* magellan_crunch_nibbles() verifies that the bytes sent from the Magellan63* have correct upper nibbles for the lower ones, if not, the packet will64* be thrown away. It also strips these upper halves to simplify further65* processing.66*/6768static int magellan_crunch_nibbles(unsigned char *data, int count)69{70static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";7172do {73if (data[count] == nibbles[data[count] & 0xf])74data[count] = data[count] & 0xf;75else76return -1;77} while (--count);7879return 0;80}8182static void magellan_process_packet(struct magellan* magellan)83{84struct input_dev *dev = magellan->dev;85unsigned char *data = magellan->data;86int i, t;8788if (!magellan->idx) return;8990switch (magellan->data[0]) {9192case 'd': /* Axis data */93if (magellan->idx != 25) return;94if (magellan_crunch_nibbles(data, 24)) return;95for (i = 0; i < 6; i++)96input_report_abs(dev, magellan_axes[i],97(data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 |98data[(i << 2) + 3] << 4 | data[(i << 2) + 4]) - 32768);99break;100101case 'k': /* Button data */102if (magellan->idx != 4) return;103if (magellan_crunch_nibbles(data, 3)) return;104t = (data[1] << 1) | (data[2] << 5) | data[3];105for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1);106break;107}108109input_sync(dev);110}111112static irqreturn_t magellan_interrupt(struct serio *serio,113unsigned char data, unsigned int flags)114{115struct magellan* magellan = serio_get_drvdata(serio);116117if (data == '\r') {118magellan_process_packet(magellan);119magellan->idx = 0;120} else {121if (magellan->idx < MAGELLAN_MAX_LENGTH)122magellan->data[magellan->idx++] = data;123}124return IRQ_HANDLED;125}126127/*128* magellan_disconnect() is the opposite of magellan_connect()129*/130131static void magellan_disconnect(struct serio *serio)132{133struct magellan* magellan = serio_get_drvdata(serio);134135serio_close(serio);136serio_set_drvdata(serio, NULL);137input_unregister_device(magellan->dev);138kfree(magellan);139}140141/*142* magellan_connect() is the routine that is called when someone adds a143* new serio device that supports Magellan protocol and registers it as144* an input device.145*/146147static int magellan_connect(struct serio *serio, struct serio_driver *drv)148{149struct magellan *magellan;150struct input_dev *input_dev;151int err = -ENOMEM;152int i;153154magellan = kzalloc(sizeof(struct magellan), GFP_KERNEL);155input_dev = input_allocate_device();156if (!magellan || !input_dev)157goto fail1;158159magellan->dev = input_dev;160snprintf(magellan->phys, sizeof(magellan->phys), "%s/input0", serio->phys);161162input_dev->name = "LogiCad3D Magellan / SpaceMouse";163input_dev->phys = magellan->phys;164input_dev->id.bustype = BUS_RS232;165input_dev->id.vendor = SERIO_MAGELLAN;166input_dev->id.product = 0x0001;167input_dev->id.version = 0x0100;168input_dev->dev.parent = &serio->dev;169170input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);171172for (i = 0; i < 9; i++)173set_bit(magellan_buttons[i], input_dev->keybit);174175for (i = 0; i < 6; i++)176input_set_abs_params(input_dev, magellan_axes[i], -360, 360, 0, 0);177178serio_set_drvdata(serio, magellan);179180err = serio_open(serio, drv);181if (err)182goto fail2;183184err = input_register_device(magellan->dev);185if (err)186goto fail3;187188return 0;189190fail3: serio_close(serio);191fail2: serio_set_drvdata(serio, NULL);192fail1: input_free_device(input_dev);193kfree(magellan);194return err;195}196197/*198* The serio driver structure.199*/200201static struct serio_device_id magellan_serio_ids[] = {202{203.type = SERIO_RS232,204.proto = SERIO_MAGELLAN,205.id = SERIO_ANY,206.extra = SERIO_ANY,207},208{ 0 }209};210211MODULE_DEVICE_TABLE(serio, magellan_serio_ids);212213static struct serio_driver magellan_drv = {214.driver = {215.name = "magellan",216},217.description = DRIVER_DESC,218.id_table = magellan_serio_ids,219.interrupt = magellan_interrupt,220.connect = magellan_connect,221.disconnect = magellan_disconnect,222};223224/*225* The functions for inserting/removing us as a module.226*/227228static int __init magellan_init(void)229{230return serio_register_driver(&magellan_drv);231}232233static void __exit magellan_exit(void)234{235serio_unregister_driver(&magellan_drv);236}237238module_init(magellan_init);239module_exit(magellan_exit);240241242