Revert "ASoC: Mark WM5100 register map cache only when going into BIAS_OFF"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / nfc / rawsock.c
blobee7b2b365ef2225e7bf6f4d2f71b0a03edfb7888
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 #include <net/tcp_states.h>
25 #include <linux/nfc.h>
26 #include <linux/export.h>
28 #include "nfc.h"
30 static void rawsock_write_queue_purge(struct sock *sk)
32 nfc_dbg("sk=%p", sk);
34 spin_lock_bh(&sk->sk_write_queue.lock);
35 __skb_queue_purge(&sk->sk_write_queue);
36 nfc_rawsock(sk)->tx_work_scheduled = false;
37 spin_unlock_bh(&sk->sk_write_queue.lock);
40 static void rawsock_report_error(struct sock *sk, int err)
42 nfc_dbg("sk=%p err=%d", sk, err);
44 sk->sk_shutdown = SHUTDOWN_MASK;
45 sk->sk_err = -err;
46 sk->sk_error_report(sk);
48 rawsock_write_queue_purge(sk);
51 static int rawsock_release(struct socket *sock)
53 struct sock *sk = sock->sk;
55 nfc_dbg("sock=%p", sock);
57 sock_orphan(sk);
58 sock_put(sk);
60 return 0;
63 static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
64 int len, int flags)
66 struct sock *sk = sock->sk;
67 struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
68 struct nfc_dev *dev;
69 int rc = 0;
71 nfc_dbg("sock=%p sk=%p flags=%d", sock, sk, flags);
73 if (!addr || len < sizeof(struct sockaddr_nfc) ||
74 addr->sa_family != AF_NFC)
75 return -EINVAL;
77 nfc_dbg("addr dev_idx=%u target_idx=%u protocol=%u", addr->dev_idx,
78 addr->target_idx, addr->nfc_protocol);
80 lock_sock(sk);
82 if (sock->state == SS_CONNECTED) {
83 rc = -EISCONN;
84 goto error;
87 dev = nfc_get_device(addr->dev_idx);
88 if (!dev) {
89 rc = -ENODEV;
90 goto error;
93 if (addr->target_idx > dev->target_idx - 1 ||
94 addr->target_idx < dev->target_idx - dev->n_targets) {
95 rc = -EINVAL;
96 goto error;
99 if (addr->target_idx > dev->target_idx - 1 ||
100 addr->target_idx < dev->target_idx - dev->n_targets) {
101 rc = -EINVAL;
102 goto error;
105 rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
106 if (rc)
107 goto put_dev;
109 nfc_rawsock(sk)->dev = dev;
110 nfc_rawsock(sk)->target_idx = addr->target_idx;
111 sock->state = SS_CONNECTED;
112 sk->sk_state = TCP_ESTABLISHED;
113 sk->sk_state_change(sk);
115 release_sock(sk);
116 return 0;
118 put_dev:
119 nfc_put_device(dev);
120 error:
121 release_sock(sk);
122 return rc;
125 static int rawsock_add_header(struct sk_buff *skb)
127 *skb_push(skb, NFC_HEADER_SIZE) = 0;
129 return 0;
132 static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
133 int err)
135 struct sock *sk = (struct sock *) context;
137 BUG_ON(in_irq());
139 nfc_dbg("sk=%p err=%d", sk, err);
141 if (err)
142 goto error;
144 err = rawsock_add_header(skb);
145 if (err)
146 goto error;
148 err = sock_queue_rcv_skb(sk, skb);
149 if (err)
150 goto error;
152 spin_lock_bh(&sk->sk_write_queue.lock);
153 if (!skb_queue_empty(&sk->sk_write_queue))
154 schedule_work(&nfc_rawsock(sk)->tx_work);
155 else
156 nfc_rawsock(sk)->tx_work_scheduled = false;
157 spin_unlock_bh(&sk->sk_write_queue.lock);
159 sock_put(sk);
160 return;
162 error:
163 rawsock_report_error(sk, err);
164 sock_put(sk);
167 static void rawsock_tx_work(struct work_struct *work)
169 struct sock *sk = to_rawsock_sk(work);
170 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
171 u32 target_idx = nfc_rawsock(sk)->target_idx;
172 struct sk_buff *skb;
173 int rc;
175 nfc_dbg("sk=%p target_idx=%u", sk, target_idx);
177 if (sk->sk_shutdown & SEND_SHUTDOWN) {
178 rawsock_write_queue_purge(sk);
179 return;
182 skb = skb_dequeue(&sk->sk_write_queue);
184 sock_hold(sk);
185 rc = nfc_data_exchange(dev, target_idx, skb,
186 rawsock_data_exchange_complete, sk);
187 if (rc) {
188 rawsock_report_error(sk, rc);
189 sock_put(sk);
193 static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
194 struct msghdr *msg, size_t len)
196 struct sock *sk = sock->sk;
197 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
198 struct sk_buff *skb;
199 int rc;
201 nfc_dbg("sock=%p sk=%p len=%zu", sock, sk, len);
203 if (msg->msg_namelen)
204 return -EOPNOTSUPP;
206 if (sock->state != SS_CONNECTED)
207 return -ENOTCONN;
209 skb = sock_alloc_send_skb(sk, len + dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE,
210 msg->msg_flags & MSG_DONTWAIT, &rc);
211 if (!skb)
212 return rc;
214 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
216 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
217 if (rc < 0) {
218 kfree_skb(skb);
219 return rc;
222 spin_lock_bh(&sk->sk_write_queue.lock);
223 __skb_queue_tail(&sk->sk_write_queue, skb);
224 if (!nfc_rawsock(sk)->tx_work_scheduled) {
225 schedule_work(&nfc_rawsock(sk)->tx_work);
226 nfc_rawsock(sk)->tx_work_scheduled = true;
228 spin_unlock_bh(&sk->sk_write_queue.lock);
230 return len;
233 static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
234 struct msghdr *msg, size_t len, int flags)
236 int noblock = flags & MSG_DONTWAIT;
237 struct sock *sk = sock->sk;
238 struct sk_buff *skb;
239 int copied;
240 int rc;
242 nfc_dbg("sock=%p sk=%p len=%zu flags=%d", sock, sk, len, flags);
244 skb = skb_recv_datagram(sk, flags, noblock, &rc);
245 if (!skb)
246 return rc;
248 msg->msg_namelen = 0;
250 copied = skb->len;
251 if (len < copied) {
252 msg->msg_flags |= MSG_TRUNC;
253 copied = len;
256 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
258 skb_free_datagram(sk, skb);
260 return rc ? : copied;
264 static const struct proto_ops rawsock_ops = {
265 .family = PF_NFC,
266 .owner = THIS_MODULE,
267 .release = rawsock_release,
268 .bind = sock_no_bind,
269 .connect = rawsock_connect,
270 .socketpair = sock_no_socketpair,
271 .accept = sock_no_accept,
272 .getname = sock_no_getname,
273 .poll = datagram_poll,
274 .ioctl = sock_no_ioctl,
275 .listen = sock_no_listen,
276 .shutdown = sock_no_shutdown,
277 .setsockopt = sock_no_setsockopt,
278 .getsockopt = sock_no_getsockopt,
279 .sendmsg = rawsock_sendmsg,
280 .recvmsg = rawsock_recvmsg,
281 .mmap = sock_no_mmap,
284 static void rawsock_destruct(struct sock *sk)
286 nfc_dbg("sk=%p", sk);
288 if (sk->sk_state == TCP_ESTABLISHED) {
289 nfc_deactivate_target(nfc_rawsock(sk)->dev,
290 nfc_rawsock(sk)->target_idx);
291 nfc_put_device(nfc_rawsock(sk)->dev);
294 skb_queue_purge(&sk->sk_receive_queue);
296 if (!sock_flag(sk, SOCK_DEAD)) {
297 nfc_err("Freeing alive NFC raw socket %p", sk);
298 return;
302 static int rawsock_create(struct net *net, struct socket *sock,
303 const struct nfc_protocol *nfc_proto)
305 struct sock *sk;
307 nfc_dbg("sock=%p", sock);
309 if (sock->type != SOCK_SEQPACKET)
310 return -ESOCKTNOSUPPORT;
312 sock->ops = &rawsock_ops;
314 sk = sk_alloc(net, PF_NFC, GFP_KERNEL, nfc_proto->proto);
315 if (!sk)
316 return -ENOMEM;
318 sock_init_data(sock, sk);
319 sk->sk_protocol = nfc_proto->id;
320 sk->sk_destruct = rawsock_destruct;
321 sock->state = SS_UNCONNECTED;
323 INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
324 nfc_rawsock(sk)->tx_work_scheduled = false;
326 return 0;
329 static struct proto rawsock_proto = {
330 .name = "NFC_RAW",
331 .owner = THIS_MODULE,
332 .obj_size = sizeof(struct nfc_rawsock),
335 static const struct nfc_protocol rawsock_nfc_proto = {
336 .id = NFC_SOCKPROTO_RAW,
337 .proto = &rawsock_proto,
338 .owner = THIS_MODULE,
339 .create = rawsock_create
342 int __init rawsock_init(void)
344 int rc;
346 rc = nfc_proto_register(&rawsock_nfc_proto);
348 return rc;
351 void rawsock_exit(void)
353 nfc_proto_unregister(&rawsock_nfc_proto);