22 #include <linux/module.h>
24 #include <linux/slab.h>
28 #include <linux/i2c.h>
36 #define DRIVER_DESC "HCI NFC driver for PN544"
38 #define PN544_HCI_DRIVER_NAME "pn544_hci"
41 #define PN544_HCI_RESETVEN_TIME 30
54 #define PN544_HCI_LLC_LEN 1
55 #define PN544_HCI_LLC_CRC 2
56 #define PN544_HCI_LLC_LEN_CRC (PN544_HCI_LLC_LEN + PN544_HCI_LLC_CRC)
57 #define PN544_HCI_LLC_MIN_SIZE (1 + PN544_HCI_LLC_LEN_CRC)
58 #define PN544_HCI_LLC_MAX_PAYLOAD 29
59 #define PN544_HCI_LLC_MAX_SIZE (PN544_HCI_LLC_LEN_CRC + 1 + \
60 PN544_HCI_LLC_MAX_PAYLOAD)
68 #define FULL_VERSION_LEN 11
71 #define PN544_WRITE 0x3f
76 #define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
77 #define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
78 #define PN544_MIFARE_CMD 0x21
81 #define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
82 #define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
85 #define PN544_ID_MGMT_FULL_VERSION_SW 0x10
87 #define PN544_RF_READER_ISO15693_GATE 0x12
89 #define PN544_RF_READER_F_GATE 0x14
90 #define PN544_FELICA_ID 0x04
91 #define PN544_FELICA_RAW 0x20
93 #define PN544_RF_READER_JEWEL_GATE 0x15
94 #define PN544_JEWEL_RAW_CMD 0x23
96 #define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
97 #define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
99 #define PN544_SYS_MGMT_GATE 0x90
100 #define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
102 #define PN544_POLLING_LOOP_MGMT_GATE 0x94
103 #define PN544_PL_RDPHASES 0x06
104 #define PN544_PL_EMULATION 0x07
105 #define PN544_PL_NFCT_DEACTIVATED 0x09
107 #define PN544_SWP_MGMT_GATE 0xA0
109 #define PN544_NFC_WI_MGMT_GATE 0xA1
130 #define PN544_CMDS_HEADROOM 2
131 #define PN544_FRAME_HEADROOM 1
132 #define PN544_FRAME_TAILROOM 2
159 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
160 int count =
sizeof(rset_cmd);
168 for (polarity = 0; polarity < 2; polarity++) {
185 "nfc_en polarity : active %s\n",
186 (polarity == 0 ?
"low" :
"high"));
193 "Could not detect nfc_en polarity, fallback to active high\n");
248 static int check_crc(
u8 *buf,
int buflen)
257 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
259 crc, buf[len - 1], buf[len - 2]);
263 16, 2, buf, buflen,
false);
286 dev_err(&client->
dev,
"cannot read len byte\n");
311 r = check_crc((*skb)->data, (*skb)->len);
357 if (!info || irq != info->
i2c_dev->irq) {
368 r = pn544_hci_i2c_read(client, &skb);
384 static int pn544_hci_open(
struct nfc_hci_dev *hdev)
396 r = pn544_hci_enable(info,
HCI_MODE);
406 static void pn544_hci_close(
struct nfc_hci_dev *hdev)
415 pn544_hci_disable(info);
423 static int pn544_hci_ready(
struct nfc_hci_dev *hdev)
426 static struct hw_config {
430 {{0x9f, 0x9a}, 0x00},
432 {{0x98, 0x10}, 0xbc},
434 {{0x9e, 0x71}, 0x00},
436 {{0x98, 0x09}, 0x00},
438 {{0x9e, 0xb4}, 0x00},
440 {{0x9e, 0xd9}, 0xff},
441 {{0x9e, 0xda}, 0xff},
442 {{0x9e, 0xdb}, 0x23},
443 {{0x9e, 0xdc}, 0x21},
444 {{0x9e, 0xdd}, 0x22},
445 {{0x9e, 0xde}, 0x24},
447 {{0x9c, 0x01}, 0x08},
449 {{0x9e, 0xaa}, 0x01},
451 {{0x9b, 0xd1}, 0x0d},
452 {{0x9b, 0xd2}, 0x24},
453 {{0x9b, 0xd3}, 0x0a},
454 {{0x9b, 0xd4}, 0x22},
455 {{0x9b, 0xd5}, 0x08},
456 {{0x9b, 0xd6}, 0x1e},
457 {{0x9b, 0xdd}, 0x1c},
459 {{0x9b, 0x84}, 0x13},
460 {{0x99, 0x81}, 0x7f},
461 {{0x99, 0x31}, 0x70},
463 {{0x98, 0x00}, 0x3f},
465 {{0x9f, 0x09}, 0x00},
467 {{0x9f, 0x0a}, 0x05},
469 {{0x9e, 0xd1}, 0xa1},
470 {{0x99, 0x23}, 0x00},
472 {{0x9e, 0x74}, 0x80},
474 {{0x9f, 0x28}, 0x10},
476 {{0x9f, 0x35}, 0x14},
478 {{0x9f, 0x36}, 0x60},
480 {{0x9c, 0x31}, 0x00},
482 {{0x9c, 0x32}, 0xc8},
484 {{0x9c, 0x19}, 0x40},
486 {{0x9c, 0x1a}, 0x40},
488 {{0x9c, 0x0c}, 0x00},
490 {{0x9c, 0x0d}, 0x00},
492 {{0x9c, 0x12}, 0x00},
494 {{0x9c, 0x13}, 0x00},
496 {{0x98, 0xa2}, 0x0e},
498 {{0x98, 0x93}, 0x40},
500 {{0x98, 0x7d}, 0x02},
501 {{0x98, 0x7e}, 0x00},
502 {{0x9f, 0xc8}, 0x01},
504 struct hw_config *
p = hw_config;
512 param[1] = p->adr[0];
513 param[2] = p->adr[1];
521 if (res_skb->
len != 1) {
526 if (res_skb->
data[0] != p->value) {
581 print_hex_dump(
KERN_DEBUG,
"FULL VERSION SOFTWARE INFO: ",
590 static void pn544_hci_add_len_crc(
struct sk_buff *skb)
604 static void pn544_hci_remove_len_crc(
struct sk_buff *skb)
619 pn544_hci_add_len_crc(skb);
620 r = pn544_hci_i2c_write(client, skb->
data, skb->
len);
621 pn544_hci_remove_len_crc(skb);
626 static int pn544_hci_start_poll(
struct nfc_hci_dev *hdev,
627 u32 im_protocols,
u32 tm_protocols)
635 __func__, im_protocols, tm_protocols);
679 static int pn544_hci_target_from_gate(
struct nfc_hci_dev *hdev,
u8 gate,
697 static int pn544_hci_complete_target_discovered(
struct nfc_hci_dev *hdev,
718 if (uid_skb->
len != 8) {
741 #define PN544_CB_TYPE_READER_F 1
743 static void pn544_hci_data_exchange_cb(
void *
context,
struct sk_buff *skb,
761 #define MIFARE_CMD_AUTH_KEY_A 0x60
762 #define MIFARE_CMD_AUTH_KEY_B 0x61
763 #define MIFARE_CMD_HEADER 2
764 #define MIFARE_UID_LEN 4
765 #define MIFARE_KEY_LEN 6
766 #define MIFARE_CMD_LEN 12
772 static int pn544_hci_data_exchange(
struct nfc_hci_dev *hdev,
820 pn544_hci_data_exchange_cb, info);
824 skb->
len, cb, cb_context);
830 static int pn544_hci_check_presence(
struct nfc_hci_dev *hdev,
839 .open = pn544_hci_open,
840 .close = pn544_hci_close,
841 .hci_ready = pn544_hci_ready,
842 .xmit = pn544_hci_xmit,
843 .start_poll = pn544_hci_start_poll,
844 .target_from_gate = pn544_hci_target_from_gate,
845 .complete_target_discovered = pn544_hci_complete_target_discovered,
846 .data_exchange = pn544_hci_data_exchange,
847 .check_presence = pn544_hci_check_presence,
854 struct pn544_nfc_platform_data *
pdata;
870 "Cannot allocate memory for pn544_hci_info.\n");
878 i2c_set_clientdata(client, info);
880 pdata = client->
dev.platform_data;
887 if (pdata->request_resources ==
NULL) {
888 dev_err(&client->
dev,
"request_resources() missing\n");
893 r = pdata->request_resources(client);
895 dev_err(&client->
dev,
"Cannot get platform resources\n");
899 info->
gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
900 info->
gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
901 info->
gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
903 pn544_hci_platform_init(info);
909 dev_err(&client->
dev,
"Unable to register IRQ handler\n");
913 init_data.gate_count =
ARRAY_SIZE(pn544_gates);
915 memcpy(init_data.gates, pn544_gates,
sizeof(pn544_gates));
921 strcpy(init_data.session_id,
"ID544HCI");
925 NFC_PROTO_FELICA_MASK |
937 dev_err(&client->
dev,
"Cannot allocate nfc hdev.\n");
957 if (pdata->free_resources !=
NULL)
958 pdata->free_resources();
970 struct pn544_nfc_platform_data *pdata = client->
dev.platform_data;
982 if (pdata->free_resources)
983 pdata->free_resources();
994 .probe = pn544_hci_probe,
995 .id_table = pn544_hci_id_table,
999 static int __init pn544_hci_init(
void)
1005 r = i2c_add_driver(&pn544_hci_driver);
1014 static void __exit pn544_hci_exit(
void)