pn544.c 24 KB

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