blob: 23d0468794e2dca53c42693ed543d543c44512e3 [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
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * MA 02111-1307 USA
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 ********************************************************************/
30
31#include <linux/sched.h>
32#include <linux/proc_fs.h>
33#include <linux/init.h>
34
35#include <net/irda/irda.h>
36#include <net/irda/irlmp.h>
37#include <net/irda/iriap.h>
38#include <net/irda/irttp.h>
39#include <net/irda/irias_object.h>
40
41#include <net/irda/ircomm_core.h>
42#include <net/irda/ircomm_event.h>
43
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090044static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct sk_buff *skb, struct ircomm_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090046static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct sk_buff *skb, struct ircomm_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090048static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct sk_buff *skb, struct ircomm_info *info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090050static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct sk_buff *skb, struct ircomm_info *info);
52
53char *ircomm_state[] = {
54 "IRCOMM_IDLE",
55 "IRCOMM_WAITI",
56 "IRCOMM_WAITR",
57 "IRCOMM_CONN",
58};
59
60#ifdef CONFIG_IRDA_DEBUG
61static char *ircomm_event[] = {
62 "IRCOMM_CONNECT_REQUEST",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090063 "IRCOMM_CONNECT_RESPONSE",
64 "IRCOMM_TTP_CONNECT_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 "IRCOMM_LMP_CONNECT_INDICATION",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090066 "IRCOMM_TTP_CONNECT_CONFIRM",
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 "IRCOMM_LMP_CONNECT_CONFIRM",
68
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090069 "IRCOMM_LMP_DISCONNECT_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 "IRCOMM_TTP_DISCONNECT_INDICATION",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090071 "IRCOMM_DISCONNECT_REQUEST",
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090073 "IRCOMM_TTP_DATA_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 "IRCOMM_LMP_DATA_INDICATION",
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090075 "IRCOMM_DATA_REQUEST",
76 "IRCOMM_CONTROL_REQUEST",
77 "IRCOMM_CONTROL_INDICATION",
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79#endif /* CONFIG_IRDA_DEBUG */
80
81static int (*state[])(struct ircomm_cb *self, IRCOMM_EVENT event,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090082 struct sk_buff *skb, struct ircomm_info *info) =
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 ircomm_state_idle,
85 ircomm_state_waiti,
86 ircomm_state_waitr,
87 ircomm_state_conn,
88};
89
90/*
91 * Function ircomm_state_idle (self, event, skb)
92 *
93 * IrCOMM is currently idle
94 *
95 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090096static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct sk_buff *skb, struct ircomm_info *info)
98{
99 int ret = 0;
100
101 switch (event) {
102 case IRCOMM_CONNECT_REQUEST:
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900103 ircomm_next_state(self, IRCOMM_WAITI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ret = self->issue.connect_request(self, skb, info);
105 break;
106 case IRCOMM_TTP_CONNECT_INDICATION:
107 case IRCOMM_LMP_CONNECT_INDICATION:
108 ircomm_next_state(self, IRCOMM_WAITR);
109 ircomm_connect_indication(self, skb, info);
110 break;
111 default:
112 IRDA_DEBUG(4, "%s(), unknown event: %s\n", __FUNCTION__ ,
113 ircomm_event[event]);
114 ret = -EINVAL;
115 }
116 return ret;
117}
118
119/*
120 * Function ircomm_state_waiti (self, event, skb)
121 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900122 * The IrCOMM user has requested an IrCOMM connection to the remote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * device and is awaiting confirmation
124 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900125static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct sk_buff *skb, struct ircomm_info *info)
127{
128 int ret = 0;
129
130 switch (event) {
131 case IRCOMM_TTP_CONNECT_CONFIRM:
132 case IRCOMM_LMP_CONNECT_CONFIRM:
133 ircomm_next_state(self, IRCOMM_CONN);
134 ircomm_connect_confirm(self, skb, info);
135 break;
136 case IRCOMM_TTP_DISCONNECT_INDICATION:
137 case IRCOMM_LMP_DISCONNECT_INDICATION:
138 ircomm_next_state(self, IRCOMM_IDLE);
139 ircomm_disconnect_indication(self, skb, info);
140 break;
141 default:
142 IRDA_DEBUG(0, "%s(), unknown event: %s\n", __FUNCTION__ ,
143 ircomm_event[event]);
144 ret = -EINVAL;
145 }
146 return ret;
147}
148
149/*
150 * Function ircomm_state_waitr (self, event, skb)
151 *
152 * IrCOMM has received an incoming connection request and is awaiting
153 * response from the user
154 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900155static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
156 struct sk_buff *skb, struct ircomm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int ret = 0;
159
160 switch (event) {
161 case IRCOMM_CONNECT_RESPONSE:
162 ircomm_next_state(self, IRCOMM_CONN);
163 ret = self->issue.connect_response(self, skb);
164 break;
165 case IRCOMM_DISCONNECT_REQUEST:
166 ircomm_next_state(self, IRCOMM_IDLE);
167 ret = self->issue.disconnect_request(self, skb, info);
168 break;
169 case IRCOMM_TTP_DISCONNECT_INDICATION:
170 case IRCOMM_LMP_DISCONNECT_INDICATION:
171 ircomm_next_state(self, IRCOMM_IDLE);
172 ircomm_disconnect_indication(self, skb, info);
173 break;
174 default:
175 IRDA_DEBUG(0, "%s(), unknown event = %s\n", __FUNCTION__ ,
176 ircomm_event[event]);
177 ret = -EINVAL;
178 }
179 return ret;
180}
181
182/*
183 * Function ircomm_state_conn (self, event, skb)
184 *
185 * IrCOMM is connected to the peer IrCOMM device
186 *
187 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900188static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct sk_buff *skb, struct ircomm_info *info)
190{
191 int ret = 0;
192
193 switch (event) {
194 case IRCOMM_DATA_REQUEST:
195 ret = self->issue.data_request(self, skb, 0);
196 break;
197 case IRCOMM_TTP_DATA_INDICATION:
198 ircomm_process_data(self, skb);
199 break;
200 case IRCOMM_LMP_DATA_INDICATION:
201 ircomm_data_indication(self, skb);
202 break;
203 case IRCOMM_CONTROL_REQUEST:
204 /* Just send a separate frame for now */
205 ret = self->issue.data_request(self, skb, skb->len);
206 break;
207 case IRCOMM_TTP_DISCONNECT_INDICATION:
208 case IRCOMM_LMP_DISCONNECT_INDICATION:
209 ircomm_next_state(self, IRCOMM_IDLE);
210 ircomm_disconnect_indication(self, skb, info);
211 break;
212 case IRCOMM_DISCONNECT_REQUEST:
213 ircomm_next_state(self, IRCOMM_IDLE);
214 ret = self->issue.disconnect_request(self, skb, info);
215 break;
216 default:
217 IRDA_DEBUG(0, "%s(), unknown event = %s\n", __FUNCTION__ ,
218 ircomm_event[event]);
219 ret = -EINVAL;
220 }
221 return ret;
222}
223
224/*
225 * Function ircomm_do_event (self, event, skb)
226 *
227 * Process event
228 *
229 */
230int ircomm_do_event(struct ircomm_cb *self, IRCOMM_EVENT event,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900231 struct sk_buff *skb, struct ircomm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 IRDA_DEBUG(4, "%s: state=%s, event=%s\n", __FUNCTION__ ,
234 ircomm_state[self->state], ircomm_event[event]);
235
236 return (*state[self->state])(self, event, skb, info);
237}
238
239/*
240 * Function ircomm_next_state (self, state)
241 *
242 * Switch state
243 *
244 */
245void ircomm_next_state(struct ircomm_cb *self, IRCOMM_STATE state)
246{
247 self->state = state;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900248
249 IRDA_DEBUG(4, "%s: next state=%s, service type=%d\n", __FUNCTION__ ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ircomm_state[self->state], self->service_type);
251}