irttp.c 51 KB

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