pn544_hci.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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/crc-ccitt.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/gpio.h>
  27. #include <linux/i2c.h>
  28. #include <linux/nfc.h>
  29. #include <net/nfc/hci.h>
  30. #include <net/nfc/llc.h>
  31. #include <linux/nfc/pn544.h>
  32. #define DRIVER_DESC "HCI NFC driver for PN544"
  33. #define PN544_HCI_DRIVER_NAME "pn544_hci"
  34. /* Timing restrictions (ms) */
  35. #define PN544_HCI_RESETVEN_TIME 30
  36. static struct i2c_device_id pn544_hci_id_table[] = {
  37. {"pn544", 0},
  38. {}
  39. };
  40. MODULE_DEVICE_TABLE(i2c, pn544_hci_id_table);
  41. #define HCI_MODE 0
  42. #define FW_MODE 1
  43. /* framing in HCI mode */
  44. #define PN544_HCI_LLC_LEN 1
  45. #define PN544_HCI_LLC_CRC 2
  46. #define PN544_HCI_LLC_LEN_CRC (PN544_HCI_LLC_LEN + PN544_HCI_LLC_CRC)
  47. #define PN544_HCI_LLC_MIN_SIZE (1 + PN544_HCI_LLC_LEN_CRC)
  48. #define PN544_HCI_LLC_MAX_PAYLOAD 29
  49. #define PN544_HCI_LLC_MAX_SIZE (PN544_HCI_LLC_LEN_CRC + 1 + \
  50. PN544_HCI_LLC_MAX_PAYLOAD)
  51. enum pn544_state {
  52. PN544_ST_COLD,
  53. PN544_ST_FW_READY,
  54. PN544_ST_READY,
  55. };
  56. #define FULL_VERSION_LEN 11
  57. /* Proprietary commands */
  58. #define PN544_WRITE 0x3f
  59. /* Proprietary gates, events, commands and registers */
  60. /* NFC_HCI_RF_READER_A_GATE additional registers and commands */
  61. #define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
  62. #define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
  63. #define PN544_MIFARE_CMD 0x21
  64. /* Commands that apply to all RF readers */
  65. #define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
  66. #define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
  67. /* NFC_HCI_ID_MGMT_GATE additional registers */
  68. #define PN544_ID_MGMT_FULL_VERSION_SW 0x10
  69. #define PN544_RF_READER_ISO15693_GATE 0x12
  70. #define PN544_RF_READER_F_GATE 0x14
  71. #define PN544_FELICA_ID 0x04
  72. #define PN544_FELICA_RAW 0x20
  73. #define PN544_RF_READER_JEWEL_GATE 0x15
  74. #define PN544_JEWEL_RAW_CMD 0x23
  75. #define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
  76. #define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
  77. #define PN544_SYS_MGMT_GATE 0x90
  78. #define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
  79. #define PN544_POLLING_LOOP_MGMT_GATE 0x94
  80. #define PN544_PL_RDPHASES 0x06
  81. #define PN544_PL_EMULATION 0x07
  82. #define PN544_PL_NFCT_DEACTIVATED 0x09
  83. #define PN544_SWP_MGMT_GATE 0xA0
  84. #define PN544_NFC_WI_MGMT_GATE 0xA1
  85. static struct nfc_hci_gate pn544_gates[] = {
  86. {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
  87. {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
  88. {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  89. {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  90. {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
  91. {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
  92. {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  93. {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  94. {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  95. {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  96. {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
  97. {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
  98. {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
  99. {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
  100. {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
  101. };
  102. /* Largest headroom needed for outgoing custom commands */
  103. #define PN544_CMDS_HEADROOM 2
  104. #define PN544_FRAME_HEADROOM 1
  105. #define PN544_FRAME_TAILROOM 2
  106. struct pn544_hci_info {
  107. struct i2c_client *i2c_dev;
  108. struct nfc_hci_dev *hdev;
  109. enum pn544_state state;
  110. struct mutex info_lock;
  111. unsigned int gpio_en;
  112. unsigned int gpio_irq;
  113. unsigned int gpio_fw;
  114. unsigned int en_polarity;
  115. int hard_fault; /*
  116. * < 0 if hardware error occured (e.g. i2c err)
  117. * and prevents normal operation.
  118. */
  119. int async_cb_type;
  120. data_exchange_cb_t async_cb;
  121. void *async_cb_context;
  122. };
  123. static void pn544_hci_platform_init(struct pn544_hci_info *info)
  124. {
  125. int polarity, retry, ret;
  126. char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
  127. int count = sizeof(rset_cmd);
  128. pr_info(DRIVER_DESC ": %s\n", __func__);
  129. dev_info(&info->i2c_dev->dev, "Detecting nfc_en polarity\n");
  130. /* Disable fw download */
  131. gpio_set_value(info->gpio_fw, 0);
  132. for (polarity = 0; polarity < 2; polarity++) {
  133. info->en_polarity = polarity;
  134. retry = 3;
  135. while (retry--) {
  136. /* power off */
  137. gpio_set_value(info->gpio_en, !info->en_polarity);
  138. usleep_range(10000, 15000);
  139. /* power on */
  140. gpio_set_value(info->gpio_en, info->en_polarity);
  141. usleep_range(10000, 15000);
  142. /* send reset */
  143. dev_dbg(&info->i2c_dev->dev, "Sending reset cmd\n");
  144. ret = i2c_master_send(info->i2c_dev, rset_cmd, count);
  145. if (ret == count) {
  146. dev_info(&info->i2c_dev->dev,
  147. "nfc_en polarity : active %s\n",
  148. (polarity == 0 ? "low" : "high"));
  149. goto out;
  150. }
  151. }
  152. }
  153. dev_err(&info->i2c_dev->dev,
  154. "Could not detect nfc_en polarity, fallback to active high\n");
  155. out:
  156. gpio_set_value(info->gpio_en, !info->en_polarity);
  157. }
  158. static int pn544_hci_enable(struct pn544_hci_info *info, int mode)
  159. {
  160. pr_info(DRIVER_DESC ": %s\n", __func__);
  161. gpio_set_value(info->gpio_fw, 0);
  162. gpio_set_value(info->gpio_en, info->en_polarity);
  163. usleep_range(10000, 15000);
  164. return 0;
  165. }
  166. static void pn544_hci_disable(struct pn544_hci_info *info)
  167. {
  168. pr_info(DRIVER_DESC ": %s\n", __func__);
  169. gpio_set_value(info->gpio_fw, 0);
  170. gpio_set_value(info->gpio_en, !info->en_polarity);
  171. usleep_range(10000, 15000);
  172. gpio_set_value(info->gpio_en, info->en_polarity);
  173. usleep_range(10000, 15000);
  174. gpio_set_value(info->gpio_en, !info->en_polarity);
  175. usleep_range(10000, 15000);
  176. }
  177. static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len)
  178. {
  179. int r;
  180. usleep_range(3000, 6000);
  181. r = i2c_master_send(client, buf, len);
  182. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  183. usleep_range(6000, 10000);
  184. r = i2c_master_send(client, buf, len);
  185. }
  186. if (r >= 0 && r != len)
  187. r = -EREMOTEIO;
  188. return r;
  189. }
  190. static int check_crc(u8 *buf, int buflen)
  191. {
  192. int len;
  193. u16 crc;
  194. len = buf[0] + 1;
  195. crc = crc_ccitt(0xffff, buf, len - 2);
  196. crc = ~crc;
  197. if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
  198. pr_err(PN544_HCI_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n",
  199. crc, buf[len - 1], buf[len - 2]);
  200. pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
  201. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  202. 16, 2, buf, buflen, false);
  203. return -EPERM;
  204. }
  205. return 0;
  206. }
  207. /*
  208. * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
  209. * that i2c bus will be flushed and that next read will start on a new frame.
  210. * returned skb contains only LLC header and payload.
  211. * returns:
  212. * -EREMOTEIO : i2c read error (fatal)
  213. * -EBADMSG : frame was incorrect and discarded
  214. * -ENOMEM : cannot allocate skb, frame dropped
  215. */
  216. static int pn544_hci_i2c_read(struct i2c_client *client, struct sk_buff **skb)
  217. {
  218. int r;
  219. u8 len;
  220. u8 tmp[PN544_HCI_LLC_MAX_SIZE - 1];
  221. r = i2c_master_recv(client, &len, 1);
  222. if (r != 1) {
  223. dev_err(&client->dev, "cannot read len byte\n");
  224. return -EREMOTEIO;
  225. }
  226. if ((len < (PN544_HCI_LLC_MIN_SIZE - 1)) ||
  227. (len > (PN544_HCI_LLC_MAX_SIZE - 1))) {
  228. dev_err(&client->dev, "invalid len byte\n");
  229. r = -EBADMSG;
  230. goto flush;
  231. }
  232. *skb = alloc_skb(1 + len, GFP_KERNEL);
  233. if (*skb == NULL) {
  234. r = -ENOMEM;
  235. goto flush;
  236. }
  237. *skb_put(*skb, 1) = len;
  238. r = i2c_master_recv(client, skb_put(*skb, len), len);
  239. if (r != len) {
  240. kfree_skb(*skb);
  241. return -EREMOTEIO;
  242. }
  243. r = check_crc((*skb)->data, (*skb)->len);
  244. if (r != 0) {
  245. kfree_skb(*skb);
  246. r = -EBADMSG;
  247. goto flush;
  248. }
  249. skb_pull(*skb, 1);
  250. skb_trim(*skb, (*skb)->len - 2);
  251. usleep_range(3000, 6000);
  252. return 0;
  253. flush:
  254. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  255. r = -EREMOTEIO;
  256. usleep_range(3000, 6000);
  257. return r;
  258. }
  259. /*
  260. * Reads an shdlc frame from the chip. This is not as straightforward as it
  261. * seems. There are cases where we could loose the frame start synchronization.
  262. * The frame format is len-data-crc, and corruption can occur anywhere while
  263. * transiting on i2c bus, such that we could read an invalid len.
  264. * In order to recover synchronization with the next frame, we must be sure
  265. * to read the real amount of data without using the len byte. We do this by
  266. * assuming the following:
  267. * - the chip will always present only one single complete frame on the bus
  268. * before triggering the interrupt
  269. * - the chip will not present a new frame until we have completely read
  270. * the previous one (or until we have handled the interrupt).
  271. * The tricky case is when we read a corrupted len that is less than the real
  272. * len. We must detect this here in order to determine that we need to flush
  273. * the bus. This is the reason why we check the crc here.
  274. */
  275. static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
  276. {
  277. struct pn544_hci_info *info = dev_id;
  278. struct i2c_client *client = info->i2c_dev;
  279. struct sk_buff *skb = NULL;
  280. int r;
  281. BUG_ON(!info);
  282. BUG_ON(irq != info->i2c_dev->irq);
  283. dev_dbg(&client->dev, "IRQ\n");
  284. if (info->hard_fault != 0)
  285. return IRQ_HANDLED;
  286. r = pn544_hci_i2c_read(client, &skb);
  287. if (r == -EREMOTEIO) {
  288. info->hard_fault = r;
  289. nfc_hci_recv_frame(info->hdev, NULL);
  290. return IRQ_HANDLED;
  291. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  292. return IRQ_HANDLED;
  293. }
  294. nfc_hci_recv_frame(info->hdev, skb);
  295. return IRQ_HANDLED;
  296. }
  297. static int pn544_hci_open(struct nfc_hci_dev *hdev)
  298. {
  299. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  300. int r = 0;
  301. mutex_lock(&info->info_lock);
  302. if (info->state != PN544_ST_COLD) {
  303. r = -EBUSY;
  304. goto out;
  305. }
  306. r = pn544_hci_enable(info, HCI_MODE);
  307. if (r == 0)
  308. info->state = PN544_ST_READY;
  309. out:
  310. mutex_unlock(&info->info_lock);
  311. return r;
  312. }
  313. static void pn544_hci_close(struct nfc_hci_dev *hdev)
  314. {
  315. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  316. mutex_lock(&info->info_lock);
  317. if (info->state == PN544_ST_COLD)
  318. goto out;
  319. pn544_hci_disable(info);
  320. info->state = PN544_ST_COLD;
  321. out:
  322. mutex_unlock(&info->info_lock);
  323. }
  324. static int pn544_hci_ready(struct nfc_hci_dev *hdev)
  325. {
  326. struct sk_buff *skb;
  327. static struct hw_config {
  328. u8 adr[2];
  329. u8 value;
  330. } hw_config[] = {
  331. {{0x9f, 0x9a}, 0x00},
  332. {{0x98, 0x10}, 0xbc},
  333. {{0x9e, 0x71}, 0x00},
  334. {{0x98, 0x09}, 0x00},
  335. {{0x9e, 0xb4}, 0x00},
  336. {{0x9e, 0xd9}, 0xff},
  337. {{0x9e, 0xda}, 0xff},
  338. {{0x9e, 0xdb}, 0x23},
  339. {{0x9e, 0xdc}, 0x21},
  340. {{0x9e, 0xdd}, 0x22},
  341. {{0x9e, 0xde}, 0x24},
  342. {{0x9c, 0x01}, 0x08},
  343. {{0x9e, 0xaa}, 0x01},
  344. {{0x9b, 0xd1}, 0x0d},
  345. {{0x9b, 0xd2}, 0x24},
  346. {{0x9b, 0xd3}, 0x0a},
  347. {{0x9b, 0xd4}, 0x22},
  348. {{0x9b, 0xd5}, 0x08},
  349. {{0x9b, 0xd6}, 0x1e},
  350. {{0x9b, 0xdd}, 0x1c},
  351. {{0x9b, 0x84}, 0x13},
  352. {{0x99, 0x81}, 0x7f},
  353. {{0x99, 0x31}, 0x70},
  354. {{0x98, 0x00}, 0x3f},
  355. {{0x9f, 0x09}, 0x00},
  356. {{0x9f, 0x0a}, 0x05},
  357. {{0x9e, 0xd1}, 0xa1},
  358. {{0x99, 0x23}, 0x00},
  359. {{0x9e, 0x74}, 0x80},
  360. {{0x9f, 0x28}, 0x10},
  361. {{0x9f, 0x35}, 0x14},
  362. {{0x9f, 0x36}, 0x60},
  363. {{0x9c, 0x31}, 0x00},
  364. {{0x9c, 0x32}, 0xc8},
  365. {{0x9c, 0x19}, 0x40},
  366. {{0x9c, 0x1a}, 0x40},
  367. {{0x9c, 0x0c}, 0x00},
  368. {{0x9c, 0x0d}, 0x00},
  369. {{0x9c, 0x12}, 0x00},
  370. {{0x9c, 0x13}, 0x00},
  371. {{0x98, 0xa2}, 0x0e},
  372. {{0x98, 0x93}, 0x40},
  373. {{0x98, 0x7d}, 0x02},
  374. {{0x98, 0x7e}, 0x00},
  375. {{0x9f, 0xc8}, 0x01},
  376. };
  377. struct hw_config *p = hw_config;
  378. int count = ARRAY_SIZE(hw_config);
  379. struct sk_buff *res_skb;
  380. u8 param[4];
  381. int r;
  382. param[0] = 0;
  383. while (count--) {
  384. param[1] = p->adr[0];
  385. param[2] = p->adr[1];
  386. param[3] = p->value;
  387. r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
  388. param, 4, &res_skb);
  389. if (r < 0)
  390. return r;
  391. if (res_skb->len != 1) {
  392. kfree_skb(res_skb);
  393. return -EPROTO;
  394. }
  395. if (res_skb->data[0] != p->value) {
  396. kfree_skb(res_skb);
  397. return -EIO;
  398. }
  399. kfree_skb(res_skb);
  400. p++;
  401. }
  402. param[0] = NFC_HCI_UICC_HOST_ID;
  403. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  404. NFC_HCI_ADMIN_WHITELIST, param, 1);
  405. if (r < 0)
  406. return r;
  407. param[0] = 0x3d;
  408. r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
  409. PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
  410. if (r < 0)
  411. return r;
  412. param[0] = 0x0;
  413. r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
  414. PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
  415. if (r < 0)
  416. return r;
  417. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  418. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  419. if (r < 0)
  420. return r;
  421. param[0] = 0x1;
  422. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  423. PN544_PL_NFCT_DEACTIVATED, param, 1);
  424. if (r < 0)
  425. return r;
  426. param[0] = 0x0;
  427. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  428. PN544_PL_RDPHASES, param, 1);
  429. if (r < 0)
  430. return r;
  431. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  432. PN544_ID_MGMT_FULL_VERSION_SW, &skb);
  433. if (r < 0)
  434. return r;
  435. if (skb->len != FULL_VERSION_LEN) {
  436. kfree_skb(skb);
  437. return -EINVAL;
  438. }
  439. print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
  440. DUMP_PREFIX_NONE, 16, 1,
  441. skb->data, FULL_VERSION_LEN, false);
  442. kfree_skb(skb);
  443. return 0;
  444. }
  445. static void pn544_hci_add_len_crc(struct sk_buff *skb)
  446. {
  447. u16 crc;
  448. int len;
  449. len = skb->len + 2;
  450. *skb_push(skb, 1) = len;
  451. crc = crc_ccitt(0xffff, skb->data, skb->len);
  452. crc = ~crc;
  453. *skb_put(skb, 1) = crc & 0xff;
  454. *skb_put(skb, 1) = crc >> 8;
  455. }
  456. static void pn544_hci_remove_len_crc(struct sk_buff *skb)
  457. {
  458. skb_pull(skb, PN544_FRAME_HEADROOM);
  459. skb_trim(skb, PN544_FRAME_TAILROOM);
  460. }
  461. static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  462. {
  463. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  464. struct i2c_client *client = info->i2c_dev;
  465. int r;
  466. if (info->hard_fault != 0)
  467. return info->hard_fault;
  468. pn544_hci_add_len_crc(skb);
  469. r = pn544_hci_i2c_write(client, skb->data, skb->len);
  470. pn544_hci_remove_len_crc(skb);
  471. return r;
  472. }
  473. static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
  474. u32 im_protocols, u32 tm_protocols)
  475. {
  476. u8 phases = 0;
  477. int r;
  478. u8 duration[2];
  479. u8 activated;
  480. pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
  481. __func__, im_protocols, tm_protocols);
  482. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  483. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  484. if (r < 0)
  485. return r;
  486. duration[0] = 0x18;
  487. duration[1] = 0x6a;
  488. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  489. PN544_PL_EMULATION, duration, 2);
  490. if (r < 0)
  491. return r;
  492. activated = 0;
  493. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  494. PN544_PL_NFCT_DEACTIVATED, &activated, 1);
  495. if (r < 0)
  496. return r;
  497. if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
  498. NFC_PROTO_JEWEL_MASK))
  499. phases |= 1; /* Type A */
  500. if (im_protocols & NFC_PROTO_FELICA_MASK) {
  501. phases |= (1 << 2); /* Type F 212 */
  502. phases |= (1 << 3); /* Type F 424 */
  503. }
  504. phases |= (1 << 5); /* NFC active */
  505. r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
  506. PN544_PL_RDPHASES, &phases, 1);
  507. if (r < 0)
  508. return r;
  509. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  510. NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
  511. if (r < 0)
  512. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  513. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  514. return r;
  515. }
  516. static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  517. struct nfc_target *target)
  518. {
  519. switch (gate) {
  520. case PN544_RF_READER_F_GATE:
  521. target->supported_protocols = NFC_PROTO_FELICA_MASK;
  522. break;
  523. case PN544_RF_READER_JEWEL_GATE:
  524. target->supported_protocols = NFC_PROTO_JEWEL_MASK;
  525. target->sens_res = 0x0c00;
  526. break;
  527. default:
  528. return -EPROTO;
  529. }
  530. return 0;
  531. }
  532. static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
  533. u8 gate,
  534. struct nfc_target *target)
  535. {
  536. struct sk_buff *uid_skb;
  537. int r = 0;
  538. if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  539. if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
  540. target->nfcid1_len != 10)
  541. return -EPROTO;
  542. r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  543. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  544. target->nfcid1, target->nfcid1_len, NULL);
  545. } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
  546. r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
  547. PN544_FELICA_ID, &uid_skb);
  548. if (r < 0)
  549. return r;
  550. if (uid_skb->len != 8) {
  551. kfree_skb(uid_skb);
  552. return -EPROTO;
  553. }
  554. r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
  555. PN544_RF_READER_CMD_ACTIVATE_NEXT,
  556. uid_skb->data, uid_skb->len, NULL);
  557. kfree_skb(uid_skb);
  558. } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
  559. /*
  560. * TODO: maybe other ISO 14443 require some kind of continue
  561. * activation, but for now we've seen only this one below.
  562. */
  563. if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
  564. r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
  565. PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
  566. NULL, 0, NULL);
  567. }
  568. return r;
  569. }
  570. #define PN544_CB_TYPE_READER_F 1
  571. static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  572. int err)
  573. {
  574. struct pn544_hci_info *info = context;
  575. switch (info->async_cb_type) {
  576. case PN544_CB_TYPE_READER_F:
  577. if (err == 0)
  578. skb_pull(skb, 1);
  579. info->async_cb(info->async_cb_context, skb, err);
  580. break;
  581. default:
  582. if (err == 0)
  583. kfree_skb(skb);
  584. break;
  585. }
  586. }
  587. #define MIFARE_CMD_AUTH_KEY_A 0x60
  588. #define MIFARE_CMD_AUTH_KEY_B 0x61
  589. #define MIFARE_CMD_HEADER 2
  590. #define MIFARE_UID_LEN 4
  591. #define MIFARE_KEY_LEN 6
  592. #define MIFARE_CMD_LEN 12
  593. /*
  594. * Returns:
  595. * <= 0: driver handled the data exchange
  596. * 1: driver doesn't especially handle, please do standard processing
  597. */
  598. static int pn544_hci_data_exchange(struct nfc_hci_dev *hdev,
  599. struct nfc_target *target,
  600. struct sk_buff *skb, data_exchange_cb_t cb,
  601. void *cb_context)
  602. {
  603. struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
  604. pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
  605. target->hci_reader_gate);
  606. switch (target->hci_reader_gate) {
  607. case NFC_HCI_RF_READER_A_GATE:
  608. if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
  609. /*
  610. * It seems that pn544 is inverting key and UID for
  611. * MIFARE authentication commands.
  612. */
  613. if (skb->len == MIFARE_CMD_LEN &&
  614. (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
  615. skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
  616. u8 uid[MIFARE_UID_LEN];
  617. u8 *data = skb->data + MIFARE_CMD_HEADER;
  618. memcpy(uid, data + MIFARE_KEY_LEN,
  619. MIFARE_UID_LEN);
  620. memmove(data + MIFARE_UID_LEN, data,
  621. MIFARE_KEY_LEN);
  622. memcpy(data, uid, MIFARE_UID_LEN);
  623. }
  624. return nfc_hci_send_cmd_async(hdev,
  625. target->hci_reader_gate,
  626. PN544_MIFARE_CMD,
  627. skb->data, skb->len,
  628. cb, cb_context);
  629. } else
  630. return 1;
  631. case PN544_RF_READER_F_GATE:
  632. *skb_push(skb, 1) = 0;
  633. *skb_push(skb, 1) = 0;
  634. info->async_cb_type = PN544_CB_TYPE_READER_F;
  635. info->async_cb = cb;
  636. info->async_cb_context = cb_context;
  637. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  638. PN544_FELICA_RAW, skb->data,
  639. skb->len,
  640. pn544_hci_data_exchange_cb, info);
  641. case PN544_RF_READER_JEWEL_GATE:
  642. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  643. PN544_JEWEL_RAW_CMD, skb->data,
  644. skb->len, cb, cb_context);
  645. default:
  646. return 1;
  647. }
  648. }
  649. static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
  650. struct nfc_target *target)
  651. {
  652. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  653. PN544_RF_READER_CMD_PRESENCE_CHECK,
  654. NULL, 0, NULL);
  655. }
  656. static struct nfc_hci_ops pn544_hci_ops = {
  657. .open = pn544_hci_open,
  658. .close = pn544_hci_close,
  659. .hci_ready = pn544_hci_ready,
  660. .xmit = pn544_hci_xmit,
  661. .start_poll = pn544_hci_start_poll,
  662. .target_from_gate = pn544_hci_target_from_gate,
  663. .complete_target_discovered = pn544_hci_complete_target_discovered,
  664. .data_exchange = pn544_hci_data_exchange,
  665. .check_presence = pn544_hci_check_presence,
  666. };
  667. static int __devinit pn544_hci_probe(struct i2c_client *client,
  668. const struct i2c_device_id *id)
  669. {
  670. struct pn544_hci_info *info;
  671. struct pn544_nfc_platform_data *pdata;
  672. int r = 0;
  673. u32 protocols;
  674. struct nfc_hci_init_data init_data;
  675. dev_dbg(&client->dev, "%s\n", __func__);
  676. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  677. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  678. dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
  679. return -ENODEV;
  680. }
  681. info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
  682. if (!info) {
  683. dev_err(&client->dev,
  684. "Cannot allocate memory for pn544_hci_info.\n");
  685. r = -ENOMEM;
  686. goto err_info_alloc;
  687. }
  688. info->i2c_dev = client;
  689. info->state = PN544_ST_COLD;
  690. mutex_init(&info->info_lock);
  691. i2c_set_clientdata(client, info);
  692. pdata = client->dev.platform_data;
  693. if (pdata == NULL) {
  694. dev_err(&client->dev, "No platform data\n");
  695. r = -EINVAL;
  696. goto err_pdata;
  697. }
  698. if (pdata->request_resources == NULL) {
  699. dev_err(&client->dev, "request_resources() missing\n");
  700. r = -EINVAL;
  701. goto err_pdata;
  702. }
  703. r = pdata->request_resources(client);
  704. if (r) {
  705. dev_err(&client->dev, "Cannot get platform resources\n");
  706. goto err_pdata;
  707. }
  708. info->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
  709. info->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
  710. info->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
  711. pn544_hci_platform_init(info);
  712. r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn,
  713. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  714. PN544_HCI_DRIVER_NAME, info);
  715. if (r < 0) {
  716. dev_err(&client->dev, "Unable to register IRQ handler\n");
  717. goto err_rti;
  718. }
  719. init_data.gate_count = ARRAY_SIZE(pn544_gates);
  720. memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
  721. /*
  722. * TODO: Session id must include the driver name + some bus addr
  723. * persistent info to discriminate 2 identical chips
  724. */
  725. strcpy(init_data.session_id, "ID544HCI");
  726. protocols = NFC_PROTO_JEWEL_MASK |
  727. NFC_PROTO_MIFARE_MASK |
  728. NFC_PROTO_FELICA_MASK |
  729. NFC_PROTO_ISO14443_MASK |
  730. NFC_PROTO_ISO14443_B_MASK |
  731. NFC_PROTO_NFC_DEP_MASK;
  732. info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
  733. protocols, LLC_SHDLC_NAME,
  734. PN544_FRAME_HEADROOM +
  735. PN544_CMDS_HEADROOM,
  736. PN544_FRAME_TAILROOM,
  737. PN544_HCI_LLC_MAX_PAYLOAD);
  738. if (!info->hdev) {
  739. dev_err(&client->dev, "Cannot allocate nfc hdev.\n");
  740. r = -ENOMEM;
  741. goto err_alloc_hdev;
  742. }
  743. nfc_hci_set_clientdata(info->hdev, info);
  744. r = nfc_hci_register_device(info->hdev);
  745. if (r)
  746. goto err_regdev;
  747. return 0;
  748. err_regdev:
  749. nfc_hci_free_device(info->hdev);
  750. err_alloc_hdev:
  751. free_irq(client->irq, info);
  752. err_rti:
  753. if (pdata->free_resources != NULL)
  754. pdata->free_resources();
  755. err_pdata:
  756. kfree(info);
  757. err_info_alloc:
  758. return r;
  759. }
  760. static __devexit int pn544_hci_remove(struct i2c_client *client)
  761. {
  762. struct pn544_hci_info *info = i2c_get_clientdata(client);
  763. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  764. dev_dbg(&client->dev, "%s\n", __func__);
  765. nfc_hci_free_device(info->hdev);
  766. if (info->state != PN544_ST_COLD) {
  767. if (pdata->disable)
  768. pdata->disable();
  769. }
  770. free_irq(client->irq, info);
  771. if (pdata->free_resources)
  772. pdata->free_resources();
  773. kfree(info);
  774. return 0;
  775. }
  776. static struct i2c_driver pn544_hci_driver = {
  777. .driver = {
  778. .name = PN544_HCI_DRIVER_NAME,
  779. },
  780. .probe = pn544_hci_probe,
  781. .id_table = pn544_hci_id_table,
  782. .remove = __devexit_p(pn544_hci_remove),
  783. };
  784. static int __init pn544_hci_init(void)
  785. {
  786. int r;
  787. pr_debug(DRIVER_DESC ": %s\n", __func__);
  788. r = i2c_add_driver(&pn544_hci_driver);
  789. if (r) {
  790. pr_err(PN544_HCI_DRIVER_NAME ": driver registration failed\n");
  791. return r;
  792. }
  793. return 0;
  794. }
  795. static void __exit pn544_hci_exit(void)
  796. {
  797. i2c_del_driver(&pn544_hci_driver);
  798. }
  799. module_init(pn544_hci_init);
  800. module_exit(pn544_hci_exit);
  801. MODULE_LICENSE("GPL");
  802. MODULE_DESCRIPTION(DRIVER_DESC);