blob: cacc4dd27794514a92a6b1fc39fb6db5f374f4e0 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Generic HDLC support routines for Linux
4 *
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02005 * Copyright (C) 1999-2005 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#ifndef __HDLC_H
8#define __HDLC_H
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/skbuff.h>
12#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hdlc/ioctl.h>
David Howells607ca462012-10-13 10:46:48 +010014#include <uapi/linux/hdlc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020016/* This structure is a private property of HDLC protocols.
17 Hardware drivers have no interest here */
18
19struct hdlc_proto {
20 int (*open)(struct net_device *dev);
21 void (*close)(struct net_device *dev);
22 void (*start)(struct net_device *dev); /* if open & DCD */
23 void (*stop)(struct net_device *dev); /* if open & !DCD */
24 void (*detach)(struct net_device *dev);
25 int (*ioctl)(struct net_device *dev, struct ifreq *ifr);
Krzysztof Halasaabf17ff2007-04-27 13:13:33 +020026 __be16 (*type_trans)(struct sk_buff *skb, struct net_device *dev);
Krzysztof Halasa40d25142008-02-01 22:37:12 +010027 int (*netif_rx)(struct sk_buff *skb);
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000028 netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020029 struct module *module;
30 struct hdlc_proto *next; /* next protocol in the list */
31};
32
33
Wang Chenb74ca3a2008-12-08 01:14:16 -080034/* Pointed to by netdev_priv(dev) */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020035typedef struct hdlc_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 /* used by HDLC layer to take control over HDLC device from hw driver*/
37 int (*attach)(struct net_device *dev,
38 unsigned short encoding, unsigned short parity);
39
40 /* hardware driver must handle this instead of dev->hard_start_xmit */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000041 netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 /* Things below are for HDLC layer internal use only */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020044 const struct hdlc_proto *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 int carrier;
46 int open;
47 spinlock_t state_lock;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020048 void *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 void *priv;
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000050} hdlc_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
53
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020054/* Exported from hdlc module */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* Called by hardware driver when a user requests HDLC service */
57int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
58
59/* Must be used by hardware driver on module startup/exit */
Krzysztof Halasa4a31e342006-06-22 22:20:19 +020060#define register_hdlc_device(dev) register_netdev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061void unregister_hdlc_device(struct net_device *dev);
62
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020063
64void register_hdlc_protocol(struct hdlc_proto *proto);
65void unregister_hdlc_protocol(struct hdlc_proto *proto);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067struct net_device *alloc_hdlcdev(void *priv);
68
Krzysztof Halasa40d25142008-02-01 22:37:12 +010069static inline struct hdlc_device* dev_to_hdlc(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Wang Chen2baf8a22008-11-21 16:34:18 -080071 return netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static __inline__ void debug_frame(const struct sk_buff *skb)
75{
76 int i;
77
78 for (i=0; i < skb->len; i++) {
79 if (i == 100) {
80 printk("...\n");
81 return;
82 }
83 printk(" %02X", skb->data[i]);
84 }
85 printk("\n");
86}
87
88
89/* Must be called by hardware driver when HDLC device is being opened */
90int hdlc_open(struct net_device *dev);
91/* Must be called by hardware driver when HDLC device is being closed */
92void hdlc_close(struct net_device *dev);
Krzysztof HaƂasa991990a2009-01-08 22:52:11 +010093/* Must be pointed to by hw driver's dev->netdev_ops->ndo_start_xmit */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +000094netdev_tx_t hdlc_start_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020096int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
Krzysztof Halasa40d25142008-02-01 22:37:12 +010097 size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* May be used by hardware driver to gain control over HDLC device */
Andrew Lunn2f8364a2015-12-03 21:12:31 +010099int detach_hdlc_protocol(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Alexey Dobriyanab611482005-07-12 12:08:43 -0700101static __inline__ __be16 hdlc_type_trans(struct sk_buff *skb,
102 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 hdlc_device *hdlc = dev_to_hdlc(dev);
105
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700106 skb->dev = dev;
107 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200109 if (hdlc->proto->type_trans)
110 return hdlc->proto->type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 else
112 return htons(ETH_P_HDLC);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#endif /* __HDLC_H */