Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
0004  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
0005  *
0006  *  USB/RS232 I-Force joysticks and wheels.
0007  */
0008 
0009 #include <asm/unaligned.h>
0010 #include "iforce.h"
0011 
0012 static struct {
0013     __s32 x;
0014     __s32 y;
0015 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
0016 
0017 
0018 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
0019 {
0020     dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
0021         __func__, msg, cmd, LO(cmd), data);
0022 }
0023 
0024 /*
0025  * Send a packet of bytes to the device
0026  */
0027 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
0028 {
0029     /* Copy data to buffer */
0030     int n = LO(cmd);
0031     int c;
0032     int empty;
0033     int head, tail;
0034     unsigned long flags;
0035 
0036 /*
0037  * Update head and tail of xmit buffer
0038  */
0039     spin_lock_irqsave(&iforce->xmit_lock, flags);
0040 
0041     head = iforce->xmit.head;
0042     tail = iforce->xmit.tail;
0043 
0044 
0045     if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
0046         dev_warn(&iforce->dev->dev,
0047              "not enough space in xmit buffer to send new packet\n");
0048         spin_unlock_irqrestore(&iforce->xmit_lock, flags);
0049         return -1;
0050     }
0051 
0052     empty = head == tail;
0053     XMIT_INC(iforce->xmit.head, n+2);
0054 
0055 /*
0056  * Store packet in xmit buffer
0057  */
0058     iforce->xmit.buf[head] = HI(cmd);
0059     XMIT_INC(head, 1);
0060     iforce->xmit.buf[head] = LO(cmd);
0061     XMIT_INC(head, 1);
0062 
0063     c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
0064     if (n < c) c=n;
0065 
0066     memcpy(&iforce->xmit.buf[head],
0067            data,
0068            c);
0069     if (n != c) {
0070         memcpy(&iforce->xmit.buf[0],
0071                data + c,
0072                n - c);
0073     }
0074     XMIT_INC(head, n);
0075 
0076     spin_unlock_irqrestore(&iforce->xmit_lock, flags);
0077 /*
0078  * If necessary, start the transmission
0079  */
0080     if (empty)
0081         iforce->xport_ops->xmit(iforce);
0082 
0083     return 0;
0084 }
0085 EXPORT_SYMBOL(iforce_send_packet);
0086 
0087 /* Start or stop an effect */
0088 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
0089 {
0090     unsigned char data[3];
0091 
0092     data[0] = LO(id);
0093     data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
0094     data[2] = LO(value);
0095     return iforce_send_packet(iforce, FF_CMD_PLAY, data);
0096 }
0097 
0098 /* Mark an effect that was being updated as ready. That means it can be updated
0099  * again */
0100 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
0101 {
0102     int i;
0103 
0104     if (!iforce->dev->ff)
0105         return 0;
0106 
0107     for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
0108         if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
0109             (iforce->core_effects[i].mod1_chunk.start == addr ||
0110              iforce->core_effects[i].mod2_chunk.start == addr)) {
0111             clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
0112             return 0;
0113         }
0114     }
0115     dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
0116     return -1;
0117 }
0118 
0119 static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
0120 {
0121     struct input_dev *dev = iforce->dev;
0122     int i;
0123 
0124     input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
0125     input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
0126 
0127     for (i = 0; iforce->type->btn[i] >= 0; i++)
0128         input_report_key(dev, iforce->type->btn[i],
0129                  data[(i >> 3) + 5] & (1 << (i & 7)));
0130 
0131     /* If there are untouched bits left, interpret them as the second hat */
0132     if (i <= 8) {
0133         u8 btns = data[6];
0134 
0135         if (test_bit(ABS_HAT1X, dev->absbit)) {
0136             if (btns & BIT(3))
0137                 input_report_abs(dev, ABS_HAT1X, -1);
0138             else if (btns & BIT(1))
0139                 input_report_abs(dev, ABS_HAT1X, 1);
0140             else
0141                 input_report_abs(dev, ABS_HAT1X, 0);
0142         }
0143 
0144         if (test_bit(ABS_HAT1Y, dev->absbit)) {
0145             if (btns & BIT(0))
0146                 input_report_abs(dev, ABS_HAT1Y, -1);
0147             else if (btns & BIT(2))
0148                 input_report_abs(dev, ABS_HAT1Y, 1);
0149             else
0150                 input_report_abs(dev, ABS_HAT1Y, 0);
0151         }
0152     }
0153 }
0154 
0155 void iforce_process_packet(struct iforce *iforce,
0156                u8 packet_id, u8 *data, size_t len)
0157 {
0158     struct input_dev *dev = iforce->dev;
0159     int i, j;
0160 
0161     switch (packet_id) {
0162 
0163     case 0x01:  /* joystick position data */
0164         input_report_abs(dev, ABS_X,
0165                  (__s16) get_unaligned_le16(data));
0166         input_report_abs(dev, ABS_Y,
0167                  (__s16) get_unaligned_le16(data + 2));
0168         input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
0169 
0170         if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
0171             input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
0172 
0173         iforce_report_hats_buttons(iforce, data);
0174 
0175         input_sync(dev);
0176         break;
0177 
0178     case 0x03:  /* wheel position data */
0179         input_report_abs(dev, ABS_WHEEL,
0180                  (__s16) get_unaligned_le16(data));
0181         input_report_abs(dev, ABS_GAS,   255 - data[2]);
0182         input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
0183 
0184         iforce_report_hats_buttons(iforce, data);
0185 
0186         input_sync(dev);
0187         break;
0188 
0189     case 0x02:  /* status report */
0190         input_report_key(dev, BTN_DEAD, data[0] & 0x02);
0191         input_sync(dev);
0192 
0193         /* Check if an effect was just started or stopped */
0194         i = data[1] & 0x7f;
0195         if (data[1] & 0x80) {
0196             if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
0197                 /* Report play event */
0198                 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
0199             }
0200         } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
0201             /* Report stop event */
0202             input_report_ff_status(dev, i, FF_STATUS_STOPPED);
0203         }
0204 
0205         for (j = 3; j < len; j += 2)
0206             mark_core_as_ready(iforce, get_unaligned_le16(data + j));
0207 
0208         break;
0209     }
0210 }
0211 EXPORT_SYMBOL(iforce_process_packet);