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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75#include <linux/usb.h>
76#include <linux/gfp.h>
77#include "i2400m-usb.h"
78
79
80#define D_SUBMODULE fw
81#include "usb-debug-levels.h"
82
83
84
85
86
87
88
89static
90ssize_t i2400mu_tx_bulk_out(struct i2400mu *i2400mu, void *buf, size_t buf_size)
91{
92 int result;
93 struct device *dev = &i2400mu->usb_iface->dev;
94 int len;
95 struct usb_endpoint_descriptor *epd;
96 int pipe, do_autopm = 1;
97
98 result = usb_autopm_get_interface(i2400mu->usb_iface);
99 if (result < 0) {
100 dev_err(dev, "BM-CMD: can't get autopm: %d\n", result);
101 do_autopm = 0;
102 }
103 epd = usb_get_epd(i2400mu->usb_iface, i2400mu->endpoint_cfg.bulk_out);
104 pipe = usb_sndbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
105retry:
106 result = usb_bulk_msg(i2400mu->usb_dev, pipe, buf, buf_size, &len, 200);
107 switch (result) {
108 case 0:
109 if (len != buf_size) {
110 dev_err(dev, "BM-CMD: short write (%u B vs %zu "
111 "expected)\n", len, buf_size);
112 result = -EIO;
113 break;
114 }
115 result = len;
116 break;
117 case -EPIPE:
118
119
120
121
122
123
124
125
126
127
128 if (edc_inc(&i2400mu->urb_edc,
129 10 * EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
130 dev_err(dev, "BM-CMD: too many stalls in "
131 "URB; resetting device\n");
132 usb_queue_reset_device(i2400mu->usb_iface);
133
134 } else {
135 usb_clear_halt(i2400mu->usb_dev, pipe);
136 msleep(10);
137 goto retry;
138 }
139 case -EINVAL:
140 case -ENODEV:
141 case -ENOENT:
142 case -ESHUTDOWN:
143 case -ECONNRESET:
144 result = -ESHUTDOWN;
145 break;
146 case -ETIMEDOUT:
147 break;
148 default:
149 if (edc_inc(&i2400mu->urb_edc,
150 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
151 dev_err(dev, "BM-CMD: maximum errors in "
152 "URB exceeded; resetting device\n");
153 usb_queue_reset_device(i2400mu->usb_iface);
154 result = -ENODEV;
155 break;
156 }
157 dev_err(dev, "BM-CMD: URB error %d, retrying\n",
158 result);
159 goto retry;
160 }
161 if (do_autopm)
162 usb_autopm_put_interface(i2400mu->usb_iface);
163 return result;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181ssize_t i2400mu_bus_bm_cmd_send(struct i2400m *i2400m,
182 const struct i2400m_bootrom_header *_cmd,
183 size_t cmd_size, int flags)
184{
185 ssize_t result;
186 struct device *dev = i2400m_dev(i2400m);
187 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
188 int opcode = _cmd == NULL ? -1 : i2400m_brh_get_opcode(_cmd);
189 struct i2400m_bootrom_header *cmd;
190 size_t cmd_size_a = ALIGN(cmd_size, 16);
191
192 d_fnstart(8, dev, "(i2400m %p cmd %p size %zu)\n",
193 i2400m, _cmd, cmd_size);
194 result = -E2BIG;
195 if (cmd_size > I2400M_BM_CMD_BUF_SIZE)
196 goto error_too_big;
197 if (_cmd != i2400m->bm_cmd_buf)
198 memmove(i2400m->bm_cmd_buf, _cmd, cmd_size);
199 cmd = i2400m->bm_cmd_buf;
200 if (cmd_size_a > cmd_size)
201 memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
202 if ((flags & I2400M_BM_CMD_RAW) == 0) {
203 if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
204 dev_warn(dev, "SW BUG: response_required == 0\n");
205 i2400m_bm_cmd_prepare(cmd);
206 }
207 result = i2400mu_tx_bulk_out(i2400mu, i2400m->bm_cmd_buf, cmd_size);
208 if (result < 0) {
209 dev_err(dev, "boot-mode cmd %d: cannot send: %zd\n",
210 opcode, result);
211 goto error_cmd_send;
212 }
213 if (result != cmd_size) {
214 dev_err(dev, "boot-mode cmd %d: incomplete transfer "
215 "(%zd vs %zu submitted)\n", opcode, result, cmd_size);
216 result = -EIO;
217 goto error_cmd_size;
218 }
219error_cmd_size:
220error_cmd_send:
221error_too_big:
222 d_fnend(8, dev, "(i2400m %p cmd %p size %zu) = %zd\n",
223 i2400m, _cmd, cmd_size, result);
224 return result;
225}
226
227
228static
229void __i2400mu_bm_notif_cb(struct urb *urb)
230{
231 complete(urb->context);
232}
233
234
235
236
237
238
239
240
241
242
243
244static
245int i2400mu_notif_submit(struct i2400mu *i2400mu, struct urb *urb,
246 struct completion *completion)
247{
248 struct i2400m *i2400m = &i2400mu->i2400m;
249 struct usb_endpoint_descriptor *epd;
250 int pipe;
251
252 epd = usb_get_epd(i2400mu->usb_iface,
253 i2400mu->endpoint_cfg.notification);
254 pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
255 usb_fill_int_urb(urb, i2400mu->usb_dev, pipe,
256 i2400m->bm_ack_buf, I2400M_BM_ACK_BUF_SIZE,
257 __i2400mu_bm_notif_cb, completion,
258 epd->bInterval);
259 return usb_submit_urb(urb, GFP_KERNEL);
260}
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276ssize_t i2400mu_bus_bm_wait_for_ack(struct i2400m *i2400m,
277 struct i2400m_bootrom_header *_ack,
278 size_t ack_size)
279{
280 ssize_t result = -ENOMEM;
281 struct device *dev = i2400m_dev(i2400m);
282 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
283 struct urb notif_urb;
284 void *ack = _ack;
285 size_t offset, len;
286 long val;
287 int do_autopm = 1;
288 DECLARE_COMPLETION_ONSTACK(notif_completion);
289
290 d_fnstart(8, dev, "(i2400m %p ack %p size %zu)\n",
291 i2400m, ack, ack_size);
292 BUG_ON(_ack == i2400m->bm_ack_buf);
293 result = usb_autopm_get_interface(i2400mu->usb_iface);
294 if (result < 0) {
295 dev_err(dev, "BM-ACK: can't get autopm: %d\n", (int) result);
296 do_autopm = 0;
297 }
298 usb_init_urb(¬if_urb);
299 usb_get_urb(¬if_urb);
300 offset = 0;
301 while (offset < ack_size) {
302 init_completion(¬if_completion);
303 result = i2400mu_notif_submit(i2400mu, ¬if_urb,
304 ¬if_completion);
305 if (result < 0)
306 goto error_notif_urb_submit;
307 val = wait_for_completion_interruptible_timeout(
308 ¬if_completion, HZ);
309 if (val == 0) {
310 result = -ETIMEDOUT;
311 usb_kill_urb(¬if_urb);
312 goto error_notif_wait;
313 }
314 if (val == -ERESTARTSYS) {
315 result = -EINTR;
316 usb_kill_urb(¬if_urb);
317 goto error_notif_wait;
318 }
319 result = notif_urb.status;
320 switch (result) {
321 case 0:
322 break;
323 case -EINVAL:
324 case -ENODEV:
325 case -ENOENT:
326 case -ESHUTDOWN:
327 case -ECONNRESET:
328 result = -ESHUTDOWN;
329 goto error_dev_gone;
330 default:
331 usb_kill_urb(¬if_urb);
332 if (edc_inc(&i2400mu->urb_edc,
333 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
334 goto error_exceeded;
335 dev_err(dev, "BM-ACK: URB error %d, "
336 "retrying\n", notif_urb.status);
337 continue;
338 }
339 if (notif_urb.actual_length == 0) {
340 d_printf(6, dev, "ZLP received, retrying\n");
341 continue;
342 }
343
344 len = min(ack_size - offset, (size_t) notif_urb.actual_length);
345 memcpy(ack + offset, i2400m->bm_ack_buf, len);
346 offset += len;
347 }
348 result = offset;
349error_notif_urb_submit:
350error_notif_wait:
351error_dev_gone:
352out:
353 if (do_autopm)
354 usb_autopm_put_interface(i2400mu->usb_iface);
355 d_fnend(8, dev, "(i2400m %p ack %p size %zu) = %ld\n",
356 i2400m, ack, ack_size, (long) result);
357 return result;
358
359error_exceeded:
360 dev_err(dev, "bm: maximum errors in notification URB exceeded; "
361 "resetting device\n");
362 usb_queue_reset_device(i2400mu->usb_iface);
363 goto out;
364}
365