blob: 7d7b4ee34015ba04c356b7a449537265aed72db8 [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
146 if (dev->polling) {
147 rc = -EBUSY;
148 goto error;
149 }
150
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200151 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200152 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300153 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200154 dev->rf_mode = NFC_RF_NONE;
155 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300156
157error:
158 device_unlock(&dev->dev);
159 return rc;
160}
161
162/**
163 * nfc_stop_poll - stop polling for nfc targets
164 *
165 * @dev: The nfc device that must stop polling
166 */
167int nfc_stop_poll(struct nfc_dev *dev)
168{
169 int rc = 0;
170
Joe Perches20c239c2011-11-29 11:37:33 -0800171 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300172
173 device_lock(&dev->dev);
174
175 if (!device_is_registered(&dev->dev)) {
176 rc = -ENODEV;
177 goto error;
178 }
179
180 if (!dev->polling) {
181 rc = -EINVAL;
182 goto error;
183 }
184
185 dev->ops->stop_poll(dev);
186 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200187 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300188
189error:
190 device_unlock(&dev->dev);
191 return rc;
192}
193
Eric Lapuyade90099432012-05-07 12:31:13 +0200194static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
195{
196 int i;
197
198 if (dev->n_targets == 0)
199 return NULL;
200
Szymon Janc0f450772012-10-17 15:23:39 +0200201 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200202 if (dev->targets[i].idx == target_idx)
203 return &dev->targets[i];
204 }
205
206 return NULL;
207}
208
Samuel Ortiz47807d32012-03-05 01:03:50 +0100209int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100210{
211 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100212 u8 *gb;
213 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200214 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100215
Samuel Ortiz47807d32012-03-05 01:03:50 +0100216 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100217
218 if (!dev->ops->dep_link_up)
219 return -EOPNOTSUPP;
220
221 device_lock(&dev->dev);
222
223 if (!device_is_registered(&dev->dev)) {
224 rc = -ENODEV;
225 goto error;
226 }
227
228 if (dev->dep_link_up == true) {
229 rc = -EALREADY;
230 goto error;
231 }
232
Samuel Ortiz47807d32012-03-05 01:03:50 +0100233 gb = nfc_llcp_general_bytes(dev, &gb_len);
234 if (gb_len > NFC_MAX_GT_LEN) {
235 rc = -EINVAL;
236 goto error;
237 }
238
Eric Lapuyade90099432012-05-07 12:31:13 +0200239 target = nfc_find_target(dev, target_index);
240 if (target == NULL) {
241 rc = -ENOTCONN;
242 goto error;
243 }
244
245 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200246 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200247 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200248 dev->rf_mode = NFC_RF_INITIATOR;
249 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100250
251error:
252 device_unlock(&dev->dev);
253 return rc;
254}
255
256int nfc_dep_link_down(struct nfc_dev *dev)
257{
258 int rc = 0;
259
260 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
261
262 if (!dev->ops->dep_link_down)
263 return -EOPNOTSUPP;
264
265 device_lock(&dev->dev);
266
267 if (!device_is_registered(&dev->dev)) {
268 rc = -ENODEV;
269 goto error;
270 }
271
272 if (dev->dep_link_up == false) {
273 rc = -EALREADY;
274 goto error;
275 }
276
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100277 rc = dev->ops->dep_link_down(dev);
278 if (!rc) {
279 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200280 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200281 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100282 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100283 nfc_genl_dep_link_down_event(dev);
284 }
285
286error:
287 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200288
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100289 return rc;
290}
291
292int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100293 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100294{
295 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100296
Samuel Ortizd6469602011-12-14 16:43:12 +0100297 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
298
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100299 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
300}
301EXPORT_SYMBOL(nfc_dep_link_is_up);
302
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300303/**
304 * nfc_activate_target - prepare the target for data exchange
305 *
306 * @dev: The nfc device that found the target
307 * @target_idx: index of the target that must be activated
308 * @protocol: nfc protocol that will be used for data exchange
309 */
310int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
311{
312 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200313 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300314
Joe Perches20c239c2011-11-29 11:37:33 -0800315 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
316 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300317
318 device_lock(&dev->dev);
319
320 if (!device_is_registered(&dev->dev)) {
321 rc = -ENODEV;
322 goto error;
323 }
324
Eric Lapuyade90099432012-05-07 12:31:13 +0200325 if (dev->active_target) {
326 rc = -EBUSY;
327 goto error;
328 }
329
330 target = nfc_find_target(dev, target_idx);
331 if (target == NULL) {
332 rc = -ENOTCONN;
333 goto error;
334 }
335
336 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200337 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200338 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200339 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300340
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100341 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200342 mod_timer(&dev->check_pres_timer, jiffies +
343 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
344 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300345
346error:
347 device_unlock(&dev->dev);
348 return rc;
349}
350
351/**
352 * nfc_deactivate_target - deactivate a nfc target
353 *
354 * @dev: The nfc device that found the target
355 * @target_idx: index of the target that must be deactivated
356 */
357int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
358{
359 int rc = 0;
360
Joe Perches20c239c2011-11-29 11:37:33 -0800361 pr_debug("dev_name=%s target_idx=%u\n",
362 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300363
364 device_lock(&dev->dev);
365
366 if (!device_is_registered(&dev->dev)) {
367 rc = -ENODEV;
368 goto error;
369 }
370
Eric Lapuyade90099432012-05-07 12:31:13 +0200371 if (dev->active_target == NULL) {
372 rc = -ENOTCONN;
373 goto error;
374 }
375
376 if (dev->active_target->idx != target_idx) {
377 rc = -ENOTCONN;
378 goto error;
379 }
380
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200381 if (dev->ops->check_presence)
382 del_timer_sync(&dev->check_pres_timer);
383
Eric Lapuyade90099432012-05-07 12:31:13 +0200384 dev->ops->deactivate_target(dev, dev->active_target);
385 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300386
387error:
388 device_unlock(&dev->dev);
389 return rc;
390}
391
392/**
393 * nfc_data_exchange - transceive data
394 *
395 * @dev: The nfc device that found the target
396 * @target_idx: index of the target
397 * @skb: data to be sent
398 * @cb: callback called when the response is received
399 * @cb_context: parameter for the callback function
400 *
401 * The user must wait for the callback before calling this function again.
402 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100403int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
404 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300405{
406 int rc;
407
Joe Perches20c239c2011-11-29 11:37:33 -0800408 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
409 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300410
411 device_lock(&dev->dev);
412
413 if (!device_is_registered(&dev->dev)) {
414 rc = -ENODEV;
415 kfree_skb(skb);
416 goto error;
417 }
418
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200419 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
420 if (dev->active_target->idx != target_idx) {
421 rc = -EADDRNOTAVAIL;
422 kfree_skb(skb);
423 goto error;
424 }
425
426 if (dev->ops->check_presence)
427 del_timer_sync(&dev->check_pres_timer);
428
429 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
430 cb_context);
431
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100432 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200433 mod_timer(&dev->check_pres_timer, jiffies +
434 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
435 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
436 rc = dev->ops->tm_send(dev, skb);
437 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200438 rc = -ENOTCONN;
439 kfree_skb(skb);
440 goto error;
441 }
442
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200443
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300444error:
445 device_unlock(&dev->dev);
446 return rc;
447}
448
Samuel Ortiz541d9202011-12-14 16:43:10 +0100449int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
450{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100451 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100452
453 if (gb_len > NFC_MAX_GT_LEN)
454 return -EINVAL;
455
Samuel Ortizd6469602011-12-14 16:43:12 +0100456 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100457}
458EXPORT_SYMBOL(nfc_set_remote_general_bytes);
459
Samuel Ortizab73b752012-04-10 12:51:52 +0200460u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
461{
462 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
463
464 return nfc_llcp_general_bytes(dev, gb_len);
465}
466EXPORT_SYMBOL(nfc_get_local_general_bytes);
467
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200468int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
469{
470 /* Only LLCP target mode for now */
471 if (dev->dep_link_up == false) {
472 kfree_skb(skb);
473 return -ENOLINK;
474 }
475
476 return nfc_llcp_data_received(dev, skb);
477}
478EXPORT_SYMBOL(nfc_tm_data_received);
479
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200480int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
481 u8 *gb, size_t gb_len)
482{
483 int rc;
484
485 device_lock(&dev->dev);
486
487 dev->polling = false;
488
489 if (gb != NULL) {
490 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
491 if (rc < 0)
492 goto out;
493 }
494
Samuel Ortizf212ad52012-05-31 00:02:26 +0200495 dev->rf_mode = NFC_RF_TARGET;
496
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200497 if (protocol == NFC_PROTO_NFC_DEP_MASK)
498 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
499
500 rc = nfc_genl_tm_activated(dev, protocol);
501
502out:
503 device_unlock(&dev->dev);
504
505 return rc;
506}
507EXPORT_SYMBOL(nfc_tm_activated);
508
509int nfc_tm_deactivated(struct nfc_dev *dev)
510{
511 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200512 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200513
514 return nfc_genl_tm_deactivated(dev);
515}
516EXPORT_SYMBOL(nfc_tm_deactivated);
517
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300518/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100519 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300520 *
521 * @size: size to allocate
522 * @gfp: gfp flags
523 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100524struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100525 unsigned int flags, unsigned int size,
526 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100527{
528 struct sk_buff *skb;
529 unsigned int total_size;
530
531 total_size = size +
532 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
533
534 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
535 if (skb)
536 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
537
538 return skb;
539}
540
541/**
542 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
543 *
544 * @size: size to allocate
545 * @gfp: gfp flags
546 */
547struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300548{
549 struct sk_buff *skb;
550 unsigned int total_size;
551
552 total_size = size + 1;
553 skb = alloc_skb(total_size, gfp);
554
555 if (skb)
556 skb_reserve(skb, 1);
557
558 return skb;
559}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100560EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300561
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300562/**
563 * nfc_targets_found - inform that targets were found
564 *
565 * @dev: The nfc device that found the targets
566 * @targets: array of nfc targets found
567 * @ntargets: targets array size
568 *
569 * The device driver must call this function when one or many nfc targets
570 * are found. After calling this function, the device driver must stop
571 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200572 * NOTE: This function can be called with targets=NULL and n_targets=0 to
573 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200574 * IMPORTANT: this function must not be called from an atomic context.
575 * In addition, it must also not be called from a context that would prevent
576 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300577 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100578int nfc_targets_found(struct nfc_dev *dev,
579 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300580{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200581 int i;
582
Joe Perches20c239c2011-11-29 11:37:33 -0800583 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300584
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200585 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200586 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200587
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200588 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300589
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200590 if (dev->polling == false) {
591 device_unlock(&dev->dev);
592 return 0;
593 }
594
595 dev->polling = false;
596
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300597 dev->targets_generation++;
598
599 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200600 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300601
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200602 if (targets) {
603 dev->targets = kmemdup(targets,
604 n_targets * sizeof(struct nfc_target),
605 GFP_ATOMIC);
606
607 if (!dev->targets) {
608 dev->n_targets = 0;
609 device_unlock(&dev->dev);
610 return -ENOMEM;
611 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300612 }
613
614 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200615 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300616
617 nfc_genl_targets_found(dev);
618
619 return 0;
620}
621EXPORT_SYMBOL(nfc_targets_found);
622
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200623/**
624 * nfc_target_lost - inform that an activated target went out of field
625 *
626 * @dev: The nfc device that had the activated target in field
627 * @target_idx: the nfc index of the target
628 *
629 * The device driver must call this function when the activated target
630 * goes out of the field.
631 * IMPORTANT: this function must not be called from an atomic context.
632 * In addition, it must also not be called from a context that would prevent
633 * the NFC Core to call other nfc ops entry point concurrently.
634 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200635int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
636{
637 struct nfc_target *tg;
638 int i;
639
640 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
641
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200642 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200643
644 for (i = 0; i < dev->n_targets; i++) {
645 tg = &dev->targets[i];
646 if (tg->idx == target_idx)
647 break;
648 }
649
650 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200651 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200652 return -EINVAL;
653 }
654
655 dev->targets_generation++;
656 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200657 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200658
659 if (dev->n_targets) {
660 memcpy(&dev->targets[i], &dev->targets[i + 1],
661 (dev->n_targets - i) * sizeof(struct nfc_target));
662 } else {
663 kfree(dev->targets);
664 dev->targets = NULL;
665 }
666
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200667 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200668
669 nfc_genl_target_lost(dev, target_idx);
670
671 return 0;
672}
673EXPORT_SYMBOL(nfc_target_lost);
674
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200675inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200676{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200677 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200678}
679EXPORT_SYMBOL(nfc_driver_failure);
680
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300681static void nfc_release(struct device *d)
682{
683 struct nfc_dev *dev = to_nfc_dev(d);
684
Joe Perches20c239c2011-11-29 11:37:33 -0800685 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300686
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300687 nfc_genl_data_exit(&dev->genl_data);
688 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300689 kfree(dev);
690}
691
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200692static void nfc_check_pres_work(struct work_struct *work)
693{
694 struct nfc_dev *dev = container_of(work, struct nfc_dev,
695 check_pres_work);
696 int rc;
697
698 device_lock(&dev->dev);
699
Eric Lapuyade90099432012-05-07 12:31:13 +0200700 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
701 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200702 if (rc == -EOPNOTSUPP)
703 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100704 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200705 u32 active_target_idx = dev->active_target->idx;
706 device_unlock(&dev->dev);
707 nfc_target_lost(dev, active_target_idx);
708 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200709 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100710
711 if (!dev->shutting_down)
712 mod_timer(&dev->check_pres_timer, jiffies +
713 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200714 }
715
Eric Lapuyade632c0162012-10-02 17:27:36 +0200716exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200717 device_unlock(&dev->dev);
718}
719
720static void nfc_check_pres_timeout(unsigned long data)
721{
722 struct nfc_dev *dev = (struct nfc_dev *)data;
723
Linus Torvalds916082b2012-10-02 16:01:31 -0700724 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200725}
726
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300727struct class nfc_class = {
728 .name = "nfc",
729 .dev_release = nfc_release,
730};
731EXPORT_SYMBOL(nfc_class);
732
733static int match_idx(struct device *d, void *data)
734{
735 struct nfc_dev *dev = to_nfc_dev(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000736 unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300737
738 return dev->idx == *idx;
739}
740
Eric Dumazet95c96172012-04-15 05:58:06 +0000741struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300742{
743 struct device *d;
744
745 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
746 if (!d)
747 return NULL;
748
749 return to_nfc_dev(d);
750}
751
752/**
753 * nfc_allocate_device - allocate a new nfc device
754 *
755 * @ops: device operations
756 * @supported_protocols: NFC protocols supported by the device
757 */
758struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100759 u32 supported_protocols,
760 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300761{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300762 struct nfc_dev *dev;
763
764 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200765 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300766 return NULL;
767
768 if (!supported_protocols)
769 return NULL;
770
771 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
772 if (!dev)
773 return NULL;
774
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300775 dev->ops = ops;
776 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200777 dev->tx_headroom = tx_headroom;
778 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300779
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300780 nfc_genl_data_init(&dev->genl_data);
781
Thierry Escande5bcf0992012-10-05 11:05:45 +0200782 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200783
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300784 /* first generation must not be 0 */
785 dev->targets_generation = 1;
786
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200787 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200788 init_timer(&dev->check_pres_timer);
789 dev->check_pres_timer.data = (unsigned long)dev;
790 dev->check_pres_timer.function = nfc_check_pres_timeout;
791
792 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200793 }
794
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300795 return dev;
796}
797EXPORT_SYMBOL(nfc_allocate_device);
798
799/**
800 * nfc_register_device - register a nfc device in the nfc subsystem
801 *
802 * @dev: The nfc device to register
803 */
804int nfc_register_device(struct nfc_dev *dev)
805{
806 int rc;
807
Joe Perches20c239c2011-11-29 11:37:33 -0800808 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300809
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200810 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
811 if (dev->idx < 0)
812 return dev->idx;
813
814 dev->dev.class = &nfc_class;
815 dev_set_name(&dev->dev, "nfc%d", dev->idx);
816 device_initialize(&dev->dev);
817
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300818 mutex_lock(&nfc_devlist_mutex);
819 nfc_devlist_generation++;
820 rc = device_add(&dev->dev);
821 mutex_unlock(&nfc_devlist_mutex);
822
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300823 if (rc < 0)
824 return rc;
825
Samuel Ortizd6469602011-12-14 16:43:12 +0100826 rc = nfc_llcp_register_device(dev);
827 if (rc)
828 pr_err("Could not register llcp device\n");
829
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300830 rc = nfc_genl_device_added(dev);
831 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800832 pr_debug("The userspace won't be notified that the device %s was added\n",
833 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300834
835 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300836}
837EXPORT_SYMBOL(nfc_register_device);
838
839/**
840 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
841 *
842 * @dev: The nfc device to unregister
843 */
844void nfc_unregister_device(struct nfc_dev *dev)
845{
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200846 int rc, id;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300847
Joe Perches20c239c2011-11-29 11:37:33 -0800848 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300849
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200850 id = dev->idx;
851
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100852 if (dev->ops->check_presence) {
853 device_lock(&dev->dev);
854 dev->shutting_down = true;
855 device_unlock(&dev->dev);
856 del_timer_sync(&dev->check_pres_timer);
857 cancel_work_sync(&dev->check_pres_work);
858 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100859
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300860 rc = nfc_genl_device_removed(dev);
861 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100862 pr_debug("The userspace won't be notified that the device %s "
863 "was removed\n", dev_name(&dev->dev));
864
865 nfc_llcp_unregister_device(dev);
866
867 mutex_lock(&nfc_devlist_mutex);
868 nfc_devlist_generation++;
869 device_del(&dev->dev);
870 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300871
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +0200872 ida_simple_remove(&nfc_index_ida, id);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300873}
874EXPORT_SYMBOL(nfc_unregister_device);
875
876static int __init nfc_init(void)
877{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300878 int rc;
879
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800880 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300881
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300882 rc = class_register(&nfc_class);
883 if (rc)
884 return rc;
885
886 rc = nfc_genl_init();
887 if (rc)
888 goto err_genl;
889
890 /* the first generation must not be 0 */
891 nfc_devlist_generation = 1;
892
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300893 rc = rawsock_init();
894 if (rc)
895 goto err_rawsock;
896
Samuel Ortizd6469602011-12-14 16:43:12 +0100897 rc = nfc_llcp_init();
898 if (rc)
899 goto err_llcp_sock;
900
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300901 rc = af_nfc_init();
902 if (rc)
903 goto err_af_nfc;
904
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300905 return 0;
906
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300907err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100908 nfc_llcp_exit();
909err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300910 rawsock_exit();
911err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300912 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300913err_genl:
914 class_unregister(&nfc_class);
915 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300916}
917
918static void __exit nfc_exit(void)
919{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300920 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100921 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300922 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300923 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300924 class_unregister(&nfc_class);
925}
926
927subsys_initcall(nfc_init);
928module_exit(nfc_exit);
929
930MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
931MODULE_DESCRIPTION("NFC Core ver " VERSION);
932MODULE_VERSION(VERSION);
933MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +0200934MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +0200935MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);