Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / net / irda / ircomm / ircomm_tty_attach.c
1 /*********************************************************************
2  *
3  * Filename:      ircomm_tty_attach.c
4  * Version:
5  * Description:   Code for attaching the serial driver to IrCOMM
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sat Jun  5 17:42:00 1999
9  * Modified at:   Tue Jan  4 14:20:49 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  *
12  *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
13  *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public License
26  *     along with this program; if not, see <http://www.gnu.org/licenses/>.
27  *
28  ********************************************************************/
29
30 #include <linux/init.h>
31 #include <linux/sched.h>
32
33 #include <net/irda/irda.h>
34 #include <net/irda/irlmp.h>
35 #include <net/irda/iriap.h>
36 #include <net/irda/irttp.h>
37 #include <net/irda/irias_object.h>
38 #include <net/irda/parameters.h>
39
40 #include <net/irda/ircomm_core.h>
41 #include <net/irda/ircomm_param.h>
42 #include <net/irda/ircomm_event.h>
43
44 #include <net/irda/ircomm_tty.h>
45 #include <net/irda/ircomm_tty_attach.h>
46
47 static void ircomm_tty_ias_register(struct ircomm_tty_cb *self);
48 static void ircomm_tty_discovery_indication(discinfo_t *discovery,
49                                             DISCOVERY_MODE mode,
50                                             void *priv);
51 static void ircomm_tty_getvalue_confirm(int result, __u16 obj_id,
52                                         struct ias_value *value, void *priv);
53 static void ircomm_tty_start_watchdog_timer(struct ircomm_tty_cb *self,
54                                             int timeout);
55 static void ircomm_tty_watchdog_timer_expired(void *data);
56
57 static int ircomm_tty_state_idle(struct ircomm_tty_cb *self,
58                                  IRCOMM_TTY_EVENT event,
59                                  struct sk_buff *skb,
60                                  struct ircomm_tty_info *info);
61 static int ircomm_tty_state_search(struct ircomm_tty_cb *self,
62                                    IRCOMM_TTY_EVENT event,
63                                    struct sk_buff *skb,
64                                    struct ircomm_tty_info *info);
65 static int ircomm_tty_state_query_parameters(struct ircomm_tty_cb *self,
66                                              IRCOMM_TTY_EVENT event,
67                                              struct sk_buff *skb,
68                                              struct ircomm_tty_info *info);
69 static int ircomm_tty_state_query_lsap_sel(struct ircomm_tty_cb *self,
70                                            IRCOMM_TTY_EVENT event,
71                                            struct sk_buff *skb,
72                                            struct ircomm_tty_info *info);
73 static int ircomm_tty_state_setup(struct ircomm_tty_cb *self,
74                                   IRCOMM_TTY_EVENT event,
75                                   struct sk_buff *skb,
76                                   struct ircomm_tty_info *info);
77 static int ircomm_tty_state_ready(struct ircomm_tty_cb *self,
78                                   IRCOMM_TTY_EVENT event,
79                                   struct sk_buff *skb,
80                                   struct ircomm_tty_info *info);
81
82 const char *const ircomm_tty_state[] = {
83         "IRCOMM_TTY_IDLE",
84         "IRCOMM_TTY_SEARCH",
85         "IRCOMM_TTY_QUERY_PARAMETERS",
86         "IRCOMM_TTY_QUERY_LSAP_SEL",
87         "IRCOMM_TTY_SETUP",
88         "IRCOMM_TTY_READY",
89         "*** ERROR *** ",
90 };
91
92 #ifdef CONFIG_IRDA_DEBUG
93 static const char *const ircomm_tty_event[] = {
94         "IRCOMM_TTY_ATTACH_CABLE",
95         "IRCOMM_TTY_DETACH_CABLE",
96         "IRCOMM_TTY_DATA_REQUEST",
97         "IRCOMM_TTY_DATA_INDICATION",
98         "IRCOMM_TTY_DISCOVERY_REQUEST",
99         "IRCOMM_TTY_DISCOVERY_INDICATION",
100         "IRCOMM_TTY_CONNECT_CONFIRM",
101         "IRCOMM_TTY_CONNECT_INDICATION",
102         "IRCOMM_TTY_DISCONNECT_REQUEST",
103         "IRCOMM_TTY_DISCONNECT_INDICATION",
104         "IRCOMM_TTY_WD_TIMER_EXPIRED",
105         "IRCOMM_TTY_GOT_PARAMETERS",
106         "IRCOMM_TTY_GOT_LSAPSEL",
107         "*** ERROR ****",
108 };
109 #endif /* CONFIG_IRDA_DEBUG */
110
111 static int (*state[])(struct ircomm_tty_cb *self, IRCOMM_TTY_EVENT event,
112                       struct sk_buff *skb, struct ircomm_tty_info *info) =
113 {
114         ircomm_tty_state_idle,
115         ircomm_tty_state_search,
116         ircomm_tty_state_query_parameters,
117         ircomm_tty_state_query_lsap_sel,
118         ircomm_tty_state_setup,
119         ircomm_tty_state_ready,
120 };
121
122 /*
123  * Function ircomm_tty_attach_cable (driver)
124  *
125  *    Try to attach cable (IrCOMM link). This function will only return
126  *    when the link has been connected, or if an error condition occurs.
127  *    If success, the return value is the resulting service type.
128  */
129 int ircomm_tty_attach_cable(struct ircomm_tty_cb *self)
130 {
131         struct tty_struct *tty;
132
133         IRDA_DEBUG(0, "%s()\n", __func__ );
134
135         IRDA_ASSERT(self != NULL, return -1;);
136         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
137
138         /* Check if somebody has already connected to us */
139         if (ircomm_is_connected(self->ircomm)) {
140                 IRDA_DEBUG(0, "%s(), already connected!\n", __func__ );
141                 return 0;
142         }
143
144         /* Make sure nobody tries to write before the link is up */
145         tty = tty_port_tty_get(&self->port);
146         if (tty) {
147                 tty->hw_stopped = 1;
148                 tty_kref_put(tty);
149         }
150
151         ircomm_tty_ias_register(self);
152
153         ircomm_tty_do_event(self, IRCOMM_TTY_ATTACH_CABLE, NULL, NULL);
154
155         return 0;
156 }
157
158 /*
159  * Function ircomm_detach_cable (driver)
160  *
161  *    Detach cable, or cable has been detached by peer
162  *
163  */
164 void ircomm_tty_detach_cable(struct ircomm_tty_cb *self)
165 {
166         IRDA_DEBUG(0, "%s()\n", __func__ );
167
168         IRDA_ASSERT(self != NULL, return;);
169         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
170
171         del_timer(&self->watchdog_timer);
172
173         /* Remove discovery handler */
174         if (self->ckey) {
175                 irlmp_unregister_client(self->ckey);
176                 self->ckey = NULL;
177         }
178         /* Remove IrCOMM hint bits */
179         if (self->skey) {
180                 irlmp_unregister_service(self->skey);
181                 self->skey = NULL;
182         }
183
184         if (self->iriap) {
185                 iriap_close(self->iriap);
186                 self->iriap = NULL;
187         }
188
189         /* Remove LM-IAS object */
190         if (self->obj) {
191                 irias_delete_object(self->obj);
192                 self->obj = NULL;
193         }
194
195         ircomm_tty_do_event(self, IRCOMM_TTY_DETACH_CABLE, NULL, NULL);
196
197         /* Reset some values */
198         self->daddr = self->saddr = 0;
199         self->dlsap_sel = self->slsap_sel = 0;
200
201         memset(&self->settings, 0, sizeof(struct ircomm_params));
202 }
203
204 /*
205  * Function ircomm_tty_ias_register (self)
206  *
207  *    Register with LM-IAS depending on which service type we are
208  *
209  */
210 static void ircomm_tty_ias_register(struct ircomm_tty_cb *self)
211 {
212         __u8 oct_seq[6];
213         __u16 hints;
214
215         IRDA_DEBUG(0, "%s()\n", __func__ );
216
217         IRDA_ASSERT(self != NULL, return;);
218         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
219
220         /* Compute hint bits based on service */
221         hints = irlmp_service_to_hint(S_COMM);
222         if (self->service_type & IRCOMM_3_WIRE_RAW)
223                 hints |= irlmp_service_to_hint(S_PRINTER);
224
225         /* Advertise IrCOMM hint bit in discovery */
226         if (!self->skey)
227                 self->skey = irlmp_register_service(hints);
228         /* Set up a discovery handler */
229         if (!self->ckey)
230                 self->ckey = irlmp_register_client(hints,
231                                                    ircomm_tty_discovery_indication,
232                                                    NULL, (void *) self);
233
234         /* If already done, no need to do it again */
235         if (self->obj)
236                 return;
237
238         if (self->service_type & IRCOMM_3_WIRE_RAW) {
239                 /* Register IrLPT with LM-IAS */
240                 self->obj = irias_new_object("IrLPT", IAS_IRLPT_ID);
241                 irias_add_integer_attrib(self->obj, "IrDA:IrLMP:LsapSel",
242                                          self->slsap_sel, IAS_KERNEL_ATTR);
243         } else {
244                 /* Register IrCOMM with LM-IAS */
245                 self->obj = irias_new_object("IrDA:IrCOMM", IAS_IRCOMM_ID);
246                 irias_add_integer_attrib(self->obj, "IrDA:TinyTP:LsapSel",
247                                          self->slsap_sel, IAS_KERNEL_ATTR);
248
249                 /* Code the parameters into the buffer */
250                 irda_param_pack(oct_seq, "bbbbbb",
251                                 IRCOMM_SERVICE_TYPE, 1, self->service_type,
252                                 IRCOMM_PORT_TYPE,    1, IRCOMM_SERIAL);
253
254                 /* Register parameters with LM-IAS */
255                 irias_add_octseq_attrib(self->obj, "Parameters", oct_seq, 6,
256                                         IAS_KERNEL_ATTR);
257         }
258         irias_insert_object(self->obj);
259 }
260
261 /*
262  * Function ircomm_tty_ias_unregister (self)
263  *
264  *    Remove our IAS object and client hook while connected.
265  *
266  */
267 static void ircomm_tty_ias_unregister(struct ircomm_tty_cb *self)
268 {
269         /* Remove LM-IAS object now so it is not reused.
270          * IrCOMM deals very poorly with multiple incoming connections.
271          * It should looks a lot more like IrNET, and "dup" a server TSAP
272          * to the application TSAP (based on various rules).
273          * This is a cheap workaround allowing multiple clients to
274          * connect to us. It will not always work.
275          * Each IrCOMM socket has an IAS entry. Incoming connection will
276          * pick the first one found. So, when we are fully connected,
277          * we remove our IAS entries so that the next IAS entry is used.
278          * We do that for *both* client and server, because a server
279          * can also create client instances.
280          * Jean II */
281         if (self->obj) {
282                 irias_delete_object(self->obj);
283                 self->obj = NULL;
284         }
285
286 #if 0
287         /* Remove discovery handler.
288          * While we are connected, we no longer need to receive
289          * discovery events. This would be the case if there is
290          * multiple IrLAP interfaces. Jean II */
291         if (self->ckey) {
292                 irlmp_unregister_client(self->ckey);
293                 self->ckey = NULL;
294         }
295 #endif
296 }
297
298 /*
299  * Function ircomm_send_initial_parameters (self)
300  *
301  *    Send initial parameters to the remote IrCOMM device. These parameters
302  *    must be sent before any data.
303  */
304 int ircomm_tty_send_initial_parameters(struct ircomm_tty_cb *self)
305 {
306         IRDA_ASSERT(self != NULL, return -1;);
307         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
308
309         if (self->service_type & IRCOMM_3_WIRE_RAW)
310                 return 0;
311
312         /*
313          * Set default values, but only if the application for some reason
314          * haven't set them already
315          */
316         IRDA_DEBUG(2, "%s(), data-rate = %d\n", __func__ ,
317                    self->settings.data_rate);
318         if (!self->settings.data_rate)
319                 self->settings.data_rate = 9600;
320         IRDA_DEBUG(2, "%s(), data-format = %d\n", __func__ ,
321                    self->settings.data_format);
322         if (!self->settings.data_format)
323                 self->settings.data_format = IRCOMM_WSIZE_8;  /* 8N1 */
324
325         IRDA_DEBUG(2, "%s(), flow-control = %d\n", __func__ ,
326                    self->settings.flow_control);
327         /*self->settings.flow_control = IRCOMM_RTS_CTS_IN|IRCOMM_RTS_CTS_OUT;*/
328
329         /* Do not set delta values for the initial parameters */
330         self->settings.dte = IRCOMM_DTR | IRCOMM_RTS;
331
332         /* Only send service type parameter when we are the client */
333         if (self->client)
334                 ircomm_param_request(self, IRCOMM_SERVICE_TYPE, FALSE);
335         ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
336         ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
337
338         /* For a 3 wire service, we just flush the last parameter and return */
339         if (self->settings.service_type == IRCOMM_3_WIRE) {
340                 ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
341                 return 0;
342         }
343
344         /* Only 9-wire service types continue here */
345         ircomm_param_request(self, IRCOMM_FLOW_CONTROL, FALSE);
346 #if 0
347         ircomm_param_request(self, IRCOMM_XON_XOFF, FALSE);
348         ircomm_param_request(self, IRCOMM_ENQ_ACK, FALSE);
349 #endif
350         /* Notify peer that we are ready to receive data */
351         ircomm_param_request(self, IRCOMM_DTE, TRUE);
352
353         return 0;
354 }
355
356 /*
357  * Function ircomm_tty_discovery_indication (discovery)
358  *
359  *    Remote device is discovered, try query the remote IAS to see which
360  *    device it is, and which services it has.
361  *
362  */
363 static void ircomm_tty_discovery_indication(discinfo_t *discovery,
364                                             DISCOVERY_MODE mode,
365                                             void *priv)
366 {
367         struct ircomm_tty_cb *self;
368         struct ircomm_tty_info info;
369
370         IRDA_DEBUG(2, "%s()\n", __func__ );
371
372         /* Important note :
373          * We need to drop all passive discoveries.
374          * The LSAP management of IrComm is deficient and doesn't deal
375          * with the case of two instance connecting to each other
376          * simultaneously (it will deadlock in LMP).
377          * The proper fix would be to use the same technique as in IrNET,
378          * to have one server socket and separate instances for the
379          * connecting/connected socket.
380          * The workaround is to drop passive discovery, which drastically
381          * reduce the probability of this happening.
382          * Jean II */
383         if(mode == DISCOVERY_PASSIVE)
384                 return;
385
386         info.daddr = discovery->daddr;
387         info.saddr = discovery->saddr;
388
389         self = priv;
390         ircomm_tty_do_event(self, IRCOMM_TTY_DISCOVERY_INDICATION,
391                             NULL, &info);
392 }
393
394 /*
395  * Function ircomm_tty_disconnect_indication (instance, sap, reason, skb)
396  *
397  *    Link disconnected
398  *
399  */
400 void ircomm_tty_disconnect_indication(void *instance, void *sap,
401                                       LM_REASON reason,
402                                       struct sk_buff *skb)
403 {
404         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
405         struct tty_struct *tty;
406
407         IRDA_DEBUG(2, "%s()\n", __func__ );
408
409         IRDA_ASSERT(self != NULL, return;);
410         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
411
412         tty = tty_port_tty_get(&self->port);
413         if (!tty)
414                 return;
415
416         /* This will stop control data transfers */
417         self->flow = FLOW_STOP;
418
419         /* Stop data transfers */
420         tty->hw_stopped = 1;
421
422         ircomm_tty_do_event(self, IRCOMM_TTY_DISCONNECT_INDICATION, NULL,
423                             NULL);
424         tty_kref_put(tty);
425 }
426
427 /*
428  * Function ircomm_tty_getvalue_confirm (result, obj_id, value, priv)
429  *
430  *    Got result from the IAS query we make
431  *
432  */
433 static void ircomm_tty_getvalue_confirm(int result, __u16 obj_id,
434                                         struct ias_value *value,
435                                         void *priv)
436 {
437         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) priv;
438
439         IRDA_DEBUG(2, "%s()\n", __func__ );
440
441         IRDA_ASSERT(self != NULL, return;);
442         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
443
444         /* We probably don't need to make any more queries */
445         iriap_close(self->iriap);
446         self->iriap = NULL;
447
448         /* Check if request succeeded */
449         if (result != IAS_SUCCESS) {
450                 IRDA_DEBUG(4, "%s(), got NULL value!\n", __func__ );
451                 return;
452         }
453
454         switch (value->type) {
455         case IAS_OCT_SEQ:
456                 IRDA_DEBUG(2, "%s(), got octet sequence\n", __func__ );
457
458                 irda_param_extract_all(self, value->t.oct_seq, value->len,
459                                        &ircomm_param_info);
460
461                 ircomm_tty_do_event(self, IRCOMM_TTY_GOT_PARAMETERS, NULL,
462                                     NULL);
463                 break;
464         case IAS_INTEGER:
465                 /* Got LSAP selector */
466                 IRDA_DEBUG(2, "%s(), got lsapsel = %d\n", __func__ ,
467                            value->t.integer);
468
469                 if (value->t.integer == -1) {
470                         IRDA_DEBUG(0, "%s(), invalid value!\n", __func__ );
471                 } else
472                         self->dlsap_sel = value->t.integer;
473
474                 ircomm_tty_do_event(self, IRCOMM_TTY_GOT_LSAPSEL, NULL, NULL);
475                 break;
476         case IAS_MISSING:
477                 IRDA_DEBUG(0, "%s(), got IAS_MISSING\n", __func__ );
478                 break;
479         default:
480                 IRDA_DEBUG(0, "%s(), got unknown type!\n", __func__ );
481                 break;
482         }
483         irias_delete_value(value);
484 }
485
486 /*
487  * Function ircomm_tty_connect_confirm (instance, sap, qos, max_sdu_size, skb)
488  *
489  *    Connection confirmed
490  *
491  */
492 void ircomm_tty_connect_confirm(void *instance, void *sap,
493                                 struct qos_info *qos,
494                                 __u32 max_data_size,
495                                 __u8 max_header_size,
496                                 struct sk_buff *skb)
497 {
498         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
499
500         IRDA_DEBUG(2, "%s()\n", __func__ );
501
502         IRDA_ASSERT(self != NULL, return;);
503         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
504
505         self->client = TRUE;
506         self->max_data_size = max_data_size;
507         self->max_header_size = max_header_size;
508         self->flow = FLOW_START;
509
510         ircomm_tty_do_event(self, IRCOMM_TTY_CONNECT_CONFIRM, NULL, NULL);
511
512         /* No need to kfree_skb - see ircomm_ttp_connect_confirm() */
513 }
514
515 /*
516  * Function ircomm_tty_connect_indication (instance, sap, qos, max_sdu_size,
517  *                                         skb)
518  *
519  *    we are discovered and being requested to connect by remote device !
520  *
521  */
522 void ircomm_tty_connect_indication(void *instance, void *sap,
523                                    struct qos_info *qos,
524                                    __u32 max_data_size,
525                                    __u8 max_header_size,
526                                    struct sk_buff *skb)
527 {
528         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
529         int clen;
530
531         IRDA_DEBUG(2, "%s()\n", __func__ );
532
533         IRDA_ASSERT(self != NULL, return;);
534         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
535
536         self->client = FALSE;
537         self->max_data_size = max_data_size;
538         self->max_header_size = max_header_size;
539         self->flow = FLOW_START;
540
541         clen = skb->data[0];
542         if (clen)
543                 irda_param_extract_all(self, skb->data+1,
544                                        IRDA_MIN(skb->len, clen),
545                                        &ircomm_param_info);
546
547         ircomm_tty_do_event(self, IRCOMM_TTY_CONNECT_INDICATION, NULL, NULL);
548
549         /* No need to kfree_skb - see ircomm_ttp_connect_indication() */
550 }
551
552 /*
553  * Function ircomm_tty_link_established (self)
554  *
555  *    Called when the IrCOMM link is established
556  *
557  */
558 void ircomm_tty_link_established(struct ircomm_tty_cb *self)
559 {
560         struct tty_struct *tty;
561
562         IRDA_DEBUG(2, "%s()\n", __func__ );
563
564         IRDA_ASSERT(self != NULL, return;);
565         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
566
567         tty = tty_port_tty_get(&self->port);
568         if (!tty)
569                 return;
570
571         del_timer(&self->watchdog_timer);
572
573         /*
574          * IrCOMM link is now up, and if we are not using hardware
575          * flow-control, then declare the hardware as running. Otherwise we
576          * will have to wait for the peer device (DCE) to raise the CTS
577          * line.
578          */
579         if (tty_port_cts_enabled(&self->port) &&
580                         ((self->settings.dce & IRCOMM_CTS) == 0)) {
581                 IRDA_DEBUG(0, "%s(), waiting for CTS ...\n", __func__ );
582                 goto put;
583         } else {
584                 IRDA_DEBUG(1, "%s(), starting hardware!\n", __func__ );
585
586                 tty->hw_stopped = 0;
587
588                 /* Wake up processes blocked on open */
589                 wake_up_interruptible(&self->port.open_wait);
590         }
591
592         schedule_work(&self->tqueue);
593 put:
594         tty_kref_put(tty);
595 }
596
597 /*
598  * Function ircomm_tty_start_watchdog_timer (self, timeout)
599  *
600  *    Start the watchdog timer. This timer is used to make sure that any
601  *    connection attempt is successful, and if not, we will retry after
602  *    the timeout
603  */
604 static void ircomm_tty_start_watchdog_timer(struct ircomm_tty_cb *self,
605                                             int timeout)
606 {
607         IRDA_ASSERT(self != NULL, return;);
608         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
609
610         irda_start_timer(&self->watchdog_timer, timeout, (void *) self,
611                          ircomm_tty_watchdog_timer_expired);
612 }
613
614 /*
615  * Function ircomm_tty_watchdog_timer_expired (data)
616  *
617  *    Called when the connect procedure have taken to much time.
618  *
619  */
620 static void ircomm_tty_watchdog_timer_expired(void *data)
621 {
622         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) data;
623
624         IRDA_DEBUG(2, "%s()\n", __func__ );
625
626         IRDA_ASSERT(self != NULL, return;);
627         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
628
629         ircomm_tty_do_event(self, IRCOMM_TTY_WD_TIMER_EXPIRED, NULL, NULL);
630 }
631
632
633 /*
634  * Function ircomm_tty_do_event (self, event, skb)
635  *
636  *    Process event
637  *
638  */
639 int ircomm_tty_do_event(struct ircomm_tty_cb *self, IRCOMM_TTY_EVENT event,
640                         struct sk_buff *skb, struct ircomm_tty_info *info)
641 {
642         IRDA_ASSERT(self != NULL, return -1;);
643         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
644
645         IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
646                    ircomm_tty_state[self->state], ircomm_tty_event[event]);
647
648         return (*state[self->state])(self, event, skb, info);
649 }
650
651 /*
652  * Function ircomm_tty_next_state (self, state)
653  *
654  *    Switch state
655  *
656  */
657 static inline void ircomm_tty_next_state(struct ircomm_tty_cb *self, IRCOMM_TTY_STATE state)
658 {
659         /*
660         IRDA_ASSERT(self != NULL, return;);
661         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
662
663         IRDA_DEBUG(2, "%s: next state=%s, service type=%d\n", __func__ ,
664                    ircomm_tty_state[self->state], self->service_type);
665         */
666         self->state = state;
667 }
668
669 /*
670  * Function ircomm_tty_state_idle (self, event, skb, info)
671  *
672  *    Just hanging around
673  *
674  */
675 static int ircomm_tty_state_idle(struct ircomm_tty_cb *self,
676                                  IRCOMM_TTY_EVENT event,
677                                  struct sk_buff *skb,
678                                  struct ircomm_tty_info *info)
679 {
680         int ret = 0;
681
682         IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
683                    ircomm_tty_state[self->state], ircomm_tty_event[event]);
684         switch (event) {
685         case IRCOMM_TTY_ATTACH_CABLE:
686                 /* Try to discover any remote devices */
687                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
688                 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
689
690                 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
691                 break;
692         case IRCOMM_TTY_DISCOVERY_INDICATION:
693                 self->daddr = info->daddr;
694                 self->saddr = info->saddr;
695
696                 if (self->iriap) {
697                         IRDA_WARNING("%s(), busy with a previous query\n",
698                                      __func__);
699                         return -EBUSY;
700                 }
701
702                 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
703                                          ircomm_tty_getvalue_confirm);
704
705                 iriap_getvaluebyclass_request(self->iriap,
706                                               self->saddr, self->daddr,
707                                               "IrDA:IrCOMM", "Parameters");
708
709                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
710                 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_PARAMETERS);
711                 break;
712         case IRCOMM_TTY_CONNECT_INDICATION:
713                 del_timer(&self->watchdog_timer);
714
715                 /* Accept connection */
716                 ircomm_connect_response(self->ircomm, NULL);
717                 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
718                 break;
719         case IRCOMM_TTY_WD_TIMER_EXPIRED:
720                 /* Just stay idle */
721                 break;
722         case IRCOMM_TTY_DETACH_CABLE:
723                 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
724                 break;
725         default:
726                 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
727                            ircomm_tty_event[event]);
728                 ret = -EINVAL;
729         }
730         return ret;
731 }
732
733 /*
734  * Function ircomm_tty_state_search (self, event, skb, info)
735  *
736  *    Trying to discover an IrCOMM device
737  *
738  */
739 static int ircomm_tty_state_search(struct ircomm_tty_cb *self,
740                                    IRCOMM_TTY_EVENT event,
741                                    struct sk_buff *skb,
742                                    struct ircomm_tty_info *info)
743 {
744         int ret = 0;
745
746         IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
747                    ircomm_tty_state[self->state], ircomm_tty_event[event]);
748
749         switch (event) {
750         case IRCOMM_TTY_DISCOVERY_INDICATION:
751                 self->daddr = info->daddr;
752                 self->saddr = info->saddr;
753
754                 if (self->iriap) {
755                         IRDA_WARNING("%s(), busy with a previous query\n",
756                                      __func__);
757                         return -EBUSY;
758                 }
759
760                 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
761                                          ircomm_tty_getvalue_confirm);
762
763                 if (self->service_type == IRCOMM_3_WIRE_RAW) {
764                         iriap_getvaluebyclass_request(self->iriap, self->saddr,
765                                                       self->daddr, "IrLPT",
766                                                       "IrDA:IrLMP:LsapSel");
767                         ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_LSAP_SEL);
768                 } else {
769                         iriap_getvaluebyclass_request(self->iriap, self->saddr,
770                                                       self->daddr,
771                                                       "IrDA:IrCOMM",
772                                                       "Parameters");
773
774                         ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_PARAMETERS);
775                 }
776                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
777                 break;
778         case IRCOMM_TTY_CONNECT_INDICATION:
779                 del_timer(&self->watchdog_timer);
780                 ircomm_tty_ias_unregister(self);
781
782                 /* Accept connection */
783                 ircomm_connect_response(self->ircomm, NULL);
784                 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
785                 break;
786         case IRCOMM_TTY_WD_TIMER_EXPIRED:
787 #if 1
788                 /* Give up */
789 #else
790                 /* Try to discover any remote devices */
791                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
792                 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
793 #endif
794                 break;
795         case IRCOMM_TTY_DETACH_CABLE:
796                 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
797                 break;
798         default:
799                 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
800                            ircomm_tty_event[event]);
801                 ret = -EINVAL;
802         }
803         return ret;
804 }
805
806 /*
807  * Function ircomm_tty_state_query (self, event, skb, info)
808  *
809  *    Querying the remote LM-IAS for IrCOMM parameters
810  *
811  */
812 static int ircomm_tty_state_query_parameters(struct ircomm_tty_cb *self,
813                                              IRCOMM_TTY_EVENT event,
814                                              struct sk_buff *skb,
815                                              struct ircomm_tty_info *info)
816 {
817         int ret = 0;
818
819         IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
820                    ircomm_tty_state[self->state], ircomm_tty_event[event]);
821
822         switch (event) {
823         case IRCOMM_TTY_GOT_PARAMETERS:
824                 if (self->iriap) {
825                         IRDA_WARNING("%s(), busy with a previous query\n",
826                                      __func__);
827                         return -EBUSY;
828                 }
829
830                 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
831                                          ircomm_tty_getvalue_confirm);
832
833                 iriap_getvaluebyclass_request(self->iriap, self->saddr,
834                                               self->daddr, "IrDA:IrCOMM",
835                                               "IrDA:TinyTP:LsapSel");
836
837                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
838                 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_LSAP_SEL);
839                 break;
840         case IRCOMM_TTY_WD_TIMER_EXPIRED:
841                 /* Go back to search mode */
842                 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
843                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
844                 break;
845         case IRCOMM_TTY_CONNECT_INDICATION:
846                 del_timer(&self->watchdog_timer);
847                 ircomm_tty_ias_unregister(self);
848
849                 /* Accept connection */
850                 ircomm_connect_response(self->ircomm, NULL);
851                 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
852                 break;
853         case IRCOMM_TTY_DETACH_CABLE:
854                 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
855                 break;
856         default:
857                 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
858                            ircomm_tty_event[event]);
859                 ret = -EINVAL;
860         }
861         return ret;
862 }
863
864 /*
865  * Function ircomm_tty_state_query_lsap_sel (self, event, skb, info)
866  *
867  *    Query remote LM-IAS for the LSAP selector which we can connect to
868  *
869  */
870 static int ircomm_tty_state_query_lsap_sel(struct ircomm_tty_cb *self,
871                                            IRCOMM_TTY_EVENT event,
872                                            struct sk_buff *skb,
873                                            struct ircomm_tty_info *info)
874 {
875         int ret = 0;
876
877         IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
878                    ircomm_tty_state[self->state], ircomm_tty_event[event]);
879
880         switch (event) {
881         case IRCOMM_TTY_GOT_LSAPSEL:
882                 /* Connect to remote device */
883                 ret = ircomm_connect_request(self->ircomm, self->dlsap_sel,
884                                              self->saddr, self->daddr,
885                                              NULL, self->service_type);
886                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
887                 ircomm_tty_next_state(self, IRCOMM_TTY_SETUP);
888                 break;
889         case IRCOMM_TTY_WD_TIMER_EXPIRED:
890                 /* Go back to search mode */
891                 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
892                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
893                 break;
894         case IRCOMM_TTY_CONNECT_INDICATION:
895                 del_timer(&self->watchdog_timer);
896                 ircomm_tty_ias_unregister(self);
897
898                 /* Accept connection */
899                 ircomm_connect_response(self->ircomm, NULL);
900                 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
901                 break;
902         case IRCOMM_TTY_DETACH_CABLE:
903                 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
904                 break;
905         default:
906                 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
907                            ircomm_tty_event[event]);
908                 ret = -EINVAL;
909         }
910         return ret;
911 }
912
913 /*
914  * Function ircomm_tty_state_setup (self, event, skb, info)
915  *
916  *    Trying to connect
917  *
918  */
919 static int ircomm_tty_state_setup(struct ircomm_tty_cb *self,
920                                   IRCOMM_TTY_EVENT event,
921                                   struct sk_buff *skb,
922                                   struct ircomm_tty_info *info)
923 {
924         int ret = 0;
925
926         IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
927                    ircomm_tty_state[self->state], ircomm_tty_event[event]);
928
929         switch (event) {
930         case IRCOMM_TTY_CONNECT_CONFIRM:
931                 del_timer(&self->watchdog_timer);
932                 ircomm_tty_ias_unregister(self);
933
934                 /*
935                  * Send initial parameters. This will also send out queued
936                  * parameters waiting for the connection to come up
937                  */
938                 ircomm_tty_send_initial_parameters(self);
939                 ircomm_tty_link_established(self);
940                 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
941                 break;
942         case IRCOMM_TTY_CONNECT_INDICATION:
943                 del_timer(&self->watchdog_timer);
944                 ircomm_tty_ias_unregister(self);
945
946                 /* Accept connection */
947                 ircomm_connect_response(self->ircomm, NULL);
948                 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
949                 break;
950         case IRCOMM_TTY_WD_TIMER_EXPIRED:
951                 /* Go back to search mode */
952                 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
953                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
954                 break;
955         case IRCOMM_TTY_DETACH_CABLE:
956                 /* ircomm_disconnect_request(self->ircomm, NULL); */
957                 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
958                 break;
959         default:
960                 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
961                            ircomm_tty_event[event]);
962                 ret = -EINVAL;
963         }
964         return ret;
965 }
966
967 /*
968  * Function ircomm_tty_state_ready (self, event, skb, info)
969  *
970  *    IrCOMM is now connected
971  *
972  */
973 static int ircomm_tty_state_ready(struct ircomm_tty_cb *self,
974                                   IRCOMM_TTY_EVENT event,
975                                   struct sk_buff *skb,
976                                   struct ircomm_tty_info *info)
977 {
978         int ret = 0;
979
980         switch (event) {
981         case IRCOMM_TTY_DATA_REQUEST:
982                 ret = ircomm_data_request(self->ircomm, skb);
983                 break;
984         case IRCOMM_TTY_DETACH_CABLE:
985                 ircomm_disconnect_request(self->ircomm, NULL);
986                 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
987                 break;
988         case IRCOMM_TTY_DISCONNECT_INDICATION:
989                 ircomm_tty_ias_register(self);
990                 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
991                 ircomm_tty_start_watchdog_timer(self, 3*HZ);
992
993                 if (self->port.flags & ASYNC_CHECK_CD) {
994                         /* Drop carrier */
995                         self->settings.dce = IRCOMM_DELTA_CD;
996                         ircomm_tty_check_modem_status(self);
997                 } else {
998                         IRDA_DEBUG(0, "%s(), hanging up!\n", __func__ );
999                         tty_port_tty_hangup(&self->port, false);
1000                 }
1001                 break;
1002         default:
1003                 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
1004                            ircomm_tty_event[event]);
1005                 ret = -EINVAL;
1006         }
1007         return ret;
1008 }
1009