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