mm, thp: change pmd_trans_huge_lock() to return taken lock
[linux-2.6.git] / net / nfc / rawsock.c
blobcd958b381f9615911b1acfc6da6aceb39d373401
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 * Authors:
5 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
26 #include <net/tcp_states.h>
27 #include <linux/nfc.h>
28 #include <linux/export.h>
30 #include "nfc.h"
32 static void rawsock_write_queue_purge(struct sock *sk)
34 pr_debug("sk=%p\n", sk);
36 spin_lock_bh(&sk->sk_write_queue.lock);
37 __skb_queue_purge(&sk->sk_write_queue);
38 nfc_rawsock(sk)->tx_work_scheduled = false;
39 spin_unlock_bh(&sk->sk_write_queue.lock);
42 static void rawsock_report_error(struct sock *sk, int err)
44 pr_debug("sk=%p err=%d\n", sk, err);
46 sk->sk_shutdown = SHUTDOWN_MASK;
47 sk->sk_err = -err;
48 sk->sk_error_report(sk);
50 rawsock_write_queue_purge(sk);
53 static int rawsock_release(struct socket *sock)
55 struct sock *sk = sock->sk;
57 pr_debug("sock=%p sk=%p\n", sock, sk);
59 if (!sk)
60 return 0;
62 sock_orphan(sk);
63 sock_put(sk);
65 return 0;
68 static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
69 int len, int flags)
71 struct sock *sk = sock->sk;
72 struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
73 struct nfc_dev *dev;
74 int rc = 0;
76 pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags);
78 if (!addr || len < sizeof(struct sockaddr_nfc) ||
79 addr->sa_family != AF_NFC)
80 return -EINVAL;
82 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
83 addr->dev_idx, addr->target_idx, addr->nfc_protocol);
85 lock_sock(sk);
87 if (sock->state == SS_CONNECTED) {
88 rc = -EISCONN;
89 goto error;
92 dev = nfc_get_device(addr->dev_idx);
93 if (!dev) {
94 rc = -ENODEV;
95 goto error;
98 if (addr->target_idx > dev->target_next_idx - 1 ||
99 addr->target_idx < dev->target_next_idx - dev->n_targets) {
100 rc = -EINVAL;
101 goto error;
104 rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
105 if (rc)
106 goto put_dev;
108 nfc_rawsock(sk)->dev = dev;
109 nfc_rawsock(sk)->target_idx = addr->target_idx;
110 sock->state = SS_CONNECTED;
111 sk->sk_state = TCP_ESTABLISHED;
112 sk->sk_state_change(sk);
114 release_sock(sk);
115 return 0;
117 put_dev:
118 nfc_put_device(dev);
119 error:
120 release_sock(sk);
121 return rc;
124 static int rawsock_add_header(struct sk_buff *skb)
126 *skb_push(skb, NFC_HEADER_SIZE) = 0;
128 return 0;
131 static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
132 int err)
134 struct sock *sk = (struct sock *) context;
136 BUG_ON(in_irq());
138 pr_debug("sk=%p err=%d\n", sk, err);
140 if (err)
141 goto error;
143 err = rawsock_add_header(skb);
144 if (err)
145 goto error_skb;
147 err = sock_queue_rcv_skb(sk, skb);
148 if (err)
149 goto error_skb;
151 spin_lock_bh(&sk->sk_write_queue.lock);
152 if (!skb_queue_empty(&sk->sk_write_queue))
153 schedule_work(&nfc_rawsock(sk)->tx_work);
154 else
155 nfc_rawsock(sk)->tx_work_scheduled = false;
156 spin_unlock_bh(&sk->sk_write_queue.lock);
158 sock_put(sk);
159 return;
161 error_skb:
162 kfree_skb(skb);
164 error:
165 rawsock_report_error(sk, err);
166 sock_put(sk);
169 static void rawsock_tx_work(struct work_struct *work)
171 struct sock *sk = to_rawsock_sk(work);
172 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
173 u32 target_idx = nfc_rawsock(sk)->target_idx;
174 struct sk_buff *skb;
175 int rc;
177 pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
179 if (sk->sk_shutdown & SEND_SHUTDOWN) {
180 rawsock_write_queue_purge(sk);
181 return;
184 skb = skb_dequeue(&sk->sk_write_queue);
186 sock_hold(sk);
187 rc = nfc_data_exchange(dev, target_idx, skb,
188 rawsock_data_exchange_complete, sk);
189 if (rc) {
190 rawsock_report_error(sk, rc);
191 sock_put(sk);
195 static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
196 struct msghdr *msg, size_t len)
198 struct sock *sk = sock->sk;
199 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
200 struct sk_buff *skb;
201 int rc;
203 pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
205 if (msg->msg_namelen)
206 return -EOPNOTSUPP;
208 if (sock->state != SS_CONNECTED)
209 return -ENOTCONN;
211 skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
212 if (skb == NULL)
213 return rc;
215 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
216 if (rc < 0) {
217 kfree_skb(skb);
218 return rc;
221 spin_lock_bh(&sk->sk_write_queue.lock);
222 __skb_queue_tail(&sk->sk_write_queue, skb);
223 if (!nfc_rawsock(sk)->tx_work_scheduled) {
224 schedule_work(&nfc_rawsock(sk)->tx_work);
225 nfc_rawsock(sk)->tx_work_scheduled = true;
227 spin_unlock_bh(&sk->sk_write_queue.lock);
229 return len;
232 static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
233 struct msghdr *msg, size_t len, int flags)
235 int noblock = flags & MSG_DONTWAIT;
236 struct sock *sk = sock->sk;
237 struct sk_buff *skb;
238 int copied;
239 int rc;
241 pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
243 skb = skb_recv_datagram(sk, flags, noblock, &rc);
244 if (!skb)
245 return rc;
247 msg->msg_namelen = 0;
249 copied = skb->len;
250 if (len < copied) {
251 msg->msg_flags |= MSG_TRUNC;
252 copied = len;
255 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
257 skb_free_datagram(sk, skb);
259 return rc ? : copied;
262 static const struct proto_ops rawsock_ops = {
263 .family = PF_NFC,
264 .owner = THIS_MODULE,
265 .release = rawsock_release,
266 .bind = sock_no_bind,
267 .connect = rawsock_connect,
268 .socketpair = sock_no_socketpair,
269 .accept = sock_no_accept,
270 .getname = sock_no_getname,
271 .poll = datagram_poll,
272 .ioctl = sock_no_ioctl,
273 .listen = sock_no_listen,
274 .shutdown = sock_no_shutdown,
275 .setsockopt = sock_no_setsockopt,
276 .getsockopt = sock_no_getsockopt,
277 .sendmsg = rawsock_sendmsg,
278 .recvmsg = rawsock_recvmsg,
279 .mmap = sock_no_mmap,
282 static void rawsock_destruct(struct sock *sk)
284 pr_debug("sk=%p\n", sk);
286 if (sk->sk_state == TCP_ESTABLISHED) {
287 nfc_deactivate_target(nfc_rawsock(sk)->dev,
288 nfc_rawsock(sk)->target_idx);
289 nfc_put_device(nfc_rawsock(sk)->dev);
292 skb_queue_purge(&sk->sk_receive_queue);
294 if (!sock_flag(sk, SOCK_DEAD)) {
295 pr_err("Freeing alive NFC raw socket %p\n", sk);
296 return;
300 static int rawsock_create(struct net *net, struct socket *sock,
301 const struct nfc_protocol *nfc_proto)
303 struct sock *sk;
305 pr_debug("sock=%p\n", sock);
307 if (sock->type != SOCK_SEQPACKET)
308 return -ESOCKTNOSUPPORT;
310 sock->ops = &rawsock_ops;
312 sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
313 if (!sk)
314 return -ENOMEM;
316 sock_init_data(sock, sk);
317 sk->sk_protocol = nfc_proto->id;
318 sk->sk_destruct = rawsock_destruct;
319 sock->state = SS_UNCONNECTED;
321 INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
322 nfc_rawsock(sk)->tx_work_scheduled = false;
324 return 0;
327 static struct proto rawsock_proto = {
328 .name = "NFC_RAW",
329 .owner = THIS_MODULE,
330 .obj_size = sizeof(struct nfc_rawsock),
333 static const struct nfc_protocol rawsock_nfc_proto = {
334 .id = NFC_SOCKPROTO_RAW,
335 .proto = &rawsock_proto,
336 .owner = THIS_MODULE,
337 .create = rawsock_create
340 int __init rawsock_init(void)
342 int rc;
344 rc = nfc_proto_register(&rawsock_nfc_proto);
346 return rc;
349 void rawsock_exit(void)
351 nfc_proto_unregister(&rawsock_nfc_proto);