| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Generic HDLC support routines for Linux | 
|  | 3 | * | 
| Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 4 | * Copyright (C) 1999-2005 Krzysztof Halasa <khc@pm.waw.pl> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 11 | #ifndef __HDLC_H | 
|  | 12 | #define __HDLC_H | 
|  | 13 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 |  | 
|  | 15 | #define HDLC_MAX_MTU 1500	/* Ethernet 1500 bytes */ | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 16 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #define HDLC_MAX_MRU (HDLC_MAX_MTU + 10 + 14 + 4) /* for ETH+VLAN over FR */ | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 18 | #else | 
|  | 19 | #define HDLC_MAX_MRU 1600 /* as required for FR network */ | 
|  | 20 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 |  | 
|  | 22 |  | 
|  | 23 | #ifdef __KERNEL__ | 
|  | 24 |  | 
|  | 25 | #include <linux/skbuff.h> | 
|  | 26 | #include <linux/netdevice.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/hdlc/ioctl.h> | 
|  | 28 |  | 
|  | 29 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 30 | /* Used by all network devices here, pointed to by netdev_priv(dev) */ | 
|  | 31 | struct hdlc_device_desc { | 
|  | 32 | int (*netif_rx)(struct sk_buff *skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | struct net_device_stats stats; | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 34 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 36 | /* This structure is a private property of HDLC protocols. | 
|  | 37 | Hardware drivers have no interest here */ | 
|  | 38 |  | 
|  | 39 | struct hdlc_proto { | 
|  | 40 | int (*open)(struct net_device *dev); | 
|  | 41 | void (*close)(struct net_device *dev); | 
|  | 42 | void (*start)(struct net_device *dev); /* if open & DCD */ | 
|  | 43 | void (*stop)(struct net_device *dev); /* if open & !DCD */ | 
|  | 44 | void (*detach)(struct net_device *dev); | 
|  | 45 | int (*ioctl)(struct net_device *dev, struct ifreq *ifr); | 
| Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 46 | __be16 (*type_trans)(struct sk_buff *skb, struct net_device *dev); | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 47 | struct module *module; | 
|  | 48 | struct hdlc_proto *next; /* next protocol in the list */ | 
|  | 49 | }; | 
|  | 50 |  | 
|  | 51 |  | 
|  | 52 | typedef struct hdlc_device { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* used by HDLC layer to take control over HDLC device from hw driver*/ | 
|  | 54 | int (*attach)(struct net_device *dev, | 
|  | 55 | unsigned short encoding, unsigned short parity); | 
|  | 56 |  | 
|  | 57 | /* hardware driver must handle this instead of dev->hard_start_xmit */ | 
|  | 58 | int (*xmit)(struct sk_buff *skb, struct net_device *dev); | 
|  | 59 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* Things below are for HDLC layer internal use only */ | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 61 | const struct hdlc_proto *proto; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int carrier; | 
|  | 63 | int open; | 
|  | 64 | spinlock_t state_lock; | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 65 | void *state; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | void *priv; | 
|  | 67 | }hdlc_device; | 
|  | 68 |  | 
|  | 69 |  | 
|  | 70 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 71 | /* Exported from hdlc module */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | /* Called by hardware driver when a user requests HDLC service */ | 
|  | 74 | int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd); | 
|  | 75 |  | 
|  | 76 | /* Must be used by hardware driver on module startup/exit */ | 
| Krzysztof Halasa | 4a31e34 | 2006-06-22 22:20:19 +0200 | [diff] [blame] | 77 | #define register_hdlc_device(dev)	register_netdev(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | void unregister_hdlc_device(struct net_device *dev); | 
|  | 79 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 80 |  | 
|  | 81 | void register_hdlc_protocol(struct hdlc_proto *proto); | 
|  | 82 | void unregister_hdlc_protocol(struct hdlc_proto *proto); | 
|  | 83 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | struct net_device *alloc_hdlcdev(void *priv); | 
|  | 85 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 86 |  | 
|  | 87 | static __inline__ struct hdlc_device_desc* dev_to_desc(struct net_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { | 
|  | 89 | return netdev_priv(dev); | 
|  | 90 | } | 
|  | 91 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 92 | static __inline__ hdlc_device* dev_to_hdlc(struct net_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 94 | return netdev_priv(dev) + sizeof(struct hdlc_device_desc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 |  | 
|  | 98 | static __inline__ void debug_frame(const struct sk_buff *skb) | 
|  | 99 | { | 
|  | 100 | int i; | 
|  | 101 |  | 
|  | 102 | for (i=0; i < skb->len; i++) { | 
|  | 103 | if (i == 100) { | 
|  | 104 | printk("...\n"); | 
|  | 105 | return; | 
|  | 106 | } | 
|  | 107 | printk(" %02X", skb->data[i]); | 
|  | 108 | } | 
|  | 109 | printk("\n"); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 |  | 
|  | 113 | /* Must be called by hardware driver when HDLC device is being opened */ | 
|  | 114 | int hdlc_open(struct net_device *dev); | 
|  | 115 | /* Must be called by hardware driver when HDLC device is being closed */ | 
|  | 116 | void hdlc_close(struct net_device *dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 118 | int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto, | 
|  | 119 | int (*rx)(struct sk_buff *skb), size_t size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* May be used by hardware driver to gain control over HDLC device */ | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 121 | void detach_hdlc_protocol(struct net_device *dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | static __inline__ struct net_device_stats *hdlc_stats(struct net_device *dev) | 
|  | 124 | { | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 125 | return &dev_to_desc(dev)->stats; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
|  | 128 |  | 
| Alexey Dobriyan | ab61148 | 2005-07-12 12:08:43 -0700 | [diff] [blame] | 129 | static __inline__ __be16 hdlc_type_trans(struct sk_buff *skb, | 
|  | 130 | struct net_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { | 
|  | 132 | hdlc_device *hdlc = dev_to_hdlc(dev); | 
|  | 133 |  | 
| Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 134 | skb->dev = dev; | 
|  | 135 | skb_reset_mac_header(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 |  | 
| Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 137 | if (hdlc->proto->type_trans) | 
|  | 138 | return hdlc->proto->type_trans(skb, dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | else | 
|  | 140 | return htons(ETH_P_HDLC); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | #endif /* __KERNEL */ | 
|  | 144 | #endif /* __HDLC_H */ |