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 | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 30 | #include <linux/nfc.h> |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 31 | |
Samuel Ortiz | 5df16ca | 2012-06-12 16:54:16 +0200 | [diff] [blame] | 32 | #include <net/genetlink.h> |
| 33 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 34 | #include "nfc.h" |
| 35 | |
| 36 | #define VERSION "0.1" |
| 37 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 38 | #define NFC_CHECK_PRES_FREQ_MS 2000 |
| 39 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 40 | int nfc_devlist_generation; |
| 41 | DEFINE_MUTEX(nfc_devlist_mutex); |
| 42 | |
Samuel Ortiz | 7eda8b8e9 | 2012-10-22 15:57:58 +0200 | [diff] [blame] | 43 | /* NFC device ID bitmap */ |
| 44 | static DEFINE_IDA(nfc_index_ida); |
| 45 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 46 | /** |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 47 | * nfc_dev_up - turn on the NFC device |
| 48 | * |
| 49 | * @dev: The nfc device to be turned on |
| 50 | * |
| 51 | * The device remains up until the nfc_dev_down function is called. |
| 52 | */ |
| 53 | int nfc_dev_up(struct nfc_dev *dev) |
| 54 | { |
| 55 | int rc = 0; |
| 56 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 57 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 58 | |
| 59 | device_lock(&dev->dev); |
| 60 | |
| 61 | if (!device_is_registered(&dev->dev)) { |
| 62 | rc = -ENODEV; |
| 63 | goto error; |
| 64 | } |
| 65 | |
| 66 | if (dev->dev_up) { |
| 67 | rc = -EALREADY; |
| 68 | goto error; |
| 69 | } |
| 70 | |
| 71 | if (dev->ops->dev_up) |
| 72 | rc = dev->ops->dev_up(dev); |
| 73 | |
| 74 | if (!rc) |
| 75 | dev->dev_up = true; |
| 76 | |
| 77 | error: |
| 78 | device_unlock(&dev->dev); |
| 79 | return rc; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * nfc_dev_down - turn off the NFC device |
| 84 | * |
| 85 | * @dev: The nfc device to be turned off |
| 86 | */ |
| 87 | int nfc_dev_down(struct nfc_dev *dev) |
| 88 | { |
| 89 | int rc = 0; |
| 90 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 91 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 92 | |
| 93 | device_lock(&dev->dev); |
| 94 | |
| 95 | if (!device_is_registered(&dev->dev)) { |
| 96 | rc = -ENODEV; |
| 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | if (!dev->dev_up) { |
| 101 | rc = -EALREADY; |
| 102 | goto error; |
| 103 | } |
| 104 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 105 | if (dev->polling || dev->active_target) { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 106 | rc = -EBUSY; |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | if (dev->ops->dev_down) |
| 111 | dev->ops->dev_down(dev); |
| 112 | |
| 113 | dev->dev_up = false; |
| 114 | |
| 115 | error: |
| 116 | device_unlock(&dev->dev); |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | /** |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 121 | * nfc_start_poll - start polling for nfc targets |
| 122 | * |
| 123 | * @dev: The nfc device that must start polling |
| 124 | * @protocols: bitset of nfc protocols that must be used for polling |
| 125 | * |
| 126 | * The device remains polling for targets until a target is found or |
| 127 | * the nfc_stop_poll function is called. |
| 128 | */ |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 129 | 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] | 130 | { |
| 131 | int rc; |
| 132 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 133 | pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n", |
| 134 | dev_name(&dev->dev), im_protocols, tm_protocols); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 135 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 136 | if (!im_protocols && !tm_protocols) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 137 | return -EINVAL; |
| 138 | |
| 139 | device_lock(&dev->dev); |
| 140 | |
| 141 | if (!device_is_registered(&dev->dev)) { |
| 142 | rc = -ENODEV; |
| 143 | goto error; |
| 144 | } |
| 145 | |
Samuel Ortiz | 7757dc8 | 2013-04-10 12:25:30 +0200 | [diff] [blame^] | 146 | if (!dev->dev_up) { |
| 147 | rc = -ENODEV; |
| 148 | goto error; |
| 149 | } |
| 150 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 151 | if (dev->polling) { |
| 152 | rc = -EBUSY; |
| 153 | goto error; |
| 154 | } |
| 155 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 156 | rc = dev->ops->start_poll(dev, im_protocols, tm_protocols); |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 157 | if (!rc) { |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 158 | dev->polling = true; |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 159 | dev->rf_mode = NFC_RF_NONE; |
| 160 | } |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 161 | |
| 162 | error: |
| 163 | device_unlock(&dev->dev); |
| 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * nfc_stop_poll - stop polling for nfc targets |
| 169 | * |
| 170 | * @dev: The nfc device that must stop polling |
| 171 | */ |
| 172 | int nfc_stop_poll(struct nfc_dev *dev) |
| 173 | { |
| 174 | int rc = 0; |
| 175 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 176 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 177 | |
| 178 | device_lock(&dev->dev); |
| 179 | |
| 180 | if (!device_is_registered(&dev->dev)) { |
| 181 | rc = -ENODEV; |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | if (!dev->polling) { |
| 186 | rc = -EINVAL; |
| 187 | goto error; |
| 188 | } |
| 189 | |
| 190 | dev->ops->stop_poll(dev); |
| 191 | dev->polling = false; |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 192 | dev->rf_mode = NFC_RF_NONE; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 193 | |
| 194 | error: |
| 195 | device_unlock(&dev->dev); |
| 196 | return rc; |
| 197 | } |
| 198 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 199 | static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx) |
| 200 | { |
| 201 | int i; |
| 202 | |
| 203 | if (dev->n_targets == 0) |
| 204 | return NULL; |
| 205 | |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 206 | for (i = 0; i < dev->n_targets; i++) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 207 | if (dev->targets[i].idx == target_idx) |
| 208 | return &dev->targets[i]; |
| 209 | } |
| 210 | |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 214 | 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] | 215 | { |
| 216 | int rc = 0; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 217 | u8 *gb; |
| 218 | size_t gb_len; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 219 | struct nfc_target *target; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 220 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 221 | 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] | 222 | |
| 223 | if (!dev->ops->dep_link_up) |
| 224 | return -EOPNOTSUPP; |
| 225 | |
| 226 | device_lock(&dev->dev); |
| 227 | |
| 228 | if (!device_is_registered(&dev->dev)) { |
| 229 | rc = -ENODEV; |
| 230 | goto error; |
| 231 | } |
| 232 | |
| 233 | if (dev->dep_link_up == true) { |
| 234 | rc = -EALREADY; |
| 235 | goto error; |
| 236 | } |
| 237 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 238 | gb = nfc_llcp_general_bytes(dev, &gb_len); |
| 239 | if (gb_len > NFC_MAX_GT_LEN) { |
| 240 | rc = -EINVAL; |
| 241 | goto error; |
| 242 | } |
| 243 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 244 | target = nfc_find_target(dev, target_index); |
| 245 | if (target == NULL) { |
| 246 | rc = -ENOTCONN; |
| 247 | goto error; |
| 248 | } |
| 249 | |
| 250 | 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] | 251 | if (!rc) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 252 | dev->active_target = target; |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 253 | dev->rf_mode = NFC_RF_INITIATOR; |
| 254 | } |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 255 | |
| 256 | error: |
| 257 | device_unlock(&dev->dev); |
| 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | int nfc_dep_link_down(struct nfc_dev *dev) |
| 262 | { |
| 263 | int rc = 0; |
| 264 | |
| 265 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
| 266 | |
| 267 | if (!dev->ops->dep_link_down) |
| 268 | return -EOPNOTSUPP; |
| 269 | |
| 270 | device_lock(&dev->dev); |
| 271 | |
| 272 | if (!device_is_registered(&dev->dev)) { |
| 273 | rc = -ENODEV; |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | if (dev->dep_link_up == false) { |
| 278 | rc = -EALREADY; |
| 279 | goto error; |
| 280 | } |
| 281 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 282 | rc = dev->ops->dep_link_down(dev); |
| 283 | if (!rc) { |
| 284 | dev->dep_link_up = false; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 285 | dev->active_target = NULL; |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 286 | dev->rf_mode = NFC_RF_NONE; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 287 | nfc_llcp_mac_is_down(dev); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 288 | nfc_genl_dep_link_down_event(dev); |
| 289 | } |
| 290 | |
| 291 | error: |
| 292 | device_unlock(&dev->dev); |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 293 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 294 | return rc; |
| 295 | } |
| 296 | |
| 297 | 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] | 298 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 299 | { |
| 300 | dev->dep_link_up = true; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 301 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 302 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); |
| 303 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 304 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); |
| 305 | } |
| 306 | EXPORT_SYMBOL(nfc_dep_link_is_up); |
| 307 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 308 | /** |
| 309 | * nfc_activate_target - prepare the target for data exchange |
| 310 | * |
| 311 | * @dev: The nfc device that found the target |
| 312 | * @target_idx: index of the target that must be activated |
| 313 | * @protocol: nfc protocol that will be used for data exchange |
| 314 | */ |
| 315 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) |
| 316 | { |
| 317 | int rc; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 318 | struct nfc_target *target; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 319 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 320 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", |
| 321 | dev_name(&dev->dev), target_idx, protocol); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 322 | |
| 323 | device_lock(&dev->dev); |
| 324 | |
| 325 | if (!device_is_registered(&dev->dev)) { |
| 326 | rc = -ENODEV; |
| 327 | goto error; |
| 328 | } |
| 329 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 330 | if (dev->active_target) { |
| 331 | rc = -EBUSY; |
| 332 | goto error; |
| 333 | } |
| 334 | |
| 335 | target = nfc_find_target(dev, target_idx); |
| 336 | if (target == NULL) { |
| 337 | rc = -ENOTCONN; |
| 338 | goto error; |
| 339 | } |
| 340 | |
| 341 | rc = dev->ops->activate_target(dev, target, protocol); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 342 | if (!rc) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 343 | dev->active_target = target; |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 344 | dev->rf_mode = NFC_RF_INITIATOR; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 345 | |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 346 | if (dev->ops->check_presence && !dev->shutting_down) |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 347 | mod_timer(&dev->check_pres_timer, jiffies + |
| 348 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
| 349 | } |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 350 | |
| 351 | error: |
| 352 | device_unlock(&dev->dev); |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * nfc_deactivate_target - deactivate a nfc target |
| 358 | * |
| 359 | * @dev: The nfc device that found the target |
| 360 | * @target_idx: index of the target that must be deactivated |
| 361 | */ |
| 362 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) |
| 363 | { |
| 364 | int rc = 0; |
| 365 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 366 | pr_debug("dev_name=%s target_idx=%u\n", |
| 367 | dev_name(&dev->dev), target_idx); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 368 | |
| 369 | device_lock(&dev->dev); |
| 370 | |
| 371 | if (!device_is_registered(&dev->dev)) { |
| 372 | rc = -ENODEV; |
| 373 | goto error; |
| 374 | } |
| 375 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 376 | if (dev->active_target == NULL) { |
| 377 | rc = -ENOTCONN; |
| 378 | goto error; |
| 379 | } |
| 380 | |
| 381 | if (dev->active_target->idx != target_idx) { |
| 382 | rc = -ENOTCONN; |
| 383 | goto error; |
| 384 | } |
| 385 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 386 | if (dev->ops->check_presence) |
| 387 | del_timer_sync(&dev->check_pres_timer); |
| 388 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 389 | dev->ops->deactivate_target(dev, dev->active_target); |
| 390 | dev->active_target = NULL; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 391 | |
| 392 | error: |
| 393 | device_unlock(&dev->dev); |
| 394 | return rc; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * nfc_data_exchange - transceive data |
| 399 | * |
| 400 | * @dev: The nfc device that found the target |
| 401 | * @target_idx: index of the target |
| 402 | * @skb: data to be sent |
| 403 | * @cb: callback called when the response is received |
| 404 | * @cb_context: parameter for the callback function |
| 405 | * |
| 406 | * The user must wait for the callback before calling this function again. |
| 407 | */ |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 408 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, |
| 409 | data_exchange_cb_t cb, void *cb_context) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 410 | { |
| 411 | int rc; |
| 412 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 413 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", |
| 414 | dev_name(&dev->dev), target_idx, skb->len); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 415 | |
| 416 | device_lock(&dev->dev); |
| 417 | |
| 418 | if (!device_is_registered(&dev->dev)) { |
| 419 | rc = -ENODEV; |
| 420 | kfree_skb(skb); |
| 421 | goto error; |
| 422 | } |
| 423 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 424 | if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) { |
| 425 | if (dev->active_target->idx != target_idx) { |
| 426 | rc = -EADDRNOTAVAIL; |
| 427 | kfree_skb(skb); |
| 428 | goto error; |
| 429 | } |
| 430 | |
| 431 | if (dev->ops->check_presence) |
| 432 | del_timer_sync(&dev->check_pres_timer); |
| 433 | |
| 434 | rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, |
| 435 | cb_context); |
| 436 | |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 437 | if (!rc && dev->ops->check_presence && !dev->shutting_down) |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 438 | mod_timer(&dev->check_pres_timer, jiffies + |
| 439 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
| 440 | } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { |
| 441 | rc = dev->ops->tm_send(dev, skb); |
| 442 | } else { |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame] | 443 | rc = -ENOTCONN; |
| 444 | kfree_skb(skb); |
| 445 | goto error; |
| 446 | } |
| 447 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 448 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 449 | error: |
| 450 | device_unlock(&dev->dev); |
| 451 | return rc; |
| 452 | } |
| 453 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 454 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 455 | { |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 456 | 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] | 457 | |
| 458 | if (gb_len > NFC_MAX_GT_LEN) |
| 459 | return -EINVAL; |
| 460 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 461 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 462 | } |
| 463 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); |
| 464 | |
Samuel Ortiz | ab73b75 | 2012-04-10 12:51:52 +0200 | [diff] [blame] | 465 | u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len) |
| 466 | { |
| 467 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
| 468 | |
| 469 | return nfc_llcp_general_bytes(dev, gb_len); |
| 470 | } |
| 471 | EXPORT_SYMBOL(nfc_get_local_general_bytes); |
| 472 | |
Samuel Ortiz | 73167ce | 2012-05-31 00:05:50 +0200 | [diff] [blame] | 473 | int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
| 474 | { |
| 475 | /* Only LLCP target mode for now */ |
| 476 | if (dev->dep_link_up == false) { |
| 477 | kfree_skb(skb); |
| 478 | return -ENOLINK; |
| 479 | } |
| 480 | |
| 481 | return nfc_llcp_data_received(dev, skb); |
| 482 | } |
| 483 | EXPORT_SYMBOL(nfc_tm_data_received); |
| 484 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 485 | int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode, |
| 486 | u8 *gb, size_t gb_len) |
| 487 | { |
| 488 | int rc; |
| 489 | |
| 490 | device_lock(&dev->dev); |
| 491 | |
| 492 | dev->polling = false; |
| 493 | |
| 494 | if (gb != NULL) { |
| 495 | rc = nfc_set_remote_general_bytes(dev, gb, gb_len); |
| 496 | if (rc < 0) |
| 497 | goto out; |
| 498 | } |
| 499 | |
Samuel Ortiz | f212ad5 | 2012-05-31 00:02:26 +0200 | [diff] [blame] | 500 | dev->rf_mode = NFC_RF_TARGET; |
| 501 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 502 | if (protocol == NFC_PROTO_NFC_DEP_MASK) |
| 503 | nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET); |
| 504 | |
| 505 | rc = nfc_genl_tm_activated(dev, protocol); |
| 506 | |
| 507 | out: |
| 508 | device_unlock(&dev->dev); |
| 509 | |
| 510 | return rc; |
| 511 | } |
| 512 | EXPORT_SYMBOL(nfc_tm_activated); |
| 513 | |
| 514 | int nfc_tm_deactivated(struct nfc_dev *dev) |
| 515 | { |
| 516 | dev->dep_link_up = false; |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 517 | dev->rf_mode = NFC_RF_NONE; |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 518 | |
| 519 | return nfc_genl_tm_deactivated(dev); |
| 520 | } |
| 521 | EXPORT_SYMBOL(nfc_tm_deactivated); |
| 522 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 523 | /** |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 524 | * nfc_alloc_send_skb - allocate a skb for data exchange responses |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 525 | * |
| 526 | * @size: size to allocate |
| 527 | * @gfp: gfp flags |
| 528 | */ |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 529 | 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] | 530 | unsigned int flags, unsigned int size, |
| 531 | unsigned int *err) |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 532 | { |
| 533 | struct sk_buff *skb; |
| 534 | unsigned int total_size; |
| 535 | |
| 536 | total_size = size + |
| 537 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 538 | |
| 539 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); |
| 540 | if (skb) |
| 541 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 542 | |
| 543 | return skb; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses |
| 548 | * |
| 549 | * @size: size to allocate |
| 550 | * @gfp: gfp flags |
| 551 | */ |
| 552 | 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] | 553 | { |
| 554 | struct sk_buff *skb; |
| 555 | unsigned int total_size; |
| 556 | |
| 557 | total_size = size + 1; |
| 558 | skb = alloc_skb(total_size, gfp); |
| 559 | |
| 560 | if (skb) |
| 561 | skb_reserve(skb, 1); |
| 562 | |
| 563 | return skb; |
| 564 | } |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 565 | EXPORT_SYMBOL(nfc_alloc_recv_skb); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 566 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 567 | /** |
| 568 | * nfc_targets_found - inform that targets were found |
| 569 | * |
| 570 | * @dev: The nfc device that found the targets |
| 571 | * @targets: array of nfc targets found |
| 572 | * @ntargets: targets array size |
| 573 | * |
| 574 | * The device driver must call this function when one or many nfc targets |
| 575 | * are found. After calling this function, the device driver must stop |
| 576 | * polling for targets. |
Eric Lapuyade | d94f9c5 | 2012-05-03 16:33:32 +0200 | [diff] [blame] | 577 | * NOTE: This function can be called with targets=NULL and n_targets=0 to |
| 578 | * notify a driver error, meaning that the polling operation cannot complete. |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 579 | * IMPORTANT: this function must not be called from an atomic context. |
| 580 | * In addition, it must also not be called from a context that would prevent |
| 581 | * the NFC Core to call other nfc ops entry point concurrently. |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 582 | */ |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 583 | int nfc_targets_found(struct nfc_dev *dev, |
| 584 | struct nfc_target *targets, int n_targets) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 585 | { |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 586 | int i; |
| 587 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 588 | 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] | 589 | |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 590 | for (i = 0; i < n_targets; i++) |
Eric Lapuyade | 01ae0ee | 2012-04-10 19:43:10 +0200 | [diff] [blame] | 591 | targets[i].idx = dev->target_next_idx++; |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 592 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 593 | device_lock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 594 | |
Eric Lapuyade | 8668fdd | 2012-05-03 16:21:58 +0200 | [diff] [blame] | 595 | if (dev->polling == false) { |
| 596 | device_unlock(&dev->dev); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | dev->polling = false; |
| 601 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 602 | dev->targets_generation++; |
| 603 | |
| 604 | kfree(dev->targets); |
Eric Lapuyade | d94f9c5 | 2012-05-03 16:33:32 +0200 | [diff] [blame] | 605 | dev->targets = NULL; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 606 | |
Eric Lapuyade | d94f9c5 | 2012-05-03 16:33:32 +0200 | [diff] [blame] | 607 | if (targets) { |
| 608 | dev->targets = kmemdup(targets, |
| 609 | n_targets * sizeof(struct nfc_target), |
| 610 | GFP_ATOMIC); |
| 611 | |
| 612 | if (!dev->targets) { |
| 613 | dev->n_targets = 0; |
| 614 | device_unlock(&dev->dev); |
| 615 | return -ENOMEM; |
| 616 | } |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | dev->n_targets = n_targets; |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 620 | device_unlock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 621 | |
| 622 | nfc_genl_targets_found(dev); |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | EXPORT_SYMBOL(nfc_targets_found); |
| 627 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 628 | /** |
| 629 | * nfc_target_lost - inform that an activated target went out of field |
| 630 | * |
| 631 | * @dev: The nfc device that had the activated target in field |
| 632 | * @target_idx: the nfc index of the target |
| 633 | * |
| 634 | * The device driver must call this function when the activated target |
| 635 | * goes out of the field. |
| 636 | * IMPORTANT: this function must not be called from an atomic context. |
| 637 | * In addition, it must also not be called from a context that would prevent |
| 638 | * the NFC Core to call other nfc ops entry point concurrently. |
| 639 | */ |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 640 | int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 641 | { |
| 642 | struct nfc_target *tg; |
| 643 | int i; |
| 644 | |
| 645 | pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); |
| 646 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 647 | device_lock(&dev->dev); |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 648 | |
| 649 | for (i = 0; i < dev->n_targets; i++) { |
| 650 | tg = &dev->targets[i]; |
| 651 | if (tg->idx == target_idx) |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | if (i == dev->n_targets) { |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 656 | device_unlock(&dev->dev); |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 657 | return -EINVAL; |
| 658 | } |
| 659 | |
| 660 | dev->targets_generation++; |
| 661 | dev->n_targets--; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 662 | dev->active_target = NULL; |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 663 | |
| 664 | if (dev->n_targets) { |
| 665 | memcpy(&dev->targets[i], &dev->targets[i + 1], |
| 666 | (dev->n_targets - i) * sizeof(struct nfc_target)); |
| 667 | } else { |
| 668 | kfree(dev->targets); |
| 669 | dev->targets = NULL; |
| 670 | } |
| 671 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 672 | device_unlock(&dev->dev); |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 673 | |
| 674 | nfc_genl_target_lost(dev, target_idx); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | EXPORT_SYMBOL(nfc_target_lost); |
| 679 | |
Eric Lapuyade | 9eb334a | 2012-06-11 15:52:38 +0200 | [diff] [blame] | 680 | inline void nfc_driver_failure(struct nfc_dev *dev, int err) |
Eric Lapuyade | 456411c | 2012-06-11 13:49:51 +0200 | [diff] [blame] | 681 | { |
Eric Lapuyade | 9eb334a | 2012-06-11 15:52:38 +0200 | [diff] [blame] | 682 | nfc_targets_found(dev, NULL, 0); |
Eric Lapuyade | 456411c | 2012-06-11 13:49:51 +0200 | [diff] [blame] | 683 | } |
| 684 | EXPORT_SYMBOL(nfc_driver_failure); |
| 685 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 686 | static void nfc_release(struct device *d) |
| 687 | { |
| 688 | struct nfc_dev *dev = to_nfc_dev(d); |
| 689 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 690 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 691 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 692 | nfc_genl_data_exit(&dev->genl_data); |
| 693 | kfree(dev->targets); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 694 | kfree(dev); |
| 695 | } |
| 696 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 697 | static void nfc_check_pres_work(struct work_struct *work) |
| 698 | { |
| 699 | struct nfc_dev *dev = container_of(work, struct nfc_dev, |
| 700 | check_pres_work); |
| 701 | int rc; |
| 702 | |
| 703 | device_lock(&dev->dev); |
| 704 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 705 | if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { |
| 706 | rc = dev->ops->check_presence(dev, dev->active_target); |
Eric Lapuyade | 632c016 | 2012-10-02 17:27:36 +0200 | [diff] [blame] | 707 | if (rc == -EOPNOTSUPP) |
| 708 | goto exit; |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 709 | if (rc) { |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 710 | u32 active_target_idx = dev->active_target->idx; |
| 711 | device_unlock(&dev->dev); |
| 712 | nfc_target_lost(dev, active_target_idx); |
| 713 | return; |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 714 | } |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 715 | |
| 716 | if (!dev->shutting_down) |
| 717 | mod_timer(&dev->check_pres_timer, jiffies + |
| 718 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Eric Lapuyade | 632c016 | 2012-10-02 17:27:36 +0200 | [diff] [blame] | 721 | exit: |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 722 | device_unlock(&dev->dev); |
| 723 | } |
| 724 | |
| 725 | static void nfc_check_pres_timeout(unsigned long data) |
| 726 | { |
| 727 | struct nfc_dev *dev = (struct nfc_dev *)data; |
| 728 | |
Linus Torvalds | 916082b | 2012-10-02 16:01:31 -0700 | [diff] [blame] | 729 | schedule_work(&dev->check_pres_work); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 732 | struct class nfc_class = { |
| 733 | .name = "nfc", |
| 734 | .dev_release = nfc_release, |
| 735 | }; |
| 736 | EXPORT_SYMBOL(nfc_class); |
| 737 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 738 | static int match_idx(struct device *d, const void *data) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 739 | { |
| 740 | struct nfc_dev *dev = to_nfc_dev(d); |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 741 | const unsigned int *idx = data; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 742 | |
| 743 | return dev->idx == *idx; |
| 744 | } |
| 745 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 746 | struct nfc_dev *nfc_get_device(unsigned int idx) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 747 | { |
| 748 | struct device *d; |
| 749 | |
| 750 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); |
| 751 | if (!d) |
| 752 | return NULL; |
| 753 | |
| 754 | return to_nfc_dev(d); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * nfc_allocate_device - allocate a new nfc device |
| 759 | * |
| 760 | * @ops: device operations |
| 761 | * @supported_protocols: NFC protocols supported by the device |
| 762 | */ |
| 763 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 764 | u32 supported_protocols, |
Samuel Ortiz | 390a1bd | 2012-12-19 19:11:32 +0100 | [diff] [blame] | 765 | u32 supported_se, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 766 | int tx_headroom, int tx_tailroom) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 767 | { |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 768 | struct nfc_dev *dev; |
| 769 | |
| 770 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 771 | !ops->deactivate_target || !ops->im_transceive) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 772 | return NULL; |
| 773 | |
| 774 | if (!supported_protocols) |
| 775 | return NULL; |
| 776 | |
| 777 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); |
| 778 | if (!dev) |
| 779 | return NULL; |
| 780 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 781 | dev->ops = ops; |
| 782 | dev->supported_protocols = supported_protocols; |
Samuel Ortiz | 390a1bd | 2012-12-19 19:11:32 +0100 | [diff] [blame] | 783 | dev->supported_se = supported_se; |
| 784 | dev->active_se = NFC_SE_NONE; |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 785 | dev->tx_headroom = tx_headroom; |
| 786 | dev->tx_tailroom = tx_tailroom; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 787 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 788 | nfc_genl_data_init(&dev->genl_data); |
| 789 | |
Thierry Escande | 5bcf099 | 2012-10-05 11:05:45 +0200 | [diff] [blame] | 790 | dev->rf_mode = NFC_RF_NONE; |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 791 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 792 | /* first generation must not be 0 */ |
| 793 | dev->targets_generation = 1; |
| 794 | |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 795 | if (ops->check_presence) { |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 796 | init_timer(&dev->check_pres_timer); |
| 797 | dev->check_pres_timer.data = (unsigned long)dev; |
| 798 | dev->check_pres_timer.function = nfc_check_pres_timeout; |
| 799 | |
| 800 | INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); |
Eric Lapuyade | c8d56ae | 2012-04-10 19:43:12 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 803 | return dev; |
| 804 | } |
| 805 | EXPORT_SYMBOL(nfc_allocate_device); |
| 806 | |
| 807 | /** |
| 808 | * nfc_register_device - register a nfc device in the nfc subsystem |
| 809 | * |
| 810 | * @dev: The nfc device to register |
| 811 | */ |
| 812 | int nfc_register_device(struct nfc_dev *dev) |
| 813 | { |
| 814 | int rc; |
| 815 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 816 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 817 | |
Samuel Ortiz | 7eda8b8e9 | 2012-10-22 15:57:58 +0200 | [diff] [blame] | 818 | dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL); |
| 819 | if (dev->idx < 0) |
| 820 | return dev->idx; |
| 821 | |
| 822 | dev->dev.class = &nfc_class; |
| 823 | dev_set_name(&dev->dev, "nfc%d", dev->idx); |
| 824 | device_initialize(&dev->dev); |
| 825 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 826 | mutex_lock(&nfc_devlist_mutex); |
| 827 | nfc_devlist_generation++; |
| 828 | rc = device_add(&dev->dev); |
| 829 | mutex_unlock(&nfc_devlist_mutex); |
| 830 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 831 | if (rc < 0) |
| 832 | return rc; |
| 833 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 834 | rc = nfc_llcp_register_device(dev); |
| 835 | if (rc) |
| 836 | pr_err("Could not register llcp device\n"); |
| 837 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 838 | rc = nfc_genl_device_added(dev); |
| 839 | if (rc) |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 840 | pr_debug("The userspace won't be notified that the device %s was added\n", |
| 841 | dev_name(&dev->dev)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 842 | |
| 843 | return 0; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 844 | } |
| 845 | EXPORT_SYMBOL(nfc_register_device); |
| 846 | |
| 847 | /** |
| 848 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem |
| 849 | * |
| 850 | * @dev: The nfc device to unregister |
| 851 | */ |
| 852 | void nfc_unregister_device(struct nfc_dev *dev) |
| 853 | { |
Samuel Ortiz | 7eda8b8e9 | 2012-10-22 15:57:58 +0200 | [diff] [blame] | 854 | int rc, id; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 855 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 856 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 857 | |
Samuel Ortiz | 7eda8b8e9 | 2012-10-22 15:57:58 +0200 | [diff] [blame] | 858 | id = dev->idx; |
| 859 | |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 860 | if (dev->ops->check_presence) { |
| 861 | device_lock(&dev->dev); |
| 862 | dev->shutting_down = true; |
| 863 | device_unlock(&dev->dev); |
| 864 | del_timer_sync(&dev->check_pres_timer); |
| 865 | cancel_work_sync(&dev->check_pres_work); |
| 866 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 867 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 868 | rc = nfc_genl_device_removed(dev); |
| 869 | if (rc) |
Eric Lapuyade | f0c9103 | 2012-11-26 18:06:27 +0100 | [diff] [blame] | 870 | pr_debug("The userspace won't be notified that the device %s " |
| 871 | "was removed\n", dev_name(&dev->dev)); |
| 872 | |
| 873 | nfc_llcp_unregister_device(dev); |
| 874 | |
| 875 | mutex_lock(&nfc_devlist_mutex); |
| 876 | nfc_devlist_generation++; |
| 877 | device_del(&dev->dev); |
| 878 | mutex_unlock(&nfc_devlist_mutex); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 879 | |
Samuel Ortiz | 7eda8b8e9 | 2012-10-22 15:57:58 +0200 | [diff] [blame] | 880 | ida_simple_remove(&nfc_index_ida, id); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 881 | } |
| 882 | EXPORT_SYMBOL(nfc_unregister_device); |
| 883 | |
| 884 | static int __init nfc_init(void) |
| 885 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 886 | int rc; |
| 887 | |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 888 | pr_info("NFC Core ver %s\n", VERSION); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 889 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 890 | rc = class_register(&nfc_class); |
| 891 | if (rc) |
| 892 | return rc; |
| 893 | |
| 894 | rc = nfc_genl_init(); |
| 895 | if (rc) |
| 896 | goto err_genl; |
| 897 | |
| 898 | /* the first generation must not be 0 */ |
| 899 | nfc_devlist_generation = 1; |
| 900 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 901 | rc = rawsock_init(); |
| 902 | if (rc) |
| 903 | goto err_rawsock; |
| 904 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 905 | rc = nfc_llcp_init(); |
| 906 | if (rc) |
| 907 | goto err_llcp_sock; |
| 908 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 909 | rc = af_nfc_init(); |
| 910 | if (rc) |
| 911 | goto err_af_nfc; |
| 912 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 913 | return 0; |
| 914 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 915 | err_af_nfc: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 916 | nfc_llcp_exit(); |
| 917 | err_llcp_sock: |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 918 | rawsock_exit(); |
| 919 | err_rawsock: |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 920 | nfc_genl_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 921 | err_genl: |
| 922 | class_unregister(&nfc_class); |
| 923 | return rc; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static void __exit nfc_exit(void) |
| 927 | { |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 928 | af_nfc_exit(); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 929 | nfc_llcp_exit(); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 930 | rawsock_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 931 | nfc_genl_exit(); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 932 | class_unregister(&nfc_class); |
| 933 | } |
| 934 | |
| 935 | subsys_initcall(nfc_init); |
| 936 | module_exit(nfc_exit); |
| 937 | |
| 938 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 939 | MODULE_DESCRIPTION("NFC Core ver " VERSION); |
| 940 | MODULE_VERSION(VERSION); |
| 941 | MODULE_LICENSE("GPL"); |
Samuel Ortiz | 1155bb6 | 2012-06-12 00:35:50 +0200 | [diff] [blame] | 942 | MODULE_ALIAS_NETPROTO(PF_NFC); |
Samuel Ortiz | 5df16ca | 2012-06-12 16:54:16 +0200 | [diff] [blame] | 943 | MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME); |