hdlc.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Generic HDLC support routines for Linux
  3. *
  4. * Copyright (C) 1999-2003 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. #ifndef __HDLC_H
  11. #define __HDLC_H
  12. #define GENERIC_HDLC_VERSION 4 /* For synchronization with sethdlc utility */
  13. #define CLOCK_DEFAULT 0 /* Default setting */
  14. #define CLOCK_EXT 1 /* External TX and RX clock - DTE */
  15. #define CLOCK_INT 2 /* Internal TX and RX clock - DCE */
  16. #define CLOCK_TXINT 3 /* Internal TX and external RX clock */
  17. #define CLOCK_TXFROMRX 4 /* TX clock derived from external RX clock */
  18. #define ENCODING_DEFAULT 0 /* Default setting */
  19. #define ENCODING_NRZ 1
  20. #define ENCODING_NRZI 2
  21. #define ENCODING_FM_MARK 3
  22. #define ENCODING_FM_SPACE 4
  23. #define ENCODING_MANCHESTER 5
  24. #define PARITY_DEFAULT 0 /* Default setting */
  25. #define PARITY_NONE 1 /* No parity */
  26. #define PARITY_CRC16_PR0 2 /* CRC16, initial value 0x0000 */
  27. #define PARITY_CRC16_PR1 3 /* CRC16, initial value 0xFFFF */
  28. #define PARITY_CRC16_PR0_CCITT 4 /* CRC16, initial 0x0000, ITU-T version */
  29. #define PARITY_CRC16_PR1_CCITT 5 /* CRC16, initial 0xFFFF, ITU-T version */
  30. #define PARITY_CRC32_PR0_CCITT 6 /* CRC32, initial value 0x00000000 */
  31. #define PARITY_CRC32_PR1_CCITT 7 /* CRC32, initial value 0xFFFFFFFF */
  32. #define LMI_DEFAULT 0 /* Default setting */
  33. #define LMI_NONE 1 /* No LMI, all PVCs are static */
  34. #define LMI_ANSI 2 /* ANSI Annex D */
  35. #define LMI_CCITT 3 /* ITU-T Annex A */
  36. #define HDLC_MAX_MTU 1500 /* Ethernet 1500 bytes */
  37. #define HDLC_MAX_MRU (HDLC_MAX_MTU + 10 + 14 + 4) /* for ETH+VLAN over FR */
  38. #ifdef __KERNEL__
  39. #include <linux/skbuff.h>
  40. #include <linux/netdevice.h>
  41. #include <net/syncppp.h>
  42. #include <linux/hdlc/ioctl.h>
  43. typedef struct { /* Used in Cisco and PPP mode */
  44. u8 address;
  45. u8 control;
  46. u16 protocol;
  47. }__attribute__ ((packed)) hdlc_header;
  48. typedef struct {
  49. u32 type; /* code */
  50. u32 par1;
  51. u32 par2;
  52. u16 rel; /* reliability */
  53. u32 time;
  54. }__attribute__ ((packed)) cisco_packet;
  55. #define CISCO_PACKET_LEN 18
  56. #define CISCO_BIG_PACKET_LEN 20
  57. typedef struct pvc_device_struct {
  58. struct net_device *master;
  59. struct net_device *main;
  60. struct net_device *ether; /* bridged Ethernet interface */
  61. struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
  62. int dlci;
  63. int open_count;
  64. struct {
  65. unsigned int new: 1;
  66. unsigned int active: 1;
  67. unsigned int exist: 1;
  68. unsigned int deleted: 1;
  69. unsigned int fecn: 1;
  70. unsigned int becn: 1;
  71. }state;
  72. }pvc_device;
  73. typedef struct hdlc_device_struct {
  74. /* To be initialized by hardware driver */
  75. struct net_device_stats stats;
  76. /* used by HDLC layer to take control over HDLC device from hw driver*/
  77. int (*attach)(struct net_device *dev,
  78. unsigned short encoding, unsigned short parity);
  79. /* hardware driver must handle this instead of dev->hard_start_xmit */
  80. int (*xmit)(struct sk_buff *skb, struct net_device *dev);
  81. /* Things below are for HDLC layer internal use only */
  82. struct {
  83. int (*open)(struct net_device *dev);
  84. void (*close)(struct net_device *dev);
  85. /* if open & DCD */
  86. void (*start)(struct net_device *dev);
  87. /* if open & !DCD */
  88. void (*stop)(struct net_device *dev);
  89. void (*detach)(struct hdlc_device_struct *hdlc);
  90. int (*netif_rx)(struct sk_buff *skb);
  91. unsigned short (*type_trans)(struct sk_buff *skb,
  92. struct net_device *dev);
  93. int id; /* IF_PROTO_HDLC/CISCO/FR/etc. */
  94. }proto;
  95. int carrier;
  96. int open;
  97. spinlock_t state_lock;
  98. union {
  99. struct {
  100. fr_proto settings;
  101. pvc_device *first_pvc;
  102. int dce_pvc_count;
  103. struct timer_list timer;
  104. unsigned long last_poll;
  105. int reliable;
  106. int dce_changed;
  107. int request;
  108. int fullrep_sent;
  109. u32 last_errors; /* last errors bit list */
  110. u8 n391cnt;
  111. u8 txseq; /* TX sequence number */
  112. u8 rxseq; /* RX sequence number */
  113. }fr;
  114. struct {
  115. cisco_proto settings;
  116. struct timer_list timer;
  117. unsigned long last_poll;
  118. int up;
  119. int request_sent;
  120. u32 txseq; /* TX sequence number */
  121. u32 rxseq; /* RX sequence number */
  122. }cisco;
  123. struct {
  124. raw_hdlc_proto settings;
  125. }raw_hdlc;
  126. struct {
  127. struct ppp_device pppdev;
  128. struct ppp_device *syncppp_ptr;
  129. int (*old_change_mtu)(struct net_device *dev,
  130. int new_mtu);
  131. }ppp;
  132. }state;
  133. void *priv;
  134. }hdlc_device;
  135. int hdlc_raw_ioctl(struct net_device *dev, struct ifreq *ifr);
  136. int hdlc_raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
  137. int hdlc_cisco_ioctl(struct net_device *dev, struct ifreq *ifr);
  138. int hdlc_ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
  139. int hdlc_fr_ioctl(struct net_device *dev, struct ifreq *ifr);
  140. int hdlc_x25_ioctl(struct net_device *dev, struct ifreq *ifr);
  141. /* Exported from hdlc.o */
  142. /* Called by hardware driver when a user requests HDLC service */
  143. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  144. /* Must be used by hardware driver on module startup/exit */
  145. int register_hdlc_device(struct net_device *dev);
  146. void unregister_hdlc_device(struct net_device *dev);
  147. struct net_device *alloc_hdlcdev(void *priv);
  148. static __inline__ hdlc_device* dev_to_hdlc(struct net_device *dev)
  149. {
  150. return netdev_priv(dev);
  151. }
  152. static __inline__ pvc_device* dev_to_pvc(struct net_device *dev)
  153. {
  154. return (pvc_device*)dev->priv;
  155. }
  156. static __inline__ void debug_frame(const struct sk_buff *skb)
  157. {
  158. int i;
  159. for (i=0; i < skb->len; i++) {
  160. if (i == 100) {
  161. printk("...\n");
  162. return;
  163. }
  164. printk(" %02X", skb->data[i]);
  165. }
  166. printk("\n");
  167. }
  168. /* Must be called by hardware driver when HDLC device is being opened */
  169. int hdlc_open(struct net_device *dev);
  170. /* Must be called by hardware driver when HDLC device is being closed */
  171. void hdlc_close(struct net_device *dev);
  172. /* Called by hardware driver when DCD line level changes */
  173. void hdlc_set_carrier(int on, struct net_device *dev);
  174. /* May be used by hardware driver to gain control over HDLC device */
  175. static __inline__ void hdlc_proto_detach(hdlc_device *hdlc)
  176. {
  177. if (hdlc->proto.detach)
  178. hdlc->proto.detach(hdlc);
  179. hdlc->proto.detach = NULL;
  180. }
  181. static __inline__ struct net_device_stats *hdlc_stats(struct net_device *dev)
  182. {
  183. return &dev_to_hdlc(dev)->stats;
  184. }
  185. static __inline__ unsigned short hdlc_type_trans(struct sk_buff *skb,
  186. struct net_device *dev)
  187. {
  188. hdlc_device *hdlc = dev_to_hdlc(dev);
  189. skb->mac.raw = skb->data;
  190. skb->dev = dev;
  191. if (hdlc->proto.type_trans)
  192. return hdlc->proto.type_trans(skb, dev);
  193. else
  194. return htons(ETH_P_HDLC);
  195. }
  196. #endif /* __KERNEL */
  197. #endif /* __HDLC_H */