2 * IrNET protocol module : Synchronous PPP over an IrDA socket.
4 * Jean II - HPL `00 - <jt@hpl.hp.com>
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.
11 #include "irnet_irda.h" /* Private header */
13 /************************* CONTROL CHANNEL *************************/
15 * When ppp is not active, /dev/irnet act as a control channel.
16 * Writting allow to set up the IrDA destination of the IrNET channel,
17 * and any application may be read events happening on IrNET...
20 /*------------------------------------------------------------------*/
22 * Post an event to the control channel...
23 * Put the event in the log, and then wait all process blocked on read
24 * so they can read the log...
27 irnet_post_event(irnet_socket * ap,
33 unsigned long flags; /* For spinlock */
34 int index; /* In the log */
36 DENTER(CTRL_TRACE, "(ap=0x%X, event=%d, daddr=%08x, name=``%s'')\n",
37 (unsigned int) ap, event, daddr, name);
39 /* Protect this section via spinlock.
40 * Note : as we are the only event producer, we only need to exclude
41 * ourself when touching the log, which is nice and easy.
43 spin_lock_irqsave(&irnet_events.spinlock, flags);
45 /* Copy the event in the log */
46 index = irnet_events.index;
47 irnet_events.log[index].event = event;
48 irnet_events.log[index].daddr = daddr;
49 irnet_events.log[index].saddr = saddr;
50 /* Try to copy IrDA nickname */
52 strcpy(irnet_events.log[index].name, name);
54 irnet_events.log[index].name[0] = '\0';
55 /* Try to get ppp unit number */
56 if((ap != (irnet_socket *) NULL) && (ap->ppp_open))
57 irnet_events.log[index].unit = ppp_unit_number(&ap->chan);
59 irnet_events.log[index].unit = -1;
61 /* Increment the index
62 * Note that we increment the index only after the event is written,
63 * to make sure that the readers don't get garbage... */
64 irnet_events.index = (index + 1) % IRNET_MAX_EVENTS;
66 DEBUG(CTRL_INFO, "New event index is %d\n", irnet_events.index);
69 spin_unlock_irqrestore(&irnet_events.spinlock, flags);
71 /* Now : wake up everybody waiting for events... */
72 wake_up_interruptible_all(&irnet_events.rwait);
74 DEXIT(CTRL_TRACE, "\n");
77 /************************* IRDA SUBROUTINES *************************/
79 * These are a bunch of subroutines called from other functions
80 * down there, mostly common code or to improve readability...
82 * Note : we duplicate quite heavily some routines of af_irda.c,
83 * because our input structure (self) is quite different
84 * (struct irnet instead of struct irda_sock), which make sharing
85 * the same code impossible (at least, without templates).
88 /*------------------------------------------------------------------*/
90 * Function irda_open_tsap (self)
92 * Open local Transport Service Access Point (TSAP)
94 * Create a IrTTP instance for us and set all the IrTTP callbacks.
97 irnet_open_tsap(irnet_socket * self)
99 notify_t notify; /* Callback structure */
101 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
103 DABORT(self->tsap != NULL, -EBUSY, IRDA_SR_ERROR, "Already busy !\n");
105 /* Initialize IrTTP callbacks to be used by the IrDA stack */
106 irda_notify_init(¬ify);
107 notify.connect_confirm = irnet_connect_confirm;
108 notify.connect_indication = irnet_connect_indication;
109 notify.disconnect_indication = irnet_disconnect_indication;
110 notify.data_indication = irnet_data_indication;
111 /*notify.udata_indication = NULL;*/
112 notify.flow_indication = irnet_flow_indication;
113 notify.status_indication = irnet_status_indication;
114 notify.instance = self;
115 strncpy(notify.name, IRNET_NOTIFY_NAME, NOTIFY_MAX_NAME);
117 /* Open an IrTTP instance */
118 self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
120 DABORT(self->tsap == NULL, -ENOMEM,
121 IRDA_SR_ERROR, "Unable to allocate TSAP !\n");
123 /* Remember which TSAP selector we actually got */
124 self->stsap_sel = self->tsap->stsap_sel;
126 DEXIT(IRDA_SR_TRACE, " - tsap=0x%X, sel=0x%X\n",
127 (unsigned int) self->tsap, self->stsap_sel);
131 /*------------------------------------------------------------------*/
133 * Function irnet_ias_to_tsap (self, result, value)
135 * Examine an IAS object and extract TSAP
137 * We do an IAP query to find the TSAP associated with the IrNET service.
138 * When IrIAP pass us the result of the query, this function look at
139 * the return values to check for failures and extract the TSAP if
141 * Also deallocate value
142 * The failure is in self->errno
146 irnet_ias_to_tsap(irnet_socket * self,
148 struct ias_value * value)
150 __u8 dtsap_sel = 0; /* TSAP we are looking for */
152 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
154 /* By default, no error */
157 /* Check if request succeeded */
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;
167 /* Other errors, most likely IrDA stack failure */
169 DEBUG(IRDA_SR_INFO, "IAS query failed ! (%d)\n", result);
170 self->errno = -EHOSTUNREACH;
173 /* Success : we got what we wanted */
178 /* Check what was returned to us */
181 /* What type of argument have we got ? */
185 DEBUG(IRDA_SR_INFO, "result=%d\n", value->t.integer);
186 if(value->t.integer != -1)
187 /* Get the remote TSAP selector */
188 dtsap_sel = value->t.integer;
190 self->errno = -EADDRNOTAVAIL;
193 self->errno = -EADDRNOTAVAIL;
194 DERROR(IRDA_SR_ERROR, "bad type ! (0x%X)\n", value->type);
199 irias_delete_value(value);
201 else /* value == NULL */
203 /* Nothing returned to us - usually result != SUCCESS */
206 DERROR(IRDA_SR_ERROR,
207 "IrDA bug : result == SUCCESS && value == NULL\n");
208 self->errno = -EHOSTUNREACH;
211 DEXIT(IRDA_SR_TRACE, "\n");
213 /* Return the TSAP */
217 /*------------------------------------------------------------------*/
219 * Function irnet_find_lsap_sel (self)
221 * Try to lookup LSAP selector in remote LM-IAS
223 * Basically, we start a IAP query, and then go to sleep. When the query
224 * return, irnet_getvalue_confirm will wake us up, and we can examine the
225 * result of the query...
226 * Note that in some case, the query fail even before we go to sleep,
227 * creating some races...
230 irnet_find_lsap_sel(irnet_socket * self)
232 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
234 /* This should not happen */
235 DABORT(self->iriap, -EBUSY, IRDA_SR_ERROR, "busy with a previous query.\n");
237 /* Create an IAP instance, will be closed in irnet_getvalue_confirm() */
238 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
239 irnet_getvalue_confirm);
241 /* Treat unexpected signals as disconnect */
242 self->errno = -EHOSTUNREACH;
244 /* Query remote LM-IAS */
245 iriap_getvaluebyclass_request(self->iriap, self->rsaddr, self->daddr,
246 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
248 /* The above request is non-blocking.
249 * After a while, IrDA will call us back in irnet_getvalue_confirm()
250 * We will then call irnet_ias_to_tsap() and finish the
251 * connection procedure */
253 DEXIT(IRDA_SR_TRACE, "\n");
257 /*------------------------------------------------------------------*/
259 * Function irnet_connect_tsap (self)
261 * Initialise the TTP socket and initiate TTP connection
265 irnet_connect_tsap(irnet_socket * self)
269 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
271 /* Open a local TSAP (an IrTTP instance) */
272 err = irnet_open_tsap(self);
275 clear_bit(0, &self->ttp_connect);
276 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
280 /* Connect to remote device */
281 err = irttp_connect_request(self->tsap, self->dtsap_sel,
282 self->rsaddr, self->daddr, NULL,
283 self->max_sdu_size_rx, NULL);
286 clear_bit(0, &self->ttp_connect);
287 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
291 /* The above call is non-blocking.
292 * After a while, the IrDA stack will either call us back in
293 * irnet_connect_confirm() or irnet_disconnect_indication()
294 * See you there ;-) */
296 DEXIT(IRDA_SR_TRACE, "\n");
300 /*------------------------------------------------------------------*/
302 * Function irnet_discover_next_daddr (self)
304 * Query the IrNET TSAP of the next device in the log.
306 * Used in the TSAP discovery procedure.
309 irnet_discover_next_daddr(irnet_socket * self)
311 /* Close the last instance of IrIAP, and open a new one.
312 * We can't reuse the IrIAP instance in the IrIAP callback */
315 iriap_close(self->iriap);
318 /* Create a new IAP instance */
319 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
320 irnet_discovervalue_confirm);
322 /* Next discovery - before the call to avoid races */
325 /* Check if we have one more address to try */
326 if(self->disco_index < self->disco_number)
328 /* Query remote LM-IAS */
329 iriap_getvaluebyclass_request(self->iriap,
330 self->discoveries[self->disco_index].saddr,
331 self->discoveries[self->disco_index].daddr,
332 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
333 /* The above request is non-blocking.
334 * After a while, IrDA will call us back in irnet_discovervalue_confirm()
335 * We will then call irnet_ias_to_tsap() and come back here again... */
342 /*------------------------------------------------------------------*/
344 * Function irnet_discover_daddr_and_lsap_sel (self)
346 * This try to find a device with the requested service.
348 * Initiate a TSAP discovery procedure.
349 * It basically look into the discovery log. For each address in the list,
350 * it queries the LM-IAS of the device to find if this device offer
351 * the requested service.
352 * If there is more than one node supporting the service, we complain
353 * to the user (it should move devices around).
354 * If we find one node which have the requested TSAP, we connect to it.
356 * This function just start the whole procedure. It request the discovery
357 * log and submit the first IAS query.
358 * The bulk of the job is handled in irnet_discovervalue_confirm()
360 * Note : this procedure fails if there is more than one device in range
361 * on the same dongle, because IrLMP doesn't disconnect the LAP when the
362 * last LSAP is closed. Moreover, we would need to wait the LAP
366 irnet_discover_daddr_and_lsap_sel(irnet_socket * self)
370 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
372 /* Ask lmp for the current discovery log */
373 self->discoveries = irlmp_get_discoveries(&self->disco_number, self->mask,
374 DISCOVERY_DEFAULT_SLOTS);
376 /* Check if the we got some results */
377 if(self->discoveries == NULL)
379 self->disco_number = -1;
380 clear_bit(0, &self->ttp_connect);
381 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "No Cachelog...\n");
383 DEBUG(IRDA_SR_INFO, "Got the log (0x%X), size is %d\n",
384 (unsigned int) self->discoveries, self->disco_number);
386 /* Start with the first discovery */
387 self->disco_index = -1;
388 self->daddr = DEV_ADDR_ANY;
390 /* This will fail if the log is empty - this is non-blocking */
391 ret = irnet_discover_next_daddr(self);
395 iriap_close(self->iriap);
398 /* Cleanup our copy of the discovery log */
399 kfree(self->discoveries);
400 self->discoveries = NULL;
402 clear_bit(0, &self->ttp_connect);
403 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
406 /* Follow me in irnet_discovervalue_confirm() */
408 DEXIT(IRDA_SR_TRACE, "\n");
412 /*------------------------------------------------------------------*/
414 * Function irnet_dname_to_daddr (self)
416 * Convert an IrDA nickname to a valid IrDA address
418 * It basically look into the discovery log until there is a match.
421 irnet_dname_to_daddr(irnet_socket * self)
423 struct irda_device_info *discoveries; /* Copy of the discovery log */
424 int number; /* Number of nodes in the log */
427 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
429 /* Ask lmp for the current discovery log */
430 discoveries = irlmp_get_discoveries(&number, 0xffff,
431 DISCOVERY_DEFAULT_SLOTS);
432 /* Check if the we got some results */
433 if(discoveries == NULL)
434 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
437 * Now, check all discovered devices (if any), and connect
438 * client only about the services that the client is
441 for(i = 0; i < number; i++)
443 /* Does the name match ? */
444 if(!strncmp(discoveries[i].info, self->rname, NICKNAME_MAX_LEN))
446 /* Yes !!! Get it.. */
447 self->daddr = discoveries[i].daddr;
448 DEBUG(IRDA_SR_INFO, "discovered device ``%s'' at address 0x%08x.\n",
449 self->rname, self->daddr);
451 DEXIT(IRDA_SR_TRACE, "\n");
456 DEBUG(IRDA_SR_INFO, "cannot discover device ``%s'' !!!\n", self->rname);
458 return(-EADDRNOTAVAIL);
462 /************************* SOCKET ROUTINES *************************/
464 * This are the main operations on IrNET sockets, basically to create
465 * and destroy IrNET sockets. These are called from the PPP part...
468 /*------------------------------------------------------------------*/
470 * Create a IrNET instance : just initialise some parameters...
473 irda_irnet_create(irnet_socket * self)
475 DENTER(IRDA_SOCK_TRACE, "(self=0x%X)\n", (unsigned int) self);
477 self->magic = IRNET_MAGIC; /* Paranoia */
479 self->ttp_open = 0; /* Prevent higher layer from accessing IrTTP */
480 self->ttp_connect = 0; /* Not connecting yet */
481 self->rname[0] = '\0'; /* May be set via control channel */
482 self->rdaddr = DEV_ADDR_ANY; /* May be set via control channel */
483 self->rsaddr = DEV_ADDR_ANY; /* May be set via control channel */
484 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
485 self->saddr = DEV_ADDR_ANY; /* Until we get connected */
486 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
488 /* Register as a client with IrLMP */
489 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
490 #ifdef DISCOVERY_NOMASK
491 self->mask = 0xffff; /* For W2k compatibility */
492 #else /* DISCOVERY_NOMASK */
493 self->mask = irlmp_service_to_hint(S_LAN);
494 #endif /* DISCOVERY_NOMASK */
495 self->tx_flow = FLOW_START; /* Flow control from IrTTP */
497 DEXIT(IRDA_SOCK_TRACE, "\n");
501 /*------------------------------------------------------------------*/
503 * Connect to the other side :
504 * o convert device name to an address
505 * o find the socket number (dlsap)
506 * o Establish the connection
508 * Note : We no longer mimic af_irda. The IAS query for finding the TSAP
509 * is done asynchronously, like the TTP connection. This allow us to
510 * call this function from any context (not only process).
511 * The downside is that following what's happening in there is tricky
512 * because it involve various functions all over the place...
515 irda_irnet_connect(irnet_socket * self)
519 DENTER(IRDA_SOCK_TRACE, "(self=0x%X)\n", (unsigned int) self);
521 /* Check if we are already trying to connect.
522 * Because irda_irnet_connect() can be called directly by pppd plus
523 * packet retries in ppp_generic and connect may take time, plus we may
524 * race with irnet_connect_indication(), we need to be careful there... */
525 if(test_and_set_bit(0, &self->ttp_connect))
526 DRETURN(-EBUSY, IRDA_SOCK_INFO, "Already connecting...\n");
527 if((self->iriap != NULL) || (self->tsap != NULL))
528 DERROR(IRDA_SOCK_ERROR, "Socket not cleaned up...\n");
530 /* Insert ourselves in the hashbin so that the IrNET server can find us.
531 * Notes : 4th arg is string of 32 char max and must be null terminated
532 * When 4th arg is used (string), 3rd arg isn't (int)
533 * Can't re-insert (MUST remove first) so check for that... */
534 if((irnet_server.running) && (self->q.q_next == NULL))
537 spin_lock_irqsave(&irnet_server.spinlock, flags);
538 hashbin_insert(irnet_server.list, (irda_queue_t *) self, 0, self->rname);
539 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
540 DEBUG(IRDA_SOCK_INFO, "Inserted ``%s'' in hashbin...\n", self->rname);
543 /* If we don't have anything (no address, no name) */
544 if((self->rdaddr == DEV_ADDR_ANY) && (self->rname[0] == '\0'))
546 /* Try to find a suitable address */
547 if((err = irnet_discover_daddr_and_lsap_sel(self)) != 0)
548 DRETURN(err, IRDA_SOCK_INFO, "auto-connect failed!\n");
549 /* In most cases, the call above is non-blocking */
553 /* If we have only the name (no address), try to get an address */
554 if(self->rdaddr == DEV_ADDR_ANY)
556 if((err = irnet_dname_to_daddr(self)) != 0)
557 DRETURN(err, IRDA_SOCK_INFO, "name connect failed!\n");
560 /* Use the requested destination address */
561 self->daddr = self->rdaddr;
563 /* Query remote LM-IAS to find LSAP selector */
564 irnet_find_lsap_sel(self);
565 /* The above call is non blocking */
568 /* At this point, we are waiting for the IrDA stack to call us back,
569 * or we have already failed.
570 * We will finish the connection procedure in irnet_connect_tsap().
572 DEXIT(IRDA_SOCK_TRACE, "\n");
576 /*------------------------------------------------------------------*/
578 * Function irda_irnet_destroy(self)
580 * Destroy irnet instance
582 * Note : this need to be called from a process context.
585 irda_irnet_destroy(irnet_socket * self)
587 DENTER(IRDA_SOCK_TRACE, "(self=0x%X)\n", (unsigned int) self);
591 /* Remove ourselves from hashbin (if we are queued in hashbin)
592 * Note : `irnet_server.running' protect us from calls in hashbin_delete() */
593 if((irnet_server.running) && (self->q.q_next != NULL))
595 struct irnet_socket * entry;
597 DEBUG(IRDA_SOCK_INFO, "Removing from hash..\n");
598 spin_lock_irqsave(&irnet_server.spinlock, flags);
599 entry = hashbin_remove_this(irnet_server.list, (irda_queue_t *) self);
600 self->q.q_next = NULL;
601 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
602 DASSERT(entry == self, , IRDA_SOCK_ERROR, "Can't remove from hash.\n");
605 /* If we were connected, post a message */
606 if(test_bit(0, &self->ttp_open))
608 /* Note : as the disconnect comes from ppp_generic, the unit number
609 * doesn't exist anymore when we post the event, so we need to pass
610 * NULL as the first arg... */
611 irnet_post_event(NULL, IRNET_DISCONNECT_TO,
612 self->saddr, self->daddr, self->rname);
615 /* Prevent various IrDA callbacks from messing up things
616 * Need to be first */
617 clear_bit(0, &self->ttp_connect);
619 /* Prevent higher layer from accessing IrTTP */
620 clear_bit(0, &self->ttp_open);
622 /* Unregister with IrLMP */
623 irlmp_unregister_client(self->ckey);
625 /* Unregister with LM-IAS */
628 iriap_close(self->iriap);
632 /* Cleanup eventual discoveries from connection attempt */
633 if(self->discoveries != NULL)
635 /* Cleanup our copy of the discovery log */
636 kfree(self->discoveries);
637 self->discoveries = NULL;
640 /* Close our IrTTP connection */
643 DEBUG(IRDA_SOCK_INFO, "Closing our TTP connection.\n");
644 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
645 irttp_close_tsap(self->tsap);
650 DEXIT(IRDA_SOCK_TRACE, "\n");
655 /************************** SERVER SOCKET **************************/
657 * The IrNET service is composed of one server socket and a variable
658 * number of regular IrNET sockets. The server socket is supposed to
659 * handle incoming connections and redirect them to one IrNET sockets.
660 * It's a superset of the regular IrNET socket, but has a very distinct
664 /*------------------------------------------------------------------*/
666 * Function irnet_daddr_to_dname (self)
668 * Convert an IrDA address to a IrDA nickname
670 * It basically look into the discovery log until there is a match.
673 irnet_daddr_to_dname(irnet_socket * self)
675 struct irda_device_info *discoveries; /* Copy of the discovery log */
676 int number; /* Number of nodes in the log */
679 DENTER(IRDA_SERV_TRACE, "(self=0x%X)\n", (unsigned int) self);
681 /* Ask lmp for the current discovery log */
682 discoveries = irlmp_get_discoveries(&number, 0xffff,
683 DISCOVERY_DEFAULT_SLOTS);
684 /* Check if the we got some results */
685 if (discoveries == NULL)
686 DRETURN(-ENETUNREACH, IRDA_SERV_INFO, "Cachelog empty...\n");
688 /* Now, check all discovered devices (if any) */
689 for(i = 0; i < number; i++)
691 /* Does the name match ? */
692 if(discoveries[i].daddr == self->daddr)
694 /* Yes !!! Get it.. */
695 strncpy(self->rname, discoveries[i].info, NICKNAME_MAX_LEN);
696 self->rname[NICKNAME_MAX_LEN + 1] = '\0';
697 DEBUG(IRDA_SERV_INFO, "Device 0x%08x is in fact ``%s''.\n",
698 self->daddr, self->rname);
700 DEXIT(IRDA_SERV_TRACE, "\n");
705 DEXIT(IRDA_SERV_INFO, ": cannot discover device 0x%08x !!!\n", self->daddr);
707 return(-EADDRNOTAVAIL);
710 /*------------------------------------------------------------------*/
712 * Function irda_find_socket (self)
714 * Find the correct IrNET socket
716 * Look into the list of IrNET sockets and finds one with the right
719 static inline irnet_socket *
720 irnet_find_socket(irnet_socket * self)
722 irnet_socket * new = (irnet_socket *) NULL;
726 DENTER(IRDA_SERV_TRACE, "(self=0x%X)\n", (unsigned int) self);
728 /* Get the addresses of the requester */
729 self->daddr = irttp_get_daddr(self->tsap);
730 self->saddr = irttp_get_saddr(self->tsap);
732 /* Try to get the IrDA nickname of the requester */
733 err = irnet_daddr_to_dname(self);
735 /* Protect access to the instance list */
736 spin_lock_irqsave(&irnet_server.spinlock, flags);
738 /* So now, try to get an socket having specifically
739 * requested that nickname */
742 new = (irnet_socket *) hashbin_find(irnet_server.list,
745 DEBUG(IRDA_SERV_INFO, "Socket 0x%X matches rname ``%s''.\n",
746 (unsigned int) new, new->rname);
749 /* If no name matches, try to find an socket by the destination address */
750 /* It can be either the requested destination address (set via the
751 * control channel), or the current destination address if the
752 * socket is in the middle of a connection request */
753 if(new == (irnet_socket *) NULL)
755 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
756 while(new !=(irnet_socket *) NULL)
758 /* Does it have the same address ? */
759 if((new->rdaddr == self->daddr) || (new->daddr == self->daddr))
761 /* Yes !!! Get it.. */
762 DEBUG(IRDA_SERV_INFO, "Socket 0x%X matches daddr %#08x.\n",
763 (unsigned int) new, self->daddr);
766 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
770 /* If we don't have any socket, get the first unconnected socket */
771 if(new == (irnet_socket *) NULL)
773 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
774 while(new !=(irnet_socket *) NULL)
776 /* Is it available ? */
777 if(!(test_bit(0, &new->ttp_open)) && (new->rdaddr == DEV_ADDR_ANY) &&
778 (new->rname[0] == '\0') && (new->ppp_open))
780 /* Yes !!! Get it.. */
781 DEBUG(IRDA_SERV_INFO, "Socket 0x%X is free.\n",
785 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
790 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
792 DEXIT(IRDA_SERV_TRACE, " - new = 0x%X\n", (unsigned int) new);
796 /*------------------------------------------------------------------*/
798 * Function irda_connect_socket (self)
800 * Connect an incoming connection to the socket
804 irnet_connect_socket(irnet_socket * server,
806 struct qos_info * qos,
808 __u8 max_header_size)
810 DENTER(IRDA_SERV_TRACE, "(server=0x%X, new=0x%X)\n",
811 (unsigned int) server, (unsigned int) new);
813 /* Now attach up the new socket */
814 new->tsap = irttp_dup(server->tsap, new);
815 DABORT(new->tsap == NULL, -1, IRDA_SERV_ERROR, "dup failed!\n");
817 /* Set up all the relevant parameters on the new socket */
818 new->stsap_sel = new->tsap->stsap_sel;
819 new->dtsap_sel = new->tsap->dtsap_sel;
820 new->saddr = irttp_get_saddr(new->tsap);
821 new->daddr = irttp_get_daddr(new->tsap);
823 new->max_header_size = max_header_size;
824 new->max_sdu_size_tx = max_sdu_size;
825 new->max_data_size = max_sdu_size;
827 /* If we want to receive "stream sockets" */
828 if(max_sdu_size == 0)
829 new->max_data_size = irttp_get_max_seg_size(new->tsap);
830 #endif /* STREAM_COMPAT */
832 /* Clean up the original one to keep it in listen state */
833 irttp_listen(server->tsap);
835 /* Send a connection response on the new socket */
836 irttp_connect_response(new->tsap, new->max_sdu_size_rx, NULL);
838 /* Allow PPP to send its junk over the new socket... */
839 set_bit(0, &new->ttp_open);
841 /* Not connecting anymore, and clean up last possible remains
842 * of connection attempts on the socket */
843 clear_bit(0, &new->ttp_connect);
846 iriap_close(new->iriap);
849 if(new->discoveries != NULL)
851 kfree(new->discoveries);
852 new->discoveries = NULL;
855 #ifdef CONNECT_INDIC_KICK
856 /* As currently we don't block packets in ppp_irnet_send() while passive,
857 * this is not really needed...
858 * Also, not doing it give IrDA a chance to finish the setup properly
859 * before beeing swamped with packets... */
860 ppp_output_wakeup(&new->chan);
861 #endif /* CONNECT_INDIC_KICK */
863 /* Notify the control channel */
864 irnet_post_event(new, IRNET_CONNECT_FROM,
865 new->saddr, new->daddr, server->rname);
867 DEXIT(IRDA_SERV_TRACE, "\n");
871 /*------------------------------------------------------------------*/
873 * Function irda_disconnect_server (self)
875 * Cleanup the server socket when the incoming connection abort
879 irnet_disconnect_server(irnet_socket * self,
882 DENTER(IRDA_SERV_TRACE, "(self=0x%X)\n", (unsigned int) self);
884 /* Put the received packet in the black hole */
887 #ifdef FAIL_SEND_DISCONNECT
888 /* Tell the other party we don't want to be connected */
889 /* Hum... Is it the right thing to do ? And do we need to send
890 * a connect response before ? It looks ok without this... */
891 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
892 #endif /* FAIL_SEND_DISCONNECT */
894 /* Notify the control channel (see irnet_find_socket()) */
895 irnet_post_event(NULL, IRNET_REQUEST_FROM,
896 self->saddr, self->daddr, self->rname);
898 /* Clean up the server to keep it in listen state */
899 irttp_listen(self->tsap);
901 DEXIT(IRDA_SERV_TRACE, "\n");
905 /*------------------------------------------------------------------*/
907 * Function irda_setup_server (self)
909 * Create a IrTTP server and set it up...
911 * Register the IrLAN hint bit, create a IrTTP instance for us,
912 * set all the IrTTP callbacks and create an IrIAS entry...
915 irnet_setup_server(void)
919 DENTER(IRDA_SERV_TRACE, "()\n");
921 /* Initialise the regular socket part of the server */
922 irda_irnet_create(&irnet_server.s);
924 /* Open a local TSAP (an IrTTP instance) for the server */
925 irnet_open_tsap(&irnet_server.s);
928 irnet_server.s.ppp_open = 0;
929 irnet_server.s.chan.private = NULL;
930 irnet_server.s.file = NULL;
932 /* Get the hint bit corresponding to IrLAN */
933 /* Note : we overload the IrLAN hint bit. As it is only a "hint", and as
934 * we provide roughly the same functionality as IrLAN, this is ok.
935 * In fact, the situation is similar as JetSend overloading the Obex hint
937 hints = irlmp_service_to_hint(S_LAN);
939 #ifdef ADVERTISE_HINT
940 /* Register with IrLMP as a service (advertise our hint bit) */
941 irnet_server.skey = irlmp_register_service(hints);
942 #endif /* ADVERTISE_HINT */
944 /* Register with LM-IAS (so that people can connect to us) */
945 irnet_server.ias_obj = irias_new_object(IRNET_SERVICE_NAME, jiffies);
946 irias_add_integer_attrib(irnet_server.ias_obj, IRNET_IAS_VALUE,
947 irnet_server.s.stsap_sel, IAS_KERNEL_ATTR);
948 irias_insert_object(irnet_server.ias_obj);
950 #ifdef DISCOVERY_EVENTS
951 /* Tell IrLMP we want to be notified of newly discovered nodes */
952 irlmp_update_client(irnet_server.s.ckey, hints,
953 irnet_discovery_indication, irnet_expiry_indication,
954 (void *) &irnet_server.s);
957 DEXIT(IRDA_SERV_TRACE, " - self=0x%X\n", (unsigned int) &irnet_server.s);
961 /*------------------------------------------------------------------*/
963 * Function irda_destroy_server (self)
965 * Destroy the IrTTP server...
967 * Reverse of the previous function...
970 irnet_destroy_server(void)
972 DENTER(IRDA_SERV_TRACE, "()\n");
974 #ifdef ADVERTISE_HINT
975 /* Unregister with IrLMP */
976 irlmp_unregister_service(irnet_server.skey);
977 #endif /* ADVERTISE_HINT */
979 /* Unregister with LM-IAS */
980 if(irnet_server.ias_obj)
981 irias_delete_object(irnet_server.ias_obj);
983 /* Cleanup the socket part */
984 irda_irnet_destroy(&irnet_server.s);
986 DEXIT(IRDA_SERV_TRACE, "\n");
991 /************************ IRDA-TTP CALLBACKS ************************/
993 * When we create a IrTTP instance, we pass to it a set of callbacks
994 * that IrTTP will call in case of various events.
995 * We take care of those events here.
998 /*------------------------------------------------------------------*/
1000 * Function irnet_data_indication (instance, sap, skb)
1002 * Received some data from TinyTP. Just queue it on the receive queue
1006 irnet_data_indication(void * instance,
1008 struct sk_buff *skb)
1010 irnet_socket * ap = (irnet_socket *) instance;
1014 DENTER(IRDA_TCB_TRACE, "(self/ap=0x%X, skb=0x%X)\n",
1015 (unsigned int) ap,(unsigned int) skb);
1016 DASSERT(skb != NULL, 0, IRDA_CB_ERROR, "skb is NULL !!!\n");
1018 /* Check is ppp is ready to receive our packet */
1021 DERROR(IRDA_CB_ERROR, "PPP not ready, dropping packet...\n");
1022 /* When we return error, TTP will need to requeue the skb and
1023 * will stop the sender. IrTTP will stall until we send it a
1024 * flow control request... */
1028 /* strip address/control field if present */
1030 if((p[0] == PPP_ALLSTATIONS) && (p[1] == PPP_UI))
1032 /* chop off address/control */
1035 p = skb_pull(skb, 2);
1038 /* decompress protocol field if compressed */
1041 /* protocol is compressed */
1042 skb_push(skb, 1)[0] = 0;
1048 /* pass to generic ppp layer */
1049 /* Note : how do I know if ppp can accept or not the packet ? This is
1050 * essential if I want to manage flow control smoothly... */
1051 ppp_input(&ap->chan, skb);
1053 DEXIT(IRDA_TCB_TRACE, "\n");
1057 DERROR(IRDA_CB_ERROR, "Packet too small, dropping...\n");
1059 ppp_input_error(&ap->chan, code);
1060 return 0; /* Don't return an error code, only for flow control... */
1063 /*------------------------------------------------------------------*/
1065 * Function irnet_disconnect_indication (instance, sap, reason, skb)
1067 * Connection has been closed. Chech reason to find out why
1069 * Note : there are many cases where we come here :
1070 * o attempted to connect, timeout
1071 * o connected, link is broken, LAP has timeout
1072 * o connected, other side close the link
1073 * o connection request on the server not handled
1076 irnet_disconnect_indication(void * instance,
1079 struct sk_buff *skb)
1081 irnet_socket * self = (irnet_socket *) instance;
1085 DENTER(IRDA_TCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1086 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1088 /* Don't care about it, but let's not leak it */
1092 /* Prevent higher layer from accessing IrTTP */
1093 test_open = test_and_clear_bit(0, &self->ttp_open);
1094 /* Not connecting anymore...
1095 * (note : TSAP is open, so IAP callbacks are no longer pending...) */
1096 test_connect = test_and_clear_bit(0, &self->ttp_connect);
1098 /* If both self->ttp_open and self->ttp_connect are NULL, it mean that we
1099 * have a race condition with irda_irnet_destroy() or
1100 * irnet_connect_indication(), so don't mess up tsap...
1102 if(!(test_open || test_connect))
1104 DERROR(IRDA_CB_ERROR, "Race condition detected...\n");
1108 /* If we were active, notify the control channel */
1110 irnet_post_event(self, IRNET_DISCONNECT_FROM,
1111 self->saddr, self->daddr, self->rname);
1113 /* If we were trying to connect, notify the control channel */
1114 if((self->tsap) && (self != &irnet_server.s))
1115 irnet_post_event(self, IRNET_NOANSWER_FROM,
1116 self->saddr, self->daddr, self->rname);
1118 /* Close our IrTTP connection, cleanup tsap */
1119 if((self->tsap) && (self != &irnet_server.s))
1121 DEBUG(IRDA_CB_INFO, "Closing our TTP connection.\n");
1122 irttp_close_tsap(self->tsap);
1125 /* Cleanup the socket in case we want to reconnect in ppp_output_wakeup() */
1126 self->stsap_sel = 0;
1127 self->daddr = DEV_ADDR_ANY;
1128 self->tx_flow = FLOW_START;
1130 /* Deal with the ppp instance if it's still alive */
1135 /* If we were connected, cleanup & close the PPP channel,
1136 * which will kill pppd (hangup) and the rest */
1137 ppp_unregister_channel(&self->chan);
1142 /* If we were trying to connect, flush (drain) ppp_generic
1143 * Tx queue (most often we have blocked it), which will
1144 * trigger an other attempt to connect. If we are passive,
1145 * this will empty the Tx queue after last try. */
1146 ppp_output_wakeup(&self->chan);
1150 DEXIT(IRDA_TCB_TRACE, "\n");
1153 /*------------------------------------------------------------------*/
1155 * Function irnet_connect_confirm (instance, sap, qos, max_sdu_size, skb)
1157 * Connections has been confirmed by the remote device
1161 irnet_connect_confirm(void * instance,
1163 struct qos_info *qos,
1165 __u8 max_header_size,
1166 struct sk_buff *skb)
1168 irnet_socket * self = (irnet_socket *) instance;
1170 DENTER(IRDA_TCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1172 /* Check if socket is closing down (via irda_irnet_destroy()) */
1173 if(! test_bit(0, &self->ttp_connect))
1175 DERROR(IRDA_CB_ERROR, "Socket no longer connecting. Ouch !\n");
1179 /* How much header space do we need to reserve */
1180 self->max_header_size = max_header_size;
1182 /* IrTTP max SDU size in transmit direction */
1183 self->max_sdu_size_tx = max_sdu_size;
1184 self->max_data_size = max_sdu_size;
1185 #ifdef STREAM_COMPAT
1186 if(max_sdu_size == 0)
1187 self->max_data_size = irttp_get_max_seg_size(self->tsap);
1188 #endif /* STREAM_COMPAT */
1190 /* At this point, IrLMP has assigned our source address */
1191 self->saddr = irttp_get_saddr(self->tsap);
1193 /* Allow higher layer to access IrTTP */
1194 set_bit(0, &self->ttp_open);
1195 clear_bit(0, &self->ttp_connect); /* Not racy, IrDA traffic is serial */
1196 /* Give a kick in the ass of ppp_generic so that he sends us some data */
1197 ppp_output_wakeup(&self->chan);
1199 /* Check size of received packet */
1202 #ifdef PASS_CONNECT_PACKETS
1203 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1204 /* Try to pass it to PPP */
1205 irnet_data_indication(instance, sap, skb);
1206 #else /* PASS_CONNECT_PACKETS */
1207 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1208 kfree_skb(skb); /* Note : will be optimised with other kfree... */
1209 #endif /* PASS_CONNECT_PACKETS */
1214 /* Notify the control channel */
1215 irnet_post_event(self, IRNET_CONNECT_TO,
1216 self->saddr, self->daddr, self->rname);
1218 DEXIT(IRDA_TCB_TRACE, "\n");
1221 /*------------------------------------------------------------------*/
1223 * Function irnet_flow_indication (instance, sap, flow)
1225 * Used by TinyTP to tell us if it can accept more data or not
1229 irnet_flow_indication(void * instance,
1233 irnet_socket * self = (irnet_socket *) instance;
1234 LOCAL_FLOW oldflow = self->tx_flow;
1236 DENTER(IRDA_TCB_TRACE, "(self=0x%X, flow=%d)\n", (unsigned int) self, flow);
1238 /* Update our state */
1239 self->tx_flow = flow;
1241 /* Check what IrTTP want us to do... */
1245 DEBUG(IRDA_CB_INFO, "IrTTP wants us to start again\n");
1246 /* Check if we really need to wake up PPP */
1247 if(oldflow == FLOW_STOP)
1248 ppp_output_wakeup(&self->chan);
1250 DEBUG(IRDA_CB_INFO, "But we were already transmitting !!!\n");
1253 DEBUG(IRDA_CB_INFO, "IrTTP wants us to slow down\n");
1256 DEBUG(IRDA_CB_INFO, "Unknown flow command!\n");
1260 DEXIT(IRDA_TCB_TRACE, "\n");
1263 /*------------------------------------------------------------------*/
1265 * Function irnet_status_indication (instance, sap, reason, skb)
1267 * Link (IrLAP) status report.
1271 irnet_status_indication(void * instance,
1275 irnet_socket * self = (irnet_socket *) instance;
1277 DENTER(IRDA_TCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1278 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1280 /* We can only get this event if we are connected */
1283 case STATUS_NO_ACTIVITY:
1284 irnet_post_event(self, IRNET_BLOCKED_LINK,
1285 self->saddr, self->daddr, self->rname);
1288 DEBUG(IRDA_CB_INFO, "Unknown status...\n");
1291 DEXIT(IRDA_TCB_TRACE, "\n");
1294 /*------------------------------------------------------------------*/
1296 * Function irnet_connect_indication(instance, sap, qos, max_sdu_size, userdata)
1298 * Incoming connection
1300 * In theory, this function is called only on the server socket.
1301 * Some other node is attempting to connect to the IrNET service, and has
1302 * sent a connection request on our server socket.
1303 * We just redirect the connection to the relevant IrNET socket.
1305 * Note : we also make sure that between 2 irnet nodes, there can
1306 * exist only one irnet connection.
1309 irnet_connect_indication(void * instance,
1311 struct qos_info *qos,
1313 __u8 max_header_size,
1314 struct sk_buff *skb)
1316 irnet_socket * server = &irnet_server.s;
1317 irnet_socket * new = (irnet_socket *) NULL;
1319 DENTER(IRDA_TCB_TRACE, "(server=0x%X)\n", (unsigned int) server);
1320 DASSERT(instance == &irnet_server, , IRDA_CB_ERROR,
1321 "Invalid instance (0x%X) !!!\n", (unsigned int) instance);
1322 DASSERT(sap == irnet_server.s.tsap, , IRDA_CB_ERROR, "Invalid sap !!!\n");
1324 /* Try to find the most appropriate IrNET socket */
1325 new = irnet_find_socket(server);
1327 /* After all this hard work, do we have an socket ? */
1328 if(new == (irnet_socket *) NULL)
1330 DEXIT(IRDA_CB_INFO, ": No socket waiting for this connection.\n");
1331 irnet_disconnect_server(server, skb);
1335 /* Is the socket already busy ? */
1336 if(test_bit(0, &new->ttp_open))
1338 DEXIT(IRDA_CB_INFO, ": Socket already connected.\n");
1339 irnet_disconnect_server(server, skb);
1343 /* The following code is a bit tricky, so need comments ;-)
1345 /* If ttp_connect is set, the socket is trying to connect to the other
1346 * end and may have sent a IrTTP connection request and is waiting for
1347 * a connection response (that may never come).
1348 * Now, the pain is that the socket may have opened a tsap and is
1349 * waiting on it, while the other end is trying to connect to it on
1351 * Because IrNET can be peer to peer, we need to workaround this.
1352 * Furthermore, the way the irnetd script is implemented, the
1353 * target will create a second IrNET connection back to the
1354 * originator and expect the originator to bind this new connection
1355 * to the original PPPD instance.
1356 * And of course, if we don't use irnetd, we can have a race when
1357 * both side try to connect simultaneously, which could leave both
1358 * connections half closed (yuck).
1360 * 1) The "originator" must accept the new connection and get rid
1361 * of the old one so that irnetd works
1362 * 2) One side must deny the new connection to avoid races,
1363 * but both side must agree on which side it is...
1364 * Most often, the originator is primary at the LAP layer.
1367 /* Now, let's look at the way I wrote the test...
1368 * We need to clear up the ttp_connect flag atomically to prevent
1369 * irnet_disconnect_indication() to mess up the tsap we are going to close.
1370 * We want to clear the ttp_connect flag only if we close the tsap,
1371 * otherwise we will never close it, so we need to check for primary
1372 * *before* doing the test on the flag.
1373 * And of course, ALLOW_SIMULT_CONNECT can disable this entirely...
1377 /* Socket already connecting ? On primary ? */
1379 #ifdef ALLOW_SIMULT_CONNECT
1380 || ((irttp_is_primary(server->tsap) == 1) /* primary */
1381 && (test_and_clear_bit(0, &new->ttp_connect)))
1382 #endif /* ALLOW_SIMULT_CONNECT */
1385 DERROR(IRDA_CB_ERROR, "Socket already connecting, but going to reuse it !\n");
1387 /* Cleanup the old TSAP if necessary - IrIAP will be cleaned up later */
1388 if(new->tsap != NULL)
1390 /* Close the old connection the new socket was attempting,
1391 * so that we can hook it up to the new connection.
1392 * It's now safe to do it... */
1393 irttp_close_tsap(new->tsap);
1400 * 1) socket was not connecting or connected : ttp_connect should be 0.
1401 * 2) we don't want to connect the socket because we are secondary or
1402 * ALLOW_SIMULT_CONNECT is undefined. ttp_connect should be 1.
1403 * 3) we are half way in irnet_disconnect_indication(), and it's a
1404 * nice race condition... Fortunately, we can detect that by checking
1405 * if tsap is still alive. On the other hand, we can't be in
1406 * irda_irnet_destroy() otherwise we would not have found this
1407 * socket in the hashbin.
1409 if((test_bit(0, &new->ttp_connect)) || (new->tsap != NULL))
1411 /* Don't mess this socket, somebody else in in charge... */
1412 DERROR(IRDA_CB_ERROR, "Race condition detected, socket in use, abort connect...\n");
1413 irnet_disconnect_server(server, skb);
1418 /* So : at this point, we have a socket, and it is idle. Good ! */
1419 irnet_connect_socket(server, new, qos, max_sdu_size, max_header_size);
1421 /* Check size of received packet */
1424 #ifdef PASS_CONNECT_PACKETS
1425 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1426 /* Try to pass it to PPP */
1427 irnet_data_indication(new, new->tsap, skb);
1428 #else /* PASS_CONNECT_PACKETS */
1429 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1430 kfree_skb(skb); /* Note : will be optimised with other kfree... */
1431 #endif /* PASS_CONNECT_PACKETS */
1436 DEXIT(IRDA_TCB_TRACE, "\n");
1440 /********************** IRDA-IAS/LMP CALLBACKS **********************/
1442 * These are the callbacks called by other layers of the IrDA stack,
1443 * mainly LMP for discovery and IAS for name queries.
1446 /*------------------------------------------------------------------*/
1448 * Function irnet_getvalue_confirm (result, obj_id, value, priv)
1450 * Got answer from remote LM-IAS, just connect
1452 * This is the reply to a IAS query we were doing to find the TSAP of
1453 * the device we want to connect to.
1454 * If we have found a valid TSAP, just initiate the TTP connection
1458 irnet_getvalue_confirm(int result,
1460 struct ias_value *value,
1463 irnet_socket * self = (irnet_socket *) priv;
1465 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1466 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1468 /* Check if already connected (via irnet_connect_socket())
1469 * or socket is closing down (via irda_irnet_destroy()) */
1470 if(! test_bit(0, &self->ttp_connect))
1472 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1476 /* We probably don't need to make any more queries */
1477 iriap_close(self->iriap);
1480 /* Post process the IAS reply */
1481 self->dtsap_sel = irnet_ias_to_tsap(self, result, value);
1483 /* If error, just go out */
1486 clear_bit(0, &self->ttp_connect);
1487 DERROR(IRDA_OCB_ERROR, "IAS connect failed ! (0x%X)\n", self->errno);
1491 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1492 self->daddr, self->dtsap_sel);
1494 /* Start up TTP - non blocking */
1495 irnet_connect_tsap(self);
1497 DEXIT(IRDA_OCB_TRACE, "\n");
1500 /*------------------------------------------------------------------*/
1502 * Function irnet_discovervalue_confirm (result, obj_id, value, priv)
1504 * Handle the TSAP discovery procedure state machine.
1505 * Got answer from remote LM-IAS, try next device
1507 * We are doing a TSAP discovery procedure, and we got an answer to
1508 * a IAS query we were doing to find the TSAP on one of the address
1509 * in the discovery log.
1511 * If we have found a valid TSAP for the first time, save it. If it's
1512 * not the first time we found one, complain.
1514 * If we have more addresses in the log, just initiate a new query.
1515 * Note that those query may fail (see irnet_discover_daddr_and_lsap_sel())
1517 * Otherwise, wrap up the procedure (cleanup), check if we have found
1518 * any device and connect to it.
1521 irnet_discovervalue_confirm(int result,
1523 struct ias_value *value,
1526 irnet_socket * self = (irnet_socket *) priv;
1527 __u8 dtsap_sel; /* TSAP we are looking for */
1529 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1530 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1532 /* Check if already connected (via irnet_connect_socket())
1533 * or socket is closing down (via irda_irnet_destroy()) */
1534 if(! test_bit(0, &self->ttp_connect))
1536 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1540 /* Post process the IAS reply */
1541 dtsap_sel = irnet_ias_to_tsap(self, result, value);
1543 /* Have we got something ? */
1544 if(self->errno == 0)
1546 /* We found the requested service */
1547 if(self->daddr != DEV_ADDR_ANY)
1549 DERROR(IRDA_OCB_ERROR, "More than one device in range supports IrNET...\n");
1553 /* First time we found that one, save it ! */
1554 self->daddr = self->discoveries[self->disco_index].daddr;
1555 self->dtsap_sel = dtsap_sel;
1560 if((self->errno == -EADDRNOTAVAIL) || (self->errno == 0))
1564 /* Search the next node */
1565 ret = irnet_discover_next_daddr(self);
1568 /* In this case, the above request was non-blocking.
1569 * We will return here after a while... */
1572 /* In this case, we have processed the last discovery item */
1575 /* No more queries to be done (failure or last one) */
1577 /* We probably don't need to make any more queries */
1578 iriap_close(self->iriap);
1581 /* No more items : remove the log and signal termination */
1582 DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%X)\n",
1583 (unsigned int) self->discoveries);
1584 if(self->discoveries != NULL)
1586 /* Cleanup our copy of the discovery log */
1587 kfree(self->discoveries);
1588 self->discoveries = NULL;
1590 self->disco_number = -1;
1592 /* Check out what we found */
1593 if(self->daddr == DEV_ADDR_ANY)
1595 self->daddr = DEV_ADDR_ANY;
1596 clear_bit(0, &self->ttp_connect);
1597 DEXIT(IRDA_OCB_TRACE, ": cannot discover IrNET in any device !!!\n");
1601 /* We have a valid address - just connect */
1603 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1604 self->daddr, self->dtsap_sel);
1606 /* Start up TTP - non blocking */
1607 irnet_connect_tsap(self);
1609 DEXIT(IRDA_OCB_TRACE, "\n");
1612 #ifdef DISCOVERY_EVENTS
1613 /*------------------------------------------------------------------*/
1615 * Function irnet_discovery_indication (discovery)
1617 * Got a discovery indication from IrLMP, post an event
1619 * Note : IrLMP take care of matching the hint mask for us, we only
1620 * check if it is a "new" node...
1622 * As IrLMP filter on the IrLAN hint bit, we get both IrLAN and IrNET
1623 * nodes, so it's only at connection time that we will know if the
1624 * node support IrNET, IrLAN or both. The other solution is to check
1625 * in IAS the PNP ids and service name.
1626 * Note : even if a node support IrNET (or IrLAN), it's no guarantee
1627 * that we will be able to connect to it, the node might already be
1630 * One last thing : in some case, this function will trigger duplicate
1631 * discovery events. On the other hand, we should catch all
1632 * discoveries properly (i.e. not miss one). Filtering duplicate here
1633 * is to messy, so we leave that to user space...
1636 irnet_discovery_indication(discovery_t * discovery,
1637 DISCOVERY_MODE mode,
1640 irnet_socket * self = &irnet_server.s;
1642 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1643 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1644 "Invalid instance (0x%X) !!!\n", (unsigned int) priv);
1646 /* Check if node is discovered is a new one or an old one.
1647 * We check when how long ago this node was discovered, with a
1648 * coarse timeout (we may miss some discovery events or be delayed).
1650 if((jiffies - discovery->first_timestamp) >= (sysctl_discovery_timeout * HZ))
1652 return; /* Too old, not interesting -> goodbye */
1655 DEBUG(IRDA_OCB_INFO, "Discovered new IrNET/IrLAN node %s...\n",
1656 discovery->nickname);
1658 /* Notify the control channel */
1659 irnet_post_event(NULL, IRNET_DISCOVER,
1660 discovery->saddr, discovery->daddr, discovery->nickname);
1662 DEXIT(IRDA_OCB_TRACE, "\n");
1665 /*------------------------------------------------------------------*/
1667 * Function irnet_expiry_indication (expiry)
1669 * Got a expiry indication from IrLMP, post an event
1671 * Note : IrLMP take care of matching the hint mask for us, we only
1672 * check if it is a "new" node...
1675 irnet_expiry_indication(discovery_t * expiry,
1676 DISCOVERY_MODE mode,
1679 irnet_socket * self = &irnet_server.s;
1681 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1682 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1683 "Invalid instance (0x%X) !!!\n", (unsigned int) priv);
1685 DEBUG(IRDA_OCB_INFO, "IrNET/IrLAN node %s expired...\n",
1688 /* Notify the control channel */
1689 irnet_post_event(NULL, IRNET_EXPIRE,
1690 expiry->saddr, expiry->daddr, expiry->nickname);
1692 DEXIT(IRDA_OCB_TRACE, "\n");
1694 #endif /* DISCOVERY_EVENTS */
1697 /*********************** PROC ENTRY CALLBACKS ***********************/
1699 * We create a instance in the /proc filesystem, and here we take care
1703 #ifdef CONFIG_PROC_FS
1704 /*------------------------------------------------------------------*/
1706 * Function irnet_proc_read (buf, start, offset, len, unused)
1708 * Give some info to the /proc file system
1711 irnet_proc_read(char * buf,
1716 irnet_socket * self;
1718 unsigned long flags;
1723 /* Get the IrNET server information... */
1724 len += sprintf(buf+len, "IrNET server - ");
1725 len += sprintf(buf+len, "IrDA state: %s, ",
1726 (irnet_server.running ? "running" : "dead"));
1727 len += sprintf(buf+len, "stsap_sel: %02x, ", irnet_server.s.stsap_sel);
1728 len += sprintf(buf+len, "dtsap_sel: %02x\n", irnet_server.s.dtsap_sel);
1730 /* Do we need to continue ? */
1731 if(!irnet_server.running)
1734 /* Protect access to the instance list */
1735 spin_lock_irqsave(&irnet_server.spinlock, flags);
1737 /* Get the sockets one by one... */
1738 self = (irnet_socket *) hashbin_get_first(irnet_server.list);
1741 /* Start printing info about the socket. */
1742 len += sprintf(buf+len, "\nIrNET socket %d - ", i++);
1744 /* First, get the requested configuration */
1745 len += sprintf(buf+len, "Requested IrDA name: \"%s\", ", self->rname);
1746 len += sprintf(buf+len, "daddr: %08x, ", self->rdaddr);
1747 len += sprintf(buf+len, "saddr: %08x\n", self->rsaddr);
1749 /* Second, get all the PPP info */
1750 len += sprintf(buf+len, " PPP state: %s",
1751 (self->ppp_open ? "registered" : "unregistered"));
1754 len += sprintf(buf+len, ", unit: ppp%d",
1755 ppp_unit_number(&self->chan));
1756 len += sprintf(buf+len, ", channel: %d",
1757 ppp_channel_index(&self->chan));
1758 len += sprintf(buf+len, ", mru: %d",
1760 /* Maybe add self->flags ? Later... */
1763 /* Then, get all the IrDA specific info... */
1765 state = "connected";
1767 if(self->tsap != NULL)
1768 state = "connecting";
1770 if(self->iriap != NULL)
1771 state = "searching";
1773 if(self->ttp_connect)
1777 len += sprintf(buf+len, "\n IrDA state: %s, ", state);
1778 len += sprintf(buf+len, "daddr: %08x, ", self->daddr);
1779 len += sprintf(buf+len, "stsap_sel: %02x, ", self->stsap_sel);
1780 len += sprintf(buf+len, "dtsap_sel: %02x\n", self->dtsap_sel);
1782 /* Next socket, please... */
1783 self = (irnet_socket *) hashbin_get_next(irnet_server.list);
1787 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
1791 #endif /* PROC_FS */
1794 /********************** CONFIGURATION/CLEANUP **********************/
1796 * Initialisation and teardown of the IrDA part, called at module
1797 * insertion and removal...
1800 /*------------------------------------------------------------------*/
1802 * Prepare the IrNET layer for operation...
1805 irda_irnet_init(void)
1809 DENTER(MODULE_TRACE, "()\n");
1811 /* Pure paranoia - should be redundant */
1812 memset(&irnet_server, 0, sizeof(struct irnet_root));
1814 /* Setup start of irnet instance list */
1815 irnet_server.list = hashbin_new(HB_NOLOCK);
1816 DABORT(irnet_server.list == NULL, -ENOMEM,
1817 MODULE_ERROR, "Can't allocate hashbin!\n");
1818 /* Init spinlock for instance list */
1819 spin_lock_init(&irnet_server.spinlock);
1821 /* Initialise control channel */
1822 init_waitqueue_head(&irnet_events.rwait);
1823 irnet_events.index = 0;
1824 /* Init spinlock for event logging */
1825 spin_lock_init(&irnet_events.spinlock);
1827 #ifdef CONFIG_PROC_FS
1828 /* Add a /proc file for irnet infos */
1829 create_proc_info_entry("irnet", 0, proc_irda, irnet_proc_read);
1830 #endif /* CONFIG_PROC_FS */
1832 /* Setup the IrNET server */
1833 err = irnet_setup_server();
1836 /* We are no longer functional... */
1837 irnet_server.running = 1;
1839 DEXIT(MODULE_TRACE, "\n");
1843 /*------------------------------------------------------------------*/
1845 * Cleanup at exit...
1848 irda_irnet_cleanup(void)
1850 DENTER(MODULE_TRACE, "()\n");
1852 /* We are no longer there... */
1853 irnet_server.running = 0;
1855 #ifdef CONFIG_PROC_FS
1856 /* Remove our /proc file */
1857 remove_proc_entry("irnet", proc_irda);
1858 #endif /* CONFIG_PROC_FS */
1860 /* Remove our IrNET server from existence */
1861 irnet_destroy_server();
1863 /* Remove all instances of IrNET socket still present */
1864 hashbin_delete(irnet_server.list, (FREE_FUNC) irda_irnet_destroy);
1866 DEXIT(MODULE_TRACE, "\n");