2 * Generic HDLC support routines for Linux
4 * Copyright (C) 1999 - 2005 Krzysztof Halasa <khc@pm.waw.pl>
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.
10 * Currently supported:
13 * * Frame Relay with ANSI or CCITT LMI (both user and network side)
17 * Use sethdlc utility to set line parameters, protocol and PVCs
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.
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/poll.h>
29 #include <linux/errno.h>
30 #include <linux/if_arp.h>
31 #include <linux/init.h>
32 #include <linux/skbuff.h>
33 #include <linux/pkt_sched.h>
34 #include <linux/inetdevice.h>
35 #include <linux/lapb.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/hdlc.h>
40 static const char* version = "HDLC support module revision 1.18";
45 static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
47 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
55 static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
57 return hdlc_stats(dev);
62 static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
63 struct packet_type *p, struct net_device *orig_dev)
65 hdlc_device *hdlc = dev_to_hdlc(dev);
66 if (hdlc->proto.netif_rx)
67 return hdlc->proto.netif_rx(skb);
69 hdlc->stats.rx_dropped++; /* Shouldn't happen */
76 static void __hdlc_set_carrier_on(struct net_device *dev)
78 hdlc_device *hdlc = dev_to_hdlc(dev);
79 if (hdlc->proto.start)
80 return hdlc->proto.start(dev);
83 if (netif_carrier_ok(dev))
84 printk(KERN_ERR "hdlc_set_carrier_on(): already on\n");
86 netif_carrier_on(dev);
92 static void __hdlc_set_carrier_off(struct net_device *dev)
94 hdlc_device *hdlc = dev_to_hdlc(dev);
96 return hdlc->proto.stop(dev);
100 if (!netif_carrier_ok(dev))
101 printk(KERN_ERR "hdlc_set_carrier_off(): already off\n");
103 netif_carrier_off(dev);
109 void hdlc_set_carrier(int on, struct net_device *dev)
111 hdlc_device *hdlc = dev_to_hdlc(dev);
116 printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
119 spin_lock_irqsave(&hdlc->state_lock, flags);
121 if (hdlc->carrier == on)
122 goto carrier_exit; /* no change in DCD line level */
125 printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
133 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
134 __hdlc_set_carrier_on(dev);
136 printk(KERN_INFO "%s: Carrier lost\n", dev->name);
137 __hdlc_set_carrier_off(dev);
141 spin_unlock_irqrestore(&hdlc->state_lock, flags);
146 /* Must be called by hardware driver when HDLC device is being opened */
147 int hdlc_open(struct net_device *dev)
149 hdlc_device *hdlc = dev_to_hdlc(dev);
151 printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
152 hdlc->carrier, hdlc->open);
155 if (hdlc->proto.id == -1)
156 return -ENOSYS; /* no protocol attached */
158 if (hdlc->proto.open) {
159 int result = hdlc->proto.open(dev);
164 spin_lock_irq(&hdlc->state_lock);
167 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
168 __hdlc_set_carrier_on(dev);
170 printk(KERN_INFO "%s: No carrier\n", dev->name);
174 spin_unlock_irq(&hdlc->state_lock);
180 /* Must be called by hardware driver when HDLC device is being closed */
181 void hdlc_close(struct net_device *dev)
183 hdlc_device *hdlc = dev_to_hdlc(dev);
185 printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
186 hdlc->carrier, hdlc->open);
189 spin_lock_irq(&hdlc->state_lock);
193 __hdlc_set_carrier_off(dev);
195 spin_unlock_irq(&hdlc->state_lock);
197 if (hdlc->proto.close)
198 hdlc->proto.close(dev);
203 #ifndef CONFIG_HDLC_RAW
204 #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
207 #ifndef CONFIG_HDLC_RAW_ETH
208 #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
211 #ifndef CONFIG_HDLC_PPP
212 #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
215 #ifndef CONFIG_HDLC_CISCO
216 #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
219 #ifndef CONFIG_HDLC_FR
220 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
223 #ifndef CONFIG_HDLC_X25
224 #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
228 int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
230 hdlc_device *hdlc = dev_to_hdlc(dev);
233 if (cmd != SIOCWANDEV)
236 switch(ifr->ifr_settings.type) {
238 case IF_PROTO_HDLC_ETH:
243 proto = ifr->ifr_settings.type;
247 proto = hdlc->proto.id;
251 case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
252 case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
253 case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
254 case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
255 case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
256 case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
257 default: return -EINVAL;
261 void hdlc_setup(struct net_device *dev)
263 hdlc_device *hdlc = dev_to_hdlc(dev);
265 dev->get_stats = hdlc_get_stats;
266 dev->change_mtu = hdlc_change_mtu;
267 dev->mtu = HDLC_MAX_MTU;
269 dev->type = ARPHRD_RAWHDLC;
270 dev->hard_header_len = 16;
272 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
275 hdlc->proto.detach = NULL;
278 spin_lock_init(&hdlc->state_lock);
281 struct net_device *alloc_hdlcdev(void *priv)
283 struct net_device *dev;
284 dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
286 dev_to_hdlc(dev)->priv = priv;
290 void unregister_hdlc_device(struct net_device *dev)
293 hdlc_proto_detach(dev_to_hdlc(dev));
294 unregister_netdevice(dev);
300 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
301 MODULE_DESCRIPTION("HDLC support module");
302 MODULE_LICENSE("GPL v2");
304 EXPORT_SYMBOL(hdlc_open);
305 EXPORT_SYMBOL(hdlc_close);
306 EXPORT_SYMBOL(hdlc_set_carrier);
307 EXPORT_SYMBOL(hdlc_ioctl);
308 EXPORT_SYMBOL(hdlc_setup);
309 EXPORT_SYMBOL(alloc_hdlcdev);
310 EXPORT_SYMBOL(unregister_hdlc_device);
312 static struct packet_type hdlc_packet_type = {
313 .type = __constant_htons(ETH_P_HDLC),
318 static int __init hdlc_module_init(void)
320 printk(KERN_INFO "%s\n", version);
321 dev_add_pack(&hdlc_packet_type);
327 static void __exit hdlc_module_exit(void)
329 dev_remove_pack(&hdlc_packet_type);
333 module_init(hdlc_module_init);
334 module_exit(hdlc_module_exit);