Path: blob/master/drivers/input/joystick/iforce/iforce-packets.c
15115 views
/*1* Copyright (c) 2000-2002 Vojtech Pavlik <[email protected]>2* Copyright (c) 2001-2002, 2007 Johann Deneux <[email protected]>3*4* USB/RS232 I-Force joysticks and wheels.5*/67/*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA21*22* Should you need to contact me, the author, you can do so either by23* e-mail - mail your message to <[email protected]>, or by paper mail:24* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic25*/2627#include "iforce.h"2829static struct {30__s32 x;31__s32 y;32} iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};333435void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)36{37int i;3839printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);40for (i = 0; i < LO(cmd); i++)41printk("%02x ", data[i]);42printk("\n");43}4445/*46* Send a packet of bytes to the device47*/48int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)49{50/* Copy data to buffer */51int n = LO(cmd);52int c;53int empty;54int head, tail;55unsigned long flags;5657/*58* Update head and tail of xmit buffer59*/60spin_lock_irqsave(&iforce->xmit_lock, flags);6162head = iforce->xmit.head;63tail = iforce->xmit.tail;646566if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {67dev_warn(&iforce->dev->dev,68"not enough space in xmit buffer to send new packet\n");69spin_unlock_irqrestore(&iforce->xmit_lock, flags);70return -1;71}7273empty = head == tail;74XMIT_INC(iforce->xmit.head, n+2);7576/*77* Store packet in xmit buffer78*/79iforce->xmit.buf[head] = HI(cmd);80XMIT_INC(head, 1);81iforce->xmit.buf[head] = LO(cmd);82XMIT_INC(head, 1);8384c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);85if (n < c) c=n;8687memcpy(&iforce->xmit.buf[head],88data,89c);90if (n != c) {91memcpy(&iforce->xmit.buf[0],92data + c,93n - c);94}95XMIT_INC(head, n);9697spin_unlock_irqrestore(&iforce->xmit_lock, flags);98/*99* If necessary, start the transmission100*/101switch (iforce->bus) {102103#ifdef CONFIG_JOYSTICK_IFORCE_232104case IFORCE_232:105if (empty)106iforce_serial_xmit(iforce);107break;108#endif109#ifdef CONFIG_JOYSTICK_IFORCE_USB110case IFORCE_USB:111112if (iforce->usbdev && empty &&113!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {114115iforce_usb_xmit(iforce);116}117break;118#endif119}120return 0;121}122123/* Start or stop an effect */124int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)125{126unsigned char data[3];127128data[0] = LO(id);129data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;130data[2] = LO(value);131return iforce_send_packet(iforce, FF_CMD_PLAY, data);132}133134/* Mark an effect that was being updated as ready. That means it can be updated135* again */136static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)137{138int i;139140if (!iforce->dev->ff)141return 0;142143for (i = 0; i < iforce->dev->ff->max_effects; ++i) {144if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&145(iforce->core_effects[i].mod1_chunk.start == addr ||146iforce->core_effects[i].mod2_chunk.start == addr)) {147clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);148return 0;149}150}151dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);152return -1;153}154155void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)156{157struct input_dev *dev = iforce->dev;158int i;159static int being_used = 0;160161if (being_used)162dev_warn(&iforce->dev->dev,163"re-entrant call to iforce_process %d\n", being_used);164being_used++;165166#ifdef CONFIG_JOYSTICK_IFORCE_232167if (HI(iforce->expect_packet) == HI(cmd)) {168iforce->expect_packet = 0;169iforce->ecmd = cmd;170memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);171}172#endif173wake_up(&iforce->wait);174175if (!iforce->type) {176being_used--;177return;178}179180switch (HI(cmd)) {181182case 0x01: /* joystick position data */183case 0x03: /* wheel position data */184if (HI(cmd) == 1) {185input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));186input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));187input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);188if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))189input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);190} else {191input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));192input_report_abs(dev, ABS_GAS, 255 - data[2]);193input_report_abs(dev, ABS_BRAKE, 255 - data[3]);194}195196input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);197input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);198199for (i = 0; iforce->type->btn[i] >= 0; i++)200input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));201202/* If there are untouched bits left, interpret them as the second hat */203if (i <= 8) {204int btns = data[6];205if (test_bit(ABS_HAT1X, dev->absbit)) {206if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);207else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);208else input_report_abs(dev, ABS_HAT1X, 0);209}210if (test_bit(ABS_HAT1Y, dev->absbit)) {211if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);212else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);213else input_report_abs(dev, ABS_HAT1Y, 0);214}215}216217input_sync(dev);218219break;220221case 0x02: /* status report */222input_report_key(dev, BTN_DEAD, data[0] & 0x02);223input_sync(dev);224225/* Check if an effect was just started or stopped */226i = data[1] & 0x7f;227if (data[1] & 0x80) {228if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {229/* Report play event */230input_report_ff_status(dev, i, FF_STATUS_PLAYING);231}232} else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {233/* Report stop event */234input_report_ff_status(dev, i, FF_STATUS_STOPPED);235}236if (LO(cmd) > 3) {237int j;238for (j = 3; j < LO(cmd); j += 2)239mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));240}241break;242}243being_used--;244}245246int iforce_get_id_packet(struct iforce *iforce, char *packet)247{248switch (iforce->bus) {249250case IFORCE_USB: {251#ifdef CONFIG_JOYSTICK_IFORCE_USB252int status;253254iforce->cr.bRequest = packet[0];255iforce->ctrl->dev = iforce->usbdev;256257status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);258if (status) {259err("usb_submit_urb failed %d", status);260return -1;261}262263wait_event_interruptible_timeout(iforce->wait,264iforce->ctrl->status != -EINPROGRESS, HZ);265266if (iforce->ctrl->status) {267dbg("iforce->ctrl->status = %d", iforce->ctrl->status);268usb_unlink_urb(iforce->ctrl);269return -1;270}271#else272dbg("iforce_get_id_packet: iforce->bus = USB!");273#endif274}275break;276277case IFORCE_232:278279#ifdef CONFIG_JOYSTICK_IFORCE_232280iforce->expect_packet = FF_CMD_QUERY;281iforce_send_packet(iforce, FF_CMD_QUERY, packet);282283wait_event_interruptible_timeout(iforce->wait,284!iforce->expect_packet, HZ);285286if (iforce->expect_packet) {287iforce->expect_packet = 0;288return -1;289}290#else291err("iforce_get_id_packet: iforce->bus = SERIO!");292#endif293break;294295default:296err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);297break;298}299300return -(iforce->edata[0] != packet[0]);301}302303304305