Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NFC hardware simulation driver |
| 4 | * Copyright (c) 2013, Intel Corporation. |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/device.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
Thierry Escande | f9ac627 | 2016-07-19 11:58:16 +0200 | [diff] [blame] | 10 | #include <linux/ctype.h> |
| 11 | #include <linux/debugfs.h> |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 12 | #include <linux/nfc.h> |
| 13 | #include <net/nfc/nfc.h> |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 14 | #include <net/nfc/digital.h> |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 15 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 16 | #define NFCSIM_ERR(d, fmt, args...) nfc_err(&d->nfc_digital_dev->nfc_dev->dev, \ |
| 17 | "%s: " fmt, __func__, ## args) |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 18 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 19 | #define NFCSIM_DBG(d, fmt, args...) dev_dbg(&d->nfc_digital_dev->nfc_dev->dev, \ |
| 20 | "%s: " fmt, __func__, ## args) |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 21 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 22 | #define NFCSIM_VERSION "0.2" |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 23 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 24 | #define NFCSIM_MODE_NONE 0 |
| 25 | #define NFCSIM_MODE_INITIATOR 1 |
| 26 | #define NFCSIM_MODE_TARGET 2 |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 27 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 28 | #define NFCSIM_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \ |
| 29 | NFC_DIGITAL_DRV_CAPS_TG_CRC) |
Saurabh Sengar | a440f1a | 2015-12-21 00:29:30 +0530 | [diff] [blame] | 30 | |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 31 | struct nfcsim { |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 32 | struct nfc_digital_dev *nfc_digital_dev; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 33 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 34 | struct work_struct recv_work; |
| 35 | struct delayed_work send_work; |
| 36 | |
| 37 | struct nfcsim_link *link_in; |
| 38 | struct nfcsim_link *link_out; |
| 39 | |
| 40 | bool up; |
| 41 | u8 mode; |
| 42 | u8 rf_tech; |
| 43 | |
| 44 | u16 recv_timeout; |
| 45 | |
| 46 | nfc_digital_cmd_complete_t cb; |
| 47 | void *arg; |
Thierry Escande | 2a0fe4f | 2016-07-19 11:58:17 +0200 | [diff] [blame] | 48 | |
| 49 | u8 dropframe; |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct nfcsim_link { |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 53 | struct mutex lock; |
| 54 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 55 | u8 rf_tech; |
| 56 | u8 mode; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 57 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 58 | u8 shutdown; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 59 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 60 | struct sk_buff *skb; |
| 61 | wait_queue_head_t recv_wait; |
| 62 | u8 cond; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 65 | static struct nfcsim_link *nfcsim_link_new(void) |
| 66 | { |
| 67 | struct nfcsim_link *link; |
| 68 | |
| 69 | link = kzalloc(sizeof(struct nfcsim_link), GFP_KERNEL); |
| 70 | if (!link) |
| 71 | return NULL; |
| 72 | |
| 73 | mutex_init(&link->lock); |
| 74 | init_waitqueue_head(&link->recv_wait); |
| 75 | |
| 76 | return link; |
| 77 | } |
| 78 | |
| 79 | static void nfcsim_link_free(struct nfcsim_link *link) |
| 80 | { |
| 81 | dev_kfree_skb(link->skb); |
| 82 | kfree(link); |
| 83 | } |
| 84 | |
| 85 | static void nfcsim_link_recv_wake(struct nfcsim_link *link) |
| 86 | { |
| 87 | link->cond = 1; |
| 88 | wake_up_interruptible(&link->recv_wait); |
| 89 | } |
| 90 | |
| 91 | static void nfcsim_link_set_skb(struct nfcsim_link *link, struct sk_buff *skb, |
| 92 | u8 rf_tech, u8 mode) |
| 93 | { |
| 94 | mutex_lock(&link->lock); |
| 95 | |
| 96 | dev_kfree_skb(link->skb); |
| 97 | link->skb = skb; |
| 98 | link->rf_tech = rf_tech; |
| 99 | link->mode = mode; |
| 100 | |
| 101 | mutex_unlock(&link->lock); |
| 102 | } |
| 103 | |
| 104 | static void nfcsim_link_recv_cancel(struct nfcsim_link *link) |
| 105 | { |
| 106 | mutex_lock(&link->lock); |
| 107 | |
| 108 | link->mode = NFCSIM_MODE_NONE; |
| 109 | |
| 110 | mutex_unlock(&link->lock); |
| 111 | |
| 112 | nfcsim_link_recv_wake(link); |
| 113 | } |
| 114 | |
| 115 | static void nfcsim_link_shutdown(struct nfcsim_link *link) |
| 116 | { |
| 117 | mutex_lock(&link->lock); |
| 118 | |
| 119 | link->shutdown = 1; |
| 120 | link->mode = NFCSIM_MODE_NONE; |
| 121 | |
| 122 | mutex_unlock(&link->lock); |
| 123 | |
| 124 | nfcsim_link_recv_wake(link); |
| 125 | } |
| 126 | |
| 127 | static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link, |
| 128 | int timeout, u8 rf_tech, u8 mode) |
| 129 | { |
| 130 | int rc; |
| 131 | struct sk_buff *skb; |
| 132 | |
| 133 | rc = wait_event_interruptible_timeout(link->recv_wait, |
| 134 | link->cond, |
| 135 | msecs_to_jiffies(timeout)); |
| 136 | |
| 137 | mutex_lock(&link->lock); |
| 138 | |
| 139 | skb = link->skb; |
| 140 | link->skb = NULL; |
| 141 | |
| 142 | if (!rc) { |
| 143 | rc = -ETIMEDOUT; |
| 144 | goto done; |
| 145 | } |
| 146 | |
| 147 | if (!skb || link->rf_tech != rf_tech || link->mode == mode) { |
| 148 | rc = -EINVAL; |
| 149 | goto done; |
| 150 | } |
| 151 | |
| 152 | if (link->shutdown) { |
| 153 | rc = -ENODEV; |
| 154 | goto done; |
| 155 | } |
| 156 | |
| 157 | done: |
| 158 | mutex_unlock(&link->lock); |
| 159 | |
| 160 | if (rc < 0) { |
| 161 | dev_kfree_skb(skb); |
| 162 | skb = ERR_PTR(rc); |
| 163 | } |
| 164 | |
| 165 | link->cond = 0; |
| 166 | |
| 167 | return skb; |
| 168 | } |
| 169 | |
| 170 | static void nfcsim_send_wq(struct work_struct *work) |
| 171 | { |
| 172 | struct nfcsim *dev = container_of(work, struct nfcsim, send_work.work); |
| 173 | |
| 174 | /* |
| 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(). |
| 178 | */ |
| 179 | nfcsim_link_recv_wake(dev->link_out); |
| 180 | } |
| 181 | |
| 182 | static void nfcsim_recv_wq(struct work_struct *work) |
| 183 | { |
| 184 | struct nfcsim *dev = container_of(work, struct nfcsim, recv_work); |
| 185 | struct sk_buff *skb; |
| 186 | |
| 187 | skb = nfcsim_link_recv_skb(dev->link_in, dev->recv_timeout, |
| 188 | dev->rf_tech, dev->mode); |
| 189 | |
| 190 | if (!dev->up) { |
| 191 | NFCSIM_ERR(dev, "Device is down\n"); |
| 192 | |
| 193 | if (!IS_ERR(skb)) |
| 194 | dev_kfree_skb(skb); |
Krzysztof Kozlowski | 5e7b30d | 2021-07-28 08:49:09 +0200 | [diff] [blame] | 195 | return; |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | dev->cb(dev->nfc_digital_dev, dev->arg, skb); |
| 199 | } |
| 200 | |
| 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) |
| 203 | { |
| 204 | struct nfcsim *dev = nfc_digital_get_drvdata(ddev); |
| 205 | u8 delay; |
| 206 | |
| 207 | if (!dev->up) { |
| 208 | NFCSIM_ERR(dev, "Device is down\n"); |
| 209 | return -ENODEV; |
| 210 | } |
| 211 | |
| 212 | dev->recv_timeout = timeout; |
| 213 | dev->cb = cb; |
| 214 | dev->arg = arg; |
| 215 | |
| 216 | schedule_work(&dev->recv_work); |
| 217 | |
Thierry Escande | 2a0fe4f | 2016-07-19 11:58:17 +0200 | [diff] [blame] | 218 | if (dev->dropframe) { |
| 219 | NFCSIM_DBG(dev, "dropping frame (out of %d)\n", dev->dropframe); |
| 220 | dev_kfree_skb(skb); |
| 221 | dev->dropframe--; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 226 | if (skb) { |
| 227 | nfcsim_link_set_skb(dev->link_out, skb, dev->rf_tech, |
| 228 | dev->mode); |
| 229 | |
| 230 | /* Add random delay (between 3 and 10 ms) before sending data */ |
| 231 | get_random_bytes(&delay, 1); |
| 232 | delay = 3 + (delay & 0x07); |
| 233 | |
| 234 | schedule_delayed_work(&dev->send_work, msecs_to_jiffies(delay)); |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void nfcsim_abort_cmd(struct nfc_digital_dev *ddev) |
| 241 | { |
Krzysztof Kozlowski | 582fdc9 | 2021-07-29 12:40:16 +0200 | [diff] [blame] | 242 | const struct nfcsim *dev = nfc_digital_get_drvdata(ddev); |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 243 | |
| 244 | nfcsim_link_recv_cancel(dev->link_in); |
| 245 | } |
| 246 | |
| 247 | static int nfcsim_switch_rf(struct nfc_digital_dev *ddev, bool on) |
| 248 | { |
| 249 | struct nfcsim *dev = nfc_digital_get_drvdata(ddev); |
| 250 | |
| 251 | dev->up = on; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int nfcsim_in_configure_hw(struct nfc_digital_dev *ddev, |
| 257 | int type, int param) |
| 258 | { |
| 259 | struct nfcsim *dev = nfc_digital_get_drvdata(ddev); |
| 260 | |
| 261 | switch (type) { |
| 262 | case NFC_DIGITAL_CONFIG_RF_TECH: |
| 263 | dev->up = true; |
| 264 | dev->mode = NFCSIM_MODE_INITIATOR; |
| 265 | dev->rf_tech = param; |
| 266 | break; |
| 267 | |
| 268 | case NFC_DIGITAL_CONFIG_FRAMING: |
| 269 | break; |
| 270 | |
| 271 | default: |
| 272 | NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 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) |
| 282 | { |
| 283 | return nfcsim_send(ddev, skb, timeout, cb, arg); |
| 284 | } |
| 285 | |
| 286 | static int nfcsim_tg_configure_hw(struct nfc_digital_dev *ddev, |
| 287 | int type, int param) |
| 288 | { |
| 289 | struct nfcsim *dev = nfc_digital_get_drvdata(ddev); |
| 290 | |
| 291 | switch (type) { |
| 292 | case NFC_DIGITAL_CONFIG_RF_TECH: |
| 293 | dev->up = true; |
| 294 | dev->mode = NFCSIM_MODE_TARGET; |
| 295 | dev->rf_tech = param; |
| 296 | break; |
| 297 | |
| 298 | case NFC_DIGITAL_CONFIG_FRAMING: |
| 299 | break; |
| 300 | |
| 301 | default: |
| 302 | NFCSIM_ERR(dev, "Invalid configuration type: %d\n", type); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 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) |
| 312 | { |
| 313 | return nfcsim_send(ddev, skb, timeout, cb, arg); |
| 314 | } |
| 315 | |
| 316 | static int nfcsim_tg_listen(struct nfc_digital_dev *ddev, u16 timeout, |
| 317 | nfc_digital_cmd_complete_t cb, void *arg) |
| 318 | { |
| 319 | return nfcsim_send(ddev, NULL, timeout, cb, arg); |
| 320 | } |
| 321 | |
Krzysztof Kozlowski | 7186aac | 2021-07-24 23:49:28 +0200 | [diff] [blame] | 322 | static const struct nfc_digital_ops nfcsim_digital_ops = { |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 323 | .in_configure_hw = nfcsim_in_configure_hw, |
| 324 | .in_send_cmd = nfcsim_in_send_cmd, |
| 325 | |
| 326 | .tg_listen = nfcsim_tg_listen, |
| 327 | .tg_configure_hw = nfcsim_tg_configure_hw, |
| 328 | .tg_send_cmd = nfcsim_tg_send_cmd, |
| 329 | |
| 330 | .abort_cmd = nfcsim_abort_cmd, |
| 331 | .switch_rf = nfcsim_switch_rf, |
| 332 | }; |
| 333 | |
Thierry Escande | f9ac627 | 2016-07-19 11:58:16 +0200 | [diff] [blame] | 334 | static struct dentry *nfcsim_debugfs_root; |
| 335 | |
| 336 | static void nfcsim_debugfs_init(void) |
| 337 | { |
| 338 | nfcsim_debugfs_root = debugfs_create_dir("nfcsim", NULL); |
| 339 | |
| 340 | if (!nfcsim_debugfs_root) |
| 341 | pr_err("Could not create debugfs entry\n"); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | static void nfcsim_debugfs_remove(void) |
| 346 | { |
| 347 | debugfs_remove_recursive(nfcsim_debugfs_root); |
| 348 | } |
| 349 | |
| 350 | static void nfcsim_debugfs_init_dev(struct nfcsim *dev) |
| 351 | { |
| 352 | struct dentry *dev_dir; |
| 353 | char devname[5]; /* nfcX\0 */ |
| 354 | u32 idx; |
| 355 | int n; |
| 356 | |
| 357 | if (!nfcsim_debugfs_root) { |
| 358 | NFCSIM_ERR(dev, "nfcsim debugfs not initialized\n"); |
| 359 | return; |
| 360 | } |
| 361 | |
| 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); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | dev_dir = debugfs_create_dir(devname, nfcsim_debugfs_root); |
| 370 | if (!dev_dir) { |
| 371 | NFCSIM_ERR(dev, "Could not create debugfs entries for nfc%d\n", |
| 372 | idx); |
| 373 | return; |
| 374 | } |
Thierry Escande | 2a0fe4f | 2016-07-19 11:58:17 +0200 | [diff] [blame] | 375 | |
| 376 | debugfs_create_u8("dropframe", 0664, dev_dir, &dev->dropframe); |
Thierry Escande | f9ac627 | 2016-07-19 11:58:16 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 379 | static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in, |
| 380 | struct nfcsim_link *link_out) |
| 381 | { |
| 382 | struct nfcsim *dev; |
| 383 | int rc; |
| 384 | |
| 385 | dev = kzalloc(sizeof(struct nfcsim), GFP_KERNEL); |
| 386 | if (!dev) |
| 387 | return ERR_PTR(-ENOMEM); |
| 388 | |
| 389 | INIT_DELAYED_WORK(&dev->send_work, nfcsim_send_wq); |
| 390 | INIT_WORK(&dev->recv_work, nfcsim_recv_wq); |
| 391 | |
| 392 | dev->nfc_digital_dev = |
| 393 | nfc_digital_allocate_device(&nfcsim_digital_ops, |
| 394 | NFC_PROTO_NFC_DEP_MASK, |
| 395 | NFCSIM_CAPABILITIES, |
| 396 | 0, 0); |
| 397 | if (!dev->nfc_digital_dev) { |
| 398 | kfree(dev); |
| 399 | return ERR_PTR(-ENOMEM); |
| 400 | } |
| 401 | |
| 402 | nfc_digital_set_drvdata(dev->nfc_digital_dev, dev); |
| 403 | |
| 404 | dev->link_in = link_in; |
| 405 | dev->link_out = link_out; |
| 406 | |
| 407 | rc = nfc_digital_register_device(dev->nfc_digital_dev); |
| 408 | if (rc) { |
| 409 | pr_err("Could not register digital device (%d)\n", rc); |
| 410 | nfc_digital_free_device(dev->nfc_digital_dev); |
| 411 | kfree(dev); |
| 412 | |
| 413 | return ERR_PTR(rc); |
| 414 | } |
| 415 | |
Thierry Escande | f9ac627 | 2016-07-19 11:58:16 +0200 | [diff] [blame] | 416 | nfcsim_debugfs_init_dev(dev); |
| 417 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 418 | return dev; |
| 419 | } |
| 420 | |
| 421 | static void nfcsim_device_free(struct nfcsim *dev) |
| 422 | { |
| 423 | nfc_digital_unregister_device(dev->nfc_digital_dev); |
| 424 | |
| 425 | dev->up = false; |
| 426 | |
| 427 | nfcsim_link_shutdown(dev->link_in); |
| 428 | |
| 429 | cancel_delayed_work_sync(&dev->send_work); |
| 430 | cancel_work_sync(&dev->recv_work); |
| 431 | |
| 432 | nfc_digital_free_device(dev->nfc_digital_dev); |
| 433 | |
| 434 | kfree(dev); |
| 435 | } |
| 436 | |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 437 | static struct nfcsim *dev0; |
| 438 | static struct nfcsim *dev1; |
| 439 | |
Thierry Escande | 40dac37 | 2013-06-24 12:02:28 +0200 | [diff] [blame] | 440 | static int __init nfcsim_init(void) |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 441 | { |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 442 | struct nfcsim_link *link0, *link1; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 443 | int rc; |
| 444 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 445 | link0 = nfcsim_link_new(); |
| 446 | link1 = nfcsim_link_new(); |
| 447 | if (!link0 || !link1) { |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 448 | rc = -ENOMEM; |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 449 | goto exit_err; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Thierry Escande | f9ac627 | 2016-07-19 11:58:16 +0200 | [diff] [blame] | 452 | nfcsim_debugfs_init(); |
| 453 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 454 | dev0 = nfcsim_device_new(link0, link1); |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 455 | if (IS_ERR(dev0)) { |
| 456 | rc = PTR_ERR(dev0); |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 457 | goto exit_err; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 460 | dev1 = nfcsim_device_new(link1, link0); |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 461 | if (IS_ERR(dev1)) { |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 462 | nfcsim_device_free(dev0); |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 463 | |
| 464 | rc = PTR_ERR(dev1); |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 465 | goto exit_err; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 468 | pr_info("nfcsim " NFCSIM_VERSION " initialized\n"); |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 469 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 470 | return 0; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 471 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 472 | exit_err: |
| 473 | pr_err("Failed to initialize nfcsim driver (%d)\n", rc); |
| 474 | |
Gustavo A. R. Silva | 6f874ba | 2017-05-30 15:43:07 -0500 | [diff] [blame] | 475 | if (link0) |
| 476 | nfcsim_link_free(link0); |
| 477 | if (link1) |
| 478 | nfcsim_link_free(link1); |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
Thierry Escande | 40dac37 | 2013-06-24 12:02:28 +0200 | [diff] [blame] | 483 | static void __exit nfcsim_exit(void) |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 484 | { |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 485 | struct nfcsim_link *link0, *link1; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 486 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 487 | link0 = dev0->link_in; |
| 488 | link1 = dev0->link_out; |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 489 | |
Thierry Escande | 204bddc | 2016-06-23 11:20:26 +0200 | [diff] [blame] | 490 | nfcsim_device_free(dev0); |
| 491 | nfcsim_device_free(dev1); |
| 492 | |
| 493 | nfcsim_link_free(link0); |
| 494 | nfcsim_link_free(link1); |
Thierry Escande | f9ac627 | 2016-07-19 11:58:16 +0200 | [diff] [blame] | 495 | |
| 496 | nfcsim_debugfs_remove(); |
Thierry Escande | 7cbe0ff | 2013-06-06 16:23:23 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | module_init(nfcsim_init); |
| 500 | module_exit(nfcsim_exit); |
| 501 | |
| 502 | MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION); |
| 503 | MODULE_VERSION(NFCSIM_VERSION); |
| 504 | MODULE_LICENSE("GPL"); |