dep.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <net/nfc/hci.h>
  17. #include "st21nfca.h"
  18. #define ST21NFCA_NFCIP1_INITIATOR 0x00
  19. #define ST21NFCA_NFCIP1_REQ 0xd4
  20. #define ST21NFCA_NFCIP1_RES 0xd5
  21. #define ST21NFCA_NFCIP1_ATR_REQ 0x00
  22. #define ST21NFCA_NFCIP1_ATR_RES 0x01
  23. #define ST21NFCA_NFCIP1_PSL_REQ 0x04
  24. #define ST21NFCA_NFCIP1_PSL_RES 0x05
  25. #define ST21NFCA_NFCIP1_DEP_REQ 0x06
  26. #define ST21NFCA_NFCIP1_DEP_RES 0x07
  27. #define ST21NFCA_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
  28. #define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
  29. #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  30. ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
  31. #define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
  32. #define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
  33. #define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10
  34. #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  35. ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
  36. #define ST21NFCA_NFC_DEP_PFB_I_PDU 0x00
  37. #define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU 0x40
  38. #define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
  39. #define ST21NFCA_ATR_REQ_MIN_SIZE 17
  40. #define ST21NFCA_ATR_REQ_MAX_SIZE 65
  41. #define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30
  42. #define ST21NFCA_GB_BIT 0x02
  43. #define ST21NFCA_EVT_SEND_DATA 0x10
  44. #define ST21NFCA_EVT_FIELD_ON 0x11
  45. #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
  46. #define ST21NFCA_EVT_CARD_ACTIVATED 0x13
  47. #define ST21NFCA_EVT_FIELD_OFF 0x14
  48. #define ST21NFCA_EVT_CARD_F_BITRATE 0x16
  49. #define ST21NFCA_EVT_READER_F_BITRATE 0x13
  50. #define ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38)
  51. #define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07)
  52. #define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4)
  53. #define ST21NFCA_CARD_BITRATE_212 0x01
  54. #define ST21NFCA_CARD_BITRATE_424 0x02
  55. #define ST21NFCA_DEFAULT_TIMEOUT 0x0a
  56. #define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \
  57. __LINE__, req)
  58. struct st21nfca_atr_req {
  59. u8 length;
  60. u8 cmd0;
  61. u8 cmd1;
  62. u8 nfcid3[NFC_NFCID3_MAXSIZE];
  63. u8 did;
  64. u8 bsi;
  65. u8 bri;
  66. u8 ppi;
  67. u8 gbi[0];
  68. } __packed;
  69. struct st21nfca_atr_res {
  70. u8 length;
  71. u8 cmd0;
  72. u8 cmd1;
  73. u8 nfcid3[NFC_NFCID3_MAXSIZE];
  74. u8 did;
  75. u8 bsi;
  76. u8 bri;
  77. u8 to;
  78. u8 ppi;
  79. u8 gbi[0];
  80. } __packed;
  81. struct st21nfca_psl_req {
  82. u8 length;
  83. u8 cmd0;
  84. u8 cmd1;
  85. u8 did;
  86. u8 brs;
  87. u8 fsl;
  88. } __packed;
  89. struct st21nfca_psl_res {
  90. u8 length;
  91. u8 cmd0;
  92. u8 cmd1;
  93. u8 did;
  94. } __packed;
  95. struct st21nfca_dep_req_res {
  96. u8 length;
  97. u8 cmd0;
  98. u8 cmd1;
  99. u8 pfb;
  100. u8 did;
  101. u8 nad;
  102. } __packed;
  103. static void st21nfca_tx_work(struct work_struct *work)
  104. {
  105. struct st21nfca_hci_info *info = container_of(work,
  106. struct st21nfca_hci_info,
  107. dep_info.tx_work);
  108. struct nfc_dev *dev;
  109. struct sk_buff *skb;
  110. if (info) {
  111. dev = info->hdev->ndev;
  112. skb = info->dep_info.tx_pending;
  113. device_lock(&dev->dev);
  114. nfc_hci_send_cmd_async(info->hdev, ST21NFCA_RF_READER_F_GATE,
  115. ST21NFCA_WR_XCHG_DATA, skb->data, skb->len,
  116. info->async_cb, info);
  117. device_unlock(&dev->dev);
  118. kfree_skb(skb);
  119. }
  120. }
  121. static void st21nfca_im_send_pdu(struct st21nfca_hci_info *info,
  122. struct sk_buff *skb)
  123. {
  124. info->dep_info.tx_pending = skb;
  125. schedule_work(&info->dep_info.tx_work);
  126. }
  127. static int st21nfca_tm_send_atr_res(struct nfc_hci_dev *hdev,
  128. struct st21nfca_atr_req *atr_req)
  129. {
  130. struct st21nfca_atr_res *atr_res;
  131. struct sk_buff *skb;
  132. size_t gb_len;
  133. int r;
  134. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  135. gb_len = atr_req->length - sizeof(struct st21nfca_atr_req);
  136. skb = alloc_skb(atr_req->length + 1, GFP_KERNEL);
  137. if (!skb)
  138. return -ENOMEM;
  139. skb_put(skb, sizeof(struct st21nfca_atr_res));
  140. atr_res = (struct st21nfca_atr_res *)skb->data;
  141. memset(atr_res, 0, sizeof(struct st21nfca_atr_res));
  142. atr_res->length = atr_req->length + 1;
  143. atr_res->cmd0 = ST21NFCA_NFCIP1_RES;
  144. atr_res->cmd1 = ST21NFCA_NFCIP1_ATR_RES;
  145. memcpy(atr_res->nfcid3, atr_req->nfcid3, 6);
  146. atr_res->bsi = 0x00;
  147. atr_res->bri = 0x00;
  148. atr_res->to = ST21NFCA_DEFAULT_TIMEOUT;
  149. atr_res->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
  150. if (gb_len) {
  151. skb_put(skb, gb_len);
  152. atr_res->ppi |= ST21NFCA_GB_BIT;
  153. memcpy(atr_res->gbi, atr_req->gbi, gb_len);
  154. r = nfc_set_remote_general_bytes(hdev->ndev, atr_res->gbi,
  155. gb_len);
  156. if (r < 0) {
  157. kfree_skb(skb);
  158. return r;
  159. }
  160. }
  161. info->dep_info.curr_nfc_dep_pni = 0;
  162. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  163. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  164. kfree_skb(skb);
  165. return r;
  166. }
  167. static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
  168. struct sk_buff *skb)
  169. {
  170. struct st21nfca_atr_req *atr_req;
  171. size_t gb_len;
  172. int r;
  173. skb_trim(skb, skb->len - 1);
  174. if (!skb->len) {
  175. r = -EIO;
  176. goto exit;
  177. }
  178. if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
  179. r = -EPROTO;
  180. goto exit;
  181. }
  182. atr_req = (struct st21nfca_atr_req *)skb->data;
  183. if (atr_req->length < sizeof(struct st21nfca_atr_req)) {
  184. r = -EPROTO;
  185. goto exit;
  186. }
  187. r = st21nfca_tm_send_atr_res(hdev, atr_req);
  188. if (r)
  189. goto exit;
  190. gb_len = skb->len - sizeof(struct st21nfca_atr_req);
  191. r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
  192. NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
  193. if (r)
  194. goto exit;
  195. r = 0;
  196. exit:
  197. return r;
  198. }
  199. static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
  200. struct st21nfca_psl_req *psl_req)
  201. {
  202. struct st21nfca_psl_res *psl_res;
  203. struct sk_buff *skb;
  204. u8 bitrate[2] = {0, 0};
  205. int r;
  206. skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
  207. if (!skb)
  208. return -ENOMEM;
  209. skb_put(skb, sizeof(struct st21nfca_psl_res));
  210. psl_res = (struct st21nfca_psl_res *)skb->data;
  211. psl_res->length = sizeof(struct st21nfca_psl_res);
  212. psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
  213. psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
  214. psl_res->did = psl_req->did;
  215. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  216. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  217. if (r < 0)
  218. goto error;
  219. /*
  220. * ST21NFCA only support P2P passive.
  221. * PSL_REQ BRS value != 0 has only a meaning to
  222. * change technology to type F.
  223. * We change to BITRATE 424Kbits.
  224. * In other case switch to BITRATE 106Kbits.
  225. */
  226. if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
  227. ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
  228. bitrate[0] = ST21NFCA_CARD_BITRATE_424;
  229. bitrate[1] = ST21NFCA_CARD_BITRATE_424;
  230. }
  231. /* Send an event to change bitrate change event to card f */
  232. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  233. ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
  234. error:
  235. kfree_skb(skb);
  236. return r;
  237. }
  238. static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
  239. struct sk_buff *skb)
  240. {
  241. struct st21nfca_psl_req *psl_req;
  242. int r;
  243. skb_trim(skb, skb->len - 1);
  244. if (!skb->len) {
  245. r = -EIO;
  246. goto exit;
  247. }
  248. psl_req = (struct st21nfca_psl_req *)skb->data;
  249. if (skb->len < sizeof(struct st21nfca_psl_req)) {
  250. r = -EIO;
  251. goto exit;
  252. }
  253. r = st21nfca_tm_send_psl_res(hdev, psl_req);
  254. exit:
  255. return r;
  256. }
  257. int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  258. {
  259. int r;
  260. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  261. *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
  262. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
  263. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
  264. *(u8 *)skb_push(skb, 1) = skb->len;
  265. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  266. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  267. kfree_skb(skb);
  268. return r;
  269. }
  270. EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
  271. static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
  272. struct sk_buff *skb)
  273. {
  274. struct st21nfca_dep_req_res *dep_req;
  275. u8 size;
  276. int r;
  277. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  278. skb_trim(skb, skb->len - 1);
  279. size = 4;
  280. dep_req = (struct st21nfca_dep_req_res *)skb->data;
  281. if (skb->len < size) {
  282. r = -EIO;
  283. goto exit;
  284. }
  285. if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
  286. size++;
  287. if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
  288. size++;
  289. if (skb->len < size) {
  290. r = -EIO;
  291. goto exit;
  292. }
  293. /* Receiving DEP_REQ - Decoding */
  294. switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
  295. case ST21NFCA_NFC_DEP_PFB_I_PDU:
  296. info->dep_info.curr_nfc_dep_pni =
  297. ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
  298. break;
  299. case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
  300. pr_err("Received a ACK/NACK PDU\n");
  301. break;
  302. case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
  303. pr_err("Received a SUPERVISOR PDU\n");
  304. break;
  305. }
  306. skb_pull(skb, size);
  307. return nfc_tm_data_received(hdev->ndev, skb);
  308. exit:
  309. return r;
  310. }
  311. static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
  312. struct sk_buff *skb)
  313. {
  314. u8 cmd0, cmd1;
  315. int r;
  316. cmd0 = skb->data[1];
  317. switch (cmd0) {
  318. case ST21NFCA_NFCIP1_REQ:
  319. cmd1 = skb->data[2];
  320. switch (cmd1) {
  321. case ST21NFCA_NFCIP1_ATR_REQ:
  322. r = st21nfca_tm_recv_atr_req(hdev, skb);
  323. break;
  324. case ST21NFCA_NFCIP1_PSL_REQ:
  325. r = st21nfca_tm_recv_psl_req(hdev, skb);
  326. break;
  327. case ST21NFCA_NFCIP1_DEP_REQ:
  328. r = st21nfca_tm_recv_dep_req(hdev, skb);
  329. break;
  330. default:
  331. return 1;
  332. }
  333. default:
  334. return 1;
  335. }
  336. return r;
  337. }
  338. /*
  339. * Returns:
  340. * <= 0: driver handled the event, skb consumed
  341. * 1: driver does not handle the event, please do standard processing
  342. */
  343. int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
  344. u8 event, struct sk_buff *skb)
  345. {
  346. int r = 0;
  347. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  348. pr_debug("dep event: %d\n", event);
  349. switch (event) {
  350. case ST21NFCA_EVT_CARD_ACTIVATED:
  351. info->dep_info.curr_nfc_dep_pni = 0;
  352. break;
  353. case ST21NFCA_EVT_CARD_DEACTIVATED:
  354. break;
  355. case ST21NFCA_EVT_FIELD_ON:
  356. break;
  357. case ST21NFCA_EVT_FIELD_OFF:
  358. break;
  359. case ST21NFCA_EVT_SEND_DATA:
  360. r = st21nfca_tm_event_send_data(hdev, skb);
  361. if (r < 0)
  362. return r;
  363. return 0;
  364. default:
  365. nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
  366. return 1;
  367. }
  368. kfree_skb(skb);
  369. return r;
  370. }
  371. EXPORT_SYMBOL(st21nfca_dep_event_received);
  372. static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
  373. u8 bri, u8 lri)
  374. {
  375. struct sk_buff *skb;
  376. struct st21nfca_psl_req *psl_req;
  377. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  378. skb =
  379. alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
  380. if (!skb)
  381. return;
  382. skb_reserve(skb, 1);
  383. skb_put(skb, sizeof(struct st21nfca_psl_req));
  384. psl_req = (struct st21nfca_psl_req *) skb->data;
  385. psl_req->length = sizeof(struct st21nfca_psl_req);
  386. psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
  387. psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
  388. psl_req->did = did;
  389. psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
  390. psl_req->fsl = lri;
  391. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
  392. st21nfca_im_send_pdu(info, skb);
  393. }
  394. #define ST21NFCA_CB_TYPE_READER_F 1
  395. static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
  396. int err)
  397. {
  398. struct st21nfca_hci_info *info = context;
  399. struct st21nfca_atr_res *atr_res;
  400. int r;
  401. if (err != 0)
  402. return;
  403. if (!skb)
  404. return;
  405. switch (info->async_cb_type) {
  406. case ST21NFCA_CB_TYPE_READER_F:
  407. skb_trim(skb, skb->len - 1);
  408. atr_res = (struct st21nfca_atr_res *)skb->data;
  409. r = nfc_set_remote_general_bytes(info->hdev->ndev,
  410. atr_res->gbi,
  411. skb->len - sizeof(struct st21nfca_atr_res));
  412. if (r < 0)
  413. return;
  414. if (atr_res->to >= 0x0e)
  415. info->dep_info.to = 0x0e;
  416. else
  417. info->dep_info.to = atr_res->to + 1;
  418. info->dep_info.to |= 0x10;
  419. r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
  420. NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
  421. if (r < 0)
  422. return;
  423. info->dep_info.curr_nfc_dep_pni = 0;
  424. if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
  425. st21nfca_im_send_psl_req(info->hdev, atr_res->did,
  426. atr_res->bsi, atr_res->bri,
  427. ST21NFCA_PP2LRI(atr_res->ppi));
  428. break;
  429. default:
  430. kfree_skb(skb);
  431. break;
  432. }
  433. }
  434. int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
  435. {
  436. struct sk_buff *skb;
  437. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  438. struct st21nfca_atr_req *atr_req;
  439. struct nfc_target *target;
  440. uint size;
  441. info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
  442. size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
  443. if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
  444. PROTOCOL_ERR("14.6.1.1");
  445. return -EINVAL;
  446. }
  447. skb =
  448. alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
  449. if (!skb)
  450. return -ENOMEM;
  451. skb_reserve(skb, 1);
  452. skb_put(skb, sizeof(struct st21nfca_atr_req));
  453. atr_req = (struct st21nfca_atr_req *)skb->data;
  454. memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
  455. atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
  456. atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
  457. memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
  458. target = hdev->ndev->targets;
  459. if (target->sensf_res_len > 0)
  460. memcpy(atr_req->nfcid3, target->sensf_res,
  461. target->sensf_res_len);
  462. else
  463. get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
  464. atr_req->did = 0x0;
  465. atr_req->bsi = 0x00;
  466. atr_req->bri = 0x00;
  467. atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
  468. if (gb_len) {
  469. atr_req->ppi |= ST21NFCA_GB_BIT;
  470. skb_put_data(skb, gb, gb_len);
  471. }
  472. atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
  473. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
  474. info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
  475. info->async_cb_context = info;
  476. info->async_cb = st21nfca_im_recv_atr_res_cb;
  477. info->dep_info.bri = atr_req->bri;
  478. info->dep_info.bsi = atr_req->bsi;
  479. info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
  480. return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
  481. ST21NFCA_WR_XCHG_DATA, skb->data,
  482. skb->len, info->async_cb, info);
  483. }
  484. EXPORT_SYMBOL(st21nfca_im_send_atr_req);
  485. static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
  486. int err)
  487. {
  488. struct st21nfca_hci_info *info = context;
  489. struct st21nfca_dep_req_res *dep_res;
  490. int size;
  491. if (err != 0)
  492. return;
  493. if (!skb)
  494. return;
  495. switch (info->async_cb_type) {
  496. case ST21NFCA_CB_TYPE_READER_F:
  497. dep_res = (struct st21nfca_dep_req_res *)skb->data;
  498. size = 3;
  499. if (skb->len < size)
  500. goto exit;
  501. if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
  502. size++;
  503. if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
  504. size++;
  505. if (skb->len < size)
  506. goto exit;
  507. skb_trim(skb, skb->len - 1);
  508. /* Receiving DEP_REQ - Decoding */
  509. switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
  510. case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
  511. pr_err("Received a ACK/NACK PDU\n");
  512. case ST21NFCA_NFC_DEP_PFB_I_PDU:
  513. info->dep_info.curr_nfc_dep_pni =
  514. ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
  515. size++;
  516. skb_pull(skb, size);
  517. nfc_tm_data_received(info->hdev->ndev, skb);
  518. break;
  519. case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
  520. pr_err("Received a SUPERVISOR PDU\n");
  521. skb_pull(skb, size);
  522. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
  523. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
  524. *(u8 *)skb_push(skb, 1) = skb->len;
  525. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
  526. st21nfca_im_send_pdu(info, skb);
  527. break;
  528. }
  529. return;
  530. default:
  531. break;
  532. }
  533. exit:
  534. kfree_skb(skb);
  535. }
  536. int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  537. {
  538. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  539. info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
  540. info->async_cb_context = info;
  541. info->async_cb = st21nfca_im_recv_dep_res_cb;
  542. *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
  543. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
  544. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
  545. *(u8 *)skb_push(skb, 1) = skb->len;
  546. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
  547. return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
  548. ST21NFCA_WR_XCHG_DATA,
  549. skb->data, skb->len,
  550. info->async_cb, info);
  551. }
  552. EXPORT_SYMBOL(st21nfca_im_send_dep_req);
  553. void st21nfca_dep_init(struct nfc_hci_dev *hdev)
  554. {
  555. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  556. INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
  557. info->dep_info.curr_nfc_dep_pni = 0;
  558. info->dep_info.idx = 0;
  559. info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
  560. }
  561. EXPORT_SYMBOL(st21nfca_dep_init);
  562. void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
  563. {
  564. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  565. cancel_work_sync(&info->dep_info.tx_work);
  566. }
  567. EXPORT_SYMBOL(st21nfca_dep_deinit);