2 * Generic HDLC support routines for Linux
4 * Copyright (C) 1999 - 2003 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 #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>
36 static const char* version = "HDLC support module revision 1.16";
41 static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
43 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
51 static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
53 return hdlc_stats(dev);
58 static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
59 struct packet_type *p)
61 hdlc_device *hdlc = dev_to_hdlc(dev);
62 if (hdlc->proto.netif_rx)
63 return hdlc->proto.netif_rx(skb);
65 hdlc->stats.rx_dropped++; /* Shouldn't happen */
72 void hdlc_set_carrier(int on, struct net_device *dev)
74 hdlc_device *hdlc = dev_to_hdlc(dev);
78 printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
81 spin_lock_irq(&hdlc->state_lock);
83 if (hdlc->carrier == on)
84 goto carrier_exit; /* no change in DCD line level */
86 printk(KERN_INFO "%s: carrier %s\n", dev->name,
94 if (hdlc->proto.start)
95 hdlc->proto.start(dev);
96 else if (!netif_carrier_ok(dev))
97 netif_carrier_on(dev);
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);
107 spin_unlock_irq(&hdlc->state_lock);
111 /* Must be called by hardware driver when HDLC device is being opened */
112 int hdlc_open(struct net_device *dev)
114 hdlc_device *hdlc = dev_to_hdlc(dev);
116 printk(KERN_DEBUG "hdlc_open carrier %i open %i\n",
117 hdlc->carrier, hdlc->open);
120 if (hdlc->proto.id == -1)
121 return -ENOSYS; /* no protocol attached */
123 if (hdlc->proto.open) {
124 int result = hdlc->proto.open(dev);
129 spin_lock_irq(&hdlc->state_lock);
132 if (hdlc->proto.start)
133 hdlc->proto.start(dev);
134 else if (!netif_carrier_ok(dev))
135 netif_carrier_on(dev);
137 } else if (netif_carrier_ok(dev))
138 netif_carrier_off(dev);
142 spin_unlock_irq(&hdlc->state_lock);
148 /* Must be called by hardware driver when HDLC device is being closed */
149 void hdlc_close(struct net_device *dev)
151 hdlc_device *hdlc = dev_to_hdlc(dev);
153 printk(KERN_DEBUG "hdlc_close carrier %i open %i\n",
154 hdlc->carrier, hdlc->open);
157 spin_lock_irq(&hdlc->state_lock);
160 if (hdlc->carrier && hdlc->proto.stop)
161 hdlc->proto.stop(dev);
163 spin_unlock_irq(&hdlc->state_lock);
165 if (hdlc->proto.close)
166 hdlc->proto.close(dev);
171 #ifndef CONFIG_HDLC_RAW
172 #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
175 #ifndef CONFIG_HDLC_RAW_ETH
176 #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
179 #ifndef CONFIG_HDLC_PPP
180 #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
183 #ifndef CONFIG_HDLC_CISCO
184 #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
187 #ifndef CONFIG_HDLC_FR
188 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
191 #ifndef CONFIG_HDLC_X25
192 #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
196 int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
198 hdlc_device *hdlc = dev_to_hdlc(dev);
201 if (cmd != SIOCWANDEV)
204 switch(ifr->ifr_settings.type) {
206 case IF_PROTO_HDLC_ETH:
211 proto = ifr->ifr_settings.type;
215 proto = hdlc->proto.id;
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;
229 static void hdlc_setup(struct net_device *dev)
231 hdlc_device *hdlc = dev_to_hdlc(dev);
233 dev->get_stats = hdlc_get_stats;
234 dev->change_mtu = hdlc_change_mtu;
235 dev->mtu = HDLC_MAX_MTU;
237 dev->type = ARPHRD_RAWHDLC;
238 dev->hard_header_len = 16;
240 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
243 hdlc->proto.detach = NULL;
246 spin_lock_init(&hdlc->state_lock);
249 struct net_device *alloc_hdlcdev(void *priv)
251 struct net_device *dev;
252 dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
254 dev_to_hdlc(dev)->priv = priv;
258 int register_hdlc_device(struct net_device *dev)
261 hdlc_device *hdlc = dev_to_hdlc(dev);
263 dev->get_stats = hdlc_get_stats;
264 dev->change_mtu = hdlc_change_mtu;
265 dev->mtu = HDLC_MAX_MTU;
267 dev->type = ARPHRD_RAWHDLC;
268 dev->hard_header_len = 16;
270 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
273 hdlc->proto.detach = NULL;
276 spin_lock_init(&hdlc->state_lock);
278 result = dev_alloc_name(dev, "hdlc%d");
282 result = register_netdev(dev);
291 void unregister_hdlc_device(struct net_device *dev)
294 hdlc_proto_detach(dev_to_hdlc(dev));
295 unregister_netdevice(dev);
301 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
302 MODULE_DESCRIPTION("HDLC support module");
303 MODULE_LICENSE("GPL v2");
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);
313 static struct packet_type hdlc_packet_type = {
314 .type = __constant_htons(ETH_P_HDLC),
319 static int __init hdlc_module_init(void)
321 printk(KERN_INFO "%s\n", version);
322 dev_add_pack(&hdlc_packet_type);
328 static void __exit hdlc_module_exit(void)
330 dev_remove_pack(&hdlc_packet_type);
334 module_init(hdlc_module_init);
335 module_exit(hdlc_module_exit);