1 /*********************************************************************
3 * Filename: discovery.c
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>
14 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
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.
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.
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,
31 ********************************************************************/
33 #include <linux/string.h>
34 #include <linux/socket.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/export.h>
40 #include <net/irda/irda.h>
41 #include <net/irda/irlmp.h>
43 #include <net/irda/discovery.h>
45 #include <asm/unaligned.h>
48 * Function irlmp_add_discovery (cachelog, discovery)
50 * Add a new discovery to the cachelog, and remove any old discoveries
51 * from the same device
53 * Note : we try to preserve the time this device was *first* discovered
54 * (as opposed to the time of last discovery used for cleanup). This is
55 * used by clients waiting for discovery events to tell if the device
56 * discovered is "new" or just the same old one. They can't rely there
57 * on a binary flag (new/old), because not all discovery events are
58 * propagated to them, and they might not always listen, so they would
59 * miss some new devices popping up...
62 void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
64 discovery_t *discovery, *node;
67 /* Set time of first discovery if node is new (see below) */
68 new->firststamp = new->timestamp;
70 spin_lock_irqsave(&cachelog->hb_spinlock, flags);
73 * Remove all discoveries of devices that has previously been
74 * discovered on the same link with the same name (info), or the
75 * same daddr. We do this since some devices (mostly PDAs) change
76 * their device address between every discovery.
78 discovery = (discovery_t *) hashbin_get_first(cachelog);
79 while (discovery != NULL ) {
82 /* Be sure to stay one item ahead */
83 discovery = (discovery_t *) hashbin_get_next(cachelog);
85 if ((node->data.saddr == new->data.saddr) &&
86 ((node->data.daddr == new->data.daddr) ||
87 (strcmp(node->data.info, new->data.info) == 0)))
89 /* This discovery is a previous discovery
90 * from the same device, so just remove it
92 hashbin_remove_this(cachelog, (irda_queue_t *) node);
93 /* Check if hints bits are unchanged */
94 if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
95 /* Set time of first discovery for this node */
96 new->firststamp = node->firststamp;
101 /* Insert the new and updated version */
102 hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
104 spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
108 * Function irlmp_add_discovery_log (cachelog, log)
110 * Merge a disovery log into the cachelog.
113 void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
115 discovery_t *discovery;
117 IRDA_DEBUG(4, "%s()\n", __func__);
120 * If log is missing this means that IrLAP was unable to perform the
121 * discovery, so restart discovery again with just the half timeout
124 /* Well... It means that there was nobody out there - Jean II */
126 /* irlmp_start_discovery_timer(irlmp, 150); */
131 * Locking : we are the only owner of this discovery log, so
132 * no need to lock it.
133 * We just need to lock the global log in irlmp_add_discovery().
135 discovery = (discovery_t *) hashbin_remove_first(log);
136 while (discovery != NULL) {
137 irlmp_add_discovery(cachelog, discovery);
139 discovery = (discovery_t *) hashbin_remove_first(log);
142 /* Delete the now empty log */
143 hashbin_delete(log, (FREE_FUNC) kfree);
147 * Function irlmp_expire_discoveries (log, saddr, force)
149 * Go through all discoveries and expire all that has stayed too long
151 * Note : this assume that IrLAP won't change its saddr, which
152 * currently is a valid assumption...
154 void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
156 discovery_t * discovery;
159 discinfo_t * buffer = NULL;
160 int n; /* Size of the full log */
161 int i = 0; /* How many we expired */
163 IRDA_ASSERT(log != NULL, return;);
164 IRDA_DEBUG(4, "%s()\n", __func__);
166 spin_lock_irqsave(&log->hb_spinlock, flags);
168 discovery = (discovery_t *) hashbin_get_first(log);
169 while (discovery != NULL) {
170 /* Be sure to be one item ahead */
172 discovery = (discovery_t *) hashbin_get_next(log);
174 /* Test if it's time to expire this discovery */
175 if ((curr->data.saddr == saddr) &&
177 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
179 /* Create buffer as needed.
180 * As this function get called a lot and most time
181 * we don't have anything to put in the log (we are
182 * quite picky), we can save a lot of overhead
183 * by not calling kmalloc. Jean II */
185 /* Create the client specific buffer */
186 n = HASHBIN_GET_SIZE(log);
187 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
188 if (buffer == NULL) {
189 spin_unlock_irqrestore(&log->hb_spinlock, flags);
195 /* Copy discovery information */
196 memcpy(&(buffer[i]), &(curr->data),
200 /* Remove it from the log */
201 curr = hashbin_remove_this(log, (irda_queue_t *) curr);
206 /* Drop the spinlock before calling the higher layers, as
207 * we can't guarantee they won't call us back and create a
208 * deadlock. We will work on our own private data, so we
209 * don't care to be interrupted. - Jean II */
210 spin_unlock_irqrestore(&log->hb_spinlock, flags);
215 /* Tell IrLMP and registered clients about it */
216 irlmp_discovery_expiry(buffer, i);
218 /* Free up our buffer */
224 * Function irlmp_dump_discoveries (log)
226 * Print out all discoveries in log
229 void irlmp_dump_discoveries(hashbin_t *log)
231 discovery_t *discovery;
233 IRDA_ASSERT(log != NULL, return;);
235 discovery = (discovery_t *) hashbin_get_first(log);
236 while (discovery != NULL) {
237 IRDA_DEBUG(0, "Discovery:\n");
238 IRDA_DEBUG(0, " daddr=%08x\n", discovery->data.daddr);
239 IRDA_DEBUG(0, " saddr=%08x\n", discovery->data.saddr);
240 IRDA_DEBUG(0, " nickname=%s\n", discovery->data.info);
242 discovery = (discovery_t *) hashbin_get_next(log);
248 * Function irlmp_copy_discoveries (log, pn, mask)
250 * Copy all discoveries in a buffer
252 * This function implement a safe way for lmp clients to access the
253 * discovery log. The basic problem is that we don't want the log
254 * to change (add/remove) while the client is reading it. If the
255 * lmp client manipulate directly the hashbin, he is sure to get
257 * The idea is that we copy all the current discovery log in a buffer
258 * which is specific to the client and pass this copy to him. As we
259 * do this operation with the spinlock grabbed, we are safe...
260 * Note : we don't want those clients to grab the spinlock, because
261 * we have no control on how long they will hold it...
262 * Note : we choose to copy the log in "struct irda_device_info" to
264 * Note : the client must kfree himself() the log...
267 struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
268 __u16 mask, int old_entries)
270 discovery_t * discovery;
272 discinfo_t * buffer = NULL;
273 int j_timeout = (sysctl_discovery_timeout * HZ);
274 int n; /* Size of the full log */
275 int i = 0; /* How many we picked */
277 IRDA_ASSERT(pn != NULL, return NULL;);
278 IRDA_ASSERT(log != NULL, return NULL;);
281 spin_lock_irqsave(&log->hb_spinlock, flags);
283 discovery = (discovery_t *) hashbin_get_first(log);
284 while (discovery != NULL) {
285 /* Mask out the ones we don't want :
286 * We want to match the discovery mask, and to get only
287 * the most recent one (unless we want old ones) */
288 if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
290 ((jiffies - discovery->firststamp) < j_timeout))) {
291 /* Create buffer as needed.
292 * As this function get called a lot and most time
293 * we don't have anything to put in the log (we are
294 * quite picky), we can save a lot of overhead
295 * by not calling kmalloc. Jean II */
297 /* Create the client specific buffer */
298 n = HASHBIN_GET_SIZE(log);
299 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
300 if (buffer == NULL) {
301 spin_unlock_irqrestore(&log->hb_spinlock, flags);
307 /* Copy discovery information */
308 memcpy(&(buffer[i]), &(discovery->data),
312 discovery = (discovery_t *) hashbin_get_next(log);
315 spin_unlock_irqrestore(&log->hb_spinlock, flags);
317 /* Get the actual number of device in the buffer and return */
322 #ifdef CONFIG_PROC_FS
323 static inline discovery_t *discovery_seq_idx(loff_t pos)
326 discovery_t *discovery;
328 for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
330 discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
338 static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
340 spin_lock_irq(&irlmp->cachelog->hb_spinlock);
341 return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
344 static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
347 return (v == SEQ_START_TOKEN)
348 ? (void *) hashbin_get_first(irlmp->cachelog)
349 : (void *) hashbin_get_next(irlmp->cachelog);
352 static void discovery_seq_stop(struct seq_file *seq, void *v)
354 spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
357 static int discovery_seq_show(struct seq_file *seq, void *v)
359 if (v == SEQ_START_TOKEN)
360 seq_puts(seq, "IrLMP: Discovery log:\n\n");
362 const discovery_t *discovery = v;
364 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
365 discovery->data.info,
366 discovery->data.hints[0],
367 discovery->data.hints[1]);
369 if ( discovery->data.hints[0] & HINT_PNP)
370 seq_puts(seq, "PnP Compatible ");
371 if ( discovery->data.hints[0] & HINT_PDA)
372 seq_puts(seq, "PDA/Palmtop ");
373 if ( discovery->data.hints[0] & HINT_COMPUTER)
374 seq_puts(seq, "Computer ");
375 if ( discovery->data.hints[0] & HINT_PRINTER)
376 seq_puts(seq, "Printer ");
377 if ( discovery->data.hints[0] & HINT_MODEM)
378 seq_puts(seq, "Modem ");
379 if ( discovery->data.hints[0] & HINT_FAX)
380 seq_puts(seq, "Fax ");
381 if ( discovery->data.hints[0] & HINT_LAN)
382 seq_puts(seq, "LAN Access ");
384 if ( discovery->data.hints[1] & HINT_TELEPHONY)
385 seq_puts(seq, "Telephony ");
386 if ( discovery->data.hints[1] & HINT_FILE_SERVER)
387 seq_puts(seq, "File Server ");
388 if ( discovery->data.hints[1] & HINT_COMM)
389 seq_puts(seq, "IrCOMM ");
390 if ( discovery->data.hints[1] & HINT_OBEX)
391 seq_puts(seq, "IrOBEX ");
393 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
394 discovery->data.saddr,
395 discovery->data.daddr);
402 static const struct seq_operations discovery_seq_ops = {
403 .start = discovery_seq_start,
404 .next = discovery_seq_next,
405 .stop = discovery_seq_stop,
406 .show = discovery_seq_show,
409 static int discovery_seq_open(struct inode *inode, struct file *file)
411 IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
413 return seq_open(file, &discovery_seq_ops);
416 const struct file_operations discovery_seq_fops = {
417 .owner = THIS_MODULE,
418 .open = discovery_seq_open,
421 .release = seq_release,