Path: blob/master/drivers/input/joystick/iforce/iforce-serio.c
15115 views
/*1* Copyright (c) 2000-2001 Vojtech Pavlik <[email protected]>2* Copyright (c) 2001, 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"2829void iforce_serial_xmit(struct iforce *iforce)30{31unsigned char cs;32int i;33unsigned long flags;3435if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {36set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags);37return;38}3940spin_lock_irqsave(&iforce->xmit_lock, flags);4142again:43if (iforce->xmit.head == iforce->xmit.tail) {44clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);45spin_unlock_irqrestore(&iforce->xmit_lock, flags);46return;47}4849cs = 0x2b;5051serio_write(iforce->serio, 0x2b);5253serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);54cs ^= iforce->xmit.buf[iforce->xmit.tail];55XMIT_INC(iforce->xmit.tail, 1);5657for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) {58serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);59cs ^= iforce->xmit.buf[iforce->xmit.tail];60XMIT_INC(iforce->xmit.tail, 1);61}6263serio_write(iforce->serio, cs);6465if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags))66goto again;6768clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);6970spin_unlock_irqrestore(&iforce->xmit_lock, flags);71}7273static void iforce_serio_write_wakeup(struct serio *serio)74{75struct iforce *iforce = serio_get_drvdata(serio);7677iforce_serial_xmit(iforce);78}7980static irqreturn_t iforce_serio_irq(struct serio *serio,81unsigned char data, unsigned int flags)82{83struct iforce *iforce = serio_get_drvdata(serio);8485if (!iforce->pkt) {86if (data == 0x2b)87iforce->pkt = 1;88goto out;89}9091if (!iforce->id) {92if (data > 3 && data != 0xff)93iforce->pkt = 0;94else95iforce->id = data;96goto out;97}9899if (!iforce->len) {100if (data > IFORCE_MAX_LENGTH) {101iforce->pkt = 0;102iforce->id = 0;103} else {104iforce->len = data;105}106goto out;107}108109if (iforce->idx < iforce->len) {110iforce->csum += iforce->data[iforce->idx++] = data;111goto out;112}113114if (iforce->idx == iforce->len) {115iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data);116iforce->pkt = 0;117iforce->id = 0;118iforce->len = 0;119iforce->idx = 0;120iforce->csum = 0;121}122out:123return IRQ_HANDLED;124}125126static int iforce_serio_connect(struct serio *serio, struct serio_driver *drv)127{128struct iforce *iforce;129int err;130131iforce = kzalloc(sizeof(struct iforce), GFP_KERNEL);132if (!iforce)133return -ENOMEM;134135iforce->bus = IFORCE_232;136iforce->serio = serio;137138serio_set_drvdata(serio, iforce);139140err = serio_open(serio, drv);141if (err)142goto fail1;143144err = iforce_init_device(iforce);145if (err)146goto fail2;147148return 0;149150fail2: serio_close(serio);151fail1: serio_set_drvdata(serio, NULL);152kfree(iforce);153return err;154}155156static void iforce_serio_disconnect(struct serio *serio)157{158struct iforce *iforce = serio_get_drvdata(serio);159160input_unregister_device(iforce->dev);161serio_close(serio);162serio_set_drvdata(serio, NULL);163kfree(iforce);164}165166static struct serio_device_id iforce_serio_ids[] = {167{168.type = SERIO_RS232,169.proto = SERIO_IFORCE,170.id = SERIO_ANY,171.extra = SERIO_ANY,172},173{ 0 }174};175176MODULE_DEVICE_TABLE(serio, iforce_serio_ids);177178struct serio_driver iforce_serio_drv = {179.driver = {180.name = "iforce",181},182.description = "RS232 I-Force joysticks and wheels driver",183.id_table = iforce_serio_ids,184.write_wakeup = iforce_serio_write_wakeup,185.interrupt = iforce_serio_irq,186.connect = iforce_serio_connect,187.disconnect = iforce_serio_disconnect,188};189190191