Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / net / irda / ircomm / ircomm_tty_attach.c
blob2ee87bf387cc191147ce9bb3809b6fd1e3096cc4
1 /*********************************************************************
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>
12 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
13 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
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.
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.
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/>.
28 ********************************************************************/
30 #include <linux/init.h>
31 #include <linux/sched.h>
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>
40 #include <net/irda/ircomm_core.h>
41 #include <net/irda/ircomm_param.h>
42 #include <net/irda/ircomm_event.h>
44 #include <net/irda/ircomm_tty.h>
45 #include <net/irda/ircomm_tty_attach.h>
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);
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);
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 *** ",
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 ****",
109 #endif /* CONFIG_IRDA_DEBUG */
111 static int (*state[])(struct ircomm_tty_cb *self, IRCOMM_TTY_EVENT event,
112 struct sk_buff *skb, struct ircomm_tty_info *info) =
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,
123 * Function ircomm_tty_attach_cable (driver)
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.
129 int ircomm_tty_attach_cable(struct ircomm_tty_cb *self)
131 struct tty_struct *tty;
133 IRDA_DEBUG(0, "%s()\n", __func__ );
135 IRDA_ASSERT(self != NULL, return -1;);
136 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
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;
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);
151 ircomm_tty_ias_register(self);
153 ircomm_tty_do_event(self, IRCOMM_TTY_ATTACH_CABLE, NULL, NULL);
155 return 0;
159 * Function ircomm_detach_cable (driver)
161 * Detach cable, or cable has been detached by peer
164 void ircomm_tty_detach_cable(struct ircomm_tty_cb *self)
166 IRDA_DEBUG(0, "%s()\n", __func__ );
168 IRDA_ASSERT(self != NULL, return;);
169 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
171 del_timer(&self->watchdog_timer);
173 /* Remove discovery handler */
174 if (self->ckey) {
175 irlmp_unregister_client(self->ckey);
176 self->ckey = NULL;
178 /* Remove IrCOMM hint bits */
179 if (self->skey) {
180 irlmp_unregister_service(self->skey);
181 self->skey = NULL;
184 if (self->iriap) {
185 iriap_close(self->iriap);
186 self->iriap = NULL;
189 /* Remove LM-IAS object */
190 if (self->obj) {
191 irias_delete_object(self->obj);
192 self->obj = NULL;
195 ircomm_tty_do_event(self, IRCOMM_TTY_DETACH_CABLE, NULL, NULL);
197 /* Reset some values */
198 self->daddr = self->saddr = 0;
199 self->dlsap_sel = self->slsap_sel = 0;
201 memset(&self->settings, 0, sizeof(struct ircomm_params));
205 * Function ircomm_tty_ias_register (self)
207 * Register with LM-IAS depending on which service type we are
210 static void ircomm_tty_ias_register(struct ircomm_tty_cb *self)
212 __u8 oct_seq[6];
213 __u16 hints;
215 IRDA_DEBUG(0, "%s()\n", __func__ );
217 IRDA_ASSERT(self != NULL, return;);
218 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
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);
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);
234 /* If already done, no need to do it again */
235 if (self->obj)
236 return;
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);
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);
254 /* Register parameters with LM-IAS */
255 irias_add_octseq_attrib(self->obj, "Parameters", oct_seq, 6,
256 IAS_KERNEL_ATTR);
258 irias_insert_object(self->obj);
262 * Function ircomm_tty_ias_unregister (self)
264 * Remove our IAS object and client hook while connected.
267 static void ircomm_tty_ias_unregister(struct ircomm_tty_cb *self)
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;
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;
295 #endif
299 * Function ircomm_send_initial_parameters (self)
301 * Send initial parameters to the remote IrCOMM device. These parameters
302 * must be sent before any data.
304 int ircomm_tty_send_initial_parameters(struct ircomm_tty_cb *self)
306 IRDA_ASSERT(self != NULL, return -1;);
307 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
309 if (self->service_type & IRCOMM_3_WIRE_RAW)
310 return 0;
313 * Set default values, but only if the application for some reason
314 * haven't set them already
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 */
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;*/
329 /* Do not set delta values for the initial parameters */
330 self->settings.dte = IRCOMM_DTR | IRCOMM_RTS;
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);
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;
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);
353 return 0;
357 * Function ircomm_tty_discovery_indication (discovery)
359 * Remote device is discovered, try query the remote IAS to see which
360 * device it is, and which services it has.
363 static void ircomm_tty_discovery_indication(discinfo_t *discovery,
364 DISCOVERY_MODE mode,
365 void *priv)
367 struct ircomm_tty_cb *self;
368 struct ircomm_tty_info info;
370 IRDA_DEBUG(2, "%s()\n", __func__ );
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;
386 info.daddr = discovery->daddr;
387 info.saddr = discovery->saddr;
389 self = priv;
390 ircomm_tty_do_event(self, IRCOMM_TTY_DISCOVERY_INDICATION,
391 NULL, &info);
395 * Function ircomm_tty_disconnect_indication (instance, sap, reason, skb)
397 * Link disconnected
400 void ircomm_tty_disconnect_indication(void *instance, void *sap,
401 LM_REASON reason,
402 struct sk_buff *skb)
404 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
405 struct tty_struct *tty;
407 IRDA_DEBUG(2, "%s()\n", __func__ );
409 IRDA_ASSERT(self != NULL, return;);
410 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
412 tty = tty_port_tty_get(&self->port);
413 if (!tty)
414 return;
416 /* This will stop control data transfers */
417 self->flow = FLOW_STOP;
419 /* Stop data transfers */
420 tty->hw_stopped = 1;
422 ircomm_tty_do_event(self, IRCOMM_TTY_DISCONNECT_INDICATION, NULL,
423 NULL);
424 tty_kref_put(tty);
428 * Function ircomm_tty_getvalue_confirm (result, obj_id, value, priv)
430 * Got result from the IAS query we make
433 static void ircomm_tty_getvalue_confirm(int result, __u16 obj_id,
434 struct ias_value *value,
435 void *priv)
437 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) priv;
439 IRDA_DEBUG(2, "%s()\n", __func__ );
441 IRDA_ASSERT(self != NULL, return;);
442 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
444 /* We probably don't need to make any more queries */
445 iriap_close(self->iriap);
446 self->iriap = NULL;
448 /* Check if request succeeded */
449 if (result != IAS_SUCCESS) {
450 IRDA_DEBUG(4, "%s(), got NULL value!\n", __func__ );
451 return;
454 switch (value->type) {
455 case IAS_OCT_SEQ:
456 IRDA_DEBUG(2, "%s(), got octet sequence\n", __func__ );
458 irda_param_extract_all(self, value->t.oct_seq, value->len,
459 &ircomm_param_info);
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);
469 if (value->t.integer == -1) {
470 IRDA_DEBUG(0, "%s(), invalid value!\n", __func__ );
471 } else
472 self->dlsap_sel = value->t.integer;
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;
483 irias_delete_value(value);
487 * Function ircomm_tty_connect_confirm (instance, sap, qos, max_sdu_size, skb)
489 * Connection confirmed
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)
498 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
500 IRDA_DEBUG(2, "%s()\n", __func__ );
502 IRDA_ASSERT(self != NULL, return;);
503 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
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;
510 ircomm_tty_do_event(self, IRCOMM_TTY_CONNECT_CONFIRM, NULL, NULL);
512 /* No need to kfree_skb - see ircomm_ttp_connect_confirm() */
516 * Function ircomm_tty_connect_indication (instance, sap, qos, max_sdu_size,
517 * skb)
519 * we are discovered and being requested to connect by remote device !
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)
528 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
529 int clen;
531 IRDA_DEBUG(2, "%s()\n", __func__ );
533 IRDA_ASSERT(self != NULL, return;);
534 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
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;
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);
547 ircomm_tty_do_event(self, IRCOMM_TTY_CONNECT_INDICATION, NULL, NULL);
549 /* No need to kfree_skb - see ircomm_ttp_connect_indication() */
553 * Function ircomm_tty_link_established (self)
555 * Called when the IrCOMM link is established
558 void ircomm_tty_link_established(struct ircomm_tty_cb *self)
560 struct tty_struct *tty;
562 IRDA_DEBUG(2, "%s()\n", __func__ );
564 IRDA_ASSERT(self != NULL, return;);
565 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
567 tty = tty_port_tty_get(&self->port);
568 if (!tty)
569 return;
571 del_timer(&self->watchdog_timer);
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.
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__ );
586 tty->hw_stopped = 0;
588 /* Wake up processes blocked on open */
589 wake_up_interruptible(&self->port.open_wait);
592 schedule_work(&self->tqueue);
593 put:
594 tty_kref_put(tty);
598 * Function ircomm_tty_start_watchdog_timer (self, timeout)
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
604 static void ircomm_tty_start_watchdog_timer(struct ircomm_tty_cb *self,
605 int timeout)
607 IRDA_ASSERT(self != NULL, return;);
608 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
610 irda_start_timer(&self->watchdog_timer, timeout, (void *) self,
611 ircomm_tty_watchdog_timer_expired);
615 * Function ircomm_tty_watchdog_timer_expired (data)
617 * Called when the connect procedure have taken to much time.
620 static void ircomm_tty_watchdog_timer_expired(void *data)
622 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) data;
624 IRDA_DEBUG(2, "%s()\n", __func__ );
626 IRDA_ASSERT(self != NULL, return;);
627 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
629 ircomm_tty_do_event(self, IRCOMM_TTY_WD_TIMER_EXPIRED, NULL, NULL);
634 * Function ircomm_tty_do_event (self, event, skb)
636 * Process event
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)
642 IRDA_ASSERT(self != NULL, return -1;);
643 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
645 IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
646 ircomm_tty_state[self->state], ircomm_tty_event[event]);
648 return (*state[self->state])(self, event, skb, info);
652 * Function ircomm_tty_next_state (self, state)
654 * Switch state
657 static inline void ircomm_tty_next_state(struct ircomm_tty_cb *self, IRCOMM_TTY_STATE state)
660 IRDA_ASSERT(self != NULL, return;);
661 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
663 IRDA_DEBUG(2, "%s: next state=%s, service type=%d\n", __func__ ,
664 ircomm_tty_state[self->state], self->service_type);
666 self->state = state;
670 * Function ircomm_tty_state_idle (self, event, skb, info)
672 * Just hanging around
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)
680 int ret = 0;
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);
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;
696 if (self->iriap) {
697 IRDA_WARNING("%s(), busy with a previous query\n",
698 __func__);
699 return -EBUSY;
702 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
703 ircomm_tty_getvalue_confirm);
705 iriap_getvaluebyclass_request(self->iriap,
706 self->saddr, self->daddr,
707 "IrDA:IrCOMM", "Parameters");
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);
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;
730 return ret;
734 * Function ircomm_tty_state_search (self, event, skb, info)
736 * Trying to discover an IrCOMM device
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)
744 int ret = 0;
746 IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
747 ircomm_tty_state[self->state], ircomm_tty_event[event]);
749 switch (event) {
750 case IRCOMM_TTY_DISCOVERY_INDICATION:
751 self->daddr = info->daddr;
752 self->saddr = info->saddr;
754 if (self->iriap) {
755 IRDA_WARNING("%s(), busy with a previous query\n",
756 __func__);
757 return -EBUSY;
760 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
761 ircomm_tty_getvalue_confirm);
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");
774 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_PARAMETERS);
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);
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;
803 return ret;
807 * Function ircomm_tty_state_query (self, event, skb, info)
809 * Querying the remote LM-IAS for IrCOMM parameters
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)
817 int ret = 0;
819 IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
820 ircomm_tty_state[self->state], ircomm_tty_event[event]);
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;
830 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
831 ircomm_tty_getvalue_confirm);
833 iriap_getvaluebyclass_request(self->iriap, self->saddr,
834 self->daddr, "IrDA:IrCOMM",
835 "IrDA:TinyTP:LsapSel");
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);
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;
861 return ret;
865 * Function ircomm_tty_state_query_lsap_sel (self, event, skb, info)
867 * Query remote LM-IAS for the LSAP selector which we can connect to
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)
875 int ret = 0;
877 IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
878 ircomm_tty_state[self->state], ircomm_tty_event[event]);
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);
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;
910 return ret;
914 * Function ircomm_tty_state_setup (self, event, skb, info)
916 * Trying to connect
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)
924 int ret = 0;
926 IRDA_DEBUG(2, "%s: state=%s, event=%s\n", __func__ ,
927 ircomm_tty_state[self->state], ircomm_tty_event[event]);
929 switch (event) {
930 case IRCOMM_TTY_CONNECT_CONFIRM:
931 del_timer(&self->watchdog_timer);
932 ircomm_tty_ias_unregister(self);
935 * Send initial parameters. This will also send out queued
936 * parameters waiting for the connection to come up
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);
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;
964 return ret;
968 * Function ircomm_tty_state_ready (self, event, skb, info)
970 * IrCOMM is now connected
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)
978 int ret = 0;
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);
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);
1001 break;
1002 default:
1003 IRDA_DEBUG(2, "%s(), unknown event: %s\n", __func__ ,
1004 ircomm_tty_event[event]);
1005 ret = -EINVAL;
1007 return ret;