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/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>
41 static const char* version
= "HDLC support module revision 1.18";
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 void __hdlc_set_carrier_on(struct net_device
*dev
)
79 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
80 if (hdlc
->proto
.start
)
81 return hdlc
->proto
.start(dev
);
84 if (netif_carrier_ok(dev
))
85 printk(KERN_ERR
"hdlc_set_carrier_on(): already on\n");
87 netif_carrier_on(dev
);
93 static void __hdlc_set_carrier_off(struct net_device
*dev
)
95 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
97 return hdlc
->proto
.stop(dev
);
101 if (!netif_carrier_ok(dev
))
102 printk(KERN_ERR
"hdlc_set_carrier_off(): already off\n");
104 netif_carrier_off(dev
);
110 void hdlc_set_carrier(int on
, struct net_device
*dev
)
112 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
117 printk(KERN_DEBUG
"hdlc_set_carrier %i\n", on
);
120 spin_lock_irqsave(&hdlc
->state_lock
, flags
);
122 if (hdlc
->carrier
== on
)
123 goto carrier_exit
; /* no change in DCD line level */
126 printk(KERN_INFO
"%s: carrier %s\n", dev
->name
, on
? "ON" : "off");
134 printk(KERN_INFO
"%s: Carrier detected\n", dev
->name
);
135 __hdlc_set_carrier_on(dev
);
137 printk(KERN_INFO
"%s: Carrier lost\n", dev
->name
);
138 __hdlc_set_carrier_off(dev
);
142 spin_unlock_irqrestore(&hdlc
->state_lock
, flags
);
147 /* Must be called by hardware driver when HDLC device is being opened */
148 int hdlc_open(struct net_device
*dev
)
150 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
152 printk(KERN_DEBUG
"hdlc_open() carrier %i open %i\n",
153 hdlc
->carrier
, hdlc
->open
);
156 if (hdlc
->proto
.id
== -1)
157 return -ENOSYS
; /* no protocol attached */
159 if (hdlc
->proto
.open
) {
160 int result
= hdlc
->proto
.open(dev
);
165 spin_lock_irq(&hdlc
->state_lock
);
168 printk(KERN_INFO
"%s: Carrier detected\n", dev
->name
);
169 __hdlc_set_carrier_on(dev
);
171 printk(KERN_INFO
"%s: No carrier\n", dev
->name
);
175 spin_unlock_irq(&hdlc
->state_lock
);
181 /* Must be called by hardware driver when HDLC device is being closed */
182 void hdlc_close(struct net_device
*dev
)
184 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
186 printk(KERN_DEBUG
"hdlc_close() carrier %i open %i\n",
187 hdlc
->carrier
, hdlc
->open
);
190 spin_lock_irq(&hdlc
->state_lock
);
194 __hdlc_set_carrier_off(dev
);
196 spin_unlock_irq(&hdlc
->state_lock
);
198 if (hdlc
->proto
.close
)
199 hdlc
->proto
.close(dev
);
204 #ifndef CONFIG_HDLC_RAW
205 #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
208 #ifndef CONFIG_HDLC_RAW_ETH
209 #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
212 #ifndef CONFIG_HDLC_PPP
213 #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
216 #ifndef CONFIG_HDLC_CISCO
217 #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
220 #ifndef CONFIG_HDLC_FR
221 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
224 #ifndef CONFIG_HDLC_X25
225 #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
229 int hdlc_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
231 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
234 if (cmd
!= SIOCWANDEV
)
237 switch(ifr
->ifr_settings
.type
) {
239 case IF_PROTO_HDLC_ETH
:
244 proto
= ifr
->ifr_settings
.type
;
248 proto
= hdlc
->proto
.id
;
252 case IF_PROTO_HDLC
: return hdlc_raw_ioctl(dev
, ifr
);
253 case IF_PROTO_HDLC_ETH
: return hdlc_raw_eth_ioctl(dev
, ifr
);
254 case IF_PROTO_PPP
: return hdlc_ppp_ioctl(dev
, ifr
);
255 case IF_PROTO_CISCO
: return hdlc_cisco_ioctl(dev
, ifr
);
256 case IF_PROTO_FR
: return hdlc_fr_ioctl(dev
, ifr
);
257 case IF_PROTO_X25
: return hdlc_x25_ioctl(dev
, ifr
);
258 default: return -EINVAL
;
262 static void hdlc_setup(struct net_device
*dev
)
264 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
266 dev
->get_stats
= hdlc_get_stats
;
267 dev
->change_mtu
= hdlc_change_mtu
;
268 dev
->mtu
= HDLC_MAX_MTU
;
270 dev
->type
= ARPHRD_RAWHDLC
;
271 dev
->hard_header_len
= 16;
273 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
276 hdlc
->proto
.detach
= NULL
;
279 spin_lock_init(&hdlc
->state_lock
);
282 struct net_device
*alloc_hdlcdev(void *priv
)
284 struct net_device
*dev
;
285 dev
= alloc_netdev(sizeof(hdlc_device
), "hdlc%d", hdlc_setup
);
287 dev_to_hdlc(dev
)->priv
= priv
;
291 int register_hdlc_device(struct net_device
*dev
)
293 int result
= dev_alloc_name(dev
, "hdlc%d");
297 result
= register_netdev(dev
);
302 if (netif_carrier_ok(dev
))
303 netif_carrier_off(dev
); /* no carrier until DCD goes up */
311 void unregister_hdlc_device(struct net_device
*dev
)
314 hdlc_proto_detach(dev_to_hdlc(dev
));
315 unregister_netdevice(dev
);
321 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
322 MODULE_DESCRIPTION("HDLC support module");
323 MODULE_LICENSE("GPL v2");
325 EXPORT_SYMBOL(hdlc_open
);
326 EXPORT_SYMBOL(hdlc_close
);
327 EXPORT_SYMBOL(hdlc_set_carrier
);
328 EXPORT_SYMBOL(hdlc_ioctl
);
329 EXPORT_SYMBOL(alloc_hdlcdev
);
330 EXPORT_SYMBOL(register_hdlc_device
);
331 EXPORT_SYMBOL(unregister_hdlc_device
);
333 static struct packet_type hdlc_packet_type
= {
334 .type
= __constant_htons(ETH_P_HDLC
),
339 static int __init
hdlc_module_init(void)
341 printk(KERN_INFO
"%s\n", version
);
342 dev_add_pack(&hdlc_packet_type
);
348 static void __exit
hdlc_module_exit(void)
350 dev_remove_pack(&hdlc_packet_type
);
354 module_init(hdlc_module_init
);
355 module_exit(hdlc_module_exit
);