blob: 76c1e207d297d0cb9107537986e558d39a3759da [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 Ortizfc40a8c2012-06-01 13:21:13 +0200465int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
466 u8 *gb, size_t gb_len)
467{
468 int rc;
469
470 device_lock(&dev->dev);
471
472 dev->polling = false;
473
474 if (gb != NULL) {
475 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
476 if (rc < 0)
477 goto out;
478 }
479
Samuel Ortizf212ad52012-05-31 00:02:26 +0200480 dev->rf_mode = NFC_RF_TARGET;
481
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200482 if (protocol == NFC_PROTO_NFC_DEP_MASK)
483 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
484
485 rc = nfc_genl_tm_activated(dev, protocol);
486
487out:
488 device_unlock(&dev->dev);
489
490 return rc;
491}
492EXPORT_SYMBOL(nfc_tm_activated);
493
494int nfc_tm_deactivated(struct nfc_dev *dev)
495{
496 dev->dep_link_up = false;
497
498 return nfc_genl_tm_deactivated(dev);
499}
500EXPORT_SYMBOL(nfc_tm_deactivated);
501
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300502/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100503 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300504 *
505 * @size: size to allocate
506 * @gfp: gfp flags
507 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100508struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100509 unsigned int flags, unsigned int size,
510 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100511{
512 struct sk_buff *skb;
513 unsigned int total_size;
514
515 total_size = size +
516 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
517
518 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
519 if (skb)
520 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
521
522 return skb;
523}
524
525/**
526 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
527 *
528 * @size: size to allocate
529 * @gfp: gfp flags
530 */
531struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300532{
533 struct sk_buff *skb;
534 unsigned int total_size;
535
536 total_size = size + 1;
537 skb = alloc_skb(total_size, gfp);
538
539 if (skb)
540 skb_reserve(skb, 1);
541
542 return skb;
543}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100544EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300545
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300546/**
547 * nfc_targets_found - inform that targets were found
548 *
549 * @dev: The nfc device that found the targets
550 * @targets: array of nfc targets found
551 * @ntargets: targets array size
552 *
553 * The device driver must call this function when one or many nfc targets
554 * are found. After calling this function, the device driver must stop
555 * polling for targets.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200556 * IMPORTANT: this function must not be called from an atomic context.
557 * In addition, it must also not be called from a context that would prevent
558 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300559 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100560int nfc_targets_found(struct nfc_dev *dev,
561 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300562{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200563 int i;
564
Joe Perches20c239c2011-11-29 11:37:33 -0800565 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300566
567 dev->polling = false;
568
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200569 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200570 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200571
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200572 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300573
574 dev->targets_generation++;
575
576 kfree(dev->targets);
577 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100578 GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300579
580 if (!dev->targets) {
581 dev->n_targets = 0;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200582 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300583 return -ENOMEM;
584 }
585
586 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200587 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300588
589 nfc_genl_targets_found(dev);
590
591 return 0;
592}
593EXPORT_SYMBOL(nfc_targets_found);
594
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200595/**
596 * nfc_target_lost - inform that an activated target went out of field
597 *
598 * @dev: The nfc device that had the activated target in field
599 * @target_idx: the nfc index of the target
600 *
601 * The device driver must call this function when the activated target
602 * goes out of the field.
603 * IMPORTANT: this function must not be called from an atomic context.
604 * In addition, it must also not be called from a context that would prevent
605 * the NFC Core to call other nfc ops entry point concurrently.
606 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200607int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
608{
609 struct nfc_target *tg;
610 int i;
611
612 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
613
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200614 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200615
616 for (i = 0; i < dev->n_targets; i++) {
617 tg = &dev->targets[i];
618 if (tg->idx == target_idx)
619 break;
620 }
621
622 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200623 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200624 return -EINVAL;
625 }
626
627 dev->targets_generation++;
628 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200629 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200630
631 if (dev->n_targets) {
632 memcpy(&dev->targets[i], &dev->targets[i + 1],
633 (dev->n_targets - i) * sizeof(struct nfc_target));
634 } else {
635 kfree(dev->targets);
636 dev->targets = NULL;
637 }
638
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200639 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200640
641 nfc_genl_target_lost(dev, target_idx);
642
643 return 0;
644}
645EXPORT_SYMBOL(nfc_target_lost);
646
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300647static void nfc_release(struct device *d)
648{
649 struct nfc_dev *dev = to_nfc_dev(d);
650
Joe Perches20c239c2011-11-29 11:37:33 -0800651 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300652
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200653 if (dev->ops->check_presence) {
654 del_timer_sync(&dev->check_pres_timer);
655 destroy_workqueue(dev->check_pres_wq);
656 }
657
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300658 nfc_genl_data_exit(&dev->genl_data);
659 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300660 kfree(dev);
661}
662
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200663static void nfc_check_pres_work(struct work_struct *work)
664{
665 struct nfc_dev *dev = container_of(work, struct nfc_dev,
666 check_pres_work);
667 int rc;
668
669 device_lock(&dev->dev);
670
Eric Lapuyade90099432012-05-07 12:31:13 +0200671 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
672 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200673 if (!rc) {
674 mod_timer(&dev->check_pres_timer, jiffies +
675 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
676 } else {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200677 u32 active_target_idx = dev->active_target->idx;
678 device_unlock(&dev->dev);
679 nfc_target_lost(dev, active_target_idx);
680 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200681 }
682 }
683
684 device_unlock(&dev->dev);
685}
686
687static void nfc_check_pres_timeout(unsigned long data)
688{
689 struct nfc_dev *dev = (struct nfc_dev *)data;
690
691 queue_work(dev->check_pres_wq, &dev->check_pres_work);
692}
693
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300694struct class nfc_class = {
695 .name = "nfc",
696 .dev_release = nfc_release,
697};
698EXPORT_SYMBOL(nfc_class);
699
700static int match_idx(struct device *d, void *data)
701{
702 struct nfc_dev *dev = to_nfc_dev(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000703 unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300704
705 return dev->idx == *idx;
706}
707
Eric Dumazet95c96172012-04-15 05:58:06 +0000708struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300709{
710 struct device *d;
711
712 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
713 if (!d)
714 return NULL;
715
716 return to_nfc_dev(d);
717}
718
719/**
720 * nfc_allocate_device - allocate a new nfc device
721 *
722 * @ops: device operations
723 * @supported_protocols: NFC protocols supported by the device
724 */
725struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100726 u32 supported_protocols,
727 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300728{
729 static atomic_t dev_no = ATOMIC_INIT(0);
730 struct nfc_dev *dev;
731
732 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200733 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300734 return NULL;
735
736 if (!supported_protocols)
737 return NULL;
738
739 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
740 if (!dev)
741 return NULL;
742
743 dev->dev.class = &nfc_class;
744 dev->idx = atomic_inc_return(&dev_no) - 1;
745 dev_set_name(&dev->dev, "nfc%d", dev->idx);
746 device_initialize(&dev->dev);
747
748 dev->ops = ops;
749 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200750 dev->tx_headroom = tx_headroom;
751 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300752
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300753 nfc_genl_data_init(&dev->genl_data);
754
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200755
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300756 /* first generation must not be 0 */
757 dev->targets_generation = 1;
758
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200759 if (ops->check_presence) {
760 char name[32];
761 init_timer(&dev->check_pres_timer);
762 dev->check_pres_timer.data = (unsigned long)dev;
763 dev->check_pres_timer.function = nfc_check_pres_timeout;
764
765 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
766 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
767 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
768 WQ_UNBOUND |
769 WQ_MEM_RECLAIM, 1);
770 if (dev->check_pres_wq == NULL) {
771 kfree(dev);
772 return NULL;
773 }
774 }
775
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300776 return dev;
777}
778EXPORT_SYMBOL(nfc_allocate_device);
779
780/**
781 * nfc_register_device - register a nfc device in the nfc subsystem
782 *
783 * @dev: The nfc device to register
784 */
785int nfc_register_device(struct nfc_dev *dev)
786{
787 int rc;
788
Joe Perches20c239c2011-11-29 11:37:33 -0800789 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300790
791 mutex_lock(&nfc_devlist_mutex);
792 nfc_devlist_generation++;
793 rc = device_add(&dev->dev);
794 mutex_unlock(&nfc_devlist_mutex);
795
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300796 if (rc < 0)
797 return rc;
798
Samuel Ortizd6469602011-12-14 16:43:12 +0100799 rc = nfc_llcp_register_device(dev);
800 if (rc)
801 pr_err("Could not register llcp device\n");
802
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300803 rc = nfc_genl_device_added(dev);
804 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800805 pr_debug("The userspace won't be notified that the device %s was added\n",
806 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300807
808 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300809}
810EXPORT_SYMBOL(nfc_register_device);
811
812/**
813 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
814 *
815 * @dev: The nfc device to unregister
816 */
817void nfc_unregister_device(struct nfc_dev *dev)
818{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300819 int rc;
820
Joe Perches20c239c2011-11-29 11:37:33 -0800821 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300822
823 mutex_lock(&nfc_devlist_mutex);
824 nfc_devlist_generation++;
825
826 /* lock to avoid unregistering a device while an operation
827 is in progress */
828 device_lock(&dev->dev);
829 device_del(&dev->dev);
830 device_unlock(&dev->dev);
831
832 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300833
Samuel Ortizd6469602011-12-14 16:43:12 +0100834 nfc_llcp_unregister_device(dev);
835
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300836 rc = nfc_genl_device_removed(dev);
837 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800838 pr_debug("The userspace won't be notified that the device %s was removed\n",
839 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300840
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300841}
842EXPORT_SYMBOL(nfc_unregister_device);
843
844static int __init nfc_init(void)
845{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300846 int rc;
847
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800848 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300849
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300850 rc = class_register(&nfc_class);
851 if (rc)
852 return rc;
853
854 rc = nfc_genl_init();
855 if (rc)
856 goto err_genl;
857
858 /* the first generation must not be 0 */
859 nfc_devlist_generation = 1;
860
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300861 rc = rawsock_init();
862 if (rc)
863 goto err_rawsock;
864
Samuel Ortizd6469602011-12-14 16:43:12 +0100865 rc = nfc_llcp_init();
866 if (rc)
867 goto err_llcp_sock;
868
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300869 rc = af_nfc_init();
870 if (rc)
871 goto err_af_nfc;
872
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300873 return 0;
874
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300875err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100876 nfc_llcp_exit();
877err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300878 rawsock_exit();
879err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300880 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300881err_genl:
882 class_unregister(&nfc_class);
883 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300884}
885
886static void __exit nfc_exit(void)
887{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300888 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100889 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300890 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300891 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300892 class_unregister(&nfc_class);
893}
894
895subsys_initcall(nfc_init);
896module_exit(nfc_exit);
897
898MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
899MODULE_DESCRIPTION("NFC Core ver " VERSION);
900MODULE_VERSION(VERSION);
901MODULE_LICENSE("GPL");