irttp.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. /*********************************************************************
  2. *
  3. * Filename: irttp.c
  4. * Version: 1.2
  5. * Description: Tiny Transport Protocol (TTP) implementation
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 31 20:14:31 1997
  9. * Modified at: Wed Jan 5 11:31:27 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
  13. * All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * Neither Dag Brattli nor University of Tromsø admit liability nor
  22. * provide warranty for any of this software. This material is
  23. * provided "AS-IS" and at no charge.
  24. *
  25. ********************************************************************/
  26. #include <linux/config.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/init.h>
  29. #include <linux/seq_file.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/unaligned.h>
  32. #include <net/irda/irda.h>
  33. #include <net/irda/irlap.h>
  34. #include <net/irda/irlmp.h>
  35. #include <net/irda/parameters.h>
  36. #include <net/irda/irttp.h>
  37. static struct irttp_cb *irttp = NULL;
  38. static void __irttp_close_tsap(struct tsap_cb *self);
  39. static int irttp_data_indication(void *instance, void *sap,
  40. struct sk_buff *skb);
  41. static int irttp_udata_indication(void *instance, void *sap,
  42. struct sk_buff *skb);
  43. static void irttp_disconnect_indication(void *instance, void *sap,
  44. LM_REASON reason, struct sk_buff *);
  45. static void irttp_connect_indication(void *instance, void *sap,
  46. struct qos_info *qos, __u32 max_sdu_size,
  47. __u8 header_size, struct sk_buff *skb);
  48. static void irttp_connect_confirm(void *instance, void *sap,
  49. struct qos_info *qos, __u32 max_sdu_size,
  50. __u8 header_size, struct sk_buff *skb);
  51. static void irttp_run_tx_queue(struct tsap_cb *self);
  52. static void irttp_run_rx_queue(struct tsap_cb *self);
  53. static void irttp_flush_queues(struct tsap_cb *self);
  54. static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
  55. static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
  56. static void irttp_todo_expired(unsigned long data);
  57. static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
  58. int get);
  59. static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow);
  60. static void irttp_status_indication(void *instance,
  61. LINK_STATUS link, LOCK_STATUS lock);
  62. /* Information for parsing parameters in IrTTP */
  63. static pi_minor_info_t pi_minor_call_table[] = {
  64. { NULL, 0 }, /* 0x00 */
  65. { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
  66. };
  67. static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
  68. static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
  69. /************************ GLOBAL PROCEDURES ************************/
  70. /*
  71. * Function irttp_init (void)
  72. *
  73. * Initialize the IrTTP layer. Called by module initialization code
  74. *
  75. */
  76. int __init irttp_init(void)
  77. {
  78. /* Initialize the irttp structure. */
  79. if (irttp == NULL) {
  80. irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
  81. if (irttp == NULL)
  82. return -ENOMEM;
  83. }
  84. memset(irttp, 0, sizeof(struct irttp_cb));
  85. irttp->magic = TTP_MAGIC;
  86. irttp->tsaps = hashbin_new(HB_LOCK);
  87. if (!irttp->tsaps) {
  88. IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n",
  89. __FUNCTION__);
  90. return -ENOMEM;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Function irttp_cleanup (void)
  96. *
  97. * Called by module destruction/cleanup code
  98. *
  99. */
  100. void __exit irttp_cleanup(void)
  101. {
  102. /* Check for main structure */
  103. IRDA_ASSERT(irttp != NULL, return;);
  104. IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;);
  105. /*
  106. * Delete hashbin and close all TSAP instances in it
  107. */
  108. hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
  109. irttp->magic = 0;
  110. /* De-allocate main structure */
  111. kfree(irttp);
  112. irttp = NULL;
  113. }
  114. /*************************** SUBROUTINES ***************************/
  115. /*
  116. * Function irttp_start_todo_timer (self, timeout)
  117. *
  118. * Start todo timer.
  119. *
  120. * Made it more effient and unsensitive to race conditions - Jean II
  121. */
  122. static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
  123. {
  124. /* Set new value for timer */
  125. mod_timer(&self->todo_timer, jiffies + timeout);
  126. }
  127. /*
  128. * Function irttp_todo_expired (data)
  129. *
  130. * Todo timer has expired!
  131. *
  132. * One of the restriction of the timer is that it is run only on the timer
  133. * interrupt which run every 10ms. This mean that even if you set the timer
  134. * with a delay of 0, it may take up to 10ms before it's run.
  135. * So, to minimise latency and keep cache fresh, we try to avoid using
  136. * it as much as possible.
  137. * Note : we can't use tasklets, because they can't be asynchronously
  138. * killed (need user context), and we can't guarantee that here...
  139. * Jean II
  140. */
  141. static void irttp_todo_expired(unsigned long data)
  142. {
  143. struct tsap_cb *self = (struct tsap_cb *) data;
  144. /* Check that we still exist */
  145. if (!self || self->magic != TTP_TSAP_MAGIC)
  146. return;
  147. IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
  148. /* Try to make some progress, especially on Tx side - Jean II */
  149. irttp_run_rx_queue(self);
  150. irttp_run_tx_queue(self);
  151. /* Check if time for disconnect */
  152. if (test_bit(0, &self->disconnect_pend)) {
  153. /* Check if it's possible to disconnect yet */
  154. if (skb_queue_empty(&self->tx_queue)) {
  155. /* Make sure disconnect is not pending anymore */
  156. clear_bit(0, &self->disconnect_pend); /* FALSE */
  157. /* Note : self->disconnect_skb may be NULL */
  158. irttp_disconnect_request(self, self->disconnect_skb,
  159. P_NORMAL);
  160. self->disconnect_skb = NULL;
  161. } else {
  162. /* Try again later */
  163. irttp_start_todo_timer(self, HZ/10);
  164. /* No reason to try and close now */
  165. return;
  166. }
  167. }
  168. /* Check if it's closing time */
  169. if (self->close_pend)
  170. /* Finish cleanup */
  171. irttp_close_tsap(self);
  172. }
  173. /*
  174. * Function irttp_flush_queues (self)
  175. *
  176. * Flushes (removes all frames) in transitt-buffer (tx_list)
  177. */
  178. void irttp_flush_queues(struct tsap_cb *self)
  179. {
  180. struct sk_buff* skb;
  181. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  182. IRDA_ASSERT(self != NULL, return;);
  183. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  184. /* Deallocate frames waiting to be sent */
  185. while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
  186. dev_kfree_skb(skb);
  187. /* Deallocate received frames */
  188. while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
  189. dev_kfree_skb(skb);
  190. /* Deallocate received fragments */
  191. while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
  192. dev_kfree_skb(skb);
  193. }
  194. /*
  195. * Function irttp_reassemble (self)
  196. *
  197. * Makes a new (continuous) skb of all the fragments in the fragment
  198. * queue
  199. *
  200. */
  201. static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
  202. {
  203. struct sk_buff *skb, *frag;
  204. int n = 0; /* Fragment index */
  205. IRDA_ASSERT(self != NULL, return NULL;);
  206. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
  207. IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __FUNCTION__,
  208. self->rx_sdu_size);
  209. skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
  210. if (!skb)
  211. return NULL;
  212. /*
  213. * Need to reserve space for TTP header in case this skb needs to
  214. * be requeued in case delivery failes
  215. */
  216. skb_reserve(skb, TTP_HEADER);
  217. skb_put(skb, self->rx_sdu_size);
  218. /*
  219. * Copy all fragments to a new buffer
  220. */
  221. while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
  222. memcpy(skb->data+n, frag->data, frag->len);
  223. n += frag->len;
  224. dev_kfree_skb(frag);
  225. }
  226. IRDA_DEBUG(2,
  227. "%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
  228. __FUNCTION__, n, self->rx_sdu_size, self->rx_max_sdu_size);
  229. /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size
  230. * by summing the size of all fragments, so we should always
  231. * have n == self->rx_sdu_size, except in cases where we
  232. * droped the last fragment (when self->rx_sdu_size exceed
  233. * self->rx_max_sdu_size), where n < self->rx_sdu_size.
  234. * Jean II */
  235. IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
  236. /* Set the new length */
  237. skb_trim(skb, n);
  238. self->rx_sdu_size = 0;
  239. return skb;
  240. }
  241. /*
  242. * Function irttp_fragment_skb (skb)
  243. *
  244. * Fragments a frame and queues all the fragments for transmission
  245. *
  246. */
  247. static inline void irttp_fragment_skb(struct tsap_cb *self,
  248. struct sk_buff *skb)
  249. {
  250. struct sk_buff *frag;
  251. __u8 *frame;
  252. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  253. IRDA_ASSERT(self != NULL, return;);
  254. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  255. IRDA_ASSERT(skb != NULL, return;);
  256. /*
  257. * Split frame into a number of segments
  258. */
  259. while (skb->len > self->max_seg_size) {
  260. IRDA_DEBUG(2, "%s(), fragmenting ...\n", __FUNCTION__);
  261. /* Make new segment */
  262. frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
  263. if (!frag)
  264. return;
  265. skb_reserve(frag, self->max_header_size);
  266. /* Copy data from the original skb into this fragment. */
  267. memcpy(skb_put(frag, self->max_seg_size), skb->data,
  268. self->max_seg_size);
  269. /* Insert TTP header, with the more bit set */
  270. frame = skb_push(frag, TTP_HEADER);
  271. frame[0] = TTP_MORE;
  272. /* Hide the copied data from the original skb */
  273. skb_pull(skb, self->max_seg_size);
  274. /* Queue fragment */
  275. skb_queue_tail(&self->tx_queue, frag);
  276. }
  277. /* Queue what is left of the original skb */
  278. IRDA_DEBUG(2, "%s(), queuing last segment\n", __FUNCTION__);
  279. frame = skb_push(skb, TTP_HEADER);
  280. frame[0] = 0x00; /* Clear more bit */
  281. /* Queue fragment */
  282. skb_queue_tail(&self->tx_queue, skb);
  283. }
  284. /*
  285. * Function irttp_param_max_sdu_size (self, param)
  286. *
  287. * Handle the MaxSduSize parameter in the connect frames, this function
  288. * will be called both when this parameter needs to be inserted into, and
  289. * extracted from the connect frames
  290. */
  291. static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
  292. int get)
  293. {
  294. struct tsap_cb *self;
  295. self = (struct tsap_cb *) instance;
  296. IRDA_ASSERT(self != NULL, return -1;);
  297. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  298. if (get)
  299. param->pv.i = self->tx_max_sdu_size;
  300. else
  301. self->tx_max_sdu_size = param->pv.i;
  302. IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __FUNCTION__, param->pv.i);
  303. return 0;
  304. }
  305. /*************************** CLIENT CALLS ***************************/
  306. /************************** LMP CALLBACKS **************************/
  307. /* Everything is happily mixed up. Waiting for next clean up - Jean II */
  308. /*
  309. * Function irttp_open_tsap (stsap, notify)
  310. *
  311. * Create TSAP connection endpoint,
  312. */
  313. struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
  314. {
  315. struct tsap_cb *self;
  316. struct lsap_cb *lsap;
  317. notify_t ttp_notify;
  318. IRDA_ASSERT(irttp != NULL, return NULL;);
  319. IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
  320. /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
  321. * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
  322. * JeanII */
  323. if((stsap_sel != LSAP_ANY) &&
  324. ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
  325. IRDA_DEBUG(0, "%s(), invalid tsap!\n", __FUNCTION__);
  326. return NULL;
  327. }
  328. self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
  329. if (self == NULL) {
  330. IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __FUNCTION__);
  331. return NULL;
  332. }
  333. memset(self, 0, sizeof(struct tsap_cb));
  334. spin_lock_init(&self->lock);
  335. /* Initialise todo timer */
  336. init_timer(&self->todo_timer);
  337. self->todo_timer.data = (unsigned long) self;
  338. self->todo_timer.function = &irttp_todo_expired;
  339. /* Initialize callbacks for IrLMP to use */
  340. irda_notify_init(&ttp_notify);
  341. ttp_notify.connect_confirm = irttp_connect_confirm;
  342. ttp_notify.connect_indication = irttp_connect_indication;
  343. ttp_notify.disconnect_indication = irttp_disconnect_indication;
  344. ttp_notify.data_indication = irttp_data_indication;
  345. ttp_notify.udata_indication = irttp_udata_indication;
  346. ttp_notify.flow_indication = irttp_flow_indication;
  347. if(notify->status_indication != NULL)
  348. ttp_notify.status_indication = irttp_status_indication;
  349. ttp_notify.instance = self;
  350. strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
  351. self->magic = TTP_TSAP_MAGIC;
  352. self->connected = FALSE;
  353. skb_queue_head_init(&self->rx_queue);
  354. skb_queue_head_init(&self->tx_queue);
  355. skb_queue_head_init(&self->rx_fragments);
  356. /*
  357. * Create LSAP at IrLMP layer
  358. */
  359. lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
  360. if (lsap == NULL) {
  361. IRDA_WARNING("%s: unable to allocate LSAP!!\n", __FUNCTION__);
  362. return NULL;
  363. }
  364. /*
  365. * If user specified LSAP_ANY as source TSAP selector, then IrLMP
  366. * will replace it with whatever source selector which is free, so
  367. * the stsap_sel we have might not be valid anymore
  368. */
  369. self->stsap_sel = lsap->slsap_sel;
  370. IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __FUNCTION__, self->stsap_sel);
  371. self->notify = *notify;
  372. self->lsap = lsap;
  373. hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
  374. if (credit > TTP_RX_MAX_CREDIT)
  375. self->initial_credit = TTP_RX_MAX_CREDIT;
  376. else
  377. self->initial_credit = credit;
  378. return self;
  379. }
  380. EXPORT_SYMBOL(irttp_open_tsap);
  381. /*
  382. * Function irttp_close (handle)
  383. *
  384. * Remove an instance of a TSAP. This function should only deal with the
  385. * deallocation of the TSAP, and resetting of the TSAPs values;
  386. *
  387. */
  388. static void __irttp_close_tsap(struct tsap_cb *self)
  389. {
  390. /* First make sure we're connected. */
  391. IRDA_ASSERT(self != NULL, return;);
  392. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  393. irttp_flush_queues(self);
  394. del_timer(&self->todo_timer);
  395. /* This one won't be cleaned up if we are disconnect_pend + close_pend
  396. * and we receive a disconnect_indication */
  397. if (self->disconnect_skb)
  398. dev_kfree_skb(self->disconnect_skb);
  399. self->connected = FALSE;
  400. self->magic = ~TTP_TSAP_MAGIC;
  401. kfree(self);
  402. }
  403. /*
  404. * Function irttp_close (self)
  405. *
  406. * Remove TSAP from list of all TSAPs and then deallocate all resources
  407. * associated with this TSAP
  408. *
  409. * Note : because we *free* the tsap structure, it is the responsibility
  410. * of the caller to make sure we are called only once and to deal with
  411. * possible race conditions. - Jean II
  412. */
  413. int irttp_close_tsap(struct tsap_cb *self)
  414. {
  415. struct tsap_cb *tsap;
  416. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  417. IRDA_ASSERT(self != NULL, return -1;);
  418. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  419. /* Make sure tsap has been disconnected */
  420. if (self->connected) {
  421. /* Check if disconnect is not pending */
  422. if (!test_bit(0, &self->disconnect_pend)) {
  423. IRDA_WARNING("%s: TSAP still connected!\n",
  424. __FUNCTION__);
  425. irttp_disconnect_request(self, NULL, P_NORMAL);
  426. }
  427. self->close_pend = TRUE;
  428. irttp_start_todo_timer(self, HZ/10);
  429. return 0; /* Will be back! */
  430. }
  431. tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);
  432. IRDA_ASSERT(tsap == self, return -1;);
  433. /* Close corresponding LSAP */
  434. if (self->lsap) {
  435. irlmp_close_lsap(self->lsap);
  436. self->lsap = NULL;
  437. }
  438. __irttp_close_tsap(self);
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(irttp_close_tsap);
  442. /*
  443. * Function irttp_udata_request (self, skb)
  444. *
  445. * Send unreliable data on this TSAP
  446. *
  447. */
  448. int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
  449. {
  450. IRDA_ASSERT(self != NULL, return -1;);
  451. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  452. IRDA_ASSERT(skb != NULL, return -1;);
  453. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  454. /* Check that nothing bad happens */
  455. if ((skb->len == 0) || (!self->connected)) {
  456. IRDA_DEBUG(1, "%s(), No data, or not connected\n",
  457. __FUNCTION__);
  458. goto err;
  459. }
  460. if (skb->len > self->max_seg_size) {
  461. IRDA_DEBUG(1, "%s(), UData is to large for IrLAP!\n",
  462. __FUNCTION__);
  463. goto err;
  464. }
  465. irlmp_udata_request(self->lsap, skb);
  466. self->stats.tx_packets++;
  467. return 0;
  468. err:
  469. dev_kfree_skb(skb);
  470. return -1;
  471. }
  472. EXPORT_SYMBOL(irttp_udata_request);
  473. /*
  474. * Function irttp_data_request (handle, skb)
  475. *
  476. * Queue frame for transmission. If SAR is enabled, fragement the frame
  477. * and queue the fragments for transmission
  478. */
  479. int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
  480. {
  481. __u8 *frame;
  482. int ret;
  483. IRDA_ASSERT(self != NULL, return -1;);
  484. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  485. IRDA_ASSERT(skb != NULL, return -1;);
  486. IRDA_DEBUG(2, "%s() : queue len = %d\n", __FUNCTION__,
  487. skb_queue_len(&self->tx_queue));
  488. /* Check that nothing bad happens */
  489. if ((skb->len == 0) || (!self->connected)) {
  490. IRDA_WARNING("%s: No data, or not connected\n", __FUNCTION__);
  491. ret = -ENOTCONN;
  492. goto err;
  493. }
  494. /*
  495. * Check if SAR is disabled, and the frame is larger than what fits
  496. * inside an IrLAP frame
  497. */
  498. if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
  499. IRDA_ERROR("%s: SAR disabled, and data is to large for IrLAP!\n",
  500. __FUNCTION__);
  501. ret = -EMSGSIZE;
  502. goto err;
  503. }
  504. /*
  505. * Check if SAR is enabled, and the frame is larger than the
  506. * TxMaxSduSize
  507. */
  508. if ((self->tx_max_sdu_size != 0) &&
  509. (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
  510. (skb->len > self->tx_max_sdu_size))
  511. {
  512. IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
  513. __FUNCTION__);
  514. ret = -EMSGSIZE;
  515. goto err;
  516. }
  517. /*
  518. * Check if transmit queue is full
  519. */
  520. if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
  521. /*
  522. * Give it a chance to empty itself
  523. */
  524. irttp_run_tx_queue(self);
  525. /* Drop packet. This error code should trigger the caller
  526. * to resend the data in the client code - Jean II */
  527. ret = -ENOBUFS;
  528. goto err;
  529. }
  530. /* Queue frame, or queue frame segments */
  531. if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
  532. /* Queue frame */
  533. IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
  534. frame = skb_push(skb, TTP_HEADER);
  535. frame[0] = 0x00; /* Clear more bit */
  536. skb_queue_tail(&self->tx_queue, skb);
  537. } else {
  538. /*
  539. * Fragment the frame, this function will also queue the
  540. * fragments, we don't care about the fact the transmit
  541. * queue may be overfilled by all the segments for a little
  542. * while
  543. */
  544. irttp_fragment_skb(self, skb);
  545. }
  546. /* Check if we can accept more data from client */
  547. if ((!self->tx_sdu_busy) &&
  548. (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
  549. /* Tx queue filling up, so stop client. */
  550. if (self->notify.flow_indication) {
  551. self->notify.flow_indication(self->notify.instance,
  552. self, FLOW_STOP);
  553. }
  554. /* self->tx_sdu_busy is the state of the client.
  555. * Update state after notifying client to avoid
  556. * race condition with irttp_flow_indication().
  557. * If the queue empty itself after our test but before
  558. * we set the flag, we will fix ourselves below in
  559. * irttp_run_tx_queue().
  560. * Jean II */
  561. self->tx_sdu_busy = TRUE;
  562. }
  563. /* Try to make some progress */
  564. irttp_run_tx_queue(self);
  565. return 0;
  566. err:
  567. dev_kfree_skb(skb);
  568. return ret;
  569. }
  570. EXPORT_SYMBOL(irttp_data_request);
  571. /*
  572. * Function irttp_run_tx_queue (self)
  573. *
  574. * Transmit packets queued for transmission (if possible)
  575. *
  576. */
  577. static void irttp_run_tx_queue(struct tsap_cb *self)
  578. {
  579. struct sk_buff *skb;
  580. unsigned long flags;
  581. int n;
  582. IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n",
  583. __FUNCTION__,
  584. self->send_credit, skb_queue_len(&self->tx_queue));
  585. /* Get exclusive access to the tx queue, otherwise don't touch it */
  586. if (irda_lock(&self->tx_queue_lock) == FALSE)
  587. return;
  588. /* Try to send out frames as long as we have credits
  589. * and as long as LAP is not full. If LAP is full, it will
  590. * poll us through irttp_flow_indication() - Jean II */
  591. while ((self->send_credit > 0) &&
  592. (!irlmp_lap_tx_queue_full(self->lsap)) &&
  593. (skb = skb_dequeue(&self->tx_queue)))
  594. {
  595. /*
  596. * Since we can transmit and receive frames concurrently,
  597. * the code below is a critical region and we must assure that
  598. * nobody messes with the credits while we update them.
  599. */
  600. spin_lock_irqsave(&self->lock, flags);
  601. n = self->avail_credit;
  602. self->avail_credit = 0;
  603. /* Only room for 127 credits in frame */
  604. if (n > 127) {
  605. self->avail_credit = n-127;
  606. n = 127;
  607. }
  608. self->remote_credit += n;
  609. self->send_credit--;
  610. spin_unlock_irqrestore(&self->lock, flags);
  611. /*
  612. * More bit must be set by the data_request() or fragment()
  613. * functions
  614. */
  615. skb->data[0] |= (n & 0x7f);
  616. /* Detach from socket.
  617. * The current skb has a reference to the socket that sent
  618. * it (skb->sk). When we pass it to IrLMP, the skb will be
  619. * stored in in IrLAP (self->wx_list). When we are within
  620. * IrLAP, we lose the notion of socket, so we should not
  621. * have a reference to a socket. So, we drop it here.
  622. *
  623. * Why does it matter ?
  624. * When the skb is freed (kfree_skb), if it is associated
  625. * with a socket, it release buffer space on the socket
  626. * (through sock_wfree() and sock_def_write_space()).
  627. * If the socket no longer exist, we may crash. Hard.
  628. * When we close a socket, we make sure that associated packets
  629. * in IrTTP are freed. However, we have no way to cancel
  630. * the packet that we have passed to IrLAP. So, if a packet
  631. * remains in IrLAP (retry on the link or else) after we
  632. * close the socket, we are dead !
  633. * Jean II */
  634. if (skb->sk != NULL) {
  635. /* IrSOCK application, IrOBEX, ... */
  636. skb_orphan(skb);
  637. }
  638. /* IrCOMM over IrTTP, IrLAN, ... */
  639. /* Pass the skb to IrLMP - done */
  640. irlmp_data_request(self->lsap, skb);
  641. self->stats.tx_packets++;
  642. }
  643. /* Check if we can accept more frames from client.
  644. * We don't want to wait until the todo timer to do that, and we
  645. * can't use tasklets (grr...), so we are obliged to give control
  646. * to client. That's ok, this test will be true not too often
  647. * (max once per LAP window) and we are called from places
  648. * where we can spend a bit of time doing stuff. - Jean II */
  649. if ((self->tx_sdu_busy) &&
  650. (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
  651. (!self->close_pend))
  652. {
  653. if (self->notify.flow_indication)
  654. self->notify.flow_indication(self->notify.instance,
  655. self, FLOW_START);
  656. /* self->tx_sdu_busy is the state of the client.
  657. * We don't really have a race here, but it's always safer
  658. * to update our state after the client - Jean II */
  659. self->tx_sdu_busy = FALSE;
  660. }
  661. /* Reset lock */
  662. self->tx_queue_lock = 0;
  663. }
  664. /*
  665. * Function irttp_give_credit (self)
  666. *
  667. * Send a dataless flowdata TTP-PDU and give available credit to peer
  668. * TSAP
  669. */
  670. static inline void irttp_give_credit(struct tsap_cb *self)
  671. {
  672. struct sk_buff *tx_skb = NULL;
  673. unsigned long flags;
  674. int n;
  675. IRDA_ASSERT(self != NULL, return;);
  676. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  677. IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n",
  678. __FUNCTION__,
  679. self->send_credit, self->avail_credit, self->remote_credit);
  680. /* Give credit to peer */
  681. tx_skb = dev_alloc_skb(64);
  682. if (!tx_skb)
  683. return;
  684. /* Reserve space for LMP, and LAP header */
  685. skb_reserve(tx_skb, self->max_header_size);
  686. /*
  687. * Since we can transmit and receive frames concurrently,
  688. * the code below is a critical region and we must assure that
  689. * nobody messes with the credits while we update them.
  690. */
  691. spin_lock_irqsave(&self->lock, flags);
  692. n = self->avail_credit;
  693. self->avail_credit = 0;
  694. /* Only space for 127 credits in frame */
  695. if (n > 127) {
  696. self->avail_credit = n - 127;
  697. n = 127;
  698. }
  699. self->remote_credit += n;
  700. spin_unlock_irqrestore(&self->lock, flags);
  701. skb_put(tx_skb, 1);
  702. tx_skb->data[0] = (__u8) (n & 0x7f);
  703. irlmp_data_request(self->lsap, tx_skb);
  704. self->stats.tx_packets++;
  705. }
  706. /*
  707. * Function irttp_udata_indication (instance, sap, skb)
  708. *
  709. * Received some unit-data (unreliable)
  710. *
  711. */
  712. static int irttp_udata_indication(void *instance, void *sap,
  713. struct sk_buff *skb)
  714. {
  715. struct tsap_cb *self;
  716. int err;
  717. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  718. self = (struct tsap_cb *) instance;
  719. IRDA_ASSERT(self != NULL, return -1;);
  720. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  721. IRDA_ASSERT(skb != NULL, return -1;);
  722. self->stats.rx_packets++;
  723. /* Just pass data to layer above */
  724. if (self->notify.udata_indication) {
  725. err = self->notify.udata_indication(self->notify.instance,
  726. self,skb);
  727. /* Same comment as in irttp_do_data_indication() */
  728. if (!err)
  729. return 0;
  730. }
  731. /* Either no handler, or handler returns an error */
  732. dev_kfree_skb(skb);
  733. return 0;
  734. }
  735. /*
  736. * Function irttp_data_indication (instance, sap, skb)
  737. *
  738. * Receive segment from IrLMP.
  739. *
  740. */
  741. static int irttp_data_indication(void *instance, void *sap,
  742. struct sk_buff *skb)
  743. {
  744. struct tsap_cb *self;
  745. unsigned long flags;
  746. int n;
  747. self = (struct tsap_cb *) instance;
  748. n = skb->data[0] & 0x7f; /* Extract the credits */
  749. self->stats.rx_packets++;
  750. /* Deal with inbound credit
  751. * Since we can transmit and receive frames concurrently,
  752. * the code below is a critical region and we must assure that
  753. * nobody messes with the credits while we update them.
  754. */
  755. spin_lock_irqsave(&self->lock, flags);
  756. self->send_credit += n;
  757. if (skb->len > 1)
  758. self->remote_credit--;
  759. spin_unlock_irqrestore(&self->lock, flags);
  760. /*
  761. * Data or dataless packet? Dataless frames contains only the
  762. * TTP_HEADER.
  763. */
  764. if (skb->len > 1) {
  765. /*
  766. * We don't remove the TTP header, since we must preserve the
  767. * more bit, so the defragment routing knows what to do
  768. */
  769. skb_queue_tail(&self->rx_queue, skb);
  770. } else {
  771. /* Dataless flowdata TTP-PDU */
  772. dev_kfree_skb(skb);
  773. }
  774. /* Push data to the higher layer.
  775. * We do it synchronously because running the todo timer for each
  776. * receive packet would be too much overhead and latency.
  777. * By passing control to the higher layer, we run the risk that
  778. * it may take time or grab a lock. Most often, the higher layer
  779. * will only put packet in a queue.
  780. * Anyway, packets are only dripping through the IrDA, so we can
  781. * have time before the next packet.
  782. * Further, we are run from NET_BH, so the worse that can happen is
  783. * us missing the optimal time to send back the PF bit in LAP.
  784. * Jean II */
  785. irttp_run_rx_queue(self);
  786. /* We now give credits to peer in irttp_run_rx_queue().
  787. * We need to send credit *NOW*, otherwise we are going
  788. * to miss the next Tx window. The todo timer may take
  789. * a while before it's run... - Jean II */
  790. /*
  791. * If the peer device has given us some credits and we didn't have
  792. * anyone from before, then we need to shedule the tx queue.
  793. * We need to do that because our Tx have stopped (so we may not
  794. * get any LAP flow indication) and the user may be stopped as
  795. * well. - Jean II
  796. */
  797. if (self->send_credit == n) {
  798. /* Restart pushing stuff to LAP */
  799. irttp_run_tx_queue(self);
  800. /* Note : we don't want to schedule the todo timer
  801. * because it has horrible latency. No tasklets
  802. * because the tasklet API is broken. - Jean II */
  803. }
  804. return 0;
  805. }
  806. /*
  807. * Function irttp_status_indication (self, reason)
  808. *
  809. * Status_indication, just pass to the higher layer...
  810. *
  811. */
  812. static void irttp_status_indication(void *instance,
  813. LINK_STATUS link, LOCK_STATUS lock)
  814. {
  815. struct tsap_cb *self;
  816. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  817. self = (struct tsap_cb *) instance;
  818. IRDA_ASSERT(self != NULL, return;);
  819. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  820. /* Check if client has already closed the TSAP and gone away */
  821. if (self->close_pend)
  822. return;
  823. /*
  824. * Inform service user if he has requested it
  825. */
  826. if (self->notify.status_indication != NULL)
  827. self->notify.status_indication(self->notify.instance,
  828. link, lock);
  829. else
  830. IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
  831. }
  832. /*
  833. * Function irttp_flow_indication (self, reason)
  834. *
  835. * Flow_indication : IrLAP tells us to send more data.
  836. *
  837. */
  838. static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
  839. {
  840. struct tsap_cb *self;
  841. self = (struct tsap_cb *) instance;
  842. IRDA_ASSERT(self != NULL, return;);
  843. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  844. IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
  845. /* We are "polled" directly from LAP, and the LAP want to fill
  846. * its Tx window. We want to do our best to send it data, so that
  847. * we maximise the window. On the other hand, we want to limit the
  848. * amount of work here so that LAP doesn't hang forever waiting
  849. * for packets. - Jean II */
  850. /* Try to send some packets. Currently, LAP calls us every time
  851. * there is one free slot, so we will send only one packet.
  852. * This allow the scheduler to do its round robin - Jean II */
  853. irttp_run_tx_queue(self);
  854. /* Note regarding the interraction with higher layer.
  855. * irttp_run_tx_queue() may call the client when its queue
  856. * start to empty, via notify.flow_indication(). Initially.
  857. * I wanted this to happen in a tasklet, to avoid client
  858. * grabbing the CPU, but we can't use tasklets safely. And timer
  859. * is definitely too slow.
  860. * This will happen only once per LAP window, and usually at
  861. * the third packet (unless window is smaller). LAP is still
  862. * doing mtt and sending first packet so it's sort of OK
  863. * to do that. Jean II */
  864. /* If we need to send disconnect. try to do it now */
  865. if(self->disconnect_pend)
  866. irttp_start_todo_timer(self, 0);
  867. }
  868. /*
  869. * Function irttp_flow_request (self, command)
  870. *
  871. * This function could be used by the upper layers to tell IrTTP to stop
  872. * delivering frames if the receive queues are starting to get full, or
  873. * to tell IrTTP to start delivering frames again.
  874. */
  875. void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
  876. {
  877. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  878. IRDA_ASSERT(self != NULL, return;);
  879. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  880. switch (flow) {
  881. case FLOW_STOP:
  882. IRDA_DEBUG(1, "%s(), flow stop\n", __FUNCTION__);
  883. self->rx_sdu_busy = TRUE;
  884. break;
  885. case FLOW_START:
  886. IRDA_DEBUG(1, "%s(), flow start\n", __FUNCTION__);
  887. self->rx_sdu_busy = FALSE;
  888. /* Client say he can accept more data, try to free our
  889. * queues ASAP - Jean II */
  890. irttp_run_rx_queue(self);
  891. break;
  892. default:
  893. IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __FUNCTION__);
  894. }
  895. }
  896. EXPORT_SYMBOL(irttp_flow_request);
  897. /*
  898. * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
  899. *
  900. * Try to connect to remote destination TSAP selector
  901. *
  902. */
  903. int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
  904. __u32 saddr, __u32 daddr,
  905. struct qos_info *qos, __u32 max_sdu_size,
  906. struct sk_buff *userdata)
  907. {
  908. struct sk_buff *tx_skb;
  909. __u8 *frame;
  910. __u8 n;
  911. IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __FUNCTION__, max_sdu_size);
  912. IRDA_ASSERT(self != NULL, return -EBADR;);
  913. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
  914. if (self->connected) {
  915. if(userdata)
  916. dev_kfree_skb(userdata);
  917. return -EISCONN;
  918. }
  919. /* Any userdata supplied? */
  920. if (userdata == NULL) {
  921. tx_skb = dev_alloc_skb(64);
  922. if (!tx_skb)
  923. return -ENOMEM;
  924. /* Reserve space for MUX_CONTROL and LAP header */
  925. skb_reserve(tx_skb, TTP_MAX_HEADER);
  926. } else {
  927. tx_skb = userdata;
  928. /*
  929. * Check that the client has reserved enough space for
  930. * headers
  931. */
  932. IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
  933. { dev_kfree_skb(userdata); return -1; } );
  934. }
  935. /* Initialize connection parameters */
  936. self->connected = FALSE;
  937. self->avail_credit = 0;
  938. self->rx_max_sdu_size = max_sdu_size;
  939. self->rx_sdu_size = 0;
  940. self->rx_sdu_busy = FALSE;
  941. self->dtsap_sel = dtsap_sel;
  942. n = self->initial_credit;
  943. self->remote_credit = 0;
  944. self->send_credit = 0;
  945. /*
  946. * Give away max 127 credits for now
  947. */
  948. if (n > 127) {
  949. self->avail_credit=n-127;
  950. n = 127;
  951. }
  952. self->remote_credit = n;
  953. /* SAR enabled? */
  954. if (max_sdu_size > 0) {
  955. IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
  956. { dev_kfree_skb(tx_skb); return -1; } );
  957. /* Insert SAR parameters */
  958. frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
  959. frame[0] = TTP_PARAMETERS | n;
  960. frame[1] = 0x04; /* Length */
  961. frame[2] = 0x01; /* MaxSduSize */
  962. frame[3] = 0x02; /* Value length */
  963. put_unaligned(cpu_to_be16((__u16) max_sdu_size),
  964. (__u16 *)(frame+4));
  965. } else {
  966. /* Insert plain TTP header */
  967. frame = skb_push(tx_skb, TTP_HEADER);
  968. /* Insert initial credit in frame */
  969. frame[0] = n & 0x7f;
  970. }
  971. /* Connect with IrLMP. No QoS parameters for now */
  972. return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
  973. tx_skb);
  974. }
  975. EXPORT_SYMBOL(irttp_connect_request);
  976. /*
  977. * Function irttp_connect_confirm (handle, qos, skb)
  978. *
  979. * Sevice user confirms TSAP connection with peer.
  980. *
  981. */
  982. static void irttp_connect_confirm(void *instance, void *sap,
  983. struct qos_info *qos, __u32 max_seg_size,
  984. __u8 max_header_size, struct sk_buff *skb)
  985. {
  986. struct tsap_cb *self;
  987. int parameters;
  988. int ret;
  989. __u8 plen;
  990. __u8 n;
  991. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  992. self = (struct tsap_cb *) instance;
  993. IRDA_ASSERT(self != NULL, return;);
  994. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  995. IRDA_ASSERT(skb != NULL, return;);
  996. self->max_seg_size = max_seg_size - TTP_HEADER;
  997. self->max_header_size = max_header_size + TTP_HEADER;
  998. /*
  999. * Check if we have got some QoS parameters back! This should be the
  1000. * negotiated QoS for the link.
  1001. */
  1002. if (qos) {
  1003. IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
  1004. qos->baud_rate.bits);
  1005. IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
  1006. qos->baud_rate.value);
  1007. }
  1008. n = skb->data[0] & 0x7f;
  1009. IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __FUNCTION__, n);
  1010. self->send_credit = n;
  1011. self->tx_max_sdu_size = 0;
  1012. self->connected = TRUE;
  1013. parameters = skb->data[0] & 0x80;
  1014. IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
  1015. skb_pull(skb, TTP_HEADER);
  1016. if (parameters) {
  1017. plen = skb->data[0];
  1018. ret = irda_param_extract_all(self, skb->data+1,
  1019. IRDA_MIN(skb->len-1, plen),
  1020. &param_info);
  1021. /* Any errors in the parameter list? */
  1022. if (ret < 0) {
  1023. IRDA_WARNING("%s: error extracting parameters\n",
  1024. __FUNCTION__);
  1025. dev_kfree_skb(skb);
  1026. /* Do not accept this connection attempt */
  1027. return;
  1028. }
  1029. /* Remove parameters */
  1030. skb_pull(skb, IRDA_MIN(skb->len, plen+1));
  1031. }
  1032. IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
  1033. self->send_credit, self->avail_credit, self->remote_credit);
  1034. IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __FUNCTION__,
  1035. self->tx_max_sdu_size);
  1036. if (self->notify.connect_confirm) {
  1037. self->notify.connect_confirm(self->notify.instance, self, qos,
  1038. self->tx_max_sdu_size,
  1039. self->max_header_size, skb);
  1040. } else
  1041. dev_kfree_skb(skb);
  1042. }
  1043. /*
  1044. * Function irttp_connect_indication (handle, skb)
  1045. *
  1046. * Some other device is connecting to this TSAP
  1047. *
  1048. */
  1049. void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
  1050. __u32 max_seg_size, __u8 max_header_size,
  1051. struct sk_buff *skb)
  1052. {
  1053. struct tsap_cb *self;
  1054. struct lsap_cb *lsap;
  1055. int parameters;
  1056. int ret;
  1057. __u8 plen;
  1058. __u8 n;
  1059. self = (struct tsap_cb *) instance;
  1060. IRDA_ASSERT(self != NULL, return;);
  1061. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  1062. IRDA_ASSERT(skb != NULL, return;);
  1063. lsap = (struct lsap_cb *) sap;
  1064. self->max_seg_size = max_seg_size - TTP_HEADER;
  1065. self->max_header_size = max_header_size+TTP_HEADER;
  1066. IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __FUNCTION__, self->stsap_sel);
  1067. /* Need to update dtsap_sel if its equal to LSAP_ANY */
  1068. self->dtsap_sel = lsap->dlsap_sel;
  1069. n = skb->data[0] & 0x7f;
  1070. self->send_credit = n;
  1071. self->tx_max_sdu_size = 0;
  1072. parameters = skb->data[0] & 0x80;
  1073. IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
  1074. skb_pull(skb, TTP_HEADER);
  1075. if (parameters) {
  1076. plen = skb->data[0];
  1077. ret = irda_param_extract_all(self, skb->data+1,
  1078. IRDA_MIN(skb->len-1, plen),
  1079. &param_info);
  1080. /* Any errors in the parameter list? */
  1081. if (ret < 0) {
  1082. IRDA_WARNING("%s: error extracting parameters\n",
  1083. __FUNCTION__);
  1084. dev_kfree_skb(skb);
  1085. /* Do not accept this connection attempt */
  1086. return;
  1087. }
  1088. /* Remove parameters */
  1089. skb_pull(skb, IRDA_MIN(skb->len, plen+1));
  1090. }
  1091. if (self->notify.connect_indication) {
  1092. self->notify.connect_indication(self->notify.instance, self,
  1093. qos, self->tx_max_sdu_size,
  1094. self->max_header_size, skb);
  1095. } else
  1096. dev_kfree_skb(skb);
  1097. }
  1098. /*
  1099. * Function irttp_connect_response (handle, userdata)
  1100. *
  1101. * Service user is accepting the connection, just pass it down to
  1102. * IrLMP!
  1103. *
  1104. */
  1105. int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
  1106. struct sk_buff *userdata)
  1107. {
  1108. struct sk_buff *tx_skb;
  1109. __u8 *frame;
  1110. int ret;
  1111. __u8 n;
  1112. IRDA_ASSERT(self != NULL, return -1;);
  1113. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  1114. IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __FUNCTION__,
  1115. self->stsap_sel);
  1116. /* Any userdata supplied? */
  1117. if (userdata == NULL) {
  1118. tx_skb = dev_alloc_skb(64);
  1119. if (!tx_skb)
  1120. return -ENOMEM;
  1121. /* Reserve space for MUX_CONTROL and LAP header */
  1122. skb_reserve(tx_skb, TTP_MAX_HEADER);
  1123. } else {
  1124. tx_skb = userdata;
  1125. /*
  1126. * Check that the client has reserved enough space for
  1127. * headers
  1128. */
  1129. IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
  1130. { dev_kfree_skb(userdata); return -1; } );
  1131. }
  1132. self->avail_credit = 0;
  1133. self->remote_credit = 0;
  1134. self->rx_max_sdu_size = max_sdu_size;
  1135. self->rx_sdu_size = 0;
  1136. self->rx_sdu_busy = FALSE;
  1137. n = self->initial_credit;
  1138. /* Frame has only space for max 127 credits (7 bits) */
  1139. if (n > 127) {
  1140. self->avail_credit = n - 127;
  1141. n = 127;
  1142. }
  1143. self->remote_credit = n;
  1144. self->connected = TRUE;
  1145. /* SAR enabled? */
  1146. if (max_sdu_size > 0) {
  1147. IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
  1148. { dev_kfree_skb(tx_skb); return -1; } );
  1149. /* Insert TTP header with SAR parameters */
  1150. frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
  1151. frame[0] = TTP_PARAMETERS | n;
  1152. frame[1] = 0x04; /* Length */
  1153. /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
  1154. /* TTP_SAR_HEADER, &param_info) */
  1155. frame[2] = 0x01; /* MaxSduSize */
  1156. frame[3] = 0x02; /* Value length */
  1157. put_unaligned(cpu_to_be16((__u16) max_sdu_size),
  1158. (__u16 *)(frame+4));
  1159. } else {
  1160. /* Insert TTP header */
  1161. frame = skb_push(tx_skb, TTP_HEADER);
  1162. frame[0] = n & 0x7f;
  1163. }
  1164. ret = irlmp_connect_response(self->lsap, tx_skb);
  1165. return ret;
  1166. }
  1167. EXPORT_SYMBOL(irttp_connect_response);
  1168. /*
  1169. * Function irttp_dup (self, instance)
  1170. *
  1171. * Duplicate TSAP, can be used by servers to confirm a connection on a
  1172. * new TSAP so it can keep listening on the old one.
  1173. */
  1174. struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
  1175. {
  1176. struct tsap_cb *new;
  1177. unsigned long flags;
  1178. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  1179. /* Protect our access to the old tsap instance */
  1180. spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
  1181. /* Find the old instance */
  1182. if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
  1183. IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __FUNCTION__);
  1184. spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
  1185. return NULL;
  1186. }
  1187. /* Allocate a new instance */
  1188. new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
  1189. if (!new) {
  1190. IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
  1191. spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
  1192. return NULL;
  1193. }
  1194. /* Dup */
  1195. memcpy(new, orig, sizeof(struct tsap_cb));
  1196. /* We don't need the old instance any more */
  1197. spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
  1198. /* Try to dup the LSAP (may fail if we were too slow) */
  1199. new->lsap = irlmp_dup(orig->lsap, new);
  1200. if (!new->lsap) {
  1201. IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
  1202. kfree(new);
  1203. return NULL;
  1204. }
  1205. /* Not everything should be copied */
  1206. new->notify.instance = instance;
  1207. init_timer(&new->todo_timer);
  1208. skb_queue_head_init(&new->rx_queue);
  1209. skb_queue_head_init(&new->tx_queue);
  1210. skb_queue_head_init(&new->rx_fragments);
  1211. /* This is locked */
  1212. hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
  1213. return new;
  1214. }
  1215. EXPORT_SYMBOL(irttp_dup);
  1216. /*
  1217. * Function irttp_disconnect_request (self)
  1218. *
  1219. * Close this connection please! If priority is high, the queued data
  1220. * segments, if any, will be deallocated first
  1221. *
  1222. */
  1223. int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
  1224. int priority)
  1225. {
  1226. int ret;
  1227. IRDA_ASSERT(self != NULL, return -1;);
  1228. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
  1229. /* Already disconnected? */
  1230. if (!self->connected) {
  1231. IRDA_DEBUG(4, "%s(), already disconnected!\n", __FUNCTION__);
  1232. if (userdata)
  1233. dev_kfree_skb(userdata);
  1234. return -1;
  1235. }
  1236. /* Disconnect already pending ?
  1237. * We need to use an atomic operation to prevent reentry. This
  1238. * function may be called from various context, like user, timer
  1239. * for following a disconnect_indication() (i.e. net_bh).
  1240. * Jean II */
  1241. if(test_and_set_bit(0, &self->disconnect_pend)) {
  1242. IRDA_DEBUG(0, "%s(), disconnect already pending\n",
  1243. __FUNCTION__);
  1244. if (userdata)
  1245. dev_kfree_skb(userdata);
  1246. /* Try to make some progress */
  1247. irttp_run_tx_queue(self);
  1248. return -1;
  1249. }
  1250. /*
  1251. * Check if there is still data segments in the transmit queue
  1252. */
  1253. if (!skb_queue_empty(&self->tx_queue)) {
  1254. if (priority == P_HIGH) {
  1255. /*
  1256. * No need to send the queued data, if we are
  1257. * disconnecting right now since the data will
  1258. * not have any usable connection to be sent on
  1259. */
  1260. IRDA_DEBUG(1, "%s(): High priority!!()\n", __FUNCTION__);
  1261. irttp_flush_queues(self);
  1262. } else if (priority == P_NORMAL) {
  1263. /*
  1264. * Must delay disconnect until after all data segments
  1265. * have been sent and the tx_queue is empty
  1266. */
  1267. /* We'll reuse this one later for the disconnect */
  1268. self->disconnect_skb = userdata; /* May be NULL */
  1269. irttp_run_tx_queue(self);
  1270. irttp_start_todo_timer(self, HZ/10);
  1271. return -1;
  1272. }
  1273. }
  1274. /* Note : we don't need to check if self->rx_queue is full and the
  1275. * state of self->rx_sdu_busy because the disconnect response will
  1276. * be sent at the LMP level (so even if the peer has its Tx queue
  1277. * full of data). - Jean II */
  1278. IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __FUNCTION__);
  1279. self->connected = FALSE;
  1280. if (!userdata) {
  1281. struct sk_buff *tx_skb;
  1282. tx_skb = dev_alloc_skb(64);
  1283. if (!tx_skb)
  1284. return -ENOMEM;
  1285. /*
  1286. * Reserve space for MUX and LAP header
  1287. */
  1288. skb_reserve(tx_skb, TTP_MAX_HEADER);
  1289. userdata = tx_skb;
  1290. }
  1291. ret = irlmp_disconnect_request(self->lsap, userdata);
  1292. /* The disconnect is no longer pending */
  1293. clear_bit(0, &self->disconnect_pend); /* FALSE */
  1294. return ret;
  1295. }
  1296. EXPORT_SYMBOL(irttp_disconnect_request);
  1297. /*
  1298. * Function irttp_disconnect_indication (self, reason)
  1299. *
  1300. * Disconnect indication, TSAP disconnected by peer?
  1301. *
  1302. */
  1303. void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason,
  1304. struct sk_buff *skb)
  1305. {
  1306. struct tsap_cb *self;
  1307. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1308. self = (struct tsap_cb *) instance;
  1309. IRDA_ASSERT(self != NULL, return;);
  1310. IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
  1311. /* Prevent higher layer to send more data */
  1312. self->connected = FALSE;
  1313. /* Check if client has already tried to close the TSAP */
  1314. if (self->close_pend) {
  1315. /* In this case, the higher layer is probably gone. Don't
  1316. * bother it and clean up the remains - Jean II */
  1317. if (skb)
  1318. dev_kfree_skb(skb);
  1319. irttp_close_tsap(self);
  1320. return;
  1321. }
  1322. /* If we are here, we assume that is the higher layer is still
  1323. * waiting for the disconnect notification and able to process it,
  1324. * even if he tried to disconnect. Otherwise, it would have already
  1325. * attempted to close the tsap and self->close_pend would be TRUE.
  1326. * Jean II */
  1327. /* No need to notify the client if has already tried to disconnect */
  1328. if(self->notify.disconnect_indication)
  1329. self->notify.disconnect_indication(self->notify.instance, self,
  1330. reason, skb);
  1331. else
  1332. if (skb)
  1333. dev_kfree_skb(skb);
  1334. }
  1335. /*
  1336. * Function irttp_do_data_indication (self, skb)
  1337. *
  1338. * Try to deliver reassembled skb to layer above, and requeue it if that
  1339. * for some reason should fail. We mark rx sdu as busy to apply back
  1340. * pressure is necessary.
  1341. */
  1342. static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
  1343. {
  1344. int err;
  1345. /* Check if client has already closed the TSAP and gone away */
  1346. if (self->close_pend) {
  1347. dev_kfree_skb(skb);
  1348. return;
  1349. }
  1350. err = self->notify.data_indication(self->notify.instance, self, skb);
  1351. /* Usually the layer above will notify that it's input queue is
  1352. * starting to get filled by using the flow request, but this may
  1353. * be difficult, so it can instead just refuse to eat it and just
  1354. * give an error back
  1355. */
  1356. if (err) {
  1357. IRDA_DEBUG(0, "%s() requeueing skb!\n", __FUNCTION__);
  1358. /* Make sure we take a break */
  1359. self->rx_sdu_busy = TRUE;
  1360. /* Need to push the header in again */
  1361. skb_push(skb, TTP_HEADER);
  1362. skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
  1363. /* Put skb back on queue */
  1364. skb_queue_head(&self->rx_queue, skb);
  1365. }
  1366. }
  1367. /*
  1368. * Function irttp_run_rx_queue (self)
  1369. *
  1370. * Check if we have any frames to be transmitted, or if we have any
  1371. * available credit to give away.
  1372. */
  1373. void irttp_run_rx_queue(struct tsap_cb *self)
  1374. {
  1375. struct sk_buff *skb;
  1376. int more = 0;
  1377. IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
  1378. self->send_credit, self->avail_credit, self->remote_credit);
  1379. /* Get exclusive access to the rx queue, otherwise don't touch it */
  1380. if (irda_lock(&self->rx_queue_lock) == FALSE)
  1381. return;
  1382. /*
  1383. * Reassemble all frames in receive queue and deliver them
  1384. */
  1385. while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
  1386. /* This bit will tell us if it's the last fragment or not */
  1387. more = skb->data[0] & 0x80;
  1388. /* Remove TTP header */
  1389. skb_pull(skb, TTP_HEADER);
  1390. /* Add the length of the remaining data */
  1391. self->rx_sdu_size += skb->len;
  1392. /*
  1393. * If SAR is disabled, or user has requested no reassembly
  1394. * of received fragments then we just deliver them
  1395. * immediately. This can be requested by clients that
  1396. * implements byte streams without any message boundaries
  1397. */
  1398. if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
  1399. irttp_do_data_indication(self, skb);
  1400. self->rx_sdu_size = 0;
  1401. continue;
  1402. }
  1403. /* Check if this is a fragment, and not the last fragment */
  1404. if (more) {
  1405. /*
  1406. * Queue the fragment if we still are within the
  1407. * limits of the maximum size of the rx_sdu
  1408. */
  1409. if (self->rx_sdu_size <= self->rx_max_sdu_size) {
  1410. IRDA_DEBUG(4, "%s(), queueing frag\n",
  1411. __FUNCTION__);
  1412. skb_queue_tail(&self->rx_fragments, skb);
  1413. } else {
  1414. /* Free the part of the SDU that is too big */
  1415. dev_kfree_skb(skb);
  1416. }
  1417. continue;
  1418. }
  1419. /*
  1420. * This is the last fragment, so time to reassemble!
  1421. */
  1422. if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
  1423. (self->rx_max_sdu_size == TTP_SAR_UNBOUND))
  1424. {
  1425. /*
  1426. * A little optimizing. Only queue the fragment if
  1427. * there are other fragments. Since if this is the
  1428. * last and only fragment, there is no need to
  1429. * reassemble :-)
  1430. */
  1431. if (!skb_queue_empty(&self->rx_fragments)) {
  1432. skb_queue_tail(&self->rx_fragments,
  1433. skb);
  1434. skb = irttp_reassemble_skb(self);
  1435. }
  1436. /* Now we can deliver the reassembled skb */
  1437. irttp_do_data_indication(self, skb);
  1438. } else {
  1439. IRDA_DEBUG(1, "%s(), Truncated frame\n", __FUNCTION__);
  1440. /* Free the part of the SDU that is too big */
  1441. dev_kfree_skb(skb);
  1442. /* Deliver only the valid but truncated part of SDU */
  1443. skb = irttp_reassemble_skb(self);
  1444. irttp_do_data_indication(self, skb);
  1445. }
  1446. self->rx_sdu_size = 0;
  1447. }
  1448. /*
  1449. * It's not trivial to keep track of how many credits are available
  1450. * by incrementing at each packet, because delivery may fail
  1451. * (irttp_do_data_indication() may requeue the frame) and because
  1452. * we need to take care of fragmentation.
  1453. * We want the other side to send up to initial_credit packets.
  1454. * We have some frames in our queues, and we have already allowed it
  1455. * to send remote_credit.
  1456. * No need to spinlock, write is atomic and self correcting...
  1457. * Jean II
  1458. */
  1459. self->avail_credit = (self->initial_credit -
  1460. (self->remote_credit +
  1461. skb_queue_len(&self->rx_queue) +
  1462. skb_queue_len(&self->rx_fragments)));
  1463. /* Do we have too much credits to send to peer ? */
  1464. if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
  1465. (self->avail_credit > 0)) {
  1466. /* Send explicit credit frame */
  1467. irttp_give_credit(self);
  1468. /* Note : do *NOT* check if tx_queue is non-empty, that
  1469. * will produce deadlocks. I repeat : send a credit frame
  1470. * even if we have something to send in our Tx queue.
  1471. * If we have credits, it means that our Tx queue is blocked.
  1472. *
  1473. * Let's suppose the peer can't keep up with our Tx. He will
  1474. * flow control us by not sending us any credits, and we
  1475. * will stop Tx and start accumulating credits here.
  1476. * Up to the point where the peer will stop its Tx queue,
  1477. * for lack of credits.
  1478. * Let's assume the peer application is single threaded.
  1479. * It will block on Tx and never consume any Rx buffer.
  1480. * Deadlock. Guaranteed. - Jean II
  1481. */
  1482. }
  1483. /* Reset lock */
  1484. self->rx_queue_lock = 0;
  1485. }
  1486. #ifdef CONFIG_PROC_FS
  1487. struct irttp_iter_state {
  1488. int id;
  1489. };
  1490. static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
  1491. {
  1492. struct irttp_iter_state *iter = seq->private;
  1493. struct tsap_cb *self;
  1494. /* Protect our access to the tsap list */
  1495. spin_lock_irq(&irttp->tsaps->hb_spinlock);
  1496. iter->id = 0;
  1497. for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
  1498. self != NULL;
  1499. self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
  1500. if (iter->id == *pos)
  1501. break;
  1502. ++iter->id;
  1503. }
  1504. return self;
  1505. }
  1506. static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1507. {
  1508. struct irttp_iter_state *iter = seq->private;
  1509. ++*pos;
  1510. ++iter->id;
  1511. return (void *) hashbin_get_next(irttp->tsaps);
  1512. }
  1513. static void irttp_seq_stop(struct seq_file *seq, void *v)
  1514. {
  1515. spin_unlock_irq(&irttp->tsaps->hb_spinlock);
  1516. }
  1517. static int irttp_seq_show(struct seq_file *seq, void *v)
  1518. {
  1519. const struct irttp_iter_state *iter = seq->private;
  1520. const struct tsap_cb *self = v;
  1521. seq_printf(seq, "TSAP %d, ", iter->id);
  1522. seq_printf(seq, "stsap_sel: %02x, ",
  1523. self->stsap_sel);
  1524. seq_printf(seq, "dtsap_sel: %02x\n",
  1525. self->dtsap_sel);
  1526. seq_printf(seq, " connected: %s, ",
  1527. self->connected? "TRUE":"FALSE");
  1528. seq_printf(seq, "avail credit: %d, ",
  1529. self->avail_credit);
  1530. seq_printf(seq, "remote credit: %d, ",
  1531. self->remote_credit);
  1532. seq_printf(seq, "send credit: %d\n",
  1533. self->send_credit);
  1534. seq_printf(seq, " tx packets: %ld, ",
  1535. self->stats.tx_packets);
  1536. seq_printf(seq, "rx packets: %ld, ",
  1537. self->stats.rx_packets);
  1538. seq_printf(seq, "tx_queue len: %d ",
  1539. skb_queue_len(&self->tx_queue));
  1540. seq_printf(seq, "rx_queue len: %d\n",
  1541. skb_queue_len(&self->rx_queue));
  1542. seq_printf(seq, " tx_sdu_busy: %s, ",
  1543. self->tx_sdu_busy? "TRUE":"FALSE");
  1544. seq_printf(seq, "rx_sdu_busy: %s\n",
  1545. self->rx_sdu_busy? "TRUE":"FALSE");
  1546. seq_printf(seq, " max_seg_size: %d, ",
  1547. self->max_seg_size);
  1548. seq_printf(seq, "tx_max_sdu_size: %d, ",
  1549. self->tx_max_sdu_size);
  1550. seq_printf(seq, "rx_max_sdu_size: %d\n",
  1551. self->rx_max_sdu_size);
  1552. seq_printf(seq, " Used by (%s)\n\n",
  1553. self->notify.name);
  1554. return 0;
  1555. }
  1556. static struct seq_operations irttp_seq_ops = {
  1557. .start = irttp_seq_start,
  1558. .next = irttp_seq_next,
  1559. .stop = irttp_seq_stop,
  1560. .show = irttp_seq_show,
  1561. };
  1562. static int irttp_seq_open(struct inode *inode, struct file *file)
  1563. {
  1564. struct seq_file *seq;
  1565. int rc = -ENOMEM;
  1566. struct irttp_iter_state *s;
  1567. IRDA_ASSERT(irttp != NULL, return -EINVAL;);
  1568. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1569. if (!s)
  1570. goto out;
  1571. rc = seq_open(file, &irttp_seq_ops);
  1572. if (rc)
  1573. goto out_kfree;
  1574. seq = file->private_data;
  1575. seq->private = s;
  1576. memset(s, 0, sizeof(*s));
  1577. out:
  1578. return rc;
  1579. out_kfree:
  1580. kfree(s);
  1581. goto out;
  1582. }
  1583. struct file_operations irttp_seq_fops = {
  1584. .owner = THIS_MODULE,
  1585. .open = irttp_seq_open,
  1586. .read = seq_read,
  1587. .llseek = seq_lseek,
  1588. .release = seq_release_private,
  1589. };
  1590. #endif /* PROC_FS */