[PATCH] WAN: register_hdlc_device() doesn't need dev_alloc_name()
[linux-2.6/mini2440.git] / drivers / net / wan / hdlc_generic.c
blob57f9538b8fb5efead8c45e792610df97bcf57500
1 /*
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:
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
17 * Use sethdlc utility to set line parameters, protocol and PVCs
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.
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";
43 #undef DEBUG_LINK
46 static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
48 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
49 return -EINVAL;
50 dev->mtu = new_mtu;
51 return 0;
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 */
71 dev_kfree_skb(skb);
72 return NET_RX_DROP;
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);
82 #if 0
83 #ifdef DEBUG_LINK
84 if (netif_carrier_ok(dev))
85 printk(KERN_ERR "hdlc_set_carrier_on(): already on\n");
86 #endif
87 netif_carrier_on(dev);
88 #endif
93 static void __hdlc_set_carrier_off(struct net_device *dev)
95 hdlc_device *hdlc = dev_to_hdlc(dev);
96 if (hdlc->proto.stop)
97 return hdlc->proto.stop(dev);
99 #if 0
100 #ifdef DEBUG_LINK
101 if (!netif_carrier_ok(dev))
102 printk(KERN_ERR "hdlc_set_carrier_off(): already off\n");
103 #endif
104 netif_carrier_off(dev);
105 #endif
110 void hdlc_set_carrier(int on, struct net_device *dev)
112 hdlc_device *hdlc = dev_to_hdlc(dev);
113 unsigned long flags;
114 on = on ? 1 : 0;
116 #ifdef DEBUG_LINK
117 printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
118 #endif
120 spin_lock_irqsave(&hdlc->state_lock, flags);
122 if (hdlc->carrier == on)
123 goto carrier_exit; /* no change in DCD line level */
125 #ifdef DEBUG_LINK
126 printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
127 #endif
128 hdlc->carrier = on;
130 if (!hdlc->open)
131 goto carrier_exit;
133 if (hdlc->carrier) {
134 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
135 __hdlc_set_carrier_on(dev);
136 } else {
137 printk(KERN_INFO "%s: Carrier lost\n", dev->name);
138 __hdlc_set_carrier_off(dev);
141 carrier_exit:
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);
151 #ifdef DEBUG_LINK
152 printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
153 hdlc->carrier, hdlc->open);
154 #endif
156 if (hdlc->proto.id == -1)
157 return -ENOSYS; /* no protocol attached */
159 if (hdlc->proto.open) {
160 int result = hdlc->proto.open(dev);
161 if (result)
162 return result;
165 spin_lock_irq(&hdlc->state_lock);
167 if (hdlc->carrier) {
168 printk(KERN_INFO "%s: Carrier detected\n", dev->name);
169 __hdlc_set_carrier_on(dev);
170 } else
171 printk(KERN_INFO "%s: No carrier\n", dev->name);
173 hdlc->open = 1;
175 spin_unlock_irq(&hdlc->state_lock);
176 return 0;
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);
185 #ifdef DEBUG_LINK
186 printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
187 hdlc->carrier, hdlc->open);
188 #endif
190 spin_lock_irq(&hdlc->state_lock);
192 hdlc->open = 0;
193 if (hdlc->carrier)
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
206 #endif
208 #ifndef CONFIG_HDLC_RAW_ETH
209 #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
210 #endif
212 #ifndef CONFIG_HDLC_PPP
213 #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
214 #endif
216 #ifndef CONFIG_HDLC_CISCO
217 #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
218 #endif
220 #ifndef CONFIG_HDLC_FR
221 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
222 #endif
224 #ifndef CONFIG_HDLC_X25
225 #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
226 #endif
229 int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
231 hdlc_device *hdlc = dev_to_hdlc(dev);
232 unsigned int proto;
234 if (cmd != SIOCWANDEV)
235 return -EINVAL;
237 switch(ifr->ifr_settings.type) {
238 case IF_PROTO_HDLC:
239 case IF_PROTO_HDLC_ETH:
240 case IF_PROTO_PPP:
241 case IF_PROTO_CISCO:
242 case IF_PROTO_FR:
243 case IF_PROTO_X25:
244 proto = ifr->ifr_settings.type;
245 break;
247 default:
248 proto = hdlc->proto.id;
251 switch(proto) {
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 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;
275 hdlc->proto.id = -1;
276 hdlc->proto.detach = NULL;
277 hdlc->carrier = 1;
278 hdlc->open = 0;
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);
286 if (dev)
287 dev_to_hdlc(dev)->priv = priv;
288 return dev;
291 void unregister_hdlc_device(struct net_device *dev)
293 rtnl_lock();
294 hdlc_proto_detach(dev_to_hdlc(dev));
295 unregister_netdevice(dev);
296 rtnl_unlock();
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(hdlc_setup);
310 EXPORT_SYMBOL(alloc_hdlcdev);
311 EXPORT_SYMBOL(unregister_hdlc_device);
313 static struct packet_type hdlc_packet_type = {
314 .type = __constant_htons(ETH_P_HDLC),
315 .func = hdlc_rcv,
319 static int __init hdlc_module_init(void)
321 printk(KERN_INFO "%s\n", version);
322 dev_add_pack(&hdlc_packet_type);
323 return 0;
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);