OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [irda/] [af_irda.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*********************************************************************
/*********************************************************************
 *
 *
 * Filename:      af_irda.c
 * Filename:      af_irda.c
 * Version:       0.9
 * Version:       0.9
 * Description:   IrDA sockets implementation
 * Description:   IrDA sockets implementation
 * Status:        Stable
 * Status:        Stable
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Sun May 31 10:12:43 1998
 * Created at:    Sun May 31 10:12:43 1998
 * Modified at:   Sat Dec 25 21:10:23 1999
 * Modified at:   Sat Dec 25 21:10:23 1999
 * Modified by:   Dag Brattli <dag@brattli.net>
 * Modified by:   Dag Brattli <dag@brattli.net>
 * Sources:       af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
 * Sources:       af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
 *
 *
 *     Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
 *     Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
 *     Copyright (c) 1999-2001 Jean Tourrilhes <jt@hpl.hp.com>
 *     Copyright (c) 1999-2001 Jean Tourrilhes <jt@hpl.hp.com>
 *     All Rights Reserved.
 *     All Rights Reserved.
 *
 *
 *     This program is free software; you can redistribute it and/or
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *     the License, or (at your option) any later version.
 *
 *
 *     This program is distributed in the hope that it will be useful,
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU General Public License for more details.
 *     GNU General Public License for more details.
 *
 *
 *     You should have received a copy of the GNU General Public License
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *     MA 02111-1307 USA
 *     MA 02111-1307 USA
 *
 *
 *     Linux-IrDA now supports four different types of IrDA sockets:
 *     Linux-IrDA now supports four different types of IrDA sockets:
 *
 *
 *     o SOCK_STREAM:    TinyTP connections with SAR disabled. The
 *     o SOCK_STREAM:    TinyTP connections with SAR disabled. The
 *                       max SDU size is 0 for conn. of this type
 *                       max SDU size is 0 for conn. of this type
 *     o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may
 *     o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may
 *                       fragment the messages, but will preserve
 *                       fragment the messages, but will preserve
 *                       the message boundaries
 *                       the message boundaries
 *     o SOCK_DGRAM:     IRDAPROTO_UNITDATA: TinyTP connections with Unitdata
 *     o SOCK_DGRAM:     IRDAPROTO_UNITDATA: TinyTP connections with Unitdata
 *                       (unreliable) transfers
 *                       (unreliable) transfers
 *                       IRDAPROTO_ULTRA: Connectionless and unreliable data
 *                       IRDAPROTO_ULTRA: Connectionless and unreliable data
 *
 *
 ********************************************************************/
 ********************************************************************/
 
 
#include <linux/config.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/sockios.h>
#include <linux/init.h>
#include <linux/init.h>
#include <linux/if_arp.h>
#include <linux/if_arp.h>
#include <linux/net.h>
#include <linux/net.h>
#include <linux/irda.h>
#include <linux/irda.h>
#include <linux/poll.h>
#include <linux/poll.h>
 
 
#include <asm/uaccess.h>
#include <asm/uaccess.h>
 
 
#include <net/sock.h>
#include <net/sock.h>
 
 
#include <net/irda/irda.h>
#include <net/irda/irda.h>
#include <net/irda/iriap.h>
#include <net/irda/iriap.h>
#include <net/irda/irias_object.h>
#include <net/irda/irias_object.h>
#include <net/irda/irlmp.h>
#include <net/irda/irlmp.h>
#include <net/irda/irttp.h>
#include <net/irda/irttp.h>
#include <net/irda/discovery.h>
#include <net/irda/discovery.h>
 
 
extern int  irda_init(void);
extern int  irda_init(void);
extern void irda_cleanup(void);
extern void irda_cleanup(void);
extern int  irlap_driver_rcv(struct sk_buff *, struct net_device *,
extern int  irlap_driver_rcv(struct sk_buff *, struct net_device *,
                             struct packet_type *);
                             struct packet_type *);
 
 
static int irda_create(struct socket *sock, int protocol);
static int irda_create(struct socket *sock, int protocol);
 
 
static struct proto_ops irda_stream_ops;
static struct proto_ops irda_stream_ops;
static struct proto_ops irda_seqpacket_ops;
static struct proto_ops irda_seqpacket_ops;
static struct proto_ops irda_dgram_ops;
static struct proto_ops irda_dgram_ops;
 
 
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
static struct proto_ops irda_ultra_ops;
static struct proto_ops irda_ultra_ops;
#define ULTRA_MAX_DATA 382
#define ULTRA_MAX_DATA 382
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
 
 
#define IRDA_MAX_HEADER (TTP_MAX_HEADER)
#define IRDA_MAX_HEADER (TTP_MAX_HEADER)
 
 
#ifdef CONFIG_IRDA_DEBUG
#ifdef CONFIG_IRDA_DEBUG
__u32 irda_debug = IRDA_DEBUG_LEVEL;
__u32 irda_debug = IRDA_DEBUG_LEVEL;
#endif
#endif
 
 
/*
/*
 * Function irda_data_indication (instance, sap, skb)
 * Function irda_data_indication (instance, sap, skb)
 *
 *
 *    Received some data from TinyTP. Just queue it on the receive queue
 *    Received some data from TinyTP. Just queue it on the receive queue
 *
 *
 */
 */
static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk;
        struct sock *sk;
        int err;
        int err;
 
 
        IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
 
 
        self = (struct irda_sock *) instance;
        self = (struct irda_sock *) instance;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        sk = self->sk;
        sk = self->sk;
        ASSERT(sk != NULL, return -1;);
        ASSERT(sk != NULL, return -1;);
 
 
        err = sock_queue_rcv_skb(sk, skb);
        err = sock_queue_rcv_skb(sk, skb);
        if (err) {
        if (err) {
                IRDA_DEBUG(1, "%s(), error: no more mem!\n", __FUNCTION__);
                IRDA_DEBUG(1, "%s(), error: no more mem!\n", __FUNCTION__);
                self->rx_flow = FLOW_STOP;
                self->rx_flow = FLOW_STOP;
 
 
                /* When we return error, TTP will need to requeue the skb */
                /* When we return error, TTP will need to requeue the skb */
                return err;
                return err;
        }
        }
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_disconnect_indication (instance, sap, reason, skb)
 * Function irda_disconnect_indication (instance, sap, reason, skb)
 *
 *
 *    Connection has been closed. Check reason to find out why
 *    Connection has been closed. Check reason to find out why
 *
 *
 */
 */
static void irda_disconnect_indication(void *instance, void *sap,
static void irda_disconnect_indication(void *instance, void *sap,
                                       LM_REASON reason, struct sk_buff *skb)
                                       LM_REASON reason, struct sk_buff *skb)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk;
        struct sock *sk;
 
 
        self = (struct irda_sock *) instance;
        self = (struct irda_sock *) instance;
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        /* Don't care about it, but let's not leak it */
        /* Don't care about it, but let's not leak it */
        if(skb)
        if(skb)
                dev_kfree_skb(skb);
                dev_kfree_skb(skb);
 
 
        sk = self->sk;
        sk = self->sk;
        if (sk == NULL)
        if (sk == NULL)
                return;
                return;
 
 
        /* Prevent race conditions with irda_release() and irda_shutdown() */
        /* Prevent race conditions with irda_release() and irda_shutdown() */
        if ((!sk->dead) && (sk->state != TCP_CLOSE)) {
        if ((!sk->dead) && (sk->state != TCP_CLOSE)) {
                sk->state     = TCP_CLOSE;
                sk->state     = TCP_CLOSE;
                sk->err       = ECONNRESET;
                sk->err       = ECONNRESET;
                sk->shutdown |= SEND_SHUTDOWN;
                sk->shutdown |= SEND_SHUTDOWN;
 
 
                sk->state_change(sk);
                sk->state_change(sk);
                sk->dead = 1;   /* Uh-oh... Should use sock_orphan ? */
                sk->dead = 1;   /* Uh-oh... Should use sock_orphan ? */
 
 
                /* Close our TSAP.
                /* Close our TSAP.
                 * If we leave it open, IrLMP put it back into the list of
                 * If we leave it open, IrLMP put it back into the list of
                 * unconnected LSAPs. The problem is that any incoming request
                 * unconnected LSAPs. The problem is that any incoming request
                 * can then be matched to this socket (and it will be, because
                 * can then be matched to this socket (and it will be, because
                 * it is at the head of the list). This would prevent any
                 * it is at the head of the list). This would prevent any
                 * listening socket waiting on the same TSAP to get those
                 * listening socket waiting on the same TSAP to get those
                 * requests. Some apps forget to close sockets, or hang to it
                 * requests. Some apps forget to close sockets, or hang to it
                 * a bit too long, so we may stay in this dead state long
                 * a bit too long, so we may stay in this dead state long
                 * enough to be noticed...
                 * enough to be noticed...
                 * Note : all socket function do check sk->state, so we are
                 * Note : all socket function do check sk->state, so we are
                 * safe...
                 * safe...
                 * Jean II
                 * Jean II
                 */
                 */
                if (self->tsap) {
                if (self->tsap) {
                        irttp_close_tsap(self->tsap);
                        irttp_close_tsap(self->tsap);
                        self->tsap = NULL;
                        self->tsap = NULL;
                }
                }
        }
        }
 
 
        /* Note : once we are there, there is not much you want to do
        /* Note : once we are there, there is not much you want to do
         * with the socket anymore, apart from closing it.
         * with the socket anymore, apart from closing it.
         * For example, bind() and connect() won't reset sk->err,
         * For example, bind() and connect() won't reset sk->err,
         * sk->shutdown and sk->dead to valid values...
         * sk->shutdown and sk->dead to valid values...
         * Jean II
         * Jean II
         */
         */
}
}
 
 
/*
/*
 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
 *
 *
 *    Connections has been confirmed by the remote device
 *    Connections has been confirmed by the remote device
 *
 *
 */
 */
static void irda_connect_confirm(void *instance, void *sap,
static void irda_connect_confirm(void *instance, void *sap,
                                 struct qos_info *qos,
                                 struct qos_info *qos,
                                 __u32 max_sdu_size, __u8 max_header_size,
                                 __u32 max_sdu_size, __u8 max_header_size,
                                 struct sk_buff *skb)
                                 struct sk_buff *skb)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk;
        struct sock *sk;
 
 
        self = (struct irda_sock *) instance;
        self = (struct irda_sock *) instance;
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        sk = self->sk;
        sk = self->sk;
        if (sk == NULL)
        if (sk == NULL)
                return;
                return;
 
 
        /* How much header space do we need to reserve */
        /* How much header space do we need to reserve */
        self->max_header_size = max_header_size;
        self->max_header_size = max_header_size;
 
 
        /* IrTTP max SDU size in transmit direction */
        /* IrTTP max SDU size in transmit direction */
        self->max_sdu_size_tx = max_sdu_size;
        self->max_sdu_size_tx = max_sdu_size;
 
 
        /* Find out what the largest chunk of data that we can transmit is */
        /* Find out what the largest chunk of data that we can transmit is */
        switch (sk->type) {
        switch (sk->type) {
        case SOCK_STREAM:
        case SOCK_STREAM:
                if (max_sdu_size != 0) {
                if (max_sdu_size != 0) {
                        ERROR("%s(), max_sdu_size must be 0\n", __FUNCTION__);
                        ERROR("%s(), max_sdu_size must be 0\n", __FUNCTION__);
                        return;
                        return;
                }
                }
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
                break;
                break;
        case SOCK_SEQPACKET:
        case SOCK_SEQPACKET:
                if (max_sdu_size == 0) {
                if (max_sdu_size == 0) {
                        ERROR("%s(), max_sdu_size cannot be 0\n", __FUNCTION__);
                        ERROR("%s(), max_sdu_size cannot be 0\n", __FUNCTION__);
                        return;
                        return;
                }
                }
                self->max_data_size = max_sdu_size;
                self->max_data_size = max_sdu_size;
                break;
                break;
        default:
        default:
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
        };
        };
 
 
        IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
        IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
                   self->max_data_size);
                   self->max_data_size);
 
 
        memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
        memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
        dev_kfree_skb(skb);
        dev_kfree_skb(skb);
        // Should be ??? skb_queue_tail(&sk->receive_queue, skb);
        // Should be ??? skb_queue_tail(&sk->receive_queue, skb);
 
 
        /* We are now connected! */
        /* We are now connected! */
        sk->state = TCP_ESTABLISHED;
        sk->state = TCP_ESTABLISHED;
        sk->state_change(sk);
        sk->state_change(sk);
}
}
 
 
/*
/*
 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
 *
 *
 *    Incoming connection
 *    Incoming connection
 *
 *
 */
 */
static void irda_connect_indication(void *instance, void *sap,
static void irda_connect_indication(void *instance, void *sap,
                                    struct qos_info *qos, __u32 max_sdu_size,
                                    struct qos_info *qos, __u32 max_sdu_size,
                                    __u8 max_header_size, struct sk_buff *skb)
                                    __u8 max_header_size, struct sk_buff *skb)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk;
        struct sock *sk;
 
 
        self = (struct irda_sock *) instance;
        self = (struct irda_sock *) instance;
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        sk = self->sk;
        sk = self->sk;
        if (sk == NULL)
        if (sk == NULL)
                return;
                return;
 
 
        /* How much header space do we need to reserve */
        /* How much header space do we need to reserve */
        self->max_header_size = max_header_size;
        self->max_header_size = max_header_size;
 
 
        /* IrTTP max SDU size in transmit direction */
        /* IrTTP max SDU size in transmit direction */
        self->max_sdu_size_tx = max_sdu_size;
        self->max_sdu_size_tx = max_sdu_size;
 
 
        /* Find out what the largest chunk of data that we can transmit is */
        /* Find out what the largest chunk of data that we can transmit is */
        switch (sk->type) {
        switch (sk->type) {
        case SOCK_STREAM:
        case SOCK_STREAM:
                if (max_sdu_size != 0) {
                if (max_sdu_size != 0) {
                        ERROR("%s(), max_sdu_size must be 0\n", __FUNCTION__);
                        ERROR("%s(), max_sdu_size must be 0\n", __FUNCTION__);
                        return;
                        return;
                }
                }
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
                break;
                break;
        case SOCK_SEQPACKET:
        case SOCK_SEQPACKET:
                if (max_sdu_size == 0) {
                if (max_sdu_size == 0) {
                        ERROR("%s(), max_sdu_size cannot be 0\n", __FUNCTION__);
                        ERROR("%s(), max_sdu_size cannot be 0\n", __FUNCTION__);
                        return;
                        return;
                }
                }
                self->max_data_size = max_sdu_size;
                self->max_data_size = max_sdu_size;
                break;
                break;
        default:
        default:
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
                self->max_data_size = irttp_get_max_seg_size(self->tsap);
        };
        };
 
 
        IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
        IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
                   self->max_data_size);
                   self->max_data_size);
 
 
        memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
        memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
 
 
        skb_queue_tail(&sk->receive_queue, skb);
        skb_queue_tail(&sk->receive_queue, skb);
        sk->state_change(sk);
        sk->state_change(sk);
}
}
 
 
/*
/*
 * Function irda_connect_response (handle)
 * Function irda_connect_response (handle)
 *
 *
 *    Accept incoming connection
 *    Accept incoming connection
 *
 *
 */
 */
void irda_connect_response(struct irda_sock *self)
void irda_connect_response(struct irda_sock *self)
{
{
        struct sk_buff *skb;
        struct sk_buff *skb;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        ASSERT(self != NULL, return;);
        ASSERT(self != NULL, return;);
 
 
        skb = dev_alloc_skb(64);
        skb = dev_alloc_skb(64);
        if (skb == NULL) {
        if (skb == NULL) {
                IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n", __FUNCTION__);
                IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n", __FUNCTION__);
                return;
                return;
        }
        }
 
 
        /* Reserve space for MUX_CONTROL and LAP header */
        /* Reserve space for MUX_CONTROL and LAP header */
        skb_reserve(skb, IRDA_MAX_HEADER);
        skb_reserve(skb, IRDA_MAX_HEADER);
 
 
        irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
        irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
}
}
 
 
/*
/*
 * Function irda_flow_indication (instance, sap, flow)
 * Function irda_flow_indication (instance, sap, flow)
 *
 *
 *    Used by TinyTP to tell us if it can accept more data or not
 *    Used by TinyTP to tell us if it can accept more data or not
 *
 *
 */
 */
static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk;
        struct sock *sk;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        self = (struct irda_sock *) instance;
        self = (struct irda_sock *) instance;
        ASSERT(self != NULL, return;);
        ASSERT(self != NULL, return;);
 
 
        sk = self->sk;
        sk = self->sk;
        ASSERT(sk != NULL, return;);
        ASSERT(sk != NULL, return;);
 
 
        switch (flow) {
        switch (flow) {
        case FLOW_STOP:
        case FLOW_STOP:
                IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n", __FUNCTION__);
                IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n", __FUNCTION__);
                self->tx_flow = flow;
                self->tx_flow = flow;
                break;
                break;
        case FLOW_START:
        case FLOW_START:
                self->tx_flow = flow;
                self->tx_flow = flow;
                IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n", __FUNCTION__);
                IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n", __FUNCTION__);
                wake_up_interruptible(sk->sleep);
                wake_up_interruptible(sk->sleep);
                break;
                break;
        default:
        default:
                IRDA_DEBUG( 0, "%s(), Unknown flow command!\n", __FUNCTION__);
                IRDA_DEBUG( 0, "%s(), Unknown flow command!\n", __FUNCTION__);
                /* Unknown flow command, better stop */
                /* Unknown flow command, better stop */
                self->tx_flow = flow;
                self->tx_flow = flow;
                break;
                break;
        }
        }
}
}
 
 
/*
/*
 * Function irda_getvalue_confirm (obj_id, value, priv)
 * Function irda_getvalue_confirm (obj_id, value, priv)
 *
 *
 *    Got answer from remote LM-IAS, just pass object to requester...
 *    Got answer from remote LM-IAS, just pass object to requester...
 *
 *
 * Note : duplicate from above, but we need our own version that
 * Note : duplicate from above, but we need our own version that
 * doesn't touch the dtsap_sel and save the full value structure...
 * doesn't touch the dtsap_sel and save the full value structure...
 */
 */
static void irda_getvalue_confirm(int result, __u16 obj_id,
static void irda_getvalue_confirm(int result, __u16 obj_id,
                                          struct ias_value *value, void *priv)
                                          struct ias_value *value, void *priv)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
 
 
        self = (struct irda_sock *) priv;
        self = (struct irda_sock *) priv;
        if (!self) {
        if (!self) {
                WARNING("%s(), lost myself!\n", __FUNCTION__);
                WARNING("%s(), lost myself!\n", __FUNCTION__);
                return;
                return;
        }
        }
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        /* We probably don't need to make any more queries */
        /* We probably don't need to make any more queries */
        iriap_close(self->iriap);
        iriap_close(self->iriap);
        self->iriap = NULL;
        self->iriap = NULL;
 
 
        /* Check if request succeeded */
        /* Check if request succeeded */
        if (result != IAS_SUCCESS) {
        if (result != IAS_SUCCESS) {
                IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __FUNCTION__,
                IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __FUNCTION__,
                           result);
                           result);
 
 
                self->errno = result;   /* We really need it later */
                self->errno = result;   /* We really need it later */
 
 
                /* Wake up any processes waiting for result */
                /* Wake up any processes waiting for result */
                wake_up_interruptible(&self->query_wait);
                wake_up_interruptible(&self->query_wait);
 
 
                return;
                return;
        }
        }
 
 
        /* Pass the object to the caller (so the caller must delete it) */
        /* Pass the object to the caller (so the caller must delete it) */
        self->ias_result = value;
        self->ias_result = value;
        self->errno = 0;
        self->errno = 0;
 
 
        /* Wake up any processes waiting for result */
        /* Wake up any processes waiting for result */
        wake_up_interruptible(&self->query_wait);
        wake_up_interruptible(&self->query_wait);
}
}
 
 
/*
/*
 * Function irda_selective_discovery_indication (discovery)
 * Function irda_selective_discovery_indication (discovery)
 *
 *
 *    Got a selective discovery indication from IrLMP.
 *    Got a selective discovery indication from IrLMP.
 *
 *
 * IrLMP is telling us that this node is matching our hint bit
 * IrLMP is telling us that this node is matching our hint bit
 * filter. Check if it's a newly discovered node (or if node changed its
 * filter. Check if it's a newly discovered node (or if node changed its
 * hint bits), and then wake up any process waiting for answer...
 * hint bits), and then wake up any process waiting for answer...
 */
 */
static void irda_selective_discovery_indication(discovery_t *discovery,
static void irda_selective_discovery_indication(discovery_t *discovery,
                                                DISCOVERY_MODE mode,
                                                DISCOVERY_MODE mode,
                                                void *priv)
                                                void *priv)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        self = (struct irda_sock *) priv;
        self = (struct irda_sock *) priv;
        if (!self) {
        if (!self) {
                WARNING("%s(), lost myself!\n", __FUNCTION__);
                WARNING("%s(), lost myself!\n", __FUNCTION__);
                return;
                return;
        }
        }
 
 
        /* Check if node is discovered is a new one or an old one.
        /* Check if node is discovered is a new one or an old one.
         * We check when how long ago this node was discovered, with a
         * We check when how long ago this node was discovered, with a
         * coarse timeout (we may miss some discovery events or be delayed).
         * coarse timeout (we may miss some discovery events or be delayed).
         * Note : by doing this test here, we avoid waking up a process ;-)
         * Note : by doing this test here, we avoid waking up a process ;-)
         */
         */
        if((jiffies - discovery->first_timestamp) >
        if((jiffies - discovery->first_timestamp) >
           (sysctl_discovery_timeout * HZ)) {
           (sysctl_discovery_timeout * HZ)) {
                return;         /* Too old, not interesting -> goodbye */
                return;         /* Too old, not interesting -> goodbye */
        }
        }
 
 
        /* Pass parameter to the caller */
        /* Pass parameter to the caller */
        self->cachediscovery = discovery;
        self->cachediscovery = discovery;
 
 
        /* Wake up process if its waiting for device to be discovered */
        /* Wake up process if its waiting for device to be discovered */
        wake_up_interruptible(&self->query_wait);
        wake_up_interruptible(&self->query_wait);
}
}
 
 
/*
/*
 * Function irda_discovery_timeout (priv)
 * Function irda_discovery_timeout (priv)
 *
 *
 *    Timeout in the selective discovery process
 *    Timeout in the selective discovery process
 *
 *
 * We were waiting for a node to be discovered, but nothing has come up
 * We were waiting for a node to be discovered, but nothing has come up
 * so far. Wake up the user and tell him that we failed...
 * so far. Wake up the user and tell him that we failed...
 */
 */
static void irda_discovery_timeout(u_long priv)
static void irda_discovery_timeout(u_long priv)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        self = (struct irda_sock *) priv;
        self = (struct irda_sock *) priv;
        ASSERT(self != NULL, return;);
        ASSERT(self != NULL, return;);
 
 
        /* Nothing for the caller */
        /* Nothing for the caller */
        self->cachelog = NULL;
        self->cachelog = NULL;
        self->cachediscovery = NULL;
        self->cachediscovery = NULL;
        self->errno = -ETIME;
        self->errno = -ETIME;
 
 
        /* Wake up process if its still waiting... */
        /* Wake up process if its still waiting... */
        wake_up_interruptible(&self->query_wait);
        wake_up_interruptible(&self->query_wait);
}
}
 
 
/*
/*
 * Function irda_open_tsap (self)
 * Function irda_open_tsap (self)
 *
 *
 *    Open local Transport Service Access Point (TSAP)
 *    Open local Transport Service Access Point (TSAP)
 *
 *
 */
 */
static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
{
{
        notify_t notify;
        notify_t notify;
 
 
        if (self->tsap) {
        if (self->tsap) {
                WARNING("%s(), busy!\n", __FUNCTION__);
                WARNING("%s(), busy!\n", __FUNCTION__);
                return -EBUSY;
                return -EBUSY;
        }
        }
 
 
        /* Initialize callbacks to be used by the IrDA stack */
        /* Initialize callbacks to be used by the IrDA stack */
        irda_notify_init(&notify);
        irda_notify_init(&notify);
        notify.connect_confirm       = irda_connect_confirm;
        notify.connect_confirm       = irda_connect_confirm;
        notify.connect_indication    = irda_connect_indication;
        notify.connect_indication    = irda_connect_indication;
        notify.disconnect_indication = irda_disconnect_indication;
        notify.disconnect_indication = irda_disconnect_indication;
        notify.data_indication       = irda_data_indication;
        notify.data_indication       = irda_data_indication;
        notify.udata_indication      = irda_data_indication;
        notify.udata_indication      = irda_data_indication;
        notify.flow_indication       = irda_flow_indication;
        notify.flow_indication       = irda_flow_indication;
        notify.instance = self;
        notify.instance = self;
        strncpy(notify.name, name, NOTIFY_MAX_NAME);
        strncpy(notify.name, name, NOTIFY_MAX_NAME);
 
 
        self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
        self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
                                     &notify);
                                     &notify);
        if (self->tsap == NULL) {
        if (self->tsap == NULL) {
                IRDA_DEBUG( 0, "%s(), Unable to allocate TSAP!\n", __FUNCTION__);
                IRDA_DEBUG( 0, "%s(), Unable to allocate TSAP!\n", __FUNCTION__);
                return -ENOMEM;
                return -ENOMEM;
        }
        }
        /* Remember which TSAP selector we actually got */
        /* Remember which TSAP selector we actually got */
        self->stsap_sel = self->tsap->stsap_sel;
        self->stsap_sel = self->tsap->stsap_sel;
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_open_lsap (self)
 * Function irda_open_lsap (self)
 *
 *
 *    Open local Link Service Access Point (LSAP). Used for opening Ultra
 *    Open local Link Service Access Point (LSAP). Used for opening Ultra
 *    sockets
 *    sockets
 */
 */
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
static int irda_open_lsap(struct irda_sock *self, int pid)
static int irda_open_lsap(struct irda_sock *self, int pid)
{
{
        notify_t notify;
        notify_t notify;
 
 
        if (self->lsap) {
        if (self->lsap) {
                WARNING("%s(), busy!\n", __FUNCTION__);
                WARNING("%s(), busy!\n", __FUNCTION__);
                return -EBUSY;
                return -EBUSY;
        }
        }
 
 
        /* Initialize callbacks to be used by the IrDA stack */
        /* Initialize callbacks to be used by the IrDA stack */
        irda_notify_init(&notify);
        irda_notify_init(&notify);
        notify.udata_indication = irda_data_indication;
        notify.udata_indication = irda_data_indication;
        notify.instance = self;
        notify.instance = self;
        strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
        strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
 
 
        self->lsap = irlmp_open_lsap(LSAP_CONNLESS, &notify, pid);
        self->lsap = irlmp_open_lsap(LSAP_CONNLESS, &notify, pid);
        if (self->lsap == NULL) {
        if (self->lsap == NULL) {
                IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __FUNCTION__);
                IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __FUNCTION__);
                return -ENOMEM;
                return -ENOMEM;
        }
        }
 
 
        return 0;
        return 0;
}
}
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
 
 
/*
/*
 * Function irda_find_lsap_sel (self, name)
 * Function irda_find_lsap_sel (self, name)
 *
 *
 *    Try to lookup LSAP selector in remote LM-IAS
 *    Try to lookup LSAP selector in remote LM-IAS
 *
 *
 * Basically, we start a IAP query, and then go to sleep. When the query
 * Basically, we start a IAP query, and then go to sleep. When the query
 * return, irda_getvalue_confirm will wake us up, and we can examine the
 * return, irda_getvalue_confirm will wake us up, and we can examine the
 * result of the query...
 * result of the query...
 * Note that in some case, the query fail even before we go to sleep,
 * Note that in some case, the query fail even before we go to sleep,
 * creating some races...
 * creating some races...
 */
 */
static int irda_find_lsap_sel(struct irda_sock *self, char *name)
static int irda_find_lsap_sel(struct irda_sock *self, char *name)
{
{
        IRDA_DEBUG(2, "%s(%p, %s)\n", __FUNCTION__, self, name);
        IRDA_DEBUG(2, "%s(%p, %s)\n", __FUNCTION__, self, name);
 
 
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        if (self->iriap) {
        if (self->iriap) {
                WARNING("%s(), busy with a previous query\n", __FUNCTION__);
                WARNING("%s(), busy with a previous query\n", __FUNCTION__);
                return -EBUSY;
                return -EBUSY;
        }
        }
 
 
        self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
        self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
                                 irda_getvalue_confirm);
                                 irda_getvalue_confirm);
        if(self->iriap == NULL)
        if(self->iriap == NULL)
                return -ENOMEM;
                return -ENOMEM;
 
 
        /* Treat unexpected signals as disconnect */
        /* Treat unexpected signals as disconnect */
        self->errno = -EHOSTUNREACH;
        self->errno = -EHOSTUNREACH;
 
 
        /* Query remote LM-IAS */
        /* Query remote LM-IAS */
        iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
        iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
                                      name, "IrDA:TinyTP:LsapSel");
                                      name, "IrDA:TinyTP:LsapSel");
        /* Wait for answer (if not already failed) */
        /* Wait for answer (if not already failed) */
        if(self->iriap != NULL)
        if(self->iriap != NULL)
                interruptible_sleep_on(&self->query_wait);
                interruptible_sleep_on(&self->query_wait);
 
 
        /* Check what happened */
        /* Check what happened */
        if (self->errno)
        if (self->errno)
        {
        {
                /* Requested object/attribute doesn't exist */
                /* Requested object/attribute doesn't exist */
                if((self->errno == IAS_CLASS_UNKNOWN) ||
                if((self->errno == IAS_CLASS_UNKNOWN) ||
                   (self->errno == IAS_ATTRIB_UNKNOWN))
                   (self->errno == IAS_ATTRIB_UNKNOWN))
                        return (-EADDRNOTAVAIL);
                        return (-EADDRNOTAVAIL);
                else
                else
                        return (-EHOSTUNREACH);
                        return (-EHOSTUNREACH);
        }
        }
 
 
        /* Get the remote TSAP selector */
        /* Get the remote TSAP selector */
        switch (self->ias_result->type) {
        switch (self->ias_result->type) {
        case IAS_INTEGER:
        case IAS_INTEGER:
                IRDA_DEBUG(4, "%s() int=%d\n", __FUNCTION__,
                IRDA_DEBUG(4, "%s() int=%d\n", __FUNCTION__,
                           self->ias_result->t.integer);
                           self->ias_result->t.integer);
 
 
                if (self->ias_result->t.integer != -1)
                if (self->ias_result->t.integer != -1)
                        self->dtsap_sel = self->ias_result->t.integer;
                        self->dtsap_sel = self->ias_result->t.integer;
                else
                else
                        self->dtsap_sel = 0;
                        self->dtsap_sel = 0;
                break;
                break;
        default:
        default:
                self->dtsap_sel = 0;
                self->dtsap_sel = 0;
                IRDA_DEBUG(0, "%s(), bad type!\n", __FUNCTION__);
                IRDA_DEBUG(0, "%s(), bad type!\n", __FUNCTION__);
                break;
                break;
        }
        }
        if (self->ias_result)
        if (self->ias_result)
                irias_delete_value(self->ias_result);
                irias_delete_value(self->ias_result);
 
 
        if (self->dtsap_sel)
        if (self->dtsap_sel)
                return 0;
                return 0;
 
 
        return -EADDRNOTAVAIL;
        return -EADDRNOTAVAIL;
}
}
 
 
/*
/*
 * Function irda_discover_daddr_and_lsap_sel (self, name)
 * Function irda_discover_daddr_and_lsap_sel (self, name)
 *
 *
 *    This try to find a device with the requested service.
 *    This try to find a device with the requested service.
 *
 *
 * It basically look into the discovery log. For each address in the list,
 * It basically look into the discovery log. For each address in the list,
 * it queries the LM-IAS of the device to find if this device offer
 * it queries the LM-IAS of the device to find if this device offer
 * the requested service.
 * the requested service.
 * If there is more than one node supporting the service, we complain
 * If there is more than one node supporting the service, we complain
 * to the user (it should move devices around).
 * to the user (it should move devices around).
 * The, we set both the destination address and the lsap selector to point
 * The, we set both the destination address and the lsap selector to point
 * on the service on the unique device we have found.
 * on the service on the unique device we have found.
 *
 *
 * Note : this function fails if there is more than one device in range,
 * Note : this function fails if there is more than one device in range,
 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
 * Moreover, we would need to wait the LAP disconnection...
 * Moreover, we would need to wait the LAP disconnection...
 */
 */
static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
{
{
        struct irda_device_info *discoveries;   /* Copy of the discovery log */
        struct irda_device_info *discoveries;   /* Copy of the discovery log */
        int     number;                 /* Number of nodes in the log */
        int     number;                 /* Number of nodes in the log */
        int     i;
        int     i;
        int     err = -ENETUNREACH;
        int     err = -ENETUNREACH;
        __u32   daddr = DEV_ADDR_ANY;   /* Address we found the service on */
        __u32   daddr = DEV_ADDR_ANY;   /* Address we found the service on */
        __u8    dtsap_sel = 0x0;        /* TSAP associated with it */
        __u8    dtsap_sel = 0x0;        /* TSAP associated with it */
 
 
        IRDA_DEBUG(2, "%s(), name=%s\n", __FUNCTION__, name);
        IRDA_DEBUG(2, "%s(), name=%s\n", __FUNCTION__, name);
 
 
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        /* Ask lmp for the current discovery log
        /* Ask lmp for the current discovery log
         * Note : we have to use irlmp_get_discoveries(), as opposed
         * Note : we have to use irlmp_get_discoveries(), as opposed
         * to play with the cachelog directly, because while we are
         * to play with the cachelog directly, because while we are
         * making our ias query, le log might change... */
         * making our ias query, le log might change... */
        discoveries = irlmp_get_discoveries(&number, self->mask, self->nslots);
        discoveries = irlmp_get_discoveries(&number, self->mask, self->nslots);
        /* Check if the we got some results */
        /* Check if the we got some results */
        if (discoveries == NULL)
        if (discoveries == NULL)
                return -ENETUNREACH;    /* No nodes discovered */
                return -ENETUNREACH;    /* No nodes discovered */
 
 
        /*
        /*
         * Now, check all discovered devices (if any), and connect
         * Now, check all discovered devices (if any), and connect
         * client only about the services that the client is
         * client only about the services that the client is
         * interested in...
         * interested in...
         */
         */
        for(i = 0; i < number; i++) {
        for(i = 0; i < number; i++) {
                /* Try the address in the log */
                /* Try the address in the log */
                self->daddr = discoveries[i].daddr;
                self->daddr = discoveries[i].daddr;
                self->saddr = 0x0;
                self->saddr = 0x0;
                IRDA_DEBUG(1, "%s(), trying daddr = %08x\n", __FUNCTION__,
                IRDA_DEBUG(1, "%s(), trying daddr = %08x\n", __FUNCTION__,
                           self->daddr);
                           self->daddr);
 
 
                /* Query remote LM-IAS for this service */
                /* Query remote LM-IAS for this service */
                err = irda_find_lsap_sel(self, name);
                err = irda_find_lsap_sel(self, name);
                switch (err) {
                switch (err) {
                case 0:
                case 0:
                        /* We found the requested service */
                        /* We found the requested service */
                        if(daddr != DEV_ADDR_ANY) {
                        if(daddr != DEV_ADDR_ANY) {
                                IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
                                IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
                                           __FUNCTION__, name);
                                           __FUNCTION__, name);
                                self->daddr = DEV_ADDR_ANY;
                                self->daddr = DEV_ADDR_ANY;
                                kfree(discoveries);
                                kfree(discoveries);
                                return(-ENOTUNIQ);
                                return(-ENOTUNIQ);
                        }
                        }
                        /* First time we found that one, save it ! */
                        /* First time we found that one, save it ! */
                        daddr = self->daddr;
                        daddr = self->daddr;
                        dtsap_sel = self->dtsap_sel;
                        dtsap_sel = self->dtsap_sel;
                        break;
                        break;
                case -EADDRNOTAVAIL:
                case -EADDRNOTAVAIL:
                        /* Requested service simply doesn't exist on this node */
                        /* Requested service simply doesn't exist on this node */
                        break;
                        break;
                default:
                default:
                        /* Something bad did happen :-( */
                        /* Something bad did happen :-( */
                        IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __FUNCTION__);
                        IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __FUNCTION__);
                        self->daddr = DEV_ADDR_ANY;
                        self->daddr = DEV_ADDR_ANY;
                        kfree(discoveries);
                        kfree(discoveries);
                        return(-EHOSTUNREACH);
                        return(-EHOSTUNREACH);
                        break;
                        break;
                }
                }
        }
        }
        /* Cleanup our copy of the discovery log */
        /* Cleanup our copy of the discovery log */
        kfree(discoveries);
        kfree(discoveries);
 
 
        /* Check out what we found */
        /* Check out what we found */
        if(daddr == DEV_ADDR_ANY) {
        if(daddr == DEV_ADDR_ANY) {
                IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
                IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
                           __FUNCTION__, name);
                           __FUNCTION__, name);
                self->daddr = DEV_ADDR_ANY;
                self->daddr = DEV_ADDR_ANY;
                return(-EADDRNOTAVAIL);
                return(-EADDRNOTAVAIL);
        }
        }
 
 
        /* Revert back to discovered device & service */
        /* Revert back to discovered device & service */
        self->daddr = daddr;
        self->daddr = daddr;
        self->saddr = 0x0;
        self->saddr = 0x0;
        self->dtsap_sel = dtsap_sel;
        self->dtsap_sel = dtsap_sel;
 
 
        IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
        IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
                   __FUNCTION__, name, self->daddr);
                   __FUNCTION__, name, self->daddr);
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_getname (sock, uaddr, uaddr_len, peer)
 * Function irda_getname (sock, uaddr, uaddr_len, peer)
 *
 *
 *    Return the our own, or peers socket address (sockaddr_irda)
 *    Return the our own, or peers socket address (sockaddr_irda)
 *
 *
 */
 */
static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
                        int *uaddr_len, int peer)
                        int *uaddr_len, int peer)
{
{
        struct sockaddr_irda saddr;
        struct sockaddr_irda saddr;
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct irda_sock *self = sk->protinfo.irda;
        struct irda_sock *self = sk->protinfo.irda;
 
 
        if (peer) {
        if (peer) {
                if (sk->state != TCP_ESTABLISHED)
                if (sk->state != TCP_ESTABLISHED)
                        return -ENOTCONN;
                        return -ENOTCONN;
 
 
                saddr.sir_family = AF_IRDA;
                saddr.sir_family = AF_IRDA;
                saddr.sir_lsap_sel = self->dtsap_sel;
                saddr.sir_lsap_sel = self->dtsap_sel;
                saddr.sir_addr = self->daddr;
                saddr.sir_addr = self->daddr;
        } else {
        } else {
                saddr.sir_family = AF_IRDA;
                saddr.sir_family = AF_IRDA;
                saddr.sir_lsap_sel = self->stsap_sel;
                saddr.sir_lsap_sel = self->stsap_sel;
                saddr.sir_addr = self->saddr;
                saddr.sir_addr = self->saddr;
        }
        }
 
 
        IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __FUNCTION__, saddr.sir_lsap_sel);
        IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __FUNCTION__, saddr.sir_lsap_sel);
        IRDA_DEBUG(1, "%s(), addr = %08x\n", __FUNCTION__, saddr.sir_addr);
        IRDA_DEBUG(1, "%s(), addr = %08x\n", __FUNCTION__, saddr.sir_addr);
 
 
        /* uaddr_len come to us uninitialised */
        /* uaddr_len come to us uninitialised */
        *uaddr_len = sizeof (struct sockaddr_irda);
        *uaddr_len = sizeof (struct sockaddr_irda);
        memcpy(uaddr, &saddr, *uaddr_len);
        memcpy(uaddr, &saddr, *uaddr_len);
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_listen (sock, backlog)
 * Function irda_listen (sock, backlog)
 *
 *
 *    Just move to the listen state
 *    Just move to the listen state
 *
 *
 */
 */
static int irda_listen(struct socket *sock, int backlog)
static int irda_listen(struct socket *sock, int backlog)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
        if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
            (sk->type != SOCK_DGRAM))
            (sk->type != SOCK_DGRAM))
                return -EOPNOTSUPP;
                return -EOPNOTSUPP;
 
 
        if (sk->state != TCP_LISTEN) {
        if (sk->state != TCP_LISTEN) {
                sk->max_ack_backlog = backlog;
                sk->max_ack_backlog = backlog;
                sk->state           = TCP_LISTEN;
                sk->state           = TCP_LISTEN;
 
 
                return 0;
                return 0;
        }
        }
 
 
        return -EOPNOTSUPP;
        return -EOPNOTSUPP;
}
}
 
 
/*
/*
 * Function irda_bind (sock, uaddr, addr_len)
 * Function irda_bind (sock, uaddr, addr_len)
 *
 *
 *    Used by servers to register their well known TSAP
 *    Used by servers to register their well known TSAP
 *
 *
 */
 */
static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
        struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
        struct irda_sock *self;
        struct irda_sock *self;
        int err;
        int err;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        if (addr_len != sizeof(struct sockaddr_irda))
        if (addr_len != sizeof(struct sockaddr_irda))
                return -EINVAL;
                return -EINVAL;
 
 
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
        /* Special care for Ultra sockets */
        /* Special care for Ultra sockets */
        if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA)) {
        if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA)) {
                self->pid = addr->sir_lsap_sel;
                self->pid = addr->sir_lsap_sel;
                if (self->pid & 0x80) {
                if (self->pid & 0x80) {
                        IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__);
                        IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__);
                        return -EOPNOTSUPP;
                        return -EOPNOTSUPP;
                }
                }
                err = irda_open_lsap(self, self->pid);
                err = irda_open_lsap(self, self->pid);
                if (err < 0)
                if (err < 0)
                        return err;
                        return err;
 
 
                self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
                self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
                self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
                self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
 
 
                /* Pretend we are connected */
                /* Pretend we are connected */
                sock->state = SS_CONNECTED;
                sock->state = SS_CONNECTED;
                sk->state   = TCP_ESTABLISHED;
                sk->state   = TCP_ESTABLISHED;
 
 
                return 0;
                return 0;
        }
        }
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
 
 
        err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
        err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
        if (err < 0)
        if (err < 0)
                return err;
                return err;
 
 
        /*  Register with LM-IAS */
        /*  Register with LM-IAS */
        self->ias_obj = irias_new_object(addr->sir_name, jiffies);
        self->ias_obj = irias_new_object(addr->sir_name, jiffies);
        irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
        irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
                                 self->stsap_sel, IAS_KERNEL_ATTR);
                                 self->stsap_sel, IAS_KERNEL_ATTR);
        irias_insert_object(self->ias_obj);
        irias_insert_object(self->ias_obj);
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_accept (sock, newsock, flags)
 * Function irda_accept (sock, newsock, flags)
 *
 *
 *    Wait for incoming connection
 *    Wait for incoming connection
 *
 *
 */
 */
static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
{
{
        struct irda_sock *self, *new;
        struct irda_sock *self, *new;
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct sock *newsk;
        struct sock *newsk;
        struct sk_buff *skb;
        struct sk_buff *skb;
        int err;
        int err;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        err = irda_create(newsock, sk->protocol);
        err = irda_create(newsock, sk->protocol);
        if (err)
        if (err)
                return err;
                return err;
 
 
        if (sock->state != SS_UNCONNECTED)
        if (sock->state != SS_UNCONNECTED)
                return -EINVAL;
                return -EINVAL;
 
 
        if ((sk = sock->sk) == NULL)
        if ((sk = sock->sk) == NULL)
                return -EINVAL;
                return -EINVAL;
 
 
        if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
        if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
            (sk->type != SOCK_DGRAM))
            (sk->type != SOCK_DGRAM))
                return -EOPNOTSUPP;
                return -EOPNOTSUPP;
 
 
        if (sk->state != TCP_LISTEN)
        if (sk->state != TCP_LISTEN)
                return -EINVAL;
                return -EINVAL;
 
 
        /*
        /*
         *      The read queue this time is holding sockets ready to use
         *      The read queue this time is holding sockets ready to use
         *      hooked into the SABM we saved
         *      hooked into the SABM we saved
         */
         */
        do {
        do {
                if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
                if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
                        if (flags & O_NONBLOCK)
                        if (flags & O_NONBLOCK)
                                return -EWOULDBLOCK;
                                return -EWOULDBLOCK;
 
 
                        interruptible_sleep_on(sk->sleep);
                        interruptible_sleep_on(sk->sleep);
                        if (signal_pending(current))
                        if (signal_pending(current))
                                return -ERESTARTSYS;
                                return -ERESTARTSYS;
                }
                }
        } while (skb == NULL);
        } while (skb == NULL);
 
 
        newsk = newsock->sk;
        newsk = newsock->sk;
        newsk->state = TCP_ESTABLISHED;
        newsk->state = TCP_ESTABLISHED;
 
 
        new = newsk->protinfo.irda;
        new = newsk->protinfo.irda;
        ASSERT(new != NULL, return -1;);
        ASSERT(new != NULL, return -1;);
 
 
        /* Now attach up the new socket */
        /* Now attach up the new socket */
        new->tsap = irttp_dup(self->tsap, new);
        new->tsap = irttp_dup(self->tsap, new);
        if (!new->tsap) {
        if (!new->tsap) {
                IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
                IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
                return -1;
                return -1;
        }
        }
 
 
        new->stsap_sel = new->tsap->stsap_sel;
        new->stsap_sel = new->tsap->stsap_sel;
        new->dtsap_sel = new->tsap->dtsap_sel;
        new->dtsap_sel = new->tsap->dtsap_sel;
        new->saddr = irttp_get_saddr(new->tsap);
        new->saddr = irttp_get_saddr(new->tsap);
        new->daddr = irttp_get_daddr(new->tsap);
        new->daddr = irttp_get_daddr(new->tsap);
 
 
        new->max_sdu_size_tx = self->max_sdu_size_tx;
        new->max_sdu_size_tx = self->max_sdu_size_tx;
        new->max_sdu_size_rx = self->max_sdu_size_rx;
        new->max_sdu_size_rx = self->max_sdu_size_rx;
        new->max_data_size   = self->max_data_size;
        new->max_data_size   = self->max_data_size;
        new->max_header_size = self->max_header_size;
        new->max_header_size = self->max_header_size;
 
 
        memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
        memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
 
 
        /* Clean up the original one to keep it in listen state */
        /* Clean up the original one to keep it in listen state */
        irttp_listen(self->tsap);
        irttp_listen(self->tsap);
 
 
        skb->sk = NULL;
        skb->sk = NULL;
        skb->destructor = NULL;
        skb->destructor = NULL;
        kfree_skb(skb);
        kfree_skb(skb);
        sk->ack_backlog--;
        sk->ack_backlog--;
 
 
        newsock->state = SS_CONNECTED;
        newsock->state = SS_CONNECTED;
 
 
        irda_connect_response(new);
        irda_connect_response(new);
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_connect (sock, uaddr, addr_len, flags)
 * Function irda_connect (sock, uaddr, addr_len, flags)
 *
 *
 *    Connect to a IrDA device
 *    Connect to a IrDA device
 *
 *
 * The main difference with a "standard" connect is that with IrDA we need
 * The main difference with a "standard" connect is that with IrDA we need
 * to resolve the service name into a TSAP selector (in TCP, port number
 * to resolve the service name into a TSAP selector (in TCP, port number
 * doesn't have to be resolved).
 * doesn't have to be resolved).
 * Because of this service name resoltion, we can offer "auto-connect",
 * Because of this service name resoltion, we can offer "auto-connect",
 * where we connect to a service without specifying a destination address.
 * where we connect to a service without specifying a destination address.
 *
 *
 * Note : by consulting "errno", the user space caller may learn the cause
 * Note : by consulting "errno", the user space caller may learn the cause
 * of the failure. Most of them are visible in the function, others may come
 * of the failure. Most of them are visible in the function, others may come
 * from subroutines called and are listed here :
 * from subroutines called and are listed here :
 *      o EBUSY : already processing a connect
 *      o EBUSY : already processing a connect
 *      o EHOSTUNREACH : bad addr->sir_addr argument
 *      o EHOSTUNREACH : bad addr->sir_addr argument
 *      o EADDRNOTAVAIL : bad addr->sir_name argument
 *      o EADDRNOTAVAIL : bad addr->sir_name argument
 *      o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
 *      o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
 *      o ENETUNREACH : no node found on the network (auto-connect)
 *      o ENETUNREACH : no node found on the network (auto-connect)
 */
 */
static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
                        int addr_len, int flags)
                        int addr_len, int flags)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
        struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
        struct irda_sock *self;
        struct irda_sock *self;
        int err;
        int err;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        /* Don't allow connect for Ultra sockets */
        /* Don't allow connect for Ultra sockets */
        if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA))
        if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA))
                return -ESOCKTNOSUPPORT;
                return -ESOCKTNOSUPPORT;
 
 
        if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
        if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
                sock->state = SS_CONNECTED;
                sock->state = SS_CONNECTED;
                return 0;   /* Connect completed during a ERESTARTSYS event */
                return 0;   /* Connect completed during a ERESTARTSYS event */
        }
        }
 
 
        if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
        if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
                sock->state = SS_UNCONNECTED;
                sock->state = SS_UNCONNECTED;
                return -ECONNREFUSED;
                return -ECONNREFUSED;
        }
        }
 
 
        if (sk->state == TCP_ESTABLISHED)
        if (sk->state == TCP_ESTABLISHED)
                return -EISCONN;      /* No reconnect on a seqpacket socket */
                return -EISCONN;      /* No reconnect on a seqpacket socket */
 
 
        sk->state   = TCP_CLOSE;
        sk->state   = TCP_CLOSE;
        sock->state = SS_UNCONNECTED;
        sock->state = SS_UNCONNECTED;
 
 
        if (addr_len != sizeof(struct sockaddr_irda))
        if (addr_len != sizeof(struct sockaddr_irda))
                return -EINVAL;
                return -EINVAL;
 
 
        /* Check if user supplied any destination device address */
        /* Check if user supplied any destination device address */
        if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
        if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
                /* Try to find one suitable */
                /* Try to find one suitable */
                err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
                err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
                if (err) {
                if (err) {
                        IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __FUNCTION__);
                        IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __FUNCTION__);
                        return err;
                        return err;
                }
                }
        } else {
        } else {
                /* Use the one provided by the user */
                /* Use the one provided by the user */
                self->daddr = addr->sir_addr;
                self->daddr = addr->sir_addr;
                IRDA_DEBUG(1, "%s(), daddr = %08x\n", __FUNCTION__, self->daddr);
                IRDA_DEBUG(1, "%s(), daddr = %08x\n", __FUNCTION__, self->daddr);
 
 
                /* Query remote LM-IAS */
                /* Query remote LM-IAS */
                err = irda_find_lsap_sel(self, addr->sir_name);
                err = irda_find_lsap_sel(self, addr->sir_name);
                if (err) {
                if (err) {
                        IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
                        IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
                        return err;
                        return err;
                }
                }
        }
        }
 
 
        /* Check if we have opened a local TSAP */
        /* Check if we have opened a local TSAP */
        if (!self->tsap)
        if (!self->tsap)
                irda_open_tsap(self, LSAP_ANY, addr->sir_name);
                irda_open_tsap(self, LSAP_ANY, addr->sir_name);
 
 
        /* Move to connecting socket, start sending Connect Requests */
        /* Move to connecting socket, start sending Connect Requests */
        sock->state = SS_CONNECTING;
        sock->state = SS_CONNECTING;
        sk->state   = TCP_SYN_SENT;
        sk->state   = TCP_SYN_SENT;
 
 
        /* Connect to remote device */
        /* Connect to remote device */
        err = irttp_connect_request(self->tsap, self->dtsap_sel,
        err = irttp_connect_request(self->tsap, self->dtsap_sel,
                                    self->saddr, self->daddr, NULL,
                                    self->saddr, self->daddr, NULL,
                                    self->max_sdu_size_rx, NULL);
                                    self->max_sdu_size_rx, NULL);
        if (err) {
        if (err) {
                IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
                IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
                return err;
                return err;
        }
        }
 
 
        /* Now the loop */
        /* Now the loop */
        if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
        if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
                return -EINPROGRESS;
                return -EINPROGRESS;
 
 
        /* Here, there is a race condition : the state may change between
        /* Here, there is a race condition : the state may change between
         * our test and the sleep, via irda_connect_confirm().
         * our test and the sleep, via irda_connect_confirm().
         * The way to workaround that is to sleep with a timeout, so that
         * The way to workaround that is to sleep with a timeout, so that
         * we don't sleep forever and check the state when waking up.
         * we don't sleep forever and check the state when waking up.
         * 50ms is plenty good enough, because the LAP is already connected.
         * 50ms is plenty good enough, because the LAP is already connected.
         * Jean II */
         * Jean II */
        while (sk->state == TCP_SYN_SENT) {
        while (sk->state == TCP_SYN_SENT) {
                interruptible_sleep_on_timeout(sk->sleep, HZ/20);
                interruptible_sleep_on_timeout(sk->sleep, HZ/20);
                if (signal_pending(current)) {
                if (signal_pending(current)) {
                        return -ERESTARTSYS;
                        return -ERESTARTSYS;
                }
                }
        }
        }
 
 
        if (sk->state != TCP_ESTABLISHED) {
        if (sk->state != TCP_ESTABLISHED) {
                sock->state = SS_UNCONNECTED;
                sock->state = SS_UNCONNECTED;
                return sock_error(sk);  /* Always set at this point */
                return sock_error(sk);  /* Always set at this point */
        }
        }
 
 
        sock->state = SS_CONNECTED;
        sock->state = SS_CONNECTED;
 
 
        /* At this point, IrLMP has assigned our source address */
        /* At this point, IrLMP has assigned our source address */
        self->saddr = irttp_get_saddr(self->tsap);
        self->saddr = irttp_get_saddr(self->tsap);
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_create (sock, protocol)
 * Function irda_create (sock, protocol)
 *
 *
 *    Create IrDA socket
 *    Create IrDA socket
 *
 *
 */
 */
static int irda_create(struct socket *sock, int protocol)
static int irda_create(struct socket *sock, int protocol)
{
{
        struct sock *sk;
        struct sock *sk;
        struct irda_sock *self;
        struct irda_sock *self;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        /* Check for valid socket type */
        /* Check for valid socket type */
        switch (sock->type) {
        switch (sock->type) {
        case SOCK_STREAM:     /* For TTP connections with SAR disabled */
        case SOCK_STREAM:     /* For TTP connections with SAR disabled */
        case SOCK_SEQPACKET:  /* For TTP connections with SAR enabled */
        case SOCK_SEQPACKET:  /* For TTP connections with SAR enabled */
        case SOCK_DGRAM:      /* For TTP Unitdata or LMP Ultra transfers */
        case SOCK_DGRAM:      /* For TTP Unitdata or LMP Ultra transfers */
                break;
                break;
        default:
        default:
                return -ESOCKTNOSUPPORT;
                return -ESOCKTNOSUPPORT;
        }
        }
 
 
        /* Allocate networking socket */
        /* Allocate networking socket */
        if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
        if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
                return -ENOMEM;
                return -ENOMEM;
 
 
        /* Allocate IrDA socket */
        /* Allocate IrDA socket */
        self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
        self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
        if (self == NULL) {
        if (self == NULL) {
                sk_free(sk);
                sk_free(sk);
                return -ENOMEM;
                return -ENOMEM;
        }
        }
        memset(self, 0, sizeof(struct irda_sock));
        memset(self, 0, sizeof(struct irda_sock));
 
 
        IRDA_DEBUG(2, "%s() : self is %p\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s() : self is %p\n", __FUNCTION__, self);
 
 
        init_waitqueue_head(&self->query_wait);
        init_waitqueue_head(&self->query_wait);
 
 
        /* Initialise networking socket struct */
        /* Initialise networking socket struct */
        sock_init_data(sock, sk);       /* Note : set sk->refcnt to 1 */
        sock_init_data(sock, sk);       /* Note : set sk->refcnt to 1 */
        sk->family = PF_IRDA;
        sk->family = PF_IRDA;
        sk->protocol = protocol;
        sk->protocol = protocol;
        /* Link networking socket and IrDA socket structs together */
        /* Link networking socket and IrDA socket structs together */
        sk->protinfo.irda = self;
        sk->protinfo.irda = self;
        self->sk = sk;
        self->sk = sk;
 
 
        switch (sock->type) {
        switch (sock->type) {
        case SOCK_STREAM:
        case SOCK_STREAM:
                sock->ops = &irda_stream_ops;
                sock->ops = &irda_stream_ops;
                self->max_sdu_size_rx = TTP_SAR_DISABLE;
                self->max_sdu_size_rx = TTP_SAR_DISABLE;
                break;
                break;
        case SOCK_SEQPACKET:
        case SOCK_SEQPACKET:
                sock->ops = &irda_seqpacket_ops;
                sock->ops = &irda_seqpacket_ops;
                self->max_sdu_size_rx = TTP_SAR_UNBOUND;
                self->max_sdu_size_rx = TTP_SAR_UNBOUND;
                break;
                break;
        case SOCK_DGRAM:
        case SOCK_DGRAM:
                switch (protocol) {
                switch (protocol) {
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
                case IRDAPROTO_ULTRA:
                case IRDAPROTO_ULTRA:
                        sock->ops = &irda_ultra_ops;
                        sock->ops = &irda_ultra_ops;
                        break;
                        break;
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
                case IRDAPROTO_UNITDATA:
                case IRDAPROTO_UNITDATA:
                        sock->ops = &irda_dgram_ops;
                        sock->ops = &irda_dgram_ops;
                        /* We let Unitdata conn. be like seqpack conn. */
                        /* We let Unitdata conn. be like seqpack conn. */
                        self->max_sdu_size_rx = TTP_SAR_UNBOUND;
                        self->max_sdu_size_rx = TTP_SAR_UNBOUND;
                        break;
                        break;
                default:
                default:
                        ERROR("%s(), protocol not supported!\n", __FUNCTION__);
                        ERROR("%s(), protocol not supported!\n", __FUNCTION__);
                        return -ESOCKTNOSUPPORT;
                        return -ESOCKTNOSUPPORT;
                }
                }
                break;
                break;
        default:
        default:
                return -ESOCKTNOSUPPORT;
                return -ESOCKTNOSUPPORT;
        }
        }
 
 
        /* Register as a client with IrLMP */
        /* Register as a client with IrLMP */
        self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
        self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
        self->mask = 0xffff;
        self->mask = 0xffff;
        self->rx_flow = self->tx_flow = FLOW_START;
        self->rx_flow = self->tx_flow = FLOW_START;
        self->nslots = DISCOVERY_DEFAULT_SLOTS;
        self->nslots = DISCOVERY_DEFAULT_SLOTS;
        self->daddr = DEV_ADDR_ANY;     /* Until we get connected */
        self->daddr = DEV_ADDR_ANY;     /* Until we get connected */
        self->saddr = 0x0;              /* so IrLMP assign us any link */
        self->saddr = 0x0;              /* so IrLMP assign us any link */
 
 
        MOD_INC_USE_COUNT;
        MOD_INC_USE_COUNT;
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_destroy_socket (self)
 * Function irda_destroy_socket (self)
 *
 *
 *    Destroy socket
 *    Destroy socket
 *
 *
 */
 */
void irda_destroy_socket(struct irda_sock *self)
void irda_destroy_socket(struct irda_sock *self)
{
{
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        ASSERT(self != NULL, return;);
        ASSERT(self != NULL, return;);
 
 
        /* Unregister with IrLMP */
        /* Unregister with IrLMP */
        irlmp_unregister_client(self->ckey);
        irlmp_unregister_client(self->ckey);
        irlmp_unregister_service(self->skey);
        irlmp_unregister_service(self->skey);
 
 
        /* Unregister with LM-IAS */
        /* Unregister with LM-IAS */
        if (self->ias_obj) {
        if (self->ias_obj) {
                irias_delete_object(self->ias_obj);
                irias_delete_object(self->ias_obj);
                self->ias_obj = NULL;
                self->ias_obj = NULL;
        }
        }
 
 
        if (self->iriap) {
        if (self->iriap) {
                iriap_close(self->iriap);
                iriap_close(self->iriap);
                self->iriap = NULL;
                self->iriap = NULL;
        }
        }
 
 
        if (self->tsap) {
        if (self->tsap) {
                irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
                irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
                irttp_close_tsap(self->tsap);
                irttp_close_tsap(self->tsap);
                self->tsap = NULL;
                self->tsap = NULL;
        }
        }
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
        if (self->lsap) {
        if (self->lsap) {
                irlmp_close_lsap(self->lsap);
                irlmp_close_lsap(self->lsap);
                self->lsap = NULL;
                self->lsap = NULL;
        }
        }
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
        kfree(self);
        kfree(self);
        MOD_DEC_USE_COUNT;
        MOD_DEC_USE_COUNT;
 
 
        return;
        return;
}
}
 
 
/*
/*
 * Function irda_release (sock)
 * Function irda_release (sock)
 *
 *
 *
 *
 *
 *
 */
 */
static int irda_release(struct socket *sock)
static int irda_release(struct socket *sock)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
 
 
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 
        if (sk == NULL)
        if (sk == NULL)
                return 0;
                return 0;
 
 
        sk->state       = TCP_CLOSE;
        sk->state       = TCP_CLOSE;
        sk->shutdown   |= SEND_SHUTDOWN;
        sk->shutdown   |= SEND_SHUTDOWN;
        sk->state_change(sk);
        sk->state_change(sk);
 
 
        /* Destroy IrDA socket */
        /* Destroy IrDA socket */
        irda_destroy_socket(sk->protinfo.irda);
        irda_destroy_socket(sk->protinfo.irda);
        /* Prevent sock_def_destruct() to create havoc */
        /* Prevent sock_def_destruct() to create havoc */
        sk->protinfo.irda = NULL;
        sk->protinfo.irda = NULL;
 
 
        sock_orphan(sk);
        sock_orphan(sk);
        sock->sk   = NULL;
        sock->sk   = NULL;
 
 
        /* Purge queues (see sock_init_data()) */
        /* Purge queues (see sock_init_data()) */
        skb_queue_purge(&sk->receive_queue);
        skb_queue_purge(&sk->receive_queue);
 
 
        /* Destroy networking socket if we are the last reference on it,
        /* Destroy networking socket if we are the last reference on it,
         * i.e. if(sk->refcnt == 0) -> sk_free(sk) */
         * i.e. if(sk->refcnt == 0) -> sk_free(sk) */
        sock_put(sk);
        sock_put(sk);
 
 
        /* Notes on socket locking and deallocation... - Jean II
        /* Notes on socket locking and deallocation... - Jean II
         * In theory we should put pairs of sock_hold() / sock_put() to
         * In theory we should put pairs of sock_hold() / sock_put() to
         * prevent the socket to be destroyed whenever there is an
         * prevent the socket to be destroyed whenever there is an
         * outstanding request or outstanding incomming packet or event.
         * outstanding request or outstanding incomming packet or event.
         *
         *
         * 1) This may include IAS request, both in connect and getsockopt.
         * 1) This may include IAS request, both in connect and getsockopt.
         * Unfortunately, the situation is a bit more messy than it looks,
         * Unfortunately, the situation is a bit more messy than it looks,
         * because we close iriap and kfree(self) above.
         * because we close iriap and kfree(self) above.
         *
         *
         * 2) This may include selective discovery in getsockopt.
         * 2) This may include selective discovery in getsockopt.
         * Same stuff as above, irlmp registration and self are gone.
         * Same stuff as above, irlmp registration and self are gone.
         *
         *
         * Probably 1 and 2 may not matter, because it's all triggered
         * Probably 1 and 2 may not matter, because it's all triggered
         * by a process and the socket layer already prevent the
         * by a process and the socket layer already prevent the
         * socket to go away while a process is holding it, through
         * socket to go away while a process is holding it, through
         * sockfd_put() and fput()...
         * sockfd_put() and fput()...
         *
         *
         * 3) This may include deferred TSAP closure. In particular,
         * 3) This may include deferred TSAP closure. In particular,
         * we may receive a late irda_disconnect_indication()
         * we may receive a late irda_disconnect_indication()
         * Fortunately, (tsap_cb *)->close_pend should protect us
         * Fortunately, (tsap_cb *)->close_pend should protect us
         * from that.
         * from that.
         *
         *
         * I did some testing on SMP, and it looks solid. And the socket
         * I did some testing on SMP, and it looks solid. And the socket
         * memory leak is now gone... - Jean II
         * memory leak is now gone... - Jean II
         */
         */
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_sendmsg (sock, msg, len, scm)
 * Function irda_sendmsg (sock, msg, len, scm)
 *
 *
 *    Send message down to TinyTP. This function is used for both STREAM and
 *    Send message down to TinyTP. This function is used for both STREAM and
 *    SEQPACK services. This is possible since it forces the client to
 *    SEQPACK services. This is possible since it forces the client to
 *    fragment the message if necessary
 *    fragment the message if necessary
 */
 */
static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len,
static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len,
                        struct scm_cookie *scm)
                        struct scm_cookie *scm)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct irda_sock *self;
        struct irda_sock *self;
        struct sk_buff *skb;
        struct sk_buff *skb;
        unsigned char *asmptr;
        unsigned char *asmptr;
        int err;
        int err;
 
 
        IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__, len);
        IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__, len);
 
 
        /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
        /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
        if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR))
        if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR))
                return -EINVAL;
                return -EINVAL;
 
 
        if (sk->shutdown & SEND_SHUTDOWN) {
        if (sk->shutdown & SEND_SHUTDOWN) {
                send_sig(SIGPIPE, current, 0);
                send_sig(SIGPIPE, current, 0);
                return -EPIPE;
                return -EPIPE;
        }
        }
 
 
        if (sk->state != TCP_ESTABLISHED)
        if (sk->state != TCP_ESTABLISHED)
                return -ENOTCONN;
                return -ENOTCONN;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        /* Check if IrTTP is wants us to slow down */
        /* Check if IrTTP is wants us to slow down */
        while (self->tx_flow == FLOW_STOP) {
        while (self->tx_flow == FLOW_STOP) {
                IRDA_DEBUG(2, "%s(), IrTTP is busy, going to sleep!\n", __FUNCTION__);
                IRDA_DEBUG(2, "%s(), IrTTP is busy, going to sleep!\n", __FUNCTION__);
                interruptible_sleep_on(sk->sleep);
                interruptible_sleep_on(sk->sleep);
 
 
                /* Check if we are still connected */
                /* Check if we are still connected */
                if (sk->state != TCP_ESTABLISHED)
                if (sk->state != TCP_ESTABLISHED)
                        return -ENOTCONN;
                        return -ENOTCONN;
                /* Handle signals */
                /* Handle signals */
                if (signal_pending(current))
                if (signal_pending(current))
                        return -ERESTARTSYS;
                        return -ERESTARTSYS;
        }
        }
 
 
        /* Check that we don't send out to big frames */
        /* Check that we don't send out to big frames */
        if (len > self->max_data_size) {
        if (len > self->max_data_size) {
                IRDA_DEBUG(2, "%s(), Chopping frame from %d to %d bytes!\n", __FUNCTION__, len,
                IRDA_DEBUG(2, "%s(), Chopping frame from %d to %d bytes!\n", __FUNCTION__, len,
                           self->max_data_size);
                           self->max_data_size);
                len = self->max_data_size;
                len = self->max_data_size;
        }
        }
 
 
        skb = sock_alloc_send_skb(sk, len + self->max_header_size,
        skb = sock_alloc_send_skb(sk, len + self->max_header_size,
                                  msg->msg_flags & MSG_DONTWAIT, &err);
                                  msg->msg_flags & MSG_DONTWAIT, &err);
        if (!skb)
        if (!skb)
                return -ENOBUFS;
                return -ENOBUFS;
 
 
        skb_reserve(skb, self->max_header_size);
        skb_reserve(skb, self->max_header_size);
 
 
        asmptr = skb->h.raw = skb_put(skb, len);
        asmptr = skb->h.raw = skb_put(skb, len);
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
 
 
        /*
        /*
         * Just send the message to TinyTP, and let it deal with possible
         * Just send the message to TinyTP, and let it deal with possible
         * errors. No need to duplicate all that here
         * errors. No need to duplicate all that here
         */
         */
        err = irttp_data_request(self->tsap, skb);
        err = irttp_data_request(self->tsap, skb);
        if (err) {
        if (err) {
                IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
                IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
                return err;
                return err;
        }
        }
        /* Tell client how much data we actually sent */
        /* Tell client how much data we actually sent */
        return len;
        return len;
}
}
 
 
/*
/*
 * Function irda_recvmsg_dgram (sock, msg, size, flags, scm)
 * Function irda_recvmsg_dgram (sock, msg, size, flags, scm)
 *
 *
 *    Try to receive message and copy it to user. The frame is discarded
 *    Try to receive message and copy it to user. The frame is discarded
 *    after being read, regardless of how much the user actually read
 *    after being read, regardless of how much the user actually read
 */
 */
static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
                              int size, int flags, struct scm_cookie *scm)
                              int size, int flags, struct scm_cookie *scm)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct sk_buff *skb;
        struct sk_buff *skb;
        int copied, err;
        int copied, err;
 
 
        IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
        skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
                                flags & MSG_DONTWAIT, &err);
                                flags & MSG_DONTWAIT, &err);
        if (!skb)
        if (!skb)
                return err;
                return err;
 
 
        skb->h.raw = skb->data;
        skb->h.raw = skb->data;
        copied     = skb->len;
        copied     = skb->len;
 
 
        if (copied > size) {
        if (copied > size) {
                IRDA_DEBUG(2, "%s(), Received truncated frame (%d < %d)!\n", __FUNCTION__,
                IRDA_DEBUG(2, "%s(), Received truncated frame (%d < %d)!\n", __FUNCTION__,
                           copied, size);
                           copied, size);
                copied = size;
                copied = size;
                msg->msg_flags |= MSG_TRUNC;
                msg->msg_flags |= MSG_TRUNC;
        }
        }
        skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
        skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
 
 
        skb_free_datagram(sk, skb);
        skb_free_datagram(sk, skb);
 
 
        /*
        /*
         *  Check if we have previously stopped IrTTP and we know
         *  Check if we have previously stopped IrTTP and we know
         *  have more free space in our rx_queue. If so tell IrTTP
         *  have more free space in our rx_queue. If so tell IrTTP
         *  to start delivering frames again before our rx_queue gets
         *  to start delivering frames again before our rx_queue gets
         *  empty
         *  empty
         */
         */
        if (self->rx_flow == FLOW_STOP) {
        if (self->rx_flow == FLOW_STOP) {
                if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
                if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
                        IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
                        IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
                        self->rx_flow = FLOW_START;
                        self->rx_flow = FLOW_START;
                        irttp_flow_request(self->tsap, FLOW_START);
                        irttp_flow_request(self->tsap, FLOW_START);
                }
                }
        }
        }
 
 
        return copied;
        return copied;
}
}
 
 
/*
/*
 * Function irda_data_wait (sk)
 * Function irda_data_wait (sk)
 *
 *
 *    Sleep until data has arrive. But check for races..
 *    Sleep until data has arrive. But check for races..
 *
 *
 */
 */
static void irda_data_wait(struct sock *sk)
static void irda_data_wait(struct sock *sk)
{
{
        if (!skb_peek(&sk->receive_queue)) {
        if (!skb_peek(&sk->receive_queue)) {
                set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
                set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
                interruptible_sleep_on(sk->sleep);
                interruptible_sleep_on(sk->sleep);
                clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
                clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
        }
        }
}
}
 
 
/*
/*
 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
 *
 *
 *
 *
 *
 *
 */
 */
static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
                               int size, int flags, struct scm_cookie *scm)
                               int size, int flags, struct scm_cookie *scm)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        int noblock = flags & MSG_DONTWAIT;
        int noblock = flags & MSG_DONTWAIT;
        int copied = 0;
        int copied = 0;
        int target = 1;
        int target = 1;
 
 
        IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        if (sock->flags & __SO_ACCEPTCON)
        if (sock->flags & __SO_ACCEPTCON)
                return(-EINVAL);
                return(-EINVAL);
 
 
        if (flags & MSG_OOB)
        if (flags & MSG_OOB)
                return -EOPNOTSUPP;
                return -EOPNOTSUPP;
 
 
        if (flags & MSG_WAITALL)
        if (flags & MSG_WAITALL)
                target = size;
                target = size;
 
 
        msg->msg_namelen = 0;
        msg->msg_namelen = 0;
 
 
        do {
        do {
                int chunk;
                int chunk;
                struct sk_buff *skb;
                struct sk_buff *skb;
 
 
                skb=skb_dequeue(&sk->receive_queue);
                skb=skb_dequeue(&sk->receive_queue);
                if (skb==NULL) {
                if (skb==NULL) {
                        if (copied >= target)
                        if (copied >= target)
                                break;
                                break;
 
 
                        /*
                        /*
                         *      POSIX 1003.1g mandates this order.
                         *      POSIX 1003.1g mandates this order.
                         */
                         */
 
 
                        if (sk->err) {
                        if (sk->err) {
                                return sock_error(sk);
                                return sock_error(sk);
                        }
                        }
 
 
                        if (sk->shutdown & RCV_SHUTDOWN)
                        if (sk->shutdown & RCV_SHUTDOWN)
                                break;
                                break;
 
 
                        if (noblock)
                        if (noblock)
                                return -EAGAIN;
                                return -EAGAIN;
                        irda_data_wait(sk);
                        irda_data_wait(sk);
                        if (signal_pending(current))
                        if (signal_pending(current))
                                return -ERESTARTSYS;
                                return -ERESTARTSYS;
                        continue;
                        continue;
                }
                }
 
 
                chunk = min_t(unsigned int, skb->len, size);
                chunk = min_t(unsigned int, skb->len, size);
                if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
                if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
                        skb_queue_head(&sk->receive_queue, skb);
                        skb_queue_head(&sk->receive_queue, skb);
                        if (copied == 0)
                        if (copied == 0)
                                copied = -EFAULT;
                                copied = -EFAULT;
                        break;
                        break;
                }
                }
                copied += chunk;
                copied += chunk;
                size -= chunk;
                size -= chunk;
 
 
                /* Mark read part of skb as used */
                /* Mark read part of skb as used */
                if (!(flags & MSG_PEEK)) {
                if (!(flags & MSG_PEEK)) {
                        skb_pull(skb, chunk);
                        skb_pull(skb, chunk);
 
 
                        /* put the skb back if we didn't use it up.. */
                        /* put the skb back if we didn't use it up.. */
                        if (skb->len) {
                        if (skb->len) {
                                IRDA_DEBUG(1, "%s(), back on q!\n", __FUNCTION__);
                                IRDA_DEBUG(1, "%s(), back on q!\n", __FUNCTION__);
                                skb_queue_head(&sk->receive_queue, skb);
                                skb_queue_head(&sk->receive_queue, skb);
                                break;
                                break;
                        }
                        }
 
 
                        kfree_skb(skb);
                        kfree_skb(skb);
                } else {
                } else {
                        IRDA_DEBUG(0, "%s() questionable!?\n", __FUNCTION__);
                        IRDA_DEBUG(0, "%s() questionable!?\n", __FUNCTION__);
 
 
                        /* put message back and return */
                        /* put message back and return */
                        skb_queue_head(&sk->receive_queue, skb);
                        skb_queue_head(&sk->receive_queue, skb);
                        break;
                        break;
                }
                }
        } while (size);
        } while (size);
 
 
        /*
        /*
         *  Check if we have previously stopped IrTTP and we know
         *  Check if we have previously stopped IrTTP and we know
         *  have more free space in our rx_queue. If so tell IrTTP
         *  have more free space in our rx_queue. If so tell IrTTP
         *  to start delivering frames again before our rx_queue gets
         *  to start delivering frames again before our rx_queue gets
         *  empty
         *  empty
         */
         */
        if (self->rx_flow == FLOW_STOP) {
        if (self->rx_flow == FLOW_STOP) {
                if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
                if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
                        IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
                        IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
                        self->rx_flow = FLOW_START;
                        self->rx_flow = FLOW_START;
                        irttp_flow_request(self->tsap, FLOW_START);
                        irttp_flow_request(self->tsap, FLOW_START);
                }
                }
        }
        }
 
 
        return copied;
        return copied;
}
}
 
 
/*
/*
 * Function irda_sendmsg_dgram (sock, msg, len, scm)
 * Function irda_sendmsg_dgram (sock, msg, len, scm)
 *
 *
 *    Send message down to TinyTP for the unreliable sequenced
 *    Send message down to TinyTP for the unreliable sequenced
 *    packet service...
 *    packet service...
 *
 *
 */
 */
static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
                              int len, struct scm_cookie *scm)
                              int len, struct scm_cookie *scm)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct irda_sock *self;
        struct irda_sock *self;
        struct sk_buff *skb;
        struct sk_buff *skb;
        unsigned char *asmptr;
        unsigned char *asmptr;
        int err;
        int err;
 
 
        IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__, len);
        IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__, len);
 
 
        if (msg->msg_flags & ~MSG_DONTWAIT)
        if (msg->msg_flags & ~MSG_DONTWAIT)
                return -EINVAL;
                return -EINVAL;
 
 
        if (sk->shutdown & SEND_SHUTDOWN) {
        if (sk->shutdown & SEND_SHUTDOWN) {
                send_sig(SIGPIPE, current, 0);
                send_sig(SIGPIPE, current, 0);
                return -EPIPE;
                return -EPIPE;
        }
        }
 
 
        if (sk->state != TCP_ESTABLISHED)
        if (sk->state != TCP_ESTABLISHED)
                return -ENOTCONN;
                return -ENOTCONN;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        /*
        /*
         * Check that we don't send out to big frames. This is an unreliable
         * Check that we don't send out to big frames. This is an unreliable
         * service, so we have no fragmentation and no coalescence
         * service, so we have no fragmentation and no coalescence
         */
         */
        if (len > self->max_data_size) {
        if (len > self->max_data_size) {
                IRDA_DEBUG(0, "%s(), Warning to much data! "
                IRDA_DEBUG(0, "%s(), Warning to much data! "
                           "Chopping frame from %d to %d bytes!\n", __FUNCTION__, len,
                           "Chopping frame from %d to %d bytes!\n", __FUNCTION__, len,
                           self->max_data_size);
                           self->max_data_size);
                len = self->max_data_size;
                len = self->max_data_size;
        }
        }
 
 
        skb = sock_alloc_send_skb(sk, len + self->max_header_size,
        skb = sock_alloc_send_skb(sk, len + self->max_header_size,
                                  msg->msg_flags & MSG_DONTWAIT, &err);
                                  msg->msg_flags & MSG_DONTWAIT, &err);
        if (!skb)
        if (!skb)
                return -ENOBUFS;
                return -ENOBUFS;
 
 
        skb_reserve(skb, self->max_header_size);
        skb_reserve(skb, self->max_header_size);
 
 
        IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
        IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
        asmptr = skb->h.raw = skb_put(skb, len);
        asmptr = skb->h.raw = skb_put(skb, len);
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
 
 
        /*
        /*
         * Just send the message to TinyTP, and let it deal with possible
         * Just send the message to TinyTP, and let it deal with possible
         * errors. No need to duplicate all that here
         * errors. No need to duplicate all that here
         */
         */
        err = irttp_udata_request(self->tsap, skb);
        err = irttp_udata_request(self->tsap, skb);
        if (err) {
        if (err) {
                IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
                IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
                return err;
                return err;
        }
        }
        return len;
        return len;
}
}
 
 
/*
/*
 * Function irda_sendmsg_ultra (sock, msg, len, scm)
 * Function irda_sendmsg_ultra (sock, msg, len, scm)
 *
 *
 *    Send message down to IrLMP for the unreliable Ultra
 *    Send message down to IrLMP for the unreliable Ultra
 *    packet service...
 *    packet service...
 */
 */
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
                              int len, struct scm_cookie *scm)
                              int len, struct scm_cookie *scm)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct irda_sock *self;
        struct irda_sock *self;
        struct sk_buff *skb;
        struct sk_buff *skb;
        unsigned char *asmptr;
        unsigned char *asmptr;
        int err;
        int err;
 
 
        IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__, len);
        IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__, len);
 
 
        if (msg->msg_flags & ~MSG_DONTWAIT)
        if (msg->msg_flags & ~MSG_DONTWAIT)
                return -EINVAL;
                return -EINVAL;
 
 
        if (sk->shutdown & SEND_SHUTDOWN) {
        if (sk->shutdown & SEND_SHUTDOWN) {
                send_sig(SIGPIPE, current, 0);
                send_sig(SIGPIPE, current, 0);
                return -EPIPE;
                return -EPIPE;
        }
        }
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        /*
        /*
         * Check that we don't send out to big frames. This is an unreliable
         * Check that we don't send out to big frames. This is an unreliable
         * service, so we have no fragmentation and no coalescence
         * service, so we have no fragmentation and no coalescence
         */
         */
        if (len > self->max_data_size) {
        if (len > self->max_data_size) {
                IRDA_DEBUG(0, "%s(), Warning to much data! "
                IRDA_DEBUG(0, "%s(), Warning to much data! "
                           "Chopping frame from %d to %d bytes!\n", __FUNCTION__, len,
                           "Chopping frame from %d to %d bytes!\n", __FUNCTION__, len,
                           self->max_data_size);
                           self->max_data_size);
                len = self->max_data_size;
                len = self->max_data_size;
        }
        }
 
 
        skb = sock_alloc_send_skb(sk, len + self->max_header_size,
        skb = sock_alloc_send_skb(sk, len + self->max_header_size,
                                  msg->msg_flags & MSG_DONTWAIT, &err);
                                  msg->msg_flags & MSG_DONTWAIT, &err);
        if (!skb)
        if (!skb)
                return -ENOBUFS;
                return -ENOBUFS;
 
 
        skb_reserve(skb, self->max_header_size);
        skb_reserve(skb, self->max_header_size);
 
 
        IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
        IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
        asmptr = skb->h.raw = skb_put(skb, len);
        asmptr = skb->h.raw = skb_put(skb, len);
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
        memcpy_fromiovec(asmptr, msg->msg_iov, len);
 
 
        err = irlmp_connless_data_request(self->lsap, skb);
        err = irlmp_connless_data_request(self->lsap, skb);
        if (err) {
        if (err) {
                IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
                IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
                return err;
                return err;
        }
        }
        return len;
        return len;
}
}
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
 
 
/*
/*
 * Function irda_shutdown (sk, how)
 * Function irda_shutdown (sk, how)
 *
 *
 *
 *
 *
 *
 */
 */
static int irda_shutdown(struct socket *sock, int how)
static int irda_shutdown(struct socket *sock, int how)
{
{
        struct irda_sock *self;
        struct irda_sock *self;
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        IRDA_DEBUG(1, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(1, "%s(%p)\n", __FUNCTION__, self);
 
 
        sk->state       = TCP_CLOSE;
        sk->state       = TCP_CLOSE;
        sk->shutdown   |= SEND_SHUTDOWN;
        sk->shutdown   |= SEND_SHUTDOWN;
        sk->state_change(sk);
        sk->state_change(sk);
 
 
        if (self->iriap) {
        if (self->iriap) {
                iriap_close(self->iriap);
                iriap_close(self->iriap);
                self->iriap = NULL;
                self->iriap = NULL;
        }
        }
 
 
        if (self->tsap) {
        if (self->tsap) {
                irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
                irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
                irttp_close_tsap(self->tsap);
                irttp_close_tsap(self->tsap);
                self->tsap = NULL;
                self->tsap = NULL;
        }
        }
 
 
        /* A few cleanup so the socket look as good as new... */
        /* A few cleanup so the socket look as good as new... */
        self->rx_flow = self->tx_flow = FLOW_START;     /* needed ??? */
        self->rx_flow = self->tx_flow = FLOW_START;     /* needed ??? */
        self->daddr = DEV_ADDR_ANY;     /* Until we get re-connected */
        self->daddr = DEV_ADDR_ANY;     /* Until we get re-connected */
        self->saddr = 0x0;              /* so IrLMP assign us any link */
        self->saddr = 0x0;              /* so IrLMP assign us any link */
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_poll (file, sock, wait)
 * Function irda_poll (file, sock, wait)
 *
 *
 *
 *
 *
 *
 */
 */
static unsigned int irda_poll(struct file * file, struct socket *sock,
static unsigned int irda_poll(struct file * file, struct socket *sock,
                              poll_table *wait)
                              poll_table *wait)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        unsigned int mask;
        unsigned int mask;
        struct irda_sock *self;
        struct irda_sock *self;
 
 
        IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
        IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        poll_wait(file, sk->sleep, wait);
        poll_wait(file, sk->sleep, wait);
        mask = 0;
        mask = 0;
 
 
        /* Exceptional events? */
        /* Exceptional events? */
        if (sk->err)
        if (sk->err)
                mask |= POLLERR;
                mask |= POLLERR;
        if (sk->shutdown & RCV_SHUTDOWN) {
        if (sk->shutdown & RCV_SHUTDOWN) {
                IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
                IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
                mask |= POLLHUP;
                mask |= POLLHUP;
        }
        }
 
 
        /* Readable? */
        /* Readable? */
        if (!skb_queue_empty(&sk->receive_queue)) {
        if (!skb_queue_empty(&sk->receive_queue)) {
                IRDA_DEBUG(4, "Socket is readable\n");
                IRDA_DEBUG(4, "Socket is readable\n");
                mask |= POLLIN | POLLRDNORM;
                mask |= POLLIN | POLLRDNORM;
        }
        }
 
 
        /* Connection-based need to check for termination and startup */
        /* Connection-based need to check for termination and startup */
        switch (sk->type) {
        switch (sk->type) {
        case SOCK_STREAM:
        case SOCK_STREAM:
                if (sk->state == TCP_CLOSE) {
                if (sk->state == TCP_CLOSE) {
                        IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
                        IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
                        mask |= POLLHUP;
                        mask |= POLLHUP;
                }
                }
 
 
                if (sk->state == TCP_ESTABLISHED) {
                if (sk->state == TCP_ESTABLISHED) {
                        if ((self->tx_flow == FLOW_START) &&
                        if ((self->tx_flow == FLOW_START) &&
                            sock_writeable(sk))
                            sock_writeable(sk))
                        {
                        {
                                mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
                                mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
                        }
                        }
                }
                }
                break;
                break;
        case SOCK_SEQPACKET:
        case SOCK_SEQPACKET:
                if ((self->tx_flow == FLOW_START) &&
                if ((self->tx_flow == FLOW_START) &&
                    sock_writeable(sk))
                    sock_writeable(sk))
                {
                {
                        mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
                        mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
                }
                }
                break;
                break;
        case SOCK_DGRAM:
        case SOCK_DGRAM:
                if (sock_writeable(sk))
                if (sock_writeable(sk))
                        mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
                        mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
                break;
                break;
        default:
        default:
                break;
                break;
        }
        }
        return mask;
        return mask;
}
}
 
 
/*
/*
 * Function irda_ioctl (sock, cmd, arg)
 * Function irda_ioctl (sock, cmd, arg)
 *
 *
 *
 *
 *
 *
 */
 */
static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
 
 
        IRDA_DEBUG(4, "%s(), cmd=%#x\n", __FUNCTION__, cmd);
        IRDA_DEBUG(4, "%s(), cmd=%#x\n", __FUNCTION__, cmd);
 
 
        switch (cmd) {
        switch (cmd) {
        case TIOCOUTQ: {
        case TIOCOUTQ: {
                long amount;
                long amount;
                amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
                amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
                if (amount < 0)
                if (amount < 0)
                        amount = 0;
                        amount = 0;
                if (put_user(amount, (unsigned int *)arg))
                if (put_user(amount, (unsigned int *)arg))
                        return -EFAULT;
                        return -EFAULT;
                return 0;
                return 0;
        }
        }
 
 
        case TIOCINQ: {
        case TIOCINQ: {
                struct sk_buff *skb;
                struct sk_buff *skb;
                long amount = 0L;
                long amount = 0L;
                /* These two are safe on a single CPU system as only user tasks fiddle here */
                /* These two are safe on a single CPU system as only user tasks fiddle here */
                if ((skb = skb_peek(&sk->receive_queue)) != NULL)
                if ((skb = skb_peek(&sk->receive_queue)) != NULL)
                        amount = skb->len;
                        amount = skb->len;
                if (put_user(amount, (unsigned int *)arg))
                if (put_user(amount, (unsigned int *)arg))
                        return -EFAULT;
                        return -EFAULT;
                return 0;
                return 0;
        }
        }
 
 
        case SIOCGSTAMP:
        case SIOCGSTAMP:
                if (sk != NULL) {
                if (sk != NULL) {
                        if (sk->stamp.tv_sec == 0)
                        if (sk->stamp.tv_sec == 0)
                                return -ENOENT;
                                return -ENOENT;
                        if (copy_to_user((void *)arg, &sk->stamp,
                        if (copy_to_user((void *)arg, &sk->stamp,
                                         sizeof(struct timeval)))
                                         sizeof(struct timeval)))
                                return -EFAULT;
                                return -EFAULT;
                        return 0;
                        return 0;
                }
                }
                return -EINVAL;
                return -EINVAL;
 
 
        case SIOCGIFADDR:
        case SIOCGIFADDR:
        case SIOCSIFADDR:
        case SIOCSIFADDR:
        case SIOCGIFDSTADDR:
        case SIOCGIFDSTADDR:
        case SIOCSIFDSTADDR:
        case SIOCSIFDSTADDR:
        case SIOCGIFBRDADDR:
        case SIOCGIFBRDADDR:
        case SIOCSIFBRDADDR:
        case SIOCSIFBRDADDR:
        case SIOCGIFNETMASK:
        case SIOCGIFNETMASK:
        case SIOCSIFNETMASK:
        case SIOCSIFNETMASK:
        case SIOCGIFMETRIC:
        case SIOCGIFMETRIC:
        case SIOCSIFMETRIC:
        case SIOCSIFMETRIC:
                return -EINVAL;
                return -EINVAL;
        default:
        default:
                IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __FUNCTION__);
                IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __FUNCTION__);
                return dev_ioctl(cmd, (void *) arg);
                return dev_ioctl(cmd, (void *) arg);
        }
        }
 
 
        /*NOTREACHED*/
        /*NOTREACHED*/
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_setsockopt (sock, level, optname, optval, optlen)
 * Function irda_setsockopt (sock, level, optname, optval, optlen)
 *
 *
 *    Set some options for the socket
 *    Set some options for the socket
 *
 *
 */
 */
static int irda_setsockopt(struct socket *sock, int level, int optname,
static int irda_setsockopt(struct socket *sock, int level, int optname,
                           char *optval, int optlen)
                           char *optval, int optlen)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct irda_sock *self;
        struct irda_sock *self;
        struct irda_ias_set    *ias_opt;
        struct irda_ias_set    *ias_opt;
        struct ias_object      *ias_obj;
        struct ias_object      *ias_obj;
        struct ias_attrib *     ias_attr;       /* Attribute in IAS object */
        struct ias_attrib *     ias_attr;       /* Attribute in IAS object */
        int opt;
        int opt;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
        ASSERT(self != NULL, return -1;);
        ASSERT(self != NULL, return -1;);
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        if (level != SOL_IRLMP)
        if (level != SOL_IRLMP)
                return -ENOPROTOOPT;
                return -ENOPROTOOPT;
 
 
        switch (optname) {
        switch (optname) {
        case IRLMP_IAS_SET:
        case IRLMP_IAS_SET:
                /* The user want to add an attribute to an existing IAS object
                /* The user want to add an attribute to an existing IAS object
                 * (in the IAS database) or to create a new object with this
                 * (in the IAS database) or to create a new object with this
                 * attribute.
                 * attribute.
                 * We first query IAS to know if the object exist, and then
                 * We first query IAS to know if the object exist, and then
                 * create the right attribute...
                 * create the right attribute...
                 */
                 */
 
 
                if (optlen != sizeof(struct irda_ias_set))
                if (optlen != sizeof(struct irda_ias_set))
                        return -EINVAL;
                        return -EINVAL;
 
 
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                if (ias_opt == NULL)
                if (ias_opt == NULL)
                        return -ENOMEM;
                        return -ENOMEM;
 
 
                /* Copy query to the driver. */
                /* Copy query to the driver. */
                if (copy_from_user(ias_opt, (char *)optval, optlen)) {
                if (copy_from_user(ias_opt, (char *)optval, optlen)) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EFAULT;
                        return -EFAULT;
                }
                }
 
 
                /* Find the object we target.
                /* Find the object we target.
                 * If the user gives us an empty string, we use the object
                 * If the user gives us an empty string, we use the object
                 * associated with this socket. This will workaround
                 * associated with this socket. This will workaround
                 * duplicated class name - Jean II */
                 * duplicated class name - Jean II */
                if(ias_opt->irda_class_name[0] == '\0') {
                if(ias_opt->irda_class_name[0] == '\0') {
                        if(self->ias_obj == NULL) {
                        if(self->ias_obj == NULL) {
                                kfree(ias_opt);
                                kfree(ias_opt);
                                return -EINVAL;
                                return -EINVAL;
                        }
                        }
                        ias_obj = self->ias_obj;
                        ias_obj = self->ias_obj;
                } else
                } else
                        ias_obj = irias_find_object(ias_opt->irda_class_name);
                        ias_obj = irias_find_object(ias_opt->irda_class_name);
 
 
                /* Only ROOT can mess with the global IAS database.
                /* Only ROOT can mess with the global IAS database.
                 * Users can only add attributes to the object associated
                 * Users can only add attributes to the object associated
                 * with the socket they own - Jean II */
                 * with the socket they own - Jean II */
                if((!capable(CAP_NET_ADMIN)) &&
                if((!capable(CAP_NET_ADMIN)) &&
                   ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
                   ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EPERM;
                        return -EPERM;
                }
                }
 
 
                /* If the object doesn't exist, create it */
                /* If the object doesn't exist, create it */
                if(ias_obj == (struct ias_object *) NULL) {
                if(ias_obj == (struct ias_object *) NULL) {
                        /* Create a new object */
                        /* Create a new object */
                        ias_obj = irias_new_object(ias_opt->irda_class_name,
                        ias_obj = irias_new_object(ias_opt->irda_class_name,
                                                   jiffies);
                                                   jiffies);
                }
                }
 
 
                /* Do we have the attribute already ? */
                /* Do we have the attribute already ? */
                if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
                if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EINVAL;
                        return -EINVAL;
                }
                }
 
 
                /* Look at the type */
                /* Look at the type */
                switch(ias_opt->irda_attrib_type) {
                switch(ias_opt->irda_attrib_type) {
                case IAS_INTEGER:
                case IAS_INTEGER:
                        /* Add an integer attribute */
                        /* Add an integer attribute */
                        irias_add_integer_attrib(
                        irias_add_integer_attrib(
                                ias_obj,
                                ias_obj,
                                ias_opt->irda_attrib_name,
                                ias_opt->irda_attrib_name,
                                ias_opt->attribute.irda_attrib_int,
                                ias_opt->attribute.irda_attrib_int,
                                IAS_USER_ATTR);
                                IAS_USER_ATTR);
                        break;
                        break;
                case IAS_OCT_SEQ:
                case IAS_OCT_SEQ:
                        /* Check length */
                        /* Check length */
                        if(ias_opt->attribute.irda_attrib_octet_seq.len >
                        if(ias_opt->attribute.irda_attrib_octet_seq.len >
                           IAS_MAX_OCTET_STRING) {
                           IAS_MAX_OCTET_STRING) {
                                kfree(ias_opt);
                                kfree(ias_opt);
                                return -EINVAL;
                                return -EINVAL;
                        }
                        }
                        /* Add an octet sequence attribute */
                        /* Add an octet sequence attribute */
                        irias_add_octseq_attrib(
                        irias_add_octseq_attrib(
                              ias_obj,
                              ias_obj,
                              ias_opt->irda_attrib_name,
                              ias_opt->irda_attrib_name,
                              ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
                              ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
                              ias_opt->attribute.irda_attrib_octet_seq.len,
                              ias_opt->attribute.irda_attrib_octet_seq.len,
                              IAS_USER_ATTR);
                              IAS_USER_ATTR);
                        break;
                        break;
                case IAS_STRING:
                case IAS_STRING:
                        /* Should check charset & co */
                        /* Should check charset & co */
                        /* Check length */
                        /* Check length */
                        if(ias_opt->attribute.irda_attrib_string.len >
                        if(ias_opt->attribute.irda_attrib_string.len >
                           IAS_MAX_STRING) {
                           IAS_MAX_STRING) {
                                kfree(ias_opt);
                                kfree(ias_opt);
                                return -EINVAL;
                                return -EINVAL;
                        }
                        }
                        /* NULL terminate the string (avoid troubles) */
                        /* NULL terminate the string (avoid troubles) */
                        ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
                        ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
                        /* Add a string attribute */
                        /* Add a string attribute */
                        irias_add_string_attrib(
                        irias_add_string_attrib(
                                ias_obj,
                                ias_obj,
                                ias_opt->irda_attrib_name,
                                ias_opt->irda_attrib_name,
                                ias_opt->attribute.irda_attrib_string.string,
                                ias_opt->attribute.irda_attrib_string.string,
                                IAS_USER_ATTR);
                                IAS_USER_ATTR);
                        break;
                        break;
                default :
                default :
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EINVAL;
                        return -EINVAL;
                }
                }
                irias_insert_object(ias_obj);
                irias_insert_object(ias_obj);
                kfree(ias_opt);
                kfree(ias_opt);
                break;
                break;
        case IRLMP_IAS_DEL:
        case IRLMP_IAS_DEL:
                /* The user want to delete an object from our local IAS
                /* The user want to delete an object from our local IAS
                 * database. We just need to query the IAS, check is the
                 * database. We just need to query the IAS, check is the
                 * object is not owned by the kernel and delete it.
                 * object is not owned by the kernel and delete it.
                 */
                 */
 
 
                if (optlen != sizeof(struct irda_ias_set))
                if (optlen != sizeof(struct irda_ias_set))
                        return -EINVAL;
                        return -EINVAL;
 
 
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                if (ias_opt == NULL)
                if (ias_opt == NULL)
                        return -ENOMEM;
                        return -ENOMEM;
 
 
                /* Copy query to the driver. */
                /* Copy query to the driver. */
                if (copy_from_user(ias_opt, (char *)optval, optlen)) {
                if (copy_from_user(ias_opt, (char *)optval, optlen)) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EFAULT;
                        return -EFAULT;
                }
                }
 
 
                /* Find the object we target.
                /* Find the object we target.
                 * If the user gives us an empty string, we use the object
                 * If the user gives us an empty string, we use the object
                 * associated with this socket. This will workaround
                 * associated with this socket. This will workaround
                 * duplicated class name - Jean II */
                 * duplicated class name - Jean II */
                if(ias_opt->irda_class_name[0] == '\0')
                if(ias_opt->irda_class_name[0] == '\0')
                        ias_obj = self->ias_obj;
                        ias_obj = self->ias_obj;
                else
                else
                        ias_obj = irias_find_object(ias_opt->irda_class_name);
                        ias_obj = irias_find_object(ias_opt->irda_class_name);
                if(ias_obj == (struct ias_object *) NULL) {
                if(ias_obj == (struct ias_object *) NULL) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EINVAL;
                        return -EINVAL;
                }
                }
 
 
                /* Only ROOT can mess with the global IAS database.
                /* Only ROOT can mess with the global IAS database.
                 * Users can only del attributes from the object associated
                 * Users can only del attributes from the object associated
                 * with the socket they own - Jean II */
                 * with the socket they own - Jean II */
                if((!capable(CAP_NET_ADMIN)) &&
                if((!capable(CAP_NET_ADMIN)) &&
                   ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
                   ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EPERM;
                        return -EPERM;
                }
                }
 
 
                /* Find the attribute (in the object) we target */
                /* Find the attribute (in the object) we target */
                ias_attr = irias_find_attrib(ias_obj,
                ias_attr = irias_find_attrib(ias_obj,
                                             ias_opt->irda_attrib_name);
                                             ias_opt->irda_attrib_name);
                if(ias_attr == (struct ias_attrib *) NULL) {
                if(ias_attr == (struct ias_attrib *) NULL) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EINVAL;
                        return -EINVAL;
                }
                }
 
 
                /* Check is the user space own the object */
                /* Check is the user space own the object */
                if(ias_attr->value->owner != IAS_USER_ATTR) {
                if(ias_attr->value->owner != IAS_USER_ATTR) {
                        IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __FUNCTION__);
                        IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __FUNCTION__);
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EPERM;
                        return -EPERM;
                }
                }
 
 
                /* Remove the attribute (and maybe the object) */
                /* Remove the attribute (and maybe the object) */
                irias_delete_attrib(ias_obj, ias_attr);
                irias_delete_attrib(ias_obj, ias_attr);
                kfree(ias_opt);
                kfree(ias_opt);
                break;
                break;
        case IRLMP_MAX_SDU_SIZE:
        case IRLMP_MAX_SDU_SIZE:
                if (optlen < sizeof(int))
                if (optlen < sizeof(int))
                        return -EINVAL;
                        return -EINVAL;
 
 
                if (get_user(opt, (int *)optval))
                if (get_user(opt, (int *)optval))
                        return -EFAULT;
                        return -EFAULT;
 
 
                /* Only possible for a seqpacket service (TTP with SAR) */
                /* Only possible for a seqpacket service (TTP with SAR) */
                if (sk->type != SOCK_SEQPACKET) {
                if (sk->type != SOCK_SEQPACKET) {
                        IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n", __FUNCTION__, opt);
                        IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n", __FUNCTION__, opt);
                        self->max_sdu_size_rx = opt;
                        self->max_sdu_size_rx = opt;
                } else {
                } else {
                        WARNING("%s(), not allowed to set MAXSDUSIZE for this "
                        WARNING("%s(), not allowed to set MAXSDUSIZE for this "
                                "socket type!\n", __FUNCTION__);
                                "socket type!\n", __FUNCTION__);
                        return -ENOPROTOOPT;
                        return -ENOPROTOOPT;
                }
                }
                break;
                break;
        case IRLMP_HINTS_SET:
        case IRLMP_HINTS_SET:
                if (optlen < sizeof(int))
                if (optlen < sizeof(int))
                        return -EINVAL;
                        return -EINVAL;
 
 
                if (get_user(opt, (int *)optval))
                if (get_user(opt, (int *)optval))
                        return -EFAULT;
                        return -EFAULT;
 
 
                /* Unregister any old registration */
                /* Unregister any old registration */
                if (self->skey)
                if (self->skey)
                        irlmp_unregister_service(self->skey);
                        irlmp_unregister_service(self->skey);
 
 
                self->skey = irlmp_register_service((__u16) opt);
                self->skey = irlmp_register_service((__u16) opt);
                break;
                break;
        case IRLMP_HINT_MASK_SET:
        case IRLMP_HINT_MASK_SET:
                /* As opposed to the previous case which set the hint bits
                /* As opposed to the previous case which set the hint bits
                 * that we advertise, this one set the filter we use when
                 * that we advertise, this one set the filter we use when
                 * making a discovery (nodes which don't match any hint
                 * making a discovery (nodes which don't match any hint
                 * bit in the mask are not reported).
                 * bit in the mask are not reported).
                 */
                 */
                if (optlen < sizeof(int))
                if (optlen < sizeof(int))
                        return -EINVAL;
                        return -EINVAL;
 
 
                if (get_user(opt, (int *)optval))
                if (get_user(opt, (int *)optval))
                        return -EFAULT;
                        return -EFAULT;
 
 
                /* Set the new hint mask */
                /* Set the new hint mask */
                self->mask = (__u16) opt;
                self->mask = (__u16) opt;
                /* Mask out extension bits */
                /* Mask out extension bits */
                self->mask &= 0x7f7f;
                self->mask &= 0x7f7f;
                /* Check if no bits */
                /* Check if no bits */
                if(!self->mask)
                if(!self->mask)
                        self->mask = 0xFFFF;
                        self->mask = 0xFFFF;
 
 
                break;
                break;
        default:
        default:
                return -ENOPROTOOPT;
                return -ENOPROTOOPT;
        }
        }
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_extract_ias_value(ias_opt, ias_value)
 * Function irda_extract_ias_value(ias_opt, ias_value)
 *
 *
 *    Translate internal IAS value structure to the user space representation
 *    Translate internal IAS value structure to the user space representation
 *
 *
 * The external representation of IAS values, as we exchange them with
 * The external representation of IAS values, as we exchange them with
 * user space program is quite different from the internal representation,
 * user space program is quite different from the internal representation,
 * as stored in the IAS database (because we need a flat structure for
 * as stored in the IAS database (because we need a flat structure for
 * crossing kernel boundary).
 * crossing kernel boundary).
 * This function transform the former in the latter. We also check
 * This function transform the former in the latter. We also check
 * that the value type is valid.
 * that the value type is valid.
 */
 */
static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
                                  struct ias_value *ias_value)
                                  struct ias_value *ias_value)
{
{
        /* Look at the type */
        /* Look at the type */
        switch (ias_value->type) {
        switch (ias_value->type) {
        case IAS_INTEGER:
        case IAS_INTEGER:
                /* Copy the integer */
                /* Copy the integer */
                ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
                ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
                break;
                break;
        case IAS_OCT_SEQ:
        case IAS_OCT_SEQ:
                /* Set length */
                /* Set length */
                ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
                ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
                /* Copy over */
                /* Copy over */
                memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
                memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
                       ias_value->t.oct_seq, ias_value->len);
                       ias_value->t.oct_seq, ias_value->len);
                break;
                break;
        case IAS_STRING:
        case IAS_STRING:
                /* Set length */
                /* Set length */
                ias_opt->attribute.irda_attrib_string.len = ias_value->len;
                ias_opt->attribute.irda_attrib_string.len = ias_value->len;
                ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
                ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
                /* Copy over */
                /* Copy over */
                memcpy(ias_opt->attribute.irda_attrib_string.string,
                memcpy(ias_opt->attribute.irda_attrib_string.string,
                       ias_value->t.string, ias_value->len);
                       ias_value->t.string, ias_value->len);
                /* NULL terminate the string (avoid troubles) */
                /* NULL terminate the string (avoid troubles) */
                ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
                ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
                break;
                break;
        case IAS_MISSING:
        case IAS_MISSING:
        default :
        default :
                return -EINVAL;
                return -EINVAL;
        }
        }
 
 
        /* Copy type over */
        /* Copy type over */
        ias_opt->irda_attrib_type = ias_value->type;
        ias_opt->irda_attrib_type = ias_value->type;
 
 
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Function irda_getsockopt (sock, level, optname, optval, optlen)
 * Function irda_getsockopt (sock, level, optname, optval, optlen)
 *
 *
 *
 *
 *
 *
 */
 */
static int irda_getsockopt(struct socket *sock, int level, int optname,
static int irda_getsockopt(struct socket *sock, int level, int optname,
                           char *optval, int *optlen)
                           char *optval, int *optlen)
{
{
        struct sock *sk = sock->sk;
        struct sock *sk = sock->sk;
        struct irda_sock *self;
        struct irda_sock *self;
        struct irda_device_list list;
        struct irda_device_list list;
        struct irda_device_info *discoveries;
        struct irda_device_info *discoveries;
        struct irda_ias_set *   ias_opt;        /* IAS get/query params */
        struct irda_ias_set *   ias_opt;        /* IAS get/query params */
        struct ias_object *     ias_obj;        /* Object in IAS */
        struct ias_object *     ias_obj;        /* Object in IAS */
        struct ias_attrib *     ias_attr;       /* Attribute in IAS object */
        struct ias_attrib *     ias_attr;       /* Attribute in IAS object */
        int daddr = DEV_ADDR_ANY;       /* Dest address for IAS queries */
        int daddr = DEV_ADDR_ANY;       /* Dest address for IAS queries */
        int val = 0;
        int val = 0;
        int len = 0;
        int len = 0;
        int err;
        int err;
        int offset, total;
        int offset, total;
 
 
        self = sk->protinfo.irda;
        self = sk->protinfo.irda;
 
 
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
        IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
 
 
        if (level != SOL_IRLMP)
        if (level != SOL_IRLMP)
                return -ENOPROTOOPT;
                return -ENOPROTOOPT;
 
 
        if (get_user(len, optlen))
        if (get_user(len, optlen))
                return -EFAULT;
                return -EFAULT;
 
 
        if(len < 0)
        if(len < 0)
                return -EINVAL;
                return -EINVAL;
 
 
        switch (optname) {
        switch (optname) {
        case IRLMP_ENUMDEVICES:
        case IRLMP_ENUMDEVICES:
                /* Ask lmp for the current discovery log */
                /* Ask lmp for the current discovery log */
                discoveries = irlmp_get_discoveries(&list.len, self->mask,
                discoveries = irlmp_get_discoveries(&list.len, self->mask,
                                                    self->nslots);
                                                    self->nslots);
                /* Check if the we got some results */
                /* Check if the we got some results */
                if (discoveries == NULL)
                if (discoveries == NULL)
                        return -EAGAIN;         /* Didn't find any devices */
                        return -EAGAIN;         /* Didn't find any devices */
                err = 0;
                err = 0;
 
 
                /* Write total list length back to client */
                /* Write total list length back to client */
                if (copy_to_user(optval, &list,
                if (copy_to_user(optval, &list,
                                 sizeof(struct irda_device_list) -
                                 sizeof(struct irda_device_list) -
                                 sizeof(struct irda_device_info)))
                                 sizeof(struct irda_device_info)))
                        err = -EFAULT;
                        err = -EFAULT;
 
 
                /* Offset to first device entry */
                /* Offset to first device entry */
                offset = sizeof(struct irda_device_list) -
                offset = sizeof(struct irda_device_list) -
                        sizeof(struct irda_device_info);
                        sizeof(struct irda_device_info);
 
 
                /* Copy the list itself - watch for overflow */
                /* Copy the list itself - watch for overflow */
                if(list.len > 2048)
                if(list.len > 2048)
                {
                {
                        err = -EINVAL;
                        err = -EINVAL;
                        goto bed;
                        goto bed;
                }
                }
                total = offset + (list.len * sizeof(struct irda_device_info));
                total = offset + (list.len * sizeof(struct irda_device_info));
                if (total > len)
                if (total > len)
                        total = len;
                        total = len;
                if (copy_to_user(optval+offset, discoveries, total - offset))
                if (copy_to_user(optval+offset, discoveries, total - offset))
                        err = -EFAULT;
                        err = -EFAULT;
 
 
                /* Write total number of bytes used back to client */
                /* Write total number of bytes used back to client */
                if (put_user(total, optlen))
                if (put_user(total, optlen))
                        err = -EFAULT;
                        err = -EFAULT;
bed:
bed:
                /* Free up our buffer */
                /* Free up our buffer */
                kfree(discoveries);
                kfree(discoveries);
                if (err)
                if (err)
                        return err;
                        return err;
                break;
                break;
        case IRLMP_MAX_SDU_SIZE:
        case IRLMP_MAX_SDU_SIZE:
                val = self->max_data_size;
                val = self->max_data_size;
                len = sizeof(int);
                len = sizeof(int);
                if (put_user(len, optlen))
                if (put_user(len, optlen))
                        return -EFAULT;
                        return -EFAULT;
 
 
                if (copy_to_user(optval, &val, len))
                if (copy_to_user(optval, &val, len))
                        return -EFAULT;
                        return -EFAULT;
                break;
                break;
        case IRLMP_IAS_GET:
        case IRLMP_IAS_GET:
                /* The user want an object from our local IAS database.
                /* The user want an object from our local IAS database.
                 * We just need to query the IAS and return the value
                 * We just need to query the IAS and return the value
                 * that we found */
                 * that we found */
 
 
                /* Check that the user has allocated the right space for us */
                /* Check that the user has allocated the right space for us */
                if (len != sizeof(struct irda_ias_set))
                if (len != sizeof(struct irda_ias_set))
                        return -EINVAL;
                        return -EINVAL;
 
 
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                if (ias_opt == NULL)
                if (ias_opt == NULL)
                        return -ENOMEM;
                        return -ENOMEM;
 
 
                /* Copy query to the driver. */
                /* Copy query to the driver. */
                if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
                if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EFAULT;
                        return -EFAULT;
                }
                }
 
 
                /* Find the object we target.
                /* Find the object we target.
                 * If the user gives us an empty string, we use the object
                 * If the user gives us an empty string, we use the object
                 * associated with this socket. This will workaround
                 * associated with this socket. This will workaround
                 * duplicated class name - Jean II */
                 * duplicated class name - Jean II */
                if(ias_opt->irda_class_name[0] == '\0')
                if(ias_opt->irda_class_name[0] == '\0')
                        ias_obj = self->ias_obj;
                        ias_obj = self->ias_obj;
                else
                else
                        ias_obj = irias_find_object(ias_opt->irda_class_name);
                        ias_obj = irias_find_object(ias_opt->irda_class_name);
                if(ias_obj == (struct ias_object *) NULL) {
                if(ias_obj == (struct ias_object *) NULL) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EINVAL;
                        return -EINVAL;
                }
                }
 
 
                /* Find the attribute (in the object) we target */
                /* Find the attribute (in the object) we target */
                ias_attr = irias_find_attrib(ias_obj,
                ias_attr = irias_find_attrib(ias_obj,
                                             ias_opt->irda_attrib_name);
                                             ias_opt->irda_attrib_name);
                if(ias_attr == (struct ias_attrib *) NULL) {
                if(ias_attr == (struct ias_attrib *) NULL) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EINVAL;
                        return -EINVAL;
                }
                }
 
 
                /* Translate from internal to user structure */
                /* Translate from internal to user structure */
                err = irda_extract_ias_value(ias_opt, ias_attr->value);
                err = irda_extract_ias_value(ias_opt, ias_attr->value);
                if(err) {
                if(err) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return err;
                        return err;
                }
                }
 
 
                /* Copy reply to the user */
                /* Copy reply to the user */
                if (copy_to_user((char *)optval, (char *) ias_opt,
                if (copy_to_user((char *)optval, (char *) ias_opt,
                                 sizeof(struct irda_ias_set))) {
                                 sizeof(struct irda_ias_set))) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EFAULT;
                        return -EFAULT;
                }
                }
                /* Note : don't need to put optlen, we checked it */
                /* Note : don't need to put optlen, we checked it */
                kfree(ias_opt);
                kfree(ias_opt);
                break;
                break;
        case IRLMP_IAS_QUERY:
        case IRLMP_IAS_QUERY:
                /* The user want an object from a remote IAS database.
                /* The user want an object from a remote IAS database.
                 * We need to use IAP to query the remote database and
                 * We need to use IAP to query the remote database and
                 * then wait for the answer to come back. */
                 * then wait for the answer to come back. */
 
 
                /* Check that the user has allocated the right space for us */
                /* Check that the user has allocated the right space for us */
                if (len != sizeof(struct irda_ias_set))
                if (len != sizeof(struct irda_ias_set))
                        return -EINVAL;
                        return -EINVAL;
 
 
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
                if (ias_opt == NULL)
                if (ias_opt == NULL)
                        return -ENOMEM;
                        return -ENOMEM;
 
 
                /* Copy query to the driver. */
                /* Copy query to the driver. */
                if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
                if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EFAULT;
                        return -EFAULT;
                }
                }
 
 
                /* At this point, there are two cases...
                /* At this point, there are two cases...
                 * 1) the socket is connected - that's the easy case, we
                 * 1) the socket is connected - that's the easy case, we
                 *      just query the device we are connected to...
                 *      just query the device we are connected to...
                 * 2) the socket is not connected - the user doesn't want
                 * 2) the socket is not connected - the user doesn't want
                 *      to connect and/or may not have a valid service name
                 *      to connect and/or may not have a valid service name
                 *      (so can't create a fake connection). In this case,
                 *      (so can't create a fake connection). In this case,
                 *      we assume that the user pass us a valid destination
                 *      we assume that the user pass us a valid destination
                 *      address in the requesting structure...
                 *      address in the requesting structure...
                 */
                 */
                if(self->daddr != DEV_ADDR_ANY) {
                if(self->daddr != DEV_ADDR_ANY) {
                        /* We are connected - reuse known daddr */
                        /* We are connected - reuse known daddr */
                        daddr = self->daddr;
                        daddr = self->daddr;
                } else {
                } else {
                        /* We are not connected, we must specify a valid
                        /* We are not connected, we must specify a valid
                         * destination address */
                         * destination address */
                        daddr = ias_opt->daddr;
                        daddr = ias_opt->daddr;
                        if((!daddr) || (daddr == DEV_ADDR_ANY)) {
                        if((!daddr) || (daddr == DEV_ADDR_ANY)) {
                                kfree(ias_opt);
                                kfree(ias_opt);
                                return -EINVAL;
                                return -EINVAL;
                        }
                        }
                }
                }
 
 
                /* Check that we can proceed with IAP */
                /* Check that we can proceed with IAP */
                if (self->iriap) {
                if (self->iriap) {
                        WARNING("%s(), busy with a previous query\n", __FUNCTION__);
                        WARNING("%s(), busy with a previous query\n", __FUNCTION__);
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EBUSY;
                        return -EBUSY;
                }
                }
 
 
                self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
                self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
                                         irda_getvalue_confirm);
                                         irda_getvalue_confirm);
 
 
                /* Treat unexpected signals as disconnect */
                /* Treat unexpected signals as disconnect */
                self->errno = -EHOSTUNREACH;
                self->errno = -EHOSTUNREACH;
 
 
                /* Query remote LM-IAS */
                /* Query remote LM-IAS */
                iriap_getvaluebyclass_request(self->iriap,
                iriap_getvaluebyclass_request(self->iriap,
                                              self->saddr, daddr,
                                              self->saddr, daddr,
                                              ias_opt->irda_class_name,
                                              ias_opt->irda_class_name,
                                              ias_opt->irda_attrib_name);
                                              ias_opt->irda_attrib_name);
                /* Wait for answer (if not already failed) */
                /* Wait for answer (if not already failed) */
                if(self->iriap != NULL)
                if(self->iriap != NULL)
                        interruptible_sleep_on(&self->query_wait);
                        interruptible_sleep_on(&self->query_wait);
                /* Check what happened */
                /* Check what happened */
                if (self->errno)
                if (self->errno)
                {
                {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        /* Requested object/attribute doesn't exist */
                        /* Requested object/attribute doesn't exist */
                        if((self->errno == IAS_CLASS_UNKNOWN) ||
                        if((self->errno == IAS_CLASS_UNKNOWN) ||
                           (self->errno == IAS_ATTRIB_UNKNOWN))
                           (self->errno == IAS_ATTRIB_UNKNOWN))
                                return (-EADDRNOTAVAIL);
                                return (-EADDRNOTAVAIL);
                        else
                        else
                                return (-EHOSTUNREACH);
                                return (-EHOSTUNREACH);
                }
                }
 
 
                /* Translate from internal to user structure */
                /* Translate from internal to user structure */
                err = irda_extract_ias_value(ias_opt, self->ias_result);
                err = irda_extract_ias_value(ias_opt, self->ias_result);
                if (self->ias_result)
                if (self->ias_result)
                        irias_delete_value(self->ias_result);
                        irias_delete_value(self->ias_result);
                if (err) {
                if (err) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return err;
                        return err;
                }
                }
 
 
                /* Copy reply to the user */
                /* Copy reply to the user */
                if (copy_to_user((char *)optval, (char *) ias_opt,
                if (copy_to_user((char *)optval, (char *) ias_opt,
                                 sizeof(struct irda_ias_set))) {
                                 sizeof(struct irda_ias_set))) {
                        kfree(ias_opt);
                        kfree(ias_opt);
                        return -EFAULT;
                        return -EFAULT;
                }
                }
                /* Note : don't need to put optlen, we checked it */
                /* Note : don't need to put optlen, we checked it */
                kfree(ias_opt);
                kfree(ias_opt);
                break;
                break;
        case IRLMP_WAITDEVICE:
        case IRLMP_WAITDEVICE:
                /* This function is just another way of seeing life ;-)
                /* This function is just another way of seeing life ;-)
                 * IRLMP_ENUMDEVICES assumes that you have a static network,
                 * IRLMP_ENUMDEVICES assumes that you have a static network,
                 * and that you just want to pick one of the devices present.
                 * and that you just want to pick one of the devices present.
                 * On the other hand, in here we assume that no device is
                 * On the other hand, in here we assume that no device is
                 * present and that at some point in the future a device will
                 * present and that at some point in the future a device will
                 * come into range. When this device arrive, we just wake
                 * come into range. When this device arrive, we just wake
                 * up the caller, so that he has time to connect to it before
                 * up the caller, so that he has time to connect to it before
                 * the device goes away...
                 * the device goes away...
                 * Note : once the node has been discovered for more than a
                 * Note : once the node has been discovered for more than a
                 * few second, it won't trigger this function, unless it
                 * few second, it won't trigger this function, unless it
                 * goes away and come back changes its hint bits (so we
                 * goes away and come back changes its hint bits (so we
                 * might call it IRLMP_WAITNEWDEVICE).
                 * might call it IRLMP_WAITNEWDEVICE).
                 */
                 */
 
 
                /* Check that the user is passing us an int */
                /* Check that the user is passing us an int */
                if (len != sizeof(int))
                if (len != sizeof(int))
                        return -EINVAL;
                        return -EINVAL;
                /* Get timeout in ms (max time we block the caller) */
                /* Get timeout in ms (max time we block the caller) */
                if (get_user(val, (int *)optval))
                if (get_user(val, (int *)optval))
                        return -EFAULT;
                        return -EFAULT;
 
 
                /* Tell IrLMP we want to be notified */
                /* Tell IrLMP we want to be notified */
                irlmp_update_client(self->ckey, self->mask,
                irlmp_update_client(self->ckey, self->mask,
                                    irda_selective_discovery_indication,
                                    irda_selective_discovery_indication,
                                    NULL, (void *) self);
                                    NULL, (void *) self);
 
 
                /* Do some discovery (and also return cached results) */
                /* Do some discovery (and also return cached results) */
                irlmp_discovery_request(self->nslots);
                irlmp_discovery_request(self->nslots);
 
 
                /* Wait until a node is discovered */
                /* Wait until a node is discovered */
                if (!self->cachediscovery) {
                if (!self->cachediscovery) {
                        IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __FUNCTION__);
                        IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __FUNCTION__);
 
 
                        /* Set watchdog timer to expire in <val> ms. */
                        /* Set watchdog timer to expire in <val> ms. */
                        self->watchdog.function = irda_discovery_timeout;
                        self->watchdog.function = irda_discovery_timeout;
                        self->watchdog.data = (unsigned long) self;
                        self->watchdog.data = (unsigned long) self;
                        self->watchdog.expires = jiffies + (val * HZ/1000);
                        self->watchdog.expires = jiffies + (val * HZ/1000);
                        add_timer(&(self->watchdog));
                        add_timer(&(self->watchdog));
 
 
                        /* Wait for IR-LMP to call us back */
                        /* Wait for IR-LMP to call us back */
                        interruptible_sleep_on(&self->query_wait);
                        interruptible_sleep_on(&self->query_wait);
 
 
                        /* If watchdog is still activated, kill it! */
                        /* If watchdog is still activated, kill it! */
                        if(timer_pending(&(self->watchdog)))
                        if(timer_pending(&(self->watchdog)))
                                del_timer(&(self->watchdog));
                                del_timer(&(self->watchdog));
 
 
                        IRDA_DEBUG(1, "%s(), ...waking up !\n", __FUNCTION__);
                        IRDA_DEBUG(1, "%s(), ...waking up !\n", __FUNCTION__);
                }
                }
                else
                else
                        IRDA_DEBUG(1, "%s(), found immediately !\n", __FUNCTION__);
                        IRDA_DEBUG(1, "%s(), found immediately !\n", __FUNCTION__);
 
 
                /* Tell IrLMP that we have been notified */
                /* Tell IrLMP that we have been notified */
                irlmp_update_client(self->ckey, self->mask, NULL, NULL, NULL);
                irlmp_update_client(self->ckey, self->mask, NULL, NULL, NULL);
 
 
                /* Check if the we got some results */
                /* Check if the we got some results */
                if (!self->cachediscovery)
                if (!self->cachediscovery)
                        return -EAGAIN;         /* Didn't find any devices */
                        return -EAGAIN;         /* Didn't find any devices */
                /* Cleanup */
                /* Cleanup */
                self->cachediscovery = NULL;
                self->cachediscovery = NULL;
 
 
                /* Note : We don't return anything to the user.
                /* Note : We don't return anything to the user.
                 * We could return the device that triggered the wake up,
                 * We could return the device that triggered the wake up,
                 * but it's probably better to force the user to query
                 * but it's probably better to force the user to query
                 * the whole discovery log and let him pick one device...
                 * the whole discovery log and let him pick one device...
                 */
                 */
                break;
                break;
        default:
        default:
                return -ENOPROTOOPT;
                return -ENOPROTOOPT;
        }
        }
 
 
        return 0;
        return 0;
}
}
 
 
static struct net_proto_family irda_family_ops =
static struct net_proto_family irda_family_ops =
{
{
        PF_IRDA,
        PF_IRDA,
        irda_create
        irda_create
};
};
 
 
static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
        family:         PF_IRDA,
        family:         PF_IRDA,
 
 
        release:        irda_release,
        release:        irda_release,
        bind:           irda_bind,
        bind:           irda_bind,
        connect:        irda_connect,
        connect:        irda_connect,
        socketpair:     sock_no_socketpair,
        socketpair:     sock_no_socketpair,
        accept:         irda_accept,
        accept:         irda_accept,
        getname:        irda_getname,
        getname:        irda_getname,
        poll:           irda_poll,
        poll:           irda_poll,
        ioctl:          irda_ioctl,
        ioctl:          irda_ioctl,
        listen:         irda_listen,
        listen:         irda_listen,
        shutdown:       irda_shutdown,
        shutdown:       irda_shutdown,
        setsockopt:     irda_setsockopt,
        setsockopt:     irda_setsockopt,
        getsockopt:     irda_getsockopt,
        getsockopt:     irda_getsockopt,
        sendmsg:        irda_sendmsg,
        sendmsg:        irda_sendmsg,
        recvmsg:        irda_recvmsg_stream,
        recvmsg:        irda_recvmsg_stream,
        mmap:           sock_no_mmap,
        mmap:           sock_no_mmap,
        sendpage:       sock_no_sendpage,
        sendpage:       sock_no_sendpage,
};
};
 
 
static struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
static struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
        family:         PF_IRDA,
        family:         PF_IRDA,
 
 
        release:        irda_release,
        release:        irda_release,
        bind:           irda_bind,
        bind:           irda_bind,
        connect:        irda_connect,
        connect:        irda_connect,
        socketpair:     sock_no_socketpair,
        socketpair:     sock_no_socketpair,
        accept:         irda_accept,
        accept:         irda_accept,
        getname:        irda_getname,
        getname:        irda_getname,
        poll:           datagram_poll,
        poll:           datagram_poll,
        ioctl:          irda_ioctl,
        ioctl:          irda_ioctl,
        listen:         irda_listen,
        listen:         irda_listen,
        shutdown:       irda_shutdown,
        shutdown:       irda_shutdown,
        setsockopt:     irda_setsockopt,
        setsockopt:     irda_setsockopt,
        getsockopt:     irda_getsockopt,
        getsockopt:     irda_getsockopt,
        sendmsg:        irda_sendmsg,
        sendmsg:        irda_sendmsg,
        recvmsg:        irda_recvmsg_dgram,
        recvmsg:        irda_recvmsg_dgram,
        mmap:           sock_no_mmap,
        mmap:           sock_no_mmap,
        sendpage:       sock_no_sendpage,
        sendpage:       sock_no_sendpage,
};
};
 
 
static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
        family:         PF_IRDA,
        family:         PF_IRDA,
 
 
        release:        irda_release,
        release:        irda_release,
        bind:           irda_bind,
        bind:           irda_bind,
        connect:        irda_connect,
        connect:        irda_connect,
        socketpair:     sock_no_socketpair,
        socketpair:     sock_no_socketpair,
        accept:         irda_accept,
        accept:         irda_accept,
        getname:        irda_getname,
        getname:        irda_getname,
        poll:           datagram_poll,
        poll:           datagram_poll,
        ioctl:          irda_ioctl,
        ioctl:          irda_ioctl,
        listen:         irda_listen,
        listen:         irda_listen,
        shutdown:       irda_shutdown,
        shutdown:       irda_shutdown,
        setsockopt:     irda_setsockopt,
        setsockopt:     irda_setsockopt,
        getsockopt:     irda_getsockopt,
        getsockopt:     irda_getsockopt,
        sendmsg:        irda_sendmsg_dgram,
        sendmsg:        irda_sendmsg_dgram,
        recvmsg:        irda_recvmsg_dgram,
        recvmsg:        irda_recvmsg_dgram,
        mmap:           sock_no_mmap,
        mmap:           sock_no_mmap,
        sendpage:       sock_no_sendpage,
        sendpage:       sock_no_sendpage,
};
};
 
 
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
static struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
static struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
        family:         PF_IRDA,
        family:         PF_IRDA,
 
 
        release:        irda_release,
        release:        irda_release,
        bind:           irda_bind,
        bind:           irda_bind,
        connect:        sock_no_connect,
        connect:        sock_no_connect,
        socketpair:     sock_no_socketpair,
        socketpair:     sock_no_socketpair,
        accept:         sock_no_accept,
        accept:         sock_no_accept,
        getname:        irda_getname,
        getname:        irda_getname,
        poll:           datagram_poll,
        poll:           datagram_poll,
        ioctl:          irda_ioctl,
        ioctl:          irda_ioctl,
        listen:         sock_no_listen,
        listen:         sock_no_listen,
        shutdown:       irda_shutdown,
        shutdown:       irda_shutdown,
        setsockopt:     irda_setsockopt,
        setsockopt:     irda_setsockopt,
        getsockopt:     irda_getsockopt,
        getsockopt:     irda_getsockopt,
        sendmsg:        irda_sendmsg_ultra,
        sendmsg:        irda_sendmsg_ultra,
        recvmsg:        irda_recvmsg_dgram,
        recvmsg:        irda_recvmsg_dgram,
        mmap:           sock_no_mmap,
        mmap:           sock_no_mmap,
        sendpage:       sock_no_sendpage,
        sendpage:       sock_no_sendpage,
};
};
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
 
 
#include <linux/smp_lock.h>
#include <linux/smp_lock.h>
SOCKOPS_WRAP(irda_stream, PF_IRDA);
SOCKOPS_WRAP(irda_stream, PF_IRDA);
SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
SOCKOPS_WRAP(irda_dgram, PF_IRDA);
SOCKOPS_WRAP(irda_dgram, PF_IRDA);
#ifdef CONFIG_IRDA_ULTRA
#ifdef CONFIG_IRDA_ULTRA
SOCKOPS_WRAP(irda_ultra, PF_IRDA);
SOCKOPS_WRAP(irda_ultra, PF_IRDA);
#endif /* CONFIG_IRDA_ULTRA */
#endif /* CONFIG_IRDA_ULTRA */
 
 
/*
/*
 * Function irda_device_event (this, event, ptr)
 * Function irda_device_event (this, event, ptr)
 *
 *
 *    Called when a device is taken up or down
 *    Called when a device is taken up or down
 *
 *
 */
 */
static int irda_device_event(struct notifier_block *this, unsigned long event,
static int irda_device_event(struct notifier_block *this, unsigned long event,
                             void *ptr)
                             void *ptr)
{
{
        struct net_device *dev = (struct net_device *) ptr;
        struct net_device *dev = (struct net_device *) ptr;
 
 
        /* Reject non IrDA devices */
        /* Reject non IrDA devices */
        if (dev->type != ARPHRD_IRDA)
        if (dev->type != ARPHRD_IRDA)
                return NOTIFY_DONE;
                return NOTIFY_DONE;
 
 
        switch (event) {
        switch (event) {
        case NETDEV_UP:
        case NETDEV_UP:
                IRDA_DEBUG(3, "%s(), NETDEV_UP\n", __FUNCTION__);
                IRDA_DEBUG(3, "%s(), NETDEV_UP\n", __FUNCTION__);
                /* irda_dev_device_up(dev); */
                /* irda_dev_device_up(dev); */
                break;
                break;
        case NETDEV_DOWN:
        case NETDEV_DOWN:
                IRDA_DEBUG(3, "%s(), NETDEV_DOWN\n", __FUNCTION__);
                IRDA_DEBUG(3, "%s(), NETDEV_DOWN\n", __FUNCTION__);
                /* irda_kill_by_device(dev); */
                /* irda_kill_by_device(dev); */
                /* irda_rt_device_down(dev); */
                /* irda_rt_device_down(dev); */
                /* irda_dev_device_down(dev); */
                /* irda_dev_device_down(dev); */
                break;
                break;
        default:
        default:
                break;
                break;
        }
        }
 
 
        return NOTIFY_DONE;
        return NOTIFY_DONE;
}
}
 
 
static struct packet_type irda_packet_type =
static struct packet_type irda_packet_type =
{
{
        0,       /* MUTTER ntohs(ETH_P_IRDA),*/
        0,       /* MUTTER ntohs(ETH_P_IRDA),*/
        NULL,
        NULL,
        irlap_driver_rcv,
        irlap_driver_rcv,
        NULL,
        NULL,
        NULL,
        NULL,
};
};
 
 
static struct notifier_block irda_dev_notifier = {
static struct notifier_block irda_dev_notifier = {
        irda_device_event,
        irda_device_event,
        NULL,
        NULL,
        0
        0
};
};
 
 
/*
/*
 * Function irda_proc_modcount (inode, fill)
 * Function irda_proc_modcount (inode, fill)
 *
 *
 *    Use by the proc file system functions to prevent the irda module
 *    Use by the proc file system functions to prevent the irda module
 *    being removed while the use is standing in the net/irda directory
 *    being removed while the use is standing in the net/irda directory
 */
 */
void irda_proc_modcount(struct inode *inode, int fill)
void irda_proc_modcount(struct inode *inode, int fill)
{
{
#ifdef MODULE
#ifdef MODULE
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
        if (fill)
        if (fill)
                MOD_INC_USE_COUNT;
                MOD_INC_USE_COUNT;
        else
        else
                MOD_DEC_USE_COUNT;
                MOD_DEC_USE_COUNT;
#endif /* CONFIG_PROC_FS */
#endif /* CONFIG_PROC_FS */
#endif /* MODULE */
#endif /* MODULE */
}
}
 
 
/*
/*
 * Function irda_proto_init (pro)
 * Function irda_proto_init (pro)
 *
 *
 *    Initialize IrDA protocol layer
 *    Initialize IrDA protocol layer
 *
 *
 */
 */
int __init irda_proto_init(void)
int __init irda_proto_init(void)
{
{
        sock_register(&irda_family_ops);
        sock_register(&irda_family_ops);
 
 
        irda_packet_type.type = htons(ETH_P_IRDA);
        irda_packet_type.type = htons(ETH_P_IRDA);
        dev_add_pack(&irda_packet_type);
        dev_add_pack(&irda_packet_type);
 
 
        register_netdevice_notifier(&irda_dev_notifier);
        register_netdevice_notifier(&irda_dev_notifier);
 
 
        irda_init();
        irda_init();
#ifdef MODULE
#ifdef MODULE
        irda_device_init();  /* Called by init/main.c when non-modular */
        irda_device_init();  /* Called by init/main.c when non-modular */
#endif
#endif
        return 0;
        return 0;
}
}
#ifdef MODULE
#ifdef MODULE
module_init(irda_proto_init);   /* If non-module, called from init/main.c */
module_init(irda_proto_init);   /* If non-module, called from init/main.c */
#endif
#endif
 
 
/*
/*
 * Function irda_proto_cleanup (void)
 * Function irda_proto_cleanup (void)
 *
 *
 *    Remove IrDA protocol layer
 *    Remove IrDA protocol layer
 *
 *
 */
 */
#ifdef MODULE
#ifdef MODULE
void irda_proto_cleanup(void)
void irda_proto_cleanup(void)
{
{
        irda_packet_type.type = htons(ETH_P_IRDA);
        irda_packet_type.type = htons(ETH_P_IRDA);
        dev_remove_pack(&irda_packet_type);
        dev_remove_pack(&irda_packet_type);
 
 
        unregister_netdevice_notifier(&irda_dev_notifier);
        unregister_netdevice_notifier(&irda_dev_notifier);
 
 
        sock_unregister(PF_IRDA);
        sock_unregister(PF_IRDA);
        irda_cleanup();
        irda_cleanup();
 
 
        return;
        return;
}
}
module_exit(irda_proto_cleanup);
module_exit(irda_proto_cleanup);
 
 
MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
MODULE_DESCRIPTION("The Linux IrDA Protocol Subsystem");
MODULE_DESCRIPTION("The Linux IrDA Protocol Subsystem");
MODULE_LICENSE("GPL");
MODULE_LICENSE("GPL");
#ifdef CONFIG_IRDA_DEBUG
#ifdef CONFIG_IRDA_DEBUG
MODULE_PARM(irda_debug, "1l");
MODULE_PARM(irda_debug, "1l");
#endif
#endif
#endif /* MODULE */
#endif /* MODULE */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.