irttp.c 51 KB

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