discovery.c 13 KB

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