ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / wan / hdlc_generic.c
1 /*
2  * Generic HDLC support routines for Linux
3  *
4  * Copyright (C) 1999 - 2003 Krzysztof Halasa <khc@pm.waw.pl>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License
8  * as published by the Free Software Foundation.
9  *
10  * Currently supported:
11  *      * raw IP-in-HDLC
12  *      * Cisco HDLC
13  *      * Frame Relay with ANSI or CCITT LMI (both user and network side)
14  *      * PPP
15  *      * X.25
16  *
17  * Use sethdlc utility to set line parameters, protocol and PVCs
18  */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/poll.h>
25 #include <linux/errno.h>
26 #include <linux/if_arp.h>
27 #include <linux/init.h>
28 #include <linux/skbuff.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/inetdevice.h>
31 #include <linux/lapb.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/hdlc.h>
34
35
36 static const char* version = "HDLC support module revision 1.16";
37
38 #undef DEBUG_LINK
39
40
41 static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
42 {
43         if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
44                 return -EINVAL;
45         dev->mtu = new_mtu;
46         return 0;
47 }
48
49
50
51 static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
52 {
53         return hdlc_stats(dev);
54 }
55
56
57
58 static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
59                     struct packet_type *p)
60 {
61         hdlc_device *hdlc = dev_to_hdlc(dev);
62         if (hdlc->proto.netif_rx)
63                 return hdlc->proto.netif_rx(skb);
64
65         hdlc->stats.rx_dropped++; /* Shouldn't happen */
66         dev_kfree_skb(skb);
67         return NET_RX_DROP;
68 }
69
70
71
72 void hdlc_set_carrier(int on, struct net_device *dev)
73 {
74         hdlc_device *hdlc = dev_to_hdlc(dev);
75         on = on ? 1 : 0;
76
77 #ifdef DEBUG_LINK
78         printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
79 #endif
80
81         spin_lock_irq(&hdlc->state_lock);
82
83         if (hdlc->carrier == on)
84                 goto carrier_exit; /* no change in DCD line level */
85
86         printk(KERN_INFO "%s: carrier %s\n", dev->name,
87                on ? "ON" : "off");
88         hdlc->carrier = on;
89
90         if (!hdlc->open)
91                 goto carrier_exit;
92
93         if (hdlc->carrier) {
94                 if (hdlc->proto.start)
95                         hdlc->proto.start(dev);
96                 else if (!netif_carrier_ok(dev))
97                         netif_carrier_on(dev);
98
99         } else { /* no carrier */
100                 if (hdlc->proto.stop)
101                         hdlc->proto.stop(dev);
102                 else if (netif_carrier_ok(dev))
103                         netif_carrier_off(dev);
104         }
105
106  carrier_exit:
107         spin_unlock_irq(&hdlc->state_lock);
108 }
109
110
111 /* Must be called by hardware driver when HDLC device is being opened */
112 int hdlc_open(struct net_device *dev)
113 {
114         hdlc_device *hdlc = dev_to_hdlc(dev);
115 #ifdef DEBUG_LINK
116         printk(KERN_DEBUG "hdlc_open carrier %i open %i\n",
117                hdlc->carrier, hdlc->open);
118 #endif
119
120         if (hdlc->proto.id == -1)
121                 return -ENOSYS; /* no protocol attached */
122
123         if (hdlc->proto.open) {
124                 int result = hdlc->proto.open(dev);
125                 if (result)
126                         return result;
127         }
128
129         spin_lock_irq(&hdlc->state_lock);
130
131         if (hdlc->carrier) {
132                 if (hdlc->proto.start)
133                         hdlc->proto.start(dev);
134                 else if (!netif_carrier_ok(dev))
135                         netif_carrier_on(dev);
136
137         } else if (netif_carrier_ok(dev))
138                 netif_carrier_off(dev);
139
140         hdlc->open = 1;
141
142         spin_unlock_irq(&hdlc->state_lock);
143         return 0;
144 }
145
146
147
148 /* Must be called by hardware driver when HDLC device is being closed */
149 void hdlc_close(struct net_device *dev)
150 {
151         hdlc_device *hdlc = dev_to_hdlc(dev);
152 #ifdef DEBUG_LINK
153         printk(KERN_DEBUG "hdlc_close carrier %i open %i\n",
154                hdlc->carrier, hdlc->open);
155 #endif
156
157         spin_lock_irq(&hdlc->state_lock);
158
159         hdlc->open = 0;
160         if (hdlc->carrier && hdlc->proto.stop)
161                 hdlc->proto.stop(dev);
162
163         spin_unlock_irq(&hdlc->state_lock);
164
165         if (hdlc->proto.close)
166                 hdlc->proto.close(dev);
167 }
168
169
170
171 #ifndef CONFIG_HDLC_RAW
172 #define hdlc_raw_ioctl(dev, ifr)        -ENOSYS
173 #endif
174
175 #ifndef CONFIG_HDLC_RAW_ETH
176 #define hdlc_raw_eth_ioctl(dev, ifr)    -ENOSYS
177 #endif
178
179 #ifndef CONFIG_HDLC_PPP
180 #define hdlc_ppp_ioctl(dev, ifr)        -ENOSYS
181 #endif
182
183 #ifndef CONFIG_HDLC_CISCO
184 #define hdlc_cisco_ioctl(dev, ifr)      -ENOSYS
185 #endif
186
187 #ifndef CONFIG_HDLC_FR
188 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
189 #endif
190
191 #ifndef CONFIG_HDLC_X25
192 #define hdlc_x25_ioctl(dev, ifr)        -ENOSYS
193 #endif
194
195
196 int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
197 {
198         hdlc_device *hdlc = dev_to_hdlc(dev);
199         unsigned int proto;
200
201         if (cmd != SIOCWANDEV)
202                 return -EINVAL;
203
204         switch(ifr->ifr_settings.type) {
205         case IF_PROTO_HDLC:
206         case IF_PROTO_HDLC_ETH:
207         case IF_PROTO_PPP:
208         case IF_PROTO_CISCO:
209         case IF_PROTO_FR:
210         case IF_PROTO_X25:
211                 proto = ifr->ifr_settings.type;
212                 break;
213
214         default:
215                 proto = hdlc->proto.id;
216         }
217
218         switch(proto) {
219         case IF_PROTO_HDLC:     return hdlc_raw_ioctl(dev, ifr);
220         case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
221         case IF_PROTO_PPP:      return hdlc_ppp_ioctl(dev, ifr);
222         case IF_PROTO_CISCO:    return hdlc_cisco_ioctl(dev, ifr);
223         case IF_PROTO_FR:       return hdlc_fr_ioctl(dev, ifr);
224         case IF_PROTO_X25:      return hdlc_x25_ioctl(dev, ifr);
225         default:                return -EINVAL;
226         }
227 }
228
229 static void hdlc_setup(struct net_device *dev)
230 {
231         hdlc_device *hdlc = dev_to_hdlc(dev);
232
233         dev->get_stats = hdlc_get_stats;
234         dev->change_mtu = hdlc_change_mtu;
235         dev->mtu = HDLC_MAX_MTU;
236
237         dev->type = ARPHRD_RAWHDLC;
238         dev->hard_header_len = 16;
239
240         dev->flags = IFF_POINTOPOINT | IFF_NOARP;
241
242         hdlc->proto.id = -1;
243         hdlc->proto.detach = NULL;
244         hdlc->carrier = 1;
245         hdlc->open = 0;
246         spin_lock_init(&hdlc->state_lock);
247 }
248
249 struct net_device *alloc_hdlcdev(void *priv)
250 {
251         struct net_device *dev;
252         dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
253         if (dev)
254                 dev_to_hdlc(dev)->priv = priv;
255         return dev;
256 }
257
258 int register_hdlc_device(struct net_device *dev)
259 {
260         int result;
261         hdlc_device *hdlc = dev_to_hdlc(dev);
262
263         dev->get_stats = hdlc_get_stats;
264         dev->change_mtu = hdlc_change_mtu;
265         dev->mtu = HDLC_MAX_MTU;
266
267         dev->type = ARPHRD_RAWHDLC;
268         dev->hard_header_len = 16;
269
270         dev->flags = IFF_POINTOPOINT | IFF_NOARP;
271
272         hdlc->proto.id = -1;
273         hdlc->proto.detach = NULL;
274         hdlc->carrier = 1;
275         hdlc->open = 0;
276         spin_lock_init(&hdlc->state_lock);
277
278         result = dev_alloc_name(dev, "hdlc%d");
279         if (result < 0)
280                 return result;
281
282         result = register_netdev(dev);
283         if (result != 0)
284                 return -EIO;
285
286         return 0;
287 }
288
289
290
291 void unregister_hdlc_device(struct net_device *dev)
292 {
293         rtnl_lock();
294         hdlc_proto_detach(dev_to_hdlc(dev));
295         unregister_netdevice(dev);
296         rtnl_unlock();
297 }
298
299
300
301 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
302 MODULE_DESCRIPTION("HDLC support module");
303 MODULE_LICENSE("GPL v2");
304
305 EXPORT_SYMBOL(hdlc_open);
306 EXPORT_SYMBOL(hdlc_close);
307 EXPORT_SYMBOL(hdlc_set_carrier);
308 EXPORT_SYMBOL(hdlc_ioctl);
309 EXPORT_SYMBOL(alloc_hdlcdev);
310 EXPORT_SYMBOL(register_hdlc_device);
311 EXPORT_SYMBOL(unregister_hdlc_device);
312
313 static struct packet_type hdlc_packet_type = {
314         .type = __constant_htons(ETH_P_HDLC),
315         .func = hdlc_rcv,
316 };
317
318
319 static int __init hdlc_module_init(void)
320 {
321         printk(KERN_INFO "%s\n", version);
322         dev_add_pack(&hdlc_packet_type);
323         return 0;
324 }
325
326
327
328 static void __exit hdlc_module_exit(void)
329 {
330         dev_remove_pack(&hdlc_packet_type);
331 }
332
333
334 module_init(hdlc_module_init);
335 module_exit(hdlc_module_exit);