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