1 // SPDX-License-Identifier: GPL-2.0-only
3 * NFC hardware simulation driver
4 * Copyright (c) 2013, Intel Corporation.
7 #include <linux/device.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/debugfs.h>
12 #include <linux/nfc.h>
13 #include <net/nfc/nfc.h>
14 #include <net/nfc/digital.h>
16 #define NFCSIM_ERR(d, fmt, args...) nfc_err(&d->nfc_digital_dev->nfc_dev->dev, \
17 "%s: " fmt, __func__, ## args)
19 #define NFCSIM_DBG(d, fmt, args...) dev_dbg(&d->nfc_digital_dev->nfc_dev->dev, \
20 "%s: " fmt, __func__, ## args)
22 #define NFCSIM_VERSION "0.2"
24 #define NFCSIM_MODE_NONE 0
25 #define NFCSIM_MODE_INITIATOR 1
26 #define NFCSIM_MODE_TARGET 2
28 #define NFCSIM_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
29 NFC_DIGITAL_DRV_CAPS_TG_CRC)
32 struct nfc_digital_dev
*nfc_digital_dev
;
34 struct work_struct recv_work
;
35 struct delayed_work send_work
;
37 struct nfcsim_link
*link_in
;
38 struct nfcsim_link
*link_out
;
46 nfc_digital_cmd_complete_t cb
;
61 wait_queue_head_t recv_wait
;
65 static struct nfcsim_link
*nfcsim_link_new(void)
67 struct nfcsim_link
*link
;
69 link
= kzalloc(sizeof(struct nfcsim_link
), GFP_KERNEL
);
73 mutex_init(&link
->lock
);
74 init_waitqueue_head(&link
->recv_wait
);
79 static void nfcsim_link_free(struct nfcsim_link
*link
)
81 dev_kfree_skb(link
->skb
);
85 static void nfcsim_link_recv_wake(struct nfcsim_link
*link
)
88 wake_up_interruptible(&link
->recv_wait
);
91 static void nfcsim_link_set_skb(struct nfcsim_link
*link
, struct sk_buff
*skb
,
94 mutex_lock(&link
->lock
);
96 dev_kfree_skb(link
->skb
);
98 link
->rf_tech
= rf_tech
;
101 mutex_unlock(&link
->lock
);
104 static void nfcsim_link_recv_cancel(struct nfcsim_link
*link
)
106 mutex_lock(&link
->lock
);
108 link
->mode
= NFCSIM_MODE_NONE
;
110 mutex_unlock(&link
->lock
);
112 nfcsim_link_recv_wake(link
);
115 static void nfcsim_link_shutdown(struct nfcsim_link
*link
)
117 mutex_lock(&link
->lock
);
120 link
->mode
= NFCSIM_MODE_NONE
;
122 mutex_unlock(&link
->lock
);
124 nfcsim_link_recv_wake(link
);
127 static struct sk_buff
*nfcsim_link_recv_skb(struct nfcsim_link
*link
,
128 int timeout
, u8 rf_tech
, u8 mode
)
133 rc
= wait_event_interruptible_timeout(link
->recv_wait
,
135 msecs_to_jiffies(timeout
));
137 mutex_lock(&link
->lock
);
147 if (!skb
|| link
->rf_tech
!= rf_tech
|| link
->mode
== mode
) {
152 if (link
->shutdown
) {
158 mutex_unlock(&link
->lock
);
170 static void nfcsim_send_wq(struct work_struct
*work
)
172 struct nfcsim
*dev
= container_of(work
, struct nfcsim
, send_work
.work
);
175 * To effectively send data, the device just wake up its link_out which
176 * is the link_in of the peer device. The exchanged skb has already been
177 * stored in the dev->link_out through nfcsim_link_set_skb().
179 nfcsim_link_recv_wake(dev
->link_out
);
182 static void nfcsim_recv_wq(struct work_struct
*work
)
184 struct nfcsim
*dev
= container_of(work
, struct nfcsim
, recv_work
);
187 skb
= nfcsim_link_recv_skb(dev
->link_in
, dev
->recv_timeout
,
188 dev
->rf_tech
, dev
->mode
);
191 NFCSIM_ERR(dev
, "Device is down\n");
198 dev
->cb(dev
->nfc_digital_dev
, dev
->arg
, skb
);
201 static int nfcsim_send(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
,
202 u16 timeout
, nfc_digital_cmd_complete_t cb
, void *arg
)
204 struct nfcsim
*dev
= nfc_digital_get_drvdata(ddev
);
208 NFCSIM_ERR(dev
, "Device is down\n");
212 dev
->recv_timeout
= timeout
;
216 schedule_work(&dev
->recv_work
);
218 if (dev
->dropframe
) {
219 NFCSIM_DBG(dev
, "dropping frame (out of %d)\n", dev
->dropframe
);
227 nfcsim_link_set_skb(dev
->link_out
, skb
, dev
->rf_tech
,
230 /* Add random delay (between 3 and 10 ms) before sending data */
231 get_random_bytes(&delay
, 1);
232 delay
= 3 + (delay
& 0x07);
234 schedule_delayed_work(&dev
->send_work
, msecs_to_jiffies(delay
));
240 static void nfcsim_abort_cmd(struct nfc_digital_dev
*ddev
)
242 const struct nfcsim
*dev
= nfc_digital_get_drvdata(ddev
);
244 nfcsim_link_recv_cancel(dev
->link_in
);
247 static int nfcsim_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
249 struct nfcsim
*dev
= nfc_digital_get_drvdata(ddev
);
256 static int nfcsim_in_configure_hw(struct nfc_digital_dev
*ddev
,
259 struct nfcsim
*dev
= nfc_digital_get_drvdata(ddev
);
262 case NFC_DIGITAL_CONFIG_RF_TECH
:
264 dev
->mode
= NFCSIM_MODE_INITIATOR
;
265 dev
->rf_tech
= param
;
268 case NFC_DIGITAL_CONFIG_FRAMING
:
272 NFCSIM_ERR(dev
, "Invalid configuration type: %d\n", type
);
279 static int nfcsim_in_send_cmd(struct nfc_digital_dev
*ddev
,
280 struct sk_buff
*skb
, u16 timeout
,
281 nfc_digital_cmd_complete_t cb
, void *arg
)
283 return nfcsim_send(ddev
, skb
, timeout
, cb
, arg
);
286 static int nfcsim_tg_configure_hw(struct nfc_digital_dev
*ddev
,
289 struct nfcsim
*dev
= nfc_digital_get_drvdata(ddev
);
292 case NFC_DIGITAL_CONFIG_RF_TECH
:
294 dev
->mode
= NFCSIM_MODE_TARGET
;
295 dev
->rf_tech
= param
;
298 case NFC_DIGITAL_CONFIG_FRAMING
:
302 NFCSIM_ERR(dev
, "Invalid configuration type: %d\n", type
);
309 static int nfcsim_tg_send_cmd(struct nfc_digital_dev
*ddev
,
310 struct sk_buff
*skb
, u16 timeout
,
311 nfc_digital_cmd_complete_t cb
, void *arg
)
313 return nfcsim_send(ddev
, skb
, timeout
, cb
, arg
);
316 static int nfcsim_tg_listen(struct nfc_digital_dev
*ddev
, u16 timeout
,
317 nfc_digital_cmd_complete_t cb
, void *arg
)
319 return nfcsim_send(ddev
, NULL
, timeout
, cb
, arg
);
322 static const struct nfc_digital_ops nfcsim_digital_ops
= {
323 .in_configure_hw
= nfcsim_in_configure_hw
,
324 .in_send_cmd
= nfcsim_in_send_cmd
,
326 .tg_listen
= nfcsim_tg_listen
,
327 .tg_configure_hw
= nfcsim_tg_configure_hw
,
328 .tg_send_cmd
= nfcsim_tg_send_cmd
,
330 .abort_cmd
= nfcsim_abort_cmd
,
331 .switch_rf
= nfcsim_switch_rf
,
334 static struct dentry
*nfcsim_debugfs_root
;
336 static void nfcsim_debugfs_init(void)
338 nfcsim_debugfs_root
= debugfs_create_dir("nfcsim", NULL
);
340 if (!nfcsim_debugfs_root
)
341 pr_err("Could not create debugfs entry\n");
345 static void nfcsim_debugfs_remove(void)
347 debugfs_remove_recursive(nfcsim_debugfs_root
);
350 static void nfcsim_debugfs_init_dev(struct nfcsim
*dev
)
352 struct dentry
*dev_dir
;
353 char devname
[5]; /* nfcX\0 */
357 if (!nfcsim_debugfs_root
) {
358 NFCSIM_ERR(dev
, "nfcsim debugfs not initialized\n");
362 idx
= dev
->nfc_digital_dev
->nfc_dev
->idx
;
363 n
= snprintf(devname
, sizeof(devname
), "nfc%d", idx
);
364 if (n
>= sizeof(devname
)) {
365 NFCSIM_ERR(dev
, "Could not compute dev name for dev %d\n", idx
);
369 dev_dir
= debugfs_create_dir(devname
, nfcsim_debugfs_root
);
371 NFCSIM_ERR(dev
, "Could not create debugfs entries for nfc%d\n",
376 debugfs_create_u8("dropframe", 0664, dev_dir
, &dev
->dropframe
);
379 static struct nfcsim
*nfcsim_device_new(struct nfcsim_link
*link_in
,
380 struct nfcsim_link
*link_out
)
385 dev
= kzalloc(sizeof(struct nfcsim
), GFP_KERNEL
);
387 return ERR_PTR(-ENOMEM
);
389 INIT_DELAYED_WORK(&dev
->send_work
, nfcsim_send_wq
);
390 INIT_WORK(&dev
->recv_work
, nfcsim_recv_wq
);
392 dev
->nfc_digital_dev
=
393 nfc_digital_allocate_device(&nfcsim_digital_ops
,
394 NFC_PROTO_NFC_DEP_MASK
,
397 if (!dev
->nfc_digital_dev
) {
399 return ERR_PTR(-ENOMEM
);
402 nfc_digital_set_drvdata(dev
->nfc_digital_dev
, dev
);
404 dev
->link_in
= link_in
;
405 dev
->link_out
= link_out
;
407 rc
= nfc_digital_register_device(dev
->nfc_digital_dev
);
409 pr_err("Could not register digital device (%d)\n", rc
);
410 nfc_digital_free_device(dev
->nfc_digital_dev
);
416 nfcsim_debugfs_init_dev(dev
);
421 static void nfcsim_device_free(struct nfcsim
*dev
)
423 nfc_digital_unregister_device(dev
->nfc_digital_dev
);
427 nfcsim_link_shutdown(dev
->link_in
);
429 cancel_delayed_work_sync(&dev
->send_work
);
430 cancel_work_sync(&dev
->recv_work
);
432 nfc_digital_free_device(dev
->nfc_digital_dev
);
437 static struct nfcsim
*dev0
;
438 static struct nfcsim
*dev1
;
440 static int __init
nfcsim_init(void)
442 struct nfcsim_link
*link0
, *link1
;
445 link0
= nfcsim_link_new();
446 link1
= nfcsim_link_new();
447 if (!link0
|| !link1
) {
452 nfcsim_debugfs_init();
454 dev0
= nfcsim_device_new(link0
, link1
);
460 dev1
= nfcsim_device_new(link1
, link0
);
462 nfcsim_device_free(dev0
);
468 pr_info("nfcsim " NFCSIM_VERSION
" initialized\n");
473 pr_err("Failed to initialize nfcsim driver (%d)\n", rc
);
476 nfcsim_link_free(link0
);
478 nfcsim_link_free(link1
);
483 static void __exit
nfcsim_exit(void)
485 struct nfcsim_link
*link0
, *link1
;
487 link0
= dev0
->link_in
;
488 link1
= dev0
->link_out
;
490 nfcsim_device_free(dev0
);
491 nfcsim_device_free(dev1
);
493 nfcsim_link_free(link0
);
494 nfcsim_link_free(link1
);
496 nfcsim_debugfs_remove();
499 module_init(nfcsim_init
);
500 module_exit(nfcsim_exit
);
502 MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION
);
503 MODULE_VERSION(NFCSIM_VERSION
);
504 MODULE_LICENSE("GPL");