1 // SPDX-License-Identifier: GPL-2.0-only
3 * NCI based Driver for STMicroelectronics NFC Chip
5 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
8 #include <linux/module.h>
10 #include <net/nfc/nci.h>
11 #include <net/nfc/nci_core.h>
15 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
17 #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
19 static int st_nci_init(struct nci_dev
*ndev
)
21 struct nci_mode_set_cmd cmd
;
23 cmd
.cmd_type
= ST_NCI_SET_NFC_MODE
;
26 return nci_prop_cmd(ndev
, ST_NCI_CORE_PROP
,
27 sizeof(struct nci_mode_set_cmd
), (__u8
*)&cmd
);
30 static int st_nci_open(struct nci_dev
*ndev
)
32 struct st_nci_info
*info
= nci_get_drvdata(ndev
);
35 if (test_and_set_bit(ST_NCI_RUNNING
, &info
->flags
))
38 r
= ndlc_open(info
->ndlc
);
40 clear_bit(ST_NCI_RUNNING
, &info
->flags
);
45 static int st_nci_close(struct nci_dev
*ndev
)
47 struct st_nci_info
*info
= nci_get_drvdata(ndev
);
49 if (!test_bit(ST_NCI_RUNNING
, &info
->flags
))
52 ndlc_close(info
->ndlc
);
54 clear_bit(ST_NCI_RUNNING
, &info
->flags
);
59 static int st_nci_send(struct nci_dev
*ndev
, struct sk_buff
*skb
)
61 struct st_nci_info
*info
= nci_get_drvdata(ndev
);
63 skb
->dev
= (void *)ndev
;
65 if (!test_bit(ST_NCI_RUNNING
, &info
->flags
))
68 return ndlc_send(info
->ndlc
, skb
);
71 static __u32
st_nci_get_rfprotocol(struct nci_dev
*ndev
,
74 return rf_protocol
== ST_NCI1_X_PROPRIETARY_ISO15693
?
75 NFC_PROTO_ISO15693_MASK
: 0;
78 static int st_nci_prop_rsp_packet(struct nci_dev
*ndev
,
81 __u8 status
= skb
->data
[0];
83 nci_req_complete(ndev
, status
);
87 static const struct nci_driver_ops st_nci_prop_ops
[] = {
89 .opcode
= nci_opcode_pack(NCI_GID_PROPRIETARY
,
91 .rsp
= st_nci_prop_rsp_packet
,
95 static const struct nci_ops st_nci_ops
= {
98 .close
= st_nci_close
,
100 .get_rfprotocol
= st_nci_get_rfprotocol
,
101 .discover_se
= st_nci_discover_se
,
102 .enable_se
= st_nci_enable_se
,
103 .disable_se
= st_nci_disable_se
,
104 .se_io
= st_nci_se_io
,
105 .hci_load_session
= st_nci_hci_load_session
,
106 .hci_event_received
= st_nci_hci_event_received
,
107 .hci_cmd_received
= st_nci_hci_cmd_received
,
108 .prop_ops
= st_nci_prop_ops
,
109 .n_prop_ops
= ARRAY_SIZE(st_nci_prop_ops
),
112 int st_nci_probe(struct llt_ndlc
*ndlc
, int phy_headroom
,
113 int phy_tailroom
, struct st_nci_se_status
*se_status
)
115 struct st_nci_info
*info
;
119 info
= devm_kzalloc(ndlc
->dev
,
120 sizeof(struct st_nci_info
), GFP_KERNEL
);
124 protocols
= NFC_PROTO_JEWEL_MASK
125 | NFC_PROTO_MIFARE_MASK
126 | NFC_PROTO_FELICA_MASK
127 | NFC_PROTO_ISO14443_MASK
128 | NFC_PROTO_ISO14443_B_MASK
129 | NFC_PROTO_ISO15693_MASK
130 | NFC_PROTO_NFC_DEP_MASK
;
132 BUILD_BUG_ON(ARRAY_SIZE(st_nci_prop_ops
) > NCI_MAX_PROPRIETARY_CMD
);
133 ndlc
->ndev
= nci_allocate_device(&st_nci_ops
, protocols
,
134 phy_headroom
, phy_tailroom
);
136 pr_err("Cannot allocate nfc ndev\n");
141 nci_set_drvdata(ndlc
->ndev
, info
);
143 r
= st_nci_vendor_cmds_init(ndlc
->ndev
);
145 pr_err("Cannot register proprietary vendor cmds\n");
149 r
= nci_register_device(ndlc
->ndev
);
151 pr_err("Cannot register nfc device to nci core\n");
155 return st_nci_se_init(ndlc
->ndev
, se_status
);
158 nci_free_device(ndlc
->ndev
);
161 EXPORT_SYMBOL_GPL(st_nci_probe
);
163 void st_nci_remove(struct nci_dev
*ndev
)
165 struct st_nci_info
*info
= nci_get_drvdata(ndev
);
167 ndlc_close(info
->ndlc
);
169 nci_unregister_device(ndev
);
170 nci_free_device(ndev
);
172 EXPORT_SYMBOL_GPL(st_nci_remove
);
174 MODULE_LICENSE("GPL");
175 MODULE_DESCRIPTION(DRIVER_DESC
);