dep.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. return -EIO;
  165. if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE)
  166. return -EPROTO;
  167. atr_req = (struct st21nfca_atr_req *)skb->data;
  168. if (atr_req->length < sizeof(struct st21nfca_atr_req))
  169. return -EPROTO;
  170. r = st21nfca_tm_send_atr_res(hdev, atr_req);
  171. if (r)
  172. return r;
  173. gb_len = skb->len - sizeof(struct st21nfca_atr_req);
  174. r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
  175. NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
  176. if (r)
  177. return r;
  178. return 0;
  179. }
  180. static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
  181. struct st21nfca_psl_req *psl_req)
  182. {
  183. struct st21nfca_psl_res *psl_res;
  184. struct sk_buff *skb;
  185. u8 bitrate[2] = {0, 0};
  186. int r;
  187. skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
  188. if (!skb)
  189. return -ENOMEM;
  190. skb_put(skb, sizeof(struct st21nfca_psl_res));
  191. psl_res = (struct st21nfca_psl_res *)skb->data;
  192. psl_res->length = sizeof(struct st21nfca_psl_res);
  193. psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
  194. psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
  195. psl_res->did = psl_req->did;
  196. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  197. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  198. if (r < 0)
  199. goto error;
  200. /*
  201. * ST21NFCA only support P2P passive.
  202. * PSL_REQ BRS value != 0 has only a meaning to
  203. * change technology to type F.
  204. * We change to BITRATE 424Kbits.
  205. * In other case switch to BITRATE 106Kbits.
  206. */
  207. if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
  208. ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
  209. bitrate[0] = ST21NFCA_CARD_BITRATE_424;
  210. bitrate[1] = ST21NFCA_CARD_BITRATE_424;
  211. }
  212. /* Send an event to change bitrate change event to card f */
  213. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  214. ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
  215. error:
  216. kfree_skb(skb);
  217. return r;
  218. }
  219. static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
  220. struct sk_buff *skb)
  221. {
  222. struct st21nfca_psl_req *psl_req;
  223. skb_trim(skb, skb->len - 1);
  224. if (!skb->len)
  225. return -EIO;
  226. psl_req = (struct st21nfca_psl_req *)skb->data;
  227. if (skb->len < sizeof(struct st21nfca_psl_req))
  228. return -EIO;
  229. return st21nfca_tm_send_psl_res(hdev, psl_req);
  230. }
  231. int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  232. {
  233. int r;
  234. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  235. *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
  236. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
  237. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
  238. *(u8 *)skb_push(skb, 1) = skb->len;
  239. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  240. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  241. kfree_skb(skb);
  242. return r;
  243. }
  244. EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
  245. static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
  246. struct sk_buff *skb)
  247. {
  248. struct st21nfca_dep_req_res *dep_req;
  249. u8 size;
  250. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  251. skb_trim(skb, skb->len - 1);
  252. size = 4;
  253. dep_req = (struct st21nfca_dep_req_res *)skb->data;
  254. if (skb->len < size)
  255. return -EIO;
  256. if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
  257. size++;
  258. if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
  259. size++;
  260. if (skb->len < size)
  261. return -EIO;
  262. /* Receiving DEP_REQ - Decoding */
  263. switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
  264. case ST21NFCA_NFC_DEP_PFB_I_PDU:
  265. info->dep_info.curr_nfc_dep_pni =
  266. ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
  267. break;
  268. case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
  269. pr_err("Received a ACK/NACK PDU\n");
  270. break;
  271. case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
  272. pr_err("Received a SUPERVISOR PDU\n");
  273. break;
  274. }
  275. skb_pull(skb, size);
  276. return nfc_tm_data_received(hdev->ndev, skb);
  277. }
  278. static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
  279. struct sk_buff *skb)
  280. {
  281. u8 cmd0, cmd1;
  282. int r;
  283. cmd0 = skb->data[1];
  284. switch (cmd0) {
  285. case ST21NFCA_NFCIP1_REQ:
  286. cmd1 = skb->data[2];
  287. switch (cmd1) {
  288. case ST21NFCA_NFCIP1_ATR_REQ:
  289. r = st21nfca_tm_recv_atr_req(hdev, skb);
  290. break;
  291. case ST21NFCA_NFCIP1_PSL_REQ:
  292. r = st21nfca_tm_recv_psl_req(hdev, skb);
  293. break;
  294. case ST21NFCA_NFCIP1_DEP_REQ:
  295. r = st21nfca_tm_recv_dep_req(hdev, skb);
  296. break;
  297. default:
  298. return 1;
  299. }
  300. break;
  301. default:
  302. return 1;
  303. }
  304. return r;
  305. }
  306. /*
  307. * Returns:
  308. * <= 0: driver handled the event, skb consumed
  309. * 1: driver does not handle the event, please do standard processing
  310. */
  311. int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
  312. u8 event, struct sk_buff *skb)
  313. {
  314. int r = 0;
  315. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  316. pr_debug("dep event: %d\n", event);
  317. switch (event) {
  318. case ST21NFCA_EVT_CARD_ACTIVATED:
  319. info->dep_info.curr_nfc_dep_pni = 0;
  320. break;
  321. case ST21NFCA_EVT_CARD_DEACTIVATED:
  322. break;
  323. case ST21NFCA_EVT_FIELD_ON:
  324. break;
  325. case ST21NFCA_EVT_FIELD_OFF:
  326. break;
  327. case ST21NFCA_EVT_SEND_DATA:
  328. r = st21nfca_tm_event_send_data(hdev, skb);
  329. if (r < 0)
  330. return r;
  331. return 0;
  332. default:
  333. nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
  334. return 1;
  335. }
  336. kfree_skb(skb);
  337. return r;
  338. }
  339. EXPORT_SYMBOL(st21nfca_dep_event_received);
  340. static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
  341. u8 bri, u8 lri)
  342. {
  343. struct sk_buff *skb;
  344. struct st21nfca_psl_req *psl_req;
  345. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  346. skb =
  347. alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
  348. if (!skb)
  349. return;
  350. skb_reserve(skb, 1);
  351. skb_put(skb, sizeof(struct st21nfca_psl_req));
  352. psl_req = (struct st21nfca_psl_req *) skb->data;
  353. psl_req->length = sizeof(struct st21nfca_psl_req);
  354. psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
  355. psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
  356. psl_req->did = did;
  357. psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
  358. psl_req->fsl = lri;
  359. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
  360. st21nfca_im_send_pdu(info, skb);
  361. }
  362. #define ST21NFCA_CB_TYPE_READER_F 1
  363. static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
  364. int err)
  365. {
  366. struct st21nfca_hci_info *info = context;
  367. struct st21nfca_atr_res *atr_res;
  368. int r;
  369. if (err != 0)
  370. return;
  371. if (!skb)
  372. return;
  373. switch (info->async_cb_type) {
  374. case ST21NFCA_CB_TYPE_READER_F:
  375. skb_trim(skb, skb->len - 1);
  376. atr_res = (struct st21nfca_atr_res *)skb->data;
  377. r = nfc_set_remote_general_bytes(info->hdev->ndev,
  378. atr_res->gbi,
  379. skb->len - sizeof(struct st21nfca_atr_res));
  380. if (r < 0)
  381. return;
  382. if (atr_res->to >= 0x0e)
  383. info->dep_info.to = 0x0e;
  384. else
  385. info->dep_info.to = atr_res->to + 1;
  386. info->dep_info.to |= 0x10;
  387. r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
  388. NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
  389. if (r < 0)
  390. return;
  391. info->dep_info.curr_nfc_dep_pni = 0;
  392. if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
  393. st21nfca_im_send_psl_req(info->hdev, atr_res->did,
  394. atr_res->bsi, atr_res->bri,
  395. ST21NFCA_PP2LRI(atr_res->ppi));
  396. break;
  397. default:
  398. kfree_skb(skb);
  399. break;
  400. }
  401. }
  402. int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
  403. {
  404. struct sk_buff *skb;
  405. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  406. struct st21nfca_atr_req *atr_req;
  407. struct nfc_target *target;
  408. uint size;
  409. info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
  410. size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
  411. if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
  412. PROTOCOL_ERR("14.6.1.1");
  413. return -EINVAL;
  414. }
  415. skb =
  416. alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
  417. if (!skb)
  418. return -ENOMEM;
  419. skb_reserve(skb, 1);
  420. skb_put(skb, sizeof(struct st21nfca_atr_req));
  421. atr_req = (struct st21nfca_atr_req *)skb->data;
  422. memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
  423. atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
  424. atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
  425. memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
  426. target = hdev->ndev->targets;
  427. if (target->sensf_res_len > 0)
  428. memcpy(atr_req->nfcid3, target->sensf_res,
  429. target->sensf_res_len);
  430. else
  431. get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
  432. atr_req->did = 0x0;
  433. atr_req->bsi = 0x00;
  434. atr_req->bri = 0x00;
  435. atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
  436. if (gb_len) {
  437. atr_req->ppi |= ST21NFCA_GB_BIT;
  438. skb_put_data(skb, gb, gb_len);
  439. }
  440. atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
  441. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
  442. info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
  443. info->async_cb_context = info;
  444. info->async_cb = st21nfca_im_recv_atr_res_cb;
  445. info->dep_info.bri = atr_req->bri;
  446. info->dep_info.bsi = atr_req->bsi;
  447. info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
  448. return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
  449. ST21NFCA_WR_XCHG_DATA, skb->data,
  450. skb->len, info->async_cb, info);
  451. }
  452. EXPORT_SYMBOL(st21nfca_im_send_atr_req);
  453. static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
  454. int err)
  455. {
  456. struct st21nfca_hci_info *info = context;
  457. struct st21nfca_dep_req_res *dep_res;
  458. int size;
  459. if (err != 0)
  460. return;
  461. if (!skb)
  462. return;
  463. switch (info->async_cb_type) {
  464. case ST21NFCA_CB_TYPE_READER_F:
  465. dep_res = (struct st21nfca_dep_req_res *)skb->data;
  466. size = 3;
  467. if (skb->len < size)
  468. goto exit;
  469. if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
  470. size++;
  471. if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
  472. size++;
  473. if (skb->len < size)
  474. goto exit;
  475. skb_trim(skb, skb->len - 1);
  476. /* Receiving DEP_REQ - Decoding */
  477. switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
  478. case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
  479. pr_err("Received a ACK/NACK PDU\n");
  480. fallthrough;
  481. case ST21NFCA_NFC_DEP_PFB_I_PDU:
  482. info->dep_info.curr_nfc_dep_pni =
  483. ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
  484. size++;
  485. skb_pull(skb, size);
  486. nfc_tm_data_received(info->hdev->ndev, skb);
  487. break;
  488. case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
  489. pr_err("Received a SUPERVISOR PDU\n");
  490. skb_pull(skb, size);
  491. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
  492. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
  493. *(u8 *)skb_push(skb, 1) = skb->len;
  494. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
  495. st21nfca_im_send_pdu(info, skb);
  496. break;
  497. }
  498. return;
  499. default:
  500. break;
  501. }
  502. exit:
  503. kfree_skb(skb);
  504. }
  505. int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  506. {
  507. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  508. info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
  509. info->async_cb_context = info;
  510. info->async_cb = st21nfca_im_recv_dep_res_cb;
  511. *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
  512. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
  513. *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
  514. *(u8 *)skb_push(skb, 1) = skb->len;
  515. *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
  516. return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
  517. ST21NFCA_WR_XCHG_DATA,
  518. skb->data, skb->len,
  519. info->async_cb, info);
  520. }
  521. EXPORT_SYMBOL(st21nfca_im_send_dep_req);
  522. void st21nfca_dep_init(struct nfc_hci_dev *hdev)
  523. {
  524. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  525. INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
  526. info->dep_info.curr_nfc_dep_pni = 0;
  527. info->dep_info.idx = 0;
  528. info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
  529. }
  530. EXPORT_SYMBOL(st21nfca_dep_init);
  531. void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
  532. {
  533. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  534. cancel_work_sync(&info->dep_info.tx_work);
  535. }
  536. EXPORT_SYMBOL(st21nfca_dep_deinit);