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 #define NFC_CHECK_PRES_FREQ_MS 2000
38 int nfc_devlist_generation
;
39 DEFINE_MUTEX(nfc_devlist_mutex
);
42 * nfc_dev_up - turn on the NFC device
44 * @dev: The nfc device to be turned on
46 * The device remains up until the nfc_dev_down function is called.
48 int nfc_dev_up(struct nfc_dev
*dev
)
52 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
54 device_lock(&dev
->dev
);
56 if (!device_is_registered(&dev
->dev
)) {
67 rc
= dev
->ops
->dev_up(dev
);
73 device_unlock(&dev
->dev
);
78 * nfc_dev_down - turn off the NFC device
80 * @dev: The nfc device to be turned off
82 int nfc_dev_down(struct nfc_dev
*dev
)
86 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
88 device_lock(&dev
->dev
);
90 if (!device_is_registered(&dev
->dev
)) {
100 if (dev
->polling
|| dev
->activated_target_idx
!= NFC_TARGET_IDX_NONE
) {
105 if (dev
->ops
->dev_down
)
106 dev
->ops
->dev_down(dev
);
111 device_unlock(&dev
->dev
);
116 * nfc_start_poll - start polling for nfc targets
118 * @dev: The nfc device that must start polling
119 * @protocols: bitset of nfc protocols that must be used for polling
121 * The device remains polling for targets until a target is found or
122 * the nfc_stop_poll function is called.
124 int nfc_start_poll(struct nfc_dev
*dev
, u32 protocols
)
128 pr_debug("dev_name=%s protocols=0x%x\n",
129 dev_name(&dev
->dev
), protocols
);
134 device_lock(&dev
->dev
);
136 if (!device_is_registered(&dev
->dev
)) {
146 rc
= dev
->ops
->start_poll(dev
, protocols
);
151 device_unlock(&dev
->dev
);
156 * nfc_stop_poll - stop polling for nfc targets
158 * @dev: The nfc device that must stop polling
160 int nfc_stop_poll(struct nfc_dev
*dev
)
164 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
166 device_lock(&dev
->dev
);
168 if (!device_is_registered(&dev
->dev
)) {
178 dev
->ops
->stop_poll(dev
);
179 dev
->polling
= false;
182 device_unlock(&dev
->dev
);
186 int nfc_dep_link_up(struct nfc_dev
*dev
, int target_index
, u8 comm_mode
)
192 pr_debug("dev_name=%s comm %d\n", dev_name(&dev
->dev
), comm_mode
);
194 if (!dev
->ops
->dep_link_up
)
197 device_lock(&dev
->dev
);
199 if (!device_is_registered(&dev
->dev
)) {
204 if (dev
->dep_link_up
== true) {
209 gb
= nfc_llcp_general_bytes(dev
, &gb_len
);
210 if (gb_len
> NFC_MAX_GT_LEN
) {
215 rc
= dev
->ops
->dep_link_up(dev
, target_index
, comm_mode
, gb
, gb_len
);
217 dev
->activated_target_idx
= target_index
;
220 device_unlock(&dev
->dev
);
224 int nfc_dep_link_down(struct nfc_dev
*dev
)
228 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
230 if (!dev
->ops
->dep_link_down
)
233 device_lock(&dev
->dev
);
235 if (!device_is_registered(&dev
->dev
)) {
240 if (dev
->dep_link_up
== false) {
245 if (dev
->dep_rf_mode
== NFC_RF_TARGET
) {
250 rc
= dev
->ops
->dep_link_down(dev
);
252 dev
->dep_link_up
= false;
253 dev
->activated_target_idx
= NFC_TARGET_IDX_NONE
;
254 nfc_llcp_mac_is_down(dev
);
255 nfc_genl_dep_link_down_event(dev
);
259 device_unlock(&dev
->dev
);
263 int nfc_dep_link_is_up(struct nfc_dev
*dev
, u32 target_idx
,
264 u8 comm_mode
, u8 rf_mode
)
266 dev
->dep_link_up
= true;
267 dev
->dep_rf_mode
= rf_mode
;
269 nfc_llcp_mac_is_up(dev
, target_idx
, comm_mode
, rf_mode
);
271 return nfc_genl_dep_link_up_event(dev
, target_idx
, comm_mode
, rf_mode
);
273 EXPORT_SYMBOL(nfc_dep_link_is_up
);
276 * nfc_activate_target - prepare the target for data exchange
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
282 int nfc_activate_target(struct nfc_dev
*dev
, u32 target_idx
, u32 protocol
)
286 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
287 dev_name(&dev
->dev
), target_idx
, protocol
);
289 device_lock(&dev
->dev
);
291 if (!device_is_registered(&dev
->dev
)) {
296 rc
= dev
->ops
->activate_target(dev
, target_idx
, protocol
);
298 dev
->activated_target_idx
= target_idx
;
300 if (dev
->ops
->check_presence
)
301 mod_timer(&dev
->check_pres_timer
, jiffies
+
302 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
306 device_unlock(&dev
->dev
);
311 * nfc_deactivate_target - deactivate a nfc target
313 * @dev: The nfc device that found the target
314 * @target_idx: index of the target that must be deactivated
316 int nfc_deactivate_target(struct nfc_dev
*dev
, u32 target_idx
)
320 pr_debug("dev_name=%s target_idx=%u\n",
321 dev_name(&dev
->dev
), target_idx
);
323 device_lock(&dev
->dev
);
325 if (!device_is_registered(&dev
->dev
)) {
330 if (dev
->ops
->check_presence
)
331 del_timer_sync(&dev
->check_pres_timer
);
333 dev
->ops
->deactivate_target(dev
, target_idx
);
334 dev
->activated_target_idx
= NFC_TARGET_IDX_NONE
;
337 device_unlock(&dev
->dev
);
342 * nfc_data_exchange - transceive data
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
350 * The user must wait for the callback before calling this function again.
352 int nfc_data_exchange(struct nfc_dev
*dev
, u32 target_idx
, struct sk_buff
*skb
,
353 data_exchange_cb_t cb
, void *cb_context
)
357 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
358 dev_name(&dev
->dev
), target_idx
, skb
->len
);
360 device_lock(&dev
->dev
);
362 if (!device_is_registered(&dev
->dev
)) {
368 if (dev
->activated_target_idx
== NFC_TARGET_IDX_NONE
) {
374 if (target_idx
!= dev
->activated_target_idx
) {
380 if (dev
->ops
->check_presence
)
381 del_timer_sync(&dev
->check_pres_timer
);
383 rc
= dev
->ops
->data_exchange(dev
, target_idx
, skb
, cb
, cb_context
);
385 if (!rc
&& dev
->ops
->check_presence
)
386 mod_timer(&dev
->check_pres_timer
, jiffies
+
387 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
390 device_unlock(&dev
->dev
);
394 int nfc_set_remote_general_bytes(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
396 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev
->dev
), gb_len
);
398 if (gb_len
> NFC_MAX_GT_LEN
)
401 return nfc_llcp_set_remote_gb(dev
, gb
, gb_len
);
403 EXPORT_SYMBOL(nfc_set_remote_general_bytes
);
406 * nfc_alloc_send_skb - allocate a skb for data exchange responses
408 * @size: size to allocate
411 struct sk_buff
*nfc_alloc_send_skb(struct nfc_dev
*dev
, struct sock
*sk
,
412 unsigned int flags
, unsigned int size
,
416 unsigned int total_size
;
419 dev
->tx_headroom
+ dev
->tx_tailroom
+ NFC_HEADER_SIZE
;
421 skb
= sock_alloc_send_skb(sk
, total_size
, flags
& MSG_DONTWAIT
, err
);
423 skb_reserve(skb
, dev
->tx_headroom
+ NFC_HEADER_SIZE
);
429 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
431 * @size: size to allocate
434 struct sk_buff
*nfc_alloc_recv_skb(unsigned int size
, gfp_t gfp
)
437 unsigned int total_size
;
439 total_size
= size
+ 1;
440 skb
= alloc_skb(total_size
, gfp
);
447 EXPORT_SYMBOL(nfc_alloc_recv_skb
);
450 * nfc_targets_found - inform that targets were found
452 * @dev: The nfc device that found the targets
453 * @targets: array of nfc targets found
454 * @ntargets: targets array size
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.
460 int nfc_targets_found(struct nfc_dev
*dev
,
461 struct nfc_target
*targets
, int n_targets
)
465 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev
->dev
), n_targets
);
467 dev
->polling
= false;
469 for (i
= 0; i
< n_targets
; i
++)
470 targets
[i
].idx
= dev
->target_next_idx
++;
472 spin_lock_bh(&dev
->targets_lock
);
474 dev
->targets_generation
++;
477 dev
->targets
= kmemdup(targets
, n_targets
* sizeof(struct nfc_target
),
482 spin_unlock_bh(&dev
->targets_lock
);
486 dev
->n_targets
= n_targets
;
487 spin_unlock_bh(&dev
->targets_lock
);
489 nfc_genl_targets_found(dev
);
493 EXPORT_SYMBOL(nfc_targets_found
);
495 int nfc_target_lost(struct nfc_dev
*dev
, u32 target_idx
)
497 struct nfc_target
*tg
;
500 pr_debug("dev_name %s n_target %d\n", dev_name(&dev
->dev
), target_idx
);
502 spin_lock_bh(&dev
->targets_lock
);
504 for (i
= 0; i
< dev
->n_targets
; i
++) {
505 tg
= &dev
->targets
[i
];
506 if (tg
->idx
== target_idx
)
510 if (i
== dev
->n_targets
) {
511 spin_unlock_bh(&dev
->targets_lock
);
515 dev
->targets_generation
++;
517 dev
->activated_target_idx
= NFC_TARGET_IDX_NONE
;
519 if (dev
->n_targets
) {
520 memcpy(&dev
->targets
[i
], &dev
->targets
[i
+ 1],
521 (dev
->n_targets
- i
) * sizeof(struct nfc_target
));
527 spin_unlock_bh(&dev
->targets_lock
);
529 nfc_genl_target_lost(dev
, target_idx
);
533 EXPORT_SYMBOL(nfc_target_lost
);
535 static void nfc_release(struct device
*d
)
537 struct nfc_dev
*dev
= to_nfc_dev(d
);
539 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
541 if (dev
->ops
->check_presence
) {
542 del_timer_sync(&dev
->check_pres_timer
);
543 destroy_workqueue(dev
->check_pres_wq
);
546 nfc_genl_data_exit(&dev
->genl_data
);
551 static void nfc_check_pres_work(struct work_struct
*work
)
553 struct nfc_dev
*dev
= container_of(work
, struct nfc_dev
,
557 device_lock(&dev
->dev
);
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
);
563 mod_timer(&dev
->check_pres_timer
, jiffies
+
564 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS
));
566 nfc_target_lost(dev
, dev
->activated_target_idx
);
567 dev
->activated_target_idx
= NFC_TARGET_IDX_NONE
;
571 device_unlock(&dev
->dev
);
574 static void nfc_check_pres_timeout(unsigned long data
)
576 struct nfc_dev
*dev
= (struct nfc_dev
*)data
;
578 queue_work(dev
->check_pres_wq
, &dev
->check_pres_work
);
581 struct class nfc_class
= {
583 .dev_release
= nfc_release
,
585 EXPORT_SYMBOL(nfc_class
);
587 static int match_idx(struct device
*d
, void *data
)
589 struct nfc_dev
*dev
= to_nfc_dev(d
);
590 unsigned int *idx
= data
;
592 return dev
->idx
== *idx
;
595 struct nfc_dev
*nfc_get_device(unsigned int idx
)
599 d
= class_find_device(&nfc_class
, NULL
, &idx
, match_idx
);
603 return to_nfc_dev(d
);
607 * nfc_allocate_device - allocate a new nfc device
609 * @ops: device operations
610 * @supported_protocols: NFC protocols supported by the device
612 struct nfc_dev
*nfc_allocate_device(struct nfc_ops
*ops
,
613 u32 supported_protocols
,
614 int tx_headroom
, int tx_tailroom
)
616 static atomic_t dev_no
= ATOMIC_INIT(0);
619 if (!ops
->start_poll
|| !ops
->stop_poll
|| !ops
->activate_target
||
620 !ops
->deactivate_target
|| !ops
->data_exchange
)
623 if (!supported_protocols
)
626 dev
= kzalloc(sizeof(struct nfc_dev
), GFP_KERNEL
);
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
);
636 dev
->supported_protocols
= supported_protocols
;
637 dev
->tx_headroom
= tx_headroom
;
638 dev
->tx_tailroom
= tx_tailroom
;
640 spin_lock_init(&dev
->targets_lock
);
641 nfc_genl_data_init(&dev
->genl_data
);
643 /* first generation must not be 0 */
644 dev
->targets_generation
= 1;
646 dev
->activated_target_idx
= NFC_TARGET_IDX_NONE
;
648 if (ops
->check_presence
) {
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
;
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
|
659 if (dev
->check_pres_wq
== NULL
) {
668 EXPORT_SYMBOL(nfc_allocate_device
);
671 * nfc_register_device - register a nfc device in the nfc subsystem
673 * @dev: The nfc device to register
675 int nfc_register_device(struct nfc_dev
*dev
)
679 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
681 mutex_lock(&nfc_devlist_mutex
);
682 nfc_devlist_generation
++;
683 rc
= device_add(&dev
->dev
);
684 mutex_unlock(&nfc_devlist_mutex
);
689 rc
= nfc_llcp_register_device(dev
);
691 pr_err("Could not register llcp device\n");
693 rc
= nfc_genl_device_added(dev
);
695 pr_debug("The userspace won't be notified that the device %s was added\n",
696 dev_name(&dev
->dev
));
700 EXPORT_SYMBOL(nfc_register_device
);
703 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
705 * @dev: The nfc device to unregister
707 void nfc_unregister_device(struct nfc_dev
*dev
)
711 pr_debug("dev_name=%s\n", dev_name(&dev
->dev
));
713 mutex_lock(&nfc_devlist_mutex
);
714 nfc_devlist_generation
++;
716 /* lock to avoid unregistering a device while an operation
718 device_lock(&dev
->dev
);
719 device_del(&dev
->dev
);
720 device_unlock(&dev
->dev
);
722 mutex_unlock(&nfc_devlist_mutex
);
724 nfc_llcp_unregister_device(dev
);
726 rc
= nfc_genl_device_removed(dev
);
728 pr_debug("The userspace won't be notified that the device %s was removed\n",
729 dev_name(&dev
->dev
));
732 EXPORT_SYMBOL(nfc_unregister_device
);
734 static int __init
nfc_init(void)
738 pr_info("NFC Core ver %s\n", VERSION
);
740 rc
= class_register(&nfc_class
);
744 rc
= nfc_genl_init();
748 /* the first generation must not be 0 */
749 nfc_devlist_generation
= 1;
755 rc
= nfc_llcp_init();
772 class_unregister(&nfc_class
);
776 static void __exit
nfc_exit(void)
782 class_unregister(&nfc_class
);
785 subsys_initcall(nfc_init
);
786 module_exit(nfc_exit
);
788 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
789 MODULE_DESCRIPTION("NFC Core ver " VERSION
);
790 MODULE_VERSION(VERSION
);
791 MODULE_LICENSE("GPL");