2 * NFC hardware simulation driver
3 * Copyright (c) 2013, Intel Corporation.
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.
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
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>
22 #define DEV_ERR(_dev, fmt, args...) nfc_err(&_dev->nfc_dev->dev, \
23 "%s: " fmt, __func__, ## args)
25 #define DEV_DBG(_dev, fmt, args...) dev_dbg(&_dev->nfc_dev->dev, \
26 "%s: " fmt, __func__, ## args)
28 #define NFCSIM_VERSION "0.1"
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)
35 #define RX_DEFAULT_DELAY 5
38 struct nfc_dev
*nfc_dev
;
42 struct delayed_work recv_work
;
44 struct sk_buff
*clone_skb
;
46 struct delayed_work poll_work
;
58 data_exchange_cb_t cb
;
61 struct nfcsim
*peer_dev
;
64 static struct nfcsim
*dev0
;
65 static struct nfcsim
*dev1
;
67 static struct workqueue_struct
*wq
;
69 static void nfcsim_cleanup_dev(struct nfcsim
*dev
, u8 shutdown
)
71 DEV_DBG(dev
, "shutdown=%d\n", shutdown
);
73 mutex_lock(&dev
->lock
);
75 dev
->polling_mode
= NFCSIM_POLL_NONE
;
76 dev
->shutting_down
= shutdown
;
78 dev_kfree_skb(dev
->clone_skb
);
79 dev
->clone_skb
= NULL
;
81 mutex_unlock(&dev
->lock
);
83 cancel_delayed_work_sync(&dev
->poll_work
);
84 cancel_delayed_work_sync(&dev
->recv_work
);
87 static int nfcsim_target_found(struct nfcsim
*dev
)
89 struct nfc_target nfc_tgt
;
93 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
95 nfc_tgt
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
96 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
101 static int nfcsim_dev_up(struct nfc_dev
*nfc_dev
)
103 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
107 mutex_lock(&dev
->lock
);
111 mutex_unlock(&dev
->lock
);
116 static int nfcsim_dev_down(struct nfc_dev
*nfc_dev
)
118 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
122 mutex_lock(&dev
->lock
);
126 mutex_unlock(&dev
->lock
);
131 static 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
)
136 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
137 struct nfcsim
*peer
= dev
->peer_dev
;
139 size_t remote_gb_len
;
141 DEV_DBG(dev
, "target_idx: %d, comm_mode: %d\n", target
->idx
, comm_mode
);
143 mutex_lock(&peer
->lock
);
145 nfc_tm_activated(peer
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
146 NFC_COMM_ACTIVE
, gb
, gb_len
);
148 remote_gb
= nfc_get_local_general_bytes(peer
->nfc_dev
, &remote_gb_len
);
150 DEV_ERR(peer
, "Can't get remote general bytes\n");
152 mutex_unlock(&peer
->lock
);
156 mutex_unlock(&peer
->lock
);
158 mutex_lock(&dev
->lock
);
160 rc
= nfc_set_remote_general_bytes(nfc_dev
, remote_gb
, remote_gb_len
);
162 DEV_ERR(dev
, "Can't set remote general bytes\n");
163 mutex_unlock(&dev
->lock
);
167 rc
= nfc_dep_link_is_up(nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
170 mutex_unlock(&dev
->lock
);
175 static int nfcsim_dep_link_down(struct nfc_dev
*nfc_dev
)
177 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
181 nfcsim_cleanup_dev(dev
, 0);
186 static int nfcsim_start_poll(struct nfc_dev
*nfc_dev
,
187 u32 im_protocols
, u32 tm_protocols
)
189 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
192 mutex_lock(&dev
->lock
);
194 if (dev
->polling_mode
!= NFCSIM_POLL_NONE
) {
195 DEV_ERR(dev
, "Already in polling mode\n");
200 if (im_protocols
& NFC_PROTO_NFC_DEP_MASK
)
201 dev
->polling_mode
|= NFCSIM_POLL_INITIATOR
;
203 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
)
204 dev
->polling_mode
|= NFCSIM_POLL_TARGET
;
206 if (dev
->polling_mode
== NFCSIM_POLL_NONE
) {
207 DEV_ERR(dev
, "Unsupported polling mode\n");
213 dev
->curr_polling_mode
= NFCSIM_POLL_NONE
;
215 queue_delayed_work(wq
, &dev
->poll_work
, 0);
217 DEV_DBG(dev
, "Start polling: im: 0x%X, tm: 0x%X\n", im_protocols
,
222 mutex_unlock(&dev
->lock
);
227 static void nfcsim_stop_poll(struct nfc_dev
*nfc_dev
)
229 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
231 DEV_DBG(dev
, "Stop poll\n");
233 mutex_lock(&dev
->lock
);
235 dev
->polling_mode
= NFCSIM_POLL_NONE
;
237 mutex_unlock(&dev
->lock
);
239 cancel_delayed_work_sync(&dev
->poll_work
);
242 static int nfcsim_activate_target(struct nfc_dev
*nfc_dev
,
243 struct nfc_target
*target
, u32 protocol
)
245 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
252 static void nfcsim_deactivate_target(struct nfc_dev
*nfc_dev
,
253 struct nfc_target
*target
, u8 mode
)
255 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
260 static void nfcsim_wq_recv(struct work_struct
*work
)
262 struct nfcsim
*dev
= container_of(work
, struct nfcsim
,
265 mutex_lock(&dev
->lock
);
267 if (dev
->shutting_down
|| !dev
->up
|| !dev
->clone_skb
) {
268 dev_kfree_skb(dev
->clone_skb
);
272 if (dev
->initiator
) {
274 DEV_ERR(dev
, "Null recv callback\n");
275 dev_kfree_skb(dev
->clone_skb
);
279 dev
->cb(dev
->cb_context
, dev
->clone_skb
, 0);
282 nfc_tm_data_received(dev
->nfc_dev
, dev
->clone_skb
);
286 dev
->clone_skb
= NULL
;
288 mutex_unlock(&dev
->lock
);
291 static int nfcsim_tx(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
292 struct sk_buff
*skb
, data_exchange_cb_t cb
,
295 struct nfcsim
*dev
= nfc_get_drvdata(nfc_dev
);
296 struct nfcsim
*peer
= dev
->peer_dev
;
299 mutex_lock(&dev
->lock
);
301 if (dev
->shutting_down
|| !dev
->up
) {
302 mutex_unlock(&dev
->lock
);
308 dev
->cb_context
= cb_context
;
310 mutex_unlock(&dev
->lock
);
312 mutex_lock(&peer
->lock
);
314 peer
->clone_skb
= skb_clone(skb
, GFP_KERNEL
);
316 if (!peer
->clone_skb
) {
317 DEV_ERR(dev
, "skb_clone failed\n");
318 mutex_unlock(&peer
->lock
);
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
328 queue_delayed_work(wq
, &peer
->recv_work
,
329 msecs_to_jiffies(dev
->rx_delay
));
331 mutex_unlock(&peer
->lock
);
340 static 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
)
344 return nfcsim_tx(nfc_dev
, target
, skb
, cb
, cb_context
);
347 static int nfcsim_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
349 return nfcsim_tx(nfc_dev
, NULL
, skb
, NULL
, NULL
);
352 static 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
,
365 static void nfcsim_set_polling_mode(struct nfcsim
*dev
)
367 if (dev
->polling_mode
== NFCSIM_POLL_NONE
) {
368 dev
->curr_polling_mode
= NFCSIM_POLL_NONE
;
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
;
376 dev
->curr_polling_mode
= NFCSIM_POLL_TARGET
;
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
;
385 dev
->curr_polling_mode
= NFCSIM_POLL_TARGET
;
389 static void nfcsim_wq_poll(struct work_struct
*work
)
391 struct nfcsim
*dev
= container_of(work
, struct nfcsim
, poll_work
.work
);
392 struct nfcsim
*peer
= dev
->peer_dev
;
394 /* These work items run on an ordered workqueue and are therefore
395 * serialized. So we can take both mutexes without being dead locked.
397 mutex_lock(&dev
->lock
);
398 mutex_lock(&peer
->lock
);
400 nfcsim_set_polling_mode(dev
);
402 if (dev
->curr_polling_mode
== NFCSIM_POLL_NONE
) {
403 DEV_DBG(dev
, "Not polling\n");
407 DEV_DBG(dev
, "Polling as %s",
408 dev
->curr_polling_mode
== NFCSIM_POLL_INITIATOR
?
409 "initiator\n" : "target\n");
411 if (dev
->curr_polling_mode
== NFCSIM_POLL_TARGET
)
414 if (peer
->curr_polling_mode
== NFCSIM_POLL_TARGET
) {
415 peer
->polling_mode
= NFCSIM_POLL_NONE
;
416 dev
->polling_mode
= NFCSIM_POLL_NONE
;
420 nfcsim_target_found(dev
);
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.
434 queue_delayed_work(wq
, &dev
->poll_work
, msecs_to_jiffies(200));
437 mutex_unlock(&peer
->lock
);
438 mutex_unlock(&dev
->lock
);
441 static struct nfcsim
*nfcsim_init_dev(void)
446 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
448 return ERR_PTR(-ENOMEM
);
450 mutex_init(&dev
->lock
);
452 INIT_DELAYED_WORK(&dev
->recv_work
, nfcsim_wq_recv
);
453 INIT_DELAYED_WORK(&dev
->poll_work
, nfcsim_wq_poll
);
455 dev
->nfc_dev
= nfc_allocate_device(&nfcsim_nfc_ops
,
456 NFC_PROTO_NFC_DEP_MASK
,
461 nfc_set_drvdata(dev
->nfc_dev
, dev
);
463 rc
= nfc_register_device(dev
->nfc_dev
);
467 dev
->rx_delay
= RX_DEFAULT_DELAY
;
471 nfc_free_device(dev
->nfc_dev
);
479 static void nfcsim_free_device(struct nfcsim
*dev
)
481 nfc_unregister_device(dev
->nfc_dev
);
483 nfc_free_device(dev
->nfc_dev
);
488 static int __init
nfcsim_init(void)
492 /* We need an ordered wq to ensure that poll_work items are executed
495 wq
= alloc_ordered_workqueue("nfcsim", 0);
501 dev0
= nfcsim_init_dev();
507 dev1
= nfcsim_init_dev();
515 dev0
->peer_dev
= dev1
;
516 dev1
->peer_dev
= dev0
;
518 pr_debug("NFCsim " NFCSIM_VERSION
" initialized\n");
523 pr_err("Failed to initialize nfcsim driver (%d)\n",
529 static void __exit
nfcsim_exit(void)
531 nfcsim_cleanup_dev(dev0
, 1);
532 nfcsim_cleanup_dev(dev1
, 1);
534 nfcsim_free_device(dev0
);
535 nfcsim_free_device(dev1
);
537 destroy_workqueue(wq
);
540 module_init(nfcsim_init
);
541 module_exit(nfcsim_exit
);
543 MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION
);
544 MODULE_VERSION(NFCSIM_VERSION
);
545 MODULE_LICENSE("GPL");