blob: e31bcd4c7859e5d01405868090246f4f94a984a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 *
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02004 * Copyright (C) 1999-2005 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#ifndef __HDLC_H
11#define __HDLC_H
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
15#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/hdlc/ioctl.h>
David Howells607ca462012-10-13 10:46:48 +010017#include <uapi/linux/hdlc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020019/* This structure is a private property of HDLC protocols.
20 Hardware drivers have no interest here */
21
22struct hdlc_proto {
23 int (*open)(struct net_device *dev);
24 void (*close)(struct net_device *dev);
25 void (*start)(struct net_device *dev); /* if open & DCD */
26 void (*stop)(struct net_device *dev); /* if open & !DCD */
27 void (*detach)(struct net_device *dev);
28 int (*ioctl)(struct net_device *dev, struct ifreq *ifr);
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +020029 __be16 (*type_trans)(struct sk_buff *skb, struct net_device *dev);
Krzysztof Halasa40d25142008-02-01 22:37:12 +010030 int (*netif_rx)(struct sk_buff *skb);
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000031 netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020032 struct module *module;
33 struct hdlc_proto *next; /* next protocol in the list */
34};
35
36
Wang Chenb74ca3a2008-12-08 01:14:16 -080037/* Pointed to by netdev_priv(dev) */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020038typedef struct hdlc_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 /* used by HDLC layer to take control over HDLC device from hw driver*/
40 int (*attach)(struct net_device *dev,
41 unsigned short encoding, unsigned short parity);
42
43 /* hardware driver must handle this instead of dev->hard_start_xmit */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000044 netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 /* Things below are for HDLC layer internal use only */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020047 const struct hdlc_proto *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int carrier;
49 int open;
50 spinlock_t state_lock;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020051 void *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 void *priv;
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000053} hdlc_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
56
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020057/* Exported from hdlc module */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Called by hardware driver when a user requests HDLC service */
60int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
61
62/* Must be used by hardware driver on module startup/exit */
Krzysztof Halasa4a31e342006-06-22 22:20:19 +020063#define register_hdlc_device(dev) register_netdev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064void unregister_hdlc_device(struct net_device *dev);
65
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020066
67void register_hdlc_protocol(struct hdlc_proto *proto);
68void unregister_hdlc_protocol(struct hdlc_proto *proto);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct net_device *alloc_hdlcdev(void *priv);
71
Krzysztof Halasa40d25142008-02-01 22:37:12 +010072static inline struct hdlc_device* dev_to_hdlc(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Wang Chen2baf8a22008-11-21 16:34:18 -080074 return netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static __inline__ void debug_frame(const struct sk_buff *skb)
78{
79 int i;
80
81 for (i=0; i < skb->len; i++) {
82 if (i == 100) {
83 printk("...\n");
84 return;
85 }
86 printk(" %02X", skb->data[i]);
87 }
88 printk("\n");
89}
90
91
92/* Must be called by hardware driver when HDLC device is being opened */
93int hdlc_open(struct net_device *dev);
94/* Must be called by hardware driver when HDLC device is being closed */
95void hdlc_close(struct net_device *dev);
Krzysztof HaƂasa991990a2009-01-08 22:52:11 +010096/* May be used by hardware driver */
97int hdlc_change_mtu(struct net_device *dev, int new_mtu);
98/* Must be pointed to by hw driver's dev->netdev_ops->ndo_start_xmit */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000099netdev_tx_t hdlc_start_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200101int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
Krzysztof Halasa40d25142008-02-01 22:37:12 +0100102 size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* May be used by hardware driver to gain control over HDLC device */
Andrew Lunn2f8364a2015-12-03 21:12:31 +0100104int detach_hdlc_protocol(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alexey Dobriyanab611482005-07-12 12:08:43 -0700106static __inline__ __be16 hdlc_type_trans(struct sk_buff *skb,
107 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 hdlc_device *hdlc = dev_to_hdlc(dev);
110
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700111 skb->dev = dev;
112 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200114 if (hdlc->proto->type_trans)
115 return hdlc->proto->type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 else
117 return htons(ETH_P_HDLC);
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#endif /* __HDLC_H */