blob: 9f6ce011d35d17135dc0780e75da036e520f7288 [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 */
124int nfc_start_poll(struct nfc_dev *dev, u32 protocols)
125{
126 int rc;
127
Joe Perches20c239c2011-11-29 11:37:33 -0800128 pr_debug("dev_name=%s protocols=0x%x\n",
129 dev_name(&dev->dev), protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300130
131 if (!protocols)
132 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
146 rc = dev->ops->start_poll(dev, protocols);
147 if (!rc)
148 dev->polling = true;
149
150error:
151 device_unlock(&dev->dev);
152 return rc;
153}
154
155/**
156 * nfc_stop_poll - stop polling for nfc targets
157 *
158 * @dev: The nfc device that must stop polling
159 */
160int nfc_stop_poll(struct nfc_dev *dev)
161{
162 int rc = 0;
163
Joe Perches20c239c2011-11-29 11:37:33 -0800164 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300165
166 device_lock(&dev->dev);
167
168 if (!device_is_registered(&dev->dev)) {
169 rc = -ENODEV;
170 goto error;
171 }
172
173 if (!dev->polling) {
174 rc = -EINVAL;
175 goto error;
176 }
177
178 dev->ops->stop_poll(dev);
179 dev->polling = false;
180
181error:
182 device_unlock(&dev->dev);
183 return rc;
184}
185
Eric Lapuyade90099432012-05-07 12:31:13 +0200186static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
187{
188 int i;
189
190 if (dev->n_targets == 0)
191 return NULL;
192
193 for (i = 0; i < dev->n_targets ; i++) {
194 if (dev->targets[i].idx == target_idx)
195 return &dev->targets[i];
196 }
197
198 return NULL;
199}
200
Samuel Ortiz47807d32012-03-05 01:03:50 +0100201int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100202{
203 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100204 u8 *gb;
205 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200206 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100207
Samuel Ortiz47807d32012-03-05 01:03:50 +0100208 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100209
210 if (!dev->ops->dep_link_up)
211 return -EOPNOTSUPP;
212
213 device_lock(&dev->dev);
214
215 if (!device_is_registered(&dev->dev)) {
216 rc = -ENODEV;
217 goto error;
218 }
219
220 if (dev->dep_link_up == true) {
221 rc = -EALREADY;
222 goto error;
223 }
224
Samuel Ortiz47807d32012-03-05 01:03:50 +0100225 gb = nfc_llcp_general_bytes(dev, &gb_len);
226 if (gb_len > NFC_MAX_GT_LEN) {
227 rc = -EINVAL;
228 goto error;
229 }
230
Eric Lapuyade90099432012-05-07 12:31:13 +0200231 target = nfc_find_target(dev, target_index);
232 if (target == NULL) {
233 rc = -ENOTCONN;
234 goto error;
235 }
236
237 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Eric Lapuyade144612c2012-04-10 19:43:11 +0200238 if (!rc)
Eric Lapuyade90099432012-05-07 12:31:13 +0200239 dev->active_target = target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100240
241error:
242 device_unlock(&dev->dev);
243 return rc;
244}
245
246int nfc_dep_link_down(struct nfc_dev *dev)
247{
248 int rc = 0;
249
250 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
251
252 if (!dev->ops->dep_link_down)
253 return -EOPNOTSUPP;
254
255 device_lock(&dev->dev);
256
257 if (!device_is_registered(&dev->dev)) {
258 rc = -ENODEV;
259 goto error;
260 }
261
262 if (dev->dep_link_up == false) {
263 rc = -EALREADY;
264 goto error;
265 }
266
267 if (dev->dep_rf_mode == NFC_RF_TARGET) {
268 rc = -EOPNOTSUPP;
269 goto error;
270 }
271
272 rc = dev->ops->dep_link_down(dev);
273 if (!rc) {
274 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200275 dev->active_target = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100276 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100277 nfc_genl_dep_link_down_event(dev);
278 }
279
280error:
281 device_unlock(&dev->dev);
282 return rc;
283}
284
285int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100286 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100287{
288 dev->dep_link_up = true;
289 dev->dep_rf_mode = rf_mode;
290
Samuel Ortizd6469602011-12-14 16:43:12 +0100291 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
292
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100293 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
294}
295EXPORT_SYMBOL(nfc_dep_link_is_up);
296
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300297/**
298 * nfc_activate_target - prepare the target for data exchange
299 *
300 * @dev: The nfc device that found the target
301 * @target_idx: index of the target that must be activated
302 * @protocol: nfc protocol that will be used for data exchange
303 */
304int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
305{
306 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200307 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300308
Joe Perches20c239c2011-11-29 11:37:33 -0800309 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
310 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300311
312 device_lock(&dev->dev);
313
314 if (!device_is_registered(&dev->dev)) {
315 rc = -ENODEV;
316 goto error;
317 }
318
Eric Lapuyade90099432012-05-07 12:31:13 +0200319 if (dev->active_target) {
320 rc = -EBUSY;
321 goto error;
322 }
323
324 target = nfc_find_target(dev, target_idx);
325 if (target == NULL) {
326 rc = -ENOTCONN;
327 goto error;
328 }
329
330 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200331 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200332 dev->active_target = target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300333
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200334 if (dev->ops->check_presence)
335 mod_timer(&dev->check_pres_timer, jiffies +
336 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
337 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300338
339error:
340 device_unlock(&dev->dev);
341 return rc;
342}
343
344/**
345 * nfc_deactivate_target - deactivate a nfc target
346 *
347 * @dev: The nfc device that found the target
348 * @target_idx: index of the target that must be deactivated
349 */
350int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
351{
352 int rc = 0;
353
Joe Perches20c239c2011-11-29 11:37:33 -0800354 pr_debug("dev_name=%s target_idx=%u\n",
355 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300356
357 device_lock(&dev->dev);
358
359 if (!device_is_registered(&dev->dev)) {
360 rc = -ENODEV;
361 goto error;
362 }
363
Eric Lapuyade90099432012-05-07 12:31:13 +0200364 if (dev->active_target == NULL) {
365 rc = -ENOTCONN;
366 goto error;
367 }
368
369 if (dev->active_target->idx != target_idx) {
370 rc = -ENOTCONN;
371 goto error;
372 }
373
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200374 if (dev->ops->check_presence)
375 del_timer_sync(&dev->check_pres_timer);
376
Eric Lapuyade90099432012-05-07 12:31:13 +0200377 dev->ops->deactivate_target(dev, dev->active_target);
378 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300379
380error:
381 device_unlock(&dev->dev);
382 return rc;
383}
384
385/**
386 * nfc_data_exchange - transceive data
387 *
388 * @dev: The nfc device that found the target
389 * @target_idx: index of the target
390 * @skb: data to be sent
391 * @cb: callback called when the response is received
392 * @cb_context: parameter for the callback function
393 *
394 * The user must wait for the callback before calling this function again.
395 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100396int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
397 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300398{
399 int rc;
400
Joe Perches20c239c2011-11-29 11:37:33 -0800401 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
402 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300403
404 device_lock(&dev->dev);
405
406 if (!device_is_registered(&dev->dev)) {
407 rc = -ENODEV;
408 kfree_skb(skb);
409 goto error;
410 }
411
Eric Lapuyade90099432012-05-07 12:31:13 +0200412 if (dev->active_target == NULL) {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200413 rc = -ENOTCONN;
414 kfree_skb(skb);
415 goto error;
416 }
417
Eric Lapuyade90099432012-05-07 12:31:13 +0200418 if (dev->active_target->idx != target_idx) {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200419 rc = -EADDRNOTAVAIL;
420 kfree_skb(skb);
421 goto error;
422 }
423
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200424 if (dev->ops->check_presence)
425 del_timer_sync(&dev->check_pres_timer);
426
Eric Lapuyade90099432012-05-07 12:31:13 +0200427 rc = dev->ops->data_exchange(dev, dev->active_target, skb, cb,
428 cb_context);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300429
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200430 if (!rc && dev->ops->check_presence)
431 mod_timer(&dev->check_pres_timer, jiffies +
432 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
433
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300434error:
435 device_unlock(&dev->dev);
436 return rc;
437}
438
Samuel Ortiz541d9202011-12-14 16:43:10 +0100439int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
440{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100441 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100442
443 if (gb_len > NFC_MAX_GT_LEN)
444 return -EINVAL;
445
Samuel Ortizd6469602011-12-14 16:43:12 +0100446 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100447}
448EXPORT_SYMBOL(nfc_set_remote_general_bytes);
449
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300450/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100451 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300452 *
453 * @size: size to allocate
454 * @gfp: gfp flags
455 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100456struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100457 unsigned int flags, unsigned int size,
458 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100459{
460 struct sk_buff *skb;
461 unsigned int total_size;
462
463 total_size = size +
464 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
465
466 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
467 if (skb)
468 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
469
470 return skb;
471}
472
473/**
474 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
475 *
476 * @size: size to allocate
477 * @gfp: gfp flags
478 */
479struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300480{
481 struct sk_buff *skb;
482 unsigned int total_size;
483
484 total_size = size + 1;
485 skb = alloc_skb(total_size, gfp);
486
487 if (skb)
488 skb_reserve(skb, 1);
489
490 return skb;
491}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100492EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300493
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300494/**
495 * nfc_targets_found - inform that targets were found
496 *
497 * @dev: The nfc device that found the targets
498 * @targets: array of nfc targets found
499 * @ntargets: targets array size
500 *
501 * The device driver must call this function when one or many nfc targets
502 * are found. After calling this function, the device driver must stop
503 * polling for targets.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200504 * IMPORTANT: this function must not be called from an atomic context.
505 * In addition, it must also not be called from a context that would prevent
506 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300507 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100508int nfc_targets_found(struct nfc_dev *dev,
509 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300510{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200511 int i;
512
Joe Perches20c239c2011-11-29 11:37:33 -0800513 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300514
515 dev->polling = false;
516
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200517 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200518 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200519
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200520 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300521
522 dev->targets_generation++;
523
524 kfree(dev->targets);
525 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100526 GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300527
528 if (!dev->targets) {
529 dev->n_targets = 0;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200530 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300531 return -ENOMEM;
532 }
533
534 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200535 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300536
537 nfc_genl_targets_found(dev);
538
539 return 0;
540}
541EXPORT_SYMBOL(nfc_targets_found);
542
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200543/**
544 * nfc_target_lost - inform that an activated target went out of field
545 *
546 * @dev: The nfc device that had the activated target in field
547 * @target_idx: the nfc index of the target
548 *
549 * The device driver must call this function when the activated target
550 * goes out of the field.
551 * IMPORTANT: this function must not be called from an atomic context.
552 * In addition, it must also not be called from a context that would prevent
553 * the NFC Core to call other nfc ops entry point concurrently.
554 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200555int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
556{
557 struct nfc_target *tg;
558 int i;
559
560 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
561
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200562 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200563
564 for (i = 0; i < dev->n_targets; i++) {
565 tg = &dev->targets[i];
566 if (tg->idx == target_idx)
567 break;
568 }
569
570 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200571 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200572 return -EINVAL;
573 }
574
575 dev->targets_generation++;
576 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200577 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200578
579 if (dev->n_targets) {
580 memcpy(&dev->targets[i], &dev->targets[i + 1],
581 (dev->n_targets - i) * sizeof(struct nfc_target));
582 } else {
583 kfree(dev->targets);
584 dev->targets = NULL;
585 }
586
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200587 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200588
589 nfc_genl_target_lost(dev, target_idx);
590
591 return 0;
592}
593EXPORT_SYMBOL(nfc_target_lost);
594
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300595static void nfc_release(struct device *d)
596{
597 struct nfc_dev *dev = to_nfc_dev(d);
598
Joe Perches20c239c2011-11-29 11:37:33 -0800599 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300600
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200601 if (dev->ops->check_presence) {
602 del_timer_sync(&dev->check_pres_timer);
603 destroy_workqueue(dev->check_pres_wq);
604 }
605
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300606 nfc_genl_data_exit(&dev->genl_data);
607 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300608 kfree(dev);
609}
610
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200611static void nfc_check_pres_work(struct work_struct *work)
612{
613 struct nfc_dev *dev = container_of(work, struct nfc_dev,
614 check_pres_work);
615 int rc;
616
617 device_lock(&dev->dev);
618
Eric Lapuyade90099432012-05-07 12:31:13 +0200619 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
620 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200621 if (!rc) {
622 mod_timer(&dev->check_pres_timer, jiffies +
623 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
624 } else {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200625 u32 active_target_idx = dev->active_target->idx;
626 device_unlock(&dev->dev);
627 nfc_target_lost(dev, active_target_idx);
628 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200629 }
630 }
631
632 device_unlock(&dev->dev);
633}
634
635static void nfc_check_pres_timeout(unsigned long data)
636{
637 struct nfc_dev *dev = (struct nfc_dev *)data;
638
639 queue_work(dev->check_pres_wq, &dev->check_pres_work);
640}
641
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300642struct class nfc_class = {
643 .name = "nfc",
644 .dev_release = nfc_release,
645};
646EXPORT_SYMBOL(nfc_class);
647
648static int match_idx(struct device *d, void *data)
649{
650 struct nfc_dev *dev = to_nfc_dev(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000651 unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300652
653 return dev->idx == *idx;
654}
655
Eric Dumazet95c96172012-04-15 05:58:06 +0000656struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300657{
658 struct device *d;
659
660 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
661 if (!d)
662 return NULL;
663
664 return to_nfc_dev(d);
665}
666
667/**
668 * nfc_allocate_device - allocate a new nfc device
669 *
670 * @ops: device operations
671 * @supported_protocols: NFC protocols supported by the device
672 */
673struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100674 u32 supported_protocols,
675 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300676{
677 static atomic_t dev_no = ATOMIC_INIT(0);
678 struct nfc_dev *dev;
679
680 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100681 !ops->deactivate_target || !ops->data_exchange)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300682 return NULL;
683
684 if (!supported_protocols)
685 return NULL;
686
687 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
688 if (!dev)
689 return NULL;
690
691 dev->dev.class = &nfc_class;
692 dev->idx = atomic_inc_return(&dev_no) - 1;
693 dev_set_name(&dev->dev, "nfc%d", dev->idx);
694 device_initialize(&dev->dev);
695
696 dev->ops = ops;
697 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200698 dev->tx_headroom = tx_headroom;
699 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300700
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300701 nfc_genl_data_init(&dev->genl_data);
702
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200703
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300704 /* first generation must not be 0 */
705 dev->targets_generation = 1;
706
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200707 if (ops->check_presence) {
708 char name[32];
709 init_timer(&dev->check_pres_timer);
710 dev->check_pres_timer.data = (unsigned long)dev;
711 dev->check_pres_timer.function = nfc_check_pres_timeout;
712
713 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
714 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
715 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
716 WQ_UNBOUND |
717 WQ_MEM_RECLAIM, 1);
718 if (dev->check_pres_wq == NULL) {
719 kfree(dev);
720 return NULL;
721 }
722 }
723
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300724 return dev;
725}
726EXPORT_SYMBOL(nfc_allocate_device);
727
728/**
729 * nfc_register_device - register a nfc device in the nfc subsystem
730 *
731 * @dev: The nfc device to register
732 */
733int nfc_register_device(struct nfc_dev *dev)
734{
735 int rc;
736
Joe Perches20c239c2011-11-29 11:37:33 -0800737 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300738
739 mutex_lock(&nfc_devlist_mutex);
740 nfc_devlist_generation++;
741 rc = device_add(&dev->dev);
742 mutex_unlock(&nfc_devlist_mutex);
743
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300744 if (rc < 0)
745 return rc;
746
Samuel Ortizd6469602011-12-14 16:43:12 +0100747 rc = nfc_llcp_register_device(dev);
748 if (rc)
749 pr_err("Could not register llcp device\n");
750
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300751 rc = nfc_genl_device_added(dev);
752 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800753 pr_debug("The userspace won't be notified that the device %s was added\n",
754 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300755
756 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300757}
758EXPORT_SYMBOL(nfc_register_device);
759
760/**
761 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
762 *
763 * @dev: The nfc device to unregister
764 */
765void nfc_unregister_device(struct nfc_dev *dev)
766{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300767 int rc;
768
Joe Perches20c239c2011-11-29 11:37:33 -0800769 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300770
771 mutex_lock(&nfc_devlist_mutex);
772 nfc_devlist_generation++;
773
774 /* lock to avoid unregistering a device while an operation
775 is in progress */
776 device_lock(&dev->dev);
777 device_del(&dev->dev);
778 device_unlock(&dev->dev);
779
780 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781
Samuel Ortizd6469602011-12-14 16:43:12 +0100782 nfc_llcp_unregister_device(dev);
783
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300784 rc = nfc_genl_device_removed(dev);
785 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800786 pr_debug("The userspace won't be notified that the device %s was removed\n",
787 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300788
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300789}
790EXPORT_SYMBOL(nfc_unregister_device);
791
792static int __init nfc_init(void)
793{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794 int rc;
795
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800796 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300797
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300798 rc = class_register(&nfc_class);
799 if (rc)
800 return rc;
801
802 rc = nfc_genl_init();
803 if (rc)
804 goto err_genl;
805
806 /* the first generation must not be 0 */
807 nfc_devlist_generation = 1;
808
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300809 rc = rawsock_init();
810 if (rc)
811 goto err_rawsock;
812
Samuel Ortizd6469602011-12-14 16:43:12 +0100813 rc = nfc_llcp_init();
814 if (rc)
815 goto err_llcp_sock;
816
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300817 rc = af_nfc_init();
818 if (rc)
819 goto err_af_nfc;
820
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300821 return 0;
822
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300823err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100824 nfc_llcp_exit();
825err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300826 rawsock_exit();
827err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300828 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300829err_genl:
830 class_unregister(&nfc_class);
831 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300832}
833
834static void __exit nfc_exit(void)
835{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300836 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100837 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300838 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300839 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300840 class_unregister(&nfc_class);
841}
842
843subsys_initcall(nfc_init);
844module_exit(nfc_exit);
845
846MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
847MODULE_DESCRIPTION("NFC Core ver " VERSION);
848MODULE_VERSION(VERSION);
849MODULE_LICENSE("GPL");