usb-fw.c 10 KB

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