| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> |
|---|
| 3 | 4 | * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> |
|---|
| .. | .. |
|---|
| 5 | 6 | * USB/RS232 I-Force joysticks and wheels. |
|---|
| 6 | 7 | */ |
|---|
| 7 | 8 | |
|---|
| 8 | | -/* |
|---|
| 9 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 10 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 11 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 12 | | - * (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 of |
|---|
| 16 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | | - * GNU General Public License for more details. |
|---|
| 18 | | - * |
|---|
| 19 | | - * You should have received a copy of the GNU General Public License |
|---|
| 20 | | - * along with this program; if not, write to the Free Software |
|---|
| 21 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 22 | | - */ |
|---|
| 23 | | - |
|---|
| 9 | +#include <asm/unaligned.h> |
|---|
| 24 | 10 | #include "iforce.h" |
|---|
| 25 | 11 | |
|---|
| 26 | 12 | static struct { |
|---|
| .. | .. |
|---|
| 91 | 77 | /* |
|---|
| 92 | 78 | * If necessary, start the transmission |
|---|
| 93 | 79 | */ |
|---|
| 94 | | - switch (iforce->bus) { |
|---|
| 80 | + if (empty) |
|---|
| 81 | + iforce->xport_ops->xmit(iforce); |
|---|
| 95 | 82 | |
|---|
| 96 | | -#ifdef CONFIG_JOYSTICK_IFORCE_232 |
|---|
| 97 | | - case IFORCE_232: |
|---|
| 98 | | - if (empty) |
|---|
| 99 | | - iforce_serial_xmit(iforce); |
|---|
| 100 | | - break; |
|---|
| 101 | | -#endif |
|---|
| 102 | | -#ifdef CONFIG_JOYSTICK_IFORCE_USB |
|---|
| 103 | | - case IFORCE_USB: |
|---|
| 104 | | - |
|---|
| 105 | | - if (iforce->usbdev && empty && |
|---|
| 106 | | - !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) { |
|---|
| 107 | | - |
|---|
| 108 | | - iforce_usb_xmit(iforce); |
|---|
| 109 | | - } |
|---|
| 110 | | - break; |
|---|
| 111 | | -#endif |
|---|
| 112 | | - } |
|---|
| 113 | 83 | return 0; |
|---|
| 114 | 84 | } |
|---|
| 85 | +EXPORT_SYMBOL(iforce_send_packet); |
|---|
| 115 | 86 | |
|---|
| 116 | 87 | /* Start or stop an effect */ |
|---|
| 117 | 88 | int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value) |
|---|
| .. | .. |
|---|
| 145 | 116 | return -1; |
|---|
| 146 | 117 | } |
|---|
| 147 | 118 | |
|---|
| 148 | | -void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data) |
|---|
| 119 | +static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data) |
|---|
| 149 | 120 | { |
|---|
| 150 | 121 | struct input_dev *dev = iforce->dev; |
|---|
| 151 | 122 | int i; |
|---|
| 152 | | - static int being_used = 0; |
|---|
| 153 | 123 | |
|---|
| 154 | | - if (being_used) |
|---|
| 155 | | - dev_warn(&iforce->dev->dev, |
|---|
| 156 | | - "re-entrant call to iforce_process %d\n", being_used); |
|---|
| 157 | | - being_used++; |
|---|
| 124 | + input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x); |
|---|
| 125 | + input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y); |
|---|
| 158 | 126 | |
|---|
| 159 | | -#ifdef CONFIG_JOYSTICK_IFORCE_232 |
|---|
| 160 | | - if (HI(iforce->expect_packet) == HI(cmd)) { |
|---|
| 161 | | - iforce->expect_packet = 0; |
|---|
| 162 | | - iforce->ecmd = cmd; |
|---|
| 163 | | - memcpy(iforce->edata, data, IFORCE_MAX_LENGTH); |
|---|
| 127 | + for (i = 0; iforce->type->btn[i] >= 0; i++) |
|---|
| 128 | + input_report_key(dev, iforce->type->btn[i], |
|---|
| 129 | + data[(i >> 3) + 5] & (1 << (i & 7))); |
|---|
| 130 | + |
|---|
| 131 | + /* If there are untouched bits left, interpret them as the second hat */ |
|---|
| 132 | + if (i <= 8) { |
|---|
| 133 | + u8 btns = data[6]; |
|---|
| 134 | + |
|---|
| 135 | + if (test_bit(ABS_HAT1X, dev->absbit)) { |
|---|
| 136 | + if (btns & BIT(3)) |
|---|
| 137 | + input_report_abs(dev, ABS_HAT1X, -1); |
|---|
| 138 | + else if (btns & BIT(1)) |
|---|
| 139 | + input_report_abs(dev, ABS_HAT1X, 1); |
|---|
| 140 | + else |
|---|
| 141 | + input_report_abs(dev, ABS_HAT1X, 0); |
|---|
| 142 | + } |
|---|
| 143 | + |
|---|
| 144 | + if (test_bit(ABS_HAT1Y, dev->absbit)) { |
|---|
| 145 | + if (btns & BIT(0)) |
|---|
| 146 | + input_report_abs(dev, ABS_HAT1Y, -1); |
|---|
| 147 | + else if (btns & BIT(2)) |
|---|
| 148 | + input_report_abs(dev, ABS_HAT1Y, 1); |
|---|
| 149 | + else |
|---|
| 150 | + input_report_abs(dev, ABS_HAT1Y, 0); |
|---|
| 151 | + } |
|---|
| 164 | 152 | } |
|---|
| 165 | | -#endif |
|---|
| 166 | | - wake_up(&iforce->wait); |
|---|
| 167 | | - |
|---|
| 168 | | - if (!iforce->type) { |
|---|
| 169 | | - being_used--; |
|---|
| 170 | | - return; |
|---|
| 171 | | - } |
|---|
| 172 | | - |
|---|
| 173 | | - switch (HI(cmd)) { |
|---|
| 174 | | - |
|---|
| 175 | | - case 0x01: /* joystick position data */ |
|---|
| 176 | | - case 0x03: /* wheel position data */ |
|---|
| 177 | | - if (HI(cmd) == 1) { |
|---|
| 178 | | - input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0])); |
|---|
| 179 | | - input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2])); |
|---|
| 180 | | - input_report_abs(dev, ABS_THROTTLE, 255 - data[4]); |
|---|
| 181 | | - if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit)) |
|---|
| 182 | | - input_report_abs(dev, ABS_RUDDER, (__s8)data[7]); |
|---|
| 183 | | - } else { |
|---|
| 184 | | - input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0])); |
|---|
| 185 | | - input_report_abs(dev, ABS_GAS, 255 - data[2]); |
|---|
| 186 | | - input_report_abs(dev, ABS_BRAKE, 255 - data[3]); |
|---|
| 187 | | - } |
|---|
| 188 | | - |
|---|
| 189 | | - input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x); |
|---|
| 190 | | - input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y); |
|---|
| 191 | | - |
|---|
| 192 | | - for (i = 0; iforce->type->btn[i] >= 0; i++) |
|---|
| 193 | | - input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7))); |
|---|
| 194 | | - |
|---|
| 195 | | - /* If there are untouched bits left, interpret them as the second hat */ |
|---|
| 196 | | - if (i <= 8) { |
|---|
| 197 | | - int btns = data[6]; |
|---|
| 198 | | - if (test_bit(ABS_HAT1X, dev->absbit)) { |
|---|
| 199 | | - if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1); |
|---|
| 200 | | - else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1); |
|---|
| 201 | | - else input_report_abs(dev, ABS_HAT1X, 0); |
|---|
| 202 | | - } |
|---|
| 203 | | - if (test_bit(ABS_HAT1Y, dev->absbit)) { |
|---|
| 204 | | - if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1); |
|---|
| 205 | | - else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1); |
|---|
| 206 | | - else input_report_abs(dev, ABS_HAT1Y, 0); |
|---|
| 207 | | - } |
|---|
| 208 | | - } |
|---|
| 209 | | - |
|---|
| 210 | | - input_sync(dev); |
|---|
| 211 | | - |
|---|
| 212 | | - break; |
|---|
| 213 | | - |
|---|
| 214 | | - case 0x02: /* status report */ |
|---|
| 215 | | - input_report_key(dev, BTN_DEAD, data[0] & 0x02); |
|---|
| 216 | | - input_sync(dev); |
|---|
| 217 | | - |
|---|
| 218 | | - /* Check if an effect was just started or stopped */ |
|---|
| 219 | | - i = data[1] & 0x7f; |
|---|
| 220 | | - if (data[1] & 0x80) { |
|---|
| 221 | | - if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { |
|---|
| 222 | | - /* Report play event */ |
|---|
| 223 | | - input_report_ff_status(dev, i, FF_STATUS_PLAYING); |
|---|
| 224 | | - } |
|---|
| 225 | | - } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { |
|---|
| 226 | | - /* Report stop event */ |
|---|
| 227 | | - input_report_ff_status(dev, i, FF_STATUS_STOPPED); |
|---|
| 228 | | - } |
|---|
| 229 | | - if (LO(cmd) > 3) { |
|---|
| 230 | | - int j; |
|---|
| 231 | | - for (j = 3; j < LO(cmd); j += 2) |
|---|
| 232 | | - mark_core_as_ready(iforce, data[j] | (data[j+1]<<8)); |
|---|
| 233 | | - } |
|---|
| 234 | | - break; |
|---|
| 235 | | - } |
|---|
| 236 | | - being_used--; |
|---|
| 237 | 153 | } |
|---|
| 238 | 154 | |
|---|
| 239 | | -int iforce_get_id_packet(struct iforce *iforce, char *packet) |
|---|
| 155 | +void iforce_process_packet(struct iforce *iforce, |
|---|
| 156 | + u8 packet_id, u8 *data, size_t len) |
|---|
| 240 | 157 | { |
|---|
| 241 | | - switch (iforce->bus) { |
|---|
| 158 | + struct input_dev *dev = iforce->dev; |
|---|
| 159 | + int i, j; |
|---|
| 242 | 160 | |
|---|
| 243 | | - case IFORCE_USB: { |
|---|
| 244 | | -#ifdef CONFIG_JOYSTICK_IFORCE_USB |
|---|
| 245 | | - int status; |
|---|
| 161 | + switch (packet_id) { |
|---|
| 246 | 162 | |
|---|
| 247 | | - iforce->cr.bRequest = packet[0]; |
|---|
| 248 | | - iforce->ctrl->dev = iforce->usbdev; |
|---|
| 163 | + case 0x01: /* joystick position data */ |
|---|
| 164 | + input_report_abs(dev, ABS_X, |
|---|
| 165 | + (__s16) get_unaligned_le16(data)); |
|---|
| 166 | + input_report_abs(dev, ABS_Y, |
|---|
| 167 | + (__s16) get_unaligned_le16(data + 2)); |
|---|
| 168 | + input_report_abs(dev, ABS_THROTTLE, 255 - data[4]); |
|---|
| 249 | 169 | |
|---|
| 250 | | - status = usb_submit_urb(iforce->ctrl, GFP_KERNEL); |
|---|
| 251 | | - if (status) { |
|---|
| 252 | | - dev_err(&iforce->intf->dev, |
|---|
| 253 | | - "usb_submit_urb failed %d\n", status); |
|---|
| 254 | | - return -1; |
|---|
| 255 | | - } |
|---|
| 170 | + if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit)) |
|---|
| 171 | + input_report_abs(dev, ABS_RUDDER, (__s8)data[7]); |
|---|
| 256 | 172 | |
|---|
| 257 | | - wait_event_interruptible_timeout(iforce->wait, |
|---|
| 258 | | - iforce->ctrl->status != -EINPROGRESS, HZ); |
|---|
| 173 | + iforce_report_hats_buttons(iforce, data); |
|---|
| 259 | 174 | |
|---|
| 260 | | - if (iforce->ctrl->status) { |
|---|
| 261 | | - dev_dbg(&iforce->intf->dev, |
|---|
| 262 | | - "iforce->ctrl->status = %d\n", |
|---|
| 263 | | - iforce->ctrl->status); |
|---|
| 264 | | - usb_unlink_urb(iforce->ctrl); |
|---|
| 265 | | - return -1; |
|---|
| 266 | | - } |
|---|
| 267 | | -#else |
|---|
| 268 | | - printk(KERN_DEBUG "iforce_get_id_packet: iforce->bus = USB!\n"); |
|---|
| 269 | | -#endif |
|---|
| 270 | | - } |
|---|
| 175 | + input_sync(dev); |
|---|
| 271 | 176 | break; |
|---|
| 272 | 177 | |
|---|
| 273 | | - case IFORCE_232: |
|---|
| 178 | + case 0x03: /* wheel position data */ |
|---|
| 179 | + input_report_abs(dev, ABS_WHEEL, |
|---|
| 180 | + (__s16) get_unaligned_le16(data)); |
|---|
| 181 | + input_report_abs(dev, ABS_GAS, 255 - data[2]); |
|---|
| 182 | + input_report_abs(dev, ABS_BRAKE, 255 - data[3]); |
|---|
| 274 | 183 | |
|---|
| 275 | | -#ifdef CONFIG_JOYSTICK_IFORCE_232 |
|---|
| 276 | | - iforce->expect_packet = FF_CMD_QUERY; |
|---|
| 277 | | - iforce_send_packet(iforce, FF_CMD_QUERY, packet); |
|---|
| 184 | + iforce_report_hats_buttons(iforce, data); |
|---|
| 278 | 185 | |
|---|
| 279 | | - wait_event_interruptible_timeout(iforce->wait, |
|---|
| 280 | | - !iforce->expect_packet, HZ); |
|---|
| 281 | | - |
|---|
| 282 | | - if (iforce->expect_packet) { |
|---|
| 283 | | - iforce->expect_packet = 0; |
|---|
| 284 | | - return -1; |
|---|
| 285 | | - } |
|---|
| 286 | | -#else |
|---|
| 287 | | - dev_err(&iforce->dev->dev, |
|---|
| 288 | | - "iforce_get_id_packet: iforce->bus = SERIO!\n"); |
|---|
| 289 | | -#endif |
|---|
| 186 | + input_sync(dev); |
|---|
| 290 | 187 | break; |
|---|
| 291 | 188 | |
|---|
| 292 | | - default: |
|---|
| 293 | | - dev_err(&iforce->dev->dev, |
|---|
| 294 | | - "iforce_get_id_packet: iforce->bus = %d\n", |
|---|
| 295 | | - iforce->bus); |
|---|
| 189 | + case 0x02: /* status report */ |
|---|
| 190 | + input_report_key(dev, BTN_DEAD, data[0] & 0x02); |
|---|
| 191 | + input_sync(dev); |
|---|
| 192 | + |
|---|
| 193 | + /* Check if an effect was just started or stopped */ |
|---|
| 194 | + i = data[1] & 0x7f; |
|---|
| 195 | + if (data[1] & 0x80) { |
|---|
| 196 | + if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { |
|---|
| 197 | + /* Report play event */ |
|---|
| 198 | + input_report_ff_status(dev, i, FF_STATUS_PLAYING); |
|---|
| 199 | + } |
|---|
| 200 | + } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { |
|---|
| 201 | + /* Report stop event */ |
|---|
| 202 | + input_report_ff_status(dev, i, FF_STATUS_STOPPED); |
|---|
| 203 | + } |
|---|
| 204 | + |
|---|
| 205 | + for (j = 3; j < len; j += 2) |
|---|
| 206 | + mark_core_as_ready(iforce, get_unaligned_le16(data + j)); |
|---|
| 207 | + |
|---|
| 296 | 208 | break; |
|---|
| 297 | 209 | } |
|---|
| 298 | | - |
|---|
| 299 | | - return -(iforce->edata[0] != packet[0]); |
|---|
| 300 | 210 | } |
|---|
| 301 | | - |
|---|
| 211 | +EXPORT_SYMBOL(iforce_process_packet); |
|---|