treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / nfc / st-nci / core.c
blob110ff1281e5f7cdb0a265fd0dad07c9139d1f839
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NCI based Driver for STMicroelectronics NFC Chip
5 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
6 */
8 #include <linux/module.h>
9 #include <linux/nfc.h>
10 #include <net/nfc/nci.h>
11 #include <net/nfc/nci_core.h>
12 #include <linux/gpio.h>
13 #include <linux/delay.h>
15 #include "st-nci.h"
17 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
19 #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
21 static int st_nci_init(struct nci_dev *ndev)
23 struct nci_mode_set_cmd cmd;
25 cmd.cmd_type = ST_NCI_SET_NFC_MODE;
26 cmd.mode = 1;
28 return nci_prop_cmd(ndev, ST_NCI_CORE_PROP,
29 sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
32 static int st_nci_open(struct nci_dev *ndev)
34 struct st_nci_info *info = nci_get_drvdata(ndev);
35 int r;
37 if (test_and_set_bit(ST_NCI_RUNNING, &info->flags))
38 return 0;
40 r = ndlc_open(info->ndlc);
41 if (r)
42 clear_bit(ST_NCI_RUNNING, &info->flags);
44 return r;
47 static int st_nci_close(struct nci_dev *ndev)
49 struct st_nci_info *info = nci_get_drvdata(ndev);
51 if (!test_bit(ST_NCI_RUNNING, &info->flags))
52 return 0;
54 ndlc_close(info->ndlc);
56 clear_bit(ST_NCI_RUNNING, &info->flags);
58 return 0;
61 static int st_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
63 struct st_nci_info *info = nci_get_drvdata(ndev);
65 skb->dev = (void *)ndev;
67 if (!test_bit(ST_NCI_RUNNING, &info->flags))
68 return -EBUSY;
70 return ndlc_send(info->ndlc, skb);
73 static __u32 st_nci_get_rfprotocol(struct nci_dev *ndev,
74 __u8 rf_protocol)
76 return rf_protocol == ST_NCI1_X_PROPRIETARY_ISO15693 ?
77 NFC_PROTO_ISO15693_MASK : 0;
80 static int st_nci_prop_rsp_packet(struct nci_dev *ndev,
81 struct sk_buff *skb)
83 __u8 status = skb->data[0];
85 nci_req_complete(ndev, status);
86 return 0;
89 static struct nci_driver_ops st_nci_prop_ops[] = {
91 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
92 ST_NCI_CORE_PROP),
93 .rsp = st_nci_prop_rsp_packet,
97 static struct nci_ops st_nci_ops = {
98 .init = st_nci_init,
99 .open = st_nci_open,
100 .close = st_nci_close,
101 .send = st_nci_send,
102 .get_rfprotocol = st_nci_get_rfprotocol,
103 .discover_se = st_nci_discover_se,
104 .enable_se = st_nci_enable_se,
105 .disable_se = st_nci_disable_se,
106 .se_io = st_nci_se_io,
107 .hci_load_session = st_nci_hci_load_session,
108 .hci_event_received = st_nci_hci_event_received,
109 .hci_cmd_received = st_nci_hci_cmd_received,
110 .prop_ops = st_nci_prop_ops,
111 .n_prop_ops = ARRAY_SIZE(st_nci_prop_ops),
114 int st_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
115 int phy_tailroom, struct st_nci_se_status *se_status)
117 struct st_nci_info *info;
118 int r;
119 u32 protocols;
121 info = devm_kzalloc(ndlc->dev,
122 sizeof(struct st_nci_info), GFP_KERNEL);
123 if (!info)
124 return -ENOMEM;
126 protocols = NFC_PROTO_JEWEL_MASK
127 | NFC_PROTO_MIFARE_MASK
128 | NFC_PROTO_FELICA_MASK
129 | NFC_PROTO_ISO14443_MASK
130 | NFC_PROTO_ISO14443_B_MASK
131 | NFC_PROTO_ISO15693_MASK
132 | NFC_PROTO_NFC_DEP_MASK;
134 ndlc->ndev = nci_allocate_device(&st_nci_ops, protocols,
135 phy_headroom, phy_tailroom);
136 if (!ndlc->ndev) {
137 pr_err("Cannot allocate nfc ndev\n");
138 return -ENOMEM;
140 info->ndlc = ndlc;
142 nci_set_drvdata(ndlc->ndev, info);
144 r = st_nci_vendor_cmds_init(ndlc->ndev);
145 if (r) {
146 pr_err("Cannot register proprietary vendor cmds\n");
147 goto err_reg_dev;
150 r = nci_register_device(ndlc->ndev);
151 if (r) {
152 pr_err("Cannot register nfc device to nci core\n");
153 goto err_reg_dev;
156 return st_nci_se_init(ndlc->ndev, se_status);
158 err_reg_dev:
159 nci_free_device(ndlc->ndev);
160 return r;
162 EXPORT_SYMBOL_GPL(st_nci_probe);
164 void st_nci_remove(struct nci_dev *ndev)
166 struct st_nci_info *info = nci_get_drvdata(ndev);
168 ndlc_close(info->ndlc);
170 nci_unregister_device(ndev);
171 nci_free_device(ndev);
173 EXPORT_SYMBOL_GPL(st_nci_remove);
175 MODULE_LICENSE("GPL");
176 MODULE_DESCRIPTION(DRIVER_DESC);