d4e7a24922cd337aafd4201e4d8361da9ee96027
[sfrench/cifs-2.6.git] / drivers / input / joystick / iforce / iforce-usb.c
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 struct iforce_usb {
27         struct iforce iforce;
28
29         struct usb_device *usbdev;
30         struct usb_interface *intf;
31         struct urb *irq, *out, *ctrl;
32         struct usb_ctrlrequest cr;
33 };
34
35 static void __iforce_usb_xmit(struct iforce *iforce)
36 {
37         struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
38                                                      iforce);
39         int n, c;
40         unsigned long flags;
41
42         spin_lock_irqsave(&iforce->xmit_lock, flags);
43
44         if (iforce->xmit.head == iforce->xmit.tail) {
45                 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
46                 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
47                 return;
48         }
49
50         ((char *)iforce_usb->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
51         XMIT_INC(iforce->xmit.tail, 1);
52         n = iforce->xmit.buf[iforce->xmit.tail];
53         XMIT_INC(iforce->xmit.tail, 1);
54
55         iforce_usb->out->transfer_buffer_length = n + 1;
56         iforce_usb->out->dev = iforce_usb->usbdev;
57
58         /* Copy rest of data then */
59         c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
60         if (n < c) c=n;
61
62         memcpy(iforce_usb->out->transfer_buffer + 1,
63                &iforce->xmit.buf[iforce->xmit.tail],
64                c);
65         if (n != c) {
66                 memcpy(iforce_usb->out->transfer_buffer + 1 + c,
67                        &iforce->xmit.buf[0],
68                        n-c);
69         }
70         XMIT_INC(iforce->xmit.tail, n);
71
72         if ( (n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC)) ) {
73                 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
74                 dev_warn(&iforce_usb->intf->dev,
75                          "usb_submit_urb failed %d\n", n);
76         }
77
78         /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
79          * As long as the urb completion handler is not called, the transmiting
80          * is considered to be running */
81         spin_unlock_irqrestore(&iforce->xmit_lock, flags);
82 }
83
84 static void iforce_usb_xmit(struct iforce *iforce)
85 {
86         if (!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags))
87                 __iforce_usb_xmit(iforce);
88 }
89
90 static int iforce_usb_get_id(struct iforce *iforce, u8 *packet)
91 {
92         struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
93                                                      iforce);
94         int status;
95
96         iforce_usb->cr.bRequest = packet[0];
97         iforce_usb->ctrl->dev = iforce_usb->usbdev;
98
99         status = usb_submit_urb(iforce_usb->ctrl, GFP_KERNEL);
100         if (status) {
101                 dev_err(&iforce_usb->intf->dev,
102                         "usb_submit_urb failed %d\n", status);
103                 return -EIO;
104         }
105
106         wait_event_interruptible_timeout(iforce->wait,
107                 iforce_usb->ctrl->status != -EINPROGRESS, HZ);
108
109         if (iforce_usb->ctrl->status) {
110                 dev_dbg(&iforce_usb->intf->dev,
111                         "iforce->ctrl->status = %d\n",
112                         iforce_usb->ctrl->status);
113                 usb_unlink_urb(iforce_usb->ctrl);
114                 return -EIO;
115         }
116
117         return -(iforce->edata[0] != packet[0]);
118 }
119
120 static int iforce_usb_start_io(struct iforce *iforce)
121 {
122         struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
123                                                      iforce);
124
125         if (usb_submit_urb(iforce_usb->irq, GFP_KERNEL))
126                 return -EIO;
127
128         return 0;
129 }
130
131 static void iforce_usb_stop_io(struct iforce *iforce)
132 {
133         struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
134                                                      iforce);
135
136         usb_kill_urb(iforce_usb->irq);
137         usb_kill_urb(iforce_usb->out);
138         usb_kill_urb(iforce_usb->ctrl);
139 }
140
141 static const struct iforce_xport_ops iforce_usb_xport_ops = {
142         .xmit           = iforce_usb_xmit,
143         .get_id         = iforce_usb_get_id,
144         .start_io       = iforce_usb_start_io,
145         .stop_io        = iforce_usb_stop_io,
146 };
147
148 static void iforce_usb_irq(struct urb *urb)
149 {
150         struct iforce_usb *iforce_usb = urb->context;
151         struct iforce *iforce = &iforce_usb->iforce;
152         struct device *dev = &iforce_usb->intf->dev;
153         int status;
154
155         switch (urb->status) {
156         case 0:
157                 /* success */
158                 break;
159         case -ECONNRESET:
160         case -ENOENT:
161         case -ESHUTDOWN:
162                 /* this urb is terminated, clean up */
163                 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
164                         __func__, urb->status);
165                 return;
166         default:
167                 dev_dbg(dev, "%s - urb has status of: %d\n",
168                         __func__, urb->status);
169                 goto exit;
170         }
171
172         iforce_process_packet(iforce,
173                 (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
174
175 exit:
176         status = usb_submit_urb(urb, GFP_ATOMIC);
177         if (status)
178                 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
179                         __func__, status);
180 }
181
182 static void iforce_usb_out(struct urb *urb)
183 {
184         struct iforce_usb *iforce_usb = urb->context;
185         struct iforce *iforce = &iforce_usb->iforce;
186
187         if (urb->status) {
188                 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
189                 dev_dbg(&iforce_usb->intf->dev, "urb->status %d, exiting\n",
190                         urb->status);
191                 return;
192         }
193
194         __iforce_usb_xmit(iforce);
195
196         wake_up(&iforce->wait);
197 }
198
199 static void iforce_usb_ctrl(struct urb *urb)
200 {
201         struct iforce_usb *iforce_usb = urb->context;
202         struct iforce *iforce = &iforce_usb->iforce;
203
204         if (urb->status)
205                 return;
206
207         iforce->ecmd = 0xff00 | urb->actual_length;
208         wake_up(&iforce->wait);
209 }
210
211 static int iforce_usb_probe(struct usb_interface *intf,
212                                 const struct usb_device_id *id)
213 {
214         struct usb_device *dev = interface_to_usbdev(intf);
215         struct usb_host_interface *interface;
216         struct usb_endpoint_descriptor *epirq, *epout;
217         struct iforce_usb *iforce_usb;
218         struct iforce *iforce;
219         int err = -ENOMEM;
220
221         interface = intf->cur_altsetting;
222
223         if (interface->desc.bNumEndpoints < 2)
224                 return -ENODEV;
225
226         epirq = &interface->endpoint[0].desc;
227         epout = &interface->endpoint[1].desc;
228
229         if (!(iforce_usb = kzalloc(sizeof(*iforce_usb) + 32, GFP_KERNEL)))
230                 goto fail;
231
232         if (!(iforce_usb->irq = usb_alloc_urb(0, GFP_KERNEL)))
233                 goto fail;
234
235         if (!(iforce_usb->out = usb_alloc_urb(0, GFP_KERNEL)))
236                 goto fail;
237
238         if (!(iforce_usb->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
239                 goto fail;
240
241         iforce = &iforce_usb->iforce;
242
243         iforce->xport_ops = &iforce_usb_xport_ops;
244         iforce->bus = IFORCE_USB;
245
246         iforce_usb->usbdev = dev;
247         iforce_usb->intf = intf;
248
249         iforce_usb->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
250         iforce_usb->cr.wIndex = 0;
251         iforce_usb->cr.wLength = cpu_to_le16(16);
252
253         usb_fill_int_urb(iforce_usb->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
254                         iforce->data, 16, iforce_usb_irq, iforce_usb, epirq->bInterval);
255
256         usb_fill_int_urb(iforce_usb->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
257                         iforce_usb + 1, 32, iforce_usb_out, iforce_usb, epout->bInterval);
258
259         usb_fill_control_urb(iforce_usb->ctrl, dev, usb_rcvctrlpipe(dev, 0),
260                         (void*) &iforce_usb->cr, iforce->edata, 16, iforce_usb_ctrl, iforce_usb);
261
262         err = iforce_init_device(&intf->dev, BUS_USB, iforce);
263         if (err)
264                 goto fail;
265
266         usb_set_intfdata(intf, iforce_usb);
267         return 0;
268
269 fail:
270         if (iforce_usb) {
271                 usb_free_urb(iforce_usb->irq);
272                 usb_free_urb(iforce_usb->out);
273                 usb_free_urb(iforce_usb->ctrl);
274                 kfree(iforce_usb);
275         }
276
277         return err;
278 }
279
280 static void iforce_usb_disconnect(struct usb_interface *intf)
281 {
282         struct iforce_usb *iforce_usb = usb_get_intfdata(intf);
283
284         usb_set_intfdata(intf, NULL);
285
286         input_unregister_device(iforce_usb->iforce.dev);
287
288         usb_free_urb(iforce_usb->irq);
289         usb_free_urb(iforce_usb->out);
290         usb_free_urb(iforce_usb->ctrl);
291
292         kfree(iforce_usb);
293 }
294
295 static const struct usb_device_id iforce_usb_ids[] = {
296         { USB_DEVICE(0x044f, 0xa01c) },         /* Thrustmaster Motor Sport GT */
297         { USB_DEVICE(0x046d, 0xc281) },         /* Logitech WingMan Force */
298         { USB_DEVICE(0x046d, 0xc291) },         /* Logitech WingMan Formula Force */
299         { USB_DEVICE(0x05ef, 0x020a) },         /* AVB Top Shot Pegasus */
300         { USB_DEVICE(0x05ef, 0x8884) },         /* AVB Mag Turbo Force */
301         { USB_DEVICE(0x05ef, 0x8888) },         /* AVB Top Shot FFB Racing Wheel */
302         { USB_DEVICE(0x061c, 0xc0a4) },         /* ACT LABS Force RS */
303         { USB_DEVICE(0x061c, 0xc084) },         /* ACT LABS Force RS */
304         { USB_DEVICE(0x06f8, 0x0001) },         /* Guillemot Race Leader Force Feedback */
305         { USB_DEVICE(0x06f8, 0x0003) },         /* Guillemot Jet Leader Force Feedback */
306         { USB_DEVICE(0x06f8, 0x0004) },         /* Guillemot Force Feedback Racing Wheel */
307         { USB_DEVICE(0x06f8, 0xa302) },         /* Guillemot Jet Leader 3D */
308         { }                                     /* Terminating entry */
309 };
310
311 MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
312
313 struct usb_driver iforce_usb_driver = {
314         .name =         "iforce",
315         .probe =        iforce_usb_probe,
316         .disconnect =   iforce_usb_disconnect,
317         .id_table =     iforce_usb_ids,
318 };