digital_dep.c 35 KB

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