blob: 81cd3416c7d4dfaae5805441adb6179b210374a3 [file] [log] [blame]
Samuel Ortizd6469602011-12-14 16:43:12 +01001/*
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
Samuel Ortiz30cc4582013-04-26 11:49:40 +020027#include "nfc.h"
Samuel Ortizd6469602011-12-14 16:43:12 +010028#include "llcp.h"
29
30static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
31
32static struct list_head llcp_devices;
33
Thierry Escande098dafc2013-03-29 11:47:43 +010034static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
35
Samuel Ortiza69f32a2012-05-04 17:04:19 +020036void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
37{
38 write_lock(&l->lock);
39 sk_add_node(sk, &l->head);
40 write_unlock(&l->lock);
41}
42
43void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
44{
45 write_lock(&l->lock);
46 sk_del_node_init(sk);
47 write_unlock(&l->lock);
48}
49
Thierry Escandeabd18d42013-04-02 10:25:15 +020050void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock)
51{
52 sock->remote_rw = LLCP_DEFAULT_RW;
53 sock->remote_miu = LLCP_MAX_MIU + 1;
54}
55
Samuel Ortizf31652a2012-10-26 18:20:10 +020056static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
57{
58 struct nfc_llcp_local *local = sock->local;
59 struct sk_buff *s, *tmp;
60
61 pr_debug("%p\n", &sock->sk);
62
63 skb_queue_purge(&sock->tx_queue);
64 skb_queue_purge(&sock->tx_pending_queue);
Samuel Ortizf31652a2012-10-26 18:20:10 +020065
66 if (local == NULL)
67 return;
68
69 /* Search for local pending SKBs that are related to this socket */
70 skb_queue_walk_safe(&local->tx_queue, s, tmp) {
71 if (s->sk != &sock->sk)
72 continue;
73
74 skb_unlink(s, &local->tx_queue);
75 kfree_skb(s);
76 }
77}
78
Samuel Ortizb436a132013-04-03 16:34:19 +020079static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +010080 int err)
Samuel Ortizd6469602011-12-14 16:43:12 +010081{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020082 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -080083 struct hlist_node *tmp;
Samuel Ortiza69f32a2012-05-04 17:04:19 +020084 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010085
Samuel Ortizf31652a2012-10-26 18:20:10 +020086 skb_queue_purge(&local->tx_queue);
87
Samuel Ortiza69f32a2012-05-04 17:04:19 +020088 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010089
Sasha Levinb67bfe02013-02-27 17:06:00 -080090 sk_for_each_safe(sk, tmp, &local->sockets.head) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +020091 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010092
Szymon Janc50b78b22012-09-26 14:22:10 +020093 bh_lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010094
Samuel Ortizf31652a2012-10-26 18:20:10 +020095 nfc_llcp_socket_purge(llcp_sock);
96
Samuel Ortiza69f32a2012-05-04 17:04:19 +020097 if (sk->sk_state == LLCP_CONNECTED)
98 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010099
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200100 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100101 struct nfc_llcp_sock *lsk, *n;
102 struct sock *accept_sk;
103
Szymon Janc0f450772012-10-17 15:23:39 +0200104 list_for_each_entry_safe(lsk, n,
105 &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100106 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100107 accept_sk = &lsk->sk;
Szymon Janc50b78b22012-09-26 14:22:10 +0200108 bh_lock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100109
110 nfc_llcp_accept_unlink(accept_sk);
111
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100112 if (err)
113 accept_sk->sk_err = err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100114 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100115 accept_sk->sk_state_change(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100116
Szymon Janc50b78b22012-09-26 14:22:10 +0200117 bh_unlock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100118 }
Samuel Ortizc8512be2012-10-15 15:09:52 +0200119 }
120
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100121 if (err)
122 sk->sk_err = err;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200123 sk->sk_state = LLCP_CLOSED;
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100124 sk->sk_state_change(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100125
Szymon Janc50b78b22012-09-26 14:22:10 +0200126 bh_unlock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100127
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200128 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100129 }
130
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200131 write_unlock(&local->sockets.lock);
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100132
Samuel Ortizb436a132013-04-03 16:34:19 +0200133 /* If we still have a device, we keep the RAW sockets alive */
134 if (device == true)
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100135 return;
136
137 write_lock(&local->raw_sockets.lock);
138
139 sk_for_each_safe(sk, tmp, &local->raw_sockets.head) {
140 llcp_sock = nfc_llcp_sock(sk);
141
142 bh_lock_sock(sk);
143
144 nfc_llcp_socket_purge(llcp_sock);
145
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100146 if (err)
147 sk->sk_err = err;
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100148 sk->sk_state = LLCP_CLOSED;
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100149 sk->sk_state_change(sk);
150
151 bh_unlock_sock(sk);
152
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100153 sk_del_node_init(sk);
154 }
155
156 write_unlock(&local->raw_sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100157}
158
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200159struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
160{
161 kref_get(&local->ref);
162
163 return local;
164}
165
Samuel Ortizc470e312013-04-03 16:40:52 +0200166static void local_cleanup(struct nfc_llcp_local *local)
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200167{
Samuel Ortizc470e312013-04-03 16:40:52 +0200168 nfc_llcp_socket_release(local, false, ENXIO);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200169 del_timer_sync(&local->link_timer);
170 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700171 cancel_work_sync(&local->tx_work);
172 cancel_work_sync(&local->rx_work);
173 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200174 kfree_skb(local->rx_pending);
Thierry Escande40213fa2013-03-04 15:43:32 +0100175 del_timer_sync(&local->sdreq_timer);
176 cancel_work_sync(&local->sdreq_timeout_work);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100177 nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
Samuel Ortiz3536da02013-02-21 15:40:04 +0100178}
179
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200180static void local_release(struct kref *ref)
181{
182 struct nfc_llcp_local *local;
183
184 local = container_of(ref, struct nfc_llcp_local, ref);
185
186 list_del(&local->list);
Samuel Ortizc470e312013-04-03 16:40:52 +0200187 local_cleanup(local);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200188 kfree(local);
189}
190
191int nfc_llcp_local_put(struct nfc_llcp_local *local)
192{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200193 if (local == NULL)
194 return 0;
195
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200196 return kref_put(&local->ref, local_release);
197}
198
Samuel Ortiz8f500202012-06-25 15:46:28 +0200199static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
200 u8 ssap, u8 dsap)
201{
202 struct sock *sk;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200203 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200204
205 pr_debug("ssap dsap %d %d\n", ssap, dsap);
206
207 if (ssap == 0 && dsap == 0)
208 return NULL;
209
210 read_lock(&local->sockets.lock);
211
212 llcp_sock = NULL;
213
Sasha Levinb67bfe02013-02-27 17:06:00 -0800214 sk_for_each(sk, &local->sockets.head) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200215 tmp_sock = nfc_llcp_sock(sk);
Samuel Ortiz8f500202012-06-25 15:46:28 +0200216
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200217 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
218 llcp_sock = tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200219 break;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200220 }
Samuel Ortiz8f500202012-06-25 15:46:28 +0200221 }
222
223 read_unlock(&local->sockets.lock);
224
225 if (llcp_sock == NULL)
226 return NULL;
227
228 sock_hold(&llcp_sock->sk);
229
230 return llcp_sock;
231}
232
233static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
234{
235 sock_put(&sock->sk);
236}
237
Samuel Ortizd6469602011-12-14 16:43:12 +0100238static void nfc_llcp_timeout_work(struct work_struct *work)
239{
240 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100241 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100242
243 nfc_dep_link_down(local->dev);
244}
245
246static void nfc_llcp_symm_timer(unsigned long data)
247{
248 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
249
250 pr_err("SYMM timeout\n");
251
Linus Torvalds916082b2012-10-02 16:01:31 -0700252 schedule_work(&local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100253}
254
Thierry Escande40213fa2013-03-04 15:43:32 +0100255static void nfc_llcp_sdreq_timeout_work(struct work_struct *work)
256{
257 unsigned long time;
258 HLIST_HEAD(nl_sdres_list);
259 struct hlist_node *n;
260 struct nfc_llcp_sdp_tlv *sdp;
261 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
262 sdreq_timeout_work);
263
264 mutex_lock(&local->sdreq_lock);
265
266 time = jiffies - msecs_to_jiffies(3 * local->remote_lto);
267
268 hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) {
269 if (time_after(sdp->time, time))
270 continue;
271
272 sdp->sap = LLCP_SDP_UNBOUND;
273
274 hlist_del(&sdp->node);
275
276 hlist_add_head(&sdp->node, &nl_sdres_list);
277 }
278
279 if (!hlist_empty(&local->pending_sdreqs))
280 mod_timer(&local->sdreq_timer,
281 jiffies + msecs_to_jiffies(3 * local->remote_lto));
282
283 mutex_unlock(&local->sdreq_lock);
284
285 if (!hlist_empty(&nl_sdres_list))
286 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
287}
288
289static void nfc_llcp_sdreq_timer(unsigned long data)
290{
291 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
292
293 schedule_work(&local->sdreq_timeout_work);
294}
295
Samuel Ortizd6469602011-12-14 16:43:12 +0100296struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
297{
298 struct nfc_llcp_local *local, *n;
299
300 list_for_each_entry_safe(local, n, &llcp_devices, list)
301 if (local->dev == dev)
302 return local;
303
304 pr_debug("No device found\n");
305
306 return NULL;
307}
308
309static char *wks[] = {
310 NULL,
311 NULL, /* SDP */
312 "urn:nfc:sn:ip",
313 "urn:nfc:sn:obex",
314 "urn:nfc:sn:snep",
315};
316
317static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
318{
319 int sap, num_wks;
320
321 pr_debug("%s\n", service_name);
322
323 if (service_name == NULL)
324 return -EINVAL;
325
326 num_wks = ARRAY_SIZE(wks);
327
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100328 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100329 if (wks[sap] == NULL)
330 continue;
331
332 if (strncmp(wks[sap], service_name, service_name_len) == 0)
333 return sap;
334 }
335
336 return -EINVAL;
337}
338
Samuel Ortiz8f500202012-06-25 15:46:28 +0200339static
340struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
341 u8 *sn, size_t sn_len)
342{
343 struct sock *sk;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200344 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
345
346 pr_debug("sn %zd %p\n", sn_len, sn);
347
348 if (sn == NULL || sn_len == 0)
349 return NULL;
350
351 read_lock(&local->sockets.lock);
352
353 llcp_sock = NULL;
354
Sasha Levinb67bfe02013-02-27 17:06:00 -0800355 sk_for_each(sk, &local->sockets.head) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200356 tmp_sock = nfc_llcp_sock(sk);
357
358 pr_debug("llcp sock %p\n", tmp_sock);
359
Samuel Ortiz54292d62012-10-15 15:08:29 +0200360 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
361 tmp_sock->sk.sk_state != LLCP_LISTEN)
362 continue;
363
364 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
365 tmp_sock->sk.sk_state != LLCP_BOUND)
Samuel Ortiz8f500202012-06-25 15:46:28 +0200366 continue;
367
368 if (tmp_sock->service_name == NULL ||
369 tmp_sock->service_name_len == 0)
370 continue;
371
372 if (tmp_sock->service_name_len != sn_len)
373 continue;
374
375 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
376 llcp_sock = tmp_sock;
377 break;
378 }
379 }
380
381 read_unlock(&local->sockets.lock);
382
383 pr_debug("Found llcp sock %p\n", llcp_sock);
384
385 return llcp_sock;
386}
387
Samuel Ortizd6469602011-12-14 16:43:12 +0100388u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100389 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100390{
391 mutex_lock(&local->sdp_lock);
392
393 if (sock->service_name != NULL && sock->service_name_len > 0) {
394 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100395 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100396
397 if (ssap > 0) {
398 pr_debug("WKS %d\n", ssap);
399
400 /* This is a WKS, let's check if it's free */
401 if (local->local_wks & BIT(ssap)) {
402 mutex_unlock(&local->sdp_lock);
403
404 return LLCP_SAP_MAX;
405 }
406
Samuel Ortiz1762c172012-03-05 01:03:38 +0100407 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100408 mutex_unlock(&local->sdp_lock);
409
410 return ssap;
411 }
412
413 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200414 * Check if there already is a non WKS socket bound
415 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100416 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200417 if (nfc_llcp_sock_from_sn(local, sock->service_name,
418 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100419 mutex_unlock(&local->sdp_lock);
420
421 return LLCP_SAP_MAX;
422 }
423
Samuel Ortizd6469602011-12-14 16:43:12 +0100424 mutex_unlock(&local->sdp_lock);
425
Samuel Ortiz8f500202012-06-25 15:46:28 +0200426 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100427
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200428 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
429 if (!test_bit(sock->ssap, &local->local_wks)) {
430 set_bit(sock->ssap, &local->local_wks);
431 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100432
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200433 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100434 }
435 }
436
437 mutex_unlock(&local->sdp_lock);
438
439 return LLCP_SAP_MAX;
440}
441
442u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
443{
444 u8 local_ssap;
445
446 mutex_lock(&local->sdp_lock);
447
448 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
449 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
450 mutex_unlock(&local->sdp_lock);
451 return LLCP_SAP_MAX;
452 }
453
Samuel Ortiz1762c172012-03-05 01:03:38 +0100454 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100455
456 mutex_unlock(&local->sdp_lock);
457
458 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
459}
460
461void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
462{
463 u8 local_ssap;
464 unsigned long *sdp;
465
466 if (ssap < LLCP_WKS_NUM_SAP) {
467 local_ssap = ssap;
468 sdp = &local->local_wks;
469 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200470 atomic_t *client_cnt;
471
Samuel Ortizd6469602011-12-14 16:43:12 +0100472 local_ssap = ssap - LLCP_WKS_NUM_SAP;
473 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200474 client_cnt = &local->local_sdp_cnt[local_ssap];
475
476 pr_debug("%d clients\n", atomic_read(client_cnt));
477
478 mutex_lock(&local->sdp_lock);
479
480 if (atomic_dec_and_test(client_cnt)) {
481 struct nfc_llcp_sock *l_sock;
482
483 pr_debug("No more clients for SAP %d\n", ssap);
484
485 clear_bit(local_ssap, sdp);
486
487 /* Find the listening sock and set it back to UNBOUND */
488 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
489 if (l_sock) {
490 l_sock->ssap = LLCP_SDP_UNBOUND;
491 nfc_llcp_sock_put(l_sock);
492 }
493 }
494
495 mutex_unlock(&local->sdp_lock);
496
497 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100498 } else if (ssap < LLCP_MAX_SAP) {
499 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
500 sdp = &local->local_sap;
501 } else {
502 return;
503 }
504
505 mutex_lock(&local->sdp_lock);
506
Samuel Ortiz1762c172012-03-05 01:03:38 +0100507 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100508
509 mutex_unlock(&local->sdp_lock);
510}
511
Samuel Ortiz8f500202012-06-25 15:46:28 +0200512static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
513{
514 u8 ssap;
515
516 mutex_lock(&local->sdp_lock);
517
518 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
519 if (ssap == LLCP_SDP_NUM_SAP) {
520 mutex_unlock(&local->sdp_lock);
521
522 return LLCP_SAP_MAX;
523 }
524
525 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
526
527 set_bit(ssap, &local->local_sdp);
528
529 mutex_unlock(&local->sdp_lock);
530
531 return LLCP_WKS_NUM_SAP + ssap;
532}
533
Samuel Ortizd6469602011-12-14 16:43:12 +0100534static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
535{
536 u8 *gb_cur, *version_tlv, version, version_length;
Thierry Escande52feb442012-10-17 14:43:39 +0200537 u8 *lto_tlv, lto_length;
Samuel Ortizd6469602011-12-14 16:43:12 +0100538 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200539 u8 *miux_tlv, miux_length;
Samuel Ortiz4ca546e2013-06-03 12:10:04 +0200540 __be16 wks = cpu_to_be16(local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100541 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800542 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100543
544 version = LLCP_VERSION_11;
545 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100546 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100547 gb_len += version_length;
548
Thierry Escande52feb442012-10-17 14:43:39 +0200549 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100550 gb_len += lto_length;
551
552 pr_debug("Local wks 0x%lx\n", local->local_wks);
Samuel Ortiz4ca546e2013-06-03 12:10:04 +0200553 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&wks, 2, &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100554 gb_len += wks_length;
555
Thierry Escande52feb442012-10-17 14:43:39 +0200556 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
Samuel Ortiz56d58762012-04-10 19:43:19 +0200557 &miux_length);
558 gb_len += miux_length;
559
Samuel Ortizd6469602011-12-14 16:43:12 +0100560 gb_len += ARRAY_SIZE(llcp_magic);
561
562 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800563 ret = -EINVAL;
564 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100565 }
566
567 gb_cur = local->gb;
568
569 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
570 gb_cur += ARRAY_SIZE(llcp_magic);
571
572 memcpy(gb_cur, version_tlv, version_length);
573 gb_cur += version_length;
574
575 memcpy(gb_cur, lto_tlv, lto_length);
576 gb_cur += lto_length;
577
578 memcpy(gb_cur, wks_tlv, wks_length);
579 gb_cur += wks_length;
580
Samuel Ortiz56d58762012-04-10 19:43:19 +0200581 memcpy(gb_cur, miux_tlv, miux_length);
582 gb_cur += miux_length;
583
Samuel Ortizd6469602011-12-14 16:43:12 +0100584 local->gb_len = gb_len;
585
Wei Yongjun52da2442012-09-02 21:21:46 +0800586out:
587 kfree(version_tlv);
588 kfree(lto_tlv);
589 kfree(wks_tlv);
590 kfree(miux_tlv);
591
592 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100593}
594
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200595u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
596{
597 struct nfc_llcp_local *local;
598
599 local = nfc_llcp_find_local(dev);
600 if (local == NULL) {
601 *general_bytes_len = 0;
602 return NULL;
603 }
604
605 nfc_llcp_build_gb(local);
606
607 *general_bytes_len = local->gb_len;
608
609 return local->gb;
610}
611
Samuel Ortizd6469602011-12-14 16:43:12 +0100612int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
613{
614 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
615
616 if (local == NULL) {
617 pr_err("No LLCP device\n");
618 return -ENODEV;
619 }
Dan Carpentere9a4aa32013-01-31 11:16:46 +0300620 if (gb_len < 3)
621 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100622
623 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
624 memcpy(local->remote_gb, gb, gb_len);
625 local->remote_gb_len = gb_len;
626
Samuel Ortizd6469602011-12-14 16:43:12 +0100627 if (memcmp(local->remote_gb, llcp_magic, 3)) {
628 pr_err("MAC does not support LLCP\n");
629 return -EINVAL;
630 }
631
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200632 return nfc_llcp_parse_gb_tlv(local,
633 &local->remote_gb[3],
634 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100635}
636
Samuel Ortizd6469602011-12-14 16:43:12 +0100637static u8 nfc_llcp_dsap(struct sk_buff *pdu)
638{
639 return (pdu->data[0] & 0xfc) >> 2;
640}
641
642static u8 nfc_llcp_ptype(struct sk_buff *pdu)
643{
644 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
645}
646
647static u8 nfc_llcp_ssap(struct sk_buff *pdu)
648{
649 return pdu->data[1] & 0x3f;
650}
651
652static u8 nfc_llcp_ns(struct sk_buff *pdu)
653{
654 return pdu->data[2] >> 4;
655}
656
657static u8 nfc_llcp_nr(struct sk_buff *pdu)
658{
659 return pdu->data[2] & 0xf;
660}
661
662static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
663{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200664 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100665 sock->send_n = (sock->send_n + 1) % 16;
666 sock->recv_ack_n = (sock->recv_n - 1) % 16;
667}
668
Thierry Escande4463523b2012-09-26 18:16:44 +0200669void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
670 struct sk_buff *skb, u8 direction)
671{
Thierry Escande4463523b2012-09-26 18:16:44 +0200672 struct sk_buff *skb_copy = NULL, *nskb;
673 struct sock *sk;
674 u8 *data;
675
676 read_lock(&local->raw_sockets.lock);
677
Sasha Levinb67bfe02013-02-27 17:06:00 -0800678 sk_for_each(sk, &local->raw_sockets.head) {
Thierry Escande4463523b2012-09-26 18:16:44 +0200679 if (sk->sk_state != LLCP_BOUND)
680 continue;
681
682 if (skb_copy == NULL) {
683 skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
684 GFP_ATOMIC);
685
686 if (skb_copy == NULL)
687 continue;
688
689 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
690
691 data[0] = local->dev ? local->dev->idx : 0xFF;
692 data[1] = direction;
693 }
694
695 nskb = skb_clone(skb_copy, GFP_ATOMIC);
696 if (!nskb)
697 continue;
698
699 if (sock_queue_rcv_skb(sk, nskb))
700 kfree_skb(nskb);
701 }
702
703 read_unlock(&local->raw_sockets.lock);
704
705 kfree_skb(skb_copy);
706}
707
Samuel Ortiz84457962012-05-30 17:48:29 +0200708static void nfc_llcp_tx_work(struct work_struct *work)
709{
710 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
711 tx_work);
712 struct sk_buff *skb;
713 struct sock *sk;
714 struct nfc_llcp_sock *llcp_sock;
715
716 skb = skb_dequeue(&local->tx_queue);
717 if (skb != NULL) {
718 sk = skb->sk;
719 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200720
721 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
Thierry Escandef1b79dc2013-06-05 17:15:59 +0200722 kfree_skb(skb);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200723 nfc_llcp_send_symm(local->dev);
Samuel Ortiz2635a4b2013-05-28 15:03:17 +0200724 } else if (llcp_sock && !llcp_sock->remote_ready) {
725 skb_queue_head(&local->tx_queue, skb);
726 nfc_llcp_send_symm(local->dev);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200727 } else {
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100728 struct sk_buff *copy_skb = NULL;
729 u8 ptype = nfc_llcp_ptype(skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200730 int ret;
731
732 pr_debug("Sending pending skb\n");
733 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
734 DUMP_PREFIX_OFFSET, 16, 1,
735 skb->data, skb->len, true);
736
Thierry Escande17f7ae162013-06-04 11:34:51 +0200737 if (ptype == LLCP_PDU_DISC && sk != NULL &&
738 sk->sk_state == LLCP_DISCONNECTING) {
739 nfc_llcp_sock_unlink(&local->sockets, sk);
740 sock_orphan(sk);
741 sock_put(sk);
742 }
743
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100744 if (ptype == LLCP_PDU_I)
745 copy_skb = skb_copy(skb, GFP_ATOMIC);
746
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100747 __net_timestamp(skb);
748
Thierry Escande4463523b2012-09-26 18:16:44 +0200749 nfc_llcp_send_to_raw_sock(local, skb,
750 NFC_LLCP_DIRECTION_TX);
751
Samuel Ortiz84457962012-05-30 17:48:29 +0200752 ret = nfc_data_exchange(local->dev, local->target_idx,
753 skb, nfc_llcp_recv, local);
754
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100755 if (ret) {
756 kfree_skb(copy_skb);
757 goto out;
Samuel Ortiz84457962012-05-30 17:48:29 +0200758 }
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100759
760 if (ptype == LLCP_PDU_I && copy_skb)
761 skb_queue_tail(&llcp_sock->tx_pending_queue,
762 copy_skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200763 }
764 } else {
765 nfc_llcp_send_symm(local->dev);
766 }
767
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100768out:
Samuel Ortiz84457962012-05-30 17:48:29 +0200769 mod_timer(&local->link_timer,
770 jiffies + msecs_to_jiffies(2 * local->remote_lto));
771}
772
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200773static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
774 u8 ssap)
775{
776 struct sock *sk;
777 struct nfc_llcp_sock *llcp_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200778
779 read_lock(&local->connecting_sockets.lock);
780
Sasha Levinb67bfe02013-02-27 17:06:00 -0800781 sk_for_each(sk, &local->connecting_sockets.head) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200782 llcp_sock = nfc_llcp_sock(sk);
783
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200784 if (llcp_sock->ssap == ssap) {
785 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200786 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200787 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200788 }
789
790 llcp_sock = NULL;
791
792out:
793 read_unlock(&local->connecting_sockets.lock);
794
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200795 return llcp_sock;
796}
797
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200798static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
799 u8 *sn, size_t sn_len)
800{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200801 struct nfc_llcp_sock *llcp_sock;
802
Samuel Ortiz8f500202012-06-25 15:46:28 +0200803 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100804
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200805 if (llcp_sock == NULL)
806 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100807
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200808 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100809
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200810 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100811}
812
Samuel Ortizd6469602011-12-14 16:43:12 +0100813static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
814{
815 u8 *tlv = &skb->data[2], type, length;
816 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
817
818 while (offset < tlv_array_len) {
819 type = tlv[0];
820 length = tlv[1];
821
822 pr_debug("type 0x%x length %d\n", type, length);
823
824 if (type == LLCP_TLV_SN) {
825 *sn_len = length;
826 return &tlv[2];
827 }
828
829 offset += length + 2;
830 tlv += length + 2;
831 }
832
833 return NULL;
834}
835
Samuel Ortiz968272b2012-10-15 16:14:37 +0200836static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
837 struct sk_buff *skb)
838{
839 struct nfc_llcp_sock *llcp_sock;
840 struct nfc_llcp_ui_cb *ui_cb;
841 u8 dsap, ssap;
842
843 dsap = nfc_llcp_dsap(skb);
844 ssap = nfc_llcp_ssap(skb);
845
846 ui_cb = nfc_llcp_ui_skb_cb(skb);
847 ui_cb->dsap = dsap;
848 ui_cb->ssap = ssap;
849
Samuel Ortiz968272b2012-10-15 16:14:37 +0200850 pr_debug("%d %d\n", dsap, ssap);
851
852 /* We're looking for a bound socket, not a client one */
853 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
854 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
855 return;
856
857 /* There is no sequence with UI frames */
858 skb_pull(skb, LLCP_HEADER_SIZE);
Samuel Ortiz1727cf92012-12-07 16:37:30 +0100859 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
860 /*
861 * UI frames will be freed from the socket layer, so we
862 * need to keep them alive until someone receives them.
863 */
864 skb_get(skb);
865 } else {
866 pr_err("Receive queue is full\n");
Samuel Ortiz968272b2012-10-15 16:14:37 +0200867 }
868
869 nfc_llcp_sock_put(llcp_sock);
870}
871
Samuel Ortizd6469602011-12-14 16:43:12 +0100872static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100873 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100874{
875 struct sock *new_sk, *parent;
876 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200877 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100878
879 dsap = nfc_llcp_dsap(skb);
880 ssap = nfc_llcp_ssap(skb);
881
882 pr_debug("%d %d\n", dsap, ssap);
883
Samuel Ortizd6469602011-12-14 16:43:12 +0100884 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200885 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
886 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100887 reason = LLCP_DM_NOBOUND;
888 goto fail;
889 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100890 } else {
891 u8 *sn;
892 size_t sn_len;
893
894 sn = nfc_llcp_connect_sn(skb, &sn_len);
895 if (sn == NULL) {
896 reason = LLCP_DM_NOBOUND;
897 goto fail;
898 }
899
900 pr_debug("Service name length %zu\n", sn_len);
901
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200902 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
903 if (sock == NULL) {
904 reason = LLCP_DM_NOBOUND;
905 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100906 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100907 }
908
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200909 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100910
Samuel Ortizd6469602011-12-14 16:43:12 +0100911 parent = &sock->sk;
912
913 if (sk_acceptq_is_full(parent)) {
914 reason = LLCP_DM_REJ;
915 release_sock(&sock->sk);
916 sock_put(&sock->sk);
917 goto fail;
918 }
919
Samuel Ortiz8f500202012-06-25 15:46:28 +0200920 if (sock->ssap == LLCP_SDP_UNBOUND) {
921 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
922
923 pr_debug("First client, reserving %d\n", ssap);
924
925 if (ssap == LLCP_SAP_MAX) {
926 reason = LLCP_DM_REJ;
927 release_sock(&sock->sk);
928 sock_put(&sock->sk);
929 goto fail;
930 }
931
932 sock->ssap = ssap;
933 }
934
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100935 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100936 if (new_sk == NULL) {
937 reason = LLCP_DM_REJ;
938 release_sock(&sock->sk);
939 sock_put(&sock->sk);
940 goto fail;
941 }
942
943 new_sock = nfc_llcp_sock(new_sk);
944 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200945 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100946 new_sock->rw = sock->rw;
947 new_sock->miux = sock->miux;
Samuel Ortize4306be2013-02-22 01:12:28 +0100948 new_sock->remote_miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100949 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100950 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200951 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100952 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200953 new_sock->ssap = sock->ssap;
954 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
955 atomic_t *client_count;
956
957 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
958
959 client_count =
960 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
961
962 atomic_inc(client_count);
963 new_sock->reserved_ssap = sock->ssap;
964 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100965
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200966 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
967 skb->len - LLCP_HEADER_SIZE);
968
Samuel Ortizd6469602011-12-14 16:43:12 +0100969 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
970
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200971 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100972
973 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
974
975 nfc_get_device(local->dev->idx);
976
977 new_sk->sk_state = LLCP_CONNECTED;
978
979 /* Wake the listening processes */
980 parent->sk_data_ready(parent, 0);
981
982 /* Send CC */
983 nfc_llcp_send_cc(new_sock);
984
985 release_sock(&sock->sk);
986 sock_put(&sock->sk);
987
988 return;
989
990fail:
991 /* Send DM */
992 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100993}
994
Samuel Ortizd094afa2012-03-05 01:03:42 +0100995int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100996{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100997 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100998 struct nfc_llcp_local *local = sock->local;
999
1000 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001001 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortize4306be2013-02-22 01:12:28 +01001002 sock->remote_rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001003
1004 /* Try to queue some I frames for transmission */
1005 while (sock->remote_ready &&
Samuel Ortize4306be2013-02-22 01:12:28 +01001006 skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +02001007 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001008
1009 pdu = skb_dequeue(&sock->tx_queue);
1010 if (pdu == NULL)
1011 break;
1012
1013 /* Update N(S)/N(R) */
1014 nfc_llcp_set_nrns(sock, pdu);
1015
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001016 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +01001017 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001018 }
Samuel Ortizd094afa2012-03-05 01:03:42 +01001019
1020 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001021}
1022
Samuel Ortizd6469602011-12-14 16:43:12 +01001023static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001024 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001025{
1026 struct nfc_llcp_sock *llcp_sock;
1027 struct sock *sk;
1028 u8 dsap, ssap, ptype, ns, nr;
1029
1030 ptype = nfc_llcp_ptype(skb);
1031 dsap = nfc_llcp_dsap(skb);
1032 ssap = nfc_llcp_ssap(skb);
1033 ns = nfc_llcp_ns(skb);
1034 nr = nfc_llcp_nr(skb);
1035
1036 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
1037
1038 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1039 if (llcp_sock == NULL) {
1040 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1041 return;
1042 }
1043
1044 sk = &llcp_sock->sk;
1045 lock_sock(sk);
1046 if (sk->sk_state == LLCP_CLOSED) {
1047 release_sock(sk);
1048 nfc_llcp_sock_put(llcp_sock);
1049 }
1050
Samuel Ortizd6469602011-12-14 16:43:12 +01001051 /* Pass the payload upstream */
1052 if (ptype == LLCP_PDU_I) {
1053 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
1054
Samuel Ortiz53aef922012-03-05 01:03:36 +01001055 if (ns == llcp_sock->recv_n)
1056 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
1057 else
1058 pr_err("Received out of sequence I PDU\n");
1059
Samuel Ortizd6469602011-12-14 16:43:12 +01001060 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001061 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
1062 /*
1063 * I frames will be freed from the socket layer, so we
1064 * need to keep them alive until someone receives them.
1065 */
1066 skb_get(skb);
1067 } else {
1068 pr_err("Receive queue is full\n");
Samuel Ortizd6469602011-12-14 16:43:12 +01001069 }
1070 }
1071
1072 /* Remove skbs from the pending queue */
1073 if (llcp_sock->send_ack_n != nr) {
1074 struct sk_buff *s, *tmp;
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001075 u8 n;
Samuel Ortizd6469602011-12-14 16:43:12 +01001076
1077 llcp_sock->send_ack_n = nr;
1078
Samuel Ortiz84457962012-05-30 17:48:29 +02001079 /* Remove and free all skbs until ns == nr */
1080 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001081 n = nfc_llcp_ns(s);
1082
Samuel Ortiz84457962012-05-30 17:48:29 +02001083 skb_unlink(s, &llcp_sock->tx_pending_queue);
1084 kfree_skb(s);
1085
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001086 if (n == nr)
Samuel Ortiz84457962012-05-30 17:48:29 +02001087 break;
1088 }
1089
1090 /* Re-queue the remaining skbs for transmission */
1091 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1092 s, tmp) {
1093 skb_unlink(s, &llcp_sock->tx_pending_queue);
1094 skb_queue_head(&local->tx_queue, s);
1095 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001096 }
1097
Samuel Ortiz53aef922012-03-05 01:03:36 +01001098 if (ptype == LLCP_PDU_RR)
1099 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001100 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +01001101 llcp_sock->remote_ready = false;
1102
Samuel Ortiz56af2562012-05-30 18:06:11 +02001103 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +01001104 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001105
1106 release_sock(sk);
1107 nfc_llcp_sock_put(llcp_sock);
1108}
1109
1110static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001111 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001112{
1113 struct nfc_llcp_sock *llcp_sock;
1114 struct sock *sk;
1115 u8 dsap, ssap;
1116
1117 dsap = nfc_llcp_dsap(skb);
1118 ssap = nfc_llcp_ssap(skb);
1119
Samuel Ortiz6d2cd972013-04-03 16:44:44 +02001120 if ((dsap == 0) && (ssap == 0)) {
1121 pr_debug("Connection termination");
1122 nfc_dep_link_down(local->dev);
1123 return;
1124 }
1125
Samuel Ortizd6469602011-12-14 16:43:12 +01001126 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1127 if (llcp_sock == NULL) {
1128 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1129 return;
1130 }
1131
1132 sk = &llcp_sock->sk;
1133 lock_sock(sk);
Samuel Ortizf31652a2012-10-26 18:20:10 +02001134
1135 nfc_llcp_socket_purge(llcp_sock);
1136
Samuel Ortizd6469602011-12-14 16:43:12 +01001137 if (sk->sk_state == LLCP_CLOSED) {
1138 release_sock(sk);
1139 nfc_llcp_sock_put(llcp_sock);
1140 }
1141
Samuel Ortizd6469602011-12-14 16:43:12 +01001142 if (sk->sk_state == LLCP_CONNECTED) {
1143 nfc_put_device(local->dev);
1144 sk->sk_state = LLCP_CLOSED;
1145 sk->sk_state_change(sk);
1146 }
1147
1148 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1149
1150 release_sock(sk);
1151 nfc_llcp_sock_put(llcp_sock);
1152}
1153
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001154static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001155{
1156 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +02001157 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001158 u8 dsap, ssap;
1159
Samuel Ortizd6469602011-12-14 16:43:12 +01001160 dsap = nfc_llcp_dsap(skb);
1161 ssap = nfc_llcp_ssap(skb);
1162
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001163 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +01001164 if (llcp_sock == NULL) {
1165 pr_err("Invalid CC\n");
1166 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1167
1168 return;
1169 }
1170
Samuel Ortizff353d82012-05-07 12:31:19 +02001171 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001172
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001173 /* Unlink from connecting and link to the client array */
1174 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1175 nfc_llcp_sock_link(&local->sockets, sk);
1176 llcp_sock->dsap = ssap;
1177
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001178 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1179 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001180
Samuel Ortizff353d82012-05-07 12:31:19 +02001181 sk->sk_state = LLCP_CONNECTED;
1182 sk->sk_state_change(sk);
1183
Samuel Ortizd6469602011-12-14 16:43:12 +01001184 nfc_llcp_sock_put(llcp_sock);
1185}
1186
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001187static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1188{
1189 struct nfc_llcp_sock *llcp_sock;
1190 struct sock *sk;
1191 u8 dsap, ssap, reason;
1192
1193 dsap = nfc_llcp_dsap(skb);
1194 ssap = nfc_llcp_ssap(skb);
1195 reason = skb->data[2];
1196
1197 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1198
1199 switch (reason) {
1200 case LLCP_DM_NOBOUND:
1201 case LLCP_DM_REJ:
1202 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1203 break;
1204
1205 default:
1206 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1207 break;
1208 }
1209
1210 if (llcp_sock == NULL) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +02001211 pr_debug("Already closed\n");
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001212 return;
1213 }
1214
1215 sk = &llcp_sock->sk;
1216
1217 sk->sk_err = ENXIO;
1218 sk->sk_state = LLCP_CLOSED;
1219 sk->sk_state_change(sk);
1220
1221 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001222}
1223
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001224static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1225 struct sk_buff *skb)
1226{
1227 struct nfc_llcp_sock *llcp_sock;
1228 u8 dsap, ssap, *tlv, type, length, tid, sap;
1229 u16 tlv_len, offset;
1230 char *service_name;
1231 size_t service_name_len;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001232 struct nfc_llcp_sdp_tlv *sdp;
1233 HLIST_HEAD(llc_sdres_list);
1234 size_t sdres_tlvs_len;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001235 HLIST_HEAD(nl_sdres_list);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001236
1237 dsap = nfc_llcp_dsap(skb);
1238 ssap = nfc_llcp_ssap(skb);
1239
1240 pr_debug("%d %d\n", dsap, ssap);
1241
1242 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1243 pr_err("Wrong SNL SAP\n");
1244 return;
1245 }
1246
1247 tlv = &skb->data[LLCP_HEADER_SIZE];
1248 tlv_len = skb->len - LLCP_HEADER_SIZE;
1249 offset = 0;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001250 sdres_tlvs_len = 0;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001251
Szymon Janc0f450772012-10-17 15:23:39 +02001252 while (offset < tlv_len) {
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001253 type = tlv[0];
1254 length = tlv[1];
1255
1256 switch (type) {
1257 case LLCP_TLV_SDREQ:
1258 tid = tlv[2];
1259 service_name = (char *) &tlv[3];
1260 service_name_len = length - 1;
1261
Samuel Ortize6904082012-10-15 14:28:13 +02001262 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001263
1264 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1265 !strncmp(service_name, "urn:nfc:sn:sdp",
1266 service_name_len)) {
1267 sap = 1;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001268 goto add_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001269 }
1270
Samuel Ortize6904082012-10-15 14:28:13 +02001271 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1272 service_name_len);
1273 if (!llcp_sock) {
1274 sap = 0;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001275 goto add_snl;
Samuel Ortize6904082012-10-15 14:28:13 +02001276 }
1277
1278 /*
1279 * We found a socket but its ssap has not been reserved
1280 * yet. We need to assign it for good and send a reply.
1281 * The ssap will be freed when the socket is closed.
1282 */
1283 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1284 atomic_t *client_count;
1285
1286 sap = nfc_llcp_reserve_sdp_ssap(local);
1287
1288 pr_debug("Reserving %d\n", sap);
1289
1290 if (sap == LLCP_SAP_MAX) {
1291 sap = 0;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001292 goto add_snl;
Samuel Ortize6904082012-10-15 14:28:13 +02001293 }
1294
1295 client_count =
1296 &local->local_sdp_cnt[sap -
1297 LLCP_WKS_NUM_SAP];
1298
1299 atomic_inc(client_count);
1300
1301 llcp_sock->ssap = sap;
1302 llcp_sock->reserved_ssap = sap;
1303 } else {
1304 sap = llcp_sock->ssap;
1305 }
1306
1307 pr_debug("%p %d\n", llcp_sock, sap);
1308
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001309add_snl:
1310 sdp = nfc_llcp_build_sdres_tlv(tid, sap);
1311 if (sdp == NULL)
1312 goto exit;
1313
1314 sdres_tlvs_len += sdp->tlv_len;
1315 hlist_add_head(&sdp->node, &llc_sdres_list);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001316 break;
1317
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001318 case LLCP_TLV_SDRES:
1319 mutex_lock(&local->sdreq_lock);
1320
1321 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]);
1322
1323 hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
1324 if (sdp->tid != tlv[2])
1325 continue;
1326
1327 sdp->sap = tlv[3];
1328
1329 pr_debug("Found: uri=%s, sap=%d\n",
1330 sdp->uri, sdp->sap);
1331
1332 hlist_del(&sdp->node);
1333
1334 hlist_add_head(&sdp->node, &nl_sdres_list);
1335
1336 break;
1337 }
1338
1339 mutex_unlock(&local->sdreq_lock);
1340 break;
1341
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001342 default:
1343 pr_err("Invalid SNL tlv value 0x%x\n", type);
1344 break;
1345 }
1346
1347 offset += length + 2;
1348 tlv += length + 2;
1349 }
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001350
1351exit:
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001352 if (!hlist_empty(&nl_sdres_list))
1353 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
1354
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001355 if (!hlist_empty(&llc_sdres_list))
1356 nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001357}
1358
Thierry Escande098dafc2013-03-29 11:47:43 +01001359static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001360{
Thierry Escande098dafc2013-03-29 11:47:43 +01001361 u8 ptype;
1362 u16 pdu_len;
1363 struct sk_buff *new_skb;
Samuel Ortizd6469602011-12-14 16:43:12 +01001364
Thierry Escande098dafc2013-03-29 11:47:43 +01001365 if (skb->len <= LLCP_HEADER_SIZE) {
1366 pr_err("Malformed AGF PDU\n");
Samuel Ortizd6469602011-12-14 16:43:12 +01001367 return;
1368 }
1369
Thierry Escande098dafc2013-03-29 11:47:43 +01001370 skb_pull(skb, LLCP_HEADER_SIZE);
1371
1372 while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
1373 pdu_len = skb->data[0] << 8 | skb->data[1];
1374
1375 skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
1376
1377 if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
1378 pr_err("Malformed AGF PDU\n");
1379 return;
1380 }
1381
1382 ptype = nfc_llcp_ptype(skb);
1383
1384 if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
1385 goto next;
1386
1387 new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
1388 if (new_skb == NULL) {
1389 pr_err("Could not allocate PDU\n");
1390 return;
1391 }
1392
1393 memcpy(skb_put(new_skb, pdu_len), skb->data, pdu_len);
1394
1395 nfc_llcp_rx_skb(local, new_skb);
1396
1397 kfree_skb(new_skb);
1398next:
1399 skb_pull(skb, pdu_len);
1400 }
1401}
1402
1403static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
1404{
1405 u8 dsap, ssap, ptype;
1406
Samuel Ortizd6469602011-12-14 16:43:12 +01001407 ptype = nfc_llcp_ptype(skb);
1408 dsap = nfc_llcp_dsap(skb);
1409 ssap = nfc_llcp_ssap(skb);
1410
1411 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1412
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001413 if (ptype != LLCP_PDU_SYMM)
1414 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1415 16, 1, skb->data, skb->len, true);
1416
Samuel Ortizd6469602011-12-14 16:43:12 +01001417 switch (ptype) {
1418 case LLCP_PDU_SYMM:
1419 pr_debug("SYMM\n");
1420 break;
1421
Samuel Ortiz968272b2012-10-15 16:14:37 +02001422 case LLCP_PDU_UI:
1423 pr_debug("UI\n");
1424 nfc_llcp_recv_ui(local, skb);
1425 break;
1426
Samuel Ortizd6469602011-12-14 16:43:12 +01001427 case LLCP_PDU_CONNECT:
1428 pr_debug("CONNECT\n");
1429 nfc_llcp_recv_connect(local, skb);
1430 break;
1431
1432 case LLCP_PDU_DISC:
1433 pr_debug("DISC\n");
1434 nfc_llcp_recv_disc(local, skb);
1435 break;
1436
1437 case LLCP_PDU_CC:
1438 pr_debug("CC\n");
1439 nfc_llcp_recv_cc(local, skb);
1440 break;
1441
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001442 case LLCP_PDU_DM:
1443 pr_debug("DM\n");
1444 nfc_llcp_recv_dm(local, skb);
1445 break;
1446
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001447 case LLCP_PDU_SNL:
1448 pr_debug("SNL\n");
1449 nfc_llcp_recv_snl(local, skb);
1450 break;
1451
Samuel Ortizd6469602011-12-14 16:43:12 +01001452 case LLCP_PDU_I:
1453 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001454 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001455 pr_debug("I frame\n");
1456 nfc_llcp_recv_hdlc(local, skb);
1457 break;
1458
Thierry Escande098dafc2013-03-29 11:47:43 +01001459 case LLCP_PDU_AGF:
1460 pr_debug("AGF frame\n");
1461 nfc_llcp_recv_agf(local, skb);
1462 break;
Samuel Ortizd6469602011-12-14 16:43:12 +01001463 }
Thierry Escande098dafc2013-03-29 11:47:43 +01001464}
1465
1466static void nfc_llcp_rx_work(struct work_struct *work)
1467{
1468 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
1469 rx_work);
1470 struct sk_buff *skb;
1471
1472 skb = local->rx_pending;
1473 if (skb == NULL) {
1474 pr_debug("No pending SKB\n");
1475 return;
1476 }
1477
1478 __net_timestamp(skb);
1479
1480 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1481
1482 nfc_llcp_rx_skb(local, skb);
Samuel Ortizd6469602011-12-14 16:43:12 +01001483
Linus Torvalds916082b2012-10-02 16:01:31 -07001484 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001485 kfree_skb(local->rx_pending);
1486 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001487}
1488
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001489static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
1490{
1491 local->rx_pending = skb;
1492 del_timer(&local->link_timer);
1493 schedule_work(&local->rx_work);
1494}
1495
Samuel Ortizd6469602011-12-14 16:43:12 +01001496void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1497{
1498 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1499
1500 pr_debug("Received an LLCP PDU\n");
1501 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001502 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001503 return;
1504 }
1505
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001506 __nfc_llcp_recv(local, skb);
Samuel Ortizd6469602011-12-14 16:43:12 +01001507}
1508
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001509int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1510{
1511 struct nfc_llcp_local *local;
1512
1513 local = nfc_llcp_find_local(dev);
1514 if (local == NULL)
1515 return -ENODEV;
1516
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001517 __nfc_llcp_recv(local, skb);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001518
1519 return 0;
1520}
1521
Samuel Ortizd6469602011-12-14 16:43:12 +01001522void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1523{
1524 struct nfc_llcp_local *local;
1525
1526 local = nfc_llcp_find_local(dev);
1527 if (local == NULL)
1528 return;
1529
Thierry Escandeabd18d42013-04-02 10:25:15 +02001530 local->remote_miu = LLCP_DEFAULT_MIU;
1531 local->remote_lto = LLCP_DEFAULT_LTO;
1532
Samuel Ortizd6469602011-12-14 16:43:12 +01001533 /* Close and purge all existing sockets */
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +01001534 nfc_llcp_socket_release(local, true, 0);
Samuel Ortizd6469602011-12-14 16:43:12 +01001535}
1536
1537void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1538 u8 comm_mode, u8 rf_mode)
1539{
1540 struct nfc_llcp_local *local;
1541
1542 pr_debug("rf mode %d\n", rf_mode);
1543
1544 local = nfc_llcp_find_local(dev);
1545 if (local == NULL)
1546 return;
1547
1548 local->target_idx = target_idx;
1549 local->comm_mode = comm_mode;
1550 local->rf_mode = rf_mode;
1551
1552 if (rf_mode == NFC_RF_INITIATOR) {
1553 pr_debug("Queueing Tx work\n");
1554
Linus Torvalds916082b2012-10-02 16:01:31 -07001555 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001556 } else {
1557 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001558 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001559 }
1560}
1561
1562int nfc_llcp_register_device(struct nfc_dev *ndev)
1563{
Samuel Ortizd6469602011-12-14 16:43:12 +01001564 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001565
1566 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1567 if (local == NULL)
1568 return -ENOMEM;
1569
1570 local->dev = ndev;
1571 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001572 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001573 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001574 init_timer(&local->link_timer);
1575 local->link_timer.data = (unsigned long) local;
1576 local->link_timer.function = nfc_llcp_symm_timer;
1577
1578 skb_queue_head_init(&local->tx_queue);
1579 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001580
1581 local->rx_pending = NULL;
1582 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001583
1584 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001585
Szymon Jancfe235b52012-09-25 12:42:50 +02001586 rwlock_init(&local->sockets.lock);
1587 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande4463523b2012-09-26 18:16:44 +02001588 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001589
Thierry Escande52feb442012-10-17 14:43:39 +02001590 local->lto = 150; /* 1500 ms */
1591 local->rw = LLCP_MAX_RW;
1592 local->miux = cpu_to_be16(LLCP_MAX_MIUX);
Samuel Ortizf768b342013-05-28 15:41:32 +02001593 local->local_wks = 0x1; /* LLC Link Management */
Thierry Escande52feb442012-10-17 14:43:39 +02001594
Samuel Ortizd6469602011-12-14 16:43:12 +01001595 nfc_llcp_build_gb(local);
1596
1597 local->remote_miu = LLCP_DEFAULT_MIU;
1598 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001599
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001600 mutex_init(&local->sdreq_lock);
1601 INIT_HLIST_HEAD(&local->pending_sdreqs);
Thierry Escande40213fa2013-03-04 15:43:32 +01001602 init_timer(&local->sdreq_timer);
1603 local->sdreq_timer.data = (unsigned long) local;
1604 local->sdreq_timer.function = nfc_llcp_sdreq_timer;
1605 INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001606
Thierry Escande16a78e92012-10-12 15:25:43 +02001607 list_add(&local->list, &llcp_devices);
Samuel Ortizd6469602011-12-14 16:43:12 +01001608
1609 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001610}
1611
1612void nfc_llcp_unregister_device(struct nfc_dev *dev)
1613{
1614 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1615
1616 if (local == NULL) {
1617 pr_debug("No such device\n");
1618 return;
1619 }
1620
Samuel Ortizc470e312013-04-03 16:40:52 +02001621 local_cleanup(local);
Samuel Ortiz3536da02013-02-21 15:40:04 +01001622
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001623 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001624}
1625
1626int __init nfc_llcp_init(void)
1627{
1628 INIT_LIST_HEAD(&llcp_devices);
1629
1630 return nfc_llcp_sock_init();
1631}
1632
1633void nfc_llcp_exit(void)
1634{
1635 nfc_llcp_sock_exit();
1636}