dfu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #define D_LOCAL 0
  37. #include <linux/uwb/debug.h>
  38. /** @return 0 if If @evt is a valid reply event; otherwise complain */
  39. int i1480_rceb_check(const struct i1480 *i1480, const struct uwb_rceb *rceb,
  40. const char *cmd, u8 context,
  41. unsigned expected_type, unsigned expected_event)
  42. {
  43. int result = 0;
  44. struct device *dev = i1480->dev;
  45. if (rceb->bEventContext != context) {
  46. dev_err(dev, "%s: "
  47. "unexpected context id 0x%02x (expected 0x%02x)\n",
  48. cmd, rceb->bEventContext, context);
  49. result = -EINVAL;
  50. }
  51. if (rceb->bEventType != expected_type) {
  52. dev_err(dev, "%s: "
  53. "unexpected event type 0x%02x (expected 0x%02x)\n",
  54. cmd, rceb->bEventType, expected_type);
  55. result = -EINVAL;
  56. }
  57. if (le16_to_cpu(rceb->wEvent) != expected_event) {
  58. dev_err(dev, "%s: "
  59. "unexpected event 0x%04x (expected 0x%04x)\n",
  60. cmd, le16_to_cpu(rceb->wEvent), expected_event);
  61. result = -EINVAL;
  62. }
  63. return result;
  64. }
  65. EXPORT_SYMBOL_GPL(i1480_rceb_check);
  66. /**
  67. * Execute a Radio Control Command
  68. *
  69. * Command data has to be in i1480->cmd_buf.
  70. *
  71. * @returns size of the reply data filled in i1480->evt_buf or < 0 errno
  72. * code on error.
  73. */
  74. ssize_t i1480_cmd(struct i1480 *i1480, const char *cmd_name, size_t cmd_size,
  75. size_t reply_size)
  76. {
  77. ssize_t result;
  78. struct uwb_rceb *reply = i1480->evt_buf;
  79. struct uwb_rccb *cmd = i1480->cmd_buf;
  80. u16 expected_event = reply->wEvent;
  81. u8 expected_type = reply->bEventType;
  82. u8 context;
  83. d_fnstart(3, i1480->dev, "(%p, %s, %zu)\n", i1480, cmd_name, cmd_size);
  84. init_completion(&i1480->evt_complete);
  85. i1480->evt_result = -EINPROGRESS;
  86. do {
  87. get_random_bytes(&context, 1);
  88. } while (context == 0x00 || context == 0xff);
  89. cmd->bCommandContext = context;
  90. result = i1480->cmd(i1480, cmd_name, cmd_size);
  91. if (result < 0)
  92. goto error;
  93. /* wait for the callback to report a event was received */
  94. result = wait_for_completion_interruptible_timeout(
  95. &i1480->evt_complete, HZ);
  96. if (result == 0) {
  97. result = -ETIMEDOUT;
  98. goto error;
  99. }
  100. if (result < 0)
  101. goto error;
  102. result = i1480->evt_result;
  103. if (result < 0) {
  104. dev_err(i1480->dev, "%s: command reply reception failed: %zd\n",
  105. cmd_name, result);
  106. goto error;
  107. }
  108. if (result != reply_size) {
  109. dev_err(i1480->dev, "%s returned only %zu bytes, %zu expected\n",
  110. cmd_name, result, reply_size);
  111. result = -EINVAL;
  112. goto error;
  113. }
  114. /* Verify we got the right event in response */
  115. result = i1480_rceb_check(i1480, i1480->evt_buf, cmd_name, context,
  116. expected_type, expected_event);
  117. error:
  118. d_fnend(3, i1480->dev, "(%p, %s, %zu) = %zd\n",
  119. i1480, cmd_name, cmd_size, result);
  120. return result;
  121. }
  122. EXPORT_SYMBOL_GPL(i1480_cmd);
  123. static
  124. int i1480_print_state(struct i1480 *i1480)
  125. {
  126. int result;
  127. u32 *buf = (u32 *) i1480->cmd_buf;
  128. result = i1480->read(i1480, 0x80080000, 2 * sizeof(*buf));
  129. if (result < 0) {
  130. dev_err(i1480->dev, "cannot read U & L states: %d\n", result);
  131. goto error;
  132. }
  133. dev_info(i1480->dev, "state U 0x%08x, L 0x%08x\n", buf[0], buf[1]);
  134. error:
  135. return result;
  136. }
  137. /*
  138. * PCI probe, firmware uploader
  139. *
  140. * _mac_fw_upload() will call rc_setup(), which needs an rc_release().
  141. */
  142. int i1480_fw_upload(struct i1480 *i1480)
  143. {
  144. int result;
  145. result = i1480_pre_fw_upload(i1480); /* PHY pre fw */
  146. if (result < 0 && result != -ENOENT) {
  147. i1480_print_state(i1480);
  148. goto error;
  149. }
  150. result = i1480_mac_fw_upload(i1480); /* MAC fw */
  151. if (result < 0) {
  152. if (result == -ENOENT)
  153. dev_err(i1480->dev, "Cannot locate MAC FW file '%s'\n",
  154. i1480->mac_fw_name);
  155. else
  156. i1480_print_state(i1480);
  157. goto error;
  158. }
  159. result = i1480_phy_fw_upload(i1480); /* PHY fw */
  160. if (result < 0 && result != -ENOENT) {
  161. i1480_print_state(i1480);
  162. goto error_rc_release;
  163. }
  164. /*
  165. * FIXME: find some reliable way to check whether firmware is running
  166. * properly. Maybe use some standard request that has no side effects?
  167. */
  168. dev_info(i1480->dev, "firmware uploaded successfully\n");
  169. error_rc_release:
  170. if (i1480->rc_release)
  171. i1480->rc_release(i1480);
  172. result = 0;
  173. error:
  174. return result;
  175. }
  176. EXPORT_SYMBOL_GPL(i1480_fw_upload);