1  /*
2  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
4  *
5  *  USB/RS232 I-Force joysticks and wheels.
6  */
7 
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include "iforce.h"
25 
26 void iforce_usb_xmit(struct iforce *iforce)
27 {
28 	int n, c;
29 	unsigned long flags;
30 
31 	spin_lock_irqsave(&iforce->xmit_lock, flags);
32 
33 	if (iforce->xmit.head == iforce->xmit.tail) {
34 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
35 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
36 		return;
37 	}
38 
39 	((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
40 	XMIT_INC(iforce->xmit.tail, 1);
41 	n = iforce->xmit.buf[iforce->xmit.tail];
42 	XMIT_INC(iforce->xmit.tail, 1);
43 
44 	iforce->out->transfer_buffer_length = n + 1;
45 	iforce->out->dev = iforce->usbdev;
46 
47 	/* Copy rest of data then */
48 	c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
49 	if (n < c) c=n;
50 
51 	memcpy(iforce->out->transfer_buffer + 1,
52 	       &iforce->xmit.buf[iforce->xmit.tail],
53 	       c);
54 	if (n != c) {
55 		memcpy(iforce->out->transfer_buffer + 1 + c,
56 		       &iforce->xmit.buf[0],
57 		       n-c);
58 	}
59 	XMIT_INC(iforce->xmit.tail, n);
60 
61 	if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
62 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
63 		dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n);
64 	}
65 
66 	/* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
67 	 * As long as the urb completion handler is not called, the transmiting
68 	 * is considered to be running */
69 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
70 }
71 
72 static void iforce_usb_irq(struct urb *urb)
73 {
74 	struct iforce *iforce = urb->context;
75 	struct device *dev = &iforce->intf->dev;
76 	int status;
77 
78 	switch (urb->status) {
79 	case 0:
80 		/* success */
81 		break;
82 	case -ECONNRESET:
83 	case -ENOENT:
84 	case -ESHUTDOWN:
85 		/* this urb is terminated, clean up */
86 		dev_dbg(dev, "%s - urb shutting down with status: %d\n",
87 			__func__, urb->status);
88 		return;
89 	default:
90 		dev_dbg(dev, "%s - urb has status of: %d\n",
91 			__func__, urb->status);
92 		goto exit;
93 	}
94 
95 	iforce_process_packet(iforce,
96 		(iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
97 
98 exit:
99 	status = usb_submit_urb (urb, GFP_ATOMIC);
100 	if (status)
101 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
102 			__func__, status);
103 }
104 
105 static void iforce_usb_out(struct urb *urb)
106 {
107 	struct iforce *iforce = urb->context;
108 
109 	if (urb->status) {
110 		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
111 		dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n",
112 			urb->status);
113 		return;
114 	}
115 
116 	iforce_usb_xmit(iforce);
117 
118 	wake_up(&iforce->wait);
119 }
120 
121 static void iforce_usb_ctrl(struct urb *urb)
122 {
123 	struct iforce *iforce = urb->context;
124 	if (urb->status) return;
125 	iforce->ecmd = 0xff00 | urb->actual_length;
126 	wake_up(&iforce->wait);
127 }
128 
129 static int iforce_usb_probe(struct usb_interface *intf,
130 				const struct usb_device_id *id)
131 {
132 	struct usb_device *dev = interface_to_usbdev(intf);
133 	struct usb_host_interface *interface;
134 	struct usb_endpoint_descriptor *epirq, *epout;
135 	struct iforce *iforce;
136 	int err = -ENOMEM;
137 
138 	interface = intf->cur_altsetting;
139 
140 	if (interface->desc.bNumEndpoints < 2)
141 		return -ENODEV;
142 
143 	epirq = &interface->endpoint[0].desc;
144 	epout = &interface->endpoint[1].desc;
145 
146 	if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
147 		goto fail;
148 
149 	if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
150 		goto fail;
151 
152 	if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
153 		goto fail;
154 
155 	if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
156 		goto fail;
157 
158 	iforce->bus = IFORCE_USB;
159 	iforce->usbdev = dev;
160 	iforce->intf = intf;
161 
162 	iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
163 	iforce->cr.wIndex = 0;
164 	iforce->cr.wLength = cpu_to_le16(16);
165 
166 	usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
167 			iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
168 
169 	usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
170 			iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
171 
172 	usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
173 			(void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
174 
175 	err = iforce_init_device(iforce);
176 	if (err)
177 		goto fail;
178 
179 	usb_set_intfdata(intf, iforce);
180 	return 0;
181 
182 fail:
183 	if (iforce) {
184 		usb_free_urb(iforce->irq);
185 		usb_free_urb(iforce->out);
186 		usb_free_urb(iforce->ctrl);
187 		kfree(iforce);
188 	}
189 
190 	return err;
191 }
192 
193 static void iforce_usb_disconnect(struct usb_interface *intf)
194 {
195 	struct iforce *iforce = usb_get_intfdata(intf);
196 
197 	usb_set_intfdata(intf, NULL);
198 
199 	input_unregister_device(iforce->dev);
200 
201 	usb_free_urb(iforce->irq);
202 	usb_free_urb(iforce->out);
203 	usb_free_urb(iforce->ctrl);
204 
205 	kfree(iforce);
206 }
207 
208 static const struct usb_device_id iforce_usb_ids[] = {
209 	{ USB_DEVICE(0x044f, 0xa01c) },		/* Thrustmaster Motor Sport GT */
210 	{ USB_DEVICE(0x046d, 0xc281) },		/* Logitech WingMan Force */
211 	{ USB_DEVICE(0x046d, 0xc291) },		/* Logitech WingMan Formula Force */
212 	{ USB_DEVICE(0x05ef, 0x020a) },		/* AVB Top Shot Pegasus */
213 	{ USB_DEVICE(0x05ef, 0x8884) },		/* AVB Mag Turbo Force */
214 	{ USB_DEVICE(0x05ef, 0x8888) },		/* AVB Top Shot FFB Racing Wheel */
215 	{ USB_DEVICE(0x061c, 0xc0a4) },         /* ACT LABS Force RS */
216 	{ USB_DEVICE(0x061c, 0xc084) },         /* ACT LABS Force RS */
217 	{ USB_DEVICE(0x06f8, 0x0001) },		/* Guillemot Race Leader Force Feedback */
218 	{ USB_DEVICE(0x06f8, 0x0003) },		/* Guillemot Jet Leader Force Feedback */
219 	{ USB_DEVICE(0x06f8, 0x0004) },		/* Guillemot Force Feedback Racing Wheel */
220 	{ USB_DEVICE(0x06f8, 0xa302) },		/* Guillemot Jet Leader 3D */
221 	{ }					/* Terminating entry */
222 };
223 
224 MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
225 
226 struct usb_driver iforce_usb_driver = {
227 	.name =		"iforce",
228 	.probe =	iforce_usb_probe,
229 	.disconnect =	iforce_usb_disconnect,
230 	.id_table =	iforce_usb_ids,
231 };
232