Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 57 | void 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 | */ |
| 108 | void 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 | */ |
| 149 | void 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 | if (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 | #if 0 |
| 219 | /* |
| 220 | * Function irlmp_dump_discoveries (log) |
| 221 | * |
| 222 | * Print out all discoveries in log |
| 223 | * |
| 224 | */ |
| 225 | void irlmp_dump_discoveries(hashbin_t *log) |
| 226 | { |
| 227 | discovery_t *discovery; |
| 228 | |
| 229 | IRDA_ASSERT(log != NULL, return;); |
| 230 | |
| 231 | discovery = (discovery_t *) hashbin_get_first(log); |
| 232 | while (discovery != NULL) { |
| 233 | IRDA_DEBUG(0, "Discovery:\n"); |
| 234 | IRDA_DEBUG(0, " daddr=%08x\n", discovery->data.daddr); |
| 235 | IRDA_DEBUG(0, " saddr=%08x\n", discovery->data.saddr); |
| 236 | IRDA_DEBUG(0, " nickname=%s\n", discovery->data.info); |
| 237 | |
| 238 | discovery = (discovery_t *) hashbin_get_next(log); |
| 239 | } |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | /* |
| 244 | * Function irlmp_copy_discoveries (log, pn, mask) |
| 245 | * |
| 246 | * Copy all discoveries in a buffer |
| 247 | * |
| 248 | * This function implement a safe way for lmp clients to access the |
| 249 | * discovery log. The basic problem is that we don't want the log |
| 250 | * to change (add/remove) while the client is reading it. If the |
| 251 | * lmp client manipulate directly the hashbin, he is sure to get |
| 252 | * into troubles... |
| 253 | * The idea is that we copy all the current discovery log in a buffer |
| 254 | * which is specific to the client and pass this copy to him. As we |
| 255 | * do this operation with the spinlock grabbed, we are safe... |
| 256 | * Note : we don't want those clients to grab the spinlock, because |
| 257 | * we have no control on how long they will hold it... |
| 258 | * Note : we choose to copy the log in "struct irda_device_info" to |
| 259 | * save space... |
| 260 | * Note : the client must kfree himself() the log... |
| 261 | * Jean II |
| 262 | */ |
| 263 | struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn, |
| 264 | __u16 mask, int old_entries) |
| 265 | { |
| 266 | discovery_t * discovery; |
| 267 | unsigned long flags; |
| 268 | discinfo_t * buffer = NULL; |
| 269 | int j_timeout = (sysctl_discovery_timeout * HZ); |
| 270 | int n; /* Size of the full log */ |
| 271 | int i = 0; /* How many we picked */ |
| 272 | |
| 273 | IRDA_ASSERT(pn != NULL, return NULL;); |
| 274 | IRDA_ASSERT(log != NULL, return NULL;); |
| 275 | |
| 276 | /* Save spin lock */ |
| 277 | spin_lock_irqsave(&log->hb_spinlock, flags); |
| 278 | |
| 279 | discovery = (discovery_t *) hashbin_get_first(log); |
| 280 | while (discovery != NULL) { |
| 281 | /* Mask out the ones we don't want : |
| 282 | * We want to match the discovery mask, and to get only |
| 283 | * the most recent one (unless we want old ones) */ |
| 284 | if ((u16ho(discovery->data.hints) & mask) && |
| 285 | ((old_entries) || |
| 286 | ((jiffies - discovery->firststamp) < j_timeout)) ) { |
| 287 | /* Create buffer as needed. |
| 288 | * As this function get called a lot and most time |
| 289 | * we don't have anything to put in the log (we are |
| 290 | * quite picky), we can save a lot of overhead |
| 291 | * by not calling kmalloc. Jean II */ |
| 292 | if(buffer == NULL) { |
| 293 | /* Create the client specific buffer */ |
| 294 | n = HASHBIN_GET_SIZE(log); |
| 295 | buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC); |
| 296 | if (buffer == NULL) { |
| 297 | spin_unlock_irqrestore(&log->hb_spinlock, flags); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | |
| 303 | /* Copy discovery information */ |
| 304 | memcpy(&(buffer[i]), &(discovery->data), |
| 305 | sizeof(discinfo_t)); |
| 306 | i++; |
| 307 | } |
| 308 | discovery = (discovery_t *) hashbin_get_next(log); |
| 309 | } |
| 310 | |
| 311 | spin_unlock_irqrestore(&log->hb_spinlock, flags); |
| 312 | |
| 313 | /* Get the actual number of device in the buffer and return */ |
| 314 | *pn = i; |
| 315 | return(buffer); |
| 316 | } |
| 317 | |
| 318 | #ifdef CONFIG_PROC_FS |
| 319 | static inline discovery_t *discovery_seq_idx(loff_t pos) |
| 320 | |
| 321 | { |
| 322 | discovery_t *discovery; |
| 323 | |
| 324 | for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog); |
| 325 | discovery != NULL; |
| 326 | discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) { |
| 327 | if (pos-- == 0) |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | return discovery; |
| 332 | } |
| 333 | |
| 334 | static void *discovery_seq_start(struct seq_file *seq, loff_t *pos) |
| 335 | { |
| 336 | spin_lock_irq(&irlmp->cachelog->hb_spinlock); |
| 337 | return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN; |
| 338 | } |
| 339 | |
| 340 | static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 341 | { |
| 342 | ++*pos; |
| 343 | return (v == SEQ_START_TOKEN) |
| 344 | ? (void *) hashbin_get_first(irlmp->cachelog) |
| 345 | : (void *) hashbin_get_next(irlmp->cachelog); |
| 346 | } |
| 347 | |
| 348 | static void discovery_seq_stop(struct seq_file *seq, void *v) |
| 349 | { |
| 350 | spin_unlock_irq(&irlmp->cachelog->hb_spinlock); |
| 351 | } |
| 352 | |
| 353 | static int discovery_seq_show(struct seq_file *seq, void *v) |
| 354 | { |
| 355 | if (v == SEQ_START_TOKEN) |
| 356 | seq_puts(seq, "IrLMP: Discovery log:\n\n"); |
| 357 | else { |
| 358 | const discovery_t *discovery = v; |
| 359 | |
| 360 | seq_printf(seq, "nickname: %s, hint: 0x%02x%02x", |
| 361 | discovery->data.info, |
| 362 | discovery->data.hints[0], |
| 363 | discovery->data.hints[1]); |
| 364 | #if 0 |
| 365 | if ( discovery->data.hints[0] & HINT_PNP) |
| 366 | seq_puts(seq, "PnP Compatible "); |
| 367 | if ( discovery->data.hints[0] & HINT_PDA) |
| 368 | seq_puts(seq, "PDA/Palmtop "); |
| 369 | if ( discovery->data.hints[0] & HINT_COMPUTER) |
| 370 | seq_puts(seq, "Computer "); |
| 371 | if ( discovery->data.hints[0] & HINT_PRINTER) |
| 372 | seq_puts(seq, "Printer "); |
| 373 | if ( discovery->data.hints[0] & HINT_MODEM) |
| 374 | seq_puts(seq, "Modem "); |
| 375 | if ( discovery->data.hints[0] & HINT_FAX) |
| 376 | seq_puts(seq, "Fax "); |
| 377 | if ( discovery->data.hints[0] & HINT_LAN) |
| 378 | seq_puts(seq, "LAN Access "); |
| 379 | |
| 380 | if ( discovery->data.hints[1] & HINT_TELEPHONY) |
| 381 | seq_puts(seq, "Telephony "); |
| 382 | if ( discovery->data.hints[1] & HINT_FILE_SERVER) |
| 383 | seq_puts(seq, "File Server "); |
| 384 | if ( discovery->data.hints[1] & HINT_COMM) |
| 385 | seq_puts(seq, "IrCOMM "); |
| 386 | if ( discovery->data.hints[1] & HINT_OBEX) |
| 387 | seq_puts(seq, "IrOBEX "); |
| 388 | #endif |
| 389 | seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n", |
| 390 | discovery->data.saddr, |
| 391 | discovery->data.daddr); |
| 392 | |
| 393 | seq_putc(seq, '\n'); |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static struct seq_operations discovery_seq_ops = { |
| 399 | .start = discovery_seq_start, |
| 400 | .next = discovery_seq_next, |
| 401 | .stop = discovery_seq_stop, |
| 402 | .show = discovery_seq_show, |
| 403 | }; |
| 404 | |
| 405 | static int discovery_seq_open(struct inode *inode, struct file *file) |
| 406 | { |
| 407 | IRDA_ASSERT(irlmp != NULL, return -EINVAL;); |
| 408 | |
| 409 | return seq_open(file, &discovery_seq_ops); |
| 410 | } |
| 411 | |
| 412 | struct file_operations discovery_seq_fops = { |
| 413 | .owner = THIS_MODULE, |
| 414 | .open = discovery_seq_open, |
| 415 | .read = seq_read, |
| 416 | .llseek = seq_lseek, |
| 417 | .release = seq_release, |
| 418 | }; |
| 419 | #endif |