2019-05-27 09:55:05 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@ucw.cz>
|
2007-04-12 09:30:24 +04:00
|
|
|
* Copyright (c) 2001, 2007 Johann Deneux <johann.deneux@gmail.com>
|
2005-04-17 02:20:36 +04:00
|
|
|
*
|
|
|
|
* USB/RS232 I-Force joysticks and wheels.
|
|
|
|
*/
|
|
|
|
|
2018-07-25 03:32:24 +03:00
|
|
|
#include <linux/serio.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "iforce.h"
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
struct iforce_serio {
|
|
|
|
struct iforce iforce;
|
|
|
|
|
|
|
|
struct serio *serio;
|
|
|
|
int idx, pkt, len, id;
|
|
|
|
u8 csum;
|
|
|
|
u8 expect_packet;
|
2018-08-10 03:28:35 +03:00
|
|
|
u8 cmd_response[IFORCE_MAX_LENGTH];
|
|
|
|
u8 cmd_response_len;
|
2018-08-10 23:44:52 +03:00
|
|
|
u8 data_in[IFORCE_MAX_LENGTH];
|
2018-08-04 02:27:45 +03:00
|
|
|
};
|
|
|
|
|
2018-07-27 03:36:36 +03:00
|
|
|
static void iforce_serio_xmit(struct iforce *iforce)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2018-08-04 02:27:45 +03:00
|
|
|
struct iforce_serio *iforce_serio = container_of(iforce,
|
|
|
|
struct iforce_serio,
|
|
|
|
iforce);
|
2005-04-17 02:20:36 +04:00
|
|
|
unsigned char cs;
|
|
|
|
int i;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
|
|
|
|
set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&iforce->xmit_lock, flags);
|
|
|
|
|
|
|
|
again:
|
|
|
|
if (iforce->xmit.head == iforce->xmit.tail) {
|
|
|
|
clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
|
|
|
|
spin_unlock_irqrestore(&iforce->xmit_lock, flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cs = 0x2b;
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
serio_write(iforce_serio->serio, 0x2b);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
serio_write(iforce_serio->serio, iforce->xmit.buf[iforce->xmit.tail]);
|
2005-04-17 02:20:36 +04:00
|
|
|
cs ^= iforce->xmit.buf[iforce->xmit.tail];
|
|
|
|
XMIT_INC(iforce->xmit.tail, 1);
|
|
|
|
|
|
|
|
for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) {
|
2018-08-04 02:27:45 +03:00
|
|
|
serio_write(iforce_serio->serio,
|
|
|
|
iforce->xmit.buf[iforce->xmit.tail]);
|
2005-04-17 02:20:36 +04:00
|
|
|
cs ^= iforce->xmit.buf[iforce->xmit.tail];
|
|
|
|
XMIT_INC(iforce->xmit.tail, 1);
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
serio_write(iforce_serio->serio, cs);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags))
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&iforce->xmit_lock, flags);
|
|
|
|
}
|
|
|
|
|
2018-08-10 03:28:35 +03:00
|
|
|
static int iforce_serio_get_id(struct iforce *iforce, u8 id,
|
|
|
|
u8 *response_data, size_t *response_len)
|
2018-07-27 03:49:34 +03:00
|
|
|
{
|
2018-08-04 02:27:45 +03:00
|
|
|
struct iforce_serio *iforce_serio = container_of(iforce,
|
|
|
|
struct iforce_serio,
|
|
|
|
iforce);
|
|
|
|
|
|
|
|
iforce_serio->expect_packet = HI(FF_CMD_QUERY);
|
2018-08-10 03:28:35 +03:00
|
|
|
iforce_serio->cmd_response_len = 0;
|
|
|
|
|
|
|
|
iforce_send_packet(iforce, FF_CMD_QUERY, &id);
|
2018-07-27 03:49:34 +03:00
|
|
|
|
|
|
|
wait_event_interruptible_timeout(iforce->wait,
|
2018-08-04 02:27:45 +03:00
|
|
|
!iforce_serio->expect_packet, HZ);
|
2018-07-27 03:49:34 +03:00
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
if (iforce_serio->expect_packet) {
|
|
|
|
iforce_serio->expect_packet = 0;
|
2018-08-10 03:28:35 +03:00
|
|
|
return -ETIMEDOUT;
|
2018-07-27 03:49:34 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 03:28:35 +03:00
|
|
|
if (iforce_serio->cmd_response[0] != id)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
memcpy(response_data, iforce_serio->cmd_response,
|
|
|
|
iforce_serio->cmd_response_len);
|
|
|
|
*response_len = iforce_serio->cmd_response_len;
|
|
|
|
|
|
|
|
return 0;
|
2018-07-27 03:49:34 +03:00
|
|
|
}
|
|
|
|
|
2018-08-04 01:26:00 +03:00
|
|
|
static int iforce_serio_start_io(struct iforce *iforce)
|
|
|
|
{
|
|
|
|
/* No special handling required */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iforce_serio_stop_io(struct iforce *iforce)
|
|
|
|
{
|
|
|
|
//TODO: Wait for the last packets to be sent
|
|
|
|
}
|
|
|
|
|
2018-07-27 03:36:36 +03:00
|
|
|
static const struct iforce_xport_ops iforce_serio_xport_ops = {
|
|
|
|
.xmit = iforce_serio_xmit,
|
2018-07-27 03:49:34 +03:00
|
|
|
.get_id = iforce_serio_get_id,
|
2018-08-04 01:26:00 +03:00
|
|
|
.start_io = iforce_serio_start_io,
|
|
|
|
.stop_io = iforce_serio_stop_io,
|
2018-07-27 03:36:36 +03:00
|
|
|
};
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
static void iforce_serio_write_wakeup(struct serio *serio)
|
|
|
|
{
|
|
|
|
struct iforce *iforce = serio_get_drvdata(serio);
|
|
|
|
|
2018-07-27 03:36:36 +03:00
|
|
|
iforce_serio_xmit(iforce);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t iforce_serio_irq(struct serio *serio,
|
2018-08-10 03:28:35 +03:00
|
|
|
unsigned char data, unsigned int flags)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2018-08-04 02:27:45 +03:00
|
|
|
struct iforce_serio *iforce_serio = serio_get_drvdata(serio);
|
|
|
|
struct iforce *iforce = &iforce_serio->iforce;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
if (!iforce_serio->pkt) {
|
2005-04-17 02:20:36 +04:00
|
|
|
if (data == 0x2b)
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->pkt = 1;
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
if (!iforce_serio->id) {
|
2005-04-17 02:20:36 +04:00
|
|
|
if (data > 3 && data != 0xff)
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->pkt = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
else
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->id = data;
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
if (!iforce_serio->len) {
|
2005-04-17 02:20:36 +04:00
|
|
|
if (data > IFORCE_MAX_LENGTH) {
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->pkt = 0;
|
|
|
|
iforce_serio->id = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->len = data;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
if (iforce_serio->idx < iforce_serio->len) {
|
2018-08-10 23:44:52 +03:00
|
|
|
iforce_serio->data_in[iforce_serio->idx++] = data;
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->csum += data;
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
if (iforce_serio->idx == iforce_serio->len) {
|
2018-08-04 01:23:40 +03:00
|
|
|
/* Handle command completion */
|
2018-08-04 02:27:45 +03:00
|
|
|
if (iforce_serio->expect_packet == iforce_serio->id) {
|
|
|
|
iforce_serio->expect_packet = 0;
|
2018-08-10 23:44:52 +03:00
|
|
|
memcpy(iforce_serio->cmd_response,
|
|
|
|
iforce_serio->data_in, IFORCE_MAX_LENGTH);
|
2018-08-10 03:28:35 +03:00
|
|
|
iforce_serio->cmd_response_len = iforce_serio->len;
|
2018-08-04 01:23:40 +03:00
|
|
|
|
2018-08-10 20:21:13 +03:00
|
|
|
/* Signal that command is done */
|
|
|
|
wake_up(&iforce->wait);
|
2018-08-10 20:34:13 +03:00
|
|
|
} else if (likely(iforce->type)) {
|
2018-08-10 20:21:13 +03:00
|
|
|
iforce_process_packet(iforce, iforce_serio->id,
|
2018-08-10 23:44:52 +03:00
|
|
|
iforce_serio->data_in,
|
|
|
|
iforce_serio->len);
|
2018-08-10 20:21:13 +03:00
|
|
|
}
|
2018-08-04 01:23:40 +03:00
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->pkt = 0;
|
|
|
|
iforce_serio->id = 0;
|
|
|
|
iforce_serio->len = 0;
|
|
|
|
iforce_serio->idx = 0;
|
|
|
|
iforce_serio->csum = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iforce_serio_connect(struct serio *serio, struct serio_driver *drv)
|
|
|
|
{
|
2018-08-04 02:27:45 +03:00
|
|
|
struct iforce_serio *iforce_serio;
|
2005-04-17 02:20:36 +04:00
|
|
|
int err;
|
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio = kzalloc(sizeof(*iforce_serio), GFP_KERNEL);
|
|
|
|
if (!iforce_serio)
|
2005-04-17 02:20:36 +04:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-08-10 23:52:31 +03:00
|
|
|
iforce_serio->iforce.xport_ops = &iforce_serio_xport_ops;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
iforce_serio->serio = serio;
|
|
|
|
serio_set_drvdata(serio, iforce_serio);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
err = serio_open(serio, drv);
|
2006-11-06 06:40:09 +03:00
|
|
|
if (err)
|
|
|
|
goto fail1;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2018-08-10 23:52:31 +03:00
|
|
|
err = iforce_init_device(&serio->dev, BUS_RS232, &iforce_serio->iforce);
|
2006-11-06 06:40:09 +03:00
|
|
|
if (err)
|
|
|
|
goto fail2;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
return 0;
|
2006-11-06 06:40:09 +03:00
|
|
|
|
|
|
|
fail2: serio_close(serio);
|
|
|
|
fail1: serio_set_drvdata(serio, NULL);
|
2018-08-04 02:27:45 +03:00
|
|
|
kfree(iforce_serio);
|
2006-11-06 06:40:09 +03:00
|
|
|
return err;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void iforce_serio_disconnect(struct serio *serio)
|
|
|
|
{
|
2018-08-04 02:27:45 +03:00
|
|
|
struct iforce_serio *iforce_serio = serio_get_drvdata(serio);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2018-08-04 02:27:45 +03:00
|
|
|
input_unregister_device(iforce_serio->iforce.dev);
|
2005-04-17 02:20:36 +04:00
|
|
|
serio_close(serio);
|
|
|
|
serio_set_drvdata(serio, NULL);
|
2018-08-04 02:27:45 +03:00
|
|
|
kfree(iforce_serio);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2017-08-19 03:07:32 +03:00
|
|
|
static const struct serio_device_id iforce_serio_ids[] = {
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
.type = SERIO_RS232,
|
|
|
|
.proto = SERIO_IFORCE,
|
|
|
|
.id = SERIO_ANY,
|
|
|
|
.extra = SERIO_ANY,
|
|
|
|
},
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(serio, iforce_serio_ids);
|
|
|
|
|
|
|
|
struct serio_driver iforce_serio_drv = {
|
|
|
|
.driver = {
|
|
|
|
.name = "iforce",
|
|
|
|
},
|
|
|
|
.description = "RS232 I-Force joysticks and wheels driver",
|
|
|
|
.id_table = iforce_serio_ids,
|
|
|
|
.write_wakeup = iforce_serio_write_wakeup,
|
|
|
|
.interrupt = iforce_serio_irq,
|
|
|
|
.connect = iforce_serio_connect,
|
|
|
|
.disconnect = iforce_serio_disconnect,
|
|
|
|
};
|
2018-07-25 03:32:24 +03:00
|
|
|
|
|
|
|
module_serio_driver(iforce_serio_drv);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>, Johann Deneux <johann.deneux@gmail.com>");
|
|
|
|
MODULE_DESCRIPTION("RS232 I-Force joysticks and wheels driver");
|
|
|
|
MODULE_LICENSE("GPL");
|