Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / drivers / net / hamradio / soundmodem / sm.c
blobaf04a2c877d7d9b5b70c2c3e84c5ea8ad0371875
1 /*****************************************************************************/
3 /*
4 * sm.c -- soundcard radio modem driver.
6 * Copyright (C) 1996-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
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.
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.
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.
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.
27 * Command line options (insmod command line)
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
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
51 /*****************************************************************************/
53 #include <linux/config.h>
54 #include <linux/version.h>
55 #include <linux/module.h>
56 #include <linux/ioport.h>
57 #include <linux/string.h>
58 #include <linux/init.h>
59 #include <linux/parport.h>
60 #include <asm/uaccess.h>
61 #include <asm/io.h>
62 #include "sm.h"
64 /* --------------------------------------------------------------------- */
66 /*static*/ const char sm_drvname[] = "soundmodem";
67 static const char sm_drvinfo[] = KERN_INFO "soundmodem: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
68 KERN_INFO "soundmodem: version 0.11 compiled " __TIME__ " " __DATE__ "\n";
70 /* --------------------------------------------------------------------- */
72 /*static*/ const struct modem_tx_info *sm_modem_tx_table[] = {
73 #ifdef CONFIG_SOUNDMODEM_AFSK1200
74 &sm_afsk1200_tx,
75 #endif /* CONFIG_SOUNDMODEM_AFSK1200 */
76 #ifdef CONFIG_SOUNDMODEM_AFSK2400_7
77 &sm_afsk2400_7_tx,
78 #endif /* CONFIG_SOUNDMODEM_AFSK2400_7 */
79 #ifdef CONFIG_SOUNDMODEM_AFSK2400_8
80 &sm_afsk2400_8_tx,
81 #endif /* CONFIG_SOUNDMODEM_AFSK2400_8 */
82 #ifdef CONFIG_SOUNDMODEM_AFSK2666
83 &sm_afsk2666_tx,
84 #endif /* CONFIG_SOUNDMODEM_AFSK2666 */
85 #ifdef CONFIG_SOUNDMODEM_PSK4800
86 &sm_psk4800_tx,
87 #endif /* CONFIG_SOUNDMODEM_PSK4800 */
88 #ifdef CONFIG_SOUNDMODEM_HAPN4800
89 &sm_hapn4800_8_tx,
90 &sm_hapn4800_10_tx,
91 &sm_hapn4800_pm8_tx,
92 &sm_hapn4800_pm10_tx,
93 #endif /* CONFIG_SOUNDMODEM_HAPN4800 */
94 #ifdef CONFIG_SOUNDMODEM_FSK9600
95 &sm_fsk9600_4_tx,
96 &sm_fsk9600_5_tx,
97 #endif /* CONFIG_SOUNDMODEM_FSK9600 */
98 NULL
101 /*static*/ const struct modem_rx_info *sm_modem_rx_table[] = {
102 #ifdef CONFIG_SOUNDMODEM_AFSK1200
103 &sm_afsk1200_rx,
104 #endif /* CONFIG_SOUNDMODEM_AFSK1200 */
105 #ifdef CONFIG_SOUNDMODEM_AFSK2400_7
106 &sm_afsk2400_7_rx,
107 #endif /* CONFIG_SOUNDMODEM_AFSK2400_7 */
108 #ifdef CONFIG_SOUNDMODEM_AFSK2400_8
109 &sm_afsk2400_8_rx,
110 #endif /* CONFIG_SOUNDMODEM_AFSK2400_8 */
111 #ifdef CONFIG_SOUNDMODEM_AFSK2666
112 &sm_afsk2666_rx,
113 #endif /* CONFIG_SOUNDMODEM_AFSK2666 */
114 #ifdef CONFIG_SOUNDMODEM_PSK4800
115 &sm_psk4800_rx,
116 #endif /* CONFIG_SOUNDMODEM_PSK4800 */
117 #ifdef CONFIG_SOUNDMODEM_HAPN4800
118 &sm_hapn4800_8_rx,
119 &sm_hapn4800_10_rx,
120 &sm_hapn4800_pm8_rx,
121 &sm_hapn4800_pm10_rx,
122 #endif /* CONFIG_SOUNDMODEM_HAPN4800 */
123 #ifdef CONFIG_SOUNDMODEM_FSK9600
124 &sm_fsk9600_4_rx,
125 &sm_fsk9600_5_rx,
126 #endif /* CONFIG_SOUNDMODEM_FSK9600 */
127 NULL
130 static const struct hardware_info *sm_hardware_table[] = {
131 #ifdef CONFIG_SOUNDMODEM_SBC
132 &sm_hw_sbc,
133 &sm_hw_sbcfdx,
134 #endif /* CONFIG_SOUNDMODEM_SBC */
135 #ifdef CONFIG_SOUNDMODEM_WSS
136 &sm_hw_wss,
137 &sm_hw_wssfdx,
138 #endif /* CONFIG_SOUNDMODEM_WSS */
139 NULL
142 /* --------------------------------------------------------------------- */
144 #define NR_PORTS 4
146 static struct net_device sm_device[NR_PORTS];
148 /* --------------------------------------------------------------------- */
150 #define UART_RBR(iobase) (iobase+0)
151 #define UART_THR(iobase) (iobase+0)
152 #define UART_IER(iobase) (iobase+1)
153 #define UART_IIR(iobase) (iobase+2)
154 #define UART_FCR(iobase) (iobase+2)
155 #define UART_LCR(iobase) (iobase+3)
156 #define UART_MCR(iobase) (iobase+4)
157 #define UART_LSR(iobase) (iobase+5)
158 #define UART_MSR(iobase) (iobase+6)
159 #define UART_SCR(iobase) (iobase+7)
160 #define UART_DLL(iobase) (iobase+0)
161 #define UART_DLM(iobase) (iobase+1)
163 #define SER_EXTENT 8
165 #define MIDI_DATA(iobase) (iobase)
166 #define MIDI_STATUS(iobase) (iobase+1)
167 #define MIDI_READ_FULL 0x80 /* attention: negative logic!! */
168 #define MIDI_WRITE_EMPTY 0x40 /* attention: negative logic!! */
170 #define MIDI_EXTENT 2
172 /* ---------------------------------------------------------------------- */
174 #define PARAM_TXDELAY 1
175 #define PARAM_PERSIST 2
176 #define PARAM_SLOTTIME 3
177 #define PARAM_TXTAIL 4
178 #define PARAM_FULLDUP 5
179 #define PARAM_HARDWARE 6
180 #define PARAM_RETURN 255
182 #define SP_SER 1
183 #define SP_PAR 2
184 #define SP_MIDI 4
187 * ===================== port checking routines ========================
190 enum uart { c_uart_unknown, c_uart_8250,
191 c_uart_16450, c_uart_16550, c_uart_16550A};
192 static const char *uart_str[] =
193 { "unknown", "8250", "16450", "16550", "16550A" };
195 static enum uart check_uart(unsigned int iobase)
197 unsigned char b1,b2,b3;
198 enum uart u;
199 enum uart uart_tab[] =
200 { c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
202 if (iobase <= 0 || iobase > 0x1000-SER_EXTENT)
203 return c_uart_unknown;
204 if (check_region(iobase, SER_EXTENT))
205 return c_uart_unknown;
206 b1 = inb(UART_MCR(iobase));
207 outb(b1 | 0x10, UART_MCR(iobase)); /* loopback mode */
208 b2 = inb(UART_MSR(iobase));
209 outb(0x1a, UART_MCR(iobase));
210 b3 = inb(UART_MSR(iobase)) & 0xf0;
211 outb(b1, UART_MCR(iobase)); /* restore old values */
212 outb(b2, UART_MSR(iobase));
213 if (b3 != 0x90)
214 return c_uart_unknown;
215 inb(UART_RBR(iobase));
216 inb(UART_RBR(iobase));
217 outb(0x01, UART_FCR(iobase)); /* enable FIFOs */
218 u = uart_tab[(inb(UART_IIR(iobase)) >> 6) & 3];
219 if (u == c_uart_16450) {
220 outb(0x5a, UART_SCR(iobase));
221 b1 = inb(UART_SCR(iobase));
222 outb(0xa5, UART_SCR(iobase));
223 b2 = inb(UART_SCR(iobase));
224 if ((b1 != 0x5a) || (b2 != 0xa5))
225 u = c_uart_8250;
227 return u;
230 /* --------------------------------------------------------------------- */
232 static int check_midi(unsigned int iobase)
234 unsigned long timeout;
235 unsigned long flags;
236 unsigned char b;
238 if (iobase <= 0 || iobase > 0x1000-MIDI_EXTENT)
239 return 0;
240 if (check_region(iobase, MIDI_EXTENT))
241 return 0;
242 timeout = jiffies + (HZ / 100);
243 while (inb(MIDI_STATUS(iobase)) & MIDI_WRITE_EMPTY)
244 if ((signed)(jiffies - timeout) > 0)
245 return 0;
246 save_flags(flags);
247 cli();
248 outb(0xff, MIDI_DATA(iobase));
249 b = inb(MIDI_STATUS(iobase));
250 restore_flags(flags);
251 if (!(b & MIDI_WRITE_EMPTY))
252 return 0;
253 while (inb(MIDI_STATUS(iobase)) & MIDI_WRITE_EMPTY)
254 if ((signed)(jiffies - timeout) > 0)
255 return 0;
256 return 1;
259 /* --------------------------------------------------------------------- */
261 void sm_output_status(struct sm_state *sm)
263 int invert_dcd = 0;
264 int invert_ptt = 0;
266 int ptt = /*hdlcdrv_ptt(&sm->hdrv)*/(sm->dma.ptt_cnt > 0) ^ invert_ptt;
267 int dcd = (!!sm->hdrv.hdlcrx.dcd) ^ invert_dcd;
269 if (sm->hdrv.ptt_out.flags & SP_SER) {
270 outb(dcd | (ptt << 1), UART_MCR(sm->hdrv.ptt_out.seriobase));
271 outb(0x40 & (-ptt), UART_LCR(sm->hdrv.ptt_out.seriobase));
273 if (sm->hdrv.ptt_out.flags & SP_PAR && sm->pardev && sm->pardev->port)
274 parport_write_data(sm->pardev->port, ptt | (dcd << 1));
275 if (sm->hdrv.ptt_out.flags & SP_MIDI && hdlcdrv_ptt(&sm->hdrv))
276 outb(0, MIDI_DATA(sm->hdrv.ptt_out.midiiobase));
279 /* --------------------------------------------------------------------- */
281 static void sm_output_open(struct sm_state *sm)
283 enum uart u = c_uart_unknown;
284 struct parport *pp = NULL;
286 sm->hdrv.ptt_out.flags = 0;
287 if (sm->hdrv.ptt_out.seriobase > 0 &&
288 sm->hdrv.ptt_out.seriobase <= 0x1000-SER_EXTENT &&
289 ((u = check_uart(sm->hdrv.ptt_out.seriobase))) != c_uart_unknown) {
290 sm->hdrv.ptt_out.flags |= SP_SER;
291 request_region(sm->hdrv.ptt_out.seriobase, SER_EXTENT, "sm ser ptt");
292 outb(0, UART_IER(sm->hdrv.ptt_out.seriobase));
293 /* 5 bits, 1 stop, no parity, no break, Div latch access */
294 outb(0x80, UART_LCR(sm->hdrv.ptt_out.seriobase));
295 outb(0, UART_DLM(sm->hdrv.ptt_out.seriobase));
296 outb(1, UART_DLL(sm->hdrv.ptt_out.seriobase)); /* as fast as possible */
297 /* LCR and MCR set by output_status */
299 sm->pardev = NULL;
300 if (sm->hdrv.ptt_out.pariobase > 0) {
301 pp = parport_enumerate();
302 while (pp && pp->base != sm->hdrv.ptt_out.pariobase)
303 pp = pp->next;
304 if (!pp)
305 printk(KERN_WARNING "%s: parport at address 0x%x not found\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
306 else if ((~pp->modes) & (PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT))
307 printk(KERN_WARNING "%s: parport at address 0x%x cannot be used\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
308 else {
309 sm->pardev = parport_register_device(pp, sm->hdrv.ifname, NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
310 if (!sm->pardev) {
311 pp = NULL;
312 printk(KERN_WARNING "%s: cannot register parport device (address 0x%x)\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
313 } else {
314 if (parport_claim(sm->pardev)) {
315 parport_unregister_device(sm->pardev);
316 sm->pardev = NULL;
317 printk(KERN_WARNING "%s: cannot claim parport at address 0x%x\n", sm_drvname, sm->hdrv.ptt_out.pariobase);
318 } else
319 sm->hdrv.ptt_out.flags |= SP_PAR;
323 if (sm->hdrv.ptt_out.midiiobase > 0 &&
324 sm->hdrv.ptt_out.midiiobase <= 0x1000-MIDI_EXTENT &&
325 check_midi(sm->hdrv.ptt_out.midiiobase)) {
326 sm->hdrv.ptt_out.flags |= SP_MIDI;
327 request_region(sm->hdrv.ptt_out.midiiobase, MIDI_EXTENT,
328 "sm midi ptt");
330 sm_output_status(sm);
332 printk(KERN_INFO "%s: ptt output:", sm_drvname);
333 if (sm->hdrv.ptt_out.flags & SP_SER)
334 printk(" serial interface at 0x%x, uart %s", sm->hdrv.ptt_out.seriobase,
335 uart_str[u]);
336 if (sm->hdrv.ptt_out.flags & SP_PAR)
337 printk(" parallel interface at 0x%x", sm->hdrv.ptt_out.pariobase);
338 if (sm->hdrv.ptt_out.flags & SP_MIDI)
339 printk(" mpu401 (midi) interface at 0x%x", sm->hdrv.ptt_out.midiiobase);
340 if (!sm->hdrv.ptt_out.flags)
341 printk(" none");
342 printk("\n");
345 /* --------------------------------------------------------------------- */
347 static void sm_output_close(struct sm_state *sm)
349 /* release regions used for PTT output */
350 sm->hdrv.hdlctx.ptt = sm->hdrv.hdlctx.calibrate = 0;
351 sm_output_status(sm);
352 if (sm->hdrv.ptt_out.flags & SP_SER)
353 release_region(sm->hdrv.ptt_out.seriobase, SER_EXTENT);
354 if (sm->hdrv.ptt_out.flags & SP_PAR && sm->pardev) {
355 parport_release(sm->pardev);
356 parport_unregister_device(sm->pardev);
358 if (sm->hdrv.ptt_out.flags & SP_MIDI)
359 release_region(sm->hdrv.ptt_out.midiiobase, MIDI_EXTENT);
360 sm->hdrv.ptt_out.flags = 0;
363 /* --------------------------------------------------------------------- */
365 static int sm_open(struct net_device *dev);
366 static int sm_close(struct net_device *dev);
367 static int sm_ioctl(struct net_device *dev, struct ifreq *ifr,
368 struct hdlcdrv_ioctl *hi, int cmd);
370 /* --------------------------------------------------------------------- */
372 static const struct hdlcdrv_ops sm_ops = {
373 sm_drvname, sm_drvinfo, sm_open, sm_close, sm_ioctl
376 /* --------------------------------------------------------------------- */
378 static int sm_open(struct net_device *dev)
380 struct sm_state *sm;
381 int err;
383 if (!dev || !dev->priv ||
384 ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
385 printk(KERN_ERR "sm_open: invalid device struct\n");
386 return -EINVAL;
388 sm = (struct sm_state *)dev->priv;
390 if (!sm->mode_tx || !sm->mode_rx || !sm->hwdrv || !sm->hwdrv->open)
391 return -ENODEV;
392 sm->hdrv.par.bitrate = sm->mode_rx->bitrate;
393 err = sm->hwdrv->open(dev, sm);
394 if (err)
395 return err;
396 sm_output_open(sm);
397 MOD_INC_USE_COUNT;
398 printk(KERN_INFO "%s: %s mode %s.%s at iobase 0x%lx irq %u dma %u dma2 %u\n",
399 sm_drvname, sm->hwdrv->hw_name, sm->mode_tx->name,
400 sm->mode_rx->name, dev->base_addr, dev->irq, dev->dma, sm->hdrv.ptt_out.dma2);
401 return 0;
404 /* --------------------------------------------------------------------- */
406 static int sm_close(struct net_device *dev)
408 struct sm_state *sm;
409 int err = -ENODEV;
411 if (!dev || !dev->priv ||
412 ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
413 printk(KERN_ERR "sm_close: invalid device struct\n");
414 return -EINVAL;
416 sm = (struct sm_state *)dev->priv;
419 if (sm->hwdrv && sm->hwdrv->close)
420 err = sm->hwdrv && sm->hwdrv->close(dev, sm);
421 sm_output_close(sm);
422 MOD_DEC_USE_COUNT;
423 printk(KERN_INFO "%s: close %s at iobase 0x%lx irq %u dma %u\n",
424 sm_drvname, sm->hwdrv->hw_name, dev->base_addr, dev->irq, dev->dma);
425 return err;
428 /* --------------------------------------------------------------------- */
430 static int sethw(struct net_device *dev, struct sm_state *sm, char *mode)
432 char *cp = strchr(mode, ':');
433 const struct hardware_info **hwp = sm_hardware_table;
435 if (!cp)
436 cp = mode;
437 else {
438 *cp++ = '\0';
439 while (hwp && (*hwp) && (*hwp)->hw_name && strcmp((*hwp)->hw_name, mode))
440 hwp++;
441 if (!hwp || !*hwp || !(*hwp)->hw_name)
442 return -EINVAL;
443 if ((*hwp)->loc_storage > sizeof(sm->hw)) {
444 printk(KERN_ERR "%s: insufficient storage for hw driver %s (%d)\n",
445 sm_drvname, (*hwp)->hw_name, (*hwp)->loc_storage);
446 return -EINVAL;
448 sm->hwdrv = *hwp;
450 if (!*cp)
451 return 0;
452 if (sm->hwdrv && sm->hwdrv->sethw)
453 return sm->hwdrv->sethw(dev, sm, cp);
454 return -EINVAL;
457 /* --------------------------------------------------------------------- */
459 static int sm_ioctl(struct net_device *dev, struct ifreq *ifr,
460 struct hdlcdrv_ioctl *hi, int cmd)
462 struct sm_state *sm;
463 struct sm_ioctl bi;
464 unsigned long flags;
465 unsigned int newdiagmode;
466 unsigned int newdiagflags;
467 char *cp;
468 const struct modem_tx_info **mtp = sm_modem_tx_table;
469 const struct modem_rx_info **mrp = sm_modem_rx_table;
470 const struct hardware_info **hwp = sm_hardware_table;
472 if (!dev || !dev->priv ||
473 ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
474 printk(KERN_ERR "sm_ioctl: invalid device struct\n");
475 return -EINVAL;
477 sm = (struct sm_state *)dev->priv;
479 if (cmd != SIOCDEVPRIVATE) {
480 if (!sm->hwdrv || !sm->hwdrv->ioctl)
481 return sm->hwdrv->ioctl(dev, sm, ifr, hi, cmd);
482 return -ENOIOCTLCMD;
484 switch (hi->cmd) {
485 default:
486 if (sm->hwdrv && sm->hwdrv->ioctl)
487 return sm->hwdrv->ioctl(dev, sm, ifr, hi, cmd);
488 return -ENOIOCTLCMD;
490 case HDLCDRVCTL_GETMODE:
491 cp = hi->data.modename;
492 if (sm->hwdrv && sm->hwdrv->hw_name)
493 cp += sprintf(cp, "%s:", sm->hwdrv->hw_name);
494 else
495 cp += sprintf(cp, "<unspec>:");
496 if (sm->mode_tx && sm->mode_tx->name)
497 cp += sprintf(cp, "%s", sm->mode_tx->name);
498 else
499 cp += sprintf(cp, "<unspec>");
500 if (!sm->mode_rx || !sm->mode_rx ||
501 strcmp(sm->mode_rx->name, sm->mode_tx->name)) {
502 if (sm->mode_rx && sm->mode_rx->name)
503 cp += sprintf(cp, ",%s", sm->mode_rx->name);
504 else
505 cp += sprintf(cp, ",<unspec>");
507 if (copy_to_user(ifr->ifr_data, hi, sizeof(*hi)))
508 return -EFAULT;
509 return 0;
511 case HDLCDRVCTL_SETMODE:
512 if (netif_running(dev) || !capable(CAP_NET_ADMIN))
513 return -EACCES;
514 hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
515 return sethw(dev, sm, hi->data.modename);
517 case HDLCDRVCTL_MODELIST:
518 cp = hi->data.modename;
519 while (*hwp) {
520 if ((*hwp)->hw_name)
521 cp += sprintf(cp, "%s:,", (*hwp)->hw_name);
522 hwp++;
524 while (*mtp) {
525 if ((*mtp)->name)
526 cp += sprintf(cp, ">%s,", (*mtp)->name);
527 mtp++;
529 while (*mrp) {
530 if ((*mrp)->name)
531 cp += sprintf(cp, "<%s,", (*mrp)->name);
532 mrp++;
534 cp[-1] = '\0';
535 if (copy_to_user(ifr->ifr_data, hi, sizeof(*hi)))
536 return -EFAULT;
537 return 0;
539 #ifdef SM_DEBUG
540 case SMCTL_GETDEBUG:
541 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
542 return -EFAULT;
543 bi.data.dbg.int_rate = sm->debug_vals.last_intcnt;
544 bi.data.dbg.mod_cycles = sm->debug_vals.mod_cyc;
545 bi.data.dbg.demod_cycles = sm->debug_vals.demod_cyc;
546 bi.data.dbg.dma_residue = sm->debug_vals.dma_residue;
547 sm->debug_vals.mod_cyc = sm->debug_vals.demod_cyc =
548 sm->debug_vals.dma_residue = 0;
549 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
550 return -EFAULT;
551 return 0;
552 #endif /* SM_DEBUG */
554 case SMCTL_DIAGNOSE:
555 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
556 return -EFAULT;
557 newdiagmode = bi.data.diag.mode;
558 newdiagflags = bi.data.diag.flags;
559 if (newdiagmode > SM_DIAGMODE_CONSTELLATION)
560 return -EINVAL;
561 bi.data.diag.mode = sm->diag.mode;
562 bi.data.diag.flags = sm->diag.flags;
563 bi.data.diag.samplesperbit = sm->mode_rx->sperbit;
564 if (sm->diag.mode != newdiagmode) {
565 save_flags(flags);
566 cli();
567 sm->diag.ptr = -1;
568 sm->diag.flags = newdiagflags & ~SM_DIAGFLAG_VALID;
569 sm->diag.mode = newdiagmode;
570 restore_flags(flags);
571 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
572 return -EFAULT;
573 return 0;
575 if (sm->diag.ptr < 0 || sm->diag.mode == SM_DIAGMODE_OFF) {
576 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
577 return -EFAULT;
578 return 0;
580 if (bi.data.diag.datalen > DIAGDATALEN)
581 bi.data.diag.datalen = DIAGDATALEN;
582 if (sm->diag.ptr < bi.data.diag.datalen) {
583 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
584 return -EFAULT;
585 return 0;
587 if (copy_to_user(bi.data.diag.data, sm->diag.data,
588 bi.data.diag.datalen * sizeof(short)))
589 return -EFAULT;
590 bi.data.diag.flags |= SM_DIAGFLAG_VALID;
591 save_flags(flags);
592 cli();
593 sm->diag.ptr = -1;
594 sm->diag.flags = newdiagflags & ~SM_DIAGFLAG_VALID;
595 sm->diag.mode = newdiagmode;
596 restore_flags(flags);
597 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
598 return -EFAULT;
599 return 0;
603 /* --------------------------------------------------------------------- */
606 * command line settable parameters
608 static char *mode[NR_PORTS] = { [0 ... NR_PORTS-1] = NULL };
609 static int iobase[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
610 static int irq[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
611 static int dma[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
612 static int dma2[NR_PORTS] = { [0 ... NR_PORTS-1] = -1 };
613 static int serio[NR_PORTS] = { [0 ... NR_PORTS-1] = 0 };
614 static int pario[NR_PORTS] = { [0 ... NR_PORTS-1] = 0 };
615 static int midiio[NR_PORTS] = { [0 ... NR_PORTS-1] = 0 };
617 MODULE_PARM(mode, "1-" __MODULE_STRING(NR_PORTS) "s");
618 MODULE_PARM_DESC(mode, "soundmodem operating mode; eg. sbc:afsk1200 or wss:fsk9600");
619 MODULE_PARM(iobase, "1-" __MODULE_STRING(NR_PORTS) "i");
620 MODULE_PARM_DESC(iobase, "soundmodem base address");
621 MODULE_PARM(irq, "1-" __MODULE_STRING(NR_PORTS) "i");
622 MODULE_PARM_DESC(irq, "soundmodem interrupt");
623 MODULE_PARM(dma, "1-" __MODULE_STRING(NR_PORTS) "i");
624 MODULE_PARM_DESC(dma, "soundmodem dma channel");
625 MODULE_PARM(dma2, "1-" __MODULE_STRING(NR_PORTS) "i");
626 MODULE_PARM_DESC(dma2, "soundmodem 2nd dma channel; full duplex only");
627 MODULE_PARM(serio, "1-" __MODULE_STRING(NR_PORTS) "i");
628 MODULE_PARM_DESC(serio, "soundmodem PTT output on serial port");
629 MODULE_PARM(pario, "1-" __MODULE_STRING(NR_PORTS) "i");
630 MODULE_PARM_DESC(pario, "soundmodem PTT output on parallel port");
631 MODULE_PARM(midiio, "1-" __MODULE_STRING(NR_PORTS) "i");
632 MODULE_PARM_DESC(midiio, "soundmodem PTT output on midi port");
634 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
635 MODULE_DESCRIPTION("Soundcard amateur radio modem driver");
637 /* --------------------------------------------------------------------- */
639 static int __init init_soundmodem(void)
641 int i, j, found = 0;
642 char set_hw = 1;
643 struct sm_state *sm;
644 char ifname[HDLCDRV_IFNAMELEN];
646 printk(sm_drvinfo);
648 * register net devices
650 for (i = 0; i < NR_PORTS; i++) {
651 struct net_device *dev = sm_device+i;
652 sprintf(ifname, "sm%d", i);
654 if (!mode[i])
655 set_hw = 0;
656 else {
657 if (!strncmp(mode[i], "sbc", 3)) {
658 if (iobase[i] == -1)
659 iobase[i] = 0x220;
660 if (irq[i] == -1)
661 irq[i] = 5;
662 if (dma[i] == -1)
663 dma[i] = 1;
664 } else {
665 if (iobase[i] == -1)
666 iobase[i] = 0x530;
667 if (irq[i] == -1)
668 irq[i] = 11;
669 if (dma[i] == -1)
670 dma[i] = 1;
673 if (!set_hw)
674 iobase[i] = irq[i] = 0;
675 j = hdlcdrv_register_hdlcdrv(dev, &sm_ops, sizeof(struct sm_state), ifname, iobase[i], irq[i], dma[i]);
676 if (!j) {
677 sm = (struct sm_state *)dev->priv;
678 sm->hdrv.ptt_out.dma2 = dma2[i];
679 sm->hdrv.ptt_out.seriobase = serio[i];
680 sm->hdrv.ptt_out.pariobase = pario[i];
681 sm->hdrv.ptt_out.midiiobase = midiio[i];
682 if (set_hw && sethw(dev, sm, mode[i]))
683 set_hw = 0;
684 found++;
685 } else {
686 printk(KERN_WARNING "%s: cannot register net device\n", sm_drvname);
689 if (!found)
690 return -ENXIO;
691 return 0;
694 static void __exit cleanup_soundmodem(void)
696 int i;
698 printk(KERN_INFO "sm: cleanup_module called\n");
700 for(i = 0; i < NR_PORTS; i++) {
701 struct net_device *dev = sm_device+i;
702 struct sm_state *sm = (struct sm_state *)dev->priv;
704 if (sm) {
705 if (sm->hdrv.magic != HDLCDRV_MAGIC)
706 printk(KERN_ERR "sm: invalid magic in "
707 "cleanup_module\n");
708 else
709 hdlcdrv_unregister_hdlcdrv(dev);
714 module_init(init_soundmodem);
715 module_exit(cleanup_soundmodem);
717 /* --------------------------------------------------------------------- */
719 #ifndef MODULE
722 * format: soundmodem=io,irq,dma[,dma2[,serio[,pario]]],mode
723 * mode: hw:modem
724 * hw: sbc, wss, wssfdx
725 * modem: afsk1200, fsk9600
728 static int __init sm_setup(char *str)
730 static unsigned nr_dev = 0;
731 int ints[8];
733 if (nr_dev >= NR_PORTS)
734 return 0;
735 str = get_options(str, 8, ints);
736 mode[nr_dev] = str;
737 if (ints[0] >= 1)
738 iobase[nr_dev] = ints[1];
739 if (ints[0] >= 2)
740 irq[nr_dev] = ints[2];
741 if (ints[0] >= 3)
742 dma[nr_dev] = ints[3];
743 if (ints[0] >= 4)
744 dma2[nr_dev] = ints[4];
745 if (ints[0] >= 5)
746 serio[nr_dev] = ints[5];
747 if (ints[0] >= 6)
748 pario[nr_dev] = ints[6];
749 if (ints[0] >= 7)
750 midiio[nr_dev] = ints[7];
751 nr_dev++;
752 return 1;
755 __setup("soundmodem=", sm_setup);
757 #endif /* MODULE */
758 /* --------------------------------------------------------------------- */