discovery.c 12 KB

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