irlmp.c 53 KB

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