hdlc.h 3.7 KB

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