ocfs2: fix several issues of append dio
[linux/fpc-iii.git] / drivers / nfc / microread / i2c.c
blobdaf352597ef8fedd353962b51b5954b66e0b8ce3
1 /*
2 * HCI based Driver for Inside Secure microread NFC Chip - i2c layer
4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/gpio.h>
28 #include <linux/nfc.h>
29 #include <net/nfc/hci.h>
30 #include <net/nfc/llc.h>
32 #include "microread.h"
34 #define MICROREAD_I2C_DRIVER_NAME "microread"
36 #define MICROREAD_I2C_FRAME_HEADROOM 1
37 #define MICROREAD_I2C_FRAME_TAILROOM 1
39 /* framing in HCI mode */
40 #define MICROREAD_I2C_LLC_LEN 1
41 #define MICROREAD_I2C_LLC_CRC 1
42 #define MICROREAD_I2C_LLC_LEN_CRC (MICROREAD_I2C_LLC_LEN + \
43 MICROREAD_I2C_LLC_CRC)
44 #define MICROREAD_I2C_LLC_MIN_SIZE (1 + MICROREAD_I2C_LLC_LEN_CRC)
45 #define MICROREAD_I2C_LLC_MAX_PAYLOAD 29
46 #define MICROREAD_I2C_LLC_MAX_SIZE (MICROREAD_I2C_LLC_LEN_CRC + 1 + \
47 MICROREAD_I2C_LLC_MAX_PAYLOAD)
49 struct microread_i2c_phy {
50 struct i2c_client *i2c_dev;
51 struct nfc_hci_dev *hdev;
53 int irq;
55 int hard_fault; /*
56 * < 0 if hardware error occured (e.g. i2c err)
57 * and prevents normal operation.
61 #define I2C_DUMP_SKB(info, skb) \
62 do { \
63 pr_debug("%s:\n", info); \
64 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
65 16, 1, (skb)->data, (skb)->len, 0); \
66 } while (0)
68 static void microread_i2c_add_len_crc(struct sk_buff *skb)
70 int i;
71 u8 crc = 0;
72 int len;
74 len = skb->len;
75 *skb_push(skb, 1) = len;
77 for (i = 0; i < skb->len; i++)
78 crc = crc ^ skb->data[i];
80 *skb_put(skb, 1) = crc;
83 static void microread_i2c_remove_len_crc(struct sk_buff *skb)
85 skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
86 skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
89 static int check_crc(struct sk_buff *skb)
91 int i;
92 u8 crc = 0;
94 for (i = 0; i < skb->len - 1; i++)
95 crc = crc ^ skb->data[i];
97 if (crc != skb->data[skb->len-1]) {
98 pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]);
99 pr_info("%s: BAD CRC\n", __func__);
100 return -EPERM;
103 return 0;
106 static int microread_i2c_enable(void *phy_id)
108 return 0;
111 static void microread_i2c_disable(void *phy_id)
113 return;
116 static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
118 int r;
119 struct microread_i2c_phy *phy = phy_id;
120 struct i2c_client *client = phy->i2c_dev;
122 if (phy->hard_fault != 0)
123 return phy->hard_fault;
125 usleep_range(3000, 6000);
127 microread_i2c_add_len_crc(skb);
129 I2C_DUMP_SKB("i2c frame written", skb);
131 r = i2c_master_send(client, skb->data, skb->len);
133 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
134 usleep_range(6000, 10000);
135 r = i2c_master_send(client, skb->data, skb->len);
138 if (r >= 0) {
139 if (r != skb->len)
140 r = -EREMOTEIO;
141 else
142 r = 0;
145 microread_i2c_remove_len_crc(skb);
147 return r;
151 static int microread_i2c_read(struct microread_i2c_phy *phy,
152 struct sk_buff **skb)
154 int r;
155 u8 len;
156 u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
157 struct i2c_client *client = phy->i2c_dev;
159 r = i2c_master_recv(client, &len, 1);
160 if (r != 1) {
161 nfc_err(&client->dev, "cannot read len byte\n");
162 return -EREMOTEIO;
165 if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
166 (len > MICROREAD_I2C_LLC_MAX_SIZE)) {
167 nfc_err(&client->dev, "invalid len byte\n");
168 r = -EBADMSG;
169 goto flush;
172 *skb = alloc_skb(1 + len, GFP_KERNEL);
173 if (*skb == NULL) {
174 r = -ENOMEM;
175 goto flush;
178 *skb_put(*skb, 1) = len;
180 r = i2c_master_recv(client, skb_put(*skb, len), len);
181 if (r != len) {
182 kfree_skb(*skb);
183 return -EREMOTEIO;
186 I2C_DUMP_SKB("cc frame read", *skb);
188 r = check_crc(*skb);
189 if (r != 0) {
190 kfree_skb(*skb);
191 r = -EBADMSG;
192 goto flush;
195 skb_pull(*skb, 1);
196 skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
198 usleep_range(3000, 6000);
200 return 0;
202 flush:
203 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
204 r = -EREMOTEIO;
206 usleep_range(3000, 6000);
208 return r;
211 static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
213 struct microread_i2c_phy *phy = phy_id;
214 struct sk_buff *skb = NULL;
215 int r;
217 if (!phy || irq != phy->i2c_dev->irq) {
218 WARN_ON_ONCE(1);
219 return IRQ_NONE;
222 if (phy->hard_fault != 0)
223 return IRQ_HANDLED;
225 r = microread_i2c_read(phy, &skb);
226 if (r == -EREMOTEIO) {
227 phy->hard_fault = r;
229 nfc_hci_recv_frame(phy->hdev, NULL);
231 return IRQ_HANDLED;
232 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
233 return IRQ_HANDLED;
236 nfc_hci_recv_frame(phy->hdev, skb);
238 return IRQ_HANDLED;
241 static struct nfc_phy_ops i2c_phy_ops = {
242 .write = microread_i2c_write,
243 .enable = microread_i2c_enable,
244 .disable = microread_i2c_disable,
247 static int microread_i2c_probe(struct i2c_client *client,
248 const struct i2c_device_id *id)
250 struct microread_i2c_phy *phy;
251 struct microread_nfc_platform_data *pdata =
252 dev_get_platdata(&client->dev);
253 int r;
255 dev_dbg(&client->dev, "client %p\n", client);
257 if (!pdata) {
258 nfc_err(&client->dev, "client %p: missing platform data\n",
259 client);
260 return -EINVAL;
263 phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
264 GFP_KERNEL);
265 if (!phy)
266 return -ENOMEM;
268 i2c_set_clientdata(client, phy);
269 phy->i2c_dev = client;
271 r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
272 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
273 MICROREAD_I2C_DRIVER_NAME, phy);
274 if (r) {
275 nfc_err(&client->dev, "Unable to register IRQ handler\n");
276 return r;
279 r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
280 MICROREAD_I2C_FRAME_HEADROOM,
281 MICROREAD_I2C_FRAME_TAILROOM,
282 MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
283 if (r < 0)
284 goto err_irq;
286 nfc_info(&client->dev, "Probed\n");
288 return 0;
290 err_irq:
291 free_irq(client->irq, phy);
293 return r;
296 static int microread_i2c_remove(struct i2c_client *client)
298 struct microread_i2c_phy *phy = i2c_get_clientdata(client);
300 microread_remove(phy->hdev);
302 free_irq(client->irq, phy);
304 return 0;
307 static struct i2c_device_id microread_i2c_id[] = {
308 { MICROREAD_I2C_DRIVER_NAME, 0},
311 MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
313 static struct i2c_driver microread_i2c_driver = {
314 .driver = {
315 .name = MICROREAD_I2C_DRIVER_NAME,
317 .probe = microread_i2c_probe,
318 .remove = microread_i2c_remove,
319 .id_table = microread_i2c_id,
322 module_i2c_driver(microread_i2c_driver);
324 MODULE_LICENSE("GPL");
325 MODULE_DESCRIPTION(DRIVER_DESC);