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