Import 2.1.42pre1
[davej-history.git] / drivers / net / soundmodem / sm.c
blobd55bc0c042d85e999a32b37dae4df220e2f14f41
1 /*****************************************************************************/
3 /*
4 * sm.c -- soundcard radio modem driver.
6 * Copyright (C) 1996 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.96 Started
38 * 18.10.96 Changed to new user space access routines (copy_{to,from}_user)
39 * 0.4 21.01.97 Separately compileable soundcard/modem modules
40 * 0.5 03.03.97 fixed LPT probing (check_lpt result was interpreted the wrong way round)
43 /*****************************************************************************/
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/ptrace.h>
48 #include <linux/types.h>
49 #include <linux/fcntl.h>
50 #include <linux/ioport.h>
51 #include <linux/net.h>
52 #include <linux/in.h>
53 #include <linux/string.h>
54 #include <asm/system.h>
55 #include <asm/io.h>
56 #include <asm/bitops.h>
57 #include <linux/delay.h>
58 #include <linux/errno.h>
59 #include <linux/init.h>
60 #include "sm.h"
62 /* --------------------------------------------------------------------- */
65 * currently this module is supposed to support both module styles, i.e.
66 * the old one present up to about 2.1.9, and the new one functioning
67 * starting with 2.1.21. The reason is I have a kit allowing to compile
68 * this module also under 2.0.x which was requested by several people.
69 * This will go in 2.2
71 #include <linux/version.h>
73 #if LINUX_VERSION_CODE >= 0x20100
74 #include <asm/uaccess.h>
75 #else
76 #include <asm/segment.h>
77 #include <linux/mm.h>
79 #undef put_user
80 #undef get_user
82 #define put_user(x,ptr) ({ __put_user((unsigned long)(x),(ptr),sizeof(*(ptr))); 0; })
83 #define get_user(x,ptr) ({ x = ((__typeof__(*(ptr)))__get_user((ptr),sizeof(*(ptr)))); 0; })
85 extern inline int copy_from_user(void *to, const void *from, unsigned long n)
87 int i = verify_area(VERIFY_READ, from, n);
88 if (i)
89 return i;
90 memcpy_fromfs(to, from, n);
91 return 0;
94 extern inline int copy_to_user(void *to, const void *from, unsigned long n)
96 int i = verify_area(VERIFY_WRITE, to, n);
97 if (i)
98 return i;
99 memcpy_tofs(to, from, n);
100 return 0;
102 #endif
104 /* --------------------------------------------------------------------- */
106 const char sm_drvname[] = "soundmodem";
107 static const char sm_drvinfo[] = KERN_INFO "soundmodem: (C) 1996 Thomas Sailer, HB9JNX/AE4WA\n"
108 KERN_INFO "soundmodem: version 0.5 compiled " __TIME__ " " __DATE__ "\n";
110 /* --------------------------------------------------------------------- */
112 const struct modem_tx_info *sm_modem_tx_table[] = {
113 #ifdef CONFIG_SOUNDMODEM_AFSK1200
114 &sm_afsk1200_tx,
115 #endif /* CONFIG_SOUNDMODEM_AFSK1200 */
116 #ifdef CONFIG_SOUNDMODEM_AFSK2666
117 &sm_afsk2666_tx,
118 #endif /* CONFIG_SOUNDMODEM_AFSK2666 */
119 #ifdef CONFIG_SOUNDMODEM_PSK4800
120 &sm_psk4800_tx,
121 #endif /* CONFIG_SOUNDMODEM_PSK4800 */
122 #ifdef CONFIG_SOUNDMODEM_HAPN4800
123 &sm_hapn4800_8_tx,
124 &sm_hapn4800_10_tx,
125 &sm_hapn4800_pm8_tx,
126 &sm_hapn4800_pm10_tx,
127 #endif /* CONFIG_SOUNDMODEM_HAPN4800 */
128 #ifdef CONFIG_SOUNDMODEM_FSK9600
129 &sm_fsk9600_4_tx,
130 &sm_fsk9600_5_tx,
131 #endif /* CONFIG_SOUNDMODEM_FSK9600 */
132 NULL
135 const struct modem_rx_info *sm_modem_rx_table[] = {
136 #ifdef CONFIG_SOUNDMODEM_AFSK1200
137 &sm_afsk1200_rx,
138 #endif /* CONFIG_SOUNDMODEM_AFSK1200 */
139 #ifdef CONFIG_SOUNDMODEM_AFSK2666
140 &sm_afsk2666_rx,
141 #endif /* CONFIG_SOUNDMODEM_AFSK2666 */
142 #ifdef CONFIG_SOUNDMODEM_PSK4800
143 &sm_psk4800_rx,
144 #endif /* CONFIG_SOUNDMODEM_PSK4800 */
145 #ifdef CONFIG_SOUNDMODEM_HAPN4800
146 &sm_hapn4800_8_rx,
147 &sm_hapn4800_10_rx,
148 &sm_hapn4800_pm8_rx,
149 &sm_hapn4800_pm10_rx,
150 #endif /* CONFIG_SOUNDMODEM_HAPN4800 */
151 #ifdef CONFIG_SOUNDMODEM_FSK9600
152 &sm_fsk9600_4_rx,
153 &sm_fsk9600_5_rx,
154 #endif /* CONFIG_SOUNDMODEM_FSK9600 */
155 NULL
158 static const struct hardware_info *sm_hardware_table[] = {
159 #ifdef CONFIG_SOUNDMODEM_SBC
160 &sm_hw_sbc,
161 &sm_hw_sbcfdx,
162 #endif /* CONFIG_SOUNDMODEM_SBC */
163 #ifdef CONFIG_SOUNDMODEM_WSS
164 &sm_hw_wss,
165 &sm_hw_wssfdx,
166 #endif /* CONFIG_SOUNDMODEM_WSS */
167 NULL
170 /* --------------------------------------------------------------------- */
172 #define NR_PORTS 4
174 /* --------------------------------------------------------------------- */
176 static struct device sm_device[NR_PORTS];
178 static struct {
179 char *mode;
180 int iobase, irq, dma, dma2, seriobase, pariobase, midiiobase;
181 } sm_ports[NR_PORTS] = {
182 { NULL, -1, 0, 0, 0, -1, -1, -1 },
185 /* --------------------------------------------------------------------- */
187 #define UART_RBR(iobase) (iobase+0)
188 #define UART_THR(iobase) (iobase+0)
189 #define UART_IER(iobase) (iobase+1)
190 #define UART_IIR(iobase) (iobase+2)
191 #define UART_FCR(iobase) (iobase+2)
192 #define UART_LCR(iobase) (iobase+3)
193 #define UART_MCR(iobase) (iobase+4)
194 #define UART_LSR(iobase) (iobase+5)
195 #define UART_MSR(iobase) (iobase+6)
196 #define UART_SCR(iobase) (iobase+7)
197 #define UART_DLL(iobase) (iobase+0)
198 #define UART_DLM(iobase) (iobase+1)
200 #define SER_EXTENT 8
202 #define LPT_DATA(iobase) (iobase+0)
203 #define LPT_STATUS(iobase) (iobase+1)
204 #define LPT_CONTROL(iobase) (iobase+2)
205 #define LPT_IRQ_ENABLE 0x10
207 #define LPT_EXTENT 3
209 #define MIDI_DATA(iobase) (iobase)
210 #define MIDI_STATUS(iobase) (iobase+1)
211 #define MIDI_READ_FULL 0x80 /* attention: negative logic!! */
212 #define MIDI_WRITE_EMPTY 0x40 /* attention: negative logic!! */
214 #define MIDI_EXTENT 2
216 /* ---------------------------------------------------------------------- */
218 #define PARAM_TXDELAY 1
219 #define PARAM_PERSIST 2
220 #define PARAM_SLOTTIME 3
221 #define PARAM_TXTAIL 4
222 #define PARAM_FULLDUP 5
223 #define PARAM_HARDWARE 6
224 #define PARAM_RETURN 255
226 #define SP_SER 1
227 #define SP_PAR 2
228 #define SP_MIDI 4
230 /* --------------------------------------------------------------------- */
232 * ===================== port checking routines ========================
236 * returns 0 if ok and != 0 on error;
237 * the same behaviour as par96_check_lpt in baycom.c
241 * returns 0 if ok and != 0 on error;
242 * the same behaviour as par96_check_lpt in baycom.c
245 static int check_lpt(unsigned int iobase)
247 unsigned char b1,b2;
248 int i;
250 if (iobase <= 0 || iobase > 0x1000-LPT_EXTENT)
251 return 0;
252 if (check_region(iobase, LPT_EXTENT))
253 return 0;
254 b1 = inb(LPT_DATA(iobase));
255 b2 = inb(LPT_CONTROL(iobase));
256 outb(0xaa, LPT_DATA(iobase));
257 i = inb(LPT_DATA(iobase)) == 0xaa;
258 outb(0x55, LPT_DATA(iobase));
259 i &= inb(LPT_DATA(iobase)) == 0x55;
260 outb(0x0a, LPT_CONTROL(iobase));
261 i &= (inb(LPT_CONTROL(iobase)) & 0xf) == 0x0a;
262 outb(0x05, LPT_CONTROL(iobase));
263 i &= (inb(LPT_CONTROL(iobase)) & 0xf) == 0x05;
264 outb(b1, LPT_DATA(iobase));
265 outb(b2, LPT_CONTROL(iobase));
266 return !i;
269 /* --------------------------------------------------------------------- */
271 enum uart { c_uart_unknown, c_uart_8250,
272 c_uart_16450, c_uart_16550, c_uart_16550A};
273 static const char *uart_str[] =
274 { "unknown", "8250", "16450", "16550", "16550A" };
276 static enum uart check_uart(unsigned int iobase)
278 unsigned char b1,b2,b3;
279 enum uart u;
280 enum uart uart_tab[] =
281 { c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
283 if (iobase <= 0 || iobase > 0x1000-SER_EXTENT)
284 return c_uart_unknown;
285 if (check_region(iobase, SER_EXTENT))
286 return c_uart_unknown;
287 b1 = inb(UART_MCR(iobase));
288 outb(b1 | 0x10, UART_MCR(iobase)); /* loopback mode */
289 b2 = inb(UART_MSR(iobase));
290 outb(0x1a, UART_MCR(iobase));
291 b3 = inb(UART_MSR(iobase)) & 0xf0;
292 outb(b1, UART_MCR(iobase)); /* restore old values */
293 outb(b2, UART_MSR(iobase));
294 if (b3 != 0x90)
295 return c_uart_unknown;
296 inb(UART_RBR(iobase));
297 inb(UART_RBR(iobase));
298 outb(0x01, UART_FCR(iobase)); /* enable FIFOs */
299 u = uart_tab[(inb(UART_IIR(iobase)) >> 6) & 3];
300 if (u == c_uart_16450) {
301 outb(0x5a, UART_SCR(iobase));
302 b1 = inb(UART_SCR(iobase));
303 outb(0xa5, UART_SCR(iobase));
304 b2 = inb(UART_SCR(iobase));
305 if ((b1 != 0x5a) || (b2 != 0xa5))
306 u = c_uart_8250;
308 return u;
311 /* --------------------------------------------------------------------- */
313 static int check_midi(unsigned int iobase)
315 unsigned long timeout;
316 unsigned long flags;
317 unsigned char b;
319 if (iobase <= 0 || iobase > 0x1000-MIDI_EXTENT)
320 return 0;
321 if (check_region(iobase, MIDI_EXTENT))
322 return 0;
323 timeout = jiffies + (HZ / 100);
324 while (inb(MIDI_STATUS(iobase)) & MIDI_WRITE_EMPTY)
325 if ((signed)(jiffies - timeout) > 0)
326 return 0;
327 save_flags(flags);
328 cli();
329 outb(0xff, MIDI_DATA(iobase));
330 b = inb(MIDI_STATUS(iobase));
331 restore_flags(flags);
332 if (!(b & MIDI_WRITE_EMPTY))
333 return 0;
334 while (inb(MIDI_STATUS(iobase)) & MIDI_WRITE_EMPTY)
335 if ((signed)(jiffies - timeout) > 0)
336 return 0;
337 return 1;
340 /* --------------------------------------------------------------------- */
342 void sm_output_status(struct sm_state *sm)
344 int invert_dcd = 0;
345 int invert_ptt = 0;
347 int ptt = hdlcdrv_ptt(&sm->hdrv) ^ invert_ptt;
348 int dcd = (!!sm->hdrv.hdlcrx.dcd) ^ invert_dcd;
350 if (sm->hdrv.ptt_out.flags & SP_SER) {
351 outb(dcd | (ptt << 1), UART_MCR(sm->hdrv.ptt_out.seriobase));
352 outb(0x40 & (-ptt), UART_LCR(sm->hdrv.ptt_out.seriobase));
354 if (sm->hdrv.ptt_out.flags & SP_PAR) {
355 outb(ptt | (dcd << 1), LPT_DATA(sm->hdrv.ptt_out.pariobase));
357 if (sm->hdrv.ptt_out.flags & SP_MIDI && hdlcdrv_ptt(&sm->hdrv)) {
358 outb(0, MIDI_DATA(sm->hdrv.ptt_out.midiiobase));
362 /* --------------------------------------------------------------------- */
364 static void sm_output_open(struct sm_state *sm)
366 enum uart u = c_uart_unknown;
368 sm->hdrv.ptt_out.flags = 0;
369 if (sm->hdrv.ptt_out.seriobase > 0 &&
370 sm->hdrv.ptt_out.seriobase <= 0x1000-SER_EXTENT &&
371 ((u = check_uart(sm->hdrv.ptt_out.seriobase))) != c_uart_unknown) {
372 sm->hdrv.ptt_out.flags |= SP_SER;
373 request_region(sm->hdrv.ptt_out.seriobase, SER_EXTENT, "sm ser ptt");
374 outb(0, UART_IER(sm->hdrv.ptt_out.seriobase));
375 /* 5 bits, 1 stop, no parity, no break, Div latch access */
376 outb(0x80, UART_LCR(sm->hdrv.ptt_out.seriobase));
377 outb(0, UART_DLM(sm->hdrv.ptt_out.seriobase));
378 outb(1, UART_DLL(sm->hdrv.ptt_out.seriobase)); /* as fast as possible */
379 /* LCR and MCR set by output_status */
381 if (sm->hdrv.ptt_out.pariobase > 0 &&
382 sm->hdrv.ptt_out.pariobase <= 0x1000-LPT_EXTENT &&
383 !check_lpt(sm->hdrv.ptt_out.pariobase)) {
384 sm->hdrv.ptt_out.flags |= SP_PAR;
385 request_region(sm->hdrv.ptt_out.pariobase, LPT_EXTENT, "sm par ptt");
387 if (sm->hdrv.ptt_out.midiiobase > 0 &&
388 sm->hdrv.ptt_out.midiiobase <= 0x1000-MIDI_EXTENT &&
389 check_midi(sm->hdrv.ptt_out.midiiobase)) {
390 sm->hdrv.ptt_out.flags |= SP_MIDI;
391 request_region(sm->hdrv.ptt_out.midiiobase, MIDI_EXTENT,
392 "sm midi ptt");
394 sm_output_status(sm);
396 printk(KERN_INFO "%s: ptt output:", sm_drvname);
397 if (sm->hdrv.ptt_out.flags & SP_SER)
398 printk(" serial interface at 0x%x, uart %s", sm->hdrv.ptt_out.seriobase,
399 uart_str[u]);
400 if (sm->hdrv.ptt_out.flags & SP_PAR)
401 printk(" parallel interface at 0x%x", sm->hdrv.ptt_out.pariobase);
402 if (sm->hdrv.ptt_out.flags & SP_MIDI)
403 printk(" mpu401 (midi) interface at 0x%x", sm->hdrv.ptt_out.midiiobase);
404 if (!sm->hdrv.ptt_out.flags)
405 printk(" none");
406 printk("\n");
409 /* --------------------------------------------------------------------- */
411 static void sm_output_close(struct sm_state *sm)
413 /* release regions used for PTT output */
414 sm->hdrv.hdlctx.ptt = sm->hdrv.hdlctx.calibrate = 0;
415 sm_output_status(sm);
416 if (sm->hdrv.ptt_out.flags & SP_SER)
417 release_region(sm->hdrv.ptt_out.seriobase, SER_EXTENT);
418 if (sm->hdrv.ptt_out.flags & SP_PAR)
419 release_region(sm->hdrv.ptt_out.pariobase, LPT_EXTENT);
420 if (sm->hdrv.ptt_out.flags & SP_MIDI)
421 release_region(sm->hdrv.ptt_out.midiiobase, MIDI_EXTENT);
422 sm->hdrv.ptt_out.flags = 0;
425 /* --------------------------------------------------------------------- */
427 static int sm_open(struct device *dev);
428 static int sm_close(struct device *dev);
429 static int sm_ioctl(struct device *dev, struct ifreq *ifr,
430 struct hdlcdrv_ioctl *hi, int cmd);
432 /* --------------------------------------------------------------------- */
434 static const struct hdlcdrv_ops sm_ops = {
435 sm_drvname, sm_drvinfo, sm_open, sm_close, sm_ioctl
438 /* --------------------------------------------------------------------- */
440 static int sm_open(struct device *dev)
442 struct sm_state *sm;
443 int err;
445 if (!dev || !dev->priv ||
446 ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
447 printk(KERN_ERR "sm_open: invalid device struct\n");
448 return -EINVAL;
450 sm = (struct sm_state *)dev->priv;
452 if (!sm->mode_tx || !sm->mode_rx || !sm->hwdrv || !sm->hwdrv->open)
453 return -ENODEV;
454 sm->hdrv.par.bitrate = sm->mode_rx->bitrate;
455 err = sm->hwdrv->open(dev, sm);
456 if (err)
457 return err;
458 sm_output_open(sm);
459 MOD_INC_USE_COUNT;
460 printk(KERN_INFO "%s: %s mode %s.%s at iobase 0x%lx irq %u dma %u\n",
461 sm_drvname, sm->hwdrv->hw_name, sm->mode_tx->name,
462 sm->mode_rx->name, dev->base_addr, dev->irq, dev->dma);
463 return 0;
466 /* --------------------------------------------------------------------- */
468 static int sm_close(struct device *dev)
470 struct sm_state *sm;
471 int err = -ENODEV;
473 if (!dev || !dev->priv ||
474 ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
475 printk(KERN_ERR "sm_close: invalid device struct\n");
476 return -EINVAL;
478 sm = (struct sm_state *)dev->priv;
481 if (sm->hwdrv && sm->hwdrv->close)
482 err = sm->hwdrv && sm->hwdrv->close(dev, sm);
483 sm_output_close(sm);
484 MOD_DEC_USE_COUNT;
485 printk(KERN_INFO "%s: close %s at iobase 0x%lx irq %u dma %u\n",
486 sm_drvname, sm->hwdrv->hw_name, dev->base_addr, dev->irq, dev->dma);
487 return err;
490 /* --------------------------------------------------------------------- */
492 static int sethw(struct device *dev, struct sm_state *sm, char *mode)
494 char *cp = strchr(mode, ':');
495 const struct hardware_info **hwp = sm_hardware_table;
497 if (!cp)
498 cp = mode;
499 else {
500 *cp++ = '\0';
501 while (hwp && (*hwp) && (*hwp)->hw_name && strcmp((*hwp)->hw_name, mode))
502 hwp++;
503 if (!hwp || !*hwp || !(*hwp)->hw_name)
504 return -EINVAL;
505 if ((*hwp)->loc_storage > sizeof(sm->hw)) {
506 printk(KERN_ERR "%s: insufficient storage for hw driver %s (%d)\n",
507 sm_drvname, (*hwp)->hw_name, (*hwp)->loc_storage);
508 return -EINVAL;
510 sm->hwdrv = *hwp;
512 if (!*cp)
513 return 0;
514 if (sm->hwdrv && sm->hwdrv->sethw)
515 return sm->hwdrv->sethw(dev, sm, cp);
516 return -EINVAL;
519 /* --------------------------------------------------------------------- */
521 static int sm_ioctl(struct device *dev, struct ifreq *ifr,
522 struct hdlcdrv_ioctl *hi, int cmd)
524 struct sm_state *sm;
525 struct sm_ioctl bi;
526 unsigned long flags;
527 unsigned int newdiagmode;
528 unsigned int newdiagflags;
529 char *cp;
530 const struct modem_tx_info **mtp = sm_modem_tx_table;
531 const struct modem_rx_info **mrp = sm_modem_rx_table;
532 const struct hardware_info **hwp = sm_hardware_table;
534 if (!dev || !dev->priv ||
535 ((struct sm_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
536 printk(KERN_ERR "sm_ioctl: invalid device struct\n");
537 return -EINVAL;
539 sm = (struct sm_state *)dev->priv;
541 if (cmd != SIOCDEVPRIVATE) {
542 if (!sm->hwdrv || !sm->hwdrv->ioctl)
543 return sm->hwdrv->ioctl(dev, sm, ifr, hi, cmd);
544 return -ENOIOCTLCMD;
546 switch (hi->cmd) {
547 default:
548 if (sm->hwdrv && sm->hwdrv->ioctl)
549 return sm->hwdrv->ioctl(dev, sm, ifr, hi, cmd);
550 return -ENOIOCTLCMD;
552 case HDLCDRVCTL_GETMODE:
553 cp = hi->data.modename;
554 if (sm->hwdrv && sm->hwdrv->hw_name)
555 cp += sprintf(cp, "%s:", sm->hwdrv->hw_name);
556 else
557 cp += sprintf(cp, "<unspec>:");
558 if (sm->mode_tx && sm->mode_tx->name)
559 cp += sprintf(cp, "%s", sm->mode_tx->name);
560 else
561 cp += sprintf(cp, "<unspec>");
562 if (!sm->mode_rx || !sm->mode_rx ||
563 strcmp(sm->mode_rx->name, sm->mode_tx->name)) {
564 if (sm->mode_rx && sm->mode_rx->name)
565 cp += sprintf(cp, ",%s", sm->mode_rx->name);
566 else
567 cp += sprintf(cp, ",<unspec>");
569 if (copy_to_user(ifr->ifr_data, hi, sizeof(*hi)))
570 return -EFAULT;
571 return 0;
573 case HDLCDRVCTL_SETMODE:
574 if (!suser() || dev->start)
575 return -EACCES;
576 hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
577 return sethw(dev, sm, hi->data.modename);
579 case HDLCDRVCTL_MODELIST:
580 cp = hi->data.modename;
581 while (*hwp) {
582 if ((*hwp)->hw_name)
583 cp += sprintf("%s:,", (*hwp)->hw_name);
584 hwp++;
586 while (*mtp) {
587 if ((*mtp)->name)
588 cp += sprintf(">%s,", (*mtp)->name);
589 mtp++;
591 while (*mrp) {
592 if ((*mrp)->name)
593 cp += sprintf("<%s,", (*mrp)->name);
594 mrp++;
596 cp[-1] = '\0';
597 if (copy_to_user(ifr->ifr_data, hi, sizeof(*hi)))
598 return -EFAULT;
599 return 0;
601 #ifdef SM_DEBUG
602 case SMCTL_GETDEBUG:
603 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
604 return -EFAULT;
605 bi.data.dbg.int_rate = sm->debug_vals.last_intcnt;
606 bi.data.dbg.mod_cycles = sm->debug_vals.mod_cyc;
607 bi.data.dbg.demod_cycles = sm->debug_vals.demod_cyc;
608 bi.data.dbg.dma_residue = sm->debug_vals.dma_residue;
609 sm->debug_vals.mod_cyc = sm->debug_vals.demod_cyc =
610 sm->debug_vals.dma_residue = 0;
611 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
612 return -EFAULT;
613 return 0;
614 #endif /* SM_DEBUG */
616 case SMCTL_DIAGNOSE:
617 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
618 return -EFAULT;
619 newdiagmode = bi.data.diag.mode;
620 newdiagflags = bi.data.diag.flags;
621 if (newdiagmode > SM_DIAGMODE_CONSTELLATION)
622 return -EINVAL;
623 bi.data.diag.mode = sm->diag.mode;
624 bi.data.diag.flags = sm->diag.flags;
625 bi.data.diag.samplesperbit = sm->mode_rx->sperbit;
626 if (sm->diag.mode != newdiagmode) {
627 save_flags(flags);
628 cli();
629 sm->diag.ptr = -1;
630 sm->diag.flags = newdiagflags & ~SM_DIAGFLAG_VALID;
631 sm->diag.mode = newdiagmode;
632 restore_flags(flags);
633 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
634 return -EFAULT;
635 return 0;
637 if (sm->diag.ptr < 0 || sm->diag.mode == SM_DIAGMODE_OFF) {
638 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
639 return -EFAULT;
640 return 0;
642 if (bi.data.diag.datalen > DIAGDATALEN)
643 bi.data.diag.datalen = DIAGDATALEN;
644 if (sm->diag.ptr < bi.data.diag.datalen) {
645 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
646 return -EFAULT;
647 return 0;
649 if (copy_to_user(bi.data.diag.data, sm->diag.data,
650 bi.data.diag.datalen * sizeof(short)))
651 return -EFAULT;
652 bi.data.diag.flags |= SM_DIAGFLAG_VALID;
653 save_flags(flags);
654 cli();
655 sm->diag.ptr = -1;
656 sm->diag.flags = newdiagflags & ~SM_DIAGFLAG_VALID;
657 sm->diag.mode = newdiagmode;
658 restore_flags(flags);
659 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
660 return -EFAULT;
661 return 0;
665 /* --------------------------------------------------------------------- */
667 __initfunc(int sm_init(void))
669 int i, j, found = 0;
670 char set_hw = 1;
671 struct sm_state *sm;
672 char ifname[HDLCDRV_IFNAMELEN];
674 printk(sm_drvinfo);
676 * register net devices
678 for (i = 0; i < NR_PORTS; i++) {
679 struct device *dev = sm_device+i;
680 sprintf(ifname, "sm%d", i);
682 if (!sm_ports[i].mode)
683 set_hw = 0;
684 if (!set_hw)
685 sm_ports[i].iobase = sm_ports[i].irq = 0;
686 j = hdlcdrv_register_hdlcdrv(dev, &sm_ops, sizeof(struct sm_state),
687 ifname, sm_ports[i].iobase,
688 sm_ports[i].irq, sm_ports[i].dma);
689 if (!j) {
690 sm = (struct sm_state *)dev->priv;
691 sm->hdrv.ptt_out.dma2 = sm_ports[i].dma2;
692 sm->hdrv.ptt_out.seriobase = sm_ports[i].seriobase;
693 sm->hdrv.ptt_out.pariobase = sm_ports[i].pariobase;
694 sm->hdrv.ptt_out.midiiobase = sm_ports[i].midiiobase;
695 if (set_hw && sethw(dev, sm, sm_ports[i].mode))
696 set_hw = 0;
697 found++;
698 } else {
699 printk(KERN_WARNING "%s: cannot register net device\n",
700 sm_drvname);
703 if (!found)
704 return -ENXIO;
705 return 0;
708 /* --------------------------------------------------------------------- */
710 #ifdef MODULE
713 * command line settable parameters
715 static char *mode = NULL;
716 static int iobase = -1;
717 static int irq = -1;
718 static int dma = -1;
719 static int dma2 = -1;
720 static int serio = 0;
721 static int pario = 0;
722 static int midiio = 0;
724 #if LINUX_VERSION_CODE >= 0x20115
726 MODULE_PARM(mode, "s");
727 MODULE_PARM_DESC(mode, "soundmodem operating mode; eg. sbc:afsk1200 or wss:fsk9600");
728 MODULE_PARM(iobase, "i");
729 MODULE_PARM_DESC(iobase, "soundmodem base address");
730 MODULE_PARM(irq, "i");
731 MODULE_PARM_DESC(irq, "soundmodem interrupt");
732 MODULE_PARM(dma, "i");
733 MODULE_PARM_DESC(dma, "soundmodem dma channel");
734 MODULE_PARM(dma2, "i");
735 MODULE_PARM_DESC(dma2, "soundmodem 2nd dma channel; full duplex only");
736 MODULE_PARM(serio, "i");
737 MODULE_PARM_DESC(serio, "soundmodem PTT output on serial port");
738 MODULE_PARM(pario, "i");
739 MODULE_PARM_DESC(pario, "soundmodem PTT output on parallel port");
740 MODULE_PARM(midiio, "i");
741 MODULE_PARM_DESC(midiio, "soundmodem PTT output on midi port");
743 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
744 MODULE_DESCRIPTION("Soundcard amateur radio modem driver");
746 #endif
748 int init_module(void)
750 if (mode) {
751 if (iobase == -1)
752 iobase = (!strncmp(mode, "sbc", 3)) ? 0x220 : 0x530;
753 if (irq == -1)
754 irq = (!strncmp(mode, "sbc", 3)) ? 5 : 11;
755 if (dma == -1)
756 dma = 1;
758 sm_ports[0].mode = mode;
759 sm_ports[0].iobase = iobase;
760 sm_ports[0].irq = irq;
761 sm_ports[0].dma = dma;
762 sm_ports[0].dma2 = dma2;
763 sm_ports[0].seriobase = serio;
764 sm_ports[0].pariobase = pario;
765 sm_ports[0].midiiobase = midiio;
766 sm_ports[1].mode = NULL;
768 return sm_init();
771 /* --------------------------------------------------------------------- */
773 void cleanup_module(void)
775 int i;
777 printk(KERN_INFO "sm: cleanup_module called\n");
779 for(i = 0; i < NR_PORTS; i++) {
780 struct device *dev = sm_device+i;
781 struct sm_state *sm = (struct sm_state *)dev->priv;
783 if (sm) {
784 if (sm->hdrv.magic != HDLCDRV_MAGIC)
785 printk(KERN_ERR "sm: invalid magic in "
786 "cleanup_module\n");
787 else
788 hdlcdrv_unregister_hdlcdrv(dev);
793 #else /* MODULE */
794 /* --------------------------------------------------------------------- */
796 * format: sm=io,irq,dma[,dma2[,serio[,pario]]],mode
797 * mode: hw:modem
798 * hw: sbc, wss, wssfdx
799 * modem: afsk1200, fsk9600
802 __initfunc(void sm_setup(char *str, int *ints))
804 int i;
806 for (i = 0; (i < NR_PORTS) && (sm_ports[i].mode); i++);
807 if ((i >= NR_PORTS) || (ints[0] < 3)) {
808 printk(KERN_INFO "%s: too many or invalid interface "
809 "specifications\n", sm_drvname);
810 return;
812 sm_ports[i].mode = str;
813 sm_ports[i].iobase = ints[1];
814 sm_ports[i].irq = ints[2];
815 sm_ports[i].dma = ints[3];
816 sm_ports[i].dma2 = (ints[0] >= 4) ? ints[4] : 0;
817 sm_ports[i].seriobase = (ints[0] >= 5) ? ints[5] : 0;
818 sm_ports[i].pariobase = (ints[0] >= 6) ? ints[6] : 0;
819 sm_ports[i].midiiobase = (ints[0] >= 7) ? ints[7] : 0;
820 if (i < NR_PORTS-1)
821 sm_ports[i+1].mode = NULL;
824 #endif /* MODULE */
825 /* --------------------------------------------------------------------- */