GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / nfc / nfcsim.c
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
40 struct 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
61 struct 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
74 static 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
88 static void nfcsim_link_free(struct nfcsim_link *link)
89 {
90         dev_kfree_skb(link->skb);
91         kfree(link);
92 }
93
94 static void nfcsim_link_recv_wake(struct nfcsim_link *link)
95 {
96         link->cond = 1;
97         wake_up_interruptible(&link->recv_wait);
98 }
99
100 static 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
113 static 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
124 static 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
136 static 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
166 done:
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
179 static 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
191 static 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                 return;
205         }
206
207         dev->cb(dev->nfc_digital_dev, dev->arg, skb);
208 }
209
210 static int nfcsim_send(struct nfc_digital_dev *ddev, struct sk_buff *skb,
211                        u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
212 {
213         struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
214         u8 delay;
215
216         if (!dev->up) {
217                 NFCSIM_ERR(dev, "Device is down\n");
218                 return -ENODEV;
219         }
220
221         dev->recv_timeout = timeout;
222         dev->cb = cb;
223         dev->arg = arg;
224
225         schedule_work(&dev->recv_work);
226
227         if (dev->dropframe) {
228                 NFCSIM_DBG(dev, "dropping frame (out of %d)\n", dev->dropframe);
229                 dev_kfree_skb(skb);
230                 dev->dropframe--;
231
232                 return 0;
233         }
234
235         if (skb) {
236                 nfcsim_link_set_skb(dev->link_out, skb, dev->rf_tech,
237                                     dev->mode);
238
239                 /* Add random delay (between 3 and 10 ms) before sending data */
240                 get_random_bytes(&delay, 1);
241                 delay = 3 + (delay & 0x07);
242
243                 schedule_delayed_work(&dev->send_work, msecs_to_jiffies(delay));
244         }
245
246         return 0;
247 }
248
249 static void nfcsim_abort_cmd(struct nfc_digital_dev *ddev)
250 {
251         struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
252
253         nfcsim_link_recv_cancel(dev->link_in);
254 }
255
256 static int nfcsim_switch_rf(struct nfc_digital_dev *ddev, bool on)
257 {
258         struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
259
260         dev->up = on;
261
262         return 0;
263 }
264
265 static int nfcsim_in_configure_hw(struct nfc_digital_dev *ddev,
266                                           int type, int param)
267 {
268         struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
269
270         switch (type) {
271         case NFC_DIGITAL_CONFIG_RF_TECH:
272                 dev->up = true;
273                 dev->mode = NFCSIM_MODE_INITIATOR;
274                 dev->rf_tech = param;
275                 break;
276
277         case NFC_DIGITAL_CONFIG_FRAMING:
278                 break;
279
280         default:
281                 NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type);
282                 return -EINVAL;
283         }
284
285         return 0;
286 }
287
288 static int nfcsim_in_send_cmd(struct nfc_digital_dev *ddev,
289                                struct sk_buff *skb, u16 timeout,
290                                nfc_digital_cmd_complete_t cb, void *arg)
291 {
292         return nfcsim_send(ddev, skb, timeout, cb, arg);
293 }
294
295 static int nfcsim_tg_configure_hw(struct nfc_digital_dev *ddev,
296                                           int type, int param)
297 {
298         struct nfcsim *dev = nfc_digital_get_drvdata(ddev);
299
300         switch (type) {
301         case NFC_DIGITAL_CONFIG_RF_TECH:
302                 dev->up = true;
303                 dev->mode = NFCSIM_MODE_TARGET;
304                 dev->rf_tech = param;
305                 break;
306
307         case NFC_DIGITAL_CONFIG_FRAMING:
308                 break;
309
310         default:
311                 NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type);
312                 return -EINVAL;
313         }
314
315         return 0;
316 }
317
318 static int nfcsim_tg_send_cmd(struct nfc_digital_dev *ddev,
319                                struct sk_buff *skb, u16 timeout,
320                                nfc_digital_cmd_complete_t cb, void *arg)
321 {
322         return nfcsim_send(ddev, skb, timeout, cb, arg);
323 }
324
325 static int nfcsim_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
326                             nfc_digital_cmd_complete_t cb, void *arg)
327 {
328         return nfcsim_send(ddev, NULL, timeout, cb, arg);
329 }
330
331 static struct nfc_digital_ops nfcsim_digital_ops = {
332         .in_configure_hw = nfcsim_in_configure_hw,
333         .in_send_cmd = nfcsim_in_send_cmd,
334
335         .tg_listen = nfcsim_tg_listen,
336         .tg_configure_hw = nfcsim_tg_configure_hw,
337         .tg_send_cmd = nfcsim_tg_send_cmd,
338
339         .abort_cmd = nfcsim_abort_cmd,
340         .switch_rf = nfcsim_switch_rf,
341 };
342
343 static struct dentry *nfcsim_debugfs_root;
344
345 static void nfcsim_debugfs_init(void)
346 {
347         nfcsim_debugfs_root = debugfs_create_dir("nfcsim", NULL);
348
349         if (!nfcsim_debugfs_root)
350                 pr_err("Could not create debugfs entry\n");
351
352 }
353
354 static void nfcsim_debugfs_remove(void)
355 {
356         debugfs_remove_recursive(nfcsim_debugfs_root);
357 }
358
359 static void nfcsim_debugfs_init_dev(struct nfcsim *dev)
360 {
361         struct dentry *dev_dir;
362         char devname[5]; /* nfcX\0 */
363         u32 idx;
364         int n;
365
366         if (!nfcsim_debugfs_root) {
367                 NFCSIM_ERR(dev, "nfcsim debugfs not initialized\n");
368                 return;
369         }
370
371         idx = dev->nfc_digital_dev->nfc_dev->idx;
372         n = snprintf(devname, sizeof(devname), "nfc%d", idx);
373         if (n >= sizeof(devname)) {
374                 NFCSIM_ERR(dev, "Could not compute dev name for dev %d\n", idx);
375                 return;
376         }
377
378         dev_dir = debugfs_create_dir(devname, nfcsim_debugfs_root);
379         if (!dev_dir) {
380                 NFCSIM_ERR(dev, "Could not create debugfs entries for nfc%d\n",
381                            idx);
382                 return;
383         }
384
385         debugfs_create_u8("dropframe", 0664, dev_dir, &dev->dropframe);
386 }
387
388 static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in,
389                                         struct nfcsim_link *link_out)
390 {
391         struct nfcsim *dev;
392         int rc;
393
394         dev = kzalloc(sizeof(struct nfcsim), GFP_KERNEL);
395         if (!dev)
396                 return ERR_PTR(-ENOMEM);
397
398         INIT_DELAYED_WORK(&dev->send_work, nfcsim_send_wq);
399         INIT_WORK(&dev->recv_work, nfcsim_recv_wq);
400
401         dev->nfc_digital_dev =
402                         nfc_digital_allocate_device(&nfcsim_digital_ops,
403                                                     NFC_PROTO_NFC_DEP_MASK,
404                                                     NFCSIM_CAPABILITIES,
405                                                     0, 0);
406         if (!dev->nfc_digital_dev) {
407                 kfree(dev);
408                 return ERR_PTR(-ENOMEM);
409         }
410
411         nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
412
413         dev->link_in = link_in;
414         dev->link_out = link_out;
415
416         rc = nfc_digital_register_device(dev->nfc_digital_dev);
417         if (rc) {
418                 pr_err("Could not register digital device (%d)\n", rc);
419                 nfc_digital_free_device(dev->nfc_digital_dev);
420                 kfree(dev);
421
422                 return ERR_PTR(rc);
423         }
424
425         nfcsim_debugfs_init_dev(dev);
426
427         return dev;
428 }
429
430 static void nfcsim_device_free(struct nfcsim *dev)
431 {
432         nfc_digital_unregister_device(dev->nfc_digital_dev);
433
434         dev->up = false;
435
436         nfcsim_link_shutdown(dev->link_in);
437
438         cancel_delayed_work_sync(&dev->send_work);
439         cancel_work_sync(&dev->recv_work);
440
441         nfc_digital_free_device(dev->nfc_digital_dev);
442
443         kfree(dev);
444 }
445
446 static struct nfcsim *dev0;
447 static struct nfcsim *dev1;
448
449 static int __init nfcsim_init(void)
450 {
451         struct nfcsim_link *link0, *link1;
452         int rc;
453
454         link0 = nfcsim_link_new();
455         link1 = nfcsim_link_new();
456         if (!link0 || !link1) {
457                 rc = -ENOMEM;
458                 goto exit_err;
459         }
460
461         nfcsim_debugfs_init();
462
463         dev0 = nfcsim_device_new(link0, link1);
464         if (IS_ERR(dev0)) {
465                 rc = PTR_ERR(dev0);
466                 goto exit_err;
467         }
468
469         dev1 = nfcsim_device_new(link1, link0);
470         if (IS_ERR(dev1)) {
471                 nfcsim_device_free(dev0);
472
473                 rc = PTR_ERR(dev1);
474                 goto exit_err;
475         }
476
477         pr_info("nfcsim " NFCSIM_VERSION " initialized\n");
478
479         return 0;
480
481 exit_err:
482         pr_err("Failed to initialize nfcsim driver (%d)\n", rc);
483
484         if (link0)
485                 nfcsim_link_free(link0);
486         if (link1)
487                 nfcsim_link_free(link1);
488
489         return rc;
490 }
491
492 static void __exit nfcsim_exit(void)
493 {
494         struct nfcsim_link *link0, *link1;
495
496         link0 = dev0->link_in;
497         link1 = dev0->link_out;
498
499         nfcsim_device_free(dev0);
500         nfcsim_device_free(dev1);
501
502         nfcsim_link_free(link0);
503         nfcsim_link_free(link1);
504
505         nfcsim_debugfs_remove();
506 }
507
508 module_init(nfcsim_init);
509 module_exit(nfcsim_exit);
510
511 MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION);
512 MODULE_VERSION(NFCSIM_VERSION);
513 MODULE_LICENSE("GPL");