digital_dep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * NFC Digital Protocol stack
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #define pr_fmt(fmt) "digital: %s: " fmt, __func__
  16. #include "digital.h"
  17. #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
  18. #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
  19. #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
  20. #define DIGITAL_CMD_ATR_REQ 0x00
  21. #define DIGITAL_CMD_ATR_RES 0x01
  22. #define DIGITAL_CMD_PSL_REQ 0x04
  23. #define DIGITAL_CMD_PSL_RES 0x05
  24. #define DIGITAL_CMD_DEP_REQ 0x06
  25. #define DIGITAL_CMD_DEP_RES 0x07
  26. #define DIGITAL_ATR_REQ_MIN_SIZE 16
  27. #define DIGITAL_ATR_REQ_MAX_SIZE 64
  28. #define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
  29. #define DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B \
  30. (DIGITAL_LR_BITS_PAYLOAD_SIZE_254B >> 4)
  31. #define DIGITAL_GB_BIT 0x02
  32. #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
  33. #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
  34. #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  35. ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
  36. #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
  37. #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
  38. #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
  39. #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
  40. #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
  41. #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
  42. #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
  43. struct digital_atr_req {
  44. u8 dir;
  45. u8 cmd;
  46. u8 nfcid3[10];
  47. u8 did;
  48. u8 bs;
  49. u8 br;
  50. u8 pp;
  51. u8 gb[0];
  52. } __packed;
  53. struct digital_atr_res {
  54. u8 dir;
  55. u8 cmd;
  56. u8 nfcid3[10];
  57. u8 did;
  58. u8 bs;
  59. u8 br;
  60. u8 to;
  61. u8 pp;
  62. u8 gb[0];
  63. } __packed;
  64. struct digital_psl_req {
  65. u8 dir;
  66. u8 cmd;
  67. u8 did;
  68. u8 brs;
  69. u8 fsl;
  70. } __packed;
  71. struct digital_psl_res {
  72. u8 dir;
  73. u8 cmd;
  74. u8 did;
  75. } __packed;
  76. struct digital_dep_req_res {
  77. u8 dir;
  78. u8 cmd;
  79. u8 pfb;
  80. } __packed;
  81. static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
  82. struct sk_buff *resp);
  83. static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
  84. struct sk_buff *skb)
  85. {
  86. skb_push(skb, sizeof(u8));
  87. skb->data[0] = skb->len;
  88. if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
  89. *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
  90. }
  91. static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
  92. struct sk_buff *skb)
  93. {
  94. u8 size;
  95. if (skb->len < 2)
  96. return -EIO;
  97. if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
  98. skb_pull(skb, sizeof(u8));
  99. size = skb->data[0];
  100. if (size != skb->len)
  101. return -EIO;
  102. skb_pull(skb, sizeof(u8));
  103. return 0;
  104. }
  105. static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
  106. struct sk_buff *resp)
  107. {
  108. struct nfc_target *target = arg;
  109. struct digital_psl_res *psl_res;
  110. int rc;
  111. if (IS_ERR(resp)) {
  112. rc = PTR_ERR(resp);
  113. resp = NULL;
  114. goto exit;
  115. }
  116. rc = ddev->skb_check_crc(resp);
  117. if (rc) {
  118. PROTOCOL_ERR("14.4.1.6");
  119. goto exit;
  120. }
  121. rc = digital_skb_pull_dep_sod(ddev, resp);
  122. if (rc) {
  123. PROTOCOL_ERR("14.4.1.2");
  124. goto exit;
  125. }
  126. psl_res = (struct digital_psl_res *)resp->data;
  127. if ((resp->len != sizeof(*psl_res)) ||
  128. (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
  129. (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
  130. rc = -EIO;
  131. goto exit;
  132. }
  133. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
  134. NFC_DIGITAL_RF_TECH_424F);
  135. if (rc)
  136. goto exit;
  137. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  138. NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
  139. if (rc)
  140. goto exit;
  141. if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
  142. (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
  143. ddev->skb_add_crc = digital_skb_add_crc_f;
  144. ddev->skb_check_crc = digital_skb_check_crc_f;
  145. }
  146. ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
  147. nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
  148. NFC_RF_INITIATOR);
  149. ddev->curr_nfc_dep_pni = 0;
  150. exit:
  151. dev_kfree_skb(resp);
  152. if (rc)
  153. ddev->curr_protocol = 0;
  154. }
  155. static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
  156. struct nfc_target *target)
  157. {
  158. struct sk_buff *skb;
  159. struct digital_psl_req *psl_req;
  160. skb = digital_skb_alloc(ddev, sizeof(*psl_req));
  161. if (!skb)
  162. return -ENOMEM;
  163. skb_put(skb, sizeof(*psl_req));
  164. psl_req = (struct digital_psl_req *)skb->data;
  165. psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  166. psl_req->cmd = DIGITAL_CMD_PSL_REQ;
  167. psl_req->did = 0;
  168. psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
  169. psl_req->fsl = DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B;
  170. digital_skb_push_dep_sod(ddev, skb);
  171. ddev->skb_add_crc(skb);
  172. return digital_in_send_cmd(ddev, skb, 500, digital_in_recv_psl_res,
  173. target);
  174. }
  175. static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
  176. struct sk_buff *resp)
  177. {
  178. struct nfc_target *target = arg;
  179. struct digital_atr_res *atr_res;
  180. u8 gb_len;
  181. int rc;
  182. if (IS_ERR(resp)) {
  183. rc = PTR_ERR(resp);
  184. resp = NULL;
  185. goto exit;
  186. }
  187. rc = ddev->skb_check_crc(resp);
  188. if (rc) {
  189. PROTOCOL_ERR("14.4.1.6");
  190. goto exit;
  191. }
  192. rc = digital_skb_pull_dep_sod(ddev, resp);
  193. if (rc) {
  194. PROTOCOL_ERR("14.4.1.2");
  195. goto exit;
  196. }
  197. if (resp->len < sizeof(struct digital_atr_res)) {
  198. rc = -EIO;
  199. goto exit;
  200. }
  201. gb_len = resp->len - sizeof(struct digital_atr_res);
  202. atr_res = (struct digital_atr_res *)resp->data;
  203. rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
  204. if (rc)
  205. goto exit;
  206. if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
  207. (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
  208. rc = digital_in_send_psl_req(ddev, target);
  209. if (!rc)
  210. goto exit;
  211. }
  212. rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
  213. NFC_RF_INITIATOR);
  214. ddev->curr_nfc_dep_pni = 0;
  215. exit:
  216. dev_kfree_skb(resp);
  217. if (rc)
  218. ddev->curr_protocol = 0;
  219. }
  220. int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
  221. struct nfc_target *target, __u8 comm_mode, __u8 *gb,
  222. size_t gb_len)
  223. {
  224. struct sk_buff *skb;
  225. struct digital_atr_req *atr_req;
  226. uint size;
  227. size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
  228. if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
  229. PROTOCOL_ERR("14.6.1.1");
  230. return -EINVAL;
  231. }
  232. skb = digital_skb_alloc(ddev, size);
  233. if (!skb)
  234. return -ENOMEM;
  235. skb_put(skb, sizeof(struct digital_atr_req));
  236. atr_req = (struct digital_atr_req *)skb->data;
  237. memset(atr_req, 0, sizeof(struct digital_atr_req));
  238. atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  239. atr_req->cmd = DIGITAL_CMD_ATR_REQ;
  240. if (target->nfcid2_len)
  241. memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
  242. else
  243. get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
  244. atr_req->did = 0;
  245. atr_req->bs = 0;
  246. atr_req->br = 0;
  247. atr_req->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
  248. if (gb_len) {
  249. atr_req->pp |= DIGITAL_GB_BIT;
  250. memcpy(skb_put(skb, gb_len), gb, gb_len);
  251. }
  252. digital_skb_push_dep_sod(ddev, skb);
  253. ddev->skb_add_crc(skb);
  254. return digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res,
  255. target);
  256. }
  257. static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
  258. struct digital_data_exch *data_exch, u8 rtox)
  259. {
  260. struct digital_dep_req_res *dep_req;
  261. struct sk_buff *skb;
  262. int rc;
  263. skb = digital_skb_alloc(ddev, 1);
  264. if (!skb)
  265. return -ENOMEM;
  266. *skb_put(skb, 1) = rtox;
  267. skb_push(skb, sizeof(struct digital_dep_req_res));
  268. dep_req = (struct digital_dep_req_res *)skb->data;
  269. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  270. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  271. dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
  272. DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
  273. digital_skb_push_dep_sod(ddev, skb);
  274. ddev->skb_add_crc(skb);
  275. rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
  276. data_exch);
  277. return rc;
  278. }
  279. static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
  280. struct sk_buff *resp)
  281. {
  282. struct digital_data_exch *data_exch = arg;
  283. struct digital_dep_req_res *dep_res;
  284. u8 pfb;
  285. uint size;
  286. int rc;
  287. if (IS_ERR(resp)) {
  288. rc = PTR_ERR(resp);
  289. resp = NULL;
  290. goto exit;
  291. }
  292. rc = ddev->skb_check_crc(resp);
  293. if (rc) {
  294. PROTOCOL_ERR("14.4.1.6");
  295. goto error;
  296. }
  297. rc = digital_skb_pull_dep_sod(ddev, resp);
  298. if (rc) {
  299. PROTOCOL_ERR("14.4.1.2");
  300. goto exit;
  301. }
  302. dep_res = (struct digital_dep_req_res *)resp->data;
  303. if (resp->len < sizeof(struct digital_dep_req_res) ||
  304. dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
  305. dep_res->cmd != DIGITAL_CMD_DEP_RES) {
  306. rc = -EIO;
  307. goto error;
  308. }
  309. pfb = dep_res->pfb;
  310. switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
  311. case DIGITAL_NFC_DEP_PFB_I_PDU:
  312. if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
  313. PROTOCOL_ERR("14.12.3.3");
  314. rc = -EIO;
  315. goto error;
  316. }
  317. ddev->curr_nfc_dep_pni =
  318. DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
  319. rc = 0;
  320. break;
  321. case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
  322. pr_err("Received a ACK/NACK PDU\n");
  323. rc = -EIO;
  324. goto error;
  325. case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
  326. if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
  327. rc = -EINVAL;
  328. goto error;
  329. }
  330. rc = digital_in_send_rtox(ddev, data_exch, resp->data[3]);
  331. if (rc)
  332. goto error;
  333. kfree_skb(resp);
  334. return;
  335. }
  336. if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
  337. pr_err("MI bit set. Chained PDU not supported\n");
  338. rc = -EIO;
  339. goto error;
  340. }
  341. size = sizeof(struct digital_dep_req_res);
  342. if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb))
  343. size++;
  344. if (size > resp->len) {
  345. rc = -EIO;
  346. goto error;
  347. }
  348. skb_pull(resp, size);
  349. exit:
  350. data_exch->cb(data_exch->cb_context, resp, rc);
  351. error:
  352. kfree(data_exch);
  353. if (rc)
  354. kfree_skb(resp);
  355. }
  356. int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
  357. struct nfc_target *target, struct sk_buff *skb,
  358. struct digital_data_exch *data_exch)
  359. {
  360. struct digital_dep_req_res *dep_req;
  361. skb_push(skb, sizeof(struct digital_dep_req_res));
  362. dep_req = (struct digital_dep_req_res *)skb->data;
  363. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  364. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  365. dep_req->pfb = ddev->curr_nfc_dep_pni;
  366. digital_skb_push_dep_sod(ddev, skb);
  367. ddev->skb_add_crc(skb);
  368. return digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
  369. data_exch);
  370. }
  371. static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
  372. {
  373. ddev->curr_rf_tech = rf_tech;
  374. ddev->skb_add_crc = digital_skb_add_crc_none;
  375. ddev->skb_check_crc = digital_skb_check_crc_none;
  376. if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
  377. return;
  378. switch (ddev->curr_rf_tech) {
  379. case NFC_DIGITAL_RF_TECH_106A:
  380. ddev->skb_add_crc = digital_skb_add_crc_a;
  381. ddev->skb_check_crc = digital_skb_check_crc_a;
  382. break;
  383. case NFC_DIGITAL_RF_TECH_212F:
  384. case NFC_DIGITAL_RF_TECH_424F:
  385. ddev->skb_add_crc = digital_skb_add_crc_f;
  386. ddev->skb_check_crc = digital_skb_check_crc_f;
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
  393. struct sk_buff *resp)
  394. {
  395. int rc;
  396. struct digital_dep_req_res *dep_req;
  397. size_t size;
  398. if (IS_ERR(resp)) {
  399. rc = PTR_ERR(resp);
  400. resp = NULL;
  401. goto exit;
  402. }
  403. rc = ddev->skb_check_crc(resp);
  404. if (rc) {
  405. PROTOCOL_ERR("14.4.1.6");
  406. goto exit;
  407. }
  408. rc = digital_skb_pull_dep_sod(ddev, resp);
  409. if (rc) {
  410. PROTOCOL_ERR("14.4.1.2");
  411. goto exit;
  412. }
  413. size = sizeof(struct digital_dep_req_res);
  414. dep_req = (struct digital_dep_req_res *)resp->data;
  415. if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
  416. dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
  417. rc = -EIO;
  418. goto exit;
  419. }
  420. if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req->pfb))
  421. size++;
  422. if (resp->len < size) {
  423. rc = -EIO;
  424. goto exit;
  425. }
  426. switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
  427. case DIGITAL_NFC_DEP_PFB_I_PDU:
  428. pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
  429. ddev->curr_nfc_dep_pni = DIGITAL_NFC_DEP_PFB_PNI(dep_req->pfb);
  430. break;
  431. case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
  432. pr_err("Received a ACK/NACK PDU\n");
  433. rc = -EINVAL;
  434. goto exit;
  435. case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
  436. pr_err("Received a SUPERVISOR PDU\n");
  437. rc = -EINVAL;
  438. goto exit;
  439. }
  440. skb_pull(resp, size);
  441. rc = nfc_tm_data_received(ddev->nfc_dev, resp);
  442. exit:
  443. if (rc)
  444. kfree_skb(resp);
  445. }
  446. int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
  447. {
  448. struct digital_dep_req_res *dep_res;
  449. skb_push(skb, sizeof(struct digital_dep_req_res));
  450. dep_res = (struct digital_dep_req_res *)skb->data;
  451. dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  452. dep_res->cmd = DIGITAL_CMD_DEP_RES;
  453. dep_res->pfb = ddev->curr_nfc_dep_pni;
  454. digital_skb_push_dep_sod(ddev, skb);
  455. ddev->skb_add_crc(skb);
  456. return digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
  457. NULL);
  458. }
  459. static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
  460. void *arg, struct sk_buff *resp)
  461. {
  462. u8 rf_tech = (unsigned long)arg;
  463. if (IS_ERR(resp))
  464. return;
  465. digital_tg_set_rf_tech(ddev, rf_tech);
  466. digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
  467. digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
  468. dev_kfree_skb(resp);
  469. }
  470. static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
  471. u8 rf_tech)
  472. {
  473. struct digital_psl_res *psl_res;
  474. struct sk_buff *skb;
  475. int rc;
  476. skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
  477. if (!skb)
  478. return -ENOMEM;
  479. skb_put(skb, sizeof(struct digital_psl_res));
  480. psl_res = (struct digital_psl_res *)skb->data;
  481. psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  482. psl_res->cmd = DIGITAL_CMD_PSL_RES;
  483. psl_res->did = did;
  484. digital_skb_push_dep_sod(ddev, skb);
  485. ddev->skb_add_crc(skb);
  486. rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
  487. (void *)(unsigned long)rf_tech);
  488. if (rc)
  489. kfree_skb(skb);
  490. return rc;
  491. }
  492. static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
  493. struct sk_buff *resp)
  494. {
  495. int rc;
  496. struct digital_psl_req *psl_req;
  497. u8 rf_tech;
  498. u8 dsi;
  499. if (IS_ERR(resp)) {
  500. rc = PTR_ERR(resp);
  501. resp = NULL;
  502. goto exit;
  503. }
  504. rc = ddev->skb_check_crc(resp);
  505. if (rc) {
  506. PROTOCOL_ERR("14.4.1.6");
  507. goto exit;
  508. }
  509. rc = digital_skb_pull_dep_sod(ddev, resp);
  510. if (rc) {
  511. PROTOCOL_ERR("14.4.1.2");
  512. goto exit;
  513. }
  514. psl_req = (struct digital_psl_req *)resp->data;
  515. if (resp->len != sizeof(struct digital_psl_req) ||
  516. psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
  517. psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
  518. rc = -EIO;
  519. goto exit;
  520. }
  521. dsi = (psl_req->brs >> 3) & 0x07;
  522. switch (dsi) {
  523. case 0:
  524. rf_tech = NFC_DIGITAL_RF_TECH_106A;
  525. break;
  526. case 1:
  527. rf_tech = NFC_DIGITAL_RF_TECH_212F;
  528. break;
  529. case 2:
  530. rf_tech = NFC_DIGITAL_RF_TECH_424F;
  531. break;
  532. default:
  533. pr_err("Unsupported dsi value %d\n", dsi);
  534. goto exit;
  535. }
  536. rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
  537. exit:
  538. kfree_skb(resp);
  539. }
  540. static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
  541. void *arg, struct sk_buff *resp)
  542. {
  543. int offset;
  544. if (IS_ERR(resp)) {
  545. digital_poll_next_tech(ddev);
  546. return;
  547. }
  548. offset = 2;
  549. if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
  550. offset++;
  551. if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
  552. digital_tg_recv_psl_req(ddev, arg, resp);
  553. else
  554. digital_tg_recv_dep_req(ddev, arg, resp);
  555. }
  556. static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
  557. struct digital_atr_req *atr_req)
  558. {
  559. struct digital_atr_res *atr_res;
  560. struct sk_buff *skb;
  561. u8 *gb;
  562. size_t gb_len;
  563. int rc;
  564. gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
  565. if (!gb)
  566. gb_len = 0;
  567. skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
  568. if (!skb)
  569. return -ENOMEM;
  570. skb_put(skb, sizeof(struct digital_atr_res));
  571. atr_res = (struct digital_atr_res *)skb->data;
  572. memset(atr_res, 0, sizeof(struct digital_atr_res));
  573. atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  574. atr_res->cmd = DIGITAL_CMD_ATR_RES;
  575. memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
  576. atr_res->to = 8;
  577. atr_res->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
  578. if (gb_len) {
  579. skb_put(skb, gb_len);
  580. atr_res->pp |= DIGITAL_GB_BIT;
  581. memcpy(atr_res->gb, gb, gb_len);
  582. }
  583. digital_skb_push_dep_sod(ddev, skb);
  584. ddev->skb_add_crc(skb);
  585. rc = digital_tg_send_cmd(ddev, skb, 999,
  586. digital_tg_send_atr_res_complete, NULL);
  587. if (rc) {
  588. kfree_skb(skb);
  589. return rc;
  590. }
  591. return rc;
  592. }
  593. void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
  594. struct sk_buff *resp)
  595. {
  596. int rc;
  597. struct digital_atr_req *atr_req;
  598. size_t gb_len, min_size;
  599. u8 poll_tech_count;
  600. if (IS_ERR(resp)) {
  601. rc = PTR_ERR(resp);
  602. resp = NULL;
  603. goto exit;
  604. }
  605. if (!resp->len) {
  606. rc = -EIO;
  607. goto exit;
  608. }
  609. if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
  610. min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
  611. digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
  612. } else {
  613. min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
  614. digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
  615. }
  616. if (resp->len < min_size) {
  617. rc = -EIO;
  618. goto exit;
  619. }
  620. ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
  621. rc = ddev->skb_check_crc(resp);
  622. if (rc) {
  623. PROTOCOL_ERR("14.4.1.6");
  624. goto exit;
  625. }
  626. rc = digital_skb_pull_dep_sod(ddev, resp);
  627. if (rc) {
  628. PROTOCOL_ERR("14.4.1.2");
  629. goto exit;
  630. }
  631. atr_req = (struct digital_atr_req *)resp->data;
  632. if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
  633. atr_req->cmd != DIGITAL_CMD_ATR_REQ) {
  634. rc = -EINVAL;
  635. goto exit;
  636. }
  637. rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  638. NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
  639. if (rc)
  640. goto exit;
  641. rc = digital_tg_send_atr_res(ddev, atr_req);
  642. if (rc)
  643. goto exit;
  644. gb_len = resp->len - sizeof(struct digital_atr_req);
  645. poll_tech_count = ddev->poll_tech_count;
  646. ddev->poll_tech_count = 0;
  647. rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  648. NFC_COMM_PASSIVE, atr_req->gb, gb_len);
  649. if (rc) {
  650. ddev->poll_tech_count = poll_tech_count;
  651. goto exit;
  652. }
  653. rc = 0;
  654. exit:
  655. if (rc)
  656. digital_poll_next_tech(ddev);
  657. dev_kfree_skb(resp);
  658. }