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 38#include <net/irda/irda.h> 39#include <net/irda/irlmp.h> 40 41#include <net/irda/discovery.h> 42 43/* 44 * Function irlmp_add_discovery (cachelog, discovery) 45 * 46 * Add a new discovery to the cachelog, and remove any old discoveries 47 * from the same device 48 * 49 * Note : we try to preserve the time this device was *first* discovered 50 * (as opposed to the time of last discovery used for cleanup). This is 51 * used by clients waiting for discovery events to tell if the device 52 * discovered is "new" or just the same old one. They can't rely there 53 * on a binary flag (new/old), because not all discovery events are 54 * propagated to them, and they might not always listen, so they would 55 * miss some new devices popping up... 56 * Jean II 57 */ 58void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new) 59{ 60 discovery_t *discovery, *node; 61 unsigned long flags; 62 63 /* Set time of first discovery if node is new (see below) */ 64 new->firststamp = new->timestamp; 65 66 spin_lock_irqsave(&cachelog->hb_spinlock, flags); 67 68 /* 69 * Remove all discoveries of devices that has previously been 70 * discovered on the same link with the same name (info), or the 71 * same daddr. We do this since some devices (mostly PDAs) change 72 * their device address between every discovery. 73 */ 74 discovery = (discovery_t *) hashbin_get_first(cachelog); 75 while (discovery != NULL ) { 76 node = discovery; 77 78 /* Be sure to stay one item ahead */ 79 discovery = (discovery_t *) hashbin_get_next(cachelog); 80 81 if ((node->data.saddr == new->data.saddr) && 82 ((node->data.daddr == new->data.daddr) || 83 (strcmp(node->data.info, new->data.info) == 0))) 84 { 85 /* This discovery is a previous discovery 86 * from the same device, so just remove it 87 */ 88 hashbin_remove_this(cachelog, (irda_queue_t *) node); 89 /* Check if hints bits are unchanged */ 90 if(u16ho(node->data.hints) == u16ho(new->data.hints)) 91 /* Set time of first discovery for this node */ 92 new->firststamp = node->firststamp; 93 kfree(node); 94 } 95 } 96 97 /* Insert the new and updated version */ 98 hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL); 99 100 spin_unlock_irqrestore(&cachelog->hb_spinlock, flags); 101} 102 103/* 104 * Function irlmp_add_discovery_log (cachelog, log) 105 * 106 * Merge a disovery log into the cachelog. 107 * 108 */ 109void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log) 110{ 111 discovery_t *discovery; 112 113 IRDA_DEBUG(4, "%s()\n", __FUNCTION__); 114 115 /* 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 } 137 138 /* 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;); 160 IRDA_DEBUG(4, "%s()\n", __FUNCTION__); 161 162 spin_lock_irqsave(&log->hb_spinlock, flags); 163 164 discovery = (discovery_t *) hashbin_get_first(log); 165 while (discovery != NULL) { 166 /* Be sure to be one item ahead */ 167 curr = discovery; 168 discovery = (discovery_t *) hashbin_get_next(log); 169 170 /* Test if it's time to expire this discovery */ 171 if ((curr->data.saddr == saddr) && 172 (force || 173 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT))) 174 { 175 /* Create buffer as needed. 176 * As this function get called a lot and most time 177 * we don't have anything to put in the log (we are 178 * quite picky), we can save a lot of overhead 179 * by not calling kmalloc. Jean II */ 180 if(buffer == NULL) { 181 /* Create the client specific buffer */ 182 n = HASHBIN_GET_SIZE(log); 183 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC); 184 if (buffer == NULL) { 185 spin_unlock_irqrestore(&log->hb_spinlock, flags); 186 return; 187 } 188 189 } 190 191 /* Copy discovery information */ 192 memcpy(&(buffer[i]), &(curr->data), 193 sizeof(discinfo_t)); 194 i++; 195 196 /* Remove it from the log */ 197 curr = hashbin_remove_this(log, (irda_queue_t *) curr); 198 kfree(curr); 199 } 200 } 201 202 /* Drop the spinlock before calling the higher layers, as 203 * we can't guarantee they won't call us back and create a 204 * deadlock. We will work on our own private data, so we 205 * don't care to be interupted. - Jean II */ 206 spin_unlock_irqrestore(&log->hb_spinlock, flags); 207 208 if(buffer == NULL) 209 return; 210 211 /* Tell IrLMP and registered clients about it */ 212 irlmp_discovery_expiry(buffer, i); 213 214 /* Free up our buffer */ 215 kfree(buffer); 216} 217 218 219/* 220 * Function irlmp_copy_discoveries (log, pn, mask) 221 * 222 * Copy all discoveries in a buffer 223 * 224 * This function implement a safe way for lmp clients to access the 225 * discovery log. The basic problem is that we don't want the log 226 * to change (add/remove) while the client is reading it. If the 227 * lmp client manipulate directly the hashbin, he is sure to get 228 * into troubles... 229 * The idea is that we copy all the current discovery log in a buffer 230 * which is specific to the client and pass this copy to him. As we 231 * do this operation with the spinlock grabbed, we are safe... 232 * Note : we don't want those clients to grab the spinlock, because 233 * we have no control on how long they will hold it... 234 * Note : we choose to copy the log in "struct irda_device_info" to 235 * save space... 236 * Note : the client must kfree himself() the log... 237 * Jean II 238 */ 239struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn, 240 __u16 mask, int old_entries) 241{ 242 discovery_t * discovery; 243 unsigned long flags; 244 discinfo_t * buffer = NULL; 245 int j_timeout = (sysctl_discovery_timeout * HZ); 246 int n; /* Size of the full log */ 247 int i = 0; /* How many we picked */ 248 249 IRDA_ASSERT(pn != NULL, return NULL;); 250 IRDA_ASSERT(log != NULL, return NULL;); 251 252 /* Save spin lock */ 253 spin_lock_irqsave(&log->hb_spinlock, flags); 254 255 discovery = (discovery_t *) hashbin_get_first(log); 256 while (discovery != NULL) { 257 /* Mask out the ones we don't want : 258 * We want to match the discovery mask, and to get only 259 * the most recent one (unless we want old ones) */ 260 if ((u16ho(discovery->data.hints) & mask) && 261 ((old_entries) || 262 ((jiffies - discovery->firststamp) < j_timeout)) ) { 263 /* Create buffer as needed. 264 * As this function get called a lot and most time 265 * we don't have anything to put in the log (we are 266 * quite picky), we can save a lot of overhead 267 * by not calling kmalloc. Jean II */ 268 if(buffer == NULL) { 269 /* Create the client specific buffer */ 270 n = HASHBIN_GET_SIZE(log); 271 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC); 272 if (buffer == NULL) { 273 spin_unlock_irqrestore(&log->hb_spinlock, flags); 274 return NULL; 275 } 276 277 } 278 279 /* Copy discovery information */ 280 memcpy(&(buffer[i]), &(discovery->data), 281 sizeof(discinfo_t)); 282 i++; 283 } 284 discovery = (discovery_t *) hashbin_get_next(log); 285 } 286 287 spin_unlock_irqrestore(&log->hb_spinlock, flags); 288 289 /* Get the actual number of device in the buffer and return */ 290 *pn = i; 291 return(buffer); 292} 293 294#ifdef CONFIG_PROC_FS 295static inline discovery_t *discovery_seq_idx(loff_t pos) 296 297{ 298 discovery_t *discovery; 299 300 for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog); 301 discovery != NULL; 302 discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) { 303 if (pos-- == 0) 304 break; 305 } 306 307 return discovery; 308} 309 310static void *discovery_seq_start(struct seq_file *seq, loff_t *pos) 311{ 312 spin_lock_irq(&irlmp->cachelog->hb_spinlock); 313 return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN; 314} 315 316static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos) 317{ 318 ++*pos; 319 return (v == SEQ_START_TOKEN) 320 ? (void *) hashbin_get_first(irlmp->cachelog) 321 : (void *) hashbin_get_next(irlmp->cachelog); 322} 323 324static void discovery_seq_stop(struct seq_file *seq, void *v) 325{ 326 spin_unlock_irq(&irlmp->cachelog->hb_spinlock); 327} 328 329static int discovery_seq_show(struct seq_file *seq, void *v) 330{ 331 if (v == SEQ_START_TOKEN) 332 seq_puts(seq, "IrLMP: Discovery log:\n\n"); 333 else { 334 const discovery_t *discovery = v; 335 336 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x", 337 discovery->data.info, 338 discovery->data.hints[0], 339 discovery->data.hints[1]); 340 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n", 341 discovery->data.saddr, 342 discovery->data.daddr); 343 344 seq_putc(seq, '\n'); 345 } 346 return 0; 347} 348 349static struct seq_operations discovery_seq_ops = { 350 .start = discovery_seq_start, 351 .next = discovery_seq_next, 352 .stop = discovery_seq_stop, 353 .show = discovery_seq_show, 354}; 355 356static int discovery_seq_open(struct inode *inode, struct file *file) 357{ 358 IRDA_ASSERT(irlmp != NULL, return -EINVAL;); 359 360 return seq_open(file, &discovery_seq_ops); 361} 362 363const struct file_operations discovery_seq_fops = { 364 .owner = THIS_MODULE, 365 .open = discovery_seq_open, 366 .read = seq_read, 367 .llseek = seq_lseek, 368 .release = seq_release, 369}; 370#endif 371