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