blob: 1457b73850e7dd03e41fdb8df932342a3857b281 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001 /*
2 * $Id: iforce-usb.c,v 1.16 2002/06/09 11:08:04 jdeneux Exp $
3 *
4 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
Johann Deneux598972d2007-04-12 01:30:24 -04005 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * USB/RS232 I-Force joysticks and wheels.
8 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * Should you need to contact me, the author, you can do so either by
26 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
27 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
28 */
29
30#include "iforce.h"
31
32void iforce_usb_xmit(struct iforce *iforce)
33{
34 int n, c;
35 unsigned long flags;
36
37 spin_lock_irqsave(&iforce->xmit_lock, flags);
38
39 if (iforce->xmit.head == iforce->xmit.tail) {
40 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
41 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
42 return;
43 }
44
45 ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
46 XMIT_INC(iforce->xmit.tail, 1);
47 n = iforce->xmit.buf[iforce->xmit.tail];
48 XMIT_INC(iforce->xmit.tail, 1);
49
50 iforce->out->transfer_buffer_length = n + 1;
51 iforce->out->dev = iforce->usbdev;
52
53 /* Copy rest of data then */
54 c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
55 if (n < c) c=n;
56
57 memcpy(iforce->out->transfer_buffer + 1,
58 &iforce->xmit.buf[iforce->xmit.tail],
59 c);
60 if (n != c) {
61 memcpy(iforce->out->transfer_buffer + 1 + c,
62 &iforce->xmit.buf[0],
63 n-c);
64 }
65 XMIT_INC(iforce->xmit.tail, n);
66
67 if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
Johann Deneuxb8691fd2007-05-14 00:09:28 -040068 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
Johann Deneux598972d2007-04-12 01:30:24 -040069 warn("usb_submit_urb failed %d\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
73 * As long as the urb completion handler is not called, the transmiting
74 * is considered to be running */
75 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
76}
77
David Howells7d12e782006-10-05 14:55:46 +010078static void iforce_usb_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct iforce *iforce = urb->context;
81 int status;
82
83 switch (urb->status) {
84 case 0:
85 /* success */
86 break;
87 case -ECONNRESET:
88 case -ENOENT:
89 case -ESHUTDOWN:
90 /* this urb is terminated, clean up */
91 dbg("%s - urb shutting down with status: %d",
92 __FUNCTION__, urb->status);
93 return;
94 default:
95 dbg("%s - urb has status of: %d", __FUNCTION__, urb->status);
96 goto exit;
97 }
98
99 iforce_process_packet(iforce,
David Howells7d12e782006-10-05 14:55:46 +0100100 (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102exit:
103 status = usb_submit_urb (urb, GFP_ATOMIC);
104 if (status)
105 err ("%s - usb_submit_urb failed with result %d",
106 __FUNCTION__, status);
107}
108
David Howells7d12e782006-10-05 14:55:46 +0100109static void iforce_usb_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct iforce *iforce = urb->context;
112
113 if (urb->status) {
Johann Deneux598972d2007-04-12 01:30:24 -0400114 dbg("urb->status %d, exiting", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return;
116 }
117
118 iforce_usb_xmit(iforce);
119
120 wake_up(&iforce->wait);
121}
122
David Howells7d12e782006-10-05 14:55:46 +0100123static void iforce_usb_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct iforce *iforce = urb->context;
126 if (urb->status) return;
127 iforce->ecmd = 0xff00 | urb->actual_length;
128 wake_up(&iforce->wait);
129}
130
131static int iforce_usb_probe(struct usb_interface *intf,
132 const struct usb_device_id *id)
133{
134 struct usb_device *dev = interface_to_usbdev(intf);
135 struct usb_host_interface *interface;
136 struct usb_endpoint_descriptor *epirq, *epout;
137 struct iforce *iforce;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500138 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 interface = intf->cur_altsetting;
141
142 epirq = &interface->endpoint[0].desc;
143 epout = &interface->endpoint[1].desc;
144
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500145 if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto fail;
147
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500148 if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500151 if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500154 if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 iforce->bus = IFORCE_USB;
158 iforce->usbdev = dev;
159
160 iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
161 iforce->cr.wIndex = 0;
162 iforce->cr.wLength = 16;
163
164 usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
165 iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
166
Johann Deneuxb8691fd2007-05-14 00:09:28 -0400167 usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
168 iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
171 (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
172
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500173 err = iforce_init_device(iforce);
174 if (err)
175 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 usb_set_intfdata(intf, iforce);
178 return 0;
179
180fail:
181 if (iforce) {
Mariusz Kozlowski8f21d112006-11-08 15:34:09 +0100182 usb_free_urb(iforce->irq);
183 usb_free_urb(iforce->out);
184 usb_free_urb(iforce->ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 kfree(iforce);
186 }
187
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/* Called by iforce_delete() */
192void iforce_usb_delete(struct iforce* iforce)
193{
Johann Deneux598972d2007-04-12 01:30:24 -0400194 usb_kill_urb(iforce->irq);
195 usb_kill_urb(iforce->out);
196 usb_kill_urb(iforce->ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 usb_free_urb(iforce->irq);
199 usb_free_urb(iforce->out);
200 usb_free_urb(iforce->ctrl);
201}
202
203static void iforce_usb_disconnect(struct usb_interface *intf)
204{
205 struct iforce *iforce = usb_get_intfdata(intf);
206 int open = 0; /* FIXME! iforce->dev.handle->open; */
207
208 usb_set_intfdata(intf, NULL);
209 if (iforce) {
210 iforce->usbdev = NULL;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500211 input_unregister_device(iforce->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (!open) {
214 iforce_delete_device(iforce);
215 kfree(iforce);
216 }
217 }
218}
219
220static struct usb_device_id iforce_usb_ids [] = {
221 { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */
222 { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */
223 { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */
224 { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */
225 { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */
226 { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */
227 { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */
228 { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */
229 { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */
Marian-Nicolae V. Ion18098a62005-05-29 02:30:01 -0500230 { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 { } /* Terminating entry */
232};
233
234MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
235
236struct usb_driver iforce_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .name = "iforce",
238 .probe = iforce_usb_probe,
239 .disconnect = iforce_usb_disconnect,
240 .id_table = iforce_usb_ids,
241};