reset.c 11 KB

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