1 /*********************************************************************
5 * Description: IrDA sockets implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun May 31 10:12:43 1998
9 * Modified at: Fri Oct 1 19:11:04 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
13 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/types.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/init.h>
30 #include <linux/if_arp.h>
31 #include <linux/net.h>
32 #include <linux/irda.h>
33 #include <linux/poll.h>
35 #include <asm/uaccess.h>
39 #include <net/irda/irda.h>
40 #include <net/irda/iriap.h>
41 #include <net/irda/irias_object.h>
42 #include <net/irda/irttp.h>
43 #include <net/irda/discovery.h>
45 extern int irda_init(void);
46 extern void irda_cleanup(void);
47 extern int irlap_driver_rcv(struct sk_buff
*, struct net_device
*,
48 struct packet_type
*);
50 static int irda_create(struct socket
*sock
, int protocol
);
52 static struct proto_ops irda_stream_ops
;
53 static struct proto_ops irda_dgram_ops
;
54 static hashbin_t
*cachelog
= NULL
;
55 static DECLARE_WAIT_QUEUE_HEAD(discovery_wait
); /* Wait for discovery */
57 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
60 * Function irda_data_indication (instance, sap, skb)
62 * Received some data from TinyTP. Just queue it on the receive queue
65 static int irda_data_indication(void *instance
, void *sap
, struct sk_buff
*skb
)
67 struct irda_sock
*self
;
71 self
= (struct irda_sock
*) instance
;
72 ASSERT(self
!= NULL
, return -1;);
75 ASSERT(sk
!= NULL
, return -1;);
77 err
= sock_queue_rcv_skb(sk
, skb
);
79 IRDA_DEBUG(1, __FUNCTION__
"(), error: no more mem!\n");
80 self
->rx_flow
= FLOW_STOP
;
82 /* When we return error, TTP will need to requeue the skb */
90 * Function irda_disconnect_indication (instance, sap, reason, skb)
92 * Connection has been closed. Chech reason to find out why
95 static void irda_disconnect_indication(void *instance
, void *sap
,
96 LM_REASON reason
, struct sk_buff
*skb
)
98 struct irda_sock
*self
;
101 IRDA_DEBUG(2, __FUNCTION__
"()\n");
103 self
= (struct irda_sock
*) instance
;
109 sk
->state
= TCP_CLOSE
;
111 sk
->shutdown
|= SEND_SHUTDOWN
;
113 sk
->state_change(sk
);
119 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
121 * Connections has been confirmed by the remote device
124 static void irda_connect_confirm(void *instance
, void *sap
,
125 struct qos_info
*qos
,
126 __u32 max_sdu_size
, __u8 max_header_size
,
129 struct irda_sock
*self
;
132 IRDA_DEBUG(2, __FUNCTION__
"()\n");
134 self
= (struct irda_sock
*) instance
;
136 /* How much header space do we need to reserve */
137 self
->max_header_size
= max_header_size
;
139 /* IrTTP max SDU size in transmit direction */
140 self
->max_sdu_size_tx
= max_sdu_size
;
142 /* Find out what the largest chunk of data that we can transmit is */
143 if (max_sdu_size
== SAR_DISABLE
)
144 self
->max_data_size
= irttp_get_max_seq_size(self
->tsap
);
146 self
->max_data_size
= max_sdu_size
;
148 IRDA_DEBUG(1, __FUNCTION__
"(), max_data_size=%d\n", self
->max_data_size
);
150 memcpy(&self
->qos_tx
, qos
, sizeof(struct qos_info
));
156 skb_queue_tail(&sk
->receive_queue
, skb
);
158 /* We are now connected! */
159 sk
->state
= TCP_ESTABLISHED
;
160 sk
->state_change(sk
);
164 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
166 * Incomming connection
169 static void irda_connect_indication(void *instance
, void *sap
,
170 struct qos_info
*qos
, __u32 max_sdu_size
,
171 __u8 max_header_size
, struct sk_buff
*skb
)
173 struct irda_sock
*self
;
176 IRDA_DEBUG(2, __FUNCTION__
"()\n");
178 self
= (struct irda_sock
*) instance
;
180 /* How much header space do we need to reserve */
181 self
->max_header_size
= max_header_size
;
183 /* IrTTP max SDU size in transmit direction */
184 self
->max_sdu_size_tx
= max_sdu_size
;
186 /* Find out what the largest chunk of data that we can transmit is */
187 if (max_sdu_size
== SAR_DISABLE
)
188 self
->max_data_size
= irttp_get_max_seq_size(self
->tsap
);
190 self
->max_data_size
= max_sdu_size
;
192 IRDA_DEBUG(1, __FUNCTION__
"(), max_data_size=%d\n", self
->max_data_size
);
194 memcpy(&self
->qos_tx
, qos
, sizeof(struct qos_info
));
200 skb_queue_tail(&sk
->receive_queue
, skb
);
202 sk
->state_change(sk
);
206 * Function irda_connect_response (handle)
208 * Accept incomming connection
211 void irda_connect_response(struct irda_sock
*self
)
215 IRDA_DEBUG(1, __FUNCTION__
"()\n");
217 ASSERT(self
!= NULL
, return;);
219 skb
= dev_alloc_skb(64);
221 IRDA_DEBUG(0, __FUNCTION__
"() Unable to allocate sk_buff!\n");
225 /* Reserve space for MUX_CONTROL and LAP header */
226 skb_reserve(skb
, IRDA_MAX_HEADER
);
228 irttp_connect_response(self
->tsap
, self
->max_sdu_size_rx
, skb
);
232 * Function irda_flow_indication (instance, sap, flow)
234 * Used by TinyTP to tell us if it can accept more data or not
237 static void irda_flow_indication(void *instance
, void *sap
, LOCAL_FLOW flow
)
239 struct irda_sock
*self
;
242 IRDA_DEBUG(1, __FUNCTION__
"()\n");
244 self
= (struct irda_sock
*) instance
;
245 ASSERT(self
!= NULL
, return;);
248 ASSERT(sk
!= NULL
, return;);
252 IRDA_DEBUG(1, __FUNCTION__
"(), IrTTP wants us to slow down\n");
253 self
->tx_flow
= flow
;
256 self
->tx_flow
= flow
;
257 IRDA_DEBUG(1, __FUNCTION__
"(), IrTTP wants us to start again\n");
258 wake_up_interruptible(sk
->sleep
);
261 IRDA_DEBUG( 0, __FUNCTION__
"(), Unknown flow command!\n");
266 * Function irda_get_value_confirm (obj_id, value, priv)
268 * Got answer from remote LM-IAS
271 static void irda_get_value_confirm(int result
, __u16 obj_id
,
272 struct ias_value
*value
, void *priv
)
274 struct irda_sock
*self
;
276 IRDA_DEBUG(1, __FUNCTION__
"()\n");
278 ASSERT(priv
!= NULL
, return;);
279 self
= (struct irda_sock
*) priv
;
284 /* Check if request succeeded */
285 if (result
!= IAS_SUCCESS
) {
286 IRDA_DEBUG(0, __FUNCTION__
"(), IAS query failed!\n");
288 self
->errno
= result
;
290 /* Wake up any processes waiting for result */
291 wake_up_interruptible(&self
->ias_wait
);
296 switch (value
->type
) {
298 IRDA_DEBUG(4, __FUNCTION__
"() int=%d\n", value
->t
.integer
);
300 if (value
->t
.integer
!= -1) {
301 self
->dtsap_sel
= value
->t
.integer
;
306 IRDA_DEBUG(0, __FUNCTION__
"(), bad type!\n");
309 /* Wake up any processes waiting for result */
310 wake_up_interruptible(&self
->ias_wait
);
314 * Function irda_discovery_indication (log)
316 * Got a discovery log from IrLMP, wake ut any process waiting for answer
319 static void irda_discovery_indication(hashbin_t
*log
)
321 IRDA_DEBUG(1, __FUNCTION__
"()\n");
325 /* Wake up process if its waiting for device to be discovered */
326 wake_up_interruptible(&discovery_wait
);
330 * Function irda_open_tsap (self)
332 * Open local Transport Service Access Point (TSAP)
335 static int irda_open_tsap(struct irda_sock
*self
, __u8 tsap_sel
, char *name
)
339 /* Initialize callbacks to be used by the IrDA stack */
340 irda_notify_init(¬ify
);
341 notify
.connect_confirm
= irda_connect_confirm
;
342 notify
.connect_indication
= irda_connect_indication
;
343 notify
.disconnect_indication
= irda_disconnect_indication
;
344 notify
.data_indication
= irda_data_indication
;
345 notify
.flow_indication
= irda_flow_indication
;
346 notify
.instance
= self
;
347 strncpy(notify
.name
, name
, NOTIFY_MAX_NAME
);
349 self
->tsap
= irttp_open_tsap(tsap_sel
, DEFAULT_INITIAL_CREDIT
,
351 if (self
->tsap
== NULL
) {
352 IRDA_DEBUG( 0, __FUNCTION__
"(), Unable to allocate TSAP!\n");
355 /* Remember which TSAP selector we actually got */
356 self
->stsap_sel
= self
->tsap
->stsap_sel
;
362 * Function irda_find_lsap_sel (self, name)
364 * Try to lookup LSAP selector in remote LM-IAS
367 static int irda_find_lsap_sel(struct irda_sock
*self
, char *name
)
369 IRDA_DEBUG(1, __FUNCTION__
"()\n");
371 ASSERT(self
!= NULL
, return -1;);
373 /* Query remote LM-IAS */
374 iriap_getvaluebyclass_request(name
, "IrDA:TinyTP:LsapSel",
375 self
->saddr
, self
->daddr
,
376 irda_get_value_confirm
, self
);
377 /* Wait for answer */
378 interruptible_sleep_on(&self
->ias_wait
);
383 return -ENETUNREACH
; /* May not be true */
387 * Function irda_getname (sock, uaddr, uaddr_len, peer)
389 * Return the our own, or peers socket address (sockaddr_irda)
392 static int irda_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
393 int *uaddr_len
, int peer
)
395 struct sockaddr_irda saddr
;
396 struct sock
*sk
= sock
->sk
;
399 if (sk
->state
!= TCP_ESTABLISHED
)
402 saddr
.sir_family
= AF_IRDA
;
403 saddr
.sir_lsap_sel
= sk
->protinfo
.irda
->dtsap_sel
;
404 saddr
.sir_addr
= sk
->protinfo
.irda
->daddr
;
406 saddr
.sir_family
= AF_IRDA
;
407 saddr
.sir_lsap_sel
= sk
->protinfo
.irda
->stsap_sel
;
408 saddr
.sir_addr
= sk
->protinfo
.irda
->saddr
;
411 IRDA_DEBUG(1, __FUNCTION__
"(), tsap_sel = %#x\n", saddr
.sir_lsap_sel
);
412 IRDA_DEBUG(1, __FUNCTION__
"(), addr = %08x\n", saddr
.sir_addr
);
414 if (*uaddr_len
> sizeof (struct sockaddr_irda
))
415 *uaddr_len
= sizeof (struct sockaddr_irda
);
416 memcpy(uaddr
, &saddr
, *uaddr_len
);
422 * Function irda_listen (sock, backlog)
424 * Just move to the listen state
427 static int irda_listen( struct socket
*sock
, int backlog
)
429 struct sock
*sk
= sock
->sk
;
431 if (sk
->type
== SOCK_STREAM
&& sk
->state
!= TCP_LISTEN
) {
432 sk
->max_ack_backlog
= backlog
;
433 sk
->state
= TCP_LISTEN
;
442 * Function irda_bind (sock, uaddr, addr_len)
444 * Used by servers to register their well known TSAP
447 static int irda_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
449 struct sock
*sk
= sock
->sk
;
450 struct sockaddr_irda
*addr
= (struct sockaddr_irda
*) uaddr
;
451 struct irda_sock
*self
;
455 self
= sk
->protinfo
.irda
;
456 ASSERT(self
!= NULL
, return -1;);
458 if ((addr_len
< sizeof(struct sockaddr_irda
)) ||
459 (addr_len
> sizeof(struct sockaddr_irda
)))
462 err
= irda_open_tsap(self
, addr
->sir_lsap_sel
, addr
->sir_name
);
466 /* Register with LM-IAS */
467 self
->ias_obj
= irias_new_object(addr
->sir_name
, jiffies
);
468 irias_add_integer_attrib(self
->ias_obj
, "IrDA:TinyTP:LsapSel",
470 irias_insert_object(self
->ias_obj
);
472 /* Fill in some default hint bits values */
473 if (strncmp(addr
->sir_name
, "OBEX", 4) == 0)
474 hints
= irlmp_service_to_hint(S_OBEX
);
477 self
->skey
= irlmp_register_service(hints
);
483 * Function irda_accept (sock, newsock, flags)
485 * Wait for incomming connection
488 static int irda_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
490 struct irda_sock
*self
, *new;
491 struct sock
*sk
= sock
->sk
;
496 IRDA_DEBUG(0, __FUNCTION__
"()\n");
498 self
= sk
->protinfo
.irda
;
499 ASSERT(self
!= NULL
, return -1;);
501 err
= irda_create(newsock
, sk
->protocol
);
505 if (sock
->state
!= SS_UNCONNECTED
)
508 if ((sk
= sock
->sk
) == NULL
)
511 if (sk
->type
!= SOCK_STREAM
)
514 if (sk
->state
!= TCP_LISTEN
)
518 * The read queue this time is holding sockets ready to use
519 * hooked into the SABM we saved
522 if ((skb
= skb_dequeue(&sk
->receive_queue
)) == NULL
) {
523 if (flags
& O_NONBLOCK
)
526 interruptible_sleep_on(sk
->sleep
);
527 if (signal_pending(current
))
530 } while (skb
== NULL
);
533 newsk
->state
= TCP_ESTABLISHED
;
535 new = newsk
->protinfo
.irda
;
536 ASSERT(new != NULL
, return -1;);
538 /* Now attach up the new socket */
539 new->tsap
= irttp_dup(self
->tsap
, new);
541 IRDA_DEBUG(0, __FUNCTION__
"(), dup failed!\n");
545 new->stsap_sel
= new->tsap
->stsap_sel
;
546 new->dtsap_sel
= new->tsap
->dtsap_sel
;
547 new->saddr
= irttp_get_saddr(new->tsap
);
548 new->daddr
= irttp_get_daddr(new->tsap
);
550 new->max_sdu_size_tx
= self
->max_sdu_size_tx
;
551 new->max_sdu_size_rx
= self
->max_sdu_size_rx
;
552 new->max_data_size
= self
->max_data_size
;
553 new->max_header_size
= self
->max_header_size
;
555 memcpy(&new->qos_tx
, &self
->qos_tx
, sizeof(struct qos_info
));
557 /* Clean up the original one to keep it in listen state */
558 self
->tsap
->dtsap_sel
= self
->tsap
->lsap
->dlsap_sel
= LSAP_ANY
;
559 self
->tsap
->lsap
->lsap_state
= LSAP_DISCONNECTED
;
562 skb
->destructor
= NULL
;
566 newsock
->state
= SS_CONNECTED
;
568 irda_connect_response(new);
574 * Function irda_connect (sock, uaddr, addr_len, flags)
576 * Connect to a IrDA device
579 static int irda_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
580 int addr_len
, int flags
)
582 struct sock
*sk
= sock
->sk
;
583 struct sockaddr_irda
*addr
= (struct sockaddr_irda
*) uaddr
;
584 struct irda_sock
*self
;
587 self
= sk
->protinfo
.irda
;
589 if (sk
->state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
590 sock
->state
= SS_CONNECTED
;
591 return 0; /* Connect completed during a ERESTARTSYS event */
594 if (sk
->state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
595 sock
->state
= SS_UNCONNECTED
;
596 return -ECONNREFUSED
;
599 if (sk
->state
== TCP_ESTABLISHED
)
600 return -EISCONN
; /* No reconnect on a seqpacket socket */
602 sk
->state
= TCP_CLOSE
;
603 sock
->state
= SS_UNCONNECTED
;
605 if (addr_len
!= sizeof(struct sockaddr_irda
))
608 /* Check if user supplied the required destination device address */
612 self
->daddr
= addr
->sir_addr
;
613 IRDA_DEBUG(1, __FUNCTION__
"(), daddr = %08x\n", self
->daddr
);
615 /* Query remote LM-IAS */
616 err
= irda_find_lsap_sel(self
, addr
->sir_name
);
618 IRDA_DEBUG(0, __FUNCTION__
"(), connect failed!\n");
622 /* Check if we have opened a local TSAP */
624 irda_open_tsap(self
, LSAP_ANY
, addr
->sir_name
);
626 /* Move to connecting socket, start sending Connect Requests */
627 sock
->state
= SS_CONNECTING
;
628 sk
->state
= TCP_SYN_SENT
;
630 /* Connect to remote device */
631 err
= irttp_connect_request(self
->tsap
, self
->dtsap_sel
,
632 self
->saddr
, self
->daddr
, NULL
,
633 self
->max_sdu_size_rx
, NULL
);
635 IRDA_DEBUG(0, __FUNCTION__
"(), connect failed!\n");
640 if (sk
->state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
643 cli(); /* To avoid races on the sleep */
645 /* A Connect Ack with Choke or timeout or failed routing will go to
647 while (sk
->state
== TCP_SYN_SENT
) {
648 interruptible_sleep_on(sk
->sleep
);
649 if (signal_pending(current
)) {
655 if (sk
->state
!= TCP_ESTABLISHED
) {
657 sock
->state
= SS_UNCONNECTED
;
658 return sock_error(sk
); /* Always set at this point */
661 sock
->state
= SS_CONNECTED
;
669 * Function irda_create (sock, protocol)
674 static int irda_create(struct socket
*sock
, int protocol
)
677 struct irda_sock
*self
;
679 IRDA_DEBUG(2, __FUNCTION__
"()\n");
681 /* Check for valid socket type */
682 switch (sock
->type
) {
683 case SOCK_STREAM
: /* FALLTHROUGH */
687 return -ESOCKTNOSUPPORT
;
690 /* Allocate socket */
691 if ((sk
= sk_alloc(PF_IRDA
, GFP_ATOMIC
, 1)) == NULL
)
694 self
= kmalloc(sizeof(struct irda_sock
), GFP_ATOMIC
);
697 memset(self
, 0, sizeof(struct irda_sock
));
699 init_waitqueue_head(&self
->ias_wait
);
702 sk
->protinfo
.irda
= self
;
703 sock_init_data(sock
, sk
);
705 if (sock
->type
== SOCK_STREAM
)
706 sock
->ops
= &irda_stream_ops
;
708 sock
->ops
= &irda_dgram_ops
;
710 sk
->protocol
= protocol
;
712 /* Register as a client with IrLMP */
713 self
->ckey
= irlmp_register_client(0, NULL
, NULL
);
715 self
->rx_flow
= self
->tx_flow
= FLOW_START
;
716 self
->max_sdu_size_rx
= SAR_DISABLE
; /* Default value */
717 self
->nslots
= DISCOVERY_DEFAULT_SLOTS
;
719 /* Notify that we are using the irda module, so nobody removes it */
720 irda_mod_inc_use_count();
726 * Function irda_destroy_socket (self)
731 void irda_destroy_socket(struct irda_sock
*self
)
733 IRDA_DEBUG(2, __FUNCTION__
"()\n");
735 ASSERT(self
!= NULL
, return;);
737 /* Unregister with IrLMP */
738 irlmp_unregister_client(self
->ckey
);
739 irlmp_unregister_service(self
->skey
);
741 /* Unregister with LM-IAS */
743 irias_delete_object(self
->ias_obj
);
746 irttp_disconnect_request(self
->tsap
, NULL
, P_NORMAL
);
747 irttp_close_tsap(self
->tsap
);
753 /* Notify that we are not using the irda module anymore */
754 irda_mod_dec_use_count();
760 * Function irda_release (sock, peer)
765 static int irda_release(struct socket
*sock
)
767 struct sock
*sk
= sock
->sk
;
769 IRDA_DEBUG(1, __FUNCTION__
"()\n");
774 sk
->state
= TCP_CLOSE
;
775 sk
->shutdown
|= SEND_SHUTDOWN
;
776 sk
->state_change(sk
);
779 irda_destroy_socket(sk
->protinfo
.irda
);
782 sk
->socket
= NULL
; /* Not used, but we should do this. */
788 * Function irda_sendmsg (sock, msg, len, scm)
790 * Send message down to TinyTP
793 static int irda_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
794 struct scm_cookie
*scm
)
796 struct sock
*sk
= sock
->sk
;
797 /* struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name; */
798 struct irda_sock
*self
;
800 unsigned char *asmptr
;
803 IRDA_DEBUG(4, __FUNCTION__
"(), len=%d\n", len
);
805 if (msg
->msg_flags
& ~MSG_DONTWAIT
)
808 if (sk
->shutdown
& SEND_SHUTDOWN
) {
809 send_sig(SIGPIPE
, current
, 0);
813 self
= sk
->protinfo
.irda
;
814 ASSERT(self
!= NULL
, return -1;);
816 /* Check if IrTTP is wants us to slow down */
817 while (self
->tx_flow
== FLOW_STOP
) {
818 IRDA_DEBUG(2, __FUNCTION__
"(), IrTTP is busy, going to sleep!\n");
819 interruptible_sleep_on(sk
->sleep
);
821 /* Check if we are still connected */
822 if (sk
->state
!= TCP_ESTABLISHED
)
826 /* Check that we don't send out to big frames */
827 if (len
> self
->max_data_size
) {
828 IRDA_DEBUG(0, __FUNCTION__
"(), Warning to much data! "
829 "Chopping frame from %d to %d bytes!\n", len
,
830 self
->max_data_size
);
831 len
= self
->max_data_size
;
834 skb
= sock_alloc_send_skb(sk
, len
+ self
->max_header_size
, 0,
835 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
839 skb_reserve(skb
, self
->max_header_size
);
841 IRDA_DEBUG(4, __FUNCTION__
"(), appending user data\n");
842 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
843 memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
846 * Just send the message to TinyTP, and let it deal with possible
847 * errors. No need to duplicate all that here
849 err
= irttp_data_request(self
->tsap
, skb
);
851 IRDA_DEBUG(0, __FUNCTION__
"(), err=%d\n", err
);
858 * Function irda_recvmsg (sock, msg, size, flags, scm)
860 * Try to receive message and copy it to user
863 static int irda_recvmsg_dgram(struct socket
*sock
, struct msghdr
*msg
,
864 int size
, int flags
, struct scm_cookie
*scm
)
866 struct irda_sock
*self
;
867 struct sock
*sk
= sock
->sk
;
871 IRDA_DEBUG(4, __FUNCTION__
"()\n");
873 self
= sk
->protinfo
.irda
;
874 ASSERT(self
!= NULL
, return -1;);
876 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
877 flags
& MSG_DONTWAIT
, &err
);
881 skb
->h
.raw
= skb
->data
;
886 msg
->msg_flags
|= MSG_TRUNC
;
888 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
890 skb_free_datagram(sk
, skb
);
893 * Check if we have previously stopped IrTTP and we know
894 * have more free space in our rx_queue. If so tell IrTTP
895 * to start delivering frames again before our rx_queue gets
898 if (self
->rx_flow
== FLOW_STOP
) {
899 if ((atomic_read(&sk
->rmem_alloc
) << 2) <= sk
->rcvbuf
) {
900 IRDA_DEBUG(2, __FUNCTION__
"(), Starting IrTTP\n");
901 self
->rx_flow
= FLOW_START
;
902 irttp_flow_request(self
->tsap
, FLOW_START
);
910 * Function irda_data_wait (sk)
912 * Sleep until data has arrive. But check for races..
915 static void irda_data_wait(struct sock
*sk
)
917 if (!skb_peek(&sk
->receive_queue
)) {
918 sk
->socket
->flags
|= SO_WAITDATA
;
919 interruptible_sleep_on(sk
->sleep
);
920 sk
->socket
->flags
&= ~SO_WAITDATA
;
925 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
930 static int irda_recvmsg_stream(struct socket
*sock
, struct msghdr
*msg
,
931 int size
, int flags
, struct scm_cookie
*scm
)
933 struct irda_sock
*self
;
934 struct sock
*sk
= sock
->sk
;
935 int noblock
= flags
& MSG_DONTWAIT
;
939 IRDA_DEBUG(3, __FUNCTION__
"()\n");
941 self
= sk
->protinfo
.irda
;
942 ASSERT(self
!= NULL
, return -1;);
944 if (sock
->flags
& SO_ACCEPTCON
)
950 if (flags
& MSG_WAITALL
)
954 msg
->msg_namelen
= 0;
956 /* Lock the socket to prevent queue disordering
957 * while sleeps in memcpy_tomsg
959 /* down(&self->readsem); */
965 skb
=skb_dequeue(&sk
->receive_queue
);
967 if (copied
>= target
)
971 * POSIX 1003.1g mandates this order.
975 /* up(&self->readsem); */
976 return sock_error(sk
);
979 if (sk
->shutdown
& RCV_SHUTDOWN
)
982 /* up(&self->readsem); */
987 if (signal_pending(current
))
989 /* down(&self->readsem); */
993 /* Never glue messages from different writers */
994 /* if (check_creds && */
995 /* memcmp(UNIXCREDS(skb), &scm->creds, sizeof(scm->creds)) != 0) */
997 /* skb_queue_head(&sk->receive_queue, skb); */
1001 chunk
= min(skb
->len
, size
);
1002 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
1003 skb_queue_head(&sk
->receive_queue
, skb
);
1011 /* Copy credentials */
1012 /* scm->creds = *UNIXCREDS(skb); */
1013 /* check_creds = 1; */
1015 /* Mark read part of skb as used */
1016 if (!(flags
& MSG_PEEK
)) {
1017 skb_pull(skb
, chunk
);
1019 /* if (UNIXCB(skb).fp) */
1020 /* unix_detach_fds(scm, skb); */
1022 /* put the skb back if we didn't use it up.. */
1024 IRDA_DEBUG(1, __FUNCTION__
"(), back on q!\n");
1025 skb_queue_head(&sk
->receive_queue
, skb
);
1034 IRDA_DEBUG(0, __FUNCTION__
"() questionable!?\n");
1035 /* It is questionable, see note in unix_dgram_recvmsg. */
1036 /* if (UNIXCB(skb).fp) */
1037 /* scm->fp = scm_fp_dup(UNIXCB(skb).fp); */
1039 /* put message back and return */
1040 skb_queue_head(&sk
->receive_queue
, skb
);
1046 * Check if we have previously stopped IrTTP and we know
1047 * have more free space in our rx_queue. If so tell IrTTP
1048 * to start delivering frames again before our rx_queue gets
1051 if (self
->rx_flow
== FLOW_STOP
) {
1052 if ((atomic_read(&sk
->rmem_alloc
) << 2) <= sk
->rcvbuf
) {
1053 IRDA_DEBUG(2, __FUNCTION__
"(), Starting IrTTP\n");
1054 self
->rx_flow
= FLOW_START
;
1055 irttp_flow_request(self
->tsap
, FLOW_START
);
1059 /* up(&self->readsem); */
1065 * Function irda_shutdown (sk, how)
1070 static int irda_shutdown( struct socket
*sk
, int how
)
1072 IRDA_DEBUG( 0, __FUNCTION__
"()\n");
1074 /* FIXME - generate DM and RNR states */
1079 * Function irda_poll (file, sock, wait)
1084 static unsigned int irda_poll(struct file
* file
, struct socket
*sock
,
1087 struct sock
*sk
= sock
->sk
;
1090 IRDA_DEBUG(1, __FUNCTION__
"()\n");
1092 poll_wait(file
, sk
->sleep
, wait
);
1095 /* exceptional events? */
1098 if (sk
->shutdown
& RCV_SHUTDOWN
)
1102 if (!skb_queue_empty(&sk
->receive_queue
))
1103 mask
|= POLLIN
| POLLRDNORM
;
1105 /* Connection-based need to check for termination and startup */
1106 if (sk
->type
== SOCK_STREAM
&& sk
->state
==TCP_CLOSE
)
1110 * we set writable also when the other side has shut down the
1111 * connection. This prevents stuck sockets.
1113 if (sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
)
1114 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1120 * Function irda_ioctl (sock, cmd, arg)
1125 static int irda_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1127 struct sock
*sk
= sock
->sk
;
1129 IRDA_DEBUG(4, __FUNCTION__
"(), cmd=%#x\n", cmd
);
1134 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
1137 if (put_user(amount
, (unsigned int *)arg
))
1143 struct sk_buff
*skb
;
1145 /* These two are safe on a single CPU system as only user tasks fiddle here */
1146 if ((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
1148 if (put_user(amount
, (unsigned int *)arg
))
1155 if (sk
->stamp
.tv_sec
== 0)
1157 if (copy_to_user((void *)arg
, &sk
->stamp
,
1158 sizeof(struct timeval
)))
1166 case SIOCGIFDSTADDR
:
1167 case SIOCSIFDSTADDR
:
1168 case SIOCGIFBRDADDR
:
1169 case SIOCSIFBRDADDR
:
1170 case SIOCGIFNETMASK
:
1171 case SIOCSIFNETMASK
:
1176 IRDA_DEBUG(1, __FUNCTION__
"(), doing device ioctl!\n");
1177 return dev_ioctl(cmd
, (void *) arg
);
1185 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1187 * Set some options for the socket
1190 static int irda_setsockopt(struct socket
*sock
, int level
, int optname
,
1191 char *optval
, int optlen
)
1193 struct sock
*sk
= sock
->sk
;
1194 struct irda_sock
*self
;
1197 self
= sk
->protinfo
.irda
;
1198 ASSERT(self
!= NULL
, return -1;);
1200 if (level
!= SOL_IRLMP
)
1201 return -ENOPROTOOPT
;
1203 if (optlen
< sizeof(int))
1206 if (get_user(opt
, (int *)optval
))
1211 IRDA_DEBUG(0, __FUNCTION__
"(), sorry not impl. yet!\n");
1213 case IRTTP_MAX_SDU_SIZE
:
1214 IRDA_DEBUG(2, __FUNCTION__
"(), setting max_sdu_size = %d\n", opt
);
1215 self
->max_sdu_size_rx
= opt
;
1218 return -ENOPROTOOPT
;
1224 * Function irda_getsockopt (sock, level, optname, optval, optlen)
1229 static int irda_getsockopt(struct socket
*sock
, int level
, int optname
,
1230 char *optval
, int *optlen
)
1232 struct sock
*sk
= sock
->sk
;
1233 struct irda_sock
*self
;
1234 struct irda_device_list list
;
1235 struct irda_device_info
*info
;
1236 discovery_t
*discovery
;
1241 self
= sk
->protinfo
.irda
;
1243 if (level
!= SOL_IRLMP
)
1244 return -ENOPROTOOPT
;
1246 if (get_user(len
, optlen
))
1250 case IRLMP_ENUMDEVICES
:
1251 /* Tell IrLMP we want to be notified */
1252 irlmp_update_client(self
->ckey
, self
->mask
, NULL
,
1253 irda_discovery_indication
);
1255 /* Do some discovery */
1256 irlmp_discovery_request(self
->nslots
);
1258 /* Check if the we got some results */
1262 info
= &list
.dev
[0];
1264 /* Offset to first device entry */
1265 offset
= sizeof(struct irda_device_list
) -
1266 sizeof(struct irda_device_info
);
1268 total
= offset
; /* Initialized to size of the device list */
1269 list
.len
= 0; /* Initialize lenght of list */
1272 * Now, check all discovered devices (if any), and notify
1273 * client only about the services that the client is
1276 discovery
= (discovery_t
*) hashbin_get_first(cachelog
);
1277 while (discovery
!= NULL
) {
1278 /* Mask out the ones we don't want */
1279 if (discovery
->hints
.word
& self
->mask
) {
1281 /* Check if room for this device entry */
1282 if (len
- total
< sizeof(struct irda_device_info
))
1285 /* Copy discovery information */
1286 info
->saddr
= discovery
->saddr
;
1287 info
->daddr
= discovery
->daddr
;
1288 info
->charset
= discovery
->charset
;
1289 info
->hints
[0] = discovery
->hints
.byte
[0];
1290 info
->hints
[1] = discovery
->hints
.byte
[1];
1291 strncpy(info
->info
, discovery
->nickname
,
1294 if (copy_to_user(optval
+offset
, info
,
1295 sizeof(struct irda_device_info
)))
1298 total
+= sizeof(struct irda_device_info
);
1300 discovery
= (discovery_t
*) hashbin_get_next(cachelog
);
1304 /* Write total number of bytes used back to client */
1305 if (put_user(total
, optlen
))
1308 /* Write total list length back to client */
1309 if (copy_to_user(optval
, &list
,
1310 sizeof(struct irda_device_list
) -
1311 sizeof(struct irda_device_info
)))
1314 case IRTTP_MAX_SDU_SIZE
:
1315 val
= self
->max_data_size
;
1316 IRDA_DEBUG(2, __FUNCTION__
"(), getting max_sdu_size = %d\n", val
);
1318 if (put_user(len
, optlen
))
1321 if (copy_to_user(optval
, &val
, len
))
1325 return -ENOPROTOOPT
;
1331 static struct net_proto_family irda_family_ops
=
1337 static struct proto_ops
SOCKOPS_WRAPPED(irda_stream_ops
) = {
1354 irda_recvmsg_stream
,
1358 static struct proto_ops
SOCKOPS_WRAPPED(irda_dgram_ops
) = {
1379 #include <linux/smp_lock.h>
1380 SOCKOPS_WRAP(irda_dgram
, PF_IRDA
);
1381 SOCKOPS_WRAP(irda_stream
, PF_IRDA
);
1384 * Function irda_device_event (this, event, ptr)
1386 * Called when a device is taken up or down
1389 static int irda_device_event(struct notifier_block
*this, unsigned long event
,
1392 struct net_device
*dev
= (struct net_device
*) ptr
;
1394 /* Reject non IrDA devices */
1395 if (dev
->type
!= ARPHRD_IRDA
)
1400 IRDA_DEBUG(3, __FUNCTION__
"(), NETDEV_UP\n");
1401 /* irda_dev_device_up(dev); */
1404 IRDA_DEBUG(3, __FUNCTION__
"(), NETDEV_DOWN\n");
1405 /* irda_kill_by_device(dev); */
1406 /* irda_rt_device_down(dev); */
1407 /* irda_dev_device_down(dev); */
1416 static struct packet_type irda_packet_type
=
1418 0, /* MUTTER ntohs(ETH_P_IRDA),*/
1425 static struct notifier_block irda_dev_notifier
= {
1432 * Function irda_proto_init (pro)
1434 * Initialize IrDA protocol layer
1437 void __init
irda_proto_init(struct net_proto
*pro
)
1439 sock_register(&irda_family_ops
);
1441 irda_packet_type
.type
= htons(ETH_P_IRDA
);
1442 dev_add_pack(&irda_packet_type
);
1444 register_netdevice_notifier(&irda_dev_notifier
);
1450 * Function irda_proto_cleanup (void)
1452 * Remove IrDA protocol layer
1456 void irda_proto_cleanup(void)
1458 irda_packet_type
.type
= htons(ETH_P_IRDA
);
1459 dev_remove_pack(&irda_packet_type
);
1461 unregister_netdevice_notifier(&irda_dev_notifier
);
1463 sock_unregister(PF_IRDA
);