Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> |
| 7 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 8 | */ |
| 9 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 11 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
Samuel Ortiz | be055b2 | 2013-04-11 11:52:20 +0200 | [diff] [blame] | 16 | #include <linux/rfkill.h> |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 17 | #include <linux/nfc.h> |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 18 | |
Samuel Ortiz | 5df16cad | 2012-06-12 16:54:16 +0200 | [diff] [blame] | 19 | #include <net/genetlink.h> |
| 20 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 21 | #include "nfc.h" |
| 22 | |
| 23 | #define VERSION "0.1" |
| 24 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 25 | #define NFC_CHECK_PRES_FREQ_MS 2000 |
| 26 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 27 | int nfc_devlist_generation; |
| 28 | DEFINE_MUTEX(nfc_devlist_mutex); |
| 29 | |
Samuel Ortiz | 7eda8b8e9 | 2012-10-22 15:57:58 +0200 | [diff] [blame] | 30 | /* NFC device ID bitmap */ |
| 31 | static DEFINE_IDA(nfc_index_ida); |
| 32 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 33 | int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 34 | { |
| 35 | int rc = 0; |
| 36 | |
| 37 | pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name); |
| 38 | |
| 39 | device_lock(&dev->dev); |
| 40 | |
| 41 | if (!device_is_registered(&dev->dev)) { |
| 42 | rc = -ENODEV; |
| 43 | goto error; |
| 44 | } |
| 45 | |
| 46 | if (dev->dev_up) { |
| 47 | rc = -EBUSY; |
| 48 | goto error; |
| 49 | } |
| 50 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 51 | if (!dev->ops->fw_download) { |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 52 | rc = -EOPNOTSUPP; |
| 53 | goto error; |
| 54 | } |
| 55 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 56 | dev->fw_download_in_progress = true; |
| 57 | rc = dev->ops->fw_download(dev, firmware_name); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 58 | if (rc) |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 59 | dev->fw_download_in_progress = false; |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 60 | |
| 61 | error: |
| 62 | device_unlock(&dev->dev); |
| 63 | return rc; |
| 64 | } |
| 65 | |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 66 | /** |
| 67 | * nfc_fw_download_done - inform that a firmware download was completed |
| 68 | * |
| 69 | * @dev: The nfc device to which firmware was downloaded |
| 70 | * @firmware_name: The firmware filename |
| 71 | * @result: The positive value of a standard errno value |
| 72 | */ |
| 73 | int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name, |
| 74 | u32 result) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 75 | { |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 76 | dev->fw_download_in_progress = false; |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 77 | |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 78 | return nfc_genl_fw_download_done(dev, firmware_name, result); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 79 | } |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 80 | EXPORT_SYMBOL(nfc_fw_download_done); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 81 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 82 | /** |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 83 | * nfc_dev_up - turn on the NFC device |
| 84 | * |
| 85 | * @dev: The nfc device to be turned on |
| 86 | * |
| 87 | * The device remains up until the nfc_dev_down function is called. |
| 88 | */ |
| 89 | int nfc_dev_up(struct nfc_dev *dev) |
| 90 | { |
| 91 | int rc = 0; |
| 92 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 93 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 94 | |
| 95 | device_lock(&dev->dev); |
| 96 | |
Samuel Ortiz | be055b2 | 2013-04-11 11:52:20 +0200 | [diff] [blame] | 97 | if (dev->rfkill && rfkill_blocked(dev->rfkill)) { |
| 98 | rc = -ERFKILL; |
| 99 | goto error; |
| 100 | } |
| 101 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 102 | if (!device_is_registered(&dev->dev)) { |
| 103 | rc = -ENODEV; |
| 104 | goto error; |
| 105 | } |
| 106 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 107 | if (dev->fw_download_in_progress) { |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 108 | rc = -EBUSY; |
| 109 | goto error; |
| 110 | } |
| 111 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 112 | if (dev->dev_up) { |
| 113 | rc = -EALREADY; |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | if (dev->ops->dev_up) |
| 118 | rc = dev->ops->dev_up(dev); |
| 119 | |
| 120 | if (!rc) |
| 121 | dev->dev_up = true; |
| 122 | |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 123 | /* We have to enable the device before discovering SEs */ |
Samuel Ortiz | a434c24 | 2013-12-22 01:00:20 +0100 | [diff] [blame] | 124 | if (dev->ops->discover_se && dev->ops->discover_se(dev)) |
| 125 | pr_err("SE discovery failed\n"); |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 126 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 127 | error: |
| 128 | device_unlock(&dev->dev); |
| 129 | return rc; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * nfc_dev_down - turn off the NFC device |
| 134 | * |
| 135 | * @dev: The nfc device to be turned off |
| 136 | */ |
| 137 | int nfc_dev_down(struct nfc_dev *dev) |
| 138 | { |
| 139 | int rc = 0; |
| 140 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 141 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 142 | |
| 143 | device_lock(&dev->dev); |
| 144 | |
| 145 | if (!device_is_registered(&dev->dev)) { |
| 146 | rc = -ENODEV; |
| 147 | goto error; |
| 148 | } |
| 149 | |
| 150 | if (!dev->dev_up) { |
| 151 | rc = -EALREADY; |
| 152 | goto error; |
| 153 | } |
| 154 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 155 | if (dev->polling || dev->active_target) { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 156 | rc = -EBUSY; |
| 157 | goto error; |
| 158 | } |
| 159 | |
| 160 | if (dev->ops->dev_down) |
| 161 | dev->ops->dev_down(dev); |
| 162 | |
| 163 | dev->dev_up = false; |
| 164 | |
| 165 | error: |
| 166 | device_unlock(&dev->dev); |
| 167 | return rc; |
| 168 | } |
| 169 | |
Samuel Ortiz | be055b2 | 2013-04-11 11:52:20 +0200 | [diff] [blame] | 170 | static int nfc_rfkill_set_block(void *data, bool blocked) |
| 171 | { |
| 172 | struct nfc_dev *dev = data; |
| 173 | |
| 174 | pr_debug("%s blocked %d", dev_name(&dev->dev), blocked); |
| 175 | |
| 176 | if (!blocked) |
| 177 | return 0; |
| 178 | |
| 179 | nfc_dev_down(dev); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static const struct rfkill_ops nfc_rfkill_ops = { |
| 185 | .set_block = nfc_rfkill_set_block, |
| 186 | }; |
| 187 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 188 | /** |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 189 | * nfc_start_poll - start polling for nfc targets |
| 190 | * |
| 191 | * @dev: The nfc device that must start polling |
| 192 | * @protocols: bitset of nfc protocols that must be used for polling |
| 193 | * |
| 194 | * The device remains polling for targets until a target is found or |
| 195 | * the nfc_stop_poll function is called. |
| 196 | */ |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 197 | int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 198 | { |
| 199 | int rc; |
| 200 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 201 | pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n", |
| 202 | dev_name(&dev->dev), im_protocols, tm_protocols); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 203 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 204 | if (!im_protocols && !tm_protocols) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 205 | return -EINVAL; |
| 206 | |
| 207 | device_lock(&dev->dev); |
| 208 | |
| 209 | if (!device_is_registered(&dev->dev)) { |
| 210 | rc = -ENODEV; |
| 211 | goto error; |
| 212 | } |
| 213 | |
Samuel Ortiz | 7757dc8 | 2013-04-10 12:25:30 +0200 | [diff] [blame] | 214 | if (!dev->dev_up) { |
| 215 | rc = -ENODEV; |
| 216 | goto error; |
| 217 | } |
| 218 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 219 | if (dev->polling) { |
| 220 | rc = -EBUSY; |
| 221 | goto error; |
| 222 | } |
| 223 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 224 | rc = dev->ops->start_poll(dev, im_protocols, tm_protocols); |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 225 | if (!rc) { |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 226 | dev->polling = true; |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 227 | dev->rf_mode = NFC_RF_NONE; |
| 228 | } |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 229 | |
| 230 | error: |
| 231 | device_unlock(&dev->dev); |
| 232 | return rc; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * nfc_stop_poll - stop polling for nfc targets |
| 237 | * |
| 238 | * @dev: The nfc device that must stop polling |
| 239 | */ |
| 240 | int nfc_stop_poll(struct nfc_dev *dev) |
| 241 | { |
| 242 | int rc = 0; |
| 243 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 244 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 245 | |
| 246 | device_lock(&dev->dev); |
| 247 | |
| 248 | if (!device_is_registered(&dev->dev)) { |
| 249 | rc = -ENODEV; |
| 250 | goto error; |
| 251 | } |
| 252 | |
| 253 | if (!dev->polling) { |
| 254 | rc = -EINVAL; |
| 255 | goto error; |
| 256 | } |
| 257 | |
| 258 | dev->ops->stop_poll(dev); |
| 259 | dev->polling = false; |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 260 | dev->rf_mode = NFC_RF_NONE; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 261 | |
| 262 | error: |
| 263 | device_unlock(&dev->dev); |
| 264 | return rc; |
| 265 | } |
| 266 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 267 | static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx) |
| 268 | { |
| 269 | int i; |
| 270 | |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 271 | for (i = 0; i < dev->n_targets; i++) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 272 | if (dev->targets[i].idx == target_idx) |
| 273 | return &dev->targets[i]; |
| 274 | } |
| 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 279 | int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 280 | { |
| 281 | int rc = 0; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 282 | u8 *gb; |
| 283 | size_t gb_len; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 284 | struct nfc_target *target; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 285 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 286 | pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 287 | |
| 288 | if (!dev->ops->dep_link_up) |
| 289 | return -EOPNOTSUPP; |
| 290 | |
| 291 | device_lock(&dev->dev); |
| 292 | |
| 293 | if (!device_is_registered(&dev->dev)) { |
| 294 | rc = -ENODEV; |
| 295 | goto error; |
| 296 | } |
| 297 | |
| 298 | if (dev->dep_link_up == true) { |
| 299 | rc = -EALREADY; |
| 300 | goto error; |
| 301 | } |
| 302 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 303 | gb = nfc_llcp_general_bytes(dev, &gb_len); |
| 304 | if (gb_len > NFC_MAX_GT_LEN) { |
| 305 | rc = -EINVAL; |
| 306 | goto error; |
| 307 | } |
| 308 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 309 | target = nfc_find_target(dev, target_index); |
| 310 | if (target == NULL) { |
| 311 | rc = -ENOTCONN; |
| 312 | goto error; |
| 313 | } |
| 314 | |
| 315 | rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len); |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 316 | if (!rc) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 317 | dev->active_target = target; |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 318 | dev->rf_mode = NFC_RF_INITIATOR; |
| 319 | } |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 320 | |
| 321 | error: |
| 322 | device_unlock(&dev->dev); |
| 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | int nfc_dep_link_down(struct nfc_dev *dev) |
| 327 | { |
| 328 | int rc = 0; |
| 329 | |
| 330 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
| 331 | |
| 332 | if (!dev->ops->dep_link_down) |
| 333 | return -EOPNOTSUPP; |
| 334 | |
| 335 | device_lock(&dev->dev); |
| 336 | |
| 337 | if (!device_is_registered(&dev->dev)) { |
| 338 | rc = -ENODEV; |
| 339 | goto error; |
| 340 | } |
| 341 | |
| 342 | if (dev->dep_link_up == false) { |
| 343 | rc = -EALREADY; |
| 344 | goto error; |
| 345 | } |
| 346 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 347 | rc = dev->ops->dep_link_down(dev); |
| 348 | if (!rc) { |
| 349 | dev->dep_link_up = false; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 350 | dev->active_target = NULL; |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 351 | dev->rf_mode = NFC_RF_NONE; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 352 | nfc_llcp_mac_is_down(dev); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 353 | nfc_genl_dep_link_down_event(dev); |
| 354 | } |
| 355 | |
| 356 | error: |
| 357 | device_unlock(&dev->dev); |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 358 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 359 | return rc; |
| 360 | } |
| 361 | |
| 362 | int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 363 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 364 | { |
| 365 | dev->dep_link_up = true; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 366 | |
Arron Wang | d31652a | 2013-11-14 17:03:41 +0800 | [diff] [blame] | 367 | if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) { |
Samuel Ortiz | e29a9e2 | 2013-08-21 14:46:20 +0200 | [diff] [blame] | 368 | struct nfc_target *target; |
| 369 | |
| 370 | target = nfc_find_target(dev, target_idx); |
| 371 | if (target == NULL) |
| 372 | return -ENOTCONN; |
| 373 | |
| 374 | dev->active_target = target; |
| 375 | } |
| 376 | |
| 377 | dev->polling = false; |
| 378 | dev->rf_mode = rf_mode; |
| 379 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 380 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); |
| 381 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 382 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); |
| 383 | } |
| 384 | EXPORT_SYMBOL(nfc_dep_link_is_up); |
| 385 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 386 | /** |
| 387 | * nfc_activate_target - prepare the target for data exchange |
| 388 | * |
| 389 | * @dev: The nfc device that found the target |
| 390 | * @target_idx: index of the target that must be activated |
| 391 | * @protocol: nfc protocol that will be used for data exchange |
| 392 | */ |
| 393 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) |
| 394 | { |
| 395 | int rc; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 396 | struct nfc_target *target; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 397 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 398 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", |
| 399 | dev_name(&dev->dev), target_idx, protocol); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 400 | |
| 401 | device_lock(&dev->dev); |
| 402 | |
| 403 | if (!device_is_registered(&dev->dev)) { |
| 404 | rc = -ENODEV; |
| 405 | goto error; |
| 406 | } |
| 407 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 408 | if (dev->active_target) { |
| 409 | rc = -EBUSY; |
| 410 | goto error; |
| 411 | } |
| 412 | |
| 413 | target = nfc_find_target(dev, target_idx); |
| 414 | if (target == NULL) { |
| 415 | rc = -ENOTCONN; |
| 416 | goto error; |
| 417 | } |
| 418 | |
| 419 | rc = dev->ops->activate_target(dev, target, protocol); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 420 | if (!rc) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 421 | dev->active_target = target; |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 422 | dev->rf_mode = NFC_RF_INITIATOR; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 423 | |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 424 | if (dev->ops->check_presence && !dev->shutting_down) |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 425 | mod_timer(&dev->check_pres_timer, jiffies + |
| 426 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
| 427 | } |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 428 | |
| 429 | error: |
| 430 | device_unlock(&dev->dev); |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * nfc_deactivate_target - deactivate a nfc target |
| 436 | * |
| 437 | * @dev: The nfc device that found the target |
| 438 | * @target_idx: index of the target that must be deactivated |
| 439 | */ |
Christophe Ricard | 96d4581 | 2015-10-25 22:54:43 +0100 | [diff] [blame] | 440 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 441 | { |
| 442 | int rc = 0; |
| 443 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 444 | pr_debug("dev_name=%s target_idx=%u\n", |
| 445 | dev_name(&dev->dev), target_idx); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 446 | |
| 447 | device_lock(&dev->dev); |
| 448 | |
| 449 | if (!device_is_registered(&dev->dev)) { |
| 450 | rc = -ENODEV; |
| 451 | goto error; |
| 452 | } |
| 453 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 454 | if (dev->active_target == NULL) { |
| 455 | rc = -ENOTCONN; |
| 456 | goto error; |
| 457 | } |
| 458 | |
| 459 | if (dev->active_target->idx != target_idx) { |
| 460 | rc = -ENOTCONN; |
| 461 | goto error; |
| 462 | } |
| 463 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 464 | if (dev->ops->check_presence) |
| 465 | del_timer_sync(&dev->check_pres_timer); |
| 466 | |
Christophe Ricard | 96d4581 | 2015-10-25 22:54:43 +0100 | [diff] [blame] | 467 | dev->ops->deactivate_target(dev, dev->active_target, mode); |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 468 | dev->active_target = NULL; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 469 | |
| 470 | error: |
| 471 | device_unlock(&dev->dev); |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * nfc_data_exchange - transceive data |
| 477 | * |
| 478 | * @dev: The nfc device that found the target |
| 479 | * @target_idx: index of the target |
| 480 | * @skb: data to be sent |
| 481 | * @cb: callback called when the response is received |
| 482 | * @cb_context: parameter for the callback function |
| 483 | * |
| 484 | * The user must wait for the callback before calling this function again. |
| 485 | */ |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 486 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, |
| 487 | data_exchange_cb_t cb, void *cb_context) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 488 | { |
| 489 | int rc; |
| 490 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 491 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", |
| 492 | dev_name(&dev->dev), target_idx, skb->len); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 493 | |
| 494 | device_lock(&dev->dev); |
| 495 | |
| 496 | if (!device_is_registered(&dev->dev)) { |
| 497 | rc = -ENODEV; |
| 498 | kfree_skb(skb); |
| 499 | goto error; |
| 500 | } |
| 501 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 502 | if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) { |
| 503 | if (dev->active_target->idx != target_idx) { |
| 504 | rc = -EADDRNOTAVAIL; |
| 505 | kfree_skb(skb); |
| 506 | goto error; |
| 507 | } |
| 508 | |
| 509 | if (dev->ops->check_presence) |
| 510 | del_timer_sync(&dev->check_pres_timer); |
| 511 | |
| 512 | rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, |
| 513 | cb_context); |
| 514 | |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 515 | if (!rc && dev->ops->check_presence && !dev->shutting_down) |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 516 | mod_timer(&dev->check_pres_timer, jiffies + |
| 517 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
| 518 | } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { |
| 519 | rc = dev->ops->tm_send(dev, skb); |
| 520 | } else { |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame] | 521 | rc = -ENOTCONN; |
| 522 | kfree_skb(skb); |
| 523 | goto error; |
| 524 | } |
| 525 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 526 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 527 | error: |
| 528 | device_unlock(&dev->dev); |
| 529 | return rc; |
| 530 | } |
| 531 | |
Arron Wang | d8eb18e | 2013-08-23 16:02:08 +0800 | [diff] [blame] | 532 | struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx) |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 533 | { |
Axel Lin | 156cef8 | 2014-02-14 19:29:19 +0800 | [diff] [blame] | 534 | struct nfc_se *se; |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 535 | |
Axel Lin | 156cef8 | 2014-02-14 19:29:19 +0800 | [diff] [blame] | 536 | list_for_each_entry(se, &dev->secure_elements, list) |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 537 | if (se->idx == se_idx) |
| 538 | return se; |
| 539 | |
| 540 | return NULL; |
| 541 | } |
Arron Wang | d8eb18e | 2013-08-23 16:02:08 +0800 | [diff] [blame] | 542 | EXPORT_SYMBOL(nfc_find_se); |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 543 | |
| 544 | int nfc_enable_se(struct nfc_dev *dev, u32 se_idx) |
| 545 | { |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 546 | struct nfc_se *se; |
| 547 | int rc; |
| 548 | |
| 549 | pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); |
| 550 | |
| 551 | device_lock(&dev->dev); |
| 552 | |
| 553 | if (!device_is_registered(&dev->dev)) { |
| 554 | rc = -ENODEV; |
| 555 | goto error; |
| 556 | } |
| 557 | |
| 558 | if (!dev->dev_up) { |
| 559 | rc = -ENODEV; |
| 560 | goto error; |
| 561 | } |
| 562 | |
| 563 | if (dev->polling) { |
| 564 | rc = -EBUSY; |
| 565 | goto error; |
| 566 | } |
| 567 | |
| 568 | if (!dev->ops->enable_se || !dev->ops->disable_se) { |
| 569 | rc = -EOPNOTSUPP; |
| 570 | goto error; |
| 571 | } |
| 572 | |
Arron Wang | d8eb18e | 2013-08-23 16:02:08 +0800 | [diff] [blame] | 573 | se = nfc_find_se(dev, se_idx); |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 574 | if (!se) { |
| 575 | rc = -EINVAL; |
| 576 | goto error; |
| 577 | } |
| 578 | |
Arron Wang | 2c38328 | 2013-07-30 14:35:35 +0200 | [diff] [blame] | 579 | if (se->state == NFC_SE_ENABLED) { |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 580 | rc = -EALREADY; |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | rc = dev->ops->enable_se(dev, se_idx); |
Arron Wang | 39525ee1 | 2013-07-30 14:40:05 +0200 | [diff] [blame] | 585 | if (rc >= 0) |
| 586 | se->state = NFC_SE_ENABLED; |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 587 | |
| 588 | error: |
| 589 | device_unlock(&dev->dev); |
| 590 | return rc; |
| 591 | } |
| 592 | |
| 593 | int nfc_disable_se(struct nfc_dev *dev, u32 se_idx) |
| 594 | { |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 595 | struct nfc_se *se; |
| 596 | int rc; |
| 597 | |
| 598 | pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); |
| 599 | |
| 600 | device_lock(&dev->dev); |
| 601 | |
| 602 | if (!device_is_registered(&dev->dev)) { |
| 603 | rc = -ENODEV; |
| 604 | goto error; |
| 605 | } |
| 606 | |
| 607 | if (!dev->dev_up) { |
| 608 | rc = -ENODEV; |
| 609 | goto error; |
| 610 | } |
| 611 | |
| 612 | if (!dev->ops->enable_se || !dev->ops->disable_se) { |
| 613 | rc = -EOPNOTSUPP; |
| 614 | goto error; |
| 615 | } |
| 616 | |
Arron Wang | d8eb18e | 2013-08-23 16:02:08 +0800 | [diff] [blame] | 617 | se = nfc_find_se(dev, se_idx); |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 618 | if (!se) { |
| 619 | rc = -EINVAL; |
| 620 | goto error; |
| 621 | } |
| 622 | |
Arron Wang | 2c38328 | 2013-07-30 14:35:35 +0200 | [diff] [blame] | 623 | if (se->state == NFC_SE_DISABLED) { |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 624 | rc = -EALREADY; |
| 625 | goto error; |
| 626 | } |
| 627 | |
| 628 | rc = dev->ops->disable_se(dev, se_idx); |
Arron Wang | 39525ee1 | 2013-07-30 14:40:05 +0200 | [diff] [blame] | 629 | if (rc >= 0) |
| 630 | se->state = NFC_SE_DISABLED; |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 631 | |
| 632 | error: |
| 633 | device_unlock(&dev->dev); |
| 634 | return rc; |
| 635 | } |
| 636 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 637 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 638 | { |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 639 | pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 640 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 641 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 642 | } |
| 643 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); |
| 644 | |
Samuel Ortiz | ab73b75 | 2012-04-10 12:51:52 +0200 | [diff] [blame] | 645 | u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len) |
| 646 | { |
| 647 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
| 648 | |
| 649 | return nfc_llcp_general_bytes(dev, gb_len); |
| 650 | } |
| 651 | EXPORT_SYMBOL(nfc_get_local_general_bytes); |
| 652 | |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 653 | int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
| 654 | { |
| 655 | /* Only LLCP target mode for now */ |
| 656 | if (dev->dep_link_up == false) { |
| 657 | kfree_skb(skb); |
| 658 | return -ENOLINK; |
| 659 | } |
| 660 | |
| 661 | return nfc_llcp_data_received(dev, skb); |
| 662 | } |
| 663 | EXPORT_SYMBOL(nfc_tm_data_received); |
| 664 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 665 | int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode, |
| 666 | u8 *gb, size_t gb_len) |
| 667 | { |
| 668 | int rc; |
| 669 | |
| 670 | device_lock(&dev->dev); |
| 671 | |
| 672 | dev->polling = false; |
| 673 | |
| 674 | if (gb != NULL) { |
| 675 | rc = nfc_set_remote_general_bytes(dev, gb, gb_len); |
| 676 | if (rc < 0) |
| 677 | goto out; |
| 678 | } |
| 679 | |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 680 | dev->rf_mode = NFC_RF_TARGET; |
| 681 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 682 | if (protocol == NFC_PROTO_NFC_DEP_MASK) |
| 683 | nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET); |
| 684 | |
| 685 | rc = nfc_genl_tm_activated(dev, protocol); |
| 686 | |
| 687 | out: |
| 688 | device_unlock(&dev->dev); |
| 689 | |
| 690 | return rc; |
| 691 | } |
| 692 | EXPORT_SYMBOL(nfc_tm_activated); |
| 693 | |
| 694 | int nfc_tm_deactivated(struct nfc_dev *dev) |
| 695 | { |
| 696 | dev->dep_link_up = false; |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 697 | dev->rf_mode = NFC_RF_NONE; |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 698 | |
| 699 | return nfc_genl_tm_deactivated(dev); |
| 700 | } |
| 701 | EXPORT_SYMBOL(nfc_tm_deactivated); |
| 702 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 703 | /** |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 704 | * nfc_alloc_send_skb - allocate a skb for data exchange responses |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 705 | * |
| 706 | * @size: size to allocate |
| 707 | * @gfp: gfp flags |
| 708 | */ |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 709 | struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 710 | unsigned int flags, unsigned int size, |
| 711 | unsigned int *err) |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 712 | { |
| 713 | struct sk_buff *skb; |
| 714 | unsigned int total_size; |
| 715 | |
| 716 | total_size = size + |
| 717 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 718 | |
| 719 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); |
| 720 | if (skb) |
| 721 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 722 | |
| 723 | return skb; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses |
| 728 | * |
| 729 | * @size: size to allocate |
| 730 | * @gfp: gfp flags |
| 731 | */ |
| 732 | struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 733 | { |
| 734 | struct sk_buff *skb; |
| 735 | unsigned int total_size; |
| 736 | |
| 737 | total_size = size + 1; |
| 738 | skb = alloc_skb(total_size, gfp); |
| 739 | |
| 740 | if (skb) |
| 741 | skb_reserve(skb, 1); |
| 742 | |
| 743 | return skb; |
| 744 | } |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 745 | EXPORT_SYMBOL(nfc_alloc_recv_skb); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 746 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 747 | /** |
| 748 | * nfc_targets_found - inform that targets were found |
| 749 | * |
| 750 | * @dev: The nfc device that found the targets |
| 751 | * @targets: array of nfc targets found |
| 752 | * @ntargets: targets array size |
| 753 | * |
| 754 | * The device driver must call this function when one or many nfc targets |
| 755 | * are found. After calling this function, the device driver must stop |
| 756 | * polling for targets. |
Eric Lapuyade | d94f9c5 | 2012-05-03 16:33:32 +0200 | [diff] [blame] | 757 | * NOTE: This function can be called with targets=NULL and n_targets=0 to |
| 758 | * notify a driver error, meaning that the polling operation cannot complete. |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 759 | * IMPORTANT: this function must not be called from an atomic context. |
| 760 | * In addition, it must also not be called from a context that would prevent |
| 761 | * the NFC Core to call other nfc ops entry point concurrently. |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 762 | */ |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 763 | int nfc_targets_found(struct nfc_dev *dev, |
| 764 | struct nfc_target *targets, int n_targets) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 765 | { |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 766 | int i; |
| 767 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 768 | pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 769 | |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 770 | for (i = 0; i < n_targets; i++) |
Eric Lapuyade | 01ae0ee | 2012-04-10 19:43:10 +0200 | [diff] [blame] | 771 | targets[i].idx = dev->target_next_idx++; |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 772 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 773 | device_lock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 774 | |
Eric Lapuyade | 8668fdd | 2012-05-03 16:21:58 +0200 | [diff] [blame] | 775 | if (dev->polling == false) { |
| 776 | device_unlock(&dev->dev); |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | dev->polling = false; |
| 781 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 782 | dev->targets_generation++; |
| 783 | |
| 784 | kfree(dev->targets); |
Eric Lapuyade | d94f9c5 | 2012-05-03 16:33:32 +0200 | [diff] [blame] | 785 | dev->targets = NULL; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 786 | |
Eric Lapuyade | d94f9c5 | 2012-05-03 16:33:32 +0200 | [diff] [blame] | 787 | if (targets) { |
| 788 | dev->targets = kmemdup(targets, |
| 789 | n_targets * sizeof(struct nfc_target), |
| 790 | GFP_ATOMIC); |
| 791 | |
| 792 | if (!dev->targets) { |
| 793 | dev->n_targets = 0; |
| 794 | device_unlock(&dev->dev); |
| 795 | return -ENOMEM; |
| 796 | } |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | dev->n_targets = n_targets; |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 800 | device_unlock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 801 | |
| 802 | nfc_genl_targets_found(dev); |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | EXPORT_SYMBOL(nfc_targets_found); |
| 807 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 808 | /** |
| 809 | * nfc_target_lost - inform that an activated target went out of field |
| 810 | * |
| 811 | * @dev: The nfc device that had the activated target in field |
| 812 | * @target_idx: the nfc index of the target |
| 813 | * |
| 814 | * The device driver must call this function when the activated target |
| 815 | * goes out of the field. |
| 816 | * IMPORTANT: this function must not be called from an atomic context. |
| 817 | * In addition, it must also not be called from a context that would prevent |
| 818 | * the NFC Core to call other nfc ops entry point concurrently. |
| 819 | */ |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 820 | int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 821 | { |
| 822 | struct nfc_target *tg; |
| 823 | int i; |
| 824 | |
| 825 | pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); |
| 826 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 827 | device_lock(&dev->dev); |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 828 | |
| 829 | for (i = 0; i < dev->n_targets; i++) { |
| 830 | tg = &dev->targets[i]; |
| 831 | if (tg->idx == target_idx) |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | if (i == dev->n_targets) { |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 836 | device_unlock(&dev->dev); |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 837 | return -EINVAL; |
| 838 | } |
| 839 | |
| 840 | dev->targets_generation++; |
| 841 | dev->n_targets--; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 842 | dev->active_target = NULL; |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 843 | |
| 844 | if (dev->n_targets) { |
| 845 | memcpy(&dev->targets[i], &dev->targets[i + 1], |
| 846 | (dev->n_targets - i) * sizeof(struct nfc_target)); |
| 847 | } else { |
| 848 | kfree(dev->targets); |
| 849 | dev->targets = NULL; |
| 850 | } |
| 851 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 852 | device_unlock(&dev->dev); |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 853 | |
| 854 | nfc_genl_target_lost(dev, target_idx); |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | EXPORT_SYMBOL(nfc_target_lost); |
| 859 | |
Eric Lapuyade | 9eb334a | 2012-06-11 15:52:38 +0200 | [diff] [blame] | 860 | inline void nfc_driver_failure(struct nfc_dev *dev, int err) |
Eric Lapuyade | 456411c | 2012-06-11 13:49:51 +0200 | [diff] [blame] | 861 | { |
Eric Lapuyade | 9eb334a | 2012-06-11 15:52:38 +0200 | [diff] [blame] | 862 | nfc_targets_found(dev, NULL, 0); |
Eric Lapuyade | 456411c | 2012-06-11 13:49:51 +0200 | [diff] [blame] | 863 | } |
| 864 | EXPORT_SYMBOL(nfc_driver_failure); |
| 865 | |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 866 | int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type) |
| 867 | { |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 868 | struct nfc_se *se; |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 869 | int rc; |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 870 | |
| 871 | pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); |
| 872 | |
Arron Wang | d8eb18e | 2013-08-23 16:02:08 +0800 | [diff] [blame] | 873 | se = nfc_find_se(dev, se_idx); |
Samuel Ortiz | c531c9e | 2013-05-10 16:15:32 +0200 | [diff] [blame] | 874 | if (se) |
| 875 | return -EALREADY; |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 876 | |
| 877 | se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL); |
| 878 | if (!se) |
| 879 | return -ENOMEM; |
| 880 | |
| 881 | se->idx = se_idx; |
| 882 | se->type = type; |
| 883 | se->state = NFC_SE_DISABLED; |
| 884 | INIT_LIST_HEAD(&se->list); |
| 885 | |
| 886 | list_add(&se->list, &dev->secure_elements); |
| 887 | |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 888 | rc = nfc_genl_se_added(dev, se_idx, type); |
| 889 | if (rc < 0) { |
| 890 | list_del(&se->list); |
| 891 | kfree(se); |
| 892 | |
| 893 | return rc; |
| 894 | } |
| 895 | |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | EXPORT_SYMBOL(nfc_add_se); |
| 899 | |
| 900 | int nfc_remove_se(struct nfc_dev *dev, u32 se_idx) |
| 901 | { |
| 902 | struct nfc_se *se, *n; |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 903 | int rc; |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 904 | |
| 905 | pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); |
| 906 | |
| 907 | list_for_each_entry_safe(se, n, &dev->secure_elements, list) |
| 908 | if (se->idx == se_idx) { |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 909 | rc = nfc_genl_se_removed(dev, se_idx); |
| 910 | if (rc < 0) |
| 911 | return rc; |
| 912 | |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 913 | list_del(&se->list); |
| 914 | kfree(se); |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | EXPORT_SYMBOL(nfc_remove_se); |
| 922 | |
Christophe Ricard | 447b27c | 2015-02-01 22:26:16 +0100 | [diff] [blame] | 923 | int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx, |
| 924 | struct nfc_evt_transaction *evt_transaction) |
| 925 | { |
| 926 | int rc; |
| 927 | |
| 928 | pr_debug("transaction: %x\n", se_idx); |
| 929 | |
| 930 | device_lock(&dev->dev); |
| 931 | |
| 932 | if (!evt_transaction) { |
| 933 | rc = -EPROTO; |
| 934 | goto out; |
| 935 | } |
| 936 | |
| 937 | rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction); |
| 938 | out: |
| 939 | device_unlock(&dev->dev); |
| 940 | return rc; |
| 941 | } |
| 942 | EXPORT_SYMBOL(nfc_se_transaction); |
| 943 | |
Christophe Ricard | 9afec6d | 2015-12-23 23:45:18 +0100 | [diff] [blame] | 944 | int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx) |
| 945 | { |
| 946 | int rc; |
| 947 | |
| 948 | pr_debug("connectivity: %x\n", se_idx); |
| 949 | |
| 950 | device_lock(&dev->dev); |
| 951 | rc = nfc_genl_se_connectivity(dev, se_idx); |
| 952 | device_unlock(&dev->dev); |
| 953 | return rc; |
| 954 | } |
| 955 | EXPORT_SYMBOL(nfc_se_connectivity); |
| 956 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 957 | static void nfc_release(struct device *d) |
| 958 | { |
| 959 | struct nfc_dev *dev = to_nfc_dev(d); |
Samuel Ortiz | ee656e9 | 2013-05-10 15:53:29 +0200 | [diff] [blame] | 960 | struct nfc_se *se, *n; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 961 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 962 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 963 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 964 | nfc_genl_data_exit(&dev->genl_data); |
| 965 | kfree(dev->targets); |
Samuel Ortiz | ee656e9 | 2013-05-10 15:53:29 +0200 | [diff] [blame] | 966 | |
| 967 | list_for_each_entry_safe(se, n, &dev->secure_elements, list) { |
| 968 | nfc_genl_se_removed(dev, se->idx); |
| 969 | list_del(&se->list); |
| 970 | kfree(se); |
| 971 | } |
| 972 | |
Johan Hovold | 20777bc | 2017-03-30 12:15:35 +0200 | [diff] [blame] | 973 | ida_simple_remove(&nfc_index_ida, dev->idx); |
| 974 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 975 | kfree(dev); |
| 976 | } |
| 977 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 978 | static void nfc_check_pres_work(struct work_struct *work) |
| 979 | { |
| 980 | struct nfc_dev *dev = container_of(work, struct nfc_dev, |
| 981 | check_pres_work); |
| 982 | int rc; |
| 983 | |
| 984 | device_lock(&dev->dev); |
| 985 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 986 | if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { |
| 987 | rc = dev->ops->check_presence(dev, dev->active_target); |
Eric Lapuyade | 632c016 | 2012-10-02 17:27:36 +0200 | [diff] [blame] | 988 | if (rc == -EOPNOTSUPP) |
| 989 | goto exit; |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 990 | if (rc) { |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 991 | u32 active_target_idx = dev->active_target->idx; |
| 992 | device_unlock(&dev->dev); |
| 993 | nfc_target_lost(dev, active_target_idx); |
| 994 | return; |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 995 | } |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 996 | |
| 997 | if (!dev->shutting_down) |
| 998 | mod_timer(&dev->check_pres_timer, jiffies + |
| 999 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Eric Lapuyade | 632c016 | 2012-10-02 17:27:36 +0200 | [diff] [blame] | 1002 | exit: |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1003 | device_unlock(&dev->dev); |
| 1004 | } |
| 1005 | |
Allen Pais | 4b519bb | 2017-10-11 16:03:44 +0530 | [diff] [blame] | 1006 | static void nfc_check_pres_timeout(struct timer_list *t) |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1007 | { |
Allen Pais | 4b519bb | 2017-10-11 16:03:44 +0530 | [diff] [blame] | 1008 | struct nfc_dev *dev = from_timer(dev, t, check_pres_timer); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1009 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 1010 | schedule_work(&dev->check_pres_work); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1011 | } |
| 1012 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1013 | struct class nfc_class = { |
| 1014 | .name = "nfc", |
| 1015 | .dev_release = nfc_release, |
| 1016 | }; |
| 1017 | EXPORT_SYMBOL(nfc_class); |
| 1018 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 1019 | static int match_idx(struct device *d, const void *data) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1020 | { |
| 1021 | struct nfc_dev *dev = to_nfc_dev(d); |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 1022 | const unsigned int *idx = data; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1023 | |
| 1024 | return dev->idx == *idx; |
| 1025 | } |
| 1026 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1027 | struct nfc_dev *nfc_get_device(unsigned int idx) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1028 | { |
| 1029 | struct device *d; |
| 1030 | |
| 1031 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); |
| 1032 | if (!d) |
| 1033 | return NULL; |
| 1034 | |
| 1035 | return to_nfc_dev(d); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * nfc_allocate_device - allocate a new nfc device |
| 1040 | * |
| 1041 | * @ops: device operations |
| 1042 | * @supported_protocols: NFC protocols supported by the device |
| 1043 | */ |
| 1044 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 1045 | u32 supported_protocols, |
| 1046 | int tx_headroom, int tx_tailroom) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1047 | { |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1048 | struct nfc_dev *dev; |
Johan Hovold | 20777bc | 2017-03-30 12:15:35 +0200 | [diff] [blame] | 1049 | int rc; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1050 | |
| 1051 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 1052 | !ops->deactivate_target || !ops->im_transceive) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1053 | return NULL; |
| 1054 | |
| 1055 | if (!supported_protocols) |
| 1056 | return NULL; |
| 1057 | |
| 1058 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); |
| 1059 | if (!dev) |
| 1060 | return NULL; |
| 1061 | |
Johan Hovold | 20777bc | 2017-03-30 12:15:35 +0200 | [diff] [blame] | 1062 | rc = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL); |
| 1063 | if (rc < 0) |
| 1064 | goto err_free_dev; |
| 1065 | dev->idx = rc; |
| 1066 | |
| 1067 | dev->dev.class = &nfc_class; |
| 1068 | dev_set_name(&dev->dev, "nfc%d", dev->idx); |
| 1069 | device_initialize(&dev->dev); |
| 1070 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1071 | dev->ops = ops; |
| 1072 | dev->supported_protocols = supported_protocols; |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 1073 | dev->tx_headroom = tx_headroom; |
| 1074 | dev->tx_tailroom = tx_tailroom; |
Samuel Ortiz | fed7c25 | 2013-05-10 15:28:38 +0200 | [diff] [blame] | 1075 | INIT_LIST_HEAD(&dev->secure_elements); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1076 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1077 | nfc_genl_data_init(&dev->genl_data); |
| 1078 | |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 1079 | dev->rf_mode = NFC_RF_NONE; |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 1080 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1081 | /* first generation must not be 0 */ |
| 1082 | dev->targets_generation = 1; |
| 1083 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1084 | if (ops->check_presence) { |
Allen Pais | 4b519bb | 2017-10-11 16:03:44 +0530 | [diff] [blame] | 1085 | timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1086 | INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1089 | return dev; |
Johan Hovold | 20777bc | 2017-03-30 12:15:35 +0200 | [diff] [blame] | 1090 | |
| 1091 | err_free_dev: |
| 1092 | kfree(dev); |
| 1093 | |
Johan Hovold | c45e3e4 | 2017-07-09 13:08:58 +0200 | [diff] [blame] | 1094 | return NULL; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1095 | } |
| 1096 | EXPORT_SYMBOL(nfc_allocate_device); |
| 1097 | |
| 1098 | /** |
| 1099 | * nfc_register_device - register a nfc device in the nfc subsystem |
| 1100 | * |
| 1101 | * @dev: The nfc device to register |
| 1102 | */ |
| 1103 | int nfc_register_device(struct nfc_dev *dev) |
| 1104 | { |
| 1105 | int rc; |
| 1106 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 1107 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1108 | |
| 1109 | mutex_lock(&nfc_devlist_mutex); |
| 1110 | nfc_devlist_generation++; |
| 1111 | rc = device_add(&dev->dev); |
| 1112 | mutex_unlock(&nfc_devlist_mutex); |
| 1113 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1114 | if (rc < 0) |
| 1115 | return rc; |
| 1116 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1117 | rc = nfc_llcp_register_device(dev); |
| 1118 | if (rc) |
| 1119 | pr_err("Could not register llcp device\n"); |
| 1120 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1121 | rc = nfc_genl_device_added(dev); |
| 1122 | if (rc) |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 1123 | pr_debug("The userspace won't be notified that the device %s was added\n", |
| 1124 | dev_name(&dev->dev)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1125 | |
Samuel Ortiz | be055b2 | 2013-04-11 11:52:20 +0200 | [diff] [blame] | 1126 | dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev, |
| 1127 | RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev); |
| 1128 | if (dev->rfkill) { |
| 1129 | if (rfkill_register(dev->rfkill) < 0) { |
| 1130 | rfkill_destroy(dev->rfkill); |
| 1131 | dev->rfkill = NULL; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1135 | return 0; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1136 | } |
| 1137 | EXPORT_SYMBOL(nfc_register_device); |
| 1138 | |
| 1139 | /** |
| 1140 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem |
| 1141 | * |
| 1142 | * @dev: The nfc device to unregister |
| 1143 | */ |
| 1144 | void nfc_unregister_device(struct nfc_dev *dev) |
| 1145 | { |
Johan Hovold | 20777bc | 2017-03-30 12:15:35 +0200 | [diff] [blame] | 1146 | int rc; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1147 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 1148 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1149 | |
Samuel Ortiz | be055b2 | 2013-04-11 11:52:20 +0200 | [diff] [blame] | 1150 | if (dev->rfkill) { |
| 1151 | rfkill_unregister(dev->rfkill); |
| 1152 | rfkill_destroy(dev->rfkill); |
| 1153 | } |
| 1154 | |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 1155 | if (dev->ops->check_presence) { |
| 1156 | device_lock(&dev->dev); |
| 1157 | dev->shutting_down = true; |
| 1158 | device_unlock(&dev->dev); |
| 1159 | del_timer_sync(&dev->check_pres_timer); |
| 1160 | cancel_work_sync(&dev->check_pres_work); |
| 1161 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1162 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1163 | rc = nfc_genl_device_removed(dev); |
| 1164 | if (rc) |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 1165 | pr_debug("The userspace won't be notified that the device %s " |
| 1166 | "was removed\n", dev_name(&dev->dev)); |
| 1167 | |
| 1168 | nfc_llcp_unregister_device(dev); |
| 1169 | |
| 1170 | mutex_lock(&nfc_devlist_mutex); |
| 1171 | nfc_devlist_generation++; |
| 1172 | device_del(&dev->dev); |
| 1173 | mutex_unlock(&nfc_devlist_mutex); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1174 | } |
| 1175 | EXPORT_SYMBOL(nfc_unregister_device); |
| 1176 | |
| 1177 | static int __init nfc_init(void) |
| 1178 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1179 | int rc; |
| 1180 | |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 1181 | pr_info("NFC Core ver %s\n", VERSION); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1182 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1183 | rc = class_register(&nfc_class); |
| 1184 | if (rc) |
| 1185 | return rc; |
| 1186 | |
| 1187 | rc = nfc_genl_init(); |
| 1188 | if (rc) |
| 1189 | goto err_genl; |
| 1190 | |
| 1191 | /* the first generation must not be 0 */ |
| 1192 | nfc_devlist_generation = 1; |
| 1193 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 1194 | rc = rawsock_init(); |
| 1195 | if (rc) |
| 1196 | goto err_rawsock; |
| 1197 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1198 | rc = nfc_llcp_init(); |
| 1199 | if (rc) |
| 1200 | goto err_llcp_sock; |
| 1201 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 1202 | rc = af_nfc_init(); |
| 1203 | if (rc) |
| 1204 | goto err_af_nfc; |
| 1205 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1206 | return 0; |
| 1207 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 1208 | err_af_nfc: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1209 | nfc_llcp_exit(); |
| 1210 | err_llcp_sock: |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 1211 | rawsock_exit(); |
| 1212 | err_rawsock: |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 1213 | nfc_genl_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1214 | err_genl: |
| 1215 | class_unregister(&nfc_class); |
| 1216 | return rc; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | static void __exit nfc_exit(void) |
| 1220 | { |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 1221 | af_nfc_exit(); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1222 | nfc_llcp_exit(); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 1223 | rawsock_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1224 | nfc_genl_exit(); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 1225 | class_unregister(&nfc_class); |
| 1226 | } |
| 1227 | |
| 1228 | subsys_initcall(nfc_init); |
| 1229 | module_exit(nfc_exit); |
| 1230 | |
| 1231 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 1232 | MODULE_DESCRIPTION("NFC Core ver " VERSION); |
| 1233 | MODULE_VERSION(VERSION); |
| 1234 | MODULE_LICENSE("GPL"); |
Samuel Ortiz | 1155bb6 | 2012-06-12 00:35:50 +0200 | [diff] [blame] | 1235 | MODULE_ALIAS_NETPROTO(PF_NFC); |
Samuel Ortiz | 5df16cad | 2012-06-12 16:54:16 +0200 | [diff] [blame] | 1236 | MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME); |