blob: b0730ac9f3880e992d8e2b93f1f7ee60d974673c [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_event.c
4 * Version: 1.0
5 * Description: IrCOMM layer state machine
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Jun 6 20:33:11 1999
9 * Modified at: Sun Dec 12 13:44:32 1999
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 Dag Brattli, All Rights Reserved.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090023 *
24 * You should have received a copy of the GNU General Public License
Jeff Kirsherd3770502013-12-06 09:13:43 -080025 * along with this program; if not, see <http://www.gnu.org/licenses/>.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 ********************************************************************/
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/proc_fs.h>
30#include <linux/init.h>
31
32#include <net/irda/irda.h>
33#include <net/irda/irlmp.h>
34#include <net/irda/iriap.h>
35#include <net/irda/irttp.h>
36#include <net/irda/irias_object.h>
37
38#include <net/irda/ircomm_core.h>
39#include <net/irda/ircomm_event.h>
40
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090041static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct sk_buff *skb, struct ircomm_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090043static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct sk_buff *skb, struct ircomm_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090045static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct sk_buff *skb, struct ircomm_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090047static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct sk_buff *skb, struct ircomm_info *info);
49
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070050const char *const ircomm_state[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 "IRCOMM_IDLE",
52 "IRCOMM_WAITI",
53 "IRCOMM_WAITR",
54 "IRCOMM_CONN",
55};
56
Joe Perchesa7688512014-11-12 18:15:47 -080057static const char *const ircomm_event[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 "IRCOMM_CONNECT_REQUEST",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090059 "IRCOMM_CONNECT_RESPONSE",
60 "IRCOMM_TTP_CONNECT_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 "IRCOMM_LMP_CONNECT_INDICATION",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090062 "IRCOMM_TTP_CONNECT_CONFIRM",
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 "IRCOMM_LMP_CONNECT_CONFIRM",
64
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090065 "IRCOMM_LMP_DISCONNECT_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 "IRCOMM_TTP_DISCONNECT_INDICATION",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090067 "IRCOMM_DISCONNECT_REQUEST",
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090069 "IRCOMM_TTP_DATA_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 "IRCOMM_LMP_DATA_INDICATION",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090071 "IRCOMM_DATA_REQUEST",
72 "IRCOMM_CONTROL_REQUEST",
73 "IRCOMM_CONTROL_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static int (*state[])(struct ircomm_cb *self, IRCOMM_EVENT event,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090077 struct sk_buff *skb, struct ircomm_info *info) =
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 ircomm_state_idle,
80 ircomm_state_waiti,
81 ircomm_state_waitr,
82 ircomm_state_conn,
83};
84
85/*
86 * Function ircomm_state_idle (self, event, skb)
87 *
88 * IrCOMM is currently idle
89 *
90 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090091static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct sk_buff *skb, struct ircomm_info *info)
93{
94 int ret = 0;
95
96 switch (event) {
97 case IRCOMM_CONNECT_REQUEST:
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090098 ircomm_next_state(self, IRCOMM_WAITI);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ret = self->issue.connect_request(self, skb, info);
100 break;
101 case IRCOMM_TTP_CONNECT_INDICATION:
102 case IRCOMM_LMP_CONNECT_INDICATION:
103 ircomm_next_state(self, IRCOMM_WAITR);
104 ircomm_connect_indication(self, skb, info);
105 break;
106 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800107 pr_debug("%s(), unknown event: %s\n", __func__ ,
108 ircomm_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 ret = -EINVAL;
110 }
111 return ret;
112}
113
114/*
115 * Function ircomm_state_waiti (self, event, skb)
116 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900117 * The IrCOMM user has requested an IrCOMM connection to the remote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * device and is awaiting confirmation
119 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900120static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct sk_buff *skb, struct ircomm_info *info)
122{
123 int ret = 0;
124
125 switch (event) {
126 case IRCOMM_TTP_CONNECT_CONFIRM:
127 case IRCOMM_LMP_CONNECT_CONFIRM:
128 ircomm_next_state(self, IRCOMM_CONN);
129 ircomm_connect_confirm(self, skb, info);
130 break;
131 case IRCOMM_TTP_DISCONNECT_INDICATION:
132 case IRCOMM_LMP_DISCONNECT_INDICATION:
133 ircomm_next_state(self, IRCOMM_IDLE);
134 ircomm_disconnect_indication(self, skb, info);
135 break;
136 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800137 pr_debug("%s(), unknown event: %s\n", __func__ ,
138 ircomm_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 ret = -EINVAL;
140 }
141 return ret;
142}
143
144/*
145 * Function ircomm_state_waitr (self, event, skb)
146 *
147 * IrCOMM has received an incoming connection request and is awaiting
148 * response from the user
149 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900150static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
151 struct sk_buff *skb, struct ircomm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int ret = 0;
154
155 switch (event) {
156 case IRCOMM_CONNECT_RESPONSE:
157 ircomm_next_state(self, IRCOMM_CONN);
158 ret = self->issue.connect_response(self, skb);
159 break;
160 case IRCOMM_DISCONNECT_REQUEST:
161 ircomm_next_state(self, IRCOMM_IDLE);
162 ret = self->issue.disconnect_request(self, skb, info);
163 break;
164 case IRCOMM_TTP_DISCONNECT_INDICATION:
165 case IRCOMM_LMP_DISCONNECT_INDICATION:
166 ircomm_next_state(self, IRCOMM_IDLE);
167 ircomm_disconnect_indication(self, skb, info);
168 break;
169 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800170 pr_debug("%s(), unknown event = %s\n", __func__ ,
171 ircomm_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ret = -EINVAL;
173 }
174 return ret;
175}
176
177/*
178 * Function ircomm_state_conn (self, event, skb)
179 *
180 * IrCOMM is connected to the peer IrCOMM device
181 *
182 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900183static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct sk_buff *skb, struct ircomm_info *info)
185{
186 int ret = 0;
187
188 switch (event) {
189 case IRCOMM_DATA_REQUEST:
190 ret = self->issue.data_request(self, skb, 0);
191 break;
192 case IRCOMM_TTP_DATA_INDICATION:
193 ircomm_process_data(self, skb);
194 break;
195 case IRCOMM_LMP_DATA_INDICATION:
196 ircomm_data_indication(self, skb);
197 break;
198 case IRCOMM_CONTROL_REQUEST:
199 /* Just send a separate frame for now */
200 ret = self->issue.data_request(self, skb, skb->len);
201 break;
202 case IRCOMM_TTP_DISCONNECT_INDICATION:
203 case IRCOMM_LMP_DISCONNECT_INDICATION:
204 ircomm_next_state(self, IRCOMM_IDLE);
205 ircomm_disconnect_indication(self, skb, info);
206 break;
207 case IRCOMM_DISCONNECT_REQUEST:
208 ircomm_next_state(self, IRCOMM_IDLE);
209 ret = self->issue.disconnect_request(self, skb, info);
210 break;
211 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800212 pr_debug("%s(), unknown event = %s\n", __func__ ,
213 ircomm_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ret = -EINVAL;
215 }
216 return ret;
217}
218
219/*
220 * Function ircomm_do_event (self, event, skb)
221 *
222 * Process event
223 *
224 */
225int ircomm_do_event(struct ircomm_cb *self, IRCOMM_EVENT event,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900226 struct sk_buff *skb, struct ircomm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Joe Perches955a9d202014-11-11 14:44:57 -0800228 pr_debug("%s: state=%s, event=%s\n", __func__ ,
229 ircomm_state[self->state], ircomm_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 return (*state[self->state])(self, event, skb, info);
232}
233
234/*
235 * Function ircomm_next_state (self, state)
236 *
237 * Switch state
238 *
239 */
240void ircomm_next_state(struct ircomm_cb *self, IRCOMM_STATE state)
241{
242 self->state = state;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900243
Joe Perches955a9d202014-11-11 14:44:57 -0800244 pr_debug("%s: next state=%s, service type=%d\n", __func__ ,
245 ircomm_state[self->state], self->service_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}