pn544.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * HCI based Driver for NXP PN544 NFC Chip
  3. *
  4. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the
  17. * Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/nfc.h>
  23. #include <net/nfc/hci.h>
  24. #include <net/nfc/llc.h>
  25. #include "pn544.h"
  26. /* Timing restrictions (ms) */
  27. #define PN544_HCI_RESETVEN_TIME 30
  28. #define HCI_MODE 0
  29. #define FW_MODE 1
  30. enum pn544_state {
  31. PN544_ST_COLD,
  32. PN544_ST_FW_READY,
  33. PN544_ST_READY,
  34. };
  35. #define FULL_VERSION_LEN 11
  36. /* Proprietary commands */
  37. #define PN544_WRITE 0x3f
  38. /* Proprietary gates, events, commands and registers */
  39. /* NFC_HCI_RF_READER_A_GATE additional registers and commands */
  40. #define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
  41. #define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
  42. #define PN544_MIFARE_CMD 0x21
  43. /* Commands that apply to all RF readers */
  44. #define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
  45. #define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
  46. /* NFC_HCI_ID_MGMT_GATE additional registers */
  47. #define PN544_ID_MGMT_FULL_VERSION_SW 0x10
  48. #define PN544_RF_READER_ISO15693_GATE 0x12
  49. #define PN544_RF_READER_F_GATE 0x14
  50. #define PN544_FELICA_ID 0x04
  51. #define PN544_FELICA_RAW 0x20
  52. #define PN544_RF_READER_JEWEL_GATE 0x15
  53. #define PN544_JEWEL_RAW_CMD 0x23
  54. #define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
  55. #define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
  56. #define PN544_SYS_MGMT_GATE 0x90
  57. #define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
  58. #define PN544_POLLING_LOOP_MGMT_GATE 0x94
  59. #define PN544_DEP_MODE 0x01
  60. #define PN544_DEP_ATR_REQ 0x02
  61. #define PN544_DEP_ATR_RES 0x03
  62. #define PN544_DEP_MERGE 0x0D
  63. #define PN544_PL_RDPHASES 0x06
  64. #define PN544_PL_EMULATION 0x07
  65. #define PN544_PL_NFCT_DEACTIVATED 0x09
  66. #define PN544_SWP_MGMT_GATE 0xA0
  67. #define PN544_NFC_WI_MGMT_GATE 0xA1
  68. #define PN544_HCI_EVT_SND_DATA 0x01
  69. #define PN544_HCI_EVT_ACTIVATED 0x02
  70. #define PN544_HCI_EVT_DEACTIVATED 0x03
  71. #define PN544_HCI_EVT_RCV_DATA 0x04
  72. #define PN544_HCI_EVT_CONTINUE_MI 0x05
  73. #define PN544_HCI_CMD_ATTREQUEST 0x12
  74. #define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13
  75. static struct nfc_hci_gate pn544_gates[] = {
  76. {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
  77. {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
  78. {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  79. {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  80. {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
  81. {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
  82. {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  83. {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  84. {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  85. {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  86. {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
  87. {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
  88. {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
  89. {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
  90. {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
  91. };
  92. /* Largest headroom needed for outgoing custom commands */
  93. #define PN544_CMDS_HEADROOM 2
  94. struct pn544_hci_info {
  95. struct nfc_phy_ops *phy_ops;
  96. void *phy_id;
  97. struct nfc_hci_dev *hdev;
  98. enum pn544_state state;
  99. struct mutex info_lock;
  100. int async_cb_type;
  101. data_exchange_cb_t async_cb;
  102. void *async_cb_context;
  103. };
  104. static int pn544_hci_open(struct nfc_hci_dev *hdev)
  105. {
  106. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  107. int r = 0;
  108. mutex_lock(&info->info_lock);
  109. if (info->state != PN544_ST_COLD) {
  110. r = -EBUSY;
  111. goto out;
  112. }
  113. r = info->phy_ops->enable(info->phy_id);
  114. if (r == 0)
  115. info->state = PN544_ST_READY;
  116. out:
  117. mutex_unlock(&info->info_lock);
  118. return r;
  119. }
  120. static void pn544_hci_close(struct nfc_hci_dev *hdev)
  121. {
  122. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  123. mutex_lock(&info->info_lock);
  124. if (info->state == PN544_ST_COLD)
  125. goto out;
  126. info->phy_ops->disable(info->phy_id);
  127. info->state = PN544_ST_COLD;
  128. out:
  129. mutex_unlock(&info->info_lock);
  130. }
  131. static int pn544_hci_ready(struct nfc_hci_dev *hdev)
  132. {
  133. struct sk_buff *skb;
  134. static struct hw_config {
  135. u8 adr[2];
  136. u8 value;
  137. } hw_config[] = {
  138. {{0x9f, 0x9a}, 0x00},
  139. {{0x98, 0x10}, 0xbc},
  140. {{0x9e, 0x71}, 0x00},
  141. {{0x98, 0x09}, 0x00},
  142. {{0x9e, 0xb4}, 0x00},
  143. {{0x9e, 0xd9}, 0xff},
  144. {{0x9e, 0xda}, 0xff},
  145. {{0x9e, 0xdb}, 0x23},
  146. {{0x9e, 0xdc}, 0x21},
  147. {{0x9e, 0xdd}, 0x22},
  148. {{0x9e, 0xde}, 0x24},
  149. {{0x9c, 0x01}, 0x08},
  150. {{0x9e, 0xaa}, 0x01},
  151. {{0x9b, 0xd1}, 0x0d},
  152. {{0x9b, 0xd2}, 0x24},
  153. {{0x9b, 0xd3}, 0x0a},
  154. {{0x9b, 0xd4}, 0x22},
  155. {{0x9b, 0xd5}, 0x08},
  156. {{0x9b, 0xd6}, 0x1e},
  157. {{0x9b, 0xdd}, 0x1c},
  158. {{0x9b, 0x84}, 0x13},
  159. {{0x99, 0x81}, 0x7f},
  160. {{0x99, 0x31}, 0x70},
  161. {{0x98, 0x00}, 0x3f},
  162. {{0x9f, 0x09}, 0x00},
  163. {{0x9f, 0x0a}, 0x05},
  164. {{0x9e, 0xd1}, 0xa1},
  165. {{0x99, 0x23}, 0x00},
  166. {{0x9e, 0x74}, 0x80},
  167. {{0x9f, 0x28}, 0x10},
  168. {{0x9f, 0x35}, 0x14},
  169. {{0x9f, 0x36}, 0x60},
  170. {{0x9c, 0x31}, 0x00},
  171. {{0x9c, 0x32}, 0xc8},
  172. {{0x9c, 0x19}, 0x40},
  173. {{0x9c, 0x1a}, 0x40},
  174. {{0x9c, 0x0c}, 0x00},
  175. {{0x9c, 0x0d}, 0x00},
  176. {{0x9c, 0x12}, 0x00},
  177. {{0x9c, 0x13}, 0x00},
  178. {{0x98, 0xa2}, 0x0e},
  179. {{0x98, 0x93}, 0x40},
  180. {{0x98, 0x7d}, 0x02},
  181. {{0x98, 0x7e}, 0x00},
  182. {{0x9f, 0xc8}, 0x01},
  183. };
  184. struct hw_config *p = hw_config;
  185. int count = ARRAY_SIZE(hw_config);
  186. struct sk_buff *res_skb;
  187. u8 param[4];
  188. int r;
  189. param[0] = 0;
  190. while (count--) {
  191. param[1] = p->adr[0];
  192. param[2] = p->adr[1];
  193. param[3] = p->value;
  194. r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
  195. param, 4, &res_skb);
  196. if (r < 0)
  197. return r;
  198. if (res_skb->len != 1) {
  199. kfree_skb(res_skb);
  200. return -EPROTO;
  201. }
  202. if (res_skb->data[0] != p->value) {
  203. kfree_skb(res_skb);
  204. return -EIO;
  205. }
  206. kfree_skb(res_skb);
  207. p++;
  208. }
  209. param[0] = NFC_HCI_UICC_HOST_ID;
  210. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  211. NFC_HCI_ADMIN_WHITELIST, param, 1);
  212. if (r < 0)
  213. return r;
  214. param[0] = 0x3d;
  215. r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
  216. PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
  217. if (r < 0)
  218. return r;
  219. param[0] = 0x0;
  220. r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
  221. PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
  222. if (r < 0)
  223. return r;
  224. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  225. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  226. if (r < 0)
  227. return r;
  228. param[0] = 0x1;
  229. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  230. PN544_PL_NFCT_DEACTIVATED, param, 1);
  231. if (r < 0)
  232. return r;
  233. param[0] = 0x0;
  234. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  235. PN544_PL_RDPHASES, param, 1);
  236. if (r < 0)
  237. return r;
  238. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  239. PN544_ID_MGMT_FULL_VERSION_SW, &skb);
  240. if (r < 0)
  241. return r;
  242. if (skb->len != FULL_VERSION_LEN) {
  243. kfree_skb(skb);
  244. return -EINVAL;
  245. }
  246. print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
  247. DUMP_PREFIX_NONE, 16, 1,
  248. skb->data, FULL_VERSION_LEN, false);
  249. kfree_skb(skb);
  250. return 0;
  251. }
  252. static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  253. {
  254. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  255. return info->phy_ops->write(info->phy_id, skb);
  256. }
  257. static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
  258. u32 im_protocols, u32 tm_protocols)
  259. {
  260. u8 phases = 0;
  261. int r;
  262. u8 duration[2];
  263. u8 activated;
  264. u8 i_mode = 0x3f; /* Enable all supported modes */
  265. u8 t_mode = 0x0f;
  266. u8 t_merge = 0x01; /* Enable merge by default */
  267. pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
  268. __func__, im_protocols, tm_protocols);
  269. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  270. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  271. if (r < 0)
  272. return r;
  273. duration[0] = 0x18;
  274. duration[1] = 0x6a;
  275. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  276. PN544_PL_EMULATION, duration, 2);
  277. if (r < 0)
  278. return r;
  279. activated = 0;
  280. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  281. PN544_PL_NFCT_DEACTIVATED, &activated, 1);
  282. if (r < 0)
  283. return r;
  284. if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
  285. NFC_PROTO_JEWEL_MASK))
  286. phases |= 1; /* Type A */
  287. if (im_protocols & NFC_PROTO_FELICA_MASK) {
  288. phases |= (1 << 2); /* Type F 212 */
  289. phases |= (1 << 3); /* Type F 424 */
  290. }
  291. phases |= (1 << 5); /* NFC active */
  292. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  293. PN544_PL_RDPHASES, &phases, 1);
  294. if (r < 0)
  295. return r;
  296. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  297. hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
  298. &hdev->gb_len);
  299. pr_debug("generate local bytes %p", hdev->gb);
  300. if (hdev->gb == NULL || hdev->gb_len == 0) {
  301. im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  302. tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  303. }
  304. }
  305. if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  306. r = nfc_hci_send_event(hdev,
  307. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  308. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  309. if (r < 0)
  310. return r;
  311. r = nfc_hci_set_param(hdev,
  312. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  313. PN544_DEP_MODE, &i_mode, 1);
  314. if (r < 0)
  315. return r;
  316. r = nfc_hci_set_param(hdev,
  317. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  318. PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len);
  319. if (r < 0)
  320. return r;
  321. r = nfc_hci_send_event(hdev,
  322. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  323. NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
  324. if (r < 0)
  325. nfc_hci_send_event(hdev,
  326. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  327. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  328. }
  329. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  330. r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
  331. PN544_DEP_MODE, &t_mode, 1);
  332. if (r < 0)
  333. return r;
  334. r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
  335. PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len);
  336. if (r < 0)
  337. return r;
  338. r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
  339. PN544_DEP_MERGE, &t_merge, 1);
  340. if (r < 0)
  341. return r;
  342. }
  343. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  344. NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
  345. if (r < 0)
  346. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  347. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  348. return r;
  349. }
  350. static int pn544_hci_dep_link_up(struct nfc_hci_dev *hdev,
  351. struct nfc_target *target, u8 comm_mode,
  352. u8 *gb, size_t gb_len)
  353. {
  354. struct sk_buff *rgb_skb = NULL;
  355. int r;
  356. r = nfc_hci_get_param(hdev, target->hci_reader_gate,
  357. PN544_DEP_ATR_RES, &rgb_skb);
  358. if (r < 0)
  359. return r;
  360. if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
  361. r = -EPROTO;
  362. goto exit;
  363. }
  364. print_hex_dump(KERN_DEBUG, "remote gb: ", DUMP_PREFIX_OFFSET,
  365. 16, 1, rgb_skb->data, rgb_skb->len, true);
  366. r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
  367. rgb_skb->len);
  368. if (r == 0)
  369. r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
  370. NFC_RF_INITIATOR);
  371. exit:
  372. kfree_skb(rgb_skb);
  373. return r;
  374. }
  375. static int pn544_hci_dep_link_down(struct nfc_hci_dev *hdev)
  376. {
  377. return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  378. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  379. }
  380. static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  381. struct nfc_target *target)
  382. {
  383. switch (gate) {
  384. case PN544_RF_READER_F_GATE:
  385. target->supported_protocols = NFC_PROTO_FELICA_MASK;
  386. break;
  387. case PN544_RF_READER_JEWEL_GATE:
  388. target->supported_protocols = NFC_PROTO_JEWEL_MASK;
  389. target->sens_res = 0x0c00;
  390. break;
  391. case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
  392. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  393. break;
  394. default:
  395. return -EPROTO;
  396. }
  397. return 0;
  398. }
  399. static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
  400. u8 gate,
  401. struct nfc_target *target)
  402. {
  403. struct sk_buff *uid_skb;
  404. int r = 0;
  405. if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
  406. return r;
  407. if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
  408. r = nfc_hci_send_cmd(hdev,
  409. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  410. PN544_HCI_CMD_CONTINUE_ACTIVATION, NULL, 0, NULL);
  411. if (r < 0)
  412. return r;
  413. target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
  414. } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  415. if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
  416. target->nfcid1_len != 10)
  417. return -EPROTO;
  418. r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  419. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  420. target->nfcid1, target->nfcid1_len, NULL);
  421. } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
  422. r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
  423. PN544_FELICA_ID, &uid_skb);
  424. if (r < 0)
  425. return r;
  426. if (uid_skb->len != 8) {
  427. kfree_skb(uid_skb);
  428. return -EPROTO;
  429. }
  430. r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
  431. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  432. uid_skb->data, uid_skb->len, NULL);
  433. kfree_skb(uid_skb);
  434. r = nfc_hci_send_cmd(hdev,
  435. PN544_RF_READER_NFCIP1_INITIATOR_GATE,
  436. PN544_HCI_CMD_CONTINUE_ACTIVATION,
  437. NULL, 0, NULL);
  438. if (r < 0)
  439. return r;
  440. target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
  441. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  442. } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
  443. /*
  444. * TODO: maybe other ISO 14443 require some kind of continue
  445. * activation, but for now we've seen only this one below.
  446. */
  447. if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
  448. r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  449. PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
  450. NULL, 0, NULL);
  451. }
  452. return r;
  453. }
  454. #define PN544_CB_TYPE_READER_F 1
  455. static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  456. int err)
  457. {
  458. struct pn544_hci_info *info = context;
  459. switch (info->async_cb_type) {
  460. case PN544_CB_TYPE_READER_F:
  461. if (err == 0)
  462. skb_pull(skb, 1);
  463. info->async_cb(info->async_cb_context, skb, err);
  464. break;
  465. default:
  466. if (err == 0)
  467. kfree_skb(skb);
  468. break;
  469. }
  470. }
  471. #define MIFARE_CMD_AUTH_KEY_A 0x60
  472. #define MIFARE_CMD_AUTH_KEY_B 0x61
  473. #define MIFARE_CMD_HEADER 2
  474. #define MIFARE_UID_LEN 4
  475. #define MIFARE_KEY_LEN 6
  476. #define MIFARE_CMD_LEN 12
  477. /*
  478. * Returns:
  479. * <= 0: driver handled the data exchange
  480. * 1: driver doesn't especially handle, please do standard processing
  481. */
  482. static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev,
  483. struct nfc_target *target,
  484. struct sk_buff *skb, data_exchange_cb_t cb,
  485. void *cb_context)
  486. {
  487. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  488. pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
  489. target->hci_reader_gate);
  490. switch (target->hci_reader_gate) {
  491. case NFC_HCI_RF_READER_A_GATE:
  492. if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  493. /*
  494. * It seems that pn544 is inverting key and UID for
  495. * MIFARE authentication commands.
  496. */
  497. if (skb->len == MIFARE_CMD_LEN &&
  498. (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
  499. skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
  500. u8 uid[MIFARE_UID_LEN];
  501. u8 *data = skb->data + MIFARE_CMD_HEADER;
  502. memcpy(uid, data + MIFARE_KEY_LEN,
  503. MIFARE_UID_LEN);
  504. memmove(data + MIFARE_UID_LEN, data,
  505. MIFARE_KEY_LEN);
  506. memcpy(data, uid, MIFARE_UID_LEN);
  507. }
  508. return nfc_hci_send_cmd_async(hdev,
  509. target->hci_reader_gate,
  510. PN544_MIFARE_CMD,
  511. skb->data, skb->len,
  512. cb, cb_context);
  513. } else
  514. return 1;
  515. case PN544_RF_READER_F_GATE:
  516. *skb_push(skb, 1) = 0;
  517. *skb_push(skb, 1) = 0;
  518. info->async_cb_type = PN544_CB_TYPE_READER_F;
  519. info->async_cb = cb;
  520. info->async_cb_context = cb_context;
  521. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  522. PN544_FELICA_RAW, skb->data,
  523. skb->len,
  524. pn544_hci_data_exchange_cb, info);
  525. case PN544_RF_READER_JEWEL_GATE:
  526. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  527. PN544_JEWEL_RAW_CMD, skb->data,
  528. skb->len, cb, cb_context);
  529. case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
  530. *skb_push(skb, 1) = 0;
  531. return nfc_hci_send_event(hdev, target->hci_reader_gate,
  532. PN544_HCI_EVT_SND_DATA, skb->data,
  533. skb->len);
  534. default:
  535. return 1;
  536. }
  537. }
  538. static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  539. {
  540. /* Set default false for multiple information chaining */
  541. *skb_push(skb, 1) = 0;
  542. return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
  543. PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
  544. }
  545. static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
  546. struct nfc_target *target)
  547. {
  548. pr_debug("supported protocol %d", target->supported_protocols);
  549. if (target->supported_protocols & (NFC_PROTO_ISO14443_MASK |
  550. NFC_PROTO_ISO14443_B_MASK)) {
  551. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  552. PN544_RF_READER_CMD_PRESENCE_CHECK,
  553. NULL, 0, NULL);
  554. } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  555. if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
  556. target->nfcid1_len != 10)
  557. return -EOPNOTSUPP;
  558. return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  559. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  560. target->nfcid1, target->nfcid1_len, NULL);
  561. } else if (target->supported_protocols & NFC_PROTO_JEWEL_MASK) {
  562. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  563. PN544_JEWEL_RAW_CMD, NULL, 0, NULL);
  564. } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
  565. return nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
  566. PN544_FELICA_RAW, NULL, 0, NULL);
  567. } else if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
  568. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  569. PN544_HCI_CMD_ATTREQUEST,
  570. NULL, 0, NULL);
  571. }
  572. return 0;
  573. }
  574. static void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
  575. u8 event, struct sk_buff *skb)
  576. {
  577. struct sk_buff *rgb_skb = NULL;
  578. int r = 0;
  579. pr_debug("hci event %d", event);
  580. switch (event) {
  581. case PN544_HCI_EVT_ACTIVATED:
  582. if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
  583. nfc_hci_target_discovered(hdev, gate);
  584. else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
  585. r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
  586. &rgb_skb);
  587. if (r < 0)
  588. goto exit;
  589. nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
  590. NFC_COMM_PASSIVE, rgb_skb->data,
  591. rgb_skb->len);
  592. kfree_skb(rgb_skb);
  593. }
  594. break;
  595. case PN544_HCI_EVT_DEACTIVATED:
  596. nfc_hci_send_event(hdev, gate,
  597. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  598. break;
  599. case PN544_HCI_EVT_RCV_DATA:
  600. if (skb->len < 2) {
  601. r = -EPROTO;
  602. goto exit;
  603. }
  604. if (skb->data[0] != 0) {
  605. pr_debug("data0 %d", skb->data[0]);
  606. r = -EPROTO;
  607. goto exit;
  608. }
  609. skb_pull(skb, 2);
  610. nfc_tm_data_received(hdev->ndev, skb);
  611. return;
  612. default:
  613. break;
  614. }
  615. exit:
  616. kfree_skb(skb);
  617. }
  618. static struct nfc_hci_ops pn544_hci_ops = {
  619. .open = pn544_hci_open,
  620. .close = pn544_hci_close,
  621. .hci_ready = pn544_hci_ready,
  622. .xmit = pn544_hci_xmit,
  623. .start_poll = pn544_hci_start_poll,
  624. .dep_link_up = pn544_hci_dep_link_up,
  625. .dep_link_down = pn544_hci_dep_link_down,
  626. .target_from_gate = pn544_hci_target_from_gate,
  627. .complete_target_discovered = pn544_hci_complete_target_discovered,
  628. .im_transceive = pn544_hci_im_transceive,
  629. .tm_send = pn544_hci_tm_send,
  630. .check_presence = pn544_hci_check_presence,
  631. .event_received = pn544_hci_event_received,
  632. };
  633. int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
  634. int phy_headroom, int phy_tailroom, int phy_payload,
  635. struct nfc_hci_dev **hdev)
  636. {
  637. struct pn544_hci_info *info;
  638. u32 protocols;
  639. struct nfc_hci_init_data init_data;
  640. int r;
  641. info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
  642. if (!info) {
  643. pr_err("Cannot allocate memory for pn544_hci_info.\n");
  644. r = -ENOMEM;
  645. goto err_info_alloc;
  646. }
  647. info->phy_ops = phy_ops;
  648. info->phy_id = phy_id;
  649. info->state = PN544_ST_COLD;
  650. mutex_init(&info->info_lock);
  651. init_data.gate_count = ARRAY_SIZE(pn544_gates);
  652. memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
  653. /*
  654. * TODO: Session id must include the driver name + some bus addr
  655. * persistent info to discriminate 2 identical chips
  656. */
  657. strcpy(init_data.session_id, "ID544HCI");
  658. protocols = NFC_PROTO_JEWEL_MASK |
  659. NFC_PROTO_MIFARE_MASK |
  660. NFC_PROTO_FELICA_MASK |
  661. NFC_PROTO_ISO14443_MASK |
  662. NFC_PROTO_ISO14443_B_MASK |
  663. NFC_PROTO_NFC_DEP_MASK;
  664. info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
  665. protocols, llc_name,
  666. phy_headroom + PN544_CMDS_HEADROOM,
  667. phy_tailroom, phy_payload);
  668. if (!info->hdev) {
  669. pr_err("Cannot allocate nfc hdev.\n");
  670. r = -ENOMEM;
  671. goto err_alloc_hdev;
  672. }
  673. nfc_hci_set_clientdata(info->hdev, info);
  674. r = nfc_hci_register_device(info->hdev);
  675. if (r)
  676. goto err_regdev;
  677. *hdev = info->hdev;
  678. return 0;
  679. err_regdev:
  680. nfc_hci_free_device(info->hdev);
  681. err_alloc_hdev:
  682. kfree(info);
  683. err_info_alloc:
  684. return r;
  685. }
  686. void pn544_hci_remove(struct nfc_hci_dev *hdev)
  687. {
  688. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  689. nfc_hci_unregister_device(hdev);
  690. nfc_hci_free_device(hdev);
  691. kfree(info);
  692. }