1 /* SPDX-License-Identifier: GPL-2.0 */
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
11 #include <linux/netdevice.h>
13 #include <linux/spinlock.h>
14 #include <uapi/linux/hdlcdrv.h>
16 #define HDLCDRV_MAGIC 0x5ac6e778
17 #define HDLCDRV_HDLCBUFFER 32 /* should be a power of 2 for speed reasons */
18 #define HDLCDRV_BITBUFFER 256 /* should be a power of 2 for speed reasons */
19 #undef HDLCDRV_LOOPBACK /* define for HDLC debugging purposes */
22 /* maximum packet length, excluding CRC */
23 #define HDLCDRV_MAXFLEN 400
26 struct hdlcdrv_hdlcbuffer
{
29 unsigned short buf
[HDLCDRV_HDLCBUFFER
];
33 struct hdlcdrv_bitbuffer
{
37 unsigned char buffer
[HDLCDRV_BITBUFFER
];
40 static inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer
*buf
,
47 buf
->shreg
|= (!!bit
) << 7;
49 buf
->buffer
[buf
->wr
] = buf
->shreg
;
50 buf
->wr
= (buf
->wr
+1) % sizeof(buf
->buffer
);
55 static inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer
*buf
,
58 buf
->buffer
[buf
->wr
] = bits
& 0xff;
59 buf
->wr
= (buf
->wr
+1) % sizeof(buf
->buffer
);
60 buf
->buffer
[buf
->wr
] = (bits
>> 8) & 0xff;
61 buf
->wr
= (buf
->wr
+1) % sizeof(buf
->buffer
);
64 #endif /* HDLCDRV_DEBUG */
66 /* -------------------------------------------------------------------- */
68 * Information that need to be kept for each driver.
73 * first some informations needed by the hdlcdrv routines
78 * the routines called by the hdlcdrv routines
80 int (*open
)(struct net_device
*);
81 int (*close
)(struct net_device
*);
82 int (*ioctl
)(struct net_device
*, struct ifreq
*,
83 struct hdlcdrv_ioctl
*, int);
86 struct hdlcdrv_state
{
90 const struct hdlcdrv_ops
*ops
;
96 struct hdlcdrv_pttoutput
{
104 struct hdlcdrv_channel_params ch_params
;
106 struct hdlcdrv_hdlcrx
{
107 struct hdlcdrv_hdlcbuffer hbuf
;
108 unsigned long in_hdlc_rx
;
109 /* 0 = sync hunt, != 0 receiving */
111 unsigned int bitstream
;
118 unsigned char buffer
[HDLCDRV_MAXFLEN
+2];
121 struct hdlcdrv_hdlctx
{
122 struct hdlcdrv_hdlcbuffer hbuf
;
123 unsigned long in_hdlc_tx
;
126 * 1 = send txtail (flags)
131 unsigned int bitstream
;
141 unsigned char buffer
[HDLCDRV_MAXFLEN
+2];
145 struct hdlcdrv_bitbuffer bitbuf_channel
;
146 struct hdlcdrv_bitbuffer bitbuf_hdlc
;
147 #endif /* HDLCDRV_DEBUG */
151 /* queued skb for transmission */
156 /* -------------------------------------------------------------------- */
158 static inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer
*hb
)
163 spin_lock_irqsave(&hb
->lock
, flags
);
164 ret
= !((HDLCDRV_HDLCBUFFER
- 1 + hb
->rd
- hb
->wr
) % HDLCDRV_HDLCBUFFER
);
165 spin_unlock_irqrestore(&hb
->lock
, flags
);
169 /* -------------------------------------------------------------------- */
171 static inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer
*hb
)
176 spin_lock_irqsave(&hb
->lock
, flags
);
177 ret
= (hb
->rd
== hb
->wr
);
178 spin_unlock_irqrestore(&hb
->lock
, flags
);
182 /* -------------------------------------------------------------------- */
184 static inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer
*hb
)
190 spin_lock_irqsave(&hb
->lock
, flags
);
191 if (hb
->rd
== hb
->wr
)
194 newr
= (hb
->rd
+1) % HDLCDRV_HDLCBUFFER
;
195 val
= hb
->buf
[hb
->rd
];
198 spin_unlock_irqrestore(&hb
->lock
, flags
);
202 /* -------------------------------------------------------------------- */
204 static inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer
*hb
,
210 spin_lock_irqsave(&hb
->lock
, flags
);
211 newp
= (hb
->wr
+1) % HDLCDRV_HDLCBUFFER
;
212 if (newp
!= hb
->rd
) {
213 hb
->buf
[hb
->wr
] = val
& 0xffff;
216 spin_unlock_irqrestore(&hb
->lock
, flags
);
219 /* -------------------------------------------------------------------- */
221 static inline void hdlcdrv_putbits(struct hdlcdrv_state
*s
, unsigned int bits
)
223 hdlcdrv_hbuf_put(&s
->hdlcrx
.hbuf
, bits
);
226 static inline unsigned int hdlcdrv_getbits(struct hdlcdrv_state
*s
)
230 if (hdlcdrv_hbuf_empty(&s
->hdlctx
.hbuf
)) {
231 if (s
->hdlctx
.calibrate
> 0)
232 s
->hdlctx
.calibrate
--;
237 ret
= hdlcdrv_hbuf_get(&s
->hdlctx
.hbuf
);
238 #ifdef HDLCDRV_LOOPBACK
239 hdlcdrv_hbuf_put(&s
->hdlcrx
.hbuf
, ret
);
240 #endif /* HDLCDRV_LOOPBACK */
244 static inline void hdlcdrv_channelbit(struct hdlcdrv_state
*s
, unsigned int bit
)
247 hdlcdrv_add_bitbuffer(&s
->bitbuf_channel
, bit
);
248 #endif /* HDLCDRV_DEBUG */
251 static inline void hdlcdrv_setdcd(struct hdlcdrv_state
*s
, int dcd
)
253 s
->hdlcrx
.dcd
= !!dcd
;
256 static inline int hdlcdrv_ptt(struct hdlcdrv_state
*s
)
258 return s
->hdlctx
.ptt
|| (s
->hdlctx
.calibrate
> 0);
261 /* -------------------------------------------------------------------- */
263 void hdlcdrv_receiver(struct net_device
*, struct hdlcdrv_state
*);
264 void hdlcdrv_transmitter(struct net_device
*, struct hdlcdrv_state
*);
265 void hdlcdrv_arbitrate(struct net_device
*, struct hdlcdrv_state
*);
266 struct net_device
*hdlcdrv_register(const struct hdlcdrv_ops
*ops
,
267 unsigned int privsize
, const char *ifname
,
268 unsigned int baseaddr
, unsigned int irq
,
270 void hdlcdrv_unregister(struct net_device
*dev
);
272 /* -------------------------------------------------------------------- */
276 #endif /* _HDLCDRV_H */