blob: 8ec8b6d56a1352dd7c8d01a6c0b134338d2e42c5 [file] [log] [blame]
Inaky Perez-Gonzalez795038102008-12-20 16:57:52 -08001/*
2 * Intel Wireless WiMAX Connection 2400m
3 * Firmware uploader's USB specifics
4 *
5 *
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 *
35 * Intel Corporation <linux-wimax@intel.com>
36 * Yanir Lubetkin <yanirx.lubetkin@intel.com>
37 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
38 * - Initial implementation
39 *
40 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
41 * - bus generic/specific split
42 *
43 * THE PROCEDURE
44 *
45 * See fw.c for the generic description of this procedure.
46 *
47 * This file implements only the USB specifics. It boils down to how
48 * to send a command and waiting for an acknowledgement from the
49 * device.
50 *
51 * This code (and process) is single threaded. It assumes it is the
52 * only thread poking around (guaranteed by fw.c).
53 *
54 * COMMAND EXECUTION
55 *
56 * A write URB is posted with the buffer to the bulk output endpoint.
57 *
58 * ACK RECEPTION
59 *
60 * We just post a URB to the notification endpoint and wait for
61 * data. We repeat until we get all the data we expect (as indicated
62 * by the call from the bus generic code).
63 *
64 * The data is not read from the bulk in endpoint for boot mode.
65 *
66 * ROADMAP
67 *
68 * i2400mu_bus_bm_cmd_send
69 * i2400m_bm_cmd_prepare...
70 * i2400mu_tx_bulk_out
71 *
72 * i2400mu_bus_bm_wait_for_ack
73 * i2400m_notif_submit
74 */
75#include <linux/usb.h>
76#include "i2400m-usb.h"
77
78
79#define D_SUBMODULE fw
80#include "usb-debug-levels.h"
81
82
83/*
84 * Synchronous write to the device
85 *
86 * Takes care of updating EDC counts and thus, handle device errors.
87 */
88static
89ssize_t i2400mu_tx_bulk_out(struct i2400mu *i2400mu, void *buf, size_t buf_size)
90{
91 int result;
92 struct device *dev = &i2400mu->usb_iface->dev;
93 int len;
94 struct usb_endpoint_descriptor *epd;
95 int pipe, do_autopm = 1;
96
97 result = usb_autopm_get_interface(i2400mu->usb_iface);
98 if (result < 0) {
99 dev_err(dev, "BM-CMD: can't get autopm: %d\n", result);
100 do_autopm = 0;
101 }
Dirk Brandewie20935862009-08-12 11:29:46 -0700102 epd = usb_get_epd(i2400mu->usb_iface, i2400mu->endpoint_cfg.bulk_out);
Inaky Perez-Gonzalez795038102008-12-20 16:57:52 -0800103 pipe = usb_sndbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
104retry:
Inaky Perez-Gonzalez296bd4b2009-10-20 11:09:25 +0900105 result = usb_bulk_msg(i2400mu->usb_dev, pipe, buf, buf_size, &len, 200);
Inaky Perez-Gonzalez795038102008-12-20 16:57:52 -0800106 switch (result) {
107 case 0:
108 if (len != buf_size) {
109 dev_err(dev, "BM-CMD: short write (%u B vs %zu "
110 "expected)\n", len, buf_size);
111 result = -EIO;
112 break;
113 }
114 result = len;
115 break;
116 case -EINVAL: /* while removing driver */
117 case -ENODEV: /* dev disconnect ... */
118 case -ENOENT: /* just ignore it */
119 case -ESHUTDOWN: /* and exit */
120 case -ECONNRESET:
121 result = -ESHUTDOWN;
122 break;
123 case -ETIMEDOUT: /* bah... */
124 break;
125 default: /* any other? */
126 if (edc_inc(&i2400mu->urb_edc,
127 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
128 dev_err(dev, "BM-CMD: maximum errors in "
129 "URB exceeded; resetting device\n");
130 usb_queue_reset_device(i2400mu->usb_iface);
131 result = -ENODEV;
132 break;
133 }
134 dev_err(dev, "BM-CMD: URB error %d, retrying\n",
135 result);
136 goto retry;
137 }
138 result = len;
139 if (do_autopm)
140 usb_autopm_put_interface(i2400mu->usb_iface);
141 return result;
142}
143
144
145/*
146 * Send a boot-mode command over the bulk-out pipe
147 *
148 * Command can be a raw command, which requires no preparation (and
149 * which might not even be following the command format). Checks that
150 * the right amount of data was transfered.
151 *
152 * To satisfy USB requirements (no onstack, vmalloc or in data segment
153 * buffers), we copy the command to i2400m->bm_cmd_buf and send it from
154 * there.
155 *
156 * @flags: pass thru from i2400m_bm_cmd()
157 * @return: cmd_size if ok, < 0 errno code on error.
158 */
159ssize_t i2400mu_bus_bm_cmd_send(struct i2400m *i2400m,
160 const struct i2400m_bootrom_header *_cmd,
161 size_t cmd_size, int flags)
162{
163 ssize_t result;
164 struct device *dev = i2400m_dev(i2400m);
165 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
166 int opcode = _cmd == NULL ? -1 : i2400m_brh_get_opcode(_cmd);
167 struct i2400m_bootrom_header *cmd;
168 size_t cmd_size_a = ALIGN(cmd_size, 16); /* USB restriction */
169
170 d_fnstart(8, dev, "(i2400m %p cmd %p size %zu)\n",
171 i2400m, _cmd, cmd_size);
172 result = -E2BIG;
173 if (cmd_size > I2400M_BM_CMD_BUF_SIZE)
174 goto error_too_big;
Inaky Perez-Gonzalez77e12512009-08-31 17:15:49 -0700175 if (_cmd != i2400m->bm_cmd_buf)
176 memmove(i2400m->bm_cmd_buf, _cmd, cmd_size);
Inaky Perez-Gonzalez795038102008-12-20 16:57:52 -0800177 cmd = i2400m->bm_cmd_buf;
178 if (cmd_size_a > cmd_size) /* Zero pad space */
179 memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
180 if ((flags & I2400M_BM_CMD_RAW) == 0) {
181 if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
182 dev_warn(dev, "SW BUG: response_required == 0\n");
183 i2400m_bm_cmd_prepare(cmd);
184 }
185 result = i2400mu_tx_bulk_out(i2400mu, i2400m->bm_cmd_buf, cmd_size);
186 if (result < 0) {
187 dev_err(dev, "boot-mode cmd %d: cannot send: %zd\n",
188 opcode, result);
189 goto error_cmd_send;
190 }
191 if (result != cmd_size) { /* all was transferred? */
192 dev_err(dev, "boot-mode cmd %d: incomplete transfer "
193 "(%zu vs %zu submitted)\n", opcode, result, cmd_size);
194 result = -EIO;
195 goto error_cmd_size;
196 }
197error_cmd_size:
198error_cmd_send:
199error_too_big:
200 d_fnend(8, dev, "(i2400m %p cmd %p size %zu) = %zd\n",
201 i2400m, _cmd, cmd_size, result);
202 return result;
203}
204
205
206static
207void __i2400mu_bm_notif_cb(struct urb *urb)
208{
209 complete(urb->context);
210}
211
212
213/*
214 * submit a read to the notification endpoint
215 *
216 * @i2400m: device descriptor
217 * @urb: urb to use
218 * @completion: completion varible to complete when done
219 *
220 * Data is always read to i2400m->bm_ack_buf
221 */
222static
223int i2400mu_notif_submit(struct i2400mu *i2400mu, struct urb *urb,
224 struct completion *completion)
225{
226 struct i2400m *i2400m = &i2400mu->i2400m;
227 struct usb_endpoint_descriptor *epd;
228 int pipe;
229
Dirk Brandewie20935862009-08-12 11:29:46 -0700230 epd = usb_get_epd(i2400mu->usb_iface,
231 i2400mu->endpoint_cfg.notification);
Inaky Perez-Gonzalez795038102008-12-20 16:57:52 -0800232 pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
233 usb_fill_int_urb(urb, i2400mu->usb_dev, pipe,
234 i2400m->bm_ack_buf, I2400M_BM_ACK_BUF_SIZE,
235 __i2400mu_bm_notif_cb, completion,
236 epd->bInterval);
237 return usb_submit_urb(urb, GFP_KERNEL);
238}
239
240
241/*
242 * Read an ack from the notification endpoint
243 *
244 * @i2400m:
245 * @_ack: pointer to where to store the read data
246 * @ack_size: how many bytes we should read
247 *
248 * Returns: < 0 errno code on error; otherwise, amount of received bytes.
249 *
250 * Submits a notification read, appends the read data to the given ack
251 * buffer and then repeats (until @ack_size bytes have been
252 * received).
253 */
254ssize_t i2400mu_bus_bm_wait_for_ack(struct i2400m *i2400m,
255 struct i2400m_bootrom_header *_ack,
256 size_t ack_size)
257{
258 ssize_t result = -ENOMEM;
259 struct device *dev = i2400m_dev(i2400m);
260 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
261 struct urb notif_urb;
262 void *ack = _ack;
263 size_t offset, len;
264 long val;
265 int do_autopm = 1;
266 DECLARE_COMPLETION_ONSTACK(notif_completion);
267
268 d_fnstart(8, dev, "(i2400m %p ack %p size %zu)\n",
269 i2400m, ack, ack_size);
270 BUG_ON(_ack == i2400m->bm_ack_buf);
271 result = usb_autopm_get_interface(i2400mu->usb_iface);
272 if (result < 0) {
273 dev_err(dev, "BM-ACK: can't get autopm: %d\n", (int) result);
274 do_autopm = 0;
275 }
276 usb_init_urb(&notif_urb); /* ready notifications */
277 usb_get_urb(&notif_urb);
278 offset = 0;
279 while (offset < ack_size) {
280 init_completion(&notif_completion);
281 result = i2400mu_notif_submit(i2400mu, &notif_urb,
282 &notif_completion);
283 if (result < 0)
284 goto error_notif_urb_submit;
285 val = wait_for_completion_interruptible_timeout(
286 &notif_completion, HZ);
287 if (val == 0) {
288 result = -ETIMEDOUT;
289 usb_kill_urb(&notif_urb); /* Timedout */
290 goto error_notif_wait;
291 }
292 if (val == -ERESTARTSYS) {
293 result = -EINTR; /* Interrupted */
294 usb_kill_urb(&notif_urb);
295 goto error_notif_wait;
296 }
297 result = notif_urb.status; /* How was the ack? */
298 switch (result) {
299 case 0:
300 break;
301 case -EINVAL: /* while removing driver */
302 case -ENODEV: /* dev disconnect ... */
303 case -ENOENT: /* just ignore it */
304 case -ESHUTDOWN: /* and exit */
305 case -ECONNRESET:
306 result = -ESHUTDOWN;
307 goto error_dev_gone;
308 default: /* any other? */
309 usb_kill_urb(&notif_urb); /* Timedout */
310 if (edc_inc(&i2400mu->urb_edc,
311 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
312 goto error_exceeded;
313 dev_err(dev, "BM-ACK: URB error %d, "
314 "retrying\n", notif_urb.status);
315 continue; /* retry */
316 }
317 if (notif_urb.actual_length == 0) {
318 d_printf(6, dev, "ZLP received, retrying\n");
319 continue;
320 }
321 /* Got data, append it to the buffer */
322 len = min(ack_size - offset, (size_t) notif_urb.actual_length);
323 memcpy(ack + offset, i2400m->bm_ack_buf, len);
324 offset += len;
325 }
326 result = offset;
327error_notif_urb_submit:
328error_notif_wait:
329error_dev_gone:
330out:
331 if (do_autopm)
332 usb_autopm_put_interface(i2400mu->usb_iface);
Inaky Perez-Gonzalez59bdc4b2009-09-02 15:41:23 -0700333 d_fnend(8, dev, "(i2400m %p ack %p size %zu) = %ld\n",
334 i2400m, ack, ack_size, (long) result);
Inaky Perez-Gonzalez795038102008-12-20 16:57:52 -0800335 return result;
336
337error_exceeded:
338 dev_err(dev, "bm: maximum errors in notification URB exceeded; "
339 "resetting device\n");
340 usb_queue_reset_device(i2400mu->usb_iface);
341 goto out;
342}