usb-fw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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_EP_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, HZ);
  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. memcpy(i2400m->bm_cmd_buf, _cmd, cmd_size);
  168. cmd = i2400m->bm_cmd_buf;
  169. if (cmd_size_a > cmd_size) /* Zero pad space */
  170. memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
  171. if ((flags & I2400M_BM_CMD_RAW) == 0) {
  172. if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
  173. dev_warn(dev, "SW BUG: response_required == 0\n");
  174. i2400m_bm_cmd_prepare(cmd);
  175. }
  176. result = i2400mu_tx_bulk_out(i2400mu, i2400m->bm_cmd_buf, cmd_size);
  177. if (result < 0) {
  178. dev_err(dev, "boot-mode cmd %d: cannot send: %zd\n",
  179. opcode, result);
  180. goto error_cmd_send;
  181. }
  182. if (result != cmd_size) { /* all was transferred? */
  183. dev_err(dev, "boot-mode cmd %d: incomplete transfer "
  184. "(%zu vs %zu submitted)\n", opcode, result, cmd_size);
  185. result = -EIO;
  186. goto error_cmd_size;
  187. }
  188. error_cmd_size:
  189. error_cmd_send:
  190. error_too_big:
  191. d_fnend(8, dev, "(i2400m %p cmd %p size %zu) = %zd\n",
  192. i2400m, _cmd, cmd_size, result);
  193. return result;
  194. }
  195. static
  196. void __i2400mu_bm_notif_cb(struct urb *urb)
  197. {
  198. complete(urb->context);
  199. }
  200. /*
  201. * submit a read to the notification endpoint
  202. *
  203. * @i2400m: device descriptor
  204. * @urb: urb to use
  205. * @completion: completion varible to complete when done
  206. *
  207. * Data is always read to i2400m->bm_ack_buf
  208. */
  209. static
  210. int i2400mu_notif_submit(struct i2400mu *i2400mu, struct urb *urb,
  211. struct completion *completion)
  212. {
  213. struct i2400m *i2400m = &i2400mu->i2400m;
  214. struct usb_endpoint_descriptor *epd;
  215. int pipe;
  216. epd = usb_get_epd(i2400mu->usb_iface, I2400MU_EP_NOTIFICATION);
  217. pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  218. usb_fill_int_urb(urb, i2400mu->usb_dev, pipe,
  219. i2400m->bm_ack_buf, I2400M_BM_ACK_BUF_SIZE,
  220. __i2400mu_bm_notif_cb, completion,
  221. epd->bInterval);
  222. return usb_submit_urb(urb, GFP_KERNEL);
  223. }
  224. /*
  225. * Read an ack from the notification endpoint
  226. *
  227. * @i2400m:
  228. * @_ack: pointer to where to store the read data
  229. * @ack_size: how many bytes we should read
  230. *
  231. * Returns: < 0 errno code on error; otherwise, amount of received bytes.
  232. *
  233. * Submits a notification read, appends the read data to the given ack
  234. * buffer and then repeats (until @ack_size bytes have been
  235. * received).
  236. */
  237. ssize_t i2400mu_bus_bm_wait_for_ack(struct i2400m *i2400m,
  238. struct i2400m_bootrom_header *_ack,
  239. size_t ack_size)
  240. {
  241. ssize_t result = -ENOMEM;
  242. struct device *dev = i2400m_dev(i2400m);
  243. struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  244. struct urb notif_urb;
  245. void *ack = _ack;
  246. size_t offset, len;
  247. long val;
  248. int do_autopm = 1;
  249. DECLARE_COMPLETION_ONSTACK(notif_completion);
  250. d_fnstart(8, dev, "(i2400m %p ack %p size %zu)\n",
  251. i2400m, ack, ack_size);
  252. BUG_ON(_ack == i2400m->bm_ack_buf);
  253. result = usb_autopm_get_interface(i2400mu->usb_iface);
  254. if (result < 0) {
  255. dev_err(dev, "BM-ACK: can't get autopm: %d\n", (int) result);
  256. do_autopm = 0;
  257. }
  258. usb_init_urb(&notif_urb); /* ready notifications */
  259. usb_get_urb(&notif_urb);
  260. offset = 0;
  261. while (offset < ack_size) {
  262. init_completion(&notif_completion);
  263. result = i2400mu_notif_submit(i2400mu, &notif_urb,
  264. &notif_completion);
  265. if (result < 0)
  266. goto error_notif_urb_submit;
  267. val = wait_for_completion_interruptible_timeout(
  268. &notif_completion, HZ);
  269. if (val == 0) {
  270. result = -ETIMEDOUT;
  271. usb_kill_urb(&notif_urb); /* Timedout */
  272. goto error_notif_wait;
  273. }
  274. if (val == -ERESTARTSYS) {
  275. result = -EINTR; /* Interrupted */
  276. usb_kill_urb(&notif_urb);
  277. goto error_notif_wait;
  278. }
  279. result = notif_urb.status; /* How was the ack? */
  280. switch (result) {
  281. case 0:
  282. break;
  283. case -EINVAL: /* while removing driver */
  284. case -ENODEV: /* dev disconnect ... */
  285. case -ENOENT: /* just ignore it */
  286. case -ESHUTDOWN: /* and exit */
  287. case -ECONNRESET:
  288. result = -ESHUTDOWN;
  289. goto error_dev_gone;
  290. default: /* any other? */
  291. usb_kill_urb(&notif_urb); /* Timedout */
  292. if (edc_inc(&i2400mu->urb_edc,
  293. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
  294. goto error_exceeded;
  295. dev_err(dev, "BM-ACK: URB error %d, "
  296. "retrying\n", notif_urb.status);
  297. continue; /* retry */
  298. }
  299. if (notif_urb.actual_length == 0) {
  300. d_printf(6, dev, "ZLP received, retrying\n");
  301. continue;
  302. }
  303. /* Got data, append it to the buffer */
  304. len = min(ack_size - offset, (size_t) notif_urb.actual_length);
  305. memcpy(ack + offset, i2400m->bm_ack_buf, len);
  306. offset += len;
  307. }
  308. result = offset;
  309. error_notif_urb_submit:
  310. error_notif_wait:
  311. error_dev_gone:
  312. out:
  313. if (do_autopm)
  314. usb_autopm_put_interface(i2400mu->usb_iface);
  315. d_fnend(8, dev, "(i2400m %p ack %p size %zu) = %zd\n",
  316. i2400m, ack, ack_size, result);
  317. return result;
  318. error_exceeded:
  319. dev_err(dev, "bm: maximum errors in notification URB exceeded; "
  320. "resetting device\n");
  321. usb_queue_reset_device(i2400mu->usb_iface);
  322. goto out;
  323. }