irnet_irda.c 56 KB

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