dep.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <net/nfc/hci.h>
  17. #include "st21nfca.h"
  18. #define ST21NFCA_NFCIP1_INITIATOR 0x00
  19. #define ST21NFCA_NFCIP1_REQ 0xd4
  20. #define ST21NFCA_NFCIP1_RES 0xd5
  21. #define ST21NFCA_NFCIP1_ATR_REQ 0x00
  22. #define ST21NFCA_NFCIP1_ATR_RES 0x01
  23. #define ST21NFCA_NFCIP1_PSL_REQ 0x04
  24. #define ST21NFCA_NFCIP1_PSL_RES 0x05
  25. #define ST21NFCA_NFCIP1_DEP_REQ 0x06
  26. #define ST21NFCA_NFCIP1_DEP_RES 0x07
  27. #define ST21NFCA_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
  28. #define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
  29. #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  30. ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
  31. #define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
  32. #define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
  33. #define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10
  34. #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  35. ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
  36. #define ST21NFCA_NFC_DEP_PFB_I_PDU 0x00
  37. #define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU 0x40
  38. #define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
  39. #define ST21NFCA_ATR_REQ_MIN_SIZE 17
  40. #define ST21NFCA_ATR_REQ_MAX_SIZE 65
  41. #define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30
  42. #define ST21NFCA_GB_BIT 0x02
  43. #define ST21NFCA_EVT_SEND_DATA 0x10
  44. #define ST21NFCA_EVT_FIELD_ON 0x11
  45. #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
  46. #define ST21NFCA_EVT_CARD_ACTIVATED 0x13
  47. #define ST21NFCA_EVT_FIELD_OFF 0x14
  48. #define ST21NFCA_EVT_CARD_F_BITRATE 0x16
  49. #define ST21NFCA_EVT_READER_F_BITRATE 0x13
  50. #define ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38)
  51. #define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07)
  52. #define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4)
  53. #define ST21NFCA_CARD_BITRATE_212 0x01
  54. #define ST21NFCA_CARD_BITRATE_424 0x02
  55. #define ST21NFCA_DEFAULT_TIMEOUT 0x0a
  56. #define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \
  57. __LINE__, req)
  58. struct st21nfca_atr_req {
  59. u8 length;
  60. u8 cmd0;
  61. u8 cmd1;
  62. u8 nfcid3[NFC_NFCID3_MAXSIZE];
  63. u8 did;
  64. u8 bsi;
  65. u8 bri;
  66. u8 ppi;
  67. u8 gbi[0];
  68. } __packed;
  69. struct st21nfca_atr_res {
  70. u8 length;
  71. u8 cmd0;
  72. u8 cmd1;
  73. u8 nfcid3[NFC_NFCID3_MAXSIZE];
  74. u8 did;
  75. u8 bsi;
  76. u8 bri;
  77. u8 to;
  78. u8 ppi;
  79. u8 gbi[0];
  80. } __packed;
  81. struct st21nfca_psl_req {
  82. u8 length;
  83. u8 cmd0;
  84. u8 cmd1;
  85. u8 did;
  86. u8 brs;
  87. u8 fsl;
  88. } __packed;
  89. struct st21nfca_psl_res {
  90. u8 length;
  91. u8 cmd0;
  92. u8 cmd1;
  93. u8 did;
  94. } __packed;
  95. struct st21nfca_dep_req_res {
  96. u8 length;
  97. u8 cmd0;
  98. u8 cmd1;
  99. u8 pfb;
  100. u8 did;
  101. u8 nad;
  102. } __packed;
  103. static void st21nfca_tx_work(struct work_struct *work)
  104. {
  105. struct st21nfca_hci_info *info = container_of(work,
  106. struct st21nfca_hci_info,
  107. dep_info.tx_work);
  108. struct nfc_dev *dev;
  109. struct sk_buff *skb;
  110. if (info) {
  111. dev = info->hdev->ndev;
  112. skb = info->dep_info.tx_pending;
  113. device_lock(&dev->dev);
  114. nfc_hci_send_cmd_async(info->hdev, ST21NFCA_RF_READER_F_GATE,
  115. ST21NFCA_WR_XCHG_DATA, skb->data, skb->len,
  116. info->async_cb, info);
  117. device_unlock(&dev->dev);
  118. kfree_skb(skb);
  119. }
  120. }
  121. static void st21nfca_im_send_pdu(struct st21nfca_hci_info *info,
  122. struct sk_buff *skb)
  123. {
  124. info->dep_info.tx_pending = skb;
  125. schedule_work(&info->dep_info.tx_work);
  126. }
  127. static int st21nfca_tm_send_atr_res(struct nfc_hci_dev *hdev,
  128. struct st21nfca_atr_req *atr_req)
  129. {
  130. struct st21nfca_atr_res *atr_res;
  131. struct sk_buff *skb;
  132. size_t gb_len;
  133. int r;
  134. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  135. gb_len = atr_req->length - sizeof(struct st21nfca_atr_req);
  136. skb = alloc_skb(atr_req->length + 1, GFP_KERNEL);
  137. if (!skb)
  138. return -ENOMEM;
  139. skb_put(skb, sizeof(struct st21nfca_atr_res));
  140. atr_res = (struct st21nfca_atr_res *)skb->data;
  141. memset(atr_res, 0, sizeof(struct st21nfca_atr_res));
  142. atr_res->length = atr_req->length + 1;
  143. atr_res->cmd0 = ST21NFCA_NFCIP1_RES;
  144. atr_res->cmd1 = ST21NFCA_NFCIP1_ATR_RES;
  145. memcpy(atr_res->nfcid3, atr_req->nfcid3, 6);
  146. atr_res->bsi = 0x00;
  147. atr_res->bri = 0x00;
  148. atr_res->to = ST21NFCA_DEFAULT_TIMEOUT;
  149. atr_res->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
  150. if (gb_len) {
  151. skb_put(skb, gb_len);
  152. atr_res->ppi |= ST21NFCA_GB_BIT;
  153. memcpy(atr_res->gbi, atr_req->gbi, gb_len);
  154. r = nfc_set_remote_general_bytes(hdev->ndev, atr_res->gbi,
  155. gb_len);
  156. if (r < 0)
  157. return r;
  158. }
  159. info->dep_info.curr_nfc_dep_pni = 0;
  160. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  161. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  162. kfree_skb(skb);
  163. return r;
  164. }
  165. static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
  166. struct sk_buff *skb)
  167. {
  168. struct st21nfca_atr_req *atr_req;
  169. size_t gb_len;
  170. int r;
  171. skb_trim(skb, skb->len - 1);
  172. if (!skb->len) {
  173. r = -EIO;
  174. goto exit;
  175. }
  176. if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
  177. r = -EPROTO;
  178. goto exit;
  179. }
  180. atr_req = (struct st21nfca_atr_req *)skb->data;
  181. if (atr_req->length < sizeof(struct st21nfca_atr_req)) {
  182. r = -EPROTO;
  183. goto exit;
  184. }
  185. r = st21nfca_tm_send_atr_res(hdev, atr_req);
  186. if (r)
  187. goto exit;
  188. gb_len = skb->len - sizeof(struct st21nfca_atr_req);
  189. r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
  190. NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
  191. if (r)
  192. goto exit;
  193. r = 0;
  194. exit:
  195. return r;
  196. }
  197. static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
  198. struct st21nfca_psl_req *psl_req)
  199. {
  200. struct st21nfca_psl_res *psl_res;
  201. struct sk_buff *skb;
  202. u8 bitrate[2] = {0, 0};
  203. int r;
  204. skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
  205. if (!skb)
  206. return -ENOMEM;
  207. skb_put(skb, sizeof(struct st21nfca_psl_res));
  208. psl_res = (struct st21nfca_psl_res *)skb->data;
  209. psl_res->length = sizeof(struct st21nfca_psl_res);
  210. psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
  211. psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
  212. psl_res->did = psl_req->did;
  213. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  214. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  215. if (r < 0)
  216. goto error;
  217. /*
  218. * ST21NFCA only support P2P passive.
  219. * PSL_REQ BRS value != 0 has only a meaning to
  220. * change technology to type F.
  221. * We change to BITRATE 424Kbits.
  222. * In other case switch to BITRATE 106Kbits.
  223. */
  224. if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
  225. ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
  226. bitrate[0] = ST21NFCA_CARD_BITRATE_424;
  227. bitrate[1] = ST21NFCA_CARD_BITRATE_424;
  228. }
  229. /* Send an event to change bitrate change event to card f */
  230. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  231. ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
  232. error:
  233. kfree_skb(skb);
  234. return r;
  235. }
  236. static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
  237. struct sk_buff *skb)
  238. {
  239. struct st21nfca_psl_req *psl_req;
  240. int r;
  241. skb_trim(skb, skb->len - 1);
  242. if (!skb->len) {
  243. r = -EIO;
  244. goto exit;
  245. }
  246. psl_req = (struct st21nfca_psl_req *)skb->data;
  247. if (skb->len < sizeof(struct st21nfca_psl_req)) {
  248. r = -EIO;
  249. goto exit;
  250. }
  251. r = st21nfca_tm_send_psl_res(hdev, psl_req);
  252. exit:
  253. return r;
  254. }
  255. int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  256. {
  257. int r;
  258. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  259. *skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
  260. *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
  261. *skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
  262. *skb_push(skb, 1) = skb->len;
  263. r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
  264. ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
  265. kfree_skb(skb);
  266. return r;
  267. }
  268. EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
  269. static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
  270. struct sk_buff *skb)
  271. {
  272. struct st21nfca_dep_req_res *dep_req;
  273. u8 size;
  274. int r;
  275. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  276. skb_trim(skb, skb->len - 1);
  277. size = 4;
  278. dep_req = (struct st21nfca_dep_req_res *)skb->data;
  279. if (skb->len < size) {
  280. r = -EIO;
  281. goto exit;
  282. }
  283. if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
  284. size++;
  285. if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
  286. size++;
  287. if (skb->len < size) {
  288. r = -EIO;
  289. goto exit;
  290. }
  291. /* Receiving DEP_REQ - Decoding */
  292. switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
  293. case ST21NFCA_NFC_DEP_PFB_I_PDU:
  294. info->dep_info.curr_nfc_dep_pni =
  295. ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
  296. break;
  297. case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
  298. pr_err("Received a ACK/NACK PDU\n");
  299. break;
  300. case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
  301. pr_err("Received a SUPERVISOR PDU\n");
  302. break;
  303. }
  304. skb_pull(skb, size);
  305. return nfc_tm_data_received(hdev->ndev, skb);
  306. exit:
  307. return r;
  308. }
  309. static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
  310. struct sk_buff *skb)
  311. {
  312. u8 cmd0, cmd1;
  313. int r;
  314. cmd0 = skb->data[1];
  315. switch (cmd0) {
  316. case ST21NFCA_NFCIP1_REQ:
  317. cmd1 = skb->data[2];
  318. switch (cmd1) {
  319. case ST21NFCA_NFCIP1_ATR_REQ:
  320. r = st21nfca_tm_recv_atr_req(hdev, skb);
  321. break;
  322. case ST21NFCA_NFCIP1_PSL_REQ:
  323. r = st21nfca_tm_recv_psl_req(hdev, skb);
  324. break;
  325. case ST21NFCA_NFCIP1_DEP_REQ:
  326. r = st21nfca_tm_recv_dep_req(hdev, skb);
  327. break;
  328. default:
  329. return 1;
  330. }
  331. default:
  332. return 1;
  333. }
  334. return r;
  335. }
  336. /*
  337. * Returns:
  338. * <= 0: driver handled the event, skb consumed
  339. * 1: driver does not handle the event, please do standard processing
  340. */
  341. int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
  342. u8 event, struct sk_buff *skb)
  343. {
  344. int r = 0;
  345. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  346. pr_debug("dep event: %d\n", event);
  347. switch (event) {
  348. case ST21NFCA_EVT_CARD_ACTIVATED:
  349. info->dep_info.curr_nfc_dep_pni = 0;
  350. break;
  351. case ST21NFCA_EVT_CARD_DEACTIVATED:
  352. break;
  353. case ST21NFCA_EVT_FIELD_ON:
  354. break;
  355. case ST21NFCA_EVT_FIELD_OFF:
  356. break;
  357. case ST21NFCA_EVT_SEND_DATA:
  358. r = st21nfca_tm_event_send_data(hdev, skb);
  359. if (r < 0)
  360. return r;
  361. return 0;
  362. default:
  363. nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
  364. return 1;
  365. }
  366. kfree_skb(skb);
  367. return r;
  368. }
  369. EXPORT_SYMBOL(st21nfca_dep_event_received);
  370. static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
  371. u8 bri, u8 lri)
  372. {
  373. struct sk_buff *skb;
  374. struct st21nfca_psl_req *psl_req;
  375. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  376. skb =
  377. alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
  378. if (!skb)
  379. return;
  380. skb_reserve(skb, 1);
  381. skb_put(skb, sizeof(struct st21nfca_psl_req));
  382. psl_req = (struct st21nfca_psl_req *) skb->data;
  383. psl_req->length = sizeof(struct st21nfca_psl_req);
  384. psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
  385. psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
  386. psl_req->did = did;
  387. psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
  388. psl_req->fsl = lri;
  389. *skb_push(skb, 1) = info->dep_info.to | 0x10;
  390. st21nfca_im_send_pdu(info, skb);
  391. }
  392. #define ST21NFCA_CB_TYPE_READER_F 1
  393. static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
  394. int err)
  395. {
  396. struct st21nfca_hci_info *info = context;
  397. struct st21nfca_atr_res *atr_res;
  398. int r;
  399. if (err != 0)
  400. return;
  401. if (!skb)
  402. return;
  403. switch (info->async_cb_type) {
  404. case ST21NFCA_CB_TYPE_READER_F:
  405. skb_trim(skb, skb->len - 1);
  406. atr_res = (struct st21nfca_atr_res *)skb->data;
  407. r = nfc_set_remote_general_bytes(info->hdev->ndev,
  408. atr_res->gbi,
  409. skb->len - sizeof(struct st21nfca_atr_res));
  410. if (r < 0)
  411. return;
  412. if (atr_res->to >= 0x0e)
  413. info->dep_info.to = 0x0e;
  414. else
  415. info->dep_info.to = atr_res->to + 1;
  416. info->dep_info.to |= 0x10;
  417. r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
  418. NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
  419. if (r < 0)
  420. return;
  421. info->dep_info.curr_nfc_dep_pni = 0;
  422. if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
  423. st21nfca_im_send_psl_req(info->hdev, atr_res->did,
  424. atr_res->bsi, atr_res->bri,
  425. ST21NFCA_PP2LRI(atr_res->ppi));
  426. break;
  427. default:
  428. kfree_skb(skb);
  429. break;
  430. }
  431. }
  432. int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
  433. {
  434. struct sk_buff *skb;
  435. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  436. struct st21nfca_atr_req *atr_req;
  437. struct nfc_target *target;
  438. uint size;
  439. info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
  440. size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
  441. if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
  442. PROTOCOL_ERR("14.6.1.1");
  443. return -EINVAL;
  444. }
  445. skb =
  446. alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
  447. if (!skb)
  448. return -ENOMEM;
  449. skb_reserve(skb, 1);
  450. skb_put(skb, sizeof(struct st21nfca_atr_req));
  451. atr_req = (struct st21nfca_atr_req *)skb->data;
  452. memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
  453. atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
  454. atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
  455. memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
  456. target = hdev->ndev->targets;
  457. if (target->sensf_res_len > 0)
  458. memcpy(atr_req->nfcid3, target->sensf_res,
  459. target->sensf_res_len);
  460. else
  461. get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
  462. atr_req->did = 0x0;
  463. atr_req->bsi = 0x00;
  464. atr_req->bri = 0x00;
  465. atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
  466. if (gb_len) {
  467. atr_req->ppi |= ST21NFCA_GB_BIT;
  468. memcpy(skb_put(skb, gb_len), gb, gb_len);
  469. }
  470. atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
  471. *skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
  472. info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
  473. info->async_cb_context = info;
  474. info->async_cb = st21nfca_im_recv_atr_res_cb;
  475. info->dep_info.bri = atr_req->bri;
  476. info->dep_info.bsi = atr_req->bsi;
  477. info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
  478. return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
  479. ST21NFCA_WR_XCHG_DATA, skb->data,
  480. skb->len, info->async_cb, info);
  481. }
  482. EXPORT_SYMBOL(st21nfca_im_send_atr_req);
  483. static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
  484. int err)
  485. {
  486. struct st21nfca_hci_info *info = context;
  487. struct st21nfca_dep_req_res *dep_res;
  488. int size;
  489. if (err != 0)
  490. return;
  491. if (!skb)
  492. return;
  493. switch (info->async_cb_type) {
  494. case ST21NFCA_CB_TYPE_READER_F:
  495. dep_res = (struct st21nfca_dep_req_res *)skb->data;
  496. size = 3;
  497. if (skb->len < size)
  498. goto exit;
  499. if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
  500. size++;
  501. if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
  502. size++;
  503. if (skb->len < size)
  504. goto exit;
  505. skb_trim(skb, skb->len - 1);
  506. /* Receiving DEP_REQ - Decoding */
  507. switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
  508. case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
  509. pr_err("Received a ACK/NACK PDU\n");
  510. case ST21NFCA_NFC_DEP_PFB_I_PDU:
  511. info->dep_info.curr_nfc_dep_pni =
  512. ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
  513. size++;
  514. skb_pull(skb, size);
  515. nfc_tm_data_received(info->hdev->ndev, skb);
  516. break;
  517. case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
  518. pr_err("Received a SUPERVISOR PDU\n");
  519. skb_pull(skb, size);
  520. *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
  521. *skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
  522. *skb_push(skb, 1) = skb->len;
  523. *skb_push(skb, 1) = info->dep_info.to | 0x10;
  524. st21nfca_im_send_pdu(info, skb);
  525. break;
  526. }
  527. return;
  528. default:
  529. break;
  530. }
  531. exit:
  532. kfree_skb(skb);
  533. }
  534. int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  535. {
  536. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  537. info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
  538. info->async_cb_context = info;
  539. info->async_cb = st21nfca_im_recv_dep_res_cb;
  540. *skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
  541. *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
  542. *skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
  543. *skb_push(skb, 1) = skb->len;
  544. *skb_push(skb, 1) = info->dep_info.to | 0x10;
  545. return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
  546. ST21NFCA_WR_XCHG_DATA,
  547. skb->data, skb->len,
  548. info->async_cb, info);
  549. }
  550. EXPORT_SYMBOL(st21nfca_im_send_dep_req);
  551. void st21nfca_dep_init(struct nfc_hci_dev *hdev)
  552. {
  553. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  554. INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
  555. info->dep_info.curr_nfc_dep_pni = 0;
  556. info->dep_info.idx = 0;
  557. info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
  558. }
  559. EXPORT_SYMBOL(st21nfca_dep_init);
  560. void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
  561. {
  562. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  563. cancel_work_sync(&info->dep_info.tx_work);
  564. }
  565. EXPORT_SYMBOL(st21nfca_dep_deinit);