blob: f1c33f23331167603c3cd9d08fa1111355031331 [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
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030043/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030044 * nfc_dev_up - turn on the NFC device
45 *
46 * @dev: The nfc device to be turned on
47 *
48 * The device remains up until the nfc_dev_down function is called.
49 */
50int nfc_dev_up(struct nfc_dev *dev)
51{
52 int rc = 0;
53
Joe Perches20c239c2011-11-29 11:37:33 -080054 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030055
56 device_lock(&dev->dev);
57
58 if (!device_is_registered(&dev->dev)) {
59 rc = -ENODEV;
60 goto error;
61 }
62
63 if (dev->dev_up) {
64 rc = -EALREADY;
65 goto error;
66 }
67
68 if (dev->ops->dev_up)
69 rc = dev->ops->dev_up(dev);
70
71 if (!rc)
72 dev->dev_up = true;
73
74error:
75 device_unlock(&dev->dev);
76 return rc;
77}
78
79/**
80 * nfc_dev_down - turn off the NFC device
81 *
82 * @dev: The nfc device to be turned off
83 */
84int nfc_dev_down(struct nfc_dev *dev)
85{
86 int rc = 0;
87
Joe Perches20c239c2011-11-29 11:37:33 -080088 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030089
90 device_lock(&dev->dev);
91
92 if (!device_is_registered(&dev->dev)) {
93 rc = -ENODEV;
94 goto error;
95 }
96
97 if (!dev->dev_up) {
98 rc = -EALREADY;
99 goto error;
100 }
101
Eric Lapuyade90099432012-05-07 12:31:13 +0200102 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300103 rc = -EBUSY;
104 goto error;
105 }
106
107 if (dev->ops->dev_down)
108 dev->ops->dev_down(dev);
109
110 dev->dev_up = false;
111
112error:
113 device_unlock(&dev->dev);
114 return rc;
115}
116
117/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300118 * nfc_start_poll - start polling for nfc targets
119 *
120 * @dev: The nfc device that must start polling
121 * @protocols: bitset of nfc protocols that must be used for polling
122 *
123 * The device remains polling for targets until a target is found or
124 * the nfc_stop_poll function is called.
125 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200126int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300127{
128 int rc;
129
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200130 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
131 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300132
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200133 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300134 return -EINVAL;
135
136 device_lock(&dev->dev);
137
138 if (!device_is_registered(&dev->dev)) {
139 rc = -ENODEV;
140 goto error;
141 }
142
143 if (dev->polling) {
144 rc = -EBUSY;
145 goto error;
146 }
147
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200148 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200149 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300150 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200151 dev->rf_mode = NFC_RF_NONE;
152 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300153
154error:
155 device_unlock(&dev->dev);
156 return rc;
157}
158
159/**
160 * nfc_stop_poll - stop polling for nfc targets
161 *
162 * @dev: The nfc device that must stop polling
163 */
164int nfc_stop_poll(struct nfc_dev *dev)
165{
166 int rc = 0;
167
Joe Perches20c239c2011-11-29 11:37:33 -0800168 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300169
170 device_lock(&dev->dev);
171
172 if (!device_is_registered(&dev->dev)) {
173 rc = -ENODEV;
174 goto error;
175 }
176
177 if (!dev->polling) {
178 rc = -EINVAL;
179 goto error;
180 }
181
182 dev->ops->stop_poll(dev);
183 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200184 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300185
186error:
187 device_unlock(&dev->dev);
188 return rc;
189}
190
Eric Lapuyade90099432012-05-07 12:31:13 +0200191static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
192{
193 int i;
194
195 if (dev->n_targets == 0)
196 return NULL;
197
198 for (i = 0; i < dev->n_targets ; i++) {
199 if (dev->targets[i].idx == target_idx)
200 return &dev->targets[i];
201 }
202
203 return NULL;
204}
205
Samuel Ortiz47807d32012-03-05 01:03:50 +0100206int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100207{
208 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100209 u8 *gb;
210 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200211 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100212
Samuel Ortiz47807d32012-03-05 01:03:50 +0100213 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100214
215 if (!dev->ops->dep_link_up)
216 return -EOPNOTSUPP;
217
218 device_lock(&dev->dev);
219
220 if (!device_is_registered(&dev->dev)) {
221 rc = -ENODEV;
222 goto error;
223 }
224
225 if (dev->dep_link_up == true) {
226 rc = -EALREADY;
227 goto error;
228 }
229
Samuel Ortiz47807d32012-03-05 01:03:50 +0100230 gb = nfc_llcp_general_bytes(dev, &gb_len);
231 if (gb_len > NFC_MAX_GT_LEN) {
232 rc = -EINVAL;
233 goto error;
234 }
235
Eric Lapuyade90099432012-05-07 12:31:13 +0200236 target = nfc_find_target(dev, target_index);
237 if (target == NULL) {
238 rc = -ENOTCONN;
239 goto error;
240 }
241
242 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200243 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200244 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200245 dev->rf_mode = NFC_RF_INITIATOR;
246 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100247
248error:
249 device_unlock(&dev->dev);
250 return rc;
251}
252
253int nfc_dep_link_down(struct nfc_dev *dev)
254{
255 int rc = 0;
256
257 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
258
259 if (!dev->ops->dep_link_down)
260 return -EOPNOTSUPP;
261
262 device_lock(&dev->dev);
263
264 if (!device_is_registered(&dev->dev)) {
265 rc = -ENODEV;
266 goto error;
267 }
268
269 if (dev->dep_link_up == false) {
270 rc = -EALREADY;
271 goto error;
272 }
273
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100274 rc = dev->ops->dep_link_down(dev);
275 if (!rc) {
276 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200277 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200278 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100279 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100280 nfc_genl_dep_link_down_event(dev);
281 }
282
283error:
284 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200285
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100286 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;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200509 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200510
511 return nfc_genl_tm_deactivated(dev);
512}
513EXPORT_SYMBOL(nfc_tm_deactivated);
514
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300515/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100516 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300517 *
518 * @size: size to allocate
519 * @gfp: gfp flags
520 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100521struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100522 unsigned int flags, unsigned int size,
523 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100524{
525 struct sk_buff *skb;
526 unsigned int total_size;
527
528 total_size = size +
529 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
530
531 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
532 if (skb)
533 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
534
535 return skb;
536}
537
538/**
539 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
540 *
541 * @size: size to allocate
542 * @gfp: gfp flags
543 */
544struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300545{
546 struct sk_buff *skb;
547 unsigned int total_size;
548
549 total_size = size + 1;
550 skb = alloc_skb(total_size, gfp);
551
552 if (skb)
553 skb_reserve(skb, 1);
554
555 return skb;
556}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100557EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300558
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300559/**
560 * nfc_targets_found - inform that targets were found
561 *
562 * @dev: The nfc device that found the targets
563 * @targets: array of nfc targets found
564 * @ntargets: targets array size
565 *
566 * The device driver must call this function when one or many nfc targets
567 * are found. After calling this function, the device driver must stop
568 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200569 * NOTE: This function can be called with targets=NULL and n_targets=0 to
570 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200571 * IMPORTANT: this function must not be called from an atomic context.
572 * In addition, it must also not be called from a context that would prevent
573 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300574 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100575int nfc_targets_found(struct nfc_dev *dev,
576 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300577{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200578 int i;
579
Joe Perches20c239c2011-11-29 11:37:33 -0800580 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300581
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200582 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200583 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200584
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200585 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300586
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200587 if (dev->polling == false) {
588 device_unlock(&dev->dev);
589 return 0;
590 }
591
592 dev->polling = false;
593
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300594 dev->targets_generation++;
595
596 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200597 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300598
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200599 if (targets) {
600 dev->targets = kmemdup(targets,
601 n_targets * sizeof(struct nfc_target),
602 GFP_ATOMIC);
603
604 if (!dev->targets) {
605 dev->n_targets = 0;
606 device_unlock(&dev->dev);
607 return -ENOMEM;
608 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300609 }
610
611 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200612 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300613
614 nfc_genl_targets_found(dev);
615
616 return 0;
617}
618EXPORT_SYMBOL(nfc_targets_found);
619
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200620/**
621 * nfc_target_lost - inform that an activated target went out of field
622 *
623 * @dev: The nfc device that had the activated target in field
624 * @target_idx: the nfc index of the target
625 *
626 * The device driver must call this function when the activated target
627 * goes out of the field.
628 * IMPORTANT: this function must not be called from an atomic context.
629 * In addition, it must also not be called from a context that would prevent
630 * the NFC Core to call other nfc ops entry point concurrently.
631 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200632int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
633{
634 struct nfc_target *tg;
635 int i;
636
637 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
638
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200639 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200640
641 for (i = 0; i < dev->n_targets; i++) {
642 tg = &dev->targets[i];
643 if (tg->idx == target_idx)
644 break;
645 }
646
647 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200648 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200649 return -EINVAL;
650 }
651
652 dev->targets_generation++;
653 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200654 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200655
656 if (dev->n_targets) {
657 memcpy(&dev->targets[i], &dev->targets[i + 1],
658 (dev->n_targets - i) * sizeof(struct nfc_target));
659 } else {
660 kfree(dev->targets);
661 dev->targets = NULL;
662 }
663
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200664 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200665
666 nfc_genl_target_lost(dev, target_idx);
667
668 return 0;
669}
670EXPORT_SYMBOL(nfc_target_lost);
671
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200672inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200673{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200674 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200675}
676EXPORT_SYMBOL(nfc_driver_failure);
677
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300678static void nfc_release(struct device *d)
679{
680 struct nfc_dev *dev = to_nfc_dev(d);
681
Joe Perches20c239c2011-11-29 11:37:33 -0800682 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300683
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200684 if (dev->ops->check_presence) {
685 del_timer_sync(&dev->check_pres_timer);
Tejun Heo474fee32012-08-22 16:22:16 -0700686 cancel_work_sync(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200687 }
688
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300689 nfc_genl_data_exit(&dev->genl_data);
690 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300691 kfree(dev);
692}
693
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200694static void nfc_check_pres_work(struct work_struct *work)
695{
696 struct nfc_dev *dev = container_of(work, struct nfc_dev,
697 check_pres_work);
698 int rc;
699
700 device_lock(&dev->dev);
701
Eric Lapuyade90099432012-05-07 12:31:13 +0200702 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
703 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200704 if (rc == -EOPNOTSUPP)
705 goto exit;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200706 if (!rc) {
707 mod_timer(&dev->check_pres_timer, jiffies +
708 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
709 } else {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200710 u32 active_target_idx = dev->active_target->idx;
711 device_unlock(&dev->dev);
712 nfc_target_lost(dev, active_target_idx);
713 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200714 }
715 }
716
Eric Lapuyade632c0162012-10-02 17:27:36 +0200717exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200718 device_unlock(&dev->dev);
719}
720
721static void nfc_check_pres_timeout(unsigned long data)
722{
723 struct nfc_dev *dev = (struct nfc_dev *)data;
724
Linus Torvalds916082b2012-10-02 16:01:31 -0700725 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200726}
727
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300728struct class nfc_class = {
729 .name = "nfc",
730 .dev_release = nfc_release,
731};
732EXPORT_SYMBOL(nfc_class);
733
734static int match_idx(struct device *d, void *data)
735{
736 struct nfc_dev *dev = to_nfc_dev(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000737 unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300738
739 return dev->idx == *idx;
740}
741
Eric Dumazet95c96172012-04-15 05:58:06 +0000742struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300743{
744 struct device *d;
745
746 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
747 if (!d)
748 return NULL;
749
750 return to_nfc_dev(d);
751}
752
753/**
754 * nfc_allocate_device - allocate a new nfc device
755 *
756 * @ops: device operations
757 * @supported_protocols: NFC protocols supported by the device
758 */
759struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100760 u32 supported_protocols,
761 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300762{
763 static atomic_t dev_no = ATOMIC_INIT(0);
764 struct nfc_dev *dev;
765
766 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200767 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300768 return NULL;
769
770 if (!supported_protocols)
771 return NULL;
772
773 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
774 if (!dev)
775 return NULL;
776
777 dev->dev.class = &nfc_class;
778 dev->idx = atomic_inc_return(&dev_no) - 1;
779 dev_set_name(&dev->dev, "nfc%d", dev->idx);
780 device_initialize(&dev->dev);
781
782 dev->ops = ops;
783 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200784 dev->tx_headroom = tx_headroom;
785 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300786
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300787 nfc_genl_data_init(&dev->genl_data);
788
Thierry Escande5bcf0992012-10-05 11:05:45 +0200789 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200790
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300791 /* first generation must not be 0 */
792 dev->targets_generation = 1;
793
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200794 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200795 init_timer(&dev->check_pres_timer);
796 dev->check_pres_timer.data = (unsigned long)dev;
797 dev->check_pres_timer.function = nfc_check_pres_timeout;
798
799 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200800 }
801
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300802 return dev;
803}
804EXPORT_SYMBOL(nfc_allocate_device);
805
806/**
807 * nfc_register_device - register a nfc device in the nfc subsystem
808 *
809 * @dev: The nfc device to register
810 */
811int nfc_register_device(struct nfc_dev *dev)
812{
813 int rc;
814
Joe Perches20c239c2011-11-29 11:37:33 -0800815 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300816
817 mutex_lock(&nfc_devlist_mutex);
818 nfc_devlist_generation++;
819 rc = device_add(&dev->dev);
820 mutex_unlock(&nfc_devlist_mutex);
821
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300822 if (rc < 0)
823 return rc;
824
Samuel Ortizd6469602011-12-14 16:43:12 +0100825 rc = nfc_llcp_register_device(dev);
826 if (rc)
827 pr_err("Could not register llcp device\n");
828
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300829 rc = nfc_genl_device_added(dev);
830 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800831 pr_debug("The userspace won't be notified that the device %s was added\n",
832 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300833
834 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300835}
836EXPORT_SYMBOL(nfc_register_device);
837
838/**
839 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
840 *
841 * @dev: The nfc device to unregister
842 */
843void nfc_unregister_device(struct nfc_dev *dev)
844{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300845 int rc;
846
Joe Perches20c239c2011-11-29 11:37:33 -0800847 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300848
849 mutex_lock(&nfc_devlist_mutex);
850 nfc_devlist_generation++;
851
852 /* lock to avoid unregistering a device while an operation
853 is in progress */
854 device_lock(&dev->dev);
855 device_del(&dev->dev);
856 device_unlock(&dev->dev);
857
858 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300859
Samuel Ortizd6469602011-12-14 16:43:12 +0100860 nfc_llcp_unregister_device(dev);
861
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300862 rc = nfc_genl_device_removed(dev);
863 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800864 pr_debug("The userspace won't be notified that the device %s was removed\n",
865 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300866
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300867}
868EXPORT_SYMBOL(nfc_unregister_device);
869
870static int __init nfc_init(void)
871{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300872 int rc;
873
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800874 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300875
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300876 rc = class_register(&nfc_class);
877 if (rc)
878 return rc;
879
880 rc = nfc_genl_init();
881 if (rc)
882 goto err_genl;
883
884 /* the first generation must not be 0 */
885 nfc_devlist_generation = 1;
886
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300887 rc = rawsock_init();
888 if (rc)
889 goto err_rawsock;
890
Samuel Ortizd6469602011-12-14 16:43:12 +0100891 rc = nfc_llcp_init();
892 if (rc)
893 goto err_llcp_sock;
894
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300895 rc = af_nfc_init();
896 if (rc)
897 goto err_af_nfc;
898
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300899 return 0;
900
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300901err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100902 nfc_llcp_exit();
903err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300904 rawsock_exit();
905err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300906 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300907err_genl:
908 class_unregister(&nfc_class);
909 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300910}
911
912static void __exit nfc_exit(void)
913{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300914 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100915 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300916 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300917 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300918 class_unregister(&nfc_class);
919}
920
921subsys_initcall(nfc_init);
922module_exit(nfc_exit);
923
924MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
925MODULE_DESCRIPTION("NFC Core ver " VERSION);
926MODULE_VERSION(VERSION);
927MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +0200928MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +0200929MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);