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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #ifndef __HDLC_H |
| 11 | #define __HDLC_H |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/netdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/hdlc/ioctl.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 17 | #include <uapi/linux/hdlc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 19 | /* This structure is a private property of HDLC protocols. |
| 20 | Hardware drivers have no interest here */ |
| 21 | |
| 22 | struct 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 Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 29 | __be16 (*type_trans)(struct sk_buff *skb, struct net_device *dev); |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 30 | int (*netif_rx)(struct sk_buff *skb); |
Stephen Hemminger | 4c5d502 | 2009-08-31 19:50:48 +0000 | [diff] [blame] | 31 | netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 32 | struct module *module; |
| 33 | struct hdlc_proto *next; /* next protocol in the list */ |
| 34 | }; |
| 35 | |
| 36 | |
Wang Chen | b74ca3a | 2008-12-08 01:14:16 -0800 | [diff] [blame] | 37 | /* Pointed to by netdev_priv(dev) */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 38 | typedef struct hdlc_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* 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 Hemminger | 4c5d502 | 2009-08-31 19:50:48 +0000 | [diff] [blame] | 44 | netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* Things below are for HDLC layer internal use only */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 47 | const struct hdlc_proto *proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | int carrier; |
| 49 | int open; |
| 50 | spinlock_t state_lock; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 51 | void *state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | void *priv; |
Stephen Hemminger | 4c5d502 | 2009-08-31 19:50:48 +0000 | [diff] [blame] | 53 | } hdlc_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 57 | /* Exported from hdlc module */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* Called by hardware driver when a user requests HDLC service */ |
| 60 | int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd); |
| 61 | |
| 62 | /* Must be used by hardware driver on module startup/exit */ |
Krzysztof Halasa | 4a31e34 | 2006-06-22 22:20:19 +0200 | [diff] [blame] | 63 | #define register_hdlc_device(dev) register_netdev(dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | void unregister_hdlc_device(struct net_device *dev); |
| 65 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 66 | |
| 67 | void register_hdlc_protocol(struct hdlc_proto *proto); |
| 68 | void unregister_hdlc_protocol(struct hdlc_proto *proto); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct net_device *alloc_hdlcdev(void *priv); |
| 71 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 72 | static inline struct hdlc_device* dev_to_hdlc(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
Wang Chen | 2baf8a2 | 2008-11-21 16:34:18 -0800 | [diff] [blame] | 74 | return netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static __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 */ |
| 93 | int hdlc_open(struct net_device *dev); |
| 94 | /* Must be called by hardware driver when HDLC device is being closed */ |
| 95 | void hdlc_close(struct net_device *dev); |
Krzysztof HaĆasa | 991990a | 2009-01-08 22:52:11 +0100 | [diff] [blame] | 96 | /* May be used by hardware driver */ |
| 97 | int 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 Hemminger | 4c5d502 | 2009-08-31 19:50:48 +0000 | [diff] [blame] | 99 | netdev_tx_t hdlc_start_xmit(struct sk_buff *skb, struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 101 | int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto, |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 102 | size_t size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* May be used by hardware driver to gain control over HDLC device */ |
Andrew Lunn | 2f8364a | 2015-12-03 21:12:31 +0100 | [diff] [blame] | 104 | int detach_hdlc_protocol(struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Alexey Dobriyan | ab61148 | 2005-07-12 12:08:43 -0700 | [diff] [blame] | 106 | static __inline__ __be16 hdlc_type_trans(struct sk_buff *skb, |
| 107 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 110 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 111 | skb->dev = dev; |
| 112 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 114 | if (hdlc->proto->type_trans) |
| 115 | return hdlc->proto->type_trans(skb, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | else |
| 117 | return htons(ETH_P_HDLC); |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | #endif /* __HDLC_H */ |