baycom_par.c 16 KB

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