2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
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, see <http://www.gnu.org/licenses/>.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
24 #include <net/tcp_states.h>
25 #include <linux/nfc.h>
26 #include <linux/export.h>
30 static void rawsock_write_queue_purge(struct sock
*sk
)
32 pr_debug("sk=%p\n", 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 pr_debug("sk=%p err=%d\n", sk
, err
);
44 sk
->sk_shutdown
= SHUTDOWN_MASK
;
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 pr_debug("sock=%p sk=%p\n", sock
, sk
);
66 static int rawsock_connect(struct socket
*sock
, struct sockaddr
*_addr
,
69 struct sock
*sk
= sock
->sk
;
70 struct sockaddr_nfc
*addr
= (struct sockaddr_nfc
*)_addr
;
74 pr_debug("sock=%p sk=%p flags=%d\n", sock
, sk
, flags
);
76 if (!addr
|| len
< sizeof(struct sockaddr_nfc
) ||
77 addr
->sa_family
!= AF_NFC
)
80 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
81 addr
->dev_idx
, addr
->target_idx
, addr
->nfc_protocol
);
85 if (sock
->state
== SS_CONNECTED
) {
90 dev
= nfc_get_device(addr
->dev_idx
);
96 if (addr
->target_idx
> dev
->target_next_idx
- 1 ||
97 addr
->target_idx
< dev
->target_next_idx
- dev
->n_targets
) {
102 rc
= nfc_activate_target(dev
, addr
->target_idx
, addr
->nfc_protocol
);
106 nfc_rawsock(sk
)->dev
= dev
;
107 nfc_rawsock(sk
)->target_idx
= addr
->target_idx
;
108 sock
->state
= SS_CONNECTED
;
109 sk
->sk_state
= TCP_ESTABLISHED
;
110 sk
->sk_state_change(sk
);
122 static int rawsock_add_header(struct sk_buff
*skb
)
124 *skb_push(skb
, NFC_HEADER_SIZE
) = 0;
129 static void rawsock_data_exchange_complete(void *context
, struct sk_buff
*skb
,
132 struct sock
*sk
= (struct sock
*) context
;
136 pr_debug("sk=%p err=%d\n", sk
, err
);
141 err
= rawsock_add_header(skb
);
145 err
= sock_queue_rcv_skb(sk
, skb
);
149 spin_lock_bh(&sk
->sk_write_queue
.lock
);
150 if (!skb_queue_empty(&sk
->sk_write_queue
))
151 schedule_work(&nfc_rawsock(sk
)->tx_work
);
153 nfc_rawsock(sk
)->tx_work_scheduled
= false;
154 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
163 rawsock_report_error(sk
, err
);
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
;
175 pr_debug("sk=%p target_idx=%u\n", sk
, target_idx
);
177 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
178 rawsock_write_queue_purge(sk
);
182 skb
= skb_dequeue(&sk
->sk_write_queue
);
185 rc
= nfc_data_exchange(dev
, target_idx
, skb
,
186 rawsock_data_exchange_complete
, sk
);
188 rawsock_report_error(sk
, rc
);
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
;
201 pr_debug("sock=%p sk=%p len=%zu\n", sock
, sk
, len
);
203 if (msg
->msg_namelen
)
206 if (sock
->state
!= SS_CONNECTED
)
209 skb
= nfc_alloc_send_skb(dev
, sk
, msg
->msg_flags
, len
, &rc
);
213 rc
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
219 spin_lock_bh(&sk
->sk_write_queue
.lock
);
220 __skb_queue_tail(&sk
->sk_write_queue
, skb
);
221 if (!nfc_rawsock(sk
)->tx_work_scheduled
) {
222 schedule_work(&nfc_rawsock(sk
)->tx_work
);
223 nfc_rawsock(sk
)->tx_work_scheduled
= true;
225 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
230 static int rawsock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
231 struct msghdr
*msg
, size_t len
, int flags
)
233 int noblock
= flags
& MSG_DONTWAIT
;
234 struct sock
*sk
= sock
->sk
;
239 pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock
, sk
, len
, flags
);
241 skb
= skb_recv_datagram(sk
, flags
, noblock
, &rc
);
247 msg
->msg_flags
|= MSG_TRUNC
;
251 rc
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
253 skb_free_datagram(sk
, skb
);
255 return rc
? : copied
;
258 static const struct proto_ops rawsock_ops
= {
260 .owner
= THIS_MODULE
,
261 .release
= rawsock_release
,
262 .bind
= sock_no_bind
,
263 .connect
= rawsock_connect
,
264 .socketpair
= sock_no_socketpair
,
265 .accept
= sock_no_accept
,
266 .getname
= sock_no_getname
,
267 .poll
= datagram_poll
,
268 .ioctl
= sock_no_ioctl
,
269 .listen
= sock_no_listen
,
270 .shutdown
= sock_no_shutdown
,
271 .setsockopt
= sock_no_setsockopt
,
272 .getsockopt
= sock_no_getsockopt
,
273 .sendmsg
= rawsock_sendmsg
,
274 .recvmsg
= rawsock_recvmsg
,
275 .mmap
= sock_no_mmap
,
278 static void rawsock_destruct(struct sock
*sk
)
280 pr_debug("sk=%p\n", sk
);
282 if (sk
->sk_state
== TCP_ESTABLISHED
) {
283 nfc_deactivate_target(nfc_rawsock(sk
)->dev
,
284 nfc_rawsock(sk
)->target_idx
);
285 nfc_put_device(nfc_rawsock(sk
)->dev
);
288 skb_queue_purge(&sk
->sk_receive_queue
);
290 if (!sock_flag(sk
, SOCK_DEAD
)) {
291 pr_err("Freeing alive NFC raw socket %p\n", sk
);
296 static int rawsock_create(struct net
*net
, struct socket
*sock
,
297 const struct nfc_protocol
*nfc_proto
)
301 pr_debug("sock=%p\n", sock
);
303 if (sock
->type
!= SOCK_SEQPACKET
)
304 return -ESOCKTNOSUPPORT
;
306 sock
->ops
= &rawsock_ops
;
308 sk
= sk_alloc(net
, PF_NFC
, GFP_ATOMIC
, nfc_proto
->proto
);
312 sock_init_data(sock
, sk
);
313 sk
->sk_protocol
= nfc_proto
->id
;
314 sk
->sk_destruct
= rawsock_destruct
;
315 sock
->state
= SS_UNCONNECTED
;
317 INIT_WORK(&nfc_rawsock(sk
)->tx_work
, rawsock_tx_work
);
318 nfc_rawsock(sk
)->tx_work_scheduled
= false;
323 static struct proto rawsock_proto
= {
325 .owner
= THIS_MODULE
,
326 .obj_size
= sizeof(struct nfc_rawsock
),
329 static const struct nfc_protocol rawsock_nfc_proto
= {
330 .id
= NFC_SOCKPROTO_RAW
,
331 .proto
= &rawsock_proto
,
332 .owner
= THIS_MODULE
,
333 .create
= rawsock_create
336 int __init
rawsock_init(void)
340 rc
= nfc_proto_register(&rawsock_nfc_proto
);
345 void rawsock_exit(void)
347 nfc_proto_unregister(&rawsock_nfc_proto
);