blob: c571ca9a960cfe29b54d5bcac9d8fe524fb33cdd [file] [log] [blame]
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001/*
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 Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080025
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030026#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/slab.h>
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +010030#include <linux/nfc.h>
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030031
Samuel Ortiz5df16ca2012-06-12 16:54:16 +020032#include <net/genetlink.h>
33
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030034#include "nfc.h"
35
36#define VERSION "0.1"
37
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +020038#define NFC_CHECK_PRES_FREQ_MS 2000
39
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030040int nfc_devlist_generation;
41DEFINE_MUTEX(nfc_devlist_mutex);
42
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +020043/* NFC device ID bitmap */
44static DEFINE_IDA(nfc_index_ida);
45
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030046/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030047 * nfc_dev_up - turn on the NFC device
48 *
49 * @dev: The nfc device to be turned on
50 *
51 * The device remains up until the nfc_dev_down function is called.
52 */
53int nfc_dev_up(struct nfc_dev *dev)
54{
55 int rc = 0;
56
Joe Perches20c239c2011-11-29 11:37:33 -080057 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030058
59 device_lock(&dev->dev);
60
61 if (!device_is_registered(&dev->dev)) {
62 rc = -ENODEV;
63 goto error;
64 }
65
66 if (dev->dev_up) {
67 rc = -EALREADY;
68 goto error;
69 }
70
71 if (dev->ops->dev_up)
72 rc = dev->ops->dev_up(dev);
73
74 if (!rc)
75 dev->dev_up = true;
76
77error:
78 device_unlock(&dev->dev);
79 return rc;
80}
81
82/**
83 * nfc_dev_down - turn off the NFC device
84 *
85 * @dev: The nfc device to be turned off
86 */
87int nfc_dev_down(struct nfc_dev *dev)
88{
89 int rc = 0;
90
Joe Perches20c239c2011-11-29 11:37:33 -080091 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030092
93 device_lock(&dev->dev);
94
95 if (!device_is_registered(&dev->dev)) {
96 rc = -ENODEV;
97 goto error;
98 }
99
100 if (!dev->dev_up) {
101 rc = -EALREADY;
102 goto error;
103 }
104
Eric Lapuyade90099432012-05-07 12:31:13 +0200105 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300106 rc = -EBUSY;
107 goto error;
108 }
109
110 if (dev->ops->dev_down)
111 dev->ops->dev_down(dev);
112
113 dev->dev_up = false;
114
115error:
116 device_unlock(&dev->dev);
117 return rc;
118}
119
120/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300121 * nfc_start_poll - start polling for nfc targets
122 *
123 * @dev: The nfc device that must start polling
124 * @protocols: bitset of nfc protocols that must be used for polling
125 *
126 * The device remains polling for targets until a target is found or
127 * the nfc_stop_poll function is called.
128 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200129int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300130{
131 int rc;
132
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200133 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
134 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300135
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200136 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300137 return -EINVAL;
138
139 device_lock(&dev->dev);
140
141 if (!device_is_registered(&dev->dev)) {
142 rc = -ENODEV;
143 goto error;
144 }
145
Samuel Ortiz7757dc82013-04-10 12:25:30 +0200146 if (!dev->dev_up) {
147 rc = -ENODEV;
148 goto error;
149 }
150
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300151 if (dev->polling) {
152 rc = -EBUSY;
153 goto error;
154 }
155
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200156 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200157 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300158 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200159 dev->rf_mode = NFC_RF_NONE;
160 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300161
162error:
163 device_unlock(&dev->dev);
164 return rc;
165}
166
167/**
168 * nfc_stop_poll - stop polling for nfc targets
169 *
170 * @dev: The nfc device that must stop polling
171 */
172int nfc_stop_poll(struct nfc_dev *dev)
173{
174 int rc = 0;
175
Joe Perches20c239c2011-11-29 11:37:33 -0800176 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300177
178 device_lock(&dev->dev);
179
180 if (!device_is_registered(&dev->dev)) {
181 rc = -ENODEV;
182 goto error;
183 }
184
185 if (!dev->polling) {
186 rc = -EINVAL;
187 goto error;
188 }
189
190 dev->ops->stop_poll(dev);
191 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200192 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300193
194error:
195 device_unlock(&dev->dev);
196 return rc;
197}
198
Eric Lapuyade90099432012-05-07 12:31:13 +0200199static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
200{
201 int i;
202
203 if (dev->n_targets == 0)
204 return NULL;
205
Szymon Janc0f450772012-10-17 15:23:39 +0200206 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200207 if (dev->targets[i].idx == target_idx)
208 return &dev->targets[i];
209 }
210
211 return NULL;
212}
213
Samuel Ortiz47807d32012-03-05 01:03:50 +0100214int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100215{
216 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100217 u8 *gb;
218 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200219 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100220
Samuel Ortiz47807d32012-03-05 01:03:50 +0100221 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100222
223 if (!dev->ops->dep_link_up)
224 return -EOPNOTSUPP;
225
226 device_lock(&dev->dev);
227
228 if (!device_is_registered(&dev->dev)) {
229 rc = -ENODEV;
230 goto error;
231 }
232
233 if (dev->dep_link_up == true) {
234 rc = -EALREADY;
235 goto error;
236 }
237
Samuel Ortiz47807d32012-03-05 01:03:50 +0100238 gb = nfc_llcp_general_bytes(dev, &gb_len);
239 if (gb_len > NFC_MAX_GT_LEN) {
240 rc = -EINVAL;
241 goto error;
242 }
243
Eric Lapuyade90099432012-05-07 12:31:13 +0200244 target = nfc_find_target(dev, target_index);
245 if (target == NULL) {
246 rc = -ENOTCONN;
247 goto error;
248 }
249
250 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200251 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200252 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200253 dev->rf_mode = NFC_RF_INITIATOR;
254 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100255
256error:
257 device_unlock(&dev->dev);
258 return rc;
259}
260
261int nfc_dep_link_down(struct nfc_dev *dev)
262{
263 int rc = 0;
264
265 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
266
267 if (!dev->ops->dep_link_down)
268 return -EOPNOTSUPP;
269
270 device_lock(&dev->dev);
271
272 if (!device_is_registered(&dev->dev)) {
273 rc = -ENODEV;
274 goto error;
275 }
276
277 if (dev->dep_link_up == false) {
278 rc = -EALREADY;
279 goto error;
280 }
281
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100282 rc = dev->ops->dep_link_down(dev);
283 if (!rc) {
284 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200285 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200286 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100287 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100288 nfc_genl_dep_link_down_event(dev);
289 }
290
291error:
292 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200293
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100294 return rc;
295}
296
297int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100298 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100299{
300 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100301
Samuel Ortizd6469602011-12-14 16:43:12 +0100302 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
303
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100304 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
305}
306EXPORT_SYMBOL(nfc_dep_link_is_up);
307
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300308/**
309 * nfc_activate_target - prepare the target for data exchange
310 *
311 * @dev: The nfc device that found the target
312 * @target_idx: index of the target that must be activated
313 * @protocol: nfc protocol that will be used for data exchange
314 */
315int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
316{
317 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200318 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300319
Joe Perches20c239c2011-11-29 11:37:33 -0800320 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
321 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300322
323 device_lock(&dev->dev);
324
325 if (!device_is_registered(&dev->dev)) {
326 rc = -ENODEV;
327 goto error;
328 }
329
Eric Lapuyade90099432012-05-07 12:31:13 +0200330 if (dev->active_target) {
331 rc = -EBUSY;
332 goto error;
333 }
334
335 target = nfc_find_target(dev, target_idx);
336 if (target == NULL) {
337 rc = -ENOTCONN;
338 goto error;
339 }
340
341 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200342 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200343 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200344 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300345
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100346 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200347 mod_timer(&dev->check_pres_timer, jiffies +
348 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
349 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300350
351error:
352 device_unlock(&dev->dev);
353 return rc;
354}
355
356/**
357 * nfc_deactivate_target - deactivate a nfc target
358 *
359 * @dev: The nfc device that found the target
360 * @target_idx: index of the target that must be deactivated
361 */
362int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
363{
364 int rc = 0;
365
Joe Perches20c239c2011-11-29 11:37:33 -0800366 pr_debug("dev_name=%s target_idx=%u\n",
367 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300368
369 device_lock(&dev->dev);
370
371 if (!device_is_registered(&dev->dev)) {
372 rc = -ENODEV;
373 goto error;
374 }
375
Eric Lapuyade90099432012-05-07 12:31:13 +0200376 if (dev->active_target == NULL) {
377 rc = -ENOTCONN;
378 goto error;
379 }
380
381 if (dev->active_target->idx != target_idx) {
382 rc = -ENOTCONN;
383 goto error;
384 }
385
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200386 if (dev->ops->check_presence)
387 del_timer_sync(&dev->check_pres_timer);
388
Eric Lapuyade90099432012-05-07 12:31:13 +0200389 dev->ops->deactivate_target(dev, dev->active_target);
390 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300391
392error:
393 device_unlock(&dev->dev);
394 return rc;
395}
396
397/**
398 * nfc_data_exchange - transceive data
399 *
400 * @dev: The nfc device that found the target
401 * @target_idx: index of the target
402 * @skb: data to be sent
403 * @cb: callback called when the response is received
404 * @cb_context: parameter for the callback function
405 *
406 * The user must wait for the callback before calling this function again.
407 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100408int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
409 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300410{
411 int rc;
412
Joe Perches20c239c2011-11-29 11:37:33 -0800413 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
414 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300415
416 device_lock(&dev->dev);
417
418 if (!device_is_registered(&dev->dev)) {
419 rc = -ENODEV;
420 kfree_skb(skb);
421 goto error;
422 }
423
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200424 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
425 if (dev->active_target->idx != target_idx) {
426 rc = -EADDRNOTAVAIL;
427 kfree_skb(skb);
428 goto error;
429 }
430
431 if (dev->ops->check_presence)
432 del_timer_sync(&dev->check_pres_timer);
433
434 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
435 cb_context);
436
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100437 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200438 mod_timer(&dev->check_pres_timer, jiffies +
439 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
440 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
441 rc = dev->ops->tm_send(dev, skb);
442 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200443 rc = -ENOTCONN;
444 kfree_skb(skb);
445 goto error;
446 }
447
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200448
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300449error:
450 device_unlock(&dev->dev);
451 return rc;
452}
453
Samuel Ortiz541d9202011-12-14 16:43:10 +0100454int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
455{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100456 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100457
458 if (gb_len > NFC_MAX_GT_LEN)
459 return -EINVAL;
460
Samuel Ortizd6469602011-12-14 16:43:12 +0100461 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100462}
463EXPORT_SYMBOL(nfc_set_remote_general_bytes);
464
Samuel Ortizab73b752012-04-10 12:51:52 +0200465u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
466{
467 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
468
469 return nfc_llcp_general_bytes(dev, gb_len);
470}
471EXPORT_SYMBOL(nfc_get_local_general_bytes);
472
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200473int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
474{
475 /* Only LLCP target mode for now */
476 if (dev->dep_link_up == false) {
477 kfree_skb(skb);
478 return -ENOLINK;
479 }
480
481 return nfc_llcp_data_received(dev, skb);
482}
483EXPORT_SYMBOL(nfc_tm_data_received);
484
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200485int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
486 u8 *gb, size_t gb_len)
487{
488 int rc;
489
490 device_lock(&dev->dev);
491
492 dev->polling = false;
493
494 if (gb != NULL) {
495 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
496 if (rc < 0)
497 goto out;
498 }
499
Samuel Ortizf212ad52012-05-31 00:02:26 +0200500 dev->rf_mode = NFC_RF_TARGET;
501
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200502 if (protocol == NFC_PROTO_NFC_DEP_MASK)
503 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
504
505 rc = nfc_genl_tm_activated(dev, protocol);
506
507out:
508 device_unlock(&dev->dev);
509
510 return rc;
511}
512EXPORT_SYMBOL(nfc_tm_activated);
513
514int nfc_tm_deactivated(struct nfc_dev *dev)
515{
516 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200517 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200518
519 return nfc_genl_tm_deactivated(dev);
520}
521EXPORT_SYMBOL(nfc_tm_deactivated);
522
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300523/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100524 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300525 *
526 * @size: size to allocate
527 * @gfp: gfp flags
528 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100529struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100530 unsigned int flags, unsigned int size,
531 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100532{
533 struct sk_buff *skb;
534 unsigned int total_size;
535
536 total_size = size +
537 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
538
539 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
540 if (skb)
541 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
542
543 return skb;
544}
545
546/**
547 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
548 *
549 * @size: size to allocate
550 * @gfp: gfp flags
551 */
552struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300553{
554 struct sk_buff *skb;
555 unsigned int total_size;
556
557 total_size = size + 1;
558 skb = alloc_skb(total_size, gfp);
559
560 if (skb)
561 skb_reserve(skb, 1);
562
563 return skb;
564}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100565EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300566
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300567/**
568 * nfc_targets_found - inform that targets were found
569 *
570 * @dev: The nfc device that found the targets
571 * @targets: array of nfc targets found
572 * @ntargets: targets array size
573 *
574 * The device driver must call this function when one or many nfc targets
575 * are found. After calling this function, the device driver must stop
576 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200577 * NOTE: This function can be called with targets=NULL and n_targets=0 to
578 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200579 * IMPORTANT: this function must not be called from an atomic context.
580 * In addition, it must also not be called from a context that would prevent
581 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300582 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100583int nfc_targets_found(struct nfc_dev *dev,
584 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300585{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200586 int i;
587
Joe Perches20c239c2011-11-29 11:37:33 -0800588 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300589
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200590 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200591 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200592
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200593 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300594
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200595 if (dev->polling == false) {
596 device_unlock(&dev->dev);
597 return 0;
598 }
599
600 dev->polling = false;
601
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300602 dev->targets_generation++;
603
604 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200605 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300606
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200607 if (targets) {
608 dev->targets = kmemdup(targets,
609 n_targets * sizeof(struct nfc_target),
610 GFP_ATOMIC);
611
612 if (!dev->targets) {
613 dev->n_targets = 0;
614 device_unlock(&dev->dev);
615 return -ENOMEM;
616 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300617 }
618
619 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200620 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300621
622 nfc_genl_targets_found(dev);
623
624 return 0;
625}
626EXPORT_SYMBOL(nfc_targets_found);
627
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200628/**
629 * nfc_target_lost - inform that an activated target went out of field
630 *
631 * @dev: The nfc device that had the activated target in field
632 * @target_idx: the nfc index of the target
633 *
634 * The device driver must call this function when the activated target
635 * goes out of the field.
636 * IMPORTANT: this function must not be called from an atomic context.
637 * In addition, it must also not be called from a context that would prevent
638 * the NFC Core to call other nfc ops entry point concurrently.
639 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200640int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
641{
642 struct nfc_target *tg;
643 int i;
644
645 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
646
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200647 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200648
649 for (i = 0; i < dev->n_targets; i++) {
650 tg = &dev->targets[i];
651 if (tg->idx == target_idx)
652 break;
653 }
654
655 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200656 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200657 return -EINVAL;
658 }
659
660 dev->targets_generation++;
661 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200662 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200663
664 if (dev->n_targets) {
665 memcpy(&dev->targets[i], &dev->targets[i + 1],
666 (dev->n_targets - i) * sizeof(struct nfc_target));
667 } else {
668 kfree(dev->targets);
669 dev->targets = NULL;
670 }
671
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200672 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200673
674 nfc_genl_target_lost(dev, target_idx);
675
676 return 0;
677}
678EXPORT_SYMBOL(nfc_target_lost);
679
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200680inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200681{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200682 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200683}
684EXPORT_SYMBOL(nfc_driver_failure);
685
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300686static void nfc_release(struct device *d)
687{
688 struct nfc_dev *dev = to_nfc_dev(d);
689
Joe Perches20c239c2011-11-29 11:37:33 -0800690 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300691
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300692 nfc_genl_data_exit(&dev->genl_data);
693 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300694 kfree(dev);
695}
696
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200697static void nfc_check_pres_work(struct work_struct *work)
698{
699 struct nfc_dev *dev = container_of(work, struct nfc_dev,
700 check_pres_work);
701 int rc;
702
703 device_lock(&dev->dev);
704
Eric Lapuyade90099432012-05-07 12:31:13 +0200705 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
706 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200707 if (rc == -EOPNOTSUPP)
708 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100709 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200710 u32 active_target_idx = dev->active_target->idx;
711 device_unlock(&dev->dev);
712 nfc_target_lost(dev, active_target_idx);
713 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200714 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100715
716 if (!dev->shutting_down)
717 mod_timer(&dev->check_pres_timer, jiffies +
718 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200719 }
720
Eric Lapuyade632c0162012-10-02 17:27:36 +0200721exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200722 device_unlock(&dev->dev);
723}
724
725static void nfc_check_pres_timeout(unsigned long data)
726{
727 struct nfc_dev *dev = (struct nfc_dev *)data;
728
Linus Torvalds916082b2012-10-02 16:01:31 -0700729 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200730}
731
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300732struct class nfc_class = {
733 .name = "nfc",
734 .dev_release = nfc_release,
735};
736EXPORT_SYMBOL(nfc_class);
737
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100738static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300739{
740 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100741 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300742
743 return dev->idx == *idx;
744}
745
Eric Dumazet95c96172012-04-15 05:58:06 +0000746struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300747{
748 struct device *d;
749
750 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
751 if (!d)
752 return NULL;
753
754 return to_nfc_dev(d);
755}
756
757/**
758 * nfc_allocate_device - allocate a new nfc device
759 *
760 * @ops: device operations
761 * @supported_protocols: NFC protocols supported by the device
762 */
763struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100764 u32 supported_protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100765 u32 supported_se,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100766 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300767{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300768 struct nfc_dev *dev;
769
770 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200771 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300772 return NULL;
773
774 if (!supported_protocols)
775 return NULL;
776
777 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
778 if (!dev)
779 return NULL;
780
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300781 dev->ops = ops;
782 dev->supported_protocols = supported_protocols;
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100783 dev->supported_se = supported_se;
784 dev->active_se = NFC_SE_NONE;
Samuel Ortize8753042011-08-19 15:47:11 +0200785 dev->tx_headroom = tx_headroom;
786 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300787
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300788 nfc_genl_data_init(&dev->genl_data);
789
Thierry Escande5bcf0992012-10-05 11:05:45 +0200790 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200791
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300792 /* first generation must not be 0 */
793 dev->targets_generation = 1;
794
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200795 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200796 init_timer(&dev->check_pres_timer);
797 dev->check_pres_timer.data = (unsigned long)dev;
798 dev->check_pres_timer.function = nfc_check_pres_timeout;
799
800 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200801 }
802
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300803 return dev;
804}
805EXPORT_SYMBOL(nfc_allocate_device);
806
807/**
808 * nfc_register_device - register a nfc device in the nfc subsystem
809 *
810 * @dev: The nfc device to register
811 */
812int nfc_register_device(struct nfc_dev *dev)
813{
814 int rc;
815
Joe Perches20c239c2011-11-29 11:37:33 -0800816 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300817
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200818 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
819 if (dev->idx < 0)
820 return dev->idx;
821
822 dev->dev.class = &nfc_class;
823 dev_set_name(&dev->dev, "nfc%d", dev->idx);
824 device_initialize(&dev->dev);
825
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300826 mutex_lock(&nfc_devlist_mutex);
827 nfc_devlist_generation++;
828 rc = device_add(&dev->dev);
829 mutex_unlock(&nfc_devlist_mutex);
830
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300831 if (rc < 0)
832 return rc;
833
Samuel Ortizd6469602011-12-14 16:43:12 +0100834 rc = nfc_llcp_register_device(dev);
835 if (rc)
836 pr_err("Could not register llcp device\n");
837
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300838 rc = nfc_genl_device_added(dev);
839 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800840 pr_debug("The userspace won't be notified that the device %s was added\n",
841 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300842
843 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300844}
845EXPORT_SYMBOL(nfc_register_device);
846
847/**
848 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
849 *
850 * @dev: The nfc device to unregister
851 */
852void nfc_unregister_device(struct nfc_dev *dev)
853{
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200854 int rc, id;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300855
Joe Perches20c239c2011-11-29 11:37:33 -0800856 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300857
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200858 id = dev->idx;
859
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100860 if (dev->ops->check_presence) {
861 device_lock(&dev->dev);
862 dev->shutting_down = true;
863 device_unlock(&dev->dev);
864 del_timer_sync(&dev->check_pres_timer);
865 cancel_work_sync(&dev->check_pres_work);
866 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100867
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300868 rc = nfc_genl_device_removed(dev);
869 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100870 pr_debug("The userspace won't be notified that the device %s "
871 "was removed\n", dev_name(&dev->dev));
872
873 nfc_llcp_unregister_device(dev);
874
875 mutex_lock(&nfc_devlist_mutex);
876 nfc_devlist_generation++;
877 device_del(&dev->dev);
878 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300879
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200880 ida_simple_remove(&nfc_index_ida, id);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300881}
882EXPORT_SYMBOL(nfc_unregister_device);
883
884static int __init nfc_init(void)
885{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300886 int rc;
887
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800888 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300889
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300890 rc = class_register(&nfc_class);
891 if (rc)
892 return rc;
893
894 rc = nfc_genl_init();
895 if (rc)
896 goto err_genl;
897
898 /* the first generation must not be 0 */
899 nfc_devlist_generation = 1;
900
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300901 rc = rawsock_init();
902 if (rc)
903 goto err_rawsock;
904
Samuel Ortizd6469602011-12-14 16:43:12 +0100905 rc = nfc_llcp_init();
906 if (rc)
907 goto err_llcp_sock;
908
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300909 rc = af_nfc_init();
910 if (rc)
911 goto err_af_nfc;
912
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300913 return 0;
914
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300915err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100916 nfc_llcp_exit();
917err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300918 rawsock_exit();
919err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300920 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300921err_genl:
922 class_unregister(&nfc_class);
923 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300924}
925
926static void __exit nfc_exit(void)
927{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300928 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100929 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300930 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300931 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300932 class_unregister(&nfc_class);
933}
934
935subsys_initcall(nfc_init);
936module_exit(nfc_exit);
937
938MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
939MODULE_DESCRIPTION("NFC Core ver " VERSION);
940MODULE_VERSION(VERSION);
941MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +0200942MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +0200943MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);