11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> 4598972d4SJohann Deneux * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * USB/RS232 I-Force joysticks and wheels. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 9*5f60d5f6SAl Viro #include <linux/unaligned.h> 101da177e4SLinus Torvalds #include "iforce.h" 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds static struct { 131da177e4SLinus Torvalds __s32 x; 141da177e4SLinus Torvalds __s32 y; 151da177e4SLinus Torvalds } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds 18305180bcSTim Schumacher void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data) 191da177e4SLinus Torvalds { 20305180bcSTim Schumacher dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n", 21305180bcSTim Schumacher __func__, msg, cmd, LO(cmd), data); 221da177e4SLinus Torvalds } 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds /* 251da177e4SLinus Torvalds * Send a packet of bytes to the device 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data) 281da177e4SLinus Torvalds { 291da177e4SLinus Torvalds /* Copy data to buffer */ 301da177e4SLinus Torvalds int n = LO(cmd); 311da177e4SLinus Torvalds int c; 321da177e4SLinus Torvalds int empty; 331da177e4SLinus Torvalds int head, tail; 341da177e4SLinus Torvalds unsigned long flags; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds /* 371da177e4SLinus Torvalds * Update head and tail of xmit buffer 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds spin_lock_irqsave(&iforce->xmit_lock, flags); 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds head = iforce->xmit.head; 421da177e4SLinus Torvalds tail = iforce->xmit.tail; 431da177e4SLinus Torvalds 44598972d4SJohann Deneux 451da177e4SLinus Torvalds if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) { 461817b169SGreg Kroah-Hartman dev_warn(&iforce->dev->dev, 471817b169SGreg Kroah-Hartman "not enough space in xmit buffer to send new packet\n"); 481da177e4SLinus Torvalds spin_unlock_irqrestore(&iforce->xmit_lock, flags); 491da177e4SLinus Torvalds return -1; 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds empty = head == tail; 531da177e4SLinus Torvalds XMIT_INC(iforce->xmit.head, n+2); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* 561da177e4SLinus Torvalds * Store packet in xmit buffer 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds iforce->xmit.buf[head] = HI(cmd); 591da177e4SLinus Torvalds XMIT_INC(head, 1); 601da177e4SLinus Torvalds iforce->xmit.buf[head] = LO(cmd); 611da177e4SLinus Torvalds XMIT_INC(head, 1); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE); 641da177e4SLinus Torvalds if (n < c) c=n; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds memcpy(&iforce->xmit.buf[head], 671da177e4SLinus Torvalds data, 681da177e4SLinus Torvalds c); 691da177e4SLinus Torvalds if (n != c) { 701da177e4SLinus Torvalds memcpy(&iforce->xmit.buf[0], 711da177e4SLinus Torvalds data + c, 721da177e4SLinus Torvalds n - c); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds XMIT_INC(head, n); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds spin_unlock_irqrestore(&iforce->xmit_lock, flags); 771da177e4SLinus Torvalds /* 781da177e4SLinus Torvalds * If necessary, start the transmission 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds if (empty) 8138d10769SDmitry Torokhov iforce->xport_ops->xmit(iforce); 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds return 0; 841da177e4SLinus Torvalds } 854f99de6dSDmitry Torokhov EXPORT_SYMBOL(iforce_send_packet); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* Start or stop an effect */ 881da177e4SLinus Torvalds int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds unsigned char data[3]; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds data[0] = LO(id); 931da177e4SLinus Torvalds data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0; 941da177e4SLinus Torvalds data[2] = LO(value); 951da177e4SLinus Torvalds return iforce_send_packet(iforce, FF_CMD_PLAY, data); 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* Mark an effect that was being updated as ready. That means it can be updated 991da177e4SLinus Torvalds * again */ 1001da177e4SLinus Torvalds static int mark_core_as_ready(struct iforce *iforce, unsigned short addr) 1011da177e4SLinus Torvalds { 1021da177e4SLinus Torvalds int i; 10317dd3f0fSDmitry Torokhov 104f6a01c85SAnssi Hannula if (!iforce->dev->ff) 105f6a01c85SAnssi Hannula return 0; 106f6a01c85SAnssi Hannula 107f6a01c85SAnssi Hannula for (i = 0; i < iforce->dev->ff->max_effects; ++i) { 1081da177e4SLinus Torvalds if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) && 1091da177e4SLinus Torvalds (iforce->core_effects[i].mod1_chunk.start == addr || 1101da177e4SLinus Torvalds iforce->core_effects[i].mod2_chunk.start == addr)) { 1111da177e4SLinus Torvalds clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags); 1121da177e4SLinus Torvalds return 0; 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds } 1151817b169SGreg Kroah-Hartman dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr); 1161da177e4SLinus Torvalds return -1; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1198a25e058SDmitry Torokhov static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data) 1201da177e4SLinus Torvalds { 12117dd3f0fSDmitry Torokhov struct input_dev *dev = iforce->dev; 1221da177e4SLinus Torvalds int i; 1231da177e4SLinus Torvalds 1248a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x); 1258a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y); 1268a25e058SDmitry Torokhov 1278a25e058SDmitry Torokhov for (i = 0; iforce->type->btn[i] >= 0; i++) 1288a25e058SDmitry Torokhov input_report_key(dev, iforce->type->btn[i], 1298a25e058SDmitry Torokhov data[(i >> 3) + 5] & (1 << (i & 7))); 1308a25e058SDmitry Torokhov 1318a25e058SDmitry Torokhov /* If there are untouched bits left, interpret them as the second hat */ 1328a25e058SDmitry Torokhov if (i <= 8) { 1338a25e058SDmitry Torokhov u8 btns = data[6]; 1348a25e058SDmitry Torokhov 1358a25e058SDmitry Torokhov if (test_bit(ABS_HAT1X, dev->absbit)) { 1368a25e058SDmitry Torokhov if (btns & BIT(3)) 1378a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT1X, -1); 1388a25e058SDmitry Torokhov else if (btns & BIT(1)) 1398a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT1X, 1); 1408a25e058SDmitry Torokhov else 1418a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT1X, 0); 1428a25e058SDmitry Torokhov } 1438a25e058SDmitry Torokhov 1448a25e058SDmitry Torokhov if (test_bit(ABS_HAT1Y, dev->absbit)) { 1458a25e058SDmitry Torokhov if (btns & BIT(0)) 1468a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT1Y, -1); 1478a25e058SDmitry Torokhov else if (btns & BIT(2)) 1488a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT1Y, 1); 1498a25e058SDmitry Torokhov else 1508a25e058SDmitry Torokhov input_report_abs(dev, ABS_HAT1Y, 0); 1518a25e058SDmitry Torokhov } 1528a25e058SDmitry Torokhov } 1538a25e058SDmitry Torokhov } 1548a25e058SDmitry Torokhov 155d3cc1000SDmitry Torokhov void iforce_process_packet(struct iforce *iforce, 156d3cc1000SDmitry Torokhov u8 packet_id, u8 *data, size_t len) 1578a25e058SDmitry Torokhov { 1588a25e058SDmitry Torokhov struct input_dev *dev = iforce->dev; 1598a25e058SDmitry Torokhov int i, j; 1608a25e058SDmitry Torokhov 161d3cc1000SDmitry Torokhov switch (packet_id) { 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds case 0x01: /* joystick position data */ 16421ae38f8SDmitry Torokhov input_report_abs(dev, ABS_X, 16521ae38f8SDmitry Torokhov (__s16) get_unaligned_le16(data)); 16621ae38f8SDmitry Torokhov input_report_abs(dev, ABS_Y, 16721ae38f8SDmitry Torokhov (__s16) get_unaligned_le16(data + 2)); 1681da177e4SLinus Torvalds input_report_abs(dev, ABS_THROTTLE, 255 - data[4]); 1698a25e058SDmitry Torokhov 170d3cc1000SDmitry Torokhov if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit)) 1711da177e4SLinus Torvalds input_report_abs(dev, ABS_RUDDER, (__s8)data[7]); 1728a25e058SDmitry Torokhov 1738a25e058SDmitry Torokhov iforce_report_hats_buttons(iforce, data); 1748a25e058SDmitry Torokhov 1758a25e058SDmitry Torokhov input_sync(dev); 1768a25e058SDmitry Torokhov break; 1778a25e058SDmitry Torokhov 1788a25e058SDmitry Torokhov case 0x03: /* wheel position data */ 17921ae38f8SDmitry Torokhov input_report_abs(dev, ABS_WHEEL, 18021ae38f8SDmitry Torokhov (__s16) get_unaligned_le16(data)); 1811da177e4SLinus Torvalds input_report_abs(dev, ABS_GAS, 255 - data[2]); 1821da177e4SLinus Torvalds input_report_abs(dev, ABS_BRAKE, 255 - data[3]); 1831da177e4SLinus Torvalds 1848a25e058SDmitry Torokhov iforce_report_hats_buttons(iforce, data); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds input_sync(dev); 1871da177e4SLinus Torvalds break; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds case 0x02: /* status report */ 1901da177e4SLinus Torvalds input_report_key(dev, BTN_DEAD, data[0] & 0x02); 1911da177e4SLinus Torvalds input_sync(dev); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* Check if an effect was just started or stopped */ 1941da177e4SLinus Torvalds i = data[1] & 0x7f; 1951da177e4SLinus Torvalds if (data[1] & 0x80) { 1961da177e4SLinus Torvalds if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { 1971da177e4SLinus Torvalds /* Report play event */ 1981da177e4SLinus Torvalds input_report_ff_status(dev, i, FF_STATUS_PLAYING); 1991da177e4SLinus Torvalds } 200f6a01c85SAnssi Hannula } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { 2011da177e4SLinus Torvalds /* Report stop event */ 2021da177e4SLinus Torvalds input_report_ff_status(dev, i, FF_STATUS_STOPPED); 2031da177e4SLinus Torvalds } 2048a25e058SDmitry Torokhov 205d3cc1000SDmitry Torokhov for (j = 3; j < len; j += 2) 20621ae38f8SDmitry Torokhov mark_core_as_ready(iforce, get_unaligned_le16(data + j)); 2078a25e058SDmitry Torokhov 2081da177e4SLinus Torvalds break; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds } 2114f99de6dSDmitry Torokhov EXPORT_SYMBOL(iforce_process_packet); 212