blob: c1c8ae939126027c5d3ddb6192d2da88677da419 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090015 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090020 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * 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.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090025 *
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * MA 02111-1307 USA
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 ********************************************************************/
32
33#include <linux/string.h>
34#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050035#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <net/irda/irda.h>
40#include <net/irda/irlmp.h>
41
42#include <net/irda/discovery.h>
43
Graf Yang33222382008-05-13 23:25:57 -070044#include <asm/unaligned.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
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
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090071 /*
72 * Remove all discoveries of devices that has previously been
73 * discovered on the same link with the same name (info), or the
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * 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) &&
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090085 ((node->data.daddr == new->data.daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 (strcmp(node->data.info, new->data.info) == 0)))
87 {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090088 /* This discovery is a previous discovery
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * 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 */
Graf Yang33222382008-05-13 23:25:57 -070093 if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* 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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800116 IRDA_DEBUG(4, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
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 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* 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;);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800163 IRDA_DEBUG(4, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
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);
Jesper Juhla51482b2005-11-08 09:41:34 -0800201 kfree(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
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
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200208 * don't care to be interrupted. - Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 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#if 0
222/*
223 * Function irlmp_dump_discoveries (log)
224 *
225 * Print out all discoveries in log
226 *
227 */
228void irlmp_dump_discoveries(hashbin_t *log)
229{
230 discovery_t *discovery;
231
232 IRDA_ASSERT(log != NULL, return;);
233
234 discovery = (discovery_t *) hashbin_get_first(log);
235 while (discovery != NULL) {
236 IRDA_DEBUG(0, "Discovery:\n");
237 IRDA_DEBUG(0, " daddr=%08x\n", discovery->data.daddr);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900238 IRDA_DEBUG(0, " saddr=%08x\n", discovery->data.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 IRDA_DEBUG(0, " nickname=%s\n", discovery->data.info);
240
241 discovery = (discovery_t *) hashbin_get_next(log);
242 }
243}
244#endif
245
246/*
247 * Function irlmp_copy_discoveries (log, pn, mask)
248 *
249 * Copy all discoveries in a buffer
250 *
251 * This function implement a safe way for lmp clients to access the
252 * discovery log. The basic problem is that we don't want the log
253 * to change (add/remove) while the client is reading it. If the
254 * lmp client manipulate directly the hashbin, he is sure to get
255 * into troubles...
256 * The idea is that we copy all the current discovery log in a buffer
257 * which is specific to the client and pass this copy to him. As we
258 * do this operation with the spinlock grabbed, we are safe...
259 * Note : we don't want those clients to grab the spinlock, because
260 * we have no control on how long they will hold it...
261 * Note : we choose to copy the log in "struct irda_device_info" to
262 * save space...
263 * Note : the client must kfree himself() the log...
264 * Jean II
265 */
266struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
267 __u16 mask, int old_entries)
268{
269 discovery_t * discovery;
270 unsigned long flags;
271 discinfo_t * buffer = NULL;
272 int j_timeout = (sysctl_discovery_timeout * HZ);
273 int n; /* Size of the full log */
274 int i = 0; /* How many we picked */
275
276 IRDA_ASSERT(pn != NULL, return NULL;);
277 IRDA_ASSERT(log != NULL, return NULL;);
278
279 /* Save spin lock */
280 spin_lock_irqsave(&log->hb_spinlock, flags);
281
282 discovery = (discovery_t *) hashbin_get_first(log);
283 while (discovery != NULL) {
284 /* Mask out the ones we don't want :
285 * We want to match the discovery mask, and to get only
286 * the most recent one (unless we want old ones) */
Graf Yang33222382008-05-13 23:25:57 -0700287 if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 ((old_entries) ||
Graf Yang33222382008-05-13 23:25:57 -0700289 ((jiffies - discovery->firststamp) < j_timeout))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* Create buffer as needed.
291 * As this function get called a lot and most time
292 * we don't have anything to put in the log (we are
293 * quite picky), we can save a lot of overhead
294 * by not calling kmalloc. Jean II */
295 if(buffer == NULL) {
296 /* Create the client specific buffer */
297 n = HASHBIN_GET_SIZE(log);
298 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
299 if (buffer == NULL) {
300 spin_unlock_irqrestore(&log->hb_spinlock, flags);
301 return NULL;
302 }
303
304 }
305
306 /* Copy discovery information */
307 memcpy(&(buffer[i]), &(discovery->data),
308 sizeof(discinfo_t));
309 i++;
310 }
311 discovery = (discovery_t *) hashbin_get_next(log);
312 }
313
314 spin_unlock_irqrestore(&log->hb_spinlock, flags);
315
316 /* Get the actual number of device in the buffer and return */
317 *pn = i;
318 return(buffer);
319}
320
321#ifdef CONFIG_PROC_FS
322static inline discovery_t *discovery_seq_idx(loff_t pos)
323
324{
325 discovery_t *discovery;
326
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900327 for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 discovery != NULL;
329 discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
330 if (pos-- == 0)
331 break;
332 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return discovery;
335}
336
337static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
338{
339 spin_lock_irq(&irlmp->cachelog->hb_spinlock);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900340 return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
344{
345 ++*pos;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900346 return (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ? (void *) hashbin_get_first(irlmp->cachelog)
348 : (void *) hashbin_get_next(irlmp->cachelog);
349}
350
351static void discovery_seq_stop(struct seq_file *seq, void *v)
352{
353 spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
354}
355
356static int discovery_seq_show(struct seq_file *seq, void *v)
357{
358 if (v == SEQ_START_TOKEN)
359 seq_puts(seq, "IrLMP: Discovery log:\n\n");
360 else {
361 const discovery_t *discovery = v;
362
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900363 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 discovery->data.info,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900365 discovery->data.hints[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 discovery->data.hints[1]);
367#if 0
368 if ( discovery->data.hints[0] & HINT_PNP)
369 seq_puts(seq, "PnP Compatible ");
370 if ( discovery->data.hints[0] & HINT_PDA)
371 seq_puts(seq, "PDA/Palmtop ");
372 if ( discovery->data.hints[0] & HINT_COMPUTER)
373 seq_puts(seq, "Computer ");
374 if ( discovery->data.hints[0] & HINT_PRINTER)
375 seq_puts(seq, "Printer ");
376 if ( discovery->data.hints[0] & HINT_MODEM)
377 seq_puts(seq, "Modem ");
378 if ( discovery->data.hints[0] & HINT_FAX)
379 seq_puts(seq, "Fax ");
380 if ( discovery->data.hints[0] & HINT_LAN)
381 seq_puts(seq, "LAN Access ");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if ( discovery->data.hints[1] & HINT_TELEPHONY)
384 seq_puts(seq, "Telephony ");
385 if ( discovery->data.hints[1] & HINT_FILE_SERVER)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900386 seq_puts(seq, "File Server ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if ( discovery->data.hints[1] & HINT_COMM)
388 seq_puts(seq, "IrCOMM ");
389 if ( discovery->data.hints[1] & HINT_OBEX)
390 seq_puts(seq, "IrOBEX ");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900391#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
393 discovery->data.saddr,
394 discovery->data.daddr);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 seq_putc(seq, '\n');
397 }
398 return 0;
399}
400
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700401static const struct seq_operations discovery_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .start = discovery_seq_start,
403 .next = discovery_seq_next,
404 .stop = discovery_seq_stop,
405 .show = discovery_seq_show,
406};
407
408static int discovery_seq_open(struct inode *inode, struct file *file)
409{
410 IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
411
412 return seq_open(file, &discovery_seq_ops);
413}
414
Arjan van de Ven9a321442007-02-12 00:55:35 -0800415const struct file_operations discovery_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .owner = THIS_MODULE,
417 .open = discovery_seq_open,
418 .read = seq_read,
419 .llseek = seq_lseek,
420 .release = seq_release,
421};
422#endif