Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[deliverable/linux.git] / drivers / net / wan / hdlc_generic.c
1 /*
2 * Generic HDLC support routines for Linux
3 *
4 * Copyright (C) 1999 - 2005 Krzysztof Halasa <khc@pm.waw.pl>
5 *
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.
9 *
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
16 *
17 * Use sethdlc utility to set line parameters, protocol and PVCs
18 *
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.
23 */
24
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>
39
40
41 static const char* version = "HDLC support module revision 1.18";
42
43 #undef DEBUG_LINK
44
45
46 static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
47 {
48 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
49 return -EINVAL;
50 dev->mtu = new_mtu;
51 return 0;
52 }
53
54
55
56 static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
57 {
58 return hdlc_stats(dev);
59 }
60
61
62
63 static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
64 struct packet_type *p, struct net_device *orig_dev)
65 {
66 hdlc_device *hdlc = dev_to_hdlc(dev);
67 if (hdlc->proto.netif_rx)
68 return hdlc->proto.netif_rx(skb);
69
70 hdlc->stats.rx_dropped++; /* Shouldn't happen */
71 dev_kfree_skb(skb);
72 return NET_RX_DROP;
73 }
74
75
76
77 static void __hdlc_set_carrier_on(struct net_device *dev)
78 {
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
89 }
90
91
92
93 static void __hdlc_set_carrier_off(struct net_device *dev)
94 {
95 hdlc_device *hdlc = dev_to_hdlc(dev);
96 if (hdlc->proto.stop)
97 return hdlc->proto.stop(dev);
98
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
106 }
107
108
109
110 void hdlc_set_carrier(int on, struct net_device *dev)
111 {
112 hdlc_device *hdlc = dev_to_hdlc(dev);
113 unsigned long flags;
114 on = on ? 1 : 0;
115
116 #ifdef DEBUG_LINK
117 printk(KERN_DEBUG "hdlc_set_carrier %i\n", on);
118 #endif
119
120 spin_lock_irqsave(&hdlc->state_lock, flags);
121
122 if (hdlc->carrier == on)
123 goto carrier_exit; /* no change in DCD line level */
124
125 #ifdef DEBUG_LINK
126 printk(KERN_INFO "%s: carrier %s\n", dev->name, on ? "ON" : "off");
127 #endif
128 hdlc->carrier = on;
129
130 if (!hdlc->open)
131 goto carrier_exit;
132
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);
139 }
140
141 carrier_exit:
142 spin_unlock_irqrestore(&hdlc->state_lock, flags);
143 }
144
145
146
147 /* Must be called by hardware driver when HDLC device is being opened */
148 int hdlc_open(struct net_device *dev)
149 {
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
155
156 if (hdlc->proto.id == -1)
157 return -ENOSYS; /* no protocol attached */
158
159 if (hdlc->proto.open) {
160 int result = hdlc->proto.open(dev);
161 if (result)
162 return result;
163 }
164
165 spin_lock_irq(&hdlc->state_lock);
166
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);
172
173 hdlc->open = 1;
174
175 spin_unlock_irq(&hdlc->state_lock);
176 return 0;
177 }
178
179
180
181 /* Must be called by hardware driver when HDLC device is being closed */
182 void hdlc_close(struct net_device *dev)
183 {
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
189
190 spin_lock_irq(&hdlc->state_lock);
191
192 hdlc->open = 0;
193 if (hdlc->carrier)
194 __hdlc_set_carrier_off(dev);
195
196 spin_unlock_irq(&hdlc->state_lock);
197
198 if (hdlc->proto.close)
199 hdlc->proto.close(dev);
200 }
201
202
203
204 #ifndef CONFIG_HDLC_RAW
205 #define hdlc_raw_ioctl(dev, ifr) -ENOSYS
206 #endif
207
208 #ifndef CONFIG_HDLC_RAW_ETH
209 #define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
210 #endif
211
212 #ifndef CONFIG_HDLC_PPP
213 #define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
214 #endif
215
216 #ifndef CONFIG_HDLC_CISCO
217 #define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
218 #endif
219
220 #ifndef CONFIG_HDLC_FR
221 #define hdlc_fr_ioctl(dev, ifr) -ENOSYS
222 #endif
223
224 #ifndef CONFIG_HDLC_X25
225 #define hdlc_x25_ioctl(dev, ifr) -ENOSYS
226 #endif
227
228
229 int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
230 {
231 hdlc_device *hdlc = dev_to_hdlc(dev);
232 unsigned int proto;
233
234 if (cmd != SIOCWANDEV)
235 return -EINVAL;
236
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;
246
247 default:
248 proto = hdlc->proto.id;
249 }
250
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;
259 }
260 }
261
262 void hdlc_setup(struct net_device *dev)
263 {
264 hdlc_device *hdlc = dev_to_hdlc(dev);
265
266 dev->get_stats = hdlc_get_stats;
267 dev->change_mtu = hdlc_change_mtu;
268 dev->mtu = HDLC_MAX_MTU;
269
270 dev->type = ARPHRD_RAWHDLC;
271 dev->hard_header_len = 16;
272
273 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
274
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);
280 }
281
282 struct net_device *alloc_hdlcdev(void *priv)
283 {
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;
289 }
290
291 void unregister_hdlc_device(struct net_device *dev)
292 {
293 rtnl_lock();
294 hdlc_proto_detach(dev_to_hdlc(dev));
295 unregister_netdevice(dev);
296 rtnl_unlock();
297 }
298
299
300
301 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
302 MODULE_DESCRIPTION("HDLC support module");
303 MODULE_LICENSE("GPL v2");
304
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);
312
313 static struct packet_type hdlc_packet_type = {
314 .type = __constant_htons(ETH_P_HDLC),
315 .func = hdlc_rcv,
316 };
317
318
319 static int __init hdlc_module_init(void)
320 {
321 printk(KERN_INFO "%s\n", version);
322 dev_add_pack(&hdlc_packet_type);
323 return 0;
324 }
325
326
327
328 static void __exit hdlc_module_exit(void)
329 {
330 dev_remove_pack(&hdlc_packet_type);
331 }
332
333
334 module_init(hdlc_module_init);
335 module_exit(hdlc_module_exit);
This page took 0.038929 seconds and 6 git commands to generate.