irnet_irda.c 56 KB

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