Blame view

net/irda/discovery.c 12.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*********************************************************************
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
2
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
   * Filename:      discovery.c
   * Version:       0.1
   * Description:   Routines for handling discoveries at the IrLMP layer
   * Status:        Experimental.
   * Author:        Dag Brattli <dagb@cs.uit.no>
   * Created at:    Tue Apr  6 15:33:50 1999
   * Modified at:   Sat Oct  9 17:11:31 1999
   * Modified by:   Dag Brattli <dagb@cs.uit.no>
   * Modified at:   Fri May 28  3:11 CST 1999
   * Modified by:   Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
13
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
   *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
15
16
17
18
   *
   *     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
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
   *     the License, or (at your option) any later version.
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
20
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
   *     This program is distributed in the hope that it will be useful,
   *     but WITHOUT ANY WARRANTY; without even the implied warranty of
   *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   *     GNU General Public License for more details.
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
25
26
27
28
   *
   *     You should have received a copy of the GNU General Public License
   *     along with this program; if not, write to the Free Software
   *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
   *     MA 02111-1307 USA
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
30
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
   ********************************************************************/
  
  #include <linux/string.h>
  #include <linux/socket.h>
d7fe0f241   Al Viro   [PATCH] severing ...
35
  #include <linux/fs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  #include <linux/seq_file.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
37
  #include <linux/slab.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
38
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
  
  #include <net/irda/irda.h>
  #include <net/irda/irlmp.h>
  
  #include <net/irda/discovery.h>
332223831   Graf Yang   irda: Fix a misal...
44
  #include <asm/unaligned.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  /*
   * Function irlmp_add_discovery (cachelog, discovery)
   *
   *    Add a new discovery to the cachelog, and remove any old discoveries
   *    from the same device
   *
   * Note : we try to preserve the time this device was *first* discovered
   * (as opposed to the time of last discovery used for cleanup). This is
   * used by clients waiting for discovery events to tell if the device
   * discovered is "new" or just the same old one. They can't rely there
   * on a binary flag (new/old), because not all discovery events are
   * propagated to them, and they might not always listen, so they would
   * miss some new devices popping up...
   * Jean II
   */
  void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
  {
  	discovery_t *discovery, *node;
  	unsigned long flags;
  
  	/* Set time of first discovery if node is new (see below) */
  	new->firststamp = new->timestamp;
  
  	spin_lock_irqsave(&cachelog->hb_spinlock, flags);
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
69
70
71
  	/*
  	 * Remove all discoveries of devices that has previously been
  	 * discovered on the same link with the same name (info), or the
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
77
78
79
80
81
82
  	 * same daddr. We do this since some devices (mostly PDAs) change
  	 * their device address between every discovery.
  	 */
  	discovery = (discovery_t *) hashbin_get_first(cachelog);
  	while (discovery != NULL ) {
  		node = discovery;
  
  		/* Be sure to stay one item ahead */
  		discovery = (discovery_t *) hashbin_get_next(cachelog);
  
  		if ((node->data.saddr == new->data.saddr) &&
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
83
  		    ((node->data.daddr == new->data.daddr) ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
  		     (strcmp(node->data.info, new->data.info) == 0)))
  		{
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
86
  			/* This discovery is a previous discovery
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
89
90
  			 * from the same device, so just remove it
  			 */
  			hashbin_remove_this(cachelog, (irda_queue_t *) node);
  			/* Check if hints bits are unchanged */
332223831   Graf Yang   irda: Fix a misal...
91
  			if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  				/* Set time of first discovery for this node */
  				new->firststamp = node->firststamp;
  			kfree(node);
  		}
  	}
  
  	/* Insert the new and updated version */
  	hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
  
  	spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
  }
  
  /*
   * Function irlmp_add_discovery_log (cachelog, log)
   *
   *    Merge a disovery log into the cachelog.
   *
   */
  void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
  {
  	discovery_t *discovery;
0dc47877a   Harvey Harrison   net: replace rema...
113
114
  	IRDA_DEBUG(4, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  
  	/*
  	 *  If log is missing this means that IrLAP was unable to perform the
  	 *  discovery, so restart discovery again with just the half timeout
  	 *  of the normal one.
  	 */
  	/* Well... It means that there was nobody out there - Jean II */
  	if (log == NULL) {
  		/* irlmp_start_discovery_timer(irlmp, 150); */
  		return;
  	}
  
  	/*
  	 * Locking : we are the only owner of this discovery log, so
  	 * no need to lock it.
  	 * We just need to lock the global log in irlmp_add_discovery().
  	 */
  	discovery = (discovery_t *) hashbin_remove_first(log);
  	while (discovery != NULL) {
  		irlmp_add_discovery(cachelog, discovery);
  
  		discovery = (discovery_t *) hashbin_remove_first(log);
  	}
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
138

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  	/* Delete the now empty log */
  	hashbin_delete(log, (FREE_FUNC) kfree);
  }
  
  /*
   * Function irlmp_expire_discoveries (log, saddr, force)
   *
   *    Go through all discoveries and expire all that has stayed too long
   *
   * Note : this assume that IrLAP won't change its saddr, which
   * currently is a valid assumption...
   */
  void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
  {
  	discovery_t *		discovery;
  	discovery_t *		curr;
  	unsigned long		flags;
  	discinfo_t *		buffer = NULL;
  	int			n;		/* Size of the full log */
  	int			i = 0;		/* How many we expired */
  
  	IRDA_ASSERT(log != NULL, return;);
0dc47877a   Harvey Harrison   net: replace rema...
161
162
  	IRDA_DEBUG(4, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  
  	spin_lock_irqsave(&log->hb_spinlock, flags);
  
  	discovery = (discovery_t *) hashbin_get_first(log);
  	while (discovery != NULL) {
  		/* Be sure to be one item ahead */
  		curr = discovery;
  		discovery = (discovery_t *) hashbin_get_next(log);
  
  		/* Test if it's time to expire this discovery */
  		if ((curr->data.saddr == saddr) &&
  		    (force ||
  		     ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
  		{
  			/* Create buffer as needed.
  			 * As this function get called a lot and most time
  			 * we don't have anything to put in the log (we are
  			 * quite picky), we can save a lot of overhead
  			 * by not calling kmalloc. Jean II */
  			if(buffer == NULL) {
  				/* Create the client specific buffer */
  				n = HASHBIN_GET_SIZE(log);
  				buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
  				if (buffer == NULL) {
  					spin_unlock_irqrestore(&log->hb_spinlock, flags);
  					return;
  				}
  
  			}
  
  			/* Copy discovery information */
  			memcpy(&(buffer[i]), &(curr->data),
  			       sizeof(discinfo_t));
  			i++;
  
  			/* Remove it from the log */
  			curr = hashbin_remove_this(log, (irda_queue_t *) curr);
a51482bde   Jesper Juhl   [NET]: kfree cleanup
200
  			kfree(curr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
203
204
205
206
  		}
  	}
  
  	/* Drop the spinlock before calling the higher layers, as
  	 * we can't guarantee they won't call us back and create a
  	 * deadlock. We will work on our own private data, so we
3a4fa0a25   Robert P. J. Day   Fix misspellings ...
207
  	 * don't care to be interrupted. - Jean II */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  	spin_unlock_irqrestore(&log->hb_spinlock, flags);
  
  	if(buffer == NULL)
  		return;
  
  	/* Tell IrLMP and registered clients about it */
  	irlmp_discovery_expiry(buffer, i);
  
  	/* Free up our buffer */
  	kfree(buffer);
  }
  
  #if 0
  /*
   * Function irlmp_dump_discoveries (log)
   *
   *    Print out all discoveries in log
   *
   */
  void irlmp_dump_discoveries(hashbin_t *log)
  {
  	discovery_t *discovery;
  
  	IRDA_ASSERT(log != NULL, return;);
  
  	discovery = (discovery_t *) hashbin_get_first(log);
  	while (discovery != NULL) {
  		IRDA_DEBUG(0, "Discovery:
  ");
  		IRDA_DEBUG(0, "  daddr=%08x
  ", discovery->data.daddr);
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
239
240
  		IRDA_DEBUG(0, "  saddr=%08x
  ", discovery->data.saddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  		IRDA_DEBUG(0, "  nickname=%s
  ", discovery->data.info);
  
  		discovery = (discovery_t *) hashbin_get_next(log);
  	}
  }
  #endif
  
  /*
   * Function irlmp_copy_discoveries (log, pn, mask)
   *
   *    Copy all discoveries in a buffer
   *
   * This function implement a safe way for lmp clients to access the
   * discovery log. The basic problem is that we don't want the log
   * to change (add/remove) while the client is reading it. If the
   * lmp client manipulate directly the hashbin, he is sure to get
   * into troubles...
   * The idea is that we copy all the current discovery log in a buffer
   * which is specific to the client and pass this copy to him. As we
   * do this operation with the spinlock grabbed, we are safe...
   * Note : we don't want those clients to grab the spinlock, because
   * we have no control on how long they will hold it...
   * Note : we choose to copy the log in "struct irda_device_info" to
   * save space...
   * Note : the client must kfree himself() the log...
   * Jean II
   */
  struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
  						__u16 mask, int old_entries)
  {
  	discovery_t *		discovery;
  	unsigned long		flags;
  	discinfo_t *		buffer = NULL;
  	int			j_timeout = (sysctl_discovery_timeout * HZ);
  	int			n;		/* Size of the full log */
  	int			i = 0;		/* How many we picked */
  
  	IRDA_ASSERT(pn != NULL, return NULL;);
  	IRDA_ASSERT(log != NULL, return NULL;);
  
  	/* Save spin lock */
  	spin_lock_irqsave(&log->hb_spinlock, flags);
  
  	discovery = (discovery_t *) hashbin_get_first(log);
  	while (discovery != NULL) {
  		/* Mask out the ones we don't want :
  		 * We want to match the discovery mask, and to get only
  		 * the most recent one (unless we want old ones) */
332223831   Graf Yang   irda: Fix a misal...
290
  		if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  		    ((old_entries) ||
332223831   Graf Yang   irda: Fix a misal...
292
  		     ((jiffies - discovery->firststamp) < j_timeout))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  			/* Create buffer as needed.
  			 * As this function get called a lot and most time
  			 * we don't have anything to put in the log (we are
  			 * quite picky), we can save a lot of overhead
  			 * by not calling kmalloc. Jean II */
  			if(buffer == NULL) {
  				/* Create the client specific buffer */
  				n = HASHBIN_GET_SIZE(log);
  				buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
  				if (buffer == NULL) {
  					spin_unlock_irqrestore(&log->hb_spinlock, flags);
  					return NULL;
  				}
  
  			}
  
  			/* Copy discovery information */
  			memcpy(&(buffer[i]), &(discovery->data),
  			       sizeof(discinfo_t));
  			i++;
  		}
  		discovery = (discovery_t *) hashbin_get_next(log);
  	}
  
  	spin_unlock_irqrestore(&log->hb_spinlock, flags);
  
  	/* Get the actual number of device in the buffer and return */
  	*pn = i;
a02cec215   Eric Dumazet   net: return opera...
321
  	return buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
323
324
325
326
327
328
  }
  
  #ifdef CONFIG_PROC_FS
  static inline discovery_t *discovery_seq_idx(loff_t pos)
  
  {
  	discovery_t *discovery;
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
329
  	for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
334
  	     discovery != NULL;
  	     discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
  		if (pos-- == 0)
  			break;
  	}
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
335

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
  	return discovery;
  }
  
  static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
  {
  	spin_lock_irq(&irlmp->cachelog->hb_spinlock);
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
342
  	return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
347
  }
  
  static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  {
  	++*pos;
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
348
  	return (v == SEQ_START_TOKEN)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  		? (void *) hashbin_get_first(irlmp->cachelog)
  		: (void *) hashbin_get_next(irlmp->cachelog);
  }
  
  static void discovery_seq_stop(struct seq_file *seq, void *v)
  {
  	spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
  }
  
  static int discovery_seq_show(struct seq_file *seq, void *v)
  {
  	if (v == SEQ_START_TOKEN)
  		seq_puts(seq, "IrLMP: Discovery log:
  
  ");
  	else {
  		const discovery_t *discovery = v;
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
366
  		seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
  			   discovery->data.info,
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
368
  			   discovery->data.hints[0],
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  			   discovery->data.hints[1]);
  #if 0
  		if ( discovery->data.hints[0] & HINT_PNP)
  			seq_puts(seq, "PnP Compatible ");
  		if ( discovery->data.hints[0] & HINT_PDA)
  			seq_puts(seq, "PDA/Palmtop ");
  		if ( discovery->data.hints[0] & HINT_COMPUTER)
  			seq_puts(seq, "Computer ");
  		if ( discovery->data.hints[0] & HINT_PRINTER)
  			seq_puts(seq, "Printer ");
  		if ( discovery->data.hints[0] & HINT_MODEM)
  			seq_puts(seq, "Modem ");
  		if ( discovery->data.hints[0] & HINT_FAX)
  			seq_puts(seq, "Fax ");
  		if ( discovery->data.hints[0] & HINT_LAN)
  			seq_puts(seq, "LAN Access ");
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
385

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
  		if ( discovery->data.hints[1] & HINT_TELEPHONY)
  			seq_puts(seq, "Telephony ");
  		if ( discovery->data.hints[1] & HINT_FILE_SERVER)
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
389
  			seq_puts(seq, "File Server ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
392
393
  		if ( discovery->data.hints[1] & HINT_COMM)
  			seq_puts(seq, "IrCOMM ");
  		if ( discovery->data.hints[1] & HINT_OBEX)
  			seq_puts(seq, "IrOBEX ");
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
394
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395
396
397
398
399
  		seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x
  
  ",
  			       discovery->data.saddr,
  			       discovery->data.daddr);
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
400

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
402
403
404
405
  		seq_putc(seq, '
  ');
  	}
  	return 0;
  }
56b3d975b   Philippe De Muyter   [NET]: Make all i...
406
  static const struct seq_operations discovery_seq_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
410
411
412
413
414
415
416
417
418
  	.start  = discovery_seq_start,
  	.next   = discovery_seq_next,
  	.stop   = discovery_seq_stop,
  	.show   = discovery_seq_show,
  };
  
  static int discovery_seq_open(struct inode *inode, struct file *file)
  {
  	IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
  
  	return seq_open(file, &discovery_seq_ops);
  }
9a32144e9   Arjan van de Ven   [PATCH] mark stru...
419
  const struct file_operations discovery_seq_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
424
425
426
  	.owner		= THIS_MODULE,
  	.open           = discovery_seq_open,
  	.read           = seq_read,
  	.llseek         = seq_lseek,
  	.release	= seq_release,
  };
  #endif