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 | |
| 36 | int nfc_devlist_generation; |
| 37 | DEFINE_MUTEX(nfc_devlist_mutex); |
| 38 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 39 | /** |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 40 | * nfc_dev_up - turn on the NFC device |
| 41 | * |
| 42 | * @dev: The nfc device to be turned on |
| 43 | * |
| 44 | * The device remains up until the nfc_dev_down function is called. |
| 45 | */ |
| 46 | int nfc_dev_up(struct nfc_dev *dev) |
| 47 | { |
| 48 | int rc = 0; |
| 49 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 50 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 51 | |
| 52 | device_lock(&dev->dev); |
| 53 | |
| 54 | if (!device_is_registered(&dev->dev)) { |
| 55 | rc = -ENODEV; |
| 56 | goto error; |
| 57 | } |
| 58 | |
| 59 | if (dev->dev_up) { |
| 60 | rc = -EALREADY; |
| 61 | goto error; |
| 62 | } |
| 63 | |
| 64 | if (dev->ops->dev_up) |
| 65 | rc = dev->ops->dev_up(dev); |
| 66 | |
| 67 | if (!rc) |
| 68 | dev->dev_up = true; |
| 69 | |
| 70 | error: |
| 71 | device_unlock(&dev->dev); |
| 72 | return rc; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * nfc_dev_down - turn off the NFC device |
| 77 | * |
| 78 | * @dev: The nfc device to be turned off |
| 79 | */ |
| 80 | int nfc_dev_down(struct nfc_dev *dev) |
| 81 | { |
| 82 | int rc = 0; |
| 83 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 84 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 85 | |
| 86 | device_lock(&dev->dev); |
| 87 | |
| 88 | if (!device_is_registered(&dev->dev)) { |
| 89 | rc = -ENODEV; |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | if (!dev->dev_up) { |
| 94 | rc = -EALREADY; |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | if (dev->polling || dev->remote_activated) { |
| 99 | rc = -EBUSY; |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | if (dev->ops->dev_down) |
| 104 | dev->ops->dev_down(dev); |
| 105 | |
| 106 | dev->dev_up = false; |
| 107 | |
| 108 | error: |
| 109 | device_unlock(&dev->dev); |
| 110 | return rc; |
| 111 | } |
| 112 | |
| 113 | /** |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 114 | * nfc_start_poll - start polling for nfc targets |
| 115 | * |
| 116 | * @dev: The nfc device that must start polling |
| 117 | * @protocols: bitset of nfc protocols that must be used for polling |
| 118 | * |
| 119 | * The device remains polling for targets until a target is found or |
| 120 | * the nfc_stop_poll function is called. |
| 121 | */ |
| 122 | int nfc_start_poll(struct nfc_dev *dev, u32 protocols) |
| 123 | { |
| 124 | int rc; |
| 125 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 126 | pr_debug("dev_name=%s protocols=0x%x\n", |
| 127 | dev_name(&dev->dev), protocols); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 128 | |
| 129 | if (!protocols) |
| 130 | return -EINVAL; |
| 131 | |
| 132 | device_lock(&dev->dev); |
| 133 | |
| 134 | if (!device_is_registered(&dev->dev)) { |
| 135 | rc = -ENODEV; |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | if (dev->polling) { |
| 140 | rc = -EBUSY; |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | rc = dev->ops->start_poll(dev, protocols); |
| 145 | if (!rc) |
| 146 | dev->polling = true; |
| 147 | |
| 148 | error: |
| 149 | device_unlock(&dev->dev); |
| 150 | return rc; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * nfc_stop_poll - stop polling for nfc targets |
| 155 | * |
| 156 | * @dev: The nfc device that must stop polling |
| 157 | */ |
| 158 | int nfc_stop_poll(struct nfc_dev *dev) |
| 159 | { |
| 160 | int rc = 0; |
| 161 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 162 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 163 | |
| 164 | device_lock(&dev->dev); |
| 165 | |
| 166 | if (!device_is_registered(&dev->dev)) { |
| 167 | rc = -ENODEV; |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | if (!dev->polling) { |
| 172 | rc = -EINVAL; |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | dev->ops->stop_poll(dev); |
| 177 | dev->polling = false; |
| 178 | |
| 179 | error: |
| 180 | device_unlock(&dev->dev); |
| 181 | return rc; |
| 182 | } |
| 183 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 184 | 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] | 185 | { |
| 186 | int rc = 0; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 187 | u8 *gb; |
| 188 | size_t gb_len; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 189 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 190 | 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] | 191 | |
| 192 | if (!dev->ops->dep_link_up) |
| 193 | return -EOPNOTSUPP; |
| 194 | |
| 195 | device_lock(&dev->dev); |
| 196 | |
| 197 | if (!device_is_registered(&dev->dev)) { |
| 198 | rc = -ENODEV; |
| 199 | goto error; |
| 200 | } |
| 201 | |
| 202 | if (dev->dep_link_up == true) { |
| 203 | rc = -EALREADY; |
| 204 | goto error; |
| 205 | } |
| 206 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 207 | gb = nfc_llcp_general_bytes(dev, &gb_len); |
| 208 | if (gb_len > NFC_MAX_GT_LEN) { |
| 209 | rc = -EINVAL; |
| 210 | goto error; |
| 211 | } |
| 212 | |
| 213 | rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 214 | |
| 215 | error: |
| 216 | device_unlock(&dev->dev); |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | int nfc_dep_link_down(struct nfc_dev *dev) |
| 221 | { |
| 222 | int rc = 0; |
| 223 | |
| 224 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
| 225 | |
| 226 | if (!dev->ops->dep_link_down) |
| 227 | return -EOPNOTSUPP; |
| 228 | |
| 229 | device_lock(&dev->dev); |
| 230 | |
| 231 | if (!device_is_registered(&dev->dev)) { |
| 232 | rc = -ENODEV; |
| 233 | goto error; |
| 234 | } |
| 235 | |
| 236 | if (dev->dep_link_up == false) { |
| 237 | rc = -EALREADY; |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | if (dev->dep_rf_mode == NFC_RF_TARGET) { |
| 242 | rc = -EOPNOTSUPP; |
| 243 | goto error; |
| 244 | } |
| 245 | |
| 246 | rc = dev->ops->dep_link_down(dev); |
| 247 | if (!rc) { |
| 248 | dev->dep_link_up = false; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 249 | nfc_llcp_mac_is_down(dev); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 250 | nfc_genl_dep_link_down_event(dev); |
| 251 | } |
| 252 | |
| 253 | error: |
| 254 | device_unlock(&dev->dev); |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, |
| 259 | u8 comm_mode, u8 rf_mode) |
| 260 | { |
| 261 | dev->dep_link_up = true; |
| 262 | dev->dep_rf_mode = rf_mode; |
| 263 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 264 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); |
| 265 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 266 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); |
| 267 | } |
| 268 | EXPORT_SYMBOL(nfc_dep_link_is_up); |
| 269 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 270 | /** |
| 271 | * nfc_activate_target - prepare the target for data exchange |
| 272 | * |
| 273 | * @dev: The nfc device that found the target |
| 274 | * @target_idx: index of the target that must be activated |
| 275 | * @protocol: nfc protocol that will be used for data exchange |
| 276 | */ |
| 277 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) |
| 278 | { |
| 279 | int rc; |
| 280 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 281 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", |
| 282 | dev_name(&dev->dev), target_idx, protocol); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 283 | |
| 284 | device_lock(&dev->dev); |
| 285 | |
| 286 | if (!device_is_registered(&dev->dev)) { |
| 287 | rc = -ENODEV; |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | rc = dev->ops->activate_target(dev, target_idx, protocol); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 292 | if (!rc) |
| 293 | dev->remote_activated = true; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 294 | |
| 295 | error: |
| 296 | device_unlock(&dev->dev); |
| 297 | return rc; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * nfc_deactivate_target - deactivate a nfc target |
| 302 | * |
| 303 | * @dev: The nfc device that found the target |
| 304 | * @target_idx: index of the target that must be deactivated |
| 305 | */ |
| 306 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) |
| 307 | { |
| 308 | int rc = 0; |
| 309 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 310 | pr_debug("dev_name=%s target_idx=%u\n", |
| 311 | dev_name(&dev->dev), target_idx); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 312 | |
| 313 | device_lock(&dev->dev); |
| 314 | |
| 315 | if (!device_is_registered(&dev->dev)) { |
| 316 | rc = -ENODEV; |
| 317 | goto error; |
| 318 | } |
| 319 | |
| 320 | dev->ops->deactivate_target(dev, target_idx); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 321 | dev->remote_activated = false; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 322 | |
| 323 | error: |
| 324 | device_unlock(&dev->dev); |
| 325 | return rc; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * nfc_data_exchange - transceive data |
| 330 | * |
| 331 | * @dev: The nfc device that found the target |
| 332 | * @target_idx: index of the target |
| 333 | * @skb: data to be sent |
| 334 | * @cb: callback called when the response is received |
| 335 | * @cb_context: parameter for the callback function |
| 336 | * |
| 337 | * The user must wait for the callback before calling this function again. |
| 338 | */ |
| 339 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, |
| 340 | struct sk_buff *skb, |
| 341 | data_exchange_cb_t cb, |
| 342 | void *cb_context) |
| 343 | { |
| 344 | int rc; |
| 345 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 346 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", |
| 347 | dev_name(&dev->dev), target_idx, skb->len); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 348 | |
| 349 | device_lock(&dev->dev); |
| 350 | |
| 351 | if (!device_is_registered(&dev->dev)) { |
| 352 | rc = -ENODEV; |
| 353 | kfree_skb(skb); |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context); |
| 358 | |
| 359 | error: |
| 360 | device_unlock(&dev->dev); |
| 361 | return rc; |
| 362 | } |
| 363 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 364 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 365 | { |
| 366 | pr_debug("dev_name=%s gb_len=%d\n", |
| 367 | dev_name(&dev->dev), gb_len); |
| 368 | |
| 369 | if (gb_len > NFC_MAX_GT_LEN) |
| 370 | return -EINVAL; |
| 371 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 372 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 373 | } |
| 374 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); |
| 375 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 376 | /** |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 377 | * nfc_alloc_send_skb - allocate a skb for data exchange responses |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 378 | * |
| 379 | * @size: size to allocate |
| 380 | * @gfp: gfp flags |
| 381 | */ |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 382 | struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, |
| 383 | unsigned int flags, unsigned int size, |
| 384 | unsigned int *err) |
| 385 | { |
| 386 | struct sk_buff *skb; |
| 387 | unsigned int total_size; |
| 388 | |
| 389 | total_size = size + |
| 390 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 391 | |
| 392 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); |
| 393 | if (skb) |
| 394 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 395 | |
| 396 | return skb; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses |
| 401 | * |
| 402 | * @size: size to allocate |
| 403 | * @gfp: gfp flags |
| 404 | */ |
| 405 | 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] | 406 | { |
| 407 | struct sk_buff *skb; |
| 408 | unsigned int total_size; |
| 409 | |
| 410 | total_size = size + 1; |
| 411 | skb = alloc_skb(total_size, gfp); |
| 412 | |
| 413 | if (skb) |
| 414 | skb_reserve(skb, 1); |
| 415 | |
| 416 | return skb; |
| 417 | } |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 418 | EXPORT_SYMBOL(nfc_alloc_recv_skb); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 419 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 420 | /** |
| 421 | * nfc_targets_found - inform that targets were found |
| 422 | * |
| 423 | * @dev: The nfc device that found the targets |
| 424 | * @targets: array of nfc targets found |
| 425 | * @ntargets: targets array size |
| 426 | * |
| 427 | * The device driver must call this function when one or many nfc targets |
| 428 | * are found. After calling this function, the device driver must stop |
| 429 | * polling for targets. |
| 430 | */ |
| 431 | int nfc_targets_found(struct nfc_dev *dev, struct nfc_target *targets, |
| 432 | int n_targets) |
| 433 | { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 434 | 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] | 435 | |
| 436 | dev->polling = false; |
| 437 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 438 | spin_lock_bh(&dev->targets_lock); |
| 439 | |
| 440 | dev->targets_generation++; |
| 441 | |
| 442 | kfree(dev->targets); |
| 443 | dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target), |
| 444 | GFP_ATOMIC); |
| 445 | |
| 446 | if (!dev->targets) { |
| 447 | dev->n_targets = 0; |
| 448 | spin_unlock_bh(&dev->targets_lock); |
| 449 | return -ENOMEM; |
| 450 | } |
| 451 | |
| 452 | dev->n_targets = n_targets; |
| 453 | spin_unlock_bh(&dev->targets_lock); |
| 454 | |
| 455 | nfc_genl_targets_found(dev); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | EXPORT_SYMBOL(nfc_targets_found); |
| 460 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 461 | static void nfc_release(struct device *d) |
| 462 | { |
| 463 | struct nfc_dev *dev = to_nfc_dev(d); |
| 464 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 465 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 466 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 467 | nfc_genl_data_exit(&dev->genl_data); |
| 468 | kfree(dev->targets); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 469 | kfree(dev); |
| 470 | } |
| 471 | |
| 472 | struct class nfc_class = { |
| 473 | .name = "nfc", |
| 474 | .dev_release = nfc_release, |
| 475 | }; |
| 476 | EXPORT_SYMBOL(nfc_class); |
| 477 | |
| 478 | static int match_idx(struct device *d, void *data) |
| 479 | { |
| 480 | struct nfc_dev *dev = to_nfc_dev(d); |
| 481 | unsigned *idx = data; |
| 482 | |
| 483 | return dev->idx == *idx; |
| 484 | } |
| 485 | |
| 486 | struct nfc_dev *nfc_get_device(unsigned idx) |
| 487 | { |
| 488 | struct device *d; |
| 489 | |
| 490 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); |
| 491 | if (!d) |
| 492 | return NULL; |
| 493 | |
| 494 | return to_nfc_dev(d); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * nfc_allocate_device - allocate a new nfc device |
| 499 | * |
| 500 | * @ops: device operations |
| 501 | * @supported_protocols: NFC protocols supported by the device |
| 502 | */ |
| 503 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 504 | u32 supported_protocols, |
| 505 | int tx_headroom, |
| 506 | int tx_tailroom) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 507 | { |
| 508 | static atomic_t dev_no = ATOMIC_INIT(0); |
| 509 | struct nfc_dev *dev; |
| 510 | |
| 511 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || |
| 512 | !ops->deactivate_target || !ops->data_exchange) |
| 513 | return NULL; |
| 514 | |
| 515 | if (!supported_protocols) |
| 516 | return NULL; |
| 517 | |
| 518 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); |
| 519 | if (!dev) |
| 520 | return NULL; |
| 521 | |
| 522 | dev->dev.class = &nfc_class; |
| 523 | dev->idx = atomic_inc_return(&dev_no) - 1; |
| 524 | dev_set_name(&dev->dev, "nfc%d", dev->idx); |
| 525 | device_initialize(&dev->dev); |
| 526 | |
| 527 | dev->ops = ops; |
| 528 | dev->supported_protocols = supported_protocols; |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 529 | dev->tx_headroom = tx_headroom; |
| 530 | dev->tx_tailroom = tx_tailroom; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 531 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 532 | spin_lock_init(&dev->targets_lock); |
| 533 | nfc_genl_data_init(&dev->genl_data); |
| 534 | |
| 535 | /* first generation must not be 0 */ |
| 536 | dev->targets_generation = 1; |
| 537 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 538 | return dev; |
| 539 | } |
| 540 | EXPORT_SYMBOL(nfc_allocate_device); |
| 541 | |
| 542 | /** |
| 543 | * nfc_register_device - register a nfc device in the nfc subsystem |
| 544 | * |
| 545 | * @dev: The nfc device to register |
| 546 | */ |
| 547 | int nfc_register_device(struct nfc_dev *dev) |
| 548 | { |
| 549 | int rc; |
| 550 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 551 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 552 | |
| 553 | mutex_lock(&nfc_devlist_mutex); |
| 554 | nfc_devlist_generation++; |
| 555 | rc = device_add(&dev->dev); |
| 556 | mutex_unlock(&nfc_devlist_mutex); |
| 557 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 558 | if (rc < 0) |
| 559 | return rc; |
| 560 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 561 | rc = nfc_llcp_register_device(dev); |
| 562 | if (rc) |
| 563 | pr_err("Could not register llcp device\n"); |
| 564 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 565 | rc = nfc_genl_device_added(dev); |
| 566 | if (rc) |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 567 | pr_debug("The userspace won't be notified that the device %s was added\n", |
| 568 | dev_name(&dev->dev)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 569 | |
| 570 | return 0; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 571 | } |
| 572 | EXPORT_SYMBOL(nfc_register_device); |
| 573 | |
| 574 | /** |
| 575 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem |
| 576 | * |
| 577 | * @dev: The nfc device to unregister |
| 578 | */ |
| 579 | void nfc_unregister_device(struct nfc_dev *dev) |
| 580 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 581 | int rc; |
| 582 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 583 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 584 | |
| 585 | mutex_lock(&nfc_devlist_mutex); |
| 586 | nfc_devlist_generation++; |
| 587 | |
| 588 | /* lock to avoid unregistering a device while an operation |
| 589 | is in progress */ |
| 590 | device_lock(&dev->dev); |
| 591 | device_del(&dev->dev); |
| 592 | device_unlock(&dev->dev); |
| 593 | |
| 594 | mutex_unlock(&nfc_devlist_mutex); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 595 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 596 | nfc_llcp_unregister_device(dev); |
| 597 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 598 | rc = nfc_genl_device_removed(dev); |
| 599 | if (rc) |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 600 | pr_debug("The userspace won't be notified that the device %s was removed\n", |
| 601 | dev_name(&dev->dev)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 602 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 603 | } |
| 604 | EXPORT_SYMBOL(nfc_unregister_device); |
| 605 | |
| 606 | static int __init nfc_init(void) |
| 607 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 608 | int rc; |
| 609 | |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 610 | pr_info("NFC Core ver %s\n", VERSION); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 611 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 612 | rc = class_register(&nfc_class); |
| 613 | if (rc) |
| 614 | return rc; |
| 615 | |
| 616 | rc = nfc_genl_init(); |
| 617 | if (rc) |
| 618 | goto err_genl; |
| 619 | |
| 620 | /* the first generation must not be 0 */ |
| 621 | nfc_devlist_generation = 1; |
| 622 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 623 | rc = rawsock_init(); |
| 624 | if (rc) |
| 625 | goto err_rawsock; |
| 626 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 627 | rc = nfc_llcp_init(); |
| 628 | if (rc) |
| 629 | goto err_llcp_sock; |
| 630 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 631 | rc = af_nfc_init(); |
| 632 | if (rc) |
| 633 | goto err_af_nfc; |
| 634 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 635 | return 0; |
| 636 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 637 | err_af_nfc: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 638 | nfc_llcp_exit(); |
| 639 | err_llcp_sock: |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 640 | rawsock_exit(); |
| 641 | err_rawsock: |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 642 | nfc_genl_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 643 | err_genl: |
| 644 | class_unregister(&nfc_class); |
| 645 | return rc; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static void __exit nfc_exit(void) |
| 649 | { |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 650 | af_nfc_exit(); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 651 | nfc_llcp_exit(); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 652 | rawsock_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 653 | nfc_genl_exit(); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 654 | class_unregister(&nfc_class); |
| 655 | } |
| 656 | |
| 657 | subsys_initcall(nfc_init); |
| 658 | module_exit(nfc_exit); |
| 659 | |
| 660 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 661 | MODULE_DESCRIPTION("NFC Core ver " VERSION); |
| 662 | MODULE_VERSION(VERSION); |
| 663 | MODULE_LICENSE("GPL"); |