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