blob: 6a3799eebc30d6d671f17cf01902f48afbfce9a7 [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
32#include "nfc.h"
33
34#define VERSION "0.1"
35
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +020036#define NFC_CHECK_PRES_FREQ_MS 2000
37
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030038int nfc_devlist_generation;
39DEFINE_MUTEX(nfc_devlist_mutex);
40
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030041/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030042 * 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 */
48int nfc_dev_up(struct nfc_dev *dev)
49{
50 int rc = 0;
51
Joe Perches20c239c2011-11-29 11:37:33 -080052 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030053
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
72error:
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 */
82int nfc_dev_down(struct nfc_dev *dev)
83{
84 int rc = 0;
85
Joe Perches20c239c2011-11-29 11:37:33 -080086 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030087
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 Lapuyade90099432012-05-07 12:31:13 +0200100 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300101 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
110error:
111 device_unlock(&dev->dev);
112 return rc;
113}
114
115/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300116 * 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 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200124int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300125{
126 int rc;
127
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200128 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
129 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300130
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200131 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300132 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
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200146 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200147 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300148 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200149 dev->rf_mode = NFC_RF_NONE;
150 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300151
152error:
153 device_unlock(&dev->dev);
154 return rc;
155}
156
157/**
158 * nfc_stop_poll - stop polling for nfc targets
159 *
160 * @dev: The nfc device that must stop polling
161 */
162int nfc_stop_poll(struct nfc_dev *dev)
163{
164 int rc = 0;
165
Joe Perches20c239c2011-11-29 11:37:33 -0800166 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300167
168 device_lock(&dev->dev);
169
170 if (!device_is_registered(&dev->dev)) {
171 rc = -ENODEV;
172 goto error;
173 }
174
175 if (!dev->polling) {
176 rc = -EINVAL;
177 goto error;
178 }
179
180 dev->ops->stop_poll(dev);
181 dev->polling = false;
182
183error:
184 device_unlock(&dev->dev);
185 return rc;
186}
187
Eric Lapuyade90099432012-05-07 12:31:13 +0200188static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
189{
190 int i;
191
192 if (dev->n_targets == 0)
193 return NULL;
194
195 for (i = 0; i < dev->n_targets ; i++) {
196 if (dev->targets[i].idx == target_idx)
197 return &dev->targets[i];
198 }
199
200 return NULL;
201}
202
Samuel Ortiz47807d32012-03-05 01:03:50 +0100203int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100204{
205 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100206 u8 *gb;
207 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200208 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100209
Samuel Ortiz47807d32012-03-05 01:03:50 +0100210 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100211
212 if (!dev->ops->dep_link_up)
213 return -EOPNOTSUPP;
214
215 device_lock(&dev->dev);
216
217 if (!device_is_registered(&dev->dev)) {
218 rc = -ENODEV;
219 goto error;
220 }
221
222 if (dev->dep_link_up == true) {
223 rc = -EALREADY;
224 goto error;
225 }
226
Samuel Ortiz47807d32012-03-05 01:03:50 +0100227 gb = nfc_llcp_general_bytes(dev, &gb_len);
228 if (gb_len > NFC_MAX_GT_LEN) {
229 rc = -EINVAL;
230 goto error;
231 }
232
Eric Lapuyade90099432012-05-07 12:31:13 +0200233 target = nfc_find_target(dev, target_index);
234 if (target == NULL) {
235 rc = -ENOTCONN;
236 goto error;
237 }
238
239 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200240 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200241 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200242 dev->rf_mode = NFC_RF_INITIATOR;
243 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100244
245error:
246 device_unlock(&dev->dev);
247 return rc;
248}
249
250int nfc_dep_link_down(struct nfc_dev *dev)
251{
252 int rc = 0;
253
254 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
255
256 if (!dev->ops->dep_link_down)
257 return -EOPNOTSUPP;
258
259 device_lock(&dev->dev);
260
261 if (!device_is_registered(&dev->dev)) {
262 rc = -ENODEV;
263 goto error;
264 }
265
266 if (dev->dep_link_up == false) {
267 rc = -EALREADY;
268 goto error;
269 }
270
Samuel Ortizf212ad52012-05-31 00:02:26 +0200271 if (dev->rf_mode == NFC_RF_TARGET) {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100272 rc = -EOPNOTSUPP;
273 goto error;
274 }
275
276 rc = dev->ops->dep_link_down(dev);
277 if (!rc) {
278 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200279 dev->active_target = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100280 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100281 nfc_genl_dep_link_down_event(dev);
282 }
283
284error:
285 device_unlock(&dev->dev);
286 return rc;
287}
288
289int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100290 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100291{
292 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100293
Samuel Ortizd6469602011-12-14 16:43:12 +0100294 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
295
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100296 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
297}
298EXPORT_SYMBOL(nfc_dep_link_is_up);
299
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300300/**
301 * nfc_activate_target - prepare the target for data exchange
302 *
303 * @dev: The nfc device that found the target
304 * @target_idx: index of the target that must be activated
305 * @protocol: nfc protocol that will be used for data exchange
306 */
307int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
308{
309 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200310 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300311
Joe Perches20c239c2011-11-29 11:37:33 -0800312 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
313 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300314
315 device_lock(&dev->dev);
316
317 if (!device_is_registered(&dev->dev)) {
318 rc = -ENODEV;
319 goto error;
320 }
321
Eric Lapuyade90099432012-05-07 12:31:13 +0200322 if (dev->active_target) {
323 rc = -EBUSY;
324 goto error;
325 }
326
327 target = nfc_find_target(dev, target_idx);
328 if (target == NULL) {
329 rc = -ENOTCONN;
330 goto error;
331 }
332
333 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200334 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200335 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200336 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300337
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200338 if (dev->ops->check_presence)
339 mod_timer(&dev->check_pres_timer, jiffies +
340 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
341 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300342
343error:
344 device_unlock(&dev->dev);
345 return rc;
346}
347
348/**
349 * nfc_deactivate_target - deactivate a nfc target
350 *
351 * @dev: The nfc device that found the target
352 * @target_idx: index of the target that must be deactivated
353 */
354int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
355{
356 int rc = 0;
357
Joe Perches20c239c2011-11-29 11:37:33 -0800358 pr_debug("dev_name=%s target_idx=%u\n",
359 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300360
361 device_lock(&dev->dev);
362
363 if (!device_is_registered(&dev->dev)) {
364 rc = -ENODEV;
365 goto error;
366 }
367
Eric Lapuyade90099432012-05-07 12:31:13 +0200368 if (dev->active_target == NULL) {
369 rc = -ENOTCONN;
370 goto error;
371 }
372
373 if (dev->active_target->idx != target_idx) {
374 rc = -ENOTCONN;
375 goto error;
376 }
377
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200378 if (dev->ops->check_presence)
379 del_timer_sync(&dev->check_pres_timer);
380
Eric Lapuyade90099432012-05-07 12:31:13 +0200381 dev->ops->deactivate_target(dev, dev->active_target);
382 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300383
384error:
385 device_unlock(&dev->dev);
386 return rc;
387}
388
389/**
390 * nfc_data_exchange - transceive data
391 *
392 * @dev: The nfc device that found the target
393 * @target_idx: index of the target
394 * @skb: data to be sent
395 * @cb: callback called when the response is received
396 * @cb_context: parameter for the callback function
397 *
398 * The user must wait for the callback before calling this function again.
399 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100400int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
401 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300402{
403 int rc;
404
Joe Perches20c239c2011-11-29 11:37:33 -0800405 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
406 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300407
408 device_lock(&dev->dev);
409
410 if (!device_is_registered(&dev->dev)) {
411 rc = -ENODEV;
412 kfree_skb(skb);
413 goto error;
414 }
415
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200416 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
417 if (dev->active_target->idx != target_idx) {
418 rc = -EADDRNOTAVAIL;
419 kfree_skb(skb);
420 goto error;
421 }
422
423 if (dev->ops->check_presence)
424 del_timer_sync(&dev->check_pres_timer);
425
426 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
427 cb_context);
428
429 if (!rc && dev->ops->check_presence)
430 mod_timer(&dev->check_pres_timer, jiffies +
431 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
432 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
433 rc = dev->ops->tm_send(dev, skb);
434 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200435 rc = -ENOTCONN;
436 kfree_skb(skb);
437 goto error;
438 }
439
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200440
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300441error:
442 device_unlock(&dev->dev);
443 return rc;
444}
445
Samuel Ortiz541d9202011-12-14 16:43:10 +0100446int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
447{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100448 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100449
450 if (gb_len > NFC_MAX_GT_LEN)
451 return -EINVAL;
452
Samuel Ortizd6469602011-12-14 16:43:12 +0100453 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100454}
455EXPORT_SYMBOL(nfc_set_remote_general_bytes);
456
Samuel Ortizab73b752012-04-10 12:51:52 +0200457u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
458{
459 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
460
461 return nfc_llcp_general_bytes(dev, gb_len);
462}
463EXPORT_SYMBOL(nfc_get_local_general_bytes);
464
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200465int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
466{
467 /* Only LLCP target mode for now */
468 if (dev->dep_link_up == false) {
469 kfree_skb(skb);
470 return -ENOLINK;
471 }
472
473 return nfc_llcp_data_received(dev, skb);
474}
475EXPORT_SYMBOL(nfc_tm_data_received);
476
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200477int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
478 u8 *gb, size_t gb_len)
479{
480 int rc;
481
482 device_lock(&dev->dev);
483
484 dev->polling = false;
485
486 if (gb != NULL) {
487 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
488 if (rc < 0)
489 goto out;
490 }
491
Samuel Ortizf212ad52012-05-31 00:02:26 +0200492 dev->rf_mode = NFC_RF_TARGET;
493
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200494 if (protocol == NFC_PROTO_NFC_DEP_MASK)
495 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
496
497 rc = nfc_genl_tm_activated(dev, protocol);
498
499out:
500 device_unlock(&dev->dev);
501
502 return rc;
503}
504EXPORT_SYMBOL(nfc_tm_activated);
505
506int nfc_tm_deactivated(struct nfc_dev *dev)
507{
508 dev->dep_link_up = false;
509
510 return nfc_genl_tm_deactivated(dev);
511}
512EXPORT_SYMBOL(nfc_tm_deactivated);
513
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300514/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100515 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300516 *
517 * @size: size to allocate
518 * @gfp: gfp flags
519 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100520struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100521 unsigned int flags, unsigned int size,
522 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100523{
524 struct sk_buff *skb;
525 unsigned int total_size;
526
527 total_size = size +
528 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
529
530 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
531 if (skb)
532 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
533
534 return skb;
535}
536
537/**
538 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
539 *
540 * @size: size to allocate
541 * @gfp: gfp flags
542 */
543struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300544{
545 struct sk_buff *skb;
546 unsigned int total_size;
547
548 total_size = size + 1;
549 skb = alloc_skb(total_size, gfp);
550
551 if (skb)
552 skb_reserve(skb, 1);
553
554 return skb;
555}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100556EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300557
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300558/**
559 * nfc_targets_found - inform that targets were found
560 *
561 * @dev: The nfc device that found the targets
562 * @targets: array of nfc targets found
563 * @ntargets: targets array size
564 *
565 * The device driver must call this function when one or many nfc targets
566 * are found. After calling this function, the device driver must stop
567 * polling for targets.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200568 * IMPORTANT: this function must not be called from an atomic context.
569 * In addition, it must also not be called from a context that would prevent
570 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300571 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100572int nfc_targets_found(struct nfc_dev *dev,
573 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300574{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200575 int i;
576
Joe Perches20c239c2011-11-29 11:37:33 -0800577 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300578
579 dev->polling = false;
580
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200581 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200582 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200583
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200584 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300585
586 dev->targets_generation++;
587
588 kfree(dev->targets);
589 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100590 GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300591
592 if (!dev->targets) {
593 dev->n_targets = 0;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200594 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300595 return -ENOMEM;
596 }
597
598 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200599 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300600
601 nfc_genl_targets_found(dev);
602
603 return 0;
604}
605EXPORT_SYMBOL(nfc_targets_found);
606
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200607/**
608 * nfc_target_lost - inform that an activated target went out of field
609 *
610 * @dev: The nfc device that had the activated target in field
611 * @target_idx: the nfc index of the target
612 *
613 * The device driver must call this function when the activated target
614 * goes out of the field.
615 * IMPORTANT: this function must not be called from an atomic context.
616 * In addition, it must also not be called from a context that would prevent
617 * the NFC Core to call other nfc ops entry point concurrently.
618 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200619int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
620{
621 struct nfc_target *tg;
622 int i;
623
624 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
625
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200626 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200627
628 for (i = 0; i < dev->n_targets; i++) {
629 tg = &dev->targets[i];
630 if (tg->idx == target_idx)
631 break;
632 }
633
634 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200635 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200636 return -EINVAL;
637 }
638
639 dev->targets_generation++;
640 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200641 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200642
643 if (dev->n_targets) {
644 memcpy(&dev->targets[i], &dev->targets[i + 1],
645 (dev->n_targets - i) * sizeof(struct nfc_target));
646 } else {
647 kfree(dev->targets);
648 dev->targets = NULL;
649 }
650
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200651 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200652
653 nfc_genl_target_lost(dev, target_idx);
654
655 return 0;
656}
657EXPORT_SYMBOL(nfc_target_lost);
658
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300659static void nfc_release(struct device *d)
660{
661 struct nfc_dev *dev = to_nfc_dev(d);
662
Joe Perches20c239c2011-11-29 11:37:33 -0800663 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300664
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200665 if (dev->ops->check_presence) {
666 del_timer_sync(&dev->check_pres_timer);
667 destroy_workqueue(dev->check_pres_wq);
668 }
669
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300670 nfc_genl_data_exit(&dev->genl_data);
671 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300672 kfree(dev);
673}
674
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200675static void nfc_check_pres_work(struct work_struct *work)
676{
677 struct nfc_dev *dev = container_of(work, struct nfc_dev,
678 check_pres_work);
679 int rc;
680
681 device_lock(&dev->dev);
682
Eric Lapuyade90099432012-05-07 12:31:13 +0200683 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
684 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200685 if (!rc) {
686 mod_timer(&dev->check_pres_timer, jiffies +
687 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
688 } else {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200689 u32 active_target_idx = dev->active_target->idx;
690 device_unlock(&dev->dev);
691 nfc_target_lost(dev, active_target_idx);
692 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200693 }
694 }
695
696 device_unlock(&dev->dev);
697}
698
699static void nfc_check_pres_timeout(unsigned long data)
700{
701 struct nfc_dev *dev = (struct nfc_dev *)data;
702
703 queue_work(dev->check_pres_wq, &dev->check_pres_work);
704}
705
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300706struct class nfc_class = {
707 .name = "nfc",
708 .dev_release = nfc_release,
709};
710EXPORT_SYMBOL(nfc_class);
711
712static int match_idx(struct device *d, void *data)
713{
714 struct nfc_dev *dev = to_nfc_dev(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000715 unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300716
717 return dev->idx == *idx;
718}
719
Eric Dumazet95c96172012-04-15 05:58:06 +0000720struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300721{
722 struct device *d;
723
724 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
725 if (!d)
726 return NULL;
727
728 return to_nfc_dev(d);
729}
730
731/**
732 * nfc_allocate_device - allocate a new nfc device
733 *
734 * @ops: device operations
735 * @supported_protocols: NFC protocols supported by the device
736 */
737struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100738 u32 supported_protocols,
739 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300740{
741 static atomic_t dev_no = ATOMIC_INIT(0);
742 struct nfc_dev *dev;
743
744 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200745 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300746 return NULL;
747
748 if (!supported_protocols)
749 return NULL;
750
751 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
752 if (!dev)
753 return NULL;
754
755 dev->dev.class = &nfc_class;
756 dev->idx = atomic_inc_return(&dev_no) - 1;
757 dev_set_name(&dev->dev, "nfc%d", dev->idx);
758 device_initialize(&dev->dev);
759
760 dev->ops = ops;
761 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200762 dev->tx_headroom = tx_headroom;
763 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300764
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300765 nfc_genl_data_init(&dev->genl_data);
766
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200767
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300768 /* first generation must not be 0 */
769 dev->targets_generation = 1;
770
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200771 if (ops->check_presence) {
772 char name[32];
773 init_timer(&dev->check_pres_timer);
774 dev->check_pres_timer.data = (unsigned long)dev;
775 dev->check_pres_timer.function = nfc_check_pres_timeout;
776
777 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
778 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
779 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
780 WQ_UNBOUND |
781 WQ_MEM_RECLAIM, 1);
782 if (dev->check_pres_wq == NULL) {
783 kfree(dev);
784 return NULL;
785 }
786 }
787
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300788 return dev;
789}
790EXPORT_SYMBOL(nfc_allocate_device);
791
792/**
793 * nfc_register_device - register a nfc device in the nfc subsystem
794 *
795 * @dev: The nfc device to register
796 */
797int nfc_register_device(struct nfc_dev *dev)
798{
799 int rc;
800
Joe Perches20c239c2011-11-29 11:37:33 -0800801 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300802
803 mutex_lock(&nfc_devlist_mutex);
804 nfc_devlist_generation++;
805 rc = device_add(&dev->dev);
806 mutex_unlock(&nfc_devlist_mutex);
807
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300808 if (rc < 0)
809 return rc;
810
Samuel Ortizd6469602011-12-14 16:43:12 +0100811 rc = nfc_llcp_register_device(dev);
812 if (rc)
813 pr_err("Could not register llcp device\n");
814
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300815 rc = nfc_genl_device_added(dev);
816 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800817 pr_debug("The userspace won't be notified that the device %s was added\n",
818 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300819
820 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300821}
822EXPORT_SYMBOL(nfc_register_device);
823
824/**
825 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
826 *
827 * @dev: The nfc device to unregister
828 */
829void nfc_unregister_device(struct nfc_dev *dev)
830{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300831 int rc;
832
Joe Perches20c239c2011-11-29 11:37:33 -0800833 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300834
835 mutex_lock(&nfc_devlist_mutex);
836 nfc_devlist_generation++;
837
838 /* lock to avoid unregistering a device while an operation
839 is in progress */
840 device_lock(&dev->dev);
841 device_del(&dev->dev);
842 device_unlock(&dev->dev);
843
844 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300845
Samuel Ortizd6469602011-12-14 16:43:12 +0100846 nfc_llcp_unregister_device(dev);
847
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300848 rc = nfc_genl_device_removed(dev);
849 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800850 pr_debug("The userspace won't be notified that the device %s was removed\n",
851 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300852
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300853}
854EXPORT_SYMBOL(nfc_unregister_device);
855
856static int __init nfc_init(void)
857{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300858 int rc;
859
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800860 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300861
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300862 rc = class_register(&nfc_class);
863 if (rc)
864 return rc;
865
866 rc = nfc_genl_init();
867 if (rc)
868 goto err_genl;
869
870 /* the first generation must not be 0 */
871 nfc_devlist_generation = 1;
872
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300873 rc = rawsock_init();
874 if (rc)
875 goto err_rawsock;
876
Samuel Ortizd6469602011-12-14 16:43:12 +0100877 rc = nfc_llcp_init();
878 if (rc)
879 goto err_llcp_sock;
880
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300881 rc = af_nfc_init();
882 if (rc)
883 goto err_af_nfc;
884
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300885 return 0;
886
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300887err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100888 nfc_llcp_exit();
889err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300890 rawsock_exit();
891err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300892 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300893err_genl:
894 class_unregister(&nfc_class);
895 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300896}
897
898static void __exit nfc_exit(void)
899{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300900 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100901 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300902 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300903 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300904 class_unregister(&nfc_class);
905}
906
907subsys_initcall(nfc_init);
908module_exit(nfc_exit);
909
910MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
911MODULE_DESCRIPTION("NFC Core ver " VERSION);
912MODULE_VERSION(VERSION);
913MODULE_LICENSE("GPL");