Path: blob/master/drivers/input/joystick/spaceorb.c
15111 views
/*1* Copyright (c) 1999-2001 Vojtech Pavlik2*3* Based on the work of:4* David Thompson5*/67/*8* SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux9*/1011/*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA25*26* Should you need to contact me, the author, you can do so either by27* e-mail - mail your message to <[email protected]>, or by paper mail:28* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic29*/3031#include <linux/kernel.h>32#include <linux/slab.h>33#include <linux/module.h>34#include <linux/init.h>35#include <linux/input.h>36#include <linux/serio.h>3738#define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"3940MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");41MODULE_DESCRIPTION(DRIVER_DESC);42MODULE_LICENSE("GPL");4344/*45* Constants.46*/4748#define SPACEORB_MAX_LENGTH 644950static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };51static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };5253/*54* Per-Orb data.55*/5657struct spaceorb {58struct input_dev *dev;59int idx;60unsigned char data[SPACEORB_MAX_LENGTH];61char phys[32];62};6364static unsigned char spaceorb_xor[] = "SpaceWare";6566static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",67"Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };6869/*70* spaceorb_process_packet() decodes packets the driver receives from the71* SpaceOrb.72*/7374static void spaceorb_process_packet(struct spaceorb *spaceorb)75{76struct input_dev *dev = spaceorb->dev;77unsigned char *data = spaceorb->data;78unsigned char c = 0;79int axes[6];80int i;8182if (spaceorb->idx < 2) return;83for (i = 0; i < spaceorb->idx; i++) c ^= data[i];84if (c) return;8586switch (data[0]) {8788case 'R': /* Reset packet */89spaceorb->data[spaceorb->idx - 1] = 0;90for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);91printk(KERN_INFO "input: %s [%s] is %s\n",92dev->name, spaceorb->data + i, spaceorb->phys);93break;9495case 'D': /* Ball + button data */96if (spaceorb->idx != 12) return;97for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];98axes[0] = ( data[2] << 3) | (data[ 3] >> 4);99axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);100axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);101axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);102axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);103axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);104for (i = 0; i < 6; i++)105input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));106for (i = 0; i < 6; i++)107input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);108break;109110case 'K': /* Button data */111if (spaceorb->idx != 5) return;112for (i = 0; i < 6; i++)113input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);114115break;116117case 'E': /* Error packet */118if (spaceorb->idx != 4) return;119printk(KERN_ERR "spaceorb: Device error. [ ");120for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);121printk("]\n");122break;123}124125input_sync(dev);126}127128static irqreturn_t spaceorb_interrupt(struct serio *serio,129unsigned char data, unsigned int flags)130{131struct spaceorb* spaceorb = serio_get_drvdata(serio);132133if (~data & 0x80) {134if (spaceorb->idx) spaceorb_process_packet(spaceorb);135spaceorb->idx = 0;136}137if (spaceorb->idx < SPACEORB_MAX_LENGTH)138spaceorb->data[spaceorb->idx++] = data & 0x7f;139return IRQ_HANDLED;140}141142/*143* spaceorb_disconnect() is the opposite of spaceorb_connect()144*/145146static void spaceorb_disconnect(struct serio *serio)147{148struct spaceorb* spaceorb = serio_get_drvdata(serio);149150serio_close(serio);151serio_set_drvdata(serio, NULL);152input_unregister_device(spaceorb->dev);153kfree(spaceorb);154}155156/*157* spaceorb_connect() is the routine that is called when someone adds a158* new serio device that supports SpaceOrb/Avenger protocol and registers159* it as an input device.160*/161162static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)163{164struct spaceorb *spaceorb;165struct input_dev *input_dev;166int err = -ENOMEM;167int i;168169spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL);170input_dev = input_allocate_device();171if (!spaceorb || !input_dev)172goto fail1;173174spaceorb->dev = input_dev;175snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys);176177input_dev->name = "SpaceTec SpaceOrb 360 / Avenger";178input_dev->phys = spaceorb->phys;179input_dev->id.bustype = BUS_RS232;180input_dev->id.vendor = SERIO_SPACEORB;181input_dev->id.product = 0x0001;182input_dev->id.version = 0x0100;183input_dev->dev.parent = &serio->dev;184185input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);186187for (i = 0; i < 6; i++)188set_bit(spaceorb_buttons[i], input_dev->keybit);189190for (i = 0; i < 6; i++)191input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0);192193serio_set_drvdata(serio, spaceorb);194195err = serio_open(serio, drv);196if (err)197goto fail2;198199err = input_register_device(spaceorb->dev);200if (err)201goto fail3;202203return 0;204205fail3: serio_close(serio);206fail2: serio_set_drvdata(serio, NULL);207fail1: input_free_device(input_dev);208kfree(spaceorb);209return err;210}211212/*213* The serio driver structure.214*/215216static struct serio_device_id spaceorb_serio_ids[] = {217{218.type = SERIO_RS232,219.proto = SERIO_SPACEORB,220.id = SERIO_ANY,221.extra = SERIO_ANY,222},223{ 0 }224};225226MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);227228static struct serio_driver spaceorb_drv = {229.driver = {230.name = "spaceorb",231},232.description = DRIVER_DESC,233.id_table = spaceorb_serio_ids,234.interrupt = spaceorb_interrupt,235.connect = spaceorb_connect,236.disconnect = spaceorb_disconnect,237};238239/*240* The functions for inserting/removing us as a module.241*/242243static int __init spaceorb_init(void)244{245return serio_register_driver(&spaceorb_drv);246}247248static void __exit spaceorb_exit(void)249{250serio_unregister_driver(&spaceorb_drv);251}252253module_init(spaceorb_init);254module_exit(spaceorb_exit);255256257