baycom_par.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*****************************************************************************/
  2. /*
  3. * baycom_par.c -- baycom par96 and picpar radio modem driver.
  4. *
  5. * Copyright (C) 1996-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * Please note that the GPL allows you to use the driver, NOT the radio.
  22. * In order to use the radio, you need a license from the communications
  23. * authority of your country.
  24. *
  25. *
  26. * Supported modems
  27. *
  28. * par96: This is a modem for 9600 baud FSK compatible to the G3RUH standard.
  29. * The modem does all the filtering and regenerates the receiver clock.
  30. * Data is transferred from and to the PC via a shift register.
  31. * The shift register is filled with 16 bits and an interrupt is
  32. * signalled. The PC then empties the shift register in a burst. This
  33. * modem connects to the parallel port, hence the name. The modem
  34. * leaves the implementation of the HDLC protocol and the scrambler
  35. * polynomial to the PC. This modem is no longer available (at least
  36. * from Baycom) and has been replaced by the PICPAR modem (see below).
  37. * You may however still build one from the schematics published in
  38. * cq-DL :-).
  39. *
  40. * picpar: This is a redesign of the par96 modem by Henning Rech, DF9IC. The
  41. * modem is protocol compatible to par96, but uses only three low
  42. * power ICs and can therefore be fed from the parallel port and
  43. * does not require an additional power supply. It features
  44. * built in DCD circuitry. The driver should therefore be configured
  45. * for hardware DCD.
  46. *
  47. *
  48. * Command line options (insmod command line)
  49. *
  50. * mode driver mode string. Valid choices are par96 and picpar.
  51. * iobase base address of the port; common values are 0x378, 0x278, 0x3bc
  52. *
  53. *
  54. * History:
  55. * 0.1 26.06.1996 Adapted from baycom.c and made network driver interface
  56. * 18.10.1996 Changed to new user space access routines (copy_{to,from}_user)
  57. * 0.3 26.04.1997 init code/data tagged
  58. * 0.4 08.07.1997 alternative ser12 decoding algorithm (uses delta CTS ints)
  59. * 0.5 11.11.1997 split into separate files for ser12/par96
  60. * 0.6 03.08.1999 adapt to Linus' new __setup/__initcall
  61. * removed some pre-2.2 kernel compatibility cruft
  62. * 0.7 10.08.1999 Check if parport can do SPP and is safe to access during interrupt contexts
  63. * 0.8 12.02.2000 adapted to softnet driver interface
  64. * removed direct parport access, uses parport driver methods
  65. * 0.9 03.07.2000 fix interface name handling
  66. */
  67. /*****************************************************************************/
  68. #include <linux/module.h>
  69. #include <linux/kernel.h>
  70. #include <linux/types.h>
  71. #include <linux/fcntl.h>
  72. #include <linux/interrupt.h>
  73. #include <linux/ioport.h>
  74. #include <linux/in.h>
  75. #include <linux/string.h>
  76. #include <linux/init.h>
  77. #include <linux/delay.h>
  78. #include <linux/errno.h>
  79. #include <linux/netdevice.h>
  80. #include <linux/hdlcdrv.h>
  81. #include <linux/baycom.h>
  82. #include <linux/parport.h>
  83. #include <linux/bitops.h>
  84. #include <linux/jiffies.h>
  85. #include <asm/bug.h>
  86. #include <asm/system.h>
  87. #include <asm/uaccess.h>
  88. /* --------------------------------------------------------------------- */
  89. #define BAYCOM_DEBUG
  90. /*
  91. * modem options; bit mask
  92. */
  93. #define BAYCOM_OPTIONS_SOFTDCD 1
  94. /* --------------------------------------------------------------------- */
  95. static const char bc_drvname[] = "baycom_par";
  96. static const char bc_drvinfo[] = KERN_INFO "baycom_par: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
  97. KERN_INFO "baycom_par: version 0.9 compiled " __TIME__ " " __DATE__ "\n";
  98. /* --------------------------------------------------------------------- */
  99. #define NR_PORTS 4
  100. static struct net_device *baycom_device[NR_PORTS];
  101. /* --------------------------------------------------------------------- */
  102. #define PAR96_BURSTBITS 16
  103. #define PAR96_BURST 4
  104. #define PAR96_PTT 2
  105. #define PAR96_TXBIT 1
  106. #define PAR96_ACK 0x40
  107. #define PAR96_RXBIT 0x20
  108. #define PAR96_DCD 0x10
  109. #define PAR97_POWER 0xf8
  110. /* ---------------------------------------------------------------------- */
  111. /*
  112. * Information that need to be kept for each board.
  113. */
  114. struct baycom_state {
  115. struct hdlcdrv_state hdrv;
  116. struct pardevice *pdev;
  117. unsigned int options;
  118. struct modem_state {
  119. short arb_divider;
  120. unsigned char flags;
  121. unsigned int shreg;
  122. struct modem_state_par96 {
  123. int dcd_count;
  124. unsigned int dcd_shreg;
  125. unsigned long descram;
  126. unsigned long scram;
  127. } par96;
  128. } modem;
  129. #ifdef BAYCOM_DEBUG
  130. struct debug_vals {
  131. unsigned long last_jiffies;
  132. unsigned cur_intcnt;
  133. unsigned last_intcnt;
  134. int cur_pllcorr;
  135. int last_pllcorr;
  136. } debug_vals;
  137. #endif /* BAYCOM_DEBUG */
  138. };
  139. /* --------------------------------------------------------------------- */
  140. static void __inline__ baycom_int_freq(struct baycom_state *bc)
  141. {
  142. #ifdef BAYCOM_DEBUG
  143. unsigned long cur_jiffies = jiffies;
  144. /*
  145. * measure the interrupt frequency
  146. */
  147. bc->debug_vals.cur_intcnt++;
  148. if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
  149. bc->debug_vals.last_jiffies = cur_jiffies;
  150. bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
  151. bc->debug_vals.cur_intcnt = 0;
  152. bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
  153. bc->debug_vals.cur_pllcorr = 0;
  154. }
  155. #endif /* BAYCOM_DEBUG */
  156. }
  157. /* --------------------------------------------------------------------- */
  158. /*
  159. * ===================== PAR96 specific routines =========================
  160. */
  161. #define PAR96_DESCRAM_TAP1 0x20000
  162. #define PAR96_DESCRAM_TAP2 0x01000
  163. #define PAR96_DESCRAM_TAP3 0x00001
  164. #define PAR96_DESCRAM_TAPSH1 17
  165. #define PAR96_DESCRAM_TAPSH2 12
  166. #define PAR96_DESCRAM_TAPSH3 0
  167. #define PAR96_SCRAM_TAP1 0x20000 /* X^17 */
  168. #define PAR96_SCRAM_TAPN 0x00021 /* X^0+X^5 */
  169. /* --------------------------------------------------------------------- */
  170. static __inline__ void par96_tx(struct net_device *dev, struct baycom_state *bc)
  171. {
  172. int i;
  173. unsigned int data = hdlcdrv_getbits(&bc->hdrv);
  174. struct parport *pp = bc->pdev->port;
  175. for(i = 0; i < PAR96_BURSTBITS; i++, data >>= 1) {
  176. unsigned char val = PAR97_POWER;
  177. bc->modem.par96.scram = ((bc->modem.par96.scram << 1) |
  178. (bc->modem.par96.scram & 1));
  179. if (!(data & 1))
  180. bc->modem.par96.scram ^= 1;
  181. if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 1))
  182. bc->modem.par96.scram ^=
  183. (PAR96_SCRAM_TAPN << 1);
  184. if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 2))
  185. val |= PAR96_TXBIT;
  186. pp->ops->write_data(pp, val);
  187. pp->ops->write_data(pp, val | PAR96_BURST);
  188. }
  189. }
  190. /* --------------------------------------------------------------------- */
  191. static __inline__ void par96_rx(struct net_device *dev, struct baycom_state *bc)
  192. {
  193. int i;
  194. unsigned int data, mask, mask2, descx;
  195. struct parport *pp = bc->pdev->port;
  196. /*
  197. * do receiver; differential decode and descramble on the fly
  198. */
  199. for(data = i = 0; i < PAR96_BURSTBITS; i++) {
  200. bc->modem.par96.descram = (bc->modem.par96.descram << 1);
  201. if (pp->ops->read_status(pp) & PAR96_RXBIT)
  202. bc->modem.par96.descram |= 1;
  203. descx = bc->modem.par96.descram ^
  204. (bc->modem.par96.descram >> 1);
  205. /* now the diff decoded data is inverted in descram */
  206. pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT);
  207. descx ^= ((descx >> PAR96_DESCRAM_TAPSH1) ^
  208. (descx >> PAR96_DESCRAM_TAPSH2));
  209. data >>= 1;
  210. if (!(descx & 1))
  211. data |= 0x8000;
  212. pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT | PAR96_BURST);
  213. }
  214. hdlcdrv_putbits(&bc->hdrv, data);
  215. /*
  216. * do DCD algorithm
  217. */
  218. if (bc->options & BAYCOM_OPTIONS_SOFTDCD) {
  219. bc->modem.par96.dcd_shreg = (bc->modem.par96.dcd_shreg >> 16)
  220. | (data << 16);
  221. /* search for flags and set the dcd counter appropriately */
  222. for(mask = 0x1fe00, mask2 = 0xfc00, i = 0;
  223. i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
  224. if ((bc->modem.par96.dcd_shreg & mask) == mask2)
  225. bc->modem.par96.dcd_count = HDLCDRV_MAXFLEN+4;
  226. /* check for abort/noise sequences */
  227. for(mask = 0x1fe00, mask2 = 0x1fe00, i = 0;
  228. i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
  229. if (((bc->modem.par96.dcd_shreg & mask) == mask2) &&
  230. (bc->modem.par96.dcd_count >= 0))
  231. bc->modem.par96.dcd_count -= HDLCDRV_MAXFLEN-10;
  232. /* decrement and set the dcd variable */
  233. if (bc->modem.par96.dcd_count >= 0)
  234. bc->modem.par96.dcd_count -= 2;
  235. hdlcdrv_setdcd(&bc->hdrv, bc->modem.par96.dcd_count > 0);
  236. } else {
  237. hdlcdrv_setdcd(&bc->hdrv, !!(pp->ops->read_status(pp) & PAR96_DCD));
  238. }
  239. }
  240. /* --------------------------------------------------------------------- */
  241. static void par96_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  242. {
  243. struct net_device *dev = (struct net_device *)dev_id;
  244. struct baycom_state *bc = netdev_priv(dev);
  245. baycom_int_freq(bc);
  246. /*
  247. * check if transmitter active
  248. */
  249. if (hdlcdrv_ptt(&bc->hdrv))
  250. par96_tx(dev, bc);
  251. else {
  252. par96_rx(dev, bc);
  253. if (--bc->modem.arb_divider <= 0) {
  254. bc->modem.arb_divider = 6;
  255. local_irq_enable();
  256. hdlcdrv_arbitrate(dev, &bc->hdrv);
  257. }
  258. }
  259. local_irq_enable();
  260. hdlcdrv_transmitter(dev, &bc->hdrv);
  261. hdlcdrv_receiver(dev, &bc->hdrv);
  262. local_irq_disable();
  263. }
  264. /* --------------------------------------------------------------------- */
  265. static void par96_wakeup(void *handle)
  266. {
  267. struct net_device *dev = (struct net_device *)handle;
  268. struct baycom_state *bc = netdev_priv(dev);
  269. printk(KERN_DEBUG "baycom_par: %s: why am I being woken up?\n", dev->name);
  270. if (!parport_claim(bc->pdev))
  271. printk(KERN_DEBUG "baycom_par: %s: I'm broken.\n", dev->name);
  272. }
  273. /* --------------------------------------------------------------------- */
  274. static int par96_open(struct net_device *dev)
  275. {
  276. struct baycom_state *bc = netdev_priv(dev);
  277. struct parport *pp;
  278. if (!dev || !bc)
  279. return -ENXIO;
  280. pp = parport_find_base(dev->base_addr);
  281. if (!pp) {
  282. printk(KERN_ERR "baycom_par: parport at 0x%lx unknown\n", dev->base_addr);
  283. return -ENXIO;
  284. }
  285. if (pp->irq < 0) {
  286. printk(KERN_ERR "baycom_par: parport at 0x%lx has no irq\n", pp->base);
  287. parport_put_port(pp);
  288. return -ENXIO;
  289. }
  290. if ((~pp->modes) & (PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
  291. printk(KERN_ERR "baycom_par: parport at 0x%lx cannot be used\n", pp->base);
  292. parport_put_port(pp);
  293. return -ENXIO;
  294. }
  295. memset(&bc->modem, 0, sizeof(bc->modem));
  296. bc->hdrv.par.bitrate = 9600;
  297. bc->pdev = parport_register_device(pp, dev->name, NULL, par96_wakeup,
  298. par96_interrupt, PARPORT_DEV_EXCL, dev);
  299. parport_put_port(pp);
  300. if (!bc->pdev) {
  301. printk(KERN_ERR "baycom_par: cannot register parport at 0x%lx\n", dev->base_addr);
  302. return -ENXIO;
  303. }
  304. if (parport_claim(bc->pdev)) {
  305. printk(KERN_ERR "baycom_par: parport at 0x%lx busy\n", pp->base);
  306. parport_unregister_device(bc->pdev);
  307. return -EBUSY;
  308. }
  309. pp = bc->pdev->port;
  310. dev->irq = pp->irq;
  311. pp->ops->data_forward(pp);
  312. bc->hdrv.par.bitrate = 9600;
  313. pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER); /* switch off PTT */
  314. pp->ops->enable_irq(pp);
  315. printk(KERN_INFO "%s: par96 at iobase 0x%lx irq %u options 0x%x\n",
  316. bc_drvname, dev->base_addr, dev->irq, bc->options);
  317. return 0;
  318. }
  319. /* --------------------------------------------------------------------- */
  320. static int par96_close(struct net_device *dev)
  321. {
  322. struct baycom_state *bc = netdev_priv(dev);
  323. struct parport *pp;
  324. if (!dev || !bc)
  325. return -EINVAL;
  326. pp = bc->pdev->port;
  327. /* disable interrupt */
  328. pp->ops->disable_irq(pp);
  329. /* switch off PTT */
  330. pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER);
  331. parport_release(bc->pdev);
  332. parport_unregister_device(bc->pdev);
  333. printk(KERN_INFO "%s: close par96 at iobase 0x%lx irq %u\n",
  334. bc_drvname, dev->base_addr, dev->irq);
  335. return 0;
  336. }
  337. /* --------------------------------------------------------------------- */
  338. /*
  339. * ===================== hdlcdrv driver interface =========================
  340. */
  341. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
  342. struct hdlcdrv_ioctl *hi, int cmd);
  343. /* --------------------------------------------------------------------- */
  344. static struct hdlcdrv_ops par96_ops = {
  345. .drvname = bc_drvname,
  346. .drvinfo = bc_drvinfo,
  347. .open = par96_open,
  348. .close = par96_close,
  349. .ioctl = baycom_ioctl
  350. };
  351. /* --------------------------------------------------------------------- */
  352. static int baycom_setmode(struct baycom_state *bc, const char *modestr)
  353. {
  354. if (!strncmp(modestr, "picpar", 6))
  355. bc->options = 0;
  356. else if (!strncmp(modestr, "par96", 5))
  357. bc->options = BAYCOM_OPTIONS_SOFTDCD;
  358. else
  359. bc->options = !!strchr(modestr, '*');
  360. return 0;
  361. }
  362. /* --------------------------------------------------------------------- */
  363. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
  364. struct hdlcdrv_ioctl *hi, int cmd)
  365. {
  366. struct baycom_state *bc;
  367. struct baycom_ioctl bi;
  368. if (!dev)
  369. return -EINVAL;
  370. bc = netdev_priv(dev);
  371. BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
  372. if (cmd != SIOCDEVPRIVATE)
  373. return -ENOIOCTLCMD;
  374. switch (hi->cmd) {
  375. default:
  376. break;
  377. case HDLCDRVCTL_GETMODE:
  378. strcpy(hi->data.modename, bc->options ? "par96" : "picpar");
  379. if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
  380. return -EFAULT;
  381. return 0;
  382. case HDLCDRVCTL_SETMODE:
  383. if (netif_running(dev) || !capable(CAP_NET_ADMIN))
  384. return -EACCES;
  385. hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
  386. return baycom_setmode(bc, hi->data.modename);
  387. case HDLCDRVCTL_MODELIST:
  388. strcpy(hi->data.modename, "par96,picpar");
  389. if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
  390. return -EFAULT;
  391. return 0;
  392. case HDLCDRVCTL_MODEMPARMASK:
  393. return HDLCDRV_PARMASK_IOBASE;
  394. }
  395. if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
  396. return -EFAULT;
  397. switch (bi.cmd) {
  398. default:
  399. return -ENOIOCTLCMD;
  400. #ifdef BAYCOM_DEBUG
  401. case BAYCOMCTL_GETDEBUG:
  402. bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
  403. bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
  404. bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
  405. break;
  406. #endif /* BAYCOM_DEBUG */
  407. }
  408. if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
  409. return -EFAULT;
  410. return 0;
  411. }
  412. /* --------------------------------------------------------------------- */
  413. /*
  414. * command line settable parameters
  415. */
  416. static const char *mode[NR_PORTS] = { "picpar", };
  417. static int iobase[NR_PORTS] = { 0x378, };
  418. module_param_array(mode, charp, NULL, 0);
  419. MODULE_PARM_DESC(mode, "baycom operating mode; eg. par96 or picpar");
  420. module_param_array(iobase, int, NULL, 0);
  421. MODULE_PARM_DESC(iobase, "baycom io base address");
  422. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  423. MODULE_DESCRIPTION("Baycom par96 and picpar amateur radio modem driver");
  424. MODULE_LICENSE("GPL");
  425. /* --------------------------------------------------------------------- */
  426. static int __init init_baycompar(void)
  427. {
  428. int i, found = 0;
  429. char set_hw = 1;
  430. printk(bc_drvinfo);
  431. /*
  432. * register net devices
  433. */
  434. for (i = 0; i < NR_PORTS; i++) {
  435. struct net_device *dev;
  436. struct baycom_state *bc;
  437. char ifname[IFNAMSIZ];
  438. sprintf(ifname, "bcp%d", i);
  439. if (!mode[i])
  440. set_hw = 0;
  441. if (!set_hw)
  442. iobase[i] = 0;
  443. dev = hdlcdrv_register(&par96_ops,
  444. sizeof(struct baycom_state),
  445. ifname, iobase[i], 0, 0);
  446. if (IS_ERR(dev))
  447. break;
  448. bc = netdev_priv(dev);
  449. if (set_hw && baycom_setmode(bc, mode[i]))
  450. set_hw = 0;
  451. found++;
  452. baycom_device[i] = dev;
  453. }
  454. if (!found)
  455. return -ENXIO;
  456. return 0;
  457. }
  458. static void __exit cleanup_baycompar(void)
  459. {
  460. int i;
  461. for(i = 0; i < NR_PORTS; i++) {
  462. struct net_device *dev = baycom_device[i];
  463. if (dev)
  464. hdlcdrv_unregister(dev);
  465. }
  466. }
  467. module_init(init_baycompar);
  468. module_exit(cleanup_baycompar);
  469. /* --------------------------------------------------------------------- */
  470. #ifndef MODULE
  471. /*
  472. * format: baycom_par=io,mode
  473. * mode: par96,picpar
  474. */
  475. static int __init baycom_par_setup(char *str)
  476. {
  477. static unsigned nr_dev;
  478. int ints[2];
  479. if (nr_dev >= NR_PORTS)
  480. return 0;
  481. str = get_options(str, 2, ints);
  482. if (ints[0] < 1)
  483. return 0;
  484. mode[nr_dev] = str;
  485. iobase[nr_dev] = ints[1];
  486. nr_dev++;
  487. return 1;
  488. }
  489. __setup("baycom_par=", baycom_par_setup);
  490. #endif /* MODULE */
  491. /* --------------------------------------------------------------------- */