discovery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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, see <http://www.gnu.org/licenses/>.
  28. *
  29. ********************************************************************/
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.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. /*
  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. spin_lock_irqsave(&log->hb_spinlock, flags);
  142. discovery = (discovery_t *) hashbin_get_first(log);
  143. while (discovery != NULL) {
  144. /* Be sure to be one item ahead */
  145. curr = discovery;
  146. discovery = (discovery_t *) hashbin_get_next(log);
  147. /* Test if it's time to expire this discovery */
  148. if ((curr->data.saddr == saddr) &&
  149. (force ||
  150. ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
  151. {
  152. /* Create buffer as needed.
  153. * As this function get called a lot and most time
  154. * we don't have anything to put in the log (we are
  155. * quite picky), we can save a lot of overhead
  156. * by not calling kmalloc. Jean II */
  157. if(buffer == NULL) {
  158. /* Create the client specific buffer */
  159. n = HASHBIN_GET_SIZE(log);
  160. buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
  161. if (buffer == NULL) {
  162. spin_unlock_irqrestore(&log->hb_spinlock, flags);
  163. return;
  164. }
  165. }
  166. /* Copy discovery information */
  167. memcpy(&(buffer[i]), &(curr->data),
  168. sizeof(discinfo_t));
  169. i++;
  170. /* Remove it from the log */
  171. curr = hashbin_remove_this(log, (irda_queue_t *) curr);
  172. kfree(curr);
  173. }
  174. }
  175. /* Drop the spinlock before calling the higher layers, as
  176. * we can't guarantee they won't call us back and create a
  177. * deadlock. We will work on our own private data, so we
  178. * don't care to be interrupted. - Jean II */
  179. spin_unlock_irqrestore(&log->hb_spinlock, flags);
  180. if(buffer == NULL)
  181. return;
  182. /* Tell IrLMP and registered clients about it */
  183. irlmp_discovery_expiry(buffer, i);
  184. /* Free up our buffer */
  185. kfree(buffer);
  186. }
  187. #if 0
  188. /*
  189. * Function irlmp_dump_discoveries (log)
  190. *
  191. * Print out all discoveries in log
  192. *
  193. */
  194. void irlmp_dump_discoveries(hashbin_t *log)
  195. {
  196. discovery_t *discovery;
  197. IRDA_ASSERT(log != NULL, return;);
  198. discovery = (discovery_t *) hashbin_get_first(log);
  199. while (discovery != NULL) {
  200. pr_debug("Discovery:\n");
  201. pr_debug(" daddr=%08x\n", discovery->data.daddr);
  202. pr_debug(" saddr=%08x\n", discovery->data.saddr);
  203. pr_debug(" nickname=%s\n", discovery->data.info);
  204. discovery = (discovery_t *) hashbin_get_next(log);
  205. }
  206. }
  207. #endif
  208. /*
  209. * Function irlmp_copy_discoveries (log, pn, mask)
  210. *
  211. * Copy all discoveries in a buffer
  212. *
  213. * This function implement a safe way for lmp clients to access the
  214. * discovery log. The basic problem is that we don't want the log
  215. * to change (add/remove) while the client is reading it. If the
  216. * lmp client manipulate directly the hashbin, he is sure to get
  217. * into troubles...
  218. * The idea is that we copy all the current discovery log in a buffer
  219. * which is specific to the client and pass this copy to him. As we
  220. * do this operation with the spinlock grabbed, we are safe...
  221. * Note : we don't want those clients to grab the spinlock, because
  222. * we have no control on how long they will hold it...
  223. * Note : we choose to copy the log in "struct irda_device_info" to
  224. * save space...
  225. * Note : the client must kfree himself() the log...
  226. * Jean II
  227. */
  228. struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
  229. __u16 mask, int old_entries)
  230. {
  231. discovery_t * discovery;
  232. unsigned long flags;
  233. discinfo_t * buffer = NULL;
  234. int j_timeout = (sysctl_discovery_timeout * HZ);
  235. int n; /* Size of the full log */
  236. int i = 0; /* How many we picked */
  237. IRDA_ASSERT(pn != NULL, return NULL;);
  238. IRDA_ASSERT(log != NULL, return NULL;);
  239. /* Save spin lock */
  240. spin_lock_irqsave(&log->hb_spinlock, flags);
  241. discovery = (discovery_t *) hashbin_get_first(log);
  242. while (discovery != NULL) {
  243. /* Mask out the ones we don't want :
  244. * We want to match the discovery mask, and to get only
  245. * the most recent one (unless we want old ones) */
  246. if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
  247. ((old_entries) ||
  248. ((jiffies - discovery->firststamp) < j_timeout))) {
  249. /* Create buffer as needed.
  250. * As this function get called a lot and most time
  251. * we don't have anything to put in the log (we are
  252. * quite picky), we can save a lot of overhead
  253. * by not calling kmalloc. Jean II */
  254. if(buffer == NULL) {
  255. /* Create the client specific buffer */
  256. n = HASHBIN_GET_SIZE(log);
  257. buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
  258. if (buffer == NULL) {
  259. spin_unlock_irqrestore(&log->hb_spinlock, flags);
  260. return NULL;
  261. }
  262. }
  263. /* Copy discovery information */
  264. memcpy(&(buffer[i]), &(discovery->data),
  265. sizeof(discinfo_t));
  266. i++;
  267. }
  268. discovery = (discovery_t *) hashbin_get_next(log);
  269. }
  270. spin_unlock_irqrestore(&log->hb_spinlock, flags);
  271. /* Get the actual number of device in the buffer and return */
  272. *pn = i;
  273. return buffer;
  274. }
  275. #ifdef CONFIG_PROC_FS
  276. static inline discovery_t *discovery_seq_idx(loff_t pos)
  277. {
  278. discovery_t *discovery;
  279. for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
  280. discovery != NULL;
  281. discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
  282. if (pos-- == 0)
  283. break;
  284. }
  285. return discovery;
  286. }
  287. static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
  288. {
  289. spin_lock_irq(&irlmp->cachelog->hb_spinlock);
  290. return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
  291. }
  292. static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  293. {
  294. ++*pos;
  295. return (v == SEQ_START_TOKEN)
  296. ? (void *) hashbin_get_first(irlmp->cachelog)
  297. : (void *) hashbin_get_next(irlmp->cachelog);
  298. }
  299. static void discovery_seq_stop(struct seq_file *seq, void *v)
  300. {
  301. spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
  302. }
  303. static int discovery_seq_show(struct seq_file *seq, void *v)
  304. {
  305. if (v == SEQ_START_TOKEN)
  306. seq_puts(seq, "IrLMP: Discovery log:\n\n");
  307. else {
  308. const discovery_t *discovery = v;
  309. seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
  310. discovery->data.info,
  311. discovery->data.hints[0],
  312. discovery->data.hints[1]);
  313. #if 0
  314. if ( discovery->data.hints[0] & HINT_PNP)
  315. seq_puts(seq, "PnP Compatible ");
  316. if ( discovery->data.hints[0] & HINT_PDA)
  317. seq_puts(seq, "PDA/Palmtop ");
  318. if ( discovery->data.hints[0] & HINT_COMPUTER)
  319. seq_puts(seq, "Computer ");
  320. if ( discovery->data.hints[0] & HINT_PRINTER)
  321. seq_puts(seq, "Printer ");
  322. if ( discovery->data.hints[0] & HINT_MODEM)
  323. seq_puts(seq, "Modem ");
  324. if ( discovery->data.hints[0] & HINT_FAX)
  325. seq_puts(seq, "Fax ");
  326. if ( discovery->data.hints[0] & HINT_LAN)
  327. seq_puts(seq, "LAN Access ");
  328. if ( discovery->data.hints[1] & HINT_TELEPHONY)
  329. seq_puts(seq, "Telephony ");
  330. if ( discovery->data.hints[1] & HINT_FILE_SERVER)
  331. seq_puts(seq, "File Server ");
  332. if ( discovery->data.hints[1] & HINT_COMM)
  333. seq_puts(seq, "IrCOMM ");
  334. if ( discovery->data.hints[1] & HINT_OBEX)
  335. seq_puts(seq, "IrOBEX ");
  336. #endif
  337. seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
  338. discovery->data.saddr,
  339. discovery->data.daddr);
  340. seq_putc(seq, '\n');
  341. }
  342. return 0;
  343. }
  344. static const struct seq_operations discovery_seq_ops = {
  345. .start = discovery_seq_start,
  346. .next = discovery_seq_next,
  347. .stop = discovery_seq_stop,
  348. .show = discovery_seq_show,
  349. };
  350. static int discovery_seq_open(struct inode *inode, struct file *file)
  351. {
  352. IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
  353. return seq_open(file, &discovery_seq_ops);
  354. }
  355. const struct file_operations discovery_seq_fops = {
  356. .owner = THIS_MODULE,
  357. .open = discovery_seq_open,
  358. .read = seq_read,
  359. .llseek = seq_lseek,
  360. .release = seq_release,
  361. };
  362. #endif