d649fbf39d580bcbd2936de0e2c81ff29acb56d0
2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/nfc.h>
30 static u8 llcp_magic
[3] = {0x46, 0x66, 0x6d};
32 static struct list_head llcp_devices
;
34 void nfc_llcp_sock_link(struct llcp_sock_list
*l
, struct sock
*sk
)
37 sk_add_node(sk
, &l
->head
);
38 write_unlock(&l
->lock
);
41 void nfc_llcp_sock_unlink(struct llcp_sock_list
*l
, struct sock
*sk
)
45 write_unlock(&l
->lock
);
48 static void nfc_llcp_socket_release(struct nfc_llcp_local
*local
, bool listen
)
51 struct hlist_node
*node
, *tmp
;
52 struct nfc_llcp_sock
*llcp_sock
;
54 write_lock(&local
->sockets
.lock
);
56 sk_for_each_safe(sk
, node
, tmp
, &local
->sockets
.head
) {
57 llcp_sock
= nfc_llcp_sock(sk
);
61 if (sk
->sk_state
== LLCP_CONNECTED
)
62 nfc_put_device(llcp_sock
->dev
);
64 if (sk
->sk_state
== LLCP_LISTEN
) {
65 struct nfc_llcp_sock
*lsk
, *n
;
66 struct sock
*accept_sk
;
68 list_for_each_entry_safe(lsk
, n
, &llcp_sock
->accept_queue
,
73 nfc_llcp_accept_unlink(accept_sk
);
75 accept_sk
->sk_state
= LLCP_CLOSED
;
77 release_sock(accept_sk
);
79 sock_orphan(accept_sk
);
88 sk
->sk_state
= LLCP_CLOSED
;
97 write_unlock(&local
->sockets
.lock
);
100 struct nfc_llcp_local
*nfc_llcp_local_get(struct nfc_llcp_local
*local
)
102 kref_get(&local
->ref
);
107 static void local_release(struct kref
*ref
)
109 struct nfc_llcp_local
*local
;
111 local
= container_of(ref
, struct nfc_llcp_local
, ref
);
113 list_del(&local
->list
);
114 nfc_llcp_socket_release(local
, false);
115 del_timer_sync(&local
->link_timer
);
116 skb_queue_purge(&local
->tx_queue
);
117 cancel_work_sync(&local
->tx_work
);
118 cancel_work_sync(&local
->rx_work
);
119 cancel_work_sync(&local
->timeout_work
);
120 kfree_skb(local
->rx_pending
);
124 int nfc_llcp_local_put(struct nfc_llcp_local
*local
)
129 return kref_put(&local
->ref
, local_release
);
132 static struct nfc_llcp_sock
*nfc_llcp_sock_get(struct nfc_llcp_local
*local
,
136 struct hlist_node
*node
;
137 struct nfc_llcp_sock
*llcp_sock
;
139 pr_debug("ssap dsap %d %d\n", ssap
, dsap
);
141 if (ssap
== 0 && dsap
== 0)
144 read_lock(&local
->sockets
.lock
);
148 sk_for_each(sk
, node
, &local
->sockets
.head
) {
149 llcp_sock
= nfc_llcp_sock(sk
);
151 if (llcp_sock
->ssap
== ssap
&& llcp_sock
->dsap
== dsap
)
155 read_unlock(&local
->sockets
.lock
);
157 if (llcp_sock
== NULL
)
160 sock_hold(&llcp_sock
->sk
);
165 static void nfc_llcp_sock_put(struct nfc_llcp_sock
*sock
)
170 static void nfc_llcp_timeout_work(struct work_struct
*work
)
172 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
175 nfc_dep_link_down(local
->dev
);
178 static void nfc_llcp_symm_timer(unsigned long data
)
180 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
182 pr_err("SYMM timeout\n");
184 queue_work(system_nrt_wq
, &local
->timeout_work
);
187 struct nfc_llcp_local
*nfc_llcp_find_local(struct nfc_dev
*dev
)
189 struct nfc_llcp_local
*local
, *n
;
191 list_for_each_entry_safe(local
, n
, &llcp_devices
, list
)
192 if (local
->dev
== dev
)
195 pr_debug("No device found\n");
200 static char *wks
[] = {
208 static int nfc_llcp_wks_sap(char *service_name
, size_t service_name_len
)
212 pr_debug("%s\n", service_name
);
214 if (service_name
== NULL
)
217 num_wks
= ARRAY_SIZE(wks
);
219 for (sap
= 0; sap
< num_wks
; sap
++) {
220 if (wks
[sap
] == NULL
)
223 if (strncmp(wks
[sap
], service_name
, service_name_len
) == 0)
231 struct nfc_llcp_sock
*nfc_llcp_sock_from_sn(struct nfc_llcp_local
*local
,
232 u8
*sn
, size_t sn_len
)
235 struct hlist_node
*node
;
236 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
238 pr_debug("sn %zd %p\n", sn_len
, sn
);
240 if (sn
== NULL
|| sn_len
== 0)
243 read_lock(&local
->sockets
.lock
);
247 sk_for_each(sk
, node
, &local
->sockets
.head
) {
248 tmp_sock
= nfc_llcp_sock(sk
);
250 pr_debug("llcp sock %p\n", tmp_sock
);
252 if (tmp_sock
->sk
.sk_state
!= LLCP_LISTEN
)
255 if (tmp_sock
->service_name
== NULL
||
256 tmp_sock
->service_name_len
== 0)
259 if (tmp_sock
->service_name_len
!= sn_len
)
262 if (memcmp(sn
, tmp_sock
->service_name
, sn_len
) == 0) {
263 llcp_sock
= tmp_sock
;
268 read_unlock(&local
->sockets
.lock
);
270 pr_debug("Found llcp sock %p\n", llcp_sock
);
275 u8
nfc_llcp_get_sdp_ssap(struct nfc_llcp_local
*local
,
276 struct nfc_llcp_sock
*sock
)
278 mutex_lock(&local
->sdp_lock
);
280 if (sock
->service_name
!= NULL
&& sock
->service_name_len
> 0) {
281 int ssap
= nfc_llcp_wks_sap(sock
->service_name
,
282 sock
->service_name_len
);
285 pr_debug("WKS %d\n", ssap
);
287 /* This is a WKS, let's check if it's free */
288 if (local
->local_wks
& BIT(ssap
)) {
289 mutex_unlock(&local
->sdp_lock
);
294 set_bit(ssap
, &local
->local_wks
);
295 mutex_unlock(&local
->sdp_lock
);
301 * Check if there already is a non WKS socket bound
302 * to this service name.
304 if (nfc_llcp_sock_from_sn(local
, sock
->service_name
,
305 sock
->service_name_len
) != NULL
) {
306 mutex_unlock(&local
->sdp_lock
);
311 mutex_unlock(&local
->sdp_lock
);
313 return LLCP_SDP_UNBOUND
;
315 } else if (sock
->ssap
!= 0 && sock
->ssap
< LLCP_WKS_NUM_SAP
) {
316 if (!test_bit(sock
->ssap
, &local
->local_wks
)) {
317 set_bit(sock
->ssap
, &local
->local_wks
);
318 mutex_unlock(&local
->sdp_lock
);
324 mutex_unlock(&local
->sdp_lock
);
329 u8
nfc_llcp_get_local_ssap(struct nfc_llcp_local
*local
)
333 mutex_lock(&local
->sdp_lock
);
335 local_ssap
= find_first_zero_bit(&local
->local_sap
, LLCP_LOCAL_NUM_SAP
);
336 if (local_ssap
== LLCP_LOCAL_NUM_SAP
) {
337 mutex_unlock(&local
->sdp_lock
);
341 set_bit(local_ssap
, &local
->local_sap
);
343 mutex_unlock(&local
->sdp_lock
);
345 return local_ssap
+ LLCP_LOCAL_SAP_OFFSET
;
348 void nfc_llcp_put_ssap(struct nfc_llcp_local
*local
, u8 ssap
)
353 if (ssap
< LLCP_WKS_NUM_SAP
) {
355 sdp
= &local
->local_wks
;
356 } else if (ssap
< LLCP_LOCAL_NUM_SAP
) {
357 atomic_t
*client_cnt
;
359 local_ssap
= ssap
- LLCP_WKS_NUM_SAP
;
360 sdp
= &local
->local_sdp
;
361 client_cnt
= &local
->local_sdp_cnt
[local_ssap
];
363 pr_debug("%d clients\n", atomic_read(client_cnt
));
365 mutex_lock(&local
->sdp_lock
);
367 if (atomic_dec_and_test(client_cnt
)) {
368 struct nfc_llcp_sock
*l_sock
;
370 pr_debug("No more clients for SAP %d\n", ssap
);
372 clear_bit(local_ssap
, sdp
);
374 /* Find the listening sock and set it back to UNBOUND */
375 l_sock
= nfc_llcp_sock_get(local
, ssap
, LLCP_SAP_SDP
);
377 l_sock
->ssap
= LLCP_SDP_UNBOUND
;
378 nfc_llcp_sock_put(l_sock
);
382 mutex_unlock(&local
->sdp_lock
);
385 } else if (ssap
< LLCP_MAX_SAP
) {
386 local_ssap
= ssap
- LLCP_LOCAL_NUM_SAP
;
387 sdp
= &local
->local_sap
;
392 mutex_lock(&local
->sdp_lock
);
394 clear_bit(local_ssap
, sdp
);
396 mutex_unlock(&local
->sdp_lock
);
399 static u8
nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local
*local
)
403 mutex_lock(&local
->sdp_lock
);
405 ssap
= find_first_zero_bit(&local
->local_sdp
, LLCP_SDP_NUM_SAP
);
406 if (ssap
== LLCP_SDP_NUM_SAP
) {
407 mutex_unlock(&local
->sdp_lock
);
412 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP
+ ssap
);
414 set_bit(ssap
, &local
->local_sdp
);
416 mutex_unlock(&local
->sdp_lock
);
418 return LLCP_WKS_NUM_SAP
+ ssap
;
421 static int nfc_llcp_build_gb(struct nfc_llcp_local
*local
)
423 u8
*gb_cur
, *version_tlv
, version
, version_length
;
424 u8
*lto_tlv
, lto
, lto_length
;
425 u8
*wks_tlv
, wks_length
;
426 u8
*miux_tlv
, miux_length
;
431 version
= LLCP_VERSION_11
;
432 version_tlv
= nfc_llcp_build_tlv(LLCP_TLV_VERSION
, &version
,
434 gb_len
+= version_length
;
438 lto_tlv
= nfc_llcp_build_tlv(LLCP_TLV_LTO
, <o
, 1, <o_length
);
439 gb_len
+= lto_length
;
441 pr_debug("Local wks 0x%lx\n", local
->local_wks
);
442 wks_tlv
= nfc_llcp_build_tlv(LLCP_TLV_WKS
, (u8
*)&local
->local_wks
, 2,
444 gb_len
+= wks_length
;
446 miux
= cpu_to_be16(LLCP_MAX_MIUX
);
447 miux_tlv
= nfc_llcp_build_tlv(LLCP_TLV_MIUX
, (u8
*)&miux
, 0,
449 gb_len
+= miux_length
;
451 gb_len
+= ARRAY_SIZE(llcp_magic
);
453 if (gb_len
> NFC_MAX_GT_LEN
) {
460 memcpy(gb_cur
, llcp_magic
, ARRAY_SIZE(llcp_magic
));
461 gb_cur
+= ARRAY_SIZE(llcp_magic
);
463 memcpy(gb_cur
, version_tlv
, version_length
);
464 gb_cur
+= version_length
;
466 memcpy(gb_cur
, lto_tlv
, lto_length
);
467 gb_cur
+= lto_length
;
469 memcpy(gb_cur
, wks_tlv
, wks_length
);
470 gb_cur
+= wks_length
;
472 memcpy(gb_cur
, miux_tlv
, miux_length
);
473 gb_cur
+= miux_length
;
475 local
->gb_len
= gb_len
;
486 u8
*nfc_llcp_general_bytes(struct nfc_dev
*dev
, size_t *general_bytes_len
)
488 struct nfc_llcp_local
*local
;
490 local
= nfc_llcp_find_local(dev
);
492 *general_bytes_len
= 0;
496 nfc_llcp_build_gb(local
);
498 *general_bytes_len
= local
->gb_len
;
503 int nfc_llcp_set_remote_gb(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
505 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
508 pr_err("No LLCP device\n");
512 memset(local
->remote_gb
, 0, NFC_MAX_GT_LEN
);
513 memcpy(local
->remote_gb
, gb
, gb_len
);
514 local
->remote_gb_len
= gb_len
;
516 if (local
->remote_gb
== NULL
|| local
->remote_gb_len
== 0)
519 if (memcmp(local
->remote_gb
, llcp_magic
, 3)) {
520 pr_err("MAC does not support LLCP\n");
524 return nfc_llcp_parse_gb_tlv(local
,
525 &local
->remote_gb
[3],
526 local
->remote_gb_len
- 3);
529 static u8
nfc_llcp_dsap(struct sk_buff
*pdu
)
531 return (pdu
->data
[0] & 0xfc) >> 2;
534 static u8
nfc_llcp_ptype(struct sk_buff
*pdu
)
536 return ((pdu
->data
[0] & 0x03) << 2) | ((pdu
->data
[1] & 0xc0) >> 6);
539 static u8
nfc_llcp_ssap(struct sk_buff
*pdu
)
541 return pdu
->data
[1] & 0x3f;
544 static u8
nfc_llcp_ns(struct sk_buff
*pdu
)
546 return pdu
->data
[2] >> 4;
549 static u8
nfc_llcp_nr(struct sk_buff
*pdu
)
551 return pdu
->data
[2] & 0xf;
554 static void nfc_llcp_set_nrns(struct nfc_llcp_sock
*sock
, struct sk_buff
*pdu
)
556 pdu
->data
[2] = (sock
->send_n
<< 4) | (sock
->recv_n
);
557 sock
->send_n
= (sock
->send_n
+ 1) % 16;
558 sock
->recv_ack_n
= (sock
->recv_n
- 1) % 16;
561 static void nfc_llcp_tx_work(struct work_struct
*work
)
563 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
567 struct nfc_llcp_sock
*llcp_sock
;
569 skb
= skb_dequeue(&local
->tx_queue
);
572 llcp_sock
= nfc_llcp_sock(sk
);
573 if (llcp_sock
!= NULL
) {
576 pr_debug("Sending pending skb\n");
577 print_hex_dump(KERN_DEBUG
, "LLCP Tx: ",
578 DUMP_PREFIX_OFFSET
, 16, 1,
579 skb
->data
, skb
->len
, true);
581 ret
= nfc_data_exchange(local
->dev
, local
->target_idx
,
582 skb
, nfc_llcp_recv
, local
);
584 if (!ret
&& nfc_llcp_ptype(skb
) == LLCP_PDU_I
) {
586 skb_queue_tail(&llcp_sock
->tx_pending_queue
,
590 nfc_llcp_send_symm(local
->dev
);
593 nfc_llcp_send_symm(local
->dev
);
596 mod_timer(&local
->link_timer
,
597 jiffies
+ msecs_to_jiffies(2 * local
->remote_lto
));
600 static struct nfc_llcp_sock
*nfc_llcp_connecting_sock_get(struct nfc_llcp_local
*local
,
604 struct nfc_llcp_sock
*llcp_sock
;
605 struct hlist_node
*node
;
607 read_lock(&local
->connecting_sockets
.lock
);
609 sk_for_each(sk
, node
, &local
->connecting_sockets
.head
) {
610 llcp_sock
= nfc_llcp_sock(sk
);
612 if (llcp_sock
->ssap
== ssap
) {
613 sock_hold(&llcp_sock
->sk
);
621 read_unlock(&local
->connecting_sockets
.lock
);
626 static struct nfc_llcp_sock
*nfc_llcp_sock_get_sn(struct nfc_llcp_local
*local
,
627 u8
*sn
, size_t sn_len
)
629 struct nfc_llcp_sock
*llcp_sock
;
631 llcp_sock
= nfc_llcp_sock_from_sn(local
, sn
, sn_len
);
633 if (llcp_sock
== NULL
)
636 sock_hold(&llcp_sock
->sk
);
641 static u8
*nfc_llcp_connect_sn(struct sk_buff
*skb
, size_t *sn_len
)
643 u8
*tlv
= &skb
->data
[2], type
, length
;
644 size_t tlv_array_len
= skb
->len
- LLCP_HEADER_SIZE
, offset
= 0;
646 while (offset
< tlv_array_len
) {
650 pr_debug("type 0x%x length %d\n", type
, length
);
652 if (type
== LLCP_TLV_SN
) {
657 offset
+= length
+ 2;
664 static void nfc_llcp_recv_connect(struct nfc_llcp_local
*local
,
667 struct sock
*new_sk
, *parent
;
668 struct nfc_llcp_sock
*sock
, *new_sock
;
669 u8 dsap
, ssap
, reason
;
671 dsap
= nfc_llcp_dsap(skb
);
672 ssap
= nfc_llcp_ssap(skb
);
674 pr_debug("%d %d\n", dsap
, ssap
);
676 if (dsap
!= LLCP_SAP_SDP
) {
677 sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
678 if (sock
== NULL
|| sock
->sk
.sk_state
!= LLCP_LISTEN
) {
679 reason
= LLCP_DM_NOBOUND
;
686 sn
= nfc_llcp_connect_sn(skb
, &sn_len
);
688 reason
= LLCP_DM_NOBOUND
;
692 pr_debug("Service name length %zu\n", sn_len
);
694 sock
= nfc_llcp_sock_get_sn(local
, sn
, sn_len
);
696 reason
= LLCP_DM_NOBOUND
;
701 lock_sock(&sock
->sk
);
705 if (sk_acceptq_is_full(parent
)) {
706 reason
= LLCP_DM_REJ
;
707 release_sock(&sock
->sk
);
712 if (sock
->ssap
== LLCP_SDP_UNBOUND
) {
713 u8 ssap
= nfc_llcp_reserve_sdp_ssap(local
);
715 pr_debug("First client, reserving %d\n", ssap
);
717 if (ssap
== LLCP_SAP_MAX
) {
718 reason
= LLCP_DM_REJ
;
719 release_sock(&sock
->sk
);
727 new_sk
= nfc_llcp_sock_alloc(NULL
, parent
->sk_type
, GFP_ATOMIC
);
728 if (new_sk
== NULL
) {
729 reason
= LLCP_DM_REJ
;
730 release_sock(&sock
->sk
);
735 new_sock
= nfc_llcp_sock(new_sk
);
736 new_sock
->dev
= local
->dev
;
737 new_sock
->local
= nfc_llcp_local_get(local
);
738 new_sock
->miu
= local
->remote_miu
;
739 new_sock
->nfc_protocol
= sock
->nfc_protocol
;
740 new_sock
->dsap
= ssap
;
741 new_sock
->target_idx
= local
->target_idx
;
742 new_sock
->parent
= parent
;
743 new_sock
->ssap
= sock
->ssap
;
744 if (sock
->ssap
< LLCP_LOCAL_NUM_SAP
&& sock
->ssap
>= LLCP_WKS_NUM_SAP
) {
745 atomic_t
*client_count
;
747 pr_debug("reserved_ssap %d for %p\n", sock
->ssap
, new_sock
);
750 &local
->local_sdp_cnt
[sock
->ssap
- LLCP_WKS_NUM_SAP
];
752 atomic_inc(client_count
);
753 new_sock
->reserved_ssap
= sock
->ssap
;
756 nfc_llcp_parse_connection_tlv(new_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
757 skb
->len
- LLCP_HEADER_SIZE
);
759 pr_debug("new sock %p sk %p\n", new_sock
, &new_sock
->sk
);
761 nfc_llcp_sock_link(&local
->sockets
, new_sk
);
763 nfc_llcp_accept_enqueue(&sock
->sk
, new_sk
);
765 nfc_get_device(local
->dev
->idx
);
767 new_sk
->sk_state
= LLCP_CONNECTED
;
769 /* Wake the listening processes */
770 parent
->sk_data_ready(parent
, 0);
773 nfc_llcp_send_cc(new_sock
);
775 release_sock(&sock
->sk
);
782 nfc_llcp_send_dm(local
, dsap
, ssap
, reason
);
788 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock
*sock
)
791 struct nfc_llcp_local
*local
= sock
->local
;
793 pr_debug("Remote ready %d tx queue len %d remote rw %d",
794 sock
->remote_ready
, skb_queue_len(&sock
->tx_pending_queue
),
797 /* Try to queue some I frames for transmission */
798 while (sock
->remote_ready
&&
799 skb_queue_len(&sock
->tx_pending_queue
) < sock
->rw
) {
802 pdu
= skb_dequeue(&sock
->tx_queue
);
806 /* Update N(S)/N(R) */
807 nfc_llcp_set_nrns(sock
, pdu
);
809 skb_queue_tail(&local
->tx_queue
, pdu
);
816 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local
*local
,
819 struct nfc_llcp_sock
*llcp_sock
;
821 u8 dsap
, ssap
, ptype
, ns
, nr
;
823 ptype
= nfc_llcp_ptype(skb
);
824 dsap
= nfc_llcp_dsap(skb
);
825 ssap
= nfc_llcp_ssap(skb
);
826 ns
= nfc_llcp_ns(skb
);
827 nr
= nfc_llcp_nr(skb
);
829 pr_debug("%d %d R %d S %d\n", dsap
, ssap
, nr
, ns
);
831 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
832 if (llcp_sock
== NULL
) {
833 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
839 if (sk
->sk_state
== LLCP_CLOSED
) {
841 nfc_llcp_sock_put(llcp_sock
);
844 /* Pass the payload upstream */
845 if (ptype
== LLCP_PDU_I
) {
846 pr_debug("I frame, queueing on %p\n", &llcp_sock
->sk
);
848 if (ns
== llcp_sock
->recv_n
)
849 llcp_sock
->recv_n
= (llcp_sock
->recv_n
+ 1) % 16;
851 pr_err("Received out of sequence I PDU\n");
853 skb_pull(skb
, LLCP_HEADER_SIZE
+ LLCP_SEQUENCE_SIZE
);
854 if (sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
855 pr_err("receive queue is full\n");
856 skb_queue_head(&llcp_sock
->tx_backlog_queue
, skb
);
860 /* Remove skbs from the pending queue */
861 if (llcp_sock
->send_ack_n
!= nr
) {
862 struct sk_buff
*s
, *tmp
;
864 llcp_sock
->send_ack_n
= nr
;
866 /* Remove and free all skbs until ns == nr */
867 skb_queue_walk_safe(&llcp_sock
->tx_pending_queue
, s
, tmp
) {
868 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
871 if (nfc_llcp_ns(s
) == nr
)
875 /* Re-queue the remaining skbs for transmission */
876 skb_queue_reverse_walk_safe(&llcp_sock
->tx_pending_queue
,
878 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
879 skb_queue_head(&local
->tx_queue
, s
);
883 if (ptype
== LLCP_PDU_RR
)
884 llcp_sock
->remote_ready
= true;
885 else if (ptype
== LLCP_PDU_RNR
)
886 llcp_sock
->remote_ready
= false;
888 if (nfc_llcp_queue_i_frames(llcp_sock
) == 0 && ptype
== LLCP_PDU_I
)
889 nfc_llcp_send_rr(llcp_sock
);
892 nfc_llcp_sock_put(llcp_sock
);
895 static void nfc_llcp_recv_disc(struct nfc_llcp_local
*local
,
898 struct nfc_llcp_sock
*llcp_sock
;
902 dsap
= nfc_llcp_dsap(skb
);
903 ssap
= nfc_llcp_ssap(skb
);
905 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
906 if (llcp_sock
== NULL
) {
907 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
913 if (sk
->sk_state
== LLCP_CLOSED
) {
915 nfc_llcp_sock_put(llcp_sock
);
918 if (sk
->sk_state
== LLCP_CONNECTED
) {
919 nfc_put_device(local
->dev
);
920 sk
->sk_state
= LLCP_CLOSED
;
921 sk
->sk_state_change(sk
);
924 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_DISC
);
927 nfc_llcp_sock_put(llcp_sock
);
930 static void nfc_llcp_recv_cc(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
932 struct nfc_llcp_sock
*llcp_sock
;
936 dsap
= nfc_llcp_dsap(skb
);
937 ssap
= nfc_llcp_ssap(skb
);
939 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
940 if (llcp_sock
== NULL
) {
941 pr_err("Invalid CC\n");
942 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
949 /* Unlink from connecting and link to the client array */
950 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
951 nfc_llcp_sock_link(&local
->sockets
, sk
);
952 llcp_sock
->dsap
= ssap
;
954 nfc_llcp_parse_connection_tlv(llcp_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
955 skb
->len
- LLCP_HEADER_SIZE
);
957 sk
->sk_state
= LLCP_CONNECTED
;
958 sk
->sk_state_change(sk
);
960 nfc_llcp_sock_put(llcp_sock
);
963 static void nfc_llcp_recv_dm(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
965 struct nfc_llcp_sock
*llcp_sock
;
967 u8 dsap
, ssap
, reason
;
969 dsap
= nfc_llcp_dsap(skb
);
970 ssap
= nfc_llcp_ssap(skb
);
971 reason
= skb
->data
[2];
973 pr_debug("%d %d reason %d\n", ssap
, dsap
, reason
);
976 case LLCP_DM_NOBOUND
:
978 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
982 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
986 if (llcp_sock
== NULL
) {
987 pr_err("Invalid DM\n");
994 sk
->sk_state
= LLCP_CLOSED
;
995 sk
->sk_state_change(sk
);
997 nfc_llcp_sock_put(llcp_sock
);
1002 static void nfc_llcp_rx_work(struct work_struct
*work
)
1004 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
1006 u8 dsap
, ssap
, ptype
;
1007 struct sk_buff
*skb
;
1009 skb
= local
->rx_pending
;
1011 pr_debug("No pending SKB\n");
1015 ptype
= nfc_llcp_ptype(skb
);
1016 dsap
= nfc_llcp_dsap(skb
);
1017 ssap
= nfc_llcp_ssap(skb
);
1019 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype
, dsap
, ssap
);
1021 if (ptype
!= LLCP_PDU_SYMM
)
1022 print_hex_dump(KERN_DEBUG
, "LLCP Rx: ", DUMP_PREFIX_OFFSET
,
1023 16, 1, skb
->data
, skb
->len
, true);
1030 case LLCP_PDU_CONNECT
:
1031 pr_debug("CONNECT\n");
1032 nfc_llcp_recv_connect(local
, skb
);
1037 nfc_llcp_recv_disc(local
, skb
);
1042 nfc_llcp_recv_cc(local
, skb
);
1047 nfc_llcp_recv_dm(local
, skb
);
1053 pr_debug("I frame\n");
1054 nfc_llcp_recv_hdlc(local
, skb
);
1059 queue_work(system_nrt_wq
, &local
->tx_work
);
1060 kfree_skb(local
->rx_pending
);
1061 local
->rx_pending
= NULL
;
1066 void nfc_llcp_recv(void *data
, struct sk_buff
*skb
, int err
)
1068 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
1070 pr_debug("Received an LLCP PDU\n");
1072 pr_err("err %d\n", err
);
1076 local
->rx_pending
= skb_get(skb
);
1077 del_timer(&local
->link_timer
);
1078 queue_work(system_nrt_wq
, &local
->rx_work
);
1083 int nfc_llcp_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
1085 struct nfc_llcp_local
*local
;
1087 local
= nfc_llcp_find_local(dev
);
1091 local
->rx_pending
= skb_get(skb
);
1092 del_timer(&local
->link_timer
);
1093 queue_work(system_nrt_wq
, &local
->rx_work
);
1098 void nfc_llcp_mac_is_down(struct nfc_dev
*dev
)
1100 struct nfc_llcp_local
*local
;
1102 local
= nfc_llcp_find_local(dev
);
1106 /* Close and purge all existing sockets */
1107 nfc_llcp_socket_release(local
, true);
1110 void nfc_llcp_mac_is_up(struct nfc_dev
*dev
, u32 target_idx
,
1111 u8 comm_mode
, u8 rf_mode
)
1113 struct nfc_llcp_local
*local
;
1115 pr_debug("rf mode %d\n", rf_mode
);
1117 local
= nfc_llcp_find_local(dev
);
1121 local
->target_idx
= target_idx
;
1122 local
->comm_mode
= comm_mode
;
1123 local
->rf_mode
= rf_mode
;
1125 if (rf_mode
== NFC_RF_INITIATOR
) {
1126 pr_debug("Queueing Tx work\n");
1128 queue_work(system_nrt_wq
, &local
->tx_work
);
1130 mod_timer(&local
->link_timer
,
1131 jiffies
+ msecs_to_jiffies(local
->remote_lto
));
1135 int nfc_llcp_register_device(struct nfc_dev
*ndev
)
1137 struct nfc_llcp_local
*local
;
1139 local
= kzalloc(sizeof(struct nfc_llcp_local
), GFP_KERNEL
);
1144 INIT_LIST_HEAD(&local
->list
);
1145 kref_init(&local
->ref
);
1146 mutex_init(&local
->sdp_lock
);
1147 init_timer(&local
->link_timer
);
1148 local
->link_timer
.data
= (unsigned long) local
;
1149 local
->link_timer
.function
= nfc_llcp_symm_timer
;
1151 skb_queue_head_init(&local
->tx_queue
);
1152 INIT_WORK(&local
->tx_work
, nfc_llcp_tx_work
);
1154 local
->rx_pending
= NULL
;
1155 INIT_WORK(&local
->rx_work
, nfc_llcp_rx_work
);
1157 INIT_WORK(&local
->timeout_work
, nfc_llcp_timeout_work
);
1159 rwlock_init(&local
->sockets
.lock
);
1160 rwlock_init(&local
->connecting_sockets
.lock
);
1162 nfc_llcp_build_gb(local
);
1164 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1165 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1167 list_add(&llcp_devices
, &local
->list
);
1172 void nfc_llcp_unregister_device(struct nfc_dev
*dev
)
1174 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
1176 if (local
== NULL
) {
1177 pr_debug("No such device\n");
1181 nfc_llcp_local_put(local
);
1184 int __init
nfc_llcp_init(void)
1186 INIT_LIST_HEAD(&llcp_devices
);
1188 return nfc_llcp_sock_init();
1191 void nfc_llcp_exit(void)
1193 nfc_llcp_sock_exit();
This page took 0.069566 seconds and 5 git commands to generate.