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