1 /*****************************************************************************/
2 
3 /*
4  *	sm.c  -- soundcard radio modem driver.
5  *
6  *	Copyright (C) 1996-2000  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  *  Command line options (insmod command line)
28  *
29  *  mode     mode string; eg. "wss:afsk1200"
30  *  iobase   base address of the soundcard; common values are 0x220 for sbc,
31  *           0x530 for wss
32  *  irq      interrupt number; common values are 7 or 5 for sbc, 11 for wss
33  *  dma      dma number; common values are 0 or 1
34  *
35  *
36  *  History:
37  *   0.1  21.09.1996  Started
38  *        18.10.1996  Changed to new user space access routines (copy_{to,from}_user)
39  *   0.4  21.01.1997  Separately compileable soundcard/modem modules
40  *   0.5  03.03.1997  fixed LPT probing (check_lpt result was interpreted the wrong way round)
41  *   0.6  16.04.1997  init code/data tagged
42  *   0.7  30.07.1997  fixed halfduplex interrupt handlers/hotfix for CS423X
43  *   0.8  14.04.1998  cleanups
44  *   0.9  03.08.1999  adapt to Linus' new __setup/__initcall
45  *                    use parport lowlevel drivers instead of directly writing to a parallel port
46  *                    removed some pre-2.2 kernel compatibility cruft
47  *   0.10 10.08.1999  Check if parport can do SPP and is safe to access during interrupt contexts
48  *   0.11 12.02.2000  adapted to softnet driver interface
49  *   0.12 03.07.2000  fix interface name handling
50  */
51 
52 /*****************************************************************************/
53 
54 #include <linux/config.h>
55 #include <linux/version.h>
56 #include <linux/module.h>
57 #include <linux/ioport.h>
58 #include <linux/string.h>
59 #include <linux/init.h>
60 #include <linux/parport.h>
61 #include <asm/uaccess.h>
62 #include <asm/io.h>
63 #include "sm.h"
64 
65 /* --------------------------------------------------------------------- */
66 
67 /*static*/ const char sm_drvname[] = "soundmodem";
68 static const char sm_drvinfo[] = KERN_INFO "soundmodem: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
69 KERN_INFO "soundmodem: version 0.12 compiled " __TIME__ " " __DATE__ "\n";
70 
71 /* --------------------------------------------------------------------- */
72 
73 /*static*/ const struct modem_tx_info *sm_modem_tx_table[] = {
74 #ifdef CONFIG_SOUNDMODEM_AFSK1200
75 	&sm_afsk1200_tx,
76 #endif /* CONFIG_SOUNDMODEM_AFSK1200 */
77 #ifdef CONFIG_SOUNDMODEM_AFSK2400_7
78 	&sm_afsk2400_7_tx,
79 #endif /* CONFIG_SOUNDMODEM_AFSK2400_7 */
80 #ifdef CONFIG_SOUNDMODEM_AFSK2400_8
81 	&sm_afsk2400_8_tx,
82 #endif /* CONFIG_SOUNDMODEM_AFSK2400_8 */
83 #ifdef CONFIG_SOUNDMODEM_AFSK2666
84 	&sm_afsk2666_tx,
85 #endif /* CONFIG_SOUNDMODEM_AFSK2666 */
86 #ifdef CONFIG_SOUNDMODEM_PSK4800
87 	&sm_psk4800_tx,
88 #endif /* CONFIG_SOUNDMODEM_PSK4800 */
89 #ifdef CONFIG_SOUNDMODEM_HAPN4800
90 	&sm_hapn4800_8_tx,
91 	&sm_hapn4800_10_tx,
92 	&sm_hapn4800_pm8_tx,
93 	&sm_hapn4800_pm10_tx,
94 #endif /* CONFIG_SOUNDMODEM_HAPN4800 */
95 #ifdef CONFIG_SOUNDMODEM_FSK9600
96 	&sm_fsk9600_4_tx,
97 	&sm_fsk9600_5_tx,
98 #endif /* CONFIG_SOUNDMODEM_FSK9600 */
99 	NULL
100 };
101 
102 /*static*/ const struct modem_rx_info *sm_modem_rx_table[] = {
103 #ifdef CONFIG_SOUNDMODEM_AFSK1200
104 	&sm_afsk1200_rx,
105 #endif /* CONFIG_SOUNDMODEM_AFSK1200 */
106 #ifdef CONFIG_SOUNDMODEM_AFSK2400_7
107 	&sm_afsk2400_7_rx,
108 #endif /* CONFIG_SOUNDMODEM_AFSK2400_7 */
109 #ifdef CONFIG_SOUNDMODEM_AFSK2400_8
110 	&sm_afsk2400_8_rx,
111 #endif /* CONFIG_SOUNDMODEM_AFSK2400_8 */
112 #ifdef CONFIG_SOUNDMODEM_AFSK2666
113 	&sm_afsk2666_rx,
114 #endif /* CONFIG_SOUNDMODEM_AFSK2666 */
115 #ifdef CONFIG_SOUNDMODEM_PSK4800
116 	&sm_psk4800_rx,
117 #endif /* CONFIG_SOUNDMODEM_PSK4800 */
118 #ifdef CONFIG_SOUNDMODEM_HAPN4800
119 	&sm_hapn4800_8_rx,
120 	&sm_hapn4800_10_rx,
121 	&sm_hapn4800_pm8_rx,
122 	&sm_hapn4800_pm10_rx,
123 #endif /* CONFIG_SOUNDMODEM_HAPN4800 */
124 #ifdef CONFIG_SOUNDMODEM_FSK9600
125 	&sm_fsk9600_4_rx,
126 	&sm_fsk9600_5_rx,
127 #endif /* CONFIG_SOUNDMODEM_FSK9600 */
128 	NULL
129 };
130 
131 static const struct hardware_info *sm_hardware_table[] = {
132 #ifdef CONFIG_SOUNDMODEM_SBC
133 	&sm_hw_sbc,
134 	&sm_hw_sbcfdx,
135 #endif /* CONFIG_SOUNDMODEM_SBC */
136 #ifdef CONFIG_SOUNDMODEM_WSS
137 	&sm_hw_wss,
138 	&sm_hw_wssfdx,
139 #endif /* CONFIG_SOUNDMODEM_WSS */
140 	NULL
141 };
142 
143 /* --------------------------------------------------------------------- */
144 
145 #define NR_PORTS 4
146 
147 static struct net_device sm_device[NR_PORTS];
148 
149 /* --------------------------------------------------------------------- */
150 
151 #define UART_RBR(iobase) (iobase+0)
152 #define UART_THR(iobase) (iobase+0)
153 #define UART_IER(iobase) (iobase+1)
154 #define UART_IIR(iobase) (iobase+2)
155 #define UART_FCR(iobase) (iobase+2)
156 #define UART_LCR(iobase) (iobase+3)
157 #define UART_MCR(iobase) (iobase+4)
158 #define UART_LSR(iobase) (iobase+5)
159 #define UART_MSR(iobase) (iobase+6)
160 #define UART_SCR(iobase) (iobase+7)
161 #define UART_DLL(iobase) (iobase+0)
162 #define UART_DLM(iobase) (iobase+1)
163 
164 #define SER_EXTENT 8
165 
166 #define MIDI_DATA(iobase)     (iobase)
167 #define MIDI_STATUS(iobase)   (iobase+1)
168 #define MIDI_READ_FULL 0x80   /* attention: negative logic!! */
169 #define MIDI_WRITE_EMPTY 0x40 /* attention: negative logic!! */
170 
171 #define MIDI_EXTENT 2
172 
173 /* ---------------------------------------------------------------------- */
174 
175 #define PARAM_TXDELAY   1
176 #define PARAM_PERSIST   2
177 #define PARAM_SLOTTIME  3
178 #define PARAM_TXTAIL    4
179 #define PARAM_FULLDUP   5
180 #define PARAM_HARDWARE  6
181 #define PARAM_RETURN    255
182 
183 #define SP_SER  1
184 #define SP_PAR  2
185 #define SP_MIDI 4
186 
187 /*
188  * ===================== port checking routines ========================
189  */
190 
191 enum uart { c_uart_unknown, c_uart_8250,
192 	c_uart_16450, c_uart_16550, c_uart_16550A};
193 static const char *uart_str[] =
194 	{ "unknown", "8250", "16450", "16550", "16550A" };
195 
check_uart(unsigned int iobase)196 static enum uart check_uart(unsigned int iobase)
197 {
198 	unsigned char b1,b2,b3;
199 	enum uart u;
200 	enum uart uart_tab[] =
201 		{ c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
202 
203 	if (iobase <= 0 || iobase > 0x1000-SER_EXTENT)
204 		return c_uart_unknown;
205 	if (check_region(iobase, SER_EXTENT))
206 		return c_uart_unknown;
207 	b1 = inb(UART_MCR(iobase));
208 	outb(b1 | 0x10, UART_MCR(iobase));	/* loopback mode */
209 	b2 = inb(UART_MSR(iobase));
210 	outb(0x1a, UART_MCR(iobase));
211 	b3 = inb(UART_MSR(iobase)) & 0xf0;
212 	outb(b1, UART_MCR(iobase));	   /* restore old values */
213 	outb(b2, UART_MSR(iobase));
214 	if (b3 != 0x90)
215 		return c_uart_unknown;
216 	inb(UART_RBR(iobase));
217 	inb(UART_RBR(iobase));
218 	outb(0x01, UART_FCR(iobase));		/* enable FIFOs */
219 	u = uart_tab[(inb(UART_IIR(iobase)) >> 6) & 3];
220 	if (u == c_uart_16450) {
221 		outb(0x5a, UART_SCR(iobase));
222 		b1 = inb(UART_SCR(iobase));
223 		outb(0xa5, UART_SCR(iobase));
224 		b2 = inb(UART_SCR(iobase));
225 		if ((b1 != 0x5a) || (b2 != 0xa5))
226 			u = c_uart_8250;
227 	}
228 	return u;
229 }
230 
231 /* --------------------------------------------------------------------- */
232 
check_midi(unsigned int iobase)233 static int check_midi(unsigned int iobase)
234 {
235 	unsigned long timeout;
236 	unsigned long flags;
237 	unsigned char b;
238 
239 	if (iobase <= 0 || iobase > 0x1000-MIDI_EXTENT)
240 		return 0;
241 	if (check_region(iobase, MIDI_EXTENT))
242 		return 0;
243 	timeout = jiffies + (HZ / 100);
244 	while (inb(MIDI_STATUS(iobase)) & MIDI_WRITE_EMPTY)
245 		if ((signed)(jiffies - timeout) > 0)
246 			return 0;
247 	save_flags(flags);
248 	cli();
249 	outb(0xff, MIDI_DATA(iobase));
250 	b = inb(MIDI_STATUS(iobase));
251 	restore_flags(flags);
252 	if (!(b & MIDI_WRITE_EMPTY))
253 		return 0;
254 	while (inb(MIDI_STATUS(iobase)) & MIDI_WRITE_EMPTY)
255 		if ((signed)(jiffies - timeout) > 0)
256 			return 0;
257 	return 1;
258 }
259 
260 /* --------------------------------------------------------------------- */
261 
sm_output_status(struct sm_state * sm)262 void sm_output_status(struct sm_state *sm)
263 {
264 	int invert_dcd = 0;
265 	int invert_ptt = 0;
266 
267 	int ptt = /*hdlcdrv_ptt(&sm->hdrv)*/(sm->dma.ptt_cnt > 0) ^ invert_ptt;
268 	int dcd = (!!sm->hdrv.hdlcrx.dcd) ^ invert_dcd;
269 
270 	if (sm->hdrv.ptt_out.flags & SP_SER) {
271 		outb(dcd | (ptt << 1), UART_MCR(sm->hdrv.ptt_out.seriobase));
272 		outb(0x40 & (-ptt), UART_LCR(sm->hdrv.ptt_out.seriobase));
273 	}
274 	if (sm->hdrv.ptt_out.flags & SP_PAR && sm->pardev && sm->pardev->port)
275 		parport_write_data(sm->pardev->port, ptt | (dcd << 1));
276 	if (sm->hdrv.ptt_out.flags & SP_MIDI && hdlcdrv_ptt(&sm->hdrv))
277 		outb(0, MIDI_DATA(sm->hdrv.ptt_out.midiiobase));
278 }
279 
280 /* --------------------------------------------------------------------- */
281 
sm_output_open(struct sm_state * sm,const char * ifname)282 static void sm_output_open(struct sm_state *sm, const char *ifname)
283 {
284 	enum uart u = c_uart_unknown;
285 	struct parport *pp = NULL;
286 
287 	sm->hdrv.ptt_out.flags = 0;
288 	if (sm->hdrv.ptt_out.seriobase > 0 &&
289 	    sm->hdrv.ptt_out.seriobase <= 0x1000-SER_EXTENT &&
290 	    ((u = check_uart(sm->hdrv.ptt_out.seriobase))) != c_uart_unknown) {
291 		sm->hdrv.ptt_out.flags |= SP_SER;
292 		request_region(sm->hdrv.ptt_out.seriobase, SER_EXTENT, "sm ser ptt");
293 		outb(0, UART_IER(sm->hdrv.ptt_out.seriobase));
294 		/* 5 bits, 1 stop, no parity, no break, Div latch access */
295 		outb(0x80, UART_LCR(sm->hdrv.ptt_out.seriobase));
296 		outb(0, UART_DLM(sm->hdrv.ptt_out.seriobase));
297 		outb(1, UART_DLL(sm->hdrv.ptt_out.seriobase)); /* as fast as possible */
298 		/* LCR and MCR set by output_status */
299 	}
300 	sm->pardev = NULL;
301 	if (sm->hdrv.ptt_out.pariobase > 0) {
302 		pp = parport_enumerate();
303 		while (pp && pp->base != sm->hdrv.ptt_out.pariobase)
304 			pp = pp->next;
305 		if (!pp)
306 			printk(KERN_WARNING "%s: parport at address 0x%x not found\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
307 		else if ((~pp->modes) & (PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT))
308 			printk(KERN_WARNING "%s: parport at address 0x%x cannot be used\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
309 		else {
310 			sm->pardev = parport_register_device(pp, ifname, NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
311 			if (!sm->pardev) {
312 				pp = NULL;
313 				printk(KERN_WARNING "%s: cannot register parport device (address 0x%x)\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
314 			} else {
315 				if (parport_claim(sm->pardev)) {
316 					parport_unregister_device(sm->pardev);
317 					sm->pardev = NULL;
318 					printk(KERN_WARNING "%s: cannot claim parport at address 0x%x\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
319 				} else
320 					sm->hdrv.ptt_out.flags |= SP_PAR;
321 			}
322 		}
323 	}
324 	if (sm->hdrv.ptt_out.midiiobase > 0 &&
325 	    sm->hdrv.ptt_out.midiiobase <= 0x1000-MIDI_EXTENT &&
326 	    check_midi(sm->hdrv.ptt_out.midiiobase)) {
327 		sm->hdrv.ptt_out.flags |= SP_MIDI;
328 		request_region(sm->hdrv.ptt_out.midiiobase, MIDI_EXTENT,
329 			       "sm midi ptt");
330 	}
331 	sm_output_status(sm);
332 
333 	printk(KERN_INFO "%s: ptt output:", sm_drvname);
334 	if (sm->hdrv.ptt_out.flags & SP_SER)
335 		printk(" serial interface at 0x%x, uart %s", sm->hdrv.ptt_out.seriobase,
336 		       uart_str[u]);
337 	if (sm->hdrv.ptt_out.flags & SP_PAR)
338 		printk(" parallel interface at 0x%x", sm->hdrv.ptt_out.pariobase);
339 	if (sm->hdrv.ptt_out.flags & SP_MIDI)
340 		printk(" mpu401 (midi) interface at 0x%x", sm->hdrv.ptt_out.midiiobase);
341 	if (!sm->hdrv.ptt_out.flags)
342 		printk(" none");
343 	printk("\n");
344 }
345 
346 /* --------------------------------------------------------------------- */
347 
sm_output_close(struct sm_state * sm)348 static void sm_output_close(struct sm_state *sm)
349 {
350 	/* release regions used for PTT output */
351 	sm->hdrv.hdlctx.ptt = sm->hdrv.hdlctx.calibrate = 0;
352 	sm_output_status(sm);
353 	if (sm->hdrv.ptt_out.flags & SP_SER)
354 		release_region(sm->hdrv.ptt_out.seriobase, SER_EXTENT);
355        	if (sm->hdrv.ptt_out.flags & SP_PAR && sm->pardev) {
356 		        parport_release(sm->pardev);
357 			parport_unregister_device(sm->pardev);
358 	}
359        	if (sm->hdrv.ptt_out.flags & SP_MIDI)
360 		release_region(sm->hdrv.ptt_out.midiiobase, MIDI_EXTENT);
361 	sm->hdrv.ptt_out.flags = 0;
362 }
363 
364 /* --------------------------------------------------------------------- */
365 
366 static int sm_open(struct net_device *dev);
367 static int sm_close(struct net_device *dev);
368 static int sm_ioctl(struct net_device *dev, struct ifreq *ifr,
369 		    struct hdlcdrv_ioctl *hi, int cmd);
370 
371 /* --------------------------------------------------------------------- */
372 
373 static const struct hdlcdrv_ops sm_ops = {
374 	sm_drvname, sm_drvinfo, sm_open, sm_close, sm_ioctl
375 };
376 
377 /* --------------------------------------------------------------------- */
378 
sm_open(struct net_device * dev)379 static int sm_open(struct net_device *dev)
380 {
381 	struct sm_state *sm;
382 	int err;
383 
384 	if (!dev || !dev->priv ||
385 	    ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
386 		printk(KERN_ERR "sm_open: invalid device struct\n");
387 		return -EINVAL;
388 	}
389 	sm = (struct sm_state *)dev->priv;
390 
391 	if (!sm->mode_tx || !sm->mode_rx || !sm->hwdrv || !sm->hwdrv->open)
392 		return -ENODEV;
393 	sm->hdrv.par.bitrate = sm->mode_rx->bitrate;
394 	err = sm->hwdrv->open(dev, sm);
395 	if (err)
396 		return err;
397 	sm_output_open(sm, dev->name);
398 	MOD_INC_USE_COUNT;
399 	printk(KERN_INFO "%s: %s mode %s.%s at iobase 0x%lx irq %u dma %u dma2 %u\n",
400 	       sm_drvname, sm->hwdrv->hw_name, sm->mode_tx->name,
401 	       sm->mode_rx->name, dev->base_addr, dev->irq, dev->dma, sm->hdrv.ptt_out.dma2);
402 	return 0;
403 }
404 
405 /* --------------------------------------------------------------------- */
406 
sm_close(struct net_device * dev)407 static int sm_close(struct net_device *dev)
408 {
409 	struct sm_state *sm;
410 	int err = -ENODEV;
411 
412 	if (!dev || !dev->priv ||
413 	    ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
414 		printk(KERN_ERR "sm_close: invalid device struct\n");
415 		return -EINVAL;
416 	}
417 	sm = (struct sm_state *)dev->priv;
418 
419 
420 	if (sm->hwdrv && sm->hwdrv->close)
421 		err = sm->hwdrv && sm->hwdrv->close(dev, sm);
422 	sm_output_close(sm);
423 	MOD_DEC_USE_COUNT;
424 	printk(KERN_INFO "%s: close %s at iobase 0x%lx irq %u dma %u\n",
425 	       sm_drvname, sm->hwdrv->hw_name, dev->base_addr, dev->irq, dev->dma);
426 	return err;
427 }
428 
429 /* --------------------------------------------------------------------- */
430 
sethw(struct net_device * dev,struct sm_state * sm,char * mode)431 static int sethw(struct net_device *dev, struct sm_state *sm, char *mode)
432 {
433 	char *cp = strchr(mode, ':');
434 	const struct hardware_info **hwp = sm_hardware_table;
435 
436 	if (!cp)
437 		cp = mode;
438 	else {
439 		*cp++ = '\0';
440 		while (hwp && (*hwp) && (*hwp)->hw_name && strcmp((*hwp)->hw_name, mode))
441 			hwp++;
442 		if (!hwp || !*hwp || !(*hwp)->hw_name)
443 			return -EINVAL;
444 		if ((*hwp)->loc_storage > sizeof(sm->hw)) {
445 			printk(KERN_ERR "%s: insufficient storage for hw driver %s (%d)\n",
446 			       sm_drvname, (*hwp)->hw_name, (*hwp)->loc_storage);
447 			return -EINVAL;
448 		}
449 		sm->hwdrv = *hwp;
450 	}
451 	if (!*cp)
452 		return 0;
453 	if (sm->hwdrv && sm->hwdrv->sethw)
454 		return sm->hwdrv->sethw(dev, sm, cp);
455 	return -EINVAL;
456 }
457 
458 /* --------------------------------------------------------------------- */
459 
sm_ioctl(struct net_device * dev,struct ifreq * ifr,struct hdlcdrv_ioctl * hi,int cmd)460 static int sm_ioctl(struct net_device *dev, struct ifreq *ifr,
461 		    struct hdlcdrv_ioctl *hi, int cmd)
462 {
463 	struct sm_state *sm;
464 	struct sm_ioctl bi;
465 	unsigned long flags;
466 	unsigned int newdiagmode;
467 	unsigned int newdiagflags;
468 	char *cp;
469 	const struct modem_tx_info **mtp = sm_modem_tx_table;
470 	const struct modem_rx_info **mrp = sm_modem_rx_table;
471 	const struct hardware_info **hwp = sm_hardware_table;
472 
473 	if (!dev || !dev->priv ||
474 	    ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
475 		printk(KERN_ERR "sm_ioctl: invalid device struct\n");
476 		return -EINVAL;
477 	}
478 	sm = (struct sm_state *)dev->priv;
479 
480 	if (cmd != SIOCDEVPRIVATE) {
481 		if (!sm->hwdrv || !sm->hwdrv->ioctl)
482 			return sm->hwdrv->ioctl(dev, sm, ifr, hi, cmd);
483 		return -ENOIOCTLCMD;
484 	}
485 	switch (hi->cmd) {
486 	default:
487 		if (sm->hwdrv && sm->hwdrv->ioctl)
488 			return sm->hwdrv->ioctl(dev, sm, ifr, hi, cmd);
489 		return -ENOIOCTLCMD;
490 
491 	case HDLCDRVCTL_GETMODE:
492 		cp = hi->data.modename;
493 		if (sm->hwdrv && sm->hwdrv->hw_name)
494 			cp += sprintf(cp, "%s:", sm->hwdrv->hw_name);
495 		else
496 			cp += sprintf(cp, "<unspec>:");
497 		if (sm->mode_tx && sm->mode_tx->name)
498 			cp += sprintf(cp, "%s", sm->mode_tx->name);
499 		else
500 			cp += sprintf(cp, "<unspec>");
501 		if (!sm->mode_rx || !sm->mode_rx ||
502 		    strcmp(sm->mode_rx->name, sm->mode_tx->name)) {
503 			if (sm->mode_rx && sm->mode_rx->name)
504 				cp += sprintf(cp, ",%s", sm->mode_rx->name);
505 			else
506 				cp += sprintf(cp, ",<unspec>");
507 		}
508 		if (copy_to_user(ifr->ifr_data, hi, sizeof(*hi)))
509 			return -EFAULT;
510 		return 0;
511 
512 	case HDLCDRVCTL_SETMODE:
513 		if (netif_running(dev) || !capable(CAP_NET_ADMIN))
514 			return -EACCES;
515 		hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
516 		return sethw(dev, sm, hi->data.modename);
517 
518 	case HDLCDRVCTL_MODELIST:
519 		cp = hi->data.modename;
520 		while (*hwp) {
521 			if ((*hwp)->hw_name)
522 				cp += sprintf(cp, "%s:,", (*hwp)->hw_name);
523 			hwp++;
524 		}
525 		while (*mtp) {
526 			if ((*mtp)->name)
527 				cp += sprintf(cp, ">%s,", (*mtp)->name);
528 			mtp++;
529 		}
530 		while (*mrp) {
531 			if ((*mrp)->name)
532 				cp += sprintf(cp, "<%s,", (*mrp)->name);
533 			mrp++;
534 		}
535 		cp[-1] = '\0';
536 		if (copy_to_user(ifr->ifr_data, hi, sizeof(*hi)))
537 			return -EFAULT;
538 		return 0;
539 
540 #ifdef SM_DEBUG
541 	case SMCTL_GETDEBUG:
542 		if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
543 			return -EFAULT;
544 		bi.data.dbg.int_rate = sm->debug_vals.last_intcnt;
545 		bi.data.dbg.mod_cycles = sm->debug_vals.mod_cyc;
546 		bi.data.dbg.demod_cycles = sm->debug_vals.demod_cyc;
547 		bi.data.dbg.dma_residue = sm->debug_vals.dma_residue;
548 		sm->debug_vals.mod_cyc = sm->debug_vals.demod_cyc =
549 			sm->debug_vals.dma_residue = 0;
550 		if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
551 			return -EFAULT;
552 		return 0;
553 #endif /* SM_DEBUG */
554 
555 	case SMCTL_DIAGNOSE:
556 		if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
557 			return -EFAULT;
558 		newdiagmode = bi.data.diag.mode;
559 		newdiagflags = bi.data.diag.flags;
560 		if (newdiagmode > SM_DIAGMODE_CONSTELLATION)
561 			return -EINVAL;
562 		bi.data.diag.mode = sm->diag.mode;
563 		bi.data.diag.flags = sm->diag.flags;
564 		bi.data.diag.samplesperbit = sm->mode_rx->sperbit;
565 		if (sm->diag.mode != newdiagmode) {
566 			save_flags(flags);
567 			cli();
568 			sm->diag.ptr = -1;
569 			sm->diag.flags = newdiagflags & ~SM_DIAGFLAG_VALID;
570 			sm->diag.mode = newdiagmode;
571 			restore_flags(flags);
572 			if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
573 				return -EFAULT;
574 			return 0;
575 		}
576 		if (sm->diag.ptr < 0 || sm->diag.mode == SM_DIAGMODE_OFF) {
577 			if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
578 				return -EFAULT;
579 			return 0;
580 		}
581 		if (bi.data.diag.datalen > DIAGDATALEN)
582 			bi.data.diag.datalen = DIAGDATALEN;
583 		if (sm->diag.ptr < bi.data.diag.datalen) {
584 			if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
585 				return -EFAULT;
586 			return 0;
587 		}
588 		if (copy_to_user(bi.data.diag.data, sm->diag.data,
589 				 bi.data.diag.datalen * sizeof(short)))
590 			return -EFAULT;
591 		bi.data.diag.flags |= SM_DIAGFLAG_VALID;
592 		save_flags(flags);
593 		cli();
594 		sm->diag.ptr = -1;
595 		sm->diag.flags = newdiagflags & ~SM_DIAGFLAG_VALID;
596 		sm->diag.mode = newdiagmode;
597 		restore_flags(flags);
598 		if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
599 			return -EFAULT;
600 		return 0;
601 	}
602 }
603 
604 /* --------------------------------------------------------------------- */
605 
606 /*
607  * command line settable parameters
608  */
609 static char *mode[NR_PORTS] = { [0 ... NR_PORTS-1] = NULL };
610 static int iobase[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
611 static int irq[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
612 static int dma[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
613 static int dma2[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
614 static int serio[NR_PORTS] = { [0 ... NR_PORTS-1] = 0 };
615 static int pario[NR_PORTS] = { [0 ... NR_PORTS-1] = 0 };
616 static int midiio[NR_PORTS] = { [0 ... NR_PORTS-1] = 0 };
617 
618 MODULE_PARM(mode, "1-" __MODULE_STRING(NR_PORTS) "s");
619 MODULE_PARM_DESC(mode, "soundmodem operating mode; eg. sbc:afsk1200 or wss:fsk9600");
620 MODULE_PARM(iobase, "1-" __MODULE_STRING(NR_PORTS) "i");
621 MODULE_PARM_DESC(iobase, "soundmodem base address");
622 MODULE_PARM(irq, "1-" __MODULE_STRING(NR_PORTS) "i");
623 MODULE_PARM_DESC(irq, "soundmodem interrupt");
624 MODULE_PARM(dma, "1-" __MODULE_STRING(NR_PORTS) "i");
625 MODULE_PARM_DESC(dma, "soundmodem dma channel");
626 MODULE_PARM(dma2, "1-" __MODULE_STRING(NR_PORTS) "i");
627 MODULE_PARM_DESC(dma2, "soundmodem 2nd dma channel; full duplex only");
628 MODULE_PARM(serio, "1-" __MODULE_STRING(NR_PORTS) "i");
629 MODULE_PARM_DESC(serio, "soundmodem PTT output on serial port");
630 MODULE_PARM(pario, "1-" __MODULE_STRING(NR_PORTS) "i");
631 MODULE_PARM_DESC(pario, "soundmodem PTT output on parallel port");
632 MODULE_PARM(midiio, "1-" __MODULE_STRING(NR_PORTS) "i");
633 MODULE_PARM_DESC(midiio, "soundmodem PTT output on midi port");
634 
635 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
636 MODULE_DESCRIPTION("Soundcard amateur radio modem driver");
637 
638 /* --------------------------------------------------------------------- */
639 
init_soundmodem(void)640 static int __init init_soundmodem(void)
641 {
642 	int i, j, found = 0;
643 	char set_hw = 1;
644 	struct sm_state *sm;
645 
646 	printk(sm_drvinfo);
647 	/*
648 	 * register net devices
649 	 */
650 	for (i = 0; i < NR_PORTS; i++) {
651 		struct net_device *dev = sm_device+i;
652 		char ifname[IFNAMSIZ];
653 
654 		sprintf(ifname, "sm%d", i);
655 		if (!mode[i])
656 			set_hw = 0;
657 		else {
658 			if (!strncmp(mode[i], "sbc", 3)) {
659 				if (iobase[i] == -1)
660 					iobase[i] = 0x220;
661 				if (irq[i] == -1)
662 					irq[i] = 5;
663 				if (dma[i] == -1)
664 					dma[i] = 1;
665 			} else {
666 				if (iobase[i] == -1)
667 					iobase[i] = 0x530;
668 				if (irq[i] == -1)
669 					irq[i] = 11;
670 				if (dma[i] == -1)
671 					dma[i] = 1;
672 			}
673 		}
674 		if (!set_hw)
675 			iobase[i] = irq[i] = 0;
676 		j = hdlcdrv_register_hdlcdrv(dev, &sm_ops, sizeof(struct sm_state), ifname, iobase[i], irq[i], dma[i]);
677 		if (!j) {
678 			sm = (struct sm_state *)dev->priv;
679 			sm->hdrv.ptt_out.dma2 = dma2[i];
680 			sm->hdrv.ptt_out.seriobase = serio[i];
681 			sm->hdrv.ptt_out.pariobase = pario[i];
682 			sm->hdrv.ptt_out.midiiobase = midiio[i];
683 			if (set_hw && sethw(dev, sm, mode[i]))
684 				set_hw = 0;
685 			found++;
686 		} else {
687 			printk(KERN_WARNING "%s: cannot register net device\n", sm_drvname);
688 		}
689 	}
690 	if (!found)
691 		return -ENXIO;
692 	return 0;
693 }
694 
cleanup_soundmodem(void)695 static void __exit cleanup_soundmodem(void)
696 {
697 	int i;
698 
699 	printk(KERN_INFO "sm: cleanup_module called\n");
700 
701 	for(i = 0; i < NR_PORTS; i++) {
702 		struct net_device *dev = sm_device+i;
703 		struct sm_state *sm = (struct sm_state *)dev->priv;
704 
705 		if (sm) {
706 			if (sm->hdrv.magic != HDLCDRV_MAGIC)
707 				printk(KERN_ERR "sm: invalid magic in "
708 				       "cleanup_module\n");
709 			else
710 				hdlcdrv_unregister_hdlcdrv(dev);
711 		}
712 	}
713 }
714 
715 module_init(init_soundmodem);
716 module_exit(cleanup_soundmodem);
717 
718 /* --------------------------------------------------------------------- */
719 
720 #ifndef MODULE
721 
722 /*
723  * format: soundmodem=io,irq,dma[,dma2[,serio[,pario]]],mode
724  * mode: hw:modem
725  * hw: sbc, wss, wssfdx
726  * modem: afsk1200, fsk9600
727  */
728 
sm_setup(char * str)729 static int __init sm_setup(char *str)
730 {
731 	static unsigned nr_dev;
732 	int ints[8];
733 
734 	if (nr_dev >= NR_PORTS)
735 		return 0;
736 	str = get_options(str, 8, ints);
737 	mode[nr_dev] = str;
738 	if (ints[0] >= 1)
739 		iobase[nr_dev] = ints[1];
740 	if (ints[0] >= 2)
741 		irq[nr_dev] = ints[2];
742 	if (ints[0] >= 3)
743 		dma[nr_dev] = ints[3];
744 	if (ints[0] >= 4)
745 		dma2[nr_dev] = ints[4];
746 	if (ints[0] >= 5)
747 		serio[nr_dev] = ints[5];
748 	if (ints[0] >= 6)
749 		pario[nr_dev] = ints[6];
750 	if (ints[0] >= 7)
751 		midiio[nr_dev] = ints[7];
752 	nr_dev++;
753 	return 1;
754 }
755 
756 __setup("soundmodem=", sm_setup);
757 
758 #endif /* MODULE */
759 /* --------------------------------------------------------------------- */
760