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