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