1/*********************************************************************
2 *
3 * Filename:      discovery.c
4 * Version:       0.1
5 * Description:   Routines for handling discoveries at the IrLMP layer
6 * Status:        Experimental.
7 * Author:        Dag Brattli <dagb@cs.uit.no>
8 * Created at:    Tue Apr  6 15:33:50 1999
9 * Modified at:   Sat Oct  9 17:11:31 1999
10 * Modified by:   Dag Brattli <dagb@cs.uit.no>
11 * Modified at:   Fri May 28  3:11 CST 1999
12 * Modified by:   Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
13 *
14 *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
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 *     This program is distributed in the hope that it will be useful,
22 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
23 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 *     GNU General Public License for more details.
25 *
26 *     You should have received a copy of the GNU General Public License
27 *     along with this program; if not, write to the Free Software
28 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 *     MA 02111-1307 USA
30 *
31 ********************************************************************/
32
33#include <linux/string.h>
34#include <linux/socket.h>
35#include <linux/fs.h>
36#include <linux/seq_file.h>
37#include <linux/slab.h>
38
39#include <net/irda/irda.h>
40#include <net/irda/irlmp.h>
41
42#include <net/irda/discovery.h>
43
44#include <asm/unaligned.h>
45
46/*
47 * Function irlmp_add_discovery (cachelog, discovery)
48 *
49 *    Add a new discovery to the cachelog, and remove any old discoveries
50 *    from the same device
51 *
52 * Note : we try to preserve the time this device was *first* discovered
53 * (as opposed to the time of last discovery used for cleanup). This is
54 * used by clients waiting for discovery events to tell if the device
55 * discovered is "new" or just the same old one. They can't rely there
56 * on a binary flag (new/old), because not all discovery events are
57 * propagated to them, and they might not always listen, so they would
58 * miss some new devices popping up...
59 * Jean II
60 */
61void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
62{
63	discovery_t *discovery, *node;
64	unsigned long flags;
65
66	/* Set time of first discovery if node is new (see below) */
67	new->firststamp = new->timestamp;
68
69	spin_lock_irqsave(&cachelog->hb_spinlock, flags);
70
71	/*
72	 * Remove all discoveries of devices that has previously been
73	 * discovered on the same link with the same name (info), or the
74	 * same daddr. We do this since some devices (mostly PDAs) change
75	 * their device address between every discovery.
76	 */
77	discovery = (discovery_t *) hashbin_get_first(cachelog);
78	while (discovery != NULL ) {
79		node = discovery;
80
81		/* Be sure to stay one item ahead */
82		discovery = (discovery_t *) hashbin_get_next(cachelog);
83
84		if ((node->data.saddr == new->data.saddr) &&
85		    ((node->data.daddr == new->data.daddr) ||
86		     (strcmp(node->data.info, new->data.info) == 0)))
87		{
88			/* This discovery is a previous discovery
89			 * from the same device, so just remove it
90			 */
91			hashbin_remove_this(cachelog, (irda_queue_t *) node);
92			/* Check if hints bits are unchanged */
93			if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
94				/* Set time of first discovery for this node */
95				new->firststamp = node->firststamp;
96			kfree(node);
97		}
98	}
99
100	/* Insert the new and updated version */
101	hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
102
103	spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
104}
105
106/*
107 * Function irlmp_add_discovery_log (cachelog, log)
108 *
109 *    Merge a disovery log into the cachelog.
110 *
111 */
112void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
113{
114	discovery_t *discovery;
115
116	IRDA_DEBUG(4, "%s()\n", __func__);
117
118	/*
119	 *  If log is missing this means that IrLAP was unable to perform the
120	 *  discovery, so restart discovery again with just the half timeout
121	 *  of the normal one.
122	 */
123	/* Well... It means that there was nobody out there - Jean II */
124	if (log == NULL) {
125		/* irlmp_start_discovery_timer(irlmp, 150); */
126		return;
127	}
128
129	/*
130	 * Locking : we are the only owner of this discovery log, so
131	 * no need to lock it.
132	 * We just need to lock the global log in irlmp_add_discovery().
133	 */
134	discovery = (discovery_t *) hashbin_remove_first(log);
135	while (discovery != NULL) {
136		irlmp_add_discovery(cachelog, discovery);
137
138		discovery = (discovery_t *) hashbin_remove_first(log);
139	}
140
141	/* Delete the now empty log */
142	hashbin_delete(log, (FREE_FUNC) kfree);
143}
144
145/*
146 * Function irlmp_expire_discoveries (log, saddr, force)
147 *
148 *    Go through all discoveries and expire all that has stayed too long
149 *
150 * Note : this assume that IrLAP won't change its saddr, which
151 * currently is a valid assumption...
152 */
153void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
154{
155	discovery_t *		discovery;
156	discovery_t *		curr;
157	unsigned long		flags;
158	discinfo_t *		buffer = NULL;
159	int			n;		/* Size of the full log */
160	int			i = 0;		/* How many we expired */
161
162	IRDA_ASSERT(log != NULL, return;);
163	IRDA_DEBUG(4, "%s()\n", __func__);
164
165	spin_lock_irqsave(&log->hb_spinlock, flags);
166
167	discovery = (discovery_t *) hashbin_get_first(log);
168	while (discovery != NULL) {
169		/* Be sure to be one item ahead */
170		curr = discovery;
171		discovery = (discovery_t *) hashbin_get_next(log);
172
173		/* Test if it's time to expire this discovery */
174		if ((curr->data.saddr == saddr) &&
175		    (force ||
176		     ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
177		{
178			/* Create buffer as needed.
179			 * As this function get called a lot and most time
180			 * we don't have anything to put in the log (we are
181			 * quite picky), we can save a lot of overhead
182			 * by not calling kmalloc. Jean II */
183			if(buffer == NULL) {
184				/* Create the client specific buffer */
185				n = HASHBIN_GET_SIZE(log);
186				buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
187				if (buffer == NULL) {
188					spin_unlock_irqrestore(&log->hb_spinlock, flags);
189					return;
190				}
191
192			}
193
194			/* Copy discovery information */
195			memcpy(&(buffer[i]), &(curr->data),
196			       sizeof(discinfo_t));
197			i++;
198
199			/* Remove it from the log */
200			curr = hashbin_remove_this(log, (irda_queue_t *) curr);
201			kfree(curr);
202		}
203	}
204
205	/* Drop the spinlock before calling the higher layers, as
206	 * we can't guarantee they won't call us back and create a
207	 * deadlock. We will work on our own private data, so we
208	 * don't care to be interrupted. - Jean II */
209	spin_unlock_irqrestore(&log->hb_spinlock, flags);
210
211	if(buffer == NULL)
212		return;
213
214	/* Tell IrLMP and registered clients about it */
215	irlmp_discovery_expiry(buffer, i);
216
217	/* Free up our buffer */
218	kfree(buffer);
219}
220
221
222/*
223 * Function irlmp_copy_discoveries (log, pn, mask)
224 *
225 *    Copy all discoveries in a buffer
226 *
227 * This function implement a safe way for lmp clients to access the
228 * discovery log. The basic problem is that we don't want the log
229 * to change (add/remove) while the client is reading it. If the
230 * lmp client manipulate directly the hashbin, he is sure to get
231 * into troubles...
232 * The idea is that we copy all the current discovery log in a buffer
233 * which is specific to the client and pass this copy to him. As we
234 * do this operation with the spinlock grabbed, we are safe...
235 * Note : we don't want those clients to grab the spinlock, because
236 * we have no control on how long they will hold it...
237 * Note : we choose to copy the log in "struct irda_device_info" to
238 * save space...
239 * Note : the client must kfree himself() the log...
240 * Jean II
241 */
242struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
243						__u16 mask, int old_entries)
244{
245	discovery_t *		discovery;
246	unsigned long		flags;
247	discinfo_t *		buffer = NULL;
248	int			j_timeout = (sysctl_discovery_timeout * HZ);
249	int			n;		/* Size of the full log */
250	int			i = 0;		/* How many we picked */
251
252	IRDA_ASSERT(pn != NULL, return NULL;);
253	IRDA_ASSERT(log != NULL, return NULL;);
254
255	/* Save spin lock */
256	spin_lock_irqsave(&log->hb_spinlock, flags);
257
258	discovery = (discovery_t *) hashbin_get_first(log);
259	while (discovery != NULL) {
260		/* Mask out the ones we don't want :
261		 * We want to match the discovery mask, and to get only
262		 * the most recent one (unless we want old ones) */
263		if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
264		    ((old_entries) ||
265		     ((jiffies - discovery->firststamp) < j_timeout))) {
266			/* Create buffer as needed.
267			 * As this function get called a lot and most time
268			 * we don't have anything to put in the log (we are
269			 * quite picky), we can save a lot of overhead
270			 * by not calling kmalloc. Jean II */
271			if(buffer == NULL) {
272				/* Create the client specific buffer */
273				n = HASHBIN_GET_SIZE(log);
274				buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
275				if (buffer == NULL) {
276					spin_unlock_irqrestore(&log->hb_spinlock, flags);
277					return NULL;
278				}
279
280			}
281
282			/* Copy discovery information */
283			memcpy(&(buffer[i]), &(discovery->data),
284			       sizeof(discinfo_t));
285			i++;
286		}
287		discovery = (discovery_t *) hashbin_get_next(log);
288	}
289
290	spin_unlock_irqrestore(&log->hb_spinlock, flags);
291
292	/* Get the actual number of device in the buffer and return */
293	*pn = i;
294	return(buffer);
295}
296
297#ifdef CONFIG_PROC_FS
298static inline discovery_t *discovery_seq_idx(loff_t pos)
299
300{
301	discovery_t *discovery;
302
303	for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
304	     discovery != NULL;
305	     discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
306		if (pos-- == 0)
307			break;
308	}
309
310	return discovery;
311}
312
313static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
314{
315	spin_lock_irq(&irlmp->cachelog->hb_spinlock);
316	return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
317}
318
319static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
320{
321	++*pos;
322	return (v == SEQ_START_TOKEN)
323		? (void *) hashbin_get_first(irlmp->cachelog)
324		: (void *) hashbin_get_next(irlmp->cachelog);
325}
326
327static void discovery_seq_stop(struct seq_file *seq, void *v)
328{
329	spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
330}
331
332static int discovery_seq_show(struct seq_file *seq, void *v)
333{
334	if (v == SEQ_START_TOKEN)
335		seq_puts(seq, "IrLMP: Discovery log:\n\n");
336	else {
337		const discovery_t *discovery = v;
338
339		seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
340			   discovery->data.info,
341			   discovery->data.hints[0],
342			   discovery->data.hints[1]);
343		seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
344			       discovery->data.saddr,
345			       discovery->data.daddr);
346
347		seq_putc(seq, '\n');
348	}
349	return 0;
350}
351
352static const struct seq_operations discovery_seq_ops = {
353	.start  = discovery_seq_start,
354	.next   = discovery_seq_next,
355	.stop   = discovery_seq_stop,
356	.show   = discovery_seq_show,
357};
358
359static int discovery_seq_open(struct inode *inode, struct file *file)
360{
361	IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
362
363	return seq_open(file, &discovery_seq_ops);
364}
365
366const struct file_operations discovery_seq_fops = {
367	.owner		= THIS_MODULE,
368	.open           = discovery_seq_open,
369	.read           = seq_read,
370	.llseek         = seq_lseek,
371	.release	= seq_release,
372};
373#endif
374