blob: 3192c3f589eea87eb5f6e04f4803b584c36101fb [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 Lapuyade144612c2012-04-10 19:43:11 +0200100 if (dev->polling || dev->activated_target_idx != NFC_TARGET_IDX_NONE) {
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
Samuel Ortiz47807d32012-03-05 01:03:50 +0100186int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100187{
188 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100189 u8 *gb;
190 size_t gb_len;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100191
Samuel Ortiz47807d32012-03-05 01:03:50 +0100192 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100193
194 if (!dev->ops->dep_link_up)
195 return -EOPNOTSUPP;
196
197 device_lock(&dev->dev);
198
199 if (!device_is_registered(&dev->dev)) {
200 rc = -ENODEV;
201 goto error;
202 }
203
204 if (dev->dep_link_up == true) {
205 rc = -EALREADY;
206 goto error;
207 }
208
Samuel Ortiz47807d32012-03-05 01:03:50 +0100209 gb = nfc_llcp_general_bytes(dev, &gb_len);
210 if (gb_len > NFC_MAX_GT_LEN) {
211 rc = -EINVAL;
212 goto error;
213 }
214
215 rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len);
Eric Lapuyade144612c2012-04-10 19:43:11 +0200216 if (!rc)
217 dev->activated_target_idx = target_index;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100218
219error:
220 device_unlock(&dev->dev);
221 return rc;
222}
223
224int nfc_dep_link_down(struct nfc_dev *dev)
225{
226 int rc = 0;
227
228 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
229
230 if (!dev->ops->dep_link_down)
231 return -EOPNOTSUPP;
232
233 device_lock(&dev->dev);
234
235 if (!device_is_registered(&dev->dev)) {
236 rc = -ENODEV;
237 goto error;
238 }
239
240 if (dev->dep_link_up == false) {
241 rc = -EALREADY;
242 goto error;
243 }
244
245 if (dev->dep_rf_mode == NFC_RF_TARGET) {
246 rc = -EOPNOTSUPP;
247 goto error;
248 }
249
250 rc = dev->ops->dep_link_down(dev);
251 if (!rc) {
252 dev->dep_link_up = false;
Eric Lapuyade144612c2012-04-10 19:43:11 +0200253 dev->activated_target_idx = NFC_TARGET_IDX_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100254 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100255 nfc_genl_dep_link_down_event(dev);
256 }
257
258error:
259 device_unlock(&dev->dev);
260 return rc;
261}
262
263int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100264 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100265{
266 dev->dep_link_up = true;
267 dev->dep_rf_mode = rf_mode;
268
Samuel Ortizd6469602011-12-14 16:43:12 +0100269 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
270
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100271 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
272}
273EXPORT_SYMBOL(nfc_dep_link_is_up);
274
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300275/**
276 * nfc_activate_target - prepare the target for data exchange
277 *
278 * @dev: The nfc device that found the target
279 * @target_idx: index of the target that must be activated
280 * @protocol: nfc protocol that will be used for data exchange
281 */
282int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
283{
284 int rc;
285
Joe Perches20c239c2011-11-29 11:37:33 -0800286 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
287 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300288
289 device_lock(&dev->dev);
290
291 if (!device_is_registered(&dev->dev)) {
292 rc = -ENODEV;
293 goto error;
294 }
295
296 rc = dev->ops->activate_target(dev, target_idx, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200297 if (!rc) {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200298 dev->activated_target_idx = target_idx;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300299
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200300 if (dev->ops->check_presence)
301 mod_timer(&dev->check_pres_timer, jiffies +
302 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
303 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300304
305error:
306 device_unlock(&dev->dev);
307 return rc;
308}
309
310/**
311 * nfc_deactivate_target - deactivate a nfc target
312 *
313 * @dev: The nfc device that found the target
314 * @target_idx: index of the target that must be deactivated
315 */
316int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
317{
318 int rc = 0;
319
Joe Perches20c239c2011-11-29 11:37:33 -0800320 pr_debug("dev_name=%s target_idx=%u\n",
321 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300322
323 device_lock(&dev->dev);
324
325 if (!device_is_registered(&dev->dev)) {
326 rc = -ENODEV;
327 goto error;
328 }
329
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200330 if (dev->ops->check_presence)
331 del_timer_sync(&dev->check_pres_timer);
332
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300333 dev->ops->deactivate_target(dev, target_idx);
Eric Lapuyade144612c2012-04-10 19:43:11 +0200334 dev->activated_target_idx = NFC_TARGET_IDX_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300335
336error:
337 device_unlock(&dev->dev);
338 return rc;
339}
340
341/**
342 * nfc_data_exchange - transceive data
343 *
344 * @dev: The nfc device that found the target
345 * @target_idx: index of the target
346 * @skb: data to be sent
347 * @cb: callback called when the response is received
348 * @cb_context: parameter for the callback function
349 *
350 * The user must wait for the callback before calling this function again.
351 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100352int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
353 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300354{
355 int rc;
356
Joe Perches20c239c2011-11-29 11:37:33 -0800357 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
358 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300359
360 device_lock(&dev->dev);
361
362 if (!device_is_registered(&dev->dev)) {
363 rc = -ENODEV;
364 kfree_skb(skb);
365 goto error;
366 }
367
Eric Lapuyade144612c2012-04-10 19:43:11 +0200368 if (dev->activated_target_idx == NFC_TARGET_IDX_NONE) {
369 rc = -ENOTCONN;
370 kfree_skb(skb);
371 goto error;
372 }
373
374 if (target_idx != dev->activated_target_idx) {
375 rc = -EADDRNOTAVAIL;
376 kfree_skb(skb);
377 goto error;
378 }
379
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200380 if (dev->ops->check_presence)
381 del_timer_sync(&dev->check_pres_timer);
382
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300383 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
384
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200385 if (!rc && dev->ops->check_presence)
386 mod_timer(&dev->check_pres_timer, jiffies +
387 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
388
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300389error:
390 device_unlock(&dev->dev);
391 return rc;
392}
393
Samuel Ortiz541d9202011-12-14 16:43:10 +0100394int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
395{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100396 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100397
398 if (gb_len > NFC_MAX_GT_LEN)
399 return -EINVAL;
400
Samuel Ortizd6469602011-12-14 16:43:12 +0100401 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100402}
403EXPORT_SYMBOL(nfc_set_remote_general_bytes);
404
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300405/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100406 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300407 *
408 * @size: size to allocate
409 * @gfp: gfp flags
410 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100411struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100412 unsigned int flags, unsigned int size,
413 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100414{
415 struct sk_buff *skb;
416 unsigned int total_size;
417
418 total_size = size +
419 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
420
421 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
422 if (skb)
423 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
424
425 return skb;
426}
427
428/**
429 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
430 *
431 * @size: size to allocate
432 * @gfp: gfp flags
433 */
434struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300435{
436 struct sk_buff *skb;
437 unsigned int total_size;
438
439 total_size = size + 1;
440 skb = alloc_skb(total_size, gfp);
441
442 if (skb)
443 skb_reserve(skb, 1);
444
445 return skb;
446}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100447EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300448
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300449/**
450 * nfc_targets_found - inform that targets were found
451 *
452 * @dev: The nfc device that found the targets
453 * @targets: array of nfc targets found
454 * @ntargets: targets array size
455 *
456 * The device driver must call this function when one or many nfc targets
457 * are found. After calling this function, the device driver must stop
458 * polling for targets.
459 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100460int nfc_targets_found(struct nfc_dev *dev,
461 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300462{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200463 int i;
464
Joe Perches20c239c2011-11-29 11:37:33 -0800465 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300466
467 dev->polling = false;
468
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200469 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200470 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200471
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300472 spin_lock_bh(&dev->targets_lock);
473
474 dev->targets_generation++;
475
476 kfree(dev->targets);
477 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100478 GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300479
480 if (!dev->targets) {
481 dev->n_targets = 0;
482 spin_unlock_bh(&dev->targets_lock);
483 return -ENOMEM;
484 }
485
486 dev->n_targets = n_targets;
487 spin_unlock_bh(&dev->targets_lock);
488
489 nfc_genl_targets_found(dev);
490
491 return 0;
492}
493EXPORT_SYMBOL(nfc_targets_found);
494
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200495int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
496{
497 struct nfc_target *tg;
498 int i;
499
500 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
501
502 spin_lock_bh(&dev->targets_lock);
503
504 for (i = 0; i < dev->n_targets; i++) {
505 tg = &dev->targets[i];
506 if (tg->idx == target_idx)
507 break;
508 }
509
510 if (i == dev->n_targets) {
511 spin_unlock_bh(&dev->targets_lock);
512 return -EINVAL;
513 }
514
515 dev->targets_generation++;
516 dev->n_targets--;
Eric Lapuyade144612c2012-04-10 19:43:11 +0200517 dev->activated_target_idx = NFC_TARGET_IDX_NONE;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200518
519 if (dev->n_targets) {
520 memcpy(&dev->targets[i], &dev->targets[i + 1],
521 (dev->n_targets - i) * sizeof(struct nfc_target));
522 } else {
523 kfree(dev->targets);
524 dev->targets = NULL;
525 }
526
527 spin_unlock_bh(&dev->targets_lock);
528
529 nfc_genl_target_lost(dev, target_idx);
530
531 return 0;
532}
533EXPORT_SYMBOL(nfc_target_lost);
534
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300535static void nfc_release(struct device *d)
536{
537 struct nfc_dev *dev = to_nfc_dev(d);
538
Joe Perches20c239c2011-11-29 11:37:33 -0800539 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300540
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200541 if (dev->ops->check_presence) {
542 del_timer_sync(&dev->check_pres_timer);
543 destroy_workqueue(dev->check_pres_wq);
544 }
545
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300546 nfc_genl_data_exit(&dev->genl_data);
547 kfree(dev->targets);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300548 kfree(dev);
549}
550
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200551static void nfc_check_pres_work(struct work_struct *work)
552{
553 struct nfc_dev *dev = container_of(work, struct nfc_dev,
554 check_pres_work);
555 int rc;
556
557 device_lock(&dev->dev);
558
559 if (dev->activated_target_idx != NFC_TARGET_IDX_NONE &&
560 timer_pending(&dev->check_pres_timer) == 0) {
561 rc = dev->ops->check_presence(dev, dev->activated_target_idx);
562 if (!rc) {
563 mod_timer(&dev->check_pres_timer, jiffies +
564 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
565 } else {
566 nfc_target_lost(dev, dev->activated_target_idx);
567 dev->activated_target_idx = NFC_TARGET_IDX_NONE;
568 }
569 }
570
571 device_unlock(&dev->dev);
572}
573
574static void nfc_check_pres_timeout(unsigned long data)
575{
576 struct nfc_dev *dev = (struct nfc_dev *)data;
577
578 queue_work(dev->check_pres_wq, &dev->check_pres_work);
579}
580
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300581struct class nfc_class = {
582 .name = "nfc",
583 .dev_release = nfc_release,
584};
585EXPORT_SYMBOL(nfc_class);
586
587static int match_idx(struct device *d, void *data)
588{
589 struct nfc_dev *dev = to_nfc_dev(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000590 unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300591
592 return dev->idx == *idx;
593}
594
Eric Dumazet95c96172012-04-15 05:58:06 +0000595struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300596{
597 struct device *d;
598
599 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
600 if (!d)
601 return NULL;
602
603 return to_nfc_dev(d);
604}
605
606/**
607 * nfc_allocate_device - allocate a new nfc device
608 *
609 * @ops: device operations
610 * @supported_protocols: NFC protocols supported by the device
611 */
612struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100613 u32 supported_protocols,
614 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300615{
616 static atomic_t dev_no = ATOMIC_INIT(0);
617 struct nfc_dev *dev;
618
619 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100620 !ops->deactivate_target || !ops->data_exchange)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300621 return NULL;
622
623 if (!supported_protocols)
624 return NULL;
625
626 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
627 if (!dev)
628 return NULL;
629
630 dev->dev.class = &nfc_class;
631 dev->idx = atomic_inc_return(&dev_no) - 1;
632 dev_set_name(&dev->dev, "nfc%d", dev->idx);
633 device_initialize(&dev->dev);
634
635 dev->ops = ops;
636 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +0200637 dev->tx_headroom = tx_headroom;
638 dev->tx_tailroom = tx_tailroom;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300639
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300640 spin_lock_init(&dev->targets_lock);
641 nfc_genl_data_init(&dev->genl_data);
642
643 /* first generation must not be 0 */
644 dev->targets_generation = 1;
645
Eric Lapuyade144612c2012-04-10 19:43:11 +0200646 dev->activated_target_idx = NFC_TARGET_IDX_NONE;
647
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200648 if (ops->check_presence) {
649 char name[32];
650 init_timer(&dev->check_pres_timer);
651 dev->check_pres_timer.data = (unsigned long)dev;
652 dev->check_pres_timer.function = nfc_check_pres_timeout;
653
654 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
655 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
656 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
657 WQ_UNBOUND |
658 WQ_MEM_RECLAIM, 1);
659 if (dev->check_pres_wq == NULL) {
660 kfree(dev);
661 return NULL;
662 }
663 }
664
665
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300666 return dev;
667}
668EXPORT_SYMBOL(nfc_allocate_device);
669
670/**
671 * nfc_register_device - register a nfc device in the nfc subsystem
672 *
673 * @dev: The nfc device to register
674 */
675int nfc_register_device(struct nfc_dev *dev)
676{
677 int rc;
678
Joe Perches20c239c2011-11-29 11:37:33 -0800679 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300680
681 mutex_lock(&nfc_devlist_mutex);
682 nfc_devlist_generation++;
683 rc = device_add(&dev->dev);
684 mutex_unlock(&nfc_devlist_mutex);
685
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300686 if (rc < 0)
687 return rc;
688
Samuel Ortizd6469602011-12-14 16:43:12 +0100689 rc = nfc_llcp_register_device(dev);
690 if (rc)
691 pr_err("Could not register llcp device\n");
692
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300693 rc = nfc_genl_device_added(dev);
694 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800695 pr_debug("The userspace won't be notified that the device %s was added\n",
696 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300697
698 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300699}
700EXPORT_SYMBOL(nfc_register_device);
701
702/**
703 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
704 *
705 * @dev: The nfc device to unregister
706 */
707void nfc_unregister_device(struct nfc_dev *dev)
708{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300709 int rc;
710
Joe Perches20c239c2011-11-29 11:37:33 -0800711 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300712
713 mutex_lock(&nfc_devlist_mutex);
714 nfc_devlist_generation++;
715
716 /* lock to avoid unregistering a device while an operation
717 is in progress */
718 device_lock(&dev->dev);
719 device_del(&dev->dev);
720 device_unlock(&dev->dev);
721
722 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300723
Samuel Ortizd6469602011-12-14 16:43:12 +0100724 nfc_llcp_unregister_device(dev);
725
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300726 rc = nfc_genl_device_removed(dev);
727 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -0800728 pr_debug("The userspace won't be notified that the device %s was removed\n",
729 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300730
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300731}
732EXPORT_SYMBOL(nfc_unregister_device);
733
734static int __init nfc_init(void)
735{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300736 int rc;
737
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800738 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300739
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300740 rc = class_register(&nfc_class);
741 if (rc)
742 return rc;
743
744 rc = nfc_genl_init();
745 if (rc)
746 goto err_genl;
747
748 /* the first generation must not be 0 */
749 nfc_devlist_generation = 1;
750
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300751 rc = rawsock_init();
752 if (rc)
753 goto err_rawsock;
754
Samuel Ortizd6469602011-12-14 16:43:12 +0100755 rc = nfc_llcp_init();
756 if (rc)
757 goto err_llcp_sock;
758
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300759 rc = af_nfc_init();
760 if (rc)
761 goto err_af_nfc;
762
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300763 return 0;
764
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300765err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +0100766 nfc_llcp_exit();
767err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300768 rawsock_exit();
769err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300770 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300771err_genl:
772 class_unregister(&nfc_class);
773 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300774}
775
776static void __exit nfc_exit(void)
777{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -0300778 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +0100779 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -0300780 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300782 class_unregister(&nfc_class);
783}
784
785subsys_initcall(nfc_init);
786module_exit(nfc_exit);
787
788MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
789MODULE_DESCRIPTION("NFC Core ver " VERSION);
790MODULE_VERSION(VERSION);
791MODULE_LICENSE("GPL");