hdlcdrv.h 6.3 KB

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