dep.c 16 KB

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