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