1 /*****************************************************************************/
2 
3 /*
4  *	sm.h  --  soundcard radio modem driver internal header.
5  *
6  *	Copyright (C) 1996-1999  Thomas Sailer (sailer@ife.ee.ethz.ch)
7  *
8  *	This program is free software; you can redistribute it and/or modify
9  *	it under the terms of the GNU General Public License as published by
10  *	the Free Software Foundation; either version 2 of the License, or
11  *	(at your option) any later version.
12  *
13  *	This program is distributed in the hope that it will be useful,
14  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *	GNU General Public License for more details.
17  *
18  *	You should have received a copy of the GNU General Public License
19  *	along with this program; if not, write to the Free Software
20  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *  Please note that the GPL allows you to use the driver, NOT the radio.
23  *  In order to use the radio, you need a license from the communications
24  *  authority of your country.
25  *
26  */
27 
28 #ifndef _SM_H
29 #define _SM_H
30 
31 /* ---------------------------------------------------------------------- */
32 
33 #include <linux/hdlcdrv.h>
34 #include <linux/soundmodem.h>
35 #include <asm/processor.h>
36 #include <linux/bitops.h>
37 #include <linux/parport.h>
38 
39 #define SM_DEBUG
40 
41 /* ---------------------------------------------------------------------- */
42 /*
43  * Information that need to be kept for each board.
44  */
45 
46 struct sm_state {
47 	struct hdlcdrv_state hdrv;
48 
49 	const struct modem_tx_info *mode_tx;
50 	const struct modem_rx_info *mode_rx;
51 
52 	const struct hardware_info *hwdrv;
53 
54 	struct pardevice *pardev;
55 
56 	/*
57 	 * Hardware (soundcard) access routines state
58 	 */
59 	struct {
60 		void *ibuf;
61 		unsigned int ifragsz;
62 		unsigned int ifragptr;
63 		unsigned int i16bit;
64 		void *obuf;
65 		unsigned int ofragsz;
66 		unsigned int ofragptr;
67 		unsigned int o16bit;
68 		int ptt_cnt;
69 	} dma;
70 
71 	union {
72 		long hw[32/sizeof(long)];
73 	} hw;
74 
75 	/*
76 	 * state of the modem code
77 	 */
78 	union {
79 		long m[48/sizeof(long)];
80 	} m;
81 	union {
82 		long d[256/sizeof(long)];
83 	} d;
84 
85 #define DIAGDATALEN 64
86 	struct diag_data {
87 		unsigned int mode;
88 		unsigned int flags;
89 		volatile int ptr;
90 		short data[DIAGDATALEN];
91 	} diag;
92 
93 
94 #ifdef SM_DEBUG
95 	struct debug_vals {
96 		unsigned long last_jiffies;
97 		unsigned cur_intcnt;
98 		unsigned last_intcnt;
99 		unsigned mod_cyc;
100 		unsigned demod_cyc;
101 		unsigned dma_residue;
102 	} debug_vals;
103 #endif /* SM_DEBUG */
104 };
105 
106 /* ---------------------------------------------------------------------- */
107 /*
108  * Mode definition structure
109  */
110 
111 struct modem_tx_info {
112 	const char *name;
113 	unsigned int loc_storage;
114 	int srate;
115 	int bitrate;
116         void (*modulator_u8)(struct sm_state *, unsigned char *, unsigned int);
117         void (*modulator_s16)(struct sm_state *, short *, unsigned int);
118         void (*init)(struct sm_state *);
119 };
120 
121 struct modem_rx_info {
122 	const char *name;
123 	unsigned int loc_storage;
124 	int srate;
125 	int bitrate;
126 	unsigned int overlap;
127 	unsigned int sperbit;
128         void (*demodulator_u8)(struct sm_state *, const unsigned char *, unsigned int);
129         void (*demodulator_s16)(struct sm_state *, const short *, unsigned int);
130         void (*init)(struct sm_state *);
131 };
132 
133 /* ---------------------------------------------------------------------- */
134 /*
135  * Soundcard driver definition structure
136  */
137 
138 struct hardware_info {
139 	char *hw_name; /* used for request_{region,irq,dma} */
140 	unsigned int loc_storage;
141 	/*
142 	 * mode specific open/close
143 	 */
144 	int (*open)(struct net_device *, struct sm_state *);
145 	int (*close)(struct net_device *, struct sm_state *);
146 	int (*ioctl)(struct net_device *, struct sm_state *, struct ifreq *,
147 		     struct hdlcdrv_ioctl *, int);
148 	int (*sethw)(struct net_device *, struct sm_state *, char *);
149 };
150 
151 /* --------------------------------------------------------------------- */
152 
153 extern const char sm_drvname[];
154 
155 /* --------------------------------------------------------------------- */
156 /*
157  * ===================== diagnostics stuff ===============================
158  */
159 
diag_trigger(struct sm_state * sm)160 static inline void diag_trigger(struct sm_state *sm)
161 {
162 	if (sm->diag.ptr < 0)
163 		if (!(sm->diag.flags & SM_DIAGFLAG_DCDGATE) || sm->hdrv.hdlcrx.dcd)
164 			sm->diag.ptr = 0;
165 }
166 
167 /* --------------------------------------------------------------------- */
168 
169 #define SHRT_MAX ((short)(((unsigned short)(~0U))>>1))
170 #define SHRT_MIN (-SHRT_MAX-1)
171 
diag_add(struct sm_state * sm,int valinp,int valdemod)172 static inline void diag_add(struct sm_state *sm, int valinp, int valdemod)
173 {
174 	int val;
175 
176 	if ((sm->diag.mode != SM_DIAGMODE_INPUT &&
177 	     sm->diag.mode != SM_DIAGMODE_DEMOD) ||
178 	    sm->diag.ptr >= DIAGDATALEN || sm->diag.ptr < 0)
179 		return;
180 	val = (sm->diag.mode == SM_DIAGMODE_DEMOD) ? valdemod : valinp;
181 	/* clip */
182 	if (val > SHRT_MAX)
183 		val = SHRT_MAX;
184 	if (val < SHRT_MIN)
185 		val = SHRT_MIN;
186 	sm->diag.data[sm->diag.ptr++] = val;
187 }
188 
189 /* --------------------------------------------------------------------- */
190 
diag_add_one(struct sm_state * sm,int val)191 static inline void diag_add_one(struct sm_state *sm, int val)
192 {
193 	if ((sm->diag.mode != SM_DIAGMODE_INPUT &&
194 	     sm->diag.mode != SM_DIAGMODE_DEMOD) ||
195 	    sm->diag.ptr >= DIAGDATALEN || sm->diag.ptr < 0)
196 		return;
197 	/* clip */
198 	if (val > SHRT_MAX)
199 		val = SHRT_MAX;
200 	if (val < SHRT_MIN)
201 		val = SHRT_MIN;
202 	sm->diag.data[sm->diag.ptr++] = val;
203 }
204 
205 /* --------------------------------------------------------------------- */
206 
diag_add_constellation(struct sm_state * sm,int vali,int valq)207 static inline void diag_add_constellation(struct sm_state *sm, int vali, int valq)
208 {
209 	if ((sm->diag.mode != SM_DIAGMODE_CONSTELLATION) ||
210 	    sm->diag.ptr >= DIAGDATALEN-1 || sm->diag.ptr < 0)
211 		return;
212 	/* clip */
213 	if (vali > SHRT_MAX)
214 		vali = SHRT_MAX;
215 	if (vali < SHRT_MIN)
216 		vali = SHRT_MIN;
217 	if (valq > SHRT_MAX)
218 		valq = SHRT_MAX;
219 	if (valq < SHRT_MIN)
220 		valq = SHRT_MIN;
221 	sm->diag.data[sm->diag.ptr++] = vali;
222 	sm->diag.data[sm->diag.ptr++] = valq;
223 }
224 
225 /* --------------------------------------------------------------------- */
226 /*
227  * ===================== utility functions ===============================
228  */
229 
230 #if 0
231 static inline unsigned int hweight32(unsigned int w)
232 	__attribute__ ((unused));
233 static inline unsigned int hweight16(unsigned short w)
234 	__attribute__ ((unused));
235 static inline unsigned int hweight8(unsigned char w)
236         __attribute__ ((unused));
237 
238 static inline unsigned int hweight32(unsigned int w)
239 {
240         unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
241         res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
242         res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
243         res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
244         return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
245 }
246 
247 static inline unsigned int hweight16(unsigned short w)
248 {
249         unsigned short res = (w & 0x5555) + ((w >> 1) & 0x5555);
250         res = (res & 0x3333) + ((res >> 2) & 0x3333);
251         res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
252         return (res & 0x00FF) + ((res >> 8) & 0x00FF);
253 }
254 
255 static inline unsigned int hweight8(unsigned char w)
256 {
257         unsigned short res = (w & 0x55) + ((w >> 1) & 0x55);
258         res = (res & 0x33) + ((res >> 2) & 0x33);
259         return (res & 0x0F) + ((res >> 4) & 0x0F);
260 }
261 
262 #endif
263 
264 static inline unsigned int gcd(unsigned int x, unsigned int y)
265 	__attribute__ ((unused));
266 static inline unsigned int lcm(unsigned int x, unsigned int y)
267 	__attribute__ ((unused));
268 
gcd(unsigned int x,unsigned int y)269 static inline unsigned int gcd(unsigned int x, unsigned int y)
270 {
271 	for (;;) {
272 		if (!x)
273 			return y;
274 		if (!y)
275 			return x;
276 		if (x > y)
277 			x %= y;
278 		else
279 			y %= x;
280 	}
281 }
282 
lcm(unsigned int x,unsigned int y)283 static inline unsigned int lcm(unsigned int x, unsigned int y)
284 {
285 	return x * y / gcd(x, y);
286 }
287 
288 /* --------------------------------------------------------------------- */
289 /*
290  * ===================== profiling =======================================
291  */
292 
293 
294 #ifdef __i386__
295 
296 #include <asm/msr.h>
297 
298 /*
299  * only do 32bit cycle counter arithmetic; we hope we won't overflow.
300  * in fact, overflowing modems would require over 2THz CPU clock speeds :-)
301  */
302 
303 #define time_exec(var,cmd)                                              \
304 ({                                                                      \
305 	if (cpu_has_tsc) {                                              \
306 		unsigned int cnt1, cnt2;                                \
307 		rdtscl(cnt1);                                           \
308 		cmd;                                                    \
309 		rdtscl(cnt2);                                           \
310 		var = cnt2-cnt1;                                        \
311 	} else {                                                        \
312 		cmd;                                                    \
313 	}                                                               \
314 })
315 
316 #else /* __i386__ */
317 
318 #define time_exec(var,cmd) cmd
319 
320 #endif /* __i386__ */
321 
322 /* --------------------------------------------------------------------- */
323 
324 extern const struct modem_tx_info sm_afsk1200_tx;
325 extern const struct modem_tx_info sm_afsk2400_7_tx;
326 extern const struct modem_tx_info sm_afsk2400_8_tx;
327 extern const struct modem_tx_info sm_afsk2666_tx;
328 extern const struct modem_tx_info sm_psk4800_tx;
329 extern const struct modem_tx_info sm_hapn4800_8_tx;
330 extern const struct modem_tx_info sm_hapn4800_10_tx;
331 extern const struct modem_tx_info sm_hapn4800_pm8_tx;
332 extern const struct modem_tx_info sm_hapn4800_pm10_tx;
333 extern const struct modem_tx_info sm_fsk9600_4_tx;
334 extern const struct modem_tx_info sm_fsk9600_5_tx;
335 
336 extern const struct modem_rx_info sm_afsk1200_rx;
337 extern const struct modem_rx_info sm_afsk2400_7_rx;
338 extern const struct modem_rx_info sm_afsk2400_8_rx;
339 extern const struct modem_rx_info sm_afsk2666_rx;
340 extern const struct modem_rx_info sm_psk4800_rx;
341 extern const struct modem_rx_info sm_hapn4800_8_rx;
342 extern const struct modem_rx_info sm_hapn4800_10_rx;
343 extern const struct modem_rx_info sm_hapn4800_pm8_rx;
344 extern const struct modem_rx_info sm_hapn4800_pm10_rx;
345 extern const struct modem_rx_info sm_fsk9600_4_rx;
346 extern const struct modem_rx_info sm_fsk9600_5_rx;
347 
348 extern const struct hardware_info sm_hw_sbc;
349 extern const struct hardware_info sm_hw_sbcfdx;
350 extern const struct hardware_info sm_hw_wss;
351 extern const struct hardware_info sm_hw_wssfdx;
352 
353 extern const struct modem_tx_info *sm_modem_tx_table[];
354 extern const struct modem_rx_info *sm_modem_rx_table[];
355 
356 /* --------------------------------------------------------------------- */
357 
358 void sm_output_status(struct sm_state *sm);
359 /*void sm_output_open(struct sm_state *sm);*/
360 /*void sm_output_close(struct sm_state *sm);*/
361 
362 /* --------------------------------------------------------------------- */
363 
sm_int_freq(struct sm_state * sm)364 extern void inline sm_int_freq(struct sm_state *sm)
365 {
366 #ifdef SM_DEBUG
367 	unsigned long cur_jiffies = jiffies;
368 	/*
369 	 * measure the interrupt frequency
370 	 */
371 	sm->debug_vals.cur_intcnt++;
372 	if ((cur_jiffies - sm->debug_vals.last_jiffies) >= HZ) {
373 		sm->debug_vals.last_jiffies = cur_jiffies;
374 		sm->debug_vals.last_intcnt = sm->debug_vals.cur_intcnt;
375 		sm->debug_vals.cur_intcnt = 0;
376 	}
377 #endif /* SM_DEBUG */
378 }
379 
380 /* --------------------------------------------------------------------- */
381 #endif /* _SM_H */
382