Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * hdlcdrv.h  -- HDLC packet radio network driver.
0004  * The Linux soundcard driver for 1200 baud and 9600 baud packet radio
0005  * (C) 1996-1998 by Thomas Sailer, HB9JNX/AE4WA
0006  */
0007 #ifndef _HDLCDRV_H
0008 #define _HDLCDRV_H
0009 
0010 
0011 #include <linux/netdevice.h>
0012 #include <linux/if.h>
0013 #include <linux/spinlock.h>
0014 #include <uapi/linux/hdlcdrv.h>
0015 
0016 #define HDLCDRV_MAGIC      0x5ac6e778
0017 #define HDLCDRV_HDLCBUFFER  32 /* should be a power of 2 for speed reasons */
0018 #define HDLCDRV_BITBUFFER  256 /* should be a power of 2 for speed reasons */
0019 #undef HDLCDRV_LOOPBACK  /* define for HDLC debugging purposes */
0020 #define HDLCDRV_DEBUG
0021 
0022 /* maximum packet length, excluding CRC */
0023 #define HDLCDRV_MAXFLEN             400 
0024 
0025 
0026 struct hdlcdrv_hdlcbuffer {
0027     spinlock_t lock;
0028     unsigned rd, wr;
0029     unsigned short buf[HDLCDRV_HDLCBUFFER];
0030 };
0031 
0032 #ifdef HDLCDRV_DEBUG
0033 struct hdlcdrv_bitbuffer {
0034     unsigned int rd;
0035     unsigned int wr;
0036     unsigned int shreg;
0037     unsigned char buffer[HDLCDRV_BITBUFFER];
0038 };
0039 
0040 static inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer *buf, 
0041                      unsigned int bit)
0042 {
0043     unsigned char new;
0044 
0045     new = buf->shreg & 1;
0046     buf->shreg >>= 1;
0047     buf->shreg |= (!!bit) << 7;
0048     if (new) {
0049         buf->buffer[buf->wr] = buf->shreg;
0050         buf->wr = (buf->wr+1) % sizeof(buf->buffer);
0051         buf->shreg = 0x80;
0052     }
0053 }
0054 
0055 static inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer *buf, 
0056                           unsigned int bits)
0057 {
0058     buf->buffer[buf->wr] = bits & 0xff;
0059     buf->wr = (buf->wr+1) % sizeof(buf->buffer);
0060     buf->buffer[buf->wr] = (bits >> 8) & 0xff;
0061     buf->wr = (buf->wr+1) % sizeof(buf->buffer);
0062 
0063 }
0064 #endif /* HDLCDRV_DEBUG */
0065 
0066 /* -------------------------------------------------------------------- */
0067 /*
0068  * Information that need to be kept for each driver. 
0069  */
0070 
0071 struct hdlcdrv_ops {
0072     /*
0073      * first some informations needed by the hdlcdrv routines
0074      */
0075     const char *drvname;
0076     const char *drvinfo;
0077     /*
0078      * the routines called by the hdlcdrv routines
0079      */
0080     int (*open)(struct net_device *);
0081     int (*close)(struct net_device *);
0082     int (*ioctl)(struct net_device *, void __user *,
0083              struct hdlcdrv_ioctl *, int);
0084 };
0085 
0086 struct hdlcdrv_state {
0087     int magic;
0088     int opened;
0089 
0090     const struct hdlcdrv_ops *ops;
0091 
0092     struct {
0093         int bitrate;
0094     } par;
0095 
0096     struct hdlcdrv_pttoutput {
0097         int dma2;
0098         int seriobase;
0099         int pariobase;
0100         int midiiobase;
0101         unsigned int flags;
0102     } ptt_out;
0103 
0104     struct hdlcdrv_channel_params ch_params;
0105 
0106     struct hdlcdrv_hdlcrx {
0107         struct hdlcdrv_hdlcbuffer hbuf;
0108         unsigned long in_hdlc_rx;
0109         /* 0 = sync hunt, != 0 receiving */
0110         int rx_state;   
0111         unsigned int bitstream;
0112         unsigned int bitbuf;
0113         int numbits;
0114         unsigned char dcd;
0115         
0116         int len;
0117         unsigned char *bp;
0118         unsigned char buffer[HDLCDRV_MAXFLEN+2];
0119     } hdlcrx;
0120 
0121     struct hdlcdrv_hdlctx {
0122         struct hdlcdrv_hdlcbuffer hbuf;
0123         unsigned long in_hdlc_tx;
0124         /*
0125          * 0 = send flags
0126          * 1 = send txtail (flags)
0127          * 2 = send packet
0128          */
0129         int tx_state;   
0130         int numflags;
0131         unsigned int bitstream;
0132         unsigned char ptt;
0133         int calibrate;
0134         int slotcnt;
0135 
0136         unsigned int bitbuf;
0137         int numbits;
0138         
0139         int len;
0140         unsigned char *bp;
0141         unsigned char buffer[HDLCDRV_MAXFLEN+2];
0142     } hdlctx;
0143 
0144 #ifdef HDLCDRV_DEBUG
0145     struct hdlcdrv_bitbuffer bitbuf_channel;
0146     struct hdlcdrv_bitbuffer bitbuf_hdlc;
0147 #endif /* HDLCDRV_DEBUG */
0148 
0149     int ptt_keyed;
0150 
0151     /* queued skb for transmission */
0152     struct sk_buff *skb;
0153 };
0154 
0155 
0156 /* -------------------------------------------------------------------- */
0157 
0158 static inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer *hb) 
0159 {
0160     unsigned long flags;
0161     int ret;
0162     
0163     spin_lock_irqsave(&hb->lock, flags);
0164     ret = !((HDLCDRV_HDLCBUFFER - 1 + hb->rd - hb->wr) % HDLCDRV_HDLCBUFFER);
0165     spin_unlock_irqrestore(&hb->lock, flags);
0166     return ret;
0167 }
0168 
0169 /* -------------------------------------------------------------------- */
0170 
0171 static inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer *hb)
0172 {
0173     unsigned long flags;
0174     int ret;
0175     
0176     spin_lock_irqsave(&hb->lock, flags);
0177     ret = (hb->rd == hb->wr);
0178     spin_unlock_irqrestore(&hb->lock, flags);
0179     return ret;
0180 }
0181 
0182 /* -------------------------------------------------------------------- */
0183 
0184 static inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer *hb)
0185 {
0186     unsigned long flags;
0187     unsigned short val;
0188     unsigned newr;
0189 
0190     spin_lock_irqsave(&hb->lock, flags);
0191     if (hb->rd == hb->wr)
0192         val = 0;
0193     else {
0194         newr = (hb->rd+1) % HDLCDRV_HDLCBUFFER;
0195         val = hb->buf[hb->rd];
0196         hb->rd = newr;
0197     }
0198     spin_unlock_irqrestore(&hb->lock, flags);
0199     return val;
0200 }
0201 
0202 /* -------------------------------------------------------------------- */
0203 
0204 static inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer *hb, 
0205                     unsigned short val)
0206 {
0207     unsigned newp;
0208     unsigned long flags;
0209     
0210     spin_lock_irqsave(&hb->lock, flags);
0211     newp = (hb->wr+1) % HDLCDRV_HDLCBUFFER;
0212     if (newp != hb->rd) { 
0213         hb->buf[hb->wr] = val & 0xffff;
0214         hb->wr = newp;
0215     }
0216     spin_unlock_irqrestore(&hb->lock, flags);
0217 }
0218 
0219 /* -------------------------------------------------------------------- */
0220 
0221 static inline void hdlcdrv_putbits(struct hdlcdrv_state *s, unsigned int bits)
0222 {
0223     hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, bits);
0224 }
0225 
0226 static inline unsigned int hdlcdrv_getbits(struct hdlcdrv_state *s)
0227 {
0228     unsigned int ret;
0229 
0230     if (hdlcdrv_hbuf_empty(&s->hdlctx.hbuf)) {
0231         if (s->hdlctx.calibrate > 0)
0232             s->hdlctx.calibrate--;
0233         else
0234             s->hdlctx.ptt = 0;
0235         ret = 0;
0236     } else 
0237         ret = hdlcdrv_hbuf_get(&s->hdlctx.hbuf);
0238 #ifdef HDLCDRV_LOOPBACK
0239     hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, ret);
0240 #endif /* HDLCDRV_LOOPBACK */
0241     return ret;
0242 }
0243 
0244 static inline void hdlcdrv_channelbit(struct hdlcdrv_state *s, unsigned int bit)
0245 {
0246 #ifdef HDLCDRV_DEBUG
0247     hdlcdrv_add_bitbuffer(&s->bitbuf_channel, bit);
0248 #endif /* HDLCDRV_DEBUG */
0249 }
0250 
0251 static inline void hdlcdrv_setdcd(struct hdlcdrv_state *s, int dcd)
0252 {
0253     s->hdlcrx.dcd = !!dcd;
0254 }
0255 
0256 static inline int hdlcdrv_ptt(struct hdlcdrv_state *s)
0257 {
0258     return s->hdlctx.ptt || (s->hdlctx.calibrate > 0);
0259 }
0260 
0261 /* -------------------------------------------------------------------- */
0262 
0263 void hdlcdrv_receiver(struct net_device *, struct hdlcdrv_state *);
0264 void hdlcdrv_transmitter(struct net_device *, struct hdlcdrv_state *);
0265 void hdlcdrv_arbitrate(struct net_device *, struct hdlcdrv_state *);
0266 struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
0267                     unsigned int privsize, const char *ifname,
0268                     unsigned int baseaddr, unsigned int irq, 
0269                     unsigned int dma);
0270 void hdlcdrv_unregister(struct net_device *dev);
0271 
0272 /* -------------------------------------------------------------------- */
0273 
0274 
0275 
0276 #endif /* _HDLCDRV_H */