digital_technology.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include "digital.h"
  16. #define DIGITAL_CMD_SENS_REQ 0x26
  17. #define DIGITAL_CMD_ALL_REQ 0x52
  18. #define DIGITAL_CMD_SEL_REQ_CL1 0x93
  19. #define DIGITAL_CMD_SEL_REQ_CL2 0x95
  20. #define DIGITAL_CMD_SEL_REQ_CL3 0x97
  21. #define DIGITAL_SDD_REQ_SEL_PAR 0x20
  22. #define DIGITAL_SDD_RES_CT 0x88
  23. #define DIGITAL_SDD_RES_LEN 5
  24. #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
  25. #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
  26. #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x000C) == 0x000C)
  27. #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
  28. ((!((sens_res) & 0x1F00) && (((sens_res) & 0x000C) == 0x000C)) || \
  29. (((sens_res) & 0x1F00) && ((sens_res) & 0x000C) != 0x000C))
  30. #define DIGITAL_MIFARE_READ_RES_LEN 16
  31. #define DIGITAL_MIFARE_ACK_RES 0x0A
  32. #define DIGITAL_CMD_SENSF_REQ 0x00
  33. #define DIGITAL_CMD_SENSF_RES 0x01
  34. #define DIGITAL_SENSF_RES_MIN_LENGTH 17
  35. #define DIGITAL_SENSF_RES_RD_AP_B1 0x00
  36. #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F
  37. #define DIGITAL_SENSF_REQ_RC_NONE 0
  38. #define DIGITAL_SENSF_REQ_RC_SC 1
  39. #define DIGITAL_SENSF_REQ_RC_AP 2
  40. struct digital_sdd_res {
  41. u8 nfcid1[4];
  42. u8 bcc;
  43. } __packed;
  44. struct digital_sel_req {
  45. u8 sel_cmd;
  46. u8 b2;
  47. u8 nfcid1[4];
  48. u8 bcc;
  49. } __packed;
  50. struct digital_sensf_req {
  51. u8 cmd;
  52. u8 sc1;
  53. u8 sc2;
  54. u8 rc;
  55. u8 tsn;
  56. } __packed;
  57. struct digital_sensf_res {
  58. u8 cmd;
  59. u8 nfcid2[8];
  60. u8 pad0[2];
  61. u8 pad1[3];
  62. u8 mrti_check;
  63. u8 mrti_update;
  64. u8 pad2;
  65. u8 rd[2];
  66. } __packed;
  67. static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
  68. struct nfc_target *target);
  69. static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
  70. struct sk_buff *resp)
  71. {
  72. struct nfc_target *target = arg;
  73. int rc;
  74. u8 sel_res;
  75. u8 nfc_proto;
  76. if (IS_ERR(resp)) {
  77. rc = PTR_ERR(resp);
  78. resp = NULL;
  79. goto exit;
  80. }
  81. if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
  82. rc = digital_skb_check_crc_a(resp);
  83. if (rc) {
  84. PROTOCOL_ERR("4.4.1.3");
  85. goto exit;
  86. }
  87. }
  88. if (!resp->len) {
  89. rc = -EIO;
  90. goto exit;
  91. }
  92. sel_res = resp->data[0];
  93. if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
  94. rc = digital_in_send_sdd_req(ddev, target);
  95. if (rc)
  96. goto exit;
  97. goto exit_free_skb;
  98. }
  99. if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
  100. nfc_proto = NFC_PROTO_MIFARE;
  101. } else {
  102. rc = -EOPNOTSUPP;
  103. goto exit;
  104. }
  105. target->sel_res = sel_res;
  106. rc = digital_target_found(ddev, target, nfc_proto);
  107. exit:
  108. kfree(target);
  109. exit_free_skb:
  110. dev_kfree_skb(resp);
  111. if (rc)
  112. digital_poll_next_tech(ddev);
  113. }
  114. static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
  115. struct nfc_target *target,
  116. struct digital_sdd_res *sdd_res)
  117. {
  118. struct sk_buff *skb;
  119. struct digital_sel_req *sel_req;
  120. u8 sel_cmd;
  121. int rc;
  122. skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
  123. if (!skb)
  124. return -ENOMEM;
  125. skb_put(skb, sizeof(struct digital_sel_req));
  126. sel_req = (struct digital_sel_req *)skb->data;
  127. if (target->nfcid1_len <= 4)
  128. sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
  129. else if (target->nfcid1_len < 10)
  130. sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
  131. else
  132. sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
  133. sel_req->sel_cmd = sel_cmd;
  134. sel_req->b2 = 0x70;
  135. memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
  136. sel_req->bcc = sdd_res->bcc;
  137. if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
  138. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  139. NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
  140. if (rc)
  141. goto exit;
  142. } else {
  143. digital_skb_add_crc_a(skb);
  144. }
  145. rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
  146. target);
  147. exit:
  148. if (rc)
  149. kfree_skb(skb);
  150. return rc;
  151. }
  152. static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
  153. struct sk_buff *resp)
  154. {
  155. struct nfc_target *target = arg;
  156. struct digital_sdd_res *sdd_res;
  157. int rc;
  158. u8 offset, size;
  159. u8 i, bcc;
  160. if (IS_ERR(resp)) {
  161. rc = PTR_ERR(resp);
  162. resp = NULL;
  163. goto exit;
  164. }
  165. if (resp->len < DIGITAL_SDD_RES_LEN) {
  166. PROTOCOL_ERR("4.7.2.8");
  167. rc = -EINVAL;
  168. goto exit;
  169. }
  170. sdd_res = (struct digital_sdd_res *)resp->data;
  171. for (i = 0, bcc = 0; i < 4; i++)
  172. bcc ^= sdd_res->nfcid1[i];
  173. if (bcc != sdd_res->bcc) {
  174. PROTOCOL_ERR("4.7.2.6");
  175. rc = -EINVAL;
  176. goto exit;
  177. }
  178. if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
  179. offset = 1;
  180. size = 3;
  181. } else {
  182. offset = 0;
  183. size = 4;
  184. }
  185. memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
  186. size);
  187. target->nfcid1_len += size;
  188. rc = digital_in_send_sel_req(ddev, target, sdd_res);
  189. exit:
  190. dev_kfree_skb(resp);
  191. if (rc) {
  192. kfree(target);
  193. digital_poll_next_tech(ddev);
  194. }
  195. }
  196. static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
  197. struct nfc_target *target)
  198. {
  199. int rc;
  200. struct sk_buff *skb;
  201. u8 sel_cmd;
  202. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  203. NFC_DIGITAL_FRAMING_NFCA_STANDARD);
  204. if (rc)
  205. return rc;
  206. skb = digital_skb_alloc(ddev, 2);
  207. if (!skb) {
  208. PR_ERR("alloc_skb failed");
  209. return -ENOMEM;
  210. }
  211. if (target->nfcid1_len == 0)
  212. sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
  213. else if (target->nfcid1_len == 3)
  214. sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
  215. else
  216. sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
  217. *skb_put(skb, sizeof(u8)) = sel_cmd;
  218. *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR;
  219. return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
  220. target);
  221. }
  222. static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
  223. struct sk_buff *resp)
  224. {
  225. struct nfc_target *target = NULL;
  226. u16 sens_res;
  227. int rc;
  228. if (IS_ERR(resp)) {
  229. rc = PTR_ERR(resp);
  230. resp = NULL;
  231. goto exit;
  232. }
  233. if (resp->len < sizeof(u16)) {
  234. rc = -EIO;
  235. goto exit;
  236. }
  237. target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
  238. if (!target) {
  239. rc = -ENOMEM;
  240. goto exit;
  241. }
  242. memcpy(&target->sens_res, resp->data, sizeof(u16));
  243. sens_res = be16_to_cpu(target->sens_res);
  244. if (!DIGITAL_SENS_RES_IS_VALID(sens_res)) {
  245. PROTOCOL_ERR("4.6.3.3");
  246. rc = -EINVAL;
  247. goto exit;
  248. }
  249. if (DIGITAL_SENS_RES_IS_T1T(sens_res))
  250. rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
  251. else
  252. rc = digital_in_send_sdd_req(ddev, target);
  253. exit:
  254. dev_kfree_skb(resp);
  255. if (rc) {
  256. kfree(target);
  257. digital_poll_next_tech(ddev);
  258. }
  259. }
  260. int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
  261. {
  262. struct sk_buff *skb;
  263. int rc;
  264. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
  265. NFC_DIGITAL_RF_TECH_106A);
  266. if (rc)
  267. return rc;
  268. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  269. NFC_DIGITAL_FRAMING_NFCA_SHORT);
  270. if (rc)
  271. return rc;
  272. skb = digital_skb_alloc(ddev, 1);
  273. if (!skb)
  274. return -ENOMEM;
  275. *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
  276. rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
  277. if (rc)
  278. kfree_skb(skb);
  279. return rc;
  280. }
  281. int digital_in_recv_mifare_res(struct sk_buff *resp)
  282. {
  283. /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
  284. * Since the driver can't differentiate a ACK/NACK response from a valid
  285. * READ response, the CRC calculation must be handled at digital level
  286. * even if the driver supports it for this technology.
  287. */
  288. if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
  289. if (digital_skb_check_crc_a(resp)) {
  290. PROTOCOL_ERR("9.4.1.2");
  291. return -EIO;
  292. }
  293. return 0;
  294. }
  295. /* ACK response (i.e. successful WRITE). */
  296. if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
  297. resp->data[0] = 0;
  298. return 0;
  299. }
  300. /* NACK and any other responses are treated as error. */
  301. return -EIO;
  302. }
  303. static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
  304. struct sk_buff *resp)
  305. {
  306. int rc;
  307. struct nfc_target target;
  308. struct digital_sensf_res *sensf_res;
  309. if (IS_ERR(resp)) {
  310. rc = PTR_ERR(resp);
  311. resp = NULL;
  312. goto exit;
  313. }
  314. if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
  315. rc = -EIO;
  316. goto exit;
  317. }
  318. if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
  319. rc = digital_skb_check_crc_f(resp);
  320. if (rc) {
  321. PROTOCOL_ERR("6.4.1.8");
  322. goto exit;
  323. }
  324. }
  325. skb_pull(resp, 1);
  326. memset(&target, 0, sizeof(struct nfc_target));
  327. sensf_res = (struct digital_sensf_res *)resp->data;
  328. memcpy(target.sensf_res, sensf_res, resp->len);
  329. target.sensf_res_len = resp->len;
  330. memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
  331. target.nfcid2_len = NFC_NFCID2_MAXSIZE;
  332. rc = digital_target_found(ddev, &target, NFC_PROTO_FELICA);
  333. exit:
  334. dev_kfree_skb(resp);
  335. if (rc)
  336. digital_poll_next_tech(ddev);
  337. }
  338. int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
  339. {
  340. struct digital_sensf_req *sensf_req;
  341. struct sk_buff *skb;
  342. int rc;
  343. u8 size;
  344. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
  345. if (rc)
  346. return rc;
  347. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  348. NFC_DIGITAL_FRAMING_NFCF);
  349. if (rc)
  350. return rc;
  351. size = sizeof(struct digital_sensf_req);
  352. skb = digital_skb_alloc(ddev, size);
  353. if (!skb)
  354. return -ENOMEM;
  355. skb_put(skb, size);
  356. sensf_req = (struct digital_sensf_req *)skb->data;
  357. sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
  358. sensf_req->sc1 = 0xFF;
  359. sensf_req->sc2 = 0xFF;
  360. sensf_req->rc = 0;
  361. sensf_req->tsn = 0;
  362. *skb_push(skb, 1) = size + 1;
  363. if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
  364. digital_skb_add_crc_f(skb);
  365. rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
  366. NULL);
  367. if (rc)
  368. kfree_skb(skb);
  369. return rc;
  370. }