irlmp.c 55 KB

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