24 #include <linux/device.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
30 #include <linux/netdevice.h>
31 #include <net/nfc/nfc.h>
35 #define PN533_VENDOR_ID 0x4CC
36 #define PN533_PRODUCT_ID 0x2533
38 #define SCM_VENDOR_ID 0x4E6
39 #define SCL3711_PRODUCT_ID 0x5591
41 #define SONY_VENDOR_ID 0x054c
42 #define PASORI_PRODUCT_ID 0x02e1
44 #define PN533_QUIRKS_TYPE_A BIT(0)
45 #define PN533_QUIRKS_TYPE_F BIT(1)
46 #define PN533_QUIRKS_DEP BIT(2)
47 #define PN533_QUIRKS_RAW_EXCHANGE BIT(3)
49 #define PN533_DEVICE_STD 0x1
50 #define PN533_DEVICE_PASORI 0x2
52 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
53 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
54 NFC_PROTO_NFC_DEP_MASK |\
55 NFC_PROTO_ISO14443_B_MASK)
57 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
58 NFC_PROTO_MIFARE_MASK | \
59 NFC_PROTO_FELICA_MASK | \
60 NFC_PROTO_ISO14443_MASK | \
61 NFC_PROTO_NFC_DEP_MASK)
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
74 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
84 #define PN533_LISTEN_TIME 2
87 #define PN533_FRAME_TAIL_SIZE 2
88 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
89 PN533_FRAME_TAIL_SIZE)
90 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
91 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
92 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
95 #define PN533_SOF 0x00FF
98 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
99 #define PN533_DIR_OUT 0xD4
100 #define PN533_DIR_IN 0xD5
103 #define PN533_FRAME_CMD(f) (f->data[1])
104 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
105 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
107 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
108 #define PN533_CMD_RF_CONFIGURATION 0x32
109 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
110 #define PN533_CMD_IN_COMM_THRU 0x42
111 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
112 #define PN533_CMD_IN_ATR 0x50
113 #define PN533_CMD_IN_RELEASE 0x52
114 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
116 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
117 #define PN533_CMD_TG_GET_DATA 0x86
118 #define PN533_CMD_TG_SET_DATA 0x8e
120 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
123 #define PN533_CMD_RET_MASK 0x3F
124 #define PN533_CMD_MI_MASK 0x40
125 #define PN533_CMD_RET_SUCCESS 0x00
128 #define PN533_STATUS_TARGET_RELEASED 0x29
146 #define PN533_CFGITEM_TIMING 0x02
147 #define PN533_CFGITEM_MAX_RETRIES 0x05
148 #define PN533_CFGITEM_PASORI 0x82
150 #define PN533_CONFIG_TIMING_102 0xb
151 #define PN533_CONFIG_TIMING_204 0xc
152 #define PN533_CONFIG_TIMING_409 0xd
153 #define PN533_CONFIG_TIMING_819 0xe
155 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
156 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
173 #define PN533_FELICA_OPC_SENSF_REQ 0
174 #define PN533_FELICA_OPC_SENSF_RES 1
176 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
177 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
178 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
179 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
182 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
183 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
184 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
210 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
233 .initiator_data.felica = {
246 .initiator_data.felica = {
266 .initiator_data.type_b = {
321 #define PN533_INIT_TARGET_PASSIVE 0x1
322 #define PN533_INIT_TARGET_DEP 0x2
324 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
325 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
326 #define PN533_INIT_TARGET_RESP_DEP 0x4
411 static inline u8 pn533_checksum(
u8 value)
425 return pn533_checksum(sum);
474 checksum = pn533_checksum(frame->
datalen);
478 checksum = pn533_data_checksum(frame->
data, frame->
datalen);
485 static bool pn533_rx_frame_is_ack(
struct pn533_frame *frame)
496 static bool pn533_rx_frame_is_cmd_response(
struct pn533_frame *frame,
u8 cmd)
522 static void pn533_recv_response(
struct urb *
urb)
524 struct pn533 *dev = urb->context;
529 switch (urb->status) {
537 " status: %d", urb->status);
547 in_frame = dev->
in_urb->transfer_buffer;
549 if (!pn533_rx_frame_is_valid(in_frame)) {
555 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->
cmd)) {
557 "response to the last command");
570 static int pn533_submit_urb_for_response(
struct pn533 *dev,
gfp_t flags)
572 dev->
in_urb->complete = pn533_recv_response;
577 static void pn533_recv_ack(
struct urb *urb)
579 struct pn533 *dev = urb->context;
583 switch (urb->status) {
591 " status: %d", urb->status);
601 in_frame = dev->
in_urb->transfer_buffer;
603 if (!pn533_rx_frame_is_ack(in_frame)) {
611 rc = pn533_submit_urb_for_response(dev,
GFP_ATOMIC);
628 dev->
in_urb->complete = pn533_recv_ack;
648 static int __pn533_send_cmd_frame_async(
struct pn533 *dev,
664 dev->
out_urb->transfer_buffer = out_frame;
665 dev->
out_urb->transfer_buffer_length =
668 dev->
in_urb->transfer_buffer = in_frame;
669 dev->
in_urb->transfer_buffer_length = in_frame_len;
675 rc = pn533_submit_urb_for_ack(dev, flags);
712 static int pn533_send_cmd_frame_async(
struct pn533 *dev,
717 void *arg,
gfp_t flags)
727 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
728 in_frame_len, cmd_complete,
738 cmd = kzalloc(
sizeof(
struct pn533_cmd), flags);
744 INIT_LIST_HEAD(&cmd->
queue);
765 static int pn533_sync_cmd_complete(
struct pn533 *dev,
void *_arg,
775 arg->
rc = params_len;
782 static int pn533_send_cmd_frame_sync(
struct pn533 *dev,
792 init_completion(&arg.
done);
794 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
804 static void pn533_send_complete(
struct urb *urb)
806 struct pn533 *dev = urb->context;
810 switch (urb->status) {
818 " status: %d", urb->status);
834 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
835 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
836 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
838 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
839 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
841 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
842 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
844 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
845 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
846 #define PN533_TYPE_A_SEL_PROT_DEP 2
847 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
878 static int pn533_target_found_type_a(
struct nfc_target *nfc_tgt,
u8 *tgt_data,
885 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
921 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
922 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
936 static int pn533_target_found_felica(
struct nfc_target *nfc_tgt,
u8 *tgt_data,
943 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
986 static int pn533_target_found_jewel(
struct nfc_target *nfc_tgt,
u8 *tgt_data,
993 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1010 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1011 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1012 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1021 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1030 int target_data_len)
1045 static int pn533_target_found_type_b(
struct nfc_target *nfc_tgt,
u8 *tgt_data,
1052 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1066 static int pn533_target_found(
struct pn533 *dev,
1069 int target_data_len;
1085 rc = pn533_target_found_type_a(&nfc_tgt, resp->
target_data,
1090 rc = pn533_target_found_felica(&nfc_tgt, resp->
target_data,
1094 rc = pn533_target_found_jewel(&nfc_tgt, resp->
target_data,
1098 rc = pn533_target_found_type_b(&nfc_tgt, resp->
target_data,
1112 " have the desired protocol");
1126 static inline void pn533_poll_next_mod(
struct pn533 *dev)
1131 static void pn533_poll_reset_mod_list(
struct pn533 *dev)
1136 static void pn533_poll_add_mod(
struct pn533 *dev,
u8 mod_index)
1143 static void pn533_poll_create_mod_list(
struct pn533 *dev,
1144 u32 im_protocols,
u32 tm_protocols)
1146 pn533_poll_reset_mod_list(dev);
1154 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1169 static int pn533_start_poll_complete(
struct pn533 *dev,
void *arg,
1170 u8 *params,
int params_len)
1179 rc = pn533_target_found(dev, resp, params_len);
1183 pn533_poll_reset_mod_list(dev);
1191 static int pn533_init_target_frame(
struct pn533_frame *frame,
1192 u8 *gb,
size_t gb_len)
1196 u8 felica_params[18] = {0x1, 0xfe,
1197 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1198 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1200 u8 mifare_params[6] = {0x1, 0x1,
1215 memcpy(cmd->felica, felica_params, 18);
1219 memset(cmd->nfcid3, 0, 10);
1220 memcpy(cmd->nfcid3, cmd->felica, 8);
1223 memcpy(cmd->mifare, mifare_params, 6);
1227 memcpy(cmd->gb, gb, gb_len);
1236 pn533_tx_frame_finish(frame);
1243 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1244 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1245 static int pn533_tm_get_data_complete(
struct pn533 *dev,
void *arg,
1246 u8 *params,
int params_len)
1253 if (params_len < 0) {
1255 "Error %d when starting as a target",
1261 if (params_len > 0 && params[0] != 0) {
1277 static void pn533_wq_tg_get_data(
struct work_struct *work)
1282 size_t skb_resp_len;
1299 pn533_send_cmd_frame_async(dev, dev->
out_frame, in_frame,
1301 pn533_tm_get_data_complete,
1307 #define ATR_REQ_GB_OFFSET 17
1308 static int pn533_init_target_complete(
struct pn533 *dev,
void *arg,
1309 u8 *params,
int params_len)
1318 if (params_len < 0) {
1320 "Error %d when starting as a target",
1332 resp->
mode, params_len);
1346 comm_mode, gb, gb_len);
1349 "Error when signaling target activation");
1360 static void pn533_listen_mode_timer(
unsigned long data)
1371 pn533_poll_next_mod(dev);
1376 static int pn533_poll_complete(
struct pn533 *dev,
void *arg,
1377 u8 *params,
int params_len)
1384 if (params_len == -
ENOENT) {
1389 "Polling operation has been stopped");
1394 if (params_len < 0) {
1396 "Error %d when running poll", params_len);
1403 if (cur_mod->
len == 0) {
1406 return pn533_init_target_complete(dev, arg, params, params_len);
1408 rc = pn533_start_poll_complete(dev, arg, params, params_len);
1413 pn533_poll_next_mod(dev);
1420 pn533_poll_reset_mod_list(dev);
1425 static void pn533_build_poll_frame(
struct pn533 *dev,
1431 if (mod->
len == 0) {
1433 pn533_init_target_frame(frame, dev->
gb, dev->
gb_len);
1441 pn533_tx_frame_finish(frame);
1445 static int pn533_send_poll_frame(
struct pn533 *dev)
1452 pn533_build_poll_frame(dev, dev->
out_frame, cur_mod);
1463 static void pn533_wq_poll(
struct work_struct *work)
1472 "%s cancel_listen %d modulation len %d",
1480 rc = pn533_send_poll_frame(dev);
1491 u32 im_protocols,
u32 tm_protocols)
1493 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1496 "%s: im protocols 0x%x tm protocols 0x%x",
1497 __func__, im_protocols, tm_protocols);
1501 "Cannot poll with a target already activated");
1507 "Cannot poll while already being activated");
1518 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1522 return pn533_send_poll_frame(dev);
1525 static void pn533_stop_poll(
struct nfc_dev *nfc_dev)
1527 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1545 pn533_poll_reset_mod_list(dev);
1548 static int pn533_activate_target_nfcdep(
struct pn533 *dev)
1585 static int pn533_activate_target(
struct nfc_dev *nfc_dev,
1588 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1614 " the requested protocol %u", protocol);
1619 rc = pn533_activate_target_nfcdep(dev);
1622 " activating target with"
1623 " NFC_DEP protocol", rc);
1634 static void pn533_deactivate_target(
struct nfc_dev *nfc_dev,
1637 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1665 " command to the controller");
1679 static int pn533_in_dep_link_up_complete(
struct pn533 *dev,
void *arg,
1680 u8 *params,
int params_len)
1691 if (params_len == -
ENOENT) {
1696 if (params_len < 0) {
1698 "Error %d when bringing DEP link up",
1706 "The target does not support DEP");
1714 "Bringing DEP link up failed %d", rc);
1736 resp->
gt, target_gt_len);
1745 static int pn533_mod_to_baud(
struct pn533 *dev)
1759 #define PASSIVE_DATA_LEN 5
1760 static int pn533_dep_link_up(
struct nfc_dev *nfc_dev,
struct nfc_target *target,
1761 u8 comm_mode,
u8* gb,
size_t gb_len)
1763 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1773 "Cannot bring the DEP link up while polling");
1779 "There is already an active target");
1783 baud = pn533_mod_to_baud(dev);
1800 cmd->
active = !comm_mode;
1803 data_ptr = cmd->
data;
1810 if (gb !=
NULL && gb_len > 0) {
1812 memcpy(data_ptr, gb, gb_len);
1823 dev->
in_maxlen, pn533_in_dep_link_up_complete,
1831 static int pn533_dep_link_down(
struct nfc_dev *nfc_dev)
1833 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1835 pn533_poll_reset_mod_list(dev);
1850 static int pn533_build_tx_frame(
struct pn533 *dev,
struct sk_buff *
skb,
1868 if (target ==
true) {
1874 pn533_tx_frame_init(out_frame,
1883 pn533_tx_frame_init(out_frame,
1903 pn533_tx_frame_finish(out_frame);
1904 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1916 static struct sk_buff *pn533_build_response(
struct pn533 *dev)
1919 unsigned int skb_len = 0, tmp_len = 0;
1923 if (skb_queue_empty(&dev->
resp_q))
1926 if (skb_queue_len(&dev->
resp_q) == 1) {
1931 skb_queue_walk_safe(&dev->
resp_q, tmp, t)
1932 skb_len += tmp->
len;
1943 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1944 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1945 tmp_len += tmp->len;
1954 static int pn533_data_exchange_complete(
struct pn533 *dev,
void *_arg,
1955 u8 *params,
int params_len)
1968 if (params_len < 0) {
1978 " exchanging data", cmd_ret);
1985 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1993 skb = pn533_build_response(dev);
2003 dev_kfree_skb(skb_resp);
2009 static int pn533_transceive(
struct nfc_dev *nfc_dev,
2013 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2024 " there is no active target");
2029 rc = pn533_build_tx_frame(dev, skb,
true);
2057 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2058 pn533_data_exchange_complete, arg,
2062 " perform data_exchange", rc);
2077 static int pn533_tm_send_complete(
struct pn533 *dev,
void *arg,
2078 u8 *params,
int params_len)
2084 dev_kfree_skb(skb_out);
2086 if (params_len < 0) {
2088 "Error %d when sending data",
2094 if (params_len > 0 && params[0] != 0) {
2107 static int pn533_tm_send(
struct nfc_dev *nfc_dev,
struct sk_buff *skb)
2109 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2115 rc = pn533_build_tx_frame(dev, skb,
false);
2121 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->
in_frame,
2126 "Error %d when trying to send data", rc);
2138 static void pn533_wq_mi_recv(
struct work_struct *work)
2153 if (skb_cmd ==
NULL)
2158 rc = pn533_build_tx_frame(dev, skb_cmd,
true);
2165 skb_resp = alloc_skb(skb_resp_len,
GFP_KERNEL);
2177 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2179 pn533_data_exchange_complete,
2185 " perform data_exchange", rc);
2200 static int pn533_set_configuration(
struct pn533 *dev,
u8 cfgitem,
u8 *
cfgdata,
2211 params[0] = cfgitem;
2212 memcpy(¶ms[1], cfgdata, cfgdata_len);
2213 dev->
out_frame->datalen += (1 + cfgdata_len);
2223 static int pn533_fw_reset(
struct pn533 *dev)
2230 pn533_tx_frame_init(dev->
out_frame, 0x18);
2244 static struct nfc_ops pn533_nfc_ops = {
2247 .dep_link_up = pn533_dep_link_up,
2248 .dep_link_down = pn533_dep_link_down,
2249 .start_poll = pn533_start_poll,
2250 .stop_poll = pn533_stop_poll,
2251 .activate_target = pn533_activate_target,
2252 .deactivate_target = pn533_deactivate_target,
2253 .im_transceive = pn533_transceive,
2254 .tm_send = pn533_tm_send,
2257 static int pn533_setup(
struct pn533 *dev)
2261 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2267 max_retries.mx_rty_psl = 2;
2268 max_retries.mx_rty_passive_act =
2278 max_retries.mx_rty_atr = 0x2;
2279 max_retries.mx_rty_psl = 0x1;
2280 max_retries.mx_rty_passive_act =
2296 (
u8 *)&max_retries,
sizeof(max_retries));
2299 "Error on setting MAX_RETRIES config");
2308 "Error on setting RF timings");
2317 pn533_fw_reset(dev);
2323 "Error while settings PASORI config");
2327 pn533_fw_reset(dev);
2340 struct usb_host_interface *iface_desc;
2342 int in_endpoint = 0;
2343 int out_endpoint = 0;
2356 iface_desc = interface->cur_altsetting;
2357 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
2358 endpoint = &iface_desc->endpoint[
i].desc;
2360 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2365 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
2372 if (!in_endpoint || !out_endpoint) {
2373 nfc_dev_err(&interface->dev,
"Could not find bulk-in or"
2374 " bulk-out endpoint");
2389 usb_rcvbulkpipe(dev->
udev, in_endpoint),
2392 usb_sndbulkpipe(dev->
udev, out_endpoint),
2394 pn533_send_complete, dev);
2409 skb_queue_head_init(&dev->
resp_q);
2413 usb_set_intfdata(interface, dev);
2426 " attached", fw_ver->
ver, fw_ver->
rev);
2447 PN533_FRAME_TAIL_SIZE);
2451 nfc_set_parent_dev(dev->
nfc_dev, &interface->dev);
2452 nfc_set_drvdata(dev->
nfc_dev, dev);
2458 rc = pn533_setup(dev);
2460 goto unregister_nfc_dev;
2468 nfc_free_device(dev->
nfc_dev);
2481 static void pn533_disconnect(
struct usb_interface *interface)
2486 dev = usb_get_intfdata(interface);
2487 usb_set_intfdata(interface,
NULL);
2490 nfc_free_device(dev->
nfc_dev);
2512 nfc_dev_info(&interface->dev,
"NXP PN533 NFC device disconnected");
2515 static struct usb_driver pn533_driver = {
2517 .probe = pn533_probe,
2518 .disconnect = pn533_disconnect,
2519 .id_table = pn533_table,