irlmp.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. /*********************************************************************
  2. *
  3. * Filename: irlmp.c
  4. * Version: 1.0
  5. * Description: IrDA Link Management Protocol (LMP) layer
  6. * Status: Stable.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 17 20:54:32 1997
  9. * Modified at: Wed Jan 5 11:26:03 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
  13. * All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  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. * Neither Dag Brattli nor University of Tromsø admit liability nor
  22. * provide warranty for any of this software. This material is
  23. * provided "AS-IS" and at no charge.
  24. *
  25. ********************************************************************/
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/types.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/init.h>
  34. #include <linux/kmod.h>
  35. #include <linux/random.h>
  36. #include <linux/seq_file.h>
  37. #include <net/irda/irda.h>
  38. #include <net/irda/timer.h>
  39. #include <net/irda/qos.h>
  40. #include <net/irda/irlap.h>
  41. #include <net/irda/iriap.h>
  42. #include <net/irda/irlmp.h>
  43. #include <net/irda/irlmp_frame.h>
  44. static __u8 irlmp_find_free_slsap(void);
  45. static int irlmp_slsap_inuse(__u8 slsap_sel);
  46. /* Master structure */
  47. struct irlmp_cb *irlmp = NULL;
  48. /* These can be altered by the sysctl interface */
  49. int sysctl_discovery = 0;
  50. int sysctl_discovery_timeout = 3; /* 3 seconds by default */
  51. int sysctl_discovery_slots = 6; /* 6 slots by default */
  52. int sysctl_lap_keepalive_time = LM_IDLE_TIMEOUT * 1000 / HZ;
  53. char sysctl_devname[65];
  54. const char *irlmp_reasons[] = {
  55. "ERROR, NOT USED",
  56. "LM_USER_REQUEST",
  57. "LM_LAP_DISCONNECT",
  58. "LM_CONNECT_FAILURE",
  59. "LM_LAP_RESET",
  60. "LM_INIT_DISCONNECT",
  61. "ERROR, NOT USED",
  62. };
  63. /*
  64. * Function irlmp_init (void)
  65. *
  66. * Create (allocate) the main IrLMP structure
  67. *
  68. */
  69. int __init irlmp_init(void)
  70. {
  71. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  72. /* Initialize the irlmp structure. */
  73. irlmp = kmalloc( sizeof(struct irlmp_cb), GFP_KERNEL);
  74. if (irlmp == NULL)
  75. return -ENOMEM;
  76. memset(irlmp, 0, sizeof(struct irlmp_cb));
  77. irlmp->magic = LMP_MAGIC;
  78. irlmp->clients = hashbin_new(HB_LOCK);
  79. irlmp->services = hashbin_new(HB_LOCK);
  80. irlmp->links = hashbin_new(HB_LOCK);
  81. irlmp->unconnected_lsaps = hashbin_new(HB_LOCK);
  82. irlmp->cachelog = hashbin_new(HB_NOLOCK);
  83. if ((irlmp->clients == NULL) ||
  84. (irlmp->services == NULL) ||
  85. (irlmp->links == NULL) ||
  86. (irlmp->unconnected_lsaps == NULL) ||
  87. (irlmp->cachelog == NULL)) {
  88. return -ENOMEM;
  89. }
  90. spin_lock_init(&irlmp->cachelog->hb_spinlock);
  91. irlmp->last_lsap_sel = 0x0f; /* Reserved 0x00-0x0f */
  92. strcpy(sysctl_devname, "Linux");
  93. /* Do discovery every 3 seconds */
  94. init_timer(&irlmp->discovery_timer);
  95. irlmp_start_discovery_timer(irlmp, sysctl_discovery_timeout*HZ);
  96. return 0;
  97. }
  98. /*
  99. * Function irlmp_cleanup (void)
  100. *
  101. * Remove IrLMP layer
  102. *
  103. */
  104. void __exit irlmp_cleanup(void)
  105. {
  106. /* Check for main structure */
  107. IRDA_ASSERT(irlmp != NULL, return;);
  108. IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return;);
  109. del_timer(&irlmp->discovery_timer);
  110. hashbin_delete(irlmp->links, (FREE_FUNC) kfree);
  111. hashbin_delete(irlmp->unconnected_lsaps, (FREE_FUNC) kfree);
  112. hashbin_delete(irlmp->clients, (FREE_FUNC) kfree);
  113. hashbin_delete(irlmp->services, (FREE_FUNC) kfree);
  114. hashbin_delete(irlmp->cachelog, (FREE_FUNC) kfree);
  115. /* De-allocate main structure */
  116. kfree(irlmp);
  117. irlmp = NULL;
  118. }
  119. /*
  120. * Function irlmp_open_lsap (slsap, notify)
  121. *
  122. * Register with IrLMP and create a local LSAP,
  123. * returns handle to LSAP.
  124. */
  125. struct lsap_cb *irlmp_open_lsap(__u8 slsap_sel, notify_t *notify, __u8 pid)
  126. {
  127. struct lsap_cb *self;
  128. IRDA_ASSERT(notify != NULL, return NULL;);
  129. IRDA_ASSERT(irlmp != NULL, return NULL;);
  130. IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return NULL;);
  131. IRDA_ASSERT(notify->instance != NULL, return NULL;);
  132. /* Does the client care which Source LSAP selector it gets? */
  133. if (slsap_sel == LSAP_ANY) {
  134. slsap_sel = irlmp_find_free_slsap();
  135. if (!slsap_sel)
  136. return NULL;
  137. } else if (irlmp_slsap_inuse(slsap_sel))
  138. return NULL;
  139. /* Allocate new instance of a LSAP connection */
  140. self = kmalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
  141. if (self == NULL) {
  142. IRDA_ERROR("%s: can't allocate memory\n", __FUNCTION__);
  143. return NULL;
  144. }
  145. memset(self, 0, sizeof(struct lsap_cb));
  146. self->magic = LMP_LSAP_MAGIC;
  147. self->slsap_sel = slsap_sel;
  148. /* Fix connectionless LSAP's */
  149. if (slsap_sel == LSAP_CONNLESS) {
  150. #ifdef CONFIG_IRDA_ULTRA
  151. self->dlsap_sel = LSAP_CONNLESS;
  152. self->pid = pid;
  153. #endif /* CONFIG_IRDA_ULTRA */
  154. } else
  155. self->dlsap_sel = LSAP_ANY;
  156. /* self->connected = FALSE; -> already NULL via memset() */
  157. init_timer(&self->watchdog_timer);
  158. self->notify = *notify;
  159. self->lsap_state = LSAP_DISCONNECTED;
  160. /* Insert into queue of unconnected LSAPs */
  161. hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
  162. (long) self, NULL);
  163. return self;
  164. }
  165. EXPORT_SYMBOL(irlmp_open_lsap);
  166. /*
  167. * Function __irlmp_close_lsap (self)
  168. *
  169. * Remove an instance of LSAP
  170. */
  171. static void __irlmp_close_lsap(struct lsap_cb *self)
  172. {
  173. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  174. IRDA_ASSERT(self != NULL, return;);
  175. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  176. /*
  177. * Set some of the variables to preset values
  178. */
  179. self->magic = 0;
  180. del_timer(&self->watchdog_timer); /* Important! */
  181. if (self->conn_skb)
  182. dev_kfree_skb(self->conn_skb);
  183. kfree(self);
  184. }
  185. /*
  186. * Function irlmp_close_lsap (self)
  187. *
  188. * Close and remove LSAP
  189. *
  190. */
  191. void irlmp_close_lsap(struct lsap_cb *self)
  192. {
  193. struct lap_cb *lap;
  194. struct lsap_cb *lsap = NULL;
  195. IRDA_ASSERT(self != NULL, return;);
  196. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  197. /*
  198. * Find out if we should remove this LSAP from a link or from the
  199. * list of unconnected lsaps (not associated with a link)
  200. */
  201. lap = self->lap;
  202. if (lap) {
  203. IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
  204. /* We might close a LSAP before it has completed the
  205. * connection setup. In those case, higher layers won't
  206. * send a proper disconnect request. Harmless, except
  207. * that we will forget to close LAP... - Jean II */
  208. if(self->lsap_state != LSAP_DISCONNECTED) {
  209. self->lsap_state = LSAP_DISCONNECTED;
  210. irlmp_do_lap_event(self->lap,
  211. LM_LAP_DISCONNECT_REQUEST, NULL);
  212. }
  213. /* Now, remove from the link */
  214. lsap = hashbin_remove(lap->lsaps, (long) self, NULL);
  215. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  216. lap->cache.valid = FALSE;
  217. #endif
  218. }
  219. self->lap = NULL;
  220. /* Check if we found the LSAP! If not then try the unconnected lsaps */
  221. if (!lsap) {
  222. lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self,
  223. NULL);
  224. }
  225. if (!lsap) {
  226. IRDA_DEBUG(0,
  227. "%s(), Looks like somebody has removed me already!\n",
  228. __FUNCTION__);
  229. return;
  230. }
  231. __irlmp_close_lsap(self);
  232. }
  233. EXPORT_SYMBOL(irlmp_close_lsap);
  234. /*
  235. * Function irlmp_register_irlap (saddr, notify)
  236. *
  237. * Register IrLAP layer with IrLMP. There is possible to have multiple
  238. * instances of the IrLAP layer, each connected to different IrDA ports
  239. *
  240. */
  241. void irlmp_register_link(struct irlap_cb *irlap, __u32 saddr, notify_t *notify)
  242. {
  243. struct lap_cb *lap;
  244. IRDA_ASSERT(irlmp != NULL, return;);
  245. IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return;);
  246. IRDA_ASSERT(notify != NULL, return;);
  247. /*
  248. * Allocate new instance of a LSAP connection
  249. */
  250. lap = kmalloc(sizeof(struct lap_cb), GFP_KERNEL);
  251. if (lap == NULL) {
  252. IRDA_ERROR("%s: unable to kmalloc\n", __FUNCTION__);
  253. return;
  254. }
  255. memset(lap, 0, sizeof(struct lap_cb));
  256. lap->irlap = irlap;
  257. lap->magic = LMP_LAP_MAGIC;
  258. lap->saddr = saddr;
  259. lap->daddr = DEV_ADDR_ANY;
  260. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  261. lap->cache.valid = FALSE;
  262. #endif
  263. lap->lsaps = hashbin_new(HB_LOCK);
  264. if (lap->lsaps == NULL) {
  265. IRDA_WARNING("%s(), unable to kmalloc lsaps\n", __FUNCTION__);
  266. kfree(lap);
  267. return;
  268. }
  269. lap->lap_state = LAP_STANDBY;
  270. init_timer(&lap->idle_timer);
  271. /*
  272. * Insert into queue of LMP links
  273. */
  274. hashbin_insert(irlmp->links, (irda_queue_t *) lap, lap->saddr, NULL);
  275. /*
  276. * We set only this variable so IrLAP can tell us on which link the
  277. * different events happened on
  278. */
  279. irda_notify_init(notify);
  280. notify->instance = lap;
  281. }
  282. /*
  283. * Function irlmp_unregister_irlap (saddr)
  284. *
  285. * IrLAP layer has been removed!
  286. *
  287. */
  288. void irlmp_unregister_link(__u32 saddr)
  289. {
  290. struct lap_cb *link;
  291. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  292. /* We must remove ourselves from the hashbin *first*. This ensure
  293. * that no more LSAPs will be open on this link and no discovery
  294. * will be triggered anymore. Jean II */
  295. link = hashbin_remove(irlmp->links, saddr, NULL);
  296. if (link) {
  297. IRDA_ASSERT(link->magic == LMP_LAP_MAGIC, return;);
  298. /* Kill all the LSAPs on this link. Jean II */
  299. link->reason = LAP_DISC_INDICATION;
  300. link->daddr = DEV_ADDR_ANY;
  301. irlmp_do_lap_event(link, LM_LAP_DISCONNECT_INDICATION, NULL);
  302. /* Remove all discoveries discovered at this link */
  303. irlmp_expire_discoveries(irlmp->cachelog, link->saddr, TRUE);
  304. /* Final cleanup */
  305. del_timer(&link->idle_timer);
  306. link->magic = 0;
  307. kfree(link);
  308. }
  309. }
  310. /*
  311. * Function irlmp_connect_request (handle, dlsap, userdata)
  312. *
  313. * Connect with a peer LSAP
  314. *
  315. */
  316. int irlmp_connect_request(struct lsap_cb *self, __u8 dlsap_sel,
  317. __u32 saddr, __u32 daddr,
  318. struct qos_info *qos, struct sk_buff *userdata)
  319. {
  320. struct sk_buff *tx_skb = userdata;
  321. struct lap_cb *lap;
  322. struct lsap_cb *lsap;
  323. int ret;
  324. IRDA_ASSERT(self != NULL, return -EBADR;);
  325. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -EBADR;);
  326. IRDA_DEBUG(2,
  327. "%s(), slsap_sel=%02x, dlsap_sel=%02x, saddr=%08x, daddr=%08x\n",
  328. __FUNCTION__, self->slsap_sel, dlsap_sel, saddr, daddr);
  329. if (test_bit(0, &self->connected)) {
  330. ret = -EISCONN;
  331. goto err;
  332. }
  333. /* Client must supply destination device address */
  334. if (!daddr) {
  335. ret = -EINVAL;
  336. goto err;
  337. }
  338. /* Any userdata? */
  339. if (tx_skb == NULL) {
  340. tx_skb = dev_alloc_skb(64);
  341. if (!tx_skb)
  342. return -ENOMEM;
  343. skb_reserve(tx_skb, LMP_MAX_HEADER);
  344. }
  345. /* Make room for MUX control header (3 bytes) */
  346. IRDA_ASSERT(skb_headroom(tx_skb) >= LMP_CONTROL_HEADER, return -1;);
  347. skb_push(tx_skb, LMP_CONTROL_HEADER);
  348. self->dlsap_sel = dlsap_sel;
  349. /*
  350. * Find the link to where we should try to connect since there may
  351. * be more than one IrDA port on this machine. If the client has
  352. * passed us the saddr (and already knows which link to use), then
  353. * we use that to find the link, if not then we have to look in the
  354. * discovery log and check if any of the links has discovered a
  355. * device with the given daddr
  356. */
  357. if ((!saddr) || (saddr == DEV_ADDR_ANY)) {
  358. discovery_t *discovery;
  359. unsigned long flags;
  360. spin_lock_irqsave(&irlmp->cachelog->hb_spinlock, flags);
  361. if (daddr != DEV_ADDR_ANY)
  362. discovery = hashbin_find(irlmp->cachelog, daddr, NULL);
  363. else {
  364. IRDA_DEBUG(2, "%s(), no daddr\n", __FUNCTION__);
  365. discovery = (discovery_t *)
  366. hashbin_get_first(irlmp->cachelog);
  367. }
  368. if (discovery) {
  369. saddr = discovery->data.saddr;
  370. daddr = discovery->data.daddr;
  371. }
  372. spin_unlock_irqrestore(&irlmp->cachelog->hb_spinlock, flags);
  373. }
  374. lap = hashbin_lock_find(irlmp->links, saddr, NULL);
  375. if (lap == NULL) {
  376. IRDA_DEBUG(1, "%s(), Unable to find a usable link!\n", __FUNCTION__);
  377. ret = -EHOSTUNREACH;
  378. goto err;
  379. }
  380. /* Check if LAP is disconnected or already connected */
  381. if (lap->daddr == DEV_ADDR_ANY)
  382. lap->daddr = daddr;
  383. else if (lap->daddr != daddr) {
  384. /* Check if some LSAPs are active on this LAP */
  385. if (HASHBIN_GET_SIZE(lap->lsaps) == 0) {
  386. /* No active connection, but LAP hasn't been
  387. * disconnected yet (waiting for timeout in LAP).
  388. * Maybe we could give LAP a bit of help in this case.
  389. */
  390. IRDA_DEBUG(0, "%s(), sorry, but I'm waiting for LAP to timeout!\n", __FUNCTION__);
  391. ret = -EAGAIN;
  392. goto err;
  393. }
  394. /* LAP is already connected to a different node, and LAP
  395. * can only talk to one node at a time */
  396. IRDA_DEBUG(0, "%s(), sorry, but link is busy!\n", __FUNCTION__);
  397. ret = -EBUSY;
  398. goto err;
  399. }
  400. self->lap = lap;
  401. /*
  402. * Remove LSAP from list of unconnected LSAPs and insert it into the
  403. * list of connected LSAPs for the particular link
  404. */
  405. lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self, NULL);
  406. IRDA_ASSERT(lsap != NULL, return -1;);
  407. IRDA_ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
  408. IRDA_ASSERT(lsap->lap != NULL, return -1;);
  409. IRDA_ASSERT(lsap->lap->magic == LMP_LAP_MAGIC, return -1;);
  410. hashbin_insert(self->lap->lsaps, (irda_queue_t *) self, (long) self,
  411. NULL);
  412. set_bit(0, &self->connected); /* TRUE */
  413. /*
  414. * User supplied qos specifications?
  415. */
  416. if (qos)
  417. self->qos = *qos;
  418. irlmp_do_lsap_event(self, LM_CONNECT_REQUEST, tx_skb);
  419. /* Drop reference count - see irlap_data_request(). */
  420. dev_kfree_skb(tx_skb);
  421. return 0;
  422. err:
  423. /* Cleanup */
  424. if(tx_skb)
  425. dev_kfree_skb(tx_skb);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(irlmp_connect_request);
  429. /*
  430. * Function irlmp_connect_indication (self)
  431. *
  432. * Incoming connection
  433. *
  434. */
  435. void irlmp_connect_indication(struct lsap_cb *self, struct sk_buff *skb)
  436. {
  437. int max_seg_size;
  438. int lap_header_size;
  439. int max_header_size;
  440. IRDA_ASSERT(self != NULL, return;);
  441. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  442. IRDA_ASSERT(skb != NULL, return;);
  443. IRDA_ASSERT(self->lap != NULL, return;);
  444. IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
  445. __FUNCTION__, self->slsap_sel, self->dlsap_sel);
  446. /* Note : self->lap is set in irlmp_link_data_indication(),
  447. * (case CONNECT_CMD:) because we have no way to set it here.
  448. * Similarly, self->dlsap_sel is usually set in irlmp_find_lsap().
  449. * Jean II */
  450. self->qos = *self->lap->qos;
  451. max_seg_size = self->lap->qos->data_size.value-LMP_HEADER;
  452. lap_header_size = IRLAP_GET_HEADER_SIZE(self->lap->irlap);
  453. max_header_size = LMP_HEADER + lap_header_size;
  454. /* Hide LMP_CONTROL_HEADER header from layer above */
  455. skb_pull(skb, LMP_CONTROL_HEADER);
  456. if (self->notify.connect_indication) {
  457. /* Don't forget to refcount it - see irlap_driver_rcv(). */
  458. skb_get(skb);
  459. self->notify.connect_indication(self->notify.instance, self,
  460. &self->qos, max_seg_size,
  461. max_header_size, skb);
  462. }
  463. }
  464. /*
  465. * Function irlmp_connect_response (handle, userdata)
  466. *
  467. * Service user is accepting connection
  468. *
  469. */
  470. int irlmp_connect_response(struct lsap_cb *self, struct sk_buff *userdata)
  471. {
  472. IRDA_ASSERT(self != NULL, return -1;);
  473. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
  474. IRDA_ASSERT(userdata != NULL, return -1;);
  475. /* We set the connected bit and move the lsap to the connected list
  476. * in the state machine itself. Jean II */
  477. IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
  478. __FUNCTION__, self->slsap_sel, self->dlsap_sel);
  479. /* Make room for MUX control header (3 bytes) */
  480. IRDA_ASSERT(skb_headroom(userdata) >= LMP_CONTROL_HEADER, return -1;);
  481. skb_push(userdata, LMP_CONTROL_HEADER);
  482. irlmp_do_lsap_event(self, LM_CONNECT_RESPONSE, userdata);
  483. /* Drop reference count - see irlap_data_request(). */
  484. dev_kfree_skb(userdata);
  485. return 0;
  486. }
  487. EXPORT_SYMBOL(irlmp_connect_response);
  488. /*
  489. * Function irlmp_connect_confirm (handle, skb)
  490. *
  491. * LSAP connection confirmed peer device!
  492. */
  493. void irlmp_connect_confirm(struct lsap_cb *self, struct sk_buff *skb)
  494. {
  495. int max_header_size;
  496. int lap_header_size;
  497. int max_seg_size;
  498. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  499. IRDA_ASSERT(skb != NULL, return;);
  500. IRDA_ASSERT(self != NULL, return;);
  501. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  502. IRDA_ASSERT(self->lap != NULL, return;);
  503. self->qos = *self->lap->qos;
  504. max_seg_size = self->lap->qos->data_size.value-LMP_HEADER;
  505. lap_header_size = IRLAP_GET_HEADER_SIZE(self->lap->irlap);
  506. max_header_size = LMP_HEADER + lap_header_size;
  507. IRDA_DEBUG(2, "%s(), max_header_size=%d\n",
  508. __FUNCTION__, max_header_size);
  509. /* Hide LMP_CONTROL_HEADER header from layer above */
  510. skb_pull(skb, LMP_CONTROL_HEADER);
  511. if (self->notify.connect_confirm) {
  512. /* Don't forget to refcount it - see irlap_driver_rcv() */
  513. skb_get(skb);
  514. self->notify.connect_confirm(self->notify.instance, self,
  515. &self->qos, max_seg_size,
  516. max_header_size, skb);
  517. }
  518. }
  519. /*
  520. * Function irlmp_dup (orig, instance)
  521. *
  522. * Duplicate LSAP, can be used by servers to confirm a connection on a
  523. * new LSAP so it can keep listening on the old one.
  524. *
  525. */
  526. struct lsap_cb *irlmp_dup(struct lsap_cb *orig, void *instance)
  527. {
  528. struct lsap_cb *new;
  529. unsigned long flags;
  530. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  531. spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  532. /* Only allowed to duplicate unconnected LSAP's, and only LSAPs
  533. * that have received a connect indication. Jean II */
  534. if ((!hashbin_find(irlmp->unconnected_lsaps, (long) orig, NULL)) ||
  535. (orig->lap == NULL)) {
  536. IRDA_DEBUG(0, "%s(), invalid LSAP (wrong state)\n",
  537. __FUNCTION__);
  538. spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
  539. flags);
  540. return NULL;
  541. }
  542. /* Allocate a new instance */
  543. new = kmalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
  544. if (!new) {
  545. IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
  546. spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
  547. flags);
  548. return NULL;
  549. }
  550. /* Dup */
  551. memcpy(new, orig, sizeof(struct lsap_cb));
  552. /* new->lap = orig->lap; => done in the memcpy() */
  553. /* new->slsap_sel = orig->slsap_sel; => done in the memcpy() */
  554. new->conn_skb = NULL;
  555. spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  556. /* Not everything is the same */
  557. new->notify.instance = instance;
  558. init_timer(&new->watchdog_timer);
  559. hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) new,
  560. (long) new, NULL);
  561. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  562. /* Make sure that we invalidate the LSAP cache */
  563. new->lap->cache.valid = FALSE;
  564. #endif /* CONFIG_IRDA_CACHE_LAST_LSAP */
  565. return new;
  566. }
  567. /*
  568. * Function irlmp_disconnect_request (handle, userdata)
  569. *
  570. * The service user is requesting disconnection, this will not remove the
  571. * LSAP, but only mark it as disconnected
  572. */
  573. int irlmp_disconnect_request(struct lsap_cb *self, struct sk_buff *userdata)
  574. {
  575. struct lsap_cb *lsap;
  576. IRDA_ASSERT(self != NULL, return -1;);
  577. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
  578. IRDA_ASSERT(userdata != NULL, return -1;);
  579. /* Already disconnected ?
  580. * There is a race condition between irlmp_disconnect_indication()
  581. * and us that might mess up the hashbins below. This fixes it.
  582. * Jean II */
  583. if (! test_and_clear_bit(0, &self->connected)) {
  584. IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
  585. dev_kfree_skb(userdata);
  586. return -1;
  587. }
  588. skb_push(userdata, LMP_CONTROL_HEADER);
  589. /*
  590. * Do the event before the other stuff since we must know
  591. * which lap layer that the frame should be transmitted on
  592. */
  593. irlmp_do_lsap_event(self, LM_DISCONNECT_REQUEST, userdata);
  594. /* Drop reference count - see irlap_data_request(). */
  595. dev_kfree_skb(userdata);
  596. /*
  597. * Remove LSAP from list of connected LSAPs for the particular link
  598. * and insert it into the list of unconnected LSAPs
  599. */
  600. IRDA_ASSERT(self->lap != NULL, return -1;);
  601. IRDA_ASSERT(self->lap->magic == LMP_LAP_MAGIC, return -1;);
  602. IRDA_ASSERT(self->lap->lsaps != NULL, return -1;);
  603. lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
  604. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  605. self->lap->cache.valid = FALSE;
  606. #endif
  607. IRDA_ASSERT(lsap != NULL, return -1;);
  608. IRDA_ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
  609. IRDA_ASSERT(lsap == self, return -1;);
  610. hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
  611. (long) self, NULL);
  612. /* Reset some values */
  613. self->dlsap_sel = LSAP_ANY;
  614. self->lap = NULL;
  615. return 0;
  616. }
  617. EXPORT_SYMBOL(irlmp_disconnect_request);
  618. /*
  619. * Function irlmp_disconnect_indication (reason, userdata)
  620. *
  621. * LSAP is being closed!
  622. */
  623. void irlmp_disconnect_indication(struct lsap_cb *self, LM_REASON reason,
  624. struct sk_buff *skb)
  625. {
  626. struct lsap_cb *lsap;
  627. IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, irlmp_reasons[reason]);
  628. IRDA_ASSERT(self != NULL, return;);
  629. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  630. IRDA_DEBUG(3, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
  631. __FUNCTION__, self->slsap_sel, self->dlsap_sel);
  632. /* Already disconnected ?
  633. * There is a race condition between irlmp_disconnect_request()
  634. * and us that might mess up the hashbins below. This fixes it.
  635. * Jean II */
  636. if (! test_and_clear_bit(0, &self->connected)) {
  637. IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
  638. return;
  639. }
  640. /*
  641. * Remove association between this LSAP and the link it used
  642. */
  643. IRDA_ASSERT(self->lap != NULL, return;);
  644. IRDA_ASSERT(self->lap->lsaps != NULL, return;);
  645. lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
  646. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  647. self->lap->cache.valid = FALSE;
  648. #endif
  649. IRDA_ASSERT(lsap != NULL, return;);
  650. IRDA_ASSERT(lsap == self, return;);
  651. hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) lsap,
  652. (long) lsap, NULL);
  653. self->dlsap_sel = LSAP_ANY;
  654. self->lap = NULL;
  655. /*
  656. * Inform service user
  657. */
  658. if (self->notify.disconnect_indication) {
  659. /* Don't forget to refcount it - see irlap_driver_rcv(). */
  660. if(skb)
  661. skb_get(skb);
  662. self->notify.disconnect_indication(self->notify.instance,
  663. self, reason, skb);
  664. } else {
  665. IRDA_DEBUG(0, "%s(), no handler\n", __FUNCTION__);
  666. }
  667. }
  668. /*
  669. * Function irlmp_do_expiry (void)
  670. *
  671. * Do a cleanup of the discovery log (remove old entries)
  672. *
  673. * Note : separate from irlmp_do_discovery() so that we can handle
  674. * passive discovery properly.
  675. */
  676. void irlmp_do_expiry(void)
  677. {
  678. struct lap_cb *lap;
  679. /*
  680. * Expire discovery on all links which are *not* connected.
  681. * On links which are connected, we can't do discovery
  682. * anymore and can't refresh the log, so we freeze the
  683. * discovery log to keep info about the device we are
  684. * connected to.
  685. * This info is mandatory if we want irlmp_connect_request()
  686. * to work properly. - Jean II
  687. */
  688. lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
  689. while (lap != NULL) {
  690. IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
  691. if (lap->lap_state == LAP_STANDBY) {
  692. /* Expire discoveries discovered on this link */
  693. irlmp_expire_discoveries(irlmp->cachelog, lap->saddr,
  694. FALSE);
  695. }
  696. lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
  697. }
  698. }
  699. /*
  700. * Function irlmp_do_discovery (nslots)
  701. *
  702. * Do some discovery on all links
  703. *
  704. * Note : log expiry is done above.
  705. */
  706. void irlmp_do_discovery(int nslots)
  707. {
  708. struct lap_cb *lap;
  709. /* Make sure the value is sane */
  710. if ((nslots != 1) && (nslots != 6) && (nslots != 8) && (nslots != 16)){
  711. IRDA_WARNING("%s: invalid value for number of slots!\n",
  712. __FUNCTION__);
  713. nslots = sysctl_discovery_slots = 8;
  714. }
  715. /* Construct new discovery info to be used by IrLAP, */
  716. u16ho(irlmp->discovery_cmd.data.hints) = irlmp->hints.word;
  717. /*
  718. * Set character set for device name (we use ASCII), and
  719. * copy device name. Remember to make room for a \0 at the
  720. * end
  721. */
  722. irlmp->discovery_cmd.data.charset = CS_ASCII;
  723. strncpy(irlmp->discovery_cmd.data.info, sysctl_devname,
  724. NICKNAME_MAX_LEN);
  725. irlmp->discovery_cmd.name_len = strlen(irlmp->discovery_cmd.data.info);
  726. irlmp->discovery_cmd.nslots = nslots;
  727. /*
  728. * Try to send discovery packets on all links
  729. */
  730. lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
  731. while (lap != NULL) {
  732. IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
  733. if (lap->lap_state == LAP_STANDBY) {
  734. /* Try to discover */
  735. irlmp_do_lap_event(lap, LM_LAP_DISCOVERY_REQUEST,
  736. NULL);
  737. }
  738. lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
  739. }
  740. }
  741. /*
  742. * Function irlmp_discovery_request (nslots)
  743. *
  744. * Do a discovery of devices in front of the computer
  745. *
  746. * If the caller has registered a client discovery callback, this
  747. * allow him to receive the full content of the discovery log through
  748. * this callback (as normally he will receive only new discoveries).
  749. */
  750. void irlmp_discovery_request(int nslots)
  751. {
  752. /* Return current cached discovery log (in full) */
  753. irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_LOG);
  754. /*
  755. * Start a single discovery operation if discovery is not already
  756. * running
  757. */
  758. if (!sysctl_discovery) {
  759. /* Check if user wants to override the default */
  760. if (nslots == DISCOVERY_DEFAULT_SLOTS)
  761. nslots = sysctl_discovery_slots;
  762. irlmp_do_discovery(nslots);
  763. /* Note : we never do expiry here. Expiry will run on the
  764. * discovery timer regardless of the state of sysctl_discovery
  765. * Jean II */
  766. }
  767. }
  768. EXPORT_SYMBOL(irlmp_discovery_request);
  769. /*
  770. * Function irlmp_get_discoveries (pn, mask, slots)
  771. *
  772. * Return the current discovery log
  773. *
  774. * If discovery is not enabled, you should call this function again
  775. * after 1 or 2 seconds (i.e. after discovery has been done).
  776. */
  777. struct irda_device_info *irlmp_get_discoveries(int *pn, __u16 mask, int nslots)
  778. {
  779. /* If discovery is not enabled, it's likely that the discovery log
  780. * will be empty. So, we trigger a single discovery, so that next
  781. * time the user call us there might be some results in the log.
  782. * Jean II
  783. */
  784. if (!sysctl_discovery) {
  785. /* Check if user wants to override the default */
  786. if (nslots == DISCOVERY_DEFAULT_SLOTS)
  787. nslots = sysctl_discovery_slots;
  788. /* Start discovery - will complete sometime later */
  789. irlmp_do_discovery(nslots);
  790. /* Note : we never do expiry here. Expiry will run on the
  791. * discovery timer regardless of the state of sysctl_discovery
  792. * Jean II */
  793. }
  794. /* Return current cached discovery log */
  795. return(irlmp_copy_discoveries(irlmp->cachelog, pn, mask, TRUE));
  796. }
  797. EXPORT_SYMBOL(irlmp_get_discoveries);
  798. /*
  799. * Function irlmp_notify_client (log)
  800. *
  801. * Notify all about discovered devices
  802. *
  803. * Clients registered with IrLMP are :
  804. * o IrComm
  805. * o IrLAN
  806. * o Any socket (in any state - ouch, that may be a lot !)
  807. * The client may have defined a callback to be notified in case of
  808. * partial/selective discovery based on the hints that it passed to IrLMP.
  809. */
  810. static inline void
  811. irlmp_notify_client(irlmp_client_t *client,
  812. hashbin_t *log, DISCOVERY_MODE mode)
  813. {
  814. discinfo_t *discoveries; /* Copy of the discovery log */
  815. int number; /* Number of nodes in the log */
  816. int i;
  817. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  818. /* Check if client wants or not partial/selective log (optimisation) */
  819. if (!client->disco_callback)
  820. return;
  821. /*
  822. * Locking notes :
  823. * the old code was manipulating the log directly, which was
  824. * very racy. Now, we use copy_discoveries, that protects
  825. * itself while dumping the log for us.
  826. * The overhead of the copy is compensated by the fact that
  827. * we only pass new discoveries in normal mode and don't
  828. * pass the same old entry every 3s to the caller as we used
  829. * to do (virtual function calling is expensive).
  830. * Jean II
  831. */
  832. /*
  833. * Now, check all discovered devices (if any), and notify client
  834. * only about the services that the client is interested in
  835. * We also notify only about the new devices unless the caller
  836. * explicitly request a dump of the log. Jean II
  837. */
  838. discoveries = irlmp_copy_discoveries(log, &number,
  839. client->hint_mask.word,
  840. (mode == DISCOVERY_LOG));
  841. /* Check if the we got some results */
  842. if (discoveries == NULL)
  843. return; /* No nodes discovered */
  844. /* Pass all entries to the listener */
  845. for(i = 0; i < number; i++)
  846. client->disco_callback(&(discoveries[i]), mode, client->priv);
  847. /* Free up our buffer */
  848. kfree(discoveries);
  849. }
  850. /*
  851. * Function irlmp_discovery_confirm ( self, log)
  852. *
  853. * Some device(s) answered to our discovery request! Check to see which
  854. * device it is, and give indication to the client(s)
  855. *
  856. */
  857. void irlmp_discovery_confirm(hashbin_t *log, DISCOVERY_MODE mode)
  858. {
  859. irlmp_client_t *client;
  860. irlmp_client_t *client_next;
  861. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  862. IRDA_ASSERT(log != NULL, return;);
  863. if (!(HASHBIN_GET_SIZE(log)))
  864. return;
  865. /* For each client - notify callback may touch client list */
  866. client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
  867. while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
  868. (void *) &client_next) ) {
  869. /* Check if we should notify client */
  870. irlmp_notify_client(client, log, mode);
  871. client = client_next;
  872. }
  873. }
  874. /*
  875. * Function irlmp_discovery_expiry (expiry)
  876. *
  877. * This device is no longer been discovered, and therefore it is being
  878. * purged from the discovery log. Inform all clients who have
  879. * registered for this event...
  880. *
  881. * Note : called exclusively from discovery.c
  882. * Note : this is no longer called under discovery spinlock, so the
  883. * client can do whatever he wants in the callback.
  884. */
  885. void irlmp_discovery_expiry(discinfo_t *expiries, int number)
  886. {
  887. irlmp_client_t *client;
  888. irlmp_client_t *client_next;
  889. int i;
  890. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  891. IRDA_ASSERT(expiries != NULL, return;);
  892. /* For each client - notify callback may touch client list */
  893. client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
  894. while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
  895. (void *) &client_next) ) {
  896. /* Pass all entries to the listener */
  897. for(i = 0; i < number; i++) {
  898. /* Check if we should notify client */
  899. if ((client->expir_callback) &&
  900. (client->hint_mask.word & u16ho(expiries[i].hints)
  901. & 0x7f7f) )
  902. client->expir_callback(&(expiries[i]),
  903. EXPIRY_TIMEOUT,
  904. client->priv);
  905. }
  906. /* Next client */
  907. client = client_next;
  908. }
  909. }
  910. /*
  911. * Function irlmp_get_discovery_response ()
  912. *
  913. * Used by IrLAP to get the discovery info it needs when answering
  914. * discovery requests by other devices.
  915. */
  916. discovery_t *irlmp_get_discovery_response(void)
  917. {
  918. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  919. IRDA_ASSERT(irlmp != NULL, return NULL;);
  920. u16ho(irlmp->discovery_rsp.data.hints) = irlmp->hints.word;
  921. /*
  922. * Set character set for device name (we use ASCII), and
  923. * copy device name. Remember to make room for a \0 at the
  924. * end
  925. */
  926. irlmp->discovery_rsp.data.charset = CS_ASCII;
  927. strncpy(irlmp->discovery_rsp.data.info, sysctl_devname,
  928. NICKNAME_MAX_LEN);
  929. irlmp->discovery_rsp.name_len = strlen(irlmp->discovery_rsp.data.info);
  930. return &irlmp->discovery_rsp;
  931. }
  932. /*
  933. * Function irlmp_data_request (self, skb)
  934. *
  935. * Send some data to peer device
  936. *
  937. * Note on skb management :
  938. * After calling the lower layers of the IrDA stack, we always
  939. * kfree() the skb, which drop the reference count (and potentially
  940. * destroy it).
  941. * IrLMP and IrLAP may queue the packet, and in those cases will need
  942. * to use skb_get() to keep it around.
  943. * Jean II
  944. */
  945. int irlmp_data_request(struct lsap_cb *self, struct sk_buff *userdata)
  946. {
  947. int ret;
  948. IRDA_ASSERT(self != NULL, return -1;);
  949. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
  950. /* Make room for MUX header */
  951. IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
  952. skb_push(userdata, LMP_HEADER);
  953. ret = irlmp_do_lsap_event(self, LM_DATA_REQUEST, userdata);
  954. /* Drop reference count - see irlap_data_request(). */
  955. dev_kfree_skb(userdata);
  956. return ret;
  957. }
  958. EXPORT_SYMBOL(irlmp_data_request);
  959. /*
  960. * Function irlmp_data_indication (handle, skb)
  961. *
  962. * Got data from LAP layer so pass it up to upper layer
  963. *
  964. */
  965. void irlmp_data_indication(struct lsap_cb *self, struct sk_buff *skb)
  966. {
  967. /* Hide LMP header from layer above */
  968. skb_pull(skb, LMP_HEADER);
  969. if (self->notify.data_indication) {
  970. /* Don't forget to refcount it - see irlap_driver_rcv(). */
  971. skb_get(skb);
  972. self->notify.data_indication(self->notify.instance, self, skb);
  973. }
  974. }
  975. /*
  976. * Function irlmp_udata_request (self, skb)
  977. */
  978. int irlmp_udata_request(struct lsap_cb *self, struct sk_buff *userdata)
  979. {
  980. int ret;
  981. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  982. IRDA_ASSERT(userdata != NULL, return -1;);
  983. /* Make room for MUX header */
  984. IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
  985. skb_push(userdata, LMP_HEADER);
  986. ret = irlmp_do_lsap_event(self, LM_UDATA_REQUEST, userdata);
  987. /* Drop reference count - see irlap_data_request(). */
  988. dev_kfree_skb(userdata);
  989. return ret;
  990. }
  991. /*
  992. * Function irlmp_udata_indication (self, skb)
  993. *
  994. * Send unreliable data (but still within the connection)
  995. *
  996. */
  997. void irlmp_udata_indication(struct lsap_cb *self, struct sk_buff *skb)
  998. {
  999. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1000. IRDA_ASSERT(self != NULL, return;);
  1001. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  1002. IRDA_ASSERT(skb != NULL, return;);
  1003. /* Hide LMP header from layer above */
  1004. skb_pull(skb, LMP_HEADER);
  1005. if (self->notify.udata_indication) {
  1006. /* Don't forget to refcount it - see irlap_driver_rcv(). */
  1007. skb_get(skb);
  1008. self->notify.udata_indication(self->notify.instance, self,
  1009. skb);
  1010. }
  1011. }
  1012. /*
  1013. * Function irlmp_connless_data_request (self, skb)
  1014. */
  1015. #ifdef CONFIG_IRDA_ULTRA
  1016. int irlmp_connless_data_request(struct lsap_cb *self, struct sk_buff *userdata,
  1017. __u8 pid)
  1018. {
  1019. struct sk_buff *clone_skb;
  1020. struct lap_cb *lap;
  1021. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1022. IRDA_ASSERT(userdata != NULL, return -1;);
  1023. /* Make room for MUX and PID header */
  1024. IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER+LMP_PID_HEADER,
  1025. return -1;);
  1026. /* Insert protocol identifier */
  1027. skb_push(userdata, LMP_PID_HEADER);
  1028. if(self != NULL)
  1029. userdata->data[0] = self->pid;
  1030. else
  1031. userdata->data[0] = pid;
  1032. /* Connectionless sockets must use 0x70 */
  1033. skb_push(userdata, LMP_HEADER);
  1034. userdata->data[0] = userdata->data[1] = LSAP_CONNLESS;
  1035. /* Try to send Connectionless packets out on all links */
  1036. lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
  1037. while (lap != NULL) {
  1038. IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return -1;);
  1039. clone_skb = skb_clone(userdata, GFP_ATOMIC);
  1040. if (!clone_skb) {
  1041. dev_kfree_skb(userdata);
  1042. return -ENOMEM;
  1043. }
  1044. irlap_unitdata_request(lap->irlap, clone_skb);
  1045. /* irlap_unitdata_request() don't increase refcount,
  1046. * so no dev_kfree_skb() - Jean II */
  1047. lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
  1048. }
  1049. dev_kfree_skb(userdata);
  1050. return 0;
  1051. }
  1052. #endif /* CONFIG_IRDA_ULTRA */
  1053. /*
  1054. * Function irlmp_connless_data_indication (self, skb)
  1055. *
  1056. * Receive unreliable data outside any connection. Mostly used by Ultra
  1057. *
  1058. */
  1059. #ifdef CONFIG_IRDA_ULTRA
  1060. void irlmp_connless_data_indication(struct lsap_cb *self, struct sk_buff *skb)
  1061. {
  1062. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1063. IRDA_ASSERT(self != NULL, return;);
  1064. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
  1065. IRDA_ASSERT(skb != NULL, return;);
  1066. /* Hide LMP and PID header from layer above */
  1067. skb_pull(skb, LMP_HEADER+LMP_PID_HEADER);
  1068. if (self->notify.udata_indication) {
  1069. /* Don't forget to refcount it - see irlap_driver_rcv(). */
  1070. skb_get(skb);
  1071. self->notify.udata_indication(self->notify.instance, self,
  1072. skb);
  1073. }
  1074. }
  1075. #endif /* CONFIG_IRDA_ULTRA */
  1076. /*
  1077. * Propagate status indication from LAP to LSAPs (via LMP)
  1078. * This don't trigger any change of state in lap_cb, lmp_cb or lsap_cb,
  1079. * and the event is stateless, therefore we can bypass both state machines
  1080. * and send the event direct to the LSAP user.
  1081. * Jean II
  1082. */
  1083. void irlmp_status_indication(struct lap_cb *self,
  1084. LINK_STATUS link, LOCK_STATUS lock)
  1085. {
  1086. struct lsap_cb *next;
  1087. struct lsap_cb *curr;
  1088. /* Send status_indication to all LSAPs using this link */
  1089. curr = (struct lsap_cb *) hashbin_get_first( self->lsaps);
  1090. while (NULL != hashbin_find_next(self->lsaps, (long) curr, NULL,
  1091. (void *) &next) ) {
  1092. IRDA_ASSERT(curr->magic == LMP_LSAP_MAGIC, return;);
  1093. /*
  1094. * Inform service user if he has requested it
  1095. */
  1096. if (curr->notify.status_indication != NULL)
  1097. curr->notify.status_indication(curr->notify.instance,
  1098. link, lock);
  1099. else
  1100. IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
  1101. curr = next;
  1102. }
  1103. }
  1104. /*
  1105. * Receive flow control indication from LAP.
  1106. * LAP want us to send it one more frame. We implement a simple round
  1107. * robin scheduler between the active sockets so that we get a bit of
  1108. * fairness. Note that the round robin is far from perfect, but it's
  1109. * better than nothing.
  1110. * We then poll the selected socket so that we can do synchronous
  1111. * refilling of IrLAP (which allow to minimise the number of buffers).
  1112. * Jean II
  1113. */
  1114. void irlmp_flow_indication(struct lap_cb *self, LOCAL_FLOW flow)
  1115. {
  1116. struct lsap_cb *next;
  1117. struct lsap_cb *curr;
  1118. int lsap_todo;
  1119. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  1120. IRDA_ASSERT(flow == FLOW_START, return;);
  1121. /* Get the number of lsap. That's the only safe way to know
  1122. * that we have looped around... - Jean II */
  1123. lsap_todo = HASHBIN_GET_SIZE(self->lsaps);
  1124. IRDA_DEBUG(4, "%s() : %d lsaps to scan\n", __FUNCTION__, lsap_todo);
  1125. /* Poll lsap in order until the queue is full or until we
  1126. * tried them all.
  1127. * Most often, the current LSAP will have something to send,
  1128. * so we will go through this loop only once. - Jean II */
  1129. while((lsap_todo--) &&
  1130. (IRLAP_GET_TX_QUEUE_LEN(self->irlap) < LAP_HIGH_THRESHOLD)) {
  1131. /* Try to find the next lsap we should poll. */
  1132. next = self->flow_next;
  1133. /* If we have no lsap, restart from first one */
  1134. if(next == NULL)
  1135. next = (struct lsap_cb *) hashbin_get_first(self->lsaps);
  1136. /* Verify current one and find the next one */
  1137. curr = hashbin_find_next(self->lsaps, (long) next, NULL,
  1138. (void *) &self->flow_next);
  1139. /* Uh-oh... Paranoia */
  1140. if(curr == NULL)
  1141. break;
  1142. IRDA_DEBUG(4, "%s() : curr is %p, next was %p and is now %p, still %d to go - queue len = %d\n", __FUNCTION__, curr, next, self->flow_next, lsap_todo, IRLAP_GET_TX_QUEUE_LEN(self->irlap));
  1143. /* Inform lsap user that it can send one more packet. */
  1144. if (curr->notify.flow_indication != NULL)
  1145. curr->notify.flow_indication(curr->notify.instance,
  1146. curr, flow);
  1147. else
  1148. IRDA_DEBUG(1, "%s(), no handler\n", __FUNCTION__);
  1149. }
  1150. }
  1151. #if 0
  1152. /*
  1153. * Function irlmp_hint_to_service (hint)
  1154. *
  1155. * Returns a list of all servics contained in the given hint bits. This
  1156. * function assumes that the hint bits have the size of two bytes only
  1157. */
  1158. __u8 *irlmp_hint_to_service(__u8 *hint)
  1159. {
  1160. __u8 *service;
  1161. int i = 0;
  1162. /*
  1163. * Allocate array to store services in. 16 entries should be safe
  1164. * since we currently only support 2 hint bytes
  1165. */
  1166. service = kmalloc(16, GFP_ATOMIC);
  1167. if (!service) {
  1168. IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
  1169. return NULL;
  1170. }
  1171. if (!hint[0]) {
  1172. IRDA_DEBUG(1, "<None>\n");
  1173. kfree(service);
  1174. return NULL;
  1175. }
  1176. if (hint[0] & HINT_PNP)
  1177. IRDA_DEBUG(1, "PnP Compatible ");
  1178. if (hint[0] & HINT_PDA)
  1179. IRDA_DEBUG(1, "PDA/Palmtop ");
  1180. if (hint[0] & HINT_COMPUTER)
  1181. IRDA_DEBUG(1, "Computer ");
  1182. if (hint[0] & HINT_PRINTER) {
  1183. IRDA_DEBUG(1, "Printer ");
  1184. service[i++] = S_PRINTER;
  1185. }
  1186. if (hint[0] & HINT_MODEM)
  1187. IRDA_DEBUG(1, "Modem ");
  1188. if (hint[0] & HINT_FAX)
  1189. IRDA_DEBUG(1, "Fax ");
  1190. if (hint[0] & HINT_LAN) {
  1191. IRDA_DEBUG(1, "LAN Access ");
  1192. service[i++] = S_LAN;
  1193. }
  1194. /*
  1195. * Test if extension byte exists. This byte will usually be
  1196. * there, but this is not really required by the standard.
  1197. * (IrLMP p. 29)
  1198. */
  1199. if (hint[0] & HINT_EXTENSION) {
  1200. if (hint[1] & HINT_TELEPHONY) {
  1201. IRDA_DEBUG(1, "Telephony ");
  1202. service[i++] = S_TELEPHONY;
  1203. } if (hint[1] & HINT_FILE_SERVER)
  1204. IRDA_DEBUG(1, "File Server ");
  1205. if (hint[1] & HINT_COMM) {
  1206. IRDA_DEBUG(1, "IrCOMM ");
  1207. service[i++] = S_COMM;
  1208. }
  1209. if (hint[1] & HINT_OBEX) {
  1210. IRDA_DEBUG(1, "IrOBEX ");
  1211. service[i++] = S_OBEX;
  1212. }
  1213. }
  1214. IRDA_DEBUG(1, "\n");
  1215. /* So that client can be notified about any discovery */
  1216. service[i++] = S_ANY;
  1217. service[i] = S_END;
  1218. return service;
  1219. }
  1220. #endif
  1221. static const __u16 service_hint_mapping[S_END][2] = {
  1222. { HINT_PNP, 0 }, /* S_PNP */
  1223. { HINT_PDA, 0 }, /* S_PDA */
  1224. { HINT_COMPUTER, 0 }, /* S_COMPUTER */
  1225. { HINT_PRINTER, 0 }, /* S_PRINTER */
  1226. { HINT_MODEM, 0 }, /* S_MODEM */
  1227. { HINT_FAX, 0 }, /* S_FAX */
  1228. { HINT_LAN, 0 }, /* S_LAN */
  1229. { HINT_EXTENSION, HINT_TELEPHONY }, /* S_TELEPHONY */
  1230. { HINT_EXTENSION, HINT_COMM }, /* S_COMM */
  1231. { HINT_EXTENSION, HINT_OBEX }, /* S_OBEX */
  1232. { 0xFF, 0xFF }, /* S_ANY */
  1233. };
  1234. /*
  1235. * Function irlmp_service_to_hint (service)
  1236. *
  1237. * Converts a service type, to a hint bit
  1238. *
  1239. * Returns: a 16 bit hint value, with the service bit set
  1240. */
  1241. __u16 irlmp_service_to_hint(int service)
  1242. {
  1243. __u16_host_order hint;
  1244. hint.byte[0] = service_hint_mapping[service][0];
  1245. hint.byte[1] = service_hint_mapping[service][1];
  1246. return hint.word;
  1247. }
  1248. EXPORT_SYMBOL(irlmp_service_to_hint);
  1249. /*
  1250. * Function irlmp_register_service (service)
  1251. *
  1252. * Register local service with IrLMP
  1253. *
  1254. */
  1255. void *irlmp_register_service(__u16 hints)
  1256. {
  1257. irlmp_service_t *service;
  1258. IRDA_DEBUG(4, "%s(), hints = %04x\n", __FUNCTION__, hints);
  1259. /* Make a new registration */
  1260. service = kmalloc(sizeof(irlmp_service_t), GFP_ATOMIC);
  1261. if (!service) {
  1262. IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
  1263. return NULL;
  1264. }
  1265. service->hints.word = hints;
  1266. hashbin_insert(irlmp->services, (irda_queue_t *) service,
  1267. (long) service, NULL);
  1268. irlmp->hints.word |= hints;
  1269. return (void *)service;
  1270. }
  1271. EXPORT_SYMBOL(irlmp_register_service);
  1272. /*
  1273. * Function irlmp_unregister_service (handle)
  1274. *
  1275. * Unregister service with IrLMP.
  1276. *
  1277. * Returns: 0 on success, -1 on error
  1278. */
  1279. int irlmp_unregister_service(void *handle)
  1280. {
  1281. irlmp_service_t *service;
  1282. unsigned long flags;
  1283. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1284. if (!handle)
  1285. return -1;
  1286. /* Caller may call with invalid handle (it's legal) - Jean II */
  1287. service = hashbin_lock_find(irlmp->services, (long) handle, NULL);
  1288. if (!service) {
  1289. IRDA_DEBUG(1, "%s(), Unknown service!\n", __FUNCTION__);
  1290. return -1;
  1291. }
  1292. hashbin_remove_this(irlmp->services, (irda_queue_t *) service);
  1293. kfree(service);
  1294. /* Remove old hint bits */
  1295. irlmp->hints.word = 0;
  1296. /* Refresh current hint bits */
  1297. spin_lock_irqsave(&irlmp->services->hb_spinlock, flags);
  1298. service = (irlmp_service_t *) hashbin_get_first(irlmp->services);
  1299. while (service) {
  1300. irlmp->hints.word |= service->hints.word;
  1301. service = (irlmp_service_t *)hashbin_get_next(irlmp->services);
  1302. }
  1303. spin_unlock_irqrestore(&irlmp->services->hb_spinlock, flags);
  1304. return 0;
  1305. }
  1306. EXPORT_SYMBOL(irlmp_unregister_service);
  1307. /*
  1308. * Function irlmp_register_client (hint_mask, callback1, callback2)
  1309. *
  1310. * Register a local client with IrLMP
  1311. * First callback is selective discovery (based on hints)
  1312. * Second callback is for selective discovery expiries
  1313. *
  1314. * Returns: handle > 0 on success, 0 on error
  1315. */
  1316. void *irlmp_register_client(__u16 hint_mask, DISCOVERY_CALLBACK1 disco_clb,
  1317. DISCOVERY_CALLBACK2 expir_clb, void *priv)
  1318. {
  1319. irlmp_client_t *client;
  1320. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  1321. IRDA_ASSERT(irlmp != NULL, return NULL;);
  1322. /* Make a new registration */
  1323. client = kmalloc(sizeof(irlmp_client_t), GFP_ATOMIC);
  1324. if (!client) {
  1325. IRDA_DEBUG( 1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
  1326. return NULL;
  1327. }
  1328. /* Register the details */
  1329. client->hint_mask.word = hint_mask;
  1330. client->disco_callback = disco_clb;
  1331. client->expir_callback = expir_clb;
  1332. client->priv = priv;
  1333. hashbin_insert(irlmp->clients, (irda_queue_t *) client,
  1334. (long) client, NULL);
  1335. return (void *) client;
  1336. }
  1337. EXPORT_SYMBOL(irlmp_register_client);
  1338. /*
  1339. * Function irlmp_update_client (handle, hint_mask, callback1, callback2)
  1340. *
  1341. * Updates specified client (handle) with possibly new hint_mask and
  1342. * callback
  1343. *
  1344. * Returns: 0 on success, -1 on error
  1345. */
  1346. int irlmp_update_client(void *handle, __u16 hint_mask,
  1347. DISCOVERY_CALLBACK1 disco_clb,
  1348. DISCOVERY_CALLBACK2 expir_clb, void *priv)
  1349. {
  1350. irlmp_client_t *client;
  1351. if (!handle)
  1352. return -1;
  1353. client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
  1354. if (!client) {
  1355. IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
  1356. return -1;
  1357. }
  1358. client->hint_mask.word = hint_mask;
  1359. client->disco_callback = disco_clb;
  1360. client->expir_callback = expir_clb;
  1361. client->priv = priv;
  1362. return 0;
  1363. }
  1364. EXPORT_SYMBOL(irlmp_update_client);
  1365. /*
  1366. * Function irlmp_unregister_client (handle)
  1367. *
  1368. * Returns: 0 on success, -1 on error
  1369. *
  1370. */
  1371. int irlmp_unregister_client(void *handle)
  1372. {
  1373. struct irlmp_client *client;
  1374. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1375. if (!handle)
  1376. return -1;
  1377. /* Caller may call with invalid handle (it's legal) - Jean II */
  1378. client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
  1379. if (!client) {
  1380. IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
  1381. return -1;
  1382. }
  1383. IRDA_DEBUG(4, "%s(), removing client!\n", __FUNCTION__);
  1384. hashbin_remove_this(irlmp->clients, (irda_queue_t *) client);
  1385. kfree(client);
  1386. return 0;
  1387. }
  1388. EXPORT_SYMBOL(irlmp_unregister_client);
  1389. /*
  1390. * Function irlmp_slsap_inuse (slsap)
  1391. *
  1392. * Check if the given source LSAP selector is in use
  1393. *
  1394. * This function is clearly not very efficient. On the mitigating side, the
  1395. * stack make sure that in 99% of the cases, we are called only once
  1396. * for each socket allocation. We could probably keep a bitmap
  1397. * of the allocated LSAP, but I'm not sure the complexity is worth it.
  1398. * Jean II
  1399. */
  1400. static int irlmp_slsap_inuse(__u8 slsap_sel)
  1401. {
  1402. struct lsap_cb *self;
  1403. struct lap_cb *lap;
  1404. unsigned long flags;
  1405. IRDA_ASSERT(irlmp != NULL, return TRUE;);
  1406. IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return TRUE;);
  1407. IRDA_ASSERT(slsap_sel != LSAP_ANY, return TRUE;);
  1408. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1409. #ifdef CONFIG_IRDA_ULTRA
  1410. /* Accept all bindings to the connectionless LSAP */
  1411. if (slsap_sel == LSAP_CONNLESS)
  1412. return FALSE;
  1413. #endif /* CONFIG_IRDA_ULTRA */
  1414. /* Valid values are between 0 and 127 (0x0-0x6F) */
  1415. if (slsap_sel > LSAP_MAX)
  1416. return TRUE;
  1417. /*
  1418. * Check if slsap is already in use. To do this we have to loop over
  1419. * every IrLAP connection and check every LSAP associated with each
  1420. * the connection.
  1421. */
  1422. spin_lock_irqsave(&irlmp->links->hb_spinlock, flags);
  1423. lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
  1424. while (lap != NULL) {
  1425. IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, goto errlap;);
  1426. /* Careful for priority inversions here !
  1427. * irlmp->links is never taken while another IrDA
  1428. * spinlock is held, so we are safe. Jean II */
  1429. spin_lock(&lap->lsaps->hb_spinlock);
  1430. /* For this IrLAP, check all the LSAPs */
  1431. self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
  1432. while (self != NULL) {
  1433. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC,
  1434. goto errlsap;);
  1435. if ((self->slsap_sel == slsap_sel)) {
  1436. IRDA_DEBUG(4, "Source LSAP selector=%02x in use\n",
  1437. self->slsap_sel);
  1438. goto errlsap;
  1439. }
  1440. self = (struct lsap_cb*) hashbin_get_next(lap->lsaps);
  1441. }
  1442. spin_unlock(&lap->lsaps->hb_spinlock);
  1443. /* Next LAP */
  1444. lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
  1445. }
  1446. spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
  1447. /*
  1448. * Server sockets are typically waiting for connections and
  1449. * therefore reside in the unconnected list. We don't want
  1450. * to give out their LSAPs for obvious reasons...
  1451. * Jean II
  1452. */
  1453. spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  1454. self = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);
  1455. while (self != NULL) {
  1456. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, goto erruncon;);
  1457. if ((self->slsap_sel == slsap_sel)) {
  1458. IRDA_DEBUG(4, "Source LSAP selector=%02x in use (unconnected)\n",
  1459. self->slsap_sel);
  1460. goto erruncon;
  1461. }
  1462. self = (struct lsap_cb*) hashbin_get_next(irlmp->unconnected_lsaps);
  1463. }
  1464. spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  1465. return FALSE;
  1466. /* Error exit from within one of the two nested loops.
  1467. * Make sure we release the right spinlock in the righ order.
  1468. * Jean II */
  1469. errlsap:
  1470. spin_unlock(&lap->lsaps->hb_spinlock);
  1471. IRDA_ASSERT_LABEL(errlap:)
  1472. spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
  1473. return TRUE;
  1474. /* Error exit from within the unconnected loop.
  1475. * Just one spinlock to release... Jean II */
  1476. erruncon:
  1477. spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  1478. return TRUE;
  1479. }
  1480. /*
  1481. * Function irlmp_find_free_slsap ()
  1482. *
  1483. * Find a free source LSAP to use. This function is called if the service
  1484. * user has requested a source LSAP equal to LM_ANY
  1485. */
  1486. static __u8 irlmp_find_free_slsap(void)
  1487. {
  1488. __u8 lsap_sel;
  1489. int wrapped = 0;
  1490. IRDA_ASSERT(irlmp != NULL, return -1;);
  1491. IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return -1;);
  1492. /* Most users don't really care which LSAPs they are given,
  1493. * and therefore we automatically give them a free LSAP.
  1494. * This function try to find a suitable LSAP, i.e. which is
  1495. * not in use and is within the acceptable range. Jean II */
  1496. do {
  1497. /* Always increment to LSAP number before using it.
  1498. * In theory, we could reuse the last LSAP number, as long
  1499. * as it is no longer in use. Some IrDA stack do that.
  1500. * However, the previous socket may be half closed, i.e.
  1501. * we closed it, we think it's no longer in use, but the
  1502. * other side did not receive our close and think it's
  1503. * active and still send data on it.
  1504. * This is similar to what is done with PIDs and TCP ports.
  1505. * Also, this reduce the number of calls to irlmp_slsap_inuse()
  1506. * which is an expensive function to call.
  1507. * Jean II */
  1508. irlmp->last_lsap_sel++;
  1509. /* Check if we need to wraparound (0x70-0x7f are reserved) */
  1510. if (irlmp->last_lsap_sel > LSAP_MAX) {
  1511. /* 0x00-0x10 are also reserved for well know ports */
  1512. irlmp->last_lsap_sel = 0x10;
  1513. /* Make sure we terminate the loop */
  1514. if (wrapped++) {
  1515. IRDA_ERROR("%s: no more free LSAPs !\n",
  1516. __FUNCTION__);
  1517. return 0;
  1518. }
  1519. }
  1520. /* If the LSAP is in use, try the next one.
  1521. * Despite the autoincrement, we need to check if the lsap
  1522. * is really in use or not, first because LSAP may be
  1523. * directly allocated in irlmp_open_lsap(), and also because
  1524. * we may wraparound on old sockets. Jean II */
  1525. } while (irlmp_slsap_inuse(irlmp->last_lsap_sel));
  1526. /* Got it ! */
  1527. lsap_sel = irlmp->last_lsap_sel;
  1528. IRDA_DEBUG(4, "%s(), found free lsap_sel=%02x\n",
  1529. __FUNCTION__, lsap_sel);
  1530. return lsap_sel;
  1531. }
  1532. /*
  1533. * Function irlmp_convert_lap_reason (lap_reason)
  1534. *
  1535. * Converts IrLAP disconnect reason codes to IrLMP disconnect reason
  1536. * codes
  1537. *
  1538. */
  1539. LM_REASON irlmp_convert_lap_reason( LAP_REASON lap_reason)
  1540. {
  1541. int reason = LM_LAP_DISCONNECT;
  1542. switch (lap_reason) {
  1543. case LAP_DISC_INDICATION: /* Received a disconnect request from peer */
  1544. IRDA_DEBUG( 1, "%s(), LAP_DISC_INDICATION\n", __FUNCTION__);
  1545. reason = LM_USER_REQUEST;
  1546. break;
  1547. case LAP_NO_RESPONSE: /* To many retransmits without response */
  1548. IRDA_DEBUG( 1, "%s(), LAP_NO_RESPONSE\n", __FUNCTION__);
  1549. reason = LM_LAP_DISCONNECT;
  1550. break;
  1551. case LAP_RESET_INDICATION:
  1552. IRDA_DEBUG( 1, "%s(), LAP_RESET_INDICATION\n", __FUNCTION__);
  1553. reason = LM_LAP_RESET;
  1554. break;
  1555. case LAP_FOUND_NONE:
  1556. case LAP_MEDIA_BUSY:
  1557. case LAP_PRIMARY_CONFLICT:
  1558. IRDA_DEBUG(1, "%s(), LAP_FOUND_NONE, LAP_MEDIA_BUSY or LAP_PRIMARY_CONFLICT\n", __FUNCTION__);
  1559. reason = LM_CONNECT_FAILURE;
  1560. break;
  1561. default:
  1562. IRDA_DEBUG(1, "%s(), Unknow IrLAP disconnect reason %d!\n",
  1563. __FUNCTION__, lap_reason);
  1564. reason = LM_LAP_DISCONNECT;
  1565. break;
  1566. }
  1567. return reason;
  1568. }
  1569. #ifdef CONFIG_PROC_FS
  1570. struct irlmp_iter_state {
  1571. hashbin_t *hashbin;
  1572. };
  1573. #define LSAP_START_TOKEN ((void *)1)
  1574. #define LINK_START_TOKEN ((void *)2)
  1575. static void *irlmp_seq_hb_idx(struct irlmp_iter_state *iter, loff_t *off)
  1576. {
  1577. void *element;
  1578. spin_lock_irq(&iter->hashbin->hb_spinlock);
  1579. for (element = hashbin_get_first(iter->hashbin);
  1580. element != NULL;
  1581. element = hashbin_get_next(iter->hashbin)) {
  1582. if (!off || *off-- == 0) {
  1583. /* NB: hashbin left locked */
  1584. return element;
  1585. }
  1586. }
  1587. spin_unlock_irq(&iter->hashbin->hb_spinlock);
  1588. iter->hashbin = NULL;
  1589. return NULL;
  1590. }
  1591. static void *irlmp_seq_start(struct seq_file *seq, loff_t *pos)
  1592. {
  1593. struct irlmp_iter_state *iter = seq->private;
  1594. void *v;
  1595. loff_t off = *pos;
  1596. iter->hashbin = NULL;
  1597. if (off-- == 0)
  1598. return LSAP_START_TOKEN;
  1599. iter->hashbin = irlmp->unconnected_lsaps;
  1600. v = irlmp_seq_hb_idx(iter, &off);
  1601. if (v)
  1602. return v;
  1603. if (off-- == 0)
  1604. return LINK_START_TOKEN;
  1605. iter->hashbin = irlmp->links;
  1606. return irlmp_seq_hb_idx(iter, &off);
  1607. }
  1608. static void *irlmp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1609. {
  1610. struct irlmp_iter_state *iter = seq->private;
  1611. ++*pos;
  1612. if (v == LSAP_START_TOKEN) { /* start of list of lsaps */
  1613. iter->hashbin = irlmp->unconnected_lsaps;
  1614. v = irlmp_seq_hb_idx(iter, NULL);
  1615. return v ? v : LINK_START_TOKEN;
  1616. }
  1617. if (v == LINK_START_TOKEN) { /* start of list of links */
  1618. iter->hashbin = irlmp->links;
  1619. return irlmp_seq_hb_idx(iter, NULL);
  1620. }
  1621. v = hashbin_get_next(iter->hashbin);
  1622. if (v == NULL) { /* no more in this hash bin */
  1623. spin_unlock_irq(&iter->hashbin->hb_spinlock);
  1624. if (iter->hashbin == irlmp->unconnected_lsaps)
  1625. v = LINK_START_TOKEN;
  1626. iter->hashbin = NULL;
  1627. }
  1628. return v;
  1629. }
  1630. static void irlmp_seq_stop(struct seq_file *seq, void *v)
  1631. {
  1632. struct irlmp_iter_state *iter = seq->private;
  1633. if (iter->hashbin)
  1634. spin_unlock_irq(&iter->hashbin->hb_spinlock);
  1635. }
  1636. static int irlmp_seq_show(struct seq_file *seq, void *v)
  1637. {
  1638. const struct irlmp_iter_state *iter = seq->private;
  1639. struct lsap_cb *self = v;
  1640. if (v == LSAP_START_TOKEN)
  1641. seq_puts(seq, "Unconnected LSAPs:\n");
  1642. else if (v == LINK_START_TOKEN)
  1643. seq_puts(seq, "\nRegistered Link Layers:\n");
  1644. else if (iter->hashbin == irlmp->unconnected_lsaps) {
  1645. self = v;
  1646. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -EINVAL; );
  1647. seq_printf(seq, "lsap state: %s, ",
  1648. irlsap_state[ self->lsap_state]);
  1649. seq_printf(seq,
  1650. "slsap_sel: %#02x, dlsap_sel: %#02x, ",
  1651. self->slsap_sel, self->dlsap_sel);
  1652. seq_printf(seq, "(%s)", self->notify.name);
  1653. seq_printf(seq, "\n");
  1654. } else if (iter->hashbin == irlmp->links) {
  1655. struct lap_cb *lap = v;
  1656. seq_printf(seq, "lap state: %s, ",
  1657. irlmp_state[lap->lap_state]);
  1658. seq_printf(seq, "saddr: %#08x, daddr: %#08x, ",
  1659. lap->saddr, lap->daddr);
  1660. seq_printf(seq, "num lsaps: %d",
  1661. HASHBIN_GET_SIZE(lap->lsaps));
  1662. seq_printf(seq, "\n");
  1663. /* Careful for priority inversions here !
  1664. * All other uses of attrib spinlock are independent of
  1665. * the object spinlock, so we are safe. Jean II */
  1666. spin_lock(&lap->lsaps->hb_spinlock);
  1667. seq_printf(seq, "\n Connected LSAPs:\n");
  1668. for (self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
  1669. self != NULL;
  1670. self = (struct lsap_cb *)hashbin_get_next(lap->lsaps)) {
  1671. IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC,
  1672. goto outloop;);
  1673. seq_printf(seq, " lsap state: %s, ",
  1674. irlsap_state[ self->lsap_state]);
  1675. seq_printf(seq,
  1676. "slsap_sel: %#02x, dlsap_sel: %#02x, ",
  1677. self->slsap_sel, self->dlsap_sel);
  1678. seq_printf(seq, "(%s)", self->notify.name);
  1679. seq_putc(seq, '\n');
  1680. }
  1681. IRDA_ASSERT_LABEL(outloop:)
  1682. spin_unlock(&lap->lsaps->hb_spinlock);
  1683. seq_putc(seq, '\n');
  1684. } else
  1685. return -EINVAL;
  1686. return 0;
  1687. }
  1688. static struct seq_operations irlmp_seq_ops = {
  1689. .start = irlmp_seq_start,
  1690. .next = irlmp_seq_next,
  1691. .stop = irlmp_seq_stop,
  1692. .show = irlmp_seq_show,
  1693. };
  1694. static int irlmp_seq_open(struct inode *inode, struct file *file)
  1695. {
  1696. struct seq_file *seq;
  1697. int rc = -ENOMEM;
  1698. struct irlmp_iter_state *s;
  1699. IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
  1700. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1701. if (!s)
  1702. goto out;
  1703. rc = seq_open(file, &irlmp_seq_ops);
  1704. if (rc)
  1705. goto out_kfree;
  1706. seq = file->private_data;
  1707. seq->private = s;
  1708. out:
  1709. return rc;
  1710. out_kfree:
  1711. kfree(s);
  1712. goto out;
  1713. }
  1714. struct file_operations irlmp_seq_fops = {
  1715. .owner = THIS_MODULE,
  1716. .open = irlmp_seq_open,
  1717. .read = seq_read,
  1718. .llseek = seq_lseek,
  1719. .release = seq_release_private,
  1720. };
  1721. #endif /* PROC_FS */