reset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Ultra Wide Band
  3. * UWB basic command support and radio reset
  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. * FIXME:
  24. *
  25. * - docs
  26. *
  27. * - Now we are serializing (using the uwb_dev->mutex) the command
  28. * execution; it should be parallelized as much as possible some
  29. * day.
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/err.h>
  33. #include <linux/delay.h>
  34. #include "uwb-internal.h"
  35. /**
  36. * Command result codes (WUSB1.0[T8-69])
  37. */
  38. static
  39. const char *__strerror[] = {
  40. "success",
  41. "failure",
  42. "hardware failure",
  43. "no more slots",
  44. "beacon is too large",
  45. "invalid parameter",
  46. "unsupported power level",
  47. "time out (wa) or invalid ie data (whci)",
  48. "beacon size exceeded",
  49. "cancelled",
  50. "invalid state",
  51. "invalid size",
  52. "ack not recieved",
  53. "no more asie notification",
  54. };
  55. /** Return a string matching the given error code */
  56. const char *uwb_rc_strerror(unsigned code)
  57. {
  58. if (code == 255)
  59. return "time out";
  60. if (code >= ARRAY_SIZE(__strerror))
  61. return "unknown error";
  62. return __strerror[code];
  63. }
  64. int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name,
  65. struct uwb_rccb *cmd, size_t cmd_size,
  66. u8 expected_type, u16 expected_event,
  67. uwb_rc_cmd_cb_f cb, void *arg)
  68. {
  69. struct device *dev = &rc->uwb_dev.dev;
  70. struct uwb_rc_neh *neh;
  71. int needtofree = 0;
  72. int result;
  73. uwb_dev_lock(&rc->uwb_dev); /* Protect against rc->priv being removed */
  74. if (rc->priv == NULL) {
  75. uwb_dev_unlock(&rc->uwb_dev);
  76. return -ESHUTDOWN;
  77. }
  78. if (rc->filter_cmd) {
  79. needtofree = rc->filter_cmd(rc, &cmd, &cmd_size);
  80. if (needtofree < 0 && needtofree != -ENOANO) {
  81. dev_err(dev, "%s: filter error: %d\n",
  82. cmd_name, needtofree);
  83. uwb_dev_unlock(&rc->uwb_dev);
  84. return needtofree;
  85. }
  86. }
  87. neh = uwb_rc_neh_add(rc, cmd, expected_type, expected_event, cb, arg);
  88. if (IS_ERR(neh)) {
  89. result = PTR_ERR(neh);
  90. goto out;
  91. }
  92. result = rc->cmd(rc, cmd, cmd_size);
  93. uwb_dev_unlock(&rc->uwb_dev);
  94. if (result < 0)
  95. uwb_rc_neh_rm(rc, neh);
  96. else
  97. uwb_rc_neh_arm(rc, neh);
  98. uwb_rc_neh_put(neh);
  99. out:
  100. if (needtofree == 1)
  101. kfree(cmd);
  102. return result < 0 ? result : 0;
  103. }
  104. EXPORT_SYMBOL_GPL(uwb_rc_cmd_async);
  105. struct uwb_rc_cmd_done_params {
  106. struct completion completion;
  107. struct uwb_rceb *reply;
  108. ssize_t reply_size;
  109. };
  110. static void uwb_rc_cmd_done(struct uwb_rc *rc, void *arg,
  111. struct uwb_rceb *reply, ssize_t reply_size)
  112. {
  113. struct uwb_rc_cmd_done_params *p = (struct uwb_rc_cmd_done_params *)arg;
  114. if (reply_size > 0) {
  115. if (p->reply)
  116. reply_size = min(p->reply_size, reply_size);
  117. else
  118. p->reply = kmalloc(reply_size, GFP_ATOMIC);
  119. if (p->reply)
  120. memcpy(p->reply, reply, reply_size);
  121. else
  122. reply_size = -ENOMEM;
  123. }
  124. p->reply_size = reply_size;
  125. complete(&p->completion);
  126. }
  127. /**
  128. * Generic function for issuing commands to the Radio Control Interface
  129. *
  130. * @rc: UWB Radio Control descriptor
  131. * @cmd_name: Name of the command being issued (for error messages)
  132. * @cmd: Pointer to rccb structure containing the command;
  133. * normally you embed this structure as the first member of
  134. * the full command structure.
  135. * @cmd_size: Size of the whole command buffer pointed to by @cmd.
  136. * @reply: Pointer to where to store the reply
  137. * @reply_size: @reply's size
  138. * @expected_type: Expected type in the return event
  139. * @expected_event: Expected event code in the return event
  140. * @preply: Here a pointer to where the event data is received will
  141. * be stored. Once done with the data, free with kfree().
  142. *
  143. * This function is generic; it works for commands that return a fixed
  144. * and known size or for commands that return a variable amount of data.
  145. *
  146. * If a buffer is provided, that is used, although it could be chopped
  147. * to the maximum size of the buffer. If the buffer is NULL, then one
  148. * be allocated in *preply with the whole contents of the reply.
  149. *
  150. * @rc needs to be referenced
  151. */
  152. static
  153. ssize_t __uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
  154. struct uwb_rccb *cmd, size_t cmd_size,
  155. struct uwb_rceb *reply, size_t reply_size,
  156. u8 expected_type, u16 expected_event,
  157. struct uwb_rceb **preply)
  158. {
  159. ssize_t result = 0;
  160. struct device *dev = &rc->uwb_dev.dev;
  161. struct uwb_rc_cmd_done_params params;
  162. init_completion(&params.completion);
  163. params.reply = reply;
  164. params.reply_size = reply_size;
  165. result = uwb_rc_cmd_async(rc, cmd_name, cmd, cmd_size,
  166. expected_type, expected_event,
  167. uwb_rc_cmd_done, &params);
  168. if (result)
  169. return result;
  170. wait_for_completion(&params.completion);
  171. if (preply)
  172. *preply = params.reply;
  173. if (params.reply_size < 0)
  174. dev_err(dev, "%s: confirmation event 0x%02x/%04x/%02x "
  175. "reception failed: %d\n", cmd_name,
  176. expected_type, expected_event, cmd->bCommandContext,
  177. (int)params.reply_size);
  178. return params.reply_size;
  179. }
  180. /**
  181. * Generic function for issuing commands to the Radio Control Interface
  182. *
  183. * @rc: UWB Radio Control descriptor
  184. * @cmd_name: Name of the command being issued (for error messages)
  185. * @cmd: Pointer to rccb structure containing the command;
  186. * normally you embed this structure as the first member of
  187. * the full command structure.
  188. * @cmd_size: Size of the whole command buffer pointed to by @cmd.
  189. * @reply: Pointer to the beginning of the confirmation event
  190. * buffer. Normally bigger than an 'struct hwarc_rceb'.
  191. * You need to fill out reply->bEventType and reply->wEvent (in
  192. * cpu order) as the function will use them to verify the
  193. * confirmation event.
  194. * @reply_size: Size of the reply buffer
  195. *
  196. * The function checks that the length returned in the reply is at
  197. * least as big as @reply_size; if not, it will be deemed an error and
  198. * -EIO returned.
  199. *
  200. * @rc needs to be referenced
  201. */
  202. ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
  203. struct uwb_rccb *cmd, size_t cmd_size,
  204. struct uwb_rceb *reply, size_t reply_size)
  205. {
  206. struct device *dev = &rc->uwb_dev.dev;
  207. ssize_t result;
  208. result = __uwb_rc_cmd(rc, cmd_name,
  209. cmd, cmd_size, reply, reply_size,
  210. reply->bEventType, reply->wEvent, NULL);
  211. if (result > 0 && result < reply_size) {
  212. dev_err(dev, "%s: not enough data returned for decoding reply "
  213. "(%zu bytes received vs at least %zu needed)\n",
  214. cmd_name, result, reply_size);
  215. result = -EIO;
  216. }
  217. return result;
  218. }
  219. EXPORT_SYMBOL_GPL(uwb_rc_cmd);
  220. /**
  221. * Generic function for issuing commands to the Radio Control
  222. * Interface that return an unknown amount of data
  223. *
  224. * @rc: UWB Radio Control descriptor
  225. * @cmd_name: Name of the command being issued (for error messages)
  226. * @cmd: Pointer to rccb structure containing the command;
  227. * normally you embed this structure as the first member of
  228. * the full command structure.
  229. * @cmd_size: Size of the whole command buffer pointed to by @cmd.
  230. * @expected_type: Expected type in the return event
  231. * @expected_event: Expected event code in the return event
  232. * @preply: Here a pointer to where the event data is received will
  233. * be stored. Once done with the data, free with kfree().
  234. *
  235. * The function checks that the length returned in the reply is at
  236. * least as big as a 'struct uwb_rceb *'; if not, it will be deemed an
  237. * error and -EIO returned.
  238. *
  239. * @rc needs to be referenced
  240. */
  241. ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name,
  242. struct uwb_rccb *cmd, size_t cmd_size,
  243. u8 expected_type, u16 expected_event,
  244. struct uwb_rceb **preply)
  245. {
  246. return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0,
  247. expected_type, expected_event, preply);
  248. }
  249. EXPORT_SYMBOL_GPL(uwb_rc_vcmd);
  250. /**
  251. * Reset a UWB Host Controller (and all radio settings)
  252. *
  253. * @rc: Host Controller descriptor
  254. * @returns: 0 if ok, < 0 errno code on error
  255. *
  256. * We put the command on kmalloc'ed memory as some arches cannot do
  257. * USB from the stack. The reply event is copied from an stage buffer,
  258. * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details.
  259. */
  260. int uwb_rc_reset(struct uwb_rc *rc)
  261. {
  262. int result = -ENOMEM;
  263. struct uwb_rc_evt_confirm reply;
  264. struct uwb_rccb *cmd;
  265. size_t cmd_size = sizeof(*cmd);
  266. mutex_lock(&rc->uwb_dev.mutex);
  267. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  268. if (cmd == NULL)
  269. goto error_kzalloc;
  270. cmd->bCommandType = UWB_RC_CET_GENERAL;
  271. cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET);
  272. reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  273. reply.rceb.wEvent = UWB_RC_CMD_RESET;
  274. result = uwb_rc_cmd(rc, "RESET", cmd, cmd_size,
  275. &reply.rceb, sizeof(reply));
  276. if (result < 0)
  277. goto error_cmd;
  278. if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  279. dev_err(&rc->uwb_dev.dev,
  280. "RESET: command execution failed: %s (%d)\n",
  281. uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
  282. result = -EIO;
  283. }
  284. error_cmd:
  285. kfree(cmd);
  286. error_kzalloc:
  287. mutex_unlock(&rc->uwb_dev.mutex);
  288. return result;
  289. }
  290. int uwbd_msg_handle_reset(struct uwb_event *evt)
  291. {
  292. struct uwb_rc *rc = evt->rc;
  293. int ret;
  294. dev_info(&rc->uwb_dev.dev, "resetting radio controller\n");
  295. ret = rc->reset(rc);
  296. if (ret < 0) {
  297. dev_err(&rc->uwb_dev.dev, "failed to reset hardware: %d\n", ret);
  298. goto error;
  299. }
  300. return 0;
  301. error:
  302. /* Nothing can be done except try the reset again. Wait a bit
  303. to avoid reset loops during probe() or remove(). */
  304. msleep(1000);
  305. uwb_rc_reset_all(rc);
  306. return ret;
  307. }
  308. /**
  309. * uwb_rc_reset_all - request a reset of the radio controller and PALs
  310. * @rc: the radio controller of the hardware device to be reset.
  311. *
  312. * The full hardware reset of the radio controller and all the PALs
  313. * will be scheduled.
  314. */
  315. void uwb_rc_reset_all(struct uwb_rc *rc)
  316. {
  317. struct uwb_event *evt;
  318. evt = kzalloc(sizeof(struct uwb_event), GFP_ATOMIC);
  319. if (unlikely(evt == NULL))
  320. return;
  321. evt->rc = __uwb_rc_get(rc); /* will be put by uwbd's uwbd_event_handle() */
  322. evt->ts_jiffies = jiffies;
  323. evt->type = UWB_EVT_TYPE_MSG;
  324. evt->message = UWB_EVT_MSG_RESET;
  325. uwbd_event_queue(evt);
  326. }
  327. EXPORT_SYMBOL_GPL(uwb_rc_reset_all);
  328. void uwb_rc_pre_reset(struct uwb_rc *rc)
  329. {
  330. rc->stop(rc);
  331. uwbd_flush(rc);
  332. uwb_radio_reset_state(rc);
  333. uwb_rsv_remove_all(rc);
  334. }
  335. EXPORT_SYMBOL_GPL(uwb_rc_pre_reset);
  336. int uwb_rc_post_reset(struct uwb_rc *rc)
  337. {
  338. int ret;
  339. ret = rc->start(rc);
  340. if (ret)
  341. goto out;
  342. ret = uwb_rc_mac_addr_set(rc, &rc->uwb_dev.mac_addr);
  343. if (ret)
  344. goto out;
  345. ret = uwb_rc_dev_addr_set(rc, &rc->uwb_dev.dev_addr);
  346. if (ret)
  347. goto out;
  348. out:
  349. return ret;
  350. }
  351. EXPORT_SYMBOL_GPL(uwb_rc_post_reset);