irlmp.c 55 KB

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