pn544.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. /* Type F NFC-DEP IDm has prefix 0x01FE */
  432. if ((uid_skb->data[0] == 0x01) && (uid_skb->data[1] == 0xfe)) {
  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->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  441. target->hci_reader_gate =
  442. PN544_RF_READER_NFCIP1_INITIATOR_GATE;
  443. } else {
  444. r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
  445. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  446. uid_skb->data, uid_skb->len, NULL);
  447. kfree_skb(uid_skb);
  448. }
  449. } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
  450. /*
  451. * TODO: maybe other ISO 14443 require some kind of continue
  452. * activation, but for now we've seen only this one below.
  453. */
  454. if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
  455. r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  456. PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
  457. NULL, 0, NULL);
  458. }
  459. return r;
  460. }
  461. #define PN544_CB_TYPE_READER_F 1
  462. static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  463. int err)
  464. {
  465. struct pn544_hci_info *info = context;
  466. switch (info->async_cb_type) {
  467. case PN544_CB_TYPE_READER_F:
  468. if (err == 0)
  469. skb_pull(skb, 1);
  470. info->async_cb(info->async_cb_context, skb, err);
  471. break;
  472. default:
  473. if (err == 0)
  474. kfree_skb(skb);
  475. break;
  476. }
  477. }
  478. #define MIFARE_CMD_AUTH_KEY_A 0x60
  479. #define MIFARE_CMD_AUTH_KEY_B 0x61
  480. #define MIFARE_CMD_HEADER 2
  481. #define MIFARE_UID_LEN 4
  482. #define MIFARE_KEY_LEN 6
  483. #define MIFARE_CMD_LEN 12
  484. /*
  485. * Returns:
  486. * <= 0: driver handled the data exchange
  487. * 1: driver doesn't especially handle, please do standard processing
  488. */
  489. static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev,
  490. struct nfc_target *target,
  491. struct sk_buff *skb, data_exchange_cb_t cb,
  492. void *cb_context)
  493. {
  494. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  495. pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
  496. target->hci_reader_gate);
  497. switch (target->hci_reader_gate) {
  498. case NFC_HCI_RF_READER_A_GATE:
  499. if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  500. /*
  501. * It seems that pn544 is inverting key and UID for
  502. * MIFARE authentication commands.
  503. */
  504. if (skb->len == MIFARE_CMD_LEN &&
  505. (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
  506. skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
  507. u8 uid[MIFARE_UID_LEN];
  508. u8 *data = skb->data + MIFARE_CMD_HEADER;
  509. memcpy(uid, data + MIFARE_KEY_LEN,
  510. MIFARE_UID_LEN);
  511. memmove(data + MIFARE_UID_LEN, data,
  512. MIFARE_KEY_LEN);
  513. memcpy(data, uid, MIFARE_UID_LEN);
  514. }
  515. return nfc_hci_send_cmd_async(hdev,
  516. target->hci_reader_gate,
  517. PN544_MIFARE_CMD,
  518. skb->data, skb->len,
  519. cb, cb_context);
  520. } else
  521. return 1;
  522. case PN544_RF_READER_F_GATE:
  523. *skb_push(skb, 1) = 0;
  524. *skb_push(skb, 1) = 0;
  525. info->async_cb_type = PN544_CB_TYPE_READER_F;
  526. info->async_cb = cb;
  527. info->async_cb_context = cb_context;
  528. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  529. PN544_FELICA_RAW, skb->data,
  530. skb->len,
  531. pn544_hci_data_exchange_cb, info);
  532. case PN544_RF_READER_JEWEL_GATE:
  533. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  534. PN544_JEWEL_RAW_CMD, skb->data,
  535. skb->len, cb, cb_context);
  536. case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
  537. *skb_push(skb, 1) = 0;
  538. return nfc_hci_send_event(hdev, target->hci_reader_gate,
  539. PN544_HCI_EVT_SND_DATA, skb->data,
  540. skb->len);
  541. default:
  542. return 1;
  543. }
  544. }
  545. static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  546. {
  547. int r;
  548. /* Set default false for multiple information chaining */
  549. *skb_push(skb, 1) = 0;
  550. r = nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
  551. PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
  552. kfree_skb(skb);
  553. return r;
  554. }
  555. static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
  556. struct nfc_target *target)
  557. {
  558. pr_debug("supported protocol %d", target->supported_protocols);
  559. if (target->supported_protocols & (NFC_PROTO_ISO14443_MASK |
  560. NFC_PROTO_ISO14443_B_MASK)) {
  561. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  562. PN544_RF_READER_CMD_PRESENCE_CHECK,
  563. NULL, 0, NULL);
  564. } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  565. if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
  566. target->nfcid1_len != 10)
  567. return -EOPNOTSUPP;
  568. return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  569. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  570. target->nfcid1, target->nfcid1_len, NULL);
  571. } else if (target->supported_protocols & (NFC_PROTO_JEWEL_MASK |
  572. NFC_PROTO_FELICA_MASK)) {
  573. return -EOPNOTSUPP;
  574. } else if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
  575. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  576. PN544_HCI_CMD_ATTREQUEST,
  577. NULL, 0, NULL);
  578. }
  579. return 0;
  580. }
  581. /*
  582. * Returns:
  583. * <= 0: driver handled the event, skb consumed
  584. * 1: driver does not handle the event, please do standard processing
  585. */
  586. static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
  587. struct sk_buff *skb)
  588. {
  589. struct sk_buff *rgb_skb = NULL;
  590. int r;
  591. pr_debug("hci event %d", event);
  592. switch (event) {
  593. case PN544_HCI_EVT_ACTIVATED:
  594. if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) {
  595. r = nfc_hci_target_discovered(hdev, gate);
  596. } else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
  597. r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
  598. &rgb_skb);
  599. if (r < 0)
  600. goto exit;
  601. r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
  602. NFC_COMM_PASSIVE, rgb_skb->data,
  603. rgb_skb->len);
  604. kfree_skb(rgb_skb);
  605. } else {
  606. r = -EINVAL;
  607. }
  608. break;
  609. case PN544_HCI_EVT_DEACTIVATED:
  610. r = nfc_hci_send_event(hdev, gate, NFC_HCI_EVT_END_OPERATION,
  611. NULL, 0);
  612. break;
  613. case PN544_HCI_EVT_RCV_DATA:
  614. if (skb->len < 2) {
  615. r = -EPROTO;
  616. goto exit;
  617. }
  618. if (skb->data[0] != 0) {
  619. pr_debug("data0 %d", skb->data[0]);
  620. r = -EPROTO;
  621. goto exit;
  622. }
  623. skb_pull(skb, 2);
  624. return nfc_tm_data_received(hdev->ndev, skb);
  625. default:
  626. return 1;
  627. }
  628. exit:
  629. kfree_skb(skb);
  630. return r;
  631. }
  632. static struct nfc_hci_ops pn544_hci_ops = {
  633. .open = pn544_hci_open,
  634. .close = pn544_hci_close,
  635. .hci_ready = pn544_hci_ready,
  636. .xmit = pn544_hci_xmit,
  637. .start_poll = pn544_hci_start_poll,
  638. .dep_link_up = pn544_hci_dep_link_up,
  639. .dep_link_down = pn544_hci_dep_link_down,
  640. .target_from_gate = pn544_hci_target_from_gate,
  641. .complete_target_discovered = pn544_hci_complete_target_discovered,
  642. .im_transceive = pn544_hci_im_transceive,
  643. .tm_send = pn544_hci_tm_send,
  644. .check_presence = pn544_hci_check_presence,
  645. .event_received = pn544_hci_event_received,
  646. };
  647. int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
  648. int phy_headroom, int phy_tailroom, int phy_payload,
  649. struct nfc_hci_dev **hdev)
  650. {
  651. struct pn544_hci_info *info;
  652. u32 protocols, se;
  653. struct nfc_hci_init_data init_data;
  654. int r;
  655. info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
  656. if (!info) {
  657. pr_err("Cannot allocate memory for pn544_hci_info.\n");
  658. r = -ENOMEM;
  659. goto err_info_alloc;
  660. }
  661. info->phy_ops = phy_ops;
  662. info->phy_id = phy_id;
  663. info->state = PN544_ST_COLD;
  664. mutex_init(&info->info_lock);
  665. init_data.gate_count = ARRAY_SIZE(pn544_gates);
  666. memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
  667. /*
  668. * TODO: Session id must include the driver name + some bus addr
  669. * persistent info to discriminate 2 identical chips
  670. */
  671. strcpy(init_data.session_id, "ID544HCI");
  672. protocols = NFC_PROTO_JEWEL_MASK |
  673. NFC_PROTO_MIFARE_MASK |
  674. NFC_PROTO_FELICA_MASK |
  675. NFC_PROTO_ISO14443_MASK |
  676. NFC_PROTO_ISO14443_B_MASK |
  677. NFC_PROTO_NFC_DEP_MASK;
  678. se = NFC_SE_UICC | NFC_SE_EMBEDDED;
  679. info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 0,
  680. protocols, se, llc_name,
  681. phy_headroom + PN544_CMDS_HEADROOM,
  682. phy_tailroom, phy_payload);
  683. if (!info->hdev) {
  684. pr_err("Cannot allocate nfc hdev.\n");
  685. r = -ENOMEM;
  686. goto err_alloc_hdev;
  687. }
  688. nfc_hci_set_clientdata(info->hdev, info);
  689. r = nfc_hci_register_device(info->hdev);
  690. if (r)
  691. goto err_regdev;
  692. *hdev = info->hdev;
  693. return 0;
  694. err_regdev:
  695. nfc_hci_free_device(info->hdev);
  696. err_alloc_hdev:
  697. kfree(info);
  698. err_info_alloc:
  699. return r;
  700. }
  701. EXPORT_SYMBOL(pn544_hci_probe);
  702. void pn544_hci_remove(struct nfc_hci_dev *hdev)
  703. {
  704. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  705. nfc_hci_unregister_device(hdev);
  706. nfc_hci_free_device(hdev);
  707. kfree(info);
  708. }
  709. EXPORT_SYMBOL(pn544_hci_remove);
  710. MODULE_LICENSE("GPL");
  711. MODULE_DESCRIPTION(DRIVER_DESC);