digital_dep.c 35 KB

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