blob: 364d70aed068d8bcec4aa3d43e2314472aef307f [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
Jeff Kirsherd3770502013-12-06 09:13:43 -080027 * along with this program; if not, see <http://www.gnu.org/licenses/>.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 ********************************************************************/
30
31#include <linux/string.h>
32#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050033#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040036#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <net/irda/irda.h>
39#include <net/irda/irlmp.h>
40
41#include <net/irda/discovery.h>
42
Graf Yang33222382008-05-13 23:25:57 -070043#include <asm/unaligned.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Function irlmp_add_discovery (cachelog, discovery)
47 *
48 * Add a new discovery to the cachelog, and remove any old discoveries
49 * from the same device
50 *
51 * Note : we try to preserve the time this device was *first* discovered
52 * (as opposed to the time of last discovery used for cleanup). This is
53 * used by clients waiting for discovery events to tell if the device
54 * discovered is "new" or just the same old one. They can't rely there
55 * on a binary flag (new/old), because not all discovery events are
56 * propagated to them, and they might not always listen, so they would
57 * miss some new devices popping up...
58 * Jean II
59 */
60void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
61{
62 discovery_t *discovery, *node;
63 unsigned long flags;
64
65 /* Set time of first discovery if node is new (see below) */
66 new->firststamp = new->timestamp;
67
68 spin_lock_irqsave(&cachelog->hb_spinlock, flags);
69
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090070 /*
71 * Remove all discoveries of devices that has previously been
72 * discovered on the same link with the same name (info), or the
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * same daddr. We do this since some devices (mostly PDAs) change
74 * their device address between every discovery.
75 */
76 discovery = (discovery_t *) hashbin_get_first(cachelog);
77 while (discovery != NULL ) {
78 node = discovery;
79
80 /* Be sure to stay one item ahead */
81 discovery = (discovery_t *) hashbin_get_next(cachelog);
82
83 if ((node->data.saddr == new->data.saddr) &&
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090084 ((node->data.daddr == new->data.daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 (strcmp(node->data.info, new->data.info) == 0)))
86 {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090087 /* This discovery is a previous discovery
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * from the same device, so just remove it
89 */
90 hashbin_remove_this(cachelog, (irda_queue_t *) node);
91 /* Check if hints bits are unchanged */
Graf Yang33222382008-05-13 23:25:57 -070092 if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* Set time of first discovery for this node */
94 new->firststamp = node->firststamp;
95 kfree(node);
96 }
97 }
98
99 /* Insert the new and updated version */
100 hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
101
102 spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
103}
104
105/*
106 * Function irlmp_add_discovery_log (cachelog, log)
107 *
108 * Merge a disovery log into the cachelog.
109 *
110 */
111void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
112{
113 discovery_t *discovery;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * If log is missing this means that IrLAP was unable to perform the
117 * discovery, so restart discovery again with just the half timeout
118 * of the normal one.
119 */
120 /* Well... It means that there was nobody out there - Jean II */
121 if (log == NULL) {
122 /* irlmp_start_discovery_timer(irlmp, 150); */
123 return;
124 }
125
126 /*
127 * Locking : we are the only owner of this discovery log, so
128 * no need to lock it.
129 * We just need to lock the global log in irlmp_add_discovery().
130 */
131 discovery = (discovery_t *) hashbin_remove_first(log);
132 while (discovery != NULL) {
133 irlmp_add_discovery(cachelog, discovery);
134
135 discovery = (discovery_t *) hashbin_remove_first(log);
136 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* Delete the now empty log */
139 hashbin_delete(log, (FREE_FUNC) kfree);
140}
141
142/*
143 * Function irlmp_expire_discoveries (log, saddr, force)
144 *
145 * Go through all discoveries and expire all that has stayed too long
146 *
147 * Note : this assume that IrLAP won't change its saddr, which
148 * currently is a valid assumption...
149 */
150void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
151{
152 discovery_t * discovery;
153 discovery_t * curr;
154 unsigned long flags;
155 discinfo_t * buffer = NULL;
156 int n; /* Size of the full log */
157 int i = 0; /* How many we expired */
158
159 IRDA_ASSERT(log != NULL, return;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 spin_lock_irqsave(&log->hb_spinlock, flags);
161
162 discovery = (discovery_t *) hashbin_get_first(log);
163 while (discovery != NULL) {
164 /* Be sure to be one item ahead */
165 curr = discovery;
166 discovery = (discovery_t *) hashbin_get_next(log);
167
168 /* Test if it's time to expire this discovery */
169 if ((curr->data.saddr == saddr) &&
170 (force ||
171 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
172 {
173 /* Create buffer as needed.
174 * As this function get called a lot and most time
175 * we don't have anything to put in the log (we are
176 * quite picky), we can save a lot of overhead
177 * by not calling kmalloc. Jean II */
178 if(buffer == NULL) {
179 /* Create the client specific buffer */
180 n = HASHBIN_GET_SIZE(log);
181 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
182 if (buffer == NULL) {
183 spin_unlock_irqrestore(&log->hb_spinlock, flags);
184 return;
185 }
186
187 }
188
189 /* Copy discovery information */
190 memcpy(&(buffer[i]), &(curr->data),
191 sizeof(discinfo_t));
192 i++;
193
194 /* Remove it from the log */
195 curr = hashbin_remove_this(log, (irda_queue_t *) curr);
Jesper Juhla51482b2005-11-08 09:41:34 -0800196 kfree(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 }
199
200 /* Drop the spinlock before calling the higher layers, as
201 * we can't guarantee they won't call us back and create a
202 * deadlock. We will work on our own private data, so we
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200203 * don't care to be interrupted. - Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 spin_unlock_irqrestore(&log->hb_spinlock, flags);
205
206 if(buffer == NULL)
207 return;
208
209 /* Tell IrLMP and registered clients about it */
210 irlmp_discovery_expiry(buffer, i);
211
212 /* Free up our buffer */
213 kfree(buffer);
214}
215
216#if 0
217/*
218 * Function irlmp_dump_discoveries (log)
219 *
220 * Print out all discoveries in log
221 *
222 */
223void irlmp_dump_discoveries(hashbin_t *log)
224{
225 discovery_t *discovery;
226
227 IRDA_ASSERT(log != NULL, return;);
228
229 discovery = (discovery_t *) hashbin_get_first(log);
230 while (discovery != NULL) {
Joe Perches955a9d202014-11-11 14:44:57 -0800231 pr_debug("Discovery:\n");
232 pr_debug(" daddr=%08x\n", discovery->data.daddr);
233 pr_debug(" saddr=%08x\n", discovery->data.saddr);
234 pr_debug(" nickname=%s\n", discovery->data.info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 discovery = (discovery_t *) hashbin_get_next(log);
237 }
238}
239#endif
240
241/*
242 * Function irlmp_copy_discoveries (log, pn, mask)
243 *
244 * Copy all discoveries in a buffer
245 *
246 * This function implement a safe way for lmp clients to access the
247 * discovery log. The basic problem is that we don't want the log
248 * to change (add/remove) while the client is reading it. If the
249 * lmp client manipulate directly the hashbin, he is sure to get
250 * into troubles...
251 * The idea is that we copy all the current discovery log in a buffer
252 * which is specific to the client and pass this copy to him. As we
253 * do this operation with the spinlock grabbed, we are safe...
254 * Note : we don't want those clients to grab the spinlock, because
255 * we have no control on how long they will hold it...
256 * Note : we choose to copy the log in "struct irda_device_info" to
257 * save space...
258 * Note : the client must kfree himself() the log...
259 * Jean II
260 */
261struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
262 __u16 mask, int old_entries)
263{
264 discovery_t * discovery;
265 unsigned long flags;
266 discinfo_t * buffer = NULL;
267 int j_timeout = (sysctl_discovery_timeout * HZ);
268 int n; /* Size of the full log */
269 int i = 0; /* How many we picked */
270
271 IRDA_ASSERT(pn != NULL, return NULL;);
272 IRDA_ASSERT(log != NULL, return NULL;);
273
274 /* Save spin lock */
275 spin_lock_irqsave(&log->hb_spinlock, flags);
276
277 discovery = (discovery_t *) hashbin_get_first(log);
278 while (discovery != NULL) {
279 /* Mask out the ones we don't want :
280 * We want to match the discovery mask, and to get only
281 * the most recent one (unless we want old ones) */
Graf Yang33222382008-05-13 23:25:57 -0700282 if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ((old_entries) ||
Graf Yang33222382008-05-13 23:25:57 -0700284 ((jiffies - discovery->firststamp) < j_timeout))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Create buffer as needed.
286 * As this function get called a lot and most time
287 * we don't have anything to put in the log (we are
288 * quite picky), we can save a lot of overhead
289 * by not calling kmalloc. Jean II */
290 if(buffer == NULL) {
291 /* Create the client specific buffer */
292 n = HASHBIN_GET_SIZE(log);
293 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
294 if (buffer == NULL) {
295 spin_unlock_irqrestore(&log->hb_spinlock, flags);
296 return NULL;
297 }
298
299 }
300
301 /* Copy discovery information */
302 memcpy(&(buffer[i]), &(discovery->data),
303 sizeof(discinfo_t));
304 i++;
305 }
306 discovery = (discovery_t *) hashbin_get_next(log);
307 }
308
309 spin_unlock_irqrestore(&log->hb_spinlock, flags);
310
311 /* Get the actual number of device in the buffer and return */
312 *pn = i;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000313 return buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316#ifdef CONFIG_PROC_FS
317static inline discovery_t *discovery_seq_idx(loff_t pos)
318
319{
320 discovery_t *discovery;
321
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900322 for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 discovery != NULL;
324 discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
325 if (pos-- == 0)
326 break;
327 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return discovery;
330}
331
332static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
333{
334 spin_lock_irq(&irlmp->cachelog->hb_spinlock);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900335 return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
339{
340 ++*pos;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900341 return (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 ? (void *) hashbin_get_first(irlmp->cachelog)
343 : (void *) hashbin_get_next(irlmp->cachelog);
344}
345
346static void discovery_seq_stop(struct seq_file *seq, void *v)
347{
348 spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
349}
350
351static int discovery_seq_show(struct seq_file *seq, void *v)
352{
353 if (v == SEQ_START_TOKEN)
354 seq_puts(seq, "IrLMP: Discovery log:\n\n");
355 else {
356 const discovery_t *discovery = v;
357
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900358 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 discovery->data.info,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900360 discovery->data.hints[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 discovery->data.hints[1]);
362#if 0
363 if ( discovery->data.hints[0] & HINT_PNP)
364 seq_puts(seq, "PnP Compatible ");
365 if ( discovery->data.hints[0] & HINT_PDA)
366 seq_puts(seq, "PDA/Palmtop ");
367 if ( discovery->data.hints[0] & HINT_COMPUTER)
368 seq_puts(seq, "Computer ");
369 if ( discovery->data.hints[0] & HINT_PRINTER)
370 seq_puts(seq, "Printer ");
371 if ( discovery->data.hints[0] & HINT_MODEM)
372 seq_puts(seq, "Modem ");
373 if ( discovery->data.hints[0] & HINT_FAX)
374 seq_puts(seq, "Fax ");
375 if ( discovery->data.hints[0] & HINT_LAN)
376 seq_puts(seq, "LAN Access ");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if ( discovery->data.hints[1] & HINT_TELEPHONY)
379 seq_puts(seq, "Telephony ");
380 if ( discovery->data.hints[1] & HINT_FILE_SERVER)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900381 seq_puts(seq, "File Server ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if ( discovery->data.hints[1] & HINT_COMM)
383 seq_puts(seq, "IrCOMM ");
384 if ( discovery->data.hints[1] & HINT_OBEX)
385 seq_puts(seq, "IrOBEX ");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900386#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
388 discovery->data.saddr,
389 discovery->data.daddr);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 seq_putc(seq, '\n');
392 }
393 return 0;
394}
395
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700396static const struct seq_operations discovery_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .start = discovery_seq_start,
398 .next = discovery_seq_next,
399 .stop = discovery_seq_stop,
400 .show = discovery_seq_show,
401};
402
403static int discovery_seq_open(struct inode *inode, struct file *file)
404{
405 IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
406
407 return seq_open(file, &discovery_seq_ops);
408}
409
Arjan van de Ven9a321442007-02-12 00:55:35 -0800410const struct file_operations discovery_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 .owner = THIS_MODULE,
412 .open = discovery_seq_open,
413 .read = seq_read,
414 .llseek = seq_lseek,
415 .release = seq_release,
416};
417#endif