2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
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.
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.
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.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
35 int nfc_devlist_generation
;
36 DEFINE_MUTEX(nfc_devlist_mutex
);
39 * nfc_dev_up - turn on the NFC device
41 * @dev: The nfc device to be turned on
43 * The device remains up until the nfc_dev_down function is called.
45 int nfc_dev_up(struct nfc_dev
*dev
)
49 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
51 device_lock(&dev
->dev
);
53 if (!device_is_registered(&dev
->dev
)) {
64 rc
= dev
->ops
->dev_up(dev
);
70 device_unlock(&dev
->dev
);
75 * nfc_dev_down - turn off the NFC device
77 * @dev: The nfc device to be turned off
79 int nfc_dev_down(struct nfc_dev
*dev
)
83 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
85 device_lock(&dev
->dev
);
87 if (!device_is_registered(&dev
->dev
)) {
97 if (dev
->polling
|| dev
->remote_activated
) {
102 if (dev
->ops
->dev_down
)
103 dev
->ops
->dev_down(dev
);
108 device_unlock(&dev
->dev
);
113 * nfc_start_poll - start polling for nfc targets
115 * @dev: The nfc device that must start polling
116 * @protocols: bitset of nfc protocols that must be used for polling
118 * The device remains polling for targets until a target is found or
119 * the nfc_stop_poll function is called.
121 int nfc_start_poll(struct nfc_dev
*dev
, u32 protocols
)
125 pr_debug("dev_name=%s protocols=0x%x\n",
126 dev_name(&dev
->dev
), protocols
);
131 device_lock(&dev
->dev
);
133 if (!device_is_registered(&dev
->dev
)) {
143 rc
= dev
->ops
->start_poll(dev
, protocols
);
148 device_unlock(&dev
->dev
);
153 * nfc_stop_poll - stop polling for nfc targets
155 * @dev: The nfc device that must stop polling
157 int nfc_stop_poll(struct nfc_dev
*dev
)
161 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
163 device_lock(&dev
->dev
);
165 if (!device_is_registered(&dev
->dev
)) {
175 dev
->ops
->stop_poll(dev
);
176 dev
->polling
= false;
179 device_unlock(&dev
->dev
);
184 * nfc_activate_target - prepare the target for data exchange
186 * @dev: The nfc device that found the target
187 * @target_idx: index of the target that must be activated
188 * @protocol: nfc protocol that will be used for data exchange
190 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
194 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
195 dev_name(&dev
->dev
), target_idx
, protocol
);
197 device_lock(&dev
->dev
);
199 if (!device_is_registered(&dev
->dev
)) {
204 rc
= dev
->ops
->activate_target(dev
, target_idx
, protocol
);
206 dev
->remote_activated
= true;
209 device_unlock(&dev
->dev
);
214 * nfc_deactivate_target - deactivate a nfc target
216 * @dev: The nfc device that found the target
217 * @target_idx: index of the target that must be deactivated
219 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
223 pr_debug("dev_name=%s target_idx=%u\n",
224 dev_name(&dev
->dev
), target_idx
);
226 device_lock(&dev
->dev
);
228 if (!device_is_registered(&dev
->dev
)) {
233 dev
->ops
->deactivate_target(dev
, target_idx
);
234 dev
->remote_activated
= false;
237 device_unlock(&dev
->dev
);
242 * nfc_data_exchange - transceive data
244 * @dev: The nfc device that found the target
245 * @target_idx: index of the target
246 * @skb: data to be sent
247 * @cb: callback called when the response is received
248 * @cb_context: parameter for the callback function
250 * The user must wait for the callback before calling this function again.
252 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
,
254 data_exchange_cb_t cb
,
259 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
260 dev_name(&dev
->dev
), target_idx
, skb
->len
);
262 device_lock(&dev
->dev
);
264 if (!device_is_registered(&dev
->dev
)) {
270 rc
= dev
->ops
->data_exchange(dev
, target_idx
, skb
, cb
, cb_context
);
273 device_unlock(&dev
->dev
);
278 * nfc_alloc_skb - allocate a skb for data exchange responses
280 * @size: size to allocate
283 struct sk_buff
*nfc_alloc_skb(unsigned int size
, gfp_t gfp
)
286 unsigned int total_size
;
288 total_size
= size
+ 1;
289 skb
= alloc_skb(total_size
, gfp
);
296 EXPORT_SYMBOL(nfc_alloc_skb
);
299 * nfc_targets_found - inform that targets were found
301 * @dev: The nfc device that found the targets
302 * @targets: array of nfc targets found
303 * @ntargets: targets array size
305 * The device driver must call this function when one or many nfc targets
306 * are found. After calling this function, the device driver must stop
307 * polling for targets.
309 int nfc_targets_found(struct nfc_dev
*dev
, struct nfc_target
*targets
,
314 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
316 dev
->polling
= false;
318 for (i
= 0; i
< n_targets
; i
++)
319 targets
[i
].idx
= dev
->target_idx
++;
321 spin_lock_bh(&dev
->targets_lock
);
323 dev
->targets_generation
++;
326 dev
->targets
= kmemdup(targets
, n_targets
* sizeof(struct nfc_target
),
331 spin_unlock_bh(&dev
->targets_lock
);
335 dev
->n_targets
= n_targets
;
336 spin_unlock_bh(&dev
->targets_lock
);
338 nfc_genl_targets_found(dev
);
342 EXPORT_SYMBOL(nfc_targets_found
);
344 static void nfc_release(struct device
*d
)
346 struct nfc_dev
*dev
= to_nfc_dev(d
);
348 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
350 nfc_genl_data_exit(&dev
->genl_data
);
355 struct class nfc_class
= {
357 .dev_release
= nfc_release
,
359 EXPORT_SYMBOL(nfc_class
);
361 static int match_idx(struct device
*d
, void *data
)
363 struct nfc_dev
*dev
= to_nfc_dev(d
);
364 unsigned *idx
= data
;
366 return dev
->idx
== *idx
;
369 struct nfc_dev
*nfc_get_device(unsigned idx
)
373 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
377 return to_nfc_dev(d
);
381 * nfc_allocate_device - allocate a new nfc device
383 * @ops: device operations
384 * @supported_protocols: NFC protocols supported by the device
386 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
387 u32 supported_protocols
,
391 static atomic_t dev_no
= ATOMIC_INIT(0);
394 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
395 !ops
->deactivate_target
|| !ops
->data_exchange
)
398 if (!supported_protocols
)
401 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
405 dev
->dev
.class = &nfc_class
;
406 dev
->idx
= atomic_inc_return(&dev_no
) - 1;
407 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
408 device_initialize(&dev
->dev
);
411 dev
->supported_protocols
= supported_protocols
;
412 dev
->tx_headroom
= tx_headroom
;
413 dev
->tx_tailroom
= tx_tailroom
;
415 spin_lock_init(&dev
->targets_lock
);
416 nfc_genl_data_init(&dev
->genl_data
);
418 /* first generation must not be 0 */
419 dev
->targets_generation
= 1;
423 EXPORT_SYMBOL(nfc_allocate_device
);
426 * nfc_register_device - register a nfc device in the nfc subsystem
428 * @dev: The nfc device to register
430 int nfc_register_device(struct nfc_dev
*dev
)
434 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
436 mutex_lock(&nfc_devlist_mutex
);
437 nfc_devlist_generation
++;
438 rc
= device_add(&dev
->dev
);
439 mutex_unlock(&nfc_devlist_mutex
);
444 rc
= nfc_genl_device_added(dev
);
446 pr_debug("The userspace won't be notified that the device %s was added\n",
447 dev_name(&dev
->dev
));
451 EXPORT_SYMBOL(nfc_register_device
);
454 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
456 * @dev: The nfc device to unregister
458 void nfc_unregister_device(struct nfc_dev
*dev
)
462 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
464 mutex_lock(&nfc_devlist_mutex
);
465 nfc_devlist_generation
++;
467 /* lock to avoid unregistering a device while an operation
469 device_lock(&dev
->dev
);
470 device_del(&dev
->dev
);
471 device_unlock(&dev
->dev
);
473 mutex_unlock(&nfc_devlist_mutex
);
475 rc
= nfc_genl_device_removed(dev
);
477 pr_debug("The userspace won't be notified that the device %s was removed\n",
478 dev_name(&dev
->dev
));
481 EXPORT_SYMBOL(nfc_unregister_device
);
483 static int __init
nfc_init(void)
487 pr_info("NFC Core ver %s\n", VERSION
);
489 rc
= class_register(&nfc_class
);
493 rc
= nfc_genl_init();
497 /* the first generation must not be 0 */
498 nfc_devlist_generation
= 1;
515 class_unregister(&nfc_class
);
519 static void __exit
nfc_exit(void)
524 class_unregister(&nfc_class
);
527 subsys_initcall(nfc_init
);
528 module_exit(nfc_exit
);
530 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
531 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
532 MODULE_VERSION(VERSION
);
533 MODULE_LICENSE("GPL");