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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#include <linux/workqueue.h>
53#include <linux/ctype.h>
54#include <linux/slab.h>
55
56#include "wa-hc.h"
57#include "wusbhc.h"
58
59
60struct wa_notif_work {
61 struct work_struct work;
62 struct wahc *wa;
63 size_t size;
64 u8 data[];
65};
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92static void wa_notif_dispatch(struct work_struct *ws)
93{
94 void *itr;
95 u8 missing = 0;
96 struct wa_notif_work *nw = container_of(ws, struct wa_notif_work, work);
97 struct wahc *wa = nw->wa;
98 struct wa_notif_hdr *notif_hdr;
99 size_t size;
100
101 struct device *dev = &wa->usb_iface->dev;
102
103#if 0
104
105 if (usb_hcd->state == HC_STATE_QUIESCING)
106 goto out;
107#endif
108 atomic_dec(&wa->notifs_queued);
109 dev = &wa->usb_iface->dev;
110 size = nw->size;
111 itr = nw->data;
112
113 while (size) {
114 if (size < sizeof(*notif_hdr)) {
115 missing = sizeof(*notif_hdr) - size;
116 goto exhausted_buffer;
117 }
118 notif_hdr = itr;
119 if (size < notif_hdr->bLength)
120 goto exhausted_buffer;
121 itr += notif_hdr->bLength;
122 size -= notif_hdr->bLength;
123
124 switch (notif_hdr->bNotifyType) {
125 case HWA_NOTIF_DN: {
126 struct hwa_notif_dn *hwa_dn;
127 hwa_dn = container_of(notif_hdr, struct hwa_notif_dn,
128 hdr);
129 wusbhc_handle_dn(wa->wusb, hwa_dn->bSourceDeviceAddr,
130 hwa_dn->dndata,
131 notif_hdr->bLength - sizeof(*hwa_dn));
132 break;
133 }
134 case WA_NOTIF_TRANSFER:
135 wa_handle_notif_xfer(wa, notif_hdr);
136 break;
137 case HWA_NOTIF_BPST_ADJ:
138 break;
139 case DWA_NOTIF_RWAKE:
140 case DWA_NOTIF_PORTSTATUS:
141
142
143 default:
144 dev_err(dev, "HWA: unknown notification 0x%x, "
145 "%zu bytes; discarding\n",
146 notif_hdr->bNotifyType,
147 (size_t)notif_hdr->bLength);
148 break;
149 }
150 }
151out:
152 wa_put(wa);
153 kfree(nw);
154 return;
155
156
157
158
159
160
161exhausted_buffer:
162 dev_warn(dev, "HWA: device sent short notification, "
163 "%d bytes missing; discarding %d bytes.\n",
164 missing, (int)size);
165 goto out;
166}
167
168
169
170
171
172
173
174
175
176
177
178
179
180static int wa_nep_queue(struct wahc *wa, size_t size)
181{
182 int result = 0;
183 struct device *dev = &wa->usb_iface->dev;
184 struct wa_notif_work *nw;
185
186
187 BUG_ON(size > wa->nep_buffer_size);
188 if (size == 0)
189 goto out;
190 if (atomic_read(&wa->notifs_queued) > 200) {
191 if (printk_ratelimit())
192 dev_err(dev, "Too many notifications queued, "
193 "throttling back\n");
194 goto out;
195 }
196 nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
197 if (nw == NULL) {
198 if (printk_ratelimit())
199 dev_err(dev, "No memory to queue notification\n");
200 goto out;
201 }
202 INIT_WORK(&nw->work, wa_notif_dispatch);
203 nw->wa = wa_get(wa);
204 nw->size = size;
205 memcpy(nw->data, wa->nep_buffer, size);
206 atomic_inc(&wa->notifs_queued);
207 queue_work(wusbd, &nw->work);
208out:
209
210 return result;
211}
212
213
214
215
216
217
218
219static void wa_nep_cb(struct urb *urb)
220{
221 int result;
222 struct wahc *wa = urb->context;
223 struct device *dev = &wa->usb_iface->dev;
224
225 switch (result = urb->status) {
226 case 0:
227 result = wa_nep_queue(wa, urb->actual_length);
228 if (result < 0)
229 dev_err(dev, "NEP: unable to process notification(s): "
230 "%d\n", result);
231 break;
232 case -ECONNRESET:
233 case -ENOENT:
234 case -ESHUTDOWN:
235 dev_dbg(dev, "NEP: going down %d\n", urb->status);
236 goto out;
237 default:
238 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
239 EDC_ERROR_TIMEFRAME)) {
240 dev_err(dev, "NEP: URB max acceptable errors "
241 "exceeded, resetting device\n");
242 wa_reset_all(wa);
243 goto out;
244 }
245 dev_err(dev, "NEP: URB error %d\n", urb->status);
246 }
247 result = wa_nep_arm(wa, GFP_ATOMIC);
248 if (result < 0) {
249 dev_err(dev, "NEP: cannot submit URB: %d\n", result);
250 wa_reset_all(wa);
251 }
252out:
253 return;
254}
255
256
257
258
259
260
261
262int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
263{
264 int result;
265 struct usb_endpoint_descriptor *epd;
266 struct usb_device *usb_dev = interface_to_usbdev(iface);
267 struct device *dev = &iface->dev;
268
269 edc_init(&wa->nep_edc);
270 epd = &iface->cur_altsetting->endpoint[0].desc;
271 wa->nep_buffer_size = 1024;
272 wa->nep_buffer = kmalloc(wa->nep_buffer_size, GFP_KERNEL);
273 if (wa->nep_buffer == NULL) {
274 dev_err(dev, "Unable to allocate notification's read buffer\n");
275 goto error_nep_buffer;
276 }
277 wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
278 if (wa->nep_urb == NULL) {
279 dev_err(dev, "Unable to allocate notification URB\n");
280 goto error_urb_alloc;
281 }
282 usb_fill_int_urb(wa->nep_urb, usb_dev,
283 usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
284 wa->nep_buffer, wa->nep_buffer_size,
285 wa_nep_cb, wa, epd->bInterval);
286 result = wa_nep_arm(wa, GFP_KERNEL);
287 if (result < 0) {
288 dev_err(dev, "Cannot submit notification URB: %d\n", result);
289 goto error_nep_arm;
290 }
291 return 0;
292
293error_nep_arm:
294 usb_free_urb(wa->nep_urb);
295error_urb_alloc:
296 kfree(wa->nep_buffer);
297error_nep_buffer:
298 return -ENOMEM;
299}
300
301void wa_nep_destroy(struct wahc *wa)
302{
303 wa_nep_disarm(wa);
304 usb_free_urb(wa->nep_urb);
305 kfree(wa->nep_buffer);
306}
307