[PATCH] jiffies.h
[opensuse:kernel.git] / net / netrom / nr_timer.c
1 /*
2  *      NET/ROM release 007
3  *
4  *      This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *      This module:
7  *              This module is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  *      History
13  *      NET/ROM 001     Jonathan(G4KLX) Cloned from ax25_timer.c
14  *      NET/ROM 007     Jonathan(G4KLX) New timer architecture.
15  *                                      Implemented idle timer.
16  */
17
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
21 #include <linux/in.h>
22 #include <linux/kernel.h>
23 #include <linux/jiffies.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <net/ax25.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <net/sock.h>
33 #include <net/tcp.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <linux/fcntl.h>
37 #include <linux/mm.h>
38 #include <linux/interrupt.h>
39 #include <net/netrom.h>
40
41 static void nr_heartbeat_expiry(unsigned long);
42 static void nr_t1timer_expiry(unsigned long);
43 static void nr_t2timer_expiry(unsigned long);
44 static void nr_t4timer_expiry(unsigned long);
45 static void nr_idletimer_expiry(unsigned long);
46
47 void nr_start_t1timer(struct sock *sk)
48 {
49         nr_cb *nr = nr_sk(sk);
50
51         del_timer(&nr->t1timer);
52
53         nr->t1timer.data     = (unsigned long)sk;
54         nr->t1timer.function = &nr_t1timer_expiry;
55         nr->t1timer.expires  = jiffies + nr->t1;
56
57         add_timer(&nr->t1timer);
58 }
59
60 void nr_start_t2timer(struct sock *sk)
61 {
62         nr_cb *nr = nr_sk(sk);
63
64         del_timer(&nr->t2timer);
65
66         nr->t2timer.data     = (unsigned long)sk;
67         nr->t2timer.function = &nr_t2timer_expiry;
68         nr->t2timer.expires  = jiffies + nr->t2;
69
70         add_timer(&nr->t2timer);
71 }
72
73 void nr_start_t4timer(struct sock *sk)
74 {
75         nr_cb *nr = nr_sk(sk);
76
77         del_timer(&nr->t4timer);
78
79         nr->t4timer.data     = (unsigned long)sk;
80         nr->t4timer.function = &nr_t4timer_expiry;
81         nr->t4timer.expires  = jiffies + nr->t4;
82
83         add_timer(&nr->t4timer);
84 }
85
86 void nr_start_idletimer(struct sock *sk)
87 {
88         nr_cb *nr = nr_sk(sk);
89
90         del_timer(&nr->idletimer);
91
92         if (nr->idle > 0) {
93                 nr->idletimer.data     = (unsigned long)sk;
94                 nr->idletimer.function = &nr_idletimer_expiry;
95                 nr->idletimer.expires  = jiffies + nr->idle;
96
97                 add_timer(&nr->idletimer);
98         }
99 }
100
101 void nr_start_heartbeat(struct sock *sk)
102 {
103         del_timer(&sk->timer);
104
105         sk->timer.data     = (unsigned long)sk;
106         sk->timer.function = &nr_heartbeat_expiry;
107         sk->timer.expires  = jiffies + 5 * HZ;
108
109         add_timer(&sk->timer);
110 }
111
112 void nr_stop_t1timer(struct sock *sk)
113 {
114         del_timer(&nr_sk(sk)->t1timer);
115 }
116
117 void nr_stop_t2timer(struct sock *sk)
118 {
119         del_timer(&nr_sk(sk)->t2timer);
120 }
121
122 void nr_stop_t4timer(struct sock *sk)
123 {
124         del_timer(&nr_sk(sk)->t4timer);
125 }
126
127 void nr_stop_idletimer(struct sock *sk)
128 {
129         del_timer(&nr_sk(sk)->idletimer);
130 }
131
132 void nr_stop_heartbeat(struct sock *sk)
133 {
134         del_timer(&sk->timer);
135 }
136
137 int nr_t1timer_running(struct sock *sk)
138 {
139         return timer_pending(&nr_sk(sk)->t1timer);
140 }
141
142 static void nr_heartbeat_expiry(unsigned long param)
143 {
144         struct sock *sk = (struct sock *)param;
145         nr_cb *nr = nr_sk(sk);
146
147         switch (nr->state) {
148
149                 case NR_STATE_0:
150                         /* Magic here: If we listen() and a new link dies before it
151                            is accepted() it isn't 'dead' so doesn't get removed. */
152                         if (sk->destroy || (sk->state == TCP_LISTEN && sk->dead)) {
153                                 nr_destroy_socket(sk);
154                                 return;
155                         }
156                         break;
157
158                 case NR_STATE_3:
159                         /*
160                          * Check for the state of the receive buffer.
161                          */
162                         if (atomic_read(&sk->rmem_alloc) < (sk->rcvbuf / 2) &&
163                             (nr->condition & NR_COND_OWN_RX_BUSY)) {
164                                 nr->condition &= ~NR_COND_OWN_RX_BUSY;
165                                 nr->condition &= ~NR_COND_ACK_PENDING;
166                                 nr->vl         = nr->vr;
167                                 nr_write_internal(sk, NR_INFOACK);
168                                 break;
169                         }
170                         break;
171         }
172
173         nr_start_heartbeat(sk);
174 }
175
176 static void nr_t2timer_expiry(unsigned long param)
177 {
178         struct sock *sk = (struct sock *)param;
179         nr_cb *nr = nr_sk(sk);
180
181         if (nr->condition & NR_COND_ACK_PENDING) {
182                 nr->condition &= ~NR_COND_ACK_PENDING;
183                 nr_enquiry_response(sk);
184         }
185 }
186
187 static void nr_t4timer_expiry(unsigned long param)
188 {
189         struct sock *sk = (struct sock *)param;
190
191         nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
192 }
193
194 static void nr_idletimer_expiry(unsigned long param)
195 {
196         struct sock *sk = (struct sock *)param;
197         nr_cb *nr = nr_sk(sk);
198
199         nr_clear_queues(sk);
200
201         nr->n2count = 0;
202         nr_write_internal(sk, NR_DISCREQ);
203         nr->state = NR_STATE_2;
204
205         nr_start_t1timer(sk);
206         nr_stop_t2timer(sk);
207         nr_stop_t4timer(sk);
208
209         sk->state     = TCP_CLOSE;
210         sk->err       = 0;
211         sk->shutdown |= SEND_SHUTDOWN;
212
213         if (!sk->dead)
214                 sk->state_change(sk);
215
216         sk->dead = 1;
217 }
218
219 static void nr_t1timer_expiry(unsigned long param)
220 {
221         struct sock *sk = (struct sock *)param;
222         nr_cb *nr = nr_sk(sk);
223
224         switch (nr->state) {
225
226                 case NR_STATE_1: 
227                         if (nr->n2count == nr->n2) {
228                                 nr_disconnect(sk, ETIMEDOUT);
229                                 return;
230                         } else {
231                                 nr->n2count++;
232                                 nr_write_internal(sk, NR_CONNREQ);
233                         }
234                         break;
235
236                 case NR_STATE_2:
237                         if (nr->n2count == nr->n2) {
238                                 nr_disconnect(sk, ETIMEDOUT);
239                                 return;
240                         } else {
241                                 nr->n2count++;
242                                 nr_write_internal(sk, NR_DISCREQ);
243                         }
244                         break;
245
246                 case NR_STATE_3:
247                         if (nr->n2count == nr->n2) {
248                                 nr_disconnect(sk, ETIMEDOUT);
249                                 return;
250                         } else {
251                                 nr->n2count++;
252                                 nr_requeue_frames(sk);
253                         }
254                         break;
255         }
256
257         nr_start_t1timer(sk);
258 }