hdlcdrv.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * hdlcdrv.h -- HDLC packet radio network driver.
  4. * The Linux soundcard driver for 1200 baud and 9600 baud packet radio
  5. * (C) 1996-1998 by Thomas Sailer, HB9JNX/AE4WA
  6. */
  7. #ifndef _HDLCDRV_H
  8. #define _HDLCDRV_H
  9. #include <linux/netdevice.h>
  10. #include <linux/if.h>
  11. #include <linux/spinlock.h>
  12. #include <uapi/linux/hdlcdrv.h>
  13. #define HDLCDRV_MAGIC 0x5ac6e778
  14. #define HDLCDRV_HDLCBUFFER 32 /* should be a power of 2 for speed reasons */
  15. #define HDLCDRV_BITBUFFER 256 /* should be a power of 2 for speed reasons */
  16. #undef HDLCDRV_LOOPBACK /* define for HDLC debugging purposes */
  17. #define HDLCDRV_DEBUG
  18. /* maximum packet length, excluding CRC */
  19. #define HDLCDRV_MAXFLEN 400
  20. struct hdlcdrv_hdlcbuffer {
  21. spinlock_t lock;
  22. unsigned rd, wr;
  23. unsigned short buf[HDLCDRV_HDLCBUFFER];
  24. };
  25. #ifdef HDLCDRV_DEBUG
  26. struct hdlcdrv_bitbuffer {
  27. unsigned int rd;
  28. unsigned int wr;
  29. unsigned int shreg;
  30. unsigned char buffer[HDLCDRV_BITBUFFER];
  31. };
  32. static inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer *buf,
  33. unsigned int bit)
  34. {
  35. unsigned char new;
  36. new = buf->shreg & 1;
  37. buf->shreg >>= 1;
  38. buf->shreg |= (!!bit) << 7;
  39. if (new) {
  40. buf->buffer[buf->wr] = buf->shreg;
  41. buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  42. buf->shreg = 0x80;
  43. }
  44. }
  45. static inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer *buf,
  46. unsigned int bits)
  47. {
  48. buf->buffer[buf->wr] = bits & 0xff;
  49. buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  50. buf->buffer[buf->wr] = (bits >> 8) & 0xff;
  51. buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  52. }
  53. #endif /* HDLCDRV_DEBUG */
  54. /* -------------------------------------------------------------------- */
  55. /*
  56. * Information that need to be kept for each driver.
  57. */
  58. struct hdlcdrv_ops {
  59. /*
  60. * first some informations needed by the hdlcdrv routines
  61. */
  62. const char *drvname;
  63. const char *drvinfo;
  64. /*
  65. * the routines called by the hdlcdrv routines
  66. */
  67. int (*open)(struct net_device *);
  68. int (*close)(struct net_device *);
  69. int (*ioctl)(struct net_device *, struct ifreq *,
  70. struct hdlcdrv_ioctl *, int);
  71. };
  72. struct hdlcdrv_state {
  73. int magic;
  74. int opened;
  75. const struct hdlcdrv_ops *ops;
  76. struct {
  77. int bitrate;
  78. } par;
  79. struct hdlcdrv_pttoutput {
  80. int dma2;
  81. int seriobase;
  82. int pariobase;
  83. int midiiobase;
  84. unsigned int flags;
  85. } ptt_out;
  86. struct hdlcdrv_channel_params ch_params;
  87. struct hdlcdrv_hdlcrx {
  88. struct hdlcdrv_hdlcbuffer hbuf;
  89. unsigned long in_hdlc_rx;
  90. /* 0 = sync hunt, != 0 receiving */
  91. int rx_state;
  92. unsigned int bitstream;
  93. unsigned int bitbuf;
  94. int numbits;
  95. unsigned char dcd;
  96. int len;
  97. unsigned char *bp;
  98. unsigned char buffer[HDLCDRV_MAXFLEN+2];
  99. } hdlcrx;
  100. struct hdlcdrv_hdlctx {
  101. struct hdlcdrv_hdlcbuffer hbuf;
  102. unsigned long in_hdlc_tx;
  103. /*
  104. * 0 = send flags
  105. * 1 = send txtail (flags)
  106. * 2 = send packet
  107. */
  108. int tx_state;
  109. int numflags;
  110. unsigned int bitstream;
  111. unsigned char ptt;
  112. int calibrate;
  113. int slotcnt;
  114. unsigned int bitbuf;
  115. int numbits;
  116. int len;
  117. unsigned char *bp;
  118. unsigned char buffer[HDLCDRV_MAXFLEN+2];
  119. } hdlctx;
  120. #ifdef HDLCDRV_DEBUG
  121. struct hdlcdrv_bitbuffer bitbuf_channel;
  122. struct hdlcdrv_bitbuffer bitbuf_hdlc;
  123. #endif /* HDLCDRV_DEBUG */
  124. int ptt_keyed;
  125. /* queued skb for transmission */
  126. struct sk_buff *skb;
  127. };
  128. /* -------------------------------------------------------------------- */
  129. static inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer *hb)
  130. {
  131. unsigned long flags;
  132. int ret;
  133. spin_lock_irqsave(&hb->lock, flags);
  134. ret = !((HDLCDRV_HDLCBUFFER - 1 + hb->rd - hb->wr) % HDLCDRV_HDLCBUFFER);
  135. spin_unlock_irqrestore(&hb->lock, flags);
  136. return ret;
  137. }
  138. /* -------------------------------------------------------------------- */
  139. static inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer *hb)
  140. {
  141. unsigned long flags;
  142. int ret;
  143. spin_lock_irqsave(&hb->lock, flags);
  144. ret = (hb->rd == hb->wr);
  145. spin_unlock_irqrestore(&hb->lock, flags);
  146. return ret;
  147. }
  148. /* -------------------------------------------------------------------- */
  149. static inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer *hb)
  150. {
  151. unsigned long flags;
  152. unsigned short val;
  153. unsigned newr;
  154. spin_lock_irqsave(&hb->lock, flags);
  155. if (hb->rd == hb->wr)
  156. val = 0;
  157. else {
  158. newr = (hb->rd+1) % HDLCDRV_HDLCBUFFER;
  159. val = hb->buf[hb->rd];
  160. hb->rd = newr;
  161. }
  162. spin_unlock_irqrestore(&hb->lock, flags);
  163. return val;
  164. }
  165. /* -------------------------------------------------------------------- */
  166. static inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer *hb,
  167. unsigned short val)
  168. {
  169. unsigned newp;
  170. unsigned long flags;
  171. spin_lock_irqsave(&hb->lock, flags);
  172. newp = (hb->wr+1) % HDLCDRV_HDLCBUFFER;
  173. if (newp != hb->rd) {
  174. hb->buf[hb->wr] = val & 0xffff;
  175. hb->wr = newp;
  176. }
  177. spin_unlock_irqrestore(&hb->lock, flags);
  178. }
  179. /* -------------------------------------------------------------------- */
  180. static inline void hdlcdrv_putbits(struct hdlcdrv_state *s, unsigned int bits)
  181. {
  182. hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, bits);
  183. }
  184. static inline unsigned int hdlcdrv_getbits(struct hdlcdrv_state *s)
  185. {
  186. unsigned int ret;
  187. if (hdlcdrv_hbuf_empty(&s->hdlctx.hbuf)) {
  188. if (s->hdlctx.calibrate > 0)
  189. s->hdlctx.calibrate--;
  190. else
  191. s->hdlctx.ptt = 0;
  192. ret = 0;
  193. } else
  194. ret = hdlcdrv_hbuf_get(&s->hdlctx.hbuf);
  195. #ifdef HDLCDRV_LOOPBACK
  196. hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, ret);
  197. #endif /* HDLCDRV_LOOPBACK */
  198. return ret;
  199. }
  200. static inline void hdlcdrv_channelbit(struct hdlcdrv_state *s, unsigned int bit)
  201. {
  202. #ifdef HDLCDRV_DEBUG
  203. hdlcdrv_add_bitbuffer(&s->bitbuf_channel, bit);
  204. #endif /* HDLCDRV_DEBUG */
  205. }
  206. static inline void hdlcdrv_setdcd(struct hdlcdrv_state *s, int dcd)
  207. {
  208. s->hdlcrx.dcd = !!dcd;
  209. }
  210. static inline int hdlcdrv_ptt(struct hdlcdrv_state *s)
  211. {
  212. return s->hdlctx.ptt || (s->hdlctx.calibrate > 0);
  213. }
  214. /* -------------------------------------------------------------------- */
  215. void hdlcdrv_receiver(struct net_device *, struct hdlcdrv_state *);
  216. void hdlcdrv_transmitter(struct net_device *, struct hdlcdrv_state *);
  217. void hdlcdrv_arbitrate(struct net_device *, struct hdlcdrv_state *);
  218. struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
  219. unsigned int privsize, const char *ifname,
  220. unsigned int baseaddr, unsigned int irq,
  221. unsigned int dma);
  222. void hdlcdrv_unregister(struct net_device *dev);
  223. /* -------------------------------------------------------------------- */
  224. #endif /* _HDLCDRV_H */