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/notifier.h>
38 #include <linux/hdlc.h>
41 static const char* version = "HDLC support module revision 1.19";
46 static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
48 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
56 static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
58 return hdlc_stats(dev);
63 static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
64 struct packet_type *p, struct net_device *orig_dev)
66 hdlc_device *hdlc = dev_to_hdlc(dev);
67 if (hdlc->proto.netif_rx)
68 return hdlc->proto.netif_rx(skb);
70 hdlc->stats.rx_dropped++; /* Shouldn't happen */
77 static inline void hdlc_proto_start(struct net_device *dev)
79 hdlc_device *hdlc = dev_to_hdlc(dev);
80 if (hdlc->proto.start)
81 return hdlc->proto.start(dev);
86 static inline void hdlc_proto_stop(struct net_device *dev)
88 hdlc_device *hdlc = dev_to_hdlc(dev);
90 return hdlc->proto.stop(dev);
95 static int hdlc_device_event(struct notifier_block *this, unsigned long event,
98 struct net_device *dev = ptr;
103 if (dev->get_stats != hdlc_get_stats)
104 return NOTIFY_DONE; /* not an HDLC device */
106 if (event != NETDEV_CHANGE)
107 return NOTIFY_DONE; /* Only interrested in carrier changes */
109 on = netif_carrier_ok(dev);
112 printk(KERN_DEBUG "%s: hdlc_device_event NETDEV_CHANGE, carrier %i\n",
116 hdlc = dev_to_hdlc(dev);
117 spin_lock_irqsave(&hdlc->state_lock, flags);
119 if (hdlc->carrier == on)
120 goto carrier_exit; /* no change in DCD line level */
128 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
129 hdlc_proto_start(dev);
131 printk(KERN_INFO "%s: Carrier lost\n", dev->name);
132 hdlc_proto_stop(dev);
136 spin_unlock_irqrestore(&hdlc->state_lock, flags);
142 /* Must be called by hardware driver when HDLC device is being opened */
143 int hdlc_open(struct net_device *dev)
145 hdlc_device *hdlc = dev_to_hdlc(dev);
147 printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
148 hdlc->carrier, hdlc->open);
151 if (hdlc->proto.id == -1)
152 return -ENOSYS; /* no protocol attached */
154 if (hdlc->proto.open) {
155 int result = hdlc->proto.open(dev);
160 spin_lock_irq(&hdlc->state_lock);
163 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
164 hdlc_proto_start(dev);
166 printk(KERN_INFO "%s: No carrier\n", dev->name);
170 spin_unlock_irq(&hdlc->state_lock);
176 /* Must be called by hardware driver when HDLC device is being closed */
177 void hdlc_close(struct net_device *dev)
179 hdlc_device *hdlc = dev_to_hdlc(dev);
181 printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
182 hdlc->carrier, hdlc->open);
185 spin_lock_irq(&hdlc->state_lock);
189 hdlc_proto_stop(dev);
191 spin_unlock_irq(&hdlc->state_lock);
193 if (hdlc->proto.close)
194 hdlc->proto.close(dev);
199 #ifndef CONFIG_HDLC_RAW
200 #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
203 #ifndef CONFIG_HDLC_RAW_ETH
204 #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
207 #ifndef CONFIG_HDLC_PPP
208 #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
211 #ifndef CONFIG_HDLC_CISCO
212 #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
215 #ifndef CONFIG_HDLC_FR
216 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
219 #ifndef CONFIG_HDLC_X25
220 #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
224 int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
226 hdlc_device *hdlc = dev_to_hdlc(dev);
229 if (cmd != SIOCWANDEV)
232 switch(ifr->ifr_settings.type) {
234 case IF_PROTO_HDLC_ETH:
239 proto = ifr->ifr_settings.type;
243 proto = hdlc->proto.id;
247 case IF_PROTO_HDLC: return hdlc_raw_ioctl(dev, ifr);
248 case IF_PROTO_HDLC_ETH: return hdlc_raw_eth_ioctl(dev, ifr);
249 case IF_PROTO_PPP: return hdlc_ppp_ioctl(dev, ifr);
250 case IF_PROTO_CISCO: return hdlc_cisco_ioctl(dev, ifr);
251 case IF_PROTO_FR: return hdlc_fr_ioctl(dev, ifr);
252 case IF_PROTO_X25: return hdlc_x25_ioctl(dev, ifr);
253 default: return -EINVAL;
257 void hdlc_setup(struct net_device *dev)
259 hdlc_device *hdlc = dev_to_hdlc(dev);
261 dev->get_stats = hdlc_get_stats;
262 dev->change_mtu = hdlc_change_mtu;
263 dev->mtu = HDLC_MAX_MTU;
265 dev->type = ARPHRD_RAWHDLC;
266 dev->hard_header_len = 16;
268 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
271 hdlc->proto.detach = NULL;
274 spin_lock_init(&hdlc->state_lock);
277 struct net_device *alloc_hdlcdev(void *priv)
279 struct net_device *dev;
280 dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
282 dev_to_hdlc(dev)->priv = priv;
286 void unregister_hdlc_device(struct net_device *dev)
289 hdlc_proto_detach(dev_to_hdlc(dev));
290 unregister_netdevice(dev);
296 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
297 MODULE_DESCRIPTION("HDLC support module");
298 MODULE_LICENSE("GPL v2");
300 EXPORT_SYMBOL(hdlc_open);
301 EXPORT_SYMBOL(hdlc_close);
302 EXPORT_SYMBOL(hdlc_ioctl);
303 EXPORT_SYMBOL(hdlc_setup);
304 EXPORT_SYMBOL(alloc_hdlcdev);
305 EXPORT_SYMBOL(unregister_hdlc_device);
307 static struct packet_type hdlc_packet_type = {
308 .type = __constant_htons(ETH_P_HDLC),
313 static struct notifier_block hdlc_notifier = {
314 .notifier_call = hdlc_device_event,
318 static int __init hdlc_module_init(void)
322 printk(KERN_INFO "%s\n", version);
323 if ((result = register_netdevice_notifier(&hdlc_notifier)) != 0)
325 dev_add_pack(&hdlc_packet_type);
331 static void __exit hdlc_module_exit(void)
333 dev_remove_pack(&hdlc_packet_type);
334 unregister_netdevice_notifier(&hdlc_notifier);
338 module_init(hdlc_module_init);
339 module_exit(hdlc_module_exit);