/*
* 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_init(&s->list);
sk = &s->sk;
lock_sock(sk);
if (sk->sk_state == LLCP_CONNECTED)
nfc_put_device(s->dev);
sk->sk_state = LLCP_CLOSED;
release_sock(sk);
sock_orphan(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;
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;
release_sock(parent_sk);
sock_orphan(parent_sk);
}
mutex_unlock(&local->socket_lock);
}
struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
{
kref_get(&local->ref);
return local;
}
static void local_release(struct kref *ref)
{
struct nfc_llcp_local *local;
local = container_of(ref, struct nfc_llcp_local, ref);
list_del(&local->list);
nfc_llcp_socket_release(local);
del_timer_sync(&local->link_timer);
skb_queue_purge(&local->tx_queue);
destroy_workqueue(local->tx_wq);
destroy_workqueue(local->rx_wq);
kfree_skb(local->rx_pending);
kfree(local);
}
int nfc_llcp_local_put(struct nfc_llcp_local *local)
{
return kref_put(&local->ref, local_release);
}
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,