Merge by hand (conflicts between pending drivers and kfree cleanups)
[linux-2.6.22.y-op.git] / net / netrom / nr_timer.c
blob75b72d389ba95283b4c0f1e9adfc69eb8d3ed2a5
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
9 */
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
13 #include <linux/in.h>
14 #include <linux/kernel.h>
15 #include <linux/jiffies.h>
16 #include <linux/timer.h>
17 #include <linux/string.h>
18 #include <linux/sockios.h>
19 #include <linux/net.h>
20 #include <net/ax25.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <net/sock.h>
25 #include <net/tcp_states.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/interrupt.h>
31 #include <net/netrom.h>
33 static void nr_heartbeat_expiry(unsigned long);
34 static void nr_t1timer_expiry(unsigned long);
35 static void nr_t2timer_expiry(unsigned long);
36 static void nr_t4timer_expiry(unsigned long);
37 static void nr_idletimer_expiry(unsigned long);
39 void nr_init_timers(struct sock *sk)
41 struct nr_sock *nr = nr_sk(sk);
43 init_timer(&nr->t1timer);
44 nr->t1timer.data = (unsigned long)sk;
45 nr->t1timer.function = &nr_t1timer_expiry;
47 init_timer(&nr->t2timer);
48 nr->t2timer.data = (unsigned long)sk;
49 nr->t2timer.function = &nr_t2timer_expiry;
51 init_timer(&nr->t4timer);
52 nr->t4timer.data = (unsigned long)sk;
53 nr->t4timer.function = &nr_t4timer_expiry;
55 init_timer(&nr->idletimer);
56 nr->idletimer.data = (unsigned long)sk;
57 nr->idletimer.function = &nr_idletimer_expiry;
59 /* initialized by sock_init_data */
60 sk->sk_timer.data = (unsigned long)sk;
61 sk->sk_timer.function = &nr_heartbeat_expiry;
64 void nr_start_t1timer(struct sock *sk)
66 struct nr_sock *nr = nr_sk(sk);
68 mod_timer(&nr->t1timer, jiffies + nr->t1);
71 void nr_start_t2timer(struct sock *sk)
73 struct nr_sock *nr = nr_sk(sk);
75 mod_timer(&nr->t2timer, jiffies + nr->t2);
78 void nr_start_t4timer(struct sock *sk)
80 struct nr_sock *nr = nr_sk(sk);
82 mod_timer(&nr->t4timer, jiffies + nr->t4);
85 void nr_start_idletimer(struct sock *sk)
87 struct nr_sock *nr = nr_sk(sk);
89 if (nr->idle > 0)
90 mod_timer(&nr->idletimer, jiffies + nr->idle);
93 void nr_start_heartbeat(struct sock *sk)
95 mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
98 void nr_stop_t1timer(struct sock *sk)
100 del_timer(&nr_sk(sk)->t1timer);
103 void nr_stop_t2timer(struct sock *sk)
105 del_timer(&nr_sk(sk)->t2timer);
108 void nr_stop_t4timer(struct sock *sk)
110 del_timer(&nr_sk(sk)->t4timer);
113 void nr_stop_idletimer(struct sock *sk)
115 del_timer(&nr_sk(sk)->idletimer);
118 void nr_stop_heartbeat(struct sock *sk)
120 del_timer(&sk->sk_timer);
123 int nr_t1timer_running(struct sock *sk)
125 return timer_pending(&nr_sk(sk)->t1timer);
128 static void nr_heartbeat_expiry(unsigned long param)
130 struct sock *sk = (struct sock *)param;
131 struct nr_sock *nr = nr_sk(sk);
133 bh_lock_sock(sk);
134 switch (nr->state) {
135 case NR_STATE_0:
136 /* Magic here: If we listen() and a new link dies before it
137 is accepted() it isn't 'dead' so doesn't get removed. */
138 if (sock_flag(sk, SOCK_DESTROY) ||
139 (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
140 sock_hold(sk);
141 nr_destroy_socket(sk);
142 bh_unlock_sock(sk);
143 sock_put(sk);
144 return;
146 break;
148 case NR_STATE_3:
150 * Check for the state of the receive buffer.
152 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
153 (nr->condition & NR_COND_OWN_RX_BUSY)) {
154 nr->condition &= ~NR_COND_OWN_RX_BUSY;
155 nr->condition &= ~NR_COND_ACK_PENDING;
156 nr->vl = nr->vr;
157 nr_write_internal(sk, NR_INFOACK);
158 break;
160 break;
163 nr_start_heartbeat(sk);
164 bh_unlock_sock(sk);
167 static void nr_t2timer_expiry(unsigned long param)
169 struct sock *sk = (struct sock *)param;
170 struct nr_sock *nr = nr_sk(sk);
172 bh_lock_sock(sk);
173 if (nr->condition & NR_COND_ACK_PENDING) {
174 nr->condition &= ~NR_COND_ACK_PENDING;
175 nr_enquiry_response(sk);
177 bh_unlock_sock(sk);
180 static void nr_t4timer_expiry(unsigned long param)
182 struct sock *sk = (struct sock *)param;
184 bh_lock_sock(sk);
185 nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
186 bh_unlock_sock(sk);
189 static void nr_idletimer_expiry(unsigned long param)
191 struct sock *sk = (struct sock *)param;
192 struct nr_sock *nr = nr_sk(sk);
194 bh_lock_sock(sk);
196 nr_clear_queues(sk);
198 nr->n2count = 0;
199 nr_write_internal(sk, NR_DISCREQ);
200 nr->state = NR_STATE_2;
202 nr_start_t1timer(sk);
203 nr_stop_t2timer(sk);
204 nr_stop_t4timer(sk);
206 sk->sk_state = TCP_CLOSE;
207 sk->sk_err = 0;
208 sk->sk_shutdown |= SEND_SHUTDOWN;
210 if (!sock_flag(sk, SOCK_DEAD)) {
211 sk->sk_state_change(sk);
212 sock_set_flag(sk, SOCK_DEAD);
214 bh_unlock_sock(sk);
217 static void nr_t1timer_expiry(unsigned long param)
219 struct sock *sk = (struct sock *)param;
220 struct nr_sock *nr = nr_sk(sk);
222 bh_lock_sock(sk);
223 switch (nr->state) {
224 case NR_STATE_1:
225 if (nr->n2count == nr->n2) {
226 nr_disconnect(sk, ETIMEDOUT);
227 bh_unlock_sock(sk);
228 return;
229 } else {
230 nr->n2count++;
231 nr_write_internal(sk, NR_CONNREQ);
233 break;
235 case NR_STATE_2:
236 if (nr->n2count == nr->n2) {
237 nr_disconnect(sk, ETIMEDOUT);
238 bh_unlock_sock(sk);
239 return;
240 } else {
241 nr->n2count++;
242 nr_write_internal(sk, NR_DISCREQ);
244 break;
246 case NR_STATE_3:
247 if (nr->n2count == nr->n2) {
248 nr_disconnect(sk, ETIMEDOUT);
249 bh_unlock_sock(sk);
250 return;
251 } else {
252 nr->n2count++;
253 nr_requeue_frames(sk);
255 break;
258 nr_start_t1timer(sk);
259 bh_unlock_sock(sk);