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