/*********************************************************************
 *                
 * Filename:      irlmp.h
 * Version:       0.9
 * Description:   IrDA Link Management Protocol (LMP) layer
 * Status:        Experimental.
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Sun Aug 17 20:54:32 1997
 * Modified at:   Fri Apr 23 09:15:07 1999
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * 
 *     Copyright (c) 1998 Dag Brattli <dagb@cs.uit.no>, All Rights Reserved.
 *     
 *     This program is free software; you can redistribute it and/or 
 *     modify it under the terms of the GNU General Public License as 
 *     published by the Free Software Foundation; either version 2 of 
 *     the License, or (at your option) any later version.
 *
 *     Neither Dag Brattli nor University of Tromsų admit liability nor
 *     provide warranty for any of this software. This material is 
 *     provided "AS-IS" and at no charge.
 *
 ********************************************************************/

#ifndef IRLMP_H
#define IRLMP_H

#include <asm/param.h>  /* for HZ */

#include <linux/config.h>
#include <linux/types.h>

#include <net/irda/irda.h>
#include <net/irda/qos.h>
#include <net/irda/irlap.h>
#include <net/irda/irlmp_event.h>
#include <net/irda/irqueue.h>
#include <net/irda/discovery.h>

/* LSAP-SEL's */
#define LSAP_MASK     0x7f
#define LSAP_IAS      0x00
#define LSAP_ANY      0xff

#define DEV_ADDR_ANY  0xffffffff

/* Predefined LSAPs used by the various servers */
#define TSAP_IRLAN    0x05
#define LSAP_IRLPT    0x06
#define TSAP_IROBEX   0x07
#define TSAP_IRCOMM   0x08

#define LMP_HEADER          2    /* Dest LSAP + Source LSAP */
#define LMP_CONTROL_HEADER  4
#define LMP_MAX_HEADER      (LAP_HEADER+LMP_HEADER)

#define LM_MAX_CONNECTIONS  10

#define LM_IDLE_TIMEOUT     2*HZ /* 2 seconds for now */

typedef enum {
	S_PNP,
	S_PDA,
	S_COMPUTER,
	S_PRINTER,
	S_MODEM,
	S_FAX,
	S_LAN,
	S_TELEPHONY,
	S_COMM,
	S_OBEX,
	S_ANY,
	S_END,
} SERVICE;

typedef void (*DISCOVERY_CALLBACK1) (discovery_t *);
typedef void (*DISCOVERY_CALLBACK2) (hashbin_t *);

typedef struct {
	QUEUE queue; /* Must be first */

	__u16 hints; /* Hint bits */
} irlmp_service_t;

typedef struct {
	QUEUE queue; /* Must be first */

	__u16 hint_mask;

	DISCOVERY_CALLBACK1 callback1;
	DISCOVERY_CALLBACK2 callback2;
} irlmp_client_t;

struct lap_cb; /* Forward decl. */

/*
 *  Information about each logical LSAP connection
 */
struct lsap_cb {
	QUEUE queue;      /* Must be first */

	int   magic;

	int   connected;
	int   persistent;

	struct irda_statistics stats;

	__u8  slsap_sel;   /* Source (this) LSAP address */
	__u8  dlsap_sel;   /* Destination LSAP address (if connected) */

	struct sk_buff *tmp_skb; /* Store skb here while connecting */

	struct timer_list watchdog_timer;

	IRLMP_STATE     lsap_state;  /* Connection state */
	struct notify_t notify;      /* Indication/Confirm entry points */
	struct qos_info qos;         /* QoS for this connection */

	struct lap_cb *lap; /* Pointer to LAP connection structure */
};

/*
 *  Information about each registred IrLAP layer
 */
struct lap_cb {
	QUEUE queue;      /* Must be first */

	int magic;
	int reason;    /* LAP disconnect reason */

	IRLMP_STATE lap_state;

	struct irlap_cb *irlap;    /* Instance of IrLAP layer */
	hashbin_t *lsaps;         /* LSAP associated with this link */

	__u8  caddr;  /* Connection address */
 	__u32 saddr;  /* Source device address */
 	__u32 daddr;  /* Destination device address */
	
	struct qos_info *qos;  /* LAP QoS for this session */
	struct timer_list idle_timer;
};

/*
 *  Used for caching the last slsap->dlsap->handle mapping
 */
typedef struct {
	int valid;

	__u8 slsap_sel;
	__u8 dlsap_sel;
	struct lsap_cb *lsap;
} CACHE_ENTRY;

/*
 *  Main structure for IrLMP
 */
struct irlmp_cb {
	int magic;

	__u8 conflict_flag;
	
	discovery_t discovery_cmd; /* Discovery command to use by IrLAP */
	discovery_t discovery_rsp; /* Discovery response to use by IrLAP */

	int free_lsap_sel;

#ifdef CONFIG_IRDA_CACHE_LAST_LSAP
	CACHE_ENTRY cache;  /* Caching last slsap->dlsap->handle mapping */
#endif
	struct timer_list discovery_timer;

 	hashbin_t *links;         /* IrLAP connection table */
	hashbin_t *unconnected_lsaps;
 	hashbin_t *clients;
	hashbin_t *services;

	hashbin_t *cachelog;
	int running;

	spinlock_t lock;

	__u16_host_order hints; /* Hint bits */
};

/* Prototype declarations */
int  irlmp_init(void);
void irlmp_cleanup(void);
struct lsap_cb *irlmp_open_lsap( __u8 slsap, struct notify_t *notify);
void irlmp_close_lsap( struct lsap_cb *self);

__u16 irlmp_service_to_hint(int service);
__u32 irlmp_register_service(__u16 hints);
int irlmp_unregister_service(__u32 handle);
__u32 irlmp_register_client(__u16 hint_mask, DISCOVERY_CALLBACK1 callback1,
			    DISCOVERY_CALLBACK2 callback2);
int irlmp_unregister_client(__u32 handle);
int irlmp_update_client(__u32 handle, __u16 hint_mask, 
			DISCOVERY_CALLBACK1, DISCOVERY_CALLBACK2);

void irlmp_register_link(struct irlap_cb *, __u32 saddr, struct notify_t *);
void irlmp_unregister_link(__u32 saddr);

int  irlmp_connect_request(struct lsap_cb *, __u8 dlsap_sel, 
			   __u32 saddr, __u32 daddr,
			   struct qos_info *, struct sk_buff *);
void irlmp_connect_indication(struct lsap_cb *self, struct sk_buff *skb);
void irlmp_connect_response(struct lsap_cb *, struct sk_buff *);
void irlmp_connect_confirm(struct lsap_cb *, struct sk_buff *);
struct lsap_cb *irlmp_dup(struct lsap_cb *self, void *instance);

void irlmp_disconnect_indication(struct lsap_cb *self, LM_REASON reason, 
				 struct sk_buff *userdata);
void irlmp_disconnect_request(struct lsap_cb *, struct sk_buff *userdata);

void irlmp_discovery_confirm(hashbin_t *discovery_log);
void irlmp_discovery_request(int nslots);
void irlmp_do_discovery(int nslots);
discovery_t *irlmp_get_discovery_response(void);

void irlmp_data_request(struct lsap_cb *, struct sk_buff *);
inline void irlmp_udata_request(struct lsap_cb *, struct sk_buff *);
inline void irlmp_data_indication(struct lsap_cb *, struct sk_buff *);
inline void irlmp_udata_indication(struct lsap_cb *, struct sk_buff *);

void irlmp_status_request(void);
void irlmp_status_indication(LINK_STATUS link, LOCK_STATUS lock);

int  irlmp_slsap_inuse(__u8 slsap);
__u8 irlmp_find_free_slsap(void);
LM_REASON irlmp_convert_lap_reason(LAP_REASON);

__u32 irlmp_get_saddr(struct lsap_cb *self);
__u32 irlmp_get_daddr(struct lsap_cb *self);

extern char *lmp_reasons[];
extern int sysctl_discovery_slots;
extern int sysctl_discovery;
extern struct irlmp_cb *irlmp;

static inline hashbin_t *irlmp_get_cachelog(void) { return irlmp->cachelog; }

#endif
