Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * HCI based Driver for Inside Secure microread NFC Chip - i2c layer
  3 *
  4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
  5 *
  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.
  9 *
 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.
 14 *
 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/>.
 17 */
 18
 19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 20
 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>
 27
 28#include <linux/nfc.h>
 29#include <net/nfc/hci.h>
 30#include <net/nfc/llc.h>
 31
 32#include "microread.h"
 33
 34#define MICROREAD_I2C_DRIVER_NAME "microread"
 35
 36#define MICROREAD_I2C_FRAME_HEADROOM 1
 37#define MICROREAD_I2C_FRAME_TAILROOM 1
 38
 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)
 48
 49struct microread_i2c_phy {
 50	struct i2c_client *i2c_dev;
 51	struct nfc_hci_dev *hdev;
 52
 53	int irq;
 54
 55	int hard_fault;		/*
 56				 * < 0 if hardware error occured (e.g. i2c err)
 57				 * and prevents normal operation.
 58				 */
 59};
 60
 61#define I2C_DUMP_SKB(info, skb)					\
 62do {								\
 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)
 67
 68static void microread_i2c_add_len_crc(struct sk_buff *skb)
 69{
 70	int i;
 71	u8 crc = 0;
 72	int len;
 73
 74	len = skb->len;
 75	*skb_push(skb, 1) = len;
 76
 77	for (i = 0; i < skb->len; i++)
 78		crc = crc ^ skb->data[i];
 79
 80	*skb_put(skb, 1) = crc;
 81}
 82
 83static void microread_i2c_remove_len_crc(struct sk_buff *skb)
 84{
 85	skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
 86	skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
 87}
 88
 89static int check_crc(struct sk_buff *skb)
 90{
 91	int i;
 92	u8 crc = 0;
 93
 94	for (i = 0; i < skb->len - 1; i++)
 95		crc = crc ^ skb->data[i];
 96
 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;
101	}
102
103	return 0;
104}
105
106static int microread_i2c_enable(void *phy_id)
107{
108	return 0;
109}
110
111static void microread_i2c_disable(void *phy_id)
112{
113	return;
114}
115
116static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
117{
118	int r;
119	struct microread_i2c_phy *phy = phy_id;
120	struct i2c_client *client = phy->i2c_dev;
121
122	if (phy->hard_fault != 0)
123		return phy->hard_fault;
124
125	usleep_range(3000, 6000);
126
127	microread_i2c_add_len_crc(skb);
128
129	I2C_DUMP_SKB("i2c frame written", skb);
130
131	r = i2c_master_send(client, skb->data, skb->len);
132
133	if (r == -EREMOTEIO) {	/* Retry, chip was in standby */
134		usleep_range(6000, 10000);
135		r = i2c_master_send(client, skb->data, skb->len);
136	}
137
138	if (r >= 0) {
139		if (r != skb->len)
140			r = -EREMOTEIO;
141		else
142			r = 0;
143	}
144
145	microread_i2c_remove_len_crc(skb);
146
147	return r;
148}
149
150
151static int microread_i2c_read(struct microread_i2c_phy *phy,
152			      struct sk_buff **skb)
153{
154	int r;
155	u8 len;
156	u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
157	struct i2c_client *client = phy->i2c_dev;
158
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;
163	}
164
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;
170	}
171
172	*skb = alloc_skb(1 + len, GFP_KERNEL);
173	if (*skb == NULL) {
174		r = -ENOMEM;
175		goto flush;
176	}
177
178	*skb_put(*skb, 1) = len;
179
180	r = i2c_master_recv(client, skb_put(*skb, len), len);
181	if (r != len) {
182		kfree_skb(*skb);
183		return -EREMOTEIO;
184	}
185
186	I2C_DUMP_SKB("cc frame read", *skb);
187
188	r = check_crc(*skb);
189	if (r != 0) {
190		kfree_skb(*skb);
191		r = -EBADMSG;
192		goto flush;
193	}
194
195	skb_pull(*skb, 1);
196	skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
197
198	usleep_range(3000, 6000);
199
200	return 0;
201
202flush:
203	if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
204		r = -EREMOTEIO;
205
206	usleep_range(3000, 6000);
207
208	return r;
209}
210
211static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
212{
213	struct microread_i2c_phy *phy = phy_id;
214	struct i2c_client *client;
215	struct sk_buff *skb = NULL;
216	int r;
217
218	if (!phy || irq != phy->i2c_dev->irq) {
219		WARN_ON_ONCE(1);
220		return IRQ_NONE;
221	}
222
223	client = phy->i2c_dev;
224
225	if (phy->hard_fault != 0)
226		return IRQ_HANDLED;
227
228	r = microread_i2c_read(phy, &skb);
229	if (r == -EREMOTEIO) {
230		phy->hard_fault = r;
231
232		nfc_hci_recv_frame(phy->hdev, NULL);
233
234		return IRQ_HANDLED;
235	} else if ((r == -ENOMEM) || (r == -EBADMSG)) {
236		return IRQ_HANDLED;
237	}
238
239	nfc_hci_recv_frame(phy->hdev, skb);
240
241	return IRQ_HANDLED;
242}
243
244static struct nfc_phy_ops i2c_phy_ops = {
245	.write = microread_i2c_write,
246	.enable = microread_i2c_enable,
247	.disable = microread_i2c_disable,
248};
249
250static int microread_i2c_probe(struct i2c_client *client,
251			       const struct i2c_device_id *id)
252{
253	struct microread_i2c_phy *phy;
254	struct microread_nfc_platform_data *pdata =
255		dev_get_platdata(&client->dev);
256	int r;
257
258	dev_dbg(&client->dev, "client %p\n", client);
259
260	if (!pdata) {
261		nfc_err(&client->dev, "client %p: missing platform data\n",
262			client);
263		return -EINVAL;
264	}
265
266	phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
267			   GFP_KERNEL);
268	if (!phy)
269		return -ENOMEM;
270
271	i2c_set_clientdata(client, phy);
272	phy->i2c_dev = client;
273
274	r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
275				 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
276				 MICROREAD_I2C_DRIVER_NAME, phy);
277	if (r) {
278		nfc_err(&client->dev, "Unable to register IRQ handler\n");
279		return r;
280	}
281
282	r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
283			    MICROREAD_I2C_FRAME_HEADROOM,
284			    MICROREAD_I2C_FRAME_TAILROOM,
285			    MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
286	if (r < 0)
287		goto err_irq;
288
289	nfc_info(&client->dev, "Probed");
290
291	return 0;
292
293err_irq:
294	free_irq(client->irq, phy);
295
296	return r;
297}
298
299static int microread_i2c_remove(struct i2c_client *client)
300{
301	struct microread_i2c_phy *phy = i2c_get_clientdata(client);
302
303	microread_remove(phy->hdev);
304
305	free_irq(client->irq, phy);
306
307	return 0;
308}
309
310static struct i2c_device_id microread_i2c_id[] = {
311	{ MICROREAD_I2C_DRIVER_NAME, 0},
312	{ }
313};
314MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
315
316static struct i2c_driver microread_i2c_driver = {
317	.driver = {
318		.name = MICROREAD_I2C_DRIVER_NAME,
319	},
320	.probe		= microread_i2c_probe,
321	.remove		= microread_i2c_remove,
322	.id_table	= microread_i2c_id,
323};
324
325module_i2c_driver(microread_i2c_driver);
326
327MODULE_LICENSE("GPL");
328MODULE_DESCRIPTION(DRIVER_DESC);
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * HCI based Driver for Inside Secure microread NFC Chip - i2c layer
  4 *
  5 * Copyright (C) 2013 Intel Corporation. All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9
 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>
 16
 17#include <linux/nfc.h>
 18#include <net/nfc/hci.h>
 19#include <net/nfc/llc.h>
 20
 21#include "microread.h"
 22
 23#define MICROREAD_I2C_DRIVER_NAME "microread"
 24
 25#define MICROREAD_I2C_FRAME_HEADROOM 1
 26#define MICROREAD_I2C_FRAME_TAILROOM 1
 27
 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)
 37
 38struct microread_i2c_phy {
 39	struct i2c_client *i2c_dev;
 40	struct nfc_hci_dev *hdev;
 41
 
 
 42	int hard_fault;		/*
 43				 * < 0 if hardware error occured (e.g. i2c err)
 44				 * and prevents normal operation.
 45				 */
 46};
 47
 48#define I2C_DUMP_SKB(info, skb)					\
 49do {								\
 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)
 54
 55static void microread_i2c_add_len_crc(struct sk_buff *skb)
 56{
 57	int i;
 58	u8 crc = 0;
 59	int len;
 60
 61	len = skb->len;
 62	*(u8 *)skb_push(skb, 1) = len;
 63
 64	for (i = 0; i < skb->len; i++)
 65		crc = crc ^ skb->data[i];
 66
 67	skb_put_u8(skb, crc);
 68}
 69
 70static void microread_i2c_remove_len_crc(struct sk_buff *skb)
 71{
 72	skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
 73	skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
 74}
 75
 76static int check_crc(const struct sk_buff *skb)
 77{
 78	int i;
 79	u8 crc = 0;
 80
 81	for (i = 0; i < skb->len - 1; i++)
 82		crc = crc ^ skb->data[i];
 83
 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;
 88	}
 89
 90	return 0;
 91}
 92
 93static int microread_i2c_enable(void *phy_id)
 94{
 95	return 0;
 96}
 97
 98static void microread_i2c_disable(void *phy_id)
 99{
100	return;
101}
102
103static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
104{
105	int r;
106	struct microread_i2c_phy *phy = phy_id;
107	struct i2c_client *client = phy->i2c_dev;
108
109	if (phy->hard_fault != 0)
110		return phy->hard_fault;
111
112	usleep_range(3000, 6000);
113
114	microread_i2c_add_len_crc(skb);
115
116	I2C_DUMP_SKB("i2c frame written", skb);
117
118	r = i2c_master_send(client, skb->data, skb->len);
119
120	if (r == -EREMOTEIO) {	/* Retry, chip was in standby */
121		usleep_range(6000, 10000);
122		r = i2c_master_send(client, skb->data, skb->len);
123	}
124
125	if (r >= 0) {
126		if (r != skb->len)
127			r = -EREMOTEIO;
128		else
129			r = 0;
130	}
131
132	microread_i2c_remove_len_crc(skb);
133
134	return r;
135}
136
137
138static int microread_i2c_read(struct microread_i2c_phy *phy,
139			      struct sk_buff **skb)
140{
141	int r;
142	u8 len;
143	u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
144	struct i2c_client *client = phy->i2c_dev;
145
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;
150	}
151
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;
157	}
158
159	*skb = alloc_skb(1 + len, GFP_KERNEL);
160	if (*skb == NULL) {
161		r = -ENOMEM;
162		goto flush;
163	}
164
165	skb_put_u8(*skb, len);
166
167	r = i2c_master_recv(client, skb_put(*skb, len), len);
168	if (r != len) {
169		kfree_skb(*skb);
170		return -EREMOTEIO;
171	}
172
173	I2C_DUMP_SKB("cc frame read", *skb);
174
175	r = check_crc(*skb);
176	if (r != 0) {
177		kfree_skb(*skb);
178		r = -EBADMSG;
179		goto flush;
180	}
181
182	skb_pull(*skb, 1);
183	skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
184
185	usleep_range(3000, 6000);
186
187	return 0;
188
189flush:
190	if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
191		r = -EREMOTEIO;
192
193	usleep_range(3000, 6000);
194
195	return r;
196}
197
198static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
199{
200	struct microread_i2c_phy *phy = phy_id;
 
201	struct sk_buff *skb = NULL;
202	int r;
203
204	if (!phy || irq != phy->i2c_dev->irq) {
205		WARN_ON_ONCE(1);
206		return IRQ_NONE;
207	}
208
 
 
209	if (phy->hard_fault != 0)
210		return IRQ_HANDLED;
211
212	r = microread_i2c_read(phy, &skb);
213	if (r == -EREMOTEIO) {
214		phy->hard_fault = r;
215
216		nfc_hci_recv_frame(phy->hdev, NULL);
217
218		return IRQ_HANDLED;
219	} else if ((r == -ENOMEM) || (r == -EBADMSG)) {
220		return IRQ_HANDLED;
221	}
222
223	nfc_hci_recv_frame(phy->hdev, skb);
224
225	return IRQ_HANDLED;
226}
227
228static const struct nfc_phy_ops i2c_phy_ops = {
229	.write = microread_i2c_write,
230	.enable = microread_i2c_enable,
231	.disable = microread_i2c_disable,
232};
233
234static int microread_i2c_probe(struct i2c_client *client)
 
235{
236	struct microread_i2c_phy *phy;
 
 
237	int r;
238
 
 
 
 
 
 
 
 
239	phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
240			   GFP_KERNEL);
241	if (!phy)
242		return -ENOMEM;
243
244	i2c_set_clientdata(client, phy);
245	phy->i2c_dev = client;
246
247	r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
248				 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
249				 MICROREAD_I2C_DRIVER_NAME, phy);
250	if (r) {
251		nfc_err(&client->dev, "Unable to register IRQ handler\n");
252		return r;
253	}
254
255	r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
256			    MICROREAD_I2C_FRAME_HEADROOM,
257			    MICROREAD_I2C_FRAME_TAILROOM,
258			    MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
259	if (r < 0)
260		goto err_irq;
261
 
 
262	return 0;
263
264err_irq:
265	free_irq(client->irq, phy);
266
267	return r;
268}
269
270static void microread_i2c_remove(struct i2c_client *client)
271{
272	struct microread_i2c_phy *phy = i2c_get_clientdata(client);
273
274	microread_remove(phy->hdev);
275
276	free_irq(client->irq, phy);
 
 
277}
278
279static const struct i2c_device_id microread_i2c_id[] = {
280	{ MICROREAD_I2C_DRIVER_NAME },
281	{ }
282};
283MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
284
285static struct i2c_driver microread_i2c_driver = {
286	.driver = {
287		.name = MICROREAD_I2C_DRIVER_NAME,
288	},
289	.probe		= microread_i2c_probe,
290	.remove		= microread_i2c_remove,
291	.id_table	= microread_i2c_id,
292};
293
294module_i2c_driver(microread_i2c_driver);
295
296MODULE_LICENSE("GPL");
297MODULE_DESCRIPTION(DRIVER_DESC);