hdlc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Generic HDLC support routines for Linux
  4. *
  5. * Copyright (C) 1999-2005 Krzysztof Halasa <khc@pm.waw.pl>
  6. */
  7. #ifndef __HDLC_H
  8. #define __HDLC_H
  9. #include <linux/skbuff.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/hdlc/ioctl.h>
  12. #include <uapi/linux/hdlc.h>
  13. /* This structure is a private property of HDLC protocols.
  14. Hardware drivers have no interest here */
  15. struct hdlc_proto {
  16. int (*open)(struct net_device *dev);
  17. void (*close)(struct net_device *dev);
  18. void (*start)(struct net_device *dev); /* if open & DCD */
  19. void (*stop)(struct net_device *dev); /* if open & !DCD */
  20. void (*detach)(struct net_device *dev);
  21. int (*ioctl)(struct net_device *dev, struct ifreq *ifr);
  22. __be16 (*type_trans)(struct sk_buff *skb, struct net_device *dev);
  23. int (*netif_rx)(struct sk_buff *skb);
  24. netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
  25. struct module *module;
  26. struct hdlc_proto *next; /* next protocol in the list */
  27. };
  28. /* Pointed to by netdev_priv(dev) */
  29. typedef struct hdlc_device {
  30. /* used by HDLC layer to take control over HDLC device from hw driver*/
  31. int (*attach)(struct net_device *dev,
  32. unsigned short encoding, unsigned short parity);
  33. /* hardware driver must handle this instead of dev->hard_start_xmit */
  34. netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
  35. /* Things below are for HDLC layer internal use only */
  36. const struct hdlc_proto *proto;
  37. int carrier;
  38. int open;
  39. spinlock_t state_lock;
  40. void *state;
  41. void *priv;
  42. } hdlc_device;
  43. /* Exported from hdlc module */
  44. /* Called by hardware driver when a user requests HDLC service */
  45. int hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  46. /* Must be used by hardware driver on module startup/exit */
  47. #define register_hdlc_device(dev) register_netdev(dev)
  48. void unregister_hdlc_device(struct net_device *dev);
  49. void register_hdlc_protocol(struct hdlc_proto *proto);
  50. void unregister_hdlc_protocol(struct hdlc_proto *proto);
  51. struct net_device *alloc_hdlcdev(void *priv);
  52. static inline struct hdlc_device* dev_to_hdlc(struct net_device *dev)
  53. {
  54. return netdev_priv(dev);
  55. }
  56. static __inline__ void debug_frame(const struct sk_buff *skb)
  57. {
  58. int i;
  59. for (i=0; i < skb->len; i++) {
  60. if (i == 100) {
  61. printk("...\n");
  62. return;
  63. }
  64. printk(" %02X", skb->data[i]);
  65. }
  66. printk("\n");
  67. }
  68. /* Must be called by hardware driver when HDLC device is being opened */
  69. int hdlc_open(struct net_device *dev);
  70. /* Must be called by hardware driver when HDLC device is being closed */
  71. void hdlc_close(struct net_device *dev);
  72. /* Must be pointed to by hw driver's dev->netdev_ops->ndo_start_xmit */
  73. netdev_tx_t hdlc_start_xmit(struct sk_buff *skb, struct net_device *dev);
  74. int attach_hdlc_protocol(struct net_device *dev, struct hdlc_proto *proto,
  75. size_t size);
  76. /* May be used by hardware driver to gain control over HDLC device */
  77. int detach_hdlc_protocol(struct net_device *dev);
  78. static __inline__ __be16 hdlc_type_trans(struct sk_buff *skb,
  79. struct net_device *dev)
  80. {
  81. hdlc_device *hdlc = dev_to_hdlc(dev);
  82. skb->dev = dev;
  83. skb_reset_mac_header(skb);
  84. if (hdlc->proto->type_trans)
  85. return hdlc->proto->type_trans(skb, dev);
  86. else
  87. return htons(ETH_P_HDLC);
  88. }
  89. #endif /* __HDLC_H */