irlmp.c 55 KB

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