linux/net/irda/discovery.c
<<
>>
Prefs
   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/seq_file.h>
  36
  37#include <net/irda/irda.h>
  38#include <net/irda/irlmp.h>
  39
  40#include <net/irda/discovery.h>
  41
  42/*
  43 * Function irlmp_add_discovery (cachelog, discovery)
  44 *
  45 *    Add a new discovery to the cachelog, and remove any old discoveries
  46 *    from the same device
  47 *
  48 * Note : we try to preserve the time this device was *first* discovered
  49 * (as opposed to the time of last discovery used for cleanup). This is
  50 * used by clients waiting for discovery events to tell if the device
  51 * discovered is "new" or just the same old one. They can't rely there
  52 * on a binary flag (new/old), because not all discovery events are
  53 * propagated to them, and they might not always listen, so they would
  54 * miss some new devices popping up...
  55 * Jean II
  56 */
  57void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
  58{
  59        discovery_t *discovery, *node;
  60        unsigned long flags;
  61
  62        /* Set time of first discovery if node is new (see below) */
  63        new->firststamp = new->timestamp;
  64
  65        spin_lock_irqsave(&cachelog->hb_spinlock, flags);
  66
  67        /* 
  68         * Remove all discoveries of devices that has previously been 
  69         * discovered on the same link with the same name (info), or the 
  70         * same daddr. We do this since some devices (mostly PDAs) change
  71         * their device address between every discovery.
  72         */
  73        discovery = (discovery_t *) hashbin_get_first(cachelog);
  74        while (discovery != NULL ) {
  75                node = discovery;
  76
  77                /* Be sure to stay one item ahead */
  78                discovery = (discovery_t *) hashbin_get_next(cachelog);
  79
  80                if ((node->data.saddr == new->data.saddr) &&
  81                    ((node->data.daddr == new->data.daddr) || 
  82                     (strcmp(node->data.info, new->data.info) == 0)))
  83                {
  84                        /* This discovery is a previous discovery 
  85                         * from the same device, so just remove it
  86                         */
  87                        hashbin_remove_this(cachelog, (irda_queue_t *) node);
  88                        /* Check if hints bits are unchanged */
  89                        if(u16ho(node->data.hints) == u16ho(new->data.hints))
  90                                /* Set time of first discovery for this node */
  91                                new->firststamp = node->firststamp;
  92                        kfree(node);
  93                }
  94        }
  95
  96        /* Insert the new and updated version */
  97        hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
  98
  99        spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
 100}
 101
 102/*
 103 * Function irlmp_add_discovery_log (cachelog, log)
 104 *
 105 *    Merge a disovery log into the cachelog.
 106 *
 107 */
 108void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
 109{
 110        discovery_t *discovery;
 111
 112        IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
 113
 114        /*
 115         *  If log is missing this means that IrLAP was unable to perform the
 116         *  discovery, so restart discovery again with just the half timeout
 117         *  of the normal one.
 118         */
 119        /* Well... It means that there was nobody out there - Jean II */
 120        if (log == NULL) {
 121                /* irlmp_start_discovery_timer(irlmp, 150); */
 122                return;
 123        }
 124
 125        /*
 126         * Locking : we are the only owner of this discovery log, so
 127         * no need to lock it.
 128         * We just need to lock the global log in irlmp_add_discovery().
 129         */
 130        discovery = (discovery_t *) hashbin_remove_first(log);
 131        while (discovery != NULL) {
 132                irlmp_add_discovery(cachelog, discovery);
 133
 134                discovery = (discovery_t *) hashbin_remove_first(log);
 135        }
 136        
 137        /* Delete the now empty log */
 138        hashbin_delete(log, (FREE_FUNC) kfree);
 139}
 140
 141/*
 142 * Function irlmp_expire_discoveries (log, saddr, force)
 143 *
 144 *    Go through all discoveries and expire all that has stayed too long
 145 *
 146 * Note : this assume that IrLAP won't change its saddr, which
 147 * currently is a valid assumption...
 148 */
 149void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
 150{
 151        discovery_t *           discovery;
 152        discovery_t *           curr;
 153        unsigned long           flags;
 154        discinfo_t *            buffer = NULL;
 155        int                     n;              /* Size of the full log */
 156        int                     i = 0;          /* How many we expired */
 157
 158        IRDA_ASSERT(log != NULL, return;);
 159        IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
 160
 161        spin_lock_irqsave(&log->hb_spinlock, flags);
 162
 163        discovery = (discovery_t *) hashbin_get_first(log);
 164        while (discovery != NULL) {
 165                /* Be sure to be one item ahead */
 166                curr = discovery;
 167                discovery = (discovery_t *) hashbin_get_next(log);
 168
 169                /* Test if it's time to expire this discovery */
 170                if ((curr->data.saddr == saddr) &&
 171                    (force ||
 172                     ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
 173                {
 174                        /* Create buffer as needed.
 175                         * As this function get called a lot and most time
 176                         * we don't have anything to put in the log (we are
 177                         * quite picky), we can save a lot of overhead
 178                         * by not calling kmalloc. Jean II */
 179                        if(buffer == NULL) {
 180                                /* Create the client specific buffer */
 181                                n = HASHBIN_GET_SIZE(log);
 182                                buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
 183                                if (buffer == NULL) {
 184                                        spin_unlock_irqrestore(&log->hb_spinlock, flags);
 185                                        return;
 186                                }
 187
 188                        }
 189
 190                        /* Copy discovery information */
 191                        memcpy(&(buffer[i]), &(curr->data),
 192                               sizeof(discinfo_t));
 193                        i++;
 194
 195                        /* Remove it from the log */
 196                        curr = hashbin_remove_this(log, (irda_queue_t *) curr);
 197                        kfree(curr);
 198                }
 199        }
 200
 201        /* Drop the spinlock before calling the higher layers, as
 202         * we can't guarantee they won't call us back and create a
 203         * deadlock. We will work on our own private data, so we
 204         * don't care to be interupted. - Jean II */
 205        spin_unlock_irqrestore(&log->hb_spinlock, flags);
 206
 207        if(buffer == NULL)
 208                return;
 209
 210        /* Tell IrLMP and registered clients about it */
 211        irlmp_discovery_expiry(buffer, i);
 212
 213        /* Free up our buffer */
 214        kfree(buffer);
 215}
 216
 217#if 0
 218/*
 219 * Function irlmp_dump_discoveries (log)
 220 *
 221 *    Print out all discoveries in log
 222 *
 223 */
 224void irlmp_dump_discoveries(hashbin_t *log)
 225{
 226        discovery_t *discovery;
 227
 228        IRDA_ASSERT(log != NULL, return;);
 229
 230        discovery = (discovery_t *) hashbin_get_first(log);
 231        while (discovery != NULL) {
 232                IRDA_DEBUG(0, "Discovery:\n");
 233                IRDA_DEBUG(0, "  daddr=%08x\n", discovery->data.daddr);
 234                IRDA_DEBUG(0, "  saddr=%08x\n", discovery->data.saddr); 
 235                IRDA_DEBUG(0, "  nickname=%s\n", discovery->data.info);
 236
 237                discovery = (discovery_t *) hashbin_get_next(log);
 238        }
 239}
 240#endif
 241
 242/*
 243 * Function irlmp_copy_discoveries (log, pn, mask)
 244 *
 245 *    Copy all discoveries in a buffer
 246 *
 247 * This function implement a safe way for lmp clients to access the
 248 * discovery log. The basic problem is that we don't want the log
 249 * to change (add/remove) while the client is reading it. If the
 250 * lmp client manipulate directly the hashbin, he is sure to get
 251 * into troubles...
 252 * The idea is that we copy all the current discovery log in a buffer
 253 * which is specific to the client and pass this copy to him. As we
 254 * do this operation with the spinlock grabbed, we are safe...
 255 * Note : we don't want those clients to grab the spinlock, because
 256 * we have no control on how long they will hold it...
 257 * Note : we choose to copy the log in "struct irda_device_info" to
 258 * save space...
 259 * Note : the client must kfree himself() the log...
 260 * Jean II
 261 */
 262struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
 263                                                __u16 mask, int old_entries)
 264{
 265        discovery_t *           discovery;
 266        unsigned long           flags;
 267        discinfo_t *            buffer = NULL;
 268        int                     j_timeout = (sysctl_discovery_timeout * HZ);
 269        int                     n;              /* Size of the full log */
 270        int                     i = 0;          /* How many we picked */
 271
 272        IRDA_ASSERT(pn != NULL, return NULL;);
 273        IRDA_ASSERT(log != NULL, return NULL;);
 274
 275        /* Save spin lock */
 276        spin_lock_irqsave(&log->hb_spinlock, flags);
 277
 278        discovery = (discovery_t *) hashbin_get_first(log);
 279        while (discovery != NULL) {
 280                /* Mask out the ones we don't want :
 281                 * We want to match the discovery mask, and to get only
 282                 * the most recent one (unless we want old ones) */
 283                if ((u16ho(discovery->data.hints) & mask) &&
 284                    ((old_entries) ||
 285                     ((jiffies - discovery->firststamp) < j_timeout)) ) {
 286                        /* Create buffer as needed.
 287                         * As this function get called a lot and most time
 288                         * we don't have anything to put in the log (we are
 289                         * quite picky), we can save a lot of overhead
 290                         * by not calling kmalloc. Jean II */
 291                        if(buffer == NULL) {
 292                                /* Create the client specific buffer */
 293                                n = HASHBIN_GET_SIZE(log);
 294                                buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
 295                                if (buffer == NULL) {
 296                                        spin_unlock_irqrestore(&log->hb_spinlock, flags);
 297                                        return NULL;
 298                                }
 299
 300                        }
 301
 302                        /* Copy discovery information */
 303                        memcpy(&(buffer[i]), &(discovery->data),
 304                               sizeof(discinfo_t));
 305                        i++;
 306                }
 307                discovery = (discovery_t *) hashbin_get_next(log);
 308        }
 309
 310        spin_unlock_irqrestore(&log->hb_spinlock, flags);
 311
 312        /* Get the actual number of device in the buffer and return */
 313        *pn = i;
 314        return(buffer);
 315}
 316
 317#ifdef CONFIG_PROC_FS
 318static inline discovery_t *discovery_seq_idx(loff_t pos)
 319
 320{
 321        discovery_t *discovery;
 322
 323        for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog); 
 324             discovery != NULL;
 325             discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
 326                if (pos-- == 0)
 327                        break;
 328        }
 329                
 330        return discovery;
 331}
 332
 333static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
 334{
 335        spin_lock_irq(&irlmp->cachelog->hb_spinlock);
 336        return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
 337}
 338
 339static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 340{
 341        ++*pos;
 342        return (v == SEQ_START_TOKEN) 
 343                ? (void *) hashbin_get_first(irlmp->cachelog)
 344                : (void *) hashbin_get_next(irlmp->cachelog);
 345}
 346
 347static void discovery_seq_stop(struct seq_file *seq, void *v)
 348{
 349        spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
 350}
 351
 352static int discovery_seq_show(struct seq_file *seq, void *v)
 353{
 354        if (v == SEQ_START_TOKEN)
 355                seq_puts(seq, "IrLMP: Discovery log:\n\n");
 356        else {
 357                const discovery_t *discovery = v;
 358
 359                seq_printf(seq, "nickname: %s, hint: 0x%02x%02x", 
 360                           discovery->data.info,
 361                           discovery->data.hints[0], 
 362                           discovery->data.hints[1]);
 363#if 0
 364                if ( discovery->data.hints[0] & HINT_PNP)
 365                        seq_puts(seq, "PnP Compatible ");
 366                if ( discovery->data.hints[0] & HINT_PDA)
 367                        seq_puts(seq, "PDA/Palmtop ");
 368                if ( discovery->data.hints[0] & HINT_COMPUTER)
 369                        seq_puts(seq, "Computer ");
 370                if ( discovery->data.hints[0] & HINT_PRINTER)
 371                        seq_puts(seq, "Printer ");
 372                if ( discovery->data.hints[0] & HINT_MODEM)
 373                        seq_puts(seq, "Modem ");
 374                if ( discovery->data.hints[0] & HINT_FAX)
 375                        seq_puts(seq, "Fax ");
 376                if ( discovery->data.hints[0] & HINT_LAN)
 377                        seq_puts(seq, "LAN Access ");
 378                
 379                if ( discovery->data.hints[1] & HINT_TELEPHONY)
 380                        seq_puts(seq, "Telephony ");
 381                if ( discovery->data.hints[1] & HINT_FILE_SERVER)
 382                        seq_puts(seq, "File Server ");       
 383                if ( discovery->data.hints[1] & HINT_COMM)
 384                        seq_puts(seq, "IrCOMM ");
 385                if ( discovery->data.hints[1] & HINT_OBEX)
 386                        seq_puts(seq, "IrOBEX ");
 387#endif          
 388                seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
 389                               discovery->data.saddr,
 390                               discovery->data.daddr);
 391                
 392                seq_putc(seq, '\n');
 393        }
 394        return 0;
 395}
 396
 397static struct seq_operations discovery_seq_ops = {
 398        .start  = discovery_seq_start,
 399        .next   = discovery_seq_next,
 400        .stop   = discovery_seq_stop,
 401        .show   = discovery_seq_show,
 402};
 403
 404static int discovery_seq_open(struct inode *inode, struct file *file)
 405{
 406        IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
 407
 408        return seq_open(file, &discovery_seq_ops);
 409}
 410
 411struct file_operations discovery_seq_fops = {
 412        .owner          = THIS_MODULE,
 413        .open           = discovery_seq_open,
 414        .read           = seq_read,
 415        .llseek         = seq_lseek,
 416        .release        = seq_release,
 417};
 418#endif
 419
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.