~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/nfc/llcp/llcp.c

Version: ~ [ linux-5.2-rc4 ] ~ [ linux-5.1.9 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.50 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.125 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.181 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.181 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.68 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2011  Intel Corporation. All rights reserved.
  3  *
  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.
  8  *
  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.
 13  *
 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.
 18  */
 19 
 20 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
 21 
 22 #include <linux/init.h>
 23 #include <linux/kernel.h>
 24 #include <linux/list.h>
 25 #include <linux/nfc.h>
 26 
 27 #include "../nfc.h"
 28 #include "llcp.h"
 29 
 30 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
 31 
 32 static struct list_head llcp_devices;
 33 
 34 void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
 35 {
 36         write_lock(&l->lock);
 37         sk_add_node(sk, &l->head);
 38         write_unlock(&l->lock);
 39 }
 40 
 41 void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
 42 {
 43         write_lock(&l->lock);
 44         sk_del_node_init(sk);
 45         write_unlock(&l->lock);
 46 }
 47 
 48 static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
 49 {
 50         struct nfc_llcp_local *local = sock->local;
 51         struct sk_buff *s, *tmp;
 52 
 53         pr_debug("%p\n", &sock->sk);
 54 
 55         skb_queue_purge(&sock->tx_queue);
 56         skb_queue_purge(&sock->tx_pending_queue);
 57         skb_queue_purge(&sock->tx_backlog_queue);
 58 
 59         if (local == NULL)
 60                 return;
 61 
 62         /* Search for local pending SKBs that are related to this socket */
 63         skb_queue_walk_safe(&local->tx_queue, s, tmp) {
 64                 if (s->sk != &sock->sk)
 65                         continue;
 66 
 67                 skb_unlink(s, &local->tx_queue);
 68                 kfree_skb(s);
 69         }
 70 }
 71 
 72 static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
 73 {
 74         struct sock *sk;
 75         struct hlist_node *node, *tmp;
 76         struct nfc_llcp_sock *llcp_sock;
 77 
 78         skb_queue_purge(&local->tx_queue);
 79 
 80         write_lock(&local->sockets.lock);
 81 
 82         sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
 83                 llcp_sock = nfc_llcp_sock(sk);
 84 
 85                 bh_lock_sock(sk);
 86 
 87                 nfc_llcp_socket_purge(llcp_sock);
 88 
 89                 if (sk->sk_state == LLCP_CONNECTED)
 90                         nfc_put_device(llcp_sock->dev);
 91 
 92                 if (sk->sk_state == LLCP_LISTEN) {
 93                         struct nfc_llcp_sock *lsk, *n;
 94                         struct sock *accept_sk;
 95 
 96                         list_for_each_entry_safe(lsk, n,
 97                                                  &llcp_sock->accept_queue,
 98                                                  accept_queue) {
 99                                 accept_sk = &lsk->sk;
100                                 bh_lock_sock(accept_sk);
101 
102                                 nfc_llcp_accept_unlink(accept_sk);
103 
104                                 accept_sk->sk_state = LLCP_CLOSED;
105 
106                                 bh_unlock_sock(accept_sk);
107 
108                                 sock_orphan(accept_sk);
109                         }
110 
111                         if (listen == true) {
112                                 bh_unlock_sock(sk);
113                                 continue;
114                         }
115                 }
116 
117                 /*
118                  * If we have a connection less socket bound, we keep it alive
119                  * if the device is still present.
120                  */
121                 if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
122                     listen == true) {
123                         bh_unlock_sock(sk);
124                         continue;
125                 }
126 
127                 sk->sk_state = LLCP_CLOSED;
128 
129                 bh_unlock_sock(sk);
130 
131                 sock_orphan(sk);
132 
133                 sk_del_node_init(sk);
134         }
135 
136         write_unlock(&local->sockets.lock);
137 }
138 
139 struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
140 {
141         kref_get(&local->ref);
142 
143         return local;
144 }
145 
146 static void local_release(struct kref *ref)
147 {
148         struct nfc_llcp_local *local;
149 
150         local = container_of(ref, struct nfc_llcp_local, ref);
151 
152         list_del(&local->list);
153         nfc_llcp_socket_release(local, false);
154         del_timer_sync(&local->link_timer);
155         skb_queue_purge(&local->tx_queue);
156         cancel_work_sync(&local->tx_work);
157         cancel_work_sync(&local->rx_work);
158         cancel_work_sync(&local->timeout_work);
159         kfree_skb(local->rx_pending);
160         kfree(local);
161 }
162 
163 int nfc_llcp_local_put(struct nfc_llcp_local *local)
164 {
165         if (local == NULL)
166                 return 0;
167 
168         return kref_put(&local->ref, local_release);
169 }
170 
171 static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
172                                                u8 ssap, u8 dsap)
173 {
174         struct sock *sk;
175         struct hlist_node *node;
176         struct nfc_llcp_sock *llcp_sock, *tmp_sock;
177 
178         pr_debug("ssap dsap %d %d\n", ssap, dsap);
179 
180         if (ssap == 0 && dsap == 0)
181                 return NULL;
182 
183         read_lock(&local->sockets.lock);
184 
185         llcp_sock = NULL;
186 
187         sk_for_each(sk, node, &local->sockets.head) {
188                 tmp_sock = nfc_llcp_sock(sk);
189 
190                 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
191                         llcp_sock = tmp_sock;
192                         break;
193                 }
194         }
195 
196         read_unlock(&local->sockets.lock);
197 
198         if (llcp_sock == NULL)
199                 return NULL;
200 
201         sock_hold(&llcp_sock->sk);
202 
203         return llcp_sock;
204 }
205 
206 static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
207 {
208         sock_put(&sock->sk);
209 }
210 
211 static void nfc_llcp_timeout_work(struct work_struct *work)
212 {
213         struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
214                                                     timeout_work);
215 
216         nfc_dep_link_down(local->dev);
217 }
218 
219 static void nfc_llcp_symm_timer(unsigned long data)
220 {
221         struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
222 
223         pr_err("SYMM timeout\n");
224 
225         schedule_work(&local->timeout_work);
226 }
227 
228 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
229 {
230         struct nfc_llcp_local *local, *n;
231 
232         list_for_each_entry_safe(local, n, &llcp_devices, list)
233                 if (local->dev == dev)
234                         return local;
235 
236         pr_debug("No device found\n");
237 
238         return NULL;
239 }
240 
241 static char *wks[] = {
242         NULL,
243         NULL, /* SDP */
244         "urn:nfc:sn:ip",
245         "urn:nfc:sn:obex",
246         "urn:nfc:sn:snep",
247 };
248 
249 static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
250 {
251         int sap, num_wks;
252 
253         pr_debug("%s\n", service_name);
254 
255         if (service_name == NULL)
256                 return -EINVAL;
257 
258         num_wks = ARRAY_SIZE(wks);
259 
260         for (sap = 0; sap < num_wks; sap++) {
261                 if (wks[sap] == NULL)
262                         continue;
263 
264                 if (strncmp(wks[sap], service_name, service_name_len) == 0)
265                         return sap;
266         }
267 
268         return -EINVAL;
269 }
270 
271 static
272 struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
273                                             u8 *sn, size_t sn_len)
274 {
275         struct sock *sk;
276         struct hlist_node *node;
277         struct nfc_llcp_sock *llcp_sock, *tmp_sock;
278 
279         pr_debug("sn %zd %p\n", sn_len, sn);
280 
281         if (sn == NULL || sn_len == 0)
282                 return NULL;
283 
284         read_lock(&local->sockets.lock);
285 
286         llcp_sock = NULL;
287 
288         sk_for_each(sk, node, &local->sockets.head) {
289                 tmp_sock = nfc_llcp_sock(sk);
290 
291                 pr_debug("llcp sock %p\n", tmp_sock);
292 
293                 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
294                     tmp_sock->sk.sk_state != LLCP_LISTEN)
295                         continue;
296 
297                 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
298                     tmp_sock->sk.sk_state != LLCP_BOUND)
299                         continue;
300 
301                 if (tmp_sock->service_name == NULL ||
302                     tmp_sock->service_name_len == 0)
303                         continue;
304 
305                 if (tmp_sock->service_name_len != sn_len)
306                         continue;
307 
308                 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
309                         llcp_sock = tmp_sock;
310                         break;
311                 }
312         }
313 
314         read_unlock(&local->sockets.lock);
315 
316         pr_debug("Found llcp sock %p\n", llcp_sock);
317 
318         return llcp_sock;
319 }
320 
321 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
322                          struct nfc_llcp_sock *sock)
323 {
324         mutex_lock(&local->sdp_lock);
325 
326         if (sock->service_name != NULL && sock->service_name_len > 0) {
327                 int ssap = nfc_llcp_wks_sap(sock->service_name,
328                                             sock->service_name_len);
329 
330                 if (ssap > 0) {
331                         pr_debug("WKS %d\n", ssap);
332 
333                         /* This is a WKS, let's check if it's free */
334                         if (local->local_wks & BIT(ssap)) {
335                                 mutex_unlock(&local->sdp_lock);
336 
337                                 return LLCP_SAP_MAX;
338                         }
339 
340                         set_bit(ssap, &local->local_wks);
341                         mutex_unlock(&local->sdp_lock);
342 
343                         return ssap;
344                 }
345 
346                 /*
347                  * Check if there already is a non WKS socket bound
348                  * to this service name.
349                  */
350                 if (nfc_llcp_sock_from_sn(local, sock->service_name,
351                                           sock->service_name_len) != NULL) {
352                         mutex_unlock(&local->sdp_lock);
353 
354                         return LLCP_SAP_MAX;
355                 }
356 
357                 mutex_unlock(&local->sdp_lock);
358 
359                 return LLCP_SDP_UNBOUND;
360 
361         } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
362                 if (!test_bit(sock->ssap, &local->local_wks)) {
363                         set_bit(sock->ssap, &local->local_wks);
364                         mutex_unlock(&local->sdp_lock);
365 
366                         return sock->ssap;
367                 }
368         }
369 
370         mutex_unlock(&local->sdp_lock);
371 
372         return LLCP_SAP_MAX;
373 }
374 
375 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
376 {
377         u8 local_ssap;
378 
379         mutex_lock(&local->sdp_lock);
380 
381         local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
382         if (local_ssap == LLCP_LOCAL_NUM_SAP) {
383                 mutex_unlock(&local->sdp_lock);
384                 return LLCP_SAP_MAX;
385         }
386 
387         set_bit(local_ssap, &local->local_sap);
388 
389         mutex_unlock(&local->sdp_lock);
390 
391         return local_ssap + LLCP_LOCAL_SAP_OFFSET;
392 }
393 
394 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
395 {
396         u8 local_ssap;
397         unsigned long *sdp;
398 
399         if (ssap < LLCP_WKS_NUM_SAP) {
400                 local_ssap = ssap;
401                 sdp = &local->local_wks;
402         } else if (ssap < LLCP_LOCAL_NUM_SAP) {
403                 atomic_t *client_cnt;
404 
405                 local_ssap = ssap - LLCP_WKS_NUM_SAP;
406                 sdp = &local->local_sdp;
407                 client_cnt = &local->local_sdp_cnt[local_ssap];
408 
409                 pr_debug("%d clients\n", atomic_read(client_cnt));
410 
411                 mutex_lock(&local->sdp_lock);
412 
413                 if (atomic_dec_and_test(client_cnt)) {
414                         struct nfc_llcp_sock *l_sock;
415 
416                         pr_debug("No more clients for SAP %d\n", ssap);
417 
418                         clear_bit(local_ssap, sdp);
419 
420                         /* Find the listening sock and set it back to UNBOUND */
421                         l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
422                         if (l_sock) {
423                                 l_sock->ssap = LLCP_SDP_UNBOUND;
424                                 nfc_llcp_sock_put(l_sock);
425                         }
426                 }
427 
428                 mutex_unlock(&local->sdp_lock);
429 
430                 return;
431         } else if (ssap < LLCP_MAX_SAP) {
432                 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
433                 sdp = &local->local_sap;
434         } else {
435                 return;
436         }
437 
438         mutex_lock(&local->sdp_lock);
439 
440         clear_bit(local_ssap, sdp);
441 
442         mutex_unlock(&local->sdp_lock);
443 }
444 
445 static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
446 {
447         u8 ssap;
448 
449         mutex_lock(&local->sdp_lock);
450 
451         ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
452         if (ssap == LLCP_SDP_NUM_SAP) {
453                 mutex_unlock(&local->sdp_lock);
454 
455                 return LLCP_SAP_MAX;
456         }
457 
458         pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
459 
460         set_bit(ssap, &local->local_sdp);
461 
462         mutex_unlock(&local->sdp_lock);
463 
464         return LLCP_WKS_NUM_SAP + ssap;
465 }
466 
467 static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
468 {
469         u8 *gb_cur, *version_tlv, version, version_length;
470         u8 *lto_tlv, lto_length;
471         u8 *wks_tlv, wks_length;
472         u8 *miux_tlv, miux_length;
473         u8 gb_len = 0;
474         int ret = 0;
475 
476         version = LLCP_VERSION_11;
477         version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
478                                          1, &version_length);
479         gb_len += version_length;
480 
481         lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
482         gb_len += lto_length;
483 
484         pr_debug("Local wks 0x%lx\n", local->local_wks);
485         wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
486                                      &wks_length);
487         gb_len += wks_length;
488 
489         miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
490                                       &miux_length);
491         gb_len += miux_length;
492 
493         gb_len += ARRAY_SIZE(llcp_magic);
494 
495         if (gb_len > NFC_MAX_GT_LEN) {
496                 ret = -EINVAL;
497                 goto out;
498         }
499 
500         gb_cur = local->gb;
501 
502         memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
503         gb_cur += ARRAY_SIZE(llcp_magic);
504 
505         memcpy(gb_cur, version_tlv, version_length);
506         gb_cur += version_length;
507 
508         memcpy(gb_cur, lto_tlv, lto_length);
509         gb_cur += lto_length;
510 
511         memcpy(gb_cur, wks_tlv, wks_length);
512         gb_cur += wks_length;
513 
514         memcpy(gb_cur, miux_tlv, miux_length);
515         gb_cur += miux_length;
516 
517         local->gb_len = gb_len;
518 
519 out:
520         kfree(version_tlv);
521         kfree(lto_tlv);
522         kfree(wks_tlv);
523         kfree(miux_tlv);
524 
525         return ret;
526 }
527 
528 u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
529 {
530         struct nfc_llcp_local *local;
531 
532         local = nfc_llcp_find_local(dev);
533         if (local == NULL) {
534                 *general_bytes_len = 0;
535                 return NULL;
536         }
537 
538         nfc_llcp_build_gb(local);
539 
540         *general_bytes_len = local->gb_len;
541 
542         return local->gb;
543 }
544 
545 int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
546 {
547         struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
548 
549         if (local == NULL) {
550                 pr_err("No LLCP device\n");
551                 return -ENODEV;
552         }
553 
554         memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
555         memcpy(local->remote_gb, gb, gb_len);
556         local->remote_gb_len = gb_len;
557 
558         if (local->remote_gb == NULL || local->remote_gb_len == 0)
559                 return -ENODEV;
560 
561         if (memcmp(local->remote_gb, llcp_magic, 3)) {
562                 pr_err("MAC does not support LLCP\n");
563                 return -EINVAL;
564         }
565 
566         return nfc_llcp_parse_gb_tlv(local,
567                                      &local->remote_gb[3],
568                                      local->remote_gb_len - 3);
569 }
570 
571 static u8 nfc_llcp_dsap(struct sk_buff *pdu)
572 {
573         return (pdu->data[0] & 0xfc) >> 2;
574 }
575 
576 static u8 nfc_llcp_ptype(struct sk_buff *pdu)
577 {
578         return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
579 }
580 
581 static u8 nfc_llcp_ssap(struct sk_buff *pdu)
582 {
583         return pdu->data[1] & 0x3f;
584 }
585 
586 static u8 nfc_llcp_ns(struct sk_buff *pdu)
587 {
588         return pdu->data[2] >> 4;
589 }
590 
591 static u8 nfc_llcp_nr(struct sk_buff *pdu)
592 {
593         return pdu->data[2] & 0xf;
594 }
595 
596 static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
597 {
598         pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
599         sock->send_n = (sock->send_n + 1) % 16;
600         sock->recv_ack_n = (sock->recv_n - 1) % 16;
601 }
602 
603 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
604                                struct sk_buff *skb, u8 direction)
605 {
606         struct hlist_node *node;
607         struct sk_buff *skb_copy = NULL, *nskb;
608         struct sock *sk;
609         u8 *data;
610 
611         read_lock(&local->raw_sockets.lock);
612 
613         sk_for_each(sk, node, &local->raw_sockets.head) {
614                 if (sk->sk_state != LLCP_BOUND)
615                         continue;
616 
617                 if (skb_copy == NULL) {
618                         skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
619                                                GFP_ATOMIC);
620 
621                         if (skb_copy == NULL)
622                                 continue;
623 
624                         data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
625 
626                         data[0] = local->dev ? local->dev->idx : 0xFF;
627                         data[1] = direction;
628                 }
629 
630                 nskb = skb_clone(skb_copy, GFP_ATOMIC);
631                 if (!nskb)
632                         continue;
633 
634                 if (sock_queue_rcv_skb(sk, nskb))
635                         kfree_skb(nskb);
636         }
637 
638         read_unlock(&local->raw_sockets.lock);
639 
640         kfree_skb(skb_copy);
641 }
642 
643 static void nfc_llcp_tx_work(struct work_struct *work)
644 {
645         struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
646                                                     tx_work);
647         struct sk_buff *skb;
648         struct sock *sk;
649         struct nfc_llcp_sock *llcp_sock;
650 
651         skb = skb_dequeue(&local->tx_queue);
652         if (skb != NULL) {
653                 sk = skb->sk;
654                 llcp_sock = nfc_llcp_sock(sk);
655 
656                 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
657                         nfc_llcp_send_symm(local->dev);
658                 } else {
659                         struct sk_buff *copy_skb = NULL;
660                         u8 ptype = nfc_llcp_ptype(skb);
661                         int ret;
662 
663                         pr_debug("Sending pending skb\n");
664                         print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
665                                        DUMP_PREFIX_OFFSET, 16, 1,
666                                        skb->data, skb->len, true);
667 
668                         if (ptype == LLCP_PDU_I)
669                                 copy_skb = skb_copy(skb, GFP_ATOMIC);
670 
671                         nfc_llcp_send_to_raw_sock(local, skb,
672                                                   NFC_LLCP_DIRECTION_TX);
673 
674                         ret = nfc_data_exchange(local->dev, local->target_idx,
675                                                 skb, nfc_llcp_recv, local);
676 
677                         if (ret) {
678                                 kfree_skb(copy_skb);
679                                 goto out;
680                         }
681 
682                         if (ptype == LLCP_PDU_I && copy_skb)
683                                 skb_queue_tail(&llcp_sock->tx_pending_queue,
684                                                copy_skb);
685                 }
686         } else {
687                 nfc_llcp_send_symm(local->dev);
688         }
689 
690 out:
691         mod_timer(&local->link_timer,
692                   jiffies + msecs_to_jiffies(2 * local->remote_lto));
693 }
694 
695 static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
696                                                           u8 ssap)
697 {
698         struct sock *sk;
699         struct nfc_llcp_sock *llcp_sock;
700         struct hlist_node *node;
701 
702         read_lock(&local->connecting_sockets.lock);
703 
704         sk_for_each(sk, node, &local->connecting_sockets.head) {
705                 llcp_sock = nfc_llcp_sock(sk);
706 
707                 if (llcp_sock->ssap == ssap) {
708                         sock_hold(&llcp_sock->sk);
709                         goto out;
710                 }
711         }
712 
713         llcp_sock = NULL;
714 
715 out:
716         read_unlock(&local->connecting_sockets.lock);
717 
718         return llcp_sock;
719 }
720 
721 static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
722                                                   u8 *sn, size_t sn_len)
723 {
724         struct nfc_llcp_sock *llcp_sock;
725 
726         llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
727 
728         if (llcp_sock == NULL)
729                 return NULL;
730 
731         sock_hold(&llcp_sock->sk);
732 
733         return llcp_sock;
734 }
735 
736 static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
737 {
738         u8 *tlv = &skb->data[2], type, length;
739         size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
740 
741         while (offset < tlv_array_len) {
742                 type = tlv[0];
743                 length = tlv[1];
744 
745                 pr_debug("type 0x%x length %d\n", type, length);
746 
747                 if (type == LLCP_TLV_SN) {
748                         *sn_len = length;
749                         return &tlv[2];
750                 }
751 
752                 offset += length + 2;
753                 tlv += length + 2;
754         }
755 
756         return NULL;
757 }
758 
759 static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
760                              struct sk_buff *skb)
761 {
762         struct nfc_llcp_sock *llcp_sock;
763         struct nfc_llcp_ui_cb *ui_cb;
764         u8 dsap, ssap;
765 
766         dsap = nfc_llcp_dsap(skb);
767         ssap = nfc_llcp_ssap(skb);
768 
769         ui_cb = nfc_llcp_ui_skb_cb(skb);
770         ui_cb->dsap = dsap;
771         ui_cb->ssap = ssap;
772 
773         printk("%s %d %d\n", __func__, dsap, ssap);
774 
775         pr_debug("%d %d\n", dsap, ssap);
776 
777         /* We're looking for a bound socket, not a client one */
778         llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
779         if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
780                 return;
781 
782         /* There is no sequence with UI frames */
783         skb_pull(skb, LLCP_HEADER_SIZE);
784         if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
785                 pr_err("receive queue is full\n");
786                 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
787         }
788 
789         nfc_llcp_sock_put(llcp_sock);
790 }
791 
792 static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
793                                   struct sk_buff *skb)
794 {
795         struct sock *new_sk, *parent;
796         struct nfc_llcp_sock *sock, *new_sock;
797         u8 dsap, ssap, reason;
798 
799         dsap = nfc_llcp_dsap(skb);
800         ssap = nfc_llcp_ssap(skb);
801 
802         pr_debug("%d %d\n", dsap, ssap);
803 
804         if (dsap != LLCP_SAP_SDP) {
805                 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
806                 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
807                         reason = LLCP_DM_NOBOUND;
808                         goto fail;
809                 }
810         } else {
811                 u8 *sn;
812                 size_t sn_len;
813 
814                 sn = nfc_llcp_connect_sn(skb, &sn_len);
815                 if (sn == NULL) {
816                         reason = LLCP_DM_NOBOUND;
817                         goto fail;
818                 }
819 
820                 pr_debug("Service name length %zu\n", sn_len);
821 
822                 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
823                 if (sock == NULL) {
824                         reason = LLCP_DM_NOBOUND;
825                         goto fail;
826                 }
827         }
828 
829         lock_sock(&sock->sk);
830 
831         parent = &sock->sk;
832 
833         if (sk_acceptq_is_full(parent)) {
834                 reason = LLCP_DM_REJ;
835                 release_sock(&sock->sk);
836                 sock_put(&sock->sk);
837                 goto fail;
838         }
839 
840         if (sock->ssap == LLCP_SDP_UNBOUND) {
841                 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
842 
843                 pr_debug("First client, reserving %d\n", ssap);
844 
845                 if (ssap == LLCP_SAP_MAX) {
846                         reason = LLCP_DM_REJ;
847                         release_sock(&sock->sk);
848                         sock_put(&sock->sk);
849                         goto fail;
850                 }
851 
852                 sock->ssap = ssap;
853         }
854 
855         new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
856         if (new_sk == NULL) {
857                 reason = LLCP_DM_REJ;
858                 release_sock(&sock->sk);
859                 sock_put(&sock->sk);
860                 goto fail;
861         }
862 
863         new_sock = nfc_llcp_sock(new_sk);
864         new_sock->dev = local->dev;
865         new_sock->local = nfc_llcp_local_get(local);
866         new_sock->miu = local->remote_miu;
867         new_sock->nfc_protocol = sock->nfc_protocol;
868         new_sock->dsap = ssap;
869         new_sock->target_idx = local->target_idx;
870         new_sock->parent = parent;
871         new_sock->ssap = sock->ssap;
872         if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
873                 atomic_t *client_count;
874 
875                 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
876 
877                 client_count =
878                         &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
879 
880                 atomic_inc(client_count);
881                 new_sock->reserved_ssap = sock->ssap;
882         }
883 
884         nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
885                                       skb->len - LLCP_HEADER_SIZE);
886 
887         pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
888 
889         nfc_llcp_sock_link(&local->sockets, new_sk);
890 
891         nfc_llcp_accept_enqueue(&sock->sk, new_sk);
892 
893         nfc_get_device(local->dev->idx);
894 
895         new_sk->sk_state = LLCP_CONNECTED;
896 
897         /* Wake the listening processes */
898         parent->sk_data_ready(parent, 0);
899 
900         /* Send CC */
901         nfc_llcp_send_cc(new_sock);
902 
903         release_sock(&sock->sk);
904         sock_put(&sock->sk);
905 
906         return;
907 
908 fail:
909         /* Send DM */
910         nfc_llcp_send_dm(local, dsap, ssap, reason);
911 }
912 
913 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
914 {
915         int nr_frames = 0;
916         struct nfc_llcp_local *local = sock->local;
917 
918         pr_debug("Remote ready %d tx queue len %d remote rw %d",
919                  sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
920                  sock->rw);
921 
922         /* Try to queue some I frames for transmission */
923         while (sock->remote_ready &&
924                skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
925                 struct sk_buff *pdu;
926 
927                 pdu = skb_dequeue(&sock->tx_queue);
928                 if (pdu == NULL)
929                         break;
930 
931                 /* Update N(S)/N(R) */
932                 nfc_llcp_set_nrns(sock, pdu);
933 
934                 skb_queue_tail(&local->tx_queue, pdu);
935                 nr_frames++;
936         }
937 
938         return nr_frames;
939 }
940 
941 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
942                                struct sk_buff *skb)
943 {
944         struct nfc_llcp_sock *llcp_sock;
945         struct sock *sk;
946         u8 dsap, ssap, ptype, ns, nr;
947 
948         ptype = nfc_llcp_ptype(skb);
949         dsap = nfc_llcp_dsap(skb);
950         ssap = nfc_llcp_ssap(skb);
951         ns = nfc_llcp_ns(skb);
952         nr = nfc_llcp_nr(skb);
953 
954         pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
955 
956         llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
957         if (llcp_sock == NULL) {
958                 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
959                 return;
960         }
961 
962         sk = &llcp_sock->sk;
963         lock_sock(sk);
964         if (sk->sk_state == LLCP_CLOSED) {
965                 release_sock(sk);
966                 nfc_llcp_sock_put(llcp_sock);
967         }
968 
969         /* Pass the payload upstream */
970         if (ptype == LLCP_PDU_I) {
971                 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
972 
973                 if (ns == llcp_sock->recv_n)
974                         llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
975                 else
976                         pr_err("Received out of sequence I PDU\n");
977 
978                 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
979                 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
980                         pr_err("receive queue is full\n");
981                         skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
982                 }
983         }
984 
985         /* Remove skbs from the pending queue */
986         if (llcp_sock->send_ack_n != nr) {
987                 struct sk_buff *s, *tmp;
988                 u8 n;
989 
990                 llcp_sock->send_ack_n = nr;
991 
992                 /* Remove and free all skbs until ns == nr */
993                 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
994                         n = nfc_llcp_ns(s);
995 
996                         skb_unlink(s, &llcp_sock->tx_pending_queue);
997                         kfree_skb(s);
998 
999                         if (n == nr)
1000                                 break;
1001                 }
1002 
1003                 /* Re-queue the remaining skbs for transmission */
1004                 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1005                                             s, tmp) {
1006                         skb_unlink(s, &llcp_sock->tx_pending_queue);
1007                         skb_queue_head(&local->tx_queue, s);
1008                 }
1009         }
1010 
1011         if (ptype == LLCP_PDU_RR)
1012                 llcp_sock->remote_ready = true;
1013         else if (ptype == LLCP_PDU_RNR)
1014                 llcp_sock->remote_ready = false;
1015 
1016         if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
1017                 nfc_llcp_send_rr(llcp_sock);
1018 
1019         release_sock(sk);
1020         nfc_llcp_sock_put(llcp_sock);
1021 }
1022 
1023 static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
1024                                struct sk_buff *skb)
1025 {
1026         struct nfc_llcp_sock *llcp_sock;
1027         struct sock *sk;
1028         u8 dsap, ssap;
1029 
1030         dsap = nfc_llcp_dsap(skb);
1031         ssap = nfc_llcp_ssap(skb);
1032 
1033         llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1034         if (llcp_sock == NULL) {
1035                 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1036                 return;
1037         }
1038 
1039         sk = &llcp_sock->sk;
1040         lock_sock(sk);
1041 
1042         nfc_llcp_socket_purge(llcp_sock);
1043 
1044         if (sk->sk_state == LLCP_CLOSED) {
1045                 release_sock(sk);
1046                 nfc_llcp_sock_put(llcp_sock);
1047         }
1048 
1049         if (sk->sk_state == LLCP_CONNECTED) {
1050                 nfc_put_device(local->dev);
1051                 sk->sk_state = LLCP_CLOSED;
1052                 sk->sk_state_change(sk);
1053         }
1054 
1055         nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1056 
1057         release_sock(sk);
1058         nfc_llcp_sock_put(llcp_sock);
1059 }
1060 
1061 static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
1062 {
1063         struct nfc_llcp_sock *llcp_sock;
1064         struct sock *sk;
1065         u8 dsap, ssap;
1066 
1067         dsap = nfc_llcp_dsap(skb);
1068         ssap = nfc_llcp_ssap(skb);
1069 
1070         llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1071         if (llcp_sock == NULL) {
1072                 pr_err("Invalid CC\n");
1073                 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1074 
1075                 return;
1076         }
1077 
1078         sk = &llcp_sock->sk;
1079 
1080         /* Unlink from connecting and link to the client array */
1081         nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1082         nfc_llcp_sock_link(&local->sockets, sk);
1083         llcp_sock->dsap = ssap;
1084 
1085         nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1086                                       skb->len - LLCP_HEADER_SIZE);
1087 
1088         sk->sk_state = LLCP_CONNECTED;
1089         sk->sk_state_change(sk);
1090 
1091         nfc_llcp_sock_put(llcp_sock);
1092 }
1093 
1094 static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1095 {
1096         struct nfc_llcp_sock *llcp_sock;
1097         struct sock *sk;
1098         u8 dsap, ssap, reason;
1099 
1100         dsap = nfc_llcp_dsap(skb);
1101         ssap = nfc_llcp_ssap(skb);
1102         reason = skb->data[2];
1103 
1104         pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1105 
1106         switch (reason) {
1107         case LLCP_DM_NOBOUND:
1108         case LLCP_DM_REJ:
1109                 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1110                 break;
1111 
1112         default:
1113                 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1114                 break;
1115         }
1116 
1117         if (llcp_sock == NULL) {
1118                 pr_debug("Already closed\n");
1119                 return;
1120         }
1121 
1122         sk = &llcp_sock->sk;
1123 
1124         sk->sk_err = ENXIO;
1125         sk->sk_state = LLCP_CLOSED;
1126         sk->sk_state_change(sk);
1127 
1128         nfc_llcp_sock_put(llcp_sock);
1129 }
1130 
1131 static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1132                               struct sk_buff *skb)
1133 {
1134         struct nfc_llcp_sock *llcp_sock;
1135         u8 dsap, ssap, *tlv, type, length, tid, sap;
1136         u16 tlv_len, offset;
1137         char *service_name;
1138         size_t service_name_len;
1139 
1140         dsap = nfc_llcp_dsap(skb);
1141         ssap = nfc_llcp_ssap(skb);
1142 
1143         pr_debug("%d %d\n", dsap, ssap);
1144 
1145         if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1146                 pr_err("Wrong SNL SAP\n");
1147                 return;
1148         }
1149 
1150         tlv = &skb->data[LLCP_HEADER_SIZE];
1151         tlv_len = skb->len - LLCP_HEADER_SIZE;
1152         offset = 0;
1153 
1154         while (offset < tlv_len) {
1155                 type = tlv[0];
1156                 length = tlv[1];
1157 
1158                 switch (type) {
1159                 case LLCP_TLV_SDREQ:
1160                         tid = tlv[2];
1161                         service_name = (char *) &tlv[3];
1162                         service_name_len = length - 1;
1163 
1164                         pr_debug("Looking for %.16s\n", service_name);
1165 
1166                         if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1167                             !strncmp(service_name, "urn:nfc:sn:sdp",
1168                                      service_name_len)) {
1169                                 sap = 1;
1170                                 goto send_snl;
1171                         }
1172 
1173                         llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1174                                                           service_name_len);
1175                         if (!llcp_sock) {
1176                                 sap = 0;
1177                                 goto send_snl;
1178                         }
1179 
1180                         /*
1181                          * We found a socket but its ssap has not been reserved
1182                          * yet. We need to assign it for good and send a reply.
1183                          * The ssap will be freed when the socket is closed.
1184                          */
1185                         if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1186                                 atomic_t *client_count;
1187 
1188                                 sap = nfc_llcp_reserve_sdp_ssap(local);
1189 
1190                                 pr_debug("Reserving %d\n", sap);
1191 
1192                                 if (sap == LLCP_SAP_MAX) {
1193                                         sap = 0;
1194                                         goto send_snl;
1195                                 }
1196 
1197                                 client_count =
1198                                         &local->local_sdp_cnt[sap -
1199                                                               LLCP_WKS_NUM_SAP];
1200 
1201                                 atomic_inc(client_count);
1202 
1203                                 llcp_sock->ssap = sap;
1204                                 llcp_sock->reserved_ssap = sap;
1205                         } else {
1206                                 sap = llcp_sock->ssap;
1207                         }
1208 
1209                         pr_debug("%p %d\n", llcp_sock, sap);
1210 
1211 send_snl:
1212                         nfc_llcp_send_snl(local, tid, sap);
1213                         break;
1214 
1215                 default:
1216                         pr_err("Invalid SNL tlv value 0x%x\n", type);
1217                         break;
1218                 }
1219 
1220                 offset += length + 2;
1221                 tlv += length + 2;
1222         }
1223 }
1224 
1225 static void nfc_llcp_rx_work(struct work_struct *work)
1226 {
1227         struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
1228                                                     rx_work);
1229         u8 dsap, ssap, ptype;
1230         struct sk_buff *skb;
1231 
1232         skb = local->rx_pending;
1233         if (skb == NULL) {
1234                 pr_debug("No pending SKB\n");
1235                 return;
1236         }
1237 
1238         ptype = nfc_llcp_ptype(skb);
1239         dsap = nfc_llcp_dsap(skb);
1240         ssap = nfc_llcp_ssap(skb);
1241 
1242         pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1243 
1244         if (ptype != LLCP_PDU_SYMM)
1245                 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1246                                16, 1, skb->data, skb->len, true);
1247 
1248         nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1249 
1250         switch (ptype) {
1251         case LLCP_PDU_SYMM:
1252                 pr_debug("SYMM\n");
1253                 break;
1254 
1255         case LLCP_PDU_UI:
1256                 pr_debug("UI\n");
1257                 nfc_llcp_recv_ui(local, skb);
1258                 break;
1259 
1260         case LLCP_PDU_CONNECT:
1261                 pr_debug("CONNECT\n");
1262                 nfc_llcp_recv_connect(local, skb);
1263                 break;
1264 
1265         case LLCP_PDU_DISC:
1266                 pr_debug("DISC\n");
1267                 nfc_llcp_recv_disc(local, skb);
1268                 break;
1269 
1270         case LLCP_PDU_CC:
1271                 pr_debug("CC\n");
1272                 nfc_llcp_recv_cc(local, skb);
1273                 break;
1274 
1275         case LLCP_PDU_DM:
1276                 pr_debug("DM\n");
1277                 nfc_llcp_recv_dm(local, skb);
1278                 break;
1279 
1280         case LLCP_PDU_SNL:
1281                 pr_debug("SNL\n");
1282                 nfc_llcp_recv_snl(local, skb);
1283                 break;
1284 
1285         case LLCP_PDU_I:
1286         case LLCP_PDU_RR:
1287         case LLCP_PDU_RNR:
1288                 pr_debug("I frame\n");
1289                 nfc_llcp_recv_hdlc(local, skb);
1290                 break;
1291 
1292         }
1293 
1294         schedule_work(&local->tx_work);
1295         kfree_skb(local->rx_pending);
1296         local->rx_pending = NULL;
1297 }
1298 
1299 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1300 {
1301         struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1302 
1303         pr_debug("Received an LLCP PDU\n");
1304         if (err < 0) {
1305                 pr_err("err %d\n", err);
1306                 return;
1307         }
1308 
1309         local->rx_pending = skb_get(skb);
1310         del_timer(&local->link_timer);
1311         schedule_work(&local->rx_work);
1312 }
1313 
1314 int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1315 {
1316         struct nfc_llcp_local *local;
1317 
1318         local = nfc_llcp_find_local(dev);
1319         if (local == NULL)
1320                 return -ENODEV;
1321 
1322         local->rx_pending = skb_get(skb);
1323         del_timer(&local->link_timer);
1324         schedule_work(&local->rx_work);
1325 
1326         return 0;
1327 }
1328 
1329 void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1330 {
1331         struct nfc_llcp_local *local;
1332 
1333         local = nfc_llcp_find_local(dev);
1334         if (local == NULL)
1335                 return;
1336 
1337         /* Close and purge all existing sockets */
1338         nfc_llcp_socket_release(local, true);
1339 }
1340 
1341 void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1342                         u8 comm_mode, u8 rf_mode)
1343 {
1344         struct nfc_llcp_local *local;
1345 
1346         pr_debug("rf mode %d\n", rf_mode);
1347 
1348         local = nfc_llcp_find_local(dev);
1349         if (local == NULL)
1350                 return;
1351 
1352         local->target_idx = target_idx;
1353         local->comm_mode = comm_mode;
1354         local->rf_mode = rf_mode;
1355 
1356         if (rf_mode == NFC_RF_INITIATOR) {
1357                 pr_debug("Queueing Tx work\n");
1358 
1359                 schedule_work(&local->tx_work);
1360         } else {
1361                 mod_timer(&local->link_timer,
1362                           jiffies + msecs_to_jiffies(local->remote_lto));
1363         }
1364 }
1365 
1366 int nfc_llcp_register_device(struct nfc_dev *ndev)
1367 {
1368         struct nfc_llcp_local *local;
1369 
1370         local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1371         if (local == NULL)
1372                 return -ENOMEM;
1373 
1374         local->dev = ndev;
1375         INIT_LIST_HEAD(&local->list);
1376         kref_init(&local->ref);
1377         mutex_init(&local->sdp_lock);
1378         init_timer(&local->link_timer);
1379         local->link_timer.data = (unsigned long) local;
1380         local->link_timer.function = nfc_llcp_symm_timer;
1381 
1382         skb_queue_head_init(&local->tx_queue);
1383         INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
1384 
1385         local->rx_pending = NULL;
1386         INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
1387 
1388         INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
1389 
1390         rwlock_init(&local->sockets.lock);
1391         rwlock_init(&local->connecting_sockets.lock);
1392         rwlock_init(&local->raw_sockets.lock);
1393 
1394         local->lto = 150; /* 1500 ms */
1395         local->rw = LLCP_MAX_RW;
1396         local->miux = cpu_to_be16(LLCP_MAX_MIUX);
1397 
1398         nfc_llcp_build_gb(local);
1399 
1400         local->remote_miu = LLCP_DEFAULT_MIU;
1401         local->remote_lto = LLCP_DEFAULT_LTO;
1402 
1403         list_add(&local->list, &llcp_devices);
1404 
1405         return 0;
1406 }
1407 
1408 void nfc_llcp_unregister_device(struct nfc_dev *dev)
1409 {
1410         struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1411 
1412         if (local == NULL) {
1413                 pr_debug("No such device\n");
1414                 return;
1415         }
1416 
1417         nfc_llcp_local_put(local);
1418 }
1419 
1420 int __init nfc_llcp_init(void)
1421 {
1422         INIT_LIST_HEAD(&llcp_devices);
1423 
1424         return nfc_llcp_sock_init();
1425 }
1426 
1427 void nfc_llcp_exit(void)
1428 {
1429         nfc_llcp_sock_exit();
1430 }
1431 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp