45 #include <linux/capability.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/socket.h>
50 #include <linux/slab.h>
52 #include <linux/net.h>
54 #include <linux/poll.h>
56 #include <asm/ioctls.h>
57 #include <asm/uaccess.h>
66 static const struct proto_ops irda_stream_ops;
67 static const struct proto_ops irda_seqpacket_ops;
68 static const struct proto_ops irda_dgram_ops;
70 #ifdef CONFIG_IRDA_ULTRA
71 static const struct proto_ops irda_ultra_ops;
72 #define ULTRA_MAX_DATA 382
75 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
83 static int irda_data_indication(
void *instance,
void *sap,
struct sk_buff *
skb)
96 IRDA_DEBUG(1,
"%s(), error: no more mem!\n", __func__);
112 static void irda_disconnect_indication(
void *instance,
void *sap,
175 static void irda_connect_confirm(
void *instance,
void *sap,
177 __u32 max_sdu_size,
__u8 max_header_size,
197 self->max_header_size = max_header_size;
200 self->max_sdu_size_tx = max_sdu_size;
205 if (max_sdu_size != 0) {
210 self->max_data_size = irttp_get_max_seg_size(self->tsap);
213 if (max_sdu_size == 0) {
218 self->max_data_size = max_sdu_size;
221 self->max_data_size = irttp_get_max_seg_size(self->tsap);
224 IRDA_DEBUG(2,
"%s(), max_data_size=%d\n", __func__,
225 self->max_data_size);
240 static void irda_connect_indication(
void *instance,
void *sap,
258 self->max_header_size = max_header_size;
261 self->max_sdu_size_tx = max_sdu_size;
266 if (max_sdu_size != 0) {
272 self->max_data_size = irttp_get_max_seg_size(self->tsap);
275 if (max_sdu_size == 0) {
281 self->max_data_size = max_sdu_size;
284 self->max_data_size = irttp_get_max_seg_size(self->tsap);
287 IRDA_DEBUG(2,
"%s(), max_data_size=%d\n", __func__,
288 self->max_data_size);
302 static void irda_connect_response(
struct irda_sock *
self)
311 IRDA_DEBUG(0,
"%s() Unable to allocate sk_buff!\n",
328 static void irda_flow_indication(
void *instance,
void *sap,
LOCAL_FLOW flow)
341 IRDA_DEBUG(1,
"%s(), IrTTP wants us to slow down\n",
343 self->tx_flow = flow;
346 self->tx_flow = flow;
347 IRDA_DEBUG(1,
"%s(), IrTTP wants us to start again\n",
352 IRDA_DEBUG(0,
"%s(), Unknown flow command!\n", __func__);
354 self->tx_flow = flow;
367 static void irda_getvalue_confirm(
int result,
__u16 obj_id,
386 IRDA_DEBUG(1,
"%s(), IAS query failed! (%d)\n", __func__,
398 self->ias_result =
value;
413 static void irda_selective_discovery_indication(
discinfo_t *discovery,
428 self->cachedaddr = discovery->
daddr;
442 static void irda_discovery_timeout(
u_long priv)
452 self->cachelog =
NULL;
453 self->cachedaddr = 0;
454 self->errno = -
ETIME;
488 if (self->tsap ==
NULL) {
489 IRDA_DEBUG(0,
"%s(), Unable to allocate TSAP!\n",
494 self->stsap_sel =
self->tsap->stsap_sel;
505 #ifdef CONFIG_IRDA_ULTRA
506 static int irda_open_lsap(
struct irda_sock *
self,
int pid)
522 if (self->lsap ==
NULL) {
523 IRDA_DEBUG( 0,
"%s(), Unable to allocate LSAP!\n", __func__);
542 static int irda_find_lsap_sel(
struct irda_sock *
self,
char *name)
544 IRDA_DEBUG(2,
"%s(%p, %s)\n", __func__,
self, name);
553 irda_getvalue_confirm);
554 if(self->iriap ==
NULL)
562 name,
"IrDA:TinyTP:LsapSel");
581 switch (self->ias_result->type) {
584 __func__, self->ias_result->t.integer);
586 if (self->ias_result->t.integer != -1)
587 self->dtsap_sel =
self->ias_result->t.integer;
596 if (self->ias_result)
622 static int irda_discover_daddr_and_lsap_sel(
struct irda_sock *
self,
char *name)
631 IRDA_DEBUG(2,
"%s(), name=%s\n", __func__, name);
640 if (discoveries ==
NULL)
648 for(i = 0; i < number; i++) {
650 self->daddr = discoveries[
i].
daddr;
653 __func__, self->daddr);
656 err = irda_find_lsap_sel(
self, name);
661 IRDA_DEBUG(1,
"%s(), discovered service ''%s'' in two different devices !!!\n",
669 dtsap_sel =
self->dtsap_sel;
676 IRDA_DEBUG(0,
"%s(), unexpected IAS query failure\n", __func__);
688 IRDA_DEBUG(1,
"%s(), cannot discover service ''%s'' in any device !!!\n",
699 IRDA_DEBUG(1,
"%s(), discovered requested service ''%s'' at address %08x\n",
700 __func__, name, self->daddr);
712 int *uaddr_len,
int peer)
715 struct sock *sk = sock->
sk;
724 saddr.sir_lsap_sel =
self->dtsap_sel;
725 saddr.sir_addr =
self->daddr;
728 saddr.sir_lsap_sel =
self->stsap_sel;
729 saddr.sir_addr =
self->saddr;
750 struct sock *sk = sock->
sk;
779 static int irda_bind(
struct socket *sock,
struct sockaddr *uaddr,
int addr_len)
781 struct sock *sk = sock->
sk;
792 #ifdef CONFIG_IRDA_ULTRA
798 if (self->pid & 0x80) {
799 IRDA_DEBUG(0,
"%s(), extension in PID not supp!\n", __func__);
802 err = irda_open_lsap(
self, self->pid);
817 if (self->ias_obj ==
NULL)
823 self->ias_obj =
NULL;
846 struct sock *sk = sock->
sk;
847 struct irda_sock *
new, *
self = irda_sk(sk);
854 err = irda_create(sock_net(sk), newsock, sk->
sk_protocol, 0);
864 if ((sk = sock->
sk) ==
NULL)
912 new = irda_sk(newsk);
918 IRDA_DEBUG(0,
"%s(), dup failed!\n", __func__);
923 new->stsap_sel =
new->tsap->stsap_sel;
924 new->dtsap_sel =
new->tsap->dtsap_sel;
925 new->saddr = irttp_get_saddr(new->tsap);
926 new->daddr = irttp_get_daddr(new->tsap);
928 new->max_sdu_size_tx =
self->max_sdu_size_tx;
929 new->max_sdu_size_rx =
self->max_sdu_size_rx;
930 new->max_data_size =
self->max_data_size;
931 new->max_header_size =
self->max_header_size;
936 irttp_listen(self->tsap);
943 irda_connect_response(
new);
970 static int irda_connect(
struct socket *sock,
struct sockaddr *uaddr,
971 int addr_len,
int flags)
973 struct sock *sk = sock->
sk;
1012 err = irda_discover_daddr_and_lsap_sel(
self, addr->
sir_name);
1014 IRDA_DEBUG(0,
"%s(), auto-connect failed!\n", __func__);
1020 IRDA_DEBUG(1,
"%s(), daddr = %08x\n", __func__, self->daddr);
1028 err = irda_find_lsap_sel(
self, addr->
sir_name);
1030 IRDA_DEBUG(0,
"%s(), connect failed!\n", __func__);
1052 self->saddr, self->daddr,
NULL,
1053 self->max_sdu_size_rx,
NULL);
1055 IRDA_DEBUG(0,
"%s(), connect failed!\n", __func__);
1071 if (sk->sk_prot->disconnect(sk, flags))
1073 err = sock_error(sk);
1082 self->saddr = irttp_get_saddr(self->tsap);
1089 static struct proto irda_proto = {
1113 switch (sock->
type) {
1128 IRDA_DEBUG(2,
"%s() : self is %p\n", __func__,
self);
1132 switch (sock->
type) {
1134 sock->
ops = &irda_stream_ops;
1138 sock->
ops = &irda_seqpacket_ops;
1143 #ifdef CONFIG_IRDA_ULTRA
1145 sock->
ops = &irda_ultra_ops;
1153 sock->
ops = &irda_dgram_ops;
1174 self->mask.word = 0xffff;
1188 static void irda_destroy_socket(
struct irda_sock *
self)
1197 if (self->ias_obj) {
1199 self->ias_obj =
NULL;
1212 #ifdef CONFIG_IRDA_ULTRA
1223 static int irda_release(
struct socket *sock)
1225 struct sock *sk = sock->
sk;
1238 irda_destroy_socket(irda_sk(sk));
1290 struct sock *sk = sock->
sk;
1295 IRDA_DEBUG(4,
"%s(), len=%zd\n", __func__, len);
1330 if (len > self->max_data_size) {
1331 IRDA_DEBUG(2,
"%s(), Chopping frame from %zd to %d bytes!\n",
1332 __func__, len, self->max_data_size);
1333 len =
self->max_data_size;
1341 skb_reserve(skb, self->max_header_size + 16);
1342 skb_reset_transport_header(skb);
1356 IRDA_DEBUG(0,
"%s(), err=%d\n", __func__, err);
1378 static int irda_recvmsg_dgram(
struct kiocb *iocb,
struct socket *sock,
1381 struct sock *sk = sock->
sk;
1394 skb_reset_transport_header(skb);
1397 if (copied > size) {
1398 IRDA_DEBUG(2,
"%s(), Received truncated frame (%zd < %zd)!\n",
1399 __func__, copied, size);
1415 IRDA_DEBUG(2,
"%s(), Starting IrTTP\n", __func__);
1427 static int irda_recvmsg_stream(
struct kiocb *iocb,
struct socket *sock,
1428 struct msghdr *msg,
size_t size,
int flags)
1430 struct sock *sk = sock->
sk;
1439 if ((err = sock_error(sk)) < 0)
1450 target = sock_rcvlowat(sk, flags &
MSG_WAITALL, size);
1451 timeo = sock_rcvtimeo(sk, noblock);
1463 if (copied >= target)
1471 err = sock_error(sk);
1478 else if (signal_pending(
current))
1479 err = sock_intr_errno(timeo);
1496 chunk =
min_t(
unsigned int, skb->
len, size);
1520 IRDA_DEBUG(0,
"%s() questionable!?\n", __func__);
1536 IRDA_DEBUG(2,
"%s(), Starting IrTTP\n", __func__);
1552 static int irda_sendmsg_dgram(
struct kiocb *iocb,
struct socket *sock,
1553 struct msghdr *msg,
size_t len)
1555 struct sock *sk = sock->
sk;
1560 IRDA_DEBUG(4,
"%s(), len=%zd\n", __func__, len);
1583 if (len > self->max_data_size) {
1585 "Chopping frame from %zd to %d bytes!\n",
1586 __func__, len, self->max_data_size);
1587 len =
self->max_data_size;
1596 skb_reserve(skb, self->max_header_size);
1597 skb_reset_transport_header(skb);
1599 IRDA_DEBUG(4,
"%s(), appending user data\n", __func__);
1613 IRDA_DEBUG(0,
"%s(), err=%d\n", __func__, err);
1631 #ifdef CONFIG_IRDA_ULTRA
1632 static int irda_sendmsg_ultra(
struct kiocb *iocb,
struct socket *sock,
1633 struct msghdr *msg,
size_t len)
1635 struct sock *sk = sock->
sk;
1642 IRDA_DEBUG(4,
"%s(), len=%zd\n", __func__, len);
1670 IRDA_DEBUG(0,
"%s(), extension in PID not supp!\n", __func__);
1677 if ((self->lsap ==
NULL) ||
1679 IRDA_DEBUG(0,
"%s(), socket not bound to Ultra PID.\n",
1692 if (len > self->max_data_size) {
1694 "Chopping frame from %zd to %d bytes!\n",
1695 __func__, len, self->max_data_size);
1696 len =
self->max_data_size;
1705 skb_reserve(skb, self->max_header_size);
1706 skb_reset_transport_header(skb);
1708 IRDA_DEBUG(4,
"%s(), appending user data\n", __func__);
1716 err = irlmp_connless_data_request((bound ? self->lsap :
NULL),
1719 IRDA_DEBUG(0,
"%s(), err=%d\n", __func__, err);
1729 static int irda_shutdown(
struct socket *sock,
int how)
1731 struct sock *sk = sock->
sk;
1766 static unsigned int irda_poll(
struct file *
file,
struct socket *sock,
1769 struct sock *sk = sock->
sk;
1775 poll_wait(file, sk_sleep(sk), wait);
1816 if (sock_writeable(sk))
1829 static int irda_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1831 struct sock *sk = sock->
sk;
1834 IRDA_DEBUG(4,
"%s(), cmd=%#x\n", __func__, cmd);
1841 amount = sk->
sk_sndbuf - sk_wmem_alloc_get(sk);
1844 err =
put_user(amount, (
unsigned int __user *)arg);
1854 err =
put_user(amount, (
unsigned int __user *)arg);
1875 IRDA_DEBUG(1,
"%s(), doing device ioctl!\n", __func__);
1882 #ifdef CONFIG_COMPAT
1886 static int irda_compat_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1901 static int irda_setsockopt(
struct socket *sock,
int level,
int optname,
1902 char __user *optval,
unsigned int optlen)
1904 struct sock *sk = sock->
sk;
1909 int opt, free_ias = 0, err = 0;
1933 if (ias_opt ==
NULL) {
1950 if(self->ias_obj ==
NULL) {
1955 ias_obj =
self->ias_obj;
1963 ((ias_obj ==
NULL) || (ias_obj != self->ias_obj))) {
1974 if (ias_obj ==
NULL) {
2064 if (ias_opt ==
NULL) {
2081 ias_obj =
self->ias_obj;
2094 ((ias_obj ==
NULL) || (ias_obj != self->ias_obj))) {
2111 IRDA_DEBUG(1,
"%s(), attempting to delete a kernel attribute\n", __func__);
2122 if (optlen <
sizeof(
int)) {
2127 if (
get_user(opt, (
int __user *)optval)) {
2134 IRDA_DEBUG(2,
"%s(), setting max_sdu_size = %d\n",
2136 self->max_sdu_size_rx =
opt;
2138 IRDA_WARNING(
"%s: not allowed to set MAXSDUSIZE for this socket type!\n",
2145 if (optlen <
sizeof(
int)) {
2151 if (
get_user(opt, (
int __user *)optval)) {
2168 if (optlen <
sizeof(
int)) {
2174 if (
get_user(opt, (
int __user *)optval)) {
2180 self->mask.word = (
__u16) opt;
2182 self->mask.word &= 0x7f7f;
2184 if(!self->mask.word)
2185 self->mask.word = 0xFFFF;
2211 static int irda_extract_ias_value(
struct irda_ias_set *ias_opt,
2215 switch (ias_value->
type) {
2251 static int irda_getsockopt(
struct socket *sock,
int level,
int optname,
2252 char __user *optval,
int __user *optlen)
2254 struct sock *sk = sock->
sk;
2296 if (discoveries ==
NULL) {
2306 if (
list.len > 2048) {
2313 if (
copy_to_user(optval+offset, discoveries, total - offset))
2324 val =
self->max_data_size;
2349 if (ias_opt ==
NULL) {
2366 ias_obj =
self->ias_obj;
2385 err = irda_extract_ias_value(ias_opt, ias_attr->
value);
2413 if (ias_opt ==
NULL) {
2436 daddr =
self->daddr;
2440 daddr = ias_opt->
daddr;
2458 irda_getvalue_confirm);
2460 if (self->iriap ==
NULL) {
2477 (self->iriap ==
NULL))) {
2501 err = irda_extract_ias_value(ias_opt, self->ias_result);
2502 if (self->ias_result)
2535 if (len !=
sizeof(
int)) {
2540 if (
get_user(val, (
int __user *)optval)) {
2547 irda_selective_discovery_indication,
2548 NULL, (
void *)
self);
2554 if (!self->cachedaddr) {
2555 IRDA_DEBUG(1,
"%s(), nothing discovered yet, going to sleep...\n", __func__);
2559 setup_timer(&self->watchdog, irda_discovery_timeout,
2560 (
unsigned long)
self);
2566 (self->cachedaddr != 0 || self->errno == -
ETIME),
2570 if(timer_pending(&(self->watchdog)))
2573 IRDA_DEBUG(1,
"%s(), ...waking up !\n", __func__);
2587 if (!self->cachedaddr)
2589 daddr =
self->cachedaddr;
2591 self->cachedaddr = 0;
2599 if (
put_user(daddr, (
int __user *)optval)) {
2618 .create = irda_create,
2622 static const struct proto_ops irda_stream_ops = {
2625 .release = irda_release,
2627 .connect = irda_connect,
2629 .accept = irda_accept,
2630 .getname = irda_getname,
2632 .ioctl = irda_ioctl,
2633 #ifdef CONFIG_COMPAT
2634 .compat_ioctl = irda_compat_ioctl,
2636 .listen = irda_listen,
2637 .shutdown = irda_shutdown,
2638 .setsockopt = irda_setsockopt,
2639 .getsockopt = irda_getsockopt,
2640 .sendmsg = irda_sendmsg,
2641 .recvmsg = irda_recvmsg_stream,
2646 static const struct proto_ops irda_seqpacket_ops = {
2649 .release = irda_release,
2651 .connect = irda_connect,
2653 .accept = irda_accept,
2654 .getname = irda_getname,
2656 .ioctl = irda_ioctl,
2657 #ifdef CONFIG_COMPAT
2658 .compat_ioctl = irda_compat_ioctl,
2660 .listen = irda_listen,
2661 .shutdown = irda_shutdown,
2662 .setsockopt = irda_setsockopt,
2663 .getsockopt = irda_getsockopt,
2664 .sendmsg = irda_sendmsg,
2665 .recvmsg = irda_recvmsg_dgram,
2670 static const struct proto_ops irda_dgram_ops = {
2673 .release = irda_release,
2675 .connect = irda_connect,
2677 .accept = irda_accept,
2678 .getname = irda_getname,
2680 .ioctl = irda_ioctl,
2681 #ifdef CONFIG_COMPAT
2682 .compat_ioctl = irda_compat_ioctl,
2684 .listen = irda_listen,
2685 .shutdown = irda_shutdown,
2686 .setsockopt = irda_setsockopt,
2687 .getsockopt = irda_getsockopt,
2688 .sendmsg = irda_sendmsg_dgram,
2689 .recvmsg = irda_recvmsg_dgram,
2694 #ifdef CONFIG_IRDA_ULTRA
2695 static const struct proto_ops irda_ultra_ops = {
2698 .release = irda_release,
2703 .getname = irda_getname,
2705 .ioctl = irda_ioctl,
2706 #ifdef CONFIG_COMPAT
2707 .compat_ioctl = irda_compat_ioctl,
2710 .shutdown = irda_shutdown,
2711 .setsockopt = irda_setsockopt,
2712 .getsockopt = irda_getsockopt,
2713 .sendmsg = irda_sendmsg_ultra,
2714 .recvmsg = irda_recvmsg_dgram,