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 | |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 98 | if (dev->polling || dev->activated_target_idx != NFC_TARGET_IDX_NONE) { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 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); |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 214 | if (!rc) |
| 215 | dev->activated_target_idx = target_index; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 216 | |
| 217 | error: |
| 218 | device_unlock(&dev->dev); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | int nfc_dep_link_down(struct nfc_dev *dev) |
| 223 | { |
| 224 | int rc = 0; |
| 225 | |
| 226 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
| 227 | |
| 228 | if (!dev->ops->dep_link_down) |
| 229 | return -EOPNOTSUPP; |
| 230 | |
| 231 | device_lock(&dev->dev); |
| 232 | |
| 233 | if (!device_is_registered(&dev->dev)) { |
| 234 | rc = -ENODEV; |
| 235 | goto error; |
| 236 | } |
| 237 | |
| 238 | if (dev->dep_link_up == false) { |
| 239 | rc = -EALREADY; |
| 240 | goto error; |
| 241 | } |
| 242 | |
| 243 | if (dev->dep_rf_mode == NFC_RF_TARGET) { |
| 244 | rc = -EOPNOTSUPP; |
| 245 | goto error; |
| 246 | } |
| 247 | |
| 248 | rc = dev->ops->dep_link_down(dev); |
| 249 | if (!rc) { |
| 250 | dev->dep_link_up = false; |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 251 | dev->activated_target_idx = NFC_TARGET_IDX_NONE; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 252 | nfc_llcp_mac_is_down(dev); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 253 | nfc_genl_dep_link_down_event(dev); |
| 254 | } |
| 255 | |
| 256 | error: |
| 257 | device_unlock(&dev->dev); |
| 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | 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] | 262 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 263 | { |
| 264 | dev->dep_link_up = true; |
| 265 | dev->dep_rf_mode = rf_mode; |
| 266 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 267 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); |
| 268 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 269 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); |
| 270 | } |
| 271 | EXPORT_SYMBOL(nfc_dep_link_is_up); |
| 272 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 273 | /** |
| 274 | * nfc_activate_target - prepare the target for data exchange |
| 275 | * |
| 276 | * @dev: The nfc device that found the target |
| 277 | * @target_idx: index of the target that must be activated |
| 278 | * @protocol: nfc protocol that will be used for data exchange |
| 279 | */ |
| 280 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) |
| 281 | { |
| 282 | int rc; |
| 283 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 284 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", |
| 285 | dev_name(&dev->dev), target_idx, protocol); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 286 | |
| 287 | device_lock(&dev->dev); |
| 288 | |
| 289 | if (!device_is_registered(&dev->dev)) { |
| 290 | rc = -ENODEV; |
| 291 | goto error; |
| 292 | } |
| 293 | |
| 294 | rc = dev->ops->activate_target(dev, target_idx, protocol); |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 295 | if (!rc) |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 296 | dev->activated_target_idx = target_idx; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 297 | |
| 298 | error: |
| 299 | device_unlock(&dev->dev); |
| 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * nfc_deactivate_target - deactivate a nfc target |
| 305 | * |
| 306 | * @dev: The nfc device that found the target |
| 307 | * @target_idx: index of the target that must be deactivated |
| 308 | */ |
| 309 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) |
| 310 | { |
| 311 | int rc = 0; |
| 312 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 313 | pr_debug("dev_name=%s target_idx=%u\n", |
| 314 | dev_name(&dev->dev), target_idx); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 315 | |
| 316 | device_lock(&dev->dev); |
| 317 | |
| 318 | if (!device_is_registered(&dev->dev)) { |
| 319 | rc = -ENODEV; |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | dev->ops->deactivate_target(dev, target_idx); |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 324 | dev->activated_target_idx = NFC_TARGET_IDX_NONE; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 325 | |
| 326 | error: |
| 327 | device_unlock(&dev->dev); |
| 328 | return rc; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * nfc_data_exchange - transceive data |
| 333 | * |
| 334 | * @dev: The nfc device that found the target |
| 335 | * @target_idx: index of the target |
| 336 | * @skb: data to be sent |
| 337 | * @cb: callback called when the response is received |
| 338 | * @cb_context: parameter for the callback function |
| 339 | * |
| 340 | * The user must wait for the callback before calling this function again. |
| 341 | */ |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 342 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, |
| 343 | data_exchange_cb_t cb, void *cb_context) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 344 | { |
| 345 | int rc; |
| 346 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 347 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", |
| 348 | dev_name(&dev->dev), target_idx, skb->len); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 349 | |
| 350 | device_lock(&dev->dev); |
| 351 | |
| 352 | if (!device_is_registered(&dev->dev)) { |
| 353 | rc = -ENODEV; |
| 354 | kfree_skb(skb); |
| 355 | goto error; |
| 356 | } |
| 357 | |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 358 | if (dev->activated_target_idx == NFC_TARGET_IDX_NONE) { |
| 359 | rc = -ENOTCONN; |
| 360 | kfree_skb(skb); |
| 361 | goto error; |
| 362 | } |
| 363 | |
| 364 | if (target_idx != dev->activated_target_idx) { |
| 365 | rc = -EADDRNOTAVAIL; |
| 366 | kfree_skb(skb); |
| 367 | goto error; |
| 368 | } |
| 369 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 370 | rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context); |
| 371 | |
| 372 | error: |
| 373 | device_unlock(&dev->dev); |
| 374 | return rc; |
| 375 | } |
| 376 | |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 377 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
| 378 | { |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 379 | 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] | 380 | |
| 381 | if (gb_len > NFC_MAX_GT_LEN) |
| 382 | return -EINVAL; |
| 383 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 384 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); |
Samuel Ortiz | 541d920 | 2011-12-14 16:43:10 +0100 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); |
| 387 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 388 | /** |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 389 | * nfc_alloc_send_skb - allocate a skb for data exchange responses |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 390 | * |
| 391 | * @size: size to allocate |
| 392 | * @gfp: gfp flags |
| 393 | */ |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 394 | 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] | 395 | unsigned int flags, unsigned int size, |
| 396 | unsigned int *err) |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 397 | { |
| 398 | struct sk_buff *skb; |
| 399 | unsigned int total_size; |
| 400 | |
| 401 | total_size = size + |
| 402 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 403 | |
| 404 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); |
| 405 | if (skb) |
| 406 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 407 | |
| 408 | return skb; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses |
| 413 | * |
| 414 | * @size: size to allocate |
| 415 | * @gfp: gfp flags |
| 416 | */ |
| 417 | 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] | 418 | { |
| 419 | struct sk_buff *skb; |
| 420 | unsigned int total_size; |
| 421 | |
| 422 | total_size = size + 1; |
| 423 | skb = alloc_skb(total_size, gfp); |
| 424 | |
| 425 | if (skb) |
| 426 | skb_reserve(skb, 1); |
| 427 | |
| 428 | return skb; |
| 429 | } |
Samuel Ortiz | 7c7cd3b | 2011-12-14 16:43:06 +0100 | [diff] [blame] | 430 | EXPORT_SYMBOL(nfc_alloc_recv_skb); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 431 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 432 | /** |
| 433 | * nfc_targets_found - inform that targets were found |
| 434 | * |
| 435 | * @dev: The nfc device that found the targets |
| 436 | * @targets: array of nfc targets found |
| 437 | * @ntargets: targets array size |
| 438 | * |
| 439 | * The device driver must call this function when one or many nfc targets |
| 440 | * are found. After calling this function, the device driver must stop |
| 441 | * polling for targets. |
| 442 | */ |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 443 | int nfc_targets_found(struct nfc_dev *dev, |
| 444 | struct nfc_target *targets, int n_targets) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 445 | { |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 446 | int i; |
| 447 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 448 | 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] | 449 | |
| 450 | dev->polling = false; |
| 451 | |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 452 | for (i = 0; i < n_targets; i++) |
Eric Lapuyade | 01ae0ee | 2012-04-10 19:43:10 +0200 | [diff] [blame] | 453 | targets[i].idx = dev->target_next_idx++; |
Samuel Ortiz | c4fbb65 | 2012-04-10 19:43:09 +0200 | [diff] [blame] | 454 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 455 | spin_lock_bh(&dev->targets_lock); |
| 456 | |
| 457 | dev->targets_generation++; |
| 458 | |
| 459 | kfree(dev->targets); |
| 460 | dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target), |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 461 | GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 462 | |
| 463 | if (!dev->targets) { |
| 464 | dev->n_targets = 0; |
| 465 | spin_unlock_bh(&dev->targets_lock); |
| 466 | return -ENOMEM; |
| 467 | } |
| 468 | |
| 469 | dev->n_targets = n_targets; |
| 470 | spin_unlock_bh(&dev->targets_lock); |
| 471 | |
| 472 | nfc_genl_targets_found(dev); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | EXPORT_SYMBOL(nfc_targets_found); |
| 477 | |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 478 | int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 479 | { |
| 480 | struct nfc_target *tg; |
| 481 | int i; |
| 482 | |
| 483 | pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); |
| 484 | |
| 485 | spin_lock_bh(&dev->targets_lock); |
| 486 | |
| 487 | for (i = 0; i < dev->n_targets; i++) { |
| 488 | tg = &dev->targets[i]; |
| 489 | if (tg->idx == target_idx) |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | if (i == dev->n_targets) { |
| 494 | spin_unlock_bh(&dev->targets_lock); |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | dev->targets_generation++; |
| 499 | dev->n_targets--; |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 500 | dev->activated_target_idx = NFC_TARGET_IDX_NONE; |
Eric Lapuyade | e1da0ef | 2012-04-10 19:43:05 +0200 | [diff] [blame] | 501 | |
| 502 | if (dev->n_targets) { |
| 503 | memcpy(&dev->targets[i], &dev->targets[i + 1], |
| 504 | (dev->n_targets - i) * sizeof(struct nfc_target)); |
| 505 | } else { |
| 506 | kfree(dev->targets); |
| 507 | dev->targets = NULL; |
| 508 | } |
| 509 | |
| 510 | spin_unlock_bh(&dev->targets_lock); |
| 511 | |
| 512 | nfc_genl_target_lost(dev, target_idx); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | EXPORT_SYMBOL(nfc_target_lost); |
| 517 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 518 | static void nfc_release(struct device *d) |
| 519 | { |
| 520 | struct nfc_dev *dev = to_nfc_dev(d); |
| 521 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 522 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 523 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 524 | nfc_genl_data_exit(&dev->genl_data); |
| 525 | kfree(dev->targets); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 526 | kfree(dev); |
| 527 | } |
| 528 | |
| 529 | struct class nfc_class = { |
| 530 | .name = "nfc", |
| 531 | .dev_release = nfc_release, |
| 532 | }; |
| 533 | EXPORT_SYMBOL(nfc_class); |
| 534 | |
| 535 | static int match_idx(struct device *d, void *data) |
| 536 | { |
| 537 | struct nfc_dev *dev = to_nfc_dev(d); |
| 538 | unsigned *idx = data; |
| 539 | |
| 540 | return dev->idx == *idx; |
| 541 | } |
| 542 | |
| 543 | struct nfc_dev *nfc_get_device(unsigned idx) |
| 544 | { |
| 545 | struct device *d; |
| 546 | |
| 547 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); |
| 548 | if (!d) |
| 549 | return NULL; |
| 550 | |
| 551 | return to_nfc_dev(d); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * nfc_allocate_device - allocate a new nfc device |
| 556 | * |
| 557 | * @ops: device operations |
| 558 | * @supported_protocols: NFC protocols supported by the device |
| 559 | */ |
| 560 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 561 | u32 supported_protocols, |
| 562 | int tx_headroom, int tx_tailroom) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 563 | { |
| 564 | static atomic_t dev_no = ATOMIC_INIT(0); |
| 565 | struct nfc_dev *dev; |
| 566 | |
| 567 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 568 | !ops->deactivate_target || !ops->data_exchange) |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 569 | return NULL; |
| 570 | |
| 571 | if (!supported_protocols) |
| 572 | return NULL; |
| 573 | |
| 574 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); |
| 575 | if (!dev) |
| 576 | return NULL; |
| 577 | |
| 578 | dev->dev.class = &nfc_class; |
| 579 | dev->idx = atomic_inc_return(&dev_no) - 1; |
| 580 | dev_set_name(&dev->dev, "nfc%d", dev->idx); |
| 581 | device_initialize(&dev->dev); |
| 582 | |
| 583 | dev->ops = ops; |
| 584 | dev->supported_protocols = supported_protocols; |
Samuel Ortiz | e875304 | 2011-08-19 15:47:11 +0200 | [diff] [blame] | 585 | dev->tx_headroom = tx_headroom; |
| 586 | dev->tx_tailroom = tx_tailroom; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 587 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 588 | spin_lock_init(&dev->targets_lock); |
| 589 | nfc_genl_data_init(&dev->genl_data); |
| 590 | |
| 591 | /* first generation must not be 0 */ |
| 592 | dev->targets_generation = 1; |
| 593 | |
Eric Lapuyade | 144612c | 2012-04-10 19:43:11 +0200 | [diff] [blame^] | 594 | dev->activated_target_idx = NFC_TARGET_IDX_NONE; |
| 595 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 596 | return dev; |
| 597 | } |
| 598 | EXPORT_SYMBOL(nfc_allocate_device); |
| 599 | |
| 600 | /** |
| 601 | * nfc_register_device - register a nfc device in the nfc subsystem |
| 602 | * |
| 603 | * @dev: The nfc device to register |
| 604 | */ |
| 605 | int nfc_register_device(struct nfc_dev *dev) |
| 606 | { |
| 607 | int rc; |
| 608 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 609 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 610 | |
| 611 | mutex_lock(&nfc_devlist_mutex); |
| 612 | nfc_devlist_generation++; |
| 613 | rc = device_add(&dev->dev); |
| 614 | mutex_unlock(&nfc_devlist_mutex); |
| 615 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 616 | if (rc < 0) |
| 617 | return rc; |
| 618 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 619 | rc = nfc_llcp_register_device(dev); |
| 620 | if (rc) |
| 621 | pr_err("Could not register llcp device\n"); |
| 622 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 623 | rc = nfc_genl_device_added(dev); |
| 624 | if (rc) |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 625 | pr_debug("The userspace won't be notified that the device %s was added\n", |
| 626 | dev_name(&dev->dev)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 627 | |
| 628 | return 0; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 629 | } |
| 630 | EXPORT_SYMBOL(nfc_register_device); |
| 631 | |
| 632 | /** |
| 633 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem |
| 634 | * |
| 635 | * @dev: The nfc device to unregister |
| 636 | */ |
| 637 | void nfc_unregister_device(struct nfc_dev *dev) |
| 638 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 639 | int rc; |
| 640 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 641 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 642 | |
| 643 | mutex_lock(&nfc_devlist_mutex); |
| 644 | nfc_devlist_generation++; |
| 645 | |
| 646 | /* lock to avoid unregistering a device while an operation |
| 647 | is in progress */ |
| 648 | device_lock(&dev->dev); |
| 649 | device_del(&dev->dev); |
| 650 | device_unlock(&dev->dev); |
| 651 | |
| 652 | mutex_unlock(&nfc_devlist_mutex); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 653 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 654 | nfc_llcp_unregister_device(dev); |
| 655 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 656 | rc = nfc_genl_device_removed(dev); |
| 657 | if (rc) |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 658 | pr_debug("The userspace won't be notified that the device %s was removed\n", |
| 659 | dev_name(&dev->dev)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 660 | |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 661 | } |
| 662 | EXPORT_SYMBOL(nfc_unregister_device); |
| 663 | |
| 664 | static int __init nfc_init(void) |
| 665 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 666 | int rc; |
| 667 | |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 668 | pr_info("NFC Core ver %s\n", VERSION); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 669 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 670 | rc = class_register(&nfc_class); |
| 671 | if (rc) |
| 672 | return rc; |
| 673 | |
| 674 | rc = nfc_genl_init(); |
| 675 | if (rc) |
| 676 | goto err_genl; |
| 677 | |
| 678 | /* the first generation must not be 0 */ |
| 679 | nfc_devlist_generation = 1; |
| 680 | |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 681 | rc = rawsock_init(); |
| 682 | if (rc) |
| 683 | goto err_rawsock; |
| 684 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 685 | rc = nfc_llcp_init(); |
| 686 | if (rc) |
| 687 | goto err_llcp_sock; |
| 688 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 689 | rc = af_nfc_init(); |
| 690 | if (rc) |
| 691 | goto err_af_nfc; |
| 692 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 693 | return 0; |
| 694 | |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 695 | err_af_nfc: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 696 | nfc_llcp_exit(); |
| 697 | err_llcp_sock: |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 698 | rawsock_exit(); |
| 699 | err_rawsock: |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 700 | nfc_genl_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 701 | err_genl: |
| 702 | class_unregister(&nfc_class); |
| 703 | return rc; |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static void __exit nfc_exit(void) |
| 707 | { |
Aloisio Almeida Jr | c7fe3b5 | 2011-07-01 19:31:35 -0300 | [diff] [blame] | 708 | af_nfc_exit(); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 709 | nfc_llcp_exit(); |
Lauro Ramos Venancio | 23b7869 | 2011-07-01 19:31:36 -0300 | [diff] [blame] | 710 | rawsock_exit(); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 711 | nfc_genl_exit(); |
Lauro Ramos Venancio | 3e256b8 | 2011-07-01 19:31:33 -0300 | [diff] [blame] | 712 | class_unregister(&nfc_class); |
| 713 | } |
| 714 | |
| 715 | subsys_initcall(nfc_init); |
| 716 | module_exit(nfc_exit); |
| 717 | |
| 718 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); |
| 719 | MODULE_DESCRIPTION("NFC Core ver " VERSION); |
| 720 | MODULE_VERSION(VERSION); |
| 721 | MODULE_LICENSE("GPL"); |