]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/kernel-video.git/blob - include/linux/hdlcdrv.h
Merge branch 'p-ti-linux-3.8.y' into p-ti-android-3.8.y
[android-sdk/kernel-video.git] / include / linux / hdlcdrv.h
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
10 #include <linux/netdevice.h>
11 #include <linux/if.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 */
19 #define HDLCDRV_DEBUG
21 /* maximum packet length, excluding CRC */
22 #define HDLCDRV_MAXFLEN             400 
25 struct hdlcdrv_hdlcbuffer {
26         spinlock_t lock;
27         unsigned rd, wr;
28         unsigned short buf[HDLCDRV_HDLCBUFFER];
29 };
31 #ifdef HDLCDRV_DEBUG
32 struct hdlcdrv_bitbuffer {
33         unsigned int rd;
34         unsigned int wr;
35         unsigned int shreg;
36         unsigned char buffer[HDLCDRV_BITBUFFER];
37 };
39 static inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer *buf, 
40                                          unsigned int bit)
41 {
42         unsigned char new;
44         new = buf->shreg & 1;
45         buf->shreg >>= 1;
46         buf->shreg |= (!!bit) << 7;
47         if (new) {
48                 buf->buffer[buf->wr] = buf->shreg;
49                 buf->wr = (buf->wr+1) % sizeof(buf->buffer);
50                 buf->shreg = 0x80;
51         }
52 }
54 static inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer *buf, 
55                                               unsigned int bits)
56 {
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);
62 }
63 #endif /* HDLCDRV_DEBUG */
65 /* -------------------------------------------------------------------- */
66 /*
67  * Information that need to be kept for each driver. 
68  */
70 struct hdlcdrv_ops {
71         /*
72          * first some informations needed by the hdlcdrv routines
73          */
74         const char *drvname;
75         const char *drvinfo;
76         /*
77          * the routines called by the hdlcdrv routines
78          */
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);
83 };
85 struct hdlcdrv_state {
86         int magic;
87         int opened;
89         const struct hdlcdrv_ops *ops;
91         struct {
92                 int bitrate;
93         } par;
95         struct hdlcdrv_pttoutput {
96                 int dma2;
97                 int seriobase;
98                 int pariobase;
99                 int midiiobase;
100                 unsigned int flags;
101         } ptt_out;
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 */
109                 int rx_state;   
110                 unsigned int bitstream;
111                 unsigned int bitbuf;
112                 int numbits;
113                 unsigned char dcd;
114                 
115                 int len;
116                 unsigned char *bp;
117                 unsigned char buffer[HDLCDRV_MAXFLEN+2];
118         } hdlcrx;
120         struct hdlcdrv_hdlctx {
121                 struct hdlcdrv_hdlcbuffer hbuf;
122                 unsigned long in_hdlc_tx;
123                 /*
124                  * 0 = send flags
125                  * 1 = send txtail (flags)
126                  * 2 = send packet
127                  */
128                 int tx_state;   
129                 int numflags;
130                 unsigned int bitstream;
131                 unsigned char ptt;
132                 int calibrate;
133                 int slotcnt;
135                 unsigned int bitbuf;
136                 int numbits;
137                 
138                 int len;
139                 unsigned char *bp;
140                 unsigned char buffer[HDLCDRV_MAXFLEN+2];
141         } hdlctx;
143 #ifdef HDLCDRV_DEBUG
144         struct hdlcdrv_bitbuffer bitbuf_channel;
145         struct hdlcdrv_bitbuffer bitbuf_hdlc;
146 #endif /* HDLCDRV_DEBUG */
148         int ptt_keyed;
150         /* queued skb for transmission */
151         struct sk_buff *skb;
152 };
155 /* -------------------------------------------------------------------- */
157 static inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer *hb) 
159         unsigned long flags;
160         int ret;
161         
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);
165         return ret;
168 /* -------------------------------------------------------------------- */
170 static inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer *hb)
172         unsigned long flags;
173         int ret;
174         
175         spin_lock_irqsave(&hb->lock, flags);
176         ret = (hb->rd == hb->wr);
177         spin_unlock_irqrestore(&hb->lock, flags);
178         return ret;
181 /* -------------------------------------------------------------------- */
183 static inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer *hb)
185         unsigned long flags;
186         unsigned short val;
187         unsigned newr;
189         spin_lock_irqsave(&hb->lock, flags);
190         if (hb->rd == hb->wr)
191                 val = 0;
192         else {
193                 newr = (hb->rd+1) % HDLCDRV_HDLCBUFFER;
194                 val = hb->buf[hb->rd];
195                 hb->rd = newr;
196         }
197         spin_unlock_irqrestore(&hb->lock, flags);
198         return val;
201 /* -------------------------------------------------------------------- */
203 static inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer *hb, 
204                                     unsigned short val)
206         unsigned newp;
207         unsigned long flags;
208         
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;
213                 hb->wr = newp;
214         }
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)
227         unsigned int ret;
229         if (hdlcdrv_hbuf_empty(&s->hdlctx.hbuf)) {
230                 if (s->hdlctx.calibrate > 0)
231                         s->hdlctx.calibrate--;
232                 else
233                         s->hdlctx.ptt = 0;
234                 ret = 0;
235         } else 
236                 ret = hdlcdrv_hbuf_get(&s->hdlctx.hbuf);
237 #ifdef HDLCDRV_LOOPBACK
238         hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, ret);
239 #endif /* HDLCDRV_LOOPBACK */
240         return ret;
243 static inline void hdlcdrv_channelbit(struct hdlcdrv_state *s, unsigned int bit)
245 #ifdef HDLCDRV_DEBUG
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, 
268                                     unsigned int dma);
269 void hdlcdrv_unregister(struct net_device *dev);
271 /* -------------------------------------------------------------------- */
275 #endif /* _HDLCDRV_H */