Skip to content

Commit eb2a2fd

Browse files
Krzysztof HalasaJeff Garzik
authored andcommitted
[PATCH] Modularize generic HDLC
This patch enables building of individual WAN protocol support routines (parts of generic HDLC) as separate modules. All protocol-private definitions are moved from hdlc.h file to protocol drivers. User-space interface and interface between generic HDLC and underlying low-level HDLC drivers are unchanged. Signed-off-by: Krzysztof Halasa <khc@pm.waw.pl> Signed-off-by: Jeff Garzik <jeff@garzik.org>
1 parent c226951 commit eb2a2fd

11 files changed

Lines changed: 743 additions & 508 deletions

File tree

‎drivers/net/wan/Kconfig‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ config HDLC
154154
If unsure, say N.
155155

156156
config HDLC_RAW
157-
bool "Raw HDLC support"
157+
tristate "Raw HDLC support"
158158
depends on HDLC
159159
help
160160
Generic HDLC driver supporting raw HDLC over WAN connections.
161161

162162
If unsure, say N.
163163

164164
config HDLC_RAW_ETH
165-
bool "Raw HDLC Ethernet device support"
165+
tristate "Raw HDLC Ethernet device support"
166166
depends on HDLC
167167
help
168168
Generic HDLC driver supporting raw HDLC Ethernet device emulation
@@ -173,31 +173,31 @@ config HDLC_RAW_ETH
173173
If unsure, say N.
174174

175175
config HDLC_CISCO
176-
bool "Cisco HDLC support"
176+
tristate "Cisco HDLC support"
177177
depends on HDLC
178178
help
179179
Generic HDLC driver supporting Cisco HDLC over WAN connections.
180180

181181
If unsure, say N.
182182

183183
config HDLC_FR
184-
bool "Frame Relay support"
184+
tristate "Frame Relay support"
185185
depends on HDLC
186186
help
187187
Generic HDLC driver supporting Frame Relay over WAN connections.
188188

189189
If unsure, say N.
190190

191191
config HDLC_PPP
192-
bool "Synchronous Point-to-Point Protocol (PPP) support"
192+
tristate "Synchronous Point-to-Point Protocol (PPP) support"
193193
depends on HDLC
194194
help
195195
Generic HDLC driver supporting PPP over WAN connections.
196196

197197
If unsure, say N.
198198

199199
config HDLC_X25
200-
bool "X.25 protocol support"
200+
tristate "X.25 protocol support"
201201
depends on HDLC && (LAPB=m && HDLC=m || LAPB=y)
202202
help
203203
Generic HDLC driver supporting X.25 over WAN connections.

‎drivers/net/wan/Makefile‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ cyclomx-y := cycx_main.o
99
cyclomx-$(CONFIG_CYCLOMX_X25) += cycx_x25.o
1010
cyclomx-objs := $(cyclomx-y)
1111

12-
hdlc-y := hdlc_generic.o
13-
hdlc-$(CONFIG_HDLC_RAW) += hdlc_raw.o
14-
hdlc-$(CONFIG_HDLC_RAW_ETH) += hdlc_raw_eth.o
15-
hdlc-$(CONFIG_HDLC_CISCO) += hdlc_cisco.o
16-
hdlc-$(CONFIG_HDLC_FR) += hdlc_fr.o
17-
hdlc-$(CONFIG_HDLC_PPP) += hdlc_ppp.o
18-
hdlc-$(CONFIG_HDLC_X25) += hdlc_x25.o
19-
hdlc-objs := $(hdlc-y)
12+
obj-$(CONFIG_HDLC) += hdlc.o
13+
obj-$(CONFIG_HDLC_RAW) += hdlc_raw.o
14+
obj-$(CONFIG_HDLC_RAW_ETH) += hdlc_raw_eth.o
15+
obj-$(CONFIG_HDLC_CISCO) += hdlc_cisco.o
16+
obj-$(CONFIG_HDLC_FR) += hdlc_fr.o
17+
obj-$(CONFIG_HDLC_PPP) += hdlc_ppp.o syncppp.o
18+
obj-$(CONFIG_HDLC_X25) += hdlc_x25.o
2019

2120
pc300-y := pc300_drv.o
2221
pc300-$(CONFIG_PC300_MLPPP) += pc300_tty.o
@@ -38,10 +37,6 @@ obj-$(CONFIG_CYCLADES_SYNC) += cycx_drv.o cyclomx.o
3837
obj-$(CONFIG_LAPBETHER) += lapbether.o
3938
obj-$(CONFIG_SBNI) += sbni.o
4039
obj-$(CONFIG_PC300) += pc300.o
41-
obj-$(CONFIG_HDLC) += hdlc.o
42-
ifeq ($(CONFIG_HDLC_PPP),y)
43-
obj-$(CONFIG_HDLC) += syncppp.o
44-
endif
4540
obj-$(CONFIG_N2) += n2.o
4641
obj-$(CONFIG_C101) += c101.o
4742
obj-$(CONFIG_WANXL) += wanxl.o
Lines changed: 99 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Generic HDLC support routines for Linux
33
*
4-
* Copyright (C) 1999 - 2005 Krzysztof Halasa <khc@pm.waw.pl>
4+
* Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
55
*
66
* This program is free software; you can redistribute it and/or modify it
77
* under the terms of version 2 of the GNU General Public License
@@ -17,9 +17,9 @@
1717
* Use sethdlc utility to set line parameters, protocol and PVCs
1818
*
1919
* How does it work:
20-
* - proto.open(), close(), start(), stop() calls are serialized.
20+
* - proto->open(), close(), start(), stop() calls are serialized.
2121
* The order is: open, [ start, stop ... ] close ...
22-
* - proto.start() and stop() are called with spin_lock_irq held.
22+
* - proto->start() and stop() are called with spin_lock_irq held.
2323
*/
2424

2525
#include <linux/module.h>
@@ -38,10 +38,12 @@
3838
#include <linux/hdlc.h>
3939

4040

41-
static const char* version = "HDLC support module revision 1.19";
41+
static const char* version = "HDLC support module revision 1.20";
4242

4343
#undef DEBUG_LINK
4444

45+
static struct hdlc_proto *first_proto = NULL;
46+
4547

4648
static int hdlc_change_mtu(struct net_device *dev, int new_mtu)
4749
{
@@ -63,11 +65,11 @@ static struct net_device_stats *hdlc_get_stats(struct net_device *dev)
6365
static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
6466
struct packet_type *p, struct net_device *orig_dev)
6567
{
66-
hdlc_device *hdlc = dev_to_hdlc(dev);
67-
if (hdlc->proto.netif_rx)
68-
return hdlc->proto.netif_rx(skb);
68+
struct hdlc_device_desc *desc = dev_to_desc(dev);
69+
if (desc->netif_rx)
70+
return desc->netif_rx(skb);
6971

70-
hdlc->stats.rx_dropped++; /* Shouldn't happen */
72+
desc->stats.rx_dropped++; /* Shouldn't happen */
7173
dev_kfree_skb(skb);
7274
return NET_RX_DROP;
7375
}
@@ -77,17 +79,17 @@ static int hdlc_rcv(struct sk_buff *skb, struct net_device *dev,
7779
static inline void hdlc_proto_start(struct net_device *dev)
7880
{
7981
hdlc_device *hdlc = dev_to_hdlc(dev);
80-
if (hdlc->proto.start)
81-
return hdlc->proto.start(dev);
82+
if (hdlc->proto->start)
83+
return hdlc->proto->start(dev);
8284
}
8385

8486

8587

8688
static inline void hdlc_proto_stop(struct net_device *dev)
8789
{
8890
hdlc_device *hdlc = dev_to_hdlc(dev);
89-
if (hdlc->proto.stop)
90-
return hdlc->proto.stop(dev);
91+
if (hdlc->proto->stop)
92+
return hdlc->proto->stop(dev);
9193
}
9294

9395

@@ -144,15 +146,15 @@ int hdlc_open(struct net_device *dev)
144146
{
145147
hdlc_device *hdlc = dev_to_hdlc(dev);
146148
#ifdef DEBUG_LINK
147-
printk(KERN_DEBUG "hdlc_open() carrier %i open %i\n",
149+
printk(KERN_DEBUG "%s: hdlc_open() carrier %i open %i\n", dev->name,
148150
hdlc->carrier, hdlc->open);
149151
#endif
150152

151-
if (hdlc->proto.id == -1)
153+
if (hdlc->proto == NULL)
152154
return -ENOSYS; /* no protocol attached */
153155

154-
if (hdlc->proto.open) {
155-
int result = hdlc->proto.open(dev);
156+
if (hdlc->proto->open) {
157+
int result = hdlc->proto->open(dev);
156158
if (result)
157159
return result;
158160
}
@@ -178,7 +180,7 @@ void hdlc_close(struct net_device *dev)
178180
{
179181
hdlc_device *hdlc = dev_to_hdlc(dev);
180182
#ifdef DEBUG_LINK
181-
printk(KERN_DEBUG "hdlc_close() carrier %i open %i\n",
183+
printk(KERN_DEBUG "%s: hdlc_close() carrier %i open %i\n", dev->name,
182184
hdlc->carrier, hdlc->open);
183185
#endif
184186

@@ -190,68 +192,34 @@ void hdlc_close(struct net_device *dev)
190192

191193
spin_unlock_irq(&hdlc->state_lock);
192194

193-
if (hdlc->proto.close)
194-
hdlc->proto.close(dev);
195+
if (hdlc->proto->close)
196+
hdlc->proto->close(dev);
195197
}
196198

197199

198200

199-
#ifndef CONFIG_HDLC_RAW
200-
#define hdlc_raw_ioctl(dev, ifr) -ENOSYS
201-
#endif
202-
203-
#ifndef CONFIG_HDLC_RAW_ETH
204-
#define hdlc_raw_eth_ioctl(dev, ifr) -ENOSYS
205-
#endif
206-
207-
#ifndef CONFIG_HDLC_PPP
208-
#define hdlc_ppp_ioctl(dev, ifr) -ENOSYS
209-
#endif
210-
211-
#ifndef CONFIG_HDLC_CISCO
212-
#define hdlc_cisco_ioctl(dev, ifr) -ENOSYS
213-
#endif
214-
215-
#ifndef CONFIG_HDLC_FR
216-
#define hdlc_fr_ioctl(dev, ifr) -ENOSYS
217-
#endif
218-
219-
#ifndef CONFIG_HDLC_X25
220-
#define hdlc_x25_ioctl(dev, ifr) -ENOSYS
221-
#endif
222-
223-
224201
int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
225202
{
226-
hdlc_device *hdlc = dev_to_hdlc(dev);
227-
unsigned int proto;
203+
struct hdlc_proto *proto = first_proto;
204+
int result;
228205

229206
if (cmd != SIOCWANDEV)
230207
return -EINVAL;
231208

232-
switch(ifr->ifr_settings.type) {
233-
case IF_PROTO_HDLC:
234-
case IF_PROTO_HDLC_ETH:
235-
case IF_PROTO_PPP:
236-
case IF_PROTO_CISCO:
237-
case IF_PROTO_FR:
238-
case IF_PROTO_X25:
239-
proto = ifr->ifr_settings.type;
240-
break;
241-
242-
default:
243-
proto = hdlc->proto.id;
209+
if (dev_to_hdlc(dev)->proto) {
210+
result = dev_to_hdlc(dev)->proto->ioctl(dev, ifr);
211+
if (result != -EINVAL)
212+
return result;
244213
}
245214

246-
switch(proto) {
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;
215+
/* Not handled by currently attached protocol (if any) */
216+
217+
while (proto) {
218+
if ((result = proto->ioctl(dev, ifr)) != -EINVAL)
219+
return result;
220+
proto = proto->next;
254221
}
222+
return -EINVAL;
255223
}
256224

257225
void hdlc_setup(struct net_device *dev)
@@ -267,8 +235,6 @@ void hdlc_setup(struct net_device *dev)
267235

268236
dev->flags = IFF_POINTOPOINT | IFF_NOARP;
269237

270-
hdlc->proto.id = -1;
271-
hdlc->proto.detach = NULL;
272238
hdlc->carrier = 1;
273239
hdlc->open = 0;
274240
spin_lock_init(&hdlc->state_lock);
@@ -277,7 +243,8 @@ void hdlc_setup(struct net_device *dev)
277243
struct net_device *alloc_hdlcdev(void *priv)
278244
{
279245
struct net_device *dev;
280-
dev = alloc_netdev(sizeof(hdlc_device), "hdlc%d", hdlc_setup);
246+
dev = alloc_netdev(sizeof(struct hdlc_device_desc) +
247+
sizeof(hdlc_device), "hdlc%d", hdlc_setup);
281248
if (dev)
282249
dev_to_hdlc(dev)->priv = priv;
283250
return dev;
@@ -286,13 +253,71 @@ struct net_device *alloc_hdlcdev(void *priv)
286253
void unregister_hdlc_device(struct net_device *dev)
287254
{
288255
rtnl_lock();
289-
hdlc_proto_detach(dev_to_hdlc(dev));
290256
unregister_netdevice(dev);
257+
detach_hdlc_protocol(dev);
291258
rtnl_unlock();
292259
}
293260

294261

295262

263+
int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
264+
int (*rx)(struct sk_buff *skb), size_t size)
265+
{
266+
detach_hdlc_protocol(dev);
267+
268+
if (!try_module_get(proto->module))
269+
return -ENOSYS;
270+
271+
if (size)
272+
if ((dev_to_hdlc(dev)->state = kmalloc(size,
273+
GFP_KERNEL)) == NULL) {
274+
printk(KERN_WARNING "Memory squeeze on"
275+
" hdlc_proto_attach()\n");
276+
module_put(proto->module);
277+
return -ENOBUFS;
278+
}
279+
dev_to_hdlc(dev)->proto = proto;
280+
dev_to_desc(dev)->netif_rx = rx;
281+
return 0;
282+
}
283+
284+
285+
void detach_hdlc_protocol(struct net_device *dev)
286+
{
287+
hdlc_device *hdlc = dev_to_hdlc(dev);
288+
289+
if (hdlc->proto) {
290+
if (hdlc->proto->detach)
291+
hdlc->proto->detach(dev);
292+
module_put(hdlc->proto->module);
293+
hdlc->proto = NULL;
294+
}
295+
kfree(hdlc->state);
296+
hdlc->state = NULL;
297+
}
298+
299+
300+
void register_hdlc_protocol(struct hdlc_proto *proto)
301+
{
302+
proto->next = first_proto;
303+
first_proto = proto;
304+
}
305+
306+
307+
void unregister_hdlc_protocol(struct hdlc_proto *proto)
308+
{
309+
struct hdlc_proto **p = &first_proto;
310+
while (*p) {
311+
if (*p == proto) {
312+
*p = proto->next;
313+
return;
314+
}
315+
p = &((*p)->next);
316+
}
317+
}
318+
319+
320+
296321
MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
297322
MODULE_DESCRIPTION("HDLC support module");
298323
MODULE_LICENSE("GPL v2");
@@ -303,6 +328,10 @@ EXPORT_SYMBOL(hdlc_ioctl);
303328
EXPORT_SYMBOL(hdlc_setup);
304329
EXPORT_SYMBOL(alloc_hdlcdev);
305330
EXPORT_SYMBOL(unregister_hdlc_device);
331+
EXPORT_SYMBOL(register_hdlc_protocol);
332+
EXPORT_SYMBOL(unregister_hdlc_protocol);
333+
EXPORT_SYMBOL(attach_hdlc_protocol);
334+
EXPORT_SYMBOL(detach_hdlc_protocol);
306335

307336
static struct packet_type hdlc_packet_type = {
308337
.type = __constant_htons(ETH_P_HDLC),

0 commit comments

Comments
 (0)