reset.c 11 KB

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