Merge branch 'topic/oss' into for-linus
[linux-2.6-block.git] / drivers / input / joystick / iforce / iforce-usb.c
CommitLineData
1da177e4 1 /*
1da177e4 2 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
598972d4 3 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
1da177e4
LT
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 * Should you need to contact me, the author, you can do so either by
24 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26 */
27
28#include "iforce.h"
29
30void iforce_usb_xmit(struct iforce *iforce)
31{
32 int n, c;
33 unsigned long flags;
34
35 spin_lock_irqsave(&iforce->xmit_lock, flags);
36
37 if (iforce->xmit.head == iforce->xmit.tail) {
38 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
39 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
40 return;
41 }
42
43 ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
44 XMIT_INC(iforce->xmit.tail, 1);
45 n = iforce->xmit.buf[iforce->xmit.tail];
46 XMIT_INC(iforce->xmit.tail, 1);
47
48 iforce->out->transfer_buffer_length = n + 1;
49 iforce->out->dev = iforce->usbdev;
50
51 /* Copy rest of data then */
52 c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
53 if (n < c) c=n;
54
55 memcpy(iforce->out->transfer_buffer + 1,
56 &iforce->xmit.buf[iforce->xmit.tail],
57 c);
58 if (n != c) {
59 memcpy(iforce->out->transfer_buffer + 1 + c,
60 &iforce->xmit.buf[0],
61 n-c);
62 }
63 XMIT_INC(iforce->xmit.tail, n);
64
65 if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
b8691fd2 66 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
1817b169 67 dev_warn(&iforce->dev->dev, "usb_submit_urb failed %d\n", n);
1da177e4
LT
68 }
69
70 /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
71 * As long as the urb completion handler is not called, the transmiting
72 * is considered to be running */
73 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
74}
75
7d12e780 76static void iforce_usb_irq(struct urb *urb)
1da177e4
LT
77{
78 struct iforce *iforce = urb->context;
79 int status;
80
81 switch (urb->status) {
82 case 0:
83 /* success */
84 break;
85 case -ECONNRESET:
86 case -ENOENT:
87 case -ESHUTDOWN:
88 /* this urb is terminated, clean up */
89 dbg("%s - urb shutting down with status: %d",
ea3e6c59 90 __func__, urb->status);
1da177e4
LT
91 return;
92 default:
ea3e6c59 93 dbg("%s - urb has status of: %d", __func__, urb->status);
1da177e4
LT
94 goto exit;
95 }
96
97 iforce_process_packet(iforce,
7d12e780 98 (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
1da177e4
LT
99
100exit:
101 status = usb_submit_urb (urb, GFP_ATOMIC);
102 if (status)
103 err ("%s - usb_submit_urb failed with result %d",
ea3e6c59 104 __func__, status);
1da177e4
LT
105}
106
7d12e780 107static void iforce_usb_out(struct urb *urb)
1da177e4
LT
108{
109 struct iforce *iforce = urb->context;
110
111 if (urb->status) {
598972d4 112 dbg("urb->status %d, exiting", urb->status);
1da177e4
LT
113 return;
114 }
115
116 iforce_usb_xmit(iforce);
117
118 wake_up(&iforce->wait);
119}
120
7d12e780 121static void iforce_usb_ctrl(struct urb *urb)
1da177e4
LT
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
129static 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;
17dd3f0f 136 int err = -ENOMEM;
1da177e4
LT
137
138 interface = intf->cur_altsetting;
139
140 epirq = &interface->endpoint[0].desc;
141 epout = &interface->endpoint[1].desc;
142
17dd3f0f 143 if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
1da177e4
LT
144 goto fail;
145
17dd3f0f 146 if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4 147 goto fail;
1da177e4 148
17dd3f0f 149 if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4 150 goto fail;
1da177e4 151
17dd3f0f 152 if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4 153 goto fail;
1da177e4
LT
154
155 iforce->bus = IFORCE_USB;
156 iforce->usbdev = dev;
157
158 iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
159 iforce->cr.wIndex = 0;
6b8588f7 160 iforce->cr.wLength = cpu_to_le16(16);
1da177e4
LT
161
162 usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
163 iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
164
b8691fd2
JD
165 usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
166 iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
1da177e4
LT
167
168 usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
169 (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
170
17dd3f0f
DT
171 err = iforce_init_device(iforce);
172 if (err)
173 goto fail;
1da177e4
LT
174
175 usb_set_intfdata(intf, iforce);
176 return 0;
177
178fail:
179 if (iforce) {
8f21d119
MK
180 usb_free_urb(iforce->irq);
181 usb_free_urb(iforce->out);
182 usb_free_urb(iforce->ctrl);
1da177e4
LT
183 kfree(iforce);
184 }
185
17dd3f0f 186 return err;
1da177e4
LT
187}
188
189/* Called by iforce_delete() */
190void iforce_usb_delete(struct iforce* iforce)
191{
598972d4
JD
192 usb_kill_urb(iforce->irq);
193 usb_kill_urb(iforce->out);
194 usb_kill_urb(iforce->ctrl);
1da177e4
LT
195
196 usb_free_urb(iforce->irq);
197 usb_free_urb(iforce->out);
198 usb_free_urb(iforce->ctrl);
199}
200
201static void iforce_usb_disconnect(struct usb_interface *intf)
202{
203 struct iforce *iforce = usb_get_intfdata(intf);
204 int open = 0; /* FIXME! iforce->dev.handle->open; */
205
206 usb_set_intfdata(intf, NULL);
207 if (iforce) {
208 iforce->usbdev = NULL;
17dd3f0f 209 input_unregister_device(iforce->dev);
1da177e4
LT
210
211 if (!open) {
212 iforce_delete_device(iforce);
213 kfree(iforce);
214 }
215 }
216}
217
218static struct usb_device_id iforce_usb_ids [] = {
219 { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */
220 { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */
221 { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */
222 { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */
223 { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */
224 { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */
225 { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */
68947b8f 226 { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */
1da177e4
LT
227 { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */
228 { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */
18098a6c 229 { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */
1da177e4
LT
230 { } /* Terminating entry */
231};
232
233MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
234
235struct usb_driver iforce_usb_driver = {
1da177e4
LT
236 .name = "iforce",
237 .probe = iforce_usb_probe,
238 .disconnect = iforce_usb_disconnect,
239 .id_table = iforce_usb_ids,
240};