blob: 61137f8b5293617c789172cf5c1840417cb7efa6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Filename: ircomm_tty_attach.c
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09004 * Version:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
13 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090014 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * 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.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090024 *
25 * You should have received a copy of the GNU General Public License
Jeff Kirsherd3770502013-12-06 09:13:43 -080026 * along with this program; if not, see <http://www.gnu.org/licenses/>.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 ********************************************************************/
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/init.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040031#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
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
47static void ircomm_tty_ias_register(struct ircomm_tty_cb *self);
48static void ircomm_tty_discovery_indication(discinfo_t *discovery,
49 DISCOVERY_MODE mode,
50 void *priv);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090051static void ircomm_tty_getvalue_confirm(int result, __u16 obj_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct ias_value *value, void *priv);
53static void ircomm_tty_start_watchdog_timer(struct ircomm_tty_cb *self,
54 int timeout);
55static void ircomm_tty_watchdog_timer_expired(void *data);
56
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090057static int ircomm_tty_state_idle(struct ircomm_tty_cb *self,
58 IRCOMM_TTY_EVENT event,
59 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct ircomm_tty_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090061static int ircomm_tty_state_search(struct ircomm_tty_cb *self,
62 IRCOMM_TTY_EVENT event,
63 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct ircomm_tty_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090065static int ircomm_tty_state_query_parameters(struct ircomm_tty_cb *self,
66 IRCOMM_TTY_EVENT event,
67 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct ircomm_tty_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090069static int ircomm_tty_state_query_lsap_sel(struct ircomm_tty_cb *self,
70 IRCOMM_TTY_EVENT event,
71 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct ircomm_tty_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090073static int ircomm_tty_state_setup(struct ircomm_tty_cb *self,
74 IRCOMM_TTY_EVENT event,
75 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct ircomm_tty_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090077static int ircomm_tty_state_ready(struct ircomm_tty_cb *self,
78 IRCOMM_TTY_EVENT event,
79 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct ircomm_tty_info *info);
81
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070082const char *const ircomm_tty_state[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 "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
Joe Perchesa7688512014-11-12 18:15:47 -080092static const char *const ircomm_tty_event[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 "IRCOMM_TTY_ATTACH_CABLE",
94 "IRCOMM_TTY_DETACH_CABLE",
95 "IRCOMM_TTY_DATA_REQUEST",
96 "IRCOMM_TTY_DATA_INDICATION",
97 "IRCOMM_TTY_DISCOVERY_REQUEST",
98 "IRCOMM_TTY_DISCOVERY_INDICATION",
99 "IRCOMM_TTY_CONNECT_CONFIRM",
100 "IRCOMM_TTY_CONNECT_INDICATION",
101 "IRCOMM_TTY_DISCONNECT_REQUEST",
102 "IRCOMM_TTY_DISCONNECT_INDICATION",
103 "IRCOMM_TTY_WD_TIMER_EXPIRED",
104 "IRCOMM_TTY_GOT_PARAMETERS",
105 "IRCOMM_TTY_GOT_LSAPSEL",
106 "*** ERROR ****",
107};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static int (*state[])(struct ircomm_tty_cb *self, IRCOMM_TTY_EVENT event,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900110 struct sk_buff *skb, struct ircomm_tty_info *info) =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 ircomm_tty_state_idle,
113 ircomm_tty_state_search,
114 ircomm_tty_state_query_parameters,
115 ircomm_tty_state_query_lsap_sel,
116 ircomm_tty_state_setup,
117 ircomm_tty_state_ready,
118};
119
120/*
121 * Function ircomm_tty_attach_cable (driver)
122 *
123 * Try to attach cable (IrCOMM link). This function will only return
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900124 * when the link has been connected, or if an error condition occurs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * If success, the return value is the resulting service type.
126 */
127int ircomm_tty_attach_cable(struct ircomm_tty_cb *self)
128{
Jiri Slaby62f228a2012-06-04 13:35:21 +0200129 struct tty_struct *tty;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 IRDA_ASSERT(self != NULL, return -1;);
132 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
133
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900134 /* Check if somebody has already connected to us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (ircomm_is_connected(self->ircomm)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800136 pr_debug("%s(), already connected!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138 }
139
140 /* Make sure nobody tries to write before the link is up */
Jiri Slaby62f228a2012-06-04 13:35:21 +0200141 tty = tty_port_tty_get(&self->port);
142 if (tty) {
143 tty->hw_stopped = 1;
144 tty_kref_put(tty);
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 ircomm_tty_ias_register(self);
148
149 ircomm_tty_do_event(self, IRCOMM_TTY_ATTACH_CABLE, NULL, NULL);
150
151 return 0;
152}
153
154/*
155 * Function ircomm_detach_cable (driver)
156 *
157 * Detach cable, or cable has been detached by peer
158 *
159 */
160void ircomm_tty_detach_cable(struct ircomm_tty_cb *self)
161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 IRDA_ASSERT(self != NULL, return;);
163 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
164
165 del_timer(&self->watchdog_timer);
166
167 /* Remove discovery handler */
168 if (self->ckey) {
169 irlmp_unregister_client(self->ckey);
170 self->ckey = NULL;
171 }
172 /* Remove IrCOMM hint bits */
173 if (self->skey) {
174 irlmp_unregister_service(self->skey);
175 self->skey = NULL;
176 }
177
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900178 if (self->iriap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 iriap_close(self->iriap);
180 self->iriap = NULL;
181 }
182
183 /* Remove LM-IAS object */
184 if (self->obj) {
185 irias_delete_object(self->obj);
186 self->obj = NULL;
187 }
188
189 ircomm_tty_do_event(self, IRCOMM_TTY_DETACH_CABLE, NULL, NULL);
190
191 /* Reset some values */
192 self->daddr = self->saddr = 0;
193 self->dlsap_sel = self->slsap_sel = 0;
194
195 memset(&self->settings, 0, sizeof(struct ircomm_params));
196}
197
198/*
199 * Function ircomm_tty_ias_register (self)
200 *
201 * Register with LM-IAS depending on which service type we are
202 *
203 */
204static void ircomm_tty_ias_register(struct ircomm_tty_cb *self)
205{
206 __u8 oct_seq[6];
207 __u16 hints;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 IRDA_ASSERT(self != NULL, return;);
210 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Compute hint bits based on service */
213 hints = irlmp_service_to_hint(S_COMM);
214 if (self->service_type & IRCOMM_3_WIRE_RAW)
215 hints |= irlmp_service_to_hint(S_PRINTER);
216
217 /* Advertise IrCOMM hint bit in discovery */
218 if (!self->skey)
219 self->skey = irlmp_register_service(hints);
220 /* Set up a discovery handler */
221 if (!self->ckey)
222 self->ckey = irlmp_register_client(hints,
223 ircomm_tty_discovery_indication,
224 NULL, (void *) self);
225
226 /* If already done, no need to do it again */
227 if (self->obj)
228 return;
229
230 if (self->service_type & IRCOMM_3_WIRE_RAW) {
231 /* Register IrLPT with LM-IAS */
232 self->obj = irias_new_object("IrLPT", IAS_IRLPT_ID);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900233 irias_add_integer_attrib(self->obj, "IrDA:IrLMP:LsapSel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 self->slsap_sel, IAS_KERNEL_ATTR);
235 } else {
236 /* Register IrCOMM with LM-IAS */
237 self->obj = irias_new_object("IrDA:IrCOMM", IAS_IRCOMM_ID);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900238 irias_add_integer_attrib(self->obj, "IrDA:TinyTP:LsapSel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 self->slsap_sel, IAS_KERNEL_ATTR);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Code the parameters into the buffer */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900242 irda_param_pack(oct_seq, "bbbbbb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 IRCOMM_SERVICE_TYPE, 1, self->service_type,
244 IRCOMM_PORT_TYPE, 1, IRCOMM_SERIAL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* Register parameters with LM-IAS */
247 irias_add_octseq_attrib(self->obj, "Parameters", oct_seq, 6,
248 IAS_KERNEL_ATTR);
249 }
250 irias_insert_object(self->obj);
251}
252
253/*
254 * Function ircomm_tty_ias_unregister (self)
255 *
256 * Remove our IAS object and client hook while connected.
257 *
258 */
259static void ircomm_tty_ias_unregister(struct ircomm_tty_cb *self)
260{
261 /* Remove LM-IAS object now so it is not reused.
262 * IrCOMM deals very poorly with multiple incoming connections.
263 * It should looks a lot more like IrNET, and "dup" a server TSAP
264 * to the application TSAP (based on various rules).
265 * This is a cheap workaround allowing multiple clients to
266 * connect to us. It will not always work.
267 * Each IrCOMM socket has an IAS entry. Incoming connection will
268 * pick the first one found. So, when we are fully connected,
269 * we remove our IAS entries so that the next IAS entry is used.
270 * We do that for *both* client and server, because a server
271 * can also create client instances.
272 * Jean II */
273 if (self->obj) {
274 irias_delete_object(self->obj);
275 self->obj = NULL;
276 }
277
278#if 0
279 /* Remove discovery handler.
280 * While we are connected, we no longer need to receive
281 * discovery events. This would be the case if there is
282 * multiple IrLAP interfaces. Jean II */
283 if (self->ckey) {
284 irlmp_unregister_client(self->ckey);
285 self->ckey = NULL;
286 }
287#endif
288}
289
290/*
291 * Function ircomm_send_initial_parameters (self)
292 *
293 * Send initial parameters to the remote IrCOMM device. These parameters
294 * must be sent before any data.
295 */
296int ircomm_tty_send_initial_parameters(struct ircomm_tty_cb *self)
297{
298 IRDA_ASSERT(self != NULL, return -1;);
299 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
300
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900301 if (self->service_type & IRCOMM_3_WIRE_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900304 /*
305 * Set default values, but only if the application for some reason
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * haven't set them already
307 */
Joe Perches955a9d202014-11-11 14:44:57 -0800308 pr_debug("%s(), data-rate = %d\n", __func__ ,
309 self->settings.data_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!self->settings.data_rate)
311 self->settings.data_rate = 9600;
Joe Perches955a9d202014-11-11 14:44:57 -0800312 pr_debug("%s(), data-format = %d\n", __func__ ,
313 self->settings.data_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!self->settings.data_format)
315 self->settings.data_format = IRCOMM_WSIZE_8; /* 8N1 */
316
Joe Perches955a9d202014-11-11 14:44:57 -0800317 pr_debug("%s(), flow-control = %d\n", __func__ ,
318 self->settings.flow_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*self->settings.flow_control = IRCOMM_RTS_CTS_IN|IRCOMM_RTS_CTS_OUT;*/
320
321 /* Do not set delta values for the initial parameters */
322 self->settings.dte = IRCOMM_DTR | IRCOMM_RTS;
323
324 /* Only send service type parameter when we are the client */
325 if (self->client)
326 ircomm_param_request(self, IRCOMM_SERVICE_TYPE, FALSE);
327 ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
328 ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* For a 3 wire service, we just flush the last parameter and return */
331 if (self->settings.service_type == IRCOMM_3_WIRE) {
332 ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
333 return 0;
334 }
335
336 /* Only 9-wire service types continue here */
337 ircomm_param_request(self, IRCOMM_FLOW_CONTROL, FALSE);
338#if 0
339 ircomm_param_request(self, IRCOMM_XON_XOFF, FALSE);
340 ircomm_param_request(self, IRCOMM_ENQ_ACK, FALSE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Notify peer that we are ready to receive data */
343 ircomm_param_request(self, IRCOMM_DTE, TRUE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
348/*
349 * Function ircomm_tty_discovery_indication (discovery)
350 *
351 * Remote device is discovered, try query the remote IAS to see which
352 * device it is, and which services it has.
353 *
354 */
355static void ircomm_tty_discovery_indication(discinfo_t *discovery,
356 DISCOVERY_MODE mode,
357 void *priv)
358{
359 struct ircomm_tty_cb *self;
360 struct ircomm_tty_info info;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Important note :
363 * We need to drop all passive discoveries.
364 * The LSAP management of IrComm is deficient and doesn't deal
365 * with the case of two instance connecting to each other
366 * simultaneously (it will deadlock in LMP).
367 * The proper fix would be to use the same technique as in IrNET,
368 * to have one server socket and separate instances for the
369 * connecting/connected socket.
370 * The workaround is to drop passive discovery, which drastically
371 * reduce the probability of this happening.
372 * Jean II */
373 if(mode == DISCOVERY_PASSIVE)
374 return;
375
376 info.daddr = discovery->daddr;
377 info.saddr = discovery->saddr;
378
Joe Perchesea110732011-06-13 16:21:26 +0000379 self = priv;
Ryan Readingc310f092007-10-17 19:34:11 -0700380 ircomm_tty_do_event(self, IRCOMM_TTY_DISCOVERY_INDICATION,
381 NULL, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/*
385 * Function ircomm_tty_disconnect_indication (instance, sap, reason, skb)
386 *
387 * Link disconnected
388 *
389 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900390void ircomm_tty_disconnect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 LM_REASON reason,
392 struct sk_buff *skb)
393{
394 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
Jiri Slaby62f228a2012-06-04 13:35:21 +0200395 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 IRDA_ASSERT(self != NULL, return;);
398 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
399
Jiri Slaby62f228a2012-06-04 13:35:21 +0200400 tty = tty_port_tty_get(&self->port);
401 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return;
403
404 /* This will stop control data transfers */
405 self->flow = FLOW_STOP;
406
407 /* Stop data transfers */
Jiri Slaby62f228a2012-06-04 13:35:21 +0200408 tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900410 ircomm_tty_do_event(self, IRCOMM_TTY_DISCONNECT_INDICATION, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 NULL);
Jiri Slaby62f228a2012-06-04 13:35:21 +0200412 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415/*
416 * Function ircomm_tty_getvalue_confirm (result, obj_id, value, priv)
417 *
418 * Got result from the IAS query we make
419 *
420 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900421static void ircomm_tty_getvalue_confirm(int result, __u16 obj_id,
422 struct ias_value *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 void *priv)
424{
425 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) priv;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 IRDA_ASSERT(self != NULL, return;);
428 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
429
430 /* We probably don't need to make any more queries */
431 iriap_close(self->iriap);
432 self->iriap = NULL;
433
434 /* Check if request succeeded */
435 if (result != IAS_SUCCESS) {
Joe Perches955a9d202014-11-11 14:44:57 -0800436 pr_debug("%s(), got NULL value!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
438 }
439
440 switch (value->type) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900441 case IAS_OCT_SEQ:
Joe Perches955a9d202014-11-11 14:44:57 -0800442 pr_debug("%s(), got octet sequence\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 irda_param_extract_all(self, value->t.oct_seq, value->len,
445 &ircomm_param_info);
446
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900447 ircomm_tty_do_event(self, IRCOMM_TTY_GOT_PARAMETERS, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 NULL);
449 break;
450 case IAS_INTEGER:
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900451 /* Got LSAP selector */
Joe Perches955a9d202014-11-11 14:44:57 -0800452 pr_debug("%s(), got lsapsel = %d\n", __func__ ,
453 value->t.integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 if (value->t.integer == -1) {
Joe Perches955a9d202014-11-11 14:44:57 -0800456 pr_debug("%s(), invalid value!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 } else
458 self->dlsap_sel = value->t.integer;
459
460 ircomm_tty_do_event(self, IRCOMM_TTY_GOT_LSAPSEL, NULL, NULL);
461 break;
462 case IAS_MISSING:
Joe Perches955a9d202014-11-11 14:44:57 -0800463 pr_debug("%s(), got IAS_MISSING\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800466 pr_debug("%s(), got unknown type!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 }
469 irias_delete_value(value);
470}
471
472/*
473 * Function ircomm_tty_connect_confirm (instance, sap, qos, max_sdu_size, skb)
474 *
475 * Connection confirmed
476 *
477 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900478void ircomm_tty_connect_confirm(void *instance, void *sap,
479 struct qos_info *qos,
480 __u32 max_data_size,
481 __u8 max_header_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct sk_buff *skb)
483{
484 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 IRDA_ASSERT(self != NULL, return;);
487 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
488
489 self->client = TRUE;
490 self->max_data_size = max_data_size;
491 self->max_header_size = max_header_size;
492 self->flow = FLOW_START;
493
494 ircomm_tty_do_event(self, IRCOMM_TTY_CONNECT_CONFIRM, NULL, NULL);
495
496 /* No need to kfree_skb - see ircomm_ttp_connect_confirm() */
497}
498
499/*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900500 * Function ircomm_tty_connect_indication (instance, sap, qos, max_sdu_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * skb)
502 *
503 * we are discovered and being requested to connect by remote device !
504 *
505 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900506void ircomm_tty_connect_indication(void *instance, void *sap,
507 struct qos_info *qos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 __u32 max_data_size,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900509 __u8 max_header_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct sk_buff *skb)
511{
512 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
513 int clen;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 IRDA_ASSERT(self != NULL, return;);
516 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
517
518 self->client = FALSE;
519 self->max_data_size = max_data_size;
520 self->max_header_size = max_header_size;
521 self->flow = FLOW_START;
522
523 clen = skb->data[0];
524 if (clen)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900525 irda_param_extract_all(self, skb->data+1,
526 IRDA_MIN(skb->len, clen),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 &ircomm_param_info);
528
529 ircomm_tty_do_event(self, IRCOMM_TTY_CONNECT_INDICATION, NULL, NULL);
530
531 /* No need to kfree_skb - see ircomm_ttp_connect_indication() */
532}
533
534/*
535 * Function ircomm_tty_link_established (self)
536 *
537 * Called when the IrCOMM link is established
538 *
539 */
540void ircomm_tty_link_established(struct ircomm_tty_cb *self)
541{
Jiri Slaby62f228a2012-06-04 13:35:21 +0200542 struct tty_struct *tty;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 IRDA_ASSERT(self != NULL, return;);
545 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
546
Jiri Slaby62f228a2012-06-04 13:35:21 +0200547 tty = tty_port_tty_get(&self->port);
548 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 del_timer(&self->watchdog_timer);
552
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900553 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * IrCOMM link is now up, and if we are not using hardware
555 * flow-control, then declare the hardware as running. Otherwise we
556 * will have to wait for the peer device (DCE) to raise the CTS
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900557 * line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Huang Shijief21ec3d2012-08-22 22:13:36 -0400559 if (tty_port_cts_enabled(&self->port) &&
Jiri Slaby849d5a92012-06-04 13:35:19 +0200560 ((self->settings.dce & IRCOMM_CTS) == 0)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800561 pr_debug("%s(), waiting for CTS ...\n", __func__);
Jiri Slaby62f228a2012-06-04 13:35:21 +0200562 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 } else {
Joe Perches955a9d202014-11-11 14:44:57 -0800564 pr_debug("%s(), starting hardware!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jiri Slaby62f228a2012-06-04 13:35:21 +0200566 tty->hw_stopped = 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Wake up processes blocked on open */
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200569 wake_up_interruptible(&self->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
572 schedule_work(&self->tqueue);
Jiri Slaby62f228a2012-06-04 13:35:21 +0200573put:
574 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * Function ircomm_tty_start_watchdog_timer (self, timeout)
579 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900580 * Start the watchdog timer. This timer is used to make sure that any
581 * connection attempt is successful, and if not, we will retry after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * the timeout
583 */
584static void ircomm_tty_start_watchdog_timer(struct ircomm_tty_cb *self,
585 int timeout)
586{
587 IRDA_ASSERT(self != NULL, return;);
588 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
589
590 irda_start_timer(&self->watchdog_timer, timeout, (void *) self,
591 ircomm_tty_watchdog_timer_expired);
592}
593
594/*
595 * Function ircomm_tty_watchdog_timer_expired (data)
596 *
597 * Called when the connect procedure have taken to much time.
598 *
599 */
600static void ircomm_tty_watchdog_timer_expired(void *data)
601{
602 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 IRDA_ASSERT(self != NULL, return;);
605 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
606
607 ircomm_tty_do_event(self, IRCOMM_TTY_WD_TIMER_EXPIRED, NULL, NULL);
608}
609
610
611/*
612 * Function ircomm_tty_do_event (self, event, skb)
613 *
614 * Process event
615 *
616 */
617int ircomm_tty_do_event(struct ircomm_tty_cb *self, IRCOMM_TTY_EVENT event,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900618 struct sk_buff *skb, struct ircomm_tty_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 IRDA_ASSERT(self != NULL, return -1;);
621 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
622
Joe Perches955a9d202014-11-11 14:44:57 -0800623 pr_debug("%s: state=%s, event=%s\n", __func__ ,
624 ircomm_tty_state[self->state], ircomm_tty_event[event]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return (*state[self->state])(self, event, skb, info);
627}
628
629/*
630 * Function ircomm_tty_next_state (self, state)
631 *
632 * Switch state
633 *
634 */
635static inline void ircomm_tty_next_state(struct ircomm_tty_cb *self, IRCOMM_TTY_STATE state)
636{
637 /*
638 IRDA_ASSERT(self != NULL, return;);
639 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
640
Joe Perches955a9d202014-11-11 14:44:57 -0800641 pr_debug("%s: next state=%s, service type=%d\n", __func__ ,
642 ircomm_tty_state[self->state], self->service_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644 self->state = state;
645}
646
647/*
648 * Function ircomm_tty_state_idle (self, event, skb, info)
649 *
650 * Just hanging around
651 *
652 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900653static int ircomm_tty_state_idle(struct ircomm_tty_cb *self,
654 IRCOMM_TTY_EVENT event,
655 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct ircomm_tty_info *info)
657{
658 int ret = 0;
659
Joe Perches955a9d202014-11-11 14:44:57 -0800660 pr_debug("%s: state=%s, event=%s\n", __func__ ,
661 ircomm_tty_state[self->state], ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 switch (event) {
663 case IRCOMM_TTY_ATTACH_CABLE:
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900664 /* Try to discover any remote devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ircomm_tty_start_watchdog_timer(self, 3*HZ);
666 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
669 break;
670 case IRCOMM_TTY_DISCOVERY_INDICATION:
671 self->daddr = info->daddr;
672 self->saddr = info->saddr;
673
674 if (self->iriap) {
Joe Perches6c910232014-11-11 13:37:30 -0800675 net_warn_ratelimited("%s(), busy with a previous query\n",
676 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EBUSY;
678 }
679
680 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
681 ircomm_tty_getvalue_confirm);
682
683 iriap_getvaluebyclass_request(self->iriap,
684 self->saddr, self->daddr,
685 "IrDA:IrCOMM", "Parameters");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ircomm_tty_start_watchdog_timer(self, 3*HZ);
688 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_PARAMETERS);
689 break;
690 case IRCOMM_TTY_CONNECT_INDICATION:
691 del_timer(&self->watchdog_timer);
692
693 /* Accept connection */
694 ircomm_connect_response(self->ircomm, NULL);
695 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
696 break;
697 case IRCOMM_TTY_WD_TIMER_EXPIRED:
698 /* Just stay idle */
699 break;
700 case IRCOMM_TTY_DETACH_CABLE:
701 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
702 break;
703 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800704 pr_debug("%s(), unknown event: %s\n", __func__ ,
705 ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ret = -EINVAL;
707 }
708 return ret;
709}
710
711/*
712 * Function ircomm_tty_state_search (self, event, skb, info)
713 *
714 * Trying to discover an IrCOMM device
715 *
716 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900717static int ircomm_tty_state_search(struct ircomm_tty_cb *self,
718 IRCOMM_TTY_EVENT event,
719 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct ircomm_tty_info *info)
721{
722 int ret = 0;
723
Joe Perches955a9d202014-11-11 14:44:57 -0800724 pr_debug("%s: state=%s, event=%s\n", __func__ ,
725 ircomm_tty_state[self->state], ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 switch (event) {
728 case IRCOMM_TTY_DISCOVERY_INDICATION:
729 self->daddr = info->daddr;
730 self->saddr = info->saddr;
731
732 if (self->iriap) {
Joe Perches6c910232014-11-11 13:37:30 -0800733 net_warn_ratelimited("%s(), busy with a previous query\n",
734 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EBUSY;
736 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
739 ircomm_tty_getvalue_confirm);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (self->service_type == IRCOMM_3_WIRE_RAW) {
742 iriap_getvaluebyclass_request(self->iriap, self->saddr,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900743 self->daddr, "IrLPT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 "IrDA:IrLMP:LsapSel");
745 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_LSAP_SEL);
746 } else {
747 iriap_getvaluebyclass_request(self->iriap, self->saddr,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900748 self->daddr,
749 "IrDA:IrCOMM",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 "Parameters");
751
752 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_PARAMETERS);
753 }
754 ircomm_tty_start_watchdog_timer(self, 3*HZ);
755 break;
756 case IRCOMM_TTY_CONNECT_INDICATION:
757 del_timer(&self->watchdog_timer);
758 ircomm_tty_ias_unregister(self);
759
760 /* Accept connection */
761 ircomm_connect_response(self->ircomm, NULL);
762 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
763 break;
764 case IRCOMM_TTY_WD_TIMER_EXPIRED:
765#if 1
766 /* Give up */
767#else
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900768 /* Try to discover any remote devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ircomm_tty_start_watchdog_timer(self, 3*HZ);
770 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
771#endif
772 break;
773 case IRCOMM_TTY_DETACH_CABLE:
774 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
775 break;
776 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800777 pr_debug("%s(), unknown event: %s\n", __func__ ,
778 ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ret = -EINVAL;
780 }
781 return ret;
782}
783
784/*
785 * Function ircomm_tty_state_query (self, event, skb, info)
786 *
787 * Querying the remote LM-IAS for IrCOMM parameters
788 *
789 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900790static int ircomm_tty_state_query_parameters(struct ircomm_tty_cb *self,
791 IRCOMM_TTY_EVENT event,
792 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct ircomm_tty_info *info)
794{
795 int ret = 0;
796
Joe Perches955a9d202014-11-11 14:44:57 -0800797 pr_debug("%s: state=%s, event=%s\n", __func__ ,
798 ircomm_tty_state[self->state], ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 switch (event) {
801 case IRCOMM_TTY_GOT_PARAMETERS:
802 if (self->iriap) {
Joe Perches6c910232014-11-11 13:37:30 -0800803 net_warn_ratelimited("%s(), busy with a previous query\n",
804 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -EBUSY;
806 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
809 ircomm_tty_getvalue_confirm);
810
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900811 iriap_getvaluebyclass_request(self->iriap, self->saddr,
812 self->daddr, "IrDA:IrCOMM",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 "IrDA:TinyTP:LsapSel");
814
815 ircomm_tty_start_watchdog_timer(self, 3*HZ);
816 ircomm_tty_next_state(self, IRCOMM_TTY_QUERY_LSAP_SEL);
817 break;
818 case IRCOMM_TTY_WD_TIMER_EXPIRED:
819 /* Go back to search mode */
820 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900821 ircomm_tty_start_watchdog_timer(self, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 break;
823 case IRCOMM_TTY_CONNECT_INDICATION:
824 del_timer(&self->watchdog_timer);
825 ircomm_tty_ias_unregister(self);
826
827 /* Accept connection */
828 ircomm_connect_response(self->ircomm, NULL);
829 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
830 break;
831 case IRCOMM_TTY_DETACH_CABLE:
832 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
833 break;
834 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800835 pr_debug("%s(), unknown event: %s\n", __func__ ,
836 ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 ret = -EINVAL;
838 }
839 return ret;
840}
841
842/*
843 * Function ircomm_tty_state_query_lsap_sel (self, event, skb, info)
844 *
845 * Query remote LM-IAS for the LSAP selector which we can connect to
846 *
847 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900848static int ircomm_tty_state_query_lsap_sel(struct ircomm_tty_cb *self,
849 IRCOMM_TTY_EVENT event,
850 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct ircomm_tty_info *info)
852{
853 int ret = 0;
854
Joe Perches955a9d202014-11-11 14:44:57 -0800855 pr_debug("%s: state=%s, event=%s\n", __func__ ,
856 ircomm_tty_state[self->state], ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 switch (event) {
859 case IRCOMM_TTY_GOT_LSAPSEL:
860 /* Connect to remote device */
861 ret = ircomm_connect_request(self->ircomm, self->dlsap_sel,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900862 self->saddr, self->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 NULL, self->service_type);
864 ircomm_tty_start_watchdog_timer(self, 3*HZ);
865 ircomm_tty_next_state(self, IRCOMM_TTY_SETUP);
866 break;
867 case IRCOMM_TTY_WD_TIMER_EXPIRED:
868 /* Go back to search mode */
869 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
870 ircomm_tty_start_watchdog_timer(self, 3*HZ);
871 break;
872 case IRCOMM_TTY_CONNECT_INDICATION:
873 del_timer(&self->watchdog_timer);
874 ircomm_tty_ias_unregister(self);
875
876 /* Accept connection */
877 ircomm_connect_response(self->ircomm, NULL);
878 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
879 break;
880 case IRCOMM_TTY_DETACH_CABLE:
881 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
882 break;
883 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800884 pr_debug("%s(), unknown event: %s\n", __func__ ,
885 ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ret = -EINVAL;
887 }
888 return ret;
889}
890
891/*
892 * Function ircomm_tty_state_setup (self, event, skb, info)
893 *
894 * Trying to connect
895 *
896 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900897static int ircomm_tty_state_setup(struct ircomm_tty_cb *self,
898 IRCOMM_TTY_EVENT event,
899 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct ircomm_tty_info *info)
901{
902 int ret = 0;
903
Joe Perches955a9d202014-11-11 14:44:57 -0800904 pr_debug("%s: state=%s, event=%s\n", __func__ ,
905 ircomm_tty_state[self->state], ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 switch (event) {
908 case IRCOMM_TTY_CONNECT_CONFIRM:
909 del_timer(&self->watchdog_timer);
910 ircomm_tty_ias_unregister(self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900911
912 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * Send initial parameters. This will also send out queued
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900914 * parameters waiting for the connection to come up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
916 ircomm_tty_send_initial_parameters(self);
917 ircomm_tty_link_established(self);
918 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
919 break;
920 case IRCOMM_TTY_CONNECT_INDICATION:
921 del_timer(&self->watchdog_timer);
922 ircomm_tty_ias_unregister(self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* Accept connection */
925 ircomm_connect_response(self->ircomm, NULL);
926 ircomm_tty_next_state(self, IRCOMM_TTY_READY);
927 break;
928 case IRCOMM_TTY_WD_TIMER_EXPIRED:
929 /* Go back to search mode */
930 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
931 ircomm_tty_start_watchdog_timer(self, 3*HZ);
932 break;
933 case IRCOMM_TTY_DETACH_CABLE:
934 /* ircomm_disconnect_request(self->ircomm, NULL); */
935 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
936 break;
937 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800938 pr_debug("%s(), unknown event: %s\n", __func__ ,
939 ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 ret = -EINVAL;
941 }
942 return ret;
943}
944
945/*
946 * Function ircomm_tty_state_ready (self, event, skb, info)
947 *
948 * IrCOMM is now connected
949 *
950 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900951static int ircomm_tty_state_ready(struct ircomm_tty_cb *self,
952 IRCOMM_TTY_EVENT event,
953 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct ircomm_tty_info *info)
955{
956 int ret = 0;
957
958 switch (event) {
959 case IRCOMM_TTY_DATA_REQUEST:
960 ret = ircomm_data_request(self->ircomm, skb);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900961 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 case IRCOMM_TTY_DETACH_CABLE:
963 ircomm_disconnect_request(self->ircomm, NULL);
964 ircomm_tty_next_state(self, IRCOMM_TTY_IDLE);
965 break;
966 case IRCOMM_TTY_DISCONNECT_INDICATION:
967 ircomm_tty_ias_register(self);
968 ircomm_tty_next_state(self, IRCOMM_TTY_SEARCH);
969 ircomm_tty_start_watchdog_timer(self, 3*HZ);
970
Jiri Slaby849d5a92012-06-04 13:35:19 +0200971 if (self->port.flags & ASYNC_CHECK_CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Drop carrier */
973 self->settings.dce = IRCOMM_DELTA_CD;
974 ircomm_tty_check_modem_status(self);
975 } else {
Joe Perches955a9d202014-11-11 14:44:57 -0800976 pr_debug("%s(), hanging up!\n", __func__);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100977 tty_port_tty_hangup(&self->port, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 break;
980 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800981 pr_debug("%s(), unknown event: %s\n", __func__ ,
982 ircomm_tty_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ret = -EINVAL;
984 }
985 return ret;
986}
987