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