1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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)) ) {
66 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
67 dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n);
68 }
69
70
71
72
73 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
74}
75
76static void iforce_usb_irq(struct urb *urb)
77{
78 struct iforce *iforce = urb->context;
79 struct device *dev = &iforce->intf->dev;
80 int status;
81
82 switch (urb->status) {
83 case 0:
84
85 break;
86 case -ECONNRESET:
87 case -ENOENT:
88 case -ESHUTDOWN:
89
90 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
91 __func__, urb->status);
92 return;
93 default:
94 dev_dbg(dev, "%s - urb has status of: %d\n",
95 __func__, urb->status);
96 goto exit;
97 }
98
99 iforce_process_packet(iforce,
100 (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
101
102exit:
103 status = usb_submit_urb (urb, GFP_ATOMIC);
104 if (status)
105 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
106 __func__, status);
107}
108
109static void iforce_usb_out(struct urb *urb)
110{
111 struct iforce *iforce = urb->context;
112
113 if (urb->status) {
114 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
115 dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n",
116 urb->status);
117 return;
118 }
119
120 iforce_usb_xmit(iforce);
121
122 wake_up(&iforce->wait);
123}
124
125static void iforce_usb_ctrl(struct urb *urb)
126{
127 struct iforce *iforce = urb->context;
128 if (urb->status) return;
129 iforce->ecmd = 0xff00 | urb->actual_length;
130 wake_up(&iforce->wait);
131}
132
133static int iforce_usb_probe(struct usb_interface *intf,
134 const struct usb_device_id *id)
135{
136 struct usb_device *dev = interface_to_usbdev(intf);
137 struct usb_host_interface *interface;
138 struct usb_endpoint_descriptor *epirq, *epout;
139 struct iforce *iforce;
140 int err = -ENOMEM;
141
142 interface = intf->cur_altsetting;
143
144 epirq = &interface->endpoint[0].desc;
145 epout = &interface->endpoint[1].desc;
146
147 if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
148 goto fail;
149
150 if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
151 goto fail;
152
153 if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
154 goto fail;
155
156 if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
157 goto fail;
158
159 iforce->bus = IFORCE_USB;
160 iforce->usbdev = dev;
161 iforce->intf = intf;
162
163 iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
164 iforce->cr.wIndex = 0;
165 iforce->cr.wLength = cpu_to_le16(16);
166
167 usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
168 iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
169
170 usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
171 iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
172
173 usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
174 (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
175
176 err = iforce_init_device(iforce);
177 if (err)
178 goto fail;
179
180 usb_set_intfdata(intf, iforce);
181 return 0;
182
183fail:
184 if (iforce) {
185 usb_free_urb(iforce->irq);
186 usb_free_urb(iforce->out);
187 usb_free_urb(iforce->ctrl);
188 kfree(iforce);
189 }
190
191 return err;
192}
193
194static void iforce_usb_disconnect(struct usb_interface *intf)
195{
196 struct iforce *iforce = usb_get_intfdata(intf);
197
198 usb_set_intfdata(intf, NULL);
199
200 input_unregister_device(iforce->dev);
201
202 usb_free_urb(iforce->irq);
203 usb_free_urb(iforce->out);
204 usb_free_urb(iforce->ctrl);
205
206 kfree(iforce);
207}
208
209static struct usb_device_id iforce_usb_ids [] = {
210 { USB_DEVICE(0x044f, 0xa01c) },
211 { USB_DEVICE(0x046d, 0xc281) },
212 { USB_DEVICE(0x046d, 0xc291) },
213 { USB_DEVICE(0x05ef, 0x020a) },
214 { USB_DEVICE(0x05ef, 0x8884) },
215 { USB_DEVICE(0x05ef, 0x8888) },
216 { USB_DEVICE(0x061c, 0xc0a4) },
217 { USB_DEVICE(0x061c, 0xc084) },
218 { USB_DEVICE(0x06f8, 0x0001) },
219 { USB_DEVICE(0x06f8, 0x0003) },
220 { USB_DEVICE(0x06f8, 0x0004) },
221 { USB_DEVICE(0x06f8, 0xa302) },
222 { }
223};
224
225MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
226
227struct usb_driver iforce_usb_driver = {
228 .name = "iforce",
229 .probe = iforce_usb_probe,
230 .disconnect = iforce_usb_disconnect,
231 .id_table = iforce_usb_ids,
232};
233