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
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>
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
4648static 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)
6365static 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,
7779static 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
8688static 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-
224201int 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
257225void 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)
277243struct 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)
286253void 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+
296321MODULE_AUTHOR ("Krzysztof Halasa <khc@pm.waw.pl>" );
297322MODULE_DESCRIPTION ("HDLC support module" );
298323MODULE_LICENSE ("GPL v2" );
@@ -303,6 +328,10 @@ EXPORT_SYMBOL(hdlc_ioctl);
303328EXPORT_SYMBOL (hdlc_setup );
304329EXPORT_SYMBOL (alloc_hdlcdev );
305330EXPORT_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
307336static struct packet_type hdlc_packet_type = {
308337 .type = __constant_htons (ETH_P_HDLC ),
0 commit comments