treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / nfc / microread / i2c.c
blobdd78d987e6c9d581b8af7c55f115800ff6e3871b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HCI based Driver for Inside Secure microread NFC Chip - i2c layer
5 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/gpio.h>
17 #include <linux/nfc.h>
18 #include <net/nfc/hci.h>
19 #include <net/nfc/llc.h>
21 #include "microread.h"
23 #define MICROREAD_I2C_DRIVER_NAME "microread"
25 #define MICROREAD_I2C_FRAME_HEADROOM 1
26 #define MICROREAD_I2C_FRAME_TAILROOM 1
28 /* framing in HCI mode */
29 #define MICROREAD_I2C_LLC_LEN 1
30 #define MICROREAD_I2C_LLC_CRC 1
31 #define MICROREAD_I2C_LLC_LEN_CRC (MICROREAD_I2C_LLC_LEN + \
32 MICROREAD_I2C_LLC_CRC)
33 #define MICROREAD_I2C_LLC_MIN_SIZE (1 + MICROREAD_I2C_LLC_LEN_CRC)
34 #define MICROREAD_I2C_LLC_MAX_PAYLOAD 29
35 #define MICROREAD_I2C_LLC_MAX_SIZE (MICROREAD_I2C_LLC_LEN_CRC + 1 + \
36 MICROREAD_I2C_LLC_MAX_PAYLOAD)
38 struct microread_i2c_phy {
39 struct i2c_client *i2c_dev;
40 struct nfc_hci_dev *hdev;
42 int hard_fault; /*
43 * < 0 if hardware error occured (e.g. i2c err)
44 * and prevents normal operation.
48 #define I2C_DUMP_SKB(info, skb) \
49 do { \
50 pr_debug("%s:\n", info); \
51 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
52 16, 1, (skb)->data, (skb)->len, 0); \
53 } while (0)
55 static void microread_i2c_add_len_crc(struct sk_buff *skb)
57 int i;
58 u8 crc = 0;
59 int len;
61 len = skb->len;
62 *(u8 *)skb_push(skb, 1) = len;
64 for (i = 0; i < skb->len; i++)
65 crc = crc ^ skb->data[i];
67 skb_put_u8(skb, crc);
70 static void microread_i2c_remove_len_crc(struct sk_buff *skb)
72 skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
73 skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
76 static int check_crc(struct sk_buff *skb)
78 int i;
79 u8 crc = 0;
81 for (i = 0; i < skb->len - 1; i++)
82 crc = crc ^ skb->data[i];
84 if (crc != skb->data[skb->len-1]) {
85 pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]);
86 pr_info("%s: BAD CRC\n", __func__);
87 return -EPERM;
90 return 0;
93 static int microread_i2c_enable(void *phy_id)
95 return 0;
98 static void microread_i2c_disable(void *phy_id)
100 return;
103 static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
105 int r;
106 struct microread_i2c_phy *phy = phy_id;
107 struct i2c_client *client = phy->i2c_dev;
109 if (phy->hard_fault != 0)
110 return phy->hard_fault;
112 usleep_range(3000, 6000);
114 microread_i2c_add_len_crc(skb);
116 I2C_DUMP_SKB("i2c frame written", skb);
118 r = i2c_master_send(client, skb->data, skb->len);
120 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
121 usleep_range(6000, 10000);
122 r = i2c_master_send(client, skb->data, skb->len);
125 if (r >= 0) {
126 if (r != skb->len)
127 r = -EREMOTEIO;
128 else
129 r = 0;
132 microread_i2c_remove_len_crc(skb);
134 return r;
138 static int microread_i2c_read(struct microread_i2c_phy *phy,
139 struct sk_buff **skb)
141 int r;
142 u8 len;
143 u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
144 struct i2c_client *client = phy->i2c_dev;
146 r = i2c_master_recv(client, &len, 1);
147 if (r != 1) {
148 nfc_err(&client->dev, "cannot read len byte\n");
149 return -EREMOTEIO;
152 if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
153 (len > MICROREAD_I2C_LLC_MAX_SIZE)) {
154 nfc_err(&client->dev, "invalid len byte\n");
155 r = -EBADMSG;
156 goto flush;
159 *skb = alloc_skb(1 + len, GFP_KERNEL);
160 if (*skb == NULL) {
161 r = -ENOMEM;
162 goto flush;
165 skb_put_u8(*skb, len);
167 r = i2c_master_recv(client, skb_put(*skb, len), len);
168 if (r != len) {
169 kfree_skb(*skb);
170 return -EREMOTEIO;
173 I2C_DUMP_SKB("cc frame read", *skb);
175 r = check_crc(*skb);
176 if (r != 0) {
177 kfree_skb(*skb);
178 r = -EBADMSG;
179 goto flush;
182 skb_pull(*skb, 1);
183 skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
185 usleep_range(3000, 6000);
187 return 0;
189 flush:
190 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
191 r = -EREMOTEIO;
193 usleep_range(3000, 6000);
195 return r;
198 static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
200 struct microread_i2c_phy *phy = phy_id;
201 struct sk_buff *skb = NULL;
202 int r;
204 if (!phy || irq != phy->i2c_dev->irq) {
205 WARN_ON_ONCE(1);
206 return IRQ_NONE;
209 if (phy->hard_fault != 0)
210 return IRQ_HANDLED;
212 r = microread_i2c_read(phy, &skb);
213 if (r == -EREMOTEIO) {
214 phy->hard_fault = r;
216 nfc_hci_recv_frame(phy->hdev, NULL);
218 return IRQ_HANDLED;
219 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
220 return IRQ_HANDLED;
223 nfc_hci_recv_frame(phy->hdev, skb);
225 return IRQ_HANDLED;
228 static struct nfc_phy_ops i2c_phy_ops = {
229 .write = microread_i2c_write,
230 .enable = microread_i2c_enable,
231 .disable = microread_i2c_disable,
234 static int microread_i2c_probe(struct i2c_client *client,
235 const struct i2c_device_id *id)
237 struct microread_i2c_phy *phy;
238 int r;
240 dev_dbg(&client->dev, "client %p\n", client);
242 phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
243 GFP_KERNEL);
244 if (!phy)
245 return -ENOMEM;
247 i2c_set_clientdata(client, phy);
248 phy->i2c_dev = client;
250 r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
251 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
252 MICROREAD_I2C_DRIVER_NAME, phy);
253 if (r) {
254 nfc_err(&client->dev, "Unable to register IRQ handler\n");
255 return r;
258 r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
259 MICROREAD_I2C_FRAME_HEADROOM,
260 MICROREAD_I2C_FRAME_TAILROOM,
261 MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
262 if (r < 0)
263 goto err_irq;
265 nfc_info(&client->dev, "Probed\n");
267 return 0;
269 err_irq:
270 free_irq(client->irq, phy);
272 return r;
275 static int microread_i2c_remove(struct i2c_client *client)
277 struct microread_i2c_phy *phy = i2c_get_clientdata(client);
279 microread_remove(phy->hdev);
281 free_irq(client->irq, phy);
283 return 0;
286 static const struct i2c_device_id microread_i2c_id[] = {
287 { MICROREAD_I2C_DRIVER_NAME, 0},
290 MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
292 static struct i2c_driver microread_i2c_driver = {
293 .driver = {
294 .name = MICROREAD_I2C_DRIVER_NAME,
296 .probe = microread_i2c_probe,
297 .remove = microread_i2c_remove,
298 .id_table = microread_i2c_id,
301 module_i2c_driver(microread_i2c_driver);
303 MODULE_LICENSE("GPL");
304 MODULE_DESCRIPTION(DRIVER_DESC);