xref: /linux/drivers/input/joystick/iforce/iforce-usb.c (revision 4873586278258453bed94ffc04bfdc439197e86b)
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 
244f99de6dSDmitry Torokhov #include <linux/usb.h>
251da177e4SLinus Torvalds #include "iforce.h"
261da177e4SLinus Torvalds 
2781fd4313SDmitry Torokhov struct iforce_usb {
2881fd4313SDmitry Torokhov 	struct iforce iforce;
2981fd4313SDmitry Torokhov 
3081fd4313SDmitry Torokhov 	struct usb_device *usbdev;
3181fd4313SDmitry Torokhov 	struct usb_interface *intf;
32*48735862SDmitry Torokhov 	struct urb *irq, *out;
3381fd4313SDmitry Torokhov };
3481fd4313SDmitry Torokhov 
3538d10769SDmitry Torokhov static void __iforce_usb_xmit(struct iforce *iforce)
361da177e4SLinus Torvalds {
3781fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
3881fd4313SDmitry Torokhov 						     iforce);
391da177e4SLinus Torvalds 	int n, c;
401da177e4SLinus Torvalds 	unsigned long flags;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	spin_lock_irqsave(&iforce->xmit_lock, flags);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	if (iforce->xmit.head == iforce->xmit.tail) {
451da177e4SLinus Torvalds 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
461da177e4SLinus Torvalds 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
471da177e4SLinus Torvalds 		return;
481da177e4SLinus Torvalds 	}
491da177e4SLinus Torvalds 
5081fd4313SDmitry Torokhov 	((char *)iforce_usb->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
511da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, 1);
521da177e4SLinus Torvalds 	n = iforce->xmit.buf[iforce->xmit.tail];
531da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, 1);
541da177e4SLinus Torvalds 
5581fd4313SDmitry Torokhov 	iforce_usb->out->transfer_buffer_length = n + 1;
5681fd4313SDmitry Torokhov 	iforce_usb->out->dev = iforce_usb->usbdev;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	/* Copy rest of data then */
591da177e4SLinus Torvalds 	c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
601da177e4SLinus Torvalds 	if (n < c) c=n;
611da177e4SLinus Torvalds 
6281fd4313SDmitry Torokhov 	memcpy(iforce_usb->out->transfer_buffer + 1,
631da177e4SLinus Torvalds 	       &iforce->xmit.buf[iforce->xmit.tail],
641da177e4SLinus Torvalds 	       c);
651da177e4SLinus Torvalds 	if (n != c) {
6681fd4313SDmitry Torokhov 		memcpy(iforce_usb->out->transfer_buffer + 1 + c,
671da177e4SLinus Torvalds 		       &iforce->xmit.buf[0],
681da177e4SLinus Torvalds 		       n-c);
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.tail, n);
711da177e4SLinus Torvalds 
7281fd4313SDmitry Torokhov 	if ( (n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC)) ) {
73b8691fd2SJohann Deneux 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
7481fd4313SDmitry Torokhov 		dev_warn(&iforce_usb->intf->dev,
7581fd4313SDmitry Torokhov 			 "usb_submit_urb failed %d\n", n);
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	/* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
791da177e4SLinus Torvalds 	 * As long as the urb completion handler is not called, the transmiting
801da177e4SLinus Torvalds 	 * is considered to be running */
811da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
8438d10769SDmitry Torokhov static void iforce_usb_xmit(struct iforce *iforce)
8538d10769SDmitry Torokhov {
8638d10769SDmitry Torokhov 	if (!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags))
8738d10769SDmitry Torokhov 		__iforce_usb_xmit(iforce);
8838d10769SDmitry Torokhov }
8938d10769SDmitry Torokhov 
902a1433ffSDmitry Torokhov static int iforce_usb_get_id(struct iforce *iforce, u8 *packet)
912a1433ffSDmitry Torokhov {
9281fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
9381fd4313SDmitry Torokhov 						     iforce);
94*48735862SDmitry Torokhov 	u8 *buf;
952a1433ffSDmitry Torokhov 	int status;
962a1433ffSDmitry Torokhov 
97*48735862SDmitry Torokhov 	buf = kmalloc(IFORCE_MAX_LENGTH, GFP_KERNEL);
98*48735862SDmitry Torokhov 	if (!buf)
99*48735862SDmitry Torokhov 		return -ENOMEM;
1002a1433ffSDmitry Torokhov 
101*48735862SDmitry Torokhov 	status = usb_control_msg(iforce_usb->usbdev,
102*48735862SDmitry Torokhov 				 usb_rcvctrlpipe(iforce_usb->usbdev, 0),
103*48735862SDmitry Torokhov 				 packet[0],
104*48735862SDmitry Torokhov 				 USB_TYPE_VENDOR | USB_DIR_IN |
105*48735862SDmitry Torokhov 					USB_RECIP_INTERFACE,
106*48735862SDmitry Torokhov 				 0, 0, buf, IFORCE_MAX_LENGTH, HZ);
107*48735862SDmitry Torokhov 	if (status < 0) {
10881fd4313SDmitry Torokhov 		dev_err(&iforce_usb->intf->dev,
109*48735862SDmitry Torokhov 			"usb_submit_urb failed: %d\n", status);
110*48735862SDmitry Torokhov 	} else if (buf[0] != packet[0]) {
111*48735862SDmitry Torokhov 		status = -EIO;
112*48735862SDmitry Torokhov 	} else {
113*48735862SDmitry Torokhov 		iforce->ecmd = 0xff00 | status;
114*48735862SDmitry Torokhov 		memcpy(iforce->edata, buf, status);
115*48735862SDmitry Torokhov 		status = 0;
1162a1433ffSDmitry Torokhov 	}
1172a1433ffSDmitry Torokhov 
118*48735862SDmitry Torokhov 	kfree(buf);
119*48735862SDmitry Torokhov 	return status;
1202a1433ffSDmitry Torokhov }
1212a1433ffSDmitry Torokhov 
12205ca3828SDmitry Torokhov static int iforce_usb_start_io(struct iforce *iforce)
12305ca3828SDmitry Torokhov {
12481fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
12581fd4313SDmitry Torokhov 						     iforce);
12681fd4313SDmitry Torokhov 
12781fd4313SDmitry Torokhov 	if (usb_submit_urb(iforce_usb->irq, GFP_KERNEL))
12805ca3828SDmitry Torokhov 		return -EIO;
12905ca3828SDmitry Torokhov 
13005ca3828SDmitry Torokhov 	return 0;
13105ca3828SDmitry Torokhov }
13205ca3828SDmitry Torokhov 
13305ca3828SDmitry Torokhov static void iforce_usb_stop_io(struct iforce *iforce)
13405ca3828SDmitry Torokhov {
13581fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
13681fd4313SDmitry Torokhov 						     iforce);
13781fd4313SDmitry Torokhov 
13881fd4313SDmitry Torokhov 	usb_kill_urb(iforce_usb->irq);
13981fd4313SDmitry Torokhov 	usb_kill_urb(iforce_usb->out);
14005ca3828SDmitry Torokhov }
14105ca3828SDmitry Torokhov 
14238d10769SDmitry Torokhov static const struct iforce_xport_ops iforce_usb_xport_ops = {
14338d10769SDmitry Torokhov 	.xmit		= iforce_usb_xmit,
1442a1433ffSDmitry Torokhov 	.get_id		= iforce_usb_get_id,
14505ca3828SDmitry Torokhov 	.start_io	= iforce_usb_start_io,
14605ca3828SDmitry Torokhov 	.stop_io	= iforce_usb_stop_io,
14738d10769SDmitry Torokhov };
14838d10769SDmitry Torokhov 
1497d12e780SDavid Howells static void iforce_usb_irq(struct urb *urb)
1501da177e4SLinus Torvalds {
15181fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = urb->context;
15281fd4313SDmitry Torokhov 	struct iforce *iforce = &iforce_usb->iforce;
15381fd4313SDmitry Torokhov 	struct device *dev = &iforce_usb->intf->dev;
1541da177e4SLinus Torvalds 	int status;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	switch (urb->status) {
1571da177e4SLinus Torvalds 	case 0:
1581da177e4SLinus Torvalds 		/* success */
1591da177e4SLinus Torvalds 		break;
1601da177e4SLinus Torvalds 	case -ECONNRESET:
1611da177e4SLinus Torvalds 	case -ENOENT:
1621da177e4SLinus Torvalds 	case -ESHUTDOWN:
1631da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
164f576125dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
165ea3e6c59SHarvey Harrison 			__func__, urb->status);
1661da177e4SLinus Torvalds 		return;
1671da177e4SLinus Torvalds 	default:
168f576125dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb has status of: %d\n",
169f576125dSGreg Kroah-Hartman 			__func__, urb->status);
1701da177e4SLinus Torvalds 		goto exit;
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	iforce_process_packet(iforce,
1747d12e780SDavid Howells 		(iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds exit:
1771da177e4SLinus Torvalds 	status = usb_submit_urb(urb, GFP_ATOMIC);
1781da177e4SLinus Torvalds 	if (status)
179f576125dSGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
180ea3e6c59SHarvey Harrison 			__func__, status);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1837d12e780SDavid Howells static void iforce_usb_out(struct urb *urb)
1841da177e4SLinus Torvalds {
18581fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = urb->context;
18681fd4313SDmitry Torokhov 	struct iforce *iforce = &iforce_usb->iforce;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	if (urb->status) {
189c2b27ef6SDmitry Torokhov 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
19081fd4313SDmitry Torokhov 		dev_dbg(&iforce_usb->intf->dev, "urb->status %d, exiting\n",
191f576125dSGreg Kroah-Hartman 			urb->status);
1921da177e4SLinus Torvalds 		return;
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
19538d10769SDmitry Torokhov 	__iforce_usb_xmit(iforce);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	wake_up(&iforce->wait);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static int iforce_usb_probe(struct usb_interface *intf,
2011da177e4SLinus Torvalds 				const struct usb_device_id *id)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	struct usb_device *dev = interface_to_usbdev(intf);
2041da177e4SLinus Torvalds 	struct usb_host_interface *interface;
2051da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *epirq, *epout;
20681fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb;
2071da177e4SLinus Torvalds 	struct iforce *iforce;
20817dd3f0fSDmitry Torokhov 	int err = -ENOMEM;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	interface = intf->cur_altsetting;
2111da177e4SLinus Torvalds 
21259cf8bedSJohan Hovold 	if (interface->desc.bNumEndpoints < 2)
21359cf8bedSJohan Hovold 		return -ENODEV;
21459cf8bedSJohan Hovold 
2151da177e4SLinus Torvalds 	epirq = &interface->endpoint[0].desc;
2161da177e4SLinus Torvalds 	epout = &interface->endpoint[1].desc;
2171da177e4SLinus Torvalds 
21881fd4313SDmitry Torokhov 	if (!(iforce_usb = kzalloc(sizeof(*iforce_usb) + 32, GFP_KERNEL)))
2191da177e4SLinus Torvalds 		goto fail;
2201da177e4SLinus Torvalds 
22181fd4313SDmitry Torokhov 	if (!(iforce_usb->irq = usb_alloc_urb(0, GFP_KERNEL)))
2221da177e4SLinus Torvalds 		goto fail;
2231da177e4SLinus Torvalds 
22481fd4313SDmitry Torokhov 	if (!(iforce_usb->out = usb_alloc_urb(0, GFP_KERNEL)))
2251da177e4SLinus Torvalds 		goto fail;
2261da177e4SLinus Torvalds 
22781fd4313SDmitry Torokhov 	iforce = &iforce_usb->iforce;
22881fd4313SDmitry Torokhov 
22938d10769SDmitry Torokhov 	iforce->xport_ops = &iforce_usb_xport_ops;
2301da177e4SLinus Torvalds 	iforce->bus = IFORCE_USB;
2311da177e4SLinus Torvalds 
23281fd4313SDmitry Torokhov 	iforce_usb->usbdev = dev;
23381fd4313SDmitry Torokhov 	iforce_usb->intf = intf;
2341da177e4SLinus Torvalds 
23581fd4313SDmitry Torokhov 	usb_fill_int_urb(iforce_usb->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
23681fd4313SDmitry Torokhov 			iforce->data, 16, iforce_usb_irq, iforce_usb, epirq->bInterval);
2371da177e4SLinus Torvalds 
23881fd4313SDmitry Torokhov 	usb_fill_int_urb(iforce_usb->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
23981fd4313SDmitry Torokhov 			iforce_usb + 1, 32, iforce_usb_out, iforce_usb, epout->bInterval);
24081fd4313SDmitry Torokhov 
241501025dfSDmitry Torokhov 	err = iforce_init_device(&intf->dev, BUS_USB, iforce);
24217dd3f0fSDmitry Torokhov 	if (err)
24317dd3f0fSDmitry Torokhov 		goto fail;
2441da177e4SLinus Torvalds 
24581fd4313SDmitry Torokhov 	usb_set_intfdata(intf, iforce_usb);
2461da177e4SLinus Torvalds 	return 0;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds fail:
24981fd4313SDmitry Torokhov 	if (iforce_usb) {
25081fd4313SDmitry Torokhov 		usb_free_urb(iforce_usb->irq);
25181fd4313SDmitry Torokhov 		usb_free_urb(iforce_usb->out);
25281fd4313SDmitry Torokhov 		kfree(iforce_usb);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
25517dd3f0fSDmitry Torokhov 	return err;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
25898b7fb04SDmitry Torokhov static void iforce_usb_disconnect(struct usb_interface *intf)
2591da177e4SLinus Torvalds {
26081fd4313SDmitry Torokhov 	struct iforce_usb *iforce_usb = usb_get_intfdata(intf);
26198b7fb04SDmitry Torokhov 
26298b7fb04SDmitry Torokhov 	usb_set_intfdata(intf, NULL);
26398b7fb04SDmitry Torokhov 
26481fd4313SDmitry Torokhov 	input_unregister_device(iforce_usb->iforce.dev);
2651da177e4SLinus Torvalds 
26681fd4313SDmitry Torokhov 	usb_free_urb(iforce_usb->irq);
26781fd4313SDmitry Torokhov 	usb_free_urb(iforce_usb->out);
2681da177e4SLinus Torvalds 
26981fd4313SDmitry Torokhov 	kfree(iforce_usb);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
272c1295851SArvind Yadav static const struct usb_device_id iforce_usb_ids[] = {
2731da177e4SLinus Torvalds 	{ USB_DEVICE(0x044f, 0xa01c) },		/* Thrustmaster Motor Sport GT */
2741da177e4SLinus Torvalds 	{ USB_DEVICE(0x046d, 0xc281) },		/* Logitech WingMan Force */
2751da177e4SLinus Torvalds 	{ USB_DEVICE(0x046d, 0xc291) },		/* Logitech WingMan Formula Force */
2761da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x020a) },		/* AVB Top Shot Pegasus */
2771da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x8884) },		/* AVB Mag Turbo Force */
2781da177e4SLinus Torvalds 	{ USB_DEVICE(0x05ef, 0x8888) },		/* AVB Top Shot FFB Racing Wheel */
2791da177e4SLinus Torvalds 	{ USB_DEVICE(0x061c, 0xc0a4) },         /* ACT LABS Force RS */
28068947b8fSJiri Kosina 	{ USB_DEVICE(0x061c, 0xc084) },         /* ACT LABS Force RS */
2811da177e4SLinus Torvalds 	{ USB_DEVICE(0x06f8, 0x0001) },		/* Guillemot Race Leader Force Feedback */
282d861f7bfSMarek Vasut 	{ USB_DEVICE(0x06f8, 0x0003) },		/* Guillemot Jet Leader Force Feedback */
2831da177e4SLinus Torvalds 	{ USB_DEVICE(0x06f8, 0x0004) },		/* Guillemot Force Feedback Racing Wheel */
28418098a6cSMarian-Nicolae V. Ion 	{ USB_DEVICE(0x06f8, 0xa302) },		/* Guillemot Jet Leader 3D */
2851da177e4SLinus Torvalds 	{ }					/* Terminating entry */
2861da177e4SLinus Torvalds };
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds struct usb_driver iforce_usb_driver = {
2911da177e4SLinus Torvalds 	.name =		"iforce",
2921da177e4SLinus Torvalds 	.probe =	iforce_usb_probe,
2931da177e4SLinus Torvalds 	.disconnect =	iforce_usb_disconnect,
2941da177e4SLinus Torvalds 	.id_table =	iforce_usb_ids,
2951da177e4SLinus Torvalds };
2964f99de6dSDmitry Torokhov 
2974f99de6dSDmitry Torokhov module_usb_driver(iforce_usb_driver);
2984f99de6dSDmitry Torokhov 
2994f99de6dSDmitry Torokhov MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>, Johann Deneux <johann.deneux@gmail.com>");
3004f99de6dSDmitry Torokhov MODULE_DESCRIPTION("USB I-Force joysticks and wheels driver");
3014f99de6dSDmitry Torokhov MODULE_LICENSE("GPL");
302