command.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #define pr_fmt(fmt) "hci: %s: " fmt, __func__
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/module.h>
  24. #include <net/nfc/hci.h>
  25. #include "hci.h"
  26. static void nfc_hci_execute_cb(struct nfc_hci_dev *hdev, int err,
  27. struct sk_buff *skb, void *cb_data)
  28. {
  29. struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)cb_data;
  30. pr_debug("HCI Cmd completed with result=%d\n", err);
  31. hcp_ew->exec_result = err;
  32. if (hcp_ew->exec_result == 0)
  33. hcp_ew->result_skb = skb;
  34. else
  35. kfree_skb(skb);
  36. hcp_ew->exec_complete = true;
  37. wake_up(hcp_ew->wq);
  38. }
  39. static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  40. const u8 *param, size_t param_len,
  41. struct sk_buff **skb)
  42. {
  43. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq);
  44. struct hcp_exec_waiter hcp_ew;
  45. hcp_ew.wq = &ew_wq;
  46. hcp_ew.exec_complete = false;
  47. hcp_ew.result_skb = NULL;
  48. pr_debug("through pipe=%d, cmd=%d, plen=%zd\n", pipe, cmd, param_len);
  49. /* TODO: Define hci cmd execution delay. Should it be the same
  50. * for all commands?
  51. */
  52. hcp_ew.exec_result = nfc_hci_hcp_message_tx(hdev, pipe,
  53. NFC_HCI_HCP_COMMAND, cmd,
  54. param, param_len,
  55. nfc_hci_execute_cb, &hcp_ew,
  56. 3000);
  57. if (hcp_ew.exec_result < 0)
  58. return hcp_ew.exec_result;
  59. wait_event(ew_wq, hcp_ew.exec_complete == true);
  60. if (hcp_ew.exec_result == 0) {
  61. if (skb)
  62. *skb = hcp_ew.result_skb;
  63. else
  64. kfree_skb(hcp_ew.result_skb);
  65. }
  66. return hcp_ew.exec_result;
  67. }
  68. int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
  69. const u8 *param, size_t param_len)
  70. {
  71. u8 pipe;
  72. pr_debug("%d to gate %d\n", event, gate);
  73. pipe = hdev->gate2pipe[gate];
  74. if (pipe == NFC_HCI_INVALID_PIPE)
  75. return -EADDRNOTAVAIL;
  76. return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_EVENT, event,
  77. param, param_len, NULL, NULL, 0);
  78. }
  79. EXPORT_SYMBOL(nfc_hci_send_event);
  80. int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response,
  81. const u8 *param, size_t param_len)
  82. {
  83. u8 pipe;
  84. pr_debug("\n");
  85. pipe = hdev->gate2pipe[gate];
  86. if (pipe == NFC_HCI_INVALID_PIPE)
  87. return -EADDRNOTAVAIL;
  88. return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
  89. response, param, param_len, NULL, NULL,
  90. 0);
  91. }
  92. EXPORT_SYMBOL(nfc_hci_send_response);
  93. /*
  94. * Execute an hci command sent to gate.
  95. * skb will contain response data if success. skb can be NULL if you are not
  96. * interested by the response.
  97. */
  98. int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
  99. const u8 *param, size_t param_len, struct sk_buff **skb)
  100. {
  101. u8 pipe;
  102. pr_debug("\n");
  103. pipe = hdev->gate2pipe[gate];
  104. if (pipe == NFC_HCI_INVALID_PIPE)
  105. return -EADDRNOTAVAIL;
  106. return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
  107. }
  108. EXPORT_SYMBOL(nfc_hci_send_cmd);
  109. int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
  110. const u8 *param, size_t param_len)
  111. {
  112. int r;
  113. u8 *tmp;
  114. /* TODO ELa: reg idx must be inserted before param, but we don't want
  115. * to ask the caller to do it to keep a simpler API.
  116. * For now, just create a new temporary param buffer. This is far from
  117. * optimal though, and the plan is to modify APIs to pass idx down to
  118. * nfc_hci_hcp_message_tx where the frame is actually built, thereby
  119. * eliminating the need for the temp allocation-copy here.
  120. */
  121. pr_debug("idx=%d to gate %d\n", idx, gate);
  122. tmp = kmalloc(1 + param_len, GFP_KERNEL);
  123. if (tmp == NULL)
  124. return -ENOMEM;
  125. *tmp = idx;
  126. memcpy(tmp + 1, param, param_len);
  127. r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
  128. tmp, param_len + 1, NULL);
  129. kfree(tmp);
  130. return r;
  131. }
  132. EXPORT_SYMBOL(nfc_hci_set_param);
  133. int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
  134. struct sk_buff **skb)
  135. {
  136. pr_debug("gate=%d regidx=%d\n", gate, idx);
  137. return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
  138. &idx, 1, skb);
  139. }
  140. EXPORT_SYMBOL(nfc_hci_get_param);
  141. static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
  142. {
  143. struct sk_buff *skb;
  144. int r;
  145. pr_debug("pipe=%d\n", pipe);
  146. r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
  147. NULL, 0, &skb);
  148. if (r == 0) {
  149. /* dest host other than host controller will send
  150. * number of pipes already open on this gate before
  151. * execution. The number can be found in skb->data[0]
  152. */
  153. kfree_skb(skb);
  154. }
  155. return r;
  156. }
  157. static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
  158. {
  159. pr_debug("\n");
  160. return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
  161. NULL, 0, NULL);
  162. }
  163. static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
  164. u8 dest_gate, int *result)
  165. {
  166. struct sk_buff *skb;
  167. struct hci_create_pipe_params params;
  168. struct hci_create_pipe_resp *resp;
  169. u8 pipe;
  170. pr_debug("gate=%d\n", dest_gate);
  171. params.src_gate = NFC_HCI_ADMIN_GATE;
  172. params.dest_host = dest_host;
  173. params.dest_gate = dest_gate;
  174. *result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
  175. NFC_HCI_ADM_CREATE_PIPE,
  176. (u8 *) &params, sizeof(params), &skb);
  177. if (*result == 0) {
  178. resp = (struct hci_create_pipe_resp *)skb->data;
  179. pipe = resp->pipe;
  180. kfree_skb(skb);
  181. pr_debug("pipe created=%d\n", pipe);
  182. return pipe;
  183. } else
  184. return NFC_HCI_INVALID_PIPE;
  185. }
  186. static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
  187. {
  188. pr_debug("\n");
  189. return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
  190. NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
  191. }
  192. static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
  193. {
  194. int r;
  195. u8 param[2];
  196. /* TODO: Find out what the identity reference data is
  197. * and fill param with it. HCI spec 6.1.3.5 */
  198. pr_debug("\n");
  199. r = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
  200. NFC_HCI_ADM_CLEAR_ALL_PIPE, param, 2, NULL);
  201. return 0;
  202. }
  203. int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
  204. {
  205. int r;
  206. u8 pipe = hdev->gate2pipe[gate];
  207. pr_debug("\n");
  208. if (pipe == NFC_HCI_INVALID_PIPE)
  209. return -EADDRNOTAVAIL;
  210. r = nfc_hci_close_pipe(hdev, pipe);
  211. if (r < 0)
  212. return r;
  213. if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
  214. r = nfc_hci_delete_pipe(hdev, pipe);
  215. if (r < 0)
  216. return r;
  217. }
  218. hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(nfc_hci_disconnect_gate);
  222. int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
  223. {
  224. int r;
  225. pr_debug("\n");
  226. r = nfc_hci_clear_all_pipes(hdev);
  227. if (r < 0)
  228. return r;
  229. memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
  230. return 0;
  231. }
  232. EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
  233. int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
  234. u8 pipe)
  235. {
  236. bool pipe_created = false;
  237. int r;
  238. pr_debug("\n");
  239. if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
  240. return -EADDRINUSE;
  241. if (pipe != NFC_HCI_INVALID_PIPE)
  242. goto pipe_is_open;
  243. switch (dest_gate) {
  244. case NFC_HCI_LINK_MGMT_GATE:
  245. pipe = NFC_HCI_LINK_MGMT_PIPE;
  246. break;
  247. case NFC_HCI_ADMIN_GATE:
  248. pipe = NFC_HCI_ADMIN_PIPE;
  249. break;
  250. default:
  251. pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
  252. if (pipe == NFC_HCI_INVALID_PIPE)
  253. return r;
  254. pipe_created = true;
  255. break;
  256. }
  257. r = nfc_hci_open_pipe(hdev, pipe);
  258. if (r < 0) {
  259. if (pipe_created)
  260. if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
  261. /* TODO: Cannot clean by deleting pipe...
  262. * -> inconsistent state */
  263. }
  264. return r;
  265. }
  266. pipe_is_open:
  267. hdev->gate2pipe[dest_gate] = pipe;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(nfc_hci_connect_gate);