irlap.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*********************************************************************
  2. *
  3. * Filename: irlap.c
  4. * Version: 1.0
  5. * Description: IrLAP implementation for Linux
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Mon Aug 4 20:40:53 1997
  9. * Modified at: Tue Dec 14 09:26:44 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
  13. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. *
  30. ********************************************************************/
  31. #include <linux/slab.h>
  32. #include <linux/string.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/delay.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/init.h>
  37. #include <linux/random.h>
  38. #include <linux/module.h>
  39. #include <linux/seq_file.h>
  40. #include <net/irda/irda.h>
  41. #include <net/irda/irda_device.h>
  42. #include <net/irda/irqueue.h>
  43. #include <net/irda/irlmp.h>
  44. #include <net/irda/irlmp_frame.h>
  45. #include <net/irda/irlap_frame.h>
  46. #include <net/irda/irlap.h>
  47. #include <net/irda/timer.h>
  48. #include <net/irda/qos.h>
  49. static hashbin_t *irlap = NULL;
  50. int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
  51. /* This is the delay of missed pf period before generating an event
  52. * to the application. The spec mandate 3 seconds, but in some cases
  53. * it's way too long. - Jean II */
  54. int sysctl_warn_noreply_time = 3;
  55. extern void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb);
  56. static void __irlap_close(struct irlap_cb *self);
  57. static void irlap_init_qos_capabilities(struct irlap_cb *self,
  58. struct qos_info *qos_user);
  59. #ifdef CONFIG_IRDA_DEBUG
  60. static char *lap_reasons[] = {
  61. "ERROR, NOT USED",
  62. "LAP_DISC_INDICATION",
  63. "LAP_NO_RESPONSE",
  64. "LAP_RESET_INDICATION",
  65. "LAP_FOUND_NONE",
  66. "LAP_MEDIA_BUSY",
  67. "LAP_PRIMARY_CONFLICT",
  68. "ERROR, NOT USED",
  69. };
  70. #endif /* CONFIG_IRDA_DEBUG */
  71. int __init irlap_init(void)
  72. {
  73. /* Check if the compiler did its job properly.
  74. * May happen on some ARM configuration, check with Russell King. */
  75. IRDA_ASSERT(sizeof(struct xid_frame) == 14, ;);
  76. IRDA_ASSERT(sizeof(struct test_frame) == 10, ;);
  77. IRDA_ASSERT(sizeof(struct ua_frame) == 10, ;);
  78. IRDA_ASSERT(sizeof(struct snrm_frame) == 11, ;);
  79. /* Allocate master array */
  80. irlap = hashbin_new(HB_LOCK);
  81. if (irlap == NULL) {
  82. IRDA_ERROR("%s: can't allocate irlap hashbin!\n",
  83. __FUNCTION__);
  84. return -ENOMEM;
  85. }
  86. return 0;
  87. }
  88. void __exit irlap_cleanup(void)
  89. {
  90. IRDA_ASSERT(irlap != NULL, return;);
  91. hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
  92. }
  93. /*
  94. * Function irlap_open (driver)
  95. *
  96. * Initialize IrLAP layer
  97. *
  98. */
  99. struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos,
  100. const char *hw_name)
  101. {
  102. struct irlap_cb *self;
  103. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  104. /* Initialize the irlap structure. */
  105. self = kmalloc(sizeof(struct irlap_cb), GFP_KERNEL);
  106. if (self == NULL)
  107. return NULL;
  108. memset(self, 0, sizeof(struct irlap_cb));
  109. self->magic = LAP_MAGIC;
  110. /* Make a binding between the layers */
  111. self->netdev = dev;
  112. self->qos_dev = qos;
  113. /* Copy hardware name */
  114. if(hw_name != NULL) {
  115. strlcpy(self->hw_name, hw_name, sizeof(self->hw_name));
  116. } else {
  117. self->hw_name[0] = '\0';
  118. }
  119. /* FIXME: should we get our own field? */
  120. dev->atalk_ptr = self;
  121. self->state = LAP_OFFLINE;
  122. /* Initialize transmit queue */
  123. skb_queue_head_init(&self->txq);
  124. skb_queue_head_init(&self->txq_ultra);
  125. skb_queue_head_init(&self->wx_list);
  126. /* My unique IrLAP device address! */
  127. /* We don't want the broadcast address, neither the NULL address
  128. * (most often used to signify "invalid"), and we don't want an
  129. * address already in use (otherwise connect won't be able
  130. * to select the proper link). - Jean II */
  131. do {
  132. get_random_bytes(&self->saddr, sizeof(self->saddr));
  133. } while ((self->saddr == 0x0) || (self->saddr == BROADCAST) ||
  134. (hashbin_lock_find(irlap, self->saddr, NULL)) );
  135. /* Copy to the driver */
  136. memcpy(dev->dev_addr, &self->saddr, 4);
  137. init_timer(&self->slot_timer);
  138. init_timer(&self->query_timer);
  139. init_timer(&self->discovery_timer);
  140. init_timer(&self->final_timer);
  141. init_timer(&self->poll_timer);
  142. init_timer(&self->wd_timer);
  143. init_timer(&self->backoff_timer);
  144. init_timer(&self->media_busy_timer);
  145. irlap_apply_default_connection_parameters(self);
  146. self->N3 = 3; /* # connections attemts to try before giving up */
  147. self->state = LAP_NDM;
  148. hashbin_insert(irlap, (irda_queue_t *) self, self->saddr, NULL);
  149. irlmp_register_link(self, self->saddr, &self->notify);
  150. return self;
  151. }
  152. EXPORT_SYMBOL(irlap_open);
  153. /*
  154. * Function __irlap_close (self)
  155. *
  156. * Remove IrLAP and all allocated memory. Stop any pending timers.
  157. *
  158. */
  159. static void __irlap_close(struct irlap_cb *self)
  160. {
  161. IRDA_ASSERT(self != NULL, return;);
  162. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  163. /* Stop timers */
  164. del_timer(&self->slot_timer);
  165. del_timer(&self->query_timer);
  166. del_timer(&self->discovery_timer);
  167. del_timer(&self->final_timer);
  168. del_timer(&self->poll_timer);
  169. del_timer(&self->wd_timer);
  170. del_timer(&self->backoff_timer);
  171. del_timer(&self->media_busy_timer);
  172. irlap_flush_all_queues(self);
  173. self->magic = 0;
  174. kfree(self);
  175. }
  176. /*
  177. * Function irlap_close (self)
  178. *
  179. * Remove IrLAP instance
  180. *
  181. */
  182. void irlap_close(struct irlap_cb *self)
  183. {
  184. struct irlap_cb *lap;
  185. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  186. IRDA_ASSERT(self != NULL, return;);
  187. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  188. /* We used to send a LAP_DISC_INDICATION here, but this was
  189. * racy. This has been move within irlmp_unregister_link()
  190. * itself. Jean II */
  191. /* Kill the LAP and all LSAPs on top of it */
  192. irlmp_unregister_link(self->saddr);
  193. self->notify.instance = NULL;
  194. /* Be sure that we manage to remove ourself from the hash */
  195. lap = hashbin_remove(irlap, self->saddr, NULL);
  196. if (!lap) {
  197. IRDA_DEBUG(1, "%s(), Didn't find myself!\n", __FUNCTION__);
  198. return;
  199. }
  200. __irlap_close(lap);
  201. }
  202. EXPORT_SYMBOL(irlap_close);
  203. /*
  204. * Function irlap_connect_indication (self, skb)
  205. *
  206. * Another device is attempting to make a connection
  207. *
  208. */
  209. void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb)
  210. {
  211. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  212. IRDA_ASSERT(self != NULL, return;);
  213. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  214. irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
  215. irlmp_link_connect_indication(self->notify.instance, self->saddr,
  216. self->daddr, &self->qos_tx, skb);
  217. }
  218. /*
  219. * Function irlap_connect_response (self, skb)
  220. *
  221. * Service user has accepted incoming connection
  222. *
  223. */
  224. void irlap_connect_response(struct irlap_cb *self, struct sk_buff *userdata)
  225. {
  226. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  227. irlap_do_event(self, CONNECT_RESPONSE, userdata, NULL);
  228. }
  229. /*
  230. * Function irlap_connect_request (self, daddr, qos_user, sniff)
  231. *
  232. * Request connection with another device, sniffing is not implemented
  233. * yet.
  234. *
  235. */
  236. void irlap_connect_request(struct irlap_cb *self, __u32 daddr,
  237. struct qos_info *qos_user, int sniff)
  238. {
  239. IRDA_DEBUG(3, "%s(), daddr=0x%08x\n", __FUNCTION__, daddr);
  240. IRDA_ASSERT(self != NULL, return;);
  241. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  242. self->daddr = daddr;
  243. /*
  244. * If the service user specifies QoS values for this connection,
  245. * then use them
  246. */
  247. irlap_init_qos_capabilities(self, qos_user);
  248. if ((self->state == LAP_NDM) && !self->media_busy)
  249. irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
  250. else
  251. self->connect_pending = TRUE;
  252. }
  253. /*
  254. * Function irlap_connect_confirm (self, skb)
  255. *
  256. * Connection request has been accepted
  257. *
  258. */
  259. void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
  260. {
  261. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  262. IRDA_ASSERT(self != NULL, return;);
  263. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  264. irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
  265. }
  266. /*
  267. * Function irlap_data_indication (self, skb)
  268. *
  269. * Received data frames from IR-port, so we just pass them up to
  270. * IrLMP for further processing
  271. *
  272. */
  273. void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
  274. int unreliable)
  275. {
  276. /* Hide LAP header from IrLMP layer */
  277. skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  278. irlmp_link_data_indication(self->notify.instance, skb, unreliable);
  279. }
  280. /*
  281. * Function irlap_data_request (self, skb)
  282. *
  283. * Queue data for transmission, must wait until XMIT state
  284. *
  285. */
  286. void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb,
  287. int unreliable)
  288. {
  289. IRDA_ASSERT(self != NULL, return;);
  290. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  291. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  292. IRDA_ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
  293. return;);
  294. skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  295. /*
  296. * Must set frame format now so that the rest of the code knows
  297. * if its dealing with an I or an UI frame
  298. */
  299. if (unreliable)
  300. skb->data[1] = UI_FRAME;
  301. else
  302. skb->data[1] = I_FRAME;
  303. /* Don't forget to refcount it - see irlmp_connect_request(). */
  304. skb_get(skb);
  305. /* Add at the end of the queue (keep ordering) - Jean II */
  306. skb_queue_tail(&self->txq, skb);
  307. /*
  308. * Send event if this frame only if we are in the right state
  309. * FIXME: udata should be sent first! (skb_queue_head?)
  310. */
  311. if ((self->state == LAP_XMIT_P) || (self->state == LAP_XMIT_S)) {
  312. /* If we are not already processing the Tx queue, trigger
  313. * transmission immediately - Jean II */
  314. if((skb_queue_len(&self->txq) <= 1) && (!self->local_busy))
  315. irlap_do_event(self, DATA_REQUEST, skb, NULL);
  316. /* Otherwise, the packets will be sent normally at the
  317. * next pf-poll - Jean II */
  318. }
  319. }
  320. /*
  321. * Function irlap_unitdata_request (self, skb)
  322. *
  323. * Send Ultra data. This is data that must be sent outside any connection
  324. *
  325. */
  326. #ifdef CONFIG_IRDA_ULTRA
  327. void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
  328. {
  329. IRDA_ASSERT(self != NULL, return;);
  330. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  331. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  332. IRDA_ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
  333. return;);
  334. skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  335. skb->data[0] = CBROADCAST;
  336. skb->data[1] = UI_FRAME;
  337. /* Don't need to refcount, see irlmp_connless_data_request() */
  338. skb_queue_tail(&self->txq_ultra, skb);
  339. irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
  340. }
  341. #endif /*CONFIG_IRDA_ULTRA */
  342. /*
  343. * Function irlap_udata_indication (self, skb)
  344. *
  345. * Receive Ultra data. This is data that is received outside any connection
  346. *
  347. */
  348. #ifdef CONFIG_IRDA_ULTRA
  349. void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
  350. {
  351. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  352. IRDA_ASSERT(self != NULL, return;);
  353. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  354. IRDA_ASSERT(skb != NULL, return;);
  355. /* Hide LAP header from IrLMP layer */
  356. skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  357. irlmp_link_unitdata_indication(self->notify.instance, skb);
  358. }
  359. #endif /* CONFIG_IRDA_ULTRA */
  360. /*
  361. * Function irlap_disconnect_request (void)
  362. *
  363. * Request to disconnect connection by service user
  364. */
  365. void irlap_disconnect_request(struct irlap_cb *self)
  366. {
  367. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  368. IRDA_ASSERT(self != NULL, return;);
  369. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  370. /* Don't disconnect until all data frames are successfully sent */
  371. if (!skb_queue_empty(&self->txq)) {
  372. self->disconnect_pending = TRUE;
  373. return;
  374. }
  375. /* Check if we are in the right state for disconnecting */
  376. switch (self->state) {
  377. case LAP_XMIT_P: /* FALLTROUGH */
  378. case LAP_XMIT_S: /* FALLTROUGH */
  379. case LAP_CONN: /* FALLTROUGH */
  380. case LAP_RESET_WAIT: /* FALLTROUGH */
  381. case LAP_RESET_CHECK:
  382. irlap_do_event(self, DISCONNECT_REQUEST, NULL, NULL);
  383. break;
  384. default:
  385. IRDA_DEBUG(2, "%s(), disconnect pending!\n", __FUNCTION__);
  386. self->disconnect_pending = TRUE;
  387. break;
  388. }
  389. }
  390. /*
  391. * Function irlap_disconnect_indication (void)
  392. *
  393. * Disconnect request from other device
  394. *
  395. */
  396. void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason)
  397. {
  398. IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, lap_reasons[reason]);
  399. IRDA_ASSERT(self != NULL, return;);
  400. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  401. /* Flush queues */
  402. irlap_flush_all_queues(self);
  403. switch (reason) {
  404. case LAP_RESET_INDICATION:
  405. IRDA_DEBUG(1, "%s(), Sending reset request!\n", __FUNCTION__);
  406. irlap_do_event(self, RESET_REQUEST, NULL, NULL);
  407. break;
  408. case LAP_NO_RESPONSE: /* FALLTROUGH */
  409. case LAP_DISC_INDICATION: /* FALLTROUGH */
  410. case LAP_FOUND_NONE: /* FALLTROUGH */
  411. case LAP_MEDIA_BUSY:
  412. irlmp_link_disconnect_indication(self->notify.instance, self,
  413. reason, NULL);
  414. break;
  415. default:
  416. IRDA_ERROR("%s: Unknown reason %d\n", __FUNCTION__, reason);
  417. }
  418. }
  419. /*
  420. * Function irlap_discovery_request (gen_addr_bit)
  421. *
  422. * Start one single discovery operation.
  423. *
  424. */
  425. void irlap_discovery_request(struct irlap_cb *self, discovery_t *discovery)
  426. {
  427. struct irlap_info info;
  428. IRDA_ASSERT(self != NULL, return;);
  429. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  430. IRDA_ASSERT(discovery != NULL, return;);
  431. IRDA_DEBUG(4, "%s(), nslots = %d\n", __FUNCTION__, discovery->nslots);
  432. IRDA_ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
  433. (discovery->nslots == 8) || (discovery->nslots == 16),
  434. return;);
  435. /* Discovery is only possible in NDM mode */
  436. if (self->state != LAP_NDM) {
  437. IRDA_DEBUG(4, "%s(), discovery only possible in NDM mode\n",
  438. __FUNCTION__);
  439. irlap_discovery_confirm(self, NULL);
  440. /* Note : in theory, if we are not in NDM, we could postpone
  441. * the discovery like we do for connection request.
  442. * In practice, it's not worth it. If the media was busy,
  443. * it's likely next time around it won't be busy. If we are
  444. * in REPLY state, we will get passive discovery info & event.
  445. * Jean II */
  446. return;
  447. }
  448. /* Check if last discovery request finished in time, or if
  449. * it was aborted due to the media busy flag. */
  450. if (self->discovery_log != NULL) {
  451. hashbin_delete(self->discovery_log, (FREE_FUNC) kfree);
  452. self->discovery_log = NULL;
  453. }
  454. /* All operations will occur at predictable time, no need to lock */
  455. self->discovery_log = hashbin_new(HB_NOLOCK);
  456. if (self->discovery_log == NULL) {
  457. IRDA_WARNING("%s(), Unable to allocate discovery log!\n",
  458. __FUNCTION__);
  459. return;
  460. }
  461. info.S = discovery->nslots; /* Number of slots */
  462. info.s = 0; /* Current slot */
  463. self->discovery_cmd = discovery;
  464. info.discovery = discovery;
  465. /* sysctl_slot_timeout bounds are checked in irsysctl.c - Jean II */
  466. self->slot_timeout = sysctl_slot_timeout * HZ / 1000;
  467. irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
  468. }
  469. /*
  470. * Function irlap_discovery_confirm (log)
  471. *
  472. * A device has been discovered in front of this station, we
  473. * report directly to LMP.
  474. */
  475. void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log)
  476. {
  477. IRDA_ASSERT(self != NULL, return;);
  478. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  479. IRDA_ASSERT(self->notify.instance != NULL, return;);
  480. /*
  481. * Check for successful discovery, since we are then allowed to clear
  482. * the media busy condition (IrLAP 6.13.4 - p.94). This should allow
  483. * us to make connection attempts much faster and easier (i.e. no
  484. * collisions).
  485. * Setting media busy to false will also generate an event allowing
  486. * to process pending events in NDM state machine.
  487. * Note : the spec doesn't define what's a successful discovery is.
  488. * If we want Ultra to work, it's successful even if there is
  489. * nobody discovered - Jean II
  490. */
  491. if (discovery_log)
  492. irda_device_set_media_busy(self->netdev, FALSE);
  493. /* Inform IrLMP */
  494. irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
  495. }
  496. /*
  497. * Function irlap_discovery_indication (log)
  498. *
  499. * Somebody is trying to discover us!
  500. *
  501. */
  502. void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery)
  503. {
  504. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  505. IRDA_ASSERT(self != NULL, return;);
  506. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  507. IRDA_ASSERT(discovery != NULL, return;);
  508. IRDA_ASSERT(self->notify.instance != NULL, return;);
  509. /* A device is very likely to connect immediately after it performs
  510. * a successful discovery. This means that in our case, we are much
  511. * more likely to receive a connection request over the medium.
  512. * So, we backoff to avoid collisions.
  513. * IrLAP spec 6.13.4 suggest 100ms...
  514. * Note : this little trick actually make a *BIG* difference. If I set
  515. * my Linux box with discovery enabled and one Ultra frame sent every
  516. * second, my Palm has no trouble connecting to it every time !
  517. * Jean II */
  518. irda_device_set_media_busy(self->netdev, SMALL);
  519. irlmp_link_discovery_indication(self->notify.instance, discovery);
  520. }
  521. /*
  522. * Function irlap_status_indication (quality_of_link)
  523. */
  524. void irlap_status_indication(struct irlap_cb *self, int quality_of_link)
  525. {
  526. switch (quality_of_link) {
  527. case STATUS_NO_ACTIVITY:
  528. IRDA_MESSAGE("IrLAP, no activity on link!\n");
  529. break;
  530. case STATUS_NOISY:
  531. IRDA_MESSAGE("IrLAP, noisy link!\n");
  532. break;
  533. default:
  534. break;
  535. }
  536. irlmp_status_indication(self->notify.instance,
  537. quality_of_link, LOCK_NO_CHANGE);
  538. }
  539. /*
  540. * Function irlap_reset_indication (void)
  541. */
  542. void irlap_reset_indication(struct irlap_cb *self)
  543. {
  544. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  545. IRDA_ASSERT(self != NULL, return;);
  546. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  547. if (self->state == LAP_RESET_WAIT)
  548. irlap_do_event(self, RESET_REQUEST, NULL, NULL);
  549. else
  550. irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
  551. }
  552. /*
  553. * Function irlap_reset_confirm (void)
  554. */
  555. void irlap_reset_confirm(void)
  556. {
  557. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  558. }
  559. /*
  560. * Function irlap_generate_rand_time_slot (S, s)
  561. *
  562. * Generate a random time slot between s and S-1 where
  563. * S = Number of slots (0 -> S-1)
  564. * s = Current slot
  565. */
  566. int irlap_generate_rand_time_slot(int S, int s)
  567. {
  568. static int rand;
  569. int slot;
  570. IRDA_ASSERT((S - s) > 0, return 0;);
  571. rand += jiffies;
  572. rand ^= (rand << 12);
  573. rand ^= (rand >> 20);
  574. slot = s + rand % (S-s);
  575. IRDA_ASSERT((slot >= s) || (slot < S), return 0;);
  576. return slot;
  577. }
  578. /*
  579. * Function irlap_update_nr_received (nr)
  580. *
  581. * Remove all acknowledged frames in current window queue. This code is
  582. * not intuitive and you should not try to change it. If you think it
  583. * contains bugs, please mail a patch to the author instead.
  584. */
  585. void irlap_update_nr_received(struct irlap_cb *self, int nr)
  586. {
  587. struct sk_buff *skb = NULL;
  588. int count = 0;
  589. /*
  590. * Remove all the ack-ed frames from the window queue.
  591. */
  592. /*
  593. * Optimize for the common case. It is most likely that the receiver
  594. * will acknowledge all the frames we have sent! So in that case we
  595. * delete all frames stored in window.
  596. */
  597. if (nr == self->vs) {
  598. while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
  599. dev_kfree_skb(skb);
  600. }
  601. /* The last acked frame is the next to send minus one */
  602. self->va = nr - 1;
  603. } else {
  604. /* Remove all acknowledged frames in current window */
  605. while ((skb_peek(&self->wx_list) != NULL) &&
  606. (((self->va+1) % 8) != nr))
  607. {
  608. skb = skb_dequeue(&self->wx_list);
  609. dev_kfree_skb(skb);
  610. self->va = (self->va + 1) % 8;
  611. count++;
  612. }
  613. }
  614. /* Advance window */
  615. self->window = self->window_size - skb_queue_len(&self->wx_list);
  616. }
  617. /*
  618. * Function irlap_validate_ns_received (ns)
  619. *
  620. * Validate the next to send (ns) field from received frame.
  621. */
  622. int irlap_validate_ns_received(struct irlap_cb *self, int ns)
  623. {
  624. /* ns as expected? */
  625. if (ns == self->vr)
  626. return NS_EXPECTED;
  627. /*
  628. * Stations are allowed to treat invalid NS as unexpected NS
  629. * IrLAP, Recv ... with-invalid-Ns. p. 84
  630. */
  631. return NS_UNEXPECTED;
  632. /* return NR_INVALID; */
  633. }
  634. /*
  635. * Function irlap_validate_nr_received (nr)
  636. *
  637. * Validate the next to receive (nr) field from received frame.
  638. *
  639. */
  640. int irlap_validate_nr_received(struct irlap_cb *self, int nr)
  641. {
  642. /* nr as expected? */
  643. if (nr == self->vs) {
  644. IRDA_DEBUG(4, "%s(), expected!\n", __FUNCTION__);
  645. return NR_EXPECTED;
  646. }
  647. /*
  648. * unexpected nr? (but within current window), first we check if the
  649. * ns numbers of the frames in the current window wrap.
  650. */
  651. if (self->va < self->vs) {
  652. if ((nr >= self->va) && (nr <= self->vs))
  653. return NR_UNEXPECTED;
  654. } else {
  655. if ((nr >= self->va) || (nr <= self->vs))
  656. return NR_UNEXPECTED;
  657. }
  658. /* Invalid nr! */
  659. return NR_INVALID;
  660. }
  661. /*
  662. * Function irlap_initiate_connection_state ()
  663. *
  664. * Initialize the connection state parameters
  665. *
  666. */
  667. void irlap_initiate_connection_state(struct irlap_cb *self)
  668. {
  669. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  670. IRDA_ASSERT(self != NULL, return;);
  671. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  672. /* Next to send and next to receive */
  673. self->vs = self->vr = 0;
  674. /* Last frame which got acked (0 - 1) % 8 */
  675. self->va = 7;
  676. self->window = 1;
  677. self->remote_busy = FALSE;
  678. self->retry_count = 0;
  679. }
  680. /*
  681. * Function irlap_wait_min_turn_around (self, qos)
  682. *
  683. * Wait negotiated minimum turn around time, this function actually sets
  684. * the number of BOS's that must be sent before the next transmitted
  685. * frame in order to delay for the specified amount of time. This is
  686. * done to avoid using timers, and the forbidden udelay!
  687. */
  688. void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos)
  689. {
  690. __u32 min_turn_time;
  691. __u32 speed;
  692. /* Get QoS values. */
  693. speed = qos->baud_rate.value;
  694. min_turn_time = qos->min_turn_time.value;
  695. /* No need to calculate XBOFs for speeds over 115200 bps */
  696. if (speed > 115200) {
  697. self->mtt_required = min_turn_time;
  698. return;
  699. }
  700. /*
  701. * Send additional BOF's for the next frame for the requested
  702. * min turn time, so now we must calculate how many chars (XBOF's) we
  703. * must send for the requested time period (min turn time)
  704. */
  705. self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
  706. }
  707. /*
  708. * Function irlap_flush_all_queues (void)
  709. *
  710. * Flush all queues
  711. *
  712. */
  713. void irlap_flush_all_queues(struct irlap_cb *self)
  714. {
  715. struct sk_buff* skb;
  716. IRDA_ASSERT(self != NULL, return;);
  717. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  718. /* Free transmission queue */
  719. while ((skb = skb_dequeue(&self->txq)) != NULL)
  720. dev_kfree_skb(skb);
  721. while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
  722. dev_kfree_skb(skb);
  723. /* Free sliding window buffered packets */
  724. while ((skb = skb_dequeue(&self->wx_list)) != NULL)
  725. dev_kfree_skb(skb);
  726. }
  727. /*
  728. * Function irlap_setspeed (self, speed)
  729. *
  730. * Change the speed of the IrDA port
  731. *
  732. */
  733. static void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
  734. {
  735. struct sk_buff *skb;
  736. IRDA_DEBUG(0, "%s(), setting speed to %d\n", __FUNCTION__, speed);
  737. IRDA_ASSERT(self != NULL, return;);
  738. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  739. self->speed = speed;
  740. /* Change speed now, or just piggyback speed on frames */
  741. if (now) {
  742. /* Send down empty frame to trigger speed change */
  743. skb = dev_alloc_skb(0);
  744. if (skb)
  745. irlap_queue_xmit(self, skb);
  746. }
  747. }
  748. /*
  749. * Function irlap_init_qos_capabilities (self, qos)
  750. *
  751. * Initialize QoS for this IrLAP session, What we do is to compute the
  752. * intersection of the QoS capabilities for the user, driver and for
  753. * IrLAP itself. Normally, IrLAP will not specify any values, but it can
  754. * be used to restrict certain values.
  755. */
  756. static void irlap_init_qos_capabilities(struct irlap_cb *self,
  757. struct qos_info *qos_user)
  758. {
  759. IRDA_ASSERT(self != NULL, return;);
  760. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  761. IRDA_ASSERT(self->netdev != NULL, return;);
  762. /* Start out with the maximum QoS support possible */
  763. irda_init_max_qos_capabilies(&self->qos_rx);
  764. /* Apply drivers QoS capabilities */
  765. irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
  766. /*
  767. * Check for user supplied QoS parameters. The service user is only
  768. * allowed to supply these values. We check each parameter since the
  769. * user may not have set all of them.
  770. */
  771. if (qos_user) {
  772. IRDA_DEBUG(1, "%s(), Found user specified QoS!\n", __FUNCTION__);
  773. if (qos_user->baud_rate.bits)
  774. self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
  775. if (qos_user->max_turn_time.bits)
  776. self->qos_rx.max_turn_time.bits &= qos_user->max_turn_time.bits;
  777. if (qos_user->data_size.bits)
  778. self->qos_rx.data_size.bits &= qos_user->data_size.bits;
  779. if (qos_user->link_disc_time.bits)
  780. self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
  781. }
  782. /* Use 500ms in IrLAP for now */
  783. self->qos_rx.max_turn_time.bits &= 0x01;
  784. /* Set data size */
  785. /*self->qos_rx.data_size.bits &= 0x03;*/
  786. irda_qos_bits_to_value(&self->qos_rx);
  787. }
  788. /*
  789. * Function irlap_apply_default_connection_parameters (void, now)
  790. *
  791. * Use the default connection and transmission parameters
  792. */
  793. void irlap_apply_default_connection_parameters(struct irlap_cb *self)
  794. {
  795. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  796. IRDA_ASSERT(self != NULL, return;);
  797. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  798. /* xbofs : Default value in NDM */
  799. self->next_bofs = 12;
  800. self->bofs_count = 12;
  801. /* NDM Speed is 9600 */
  802. irlap_change_speed(self, 9600, TRUE);
  803. /* Set mbusy when going to NDM state */
  804. irda_device_set_media_busy(self->netdev, TRUE);
  805. /*
  806. * Generate random connection address for this session, which must
  807. * be 7 bits wide and different from 0x00 and 0xfe
  808. */
  809. while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
  810. get_random_bytes(&self->caddr, sizeof(self->caddr));
  811. self->caddr &= 0xfe;
  812. }
  813. /* Use default values until connection has been negitiated */
  814. self->slot_timeout = sysctl_slot_timeout;
  815. self->final_timeout = FINAL_TIMEOUT;
  816. self->poll_timeout = POLL_TIMEOUT;
  817. self->wd_timeout = WD_TIMEOUT;
  818. /* Set some default values */
  819. self->qos_tx.baud_rate.value = 9600;
  820. self->qos_rx.baud_rate.value = 9600;
  821. self->qos_tx.max_turn_time.value = 0;
  822. self->qos_rx.max_turn_time.value = 0;
  823. self->qos_tx.min_turn_time.value = 0;
  824. self->qos_rx.min_turn_time.value = 0;
  825. self->qos_tx.data_size.value = 64;
  826. self->qos_rx.data_size.value = 64;
  827. self->qos_tx.window_size.value = 1;
  828. self->qos_rx.window_size.value = 1;
  829. self->qos_tx.additional_bofs.value = 12;
  830. self->qos_rx.additional_bofs.value = 12;
  831. self->qos_tx.link_disc_time.value = 0;
  832. self->qos_rx.link_disc_time.value = 0;
  833. irlap_flush_all_queues(self);
  834. self->disconnect_pending = FALSE;
  835. self->connect_pending = FALSE;
  836. }
  837. /*
  838. * Function irlap_apply_connection_parameters (qos, now)
  839. *
  840. * Initialize IrLAP with the negotiated QoS values
  841. *
  842. * If 'now' is false, the speed and xbofs will be changed after the next
  843. * frame is sent.
  844. * If 'now' is true, the speed and xbofs is changed immediately
  845. */
  846. void irlap_apply_connection_parameters(struct irlap_cb *self, int now)
  847. {
  848. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  849. IRDA_ASSERT(self != NULL, return;);
  850. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  851. /* Set the negotiated xbofs value */
  852. self->next_bofs = self->qos_tx.additional_bofs.value;
  853. if (now)
  854. self->bofs_count = self->next_bofs;
  855. /* Set the negotiated link speed (may need the new xbofs value) */
  856. irlap_change_speed(self, self->qos_tx.baud_rate.value, now);
  857. self->window_size = self->qos_tx.window_size.value;
  858. self->window = self->qos_tx.window_size.value;
  859. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  860. /*
  861. * Calculate how many bytes it is possible to transmit before the
  862. * link must be turned around
  863. */
  864. self->line_capacity =
  865. irlap_max_line_capacity(self->qos_tx.baud_rate.value,
  866. self->qos_tx.max_turn_time.value);
  867. self->bytes_left = self->line_capacity;
  868. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  869. /*
  870. * Initialize timeout values, some of the rules are listed on
  871. * page 92 in IrLAP.
  872. */
  873. IRDA_ASSERT(self->qos_tx.max_turn_time.value != 0, return;);
  874. IRDA_ASSERT(self->qos_rx.max_turn_time.value != 0, return;);
  875. /* The poll timeout applies only to the primary station.
  876. * It defines the maximum time the primary stay in XMIT mode
  877. * before timeout and turning the link around (sending a RR).
  878. * Or, this is how much we can keep the pf bit in primary mode.
  879. * Therefore, it must be lower or equal than our *OWN* max turn around.
  880. * Jean II */
  881. self->poll_timeout = self->qos_tx.max_turn_time.value * HZ / 1000;
  882. /* The Final timeout applies only to the primary station.
  883. * It defines the maximum time the primary wait (mostly in RECV mode)
  884. * for an answer from the secondary station before polling it again.
  885. * Therefore, it must be greater or equal than our *PARTNER*
  886. * max turn around time - Jean II */
  887. self->final_timeout = self->qos_rx.max_turn_time.value * HZ / 1000;
  888. /* The Watchdog Bit timeout applies only to the secondary station.
  889. * It defines the maximum time the secondary wait (mostly in RECV mode)
  890. * for poll from the primary station before getting annoyed.
  891. * Therefore, it must be greater or equal than our *PARTNER*
  892. * max turn around time - Jean II */
  893. self->wd_timeout = self->final_timeout * 2;
  894. /*
  895. * N1 and N2 are maximum retry count for *both* the final timer
  896. * and the wd timer (with a factor 2) as defined above.
  897. * After N1 retry of a timer, we give a warning to the user.
  898. * After N2 retry, we consider the link dead and disconnect it.
  899. * Jean II
  900. */
  901. /*
  902. * Set N1 to 0 if Link Disconnect/Threshold Time = 3 and set it to
  903. * 3 seconds otherwise. See page 71 in IrLAP for more details.
  904. * Actually, it's not always 3 seconds, as we allow to set
  905. * it via sysctl... Max maxtt is 500ms, and N1 need to be multiple
  906. * of 2, so 1 second is minimum we can allow. - Jean II
  907. */
  908. if (self->qos_tx.link_disc_time.value == sysctl_warn_noreply_time)
  909. /*
  910. * If we set N1 to 0, it will trigger immediately, which is
  911. * not what we want. What we really want is to disable it,
  912. * Jean II
  913. */
  914. self->N1 = -2; /* Disable - Need to be multiple of 2*/
  915. else
  916. self->N1 = sysctl_warn_noreply_time * 1000 /
  917. self->qos_rx.max_turn_time.value;
  918. IRDA_DEBUG(4, "Setting N1 = %d\n", self->N1);
  919. /* Set N2 to match our own disconnect time */
  920. self->N2 = self->qos_tx.link_disc_time.value * 1000 /
  921. self->qos_rx.max_turn_time.value;
  922. IRDA_DEBUG(4, "Setting N2 = %d\n", self->N2);
  923. }
  924. #ifdef CONFIG_PROC_FS
  925. struct irlap_iter_state {
  926. int id;
  927. };
  928. static void *irlap_seq_start(struct seq_file *seq, loff_t *pos)
  929. {
  930. struct irlap_iter_state *iter = seq->private;
  931. struct irlap_cb *self;
  932. /* Protect our access to the tsap list */
  933. spin_lock_irq(&irlap->hb_spinlock);
  934. iter->id = 0;
  935. for (self = (struct irlap_cb *) hashbin_get_first(irlap);
  936. self; self = (struct irlap_cb *) hashbin_get_next(irlap)) {
  937. if (iter->id == *pos)
  938. break;
  939. ++iter->id;
  940. }
  941. return self;
  942. }
  943. static void *irlap_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  944. {
  945. struct irlap_iter_state *iter = seq->private;
  946. ++*pos;
  947. ++iter->id;
  948. return (void *) hashbin_get_next(irlap);
  949. }
  950. static void irlap_seq_stop(struct seq_file *seq, void *v)
  951. {
  952. spin_unlock_irq(&irlap->hb_spinlock);
  953. }
  954. static int irlap_seq_show(struct seq_file *seq, void *v)
  955. {
  956. const struct irlap_iter_state *iter = seq->private;
  957. const struct irlap_cb *self = v;
  958. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EINVAL;);
  959. seq_printf(seq, "irlap%d ", iter->id);
  960. seq_printf(seq, "state: %s\n",
  961. irlap_state[self->state]);
  962. seq_printf(seq, " device name: %s, ",
  963. (self->netdev) ? self->netdev->name : "bug");
  964. seq_printf(seq, "hardware name: %s\n", self->hw_name);
  965. seq_printf(seq, " caddr: %#02x, ", self->caddr);
  966. seq_printf(seq, "saddr: %#08x, ", self->saddr);
  967. seq_printf(seq, "daddr: %#08x\n", self->daddr);
  968. seq_printf(seq, " win size: %d, ",
  969. self->window_size);
  970. seq_printf(seq, "win: %d, ", self->window);
  971. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  972. seq_printf(seq, "line capacity: %d, ",
  973. self->line_capacity);
  974. seq_printf(seq, "bytes left: %d\n", self->bytes_left);
  975. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  976. seq_printf(seq, " tx queue len: %d ",
  977. skb_queue_len(&self->txq));
  978. seq_printf(seq, "win queue len: %d ",
  979. skb_queue_len(&self->wx_list));
  980. seq_printf(seq, "rbusy: %s", self->remote_busy ?
  981. "TRUE" : "FALSE");
  982. seq_printf(seq, " mbusy: %s\n", self->media_busy ?
  983. "TRUE" : "FALSE");
  984. seq_printf(seq, " retrans: %d ", self->retry_count);
  985. seq_printf(seq, "vs: %d ", self->vs);
  986. seq_printf(seq, "vr: %d ", self->vr);
  987. seq_printf(seq, "va: %d\n", self->va);
  988. seq_printf(seq, " qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
  989. seq_printf(seq, " tx\t%d\t",
  990. self->qos_tx.baud_rate.value);
  991. seq_printf(seq, "%d\t",
  992. self->qos_tx.max_turn_time.value);
  993. seq_printf(seq, "%d\t",
  994. self->qos_tx.data_size.value);
  995. seq_printf(seq, "%d\t",
  996. self->qos_tx.window_size.value);
  997. seq_printf(seq, "%d\t",
  998. self->qos_tx.additional_bofs.value);
  999. seq_printf(seq, "%d\t",
  1000. self->qos_tx.min_turn_time.value);
  1001. seq_printf(seq, "%d\t",
  1002. self->qos_tx.link_disc_time.value);
  1003. seq_printf(seq, "\n");
  1004. seq_printf(seq, " rx\t%d\t",
  1005. self->qos_rx.baud_rate.value);
  1006. seq_printf(seq, "%d\t",
  1007. self->qos_rx.max_turn_time.value);
  1008. seq_printf(seq, "%d\t",
  1009. self->qos_rx.data_size.value);
  1010. seq_printf(seq, "%d\t",
  1011. self->qos_rx.window_size.value);
  1012. seq_printf(seq, "%d\t",
  1013. self->qos_rx.additional_bofs.value);
  1014. seq_printf(seq, "%d\t",
  1015. self->qos_rx.min_turn_time.value);
  1016. seq_printf(seq, "%d\n",
  1017. self->qos_rx.link_disc_time.value);
  1018. return 0;
  1019. }
  1020. static struct seq_operations irlap_seq_ops = {
  1021. .start = irlap_seq_start,
  1022. .next = irlap_seq_next,
  1023. .stop = irlap_seq_stop,
  1024. .show = irlap_seq_show,
  1025. };
  1026. static int irlap_seq_open(struct inode *inode, struct file *file)
  1027. {
  1028. struct seq_file *seq;
  1029. int rc = -ENOMEM;
  1030. struct irlap_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  1031. if (!s)
  1032. goto out;
  1033. if (irlap == NULL) {
  1034. rc = -EINVAL;
  1035. goto out_kfree;
  1036. }
  1037. rc = seq_open(file, &irlap_seq_ops);
  1038. if (rc)
  1039. goto out_kfree;
  1040. seq = file->private_data;
  1041. seq->private = s;
  1042. memset(s, 0, sizeof(*s));
  1043. out:
  1044. return rc;
  1045. out_kfree:
  1046. kfree(s);
  1047. goto out;
  1048. }
  1049. struct file_operations irlap_seq_fops = {
  1050. .owner = THIS_MODULE,
  1051. .open = irlap_seq_open,
  1052. .read = seq_read,
  1053. .llseek = seq_lseek,
  1054. .release = seq_release_private,
  1055. };
  1056. #endif /* CONFIG_PROC_FS */