/*
* Copyright (C) 2011 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/nfc.h>
#include "../nfc.h"
#include "llcp.h"
static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
static struct list_head llcp_devices;
static void nfc_llcp_socket_release(struct nfc_llcp_local *local)
{
struct nfc_llcp_sock *parent, *s, *n;
struct sock *sk, *parent_sk;
int i;
mutex_lock(&local->socket_lock);
for (i = 0; i < LLCP_MAX_SAP; i++) {
parent = local->sockets[i];
if (parent == NULL)
continue;
/* Release all child sockets */
list_for_each_entry_safe(s, n, &parent->list, list) {
list_del(&s->list);
sk = &s->sk;
lock_sock(sk);
if (sk->sk_state == LLCP_CONNECTED)
nfc_put_device(s->dev);
sk->sk_state = LLCP_CLOSED;
sock_set_flag(sk, SOCK_DEAD);
release_sock(sk);
}
parent_sk = &parent->sk;
lock_sock(parent_sk);
if (parent_sk->sk_state == LLCP_LISTEN) {
struct nfc_llcp_sock *lsk, *n;
struct sock *accept_sk;
list_for_each_entry_safe(lsk, n, &parent->accept_queue,
accept_queue) {
accept_sk = &lsk->sk;
lock_sock(accept_sk);
nfc_llcp_accept_unlink(accept_sk);
accept_sk->sk_state = LLCP_CLOSED;
sock_set_flag(accept_sk, SOCK_DEAD);
release_sock(accept_sk);
sock_orphan(accept_sk);
}
}
if (parent_sk->sk_state == LLCP_CONNECTED)
nfc_put_device(parent->dev);
parent_sk->sk_state = LLCP_CLOSED;
sock_set_flag(parent_sk, SOCK_DEAD);
release_sock(parent_sk);
}
mutex_unlock(&local->socket_lock);
}
static void nfc_llcp_clear_sdp(struct nfc_llcp_local *local)
{
mutex_lock(&local->sdp_lock);
local->local_wks = 0;
local->local_sdp = 0;
local->local_sap = 0;
mutex_unlock(&local->sdp_lock);
}
static void nfc_llcp_timeout_work(struct work_struct *work)
{
struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
timeout_work);
nfc_dep_link_down(local->dev);
}
static void nfc_llcp_symm_timer(unsigned long data)
{
struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
pr_err("SYMM timeout\n");
queue_work(local->timeout_wq, &local->timeout_work);
}
struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
{
struct nfc_llcp_local *local, *n;
list_for_each_entry_safe(local, n, &llcp_devices, list)
if (local->dev == dev)
return local;
pr_debug("No device found\n");
return NULL;
}
static char *wks[] = {
NULL,
NULL, /* SDP */
"urn:nfc:sn:ip",
"urn:nfc:sn:obex",
"urn:nfc:sn:snep",
};
static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
{
int sap, num_wks;
pr_debug("%s\n", service_name);
if (service_name == NULL)
return -EINVAL;
num_wks = ARRAY_SIZE(wks);
for (sap = 0 ; sap < num_wks; sap++) {
if (wks[sap] == NULL)
continue;
if (strncmp(wks[sap], service_name, service_name_len) == 0)
return sap;
}
return -EINVAL;
}
u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,