dfu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Intel Wireless UWB Link 1480
  3. * Main driver
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * Common code for firmware upload used by the USB and PCI version;
  24. * i1480_fw_upload() takes a device descriptor and uses the function
  25. * pointers it provides to upload firmware and prepare the PHY.
  26. *
  27. * As well, provides common functions used by the rest of the code.
  28. */
  29. #include "i1480-dfu.h"
  30. #include <linux/errno.h>
  31. #include <linux/delay.h>
  32. #include <linux/pci.h>
  33. #include <linux/device.h>
  34. #include <linux/uwb.h>
  35. #include <linux/random.h>
  36. /*
  37. * i1480_rceb_check - Check RCEB for expected field values
  38. * @i1480: pointer to device for which RCEB is being checked
  39. * @rceb: RCEB being checked
  40. * @cmd: which command the RCEB is related to
  41. * @context: expected context
  42. * @expected_type: expected event type
  43. * @expected_event: expected event
  44. *
  45. * If @cmd is NULL, do not print error messages, but still return an error
  46. * code.
  47. *
  48. * Return 0 if @rceb matches the expected values, -EINVAL otherwise.
  49. */
  50. int i1480_rceb_check(const struct i1480 *i1480, const struct uwb_rceb *rceb,
  51. const char *cmd, u8 context, u8 expected_type,
  52. unsigned expected_event)
  53. {
  54. int result = 0;
  55. struct device *dev = i1480->dev;
  56. if (rceb->bEventContext != context) {
  57. if (cmd)
  58. dev_err(dev, "%s: unexpected context id 0x%02x "
  59. "(expected 0x%02x)\n", cmd,
  60. rceb->bEventContext, context);
  61. result = -EINVAL;
  62. }
  63. if (rceb->bEventType != expected_type) {
  64. if (cmd)
  65. dev_err(dev, "%s: unexpected event type 0x%02x "
  66. "(expected 0x%02x)\n", cmd,
  67. rceb->bEventType, expected_type);
  68. result = -EINVAL;
  69. }
  70. if (le16_to_cpu(rceb->wEvent) != expected_event) {
  71. if (cmd)
  72. dev_err(dev, "%s: unexpected event 0x%04x "
  73. "(expected 0x%04x)\n", cmd,
  74. le16_to_cpu(rceb->wEvent), expected_event);
  75. result = -EINVAL;
  76. }
  77. return result;
  78. }
  79. EXPORT_SYMBOL_GPL(i1480_rceb_check);
  80. /*
  81. * Execute a Radio Control Command
  82. *
  83. * Command data has to be in i1480->cmd_buf.
  84. *
  85. * @returns size of the reply data filled in i1480->evt_buf or < 0 errno
  86. * code on error.
  87. */
  88. ssize_t i1480_cmd(struct i1480 *i1480, const char *cmd_name, size_t cmd_size,
  89. size_t reply_size)
  90. {
  91. ssize_t result;
  92. struct uwb_rceb *reply = i1480->evt_buf;
  93. struct uwb_rccb *cmd = i1480->cmd_buf;
  94. u16 expected_event = reply->wEvent;
  95. u8 expected_type = reply->bEventType;
  96. u8 context;
  97. init_completion(&i1480->evt_complete);
  98. i1480->evt_result = -EINPROGRESS;
  99. do {
  100. get_random_bytes(&context, 1);
  101. } while (context == 0x00 || context == 0xff);
  102. cmd->bCommandContext = context;
  103. result = i1480->cmd(i1480, cmd_name, cmd_size);
  104. if (result < 0)
  105. goto error;
  106. /* wait for the callback to report a event was received */
  107. result = wait_for_completion_interruptible_timeout(
  108. &i1480->evt_complete, HZ);
  109. if (result == 0) {
  110. result = -ETIMEDOUT;
  111. goto error;
  112. }
  113. if (result < 0)
  114. goto error;
  115. result = i1480->evt_result;
  116. if (result < 0) {
  117. dev_err(i1480->dev, "%s: command reply reception failed: %zd\n",
  118. cmd_name, result);
  119. goto error;
  120. }
  121. /*
  122. * Firmware versions >= 1.4.12224 for IOGear GUWA100U generate a
  123. * spurious notification after firmware is downloaded. So check whether
  124. * the receibed RCEB is such notification before assuming that the
  125. * command has failed.
  126. */
  127. if (i1480_rceb_check(i1480, i1480->evt_buf, NULL,
  128. 0, 0xfd, 0x0022) == 0) {
  129. /* Now wait for the actual RCEB for this command. */
  130. result = i1480->wait_init_done(i1480);
  131. if (result < 0)
  132. goto error;
  133. result = i1480->evt_result;
  134. }
  135. if (result != reply_size) {
  136. dev_err(i1480->dev, "%s returned only %zu bytes, %zu expected\n",
  137. cmd_name, result, reply_size);
  138. result = -EINVAL;
  139. goto error;
  140. }
  141. /* Verify we got the right event in response */
  142. result = i1480_rceb_check(i1480, i1480->evt_buf, cmd_name, context,
  143. expected_type, expected_event);
  144. error:
  145. return result;
  146. }
  147. EXPORT_SYMBOL_GPL(i1480_cmd);
  148. static
  149. int i1480_print_state(struct i1480 *i1480)
  150. {
  151. int result;
  152. u32 *buf = (u32 *) i1480->cmd_buf;
  153. result = i1480->read(i1480, 0x80080000, 2 * sizeof(*buf));
  154. if (result < 0) {
  155. dev_err(i1480->dev, "cannot read U & L states: %d\n", result);
  156. goto error;
  157. }
  158. dev_info(i1480->dev, "state U 0x%08x, L 0x%08x\n", buf[0], buf[1]);
  159. error:
  160. return result;
  161. }
  162. /*
  163. * PCI probe, firmware uploader
  164. *
  165. * _mac_fw_upload() will call rc_setup(), which needs an rc_release().
  166. */
  167. int i1480_fw_upload(struct i1480 *i1480)
  168. {
  169. int result;
  170. result = i1480_pre_fw_upload(i1480); /* PHY pre fw */
  171. if (result < 0 && result != -ENOENT) {
  172. i1480_print_state(i1480);
  173. goto error;
  174. }
  175. result = i1480_mac_fw_upload(i1480); /* MAC fw */
  176. if (result < 0) {
  177. if (result == -ENOENT)
  178. dev_err(i1480->dev, "Cannot locate MAC FW file '%s'\n",
  179. i1480->mac_fw_name);
  180. else
  181. i1480_print_state(i1480);
  182. goto error;
  183. }
  184. result = i1480_phy_fw_upload(i1480); /* PHY fw */
  185. if (result < 0 && result != -ENOENT) {
  186. i1480_print_state(i1480);
  187. goto error_rc_release;
  188. }
  189. /*
  190. * FIXME: find some reliable way to check whether firmware is running
  191. * properly. Maybe use some standard request that has no side effects?
  192. */
  193. dev_info(i1480->dev, "firmware uploaded successfully\n");
  194. error_rc_release:
  195. if (i1480->rc_release)
  196. i1480->rc_release(i1480);
  197. result = 0;
  198. error:
  199. return result;
  200. }
  201. EXPORT_SYMBOL_GPL(i1480_fw_upload);