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