m68k: Delete an unnecessary variable assignment in sys_cacheflush()
[linux-2.6/btrfs-unstable.git] / drivers / nfc / nfcsim.c
bloba466e79784668ef44955aad0d2b61cc9e1a5b3b6
1 /*
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
12 * more details.
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>
25 #define NFCSIM_ERR(d, fmt, args...) nfc_err(&d->nfc_digital_dev->nfc_dev->dev, \
26 "%s: " fmt, __func__, ## args)
28 #define NFCSIM_DBG(d, fmt, args...) dev_dbg(&d->nfc_digital_dev->nfc_dev->dev, \
29 "%s: " fmt, __func__, ## args)
31 #define NFCSIM_VERSION "0.2"
33 #define NFCSIM_MODE_NONE 0
34 #define NFCSIM_MODE_INITIATOR 1
35 #define NFCSIM_MODE_TARGET 2
37 #define NFCSIM_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
38 NFC_DIGITAL_DRV_CAPS_TG_CRC)
40 struct nfcsim {
41 struct nfc_digital_dev *nfc_digital_dev;
43 struct work_struct recv_work;
44 struct delayed_work send_work;
46 struct nfcsim_link *link_in;
47 struct nfcsim_link *link_out;
49 bool up;
50 u8 mode;
51 u8 rf_tech;
53 u16 recv_timeout;
55 nfc_digital_cmd_complete_t cb;
56 void *arg;
58 u8 dropframe;
61 struct nfcsim_link {
62 struct mutex lock;
64 u8 rf_tech;
65 u8 mode;
67 u8 shutdown;
69 struct sk_buff *skb;
70 wait_queue_head_t recv_wait;
71 u8 cond;
74 static struct nfcsim_link *nfcsim_link_new(void)
76 struct nfcsim_link *link;
78 link = kzalloc(sizeof(struct nfcsim_link), GFP_KERNEL);
79 if (!link)
80 return NULL;
82 mutex_init(&link->lock);
83 init_waitqueue_head(&link->recv_wait);
85 return link;
88 static void nfcsim_link_free(struct nfcsim_link *link)
90 dev_kfree_skb(link->skb);
91 kfree(link);
94 static void nfcsim_link_recv_wake(struct nfcsim_link *link)
96 link->cond = 1;
97 wake_up_interruptible(&link->recv_wait);
100 static void nfcsim_link_set_skb(struct nfcsim_link *link, struct sk_buff *skb,
101 u8 rf_tech, u8 mode)
103 mutex_lock(&link->lock);
105 dev_kfree_skb(link->skb);
106 link->skb = skb;
107 link->rf_tech = rf_tech;
108 link->mode = mode;
110 mutex_unlock(&link->lock);
113 static void nfcsim_link_recv_cancel(struct nfcsim_link *link)
115 mutex_lock(&link->lock);
117 link->mode = NFCSIM_MODE_NONE;
119 mutex_unlock(&link->lock);
121 nfcsim_link_recv_wake(link);
124 static void nfcsim_link_shutdown(struct nfcsim_link *link)
126 mutex_lock(&link->lock);
128 link->shutdown = 1;
129 link->mode = NFCSIM_MODE_NONE;
131 mutex_unlock(&link->lock);
133 nfcsim_link_recv_wake(link);
136 static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link,
137 int timeout, u8 rf_tech, u8 mode)
139 int rc;
140 struct sk_buff *skb;
142 rc = wait_event_interruptible_timeout(link->recv_wait,
143 link->cond,
144 msecs_to_jiffies(timeout));
146 mutex_lock(&link->lock);
148 skb = link->skb;
149 link->skb = NULL;
151 if (!rc) {
152 rc = -ETIMEDOUT;
153 goto done;
156 if (!skb || link->rf_tech != rf_tech || link->mode == mode) {
157 rc = -EINVAL;
158 goto done;
161 if (link->shutdown) {
162 rc = -ENODEV;
163 goto done;
166 done:
167 mutex_unlock(&link->lock);
169 if (rc < 0) {
170 dev_kfree_skb(skb);
171 skb = ERR_PTR(rc);
174 link->cond = 0;
176 return skb;
179 static void nfcsim_send_wq(struct work_struct *work)
181 struct nfcsim *dev = container_of(work, struct nfcsim, send_work.work);
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().
188 nfcsim_link_recv_wake(dev->link_out);
191 static void nfcsim_recv_wq(struct work_struct *work)
193 struct nfcsim *dev = container_of(work, struct nfcsim, recv_work);
194 struct sk_buff *skb;
196 skb = nfcsim_link_recv_skb(dev->link_in, dev->recv_timeout,
197 dev->rf_tech, dev->mode);
199 if (!dev->up) {
200 NFCSIM_ERR(dev, "Device is down\n");
202 if (!IS_ERR(skb))
203 dev_kfree_skb(skb);
205 skb = ERR_PTR(-ENODEV);
208 dev->cb(dev->nfc_digital_dev, dev->arg, skb);
211 static int nfcsim_send(struct nfc_digital_dev *ddev, struct sk_buff *skb,
212 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
214 struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
215 u8 delay;
217 if (!dev->up) {
218 NFCSIM_ERR(dev, "Device is down\n");
219 return -ENODEV;
222 dev->recv_timeout = timeout;
223 dev->cb = cb;
224 dev->arg = arg;
226 schedule_work(&dev->recv_work);
228 if (dev->dropframe) {
229 NFCSIM_DBG(dev, "dropping frame (out of %d)\n", dev->dropframe);
230 dev_kfree_skb(skb);
231 dev->dropframe--;
233 return 0;
236 if (skb) {
237 nfcsim_link_set_skb(dev->link_out, skb, dev->rf_tech,
238 dev->mode);
240 /* Add random delay (between 3 and 10 ms) before sending data */
241 get_random_bytes(&delay, 1);
242 delay = 3 + (delay & 0x07);
244 schedule_delayed_work(&dev->send_work, msecs_to_jiffies(delay));
247 return 0;
250 static void nfcsim_abort_cmd(struct nfc_digital_dev *ddev)
252 struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
254 nfcsim_link_recv_cancel(dev->link_in);
257 static int nfcsim_switch_rf(struct nfc_digital_dev *ddev, bool on)
259 struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
261 dev->up = on;
263 return 0;
266 static int nfcsim_in_configure_hw(struct nfc_digital_dev *ddev,
267 int type, int param)
269 struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
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;
278 case NFC_DIGITAL_CONFIG_FRAMING:
279 break;
281 default:
282 NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type);
283 return -EINVAL;
286 return 0;
289 static 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)
293 return nfcsim_send(ddev, skb, timeout, cb, arg);
296 static int nfcsim_tg_configure_hw(struct nfc_digital_dev *ddev,
297 int type, int param)
299 struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
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;
308 case NFC_DIGITAL_CONFIG_FRAMING:
309 break;
311 default:
312 NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type);
313 return -EINVAL;
316 return 0;
319 static 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)
323 return nfcsim_send(ddev, skb, timeout, cb, arg);
326 static int nfcsim_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
327 nfc_digital_cmd_complete_t cb, void *arg)
329 return nfcsim_send(ddev, NULL, timeout, cb, arg);
332 static struct nfc_digital_ops nfcsim_digital_ops = {
333 .in_configure_hw = nfcsim_in_configure_hw,
334 .in_send_cmd = nfcsim_in_send_cmd,
336 .tg_listen = nfcsim_tg_listen,
337 .tg_configure_hw = nfcsim_tg_configure_hw,
338 .tg_send_cmd = nfcsim_tg_send_cmd,
340 .abort_cmd = nfcsim_abort_cmd,
341 .switch_rf = nfcsim_switch_rf,
344 static struct dentry *nfcsim_debugfs_root;
346 static void nfcsim_debugfs_init(void)
348 nfcsim_debugfs_root = debugfs_create_dir("nfcsim", NULL);
350 if (!nfcsim_debugfs_root)
351 pr_err("Could not create debugfs entry\n");
355 static void nfcsim_debugfs_remove(void)
357 debugfs_remove_recursive(nfcsim_debugfs_root);
360 static void nfcsim_debugfs_init_dev(struct nfcsim *dev)
362 struct dentry *dev_dir;
363 char devname[5]; /* nfcX\0 */
364 u32 idx;
365 int n;
367 if (!nfcsim_debugfs_root) {
368 NFCSIM_ERR(dev, "nfcsim debugfs not initialized\n");
369 return;
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;
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;
386 debugfs_create_u8("dropframe", 0664, dev_dir, &dev->dropframe);
389 static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in,
390 struct nfcsim_link *link_out)
392 struct nfcsim *dev;
393 int rc;
395 dev = kzalloc(sizeof(struct nfcsim), GFP_KERNEL);
396 if (!dev)
397 return ERR_PTR(-ENOMEM);
399 INIT_DELAYED_WORK(&dev->send_work, nfcsim_send_wq);
400 INIT_WORK(&dev->recv_work, nfcsim_recv_wq);
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);
412 nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
414 dev->link_in = link_in;
415 dev->link_out = link_out;
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);
423 return ERR_PTR(rc);
426 nfcsim_debugfs_init_dev(dev);
428 return dev;
431 static void nfcsim_device_free(struct nfcsim *dev)
433 nfc_digital_unregister_device(dev->nfc_digital_dev);
435 dev->up = false;
437 nfcsim_link_shutdown(dev->link_in);
439 cancel_delayed_work_sync(&dev->send_work);
440 cancel_work_sync(&dev->recv_work);
442 nfc_digital_free_device(dev->nfc_digital_dev);
444 kfree(dev);
447 static struct nfcsim *dev0;
448 static struct nfcsim *dev1;
450 static int __init nfcsim_init(void)
452 struct nfcsim_link *link0, *link1;
453 int rc;
455 link0 = nfcsim_link_new();
456 link1 = nfcsim_link_new();
457 if (!link0 || !link1) {
458 rc = -ENOMEM;
459 goto exit_err;
462 nfcsim_debugfs_init();
464 dev0 = nfcsim_device_new(link0, link1);
465 if (IS_ERR(dev0)) {
466 rc = PTR_ERR(dev0);
467 goto exit_err;
470 dev1 = nfcsim_device_new(link1, link0);
471 if (IS_ERR(dev1)) {
472 nfcsim_device_free(dev0);
474 rc = PTR_ERR(dev1);
475 goto exit_err;
478 pr_info("nfcsim " NFCSIM_VERSION " initialized\n");
480 return 0;
482 exit_err:
483 pr_err("Failed to initialize nfcsim driver (%d)\n", rc);
485 nfcsim_link_free(link0);
486 nfcsim_link_free(link1);
488 return rc;
491 static void __exit nfcsim_exit(void)
493 struct nfcsim_link *link0, *link1;
495 link0 = dev0->link_in;
496 link1 = dev0->link_out;
498 nfcsim_device_free(dev0);
499 nfcsim_device_free(dev1);
501 nfcsim_link_free(link0);
502 nfcsim_link_free(link1);
504 nfcsim_debugfs_remove();
507 module_init(nfcsim_init);
508 module_exit(nfcsim_exit);
510 MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION);
511 MODULE_VERSION(NFCSIM_VERSION);
512 MODULE_LICENSE("GPL");