irlap.c 34 KB

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