/*********************************************************************1*2* Filename: discovery.c3* Version: 0.14* Description: Routines for handling discoveries at the IrLMP layer5* Status: Experimental.6* Author: Dag Brattli <[email protected]>7* Created at: Tue Apr 6 15:33:50 19998* Modified at: Sat Oct 9 17:11:31 19999* Modified by: Dag Brattli <[email protected]>10* Modified at: Fri May 28 3:11 CST 199911* Modified by: Horst von Brand <[email protected]>12*13* Copyright (c) 1999 Dag Brattli, All Rights Reserved.14*15* This program is free software; you can redistribute it and/or16* modify it under the terms of the GNU General Public License as17* published by the Free Software Foundation; either version 2 of18* the License, or (at your option) any later version.19*20* This program is distributed in the hope that it will be useful,21* but WITHOUT ANY WARRANTY; without even the implied warranty of22* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the23* GNU General Public License for more details.24*25* You should have received a copy of the GNU General Public License26* along with this program; if not, write to the Free Software27* Foundation, Inc., 59 Temple Place, Suite 330, Boston,28* MA 02111-1307 USA29*30********************************************************************/3132#include <linux/string.h>33#include <linux/socket.h>34#include <linux/fs.h>35#include <linux/seq_file.h>36#include <linux/slab.h>3738#include <net/irda/irda.h>39#include <net/irda/irlmp.h>4041#include <net/irda/discovery.h>4243#include <asm/unaligned.h>4445/*46* Function irlmp_add_discovery (cachelog, discovery)47*48* Add a new discovery to the cachelog, and remove any old discoveries49* from the same device50*51* Note : we try to preserve the time this device was *first* discovered52* (as opposed to the time of last discovery used for cleanup). This is53* used by clients waiting for discovery events to tell if the device54* discovered is "new" or just the same old one. They can't rely there55* on a binary flag (new/old), because not all discovery events are56* propagated to them, and they might not always listen, so they would57* miss some new devices popping up...58* Jean II59*/60void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)61{62discovery_t *discovery, *node;63unsigned long flags;6465/* Set time of first discovery if node is new (see below) */66new->firststamp = new->timestamp;6768spin_lock_irqsave(&cachelog->hb_spinlock, flags);6970/*71* Remove all discoveries of devices that has previously been72* discovered on the same link with the same name (info), or the73* same daddr. We do this since some devices (mostly PDAs) change74* their device address between every discovery.75*/76discovery = (discovery_t *) hashbin_get_first(cachelog);77while (discovery != NULL ) {78node = discovery;7980/* Be sure to stay one item ahead */81discovery = (discovery_t *) hashbin_get_next(cachelog);8283if ((node->data.saddr == new->data.saddr) &&84((node->data.daddr == new->data.daddr) ||85(strcmp(node->data.info, new->data.info) == 0)))86{87/* This discovery is a previous discovery88* from the same device, so just remove it89*/90hashbin_remove_this(cachelog, (irda_queue_t *) node);91/* Check if hints bits are unchanged */92if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))93/* Set time of first discovery for this node */94new->firststamp = node->firststamp;95kfree(node);96}97}9899/* Insert the new and updated version */100hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);101102spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);103}104105/*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{113discovery_t *discovery;114115IRDA_DEBUG(4, "%s()\n", __func__);116117/*118* If log is missing this means that IrLAP was unable to perform the119* discovery, so restart discovery again with just the half timeout120* of the normal one.121*/122/* Well... It means that there was nobody out there - Jean II */123if (log == NULL) {124/* irlmp_start_discovery_timer(irlmp, 150); */125return;126}127128/*129* Locking : we are the only owner of this discovery log, so130* no need to lock it.131* We just need to lock the global log in irlmp_add_discovery().132*/133discovery = (discovery_t *) hashbin_remove_first(log);134while (discovery != NULL) {135irlmp_add_discovery(cachelog, discovery);136137discovery = (discovery_t *) hashbin_remove_first(log);138}139140/* Delete the now empty log */141hashbin_delete(log, (FREE_FUNC) kfree);142}143144/*145* Function irlmp_expire_discoveries (log, saddr, force)146*147* Go through all discoveries and expire all that has stayed too long148*149* Note : this assume that IrLAP won't change its saddr, which150* currently is a valid assumption...151*/152void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)153{154discovery_t * discovery;155discovery_t * curr;156unsigned long flags;157discinfo_t * buffer = NULL;158int n; /* Size of the full log */159int i = 0; /* How many we expired */160161IRDA_ASSERT(log != NULL, return;);162IRDA_DEBUG(4, "%s()\n", __func__);163164spin_lock_irqsave(&log->hb_spinlock, flags);165166discovery = (discovery_t *) hashbin_get_first(log);167while (discovery != NULL) {168/* Be sure to be one item ahead */169curr = discovery;170discovery = (discovery_t *) hashbin_get_next(log);171172/* Test if it's time to expire this discovery */173if ((curr->data.saddr == saddr) &&174(force ||175((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))176{177/* Create buffer as needed.178* As this function get called a lot and most time179* we don't have anything to put in the log (we are180* quite picky), we can save a lot of overhead181* by not calling kmalloc. Jean II */182if(buffer == NULL) {183/* Create the client specific buffer */184n = HASHBIN_GET_SIZE(log);185buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);186if (buffer == NULL) {187spin_unlock_irqrestore(&log->hb_spinlock, flags);188return;189}190191}192193/* Copy discovery information */194memcpy(&(buffer[i]), &(curr->data),195sizeof(discinfo_t));196i++;197198/* Remove it from the log */199curr = hashbin_remove_this(log, (irda_queue_t *) curr);200kfree(curr);201}202}203204/* Drop the spinlock before calling the higher layers, as205* we can't guarantee they won't call us back and create a206* deadlock. We will work on our own private data, so we207* don't care to be interrupted. - Jean II */208spin_unlock_irqrestore(&log->hb_spinlock, flags);209210if(buffer == NULL)211return;212213/* Tell IrLMP and registered clients about it */214irlmp_discovery_expiry(buffer, i);215216/* Free up our buffer */217kfree(buffer);218}219220#if 0221/*222* Function irlmp_dump_discoveries (log)223*224* Print out all discoveries in log225*226*/227void irlmp_dump_discoveries(hashbin_t *log)228{229discovery_t *discovery;230231IRDA_ASSERT(log != NULL, return;);232233discovery = (discovery_t *) hashbin_get_first(log);234while (discovery != NULL) {235IRDA_DEBUG(0, "Discovery:\n");236IRDA_DEBUG(0, " daddr=%08x\n", discovery->data.daddr);237IRDA_DEBUG(0, " saddr=%08x\n", discovery->data.saddr);238IRDA_DEBUG(0, " nickname=%s\n", discovery->data.info);239240discovery = (discovery_t *) hashbin_get_next(log);241}242}243#endif244245/*246* Function irlmp_copy_discoveries (log, pn, mask)247*248* Copy all discoveries in a buffer249*250* This function implement a safe way for lmp clients to access the251* discovery log. The basic problem is that we don't want the log252* to change (add/remove) while the client is reading it. If the253* lmp client manipulate directly the hashbin, he is sure to get254* into troubles...255* The idea is that we copy all the current discovery log in a buffer256* which is specific to the client and pass this copy to him. As we257* do this operation with the spinlock grabbed, we are safe...258* Note : we don't want those clients to grab the spinlock, because259* we have no control on how long they will hold it...260* Note : we choose to copy the log in "struct irda_device_info" to261* save space...262* Note : the client must kfree himself() the log...263* Jean II264*/265struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,266__u16 mask, int old_entries)267{268discovery_t * discovery;269unsigned long flags;270discinfo_t * buffer = NULL;271int j_timeout = (sysctl_discovery_timeout * HZ);272int n; /* Size of the full log */273int i = 0; /* How many we picked */274275IRDA_ASSERT(pn != NULL, return NULL;);276IRDA_ASSERT(log != NULL, return NULL;);277278/* Save spin lock */279spin_lock_irqsave(&log->hb_spinlock, flags);280281discovery = (discovery_t *) hashbin_get_first(log);282while (discovery != NULL) {283/* Mask out the ones we don't want :284* We want to match the discovery mask, and to get only285* the most recent one (unless we want old ones) */286if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&287((old_entries) ||288((jiffies - discovery->firststamp) < j_timeout))) {289/* Create buffer as needed.290* As this function get called a lot and most time291* we don't have anything to put in the log (we are292* quite picky), we can save a lot of overhead293* by not calling kmalloc. Jean II */294if(buffer == NULL) {295/* Create the client specific buffer */296n = HASHBIN_GET_SIZE(log);297buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);298if (buffer == NULL) {299spin_unlock_irqrestore(&log->hb_spinlock, flags);300return NULL;301}302303}304305/* Copy discovery information */306memcpy(&(buffer[i]), &(discovery->data),307sizeof(discinfo_t));308i++;309}310discovery = (discovery_t *) hashbin_get_next(log);311}312313spin_unlock_irqrestore(&log->hb_spinlock, flags);314315/* Get the actual number of device in the buffer and return */316*pn = i;317return buffer;318}319320#ifdef CONFIG_PROC_FS321static inline discovery_t *discovery_seq_idx(loff_t pos)322323{324discovery_t *discovery;325326for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);327discovery != NULL;328discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {329if (pos-- == 0)330break;331}332333return discovery;334}335336static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)337{338spin_lock_irq(&irlmp->cachelog->hb_spinlock);339return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;340}341342static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)343{344++*pos;345return (v == SEQ_START_TOKEN)346? (void *) hashbin_get_first(irlmp->cachelog)347: (void *) hashbin_get_next(irlmp->cachelog);348}349350static void discovery_seq_stop(struct seq_file *seq, void *v)351{352spin_unlock_irq(&irlmp->cachelog->hb_spinlock);353}354355static int discovery_seq_show(struct seq_file *seq, void *v)356{357if (v == SEQ_START_TOKEN)358seq_puts(seq, "IrLMP: Discovery log:\n\n");359else {360const discovery_t *discovery = v;361362seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",363discovery->data.info,364discovery->data.hints[0],365discovery->data.hints[1]);366#if 0367if ( discovery->data.hints[0] & HINT_PNP)368seq_puts(seq, "PnP Compatible ");369if ( discovery->data.hints[0] & HINT_PDA)370seq_puts(seq, "PDA/Palmtop ");371if ( discovery->data.hints[0] & HINT_COMPUTER)372seq_puts(seq, "Computer ");373if ( discovery->data.hints[0] & HINT_PRINTER)374seq_puts(seq, "Printer ");375if ( discovery->data.hints[0] & HINT_MODEM)376seq_puts(seq, "Modem ");377if ( discovery->data.hints[0] & HINT_FAX)378seq_puts(seq, "Fax ");379if ( discovery->data.hints[0] & HINT_LAN)380seq_puts(seq, "LAN Access ");381382if ( discovery->data.hints[1] & HINT_TELEPHONY)383seq_puts(seq, "Telephony ");384if ( discovery->data.hints[1] & HINT_FILE_SERVER)385seq_puts(seq, "File Server ");386if ( discovery->data.hints[1] & HINT_COMM)387seq_puts(seq, "IrCOMM ");388if ( discovery->data.hints[1] & HINT_OBEX)389seq_puts(seq, "IrOBEX ");390#endif391seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",392discovery->data.saddr,393discovery->data.daddr);394395seq_putc(seq, '\n');396}397return 0;398}399400static const struct seq_operations discovery_seq_ops = {401.start = discovery_seq_start,402.next = discovery_seq_next,403.stop = discovery_seq_stop,404.show = discovery_seq_show,405};406407static int discovery_seq_open(struct inode *inode, struct file *file)408{409IRDA_ASSERT(irlmp != NULL, return -EINVAL;);410411return seq_open(file, &discovery_seq_ops);412}413414const struct file_operations discovery_seq_fops = {415.owner = THIS_MODULE,416.open = discovery_seq_open,417.read = seq_read,418.llseek = seq_lseek,419.release = seq_release,420};421#endif422423424