pn544.c 22 KB

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