blob: 6f368412ffd26843d1f807b659541898f84bdcc3 [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
27#include "../nfc.h"
28#include "llcp.h"
29
30static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
31
32static struct list_head llcp_devices;
33
Samuel Ortiza69f32a2012-05-04 17:04:19 +020034void 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
41void 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
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020048static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
Samuel Ortizd6469602011-12-14 16:43:12 +010049{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020050 struct sock *sk;
51 struct hlist_node *node, *tmp;
52 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010053
Samuel Ortiza69f32a2012-05-04 17:04:19 +020054 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010055
Samuel Ortiza69f32a2012-05-04 17:04:19 +020056 sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
57 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010058
Samuel Ortiza69f32a2012-05-04 17:04:19 +020059 lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010060
Samuel Ortiza69f32a2012-05-04 17:04:19 +020061 if (sk->sk_state == LLCP_CONNECTED)
62 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010063
Samuel Ortiza69f32a2012-05-04 17:04:19 +020064 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +010065 struct nfc_llcp_sock *lsk, *n;
66 struct sock *accept_sk;
67
Samuel Ortiza69f32a2012-05-04 17:04:19 +020068 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +010069 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +010070 accept_sk = &lsk->sk;
71 lock_sock(accept_sk);
72
73 nfc_llcp_accept_unlink(accept_sk);
74
75 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010076
77 release_sock(accept_sk);
78
79 sock_orphan(accept_sk);
80 }
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020081
82 if (listen == true) {
83 release_sock(sk);
84 continue;
85 }
Samuel Ortizd6469602011-12-14 16:43:12 +010086 }
87
Samuel Ortiza69f32a2012-05-04 17:04:19 +020088 sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010089
Samuel Ortiza69f32a2012-05-04 17:04:19 +020090 release_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010091
Samuel Ortiza69f32a2012-05-04 17:04:19 +020092 sock_orphan(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +010093
Samuel Ortiza69f32a2012-05-04 17:04:19 +020094 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010095 }
96
Samuel Ortiza69f32a2012-05-04 17:04:19 +020097 write_unlock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010098}
99
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200100struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
101{
102 kref_get(&local->ref);
103
104 return local;
105}
106
107static void local_release(struct kref *ref)
108{
109 struct nfc_llcp_local *local;
110
111 local = container_of(ref, struct nfc_llcp_local, ref);
112
113 list_del(&local->list);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200114 nfc_llcp_socket_release(local, false);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200115 del_timer_sync(&local->link_timer);
116 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700117 cancel_work_sync(&local->tx_work);
118 cancel_work_sync(&local->rx_work);
119 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200120 kfree_skb(local->rx_pending);
121 kfree(local);
122}
123
124int nfc_llcp_local_put(struct nfc_llcp_local *local)
125{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200126 if (local == NULL)
127 return 0;
128
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200129 return kref_put(&local->ref, local_release);
130}
131
Samuel Ortiz8f500202012-06-25 15:46:28 +0200132static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
133 u8 ssap, u8 dsap)
134{
135 struct sock *sk;
136 struct hlist_node *node;
137 struct nfc_llcp_sock *llcp_sock;
138
139 pr_debug("ssap dsap %d %d\n", ssap, dsap);
140
141 if (ssap == 0 && dsap == 0)
142 return NULL;
143
144 read_lock(&local->sockets.lock);
145
146 llcp_sock = NULL;
147
148 sk_for_each(sk, node, &local->sockets.head) {
149 llcp_sock = nfc_llcp_sock(sk);
150
151 if (llcp_sock->ssap == ssap && llcp_sock->dsap == dsap)
152 break;
153 }
154
155 read_unlock(&local->sockets.lock);
156
157 if (llcp_sock == NULL)
158 return NULL;
159
160 sock_hold(&llcp_sock->sk);
161
162 return llcp_sock;
163}
164
165static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
166{
167 sock_put(&sock->sk);
168}
169
Samuel Ortizd6469602011-12-14 16:43:12 +0100170static void nfc_llcp_timeout_work(struct work_struct *work)
171{
172 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100173 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100174
175 nfc_dep_link_down(local->dev);
176}
177
178static void nfc_llcp_symm_timer(unsigned long data)
179{
180 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
181
182 pr_err("SYMM timeout\n");
183
Tejun Heo474fee32012-08-22 16:22:16 -0700184 queue_work(system_nrt_wq, &local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100185}
186
187struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
188{
189 struct nfc_llcp_local *local, *n;
190
191 list_for_each_entry_safe(local, n, &llcp_devices, list)
192 if (local->dev == dev)
193 return local;
194
195 pr_debug("No device found\n");
196
197 return NULL;
198}
199
200static char *wks[] = {
201 NULL,
202 NULL, /* SDP */
203 "urn:nfc:sn:ip",
204 "urn:nfc:sn:obex",
205 "urn:nfc:sn:snep",
206};
207
208static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
209{
210 int sap, num_wks;
211
212 pr_debug("%s\n", service_name);
213
214 if (service_name == NULL)
215 return -EINVAL;
216
217 num_wks = ARRAY_SIZE(wks);
218
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100219 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100220 if (wks[sap] == NULL)
221 continue;
222
223 if (strncmp(wks[sap], service_name, service_name_len) == 0)
224 return sap;
225 }
226
227 return -EINVAL;
228}
229
Samuel Ortiz8f500202012-06-25 15:46:28 +0200230static
231struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
232 u8 *sn, size_t sn_len)
233{
234 struct sock *sk;
235 struct hlist_node *node;
236 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
237
238 pr_debug("sn %zd %p\n", sn_len, sn);
239
240 if (sn == NULL || sn_len == 0)
241 return NULL;
242
243 read_lock(&local->sockets.lock);
244
245 llcp_sock = NULL;
246
247 sk_for_each(sk, node, &local->sockets.head) {
248 tmp_sock = nfc_llcp_sock(sk);
249
250 pr_debug("llcp sock %p\n", tmp_sock);
251
252 if (tmp_sock->sk.sk_state != LLCP_LISTEN)
253 continue;
254
255 if (tmp_sock->service_name == NULL ||
256 tmp_sock->service_name_len == 0)
257 continue;
258
259 if (tmp_sock->service_name_len != sn_len)
260 continue;
261
262 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
263 llcp_sock = tmp_sock;
264 break;
265 }
266 }
267
268 read_unlock(&local->sockets.lock);
269
270 pr_debug("Found llcp sock %p\n", llcp_sock);
271
272 return llcp_sock;
273}
274
Samuel Ortizd6469602011-12-14 16:43:12 +0100275u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100276 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100277{
278 mutex_lock(&local->sdp_lock);
279
280 if (sock->service_name != NULL && sock->service_name_len > 0) {
281 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100282 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100283
284 if (ssap > 0) {
285 pr_debug("WKS %d\n", ssap);
286
287 /* This is a WKS, let's check if it's free */
288 if (local->local_wks & BIT(ssap)) {
289 mutex_unlock(&local->sdp_lock);
290
291 return LLCP_SAP_MAX;
292 }
293
Samuel Ortiz1762c172012-03-05 01:03:38 +0100294 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100295 mutex_unlock(&local->sdp_lock);
296
297 return ssap;
298 }
299
300 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200301 * Check if there already is a non WKS socket bound
302 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100303 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200304 if (nfc_llcp_sock_from_sn(local, sock->service_name,
305 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100306 mutex_unlock(&local->sdp_lock);
307
308 return LLCP_SAP_MAX;
309 }
310
Samuel Ortizd6469602011-12-14 16:43:12 +0100311 mutex_unlock(&local->sdp_lock);
312
Samuel Ortiz8f500202012-06-25 15:46:28 +0200313 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100314
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200315 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
316 if (!test_bit(sock->ssap, &local->local_wks)) {
317 set_bit(sock->ssap, &local->local_wks);
318 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100319
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200320 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100321 }
322 }
323
324 mutex_unlock(&local->sdp_lock);
325
326 return LLCP_SAP_MAX;
327}
328
329u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
330{
331 u8 local_ssap;
332
333 mutex_lock(&local->sdp_lock);
334
335 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
336 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
337 mutex_unlock(&local->sdp_lock);
338 return LLCP_SAP_MAX;
339 }
340
Samuel Ortiz1762c172012-03-05 01:03:38 +0100341 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100342
343 mutex_unlock(&local->sdp_lock);
344
345 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
346}
347
348void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
349{
350 u8 local_ssap;
351 unsigned long *sdp;
352
353 if (ssap < LLCP_WKS_NUM_SAP) {
354 local_ssap = ssap;
355 sdp = &local->local_wks;
356 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200357 atomic_t *client_cnt;
358
Samuel Ortizd6469602011-12-14 16:43:12 +0100359 local_ssap = ssap - LLCP_WKS_NUM_SAP;
360 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200361 client_cnt = &local->local_sdp_cnt[local_ssap];
362
363 pr_debug("%d clients\n", atomic_read(client_cnt));
364
365 mutex_lock(&local->sdp_lock);
366
367 if (atomic_dec_and_test(client_cnt)) {
368 struct nfc_llcp_sock *l_sock;
369
370 pr_debug("No more clients for SAP %d\n", ssap);
371
372 clear_bit(local_ssap, sdp);
373
374 /* Find the listening sock and set it back to UNBOUND */
375 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
376 if (l_sock) {
377 l_sock->ssap = LLCP_SDP_UNBOUND;
378 nfc_llcp_sock_put(l_sock);
379 }
380 }
381
382 mutex_unlock(&local->sdp_lock);
383
384 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100385 } else if (ssap < LLCP_MAX_SAP) {
386 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
387 sdp = &local->local_sap;
388 } else {
389 return;
390 }
391
392 mutex_lock(&local->sdp_lock);
393
Samuel Ortiz1762c172012-03-05 01:03:38 +0100394 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100395
396 mutex_unlock(&local->sdp_lock);
397}
398
Samuel Ortiz8f500202012-06-25 15:46:28 +0200399static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
400{
401 u8 ssap;
402
403 mutex_lock(&local->sdp_lock);
404
405 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
406 if (ssap == LLCP_SDP_NUM_SAP) {
407 mutex_unlock(&local->sdp_lock);
408
409 return LLCP_SAP_MAX;
410 }
411
412 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
413
414 set_bit(ssap, &local->local_sdp);
415
416 mutex_unlock(&local->sdp_lock);
417
418 return LLCP_WKS_NUM_SAP + ssap;
419}
420
Samuel Ortizd6469602011-12-14 16:43:12 +0100421static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
422{
423 u8 *gb_cur, *version_tlv, version, version_length;
424 u8 *lto_tlv, lto, lto_length;
425 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200426 u8 *miux_tlv, miux_length;
427 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100428 u8 gb_len = 0;
429
430 version = LLCP_VERSION_11;
431 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100432 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100433 gb_len += version_length;
434
435 /* 1500 ms */
436 lto = 150;
Samuel Ortiz91b0ade2012-04-10 19:43:20 +0200437 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100438 gb_len += lto_length;
439
440 pr_debug("Local wks 0x%lx\n", local->local_wks);
441 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100442 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100443 gb_len += wks_length;
444
Samuel Ortiz56d58762012-04-10 19:43:19 +0200445 miux = cpu_to_be16(LLCP_MAX_MIUX);
446 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
447 &miux_length);
448 gb_len += miux_length;
449
Samuel Ortizd6469602011-12-14 16:43:12 +0100450 gb_len += ARRAY_SIZE(llcp_magic);
451
452 if (gb_len > NFC_MAX_GT_LEN) {
453 kfree(version_tlv);
454 return -EINVAL;
455 }
456
457 gb_cur = local->gb;
458
459 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
460 gb_cur += ARRAY_SIZE(llcp_magic);
461
462 memcpy(gb_cur, version_tlv, version_length);
463 gb_cur += version_length;
464
465 memcpy(gb_cur, lto_tlv, lto_length);
466 gb_cur += lto_length;
467
468 memcpy(gb_cur, wks_tlv, wks_length);
469 gb_cur += wks_length;
470
Samuel Ortiz56d58762012-04-10 19:43:19 +0200471 memcpy(gb_cur, miux_tlv, miux_length);
472 gb_cur += miux_length;
473
Samuel Ortizd6469602011-12-14 16:43:12 +0100474 kfree(version_tlv);
475 kfree(lto_tlv);
476
477 local->gb_len = gb_len;
478
479 return 0;
480}
481
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200482u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
483{
484 struct nfc_llcp_local *local;
485
486 local = nfc_llcp_find_local(dev);
487 if (local == NULL) {
488 *general_bytes_len = 0;
489 return NULL;
490 }
491
492 nfc_llcp_build_gb(local);
493
494 *general_bytes_len = local->gb_len;
495
496 return local->gb;
497}
498
Samuel Ortizd6469602011-12-14 16:43:12 +0100499int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
500{
501 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
502
503 if (local == NULL) {
504 pr_err("No LLCP device\n");
505 return -ENODEV;
506 }
507
508 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
509 memcpy(local->remote_gb, gb, gb_len);
510 local->remote_gb_len = gb_len;
511
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100512 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100513 return -ENODEV;
514
515 if (memcmp(local->remote_gb, llcp_magic, 3)) {
516 pr_err("MAC does not support LLCP\n");
517 return -EINVAL;
518 }
519
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200520 return nfc_llcp_parse_gb_tlv(local,
521 &local->remote_gb[3],
522 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100523}
524
Samuel Ortizd6469602011-12-14 16:43:12 +0100525static u8 nfc_llcp_dsap(struct sk_buff *pdu)
526{
527 return (pdu->data[0] & 0xfc) >> 2;
528}
529
530static u8 nfc_llcp_ptype(struct sk_buff *pdu)
531{
532 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
533}
534
535static u8 nfc_llcp_ssap(struct sk_buff *pdu)
536{
537 return pdu->data[1] & 0x3f;
538}
539
540static u8 nfc_llcp_ns(struct sk_buff *pdu)
541{
542 return pdu->data[2] >> 4;
543}
544
545static u8 nfc_llcp_nr(struct sk_buff *pdu)
546{
547 return pdu->data[2] & 0xf;
548}
549
550static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
551{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200552 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100553 sock->send_n = (sock->send_n + 1) % 16;
554 sock->recv_ack_n = (sock->recv_n - 1) % 16;
555}
556
Samuel Ortiz84457962012-05-30 17:48:29 +0200557static void nfc_llcp_tx_work(struct work_struct *work)
558{
559 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
560 tx_work);
561 struct sk_buff *skb;
562 struct sock *sk;
563 struct nfc_llcp_sock *llcp_sock;
564
565 skb = skb_dequeue(&local->tx_queue);
566 if (skb != NULL) {
567 sk = skb->sk;
568 llcp_sock = nfc_llcp_sock(sk);
569 if (llcp_sock != NULL) {
570 int ret;
571
572 pr_debug("Sending pending skb\n");
573 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
574 DUMP_PREFIX_OFFSET, 16, 1,
575 skb->data, skb->len, true);
576
577 ret = nfc_data_exchange(local->dev, local->target_idx,
578 skb, nfc_llcp_recv, local);
579
580 if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
581 skb = skb_get(skb);
582 skb_queue_tail(&llcp_sock->tx_pending_queue,
583 skb);
584 }
585 } else {
586 nfc_llcp_send_symm(local->dev);
587 }
588 } else {
589 nfc_llcp_send_symm(local->dev);
590 }
591
592 mod_timer(&local->link_timer,
593 jiffies + msecs_to_jiffies(2 * local->remote_lto));
594}
595
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200596static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
597 u8 ssap)
598{
599 struct sock *sk;
600 struct nfc_llcp_sock *llcp_sock;
601 struct hlist_node *node;
602
603 read_lock(&local->connecting_sockets.lock);
604
605 sk_for_each(sk, node, &local->connecting_sockets.head) {
606 llcp_sock = nfc_llcp_sock(sk);
607
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200608 if (llcp_sock->ssap == ssap) {
609 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200610 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200611 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200612 }
613
614 llcp_sock = NULL;
615
616out:
617 read_unlock(&local->connecting_sockets.lock);
618
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200619 return llcp_sock;
620}
621
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200622static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
623 u8 *sn, size_t sn_len)
624{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200625 struct nfc_llcp_sock *llcp_sock;
626
Samuel Ortiz8f500202012-06-25 15:46:28 +0200627 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100628
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200629 if (llcp_sock == NULL)
630 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100631
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200632 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100633
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200634 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100635}
636
Samuel Ortizd6469602011-12-14 16:43:12 +0100637static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
638{
639 u8 *tlv = &skb->data[2], type, length;
640 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
641
642 while (offset < tlv_array_len) {
643 type = tlv[0];
644 length = tlv[1];
645
646 pr_debug("type 0x%x length %d\n", type, length);
647
648 if (type == LLCP_TLV_SN) {
649 *sn_len = length;
650 return &tlv[2];
651 }
652
653 offset += length + 2;
654 tlv += length + 2;
655 }
656
657 return NULL;
658}
659
660static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100661 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100662{
663 struct sock *new_sk, *parent;
664 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200665 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100666
667 dsap = nfc_llcp_dsap(skb);
668 ssap = nfc_llcp_ssap(skb);
669
670 pr_debug("%d %d\n", dsap, ssap);
671
Samuel Ortizd6469602011-12-14 16:43:12 +0100672 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200673 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
674 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100675 reason = LLCP_DM_NOBOUND;
676 goto fail;
677 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100678 } else {
679 u8 *sn;
680 size_t sn_len;
681
682 sn = nfc_llcp_connect_sn(skb, &sn_len);
683 if (sn == NULL) {
684 reason = LLCP_DM_NOBOUND;
685 goto fail;
686 }
687
688 pr_debug("Service name length %zu\n", sn_len);
689
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200690 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
691 if (sock == NULL) {
692 reason = LLCP_DM_NOBOUND;
693 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100694 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100695 }
696
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200697 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100698
Samuel Ortizd6469602011-12-14 16:43:12 +0100699 parent = &sock->sk;
700
701 if (sk_acceptq_is_full(parent)) {
702 reason = LLCP_DM_REJ;
703 release_sock(&sock->sk);
704 sock_put(&sock->sk);
705 goto fail;
706 }
707
Samuel Ortiz8f500202012-06-25 15:46:28 +0200708 if (sock->ssap == LLCP_SDP_UNBOUND) {
709 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
710
711 pr_debug("First client, reserving %d\n", ssap);
712
713 if (ssap == LLCP_SAP_MAX) {
714 reason = LLCP_DM_REJ;
715 release_sock(&sock->sk);
716 sock_put(&sock->sk);
717 goto fail;
718 }
719
720 sock->ssap = ssap;
721 }
722
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100723 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100724 if (new_sk == NULL) {
725 reason = LLCP_DM_REJ;
726 release_sock(&sock->sk);
727 sock_put(&sock->sk);
728 goto fail;
729 }
730
731 new_sock = nfc_llcp_sock(new_sk);
732 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200733 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200734 new_sock->miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100735 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100736 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200737 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100738 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200739 new_sock->ssap = sock->ssap;
740 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
741 atomic_t *client_count;
742
743 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
744
745 client_count =
746 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
747
748 atomic_inc(client_count);
749 new_sock->reserved_ssap = sock->ssap;
750 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100751
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200752 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
753 skb->len - LLCP_HEADER_SIZE);
754
Samuel Ortizd6469602011-12-14 16:43:12 +0100755 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
756
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200757 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100758
759 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
760
761 nfc_get_device(local->dev->idx);
762
763 new_sk->sk_state = LLCP_CONNECTED;
764
765 /* Wake the listening processes */
766 parent->sk_data_ready(parent, 0);
767
768 /* Send CC */
769 nfc_llcp_send_cc(new_sock);
770
771 release_sock(&sock->sk);
772 sock_put(&sock->sk);
773
774 return;
775
776fail:
777 /* Send DM */
778 nfc_llcp_send_dm(local, dsap, ssap, reason);
779
780 return;
781
782}
783
Samuel Ortizd094afa2012-03-05 01:03:42 +0100784int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100785{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100786 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100787 struct nfc_llcp_local *local = sock->local;
788
789 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100790 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200791 sock->rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100792
793 /* Try to queue some I frames for transmission */
794 while (sock->remote_ready &&
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200795 skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +0200796 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100797
798 pdu = skb_dequeue(&sock->tx_queue);
799 if (pdu == NULL)
800 break;
801
802 /* Update N(S)/N(R) */
803 nfc_llcp_set_nrns(sock, pdu);
804
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100805 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100806 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100807 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100808
809 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100810}
811
Samuel Ortizd6469602011-12-14 16:43:12 +0100812static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100813 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100814{
815 struct nfc_llcp_sock *llcp_sock;
816 struct sock *sk;
817 u8 dsap, ssap, ptype, ns, nr;
818
819 ptype = nfc_llcp_ptype(skb);
820 dsap = nfc_llcp_dsap(skb);
821 ssap = nfc_llcp_ssap(skb);
822 ns = nfc_llcp_ns(skb);
823 nr = nfc_llcp_nr(skb);
824
825 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
826
827 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
828 if (llcp_sock == NULL) {
829 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
830 return;
831 }
832
833 sk = &llcp_sock->sk;
834 lock_sock(sk);
835 if (sk->sk_state == LLCP_CLOSED) {
836 release_sock(sk);
837 nfc_llcp_sock_put(llcp_sock);
838 }
839
Samuel Ortizd6469602011-12-14 16:43:12 +0100840 /* Pass the payload upstream */
841 if (ptype == LLCP_PDU_I) {
842 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
843
Samuel Ortiz53aef922012-03-05 01:03:36 +0100844 if (ns == llcp_sock->recv_n)
845 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
846 else
847 pr_err("Received out of sequence I PDU\n");
848
Samuel Ortizd6469602011-12-14 16:43:12 +0100849 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
850 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
851 pr_err("receive queue is full\n");
852 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
853 }
854 }
855
856 /* Remove skbs from the pending queue */
857 if (llcp_sock->send_ack_n != nr) {
858 struct sk_buff *s, *tmp;
859
860 llcp_sock->send_ack_n = nr;
861
Samuel Ortiz84457962012-05-30 17:48:29 +0200862 /* Remove and free all skbs until ns == nr */
863 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
864 skb_unlink(s, &llcp_sock->tx_pending_queue);
865 kfree_skb(s);
866
867 if (nfc_llcp_ns(s) == nr)
868 break;
869 }
870
871 /* Re-queue the remaining skbs for transmission */
872 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
873 s, tmp) {
874 skb_unlink(s, &llcp_sock->tx_pending_queue);
875 skb_queue_head(&local->tx_queue, s);
876 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100877 }
878
Samuel Ortiz53aef922012-03-05 01:03:36 +0100879 if (ptype == LLCP_PDU_RR)
880 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100881 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +0100882 llcp_sock->remote_ready = false;
883
Samuel Ortiz56af2562012-05-30 18:06:11 +0200884 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +0100885 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100886
887 release_sock(sk);
888 nfc_llcp_sock_put(llcp_sock);
889}
890
891static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100892 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100893{
894 struct nfc_llcp_sock *llcp_sock;
895 struct sock *sk;
896 u8 dsap, ssap;
897
898 dsap = nfc_llcp_dsap(skb);
899 ssap = nfc_llcp_ssap(skb);
900
901 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
902 if (llcp_sock == NULL) {
903 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
904 return;
905 }
906
907 sk = &llcp_sock->sk;
908 lock_sock(sk);
909 if (sk->sk_state == LLCP_CLOSED) {
910 release_sock(sk);
911 nfc_llcp_sock_put(llcp_sock);
912 }
913
Samuel Ortizd6469602011-12-14 16:43:12 +0100914 if (sk->sk_state == LLCP_CONNECTED) {
915 nfc_put_device(local->dev);
916 sk->sk_state = LLCP_CLOSED;
917 sk->sk_state_change(sk);
918 }
919
920 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
921
922 release_sock(sk);
923 nfc_llcp_sock_put(llcp_sock);
924}
925
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100926static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100927{
928 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +0200929 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +0100930 u8 dsap, ssap;
931
Samuel Ortizd6469602011-12-14 16:43:12 +0100932 dsap = nfc_llcp_dsap(skb);
933 ssap = nfc_llcp_ssap(skb);
934
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200935 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100936 if (llcp_sock == NULL) {
937 pr_err("Invalid CC\n");
938 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
939
940 return;
941 }
942
Samuel Ortizff353d82012-05-07 12:31:19 +0200943 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +0100944
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200945 /* Unlink from connecting and link to the client array */
946 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
947 nfc_llcp_sock_link(&local->sockets, sk);
948 llcp_sock->dsap = ssap;
949
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200950 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
951 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +0100952
Samuel Ortizff353d82012-05-07 12:31:19 +0200953 sk->sk_state = LLCP_CONNECTED;
954 sk->sk_state_change(sk);
955
Samuel Ortizd6469602011-12-14 16:43:12 +0100956 nfc_llcp_sock_put(llcp_sock);
957}
958
Samuel Ortiz5c0560b2012-06-26 16:13:29 +0200959static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
960{
961 struct nfc_llcp_sock *llcp_sock;
962 struct sock *sk;
963 u8 dsap, ssap, reason;
964
965 dsap = nfc_llcp_dsap(skb);
966 ssap = nfc_llcp_ssap(skb);
967 reason = skb->data[2];
968
969 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
970
971 switch (reason) {
972 case LLCP_DM_NOBOUND:
973 case LLCP_DM_REJ:
974 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
975 break;
976
977 default:
978 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
979 break;
980 }
981
982 if (llcp_sock == NULL) {
983 pr_err("Invalid DM\n");
984 return;
985 }
986
987 sk = &llcp_sock->sk;
988
989 sk->sk_err = ENXIO;
990 sk->sk_state = LLCP_CLOSED;
991 sk->sk_state_change(sk);
992
993 nfc_llcp_sock_put(llcp_sock);
994
995 return;
996}
997
Samuel Ortizd6469602011-12-14 16:43:12 +0100998static void nfc_llcp_rx_work(struct work_struct *work)
999{
1000 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001001 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001002 u8 dsap, ssap, ptype;
1003 struct sk_buff *skb;
1004
1005 skb = local->rx_pending;
1006 if (skb == NULL) {
1007 pr_debug("No pending SKB\n");
1008 return;
1009 }
1010
1011 ptype = nfc_llcp_ptype(skb);
1012 dsap = nfc_llcp_dsap(skb);
1013 ssap = nfc_llcp_ssap(skb);
1014
1015 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1016
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001017 if (ptype != LLCP_PDU_SYMM)
1018 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1019 16, 1, skb->data, skb->len, true);
1020
Samuel Ortizd6469602011-12-14 16:43:12 +01001021 switch (ptype) {
1022 case LLCP_PDU_SYMM:
1023 pr_debug("SYMM\n");
1024 break;
1025
1026 case LLCP_PDU_CONNECT:
1027 pr_debug("CONNECT\n");
1028 nfc_llcp_recv_connect(local, skb);
1029 break;
1030
1031 case LLCP_PDU_DISC:
1032 pr_debug("DISC\n");
1033 nfc_llcp_recv_disc(local, skb);
1034 break;
1035
1036 case LLCP_PDU_CC:
1037 pr_debug("CC\n");
1038 nfc_llcp_recv_cc(local, skb);
1039 break;
1040
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001041 case LLCP_PDU_DM:
1042 pr_debug("DM\n");
1043 nfc_llcp_recv_dm(local, skb);
1044 break;
1045
Samuel Ortizd6469602011-12-14 16:43:12 +01001046 case LLCP_PDU_I:
1047 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001048 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001049 pr_debug("I frame\n");
1050 nfc_llcp_recv_hdlc(local, skb);
1051 break;
1052
1053 }
1054
Tejun Heo474fee32012-08-22 16:22:16 -07001055 queue_work(system_nrt_wq, &local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001056 kfree_skb(local->rx_pending);
1057 local->rx_pending = NULL;
1058
1059 return;
1060}
1061
1062void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1063{
1064 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1065
1066 pr_debug("Received an LLCP PDU\n");
1067 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001068 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001069 return;
1070 }
1071
1072 local->rx_pending = skb_get(skb);
1073 del_timer(&local->link_timer);
Tejun Heo474fee32012-08-22 16:22:16 -07001074 queue_work(system_nrt_wq, &local->rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001075
1076 return;
1077}
1078
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001079int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1080{
1081 struct nfc_llcp_local *local;
1082
1083 local = nfc_llcp_find_local(dev);
1084 if (local == NULL)
1085 return -ENODEV;
1086
1087 local->rx_pending = skb_get(skb);
1088 del_timer(&local->link_timer);
Tejun Heo474fee32012-08-22 16:22:16 -07001089 queue_work(system_nrt_wq, &local->rx_work);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001090
1091 return 0;
1092}
1093
Samuel Ortizd6469602011-12-14 16:43:12 +01001094void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1095{
1096 struct nfc_llcp_local *local;
1097
1098 local = nfc_llcp_find_local(dev);
1099 if (local == NULL)
1100 return;
1101
1102 /* Close and purge all existing sockets */
Samuel Ortiz4d22ea12012-06-22 14:40:34 +02001103 nfc_llcp_socket_release(local, true);
Samuel Ortizd6469602011-12-14 16:43:12 +01001104}
1105
1106void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1107 u8 comm_mode, u8 rf_mode)
1108{
1109 struct nfc_llcp_local *local;
1110
1111 pr_debug("rf mode %d\n", rf_mode);
1112
1113 local = nfc_llcp_find_local(dev);
1114 if (local == NULL)
1115 return;
1116
1117 local->target_idx = target_idx;
1118 local->comm_mode = comm_mode;
1119 local->rf_mode = rf_mode;
1120
1121 if (rf_mode == NFC_RF_INITIATOR) {
1122 pr_debug("Queueing Tx work\n");
1123
Tejun Heo474fee32012-08-22 16:22:16 -07001124 queue_work(system_nrt_wq, &local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001125 } else {
1126 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001127 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001128 }
1129}
1130
1131int nfc_llcp_register_device(struct nfc_dev *ndev)
1132{
Samuel Ortizd6469602011-12-14 16:43:12 +01001133 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001134
1135 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1136 if (local == NULL)
1137 return -ENOMEM;
1138
1139 local->dev = ndev;
1140 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001141 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001142 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001143 init_timer(&local->link_timer);
1144 local->link_timer.data = (unsigned long) local;
1145 local->link_timer.function = nfc_llcp_symm_timer;
1146
1147 skb_queue_head_init(&local->tx_queue);
1148 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001149
1150 local->rx_pending = NULL;
1151 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001152
1153 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001154
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001155 local->sockets.lock = __RW_LOCK_UNLOCKED(local->sockets.lock);
1156 local->connecting_sockets.lock = __RW_LOCK_UNLOCKED(local->connecting_sockets.lock);
1157
Samuel Ortizd6469602011-12-14 16:43:12 +01001158 nfc_llcp_build_gb(local);
1159
1160 local->remote_miu = LLCP_DEFAULT_MIU;
1161 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001162
1163 list_add(&llcp_devices, &local->list);
1164
1165 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001166}
1167
1168void nfc_llcp_unregister_device(struct nfc_dev *dev)
1169{
1170 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1171
1172 if (local == NULL) {
1173 pr_debug("No such device\n");
1174 return;
1175 }
1176
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001177 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001178}
1179
1180int __init nfc_llcp_init(void)
1181{
1182 INIT_LIST_HEAD(&llcp_devices);
1183
1184 return nfc_llcp_sock_init();
1185}
1186
1187void nfc_llcp_exit(void)
1188{
1189 nfc_llcp_sock_exit();
1190}