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
10 #include <linux/netdevice.h>
12 #include <linux/spinlock.h>
13 #include <uapi/linux/hdlcdrv.h>
15 #define HDLCDRV_MAGIC 0x5ac6e778
16 #define HDLCDRV_HDLCBUFFER 32 /* should be a power of 2 for speed reasons */
17 #define HDLCDRV_BITBUFFER 256 /* should be a power of 2 for speed reasons */
18 #undef HDLCDRV_LOOPBACK /* define for HDLC debugging purposes */
21 /* maximum packet length, excluding CRC */
22 #define HDLCDRV_MAXFLEN 400
25 struct hdlcdrv_hdlcbuffer {
28 unsigned short buf[HDLCDRV_HDLCBUFFER];
32 struct hdlcdrv_bitbuffer {
36 unsigned char buffer[HDLCDRV_BITBUFFER];
39 static inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer *buf,
46 buf->shreg |= (!!bit) << 7;
48 buf->buffer[buf->wr] = buf->shreg;
49 buf->wr = (buf->wr+1) % sizeof(buf->buffer);
54 static inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer *buf,
57 buf->buffer[buf->wr] = bits & 0xff;
58 buf->wr = (buf->wr+1) % sizeof(buf->buffer);
59 buf->buffer[buf->wr] = (bits >> 8) & 0xff;
60 buf->wr = (buf->wr+1) % sizeof(buf->buffer);
63 #endif /* HDLCDRV_DEBUG */
65 /* -------------------------------------------------------------------- */
67 * Information that need to be kept for each driver.
72 * first some informations needed by the hdlcdrv routines
77 * the routines called by the hdlcdrv routines
79 int (*open)(struct net_device *);
80 int (*close)(struct net_device *);
81 int (*ioctl)(struct net_device *, struct ifreq *,
82 struct hdlcdrv_ioctl *, int);
85 struct hdlcdrv_state {
89 const struct hdlcdrv_ops *ops;
95 struct hdlcdrv_pttoutput {
103 struct hdlcdrv_channel_params ch_params;
105 struct hdlcdrv_hdlcrx {
106 struct hdlcdrv_hdlcbuffer hbuf;
107 unsigned long in_hdlc_rx;
108 /* 0 = sync hunt, != 0 receiving */
110 unsigned int bitstream;
117 unsigned char buffer[HDLCDRV_MAXFLEN+2];
120 struct hdlcdrv_hdlctx {
121 struct hdlcdrv_hdlcbuffer hbuf;
122 unsigned long in_hdlc_tx;
125 * 1 = send txtail (flags)
130 unsigned int bitstream;
140 unsigned char buffer[HDLCDRV_MAXFLEN+2];
144 struct hdlcdrv_bitbuffer bitbuf_channel;
145 struct hdlcdrv_bitbuffer bitbuf_hdlc;
146 #endif /* HDLCDRV_DEBUG */
150 /* queued skb for transmission */
155 /* -------------------------------------------------------------------- */
157 static inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer *hb)
162 spin_lock_irqsave(&hb->lock, flags);
163 ret = !((HDLCDRV_HDLCBUFFER - 1 + hb->rd - hb->wr) % HDLCDRV_HDLCBUFFER);
164 spin_unlock_irqrestore(&hb->lock, flags);
168 /* -------------------------------------------------------------------- */
170 static inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer *hb)
175 spin_lock_irqsave(&hb->lock, flags);
176 ret = (hb->rd == hb->wr);
177 spin_unlock_irqrestore(&hb->lock, flags);
181 /* -------------------------------------------------------------------- */
183 static inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer *hb)
189 spin_lock_irqsave(&hb->lock, flags);
190 if (hb->rd == hb->wr)
193 newr = (hb->rd+1) % HDLCDRV_HDLCBUFFER;
194 val = hb->buf[hb->rd];
197 spin_unlock_irqrestore(&hb->lock, flags);
201 /* -------------------------------------------------------------------- */
203 static inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer *hb,
209 spin_lock_irqsave(&hb->lock, flags);
210 newp = (hb->wr+1) % HDLCDRV_HDLCBUFFER;
211 if (newp != hb->rd) {
212 hb->buf[hb->wr] = val & 0xffff;
215 spin_unlock_irqrestore(&hb->lock, flags);
218 /* -------------------------------------------------------------------- */
220 static inline void hdlcdrv_putbits(struct hdlcdrv_state *s, unsigned int bits)
222 hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, bits);
225 static inline unsigned int hdlcdrv_getbits(struct hdlcdrv_state *s)
229 if (hdlcdrv_hbuf_empty(&s->hdlctx.hbuf)) {
230 if (s->hdlctx.calibrate > 0)
231 s->hdlctx.calibrate--;
236 ret = hdlcdrv_hbuf_get(&s->hdlctx.hbuf);
237 #ifdef HDLCDRV_LOOPBACK
238 hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, ret);
239 #endif /* HDLCDRV_LOOPBACK */
243 static inline void hdlcdrv_channelbit(struct hdlcdrv_state *s, unsigned int bit)
246 hdlcdrv_add_bitbuffer(&s->bitbuf_channel, bit);
247 #endif /* HDLCDRV_DEBUG */
250 static inline void hdlcdrv_setdcd(struct hdlcdrv_state *s, int dcd)
252 s->hdlcrx.dcd = !!dcd;
255 static inline int hdlcdrv_ptt(struct hdlcdrv_state *s)
257 return s->hdlctx.ptt || (s->hdlctx.calibrate > 0);
260 /* -------------------------------------------------------------------- */
262 void hdlcdrv_receiver(struct net_device *, struct hdlcdrv_state *);
263 void hdlcdrv_transmitter(struct net_device *, struct hdlcdrv_state *);
264 void hdlcdrv_arbitrate(struct net_device *, struct hdlcdrv_state *);
265 struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
266 unsigned int privsize, const char *ifname,
267 unsigned int baseaddr, unsigned int irq,
269 void hdlcdrv_unregister(struct net_device *dev);
271 /* -------------------------------------------------------------------- */
275 #endif /* _HDLCDRV_H */