xref: /linux/drivers/input/joystick/iforce/iforce-usb.c (revision 8f21d119f457ece27a69ac1dadd655deee6f2a20)
11da177e4SLinus Torvalds  /*
21da177e4SLinus Torvalds  * $Id: iforce-usb.c,v 1.16 2002/06/09 11:08:04 jdeneux Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
51da177e4SLinus Torvalds  *  Copyright (c) 2001-2002 Johann Deneux <deneux@ifrance.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  USB/RS232 I-Force joysticks and wheels.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
121da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
131da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
141da177e4SLinus Torvalds  * (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
171da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
181da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
191da177e4SLinus Torvalds  * GNU General Public License for more details.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
221da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
231da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
261da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
271da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include "iforce.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds void iforce_usb_xmit(struct iforce *iforce)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	int n, c;
351da177e4SLinus Torvalds 	unsigned long flags;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	spin_lock_irqsave(&iforce->xmit_lock, flags);
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	if (iforce->xmit.head == iforce->xmit.tail) {
401da177e4SLinus Torvalds 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
411da177e4SLinus Torvalds 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
421da177e4SLinus Torvalds 		return;
431da177e4SLinus Torvalds 	}
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
461da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, 1);
471da177e4SLinus Torvalds 	n = iforce->xmit.buf[iforce->xmit.tail];
481da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, 1);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	iforce->out->transfer_buffer_length = n + 1;
511da177e4SLinus Torvalds 	iforce->out->dev = iforce->usbdev;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	/* Copy rest of data then */
541da177e4SLinus Torvalds 	c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
551da177e4SLinus Torvalds 	if (n < c) c=n;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	memcpy(iforce->out->transfer_buffer + 1,
581da177e4SLinus Torvalds 	       &iforce->xmit.buf[iforce->xmit.tail],
591da177e4SLinus Torvalds 	       c);
601da177e4SLinus Torvalds 	if (n != c) {
611da177e4SLinus Torvalds 		memcpy(iforce->out->transfer_buffer + 1 + c,
621da177e4SLinus Torvalds 		       &iforce->xmit.buf[0],
631da177e4SLinus Torvalds 		       n-c);
641da177e4SLinus Torvalds 	}
651da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, n);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
681da177e4SLinus Torvalds 		printk(KERN_WARNING "iforce-usb.c: iforce_usb_xmit: usb_submit_urb failed %d\n", n);
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	/* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
721da177e4SLinus Torvalds 	 * As long as the urb completion handler is not called, the transmiting
731da177e4SLinus Torvalds 	 * is considered to be running */
741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
777d12e780SDavid Howells static void iforce_usb_irq(struct urb *urb)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
801da177e4SLinus Torvalds 	int status;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	switch (urb->status) {
831da177e4SLinus Torvalds 	case 0:
841da177e4SLinus Torvalds 		/* success */
851da177e4SLinus Torvalds 		break;
861da177e4SLinus Torvalds 	case -ECONNRESET:
871da177e4SLinus Torvalds 	case -ENOENT:
881da177e4SLinus Torvalds 	case -ESHUTDOWN:
891da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
901da177e4SLinus Torvalds 		dbg("%s - urb shutting down with status: %d",
911da177e4SLinus Torvalds 		    __FUNCTION__, urb->status);
921da177e4SLinus Torvalds 		return;
931da177e4SLinus Torvalds 	default:
941da177e4SLinus Torvalds 		dbg("%s - urb has status of: %d", __FUNCTION__, urb->status);
951da177e4SLinus Torvalds 		goto exit;
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	iforce_process_packet(iforce,
997d12e780SDavid Howells 		(iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds exit:
1021da177e4SLinus Torvalds 	status = usb_submit_urb (urb, GFP_ATOMIC);
1031da177e4SLinus Torvalds 	if (status)
1041da177e4SLinus Torvalds 		err ("%s - usb_submit_urb failed with result %d",
1051da177e4SLinus Torvalds 		     __FUNCTION__, status);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1087d12e780SDavid Howells static void iforce_usb_out(struct urb *urb)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	if (urb->status) {
1131da177e4SLinus Torvalds 		printk(KERN_DEBUG "iforce_usb_out: urb->status %d, exiting", urb->status);
1141da177e4SLinus Torvalds 		return;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	iforce_usb_xmit(iforce);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1227d12e780SDavid Howells static void iforce_usb_ctrl(struct urb *urb)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
1251da177e4SLinus Torvalds 	if (urb->status) return;
1261da177e4SLinus Torvalds 	iforce->ecmd = 0xff00 | urb->actual_length;
1271da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static int iforce_usb_probe(struct usb_interface *intf,
1311da177e4SLinus Torvalds 				const struct usb_device_id *id)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	struct usb_device *dev = interface_to_usbdev(intf);
1341da177e4SLinus Torvalds 	struct usb_host_interface *interface;
1351da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *epirq, *epout;
1361da177e4SLinus Torvalds 	struct iforce *iforce;
13717dd3f0fSDmitry Torokhov 	int err = -ENOMEM;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	interface = intf->cur_altsetting;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	epirq = &interface->endpoint[0].desc;
1421da177e4SLinus Torvalds 	epout = &interface->endpoint[1].desc;
1431da177e4SLinus Torvalds 
14417dd3f0fSDmitry Torokhov 	if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
1451da177e4SLinus Torvalds 		goto fail;
1461da177e4SLinus Torvalds 
14717dd3f0fSDmitry Torokhov 	if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
1481da177e4SLinus Torvalds 		goto fail;
1491da177e4SLinus Torvalds 
15017dd3f0fSDmitry Torokhov 	if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
1511da177e4SLinus Torvalds 		goto fail;
1521da177e4SLinus Torvalds 
15317dd3f0fSDmitry Torokhov 	if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
1541da177e4SLinus Torvalds 		goto fail;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	iforce->bus = IFORCE_USB;
1571da177e4SLinus Torvalds 	iforce->usbdev = dev;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
1601da177e4SLinus Torvalds 	iforce->cr.wIndex = 0;
1611da177e4SLinus Torvalds 	iforce->cr.wLength = 16;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
1641da177e4SLinus Torvalds 			iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	usb_fill_bulk_urb(iforce->out, dev, usb_sndbulkpipe(dev, epout->bEndpointAddress),
1671da177e4SLinus Torvalds 			iforce + 1, 32, iforce_usb_out, iforce);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
1701da177e4SLinus Torvalds 			(void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
1711da177e4SLinus Torvalds 
17217dd3f0fSDmitry Torokhov 	err = iforce_init_device(iforce);
17317dd3f0fSDmitry Torokhov 	if (err)
17417dd3f0fSDmitry Torokhov 		goto fail;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	usb_set_intfdata(intf, iforce);
1771da177e4SLinus Torvalds 	return 0;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds fail:
1801da177e4SLinus Torvalds 	if (iforce) {
181*8f21d119SMariusz Kozlowski 		usb_free_urb(iforce->irq);
182*8f21d119SMariusz Kozlowski 		usb_free_urb(iforce->out);
183*8f21d119SMariusz Kozlowski 		usb_free_urb(iforce->ctrl);
1841da177e4SLinus Torvalds 		kfree(iforce);
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
18717dd3f0fSDmitry Torokhov 	return err;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /* Called by iforce_delete() */
1911da177e4SLinus Torvalds void iforce_usb_delete(struct iforce* iforce)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	usb_unlink_urb(iforce->irq);
1941da177e4SLinus Torvalds /* Is it ok to unlink those ? */
1951da177e4SLinus Torvalds 	usb_unlink_urb(iforce->out);
1961da177e4SLinus Torvalds 	usb_unlink_urb(iforce->ctrl);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	usb_free_urb(iforce->irq);
1991da177e4SLinus Torvalds 	usb_free_urb(iforce->out);
2001da177e4SLinus Torvalds 	usb_free_urb(iforce->ctrl);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static void iforce_usb_disconnect(struct usb_interface *intf)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct iforce *iforce = usb_get_intfdata(intf);
2061da177e4SLinus Torvalds 	int open = 0; /* FIXME! iforce->dev.handle->open; */
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	usb_set_intfdata(intf, NULL);
2091da177e4SLinus Torvalds 	if (iforce) {
2101da177e4SLinus Torvalds 		iforce->usbdev = NULL;
21117dd3f0fSDmitry Torokhov 		input_unregister_device(iforce->dev);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 		if (!open) {
2141da177e4SLinus Torvalds 			iforce_delete_device(iforce);
2151da177e4SLinus Torvalds 			kfree(iforce);
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static struct usb_device_id iforce_usb_ids [] = {
2211da177e4SLinus Torvalds 	{ USB_DEVICE(0x044f, 0xa01c) },		/* Thrustmaster Motor Sport GT */
2221da177e4SLinus Torvalds 	{ USB_DEVICE(0x046d, 0xc281) },		/* Logitech WingMan Force */
2231da177e4SLinus Torvalds 	{ USB_DEVICE(0x046d, 0xc291) },		/* Logitech WingMan Formula Force */
2241da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x020a) },		/* AVB Top Shot Pegasus */
2251da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x8884) },		/* AVB Mag Turbo Force */
2261da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x8888) },		/* AVB Top Shot FFB Racing Wheel */
2271da177e4SLinus Torvalds 	{ USB_DEVICE(0x061c, 0xc0a4) },         /* ACT LABS Force RS */
2281da177e4SLinus Torvalds 	{ USB_DEVICE(0x06f8, 0x0001) },		/* Guillemot Race Leader Force Feedback */
2291da177e4SLinus Torvalds 	{ USB_DEVICE(0x06f8, 0x0004) },		/* Guillemot Force Feedback Racing Wheel */
23018098a6cSMarian-Nicolae V. Ion 	{ USB_DEVICE(0x06f8, 0xa302) },		/* Guillemot Jet Leader 3D */
2311da177e4SLinus Torvalds 	{ }					/* Terminating entry */
2321da177e4SLinus Torvalds };
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds struct usb_driver iforce_usb_driver = {
2371da177e4SLinus Torvalds 	.name =		"iforce",
2381da177e4SLinus Torvalds 	.probe =	iforce_usb_probe,
2391da177e4SLinus Torvalds 	.disconnect =	iforce_usb_disconnect,
2401da177e4SLinus Torvalds 	.id_table =	iforce_usb_ids,
2411da177e4SLinus Torvalds };
242