2 * Intel Wireless WiMAX Connection 2400m
3 * Firmware uploader's USB specifics
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
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
40 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
41 * - bus generic/specific split
45 * See fw.c for the generic description of this procedure.
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
51 * This code (and process) is single threaded. It assumes it is the
52 * only thread poking around (guaranteed by fw.c).
56 * A write URB is posted with the buffer to the bulk output endpoint.
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).
64 * The data is not read from the bulk in endpoint for boot mode.
68 * i2400mu_bus_bm_cmd_send
69 * i2400m_bm_cmd_prepare...
72 * i2400mu_bus_bm_wait_for_ack
75 #include <linux/usb.h>
76 #include "i2400m-usb.h"
79 #define D_SUBMODULE fw
80 #include "usb-debug-levels.h"
84 * Synchronous write to the device
86 * Takes care of updating EDC counts and thus, handle device errors.
89 ssize_t
i2400mu_tx_bulk_out(struct i2400mu
*i2400mu
, void *buf
, size_t buf_size
)
92 struct device
*dev
= &i2400mu
->usb_iface
->dev
;
94 struct usb_endpoint_descriptor
*epd
;
95 int pipe
, do_autopm
= 1;
97 result
= usb_autopm_get_interface(i2400mu
->usb_iface
);
99 dev_err(dev
, "BM-CMD: can't get autopm: %d\n", result
);
102 epd
= usb_get_epd(i2400mu
->usb_iface
, I2400MU_EP_BULK_OUT
);
103 pipe
= usb_sndbulkpipe(i2400mu
->usb_dev
, epd
->bEndpointAddress
);
105 result
= usb_bulk_msg(i2400mu
->usb_dev
, pipe
, buf
, buf_size
, &len
, HZ
);
108 if (len
!= buf_size
) {
109 dev_err(dev
, "BM-CMD: short write (%u B vs %zu "
110 "expected)\n", len
, buf_size
);
116 case -EINVAL
: /* while removing driver */
117 case -ENODEV
: /* dev disconnect ... */
118 case -ENOENT
: /* just ignore it */
119 case -ESHUTDOWN
: /* and exit */
123 case -ETIMEDOUT
: /* bah... */
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
);
134 dev_err(dev
, "BM-CMD: URB error %d, retrying\n",
140 usb_autopm_put_interface(i2400mu
->usb_iface
);
146 * Send a boot-mode command over the bulk-out pipe
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.
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
156 * @flags: pass thru from i2400m_bm_cmd()
157 * @return: cmd_size if ok, < 0 errno code on error.
159 ssize_t
i2400mu_bus_bm_cmd_send(struct i2400m
*i2400m
,
160 const struct i2400m_bootrom_header
*_cmd
,
161 size_t cmd_size
, int flags
)
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 */
170 d_fnstart(8, dev
, "(i2400m %p cmd %p size %zu)\n",
171 i2400m
, _cmd
, cmd_size
);
173 if (cmd_size
> I2400M_BM_CMD_BUF_SIZE
)
175 memcpy(i2400m
->bm_cmd_buf
, _cmd
, cmd_size
);
176 cmd
= i2400m
->bm_cmd_buf
;
177 if (cmd_size_a
> cmd_size
) /* Zero pad space */
178 memset(i2400m
->bm_cmd_buf
+ cmd_size
, 0, cmd_size_a
- cmd_size
);
179 if ((flags
& I2400M_BM_CMD_RAW
) == 0) {
180 if (WARN_ON(i2400m_brh_get_response_required(cmd
) == 0))
181 dev_warn(dev
, "SW BUG: response_required == 0\n");
182 i2400m_bm_cmd_prepare(cmd
);
184 result
= i2400mu_tx_bulk_out(i2400mu
, i2400m
->bm_cmd_buf
, cmd_size
);
186 dev_err(dev
, "boot-mode cmd %d: cannot send: %zd\n",
190 if (result
!= cmd_size
) { /* all was transferred? */
191 dev_err(dev
, "boot-mode cmd %d: incomplete transfer "
192 "(%zu vs %zu submitted)\n", opcode
, result
, cmd_size
);
199 d_fnend(8, dev
, "(i2400m %p cmd %p size %zu) = %zd\n",
200 i2400m
, _cmd
, cmd_size
, result
);
206 void __i2400mu_bm_notif_cb(struct urb
*urb
)
208 complete(urb
->context
);
213 * submit a read to the notification endpoint
215 * @i2400m: device descriptor
217 * @completion: completion varible to complete when done
219 * Data is always read to i2400m->bm_ack_buf
222 int i2400mu_notif_submit(struct i2400mu
*i2400mu
, struct urb
*urb
,
223 struct completion
*completion
)
225 struct i2400m
*i2400m
= &i2400mu
->i2400m
;
226 struct usb_endpoint_descriptor
*epd
;
229 epd
= usb_get_epd(i2400mu
->usb_iface
, I2400MU_EP_NOTIFICATION
);
230 pipe
= usb_rcvintpipe(i2400mu
->usb_dev
, epd
->bEndpointAddress
);
231 usb_fill_int_urb(urb
, i2400mu
->usb_dev
, pipe
,
232 i2400m
->bm_ack_buf
, I2400M_BM_ACK_BUF_SIZE
,
233 __i2400mu_bm_notif_cb
, completion
,
235 return usb_submit_urb(urb
, GFP_KERNEL
);
240 * Read an ack from the notification endpoint
243 * @_ack: pointer to where to store the read data
244 * @ack_size: how many bytes we should read
246 * Returns: < 0 errno code on error; otherwise, amount of received bytes.
248 * Submits a notification read, appends the read data to the given ack
249 * buffer and then repeats (until @ack_size bytes have been
252 ssize_t
i2400mu_bus_bm_wait_for_ack(struct i2400m
*i2400m
,
253 struct i2400m_bootrom_header
*_ack
,
256 ssize_t result
= -ENOMEM
;
257 struct device
*dev
= i2400m_dev(i2400m
);
258 struct i2400mu
*i2400mu
= container_of(i2400m
, struct i2400mu
, i2400m
);
259 struct urb notif_urb
;
264 DECLARE_COMPLETION_ONSTACK(notif_completion
);
266 d_fnstart(8, dev
, "(i2400m %p ack %p size %zu)\n",
267 i2400m
, ack
, ack_size
);
268 BUG_ON(_ack
== i2400m
->bm_ack_buf
);
269 result
= usb_autopm_get_interface(i2400mu
->usb_iface
);
271 dev_err(dev
, "BM-ACK: can't get autopm: %d\n", (int) result
);
274 usb_init_urb(¬if_urb
); /* ready notifications */
275 usb_get_urb(¬if_urb
);
277 while (offset
< ack_size
) {
278 init_completion(¬if_completion
);
279 result
= i2400mu_notif_submit(i2400mu
, ¬if_urb
,
282 goto error_notif_urb_submit
;
283 val
= wait_for_completion_interruptible_timeout(
284 ¬if_completion
, HZ
);
287 usb_kill_urb(¬if_urb
); /* Timedout */
288 goto error_notif_wait
;
290 if (val
== -ERESTARTSYS
) {
291 result
= -EINTR
; /* Interrupted */
292 usb_kill_urb(¬if_urb
);
293 goto error_notif_wait
;
295 result
= notif_urb
.status
; /* How was the ack? */
299 case -EINVAL
: /* while removing driver */
300 case -ENODEV
: /* dev disconnect ... */
301 case -ENOENT
: /* just ignore it */
302 case -ESHUTDOWN
: /* and exit */
306 default: /* any other? */
307 usb_kill_urb(¬if_urb
); /* Timedout */
308 if (edc_inc(&i2400mu
->urb_edc
,
309 EDC_MAX_ERRORS
, EDC_ERROR_TIMEFRAME
))
311 dev_err(dev
, "BM-ACK: URB error %d, "
312 "retrying\n", notif_urb
.status
);
313 continue; /* retry */
315 if (notif_urb
.actual_length
== 0) {
316 d_printf(6, dev
, "ZLP received, retrying\n");
319 /* Got data, append it to the buffer */
320 len
= min(ack_size
- offset
, (size_t) notif_urb
.actual_length
);
321 memcpy(ack
+ offset
, i2400m
->bm_ack_buf
, len
);
325 error_notif_urb_submit
:
330 usb_autopm_put_interface(i2400mu
->usb_iface
);
331 d_fnend(8, dev
, "(i2400m %p ack %p size %zu) = %zd\n",
332 i2400m
, ack
, ack_size
, result
);
336 dev_err(dev
, "bm: maximum errors in notification URB exceeded; "
337 "resetting device\n");
338 usb_queue_reset_device(i2400mu
->usb_iface
);