irnet_irda.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /*
  2. * IrNET protocol module : Synchronous PPP over an IrDA socket.
  3. *
  4. * Jean II - HPL `00 - <jt@hpl.hp.com>
  5. *
  6. * This file implement the IRDA interface of IrNET.
  7. * Basically, we sit on top of IrTTP. We set up IrTTP, IrIAS properly,
  8. * and exchange frames with IrTTP.
  9. */
  10. #include "irnet_irda.h" /* Private header */
  11. #include <linux/seq_file.h>
  12. /*
  13. * PPP disconnect work: we need to make sure we're in
  14. * process context when calling ppp_unregister_channel().
  15. */
  16. static void irnet_ppp_disconnect(struct work_struct *work)
  17. {
  18. irnet_socket * self =
  19. container_of(work, irnet_socket, disconnect_work);
  20. if (self == NULL)
  21. return;
  22. /*
  23. * If we were connected, cleanup & close the PPP
  24. * channel, which will kill pppd (hangup) and the rest.
  25. */
  26. if (self->ppp_open && !self->ttp_open && !self->ttp_connect) {
  27. ppp_unregister_channel(&self->chan);
  28. self->ppp_open = 0;
  29. }
  30. }
  31. /************************* CONTROL CHANNEL *************************/
  32. /*
  33. * When ppp is not active, /dev/irnet act as a control channel.
  34. * Writing allow to set up the IrDA destination of the IrNET channel,
  35. * and any application may be read events happening on IrNET...
  36. */
  37. /*------------------------------------------------------------------*/
  38. /*
  39. * Post an event to the control channel...
  40. * Put the event in the log, and then wait all process blocked on read
  41. * so they can read the log...
  42. */
  43. static void
  44. irnet_post_event(irnet_socket * ap,
  45. irnet_event event,
  46. __u32 saddr,
  47. __u32 daddr,
  48. char * name,
  49. __u16 hints)
  50. {
  51. int index; /* In the log */
  52. DENTER(CTRL_TRACE, "(ap=0x%p, event=%d, daddr=%08x, name=``%s'')\n",
  53. ap, event, daddr, name);
  54. /* Protect this section via spinlock.
  55. * Note : as we are the only event producer, we only need to exclude
  56. * ourself when touching the log, which is nice and easy.
  57. */
  58. spin_lock_bh(&irnet_events.spinlock);
  59. /* Copy the event in the log */
  60. index = irnet_events.index;
  61. irnet_events.log[index].event = event;
  62. irnet_events.log[index].daddr = daddr;
  63. irnet_events.log[index].saddr = saddr;
  64. /* Try to copy IrDA nickname */
  65. if(name)
  66. strcpy(irnet_events.log[index].name, name);
  67. else
  68. irnet_events.log[index].name[0] = '\0';
  69. /* Copy hints */
  70. irnet_events.log[index].hints.word = hints;
  71. /* Try to get ppp unit number */
  72. if((ap != (irnet_socket *) NULL) && (ap->ppp_open))
  73. irnet_events.log[index].unit = ppp_unit_number(&ap->chan);
  74. else
  75. irnet_events.log[index].unit = -1;
  76. /* Increment the index
  77. * Note that we increment the index only after the event is written,
  78. * to make sure that the readers don't get garbage... */
  79. irnet_events.index = (index + 1) % IRNET_MAX_EVENTS;
  80. DEBUG(CTRL_INFO, "New event index is %d\n", irnet_events.index);
  81. /* Spin lock end */
  82. spin_unlock_bh(&irnet_events.spinlock);
  83. /* Now : wake up everybody waiting for events... */
  84. wake_up_interruptible_all(&irnet_events.rwait);
  85. DEXIT(CTRL_TRACE, "\n");
  86. }
  87. /************************* IRDA SUBROUTINES *************************/
  88. /*
  89. * These are a bunch of subroutines called from other functions
  90. * down there, mostly common code or to improve readability...
  91. *
  92. * Note : we duplicate quite heavily some routines of af_irda.c,
  93. * because our input structure (self) is quite different
  94. * (struct irnet instead of struct irda_sock), which make sharing
  95. * the same code impossible (at least, without templates).
  96. */
  97. /*------------------------------------------------------------------*/
  98. /*
  99. * Function irda_open_tsap (self)
  100. *
  101. * Open local Transport Service Access Point (TSAP)
  102. *
  103. * Create a IrTTP instance for us and set all the IrTTP callbacks.
  104. */
  105. static inline int
  106. irnet_open_tsap(irnet_socket * self)
  107. {
  108. notify_t notify; /* Callback structure */
  109. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  110. DABORT(self->tsap != NULL, -EBUSY, IRDA_SR_ERROR, "Already busy !\n");
  111. /* Initialize IrTTP callbacks to be used by the IrDA stack */
  112. irda_notify_init(&notify);
  113. notify.connect_confirm = irnet_connect_confirm;
  114. notify.connect_indication = irnet_connect_indication;
  115. notify.disconnect_indication = irnet_disconnect_indication;
  116. notify.data_indication = irnet_data_indication;
  117. /*notify.udata_indication = NULL;*/
  118. notify.flow_indication = irnet_flow_indication;
  119. notify.status_indication = irnet_status_indication;
  120. notify.instance = self;
  121. strlcpy(notify.name, IRNET_NOTIFY_NAME, sizeof(notify.name));
  122. /* Open an IrTTP instance */
  123. self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
  124. &notify);
  125. DABORT(self->tsap == NULL, -ENOMEM,
  126. IRDA_SR_ERROR, "Unable to allocate TSAP !\n");
  127. /* Remember which TSAP selector we actually got */
  128. self->stsap_sel = self->tsap->stsap_sel;
  129. DEXIT(IRDA_SR_TRACE, " - tsap=0x%p, sel=0x%X\n",
  130. self->tsap, self->stsap_sel);
  131. return 0;
  132. }
  133. /*------------------------------------------------------------------*/
  134. /*
  135. * Function irnet_ias_to_tsap (self, result, value)
  136. *
  137. * Examine an IAS object and extract TSAP
  138. *
  139. * We do an IAP query to find the TSAP associated with the IrNET service.
  140. * When IrIAP pass us the result of the query, this function look at
  141. * the return values to check for failures and extract the TSAP if
  142. * possible.
  143. * Also deallocate value
  144. * The failure is in self->errno
  145. * Return TSAP or -1
  146. */
  147. static inline __u8
  148. irnet_ias_to_tsap(irnet_socket * self,
  149. int result,
  150. struct ias_value * value)
  151. {
  152. __u8 dtsap_sel = 0; /* TSAP we are looking for */
  153. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  154. /* By default, no error */
  155. self->errno = 0;
  156. /* Check if request succeeded */
  157. switch(result)
  158. {
  159. /* Standard errors : service not available */
  160. case IAS_CLASS_UNKNOWN:
  161. case IAS_ATTRIB_UNKNOWN:
  162. DEBUG(IRDA_SR_INFO, "IAS object doesn't exist ! (%d)\n", result);
  163. self->errno = -EADDRNOTAVAIL;
  164. break;
  165. /* Other errors, most likely IrDA stack failure */
  166. default :
  167. DEBUG(IRDA_SR_INFO, "IAS query failed ! (%d)\n", result);
  168. self->errno = -EHOSTUNREACH;
  169. break;
  170. /* Success : we got what we wanted */
  171. case IAS_SUCCESS:
  172. break;
  173. }
  174. /* Check what was returned to us */
  175. if(value != NULL)
  176. {
  177. /* What type of argument have we got ? */
  178. switch(value->type)
  179. {
  180. case IAS_INTEGER:
  181. DEBUG(IRDA_SR_INFO, "result=%d\n", value->t.integer);
  182. if(value->t.integer != -1)
  183. /* Get the remote TSAP selector */
  184. dtsap_sel = value->t.integer;
  185. else
  186. self->errno = -EADDRNOTAVAIL;
  187. break;
  188. default:
  189. self->errno = -EADDRNOTAVAIL;
  190. DERROR(IRDA_SR_ERROR, "bad type ! (0x%X)\n", value->type);
  191. break;
  192. }
  193. /* Cleanup */
  194. irias_delete_value(value);
  195. }
  196. else /* value == NULL */
  197. {
  198. /* Nothing returned to us - usually result != SUCCESS */
  199. if(!(self->errno))
  200. {
  201. DERROR(IRDA_SR_ERROR,
  202. "IrDA bug : result == SUCCESS && value == NULL\n");
  203. self->errno = -EHOSTUNREACH;
  204. }
  205. }
  206. DEXIT(IRDA_SR_TRACE, "\n");
  207. /* Return the TSAP */
  208. return(dtsap_sel);
  209. }
  210. /*------------------------------------------------------------------*/
  211. /*
  212. * Function irnet_find_lsap_sel (self)
  213. *
  214. * Try to lookup LSAP selector in remote LM-IAS
  215. *
  216. * Basically, we start a IAP query, and then go to sleep. When the query
  217. * return, irnet_getvalue_confirm will wake us up, and we can examine the
  218. * result of the query...
  219. * Note that in some case, the query fail even before we go to sleep,
  220. * creating some races...
  221. */
  222. static inline int
  223. irnet_find_lsap_sel(irnet_socket * self)
  224. {
  225. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  226. /* This should not happen */
  227. DABORT(self->iriap, -EBUSY, IRDA_SR_ERROR, "busy with a previous query.\n");
  228. /* Create an IAP instance, will be closed in irnet_getvalue_confirm() */
  229. self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  230. irnet_getvalue_confirm);
  231. /* Treat unexpected signals as disconnect */
  232. self->errno = -EHOSTUNREACH;
  233. /* Query remote LM-IAS */
  234. iriap_getvaluebyclass_request(self->iriap, self->rsaddr, self->daddr,
  235. IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
  236. /* The above request is non-blocking.
  237. * After a while, IrDA will call us back in irnet_getvalue_confirm()
  238. * We will then call irnet_ias_to_tsap() and finish the
  239. * connection procedure */
  240. DEXIT(IRDA_SR_TRACE, "\n");
  241. return 0;
  242. }
  243. /*------------------------------------------------------------------*/
  244. /*
  245. * Function irnet_connect_tsap (self)
  246. *
  247. * Initialise the TTP socket and initiate TTP connection
  248. *
  249. */
  250. static inline int
  251. irnet_connect_tsap(irnet_socket * self)
  252. {
  253. int err;
  254. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  255. /* Open a local TSAP (an IrTTP instance) */
  256. err = irnet_open_tsap(self);
  257. if(err != 0)
  258. {
  259. clear_bit(0, &self->ttp_connect);
  260. DERROR(IRDA_SR_ERROR, "connect aborted!\n");
  261. return(err);
  262. }
  263. /* Connect to remote device */
  264. err = irttp_connect_request(self->tsap, self->dtsap_sel,
  265. self->rsaddr, self->daddr, NULL,
  266. self->max_sdu_size_rx, NULL);
  267. if(err != 0)
  268. {
  269. clear_bit(0, &self->ttp_connect);
  270. DERROR(IRDA_SR_ERROR, "connect aborted!\n");
  271. return(err);
  272. }
  273. /* The above call is non-blocking.
  274. * After a while, the IrDA stack will either call us back in
  275. * irnet_connect_confirm() or irnet_disconnect_indication()
  276. * See you there ;-) */
  277. DEXIT(IRDA_SR_TRACE, "\n");
  278. return(err);
  279. }
  280. /*------------------------------------------------------------------*/
  281. /*
  282. * Function irnet_discover_next_daddr (self)
  283. *
  284. * Query the IrNET TSAP of the next device in the log.
  285. *
  286. * Used in the TSAP discovery procedure.
  287. */
  288. static inline int
  289. irnet_discover_next_daddr(irnet_socket * self)
  290. {
  291. /* Close the last instance of IrIAP, and open a new one.
  292. * We can't reuse the IrIAP instance in the IrIAP callback */
  293. if(self->iriap)
  294. {
  295. iriap_close(self->iriap);
  296. self->iriap = NULL;
  297. }
  298. /* Create a new IAP instance */
  299. self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  300. irnet_discovervalue_confirm);
  301. if(self->iriap == NULL)
  302. return -ENOMEM;
  303. /* Next discovery - before the call to avoid races */
  304. self->disco_index++;
  305. /* Check if we have one more address to try */
  306. if(self->disco_index < self->disco_number)
  307. {
  308. /* Query remote LM-IAS */
  309. iriap_getvaluebyclass_request(self->iriap,
  310. self->discoveries[self->disco_index].saddr,
  311. self->discoveries[self->disco_index].daddr,
  312. IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
  313. /* The above request is non-blocking.
  314. * After a while, IrDA will call us back in irnet_discovervalue_confirm()
  315. * We will then call irnet_ias_to_tsap() and come back here again... */
  316. return(0);
  317. }
  318. else
  319. return(1);
  320. }
  321. /*------------------------------------------------------------------*/
  322. /*
  323. * Function irnet_discover_daddr_and_lsap_sel (self)
  324. *
  325. * This try to find a device with the requested service.
  326. *
  327. * Initiate a TSAP discovery procedure.
  328. * It basically look into the discovery log. For each address in the list,
  329. * it queries the LM-IAS of the device to find if this device offer
  330. * the requested service.
  331. * If there is more than one node supporting the service, we complain
  332. * to the user (it should move devices around).
  333. * If we find one node which have the requested TSAP, we connect to it.
  334. *
  335. * This function just start the whole procedure. It request the discovery
  336. * log and submit the first IAS query.
  337. * The bulk of the job is handled in irnet_discovervalue_confirm()
  338. *
  339. * Note : this procedure fails if there is more than one device in range
  340. * on the same dongle, because IrLMP doesn't disconnect the LAP when the
  341. * last LSAP is closed. Moreover, we would need to wait the LAP
  342. * disconnection...
  343. */
  344. static inline int
  345. irnet_discover_daddr_and_lsap_sel(irnet_socket * self)
  346. {
  347. int ret;
  348. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  349. /* Ask lmp for the current discovery log */
  350. self->discoveries = irlmp_get_discoveries(&self->disco_number, self->mask,
  351. DISCOVERY_DEFAULT_SLOTS);
  352. /* Check if the we got some results */
  353. if(self->discoveries == NULL)
  354. {
  355. self->disco_number = -1;
  356. clear_bit(0, &self->ttp_connect);
  357. DRETURN(-ENETUNREACH, IRDA_SR_INFO, "No Cachelog...\n");
  358. }
  359. DEBUG(IRDA_SR_INFO, "Got the log (0x%p), size is %d\n",
  360. self->discoveries, self->disco_number);
  361. /* Start with the first discovery */
  362. self->disco_index = -1;
  363. self->daddr = DEV_ADDR_ANY;
  364. /* This will fail if the log is empty - this is non-blocking */
  365. ret = irnet_discover_next_daddr(self);
  366. if(ret)
  367. {
  368. /* Close IAP */
  369. if(self->iriap)
  370. iriap_close(self->iriap);
  371. self->iriap = NULL;
  372. /* Cleanup our copy of the discovery log */
  373. kfree(self->discoveries);
  374. self->discoveries = NULL;
  375. clear_bit(0, &self->ttp_connect);
  376. DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
  377. }
  378. /* Follow me in irnet_discovervalue_confirm() */
  379. DEXIT(IRDA_SR_TRACE, "\n");
  380. return(0);
  381. }
  382. /*------------------------------------------------------------------*/
  383. /*
  384. * Function irnet_dname_to_daddr (self)
  385. *
  386. * Convert an IrDA nickname to a valid IrDA address
  387. *
  388. * It basically look into the discovery log until there is a match.
  389. */
  390. static inline int
  391. irnet_dname_to_daddr(irnet_socket * self)
  392. {
  393. struct irda_device_info *discoveries; /* Copy of the discovery log */
  394. int number; /* Number of nodes in the log */
  395. int i;
  396. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  397. /* Ask lmp for the current discovery log */
  398. discoveries = irlmp_get_discoveries(&number, 0xffff,
  399. DISCOVERY_DEFAULT_SLOTS);
  400. /* Check if the we got some results */
  401. if(discoveries == NULL)
  402. DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
  403. /*
  404. * Now, check all discovered devices (if any), and connect
  405. * client only about the services that the client is
  406. * interested in...
  407. */
  408. for(i = 0; i < number; i++)
  409. {
  410. /* Does the name match ? */
  411. if(!strncmp(discoveries[i].info, self->rname, NICKNAME_MAX_LEN))
  412. {
  413. /* Yes !!! Get it.. */
  414. self->daddr = discoveries[i].daddr;
  415. DEBUG(IRDA_SR_INFO, "discovered device ``%s'' at address 0x%08x.\n",
  416. self->rname, self->daddr);
  417. kfree(discoveries);
  418. DEXIT(IRDA_SR_TRACE, "\n");
  419. return 0;
  420. }
  421. }
  422. /* No luck ! */
  423. DEBUG(IRDA_SR_INFO, "cannot discover device ``%s'' !!!\n", self->rname);
  424. kfree(discoveries);
  425. return(-EADDRNOTAVAIL);
  426. }
  427. /************************* SOCKET ROUTINES *************************/
  428. /*
  429. * This are the main operations on IrNET sockets, basically to create
  430. * and destroy IrNET sockets. These are called from the PPP part...
  431. */
  432. /*------------------------------------------------------------------*/
  433. /*
  434. * Create a IrNET instance : just initialise some parameters...
  435. */
  436. int
  437. irda_irnet_create(irnet_socket * self)
  438. {
  439. DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
  440. self->magic = IRNET_MAGIC; /* Paranoia */
  441. self->ttp_open = 0; /* Prevent higher layer from accessing IrTTP */
  442. self->ttp_connect = 0; /* Not connecting yet */
  443. self->rname[0] = '\0'; /* May be set via control channel */
  444. self->rdaddr = DEV_ADDR_ANY; /* May be set via control channel */
  445. self->rsaddr = DEV_ADDR_ANY; /* May be set via control channel */
  446. self->daddr = DEV_ADDR_ANY; /* Until we get connected */
  447. self->saddr = DEV_ADDR_ANY; /* Until we get connected */
  448. self->max_sdu_size_rx = TTP_SAR_UNBOUND;
  449. /* Register as a client with IrLMP */
  450. self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
  451. #ifdef DISCOVERY_NOMASK
  452. self->mask = 0xffff; /* For W2k compatibility */
  453. #else /* DISCOVERY_NOMASK */
  454. self->mask = irlmp_service_to_hint(S_LAN);
  455. #endif /* DISCOVERY_NOMASK */
  456. self->tx_flow = FLOW_START; /* Flow control from IrTTP */
  457. INIT_WORK(&self->disconnect_work, irnet_ppp_disconnect);
  458. DEXIT(IRDA_SOCK_TRACE, "\n");
  459. return(0);
  460. }
  461. /*------------------------------------------------------------------*/
  462. /*
  463. * Connect to the other side :
  464. * o convert device name to an address
  465. * o find the socket number (dlsap)
  466. * o Establish the connection
  467. *
  468. * Note : We no longer mimic af_irda. The IAS query for finding the TSAP
  469. * is done asynchronously, like the TTP connection. This allow us to
  470. * call this function from any context (not only process).
  471. * The downside is that following what's happening in there is tricky
  472. * because it involve various functions all over the place...
  473. */
  474. int
  475. irda_irnet_connect(irnet_socket * self)
  476. {
  477. int err;
  478. DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
  479. /* Check if we are already trying to connect.
  480. * Because irda_irnet_connect() can be called directly by pppd plus
  481. * packet retries in ppp_generic and connect may take time, plus we may
  482. * race with irnet_connect_indication(), we need to be careful there... */
  483. if(test_and_set_bit(0, &self->ttp_connect))
  484. DRETURN(-EBUSY, IRDA_SOCK_INFO, "Already connecting...\n");
  485. if((self->iriap != NULL) || (self->tsap != NULL))
  486. DERROR(IRDA_SOCK_ERROR, "Socket not cleaned up...\n");
  487. /* Insert ourselves in the hashbin so that the IrNET server can find us.
  488. * Notes : 4th arg is string of 32 char max and must be null terminated
  489. * When 4th arg is used (string), 3rd arg isn't (int)
  490. * Can't re-insert (MUST remove first) so check for that... */
  491. if((irnet_server.running) && (self->q.q_next == NULL))
  492. {
  493. spin_lock_bh(&irnet_server.spinlock);
  494. hashbin_insert(irnet_server.list, (irda_queue_t *) self, 0, self->rname);
  495. spin_unlock_bh(&irnet_server.spinlock);
  496. DEBUG(IRDA_SOCK_INFO, "Inserted ``%s'' in hashbin...\n", self->rname);
  497. }
  498. /* If we don't have anything (no address, no name) */
  499. if((self->rdaddr == DEV_ADDR_ANY) && (self->rname[0] == '\0'))
  500. {
  501. /* Try to find a suitable address */
  502. if((err = irnet_discover_daddr_and_lsap_sel(self)) != 0)
  503. DRETURN(err, IRDA_SOCK_INFO, "auto-connect failed!\n");
  504. /* In most cases, the call above is non-blocking */
  505. }
  506. else
  507. {
  508. /* If we have only the name (no address), try to get an address */
  509. if(self->rdaddr == DEV_ADDR_ANY)
  510. {
  511. if((err = irnet_dname_to_daddr(self)) != 0)
  512. DRETURN(err, IRDA_SOCK_INFO, "name connect failed!\n");
  513. }
  514. else
  515. /* Use the requested destination address */
  516. self->daddr = self->rdaddr;
  517. /* Query remote LM-IAS to find LSAP selector */
  518. irnet_find_lsap_sel(self);
  519. /* The above call is non blocking */
  520. }
  521. /* At this point, we are waiting for the IrDA stack to call us back,
  522. * or we have already failed.
  523. * We will finish the connection procedure in irnet_connect_tsap().
  524. */
  525. DEXIT(IRDA_SOCK_TRACE, "\n");
  526. return(0);
  527. }
  528. /*------------------------------------------------------------------*/
  529. /*
  530. * Function irda_irnet_destroy(self)
  531. *
  532. * Destroy irnet instance
  533. *
  534. * Note : this need to be called from a process context.
  535. */
  536. void
  537. irda_irnet_destroy(irnet_socket * self)
  538. {
  539. DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
  540. if(self == NULL)
  541. return;
  542. /* Remove ourselves from hashbin (if we are queued in hashbin)
  543. * Note : `irnet_server.running' protect us from calls in hashbin_delete() */
  544. if((irnet_server.running) && (self->q.q_next != NULL))
  545. {
  546. struct irnet_socket * entry;
  547. DEBUG(IRDA_SOCK_INFO, "Removing from hash..\n");
  548. spin_lock_bh(&irnet_server.spinlock);
  549. entry = hashbin_remove_this(irnet_server.list, (irda_queue_t *) self);
  550. self->q.q_next = NULL;
  551. spin_unlock_bh(&irnet_server.spinlock);
  552. DASSERT(entry == self, , IRDA_SOCK_ERROR, "Can't remove from hash.\n");
  553. }
  554. /* If we were connected, post a message */
  555. if(test_bit(0, &self->ttp_open))
  556. {
  557. /* Note : as the disconnect comes from ppp_generic, the unit number
  558. * doesn't exist anymore when we post the event, so we need to pass
  559. * NULL as the first arg... */
  560. irnet_post_event(NULL, IRNET_DISCONNECT_TO,
  561. self->saddr, self->daddr, self->rname, 0);
  562. }
  563. /* Prevent various IrDA callbacks from messing up things
  564. * Need to be first */
  565. clear_bit(0, &self->ttp_connect);
  566. /* Prevent higher layer from accessing IrTTP */
  567. clear_bit(0, &self->ttp_open);
  568. /* Unregister with IrLMP */
  569. irlmp_unregister_client(self->ckey);
  570. /* Unregister with LM-IAS */
  571. if(self->iriap)
  572. {
  573. iriap_close(self->iriap);
  574. self->iriap = NULL;
  575. }
  576. /* Cleanup eventual discoveries from connection attempt or control channel */
  577. if(self->discoveries != NULL)
  578. {
  579. /* Cleanup our copy of the discovery log */
  580. kfree(self->discoveries);
  581. self->discoveries = NULL;
  582. }
  583. /* Close our IrTTP connection */
  584. if(self->tsap)
  585. {
  586. DEBUG(IRDA_SOCK_INFO, "Closing our TTP connection.\n");
  587. irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
  588. irttp_close_tsap(self->tsap);
  589. self->tsap = NULL;
  590. }
  591. self->stsap_sel = 0;
  592. DEXIT(IRDA_SOCK_TRACE, "\n");
  593. return;
  594. }
  595. /************************** SERVER SOCKET **************************/
  596. /*
  597. * The IrNET service is composed of one server socket and a variable
  598. * number of regular IrNET sockets. The server socket is supposed to
  599. * handle incoming connections and redirect them to one IrNET sockets.
  600. * It's a superset of the regular IrNET socket, but has a very distinct
  601. * behaviour...
  602. */
  603. /*------------------------------------------------------------------*/
  604. /*
  605. * Function irnet_daddr_to_dname (self)
  606. *
  607. * Convert an IrDA address to a IrDA nickname
  608. *
  609. * It basically look into the discovery log until there is a match.
  610. */
  611. static inline int
  612. irnet_daddr_to_dname(irnet_socket * self)
  613. {
  614. struct irda_device_info *discoveries; /* Copy of the discovery log */
  615. int number; /* Number of nodes in the log */
  616. int i;
  617. DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
  618. /* Ask lmp for the current discovery log */
  619. discoveries = irlmp_get_discoveries(&number, 0xffff,
  620. DISCOVERY_DEFAULT_SLOTS);
  621. /* Check if the we got some results */
  622. if (discoveries == NULL)
  623. DRETURN(-ENETUNREACH, IRDA_SERV_INFO, "Cachelog empty...\n");
  624. /* Now, check all discovered devices (if any) */
  625. for(i = 0; i < number; i++)
  626. {
  627. /* Does the name match ? */
  628. if(discoveries[i].daddr == self->daddr)
  629. {
  630. /* Yes !!! Get it.. */
  631. strlcpy(self->rname, discoveries[i].info, sizeof(self->rname));
  632. self->rname[sizeof(self->rname) - 1] = '\0';
  633. DEBUG(IRDA_SERV_INFO, "Device 0x%08x is in fact ``%s''.\n",
  634. self->daddr, self->rname);
  635. kfree(discoveries);
  636. DEXIT(IRDA_SERV_TRACE, "\n");
  637. return 0;
  638. }
  639. }
  640. /* No luck ! */
  641. DEXIT(IRDA_SERV_INFO, ": cannot discover device 0x%08x !!!\n", self->daddr);
  642. kfree(discoveries);
  643. return(-EADDRNOTAVAIL);
  644. }
  645. /*------------------------------------------------------------------*/
  646. /*
  647. * Function irda_find_socket (self)
  648. *
  649. * Find the correct IrNET socket
  650. *
  651. * Look into the list of IrNET sockets and finds one with the right
  652. * properties...
  653. */
  654. static inline irnet_socket *
  655. irnet_find_socket(irnet_socket * self)
  656. {
  657. irnet_socket * new = (irnet_socket *) NULL;
  658. int err;
  659. DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
  660. /* Get the addresses of the requester */
  661. self->daddr = irttp_get_daddr(self->tsap);
  662. self->saddr = irttp_get_saddr(self->tsap);
  663. /* Try to get the IrDA nickname of the requester */
  664. err = irnet_daddr_to_dname(self);
  665. /* Protect access to the instance list */
  666. spin_lock_bh(&irnet_server.spinlock);
  667. /* So now, try to get an socket having specifically
  668. * requested that nickname */
  669. if(err == 0)
  670. {
  671. new = (irnet_socket *) hashbin_find(irnet_server.list,
  672. 0, self->rname);
  673. if(new)
  674. DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches rname ``%s''.\n",
  675. new, new->rname);
  676. }
  677. /* If no name matches, try to find an socket by the destination address */
  678. /* It can be either the requested destination address (set via the
  679. * control channel), or the current destination address if the
  680. * socket is in the middle of a connection request */
  681. if(new == (irnet_socket *) NULL)
  682. {
  683. new = (irnet_socket *) hashbin_get_first(irnet_server.list);
  684. while(new !=(irnet_socket *) NULL)
  685. {
  686. /* Does it have the same address ? */
  687. if((new->rdaddr == self->daddr) || (new->daddr == self->daddr))
  688. {
  689. /* Yes !!! Get it.. */
  690. DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches daddr %#08x.\n",
  691. new, self->daddr);
  692. break;
  693. }
  694. new = (irnet_socket *) hashbin_get_next(irnet_server.list);
  695. }
  696. }
  697. /* If we don't have any socket, get the first unconnected socket */
  698. if(new == (irnet_socket *) NULL)
  699. {
  700. new = (irnet_socket *) hashbin_get_first(irnet_server.list);
  701. while(new !=(irnet_socket *) NULL)
  702. {
  703. /* Is it available ? */
  704. if(!(test_bit(0, &new->ttp_open)) && (new->rdaddr == DEV_ADDR_ANY) &&
  705. (new->rname[0] == '\0') && (new->ppp_open))
  706. {
  707. /* Yes !!! Get it.. */
  708. DEBUG(IRDA_SERV_INFO, "Socket 0x%p is free.\n",
  709. new);
  710. break;
  711. }
  712. new = (irnet_socket *) hashbin_get_next(irnet_server.list);
  713. }
  714. }
  715. /* Spin lock end */
  716. spin_unlock_bh(&irnet_server.spinlock);
  717. DEXIT(IRDA_SERV_TRACE, " - new = 0x%p\n", new);
  718. return new;
  719. }
  720. /*------------------------------------------------------------------*/
  721. /*
  722. * Function irda_connect_socket (self)
  723. *
  724. * Connect an incoming connection to the socket
  725. *
  726. */
  727. static inline int
  728. irnet_connect_socket(irnet_socket * server,
  729. irnet_socket * new,
  730. struct qos_info * qos,
  731. __u32 max_sdu_size,
  732. __u8 max_header_size)
  733. {
  734. DENTER(IRDA_SERV_TRACE, "(server=0x%p, new=0x%p)\n",
  735. server, new);
  736. /* Now attach up the new socket */
  737. new->tsap = irttp_dup(server->tsap, new);
  738. DABORT(new->tsap == NULL, -1, IRDA_SERV_ERROR, "dup failed!\n");
  739. /* Set up all the relevant parameters on the new socket */
  740. new->stsap_sel = new->tsap->stsap_sel;
  741. new->dtsap_sel = new->tsap->dtsap_sel;
  742. new->saddr = irttp_get_saddr(new->tsap);
  743. new->daddr = irttp_get_daddr(new->tsap);
  744. new->max_header_size = max_header_size;
  745. new->max_sdu_size_tx = max_sdu_size;
  746. new->max_data_size = max_sdu_size;
  747. #ifdef STREAM_COMPAT
  748. /* If we want to receive "stream sockets" */
  749. if(max_sdu_size == 0)
  750. new->max_data_size = irttp_get_max_seg_size(new->tsap);
  751. #endif /* STREAM_COMPAT */
  752. /* Clean up the original one to keep it in listen state */
  753. irttp_listen(server->tsap);
  754. /* Send a connection response on the new socket */
  755. irttp_connect_response(new->tsap, new->max_sdu_size_rx, NULL);
  756. /* Allow PPP to send its junk over the new socket... */
  757. set_bit(0, &new->ttp_open);
  758. /* Not connecting anymore, and clean up last possible remains
  759. * of connection attempts on the socket */
  760. clear_bit(0, &new->ttp_connect);
  761. if(new->iriap)
  762. {
  763. iriap_close(new->iriap);
  764. new->iriap = NULL;
  765. }
  766. if(new->discoveries != NULL)
  767. {
  768. kfree(new->discoveries);
  769. new->discoveries = NULL;
  770. }
  771. #ifdef CONNECT_INDIC_KICK
  772. /* As currently we don't block packets in ppp_irnet_send() while passive,
  773. * this is not really needed...
  774. * Also, not doing it give IrDA a chance to finish the setup properly
  775. * before being swamped with packets... */
  776. ppp_output_wakeup(&new->chan);
  777. #endif /* CONNECT_INDIC_KICK */
  778. /* Notify the control channel */
  779. irnet_post_event(new, IRNET_CONNECT_FROM,
  780. new->saddr, new->daddr, server->rname, 0);
  781. DEXIT(IRDA_SERV_TRACE, "\n");
  782. return 0;
  783. }
  784. /*------------------------------------------------------------------*/
  785. /*
  786. * Function irda_disconnect_server (self)
  787. *
  788. * Cleanup the server socket when the incoming connection abort
  789. *
  790. */
  791. static inline void
  792. irnet_disconnect_server(irnet_socket * self,
  793. struct sk_buff *skb)
  794. {
  795. DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
  796. /* Put the received packet in the black hole */
  797. kfree_skb(skb);
  798. #ifdef FAIL_SEND_DISCONNECT
  799. /* Tell the other party we don't want to be connected */
  800. /* Hum... Is it the right thing to do ? And do we need to send
  801. * a connect response before ? It looks ok without this... */
  802. irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
  803. #endif /* FAIL_SEND_DISCONNECT */
  804. /* Notify the control channel (see irnet_find_socket()) */
  805. irnet_post_event(NULL, IRNET_REQUEST_FROM,
  806. self->saddr, self->daddr, self->rname, 0);
  807. /* Clean up the server to keep it in listen state */
  808. irttp_listen(self->tsap);
  809. DEXIT(IRDA_SERV_TRACE, "\n");
  810. return;
  811. }
  812. /*------------------------------------------------------------------*/
  813. /*
  814. * Function irda_setup_server (self)
  815. *
  816. * Create a IrTTP server and set it up...
  817. *
  818. * Register the IrLAN hint bit, create a IrTTP instance for us,
  819. * set all the IrTTP callbacks and create an IrIAS entry...
  820. */
  821. static inline int
  822. irnet_setup_server(void)
  823. {
  824. __u16 hints;
  825. DENTER(IRDA_SERV_TRACE, "()\n");
  826. /* Initialise the regular socket part of the server */
  827. irda_irnet_create(&irnet_server.s);
  828. /* Open a local TSAP (an IrTTP instance) for the server */
  829. irnet_open_tsap(&irnet_server.s);
  830. /* PPP part setup */
  831. irnet_server.s.ppp_open = 0;
  832. irnet_server.s.chan.private = NULL;
  833. irnet_server.s.file = NULL;
  834. /* Get the hint bit corresponding to IrLAN */
  835. /* Note : we overload the IrLAN hint bit. As it is only a "hint", and as
  836. * we provide roughly the same functionality as IrLAN, this is ok.
  837. * In fact, the situation is similar as JetSend overloading the Obex hint
  838. */
  839. hints = irlmp_service_to_hint(S_LAN);
  840. #ifdef ADVERTISE_HINT
  841. /* Register with IrLMP as a service (advertise our hint bit) */
  842. irnet_server.skey = irlmp_register_service(hints);
  843. #endif /* ADVERTISE_HINT */
  844. /* Register with LM-IAS (so that people can connect to us) */
  845. irnet_server.ias_obj = irias_new_object(IRNET_SERVICE_NAME, jiffies);
  846. irias_add_integer_attrib(irnet_server.ias_obj, IRNET_IAS_VALUE,
  847. irnet_server.s.stsap_sel, IAS_KERNEL_ATTR);
  848. irias_insert_object(irnet_server.ias_obj);
  849. #ifdef DISCOVERY_EVENTS
  850. /* Tell IrLMP we want to be notified of newly discovered nodes */
  851. irlmp_update_client(irnet_server.s.ckey, hints,
  852. irnet_discovery_indication, irnet_expiry_indication,
  853. (void *) &irnet_server.s);
  854. #endif
  855. DEXIT(IRDA_SERV_TRACE, " - self=0x%p\n", &irnet_server.s);
  856. return 0;
  857. }
  858. /*------------------------------------------------------------------*/
  859. /*
  860. * Function irda_destroy_server (self)
  861. *
  862. * Destroy the IrTTP server...
  863. *
  864. * Reverse of the previous function...
  865. */
  866. static inline void
  867. irnet_destroy_server(void)
  868. {
  869. DENTER(IRDA_SERV_TRACE, "()\n");
  870. #ifdef ADVERTISE_HINT
  871. /* Unregister with IrLMP */
  872. irlmp_unregister_service(irnet_server.skey);
  873. #endif /* ADVERTISE_HINT */
  874. /* Unregister with LM-IAS */
  875. if(irnet_server.ias_obj)
  876. irias_delete_object(irnet_server.ias_obj);
  877. /* Cleanup the socket part */
  878. irda_irnet_destroy(&irnet_server.s);
  879. DEXIT(IRDA_SERV_TRACE, "\n");
  880. return;
  881. }
  882. /************************ IRDA-TTP CALLBACKS ************************/
  883. /*
  884. * When we create a IrTTP instance, we pass to it a set of callbacks
  885. * that IrTTP will call in case of various events.
  886. * We take care of those events here.
  887. */
  888. /*------------------------------------------------------------------*/
  889. /*
  890. * Function irnet_data_indication (instance, sap, skb)
  891. *
  892. * Received some data from TinyTP. Just queue it on the receive queue
  893. *
  894. */
  895. static int
  896. irnet_data_indication(void * instance,
  897. void * sap,
  898. struct sk_buff *skb)
  899. {
  900. irnet_socket * ap = (irnet_socket *) instance;
  901. unsigned char * p;
  902. int code = 0;
  903. DENTER(IRDA_TCB_TRACE, "(self/ap=0x%p, skb=0x%p)\n",
  904. ap, skb);
  905. DASSERT(skb != NULL, 0, IRDA_CB_ERROR, "skb is NULL !!!\n");
  906. /* Check is ppp is ready to receive our packet */
  907. if(!ap->ppp_open)
  908. {
  909. DERROR(IRDA_CB_ERROR, "PPP not ready, dropping packet...\n");
  910. /* When we return error, TTP will need to requeue the skb and
  911. * will stop the sender. IrTTP will stall until we send it a
  912. * flow control request... */
  913. return -ENOMEM;
  914. }
  915. /* strip address/control field if present */
  916. p = skb->data;
  917. if((p[0] == PPP_ALLSTATIONS) && (p[1] == PPP_UI))
  918. {
  919. /* chop off address/control */
  920. if(skb->len < 3)
  921. goto err_exit;
  922. p = skb_pull(skb, 2);
  923. }
  924. /* decompress protocol field if compressed */
  925. if(p[0] & 1)
  926. {
  927. /* protocol is compressed */
  928. skb_push(skb, 1)[0] = 0;
  929. }
  930. else
  931. if(skb->len < 2)
  932. goto err_exit;
  933. /* pass to generic ppp layer */
  934. /* Note : how do I know if ppp can accept or not the packet ? This is
  935. * essential if I want to manage flow control smoothly... */
  936. ppp_input(&ap->chan, skb);
  937. DEXIT(IRDA_TCB_TRACE, "\n");
  938. return 0;
  939. err_exit:
  940. DERROR(IRDA_CB_ERROR, "Packet too small, dropping...\n");
  941. kfree_skb(skb);
  942. ppp_input_error(&ap->chan, code);
  943. return 0; /* Don't return an error code, only for flow control... */
  944. }
  945. /*------------------------------------------------------------------*/
  946. /*
  947. * Function irnet_disconnect_indication (instance, sap, reason, skb)
  948. *
  949. * Connection has been closed. Chech reason to find out why
  950. *
  951. * Note : there are many cases where we come here :
  952. * o attempted to connect, timeout
  953. * o connected, link is broken, LAP has timeout
  954. * o connected, other side close the link
  955. * o connection request on the server not handled
  956. */
  957. static void
  958. irnet_disconnect_indication(void * instance,
  959. void * sap,
  960. LM_REASON reason,
  961. struct sk_buff *skb)
  962. {
  963. irnet_socket * self = (irnet_socket *) instance;
  964. int test_open;
  965. int test_connect;
  966. DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
  967. DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
  968. /* Don't care about it, but let's not leak it */
  969. if(skb)
  970. dev_kfree_skb(skb);
  971. /* Prevent higher layer from accessing IrTTP */
  972. test_open = test_and_clear_bit(0, &self->ttp_open);
  973. /* Not connecting anymore...
  974. * (note : TSAP is open, so IAP callbacks are no longer pending...) */
  975. test_connect = test_and_clear_bit(0, &self->ttp_connect);
  976. /* If both self->ttp_open and self->ttp_connect are NULL, it mean that we
  977. * have a race condition with irda_irnet_destroy() or
  978. * irnet_connect_indication(), so don't mess up tsap...
  979. */
  980. if(!(test_open || test_connect))
  981. {
  982. DERROR(IRDA_CB_ERROR, "Race condition detected...\n");
  983. return;
  984. }
  985. /* If we were active, notify the control channel */
  986. if(test_open)
  987. irnet_post_event(self, IRNET_DISCONNECT_FROM,
  988. self->saddr, self->daddr, self->rname, 0);
  989. else
  990. /* If we were trying to connect, notify the control channel */
  991. if((self->tsap) && (self != &irnet_server.s))
  992. irnet_post_event(self, IRNET_NOANSWER_FROM,
  993. self->saddr, self->daddr, self->rname, 0);
  994. /* Close our IrTTP connection, cleanup tsap */
  995. if((self->tsap) && (self != &irnet_server.s))
  996. {
  997. DEBUG(IRDA_CB_INFO, "Closing our TTP connection.\n");
  998. irttp_close_tsap(self->tsap);
  999. self->tsap = NULL;
  1000. }
  1001. /* Cleanup the socket in case we want to reconnect in ppp_output_wakeup() */
  1002. self->stsap_sel = 0;
  1003. self->daddr = DEV_ADDR_ANY;
  1004. self->tx_flow = FLOW_START;
  1005. /* Deal with the ppp instance if it's still alive */
  1006. if(self->ppp_open)
  1007. {
  1008. if(test_open)
  1009. {
  1010. /* ppp_unregister_channel() wants a user context. */
  1011. schedule_work(&self->disconnect_work);
  1012. }
  1013. else
  1014. {
  1015. /* If we were trying to connect, flush (drain) ppp_generic
  1016. * Tx queue (most often we have blocked it), which will
  1017. * trigger an other attempt to connect. If we are passive,
  1018. * this will empty the Tx queue after last try. */
  1019. ppp_output_wakeup(&self->chan);
  1020. }
  1021. }
  1022. DEXIT(IRDA_TCB_TRACE, "\n");
  1023. }
  1024. /*------------------------------------------------------------------*/
  1025. /*
  1026. * Function irnet_connect_confirm (instance, sap, qos, max_sdu_size, skb)
  1027. *
  1028. * Connections has been confirmed by the remote device
  1029. *
  1030. */
  1031. static void
  1032. irnet_connect_confirm(void * instance,
  1033. void * sap,
  1034. struct qos_info *qos,
  1035. __u32 max_sdu_size,
  1036. __u8 max_header_size,
  1037. struct sk_buff *skb)
  1038. {
  1039. irnet_socket * self = (irnet_socket *) instance;
  1040. DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
  1041. /* Check if socket is closing down (via irda_irnet_destroy()) */
  1042. if(! test_bit(0, &self->ttp_connect))
  1043. {
  1044. DERROR(IRDA_CB_ERROR, "Socket no longer connecting. Ouch !\n");
  1045. return;
  1046. }
  1047. /* How much header space do we need to reserve */
  1048. self->max_header_size = max_header_size;
  1049. /* IrTTP max SDU size in transmit direction */
  1050. self->max_sdu_size_tx = max_sdu_size;
  1051. self->max_data_size = max_sdu_size;
  1052. #ifdef STREAM_COMPAT
  1053. if(max_sdu_size == 0)
  1054. self->max_data_size = irttp_get_max_seg_size(self->tsap);
  1055. #endif /* STREAM_COMPAT */
  1056. /* At this point, IrLMP has assigned our source address */
  1057. self->saddr = irttp_get_saddr(self->tsap);
  1058. /* Allow higher layer to access IrTTP */
  1059. set_bit(0, &self->ttp_open);
  1060. clear_bit(0, &self->ttp_connect); /* Not racy, IrDA traffic is serial */
  1061. /* Give a kick in the ass of ppp_generic so that he sends us some data */
  1062. ppp_output_wakeup(&self->chan);
  1063. /* Check size of received packet */
  1064. if(skb->len > 0)
  1065. {
  1066. #ifdef PASS_CONNECT_PACKETS
  1067. DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
  1068. /* Try to pass it to PPP */
  1069. irnet_data_indication(instance, sap, skb);
  1070. #else /* PASS_CONNECT_PACKETS */
  1071. DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
  1072. kfree_skb(skb); /* Note : will be optimised with other kfree... */
  1073. #endif /* PASS_CONNECT_PACKETS */
  1074. }
  1075. else
  1076. kfree_skb(skb);
  1077. /* Notify the control channel */
  1078. irnet_post_event(self, IRNET_CONNECT_TO,
  1079. self->saddr, self->daddr, self->rname, 0);
  1080. DEXIT(IRDA_TCB_TRACE, "\n");
  1081. }
  1082. /*------------------------------------------------------------------*/
  1083. /*
  1084. * Function irnet_flow_indication (instance, sap, flow)
  1085. *
  1086. * Used by TinyTP to tell us if it can accept more data or not
  1087. *
  1088. */
  1089. static void
  1090. irnet_flow_indication(void * instance,
  1091. void * sap,
  1092. LOCAL_FLOW flow)
  1093. {
  1094. irnet_socket * self = (irnet_socket *) instance;
  1095. LOCAL_FLOW oldflow = self->tx_flow;
  1096. DENTER(IRDA_TCB_TRACE, "(self=0x%p, flow=%d)\n", self, flow);
  1097. /* Update our state */
  1098. self->tx_flow = flow;
  1099. /* Check what IrTTP want us to do... */
  1100. switch(flow)
  1101. {
  1102. case FLOW_START:
  1103. DEBUG(IRDA_CB_INFO, "IrTTP wants us to start again\n");
  1104. /* Check if we really need to wake up PPP */
  1105. if(oldflow == FLOW_STOP)
  1106. ppp_output_wakeup(&self->chan);
  1107. else
  1108. DEBUG(IRDA_CB_INFO, "But we were already transmitting !!!\n");
  1109. break;
  1110. case FLOW_STOP:
  1111. DEBUG(IRDA_CB_INFO, "IrTTP wants us to slow down\n");
  1112. break;
  1113. default:
  1114. DEBUG(IRDA_CB_INFO, "Unknown flow command!\n");
  1115. break;
  1116. }
  1117. DEXIT(IRDA_TCB_TRACE, "\n");
  1118. }
  1119. /*------------------------------------------------------------------*/
  1120. /*
  1121. * Function irnet_status_indication (instance, sap, reason, skb)
  1122. *
  1123. * Link (IrLAP) status report.
  1124. *
  1125. */
  1126. static void
  1127. irnet_status_indication(void * instance,
  1128. LINK_STATUS link,
  1129. LOCK_STATUS lock)
  1130. {
  1131. irnet_socket * self = (irnet_socket *) instance;
  1132. DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
  1133. DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
  1134. /* We can only get this event if we are connected */
  1135. switch(link)
  1136. {
  1137. case STATUS_NO_ACTIVITY:
  1138. irnet_post_event(self, IRNET_BLOCKED_LINK,
  1139. self->saddr, self->daddr, self->rname, 0);
  1140. break;
  1141. default:
  1142. DEBUG(IRDA_CB_INFO, "Unknown status...\n");
  1143. }
  1144. DEXIT(IRDA_TCB_TRACE, "\n");
  1145. }
  1146. /*------------------------------------------------------------------*/
  1147. /*
  1148. * Function irnet_connect_indication(instance, sap, qos, max_sdu_size, userdata)
  1149. *
  1150. * Incoming connection
  1151. *
  1152. * In theory, this function is called only on the server socket.
  1153. * Some other node is attempting to connect to the IrNET service, and has
  1154. * sent a connection request on our server socket.
  1155. * We just redirect the connection to the relevant IrNET socket.
  1156. *
  1157. * Note : we also make sure that between 2 irnet nodes, there can
  1158. * exist only one irnet connection.
  1159. */
  1160. static void
  1161. irnet_connect_indication(void * instance,
  1162. void * sap,
  1163. struct qos_info *qos,
  1164. __u32 max_sdu_size,
  1165. __u8 max_header_size,
  1166. struct sk_buff *skb)
  1167. {
  1168. irnet_socket * server = &irnet_server.s;
  1169. irnet_socket * new = (irnet_socket *) NULL;
  1170. DENTER(IRDA_TCB_TRACE, "(server=0x%p)\n", server);
  1171. DASSERT(instance == &irnet_server, , IRDA_CB_ERROR,
  1172. "Invalid instance (0x%p) !!!\n", instance);
  1173. DASSERT(sap == irnet_server.s.tsap, , IRDA_CB_ERROR, "Invalid sap !!!\n");
  1174. /* Try to find the most appropriate IrNET socket */
  1175. new = irnet_find_socket(server);
  1176. /* After all this hard work, do we have an socket ? */
  1177. if(new == (irnet_socket *) NULL)
  1178. {
  1179. DEXIT(IRDA_CB_INFO, ": No socket waiting for this connection.\n");
  1180. irnet_disconnect_server(server, skb);
  1181. return;
  1182. }
  1183. /* Is the socket already busy ? */
  1184. if(test_bit(0, &new->ttp_open))
  1185. {
  1186. DEXIT(IRDA_CB_INFO, ": Socket already connected.\n");
  1187. irnet_disconnect_server(server, skb);
  1188. return;
  1189. }
  1190. /* The following code is a bit tricky, so need comments ;-)
  1191. */
  1192. /* If ttp_connect is set, the socket is trying to connect to the other
  1193. * end and may have sent a IrTTP connection request and is waiting for
  1194. * a connection response (that may never come).
  1195. * Now, the pain is that the socket may have opened a tsap and is
  1196. * waiting on it, while the other end is trying to connect to it on
  1197. * another tsap.
  1198. * Because IrNET can be peer to peer, we need to workaround this.
  1199. * Furthermore, the way the irnetd script is implemented, the
  1200. * target will create a second IrNET connection back to the
  1201. * originator and expect the originator to bind this new connection
  1202. * to the original PPPD instance.
  1203. * And of course, if we don't use irnetd, we can have a race when
  1204. * both side try to connect simultaneously, which could leave both
  1205. * connections half closed (yuck).
  1206. * Conclusions :
  1207. * 1) The "originator" must accept the new connection and get rid
  1208. * of the old one so that irnetd works
  1209. * 2) One side must deny the new connection to avoid races,
  1210. * but both side must agree on which side it is...
  1211. * Most often, the originator is primary at the LAP layer.
  1212. * Jean II
  1213. */
  1214. /* Now, let's look at the way I wrote the test...
  1215. * We need to clear up the ttp_connect flag atomically to prevent
  1216. * irnet_disconnect_indication() to mess up the tsap we are going to close.
  1217. * We want to clear the ttp_connect flag only if we close the tsap,
  1218. * otherwise we will never close it, so we need to check for primary
  1219. * *before* doing the test on the flag.
  1220. * And of course, ALLOW_SIMULT_CONNECT can disable this entirely...
  1221. * Jean II
  1222. */
  1223. /* Socket already connecting ? On primary ? */
  1224. if(0
  1225. #ifdef ALLOW_SIMULT_CONNECT
  1226. || ((irttp_is_primary(server->tsap) == 1) /* primary */
  1227. && (test_and_clear_bit(0, &new->ttp_connect)))
  1228. #endif /* ALLOW_SIMULT_CONNECT */
  1229. )
  1230. {
  1231. DERROR(IRDA_CB_ERROR, "Socket already connecting, but going to reuse it !\n");
  1232. /* Cleanup the old TSAP if necessary - IrIAP will be cleaned up later */
  1233. if(new->tsap != NULL)
  1234. {
  1235. /* Close the old connection the new socket was attempting,
  1236. * so that we can hook it up to the new connection.
  1237. * It's now safe to do it... */
  1238. irttp_close_tsap(new->tsap);
  1239. new->tsap = NULL;
  1240. }
  1241. }
  1242. else
  1243. {
  1244. /* Three options :
  1245. * 1) socket was not connecting or connected : ttp_connect should be 0.
  1246. * 2) we don't want to connect the socket because we are secondary or
  1247. * ALLOW_SIMULT_CONNECT is undefined. ttp_connect should be 1.
  1248. * 3) we are half way in irnet_disconnect_indication(), and it's a
  1249. * nice race condition... Fortunately, we can detect that by checking
  1250. * if tsap is still alive. On the other hand, we can't be in
  1251. * irda_irnet_destroy() otherwise we would not have found this
  1252. * socket in the hashbin.
  1253. * Jean II */
  1254. if((test_bit(0, &new->ttp_connect)) || (new->tsap != NULL))
  1255. {
  1256. /* Don't mess this socket, somebody else in in charge... */
  1257. DERROR(IRDA_CB_ERROR, "Race condition detected, socket in use, abort connect...\n");
  1258. irnet_disconnect_server(server, skb);
  1259. return;
  1260. }
  1261. }
  1262. /* So : at this point, we have a socket, and it is idle. Good ! */
  1263. irnet_connect_socket(server, new, qos, max_sdu_size, max_header_size);
  1264. /* Check size of received packet */
  1265. if(skb->len > 0)
  1266. {
  1267. #ifdef PASS_CONNECT_PACKETS
  1268. DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
  1269. /* Try to pass it to PPP */
  1270. irnet_data_indication(new, new->tsap, skb);
  1271. #else /* PASS_CONNECT_PACKETS */
  1272. DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
  1273. kfree_skb(skb); /* Note : will be optimised with other kfree... */
  1274. #endif /* PASS_CONNECT_PACKETS */
  1275. }
  1276. else
  1277. kfree_skb(skb);
  1278. DEXIT(IRDA_TCB_TRACE, "\n");
  1279. }
  1280. /********************** IRDA-IAS/LMP CALLBACKS **********************/
  1281. /*
  1282. * These are the callbacks called by other layers of the IrDA stack,
  1283. * mainly LMP for discovery and IAS for name queries.
  1284. */
  1285. /*------------------------------------------------------------------*/
  1286. /*
  1287. * Function irnet_getvalue_confirm (result, obj_id, value, priv)
  1288. *
  1289. * Got answer from remote LM-IAS, just connect
  1290. *
  1291. * This is the reply to a IAS query we were doing to find the TSAP of
  1292. * the device we want to connect to.
  1293. * If we have found a valid TSAP, just initiate the TTP connection
  1294. * on this TSAP.
  1295. */
  1296. static void
  1297. irnet_getvalue_confirm(int result,
  1298. __u16 obj_id,
  1299. struct ias_value *value,
  1300. void * priv)
  1301. {
  1302. irnet_socket * self = (irnet_socket *) priv;
  1303. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1304. DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
  1305. /* Check if already connected (via irnet_connect_socket())
  1306. * or socket is closing down (via irda_irnet_destroy()) */
  1307. if(! test_bit(0, &self->ttp_connect))
  1308. {
  1309. DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
  1310. return;
  1311. }
  1312. /* We probably don't need to make any more queries */
  1313. iriap_close(self->iriap);
  1314. self->iriap = NULL;
  1315. /* Post process the IAS reply */
  1316. self->dtsap_sel = irnet_ias_to_tsap(self, result, value);
  1317. /* If error, just go out */
  1318. if(self->errno)
  1319. {
  1320. clear_bit(0, &self->ttp_connect);
  1321. DERROR(IRDA_OCB_ERROR, "IAS connect failed ! (0x%X)\n", self->errno);
  1322. return;
  1323. }
  1324. DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
  1325. self->daddr, self->dtsap_sel);
  1326. /* Start up TTP - non blocking */
  1327. irnet_connect_tsap(self);
  1328. DEXIT(IRDA_OCB_TRACE, "\n");
  1329. }
  1330. /*------------------------------------------------------------------*/
  1331. /*
  1332. * Function irnet_discovervalue_confirm (result, obj_id, value, priv)
  1333. *
  1334. * Handle the TSAP discovery procedure state machine.
  1335. * Got answer from remote LM-IAS, try next device
  1336. *
  1337. * We are doing a TSAP discovery procedure, and we got an answer to
  1338. * a IAS query we were doing to find the TSAP on one of the address
  1339. * in the discovery log.
  1340. *
  1341. * If we have found a valid TSAP for the first time, save it. If it's
  1342. * not the first time we found one, complain.
  1343. *
  1344. * If we have more addresses in the log, just initiate a new query.
  1345. * Note that those query may fail (see irnet_discover_daddr_and_lsap_sel())
  1346. *
  1347. * Otherwise, wrap up the procedure (cleanup), check if we have found
  1348. * any device and connect to it.
  1349. */
  1350. static void
  1351. irnet_discovervalue_confirm(int result,
  1352. __u16 obj_id,
  1353. struct ias_value *value,
  1354. void * priv)
  1355. {
  1356. irnet_socket * self = (irnet_socket *) priv;
  1357. __u8 dtsap_sel; /* TSAP we are looking for */
  1358. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1359. DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
  1360. /* Check if already connected (via irnet_connect_socket())
  1361. * or socket is closing down (via irda_irnet_destroy()) */
  1362. if(! test_bit(0, &self->ttp_connect))
  1363. {
  1364. DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
  1365. return;
  1366. }
  1367. /* Post process the IAS reply */
  1368. dtsap_sel = irnet_ias_to_tsap(self, result, value);
  1369. /* Have we got something ? */
  1370. if(self->errno == 0)
  1371. {
  1372. /* We found the requested service */
  1373. if(self->daddr != DEV_ADDR_ANY)
  1374. {
  1375. DERROR(IRDA_OCB_ERROR, "More than one device in range supports IrNET...\n");
  1376. }
  1377. else
  1378. {
  1379. /* First time we found that one, save it ! */
  1380. self->daddr = self->discoveries[self->disco_index].daddr;
  1381. self->dtsap_sel = dtsap_sel;
  1382. }
  1383. }
  1384. /* If no failure */
  1385. if((self->errno == -EADDRNOTAVAIL) || (self->errno == 0))
  1386. {
  1387. int ret;
  1388. /* Search the next node */
  1389. ret = irnet_discover_next_daddr(self);
  1390. if(!ret)
  1391. {
  1392. /* In this case, the above request was non-blocking.
  1393. * We will return here after a while... */
  1394. return;
  1395. }
  1396. /* In this case, we have processed the last discovery item */
  1397. }
  1398. /* No more queries to be done (failure or last one) */
  1399. /* We probably don't need to make any more queries */
  1400. iriap_close(self->iriap);
  1401. self->iriap = NULL;
  1402. /* No more items : remove the log and signal termination */
  1403. DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%p)\n",
  1404. self->discoveries);
  1405. if(self->discoveries != NULL)
  1406. {
  1407. /* Cleanup our copy of the discovery log */
  1408. kfree(self->discoveries);
  1409. self->discoveries = NULL;
  1410. }
  1411. self->disco_number = -1;
  1412. /* Check out what we found */
  1413. if(self->daddr == DEV_ADDR_ANY)
  1414. {
  1415. self->daddr = DEV_ADDR_ANY;
  1416. clear_bit(0, &self->ttp_connect);
  1417. DEXIT(IRDA_OCB_TRACE, ": cannot discover IrNET in any device !!!\n");
  1418. return;
  1419. }
  1420. /* We have a valid address - just connect */
  1421. DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
  1422. self->daddr, self->dtsap_sel);
  1423. /* Start up TTP - non blocking */
  1424. irnet_connect_tsap(self);
  1425. DEXIT(IRDA_OCB_TRACE, "\n");
  1426. }
  1427. #ifdef DISCOVERY_EVENTS
  1428. /*------------------------------------------------------------------*/
  1429. /*
  1430. * Function irnet_discovery_indication (discovery)
  1431. *
  1432. * Got a discovery indication from IrLMP, post an event
  1433. *
  1434. * Note : IrLMP take care of matching the hint mask for us, and also
  1435. * check if it is a "new" node for us...
  1436. *
  1437. * As IrLMP filter on the IrLAN hint bit, we get both IrLAN and IrNET
  1438. * nodes, so it's only at connection time that we will know if the
  1439. * node support IrNET, IrLAN or both. The other solution is to check
  1440. * in IAS the PNP ids and service name.
  1441. * Note : even if a node support IrNET (or IrLAN), it's no guarantee
  1442. * that we will be able to connect to it, the node might already be
  1443. * busy...
  1444. *
  1445. * One last thing : in some case, this function will trigger duplicate
  1446. * discovery events. On the other hand, we should catch all
  1447. * discoveries properly (i.e. not miss one). Filtering duplicate here
  1448. * is to messy, so we leave that to user space...
  1449. */
  1450. static void
  1451. irnet_discovery_indication(discinfo_t * discovery,
  1452. DISCOVERY_MODE mode,
  1453. void * priv)
  1454. {
  1455. irnet_socket * self = &irnet_server.s;
  1456. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1457. DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
  1458. "Invalid instance (0x%p) !!!\n", priv);
  1459. DEBUG(IRDA_OCB_INFO, "Discovered new IrNET/IrLAN node %s...\n",
  1460. discovery->info);
  1461. /* Notify the control channel */
  1462. irnet_post_event(NULL, IRNET_DISCOVER,
  1463. discovery->saddr, discovery->daddr, discovery->info,
  1464. u16ho(discovery->hints));
  1465. DEXIT(IRDA_OCB_TRACE, "\n");
  1466. }
  1467. /*------------------------------------------------------------------*/
  1468. /*
  1469. * Function irnet_expiry_indication (expiry)
  1470. *
  1471. * Got a expiry indication from IrLMP, post an event
  1472. *
  1473. * Note : IrLMP take care of matching the hint mask for us, we only
  1474. * check if it is a "new" node...
  1475. */
  1476. static void
  1477. irnet_expiry_indication(discinfo_t * expiry,
  1478. DISCOVERY_MODE mode,
  1479. void * priv)
  1480. {
  1481. irnet_socket * self = &irnet_server.s;
  1482. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1483. DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
  1484. "Invalid instance (0x%p) !!!\n", priv);
  1485. DEBUG(IRDA_OCB_INFO, "IrNET/IrLAN node %s expired...\n",
  1486. expiry->info);
  1487. /* Notify the control channel */
  1488. irnet_post_event(NULL, IRNET_EXPIRE,
  1489. expiry->saddr, expiry->daddr, expiry->info,
  1490. u16ho(expiry->hints));
  1491. DEXIT(IRDA_OCB_TRACE, "\n");
  1492. }
  1493. #endif /* DISCOVERY_EVENTS */
  1494. /*********************** PROC ENTRY CALLBACKS ***********************/
  1495. /*
  1496. * We create a instance in the /proc filesystem, and here we take care
  1497. * of that...
  1498. */
  1499. #ifdef CONFIG_PROC_FS
  1500. static int
  1501. irnet_proc_show(struct seq_file *m, void *v)
  1502. {
  1503. irnet_socket * self;
  1504. char * state;
  1505. int i = 0;
  1506. /* Get the IrNET server information... */
  1507. seq_printf(m, "IrNET server - ");
  1508. seq_printf(m, "IrDA state: %s, ",
  1509. (irnet_server.running ? "running" : "dead"));
  1510. seq_printf(m, "stsap_sel: %02x, ", irnet_server.s.stsap_sel);
  1511. seq_printf(m, "dtsap_sel: %02x\n", irnet_server.s.dtsap_sel);
  1512. /* Do we need to continue ? */
  1513. if(!irnet_server.running)
  1514. return 0;
  1515. /* Protect access to the instance list */
  1516. spin_lock_bh(&irnet_server.spinlock);
  1517. /* Get the sockets one by one... */
  1518. self = (irnet_socket *) hashbin_get_first(irnet_server.list);
  1519. while(self != NULL)
  1520. {
  1521. /* Start printing info about the socket. */
  1522. seq_printf(m, "\nIrNET socket %d - ", i++);
  1523. /* First, get the requested configuration */
  1524. seq_printf(m, "Requested IrDA name: \"%s\", ", self->rname);
  1525. seq_printf(m, "daddr: %08x, ", self->rdaddr);
  1526. seq_printf(m, "saddr: %08x\n", self->rsaddr);
  1527. /* Second, get all the PPP info */
  1528. seq_printf(m, " PPP state: %s",
  1529. (self->ppp_open ? "registered" : "unregistered"));
  1530. if(self->ppp_open)
  1531. {
  1532. seq_printf(m, ", unit: ppp%d",
  1533. ppp_unit_number(&self->chan));
  1534. seq_printf(m, ", channel: %d",
  1535. ppp_channel_index(&self->chan));
  1536. seq_printf(m, ", mru: %d",
  1537. self->mru);
  1538. /* Maybe add self->flags ? Later... */
  1539. }
  1540. /* Then, get all the IrDA specific info... */
  1541. if(self->ttp_open)
  1542. state = "connected";
  1543. else
  1544. if(self->tsap != NULL)
  1545. state = "connecting";
  1546. else
  1547. if(self->iriap != NULL)
  1548. state = "searching";
  1549. else
  1550. if(self->ttp_connect)
  1551. state = "weird";
  1552. else
  1553. state = "idle";
  1554. seq_printf(m, "\n IrDA state: %s, ", state);
  1555. seq_printf(m, "daddr: %08x, ", self->daddr);
  1556. seq_printf(m, "stsap_sel: %02x, ", self->stsap_sel);
  1557. seq_printf(m, "dtsap_sel: %02x\n", self->dtsap_sel);
  1558. /* Next socket, please... */
  1559. self = (irnet_socket *) hashbin_get_next(irnet_server.list);
  1560. }
  1561. /* Spin lock end */
  1562. spin_unlock_bh(&irnet_server.spinlock);
  1563. return 0;
  1564. }
  1565. static int irnet_proc_open(struct inode *inode, struct file *file)
  1566. {
  1567. return single_open(file, irnet_proc_show, NULL);
  1568. }
  1569. static const struct file_operations irnet_proc_fops = {
  1570. .owner = THIS_MODULE,
  1571. .open = irnet_proc_open,
  1572. .read = seq_read,
  1573. .llseek = seq_lseek,
  1574. .release = single_release,
  1575. };
  1576. #endif /* PROC_FS */
  1577. /********************** CONFIGURATION/CLEANUP **********************/
  1578. /*
  1579. * Initialisation and teardown of the IrDA part, called at module
  1580. * insertion and removal...
  1581. */
  1582. /*------------------------------------------------------------------*/
  1583. /*
  1584. * Prepare the IrNET layer for operation...
  1585. */
  1586. int __init
  1587. irda_irnet_init(void)
  1588. {
  1589. int err = 0;
  1590. DENTER(MODULE_TRACE, "()\n");
  1591. /* Pure paranoia - should be redundant */
  1592. memset(&irnet_server, 0, sizeof(struct irnet_root));
  1593. /* Setup start of irnet instance list */
  1594. irnet_server.list = hashbin_new(HB_NOLOCK);
  1595. DABORT(irnet_server.list == NULL, -ENOMEM,
  1596. MODULE_ERROR, "Can't allocate hashbin!\n");
  1597. /* Init spinlock for instance list */
  1598. spin_lock_init(&irnet_server.spinlock);
  1599. /* Initialise control channel */
  1600. init_waitqueue_head(&irnet_events.rwait);
  1601. irnet_events.index = 0;
  1602. /* Init spinlock for event logging */
  1603. spin_lock_init(&irnet_events.spinlock);
  1604. #ifdef CONFIG_PROC_FS
  1605. /* Add a /proc file for irnet infos */
  1606. proc_create("irnet", 0, proc_irda, &irnet_proc_fops);
  1607. #endif /* CONFIG_PROC_FS */
  1608. /* Setup the IrNET server */
  1609. err = irnet_setup_server();
  1610. if(!err)
  1611. /* We are no longer functional... */
  1612. irnet_server.running = 1;
  1613. DEXIT(MODULE_TRACE, "\n");
  1614. return err;
  1615. }
  1616. /*------------------------------------------------------------------*/
  1617. /*
  1618. * Cleanup at exit...
  1619. */
  1620. void __exit
  1621. irda_irnet_cleanup(void)
  1622. {
  1623. DENTER(MODULE_TRACE, "()\n");
  1624. /* We are no longer there... */
  1625. irnet_server.running = 0;
  1626. #ifdef CONFIG_PROC_FS
  1627. /* Remove our /proc file */
  1628. remove_proc_entry("irnet", proc_irda);
  1629. #endif /* CONFIG_PROC_FS */
  1630. /* Remove our IrNET server from existence */
  1631. irnet_destroy_server();
  1632. /* Remove all instances of IrNET socket still present */
  1633. hashbin_delete(irnet_server.list, (FREE_FUNC) irda_irnet_destroy);
  1634. DEXIT(MODULE_TRACE, "\n");
  1635. }