Merge upstream kernel into libata 'passthru' branch
[linux-2.6] / drivers / net / hamradio / baycom_par.c
1 /*****************************************************************************/
2
3 /*
4  *      baycom_par.c  -- baycom par96 and picpar 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  *  Supported modems
28  *
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 :-).
40  *
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.
47  *
48  *
49  *  Command line options (insmod command line)
50  *
51  *  mode     driver mode string. Valid choices are par96 and picpar.
52  *  iobase   base address of the port; common values are 0x378, 0x278, 0x3bc
53  *
54  *
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
67  */
68
69 /*****************************************************************************/
70
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>
88
89 #include <asm/bug.h>
90 #include <asm/system.h>
91 #include <asm/uaccess.h>
92
93 /* --------------------------------------------------------------------- */
94
95 #define BAYCOM_DEBUG
96
97 /*
98  * modem options; bit mask
99  */
100 #define BAYCOM_OPTIONS_SOFTDCD  1
101
102 /* --------------------------------------------------------------------- */
103
104 static const char bc_drvname[] = "baycom_par";
105 static const char bc_drvinfo[] = KERN_INFO "baycom_par: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
106 KERN_INFO "baycom_par: version 0.9 compiled " __TIME__ " " __DATE__ "\n";
107
108 /* --------------------------------------------------------------------- */
109
110 #define NR_PORTS 4
111
112 static struct net_device *baycom_device[NR_PORTS];
113
114 /* --------------------------------------------------------------------- */
115
116 #define PAR96_BURSTBITS 16
117 #define PAR96_BURST     4
118 #define PAR96_PTT       2
119 #define PAR96_TXBIT     1
120 #define PAR96_ACK       0x40
121 #define PAR96_RXBIT     0x20
122 #define PAR96_DCD       0x10
123 #define PAR97_POWER     0xf8
124
125 /* ---------------------------------------------------------------------- */
126 /*
127  * Information that need to be kept for each board.
128  */
129
130 struct baycom_state {
131         struct hdlcdrv_state hdrv;
132
133         struct pardevice *pdev;
134         unsigned int options;
135
136         struct modem_state {
137                 short arb_divider;
138                 unsigned char flags;
139                 unsigned int shreg;
140                 struct modem_state_par96 {
141                         int dcd_count;
142                         unsigned int dcd_shreg;
143                         unsigned long descram;
144                         unsigned long scram;
145                 } par96;
146         } modem;
147
148 #ifdef BAYCOM_DEBUG
149         struct debug_vals {
150                 unsigned long last_jiffies;
151                 unsigned cur_intcnt;
152                 unsigned last_intcnt;
153                 int cur_pllcorr;
154                 int last_pllcorr;
155         } debug_vals;
156 #endif /* BAYCOM_DEBUG */
157 };
158
159 /* --------------------------------------------------------------------- */
160
161 static void __inline__ baycom_int_freq(struct baycom_state *bc)
162 {
163 #ifdef BAYCOM_DEBUG
164         unsigned long cur_jiffies = jiffies;
165         /*
166          * measure the interrupt frequency
167          */
168         bc->debug_vals.cur_intcnt++;
169         if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
170                 bc->debug_vals.last_jiffies = cur_jiffies;
171                 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
172                 bc->debug_vals.cur_intcnt = 0;
173                 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
174                 bc->debug_vals.cur_pllcorr = 0;
175         }
176 #endif /* BAYCOM_DEBUG */
177 }
178
179 /* --------------------------------------------------------------------- */
180 /*
181  * ===================== PAR96 specific routines =========================
182  */
183
184 #define PAR96_DESCRAM_TAP1 0x20000
185 #define PAR96_DESCRAM_TAP2 0x01000
186 #define PAR96_DESCRAM_TAP3 0x00001
187
188 #define PAR96_DESCRAM_TAPSH1 17
189 #define PAR96_DESCRAM_TAPSH2 12
190 #define PAR96_DESCRAM_TAPSH3 0
191
192 #define PAR96_SCRAM_TAP1 0x20000 /* X^17 */
193 #define PAR96_SCRAM_TAPN 0x00021 /* X^0+X^5 */
194
195 /* --------------------------------------------------------------------- */
196
197 static __inline__ void par96_tx(struct net_device *dev, struct baycom_state *bc)
198 {
199         int i;
200         unsigned int data = hdlcdrv_getbits(&bc->hdrv);
201         struct parport *pp = bc->pdev->port;
202
203         for(i = 0; i < PAR96_BURSTBITS; i++, data >>= 1) {
204                 unsigned char val = PAR97_POWER;
205                 bc->modem.par96.scram = ((bc->modem.par96.scram << 1) |
206                                          (bc->modem.par96.scram & 1));
207                 if (!(data & 1))
208                         bc->modem.par96.scram ^= 1;
209                 if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 1))
210                         bc->modem.par96.scram ^=
211                                 (PAR96_SCRAM_TAPN << 1);
212                 if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 2))
213                         val |= PAR96_TXBIT;
214                 pp->ops->write_data(pp, val);
215                 pp->ops->write_data(pp, val | PAR96_BURST);
216         }
217 }
218
219 /* --------------------------------------------------------------------- */
220
221 static __inline__ void par96_rx(struct net_device *dev, struct baycom_state *bc)
222 {
223         int i;
224         unsigned int data, mask, mask2, descx;
225         struct parport *pp = bc->pdev->port;
226
227         /*
228          * do receiver; differential decode and descramble on the fly
229          */
230         for(data = i = 0; i < PAR96_BURSTBITS; i++) {
231                 bc->modem.par96.descram = (bc->modem.par96.descram << 1);
232                 if (pp->ops->read_status(pp) & PAR96_RXBIT)
233                         bc->modem.par96.descram |= 1;
234                 descx = bc->modem.par96.descram ^
235                         (bc->modem.par96.descram >> 1);
236                 /* now the diff decoded data is inverted in descram */
237                 pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT);
238                 descx ^= ((descx >> PAR96_DESCRAM_TAPSH1) ^
239                           (descx >> PAR96_DESCRAM_TAPSH2));
240                 data >>= 1;
241                 if (!(descx & 1))
242                         data |= 0x8000;
243                 pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT | PAR96_BURST);
244         }
245         hdlcdrv_putbits(&bc->hdrv, data);
246         /*
247          * do DCD algorithm
248          */
249         if (bc->options & BAYCOM_OPTIONS_SOFTDCD) {
250                 bc->modem.par96.dcd_shreg = (bc->modem.par96.dcd_shreg >> 16)
251                         | (data << 16);
252                 /* search for flags and set the dcd counter appropriately */
253                 for(mask = 0x1fe00, mask2 = 0xfc00, i = 0;
254                     i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
255                         if ((bc->modem.par96.dcd_shreg & mask) == mask2)
256                                 bc->modem.par96.dcd_count = HDLCDRV_MAXFLEN+4;
257                 /* check for abort/noise sequences */
258                 for(mask = 0x1fe00, mask2 = 0x1fe00, i = 0;
259                     i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
260                         if (((bc->modem.par96.dcd_shreg & mask) == mask2) &&
261                             (bc->modem.par96.dcd_count >= 0))
262                                 bc->modem.par96.dcd_count -= HDLCDRV_MAXFLEN-10;
263                 /* decrement and set the dcd variable */
264                 if (bc->modem.par96.dcd_count >= 0)
265                         bc->modem.par96.dcd_count -= 2;
266                 hdlcdrv_setdcd(&bc->hdrv, bc->modem.par96.dcd_count > 0);
267         } else {
268                 hdlcdrv_setdcd(&bc->hdrv, !!(pp->ops->read_status(pp) & PAR96_DCD));
269         }
270 }
271
272 /* --------------------------------------------------------------------- */
273
274 static void par96_interrupt(int irq, void *dev_id, struct pt_regs *regs)
275 {
276         struct net_device *dev = (struct net_device *)dev_id;
277         struct baycom_state *bc = netdev_priv(dev);
278
279         baycom_int_freq(bc);
280         /*
281          * check if transmitter active
282          */
283         if (hdlcdrv_ptt(&bc->hdrv))
284                 par96_tx(dev, bc);
285         else {
286                 par96_rx(dev, bc);
287                 if (--bc->modem.arb_divider <= 0) {
288                         bc->modem.arb_divider = 6;
289                         local_irq_enable();
290                         hdlcdrv_arbitrate(dev, &bc->hdrv);
291                 }
292         }
293         local_irq_enable();
294         hdlcdrv_transmitter(dev, &bc->hdrv);
295         hdlcdrv_receiver(dev, &bc->hdrv);
296         local_irq_disable();
297 }
298
299 /* --------------------------------------------------------------------- */
300
301 static void par96_wakeup(void *handle)
302 {
303         struct net_device *dev = (struct net_device *)handle;
304         struct baycom_state *bc = netdev_priv(dev);
305
306         printk(KERN_DEBUG "baycom_par: %s: why am I being woken up?\n", dev->name);
307         if (!parport_claim(bc->pdev))
308                 printk(KERN_DEBUG "baycom_par: %s: I'm broken.\n", dev->name);
309 }
310
311 /* --------------------------------------------------------------------- */
312
313 static int par96_open(struct net_device *dev)
314 {
315         struct baycom_state *bc = netdev_priv(dev);
316         struct parport *pp;
317
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;
324         }
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;
329         }
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;
334         }
335         memset(&bc->modem, 0, sizeof(bc->modem));
336         bc->hdrv.par.bitrate = 9600;
337         bc->pdev = parport_register_device(pp, dev->name, NULL, par96_wakeup, 
338                                  par96_interrupt, PARPORT_DEV_EXCL, dev);
339         parport_put_port(pp);
340         if (!bc->pdev) {
341                 printk(KERN_ERR "baycom_par: cannot register parport at 0x%lx\n", dev->base_addr);
342                 return -ENXIO;
343         }
344         if (parport_claim(bc->pdev)) {
345                 printk(KERN_ERR "baycom_par: parport at 0x%lx busy\n", pp->base);
346                 parport_unregister_device(bc->pdev);
347                 return -EBUSY;
348         }
349         pp = bc->pdev->port;
350         dev->irq = pp->irq;
351         pp->ops->data_forward(pp);
352         bc->hdrv.par.bitrate = 9600;
353         pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER); /* switch off PTT */
354         pp->ops->enable_irq(pp);
355         printk(KERN_INFO "%s: par96 at iobase 0x%lx irq %u options 0x%x\n",
356                bc_drvname, dev->base_addr, dev->irq, bc->options);
357         return 0;
358 }
359
360 /* --------------------------------------------------------------------- */
361
362 static int par96_close(struct net_device *dev)
363 {
364         struct baycom_state *bc = netdev_priv(dev);
365         struct parport *pp;
366
367         if (!dev || !bc)
368                 return -EINVAL;
369         pp = bc->pdev->port;
370         /* disable interrupt */
371         pp->ops->disable_irq(pp);
372         /* switch off PTT */
373         pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER);
374         parport_release(bc->pdev);
375         parport_unregister_device(bc->pdev);
376         printk(KERN_INFO "%s: close par96 at iobase 0x%lx irq %u\n",
377                bc_drvname, dev->base_addr, dev->irq);
378         return 0;
379 }
380
381 /* --------------------------------------------------------------------- */
382 /*
383  * ===================== hdlcdrv driver interface =========================
384  */
385
386 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
387                         struct hdlcdrv_ioctl *hi, int cmd);
388
389 /* --------------------------------------------------------------------- */
390
391 static struct hdlcdrv_ops par96_ops = {
392         .drvname = bc_drvname,
393         .drvinfo = bc_drvinfo,
394         .open    = par96_open,
395         .close   = par96_close,
396         .ioctl   = baycom_ioctl
397 };
398
399 /* --------------------------------------------------------------------- */
400
401 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
402 {
403         if (!strncmp(modestr, "picpar", 6))
404                 bc->options = 0;
405         else if (!strncmp(modestr, "par96", 5))
406                 bc->options = BAYCOM_OPTIONS_SOFTDCD;
407         else
408                 bc->options = !!strchr(modestr, '*');
409         return 0;
410 }
411
412 /* --------------------------------------------------------------------- */
413
414 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
415                         struct hdlcdrv_ioctl *hi, int cmd)
416 {
417         struct baycom_state *bc;
418         struct baycom_ioctl bi;
419
420         if (!dev)
421                 return -EINVAL;
422
423         bc = netdev_priv(dev);
424         BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
425
426         if (cmd != SIOCDEVPRIVATE)
427                 return -ENOIOCTLCMD;
428         switch (hi->cmd) {
429         default:
430                 break;
431
432         case HDLCDRVCTL_GETMODE:
433                 strcpy(hi->data.modename, bc->options ? "par96" : "picpar");
434                 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
435                         return -EFAULT;
436                 return 0;
437
438         case HDLCDRVCTL_SETMODE:
439                 if (netif_running(dev) || !capable(CAP_NET_ADMIN))
440                         return -EACCES;
441                 hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
442                 return baycom_setmode(bc, hi->data.modename);
443
444         case HDLCDRVCTL_MODELIST:
445                 strcpy(hi->data.modename, "par96,picpar");
446                 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
447                         return -EFAULT;
448                 return 0;
449
450         case HDLCDRVCTL_MODEMPARMASK:
451                 return HDLCDRV_PARMASK_IOBASE;
452
453         }
454
455         if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
456                 return -EFAULT;
457         switch (bi.cmd) {
458         default:
459                 return -ENOIOCTLCMD;
460
461 #ifdef BAYCOM_DEBUG
462         case BAYCOMCTL_GETDEBUG:
463                 bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
464                 bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
465                 bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
466                 break;
467 #endif /* BAYCOM_DEBUG */
468
469         }
470         if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
471                 return -EFAULT;
472         return 0;
473
474 }
475
476 /* --------------------------------------------------------------------- */
477
478 /*
479  * command line settable parameters
480  */
481 static const char *mode[NR_PORTS] = { "picpar", };
482 static int iobase[NR_PORTS] = { 0x378, };
483
484 module_param_array(mode, charp, NULL, 0);
485 MODULE_PARM_DESC(mode, "baycom operating mode; eg. par96 or picpar");
486 module_param_array(iobase, int, NULL, 0);
487 MODULE_PARM_DESC(iobase, "baycom io base address");
488
489 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
490 MODULE_DESCRIPTION("Baycom par96 and picpar amateur radio modem driver");
491 MODULE_LICENSE("GPL");
492
493 /* --------------------------------------------------------------------- */
494
495 static int __init init_baycompar(void)
496 {
497         int i, found = 0;
498         char set_hw = 1;
499
500         printk(bc_drvinfo);
501         /*
502          * register net devices
503          */
504         for (i = 0; i < NR_PORTS; i++) {
505                 struct net_device *dev;
506                 struct baycom_state *bc;
507                 char ifname[IFNAMSIZ];
508
509                 sprintf(ifname, "bcp%d", i);
510
511                 if (!mode[i])
512                         set_hw = 0;
513                 if (!set_hw)
514                         iobase[i] = 0;
515
516                 dev = hdlcdrv_register(&par96_ops,
517                                        sizeof(struct baycom_state),
518                                        ifname, iobase[i], 0, 0);
519                 if (IS_ERR(dev)) 
520                         break;
521
522                 bc = netdev_priv(dev);
523                 if (set_hw && baycom_setmode(bc, mode[i]))
524                         set_hw = 0;
525                 found++;
526                 baycom_device[i] = dev;
527         }
528
529         if (!found)
530                 return -ENXIO;
531         return 0;
532 }
533
534 static void __exit cleanup_baycompar(void)
535 {
536         int i;
537
538         for(i = 0; i < NR_PORTS; i++) {
539                 struct net_device *dev = baycom_device[i];
540
541                 if (dev)
542                         hdlcdrv_unregister(dev);
543         }
544 }
545
546 module_init(init_baycompar);
547 module_exit(cleanup_baycompar);
548
549 /* --------------------------------------------------------------------- */
550
551 #ifndef MODULE
552
553 /*
554  * format: baycom_par=io,mode
555  * mode: par96,picpar
556  */
557
558 static int __init baycom_par_setup(char *str)
559 {
560         static unsigned nr_dev;
561         int ints[2];
562
563         if (nr_dev >= NR_PORTS)
564                 return 0;
565         str = get_options(str, 2, ints);
566         if (ints[0] < 1)
567                 return 0;
568         mode[nr_dev] = str;
569         iobase[nr_dev] = ints[1];
570         nr_dev++;
571         return 1;
572 }
573
574 __setup("baycom_par=", baycom_par_setup);
575
576 #endif /* MODULE */
577 /* --------------------------------------------------------------------- */