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 ": %s: " fmt, __func__
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/nfc.h>
36 int nfc_devlist_generation
;
37 DEFINE_MUTEX(nfc_devlist_mutex
);
40 * nfc_dev_up - turn on the NFC device
42 * @dev: The nfc device to be turned on
44 * The device remains up until the nfc_dev_down function is called.
46 int nfc_dev_up(struct nfc_dev
*dev
)
50 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
52 device_lock(&dev
->dev
);
54 if (!device_is_registered(&dev
->dev
)) {
65 rc
= dev
->ops
->dev_up(dev
);
71 device_unlock(&dev
->dev
);
76 * nfc_dev_down - turn off the NFC device
78 * @dev: The nfc device to be turned off
80 int nfc_dev_down(struct nfc_dev
*dev
)
84 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
86 device_lock(&dev
->dev
);
88 if (!device_is_registered(&dev
->dev
)) {
98 if (dev
->polling
|| dev
->remote_activated
) {
103 if (dev
->ops
->dev_down
)
104 dev
->ops
->dev_down(dev
);
109 device_unlock(&dev
->dev
);
114 * nfc_start_poll - start polling for nfc targets
116 * @dev: The nfc device that must start polling
117 * @protocols: bitset of nfc protocols that must be used for polling
119 * The device remains polling for targets until a target is found or
120 * the nfc_stop_poll function is called.
122 int nfc_start_poll(struct nfc_dev
*dev
, u32 protocols
)
126 pr_debug("dev_name=%s protocols=0x%x\n",
127 dev_name(&dev
->dev
), protocols
);
132 device_lock(&dev
->dev
);
134 if (!device_is_registered(&dev
->dev
)) {
144 rc
= dev
->ops
->start_poll(dev
, protocols
);
149 device_unlock(&dev
->dev
);
154 * nfc_stop_poll - stop polling for nfc targets
156 * @dev: The nfc device that must stop polling
158 int nfc_stop_poll(struct nfc_dev
*dev
)
162 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
164 device_lock(&dev
->dev
);
166 if (!device_is_registered(&dev
->dev
)) {
176 dev
->ops
->stop_poll(dev
);
177 dev
->polling
= false;
180 device_unlock(&dev
->dev
);
184 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
190 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
192 if (!dev
->ops
->dep_link_up
)
195 device_lock(&dev
->dev
);
197 if (!device_is_registered(&dev
->dev
)) {
202 if (dev
->dep_link_up
== true) {
207 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
208 if (gb_len
> NFC_MAX_GT_LEN
) {
213 rc
= dev
->ops
->dep_link_up(dev
, target_index
, comm_mode
, gb
, gb_len
);
216 device_unlock(&dev
->dev
);
220 int nfc_dep_link_down(struct nfc_dev
*dev
)
224 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
226 if (!dev
->ops
->dep_link_down
)
229 device_lock(&dev
->dev
);
231 if (!device_is_registered(&dev
->dev
)) {
236 if (dev
->dep_link_up
== false) {
241 if (dev
->dep_rf_mode
== NFC_RF_TARGET
) {
246 rc
= dev
->ops
->dep_link_down(dev
);
248 dev
->dep_link_up
= false;
249 nfc_llcp_mac_is_down(dev
);
250 nfc_genl_dep_link_down_event(dev
);
254 device_unlock(&dev
->dev
);
258 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
259 u8 comm_mode
, u8 rf_mode
)
261 dev
->dep_link_up
= true;
262 dev
->dep_rf_mode
= rf_mode
;
264 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
266 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
268 EXPORT_SYMBOL(nfc_dep_link_is_up
);
271 * nfc_activate_target - prepare the target for data exchange
273 * @dev: The nfc device that found the target
274 * @target_idx: index of the target that must be activated
275 * @protocol: nfc protocol that will be used for data exchange
277 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
281 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
282 dev_name(&dev
->dev
), target_idx
, protocol
);
284 device_lock(&dev
->dev
);
286 if (!device_is_registered(&dev
->dev
)) {
291 rc
= dev
->ops
->activate_target(dev
, target_idx
, protocol
);
293 dev
->remote_activated
= true;
296 device_unlock(&dev
->dev
);
301 * nfc_deactivate_target - deactivate a nfc target
303 * @dev: The nfc device that found the target
304 * @target_idx: index of the target that must be deactivated
306 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
310 pr_debug("dev_name=%s target_idx=%u\n",
311 dev_name(&dev
->dev
), target_idx
);
313 device_lock(&dev
->dev
);
315 if (!device_is_registered(&dev
->dev
)) {
320 dev
->ops
->deactivate_target(dev
, target_idx
);
321 dev
->remote_activated
= false;
324 device_unlock(&dev
->dev
);
329 * nfc_data_exchange - transceive data
331 * @dev: The nfc device that found the target
332 * @target_idx: index of the target
333 * @skb: data to be sent
334 * @cb: callback called when the response is received
335 * @cb_context: parameter for the callback function
337 * The user must wait for the callback before calling this function again.
339 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
,
341 data_exchange_cb_t cb
,
346 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
347 dev_name(&dev
->dev
), target_idx
, skb
->len
);
349 device_lock(&dev
->dev
);
351 if (!device_is_registered(&dev
->dev
)) {
357 rc
= dev
->ops
->data_exchange(dev
, target_idx
, skb
, cb
, cb_context
);
360 device_unlock(&dev
->dev
);
364 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
366 pr_debug("dev_name=%s gb_len=%d\n",
367 dev_name(&dev
->dev
), gb_len
);
369 if (gb_len
> NFC_MAX_GT_LEN
)
372 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
374 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
377 * nfc_alloc_send_skb - allocate a skb for data exchange responses
379 * @size: size to allocate
382 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
383 unsigned int flags
, unsigned int size
,
387 unsigned int total_size
;
390 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
392 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
394 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
400 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
402 * @size: size to allocate
405 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
408 unsigned int total_size
;
410 total_size
= size
+ 1;
411 skb
= alloc_skb(total_size
, gfp
);
418 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
421 * nfc_targets_found - inform that targets were found
423 * @dev: The nfc device that found the targets
424 * @targets: array of nfc targets found
425 * @ntargets: targets array size
427 * The device driver must call this function when one or many nfc targets
428 * are found. After calling this function, the device driver must stop
429 * polling for targets.
431 int nfc_targets_found(struct nfc_dev
*dev
, struct nfc_target
*targets
,
434 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
436 dev
->polling
= false;
438 spin_lock_bh(&dev
->targets_lock
);
440 dev
->targets_generation
++;
443 dev
->targets
= kmemdup(targets
, n_targets
* sizeof(struct nfc_target
),
448 spin_unlock_bh(&dev
->targets_lock
);
452 dev
->n_targets
= n_targets
;
453 spin_unlock_bh(&dev
->targets_lock
);
455 nfc_genl_targets_found(dev
);
459 EXPORT_SYMBOL(nfc_targets_found
);
461 static void nfc_release(struct device
*d
)
463 struct nfc_dev
*dev
= to_nfc_dev(d
);
465 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
467 nfc_genl_data_exit(&dev
->genl_data
);
472 struct class nfc_class
= {
474 .dev_release
= nfc_release
,
476 EXPORT_SYMBOL(nfc_class
);
478 static int match_idx(struct device
*d
, void *data
)
480 struct nfc_dev
*dev
= to_nfc_dev(d
);
481 unsigned *idx
= data
;
483 return dev
->idx
== *idx
;
486 struct nfc_dev
*nfc_get_device(unsigned idx
)
490 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
494 return to_nfc_dev(d
);
498 * nfc_allocate_device - allocate a new nfc device
500 * @ops: device operations
501 * @supported_protocols: NFC protocols supported by the device
503 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
504 u32 supported_protocols
,
508 static atomic_t dev_no
= ATOMIC_INIT(0);
511 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
512 !ops
->deactivate_target
|| !ops
->data_exchange
)
515 if (!supported_protocols
)
518 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
522 dev
->dev
.class = &nfc_class
;
523 dev
->idx
= atomic_inc_return(&dev_no
) - 1;
524 dev_set_name(&dev
->dev
, "nfc%d", dev
->idx
);
525 device_initialize(&dev
->dev
);
528 dev
->supported_protocols
= supported_protocols
;
529 dev
->tx_headroom
= tx_headroom
;
530 dev
->tx_tailroom
= tx_tailroom
;
532 spin_lock_init(&dev
->targets_lock
);
533 nfc_genl_data_init(&dev
->genl_data
);
535 /* first generation must not be 0 */
536 dev
->targets_generation
= 1;
540 EXPORT_SYMBOL(nfc_allocate_device
);
543 * nfc_register_device - register a nfc device in the nfc subsystem
545 * @dev: The nfc device to register
547 int nfc_register_device(struct nfc_dev
*dev
)
551 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
553 mutex_lock(&nfc_devlist_mutex
);
554 nfc_devlist_generation
++;
555 rc
= device_add(&dev
->dev
);
556 mutex_unlock(&nfc_devlist_mutex
);
561 rc
= nfc_llcp_register_device(dev
);
563 pr_err("Could not register llcp device\n");
565 rc
= nfc_genl_device_added(dev
);
567 pr_debug("The userspace won't be notified that the device %s was added\n",
568 dev_name(&dev
->dev
));
572 EXPORT_SYMBOL(nfc_register_device
);
575 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
577 * @dev: The nfc device to unregister
579 void nfc_unregister_device(struct nfc_dev
*dev
)
583 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
585 mutex_lock(&nfc_devlist_mutex
);
586 nfc_devlist_generation
++;
588 /* lock to avoid unregistering a device while an operation
590 device_lock(&dev
->dev
);
591 device_del(&dev
->dev
);
592 device_unlock(&dev
->dev
);
594 mutex_unlock(&nfc_devlist_mutex
);
596 nfc_llcp_unregister_device(dev
);
598 rc
= nfc_genl_device_removed(dev
);
600 pr_debug("The userspace won't be notified that the device %s was removed\n",
601 dev_name(&dev
->dev
));
604 EXPORT_SYMBOL(nfc_unregister_device
);
606 static int __init
nfc_init(void)
610 pr_info("NFC Core ver %s\n", VERSION
);
612 rc
= class_register(&nfc_class
);
616 rc
= nfc_genl_init();
620 /* the first generation must not be 0 */
621 nfc_devlist_generation
= 1;
627 rc
= nfc_llcp_init();
644 class_unregister(&nfc_class
);
648 static void __exit
nfc_exit(void)
654 class_unregister(&nfc_class
);
657 subsys_initcall(nfc_init
);
658 module_exit(nfc_exit
);
660 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
661 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
662 MODULE_VERSION(VERSION
);
663 MODULE_LICENSE("GPL");