discovery.c 12 KB

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