xref: /linux/drivers/input/joystick/iforce/iforce-usb.c (revision 59cf8bed44a79ec42303151dd014fdb6434254bb)
11da177e4SLinus Torvalds  /*
21da177e4SLinus Torvalds  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3598972d4SJohann Deneux  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  USB/RS232 I-Force joysticks and wheels.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
101da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
111da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
121da177e4SLinus Torvalds  * (at your option) any later version.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
151da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
161da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171da177e4SLinus Torvalds  * GNU General Public License for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
201da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
211da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
241da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
251da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include "iforce.h"
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds void iforce_usb_xmit(struct iforce *iforce)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	int n, c;
331da177e4SLinus Torvalds 	unsigned long flags;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 	spin_lock_irqsave(&iforce->xmit_lock, flags);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	if (iforce->xmit.head == iforce->xmit.tail) {
381da177e4SLinus Torvalds 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
391da177e4SLinus Torvalds 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
401da177e4SLinus Torvalds 		return;
411da177e4SLinus Torvalds 	}
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
441da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, 1);
451da177e4SLinus Torvalds 	n = iforce->xmit.buf[iforce->xmit.tail];
461da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, 1);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	iforce->out->transfer_buffer_length = n + 1;
491da177e4SLinus Torvalds 	iforce->out->dev = iforce->usbdev;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	/* Copy rest of data then */
521da177e4SLinus Torvalds 	c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
531da177e4SLinus Torvalds 	if (n < c) c=n;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	memcpy(iforce->out->transfer_buffer + 1,
561da177e4SLinus Torvalds 	       &iforce->xmit.buf[iforce->xmit.tail],
571da177e4SLinus Torvalds 	       c);
581da177e4SLinus Torvalds 	if (n != c) {
591da177e4SLinus Torvalds 		memcpy(iforce->out->transfer_buffer + 1 + c,
601da177e4SLinus Torvalds 		       &iforce->xmit.buf[0],
611da177e4SLinus Torvalds 		       n-c);
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, n);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
66b8691fd2SJohann Deneux 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
67a852d78eSGreg Kroah-Hartman 		dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n);
681da177e4SLinus Torvalds 	}
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	/* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
711da177e4SLinus Torvalds 	 * As long as the urb completion handler is not called, the transmiting
721da177e4SLinus Torvalds 	 * is considered to be running */
731da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
767d12e780SDavid Howells static void iforce_usb_irq(struct urb *urb)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
79a852d78eSGreg Kroah-Hartman 	struct device *dev = &iforce->intf->dev;
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 */
90f576125dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
91ea3e6c59SHarvey Harrison 			__func__, urb->status);
921da177e4SLinus Torvalds 		return;
931da177e4SLinus Torvalds 	default:
94f576125dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb has status of: %d\n",
95f576125dSGreg Kroah-Hartman 			__func__, 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)
105f576125dSGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
106ea3e6c59SHarvey Harrison 			__func__, 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) {
114c2b27ef6SDmitry Torokhov 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
115a852d78eSGreg Kroah-Hartman 		dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n",
116f576125dSGreg Kroah-Hartman 			urb->status);
1171da177e4SLinus Torvalds 		return;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	iforce_usb_xmit(iforce);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1257d12e780SDavid Howells static void iforce_usb_ctrl(struct urb *urb)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct iforce *iforce = urb->context;
1281da177e4SLinus Torvalds 	if (urb->status) return;
1291da177e4SLinus Torvalds 	iforce->ecmd = 0xff00 | urb->actual_length;
1301da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static int iforce_usb_probe(struct usb_interface *intf,
1341da177e4SLinus Torvalds 				const struct usb_device_id *id)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct usb_device *dev = interface_to_usbdev(intf);
1371da177e4SLinus Torvalds 	struct usb_host_interface *interface;
1381da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *epirq, *epout;
1391da177e4SLinus Torvalds 	struct iforce *iforce;
14017dd3f0fSDmitry Torokhov 	int err = -ENOMEM;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	interface = intf->cur_altsetting;
1431da177e4SLinus Torvalds 
144*59cf8bedSJohan Hovold 	if (interface->desc.bNumEndpoints < 2)
145*59cf8bedSJohan Hovold 		return -ENODEV;
146*59cf8bedSJohan Hovold 
1471da177e4SLinus Torvalds 	epirq = &interface->endpoint[0].desc;
1481da177e4SLinus Torvalds 	epout = &interface->endpoint[1].desc;
1491da177e4SLinus Torvalds 
15017dd3f0fSDmitry Torokhov 	if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
1511da177e4SLinus Torvalds 		goto fail;
1521da177e4SLinus Torvalds 
15317dd3f0fSDmitry Torokhov 	if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
1541da177e4SLinus Torvalds 		goto fail;
1551da177e4SLinus Torvalds 
15617dd3f0fSDmitry Torokhov 	if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
1571da177e4SLinus Torvalds 		goto fail;
1581da177e4SLinus Torvalds 
15917dd3f0fSDmitry Torokhov 	if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
1601da177e4SLinus Torvalds 		goto fail;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	iforce->bus = IFORCE_USB;
1631da177e4SLinus Torvalds 	iforce->usbdev = dev;
164a852d78eSGreg Kroah-Hartman 	iforce->intf = intf;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
1671da177e4SLinus Torvalds 	iforce->cr.wIndex = 0;
1686b8588f7SAl Viro 	iforce->cr.wLength = cpu_to_le16(16);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
1711da177e4SLinus Torvalds 			iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
1721da177e4SLinus Torvalds 
173b8691fd2SJohann Deneux 	usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
174b8691fd2SJohann Deneux 			iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
1771da177e4SLinus Torvalds 			(void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
1781da177e4SLinus Torvalds 
17917dd3f0fSDmitry Torokhov 	err = iforce_init_device(iforce);
18017dd3f0fSDmitry Torokhov 	if (err)
18117dd3f0fSDmitry Torokhov 		goto fail;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	usb_set_intfdata(intf, iforce);
1841da177e4SLinus Torvalds 	return 0;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds fail:
1871da177e4SLinus Torvalds 	if (iforce) {
1888f21d119SMariusz Kozlowski 		usb_free_urb(iforce->irq);
1898f21d119SMariusz Kozlowski 		usb_free_urb(iforce->out);
1908f21d119SMariusz Kozlowski 		usb_free_urb(iforce->ctrl);
1911da177e4SLinus Torvalds 		kfree(iforce);
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
19417dd3f0fSDmitry Torokhov 	return err;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
19798b7fb04SDmitry Torokhov static void iforce_usb_disconnect(struct usb_interface *intf)
1981da177e4SLinus Torvalds {
19998b7fb04SDmitry Torokhov 	struct iforce *iforce = usb_get_intfdata(intf);
20098b7fb04SDmitry Torokhov 
20198b7fb04SDmitry Torokhov 	usb_set_intfdata(intf, NULL);
20298b7fb04SDmitry Torokhov 
20398b7fb04SDmitry Torokhov 	input_unregister_device(iforce->dev);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	usb_free_urb(iforce->irq);
2061da177e4SLinus Torvalds 	usb_free_urb(iforce->out);
2071da177e4SLinus Torvalds 	usb_free_urb(iforce->ctrl);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	kfree(iforce);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static struct usb_device_id iforce_usb_ids [] = {
2131da177e4SLinus Torvalds 	{ USB_DEVICE(0x044f, 0xa01c) },		/* Thrustmaster Motor Sport GT */
2141da177e4SLinus Torvalds 	{ USB_DEVICE(0x046d, 0xc281) },		/* Logitech WingMan Force */
2151da177e4SLinus Torvalds 	{ USB_DEVICE(0x046d, 0xc291) },		/* Logitech WingMan Formula Force */
2161da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x020a) },		/* AVB Top Shot Pegasus */
2171da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x8884) },		/* AVB Mag Turbo Force */
2181da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x8888) },		/* AVB Top Shot FFB Racing Wheel */
2191da177e4SLinus Torvalds 	{ USB_DEVICE(0x061c, 0xc0a4) },         /* ACT LABS Force RS */
22068947b8fSJiri Kosina 	{ USB_DEVICE(0x061c, 0xc084) },         /* ACT LABS Force RS */
2211da177e4SLinus Torvalds 	{ USB_DEVICE(0x06f8, 0x0001) },		/* Guillemot Race Leader Force Feedback */
222d861f7bfSMarek Vasut 	{ USB_DEVICE(0x06f8, 0x0003) },		/* Guillemot Jet Leader Force Feedback */
2231da177e4SLinus Torvalds 	{ USB_DEVICE(0x06f8, 0x0004) },		/* Guillemot Force Feedback Racing Wheel */
22418098a6cSMarian-Nicolae V. Ion 	{ USB_DEVICE(0x06f8, 0xa302) },		/* Guillemot Jet Leader 3D */
2251da177e4SLinus Torvalds 	{ }					/* Terminating entry */
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds struct usb_driver iforce_usb_driver = {
2311da177e4SLinus Torvalds 	.name =		"iforce",
2321da177e4SLinus Torvalds 	.probe =	iforce_usb_probe,
2331da177e4SLinus Torvalds 	.disconnect =	iforce_usb_disconnect,
2341da177e4SLinus Torvalds 	.id_table =	iforce_usb_ids,
2351da177e4SLinus Torvalds };
236