dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / net / hamradio / baycom_par.c
blob1f7ceafd61ffa4c1ac9d93c9720d5e6863250817
1 /*****************************************************************************/
3 /*
4 * baycom_par.c -- baycom par96 and picpar 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 * Supported modems
29 * par96: This is a modem for 9600 baud FSK compatible to the G3RUH standard.
30 * The modem does all the filtering and regenerates the receiver clock.
31 * Data is transferred from and to the PC via a shift register.
32 * The shift register is filled with 16 bits and an interrupt is
33 * signalled. The PC then empties the shift register in a burst. This
34 * modem connects to the parallel port, hence the name. The modem
35 * leaves the implementation of the HDLC protocol and the scrambler
36 * polynomial to the PC. This modem is no longer available (at least
37 * from Baycom) and has been replaced by the PICPAR modem (see below).
38 * You may however still build one from the schematics published in
39 * cq-DL :-).
41 * picpar: This is a redesign of the par96 modem by Henning Rech, DF9IC. The
42 * modem is protocol compatible to par96, but uses only three low
43 * power ICs and can therefore be fed from the parallel port and
44 * does not require an additional power supply. It features
45 * built in DCD circuitry. The driver should therefore be configured
46 * for hardware DCD.
49 * Command line options (insmod command line)
51 * mode driver mode string. Valid choices are par96 and picpar.
52 * iobase base address of the port; common values are 0x378, 0x278, 0x3bc
55 * History:
56 * 0.1 26.06.1996 Adapted from baycom.c and made network driver interface
57 * 18.10.1996 Changed to new user space access routines (copy_{to,from}_user)
58 * 0.3 26.04.1997 init code/data tagged
59 * 0.4 08.07.1997 alternative ser12 decoding algorithm (uses delta CTS ints)
60 * 0.5 11.11.1997 split into separate files for ser12/par96
61 * 0.6 03.08.1999 adapt to Linus' new __setup/__initcall
62 * removed some pre-2.2 kernel compatibility cruft
63 * 0.7 10.08.1999 Check if parport can do SPP and is safe to access during interrupt contexts
64 * 0.8 12.02.2000 adapted to softnet driver interface
65 * removed direct parport access, uses parport driver methods
66 * 0.9 03.07.2000 fix interface name handling
69 /*****************************************************************************/
71 #include <linux/module.h>
72 #include <linux/kernel.h>
73 #include <linux/types.h>
74 #include <linux/fcntl.h>
75 #include <linux/interrupt.h>
76 #include <linux/ioport.h>
77 #include <linux/in.h>
78 #include <linux/string.h>
79 #include <linux/init.h>
80 #include <linux/delay.h>
81 #include <linux/errno.h>
82 #include <linux/netdevice.h>
83 #include <linux/hdlcdrv.h>
84 #include <linux/baycom.h>
85 #include <linux/parport.h>
86 #include <linux/bitops.h>
87 #include <linux/jiffies.h>
89 #include <linux/uaccess.h>
91 /* --------------------------------------------------------------------- */
93 #define BAYCOM_DEBUG
96 * modem options; bit mask
98 #define BAYCOM_OPTIONS_SOFTDCD 1
100 /* --------------------------------------------------------------------- */
102 static const char bc_drvname[] = "baycom_par";
103 static const char bc_drvinfo[] = KERN_INFO "baycom_par: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
104 "baycom_par: version 0.9\n";
106 /* --------------------------------------------------------------------- */
108 #define NR_PORTS 4
110 static struct net_device *baycom_device[NR_PORTS];
112 /* --------------------------------------------------------------------- */
114 #define PAR96_BURSTBITS 16
115 #define PAR96_BURST 4
116 #define PAR96_PTT 2
117 #define PAR96_TXBIT 1
118 #define PAR96_ACK 0x40
119 #define PAR96_RXBIT 0x20
120 #define PAR96_DCD 0x10
121 #define PAR97_POWER 0xf8
123 /* ---------------------------------------------------------------------- */
125 * Information that need to be kept for each board.
128 struct baycom_state {
129 struct hdlcdrv_state hdrv;
131 struct pardevice *pdev;
132 unsigned int options;
134 struct modem_state {
135 short arb_divider;
136 unsigned char flags;
137 unsigned int shreg;
138 struct modem_state_par96 {
139 int dcd_count;
140 unsigned int dcd_shreg;
141 unsigned long descram;
142 unsigned long scram;
143 } par96;
144 } modem;
146 #ifdef BAYCOM_DEBUG
147 struct debug_vals {
148 unsigned long last_jiffies;
149 unsigned cur_intcnt;
150 unsigned last_intcnt;
151 int cur_pllcorr;
152 int last_pllcorr;
153 } debug_vals;
154 #endif /* BAYCOM_DEBUG */
157 /* --------------------------------------------------------------------- */
159 static inline void baycom_int_freq(struct baycom_state *bc)
161 #ifdef BAYCOM_DEBUG
162 unsigned long cur_jiffies = jiffies;
164 * measure the interrupt frequency
166 bc->debug_vals.cur_intcnt++;
167 if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
168 bc->debug_vals.last_jiffies = cur_jiffies;
169 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
170 bc->debug_vals.cur_intcnt = 0;
171 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
172 bc->debug_vals.cur_pllcorr = 0;
174 #endif /* BAYCOM_DEBUG */
177 /* --------------------------------------------------------------------- */
179 * ===================== PAR96 specific routines =========================
182 #define PAR96_DESCRAM_TAP1 0x20000
183 #define PAR96_DESCRAM_TAP2 0x01000
184 #define PAR96_DESCRAM_TAP3 0x00001
186 #define PAR96_DESCRAM_TAPSH1 17
187 #define PAR96_DESCRAM_TAPSH2 12
188 #define PAR96_DESCRAM_TAPSH3 0
190 #define PAR96_SCRAM_TAP1 0x20000 /* X^17 */
191 #define PAR96_SCRAM_TAPN 0x00021 /* X^0+X^5 */
193 /* --------------------------------------------------------------------- */
195 static inline void par96_tx(struct net_device *dev, struct baycom_state *bc)
197 int i;
198 unsigned int data = hdlcdrv_getbits(&bc->hdrv);
199 struct parport *pp = bc->pdev->port;
201 for(i = 0; i < PAR96_BURSTBITS; i++, data >>= 1) {
202 unsigned char val = PAR97_POWER;
203 bc->modem.par96.scram = ((bc->modem.par96.scram << 1) |
204 (bc->modem.par96.scram & 1));
205 if (!(data & 1))
206 bc->modem.par96.scram ^= 1;
207 if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 1))
208 bc->modem.par96.scram ^=
209 (PAR96_SCRAM_TAPN << 1);
210 if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 2))
211 val |= PAR96_TXBIT;
212 pp->ops->write_data(pp, val);
213 pp->ops->write_data(pp, val | PAR96_BURST);
217 /* --------------------------------------------------------------------- */
219 static inline void par96_rx(struct net_device *dev, struct baycom_state *bc)
221 int i;
222 unsigned int data, mask, mask2, descx;
223 struct parport *pp = bc->pdev->port;
226 * do receiver; differential decode and descramble on the fly
228 for(data = i = 0; i < PAR96_BURSTBITS; i++) {
229 bc->modem.par96.descram = (bc->modem.par96.descram << 1);
230 if (pp->ops->read_status(pp) & PAR96_RXBIT)
231 bc->modem.par96.descram |= 1;
232 descx = bc->modem.par96.descram ^
233 (bc->modem.par96.descram >> 1);
234 /* now the diff decoded data is inverted in descram */
235 pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT);
236 descx ^= ((descx >> PAR96_DESCRAM_TAPSH1) ^
237 (descx >> PAR96_DESCRAM_TAPSH2));
238 data >>= 1;
239 if (!(descx & 1))
240 data |= 0x8000;
241 pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT | PAR96_BURST);
243 hdlcdrv_putbits(&bc->hdrv, data);
245 * do DCD algorithm
247 if (bc->options & BAYCOM_OPTIONS_SOFTDCD) {
248 bc->modem.par96.dcd_shreg = (bc->modem.par96.dcd_shreg >> 16)
249 | (data << 16);
250 /* search for flags and set the dcd counter appropriately */
251 for(mask = 0x1fe00, mask2 = 0xfc00, i = 0;
252 i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
253 if ((bc->modem.par96.dcd_shreg & mask) == mask2)
254 bc->modem.par96.dcd_count = HDLCDRV_MAXFLEN+4;
255 /* check for abort/noise sequences */
256 for(mask = 0x1fe00, mask2 = 0x1fe00, i = 0;
257 i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
258 if (((bc->modem.par96.dcd_shreg & mask) == mask2) &&
259 (bc->modem.par96.dcd_count >= 0))
260 bc->modem.par96.dcd_count -= HDLCDRV_MAXFLEN-10;
261 /* decrement and set the dcd variable */
262 if (bc->modem.par96.dcd_count >= 0)
263 bc->modem.par96.dcd_count -= 2;
264 hdlcdrv_setdcd(&bc->hdrv, bc->modem.par96.dcd_count > 0);
265 } else {
266 hdlcdrv_setdcd(&bc->hdrv, !!(pp->ops->read_status(pp) & PAR96_DCD));
270 /* --------------------------------------------------------------------- */
272 static void par96_interrupt(void *dev_id)
274 struct net_device *dev = dev_id;
275 struct baycom_state *bc = netdev_priv(dev);
277 baycom_int_freq(bc);
279 * check if transmitter active
281 if (hdlcdrv_ptt(&bc->hdrv))
282 par96_tx(dev, bc);
283 else {
284 par96_rx(dev, bc);
285 if (--bc->modem.arb_divider <= 0) {
286 bc->modem.arb_divider = 6;
287 local_irq_enable();
288 hdlcdrv_arbitrate(dev, &bc->hdrv);
291 local_irq_enable();
292 hdlcdrv_transmitter(dev, &bc->hdrv);
293 hdlcdrv_receiver(dev, &bc->hdrv);
294 local_irq_disable();
297 /* --------------------------------------------------------------------- */
299 static void par96_wakeup(void *handle)
301 struct net_device *dev = (struct net_device *)handle;
302 struct baycom_state *bc = netdev_priv(dev);
304 printk(KERN_DEBUG "baycom_par: %s: why am I being woken up?\n", dev->name);
305 if (!parport_claim(bc->pdev))
306 printk(KERN_DEBUG "baycom_par: %s: I'm broken.\n", dev->name);
309 /* --------------------------------------------------------------------- */
311 static int par96_open(struct net_device *dev)
313 struct baycom_state *bc = netdev_priv(dev);
314 struct pardev_cb par_cb;
315 struct parport *pp;
316 int i;
318 if (!dev || !bc)
319 return -ENXIO;
320 pp = parport_find_base(dev->base_addr);
321 if (!pp) {
322 printk(KERN_ERR "baycom_par: parport at 0x%lx unknown\n", dev->base_addr);
323 return -ENXIO;
325 if (pp->irq < 0) {
326 printk(KERN_ERR "baycom_par: parport at 0x%lx has no irq\n", pp->base);
327 parport_put_port(pp);
328 return -ENXIO;
330 if ((~pp->modes) & (PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
331 printk(KERN_ERR "baycom_par: parport at 0x%lx cannot be used\n", pp->base);
332 parport_put_port(pp);
333 return -ENXIO;
335 memset(&bc->modem, 0, sizeof(bc->modem));
336 bc->hdrv.par.bitrate = 9600;
337 memset(&par_cb, 0, sizeof(par_cb));
338 par_cb.wakeup = par96_wakeup;
339 par_cb.irq_func = par96_interrupt;
340 par_cb.private = (void *)dev;
341 par_cb.flags = PARPORT_DEV_EXCL;
342 for (i = 0; i < NR_PORTS; i++)
343 if (baycom_device[i] == dev)
344 break;
346 if (i == NR_PORTS) {
347 pr_err("%s: no device found\n", bc_drvname);
348 parport_put_port(pp);
349 return -ENODEV;
351 bc->pdev = parport_register_dev_model(pp, dev->name, &par_cb, i);
352 parport_put_port(pp);
353 if (!bc->pdev) {
354 printk(KERN_ERR "baycom_par: cannot register parport at 0x%lx\n", dev->base_addr);
355 return -ENXIO;
357 if (parport_claim(bc->pdev)) {
358 printk(KERN_ERR "baycom_par: parport at 0x%lx busy\n", pp->base);
359 parport_unregister_device(bc->pdev);
360 return -EBUSY;
362 pp = bc->pdev->port;
363 dev->irq = pp->irq;
364 pp->ops->data_forward(pp);
365 bc->hdrv.par.bitrate = 9600;
366 pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER); /* switch off PTT */
367 pp->ops->enable_irq(pp);
368 printk(KERN_INFO "%s: par96 at iobase 0x%lx irq %u options 0x%x\n",
369 bc_drvname, dev->base_addr, dev->irq, bc->options);
370 return 0;
373 /* --------------------------------------------------------------------- */
375 static int par96_close(struct net_device *dev)
377 struct baycom_state *bc = netdev_priv(dev);
378 struct parport *pp;
380 if (!dev || !bc)
381 return -EINVAL;
382 pp = bc->pdev->port;
383 /* disable interrupt */
384 pp->ops->disable_irq(pp);
385 /* switch off PTT */
386 pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER);
387 parport_release(bc->pdev);
388 parport_unregister_device(bc->pdev);
389 printk(KERN_INFO "%s: close par96 at iobase 0x%lx irq %u\n",
390 bc_drvname, dev->base_addr, dev->irq);
391 return 0;
394 /* --------------------------------------------------------------------- */
396 * ===================== hdlcdrv driver interface =========================
399 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
400 struct hdlcdrv_ioctl *hi, int cmd);
402 /* --------------------------------------------------------------------- */
404 static const struct hdlcdrv_ops par96_ops = {
405 .drvname = bc_drvname,
406 .drvinfo = bc_drvinfo,
407 .open = par96_open,
408 .close = par96_close,
409 .ioctl = baycom_ioctl
412 /* --------------------------------------------------------------------- */
414 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
416 if (!strncmp(modestr, "picpar", 6))
417 bc->options = 0;
418 else if (!strncmp(modestr, "par96", 5))
419 bc->options = BAYCOM_OPTIONS_SOFTDCD;
420 else
421 bc->options = !!strchr(modestr, '*');
422 return 0;
425 /* --------------------------------------------------------------------- */
427 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
428 struct hdlcdrv_ioctl *hi, int cmd)
430 struct baycom_state *bc;
431 struct baycom_ioctl bi;
433 if (!dev)
434 return -EINVAL;
436 bc = netdev_priv(dev);
437 BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
439 if (cmd != SIOCDEVPRIVATE)
440 return -ENOIOCTLCMD;
441 switch (hi->cmd) {
442 default:
443 break;
445 case HDLCDRVCTL_GETMODE:
446 strcpy(hi->data.modename, bc->options ? "par96" : "picpar");
447 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
448 return -EFAULT;
449 return 0;
451 case HDLCDRVCTL_SETMODE:
452 if (netif_running(dev) || !capable(CAP_NET_ADMIN))
453 return -EACCES;
454 hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
455 return baycom_setmode(bc, hi->data.modename);
457 case HDLCDRVCTL_MODELIST:
458 strcpy(hi->data.modename, "par96,picpar");
459 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
460 return -EFAULT;
461 return 0;
463 case HDLCDRVCTL_MODEMPARMASK:
464 return HDLCDRV_PARMASK_IOBASE;
468 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
469 return -EFAULT;
470 switch (bi.cmd) {
471 default:
472 return -ENOIOCTLCMD;
474 #ifdef BAYCOM_DEBUG
475 case BAYCOMCTL_GETDEBUG:
476 bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
477 bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
478 bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
479 break;
480 #endif /* BAYCOM_DEBUG */
483 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
484 return -EFAULT;
485 return 0;
489 /* --------------------------------------------------------------------- */
492 * command line settable parameters
494 static char *mode[NR_PORTS] = { "picpar", };
495 static int iobase[NR_PORTS] = { 0x378, };
497 module_param_array(mode, charp, NULL, 0);
498 MODULE_PARM_DESC(mode, "baycom operating mode; eg. par96 or picpar");
499 module_param_hw_array(iobase, int, ioport, NULL, 0);
500 MODULE_PARM_DESC(iobase, "baycom io base address");
502 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
503 MODULE_DESCRIPTION("Baycom par96 and picpar amateur radio modem driver");
504 MODULE_LICENSE("GPL");
506 /* --------------------------------------------------------------------- */
508 static int baycom_par_probe(struct pardevice *par_dev)
510 struct device_driver *drv = par_dev->dev.driver;
511 int len = strlen(drv->name);
513 if (strncmp(par_dev->name, drv->name, len))
514 return -ENODEV;
516 return 0;
519 static struct parport_driver baycom_par_driver = {
520 .name = "bcp",
521 .probe = baycom_par_probe,
522 .devmodel = true,
525 static int __init init_baycompar(void)
527 int i, found = 0, ret;
528 char set_hw = 1;
530 printk(bc_drvinfo);
532 ret = parport_register_driver(&baycom_par_driver);
533 if (ret)
534 return ret;
537 * register net devices
539 for (i = 0; i < NR_PORTS; i++) {
540 struct net_device *dev;
541 struct baycom_state *bc;
542 char ifname[IFNAMSIZ];
544 sprintf(ifname, "bcp%d", i);
546 if (!mode[i])
547 set_hw = 0;
548 if (!set_hw)
549 iobase[i] = 0;
551 dev = hdlcdrv_register(&par96_ops,
552 sizeof(struct baycom_state),
553 ifname, iobase[i], 0, 0);
554 if (IS_ERR(dev))
555 break;
557 bc = netdev_priv(dev);
558 if (set_hw && baycom_setmode(bc, mode[i]))
559 set_hw = 0;
560 found++;
561 baycom_device[i] = dev;
564 if (!found) {
565 parport_unregister_driver(&baycom_par_driver);
566 return -ENXIO;
568 return 0;
571 static void __exit cleanup_baycompar(void)
573 int i;
575 for(i = 0; i < NR_PORTS; i++) {
576 struct net_device *dev = baycom_device[i];
578 if (dev)
579 hdlcdrv_unregister(dev);
581 parport_unregister_driver(&baycom_par_driver);
584 module_init(init_baycompar);
585 module_exit(cleanup_baycompar);
587 /* --------------------------------------------------------------------- */
589 #ifndef MODULE
592 * format: baycom_par=io,mode
593 * mode: par96,picpar
596 static int __init baycom_par_setup(char *str)
598 static unsigned nr_dev;
599 int ints[2];
601 if (nr_dev >= NR_PORTS)
602 return 0;
603 str = get_options(str, 2, ints);
604 if (ints[0] < 1)
605 return 0;
606 mode[nr_dev] = str;
607 iobase[nr_dev] = ints[1];
608 nr_dev++;
609 return 1;
612 __setup("baycom_par=", baycom_par_setup);
614 #endif /* MODULE */
615 /* --------------------------------------------------------------------- */