Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * NFC hardware simulation driver
  3 * Copyright (c) 2013, Intel Corporation.
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms and conditions of the GNU General Public License,
  7 * version 2, as published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 12 * more details.
 13 *
 14 */
 15
 16#include <linux/device.h>
 17#include <linux/kernel.h>
 18#include <linux/module.h>
 
 
 19#include <linux/nfc.h>
 20#include <net/nfc/nfc.h>
 
 21
 22#define DEV_ERR(_dev, fmt, args...) nfc_err(&_dev->nfc_dev->dev, \
 23						"%s: " fmt, __func__, ## args)
 24
 25#define DEV_DBG(_dev, fmt, args...) dev_dbg(&_dev->nfc_dev->dev, \
 26						"%s: " fmt, __func__, ## args)
 27
 28#define NFCSIM_VERSION "0.1"
 29
 30#define NFCSIM_POLL_NONE	0
 31#define NFCSIM_POLL_INITIATOR	1
 32#define NFCSIM_POLL_TARGET	2
 33#define NFCSIM_POLL_DUAL	(NFCSIM_POLL_INITIATOR | NFCSIM_POLL_TARGET)
 34
 35#define RX_DEFAULT_DELAY	5
 
 36
 37struct nfcsim {
 38	struct nfc_dev *nfc_dev;
 39
 40	struct mutex lock;
 
 41
 42	struct delayed_work recv_work;
 
 43
 44	struct sk_buff *clone_skb;
 
 
 45
 46	struct delayed_work poll_work;
 47	u8 polling_mode;
 48	u8 curr_polling_mode;
 49
 50	u8 shutting_down;
 
 51
 52	u8 up;
 
 53
 54	u8 initiator;
 
 55
 56	u32 rx_delay;
 
 57
 58	data_exchange_cb_t cb;
 59	void *cb_context;
 60
 61	struct nfcsim *peer_dev;
 
 
 62};
 63
 64static struct nfcsim *dev0;
 65static struct nfcsim *dev1;
 66
 67static struct workqueue_struct *wq;
 68
 69static void nfcsim_cleanup_dev(struct nfcsim *dev, u8 shutdown)
 70{
 71	DEV_DBG(dev, "shutdown=%d\n", shutdown);
 72
 73	mutex_lock(&dev->lock);
 
 
 74
 75	dev->polling_mode = NFCSIM_POLL_NONE;
 76	dev->shutting_down = shutdown;
 77	dev->cb = NULL;
 78	dev_kfree_skb(dev->clone_skb);
 79	dev->clone_skb = NULL;
 80
 81	mutex_unlock(&dev->lock);
 82
 83	cancel_delayed_work_sync(&dev->poll_work);
 84	cancel_delayed_work_sync(&dev->recv_work);
 85}
 86
 87static int nfcsim_target_found(struct nfcsim *dev)
 88{
 89	struct nfc_target nfc_tgt;
 
 
 90
 91	DEV_DBG(dev, "\n");
 
 
 
 
 92
 93	memset(&nfc_tgt, 0, sizeof(struct nfc_target));
 
 
 
 94
 95	nfc_tgt.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
 96	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
 
 
 97
 98	return 0;
 99}
100
101static int nfcsim_dev_up(struct nfc_dev *nfc_dev)
102{
103	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
104
105	DEV_DBG(dev, "\n");
106
107	mutex_lock(&dev->lock);
108
109	dev->up = 1;
110
111	mutex_unlock(&dev->lock);
112
113	return 0;
114}
115
116static int nfcsim_dev_down(struct nfc_dev *nfc_dev)
117{
118	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
119
120	DEV_DBG(dev, "\n");
121
122	mutex_lock(&dev->lock);
123
124	dev->up = 0;
 
125
126	mutex_unlock(&dev->lock);
127
128	return 0;
129}
130
131static int nfcsim_dep_link_up(struct nfc_dev *nfc_dev,
132			      struct nfc_target *target,
133			      u8 comm_mode, u8 *gb, size_t gb_len)
134{
135	int rc;
136	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
137	struct nfcsim *peer = dev->peer_dev;
138	u8 *remote_gb;
139	size_t remote_gb_len;
140
141	DEV_DBG(dev, "target_idx: %d, comm_mode: %d\n", target->idx, comm_mode);
 
 
142
143	mutex_lock(&peer->lock);
144
145	nfc_tm_activated(peer->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
146			 NFC_COMM_ACTIVE, gb, gb_len);
147
148	remote_gb = nfc_get_local_general_bytes(peer->nfc_dev, &remote_gb_len);
149	if (!remote_gb) {
150		DEV_ERR(peer, "Can't get remote general bytes\n");
 
151
152		mutex_unlock(&peer->lock);
153		return -EINVAL;
 
154	}
155
156	mutex_unlock(&peer->lock);
 
 
 
157
158	mutex_lock(&dev->lock);
 
159
160	rc = nfc_set_remote_general_bytes(nfc_dev, remote_gb, remote_gb_len);
161	if (rc) {
162		DEV_ERR(dev, "Can't set remote general bytes\n");
163		mutex_unlock(&dev->lock);
164		return rc;
165	}
166
167	rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_ACTIVE,
168				NFC_RF_INITIATOR);
169
170	mutex_unlock(&dev->lock);
171
172	return rc;
173}
174
175static int nfcsim_dep_link_down(struct nfc_dev *nfc_dev)
176{
177	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
178
179	DEV_DBG(dev, "\n");
180
181	nfcsim_cleanup_dev(dev, 0);
182
183	return 0;
 
 
 
184}
185
186static int nfcsim_start_poll(struct nfc_dev *nfc_dev,
187			     u32 im_protocols, u32 tm_protocols)
188{
189	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
190	int rc;
191
192	mutex_lock(&dev->lock);
 
193
194	if (dev->polling_mode != NFCSIM_POLL_NONE) {
195		DEV_ERR(dev, "Already in polling mode\n");
196		rc = -EBUSY;
197		goto exit;
198	}
199
200	if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
201		dev->polling_mode |= NFCSIM_POLL_INITIATOR;
202
203	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK)
204		dev->polling_mode |= NFCSIM_POLL_TARGET;
205
206	if (dev->polling_mode == NFCSIM_POLL_NONE) {
207		DEV_ERR(dev, "Unsupported polling mode\n");
208		rc = -EINVAL;
209		goto exit;
210	}
211
212	dev->initiator = 0;
213	dev->curr_polling_mode = NFCSIM_POLL_NONE;
214
215	queue_delayed_work(wq, &dev->poll_work, 0);
 
 
 
 
216
217	DEV_DBG(dev, "Start polling: im: 0x%X, tm: 0x%X\n", im_protocols,
218		tm_protocols);
 
 
219
220	rc = 0;
221exit:
222	mutex_unlock(&dev->lock);
223
224	return rc;
225}
226
227static void nfcsim_stop_poll(struct nfc_dev *nfc_dev)
228{
229	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
 
230
231	DEV_DBG(dev, "Stop poll\n");
 
232
233	mutex_lock(&dev->lock);
 
 
234
235	dev->polling_mode = NFCSIM_POLL_NONE;
 
 
236
237	mutex_unlock(&dev->lock);
 
238
239	cancel_delayed_work_sync(&dev->poll_work);
240}
241
242static int nfcsim_activate_target(struct nfc_dev *nfc_dev,
243				  struct nfc_target *target, u32 protocol)
244{
245	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
246
247	DEV_DBG(dev, "\n");
248
249	return -ENOTSUPP;
250}
251
252static void nfcsim_deactivate_target(struct nfc_dev *nfc_dev,
253				     struct nfc_target *target, u8 mode)
254{
255	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
 
 
256
257	DEV_DBG(dev, "\n");
258}
259
260static void nfcsim_wq_recv(struct work_struct *work)
 
261{
262	struct nfcsim *dev = container_of(work, struct nfcsim,
263					  recv_work.work);
264
265	mutex_lock(&dev->lock);
 
 
 
 
 
266
267	if (dev->shutting_down || !dev->up || !dev->clone_skb) {
268		dev_kfree_skb(dev->clone_skb);
269		goto exit;
270	}
271
272	if (dev->initiator) {
273		if (!dev->cb) {
274			DEV_ERR(dev, "Null recv callback\n");
275			dev_kfree_skb(dev->clone_skb);
276			goto exit;
277		}
278
279		dev->cb(dev->cb_context, dev->clone_skb, 0);
280		dev->cb = NULL;
281	} else {
282		nfc_tm_data_received(dev->nfc_dev, dev->clone_skb);
283	}
284
285exit:
286	dev->clone_skb = NULL;
287
288	mutex_unlock(&dev->lock);
 
 
 
 
289}
290
291static int nfcsim_tx(struct nfc_dev *nfc_dev, struct nfc_target *target,
292		     struct sk_buff *skb, data_exchange_cb_t cb,
293		     void *cb_context)
294{
295	struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
296	struct nfcsim *peer = dev->peer_dev;
297	int err;
298
299	mutex_lock(&dev->lock);
 
 
 
 
 
300
301	if (dev->shutting_down || !dev->up) {
302		mutex_unlock(&dev->lock);
303		err = -ENODEV;
304		goto exit;
 
 
305	}
306
307	dev->cb = cb;
308	dev->cb_context = cb_context;
309
310	mutex_unlock(&dev->lock);
 
 
 
 
 
311
312	mutex_lock(&peer->lock);
 
 
 
 
313
314	peer->clone_skb = skb_clone(skb, GFP_KERNEL);
 
 
315
316	if (!peer->clone_skb) {
317		DEV_ERR(dev, "skb_clone failed\n");
318		mutex_unlock(&peer->lock);
319		err = -ENOMEM;
320		goto exit;
321	}
322
323	/* This simulates an arbitrary transmission delay between the 2 devices.
324	 * If packet transmission occurs immediately between them, we have a
325	 * non-stop flow of several tens of thousands SYMM packets per second
326	 * and a burning cpu.
327	 */
328	queue_delayed_work(wq, &peer->recv_work,
329			msecs_to_jiffies(dev->rx_delay));
330
331	mutex_unlock(&peer->lock);
 
 
332
333	err = 0;
334exit:
335	dev_kfree_skb(skb);
336
337	return err;
338}
339
340static int nfcsim_im_transceive(struct nfc_dev *nfc_dev,
341				struct nfc_target *target, struct sk_buff *skb,
342				data_exchange_cb_t cb, void *cb_context)
343{
344	return nfcsim_tx(nfc_dev, target, skb, cb, cb_context);
345}
346
347static int nfcsim_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
348{
349	return nfcsim_tx(nfc_dev, NULL, skb, NULL, NULL);
350}
351
352static struct nfc_ops nfcsim_nfc_ops = {
353	.dev_up = nfcsim_dev_up,
354	.dev_down = nfcsim_dev_down,
355	.dep_link_up = nfcsim_dep_link_up,
356	.dep_link_down = nfcsim_dep_link_down,
357	.start_poll = nfcsim_start_poll,
358	.stop_poll = nfcsim_stop_poll,
359	.activate_target = nfcsim_activate_target,
360	.deactivate_target = nfcsim_deactivate_target,
361	.im_transceive = nfcsim_im_transceive,
362	.tm_send = nfcsim_tm_send,
363};
364
365static void nfcsim_set_polling_mode(struct nfcsim *dev)
366{
367	if (dev->polling_mode == NFCSIM_POLL_NONE) {
368		dev->curr_polling_mode = NFCSIM_POLL_NONE;
369		return;
370	}
371
372	if (dev->curr_polling_mode == NFCSIM_POLL_NONE) {
373		if (dev->polling_mode & NFCSIM_POLL_INITIATOR)
374			dev->curr_polling_mode = NFCSIM_POLL_INITIATOR;
375		else
376			dev->curr_polling_mode = NFCSIM_POLL_TARGET;
377
378		return;
379	}
380
381	if (dev->polling_mode == NFCSIM_POLL_DUAL) {
382		if (dev->curr_polling_mode == NFCSIM_POLL_TARGET)
383			dev->curr_polling_mode = NFCSIM_POLL_INITIATOR;
384		else
385			dev->curr_polling_mode = NFCSIM_POLL_TARGET;
386	}
 
 
387}
388
389static void nfcsim_wq_poll(struct work_struct *work)
 
390{
391	struct nfcsim *dev = container_of(work, struct nfcsim, poll_work.work);
392	struct nfcsim *peer = dev->peer_dev;
393
394	/* These work items run on an ordered workqueue and are therefore
395	 * serialized. So we can take both mutexes without being dead locked.
396	 */
397	mutex_lock(&dev->lock);
398	mutex_lock(&peer->lock);
399
400	nfcsim_set_polling_mode(dev);
 
401
402	if (dev->curr_polling_mode == NFCSIM_POLL_NONE) {
403		DEV_DBG(dev, "Not polling\n");
404		goto unlock;
 
 
 
 
 
405	}
406
407	DEV_DBG(dev, "Polling as %s",
408		dev->curr_polling_mode == NFCSIM_POLL_INITIATOR ?
409		"initiator\n" : "target\n");
410
411	if (dev->curr_polling_mode == NFCSIM_POLL_TARGET)
412		goto sched_work;
413
414	if (peer->curr_polling_mode == NFCSIM_POLL_TARGET) {
415		peer->polling_mode = NFCSIM_POLL_NONE;
416		dev->polling_mode = NFCSIM_POLL_NONE;
417
418		dev->initiator = 1;
419
420		nfcsim_target_found(dev);
421
422		goto unlock;
423	}
424
425sched_work:
426	/* This defines the delay for an initiator to check if the other device
427	 * is polling in target mode.
428	 * If the device starts in dual mode polling, it switches between
429	 * initiator and target at every round.
430	 * Because the wq is ordered and only 1 work item is executed at a time,
431	 * we'll always have one device polling as initiator and the other as
432	 * target at some point, even if both are started in dual mode.
433	 */
434	queue_delayed_work(wq, &dev->poll_work, msecs_to_jiffies(200));
435
436unlock:
437	mutex_unlock(&peer->lock);
438	mutex_unlock(&dev->lock);
439}
440
441static struct nfcsim *nfcsim_init_dev(void)
442{
443	struct nfcsim *dev;
444	int rc = -ENOMEM;
445
446	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
447	if (dev == NULL)
448		return ERR_PTR(-ENOMEM);
449
450	mutex_init(&dev->lock);
451
452	INIT_DELAYED_WORK(&dev->recv_work, nfcsim_wq_recv);
453	INIT_DELAYED_WORK(&dev->poll_work, nfcsim_wq_poll);
454
455	dev->nfc_dev = nfc_allocate_device(&nfcsim_nfc_ops,
456					   NFC_PROTO_NFC_DEP_MASK,
457					   0, 0);
458	if (!dev->nfc_dev)
459		goto error;
460
461	nfc_set_drvdata(dev->nfc_dev, dev);
462
463	rc = nfc_register_device(dev->nfc_dev);
464	if (rc)
465		goto free_nfc_dev;
466
467	dev->rx_delay = RX_DEFAULT_DELAY;
468	return dev;
469
470free_nfc_dev:
471	nfc_free_device(dev->nfc_dev);
472
473error:
474	kfree(dev);
475
476	return ERR_PTR(rc);
477}
478
479static void nfcsim_free_device(struct nfcsim *dev)
480{
481	nfc_unregister_device(dev->nfc_dev);
482
483	nfc_free_device(dev->nfc_dev);
484
485	kfree(dev);
486}
487
488static int __init nfcsim_init(void)
489{
 
490	int rc;
491
492	/* We need an ordered wq to ensure that poll_work items are executed
493	 * one at a time.
494	 */
495	wq = alloc_ordered_workqueue("nfcsim", 0);
496	if (!wq) {
497		rc = -ENOMEM;
498		goto exit;
499	}
500
501	dev0 = nfcsim_init_dev();
 
 
502	if (IS_ERR(dev0)) {
503		rc = PTR_ERR(dev0);
504		goto exit;
505	}
506
507	dev1 = nfcsim_init_dev();
508	if (IS_ERR(dev1)) {
509		kfree(dev0);
510
511		rc = PTR_ERR(dev1);
512		goto exit;
513	}
514
515	dev0->peer_dev = dev1;
516	dev1->peer_dev = dev0;
 
517
518	pr_debug("NFCsim " NFCSIM_VERSION " initialized\n");
 
519
520	rc = 0;
521exit:
522	if (rc)
523		pr_err("Failed to initialize nfcsim driver (%d)\n",
524		       rc);
525
526	return rc;
527}
528
529static void __exit nfcsim_exit(void)
530{
531	nfcsim_cleanup_dev(dev0, 1);
532	nfcsim_cleanup_dev(dev1, 1);
 
 
 
 
 
533
534	nfcsim_free_device(dev0);
535	nfcsim_free_device(dev1);
536
537	destroy_workqueue(wq);
538}
539
540module_init(nfcsim_init);
541module_exit(nfcsim_exit);
542
543MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION);
544MODULE_VERSION(NFCSIM_VERSION);
545MODULE_LICENSE("GPL");
v4.10.11
  1/*
  2 * NFC hardware simulation driver
  3 * Copyright (c) 2013, Intel Corporation.
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms and conditions of the GNU General Public License,
  7 * version 2, as published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 12 * more details.
 13 *
 14 */
 15
 16#include <linux/device.h>
 17#include <linux/kernel.h>
 18#include <linux/module.h>
 19#include <linux/ctype.h>
 20#include <linux/debugfs.h>
 21#include <linux/nfc.h>
 22#include <net/nfc/nfc.h>
 23#include <net/nfc/digital.h>
 24
 25#define NFCSIM_ERR(d, fmt, args...) nfc_err(&d->nfc_digital_dev->nfc_dev->dev, \
 26					    "%s: " fmt, __func__, ## args)
 27
 28#define NFCSIM_DBG(d, fmt, args...) dev_dbg(&d->nfc_digital_dev->nfc_dev->dev, \
 29					    "%s: " fmt, __func__, ## args)
 30
 31#define NFCSIM_VERSION "0.2"
 32
 33#define NFCSIM_MODE_NONE	0
 34#define NFCSIM_MODE_INITIATOR	1
 35#define NFCSIM_MODE_TARGET	2
 
 36
 37#define NFCSIM_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC   | \
 38			     NFC_DIGITAL_DRV_CAPS_TG_CRC)
 39
 40struct nfcsim {
 41	struct nfc_digital_dev *nfc_digital_dev;
 42
 43	struct work_struct recv_work;
 44	struct delayed_work send_work;
 45
 46	struct nfcsim_link *link_in;
 47	struct nfcsim_link *link_out;
 48
 49	bool up;
 50	u8 mode;
 51	u8 rf_tech;
 52
 53	u16 recv_timeout;
 
 
 54
 55	nfc_digital_cmd_complete_t cb;
 56	void *arg;
 57
 58	u8 dropframe;
 59};
 60
 61struct nfcsim_link {
 62	struct mutex lock;
 63
 64	u8 rf_tech;
 65	u8 mode;
 66
 67	u8 shutdown;
 
 68
 69	struct sk_buff *skb;
 70	wait_queue_head_t recv_wait;
 71	u8 cond;
 72};
 73
 74static struct nfcsim_link *nfcsim_link_new(void)
 
 
 
 
 
 75{
 76	struct nfcsim_link *link;
 77
 78	link = kzalloc(sizeof(struct nfcsim_link), GFP_KERNEL);
 79	if (!link)
 80		return NULL;
 81
 82	mutex_init(&link->lock);
 83	init_waitqueue_head(&link->recv_wait);
 
 
 
 84
 85	return link;
 
 
 
 86}
 87
 88static void nfcsim_link_free(struct nfcsim_link *link)
 89{
 90	dev_kfree_skb(link->skb);
 91	kfree(link);
 92}
 93
 94static void nfcsim_link_recv_wake(struct nfcsim_link *link)
 95{
 96	link->cond = 1;
 97	wake_up_interruptible(&link->recv_wait);
 98}
 99
100static void nfcsim_link_set_skb(struct nfcsim_link *link, struct sk_buff *skb,
101				u8 rf_tech, u8 mode)
102{
103	mutex_lock(&link->lock);
104
105	dev_kfree_skb(link->skb);
106	link->skb = skb;
107	link->rf_tech = rf_tech;
108	link->mode = mode;
109
110	mutex_unlock(&link->lock);
111}
112
113static void nfcsim_link_recv_cancel(struct nfcsim_link *link)
114{
115	mutex_lock(&link->lock);
 
 
116
117	link->mode = NFCSIM_MODE_NONE;
118
119	mutex_unlock(&link->lock);
120
121	nfcsim_link_recv_wake(link);
 
 
122}
123
124static void nfcsim_link_shutdown(struct nfcsim_link *link)
125{
126	mutex_lock(&link->lock);
 
 
 
 
127
128	link->shutdown = 1;
129	link->mode = NFCSIM_MODE_NONE;
130
131	mutex_unlock(&link->lock);
132
133	nfcsim_link_recv_wake(link);
134}
135
136static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link,
137					    int timeout, u8 rf_tech, u8 mode)
 
138{
139	int rc;
140	struct sk_buff *skb;
 
 
 
141
142	rc = wait_event_interruptible_timeout(link->recv_wait,
143					      link->cond,
144					      msecs_to_jiffies(timeout));
145
146	mutex_lock(&link->lock);
147
148	skb = link->skb;
149	link->skb = NULL;
150
151	if (!rc) {
152		rc = -ETIMEDOUT;
153		goto done;
154	}
155
156	if (!skb || link->rf_tech != rf_tech || link->mode == mode) {
157		rc = -EINVAL;
158		goto done;
159	}
160
161	if (link->shutdown) {
162		rc = -ENODEV;
163		goto done;
164	}
165
166done:
167	mutex_unlock(&link->lock);
168
169	if (rc < 0) {
170		dev_kfree_skb(skb);
171		skb = ERR_PTR(rc);
 
 
172	}
173
174	link->cond = 0;
 
175
176	return skb;
 
 
177}
178
179static void nfcsim_send_wq(struct work_struct *work)
180{
181	struct nfcsim *dev = container_of(work, struct nfcsim, send_work.work);
 
 
182
183	/*
184	 * To effectively send data, the device just wake up its link_out which
185	 * is the link_in of the peer device. The exchanged skb has already been
186	 * stored in the dev->link_out through nfcsim_link_set_skb().
187	 */
188	nfcsim_link_recv_wake(dev->link_out);
189}
190
191static void nfcsim_recv_wq(struct work_struct *work)
 
192{
193	struct nfcsim *dev = container_of(work, struct nfcsim, recv_work);
194	struct sk_buff *skb;
195
196	skb = nfcsim_link_recv_skb(dev->link_in, dev->recv_timeout,
197				   dev->rf_tech, dev->mode);
198
199	if (!dev->up) {
200		NFCSIM_ERR(dev, "Device is down\n");
 
 
 
 
 
 
201
202		if (!IS_ERR(skb))
203			dev_kfree_skb(skb);
204
205		skb = ERR_PTR(-ENODEV);
 
 
 
206	}
207
208	dev->cb(dev->nfc_digital_dev, dev->arg, skb);
209}
210
211static int nfcsim_send(struct nfc_digital_dev *ddev, struct sk_buff *skb,
212		       u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
213{
214	struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
215	u8 delay;
216
217	if (!dev->up) {
218		NFCSIM_ERR(dev, "Device is down\n");
219		return -ENODEV;
220	}
221
222	dev->recv_timeout = timeout;
223	dev->cb = cb;
224	dev->arg = arg;
225
226	schedule_work(&dev->recv_work);
 
227
228	if (dev->dropframe) {
229		NFCSIM_DBG(dev, "dropping frame (out of %d)\n", dev->dropframe);
230		dev_kfree_skb(skb);
231		dev->dropframe--;
232
233		return 0;
234	}
235
236	if (skb) {
237		nfcsim_link_set_skb(dev->link_out, skb, dev->rf_tech,
238				    dev->mode);
239
240		/* Add random delay (between 3 and 10 ms) before sending data */
241		get_random_bytes(&delay, 1);
242		delay = 3 + (delay & 0x07);
243
244		schedule_delayed_work(&dev->send_work, msecs_to_jiffies(delay));
245	}
246
247	return 0;
248}
249
250static void nfcsim_abort_cmd(struct nfc_digital_dev *ddev)
 
251{
252	struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
253
254	nfcsim_link_recv_cancel(dev->link_in);
 
 
255}
256
257static int nfcsim_switch_rf(struct nfc_digital_dev *ddev, bool on)
 
258{
259	struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
260
261	dev->up = on;
262
263	return 0;
264}
265
266static int nfcsim_in_configure_hw(struct nfc_digital_dev *ddev,
267					  int type, int param)
268{
269	struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
 
270
271	switch (type) {
272	case NFC_DIGITAL_CONFIG_RF_TECH:
273		dev->up = true;
274		dev->mode = NFCSIM_MODE_INITIATOR;
275		dev->rf_tech = param;
276		break;
277
278	case NFC_DIGITAL_CONFIG_FRAMING:
279		break;
 
 
 
 
 
 
 
 
 
280
281	default:
282		NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type);
283		return -EINVAL;
 
284	}
285
286	return 0;
287}
288
289static int nfcsim_in_send_cmd(struct nfc_digital_dev *ddev,
290			       struct sk_buff *skb, u16 timeout,
291			       nfc_digital_cmd_complete_t cb, void *arg)
292{
293	return nfcsim_send(ddev, skb, timeout, cb, arg);
294}
295
296static int nfcsim_tg_configure_hw(struct nfc_digital_dev *ddev,
297					  int type, int param)
 
298{
299	struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
 
 
300
301	switch (type) {
302	case NFC_DIGITAL_CONFIG_RF_TECH:
303		dev->up = true;
304		dev->mode = NFCSIM_MODE_TARGET;
305		dev->rf_tech = param;
306		break;
307
308	case NFC_DIGITAL_CONFIG_FRAMING:
309		break;
310
311	default:
312		NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type);
313		return -EINVAL;
314	}
315
316	return 0;
317}
318
319static int nfcsim_tg_send_cmd(struct nfc_digital_dev *ddev,
320			       struct sk_buff *skb, u16 timeout,
321			       nfc_digital_cmd_complete_t cb, void *arg)
322{
323	return nfcsim_send(ddev, skb, timeout, cb, arg);
324}
325
326static int nfcsim_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
327			    nfc_digital_cmd_complete_t cb, void *arg)
328{
329	return nfcsim_send(ddev, NULL, timeout, cb, arg);
330}
331
332static struct nfc_digital_ops nfcsim_digital_ops = {
333	.in_configure_hw = nfcsim_in_configure_hw,
334	.in_send_cmd = nfcsim_in_send_cmd,
335
336	.tg_listen = nfcsim_tg_listen,
337	.tg_configure_hw = nfcsim_tg_configure_hw,
338	.tg_send_cmd = nfcsim_tg_send_cmd,
 
 
 
339
340	.abort_cmd = nfcsim_abort_cmd,
341	.switch_rf = nfcsim_switch_rf,
342};
343
344static struct dentry *nfcsim_debugfs_root;
 
 
345
346static void nfcsim_debugfs_init(void)
347{
348	nfcsim_debugfs_root = debugfs_create_dir("nfcsim", NULL);
349
350	if (!nfcsim_debugfs_root)
351		pr_err("Could not create debugfs entry\n");
 
352
 
353}
354
355static void nfcsim_debugfs_remove(void)
 
 
356{
357	debugfs_remove_recursive(nfcsim_debugfs_root);
358}
359
360static void nfcsim_debugfs_init_dev(struct nfcsim *dev)
361{
362	struct dentry *dev_dir;
363	char devname[5]; /* nfcX\0 */
364	u32 idx;
365	int n;
 
 
 
 
 
 
 
 
 
 
 
366
367	if (!nfcsim_debugfs_root) {
368		NFCSIM_ERR(dev, "nfcsim debugfs not initialized\n");
 
 
369		return;
370	}
371
372	idx = dev->nfc_digital_dev->nfc_dev->idx;
373	n = snprintf(devname, sizeof(devname), "nfc%d", idx);
374	if (n >= sizeof(devname)) {
375		NFCSIM_ERR(dev, "Could not compute dev name for dev %d\n", idx);
 
 
376		return;
377	}
378
379	dev_dir = debugfs_create_dir(devname, nfcsim_debugfs_root);
380	if (!dev_dir) {
381		NFCSIM_ERR(dev, "Could not create debugfs entries for nfc%d\n",
382			   idx);
383		return;
384	}
385
386	debugfs_create_u8("dropframe", 0664, dev_dir, &dev->dropframe);
387}
388
389static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in,
390					struct nfcsim_link *link_out)
391{
392	struct nfcsim *dev;
393	int rc;
394
395	dev = kzalloc(sizeof(struct nfcsim), GFP_KERNEL);
396	if (!dev)
397		return ERR_PTR(-ENOMEM);
 
 
398
399	INIT_DELAYED_WORK(&dev->send_work, nfcsim_send_wq);
400	INIT_WORK(&dev->recv_work, nfcsim_recv_wq);
401
402	dev->nfc_digital_dev =
403			nfc_digital_allocate_device(&nfcsim_digital_ops,
404						    NFC_PROTO_NFC_DEP_MASK,
405						    NFCSIM_CAPABILITIES,
406						    0, 0);
407	if (!dev->nfc_digital_dev) {
408		kfree(dev);
409		return ERR_PTR(-ENOMEM);
410	}
411
412	nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
 
 
413
414	dev->link_in = link_in;
415	dev->link_out = link_out;
416
417	rc = nfc_digital_register_device(dev->nfc_digital_dev);
418	if (rc) {
419		pr_err("Could not register digital device (%d)\n", rc);
420		nfc_digital_free_device(dev->nfc_digital_dev);
421		kfree(dev);
 
 
422
423		return ERR_PTR(rc);
424	}
425
426	nfcsim_debugfs_init_dev(dev);
 
 
 
 
 
 
 
 
 
427
428	return dev;
 
 
429}
430
431static void nfcsim_device_free(struct nfcsim *dev)
432{
433	nfc_digital_unregister_device(dev->nfc_digital_dev);
 
434
435	dev->up = false;
 
 
436
437	nfcsim_link_shutdown(dev->link_in);
438
439	cancel_delayed_work_sync(&dev->send_work);
440	cancel_work_sync(&dev->recv_work);
441
442	nfc_digital_free_device(dev->nfc_digital_dev);
 
 
 
 
443
 
 
 
 
 
 
 
 
 
 
 
 
 
444	kfree(dev);
 
 
445}
446
447static struct nfcsim *dev0;
448static struct nfcsim *dev1;
 
 
 
 
 
 
449
450static int __init nfcsim_init(void)
451{
452	struct nfcsim_link *link0, *link1;
453	int rc;
454
455	link0 = nfcsim_link_new();
456	link1 = nfcsim_link_new();
457	if (!link0 || !link1) {
 
 
458		rc = -ENOMEM;
459		goto exit_err;
460	}
461
462	nfcsim_debugfs_init();
463
464	dev0 = nfcsim_device_new(link0, link1);
465	if (IS_ERR(dev0)) {
466		rc = PTR_ERR(dev0);
467		goto exit_err;
468	}
469
470	dev1 = nfcsim_device_new(link1, link0);
471	if (IS_ERR(dev1)) {
472		nfcsim_device_free(dev0);
473
474		rc = PTR_ERR(dev1);
475		goto exit_err;
476	}
477
478	pr_info("nfcsim " NFCSIM_VERSION " initialized\n");
479
480	return 0;
481
482exit_err:
483	pr_err("Failed to initialize nfcsim driver (%d)\n", rc);
484
485	nfcsim_link_free(link0);
486	nfcsim_link_free(link1);
 
 
 
487
488	return rc;
489}
490
491static void __exit nfcsim_exit(void)
492{
493	struct nfcsim_link *link0, *link1;
494
495	link0 = dev0->link_in;
496	link1 = dev0->link_out;
497
498	nfcsim_device_free(dev0);
499	nfcsim_device_free(dev1);
500
501	nfcsim_link_free(link0);
502	nfcsim_link_free(link1);
503
504	nfcsim_debugfs_remove();
505}
506
507module_init(nfcsim_init);
508module_exit(nfcsim_exit);
509
510MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION);
511MODULE_VERSION(NFCSIM_VERSION);
512MODULE_LICENSE("GPL");