xref: /linux/drivers/input/joystick/iforce/iforce-usb.c (revision b8691fd2258d2ae5941c5e5f8bf3dfdaa8951b81)
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>
5598972d4SJohann Deneux  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.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)) ) {
68*b8691fd2SJohann Deneux 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
69598972d4SJohann Deneux 		warn("usb_submit_urb failed %d\n", n);
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	/* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
731da177e4SLinus Torvalds 	 * As long as the urb completion handler is not called, the transmiting
741da177e4SLinus Torvalds 	 * is considered to be running */
751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
787d12e780SDavid Howells static void iforce_usb_irq(struct urb *urb)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
811da177e4SLinus Torvalds 	int status;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	switch (urb->status) {
841da177e4SLinus Torvalds 	case 0:
851da177e4SLinus Torvalds 		/* success */
861da177e4SLinus Torvalds 		break;
871da177e4SLinus Torvalds 	case -ECONNRESET:
881da177e4SLinus Torvalds 	case -ENOENT:
891da177e4SLinus Torvalds 	case -ESHUTDOWN:
901da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
911da177e4SLinus Torvalds 		dbg("%s - urb shutting down with status: %d",
921da177e4SLinus Torvalds 		    __FUNCTION__, urb->status);
931da177e4SLinus Torvalds 		return;
941da177e4SLinus Torvalds 	default:
951da177e4SLinus Torvalds 		dbg("%s - urb has status of: %d", __FUNCTION__, urb->status);
961da177e4SLinus Torvalds 		goto exit;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	iforce_process_packet(iforce,
1007d12e780SDavid Howells 		(iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds exit:
1031da177e4SLinus Torvalds 	status = usb_submit_urb (urb, GFP_ATOMIC);
1041da177e4SLinus Torvalds 	if (status)
1051da177e4SLinus Torvalds 		err ("%s - usb_submit_urb failed with result %d",
1061da177e4SLinus Torvalds 		     __FUNCTION__, status);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1097d12e780SDavid Howells static void iforce_usb_out(struct urb *urb)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (urb->status) {
114598972d4SJohann Deneux 		dbg("urb->status %d, exiting", urb->status);
1151da177e4SLinus Torvalds 		return;
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	iforce_usb_xmit(iforce);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1237d12e780SDavid Howells static void iforce_usb_ctrl(struct urb *urb)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
1261da177e4SLinus Torvalds 	if (urb->status) return;
1271da177e4SLinus Torvalds 	iforce->ecmd = 0xff00 | urb->actual_length;
1281da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds static int iforce_usb_probe(struct usb_interface *intf,
1321da177e4SLinus Torvalds 				const struct usb_device_id *id)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	struct usb_device *dev = interface_to_usbdev(intf);
1351da177e4SLinus Torvalds 	struct usb_host_interface *interface;
1361da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *epirq, *epout;
1371da177e4SLinus Torvalds 	struct iforce *iforce;
13817dd3f0fSDmitry Torokhov 	int err = -ENOMEM;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	interface = intf->cur_altsetting;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	epirq = &interface->endpoint[0].desc;
1431da177e4SLinus Torvalds 	epout = &interface->endpoint[1].desc;
1441da177e4SLinus Torvalds 
14517dd3f0fSDmitry Torokhov 	if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
1461da177e4SLinus Torvalds 		goto fail;
1471da177e4SLinus Torvalds 
14817dd3f0fSDmitry Torokhov 	if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
1491da177e4SLinus Torvalds 		goto fail;
1501da177e4SLinus Torvalds 
15117dd3f0fSDmitry Torokhov 	if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
1521da177e4SLinus Torvalds 		goto fail;
1531da177e4SLinus Torvalds 
15417dd3f0fSDmitry Torokhov 	if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
1551da177e4SLinus Torvalds 		goto fail;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	iforce->bus = IFORCE_USB;
1581da177e4SLinus Torvalds 	iforce->usbdev = dev;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
1611da177e4SLinus Torvalds 	iforce->cr.wIndex = 0;
1621da177e4SLinus Torvalds 	iforce->cr.wLength = 16;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
1651da177e4SLinus Torvalds 			iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
1661da177e4SLinus Torvalds 
167*b8691fd2SJohann Deneux 	usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
168*b8691fd2SJohann Deneux 			iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
1711da177e4SLinus Torvalds 			(void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
1721da177e4SLinus Torvalds 
17317dd3f0fSDmitry Torokhov 	err = iforce_init_device(iforce);
17417dd3f0fSDmitry Torokhov 	if (err)
17517dd3f0fSDmitry Torokhov 		goto fail;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	usb_set_intfdata(intf, iforce);
1781da177e4SLinus Torvalds 	return 0;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds fail:
1811da177e4SLinus Torvalds 	if (iforce) {
1828f21d119SMariusz Kozlowski 		usb_free_urb(iforce->irq);
1838f21d119SMariusz Kozlowski 		usb_free_urb(iforce->out);
1848f21d119SMariusz Kozlowski 		usb_free_urb(iforce->ctrl);
1851da177e4SLinus Torvalds 		kfree(iforce);
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 
18817dd3f0fSDmitry Torokhov 	return err;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /* Called by iforce_delete() */
1921da177e4SLinus Torvalds void iforce_usb_delete(struct iforce* iforce)
1931da177e4SLinus Torvalds {
194598972d4SJohann Deneux 	usb_kill_urb(iforce->irq);
195598972d4SJohann Deneux 	usb_kill_urb(iforce->out);
196598972d4SJohann Deneux 	usb_kill_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