Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /********************************************************************* |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Copyright (c) 1999 Dag Brattli, All Rights Reserved. |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 13 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * the License, or (at your option) any later version. |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * 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 Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | d377050 | 2013-12-06 09:13:43 -0800 | [diff] [blame] | 25 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 26 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | ********************************************************************/ |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #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 Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 41 | static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct sk_buff *skb, struct ircomm_info *info); |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 43 | static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct sk_buff *skb, struct ircomm_info *info); |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 45 | static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | struct sk_buff *skb, struct ircomm_info *info); |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 47 | static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct sk_buff *skb, struct ircomm_info *info); |
| 49 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 50 | const char *const ircomm_state[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | "IRCOMM_IDLE", |
| 52 | "IRCOMM_WAITI", |
| 53 | "IRCOMM_WAITR", |
| 54 | "IRCOMM_CONN", |
| 55 | }; |
| 56 | |
| 57 | #ifdef CONFIG_IRDA_DEBUG |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 58 | static const char *const ircomm_event[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | "IRCOMM_CONNECT_REQUEST", |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 60 | "IRCOMM_CONNECT_RESPONSE", |
| 61 | "IRCOMM_TTP_CONNECT_INDICATION", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | "IRCOMM_LMP_CONNECT_INDICATION", |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 63 | "IRCOMM_TTP_CONNECT_CONFIRM", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | "IRCOMM_LMP_CONNECT_CONFIRM", |
| 65 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 66 | "IRCOMM_LMP_DISCONNECT_INDICATION", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | "IRCOMM_TTP_DISCONNECT_INDICATION", |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 68 | "IRCOMM_DISCONNECT_REQUEST", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 70 | "IRCOMM_TTP_DATA_INDICATION", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | "IRCOMM_LMP_DATA_INDICATION", |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 72 | "IRCOMM_DATA_REQUEST", |
| 73 | "IRCOMM_CONTROL_REQUEST", |
| 74 | "IRCOMM_CONTROL_INDICATION", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | }; |
| 76 | #endif /* CONFIG_IRDA_DEBUG */ |
| 77 | |
| 78 | static int (*state[])(struct ircomm_cb *self, IRCOMM_EVENT event, |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 79 | struct sk_buff *skb, struct ircomm_info *info) = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | ircomm_state_idle, |
| 82 | ircomm_state_waiti, |
| 83 | ircomm_state_waitr, |
| 84 | ircomm_state_conn, |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * Function ircomm_state_idle (self, event, skb) |
| 89 | * |
| 90 | * IrCOMM is currently idle |
| 91 | * |
| 92 | */ |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 93 | static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct sk_buff *skb, struct ircomm_info *info) |
| 95 | { |
| 96 | int ret = 0; |
| 97 | |
| 98 | switch (event) { |
| 99 | case IRCOMM_CONNECT_REQUEST: |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 100 | ircomm_next_state(self, IRCOMM_WAITI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | ret = self->issue.connect_request(self, skb, info); |
| 102 | break; |
| 103 | case IRCOMM_TTP_CONNECT_INDICATION: |
| 104 | case IRCOMM_LMP_CONNECT_INDICATION: |
| 105 | ircomm_next_state(self, IRCOMM_WAITR); |
| 106 | ircomm_connect_indication(self, skb, info); |
| 107 | break; |
| 108 | default: |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame^] | 109 | pr_debug("%s(), unknown event: %s\n", __func__ , |
| 110 | ircomm_event[event]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | ret = -EINVAL; |
| 112 | } |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Function ircomm_state_waiti (self, event, skb) |
| 118 | * |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 119 | * The IrCOMM user has requested an IrCOMM connection to the remote |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * device and is awaiting confirmation |
| 121 | */ |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 122 | static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct sk_buff *skb, struct ircomm_info *info) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | |
| 127 | switch (event) { |
| 128 | case IRCOMM_TTP_CONNECT_CONFIRM: |
| 129 | case IRCOMM_LMP_CONNECT_CONFIRM: |
| 130 | ircomm_next_state(self, IRCOMM_CONN); |
| 131 | ircomm_connect_confirm(self, skb, info); |
| 132 | break; |
| 133 | case IRCOMM_TTP_DISCONNECT_INDICATION: |
| 134 | case IRCOMM_LMP_DISCONNECT_INDICATION: |
| 135 | ircomm_next_state(self, IRCOMM_IDLE); |
| 136 | ircomm_disconnect_indication(self, skb, info); |
| 137 | break; |
| 138 | default: |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame^] | 139 | pr_debug("%s(), unknown event: %s\n", __func__ , |
| 140 | ircomm_event[event]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | ret = -EINVAL; |
| 142 | } |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Function ircomm_state_waitr (self, event, skb) |
| 148 | * |
| 149 | * IrCOMM has received an incoming connection request and is awaiting |
| 150 | * response from the user |
| 151 | */ |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 152 | static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event, |
| 153 | struct sk_buff *skb, struct ircomm_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | int ret = 0; |
| 156 | |
| 157 | switch (event) { |
| 158 | case IRCOMM_CONNECT_RESPONSE: |
| 159 | ircomm_next_state(self, IRCOMM_CONN); |
| 160 | ret = self->issue.connect_response(self, skb); |
| 161 | break; |
| 162 | case IRCOMM_DISCONNECT_REQUEST: |
| 163 | ircomm_next_state(self, IRCOMM_IDLE); |
| 164 | ret = self->issue.disconnect_request(self, skb, info); |
| 165 | break; |
| 166 | case IRCOMM_TTP_DISCONNECT_INDICATION: |
| 167 | case IRCOMM_LMP_DISCONNECT_INDICATION: |
| 168 | ircomm_next_state(self, IRCOMM_IDLE); |
| 169 | ircomm_disconnect_indication(self, skb, info); |
| 170 | break; |
| 171 | default: |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame^] | 172 | pr_debug("%s(), unknown event = %s\n", __func__ , |
| 173 | ircomm_event[event]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | ret = -EINVAL; |
| 175 | } |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Function ircomm_state_conn (self, event, skb) |
| 181 | * |
| 182 | * IrCOMM is connected to the peer IrCOMM device |
| 183 | * |
| 184 | */ |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 185 | static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | struct sk_buff *skb, struct ircomm_info *info) |
| 187 | { |
| 188 | int ret = 0; |
| 189 | |
| 190 | switch (event) { |
| 191 | case IRCOMM_DATA_REQUEST: |
| 192 | ret = self->issue.data_request(self, skb, 0); |
| 193 | break; |
| 194 | case IRCOMM_TTP_DATA_INDICATION: |
| 195 | ircomm_process_data(self, skb); |
| 196 | break; |
| 197 | case IRCOMM_LMP_DATA_INDICATION: |
| 198 | ircomm_data_indication(self, skb); |
| 199 | break; |
| 200 | case IRCOMM_CONTROL_REQUEST: |
| 201 | /* Just send a separate frame for now */ |
| 202 | ret = self->issue.data_request(self, skb, skb->len); |
| 203 | break; |
| 204 | case IRCOMM_TTP_DISCONNECT_INDICATION: |
| 205 | case IRCOMM_LMP_DISCONNECT_INDICATION: |
| 206 | ircomm_next_state(self, IRCOMM_IDLE); |
| 207 | ircomm_disconnect_indication(self, skb, info); |
| 208 | break; |
| 209 | case IRCOMM_DISCONNECT_REQUEST: |
| 210 | ircomm_next_state(self, IRCOMM_IDLE); |
| 211 | ret = self->issue.disconnect_request(self, skb, info); |
| 212 | break; |
| 213 | default: |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame^] | 214 | pr_debug("%s(), unknown event = %s\n", __func__ , |
| 215 | ircomm_event[event]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | ret = -EINVAL; |
| 217 | } |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Function ircomm_do_event (self, event, skb) |
| 223 | * |
| 224 | * Process event |
| 225 | * |
| 226 | */ |
| 227 | int ircomm_do_event(struct ircomm_cb *self, IRCOMM_EVENT event, |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 228 | struct sk_buff *skb, struct ircomm_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame^] | 230 | pr_debug("%s: state=%s, event=%s\n", __func__ , |
| 231 | ircomm_state[self->state], ircomm_event[event]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | return (*state[self->state])(self, event, skb, info); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Function ircomm_next_state (self, state) |
| 238 | * |
| 239 | * Switch state |
| 240 | * |
| 241 | */ |
| 242 | void ircomm_next_state(struct ircomm_cb *self, IRCOMM_STATE state) |
| 243 | { |
| 244 | self->state = state; |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 245 | |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame^] | 246 | pr_debug("%s: next state=%s, service type=%d\n", __func__ , |
| 247 | ircomm_state[self->state], self->service_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |