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