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