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