baycom_ser_hdx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*****************************************************************************/
  2. /*
  3. * baycom_ser_hdx.c -- baycom ser12 halfduplex 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. * ser12: This is a very simple 1200 baud AFSK modem. The modem consists only
  29. * of a modulator/demodulator chip, usually a TI TCM3105. The computer
  30. * is responsible for regenerating the receiver bit clock, as well as
  31. * for handling the HDLC protocol. The modem connects to a serial port,
  32. * hence the name. Since the serial port is not used as an async serial
  33. * port, the kernel driver for serial ports cannot be used, and this
  34. * driver only supports standard serial hardware (8250, 16450, 16550A)
  35. *
  36. *
  37. * Command line options (insmod command line)
  38. *
  39. * mode ser12 hardware DCD
  40. * ser12* software DCD
  41. * ser12@ hardware/software DCD, i.e. no explicit DCD signal but hardware
  42. * mutes audio input to the modem
  43. * ser12+ hardware DCD, inverted signal at DCD pin
  44. * iobase base address of the port; common values are 0x3f8, 0x2f8, 0x3e8, 0x2e8
  45. * irq interrupt line of the port; common values are 4,3
  46. *
  47. *
  48. * History:
  49. * 0.1 26.06.1996 Adapted from baycom.c and made network driver interface
  50. * 18.10.1996 Changed to new user space access routines (copy_{to,from}_user)
  51. * 0.3 26.04.1997 init code/data tagged
  52. * 0.4 08.07.1997 alternative ser12 decoding algorithm (uses delta CTS ints)
  53. * 0.5 11.11.1997 ser12/par96 split into separate files
  54. * 0.6 14.04.1998 cleanups
  55. * 0.7 03.08.1999 adapt to Linus' new __setup/__initcall
  56. * 0.8 10.08.1999 use module_init/module_exit
  57. * 0.9 12.02.2000 adapted to softnet driver interface
  58. * 0.10 03.07.2000 fix interface name handling
  59. */
  60. /*****************************************************************************/
  61. #include <linux/capability.h>
  62. #include <linux/module.h>
  63. #include <linux/ioport.h>
  64. #include <linux/string.h>
  65. #include <linux/init.h>
  66. #include <linux/interrupt.h>
  67. #include <asm/uaccess.h>
  68. #include <asm/io.h>
  69. #include <linux/hdlcdrv.h>
  70. #include <linux/baycom.h>
  71. #include <linux/jiffies.h>
  72. /* --------------------------------------------------------------------- */
  73. #define BAYCOM_DEBUG
  74. /* --------------------------------------------------------------------- */
  75. static const char bc_drvname[] = "baycom_ser_hdx";
  76. static const char bc_drvinfo[] = KERN_INFO "baycom_ser_hdx: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
  77. "baycom_ser_hdx: version 0.10\n";
  78. /* --------------------------------------------------------------------- */
  79. #define NR_PORTS 4
  80. static struct net_device *baycom_device[NR_PORTS];
  81. /* --------------------------------------------------------------------- */
  82. #define RBR(iobase) (iobase+0)
  83. #define THR(iobase) (iobase+0)
  84. #define IER(iobase) (iobase+1)
  85. #define IIR(iobase) (iobase+2)
  86. #define FCR(iobase) (iobase+2)
  87. #define LCR(iobase) (iobase+3)
  88. #define MCR(iobase) (iobase+4)
  89. #define LSR(iobase) (iobase+5)
  90. #define MSR(iobase) (iobase+6)
  91. #define SCR(iobase) (iobase+7)
  92. #define DLL(iobase) (iobase+0)
  93. #define DLM(iobase) (iobase+1)
  94. #define SER12_EXTENT 8
  95. /* ---------------------------------------------------------------------- */
  96. /*
  97. * Information that need to be kept for each board.
  98. */
  99. struct baycom_state {
  100. struct hdlcdrv_state hdrv;
  101. int opt_dcd;
  102. struct modem_state {
  103. short arb_divider;
  104. unsigned char flags;
  105. unsigned int shreg;
  106. struct modem_state_ser12 {
  107. unsigned char tx_bit;
  108. int dcd_sum0, dcd_sum1, dcd_sum2;
  109. unsigned char last_sample;
  110. unsigned char last_rxbit;
  111. unsigned int dcd_shreg;
  112. unsigned int dcd_time;
  113. unsigned int bit_pll;
  114. unsigned char interm_sample;
  115. } ser12;
  116. } modem;
  117. #ifdef BAYCOM_DEBUG
  118. struct debug_vals {
  119. unsigned long last_jiffies;
  120. unsigned cur_intcnt;
  121. unsigned last_intcnt;
  122. int cur_pllcorr;
  123. int last_pllcorr;
  124. } debug_vals;
  125. #endif /* BAYCOM_DEBUG */
  126. };
  127. /* --------------------------------------------------------------------- */
  128. static inline void baycom_int_freq(struct baycom_state *bc)
  129. {
  130. #ifdef BAYCOM_DEBUG
  131. unsigned long cur_jiffies = jiffies;
  132. /*
  133. * measure the interrupt frequency
  134. */
  135. bc->debug_vals.cur_intcnt++;
  136. if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
  137. bc->debug_vals.last_jiffies = cur_jiffies;
  138. bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
  139. bc->debug_vals.cur_intcnt = 0;
  140. bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
  141. bc->debug_vals.cur_pllcorr = 0;
  142. }
  143. #endif /* BAYCOM_DEBUG */
  144. }
  145. /* --------------------------------------------------------------------- */
  146. /*
  147. * ===================== SER12 specific routines =========================
  148. */
  149. static inline void ser12_set_divisor(struct net_device *dev,
  150. unsigned char divisor)
  151. {
  152. outb(0x81, LCR(dev->base_addr)); /* DLAB = 1 */
  153. outb(divisor, DLL(dev->base_addr));
  154. outb(0, DLM(dev->base_addr));
  155. outb(0x01, LCR(dev->base_addr)); /* word length = 6 */
  156. /*
  157. * make sure the next interrupt is generated;
  158. * 0 must be used to power the modem; the modem draws its
  159. * power from the TxD line
  160. */
  161. outb(0x00, THR(dev->base_addr));
  162. /*
  163. * it is important not to set the divider while transmitting;
  164. * this reportedly makes some UARTs generating interrupts
  165. * in the hundredthousands per second region
  166. * Reported by: Ignacio.Arenaza@studi.epfl.ch (Ignacio Arenaza Nuno)
  167. */
  168. }
  169. /* --------------------------------------------------------------------- */
  170. /*
  171. * must call the TX arbitrator every 10ms
  172. */
  173. #define SER12_ARB_DIVIDER(bc) (bc->opt_dcd ? 24 : 36)
  174. #define SER12_DCD_INTERVAL(bc) (bc->opt_dcd ? 12 : 240)
  175. static inline void ser12_tx(struct net_device *dev, struct baycom_state *bc)
  176. {
  177. /* one interrupt per channel bit */
  178. ser12_set_divisor(dev, 12);
  179. /*
  180. * first output the last bit (!) then call HDLC transmitter,
  181. * since this may take quite long
  182. */
  183. outb(0x0e | (!!bc->modem.ser12.tx_bit), MCR(dev->base_addr));
  184. if (bc->modem.shreg <= 1)
  185. bc->modem.shreg = 0x10000 | hdlcdrv_getbits(&bc->hdrv);
  186. bc->modem.ser12.tx_bit = !(bc->modem.ser12.tx_bit ^
  187. (bc->modem.shreg & 1));
  188. bc->modem.shreg >>= 1;
  189. }
  190. /* --------------------------------------------------------------------- */
  191. static inline void ser12_rx(struct net_device *dev, struct baycom_state *bc)
  192. {
  193. unsigned char cur_s;
  194. /*
  195. * do demodulator
  196. */
  197. cur_s = inb(MSR(dev->base_addr)) & 0x10; /* the CTS line */
  198. hdlcdrv_channelbit(&bc->hdrv, cur_s);
  199. bc->modem.ser12.dcd_shreg = (bc->modem.ser12.dcd_shreg << 1) |
  200. (cur_s != bc->modem.ser12.last_sample);
  201. bc->modem.ser12.last_sample = cur_s;
  202. if(bc->modem.ser12.dcd_shreg & 1) {
  203. if (!bc->opt_dcd) {
  204. unsigned int dcdspos, dcdsneg;
  205. dcdspos = dcdsneg = 0;
  206. dcdspos += ((bc->modem.ser12.dcd_shreg >> 1) & 1);
  207. if (!(bc->modem.ser12.dcd_shreg & 0x7ffffffe))
  208. dcdspos += 2;
  209. dcdsneg += ((bc->modem.ser12.dcd_shreg >> 2) & 1);
  210. dcdsneg += ((bc->modem.ser12.dcd_shreg >> 3) & 1);
  211. dcdsneg += ((bc->modem.ser12.dcd_shreg >> 4) & 1);
  212. bc->modem.ser12.dcd_sum0 += 16*dcdspos - dcdsneg;
  213. } else
  214. bc->modem.ser12.dcd_sum0--;
  215. }
  216. if(!bc->modem.ser12.dcd_time) {
  217. hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 +
  218. bc->modem.ser12.dcd_sum1 +
  219. bc->modem.ser12.dcd_sum2) < 0);
  220. bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
  221. bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
  222. /* offset to ensure DCD off on silent input */
  223. bc->modem.ser12.dcd_sum0 = 2;
  224. bc->modem.ser12.dcd_time = SER12_DCD_INTERVAL(bc);
  225. }
  226. bc->modem.ser12.dcd_time--;
  227. if (!bc->opt_dcd) {
  228. /*
  229. * PLL code for the improved software DCD algorithm
  230. */
  231. if (bc->modem.ser12.interm_sample) {
  232. /*
  233. * intermediate sample; set timing correction to normal
  234. */
  235. ser12_set_divisor(dev, 4);
  236. } else {
  237. /*
  238. * do PLL correction and call HDLC receiver
  239. */
  240. switch (bc->modem.ser12.dcd_shreg & 7) {
  241. case 1: /* transition too late */
  242. ser12_set_divisor(dev, 5);
  243. #ifdef BAYCOM_DEBUG
  244. bc->debug_vals.cur_pllcorr++;
  245. #endif /* BAYCOM_DEBUG */
  246. break;
  247. case 4: /* transition too early */
  248. ser12_set_divisor(dev, 3);
  249. #ifdef BAYCOM_DEBUG
  250. bc->debug_vals.cur_pllcorr--;
  251. #endif /* BAYCOM_DEBUG */
  252. break;
  253. default:
  254. ser12_set_divisor(dev, 4);
  255. break;
  256. }
  257. bc->modem.shreg >>= 1;
  258. if (bc->modem.ser12.last_sample ==
  259. bc->modem.ser12.last_rxbit)
  260. bc->modem.shreg |= 0x10000;
  261. bc->modem.ser12.last_rxbit =
  262. bc->modem.ser12.last_sample;
  263. }
  264. if (++bc->modem.ser12.interm_sample >= 3)
  265. bc->modem.ser12.interm_sample = 0;
  266. /*
  267. * DCD stuff
  268. */
  269. if (bc->modem.ser12.dcd_shreg & 1) {
  270. unsigned int dcdspos, dcdsneg;
  271. dcdspos = dcdsneg = 0;
  272. dcdspos += ((bc->modem.ser12.dcd_shreg >> 1) & 1);
  273. dcdspos += (!(bc->modem.ser12.dcd_shreg & 0x7ffffffe))
  274. << 1;
  275. dcdsneg += ((bc->modem.ser12.dcd_shreg >> 2) & 1);
  276. dcdsneg += ((bc->modem.ser12.dcd_shreg >> 3) & 1);
  277. dcdsneg += ((bc->modem.ser12.dcd_shreg >> 4) & 1);
  278. bc->modem.ser12.dcd_sum0 += 16*dcdspos - dcdsneg;
  279. }
  280. } else {
  281. /*
  282. * PLL algorithm for the hardware squelch DCD algorithm
  283. */
  284. if (bc->modem.ser12.interm_sample) {
  285. /*
  286. * intermediate sample; set timing correction to normal
  287. */
  288. ser12_set_divisor(dev, 6);
  289. } else {
  290. /*
  291. * do PLL correction and call HDLC receiver
  292. */
  293. switch (bc->modem.ser12.dcd_shreg & 3) {
  294. case 1: /* transition too late */
  295. ser12_set_divisor(dev, 7);
  296. #ifdef BAYCOM_DEBUG
  297. bc->debug_vals.cur_pllcorr++;
  298. #endif /* BAYCOM_DEBUG */
  299. break;
  300. case 2: /* transition too early */
  301. ser12_set_divisor(dev, 5);
  302. #ifdef BAYCOM_DEBUG
  303. bc->debug_vals.cur_pllcorr--;
  304. #endif /* BAYCOM_DEBUG */
  305. break;
  306. default:
  307. ser12_set_divisor(dev, 6);
  308. break;
  309. }
  310. bc->modem.shreg >>= 1;
  311. if (bc->modem.ser12.last_sample ==
  312. bc->modem.ser12.last_rxbit)
  313. bc->modem.shreg |= 0x10000;
  314. bc->modem.ser12.last_rxbit =
  315. bc->modem.ser12.last_sample;
  316. }
  317. bc->modem.ser12.interm_sample = !bc->modem.ser12.interm_sample;
  318. /*
  319. * DCD stuff
  320. */
  321. bc->modem.ser12.dcd_sum0 -= (bc->modem.ser12.dcd_shreg & 1);
  322. }
  323. outb(0x0d, MCR(dev->base_addr)); /* transmitter off */
  324. if (bc->modem.shreg & 1) {
  325. hdlcdrv_putbits(&bc->hdrv, bc->modem.shreg >> 1);
  326. bc->modem.shreg = 0x10000;
  327. }
  328. if(!bc->modem.ser12.dcd_time) {
  329. if (bc->opt_dcd & 1)
  330. hdlcdrv_setdcd(&bc->hdrv, !((inb(MSR(dev->base_addr)) ^ bc->opt_dcd) & 0x80));
  331. else
  332. hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 +
  333. bc->modem.ser12.dcd_sum1 +
  334. bc->modem.ser12.dcd_sum2) < 0);
  335. bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
  336. bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
  337. /* offset to ensure DCD off on silent input */
  338. bc->modem.ser12.dcd_sum0 = 2;
  339. bc->modem.ser12.dcd_time = SER12_DCD_INTERVAL(bc);
  340. }
  341. bc->modem.ser12.dcd_time--;
  342. }
  343. /* --------------------------------------------------------------------- */
  344. static irqreturn_t ser12_interrupt(int irq, void *dev_id)
  345. {
  346. struct net_device *dev = (struct net_device *)dev_id;
  347. struct baycom_state *bc = netdev_priv(dev);
  348. unsigned char iir;
  349. if (!dev || !bc || bc->hdrv.magic != HDLCDRV_MAGIC)
  350. return IRQ_NONE;
  351. /* fast way out */
  352. if ((iir = inb(IIR(dev->base_addr))) & 1)
  353. return IRQ_NONE;
  354. baycom_int_freq(bc);
  355. do {
  356. switch (iir & 6) {
  357. case 6:
  358. inb(LSR(dev->base_addr));
  359. break;
  360. case 4:
  361. inb(RBR(dev->base_addr));
  362. break;
  363. case 2:
  364. /*
  365. * check if transmitter active
  366. */
  367. if (hdlcdrv_ptt(&bc->hdrv))
  368. ser12_tx(dev, bc);
  369. else {
  370. ser12_rx(dev, bc);
  371. bc->modem.arb_divider--;
  372. }
  373. outb(0x00, THR(dev->base_addr));
  374. break;
  375. default:
  376. inb(MSR(dev->base_addr));
  377. break;
  378. }
  379. iir = inb(IIR(dev->base_addr));
  380. } while (!(iir & 1));
  381. if (bc->modem.arb_divider <= 0) {
  382. bc->modem.arb_divider = SER12_ARB_DIVIDER(bc);
  383. local_irq_enable();
  384. hdlcdrv_arbitrate(dev, &bc->hdrv);
  385. }
  386. local_irq_enable();
  387. hdlcdrv_transmitter(dev, &bc->hdrv);
  388. hdlcdrv_receiver(dev, &bc->hdrv);
  389. local_irq_disable();
  390. return IRQ_HANDLED;
  391. }
  392. /* --------------------------------------------------------------------- */
  393. enum uart { c_uart_unknown, c_uart_8250,
  394. c_uart_16450, c_uart_16550, c_uart_16550A};
  395. static const char *uart_str[] = {
  396. "unknown", "8250", "16450", "16550", "16550A"
  397. };
  398. static enum uart ser12_check_uart(unsigned int iobase)
  399. {
  400. unsigned char b1,b2,b3;
  401. enum uart u;
  402. enum uart uart_tab[] =
  403. { c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
  404. b1 = inb(MCR(iobase));
  405. outb(b1 | 0x10, MCR(iobase)); /* loopback mode */
  406. b2 = inb(MSR(iobase));
  407. outb(0x1a, MCR(iobase));
  408. b3 = inb(MSR(iobase)) & 0xf0;
  409. outb(b1, MCR(iobase)); /* restore old values */
  410. outb(b2, MSR(iobase));
  411. if (b3 != 0x90)
  412. return c_uart_unknown;
  413. inb(RBR(iobase));
  414. inb(RBR(iobase));
  415. outb(0x01, FCR(iobase)); /* enable FIFOs */
  416. u = uart_tab[(inb(IIR(iobase)) >> 6) & 3];
  417. if (u == c_uart_16450) {
  418. outb(0x5a, SCR(iobase));
  419. b1 = inb(SCR(iobase));
  420. outb(0xa5, SCR(iobase));
  421. b2 = inb(SCR(iobase));
  422. if ((b1 != 0x5a) || (b2 != 0xa5))
  423. u = c_uart_8250;
  424. }
  425. return u;
  426. }
  427. /* --------------------------------------------------------------------- */
  428. static int ser12_open(struct net_device *dev)
  429. {
  430. struct baycom_state *bc = netdev_priv(dev);
  431. enum uart u;
  432. if (!dev || !bc)
  433. return -ENXIO;
  434. if (!dev->base_addr || dev->base_addr > 0x1000-SER12_EXTENT ||
  435. dev->irq < 2 || dev->irq > 15)
  436. return -ENXIO;
  437. if (!request_region(dev->base_addr, SER12_EXTENT, "baycom_ser12"))
  438. return -EACCES;
  439. memset(&bc->modem, 0, sizeof(bc->modem));
  440. bc->hdrv.par.bitrate = 1200;
  441. if ((u = ser12_check_uart(dev->base_addr)) == c_uart_unknown) {
  442. release_region(dev->base_addr, SER12_EXTENT);
  443. return -EIO;
  444. }
  445. outb(0, FCR(dev->base_addr)); /* disable FIFOs */
  446. outb(0x0d, MCR(dev->base_addr));
  447. outb(0, IER(dev->base_addr));
  448. if (request_irq(dev->irq, ser12_interrupt, IRQF_SHARED,
  449. "baycom_ser12", dev)) {
  450. release_region(dev->base_addr, SER12_EXTENT);
  451. return -EBUSY;
  452. }
  453. /*
  454. * enable transmitter empty interrupt
  455. */
  456. outb(2, IER(dev->base_addr));
  457. /*
  458. * set the SIO to 6 Bits/character and 19200 or 28800 baud, so that
  459. * we get exactly (hopefully) 2 or 3 interrupts per radio symbol,
  460. * depending on the usage of the software DCD routine
  461. */
  462. ser12_set_divisor(dev, bc->opt_dcd ? 6 : 4);
  463. printk(KERN_INFO "%s: ser12 at iobase 0x%lx irq %u uart %s\n",
  464. bc_drvname, dev->base_addr, dev->irq, uart_str[u]);
  465. return 0;
  466. }
  467. /* --------------------------------------------------------------------- */
  468. static int ser12_close(struct net_device *dev)
  469. {
  470. struct baycom_state *bc = netdev_priv(dev);
  471. if (!dev || !bc)
  472. return -EINVAL;
  473. /*
  474. * disable interrupts
  475. */
  476. outb(0, IER(dev->base_addr));
  477. outb(1, MCR(dev->base_addr));
  478. free_irq(dev->irq, dev);
  479. release_region(dev->base_addr, SER12_EXTENT);
  480. printk(KERN_INFO "%s: close ser12 at iobase 0x%lx irq %u\n",
  481. bc_drvname, dev->base_addr, dev->irq);
  482. return 0;
  483. }
  484. /* --------------------------------------------------------------------- */
  485. /*
  486. * ===================== hdlcdrv driver interface =========================
  487. */
  488. /* --------------------------------------------------------------------- */
  489. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
  490. struct hdlcdrv_ioctl *hi, int cmd);
  491. /* --------------------------------------------------------------------- */
  492. static struct hdlcdrv_ops ser12_ops = {
  493. .drvname = bc_drvname,
  494. .drvinfo = bc_drvinfo,
  495. .open = ser12_open,
  496. .close = ser12_close,
  497. .ioctl = baycom_ioctl,
  498. };
  499. /* --------------------------------------------------------------------- */
  500. static int baycom_setmode(struct baycom_state *bc, const char *modestr)
  501. {
  502. if (strchr(modestr, '*'))
  503. bc->opt_dcd = 0;
  504. else if (strchr(modestr, '+'))
  505. bc->opt_dcd = -1;
  506. else if (strchr(modestr, '@'))
  507. bc->opt_dcd = -2;
  508. else
  509. bc->opt_dcd = 1;
  510. return 0;
  511. }
  512. /* --------------------------------------------------------------------- */
  513. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
  514. struct hdlcdrv_ioctl *hi, int cmd)
  515. {
  516. struct baycom_state *bc;
  517. struct baycom_ioctl bi;
  518. if (!dev)
  519. return -EINVAL;
  520. bc = netdev_priv(dev);
  521. BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
  522. if (cmd != SIOCDEVPRIVATE)
  523. return -ENOIOCTLCMD;
  524. switch (hi->cmd) {
  525. default:
  526. break;
  527. case HDLCDRVCTL_GETMODE:
  528. strcpy(hi->data.modename, "ser12");
  529. if (bc->opt_dcd <= 0)
  530. strcat(hi->data.modename, (!bc->opt_dcd) ? "*" : (bc->opt_dcd == -2) ? "@" : "+");
  531. if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
  532. return -EFAULT;
  533. return 0;
  534. case HDLCDRVCTL_SETMODE:
  535. if (netif_running(dev) || !capable(CAP_NET_ADMIN))
  536. return -EACCES;
  537. hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
  538. return baycom_setmode(bc, hi->data.modename);
  539. case HDLCDRVCTL_MODELIST:
  540. strcpy(hi->data.modename, "ser12");
  541. if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
  542. return -EFAULT;
  543. return 0;
  544. case HDLCDRVCTL_MODEMPARMASK:
  545. return HDLCDRV_PARMASK_IOBASE | HDLCDRV_PARMASK_IRQ;
  546. }
  547. if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
  548. return -EFAULT;
  549. switch (bi.cmd) {
  550. default:
  551. return -ENOIOCTLCMD;
  552. #ifdef BAYCOM_DEBUG
  553. case BAYCOMCTL_GETDEBUG:
  554. bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
  555. bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
  556. bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
  557. break;
  558. #endif /* BAYCOM_DEBUG */
  559. }
  560. if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
  561. return -EFAULT;
  562. return 0;
  563. }
  564. /* --------------------------------------------------------------------- */
  565. /*
  566. * command line settable parameters
  567. */
  568. static char *mode[NR_PORTS] = { "ser12*", };
  569. static int iobase[NR_PORTS] = { 0x3f8, };
  570. static int irq[NR_PORTS] = { 4, };
  571. module_param_array(mode, charp, NULL, 0);
  572. MODULE_PARM_DESC(mode, "baycom operating mode; * for software DCD");
  573. module_param_array(iobase, int, NULL, 0);
  574. MODULE_PARM_DESC(iobase, "baycom io base address");
  575. module_param_array(irq, int, NULL, 0);
  576. MODULE_PARM_DESC(irq, "baycom irq number");
  577. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  578. MODULE_DESCRIPTION("Baycom ser12 half duplex amateur radio modem driver");
  579. MODULE_LICENSE("GPL");
  580. /* --------------------------------------------------------------------- */
  581. static int __init init_baycomserhdx(void)
  582. {
  583. int i, found = 0;
  584. char set_hw = 1;
  585. printk(bc_drvinfo);
  586. /*
  587. * register net devices
  588. */
  589. for (i = 0; i < NR_PORTS; i++) {
  590. struct net_device *dev;
  591. struct baycom_state *bc;
  592. char ifname[IFNAMSIZ];
  593. sprintf(ifname, "bcsh%d", i);
  594. if (!mode[i])
  595. set_hw = 0;
  596. if (!set_hw)
  597. iobase[i] = irq[i] = 0;
  598. dev = hdlcdrv_register(&ser12_ops,
  599. sizeof(struct baycom_state),
  600. ifname, iobase[i], irq[i], 0);
  601. if (IS_ERR(dev))
  602. break;
  603. bc = netdev_priv(dev);
  604. if (set_hw && baycom_setmode(bc, mode[i]))
  605. set_hw = 0;
  606. found++;
  607. baycom_device[i] = dev;
  608. }
  609. if (!found)
  610. return -ENXIO;
  611. return 0;
  612. }
  613. static void __exit cleanup_baycomserhdx(void)
  614. {
  615. int i;
  616. for(i = 0; i < NR_PORTS; i++) {
  617. struct net_device *dev = baycom_device[i];
  618. if (dev)
  619. hdlcdrv_unregister(dev);
  620. }
  621. }
  622. module_init(init_baycomserhdx);
  623. module_exit(cleanup_baycomserhdx);
  624. /* --------------------------------------------------------------------- */
  625. #ifndef MODULE
  626. /*
  627. * format: baycom_ser_hdx=io,irq,mode
  628. * mode: ser12 hardware DCD
  629. * ser12* software DCD
  630. * ser12@ hardware/software DCD, i.e. no explicit DCD signal but hardware
  631. * mutes audio input to the modem
  632. * ser12+ hardware DCD, inverted signal at DCD pin
  633. */
  634. static int __init baycom_ser_hdx_setup(char *str)
  635. {
  636. static unsigned nr_dev;
  637. int ints[3];
  638. if (nr_dev >= NR_PORTS)
  639. return 0;
  640. str = get_options(str, 3, ints);
  641. if (ints[0] < 2)
  642. return 0;
  643. mode[nr_dev] = str;
  644. iobase[nr_dev] = ints[1];
  645. irq[nr_dev] = ints[2];
  646. nr_dev++;
  647. return 1;
  648. }
  649. __setup("baycom_ser_hdx=", baycom_ser_hdx_setup);
  650. #endif /* MODULE */
  651. /* --------------------------------------------------------------------- */