usb-fw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  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 <linux/gfp.h>
  77. #include "i2400m-usb.h"
  78. #define D_SUBMODULE fw
  79. #include "usb-debug-levels.h"
  80. /*
  81. * Synchronous write to the device
  82. *
  83. * Takes care of updating EDC counts and thus, handle device errors.
  84. */
  85. static
  86. ssize_t i2400mu_tx_bulk_out(struct i2400mu *i2400mu, void *buf, size_t buf_size)
  87. {
  88. int result;
  89. struct device *dev = &i2400mu->usb_iface->dev;
  90. int len;
  91. struct usb_endpoint_descriptor *epd;
  92. int pipe, do_autopm = 1;
  93. result = usb_autopm_get_interface(i2400mu->usb_iface);
  94. if (result < 0) {
  95. dev_err(dev, "BM-CMD: can't get autopm: %d\n", result);
  96. do_autopm = 0;
  97. }
  98. epd = usb_get_epd(i2400mu->usb_iface, i2400mu->endpoint_cfg.bulk_out);
  99. pipe = usb_sndbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  100. retry:
  101. result = usb_bulk_msg(i2400mu->usb_dev, pipe, buf, buf_size, &len, 200);
  102. switch (result) {
  103. case 0:
  104. if (len != buf_size) {
  105. dev_err(dev, "BM-CMD: short write (%u B vs %zu "
  106. "expected)\n", len, buf_size);
  107. result = -EIO;
  108. break;
  109. }
  110. result = len;
  111. break;
  112. case -EPIPE:
  113. /*
  114. * Stall -- maybe the device is choking with our
  115. * requests. Clear it and give it some time. If they
  116. * happen to often, it might be another symptom, so we
  117. * reset.
  118. *
  119. * No error handling for usb_clear_halt(0; if it
  120. * works, the retry works; if it fails, this switch
  121. * does the error handling for us.
  122. */
  123. if (edc_inc(&i2400mu->urb_edc,
  124. 10 * EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  125. dev_err(dev, "BM-CMD: too many stalls in "
  126. "URB; resetting device\n");
  127. usb_queue_reset_device(i2400mu->usb_iface);
  128. /* fallthrough */
  129. } else {
  130. usb_clear_halt(i2400mu->usb_dev, pipe);
  131. msleep(10); /* give the device some time */
  132. goto retry;
  133. }
  134. case -EINVAL: /* while removing driver */
  135. case -ENODEV: /* dev disconnect ... */
  136. case -ENOENT: /* just ignore it */
  137. case -ESHUTDOWN: /* and exit */
  138. case -ECONNRESET:
  139. result = -ESHUTDOWN;
  140. break;
  141. case -ETIMEDOUT: /* bah... */
  142. break;
  143. default: /* any other? */
  144. if (edc_inc(&i2400mu->urb_edc,
  145. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  146. dev_err(dev, "BM-CMD: maximum errors in "
  147. "URB exceeded; resetting device\n");
  148. usb_queue_reset_device(i2400mu->usb_iface);
  149. result = -ENODEV;
  150. break;
  151. }
  152. dev_err(dev, "BM-CMD: URB error %d, retrying\n",
  153. result);
  154. goto retry;
  155. }
  156. if (do_autopm)
  157. usb_autopm_put_interface(i2400mu->usb_iface);
  158. return result;
  159. }
  160. /*
  161. * Send a boot-mode command over the bulk-out pipe
  162. *
  163. * Command can be a raw command, which requires no preparation (and
  164. * which might not even be following the command format). Checks that
  165. * the right amount of data was transferred.
  166. *
  167. * To satisfy USB requirements (no onstack, vmalloc or in data segment
  168. * buffers), we copy the command to i2400m->bm_cmd_buf and send it from
  169. * there.
  170. *
  171. * @flags: pass thru from i2400m_bm_cmd()
  172. * @return: cmd_size if ok, < 0 errno code on error.
  173. */
  174. ssize_t i2400mu_bus_bm_cmd_send(struct i2400m *i2400m,
  175. const struct i2400m_bootrom_header *_cmd,
  176. size_t cmd_size, int flags)
  177. {
  178. ssize_t result;
  179. struct device *dev = i2400m_dev(i2400m);
  180. struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  181. int opcode = _cmd == NULL ? -1 : i2400m_brh_get_opcode(_cmd);
  182. struct i2400m_bootrom_header *cmd;
  183. size_t cmd_size_a = ALIGN(cmd_size, 16); /* USB restriction */
  184. d_fnstart(8, dev, "(i2400m %p cmd %p size %zu)\n",
  185. i2400m, _cmd, cmd_size);
  186. result = -E2BIG;
  187. if (cmd_size > I2400M_BM_CMD_BUF_SIZE)
  188. goto error_too_big;
  189. if (_cmd != i2400m->bm_cmd_buf)
  190. memmove(i2400m->bm_cmd_buf, _cmd, cmd_size);
  191. cmd = i2400m->bm_cmd_buf;
  192. if (cmd_size_a > cmd_size) /* Zero pad space */
  193. memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
  194. if ((flags & I2400M_BM_CMD_RAW) == 0) {
  195. if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
  196. dev_warn(dev, "SW BUG: response_required == 0\n");
  197. i2400m_bm_cmd_prepare(cmd);
  198. }
  199. result = i2400mu_tx_bulk_out(i2400mu, i2400m->bm_cmd_buf, cmd_size);
  200. if (result < 0) {
  201. dev_err(dev, "boot-mode cmd %d: cannot send: %zd\n",
  202. opcode, result);
  203. goto error_cmd_send;
  204. }
  205. if (result != cmd_size) { /* all was transferred? */
  206. dev_err(dev, "boot-mode cmd %d: incomplete transfer "
  207. "(%zd vs %zu submitted)\n", opcode, result, cmd_size);
  208. result = -EIO;
  209. goto error_cmd_size;
  210. }
  211. error_cmd_size:
  212. error_cmd_send:
  213. error_too_big:
  214. d_fnend(8, dev, "(i2400m %p cmd %p size %zu) = %zd\n",
  215. i2400m, _cmd, cmd_size, result);
  216. return result;
  217. }
  218. static
  219. void __i2400mu_bm_notif_cb(struct urb *urb)
  220. {
  221. complete(urb->context);
  222. }
  223. /*
  224. * submit a read to the notification endpoint
  225. *
  226. * @i2400m: device descriptor
  227. * @urb: urb to use
  228. * @completion: completion varible to complete when done
  229. *
  230. * Data is always read to i2400m->bm_ack_buf
  231. */
  232. static
  233. int i2400mu_notif_submit(struct i2400mu *i2400mu, struct urb *urb,
  234. struct completion *completion)
  235. {
  236. struct i2400m *i2400m = &i2400mu->i2400m;
  237. struct usb_endpoint_descriptor *epd;
  238. int pipe;
  239. epd = usb_get_epd(i2400mu->usb_iface,
  240. i2400mu->endpoint_cfg.notification);
  241. pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  242. usb_fill_int_urb(urb, i2400mu->usb_dev, pipe,
  243. i2400m->bm_ack_buf, I2400M_BM_ACK_BUF_SIZE,
  244. __i2400mu_bm_notif_cb, completion,
  245. epd->bInterval);
  246. return usb_submit_urb(urb, GFP_KERNEL);
  247. }
  248. /*
  249. * Read an ack from the notification endpoint
  250. *
  251. * @i2400m:
  252. * @_ack: pointer to where to store the read data
  253. * @ack_size: how many bytes we should read
  254. *
  255. * Returns: < 0 errno code on error; otherwise, amount of received bytes.
  256. *
  257. * Submits a notification read, appends the read data to the given ack
  258. * buffer and then repeats (until @ack_size bytes have been
  259. * received).
  260. */
  261. ssize_t i2400mu_bus_bm_wait_for_ack(struct i2400m *i2400m,
  262. struct i2400m_bootrom_header *_ack,
  263. size_t ack_size)
  264. {
  265. ssize_t result = -ENOMEM;
  266. struct device *dev = i2400m_dev(i2400m);
  267. struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  268. struct urb notif_urb;
  269. void *ack = _ack;
  270. size_t offset, len;
  271. long val;
  272. int do_autopm = 1;
  273. DECLARE_COMPLETION_ONSTACK(notif_completion);
  274. d_fnstart(8, dev, "(i2400m %p ack %p size %zu)\n",
  275. i2400m, ack, ack_size);
  276. BUG_ON(_ack == i2400m->bm_ack_buf);
  277. result = usb_autopm_get_interface(i2400mu->usb_iface);
  278. if (result < 0) {
  279. dev_err(dev, "BM-ACK: can't get autopm: %d\n", (int) result);
  280. do_autopm = 0;
  281. }
  282. usb_init_urb(&notif_urb); /* ready notifications */
  283. usb_get_urb(&notif_urb);
  284. offset = 0;
  285. while (offset < ack_size) {
  286. init_completion(&notif_completion);
  287. result = i2400mu_notif_submit(i2400mu, &notif_urb,
  288. &notif_completion);
  289. if (result < 0)
  290. goto error_notif_urb_submit;
  291. val = wait_for_completion_interruptible_timeout(
  292. &notif_completion, HZ);
  293. if (val == 0) {
  294. result = -ETIMEDOUT;
  295. usb_kill_urb(&notif_urb); /* Timedout */
  296. goto error_notif_wait;
  297. }
  298. if (val == -ERESTARTSYS) {
  299. result = -EINTR; /* Interrupted */
  300. usb_kill_urb(&notif_urb);
  301. goto error_notif_wait;
  302. }
  303. result = notif_urb.status; /* How was the ack? */
  304. switch (result) {
  305. case 0:
  306. break;
  307. case -EINVAL: /* while removing driver */
  308. case -ENODEV: /* dev disconnect ... */
  309. case -ENOENT: /* just ignore it */
  310. case -ESHUTDOWN: /* and exit */
  311. case -ECONNRESET:
  312. result = -ESHUTDOWN;
  313. goto error_dev_gone;
  314. default: /* any other? */
  315. usb_kill_urb(&notif_urb); /* Timedout */
  316. if (edc_inc(&i2400mu->urb_edc,
  317. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
  318. goto error_exceeded;
  319. dev_err(dev, "BM-ACK: URB error %d, "
  320. "retrying\n", notif_urb.status);
  321. continue; /* retry */
  322. }
  323. if (notif_urb.actual_length == 0) {
  324. d_printf(6, dev, "ZLP received, retrying\n");
  325. continue;
  326. }
  327. /* Got data, append it to the buffer */
  328. len = min(ack_size - offset, (size_t) notif_urb.actual_length);
  329. memcpy(ack + offset, i2400m->bm_ack_buf, len);
  330. offset += len;
  331. }
  332. result = offset;
  333. error_notif_urb_submit:
  334. error_notif_wait:
  335. error_dev_gone:
  336. out:
  337. if (do_autopm)
  338. usb_autopm_put_interface(i2400mu->usb_iface);
  339. d_fnend(8, dev, "(i2400m %p ack %p size %zu) = %ld\n",
  340. i2400m, ack, ack_size, (long) result);
  341. return result;
  342. error_exceeded:
  343. dev_err(dev, "bm: maximum errors in notification URB exceeded; "
  344. "resetting device\n");
  345. usb_queue_reset_device(i2400mu->usb_iface);
  346. goto out;
  347. }