usb-fw.c 11 KB

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