spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / net / nfc / llcp / llcp.c
blob1d32680807d67f3729bb19724d9c09ba2f4cc599
1 /*
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>
27 #include "../nfc.h"
28 #include "llcp.h"
30 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
32 static struct list_head llcp_devices;
34 static void nfc_llcp_socket_release(struct nfc_llcp_local *local)
36 struct nfc_llcp_sock *parent, *s, *n;
37 struct sock *sk, *parent_sk;
38 int i;
41 mutex_lock(&local->socket_lock);
43 for (i = 0; i < LLCP_MAX_SAP; i++) {
44 parent = local->sockets[i];
45 if (parent == NULL)
46 continue;
48 /* Release all child sockets */
49 list_for_each_entry_safe(s, n, &parent->list, list) {
50 list_del(&s->list);
51 sk = &s->sk;
53 lock_sock(sk);
55 if (sk->sk_state == LLCP_CONNECTED)
56 nfc_put_device(s->dev);
58 sk->sk_state = LLCP_CLOSED;
59 sock_set_flag(sk, SOCK_DEAD);
61 release_sock(sk);
64 parent_sk = &parent->sk;
66 lock_sock(parent_sk);
68 if (parent_sk->sk_state == LLCP_LISTEN) {
69 struct nfc_llcp_sock *lsk, *n;
70 struct sock *accept_sk;
72 list_for_each_entry_safe(lsk, n, &parent->accept_queue,
73 accept_queue) {
74 accept_sk = &lsk->sk;
75 lock_sock(accept_sk);
77 nfc_llcp_accept_unlink(accept_sk);
79 accept_sk->sk_state = LLCP_CLOSED;
80 sock_set_flag(accept_sk, SOCK_DEAD);
82 release_sock(accept_sk);
84 sock_orphan(accept_sk);
88 if (parent_sk->sk_state == LLCP_CONNECTED)
89 nfc_put_device(parent->dev);
91 parent_sk->sk_state = LLCP_CLOSED;
92 sock_set_flag(parent_sk, SOCK_DEAD);
94 release_sock(parent_sk);
97 mutex_unlock(&local->socket_lock);
100 static void nfc_llcp_timeout_work(struct work_struct *work)
102 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
103 timeout_work);
105 nfc_dep_link_down(local->dev);
108 static void nfc_llcp_symm_timer(unsigned long data)
110 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
112 pr_err("SYMM timeout\n");
114 queue_work(local->timeout_wq, &local->timeout_work);
117 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
119 struct nfc_llcp_local *local, *n;
121 list_for_each_entry_safe(local, n, &llcp_devices, list)
122 if (local->dev == dev)
123 return local;
125 pr_debug("No device found\n");
127 return NULL;
130 static char *wks[] = {
131 NULL,
132 NULL, /* SDP */
133 "urn:nfc:sn:ip",
134 "urn:nfc:sn:obex",
135 "urn:nfc:sn:snep",
138 static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
140 int sap, num_wks;
142 pr_debug("%s\n", service_name);
144 if (service_name == NULL)
145 return -EINVAL;
147 num_wks = ARRAY_SIZE(wks);
149 for (sap = 0 ; sap < num_wks; sap++) {
150 if (wks[sap] == NULL)
151 continue;
153 if (strncmp(wks[sap], service_name, service_name_len) == 0)
154 return sap;
157 return -EINVAL;
160 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
161 struct nfc_llcp_sock *sock)
163 mutex_lock(&local->sdp_lock);
165 if (sock->service_name != NULL && sock->service_name_len > 0) {
166 int ssap = nfc_llcp_wks_sap(sock->service_name,
167 sock->service_name_len);
169 if (ssap > 0) {
170 pr_debug("WKS %d\n", ssap);
172 /* This is a WKS, let's check if it's free */
173 if (local->local_wks & BIT(ssap)) {
174 mutex_unlock(&local->sdp_lock);
176 return LLCP_SAP_MAX;
179 set_bit(BIT(ssap), &local->local_wks);
180 mutex_unlock(&local->sdp_lock);
182 return ssap;
186 * This is not a well known service,
187 * we should try to find a local SDP free spot
189 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
190 if (ssap == LLCP_SDP_NUM_SAP) {
191 mutex_unlock(&local->sdp_lock);
193 return LLCP_SAP_MAX;
196 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
198 set_bit(BIT(ssap), &local->local_sdp);
199 mutex_unlock(&local->sdp_lock);
201 return LLCP_WKS_NUM_SAP + ssap;
203 } else if (sock->ssap != 0) {
204 if (sock->ssap < LLCP_WKS_NUM_SAP) {
205 if (!(local->local_wks & BIT(sock->ssap))) {
206 set_bit(BIT(sock->ssap), &local->local_wks);
207 mutex_unlock(&local->sdp_lock);
209 return sock->ssap;
212 } else if (sock->ssap < LLCP_SDP_NUM_SAP) {
213 if (!(local->local_sdp &
214 BIT(sock->ssap - LLCP_WKS_NUM_SAP))) {
215 set_bit(BIT(sock->ssap - LLCP_WKS_NUM_SAP),
216 &local->local_sdp);
217 mutex_unlock(&local->sdp_lock);
219 return sock->ssap;
224 mutex_unlock(&local->sdp_lock);
226 return LLCP_SAP_MAX;
229 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
231 u8 local_ssap;
233 mutex_lock(&local->sdp_lock);
235 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
236 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
237 mutex_unlock(&local->sdp_lock);
238 return LLCP_SAP_MAX;
241 set_bit(BIT(local_ssap), &local->local_sap);
243 mutex_unlock(&local->sdp_lock);
245 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
248 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
250 u8 local_ssap;
251 unsigned long *sdp;
253 if (ssap < LLCP_WKS_NUM_SAP) {
254 local_ssap = ssap;
255 sdp = &local->local_wks;
256 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
257 local_ssap = ssap - LLCP_WKS_NUM_SAP;
258 sdp = &local->local_sdp;
259 } else if (ssap < LLCP_MAX_SAP) {
260 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
261 sdp = &local->local_sap;
262 } else {
263 return;
266 mutex_lock(&local->sdp_lock);
268 clear_bit(1 << local_ssap, sdp);
270 mutex_unlock(&local->sdp_lock);
273 u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, u8 *general_bytes_len)
275 struct nfc_llcp_local *local;
277 local = nfc_llcp_find_local(dev);
278 if (local == NULL) {
279 *general_bytes_len = 0;
280 return NULL;
283 *general_bytes_len = local->gb_len;
285 return local->gb;
288 static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
290 u8 *gb_cur, *version_tlv, version, version_length;
291 u8 *lto_tlv, lto, lto_length;
292 u8 *wks_tlv, wks_length;
293 u8 gb_len = 0;
295 version = LLCP_VERSION_11;
296 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
297 1, &version_length);
298 gb_len += version_length;
300 /* 1500 ms */
301 lto = 150;
302 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &lto, 1, &lto_length);
303 gb_len += lto_length;
305 pr_debug("Local wks 0x%lx\n", local->local_wks);
306 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
307 &wks_length);
308 gb_len += wks_length;
310 gb_len += ARRAY_SIZE(llcp_magic);
312 if (gb_len > NFC_MAX_GT_LEN) {
313 kfree(version_tlv);
314 return -EINVAL;
317 gb_cur = local->gb;
319 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
320 gb_cur += ARRAY_SIZE(llcp_magic);
322 memcpy(gb_cur, version_tlv, version_length);
323 gb_cur += version_length;
325 memcpy(gb_cur, lto_tlv, lto_length);
326 gb_cur += lto_length;
328 memcpy(gb_cur, wks_tlv, wks_length);
329 gb_cur += wks_length;
331 kfree(version_tlv);
332 kfree(lto_tlv);
334 local->gb_len = gb_len;
336 return 0;
339 int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
341 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
343 if (local == NULL) {
344 pr_err("No LLCP device\n");
345 return -ENODEV;
348 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
349 memcpy(local->remote_gb, gb, gb_len);
350 local->remote_gb_len = gb_len;
352 if (local->remote_gb == NULL ||
353 local->remote_gb_len == 0)
354 return -ENODEV;
356 if (memcmp(local->remote_gb, llcp_magic, 3)) {
357 pr_err("MAC does not support LLCP\n");
358 return -EINVAL;
361 return nfc_llcp_parse_tlv(local,
362 &local->remote_gb[3], local->remote_gb_len - 3);
365 static void nfc_llcp_tx_work(struct work_struct *work)
367 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
368 tx_work);
369 struct sk_buff *skb;
371 skb = skb_dequeue(&local->tx_queue);
372 if (skb != NULL) {
373 pr_debug("Sending pending skb\n");
374 nfc_data_exchange(local->dev, local->target_idx,
375 skb, nfc_llcp_recv, local);
376 } else {
377 nfc_llcp_send_symm(local->dev);
380 mod_timer(&local->link_timer,
381 jiffies + msecs_to_jiffies(local->remote_lto));
384 static u8 nfc_llcp_dsap(struct sk_buff *pdu)
386 return (pdu->data[0] & 0xfc) >> 2;
389 static u8 nfc_llcp_ptype(struct sk_buff *pdu)
391 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
394 static u8 nfc_llcp_ssap(struct sk_buff *pdu)
396 return pdu->data[1] & 0x3f;
399 static u8 nfc_llcp_ns(struct sk_buff *pdu)
401 return pdu->data[2] >> 4;
404 static u8 nfc_llcp_nr(struct sk_buff *pdu)
406 return pdu->data[2] & 0xf;
409 static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
411 pdu->data[2] = (sock->send_n << 4) | ((sock->recv_n - 1) % 16);
412 sock->send_n = (sock->send_n + 1) % 16;
413 sock->recv_ack_n = (sock->recv_n - 1) % 16;
416 static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
417 u8 ssap, u8 dsap)
419 struct nfc_llcp_sock *sock, *llcp_sock, *n;
421 if (ssap == 0 && dsap == 0)
422 return NULL;
424 mutex_lock(&local->socket_lock);
425 sock = local->sockets[ssap];
426 if (sock == NULL) {
427 mutex_unlock(&local->socket_lock);
428 return NULL;
431 pr_debug("root dsap %d (%d)\n", sock->dsap, dsap);
433 if (sock->dsap == dsap) {
434 sock_hold(&sock->sk);
435 mutex_unlock(&local->socket_lock);
436 return sock;
439 list_for_each_entry_safe(llcp_sock, n, &sock->list, list) {
440 pr_debug("llcp_sock %p sk %p dsap %d\n", llcp_sock,
441 &llcp_sock->sk, llcp_sock->dsap);
442 if (llcp_sock->dsap == dsap) {
443 sock_hold(&llcp_sock->sk);
444 mutex_unlock(&local->socket_lock);
445 return llcp_sock;
449 pr_err("Could not find socket for %d %d\n", ssap, dsap);
451 mutex_unlock(&local->socket_lock);
453 return NULL;
456 static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
458 sock_put(&sock->sk);
461 static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
463 u8 *tlv = &skb->data[2], type, length;
464 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
466 while (offset < tlv_array_len) {
467 type = tlv[0];
468 length = tlv[1];
470 pr_debug("type 0x%x length %d\n", type, length);
472 if (type == LLCP_TLV_SN) {
473 *sn_len = length;
474 return &tlv[2];
477 offset += length + 2;
478 tlv += length + 2;
481 return NULL;
484 static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
485 struct sk_buff *skb)
487 struct sock *new_sk, *parent;
488 struct nfc_llcp_sock *sock, *new_sock;
489 u8 dsap, ssap, bound_sap, reason;
491 dsap = nfc_llcp_dsap(skb);
492 ssap = nfc_llcp_ssap(skb);
494 pr_debug("%d %d\n", dsap, ssap);
496 nfc_llcp_parse_tlv(local, &skb->data[LLCP_HEADER_SIZE],
497 skb->len - LLCP_HEADER_SIZE);
499 if (dsap != LLCP_SAP_SDP) {
500 bound_sap = dsap;
502 mutex_lock(&local->socket_lock);
503 sock = local->sockets[dsap];
504 if (sock == NULL) {
505 mutex_unlock(&local->socket_lock);
506 reason = LLCP_DM_NOBOUND;
507 goto fail;
510 sock_hold(&sock->sk);
511 mutex_unlock(&local->socket_lock);
513 lock_sock(&sock->sk);
515 if (sock->dsap == LLCP_SAP_SDP &&
516 sock->sk.sk_state == LLCP_LISTEN)
517 goto enqueue;
518 } else {
519 u8 *sn;
520 size_t sn_len;
522 sn = nfc_llcp_connect_sn(skb, &sn_len);
523 if (sn == NULL) {
524 reason = LLCP_DM_NOBOUND;
525 goto fail;
528 pr_debug("Service name length %zu\n", sn_len);
530 mutex_lock(&local->socket_lock);
531 for (bound_sap = 0; bound_sap < LLCP_LOCAL_SAP_OFFSET;
532 bound_sap++) {
533 sock = local->sockets[bound_sap];
534 if (sock == NULL)
535 continue;
537 if (sock->service_name == NULL ||
538 sock->service_name_len == 0)
539 continue;
541 if (sock->service_name_len != sn_len)
542 continue;
544 if (sock->dsap == LLCP_SAP_SDP &&
545 sock->sk.sk_state == LLCP_LISTEN &&
546 !memcmp(sn, sock->service_name, sn_len)) {
547 pr_debug("Found service name at SAP %d\n",
548 bound_sap);
549 sock_hold(&sock->sk);
550 mutex_unlock(&local->socket_lock);
552 lock_sock(&sock->sk);
554 goto enqueue;
557 mutex_unlock(&local->socket_lock);
560 reason = LLCP_DM_NOBOUND;
561 goto fail;
563 enqueue:
564 parent = &sock->sk;
566 if (sk_acceptq_is_full(parent)) {
567 reason = LLCP_DM_REJ;
568 release_sock(&sock->sk);
569 sock_put(&sock->sk);
570 goto fail;
573 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type,
574 GFP_ATOMIC);
575 if (new_sk == NULL) {
576 reason = LLCP_DM_REJ;
577 release_sock(&sock->sk);
578 sock_put(&sock->sk);
579 goto fail;
582 new_sock = nfc_llcp_sock(new_sk);
583 new_sock->dev = local->dev;
584 new_sock->local = local;
585 new_sock->nfc_protocol = sock->nfc_protocol;
586 new_sock->ssap = bound_sap;
587 new_sock->dsap = ssap;
588 new_sock->parent = parent;
590 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
592 list_add_tail(&new_sock->list, &sock->list);
594 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
596 nfc_get_device(local->dev->idx);
598 new_sk->sk_state = LLCP_CONNECTED;
600 /* Wake the listening processes */
601 parent->sk_data_ready(parent, 0);
603 /* Send CC */
604 nfc_llcp_send_cc(new_sock);
606 release_sock(&sock->sk);
607 sock_put(&sock->sk);
609 return;
611 fail:
612 /* Send DM */
613 nfc_llcp_send_dm(local, dsap, ssap, reason);
615 return;
619 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
620 struct sk_buff *skb)
622 struct nfc_llcp_sock *llcp_sock;
623 struct sock *sk;
624 u8 dsap, ssap, ptype, ns, nr;
626 ptype = nfc_llcp_ptype(skb);
627 dsap = nfc_llcp_dsap(skb);
628 ssap = nfc_llcp_ssap(skb);
629 ns = nfc_llcp_ns(skb);
630 nr = nfc_llcp_nr(skb);
632 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
634 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
635 if (llcp_sock == NULL) {
636 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
637 return;
640 sk = &llcp_sock->sk;
641 lock_sock(sk);
642 if (sk->sk_state == LLCP_CLOSED) {
643 release_sock(sk);
644 nfc_llcp_sock_put(llcp_sock);
647 if (ns == llcp_sock->recv_n)
648 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
649 else
650 pr_err("Received out of sequence I PDU\n");
652 /* Pass the payload upstream */
653 if (ptype == LLCP_PDU_I) {
654 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
656 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
657 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
658 pr_err("receive queue is full\n");
659 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
663 /* Remove skbs from the pending queue */
664 if (llcp_sock->send_ack_n != nr) {
665 struct sk_buff *s, *tmp;
667 llcp_sock->send_ack_n = nr;
669 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp)
670 if (nfc_llcp_ns(s) <= nr) {
671 skb_unlink(s, &llcp_sock->tx_pending_queue);
672 kfree_skb(s);
676 /* Queue some I frames for transmission */
677 while (llcp_sock->remote_ready &&
678 skb_queue_len(&llcp_sock->tx_pending_queue) <= local->remote_rw) {
679 struct sk_buff *pdu, *pending_pdu;
681 pdu = skb_dequeue(&llcp_sock->tx_queue);
682 if (pdu == NULL)
683 break;
685 /* Update N(S)/N(R) */
686 nfc_llcp_set_nrns(llcp_sock, pdu);
688 pending_pdu = skb_clone(pdu, GFP_KERNEL);
690 skb_queue_tail(&local->tx_queue, pdu);
691 skb_queue_tail(&llcp_sock->tx_pending_queue, pending_pdu);
694 release_sock(sk);
695 nfc_llcp_sock_put(llcp_sock);
698 static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
699 struct sk_buff *skb)
701 struct nfc_llcp_sock *llcp_sock;
702 struct sock *sk;
703 u8 dsap, ssap;
705 dsap = nfc_llcp_dsap(skb);
706 ssap = nfc_llcp_ssap(skb);
708 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
709 if (llcp_sock == NULL) {
710 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
711 return;
714 sk = &llcp_sock->sk;
715 lock_sock(sk);
716 if (sk->sk_state == LLCP_CLOSED) {
717 release_sock(sk);
718 nfc_llcp_sock_put(llcp_sock);
722 if (sk->sk_state == LLCP_CONNECTED) {
723 nfc_put_device(local->dev);
724 sk->sk_state = LLCP_CLOSED;
725 sk->sk_state_change(sk);
728 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
730 release_sock(sk);
731 nfc_llcp_sock_put(llcp_sock);
734 static void nfc_llcp_recv_cc(struct nfc_llcp_local *local,
735 struct sk_buff *skb)
737 struct nfc_llcp_sock *llcp_sock;
738 u8 dsap, ssap;
741 dsap = nfc_llcp_dsap(skb);
742 ssap = nfc_llcp_ssap(skb);
744 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
746 if (llcp_sock == NULL)
747 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
749 if (llcp_sock == NULL) {
750 pr_err("Invalid CC\n");
751 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
753 return;
756 llcp_sock->dsap = ssap;
758 nfc_llcp_parse_tlv(local, &skb->data[LLCP_HEADER_SIZE],
759 skb->len - LLCP_HEADER_SIZE);
761 nfc_llcp_sock_put(llcp_sock);
764 static void nfc_llcp_rx_work(struct work_struct *work)
766 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
767 rx_work);
768 u8 dsap, ssap, ptype;
769 struct sk_buff *skb;
771 skb = local->rx_pending;
772 if (skb == NULL) {
773 pr_debug("No pending SKB\n");
774 return;
777 ptype = nfc_llcp_ptype(skb);
778 dsap = nfc_llcp_dsap(skb);
779 ssap = nfc_llcp_ssap(skb);
781 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
783 switch (ptype) {
784 case LLCP_PDU_SYMM:
785 pr_debug("SYMM\n");
786 break;
788 case LLCP_PDU_CONNECT:
789 pr_debug("CONNECT\n");
790 nfc_llcp_recv_connect(local, skb);
791 break;
793 case LLCP_PDU_DISC:
794 pr_debug("DISC\n");
795 nfc_llcp_recv_disc(local, skb);
796 break;
798 case LLCP_PDU_CC:
799 pr_debug("CC\n");
800 nfc_llcp_recv_cc(local, skb);
801 break;
803 case LLCP_PDU_I:
804 case LLCP_PDU_RR:
805 pr_debug("I frame\n");
806 nfc_llcp_recv_hdlc(local, skb);
807 break;
811 queue_work(local->tx_wq, &local->tx_work);
812 kfree_skb(local->rx_pending);
813 local->rx_pending = NULL;
815 return;
818 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
820 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
822 pr_debug("Received an LLCP PDU\n");
823 if (err < 0) {
824 pr_err("err %d", err);
825 return;
828 local->rx_pending = skb_get(skb);
829 del_timer(&local->link_timer);
830 queue_work(local->rx_wq, &local->rx_work);
832 return;
835 void nfc_llcp_mac_is_down(struct nfc_dev *dev)
837 struct nfc_llcp_local *local;
839 local = nfc_llcp_find_local(dev);
840 if (local == NULL)
841 return;
843 /* Close and purge all existing sockets */
844 nfc_llcp_socket_release(local);
847 void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
848 u8 comm_mode, u8 rf_mode)
850 struct nfc_llcp_local *local;
852 pr_debug("rf mode %d\n", rf_mode);
854 local = nfc_llcp_find_local(dev);
855 if (local == NULL)
856 return;
858 local->target_idx = target_idx;
859 local->comm_mode = comm_mode;
860 local->rf_mode = rf_mode;
862 if (rf_mode == NFC_RF_INITIATOR) {
863 pr_debug("Queueing Tx work\n");
865 queue_work(local->tx_wq, &local->tx_work);
866 } else {
867 mod_timer(&local->link_timer,
868 jiffies + msecs_to_jiffies(local->remote_lto));
872 int nfc_llcp_register_device(struct nfc_dev *ndev)
874 struct device *dev = &ndev->dev;
875 struct nfc_llcp_local *local;
876 char name[32];
877 int err;
879 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
880 if (local == NULL)
881 return -ENOMEM;
883 local->dev = ndev;
884 INIT_LIST_HEAD(&local->list);
885 mutex_init(&local->sdp_lock);
886 mutex_init(&local->socket_lock);
887 init_timer(&local->link_timer);
888 local->link_timer.data = (unsigned long) local;
889 local->link_timer.function = nfc_llcp_symm_timer;
891 skb_queue_head_init(&local->tx_queue);
892 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
893 snprintf(name, sizeof(name), "%s_llcp_tx_wq", dev_name(dev));
894 local->tx_wq = alloc_workqueue(name,
895 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
896 if (local->tx_wq == NULL) {
897 err = -ENOMEM;
898 goto err_local;
901 local->rx_pending = NULL;
902 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
903 snprintf(name, sizeof(name), "%s_llcp_rx_wq", dev_name(dev));
904 local->rx_wq = alloc_workqueue(name,
905 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
906 if (local->rx_wq == NULL) {
907 err = -ENOMEM;
908 goto err_tx_wq;
911 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
912 snprintf(name, sizeof(name), "%s_llcp_timeout_wq", dev_name(dev));
913 local->timeout_wq = alloc_workqueue(name,
914 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
915 if (local->timeout_wq == NULL) {
916 err = -ENOMEM;
917 goto err_rx_wq;
920 nfc_llcp_build_gb(local);
922 local->remote_miu = LLCP_DEFAULT_MIU;
923 local->remote_lto = LLCP_DEFAULT_LTO;
924 local->remote_rw = LLCP_DEFAULT_RW;
926 list_add(&llcp_devices, &local->list);
928 return 0;
930 err_rx_wq:
931 destroy_workqueue(local->rx_wq);
933 err_tx_wq:
934 destroy_workqueue(local->tx_wq);
936 err_local:
937 kfree(local);
939 return 0;
942 void nfc_llcp_unregister_device(struct nfc_dev *dev)
944 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
946 if (local == NULL) {
947 pr_debug("No such device\n");
948 return;
951 list_del(&local->list);
952 nfc_llcp_socket_release(local);
953 del_timer_sync(&local->link_timer);
954 skb_queue_purge(&local->tx_queue);
955 destroy_workqueue(local->tx_wq);
956 destroy_workqueue(local->rx_wq);
957 kfree_skb(local->rx_pending);
958 kfree(local);
961 int __init nfc_llcp_init(void)
963 INIT_LIST_HEAD(&llcp_devices);
965 return nfc_llcp_sock_init();
968 void nfc_llcp_exit(void)
970 nfc_llcp_sock_exit();