~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/irda/discovery.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 #include <linux/export.h>
 39 
 40 #include <net/irda/irda.h>
 41 #include <net/irda/irlmp.h>
 42 
 43 #include <net/irda/discovery.h>
 44 
 45 #include <asm/unaligned.h>
 46 
 47 /*
 48  * Function irlmp_add_discovery (cachelog, discovery)
 49  *
 50  *    Add a new discovery to the cachelog, and remove any old discoveries
 51  *    from the same device
 52  *
 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...
 60  * Jean II
 61  */
 62 void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
 63 {
 64         discovery_t *discovery, *node;
 65         unsigned long flags;
 66 
 67         /* Set time of first discovery if node is new (see below) */
 68         new->firststamp = new->timestamp;
 69 
 70         spin_lock_irqsave(&cachelog->hb_spinlock, flags);
 71 
 72         /*
 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.
 77          */
 78         discovery = (discovery_t *) hashbin_get_first(cachelog);
 79         while (discovery != NULL ) {
 80                 node = discovery;
 81 
 82                 /* Be sure to stay one item ahead */
 83                 discovery = (discovery_t *) hashbin_get_next(cachelog);
 84 
 85                 if ((node->data.saddr == new->data.saddr) &&
 86                     ((node->data.daddr == new->data.daddr) ||
 87                      (strcmp(node->data.info, new->data.info) == 0)))
 88                 {
 89                         /* This discovery is a previous discovery
 90                          * from the same device, so just remove it
 91                          */
 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;
 97                         kfree(node);
 98                 }
 99         }
100 
101         /* Insert the new and updated version */
102         hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
103 
104         spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
105 }
106 
107 /*
108  * Function irlmp_add_discovery_log (cachelog, log)
109  *
110  *    Merge a disovery log into the cachelog.
111  *
112  */
113 void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
114 {
115         discovery_t *discovery;
116 
117         IRDA_DEBUG(4, "%s()\n", __func__);
118 
119         /*
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
122          *  of the normal one.
123          */
124         /* Well... It means that there was nobody out there - Jean II */
125         if (log == NULL) {
126                 /* irlmp_start_discovery_timer(irlmp, 150); */
127                 return;
128         }
129 
130         /*
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().
134          */
135         discovery = (discovery_t *) hashbin_remove_first(log);
136         while (discovery != NULL) {
137                 irlmp_add_discovery(cachelog, discovery);
138 
139                 discovery = (discovery_t *) hashbin_remove_first(log);
140         }
141 
142         /* Delete the now empty log */
143         hashbin_delete(log, (FREE_FUNC) kfree);
144 }
145 
146 /*
147  * Function irlmp_expire_discoveries (log, saddr, force)
148  *
149  *    Go through all discoveries and expire all that has stayed too long
150  *
151  * Note : this assume that IrLAP won't change its saddr, which
152  * currently is a valid assumption...
153  */
154 void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
155 {
156         discovery_t *           discovery;
157         discovery_t *           curr;
158         unsigned long           flags;
159         discinfo_t *            buffer = NULL;
160         int                     n;              /* Size of the full log */
161         int                     i = 0;          /* How many we expired */
162 
163         IRDA_ASSERT(log != NULL, return;);
164         IRDA_DEBUG(4, "%s()\n", __func__);
165 
166         spin_lock_irqsave(&log->hb_spinlock, flags);
167 
168         discovery = (discovery_t *) hashbin_get_first(log);
169         while (discovery != NULL) {
170                 /* Be sure to be one item ahead */
171                 curr = discovery;
172                 discovery = (discovery_t *) hashbin_get_next(log);
173 
174                 /* Test if it's time to expire this discovery */
175                 if ((curr->data.saddr == saddr) &&
176                     (force ||
177                      ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
178                 {
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 */
184                         if(buffer == NULL) {
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);
190                                         return;
191                                 }
192 
193                         }
194 
195                         /* Copy discovery information */
196                         memcpy(&(buffer[i]), &(curr->data),
197                                sizeof(discinfo_t));
198                         i++;
199 
200                         /* Remove it from the log */
201                         curr = hashbin_remove_this(log, (irda_queue_t *) curr);
202                         kfree(curr);
203                 }
204         }
205 
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);
211 
212         if(buffer == NULL)
213                 return;
214 
215         /* Tell IrLMP and registered clients about it */
216         irlmp_discovery_expiry(buffer, i);
217 
218         /* Free up our buffer */
219         kfree(buffer);
220 }
221 
222 #if 0
223 /*
224  * Function irlmp_dump_discoveries (log)
225  *
226  *    Print out all discoveries in log
227  *
228  */
229 void irlmp_dump_discoveries(hashbin_t *log)
230 {
231         discovery_t *discovery;
232 
233         IRDA_ASSERT(log != NULL, return;);
234 
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);
241 
242                 discovery = (discovery_t *) hashbin_get_next(log);
243         }
244 }
245 #endif
246 
247 /*
248  * Function irlmp_copy_discoveries (log, pn, mask)
249  *
250  *    Copy all discoveries in a buffer
251  *
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
256  * into troubles...
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
263  * save space...
264  * Note : the client must kfree himself() the log...
265  * Jean II
266  */
267 struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
268                                                 __u16 mask, int old_entries)
269 {
270         discovery_t *           discovery;
271         unsigned long           flags;
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 */
276 
277         IRDA_ASSERT(pn != NULL, return NULL;);
278         IRDA_ASSERT(log != NULL, return NULL;);
279 
280         /* Save spin lock */
281         spin_lock_irqsave(&log->hb_spinlock, flags);
282 
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) &&
289                     ((old_entries) ||
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 */
296                         if(buffer == NULL) {
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);
302                                         return NULL;
303                                 }
304 
305                         }
306 
307                         /* Copy discovery information */
308                         memcpy(&(buffer[i]), &(discovery->data),
309                                sizeof(discinfo_t));
310                         i++;
311                 }
312                 discovery = (discovery_t *) hashbin_get_next(log);
313         }
314 
315         spin_unlock_irqrestore(&log->hb_spinlock, flags);
316 
317         /* Get the actual number of device in the buffer and return */
318         *pn = i;
319         return buffer;
320 }
321 
322 #ifdef CONFIG_PROC_FS
323 static inline discovery_t *discovery_seq_idx(loff_t pos)
324 
325 {
326         discovery_t *discovery;
327 
328         for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
329              discovery != NULL;
330              discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
331                 if (pos-- == 0)
332                         break;
333         }
334 
335         return discovery;
336 }
337 
338 static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
339 {
340         spin_lock_irq(&irlmp->cachelog->hb_spinlock);
341         return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
342 }
343 
344 static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
345 {
346         ++*pos;
347         return (v == SEQ_START_TOKEN)
348                 ? (void *) hashbin_get_first(irlmp->cachelog)
349                 : (void *) hashbin_get_next(irlmp->cachelog);
350 }
351 
352 static void discovery_seq_stop(struct seq_file *seq, void *v)
353 {
354         spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
355 }
356 
357 static int discovery_seq_show(struct seq_file *seq, void *v)
358 {
359         if (v == SEQ_START_TOKEN)
360                 seq_puts(seq, "IrLMP: Discovery log:\n\n");
361         else {
362                 const discovery_t *discovery = v;
363 
364                 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
365                            discovery->data.info,
366                            discovery->data.hints[0],
367                            discovery->data.hints[1]);
368 #if 0
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 ");
383 
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 ");
392 #endif
393                 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
394                                discovery->data.saddr,
395                                discovery->data.daddr);
396 
397                 seq_putc(seq, '\n');
398         }
399         return 0;
400 }
401 
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,
407 };
408 
409 static int discovery_seq_open(struct inode *inode, struct file *file)
410 {
411         IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
412 
413         return seq_open(file, &discovery_seq_ops);
414 }
415 
416 const struct file_operations discovery_seq_fops = {
417         .owner          = THIS_MODULE,
418         .open           = discovery_seq_open,
419         .read           = seq_read,
420         .llseek         = seq_lseek,
421         .release        = seq_release,
422 };
423 #endif
424 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp