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