digital_dep.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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_N_RETRY_NACK 2
  18. #define DIGITAL_NFC_DEP_N_RETRY_ATN 2
  19. #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
  20. #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
  21. #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
  22. #define DIGITAL_CMD_ATR_REQ 0x00
  23. #define DIGITAL_CMD_ATR_RES 0x01
  24. #define DIGITAL_CMD_PSL_REQ 0x04
  25. #define DIGITAL_CMD_PSL_RES 0x05
  26. #define DIGITAL_CMD_DEP_REQ 0x06
  27. #define DIGITAL_CMD_DEP_RES 0x07
  28. #define DIGITAL_ATR_REQ_MIN_SIZE 16
  29. #define DIGITAL_ATR_REQ_MAX_SIZE 64
  30. #define DIGITAL_DID_MAX 14
  31. #define DIGITAL_PAYLOAD_SIZE_MAX 254
  32. #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
  33. #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
  34. #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
  35. #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
  36. #define DIGITAL_GB_BIT 0x02
  37. #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
  38. #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
  39. #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
  40. #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
  41. #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
  42. #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
  43. #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
  44. #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  45. ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
  46. #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
  47. #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
  48. #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
  49. #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
  50. #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
  51. #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
  52. #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
  53. #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
  54. struct digital_atr_req {
  55. u8 dir;
  56. u8 cmd;
  57. u8 nfcid3[10];
  58. u8 did;
  59. u8 bs;
  60. u8 br;
  61. u8 pp;
  62. u8 gb[0];
  63. } __packed;
  64. struct digital_atr_res {
  65. u8 dir;
  66. u8 cmd;
  67. u8 nfcid3[10];
  68. u8 did;
  69. u8 bs;
  70. u8 br;
  71. u8 to;
  72. u8 pp;
  73. u8 gb[0];
  74. } __packed;
  75. struct digital_psl_req {
  76. u8 dir;
  77. u8 cmd;
  78. u8 did;
  79. u8 brs;
  80. u8 fsl;
  81. } __packed;
  82. struct digital_psl_res {
  83. u8 dir;
  84. u8 cmd;
  85. u8 did;
  86. } __packed;
  87. struct digital_dep_req_res {
  88. u8 dir;
  89. u8 cmd;
  90. u8 pfb;
  91. } __packed;
  92. static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
  93. struct sk_buff *resp);
  94. static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
  95. struct sk_buff *resp);
  96. static const u8 digital_payload_bits_map[4] = {
  97. [0] = 64,
  98. [1] = 128,
  99. [2] = 192,
  100. [3] = 254
  101. };
  102. static u8 digital_payload_bits_to_size(u8 payload_bits)
  103. {
  104. if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
  105. return 0;
  106. return digital_payload_bits_map[payload_bits];
  107. }
  108. static u8 digital_payload_size_to_bits(u8 payload_size)
  109. {
  110. int i;
  111. for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
  112. if (digital_payload_bits_map[i] == payload_size)
  113. return i;
  114. return 0xff;
  115. }
  116. static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
  117. struct sk_buff *skb)
  118. {
  119. skb_push(skb, sizeof(u8));
  120. skb->data[0] = skb->len;
  121. if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
  122. *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
  123. }
  124. static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
  125. struct sk_buff *skb)
  126. {
  127. u8 size;
  128. if (skb->len < 2)
  129. return -EIO;
  130. if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
  131. skb_pull(skb, sizeof(u8));
  132. size = skb->data[0];
  133. if (size != skb->len)
  134. return -EIO;
  135. skb_pull(skb, sizeof(u8));
  136. return 0;
  137. }
  138. static struct sk_buff *
  139. digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
  140. struct digital_dep_req_res *dep_req_res,
  141. struct digital_data_exch *data_exch)
  142. {
  143. struct sk_buff *new_skb;
  144. if (skb->len > ddev->remote_payload_max) {
  145. dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
  146. new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
  147. if (!new_skb) {
  148. kfree_skb(ddev->chaining_skb);
  149. ddev->chaining_skb = NULL;
  150. return ERR_PTR(-ENOMEM);
  151. }
  152. skb_reserve(new_skb, ddev->tx_headroom + NFC_HEADER_SIZE +
  153. DIGITAL_NFC_DEP_REQ_RES_HEADROOM);
  154. memcpy(skb_put(new_skb, ddev->remote_payload_max), skb->data,
  155. ddev->remote_payload_max);
  156. skb_pull(skb, ddev->remote_payload_max);
  157. ddev->chaining_skb = skb;
  158. ddev->data_exch = data_exch;
  159. } else {
  160. ddev->chaining_skb = NULL;
  161. new_skb = skb;
  162. }
  163. return new_skb;
  164. }
  165. static struct sk_buff *
  166. digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
  167. struct sk_buff *resp,
  168. int (*send_ack)(struct nfc_digital_dev *ddev,
  169. struct digital_data_exch
  170. *data_exch),
  171. struct digital_data_exch *data_exch)
  172. {
  173. struct sk_buff *new_skb;
  174. int rc;
  175. if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
  176. ddev->chaining_skb =
  177. nfc_alloc_recv_skb(8 * ddev->local_payload_max,
  178. GFP_KERNEL);
  179. if (!ddev->chaining_skb) {
  180. rc = -ENOMEM;
  181. goto error;
  182. }
  183. }
  184. if (ddev->chaining_skb) {
  185. if (resp->len > skb_tailroom(ddev->chaining_skb)) {
  186. new_skb = skb_copy_expand(ddev->chaining_skb,
  187. skb_headroom(
  188. ddev->chaining_skb),
  189. 8 * ddev->local_payload_max,
  190. GFP_KERNEL);
  191. if (!new_skb) {
  192. rc = -ENOMEM;
  193. goto error;
  194. }
  195. kfree_skb(ddev->chaining_skb);
  196. ddev->chaining_skb = new_skb;
  197. }
  198. memcpy(skb_put(ddev->chaining_skb, resp->len), resp->data,
  199. resp->len);
  200. kfree_skb(resp);
  201. resp = NULL;
  202. if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
  203. rc = send_ack(ddev, data_exch);
  204. if (rc)
  205. goto error;
  206. return NULL;
  207. }
  208. resp = ddev->chaining_skb;
  209. ddev->chaining_skb = NULL;
  210. }
  211. return resp;
  212. error:
  213. kfree_skb(resp);
  214. kfree_skb(ddev->chaining_skb);
  215. ddev->chaining_skb = NULL;
  216. return ERR_PTR(rc);
  217. }
  218. static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
  219. struct sk_buff *resp)
  220. {
  221. struct nfc_target *target = arg;
  222. struct digital_psl_res *psl_res;
  223. int rc;
  224. if (IS_ERR(resp)) {
  225. rc = PTR_ERR(resp);
  226. resp = NULL;
  227. goto exit;
  228. }
  229. rc = ddev->skb_check_crc(resp);
  230. if (rc) {
  231. PROTOCOL_ERR("14.4.1.6");
  232. goto exit;
  233. }
  234. rc = digital_skb_pull_dep_sod(ddev, resp);
  235. if (rc) {
  236. PROTOCOL_ERR("14.4.1.2");
  237. goto exit;
  238. }
  239. psl_res = (struct digital_psl_res *)resp->data;
  240. if ((resp->len != sizeof(*psl_res)) ||
  241. (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
  242. (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
  243. rc = -EIO;
  244. goto exit;
  245. }
  246. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
  247. NFC_DIGITAL_RF_TECH_424F);
  248. if (rc)
  249. goto exit;
  250. rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  251. NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
  252. if (rc)
  253. goto exit;
  254. if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
  255. (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
  256. ddev->skb_add_crc = digital_skb_add_crc_f;
  257. ddev->skb_check_crc = digital_skb_check_crc_f;
  258. }
  259. ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
  260. nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
  261. NFC_RF_INITIATOR);
  262. ddev->curr_nfc_dep_pni = 0;
  263. exit:
  264. dev_kfree_skb(resp);
  265. if (rc)
  266. ddev->curr_protocol = 0;
  267. }
  268. static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
  269. struct nfc_target *target)
  270. {
  271. struct sk_buff *skb;
  272. struct digital_psl_req *psl_req;
  273. int rc;
  274. u8 payload_size, payload_bits;
  275. skb = digital_skb_alloc(ddev, sizeof(*psl_req));
  276. if (!skb)
  277. return -ENOMEM;
  278. skb_put(skb, sizeof(*psl_req));
  279. psl_req = (struct digital_psl_req *)skb->data;
  280. psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  281. psl_req->cmd = DIGITAL_CMD_PSL_REQ;
  282. psl_req->did = 0;
  283. psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
  284. payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
  285. payload_bits = digital_payload_size_to_bits(payload_size);
  286. psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
  287. ddev->local_payload_max = payload_size;
  288. ddev->remote_payload_max = payload_size;
  289. digital_skb_push_dep_sod(ddev, skb);
  290. ddev->skb_add_crc(skb);
  291. rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_psl_res,
  292. target);
  293. if (rc)
  294. kfree_skb(skb);
  295. return rc;
  296. }
  297. static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
  298. struct sk_buff *resp)
  299. {
  300. struct nfc_target *target = arg;
  301. struct digital_atr_res *atr_res;
  302. u8 gb_len, payload_bits;
  303. int rc;
  304. if (IS_ERR(resp)) {
  305. rc = PTR_ERR(resp);
  306. resp = NULL;
  307. goto exit;
  308. }
  309. rc = ddev->skb_check_crc(resp);
  310. if (rc) {
  311. PROTOCOL_ERR("14.4.1.6");
  312. goto exit;
  313. }
  314. rc = digital_skb_pull_dep_sod(ddev, resp);
  315. if (rc) {
  316. PROTOCOL_ERR("14.4.1.2");
  317. goto exit;
  318. }
  319. if (resp->len < sizeof(struct digital_atr_res)) {
  320. rc = -EIO;
  321. goto exit;
  322. }
  323. gb_len = resp->len - sizeof(struct digital_atr_res);
  324. atr_res = (struct digital_atr_res *)resp->data;
  325. payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
  326. ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
  327. if (!ddev->remote_payload_max) {
  328. rc = -EINVAL;
  329. goto exit;
  330. }
  331. rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
  332. if (rc)
  333. goto exit;
  334. if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
  335. (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
  336. rc = digital_in_send_psl_req(ddev, target);
  337. if (!rc)
  338. goto exit;
  339. }
  340. rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
  341. NFC_RF_INITIATOR);
  342. ddev->curr_nfc_dep_pni = 0;
  343. exit:
  344. dev_kfree_skb(resp);
  345. if (rc)
  346. ddev->curr_protocol = 0;
  347. }
  348. int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
  349. struct nfc_target *target, __u8 comm_mode, __u8 *gb,
  350. size_t gb_len)
  351. {
  352. struct sk_buff *skb;
  353. struct digital_atr_req *atr_req;
  354. uint size;
  355. int rc;
  356. u8 payload_bits;
  357. size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
  358. if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
  359. PROTOCOL_ERR("14.6.1.1");
  360. return -EINVAL;
  361. }
  362. skb = digital_skb_alloc(ddev, size);
  363. if (!skb)
  364. return -ENOMEM;
  365. skb_put(skb, sizeof(struct digital_atr_req));
  366. atr_req = (struct digital_atr_req *)skb->data;
  367. memset(atr_req, 0, sizeof(struct digital_atr_req));
  368. atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  369. atr_req->cmd = DIGITAL_CMD_ATR_REQ;
  370. if (target->nfcid2_len)
  371. memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
  372. else
  373. get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
  374. atr_req->did = 0;
  375. atr_req->bs = 0;
  376. atr_req->br = 0;
  377. ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
  378. payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
  379. atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
  380. if (gb_len) {
  381. atr_req->pp |= DIGITAL_GB_BIT;
  382. memcpy(skb_put(skb, gb_len), gb, gb_len);
  383. }
  384. digital_skb_push_dep_sod(ddev, skb);
  385. ddev->skb_add_crc(skb);
  386. rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res,
  387. target);
  388. if (rc)
  389. kfree_skb(skb);
  390. return rc;
  391. }
  392. static int digital_in_send_ack(struct nfc_digital_dev *ddev,
  393. struct digital_data_exch *data_exch)
  394. {
  395. struct digital_dep_req_res *dep_req;
  396. struct sk_buff *skb;
  397. int rc;
  398. skb = digital_skb_alloc(ddev, 1);
  399. if (!skb)
  400. return -ENOMEM;
  401. skb_push(skb, sizeof(struct digital_dep_req_res));
  402. dep_req = (struct digital_dep_req_res *)skb->data;
  403. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  404. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  405. dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
  406. ddev->curr_nfc_dep_pni;
  407. digital_skb_push_dep_sod(ddev, skb);
  408. ddev->skb_add_crc(skb);
  409. ddev->saved_skb = skb_get(skb);
  410. ddev->saved_skb_len = skb->len;
  411. rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
  412. data_exch);
  413. if (rc) {
  414. kfree_skb(skb);
  415. kfree_skb(ddev->saved_skb);
  416. ddev->saved_skb = NULL;
  417. }
  418. return rc;
  419. }
  420. static int digital_in_send_nack(struct nfc_digital_dev *ddev,
  421. struct digital_data_exch *data_exch)
  422. {
  423. struct digital_dep_req_res *dep_req;
  424. struct sk_buff *skb;
  425. int rc;
  426. skb = digital_skb_alloc(ddev, 1);
  427. if (!skb)
  428. return -ENOMEM;
  429. skb_push(skb, sizeof(struct digital_dep_req_res));
  430. dep_req = (struct digital_dep_req_res *)skb->data;
  431. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  432. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  433. dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
  434. DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
  435. digital_skb_push_dep_sod(ddev, skb);
  436. ddev->skb_add_crc(skb);
  437. rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
  438. data_exch);
  439. if (rc)
  440. kfree_skb(skb);
  441. return rc;
  442. }
  443. static int digital_in_send_atn(struct nfc_digital_dev *ddev,
  444. struct digital_data_exch *data_exch)
  445. {
  446. struct digital_dep_req_res *dep_req;
  447. struct sk_buff *skb;
  448. int rc;
  449. skb = digital_skb_alloc(ddev, 1);
  450. if (!skb)
  451. return -ENOMEM;
  452. skb_push(skb, sizeof(struct digital_dep_req_res));
  453. dep_req = (struct digital_dep_req_res *)skb->data;
  454. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  455. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  456. dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
  457. digital_skb_push_dep_sod(ddev, skb);
  458. ddev->skb_add_crc(skb);
  459. rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
  460. data_exch);
  461. if (rc)
  462. kfree_skb(skb);
  463. return rc;
  464. }
  465. static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
  466. struct digital_data_exch *data_exch, u8 rtox)
  467. {
  468. struct digital_dep_req_res *dep_req;
  469. struct sk_buff *skb;
  470. int rc;
  471. skb = digital_skb_alloc(ddev, 1);
  472. if (!skb)
  473. return -ENOMEM;
  474. *skb_put(skb, 1) = rtox;
  475. skb_push(skb, sizeof(struct digital_dep_req_res));
  476. dep_req = (struct digital_dep_req_res *)skb->data;
  477. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  478. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  479. dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
  480. DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
  481. digital_skb_push_dep_sod(ddev, skb);
  482. ddev->skb_add_crc(skb);
  483. ddev->saved_skb = skb_get(skb);
  484. ddev->saved_skb_len = skb->len;
  485. rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
  486. data_exch);
  487. if (rc) {
  488. kfree_skb(skb);
  489. kfree_skb(ddev->saved_skb);
  490. ddev->saved_skb = NULL;
  491. }
  492. return rc;
  493. }
  494. static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
  495. struct digital_data_exch *data_exch)
  496. {
  497. skb_get(ddev->saved_skb);
  498. skb_push(ddev->saved_skb, ddev->saved_skb_len);
  499. return digital_in_send_cmd(ddev, ddev->saved_skb, 1500,
  500. digital_in_recv_dep_res, data_exch);
  501. }
  502. static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
  503. struct sk_buff *resp)
  504. {
  505. struct digital_data_exch *data_exch = arg;
  506. struct digital_dep_req_res *dep_res;
  507. u8 pfb;
  508. uint size;
  509. int rc;
  510. if (IS_ERR(resp)) {
  511. rc = PTR_ERR(resp);
  512. resp = NULL;
  513. if (((rc != -ETIMEDOUT) || ddev->nack_count) &&
  514. (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
  515. ddev->atn_count = 0;
  516. rc = digital_in_send_nack(ddev, data_exch);
  517. if (rc)
  518. goto error;
  519. return;
  520. } else if ((rc == -ETIMEDOUT) &&
  521. (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
  522. ddev->nack_count = 0;
  523. rc = digital_in_send_atn(ddev, data_exch);
  524. if (rc)
  525. goto error;
  526. return;
  527. }
  528. goto exit;
  529. }
  530. rc = digital_skb_pull_dep_sod(ddev, resp);
  531. if (rc) {
  532. PROTOCOL_ERR("14.4.1.2");
  533. goto exit;
  534. }
  535. rc = ddev->skb_check_crc(resp);
  536. if (rc) {
  537. if ((resp->len >= 4) &&
  538. (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
  539. ddev->atn_count = 0;
  540. rc = digital_in_send_nack(ddev, data_exch);
  541. if (rc)
  542. goto error;
  543. kfree_skb(resp);
  544. return;
  545. }
  546. PROTOCOL_ERR("14.4.1.6");
  547. goto error;
  548. }
  549. ddev->atn_count = 0;
  550. ddev->nack_count = 0;
  551. if (resp->len > ddev->local_payload_max) {
  552. rc = -EMSGSIZE;
  553. goto exit;
  554. }
  555. size = sizeof(struct digital_dep_req_res);
  556. dep_res = (struct digital_dep_req_res *)resp->data;
  557. if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
  558. dep_res->cmd != DIGITAL_CMD_DEP_RES) {
  559. rc = -EIO;
  560. goto error;
  561. }
  562. pfb = dep_res->pfb;
  563. if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
  564. PROTOCOL_ERR("14.8.2.1");
  565. rc = -EIO;
  566. goto error;
  567. }
  568. if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
  569. rc = -EIO;
  570. goto exit;
  571. }
  572. if (size > resp->len) {
  573. rc = -EIO;
  574. goto error;
  575. }
  576. skb_pull(resp, size);
  577. switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
  578. case DIGITAL_NFC_DEP_PFB_I_PDU:
  579. if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
  580. PROTOCOL_ERR("14.12.3.3");
  581. rc = -EIO;
  582. goto error;
  583. }
  584. ddev->curr_nfc_dep_pni =
  585. DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
  586. kfree_skb(ddev->saved_skb);
  587. ddev->saved_skb = NULL;
  588. resp = digital_recv_dep_data_gather(ddev, pfb, resp,
  589. digital_in_send_ack,
  590. data_exch);
  591. if (IS_ERR(resp)) {
  592. rc = PTR_ERR(resp);
  593. resp = NULL;
  594. goto error;
  595. }
  596. /* If resp is NULL then we're still chaining so return and
  597. * wait for the next part of the PDU. Else, the PDU is
  598. * complete so pass it up.
  599. */
  600. if (!resp)
  601. return;
  602. rc = 0;
  603. break;
  604. case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
  605. if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
  606. PROTOCOL_ERR("14.12.3.3");
  607. rc = -EIO;
  608. goto exit;
  609. }
  610. ddev->curr_nfc_dep_pni =
  611. DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
  612. if (ddev->chaining_skb && !DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
  613. kfree_skb(ddev->saved_skb);
  614. ddev->saved_skb = NULL;
  615. rc = digital_in_send_dep_req(ddev, NULL,
  616. ddev->chaining_skb,
  617. ddev->data_exch);
  618. if (rc)
  619. goto error;
  620. return;
  621. }
  622. pr_err("Received a ACK/NACK PDU\n");
  623. rc = -EINVAL;
  624. goto exit;
  625. case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
  626. if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
  627. rc = digital_in_send_saved_skb(ddev, data_exch);
  628. if (rc) {
  629. kfree_skb(ddev->saved_skb);
  630. goto error;
  631. }
  632. return;
  633. }
  634. kfree_skb(ddev->saved_skb);
  635. ddev->saved_skb = NULL;
  636. rc = digital_in_send_rtox(ddev, data_exch, resp->data[0]);
  637. if (rc)
  638. goto error;
  639. kfree_skb(resp);
  640. return;
  641. }
  642. exit:
  643. data_exch->cb(data_exch->cb_context, resp, rc);
  644. error:
  645. kfree(data_exch);
  646. kfree_skb(ddev->chaining_skb);
  647. ddev->chaining_skb = NULL;
  648. kfree_skb(ddev->saved_skb);
  649. ddev->saved_skb = NULL;
  650. if (rc)
  651. kfree_skb(resp);
  652. }
  653. int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
  654. struct nfc_target *target, struct sk_buff *skb,
  655. struct digital_data_exch *data_exch)
  656. {
  657. struct digital_dep_req_res *dep_req;
  658. struct sk_buff *chaining_skb, *tmp_skb;
  659. int rc;
  660. skb_push(skb, sizeof(struct digital_dep_req_res));
  661. dep_req = (struct digital_dep_req_res *)skb->data;
  662. dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
  663. dep_req->cmd = DIGITAL_CMD_DEP_REQ;
  664. dep_req->pfb = ddev->curr_nfc_dep_pni;
  665. ddev->atn_count = 0;
  666. ddev->nack_count = 0;
  667. chaining_skb = ddev->chaining_skb;
  668. tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
  669. if (IS_ERR(tmp_skb))
  670. return PTR_ERR(tmp_skb);
  671. digital_skb_push_dep_sod(ddev, tmp_skb);
  672. ddev->skb_add_crc(tmp_skb);
  673. ddev->saved_skb = skb_get(tmp_skb);
  674. ddev->saved_skb_len = tmp_skb->len;
  675. rc = digital_in_send_cmd(ddev, tmp_skb, 1500, digital_in_recv_dep_res,
  676. data_exch);
  677. if (rc) {
  678. if (tmp_skb != skb)
  679. kfree_skb(tmp_skb);
  680. kfree_skb(chaining_skb);
  681. ddev->chaining_skb = NULL;
  682. kfree_skb(ddev->saved_skb);
  683. ddev->saved_skb = NULL;
  684. }
  685. return rc;
  686. }
  687. static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
  688. {
  689. ddev->curr_rf_tech = rf_tech;
  690. ddev->skb_add_crc = digital_skb_add_crc_none;
  691. ddev->skb_check_crc = digital_skb_check_crc_none;
  692. if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
  693. return;
  694. switch (ddev->curr_rf_tech) {
  695. case NFC_DIGITAL_RF_TECH_106A:
  696. ddev->skb_add_crc = digital_skb_add_crc_a;
  697. ddev->skb_check_crc = digital_skb_check_crc_a;
  698. break;
  699. case NFC_DIGITAL_RF_TECH_212F:
  700. case NFC_DIGITAL_RF_TECH_424F:
  701. ddev->skb_add_crc = digital_skb_add_crc_f;
  702. ddev->skb_check_crc = digital_skb_check_crc_f;
  703. break;
  704. default:
  705. break;
  706. }
  707. }
  708. static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
  709. struct digital_data_exch *data_exch)
  710. {
  711. struct digital_dep_req_res *dep_res;
  712. struct sk_buff *skb;
  713. int rc;
  714. skb = digital_skb_alloc(ddev, 1);
  715. if (!skb)
  716. return -ENOMEM;
  717. skb_push(skb, sizeof(struct digital_dep_req_res));
  718. dep_res = (struct digital_dep_req_res *)skb->data;
  719. dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  720. dep_res->cmd = DIGITAL_CMD_DEP_RES;
  721. dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
  722. ddev->curr_nfc_dep_pni;
  723. if (ddev->did) {
  724. dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
  725. memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
  726. sizeof(ddev->did));
  727. }
  728. ddev->curr_nfc_dep_pni =
  729. DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
  730. digital_skb_push_dep_sod(ddev, skb);
  731. ddev->skb_add_crc(skb);
  732. ddev->saved_skb = skb_get(skb);
  733. ddev->saved_skb_len = skb->len;
  734. rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
  735. data_exch);
  736. if (rc) {
  737. kfree_skb(skb);
  738. kfree_skb(ddev->saved_skb);
  739. ddev->saved_skb = NULL;
  740. }
  741. return rc;
  742. }
  743. static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
  744. {
  745. struct digital_dep_req_res *dep_res;
  746. struct sk_buff *skb;
  747. int rc;
  748. skb = digital_skb_alloc(ddev, 1);
  749. if (!skb)
  750. return -ENOMEM;
  751. skb_push(skb, sizeof(struct digital_dep_req_res));
  752. dep_res = (struct digital_dep_req_res *)skb->data;
  753. dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  754. dep_res->cmd = DIGITAL_CMD_DEP_RES;
  755. dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
  756. if (ddev->did) {
  757. dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
  758. memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
  759. sizeof(ddev->did));
  760. }
  761. digital_skb_push_dep_sod(ddev, skb);
  762. ddev->skb_add_crc(skb);
  763. rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
  764. NULL);
  765. if (rc)
  766. kfree_skb(skb);
  767. return rc;
  768. }
  769. static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
  770. {
  771. skb_get(ddev->saved_skb);
  772. skb_push(ddev->saved_skb, ddev->saved_skb_len);
  773. return digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
  774. digital_tg_recv_dep_req, NULL);
  775. }
  776. static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
  777. struct sk_buff *resp)
  778. {
  779. int rc;
  780. struct digital_dep_req_res *dep_req;
  781. u8 pfb;
  782. size_t size;
  783. if (IS_ERR(resp)) {
  784. rc = PTR_ERR(resp);
  785. resp = NULL;
  786. goto exit;
  787. }
  788. rc = ddev->skb_check_crc(resp);
  789. if (rc) {
  790. PROTOCOL_ERR("14.4.1.6");
  791. goto exit;
  792. }
  793. rc = digital_skb_pull_dep_sod(ddev, resp);
  794. if (rc) {
  795. PROTOCOL_ERR("14.4.1.2");
  796. goto exit;
  797. }
  798. if (resp->len > ddev->local_payload_max) {
  799. rc = -EMSGSIZE;
  800. goto exit;
  801. }
  802. size = sizeof(struct digital_dep_req_res);
  803. dep_req = (struct digital_dep_req_res *)resp->data;
  804. if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
  805. dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
  806. rc = -EIO;
  807. goto exit;
  808. }
  809. pfb = dep_req->pfb;
  810. if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
  811. if (ddev->did && (ddev->did == resp->data[3])) {
  812. size++;
  813. } else {
  814. rc = -EIO;
  815. goto exit;
  816. }
  817. } else if (ddev->did) {
  818. rc = -EIO;
  819. goto exit;
  820. }
  821. if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
  822. rc = -EIO;
  823. goto exit;
  824. }
  825. if (size > resp->len) {
  826. rc = -EIO;
  827. goto exit;
  828. }
  829. skb_pull(resp, size);
  830. switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
  831. case DIGITAL_NFC_DEP_PFB_I_PDU:
  832. pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
  833. if ((ddev->atn_count && (DIGITAL_NFC_DEP_PFB_PNI(pfb - 1) !=
  834. ddev->curr_nfc_dep_pni)) ||
  835. (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni)) {
  836. PROTOCOL_ERR("14.12.3.4");
  837. rc = -EIO;
  838. goto exit;
  839. }
  840. if (ddev->atn_count) {
  841. ddev->atn_count = 0;
  842. rc = digital_tg_send_saved_skb(ddev);
  843. if (rc)
  844. goto exit;
  845. return;
  846. }
  847. kfree_skb(ddev->saved_skb);
  848. ddev->saved_skb = NULL;
  849. resp = digital_recv_dep_data_gather(ddev, pfb, resp,
  850. digital_tg_send_ack, NULL);
  851. if (IS_ERR(resp)) {
  852. rc = PTR_ERR(resp);
  853. resp = NULL;
  854. goto exit;
  855. }
  856. /* If resp is NULL then we're still chaining so return and
  857. * wait for the next part of the PDU. Else, the PDU is
  858. * complete so pass it up.
  859. */
  860. if (!resp)
  861. return;
  862. rc = 0;
  863. break;
  864. case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
  865. if (!DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* ACK */
  866. if ((ddev->atn_count &&
  867. (DIGITAL_NFC_DEP_PFB_PNI(pfb - 1) !=
  868. ddev->curr_nfc_dep_pni)) ||
  869. (DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
  870. ddev->curr_nfc_dep_pni) ||
  871. !ddev->chaining_skb || !ddev->saved_skb) {
  872. rc = -EIO;
  873. goto exit;
  874. }
  875. if (ddev->atn_count) {
  876. ddev->atn_count = 0;
  877. rc = digital_tg_send_saved_skb(ddev);
  878. if (rc)
  879. goto exit;
  880. return;
  881. }
  882. kfree_skb(ddev->saved_skb);
  883. ddev->saved_skb = NULL;
  884. rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
  885. if (rc)
  886. goto exit;
  887. } else { /* NACK */
  888. if ((DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
  889. ddev->curr_nfc_dep_pni) ||
  890. !ddev->saved_skb) {
  891. rc = -EIO;
  892. goto exit;
  893. }
  894. ddev->atn_count = 0;
  895. rc = digital_tg_send_saved_skb(ddev);
  896. if (rc) {
  897. kfree_skb(ddev->saved_skb);
  898. goto exit;
  899. }
  900. }
  901. return;
  902. case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
  903. if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
  904. rc = -EINVAL;
  905. goto exit;
  906. }
  907. rc = digital_tg_send_atn(ddev);
  908. if (rc)
  909. goto exit;
  910. ddev->atn_count++;
  911. kfree_skb(resp);
  912. return;
  913. }
  914. rc = nfc_tm_data_received(ddev->nfc_dev, resp);
  915. exit:
  916. kfree_skb(ddev->chaining_skb);
  917. ddev->chaining_skb = NULL;
  918. ddev->atn_count = 0;
  919. kfree_skb(ddev->saved_skb);
  920. ddev->saved_skb = NULL;
  921. if (rc)
  922. kfree_skb(resp);
  923. }
  924. int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
  925. {
  926. struct digital_dep_req_res *dep_res;
  927. struct sk_buff *chaining_skb, *tmp_skb;
  928. int rc;
  929. skb_push(skb, sizeof(struct digital_dep_req_res));
  930. dep_res = (struct digital_dep_req_res *)skb->data;
  931. dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  932. dep_res->cmd = DIGITAL_CMD_DEP_RES;
  933. dep_res->pfb = ddev->curr_nfc_dep_pni;
  934. if (ddev->did) {
  935. dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
  936. memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
  937. sizeof(ddev->did));
  938. }
  939. ddev->curr_nfc_dep_pni =
  940. DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
  941. chaining_skb = ddev->chaining_skb;
  942. tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
  943. if (IS_ERR(tmp_skb))
  944. return PTR_ERR(tmp_skb);
  945. digital_skb_push_dep_sod(ddev, tmp_skb);
  946. ddev->skb_add_crc(tmp_skb);
  947. ddev->saved_skb = skb_get(tmp_skb);
  948. ddev->saved_skb_len = tmp_skb->len;
  949. rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
  950. NULL);
  951. if (rc) {
  952. if (tmp_skb != skb)
  953. kfree_skb(tmp_skb);
  954. kfree_skb(chaining_skb);
  955. ddev->chaining_skb = NULL;
  956. kfree_skb(ddev->saved_skb);
  957. ddev->saved_skb = NULL;
  958. }
  959. return rc;
  960. }
  961. static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
  962. void *arg, struct sk_buff *resp)
  963. {
  964. u8 rf_tech = (unsigned long)arg;
  965. if (IS_ERR(resp))
  966. return;
  967. digital_tg_set_rf_tech(ddev, rf_tech);
  968. digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
  969. digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
  970. dev_kfree_skb(resp);
  971. }
  972. static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
  973. u8 rf_tech)
  974. {
  975. struct digital_psl_res *psl_res;
  976. struct sk_buff *skb;
  977. int rc;
  978. skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
  979. if (!skb)
  980. return -ENOMEM;
  981. skb_put(skb, sizeof(struct digital_psl_res));
  982. psl_res = (struct digital_psl_res *)skb->data;
  983. psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  984. psl_res->cmd = DIGITAL_CMD_PSL_RES;
  985. psl_res->did = did;
  986. digital_skb_push_dep_sod(ddev, skb);
  987. ddev->skb_add_crc(skb);
  988. ddev->curr_nfc_dep_pni = 0;
  989. rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
  990. (void *)(unsigned long)rf_tech);
  991. if (rc)
  992. kfree_skb(skb);
  993. return rc;
  994. }
  995. static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
  996. struct sk_buff *resp)
  997. {
  998. int rc;
  999. struct digital_psl_req *psl_req;
  1000. u8 rf_tech;
  1001. u8 dsi, payload_size, payload_bits;
  1002. if (IS_ERR(resp)) {
  1003. rc = PTR_ERR(resp);
  1004. resp = NULL;
  1005. goto exit;
  1006. }
  1007. rc = ddev->skb_check_crc(resp);
  1008. if (rc) {
  1009. PROTOCOL_ERR("14.4.1.6");
  1010. goto exit;
  1011. }
  1012. rc = digital_skb_pull_dep_sod(ddev, resp);
  1013. if (rc) {
  1014. PROTOCOL_ERR("14.4.1.2");
  1015. goto exit;
  1016. }
  1017. psl_req = (struct digital_psl_req *)resp->data;
  1018. if (resp->len != sizeof(struct digital_psl_req) ||
  1019. psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
  1020. psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
  1021. rc = -EIO;
  1022. goto exit;
  1023. }
  1024. dsi = (psl_req->brs >> 3) & 0x07;
  1025. switch (dsi) {
  1026. case 0:
  1027. rf_tech = NFC_DIGITAL_RF_TECH_106A;
  1028. break;
  1029. case 1:
  1030. rf_tech = NFC_DIGITAL_RF_TECH_212F;
  1031. break;
  1032. case 2:
  1033. rf_tech = NFC_DIGITAL_RF_TECH_424F;
  1034. break;
  1035. default:
  1036. pr_err("Unsupported dsi value %d\n", dsi);
  1037. goto exit;
  1038. }
  1039. payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
  1040. payload_size = digital_payload_bits_to_size(payload_bits);
  1041. if (!payload_size || (payload_size > min(ddev->local_payload_max,
  1042. ddev->remote_payload_max))) {
  1043. rc = -EINVAL;
  1044. goto exit;
  1045. }
  1046. ddev->local_payload_max = payload_size;
  1047. ddev->remote_payload_max = payload_size;
  1048. rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
  1049. exit:
  1050. kfree_skb(resp);
  1051. }
  1052. static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
  1053. void *arg, struct sk_buff *resp)
  1054. {
  1055. int offset;
  1056. if (IS_ERR(resp)) {
  1057. digital_poll_next_tech(ddev);
  1058. return;
  1059. }
  1060. offset = 2;
  1061. if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
  1062. offset++;
  1063. ddev->atn_count = 0;
  1064. if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
  1065. digital_tg_recv_psl_req(ddev, arg, resp);
  1066. else
  1067. digital_tg_recv_dep_req(ddev, arg, resp);
  1068. }
  1069. static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
  1070. struct digital_atr_req *atr_req)
  1071. {
  1072. struct digital_atr_res *atr_res;
  1073. struct sk_buff *skb;
  1074. u8 *gb, payload_bits;
  1075. size_t gb_len;
  1076. int rc;
  1077. gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
  1078. if (!gb)
  1079. gb_len = 0;
  1080. skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
  1081. if (!skb)
  1082. return -ENOMEM;
  1083. skb_put(skb, sizeof(struct digital_atr_res));
  1084. atr_res = (struct digital_atr_res *)skb->data;
  1085. memset(atr_res, 0, sizeof(struct digital_atr_res));
  1086. atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
  1087. atr_res->cmd = DIGITAL_CMD_ATR_RES;
  1088. memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
  1089. atr_res->to = 8;
  1090. ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
  1091. payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
  1092. atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
  1093. if (gb_len) {
  1094. skb_put(skb, gb_len);
  1095. atr_res->pp |= DIGITAL_GB_BIT;
  1096. memcpy(atr_res->gb, gb, gb_len);
  1097. }
  1098. digital_skb_push_dep_sod(ddev, skb);
  1099. ddev->skb_add_crc(skb);
  1100. ddev->curr_nfc_dep_pni = 0;
  1101. rc = digital_tg_send_cmd(ddev, skb, 999,
  1102. digital_tg_send_atr_res_complete, NULL);
  1103. if (rc)
  1104. kfree_skb(skb);
  1105. return rc;
  1106. }
  1107. void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
  1108. struct sk_buff *resp)
  1109. {
  1110. int rc;
  1111. struct digital_atr_req *atr_req;
  1112. size_t gb_len, min_size;
  1113. u8 poll_tech_count, payload_bits;
  1114. if (IS_ERR(resp)) {
  1115. rc = PTR_ERR(resp);
  1116. resp = NULL;
  1117. goto exit;
  1118. }
  1119. if (!resp->len) {
  1120. rc = -EIO;
  1121. goto exit;
  1122. }
  1123. if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
  1124. min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
  1125. digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
  1126. } else {
  1127. min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
  1128. digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
  1129. }
  1130. if (resp->len < min_size) {
  1131. rc = -EIO;
  1132. goto exit;
  1133. }
  1134. ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
  1135. rc = ddev->skb_check_crc(resp);
  1136. if (rc) {
  1137. PROTOCOL_ERR("14.4.1.6");
  1138. goto exit;
  1139. }
  1140. rc = digital_skb_pull_dep_sod(ddev, resp);
  1141. if (rc) {
  1142. PROTOCOL_ERR("14.4.1.2");
  1143. goto exit;
  1144. }
  1145. atr_req = (struct digital_atr_req *)resp->data;
  1146. if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
  1147. atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
  1148. atr_req->did > DIGITAL_DID_MAX) {
  1149. rc = -EINVAL;
  1150. goto exit;
  1151. }
  1152. payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
  1153. ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
  1154. if (!ddev->remote_payload_max) {
  1155. rc = -EINVAL;
  1156. goto exit;
  1157. }
  1158. ddev->did = atr_req->did;
  1159. rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  1160. NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
  1161. if (rc)
  1162. goto exit;
  1163. rc = digital_tg_send_atr_res(ddev, atr_req);
  1164. if (rc)
  1165. goto exit;
  1166. gb_len = resp->len - sizeof(struct digital_atr_req);
  1167. poll_tech_count = ddev->poll_tech_count;
  1168. ddev->poll_tech_count = 0;
  1169. rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  1170. NFC_COMM_PASSIVE, atr_req->gb, gb_len);
  1171. if (rc) {
  1172. ddev->poll_tech_count = poll_tech_count;
  1173. goto exit;
  1174. }
  1175. rc = 0;
  1176. exit:
  1177. if (rc)
  1178. digital_poll_next_tech(ddev);
  1179. dev_kfree_skb(resp);
  1180. }