1 /*********************************************************************
2  *
3  * Filename:      irlmp.h
4  * Version:       0.9
5  * Description:   IrDA Link Management Protocol (LMP) layer
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Aug 17 20:54:32 1997
9  * Modified at:   Fri Dec 10 13:23:01 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  *
12  *     Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
13  *     All Rights Reserved.
14  *     Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
15  *
16  *     This program is free software; you can redistribute it and/or
17  *     modify it under the terms of the GNU General Public License as
18  *     published by the Free Software Foundation; either version 2 of
19  *     the License, or (at your option) any later version.
20  *
21  *     Neither Dag Brattli nor University of Troms� admit liability nor
22  *     provide warranty for any of this software. This material is
23  *     provided "AS-IS" and at no charge.
24  *
25  ********************************************************************/
26 
27 #ifndef IRLMP_H
28 #define IRLMP_H
29 
30 #include <asm/param.h>  /* for HZ */
31 
32 #include <linux/config.h>
33 #include <linux/types.h>
34 
35 #include <net/irda/irda.h>
36 #include <net/irda/qos.h>
37 #include <net/irda/irlap.h>
38 #include <net/irda/irlmp_event.h>
39 #include <net/irda/irqueue.h>
40 #include <net/irda/discovery.h>
41 
42 /* LSAP-SEL's */
43 #define LSAP_MASK     0x7f
44 #define LSAP_IAS      0x00
45 #define LSAP_ANY      0xff
46 #define LSAP_MAX      0x6f /* 0x70-0x7f are reserved */
47 #define LSAP_CONNLESS 0x70 /* Connectionless LSAP, mostly used for Ultra */
48 
49 #define DEV_ADDR_ANY  0xffffffff
50 
51 #define LMP_HEADER          2    /* Dest LSAP + Source LSAP */
52 #define LMP_CONTROL_HEADER  4
53 #define LMP_PID_HEADER      1    /* Used by Ultra */
54 #define LMP_MAX_HEADER      (LMP_CONTROL_HEADER+LAP_MAX_HEADER)
55 
56 #define LM_MAX_CONNECTIONS  10
57 
58 #define LM_IDLE_TIMEOUT     2*HZ /* 2 seconds for now */
59 
60 typedef enum {
61 	S_PNP,
62 	S_PDA,
63 	S_COMPUTER,
64 	S_PRINTER,
65 	S_MODEM,
66 	S_FAX,
67 	S_LAN,
68 	S_TELEPHONY,
69 	S_COMM,
70 	S_OBEX,
71 	S_ANY,
72 	S_END,
73 } SERVICE;
74 
75 typedef void (*DISCOVERY_CALLBACK1) (discovery_t *, DISCOVERY_MODE, void *);
76 typedef void (*DISCOVERY_CALLBACK2) (hashbin_t *, void *);
77 
78 typedef struct {
79 	irda_queue_t queue; /* Must be first */
80 
81 	__u16 hints; /* Hint bits */
82 } irlmp_service_t;
83 
84 typedef struct {
85 	irda_queue_t queue; /* Must be first */
86 
87 	__u16 hint_mask;
88 
89 	DISCOVERY_CALLBACK1 disco_callback;	/* Selective discovery */
90 	DISCOVERY_CALLBACK1 expir_callback;	/* Selective expiration */
91 	void *priv;                /* Used to identify client */
92 } irlmp_client_t;
93 
94 struct lap_cb; /* Forward decl. */
95 
96 /*
97  *  Information about each logical LSAP connection
98  */
99 struct lsap_cb {
100 	irda_queue_t queue;      /* Must be first */
101 	magic_t magic;
102 
103 	int  connected;
104 	int  persistent;
105 
106 	__u8 slsap_sel;   /* Source (this) LSAP address */
107 	__u8 dlsap_sel;   /* Destination LSAP address (if connected) */
108 #ifdef CONFIG_IRDA_ULTRA
109 	__u8 pid;         /* Used by connectionless LSAP */
110 #endif /* CONFIG_IRDA_ULTRA */
111 	struct sk_buff *conn_skb; /* Store skb here while connecting */
112 
113 	struct timer_list watchdog_timer;
114 
115 	IRLMP_STATE     lsap_state;  /* Connection state */
116 	notify_t        notify;      /* Indication/Confirm entry points */
117 	struct qos_info qos;         /* QoS for this connection */
118 
119 	struct lap_cb *lap; /* Pointer to LAP connection structure */
120 };
121 
122 /*
123  *  Information about each registred IrLAP layer
124  */
125 struct lap_cb {
126 	irda_queue_t queue; /* Must be first */
127 	magic_t magic;
128 
129 	int reason;    /* LAP disconnect reason */
130 
131 	IRLMP_STATE lap_state;
132 
133 	struct irlap_cb *irlap;   /* Instance of IrLAP layer */
134 	hashbin_t *lsaps;         /* LSAP associated with this link */
135 	struct lsap_cb *flow_next;	/* Next lsap to be polled for Tx */
136 
137 	__u8  caddr;  /* Connection address */
138  	__u32 saddr;  /* Source device address */
139  	__u32 daddr;  /* Destination device address */
140 
141 	struct qos_info *qos;  /* LAP QoS for this session */
142 	struct timer_list idle_timer;
143 };
144 
145 /*
146  *  Used for caching the last slsap->dlsap->handle mapping
147  */
148 typedef struct {
149 	int valid;
150 
151 	__u8 slsap_sel;
152 	__u8 dlsap_sel;
153 	struct lsap_cb *lsap;
154 } CACHE_ENTRY;
155 
156 /*
157  *  Main structure for IrLMP
158  */
159 struct irlmp_cb {
160 	magic_t magic;
161 
162 	__u8 conflict_flag;
163 
164 	discovery_t discovery_cmd; /* Discovery command to use by IrLAP */
165 	discovery_t discovery_rsp; /* Discovery response to use by IrLAP */
166 
167 	int free_lsap_sel;
168 
169 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
170 	CACHE_ENTRY cache;  /* Caching last slsap->dlsap->handle mapping */
171 #endif
172 	struct timer_list discovery_timer;
173 
174  	hashbin_t *links;         /* IrLAP connection table */
175 	hashbin_t *unconnected_lsaps;
176  	hashbin_t *clients;
177 	hashbin_t *services;
178 
179 	hashbin_t *cachelog;	/* Current discovery log */
180 	spinlock_t log_lock;	/* discovery log spinlock */
181 
182 	int running;
183 
184 	__u16_host_order hints; /* Hint bits */
185 };
186 
187 /* Prototype declarations */
188 int  irlmp_init(void);
189 void irlmp_cleanup(void);
190 struct lsap_cb *irlmp_open_lsap(__u8 slsap, notify_t *notify, __u8 pid);
191 void irlmp_close_lsap( struct lsap_cb *self);
192 
193 __u16 irlmp_service_to_hint(int service);
194 __u32 irlmp_register_service(__u16 hints);
195 int irlmp_unregister_service(__u32 handle);
196 __u32 irlmp_register_client(__u16 hint_mask, DISCOVERY_CALLBACK1 disco_clb,
197 			    DISCOVERY_CALLBACK1 expir_clb, void *priv);
198 int irlmp_unregister_client(__u32 handle);
199 int irlmp_update_client(__u32 handle, __u16 hint_mask,
200 			DISCOVERY_CALLBACK1 disco_clb,
201 			DISCOVERY_CALLBACK1 expir_clb, void *priv);
202 
203 void irlmp_register_link(struct irlap_cb *, __u32 saddr, notify_t *);
204 void irlmp_unregister_link(__u32 saddr);
205 
206 int  irlmp_connect_request(struct lsap_cb *, __u8 dlsap_sel,
207 			   __u32 saddr, __u32 daddr,
208 			   struct qos_info *, struct sk_buff *);
209 void irlmp_connect_indication(struct lsap_cb *self, struct sk_buff *skb);
210 int  irlmp_connect_response(struct lsap_cb *, struct sk_buff *);
211 void irlmp_connect_confirm(struct lsap_cb *, struct sk_buff *);
212 struct lsap_cb *irlmp_dup(struct lsap_cb *self, void *instance);
213 
214 void irlmp_disconnect_indication(struct lsap_cb *self, LM_REASON reason,
215 				 struct sk_buff *userdata);
216 int  irlmp_disconnect_request(struct lsap_cb *, struct sk_buff *userdata);
217 
218 void irlmp_discovery_confirm(hashbin_t *discovery_log, DISCOVERY_MODE);
219 void irlmp_discovery_request(int nslots);
220 struct irda_device_info *irlmp_get_discoveries(int *pn, __u16 mask, int nslots);
221 void irlmp_do_expiry(void);
222 void irlmp_do_discovery(int nslots);
223 discovery_t *irlmp_get_discovery_response(void);
224 void irlmp_discovery_expiry(discovery_t *expiry);
225 
226 int  irlmp_data_request(struct lsap_cb *, struct sk_buff *);
227 void irlmp_data_indication(struct lsap_cb *, struct sk_buff *);
228 
229 int  irlmp_udata_request(struct lsap_cb *, struct sk_buff *);
230 void irlmp_udata_indication(struct lsap_cb *, struct sk_buff *);
231 
232 #ifdef CONFIG_IRDA_ULTRA
233 int  irlmp_connless_data_request(struct lsap_cb *, struct sk_buff *);
234 void irlmp_connless_data_indication(struct lsap_cb *, struct sk_buff *);
235 #endif /* CONFIG_IRDA_ULTRA */
236 
237 void irlmp_status_request(void);
238 void irlmp_status_indication(struct lap_cb *, LINK_STATUS link, LOCK_STATUS lock);
239 void irlmp_flow_indication(struct lap_cb *self, LOCAL_FLOW flow);
240 
241 int  irlmp_slsap_inuse(__u8 slsap);
242 __u8 irlmp_find_free_slsap(void);
243 LM_REASON irlmp_convert_lap_reason(LAP_REASON);
244 
245 __u32 irlmp_get_saddr(struct lsap_cb *self);
246 __u32 irlmp_get_daddr(struct lsap_cb *self);
247 
248 extern char *lmp_reasons[];
249 extern int sysctl_discovery_timeout;
250 extern int sysctl_discovery_slots;
251 extern int sysctl_discovery;
252 extern int sysctl_lap_keepalive_time;	/* in ms, default is LM_IDLE_TIMEOUT */
253 extern struct irlmp_cb *irlmp;
254 
irlmp_get_cachelog(void)255 static inline hashbin_t *irlmp_get_cachelog(void) { return irlmp->cachelog; }
256 
257 /* Check if LAP queue is full.
258  * Used by IrTTP for low control, see comments in irlap.h - Jean II */
irlmp_lap_tx_queue_full(struct lsap_cb * self)259 static inline int irlmp_lap_tx_queue_full(struct lsap_cb *self)
260 {
261 	if (self == NULL)
262 		return 0;
263 	if (self->lap == NULL)
264 		return 0;
265 	if (self->lap->irlap == NULL)
266 		return 0;
267 
268 	return(IRLAP_GET_TX_QUEUE_LEN(self->lap->irlap) >= LAP_HIGH_THRESHOLD);
269 }
270 
271 /* After doing a irlmp_dup(), this get one of the two socket back into
272  * a state where it's waiting incomming connections.
273  * Note : this can be used *only* if the socket is not yet connected
274  * (i.e. NO irlmp_connect_response() done on this socket).
275  * - Jean II */
irlmp_listen(struct lsap_cb * self)276 static inline void irlmp_listen(struct lsap_cb *self)
277 {
278 	self->dlsap_sel = LSAP_ANY;
279 	self->lap = NULL;
280 	self->lsap_state = LSAP_DISCONNECTED;
281 	/* Started when we received the LM_CONNECT_INDICATION */
282 	del_timer(&self->watchdog_timer);
283 }
284 
285 #endif
286