baycom_ser_fdx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*****************************************************************************/
  2. /*
  3. * baycom_ser_fdx.c -- baycom ser12 fullduplex 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. * This modem usually draws its supply current out of the otherwise unused
  37. * TXD pin of the serial port. Thus a contiguous stream of 0x00-bytes
  38. * is transmitted to achieve a positive supply voltage.
  39. *
  40. * hsk: This is a 4800 baud FSK modem, designed for TNC use. It works fine
  41. * in 'baycom-mode' :-) In contrast to the TCM3105 modem, power is
  42. * externally supplied. So there's no need to provide the 0x00-byte-stream
  43. * when receiving or idle, which drastically reduces interrupt load.
  44. *
  45. * Command line options (insmod command line)
  46. *
  47. * mode ser# hardware DCD
  48. * ser#* software DCD
  49. * ser#+ hardware DCD, inverted signal at DCD pin
  50. * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
  51. * iobase base address of the port; common values are 0x3f8, 0x2f8, 0x3e8, 0x2e8
  52. * baud baud rate (between 300 and 4800)
  53. * irq interrupt line of the port; common values are 4,3
  54. *
  55. *
  56. * History:
  57. * 0.1 26.06.1996 Adapted from baycom.c and made network driver interface
  58. * 18.10.1996 Changed to new user space access routines (copy_{to,from}_user)
  59. * 0.3 26.04.1997 init code/data tagged
  60. * 0.4 08.07.1997 alternative ser12 decoding algorithm (uses delta CTS ints)
  61. * 0.5 11.11.1997 ser12/par96 split into separate files
  62. * 0.6 24.01.1998 Thorsten Kranzkowski, dl8bcu and Thomas Sailer:
  63. * reduced interrupt load in transmit case
  64. * reworked receiver
  65. * 0.7 03.08.1999 adapt to Linus' new __setup/__initcall
  66. * 0.8 10.08.1999 use module_init/module_exit
  67. * 0.9 12.02.2000 adapted to softnet driver interface
  68. * 0.10 03.07.2000 fix interface name handling
  69. */
  70. /*****************************************************************************/
  71. #include <linux/capability.h>
  72. #include <linux/module.h>
  73. #include <linux/ioport.h>
  74. #include <linux/string.h>
  75. #include <linux/init.h>
  76. #include <linux/interrupt.h>
  77. #include <linux/hdlcdrv.h>
  78. #include <linux/baycom.h>
  79. #include <linux/jiffies.h>
  80. #include <linux/time64.h>
  81. #include <asm/uaccess.h>
  82. #include <asm/io.h>
  83. #include <asm/irq.h>
  84. /* --------------------------------------------------------------------- */
  85. #define BAYCOM_DEBUG
  86. /* --------------------------------------------------------------------- */
  87. static const char bc_drvname[] = "baycom_ser_fdx";
  88. static const char bc_drvinfo[] = KERN_INFO "baycom_ser_fdx: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
  89. "baycom_ser_fdx: version 0.10\n";
  90. /* --------------------------------------------------------------------- */
  91. #define NR_PORTS 4
  92. static struct net_device *baycom_device[NR_PORTS];
  93. /* --------------------------------------------------------------------- */
  94. #define RBR(iobase) (iobase+0)
  95. #define THR(iobase) (iobase+0)
  96. #define IER(iobase) (iobase+1)
  97. #define IIR(iobase) (iobase+2)
  98. #define FCR(iobase) (iobase+2)
  99. #define LCR(iobase) (iobase+3)
  100. #define MCR(iobase) (iobase+4)
  101. #define LSR(iobase) (iobase+5)
  102. #define MSR(iobase) (iobase+6)
  103. #define SCR(iobase) (iobase+7)
  104. #define DLL(iobase) (iobase+0)
  105. #define DLM(iobase) (iobase+1)
  106. #define SER12_EXTENT 8
  107. /* ---------------------------------------------------------------------- */
  108. /*
  109. * Information that need to be kept for each board.
  110. */
  111. struct baycom_state {
  112. struct hdlcdrv_state hdrv;
  113. unsigned int baud, baud_us, baud_arbdiv, baud_uartdiv, baud_dcdtimeout;
  114. int opt_dcd;
  115. struct modem_state {
  116. unsigned char flags;
  117. unsigned char ptt;
  118. unsigned int shreg;
  119. struct modem_state_ser12 {
  120. unsigned char tx_bit;
  121. unsigned char last_rxbit;
  122. int dcd_sum0, dcd_sum1, dcd_sum2;
  123. int dcd_time;
  124. unsigned int pll_time;
  125. unsigned int txshreg;
  126. } ser12;
  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 inline void 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. * ===================== SER12 specific routines =========================
  159. */
  160. /* --------------------------------------------------------------------- */
  161. static inline void ser12_set_divisor(struct net_device *dev,
  162. unsigned int divisor)
  163. {
  164. outb(0x81, LCR(dev->base_addr)); /* DLAB = 1 */
  165. outb(divisor, DLL(dev->base_addr));
  166. outb(divisor >> 8, DLM(dev->base_addr));
  167. outb(0x01, LCR(dev->base_addr)); /* word length = 6 */
  168. /*
  169. * make sure the next interrupt is generated;
  170. * 0 must be used to power the modem; the modem draws its
  171. * power from the TxD line
  172. */
  173. outb(0x00, THR(dev->base_addr));
  174. /*
  175. * it is important not to set the divider while transmitting;
  176. * this reportedly makes some UARTs generating interrupts
  177. * in the hundredthousands per second region
  178. * Reported by: Ignacio.Arenaza@studi.epfl.ch (Ignacio Arenaza Nuno)
  179. */
  180. }
  181. /* --------------------------------------------------------------------- */
  182. #if 0
  183. static inline unsigned int hweight16(unsigned int w)
  184. __attribute__ ((unused));
  185. static inline unsigned int hweight8(unsigned int w)
  186. __attribute__ ((unused));
  187. static inline unsigned int hweight16(unsigned int w)
  188. {
  189. unsigned short res = (w & 0x5555) + ((w >> 1) & 0x5555);
  190. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  191. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  192. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  193. }
  194. static inline unsigned int hweight8(unsigned int w)
  195. {
  196. unsigned short res = (w & 0x55) + ((w >> 1) & 0x55);
  197. res = (res & 0x33) + ((res >> 2) & 0x33);
  198. return (res & 0x0F) + ((res >> 4) & 0x0F);
  199. }
  200. #endif
  201. /* --------------------------------------------------------------------- */
  202. static __inline__ void ser12_rx(struct net_device *dev, struct baycom_state *bc, struct timespec64 *ts, unsigned char curs)
  203. {
  204. int timediff;
  205. int bdus8 = bc->baud_us >> 3;
  206. int bdus4 = bc->baud_us >> 2;
  207. int bdus2 = bc->baud_us >> 1;
  208. timediff = 1000000 + ts->tv_nsec / NSEC_PER_USEC -
  209. bc->modem.ser12.pll_time;
  210. while (timediff >= 500000)
  211. timediff -= 1000000;
  212. while (timediff >= bdus2) {
  213. timediff -= bc->baud_us;
  214. bc->modem.ser12.pll_time += bc->baud_us;
  215. bc->modem.ser12.dcd_time--;
  216. /* first check if there is room to add a bit */
  217. if (bc->modem.shreg & 1) {
  218. hdlcdrv_putbits(&bc->hdrv, (bc->modem.shreg >> 1) ^ 0xffff);
  219. bc->modem.shreg = 0x10000;
  220. }
  221. /* add a one bit */
  222. bc->modem.shreg >>= 1;
  223. }
  224. if (bc->modem.ser12.dcd_time <= 0) {
  225. if (!bc->opt_dcd)
  226. hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 +
  227. bc->modem.ser12.dcd_sum1 +
  228. bc->modem.ser12.dcd_sum2) < 0);
  229. bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
  230. bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
  231. bc->modem.ser12.dcd_sum0 = 2; /* slight bias */
  232. bc->modem.ser12.dcd_time += 120;
  233. }
  234. if (bc->modem.ser12.last_rxbit != curs) {
  235. bc->modem.ser12.last_rxbit = curs;
  236. bc->modem.shreg |= 0x10000;
  237. /* adjust the PLL */
  238. if (timediff > 0)
  239. bc->modem.ser12.pll_time += bdus8;
  240. else
  241. bc->modem.ser12.pll_time += 1000000 - bdus8;
  242. /* update DCD */
  243. if (abs(timediff) > bdus4)
  244. bc->modem.ser12.dcd_sum0 += 4;
  245. else
  246. bc->modem.ser12.dcd_sum0--;
  247. #ifdef BAYCOM_DEBUG
  248. bc->debug_vals.cur_pllcorr = timediff;
  249. #endif /* BAYCOM_DEBUG */
  250. }
  251. while (bc->modem.ser12.pll_time >= 1000000)
  252. bc->modem.ser12.pll_time -= 1000000;
  253. }
  254. /* --------------------------------------------------------------------- */
  255. static irqreturn_t ser12_interrupt(int irq, void *dev_id)
  256. {
  257. struct net_device *dev = (struct net_device *)dev_id;
  258. struct baycom_state *bc = netdev_priv(dev);
  259. struct timespec64 ts;
  260. unsigned char iir, msr;
  261. unsigned int txcount = 0;
  262. if (!bc || bc->hdrv.magic != HDLCDRV_MAGIC)
  263. return IRQ_NONE;
  264. /* fast way out for shared irq */
  265. if ((iir = inb(IIR(dev->base_addr))) & 1)
  266. return IRQ_NONE;
  267. /* get current time */
  268. ktime_get_ts64(&ts);
  269. msr = inb(MSR(dev->base_addr));
  270. /* delta DCD */
  271. if ((msr & 8) && bc->opt_dcd)
  272. hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
  273. do {
  274. switch (iir & 6) {
  275. case 6:
  276. inb(LSR(dev->base_addr));
  277. break;
  278. case 4:
  279. inb(RBR(dev->base_addr));
  280. break;
  281. case 2:
  282. /*
  283. * make sure the next interrupt is generated;
  284. * 0 must be used to power the modem; the modem draws its
  285. * power from the TxD line
  286. */
  287. outb(0x00, THR(dev->base_addr));
  288. baycom_int_freq(bc);
  289. txcount++;
  290. /*
  291. * first output the last bit (!) then call HDLC transmitter,
  292. * since this may take quite long
  293. */
  294. if (bc->modem.ptt)
  295. outb(0x0e | (!!bc->modem.ser12.tx_bit), MCR(dev->base_addr));
  296. else
  297. outb(0x0d, MCR(dev->base_addr)); /* transmitter off */
  298. break;
  299. default:
  300. msr = inb(MSR(dev->base_addr));
  301. /* delta DCD */
  302. if ((msr & 8) && bc->opt_dcd)
  303. hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
  304. break;
  305. }
  306. iir = inb(IIR(dev->base_addr));
  307. } while (!(iir & 1));
  308. ser12_rx(dev, bc, &ts, msr & 0x10); /* CTS */
  309. if (bc->modem.ptt && txcount) {
  310. if (bc->modem.ser12.txshreg <= 1) {
  311. bc->modem.ser12.txshreg = 0x10000 | hdlcdrv_getbits(&bc->hdrv);
  312. if (!hdlcdrv_ptt(&bc->hdrv)) {
  313. ser12_set_divisor(dev, 115200/100/8);
  314. bc->modem.ptt = 0;
  315. goto end_transmit;
  316. }
  317. }
  318. bc->modem.ser12.tx_bit = !(bc->modem.ser12.tx_bit ^ (bc->modem.ser12.txshreg & 1));
  319. bc->modem.ser12.txshreg >>= 1;
  320. }
  321. end_transmit:
  322. local_irq_enable();
  323. if (!bc->modem.ptt && txcount) {
  324. hdlcdrv_arbitrate(dev, &bc->hdrv);
  325. if (hdlcdrv_ptt(&bc->hdrv)) {
  326. ser12_set_divisor(dev, bc->baud_uartdiv);
  327. bc->modem.ser12.txshreg = 1;
  328. bc->modem.ptt = 1;
  329. }
  330. }
  331. hdlcdrv_transmitter(dev, &bc->hdrv);
  332. hdlcdrv_receiver(dev, &bc->hdrv);
  333. local_irq_disable();
  334. return IRQ_HANDLED;
  335. }
  336. /* --------------------------------------------------------------------- */
  337. enum uart { c_uart_unknown, c_uart_8250,
  338. c_uart_16450, c_uart_16550, c_uart_16550A};
  339. static const char *uart_str[] = {
  340. "unknown", "8250", "16450", "16550", "16550A"
  341. };
  342. static enum uart ser12_check_uart(unsigned int iobase)
  343. {
  344. unsigned char b1,b2,b3;
  345. enum uart u;
  346. enum uart uart_tab[] =
  347. { c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
  348. b1 = inb(MCR(iobase));
  349. outb(b1 | 0x10, MCR(iobase)); /* loopback mode */
  350. b2 = inb(MSR(iobase));
  351. outb(0x1a, MCR(iobase));
  352. b3 = inb(MSR(iobase)) & 0xf0;
  353. outb(b1, MCR(iobase)); /* restore old values */
  354. outb(b2, MSR(iobase));
  355. if (b3 != 0x90)
  356. return c_uart_unknown;
  357. inb(RBR(iobase));
  358. inb(RBR(iobase));
  359. outb(0x01, FCR(iobase)); /* enable FIFOs */
  360. u = uart_tab[(inb(IIR(iobase)) >> 6) & 3];
  361. if (u == c_uart_16450) {
  362. outb(0x5a, SCR(iobase));
  363. b1 = inb(SCR(iobase));
  364. outb(0xa5, SCR(iobase));
  365. b2 = inb(SCR(iobase));
  366. if ((b1 != 0x5a) || (b2 != 0xa5))
  367. u = c_uart_8250;
  368. }
  369. return u;
  370. }
  371. /* --------------------------------------------------------------------- */
  372. static int ser12_open(struct net_device *dev)
  373. {
  374. struct baycom_state *bc = netdev_priv(dev);
  375. enum uart u;
  376. if (!dev || !bc)
  377. return -ENXIO;
  378. if (!dev->base_addr || dev->base_addr > 0xffff-SER12_EXTENT ||
  379. dev->irq < 2 || dev->irq > nr_irqs) {
  380. printk(KERN_INFO "baycom_ser_fdx: invalid portnumber (max %u) "
  381. "or irq (2 <= irq <= %d)\n",
  382. 0xffff-SER12_EXTENT, nr_irqs);
  383. return -ENXIO;
  384. }
  385. if (bc->baud < 300 || bc->baud > 4800) {
  386. printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
  387. "(300...4800)\n");
  388. return -EINVAL;
  389. }
  390. if (!request_region(dev->base_addr, SER12_EXTENT, "baycom_ser_fdx")) {
  391. printk(KERN_WARNING "BAYCOM_SER_FSX: I/O port 0x%04lx busy\n",
  392. dev->base_addr);
  393. return -EACCES;
  394. }
  395. memset(&bc->modem, 0, sizeof(bc->modem));
  396. bc->hdrv.par.bitrate = bc->baud;
  397. bc->baud_us = 1000000/bc->baud;
  398. bc->baud_uartdiv = (115200/8)/bc->baud;
  399. if ((u = ser12_check_uart(dev->base_addr)) == c_uart_unknown){
  400. release_region(dev->base_addr, SER12_EXTENT);
  401. return -EIO;
  402. }
  403. outb(0, FCR(dev->base_addr)); /* disable FIFOs */
  404. outb(0x0d, MCR(dev->base_addr));
  405. outb(0, IER(dev->base_addr));
  406. if (request_irq(dev->irq, ser12_interrupt, IRQF_SHARED,
  407. "baycom_ser_fdx", dev)) {
  408. release_region(dev->base_addr, SER12_EXTENT);
  409. return -EBUSY;
  410. }
  411. /*
  412. * set the SIO to 6 Bits/character; during receive,
  413. * the baud rate is set to produce 100 ints/sec
  414. * to feed the channel arbitration process,
  415. * during transmit to baud ints/sec to run
  416. * the transmitter
  417. */
  418. ser12_set_divisor(dev, 115200/100/8);
  419. /*
  420. * enable transmitter empty interrupt and modem status interrupt
  421. */
  422. outb(0x0a, IER(dev->base_addr));
  423. /*
  424. * make sure the next interrupt is generated;
  425. * 0 must be used to power the modem; the modem draws its
  426. * power from the TxD line
  427. */
  428. outb(0x00, THR(dev->base_addr));
  429. hdlcdrv_setdcd(&bc->hdrv, 0);
  430. printk(KERN_INFO "%s: ser_fdx at iobase 0x%lx irq %u baud %u uart %s\n",
  431. bc_drvname, dev->base_addr, dev->irq, bc->baud, uart_str[u]);
  432. return 0;
  433. }
  434. /* --------------------------------------------------------------------- */
  435. static int ser12_close(struct net_device *dev)
  436. {
  437. struct baycom_state *bc = netdev_priv(dev);
  438. if (!dev || !bc)
  439. return -EINVAL;
  440. /*
  441. * disable interrupts
  442. */
  443. outb(0, IER(dev->base_addr));
  444. outb(1, MCR(dev->base_addr));
  445. free_irq(dev->irq, dev);
  446. release_region(dev->base_addr, SER12_EXTENT);
  447. printk(KERN_INFO "%s: close ser_fdx at iobase 0x%lx irq %u\n",
  448. bc_drvname, dev->base_addr, dev->irq);
  449. return 0;
  450. }
  451. /* --------------------------------------------------------------------- */
  452. /*
  453. * ===================== hdlcdrv driver interface =========================
  454. */
  455. /* --------------------------------------------------------------------- */
  456. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
  457. struct hdlcdrv_ioctl *hi, int cmd);
  458. /* --------------------------------------------------------------------- */
  459. static struct hdlcdrv_ops ser12_ops = {
  460. .drvname = bc_drvname,
  461. .drvinfo = bc_drvinfo,
  462. .open = ser12_open,
  463. .close = ser12_close,
  464. .ioctl = baycom_ioctl,
  465. };
  466. /* --------------------------------------------------------------------- */
  467. static int baycom_setmode(struct baycom_state *bc, const char *modestr)
  468. {
  469. unsigned int baud;
  470. if (!strncmp(modestr, "ser", 3)) {
  471. baud = simple_strtoul(modestr+3, NULL, 10);
  472. if (baud >= 3 && baud <= 48)
  473. bc->baud = baud*100;
  474. }
  475. if (strchr(modestr, '*'))
  476. bc->opt_dcd = 0;
  477. else if (strchr(modestr, '+'))
  478. bc->opt_dcd = -1;
  479. else
  480. bc->opt_dcd = 1;
  481. return 0;
  482. }
  483. /* --------------------------------------------------------------------- */
  484. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
  485. struct hdlcdrv_ioctl *hi, int cmd)
  486. {
  487. struct baycom_state *bc;
  488. struct baycom_ioctl bi;
  489. if (!dev)
  490. return -EINVAL;
  491. bc = netdev_priv(dev);
  492. BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
  493. if (cmd != SIOCDEVPRIVATE)
  494. return -ENOIOCTLCMD;
  495. switch (hi->cmd) {
  496. default:
  497. break;
  498. case HDLCDRVCTL_GETMODE:
  499. sprintf(hi->data.modename, "ser%u", bc->baud / 100);
  500. if (bc->opt_dcd <= 0)
  501. strcat(hi->data.modename, (!bc->opt_dcd) ? "*" : "+");
  502. if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
  503. return -EFAULT;
  504. return 0;
  505. case HDLCDRVCTL_SETMODE:
  506. if (netif_running(dev) || !capable(CAP_NET_ADMIN))
  507. return -EACCES;
  508. hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
  509. return baycom_setmode(bc, hi->data.modename);
  510. case HDLCDRVCTL_MODELIST:
  511. strcpy(hi->data.modename, "ser12,ser3,ser24");
  512. if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
  513. return -EFAULT;
  514. return 0;
  515. case HDLCDRVCTL_MODEMPARMASK:
  516. return HDLCDRV_PARMASK_IOBASE | HDLCDRV_PARMASK_IRQ;
  517. }
  518. if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
  519. return -EFAULT;
  520. switch (bi.cmd) {
  521. default:
  522. return -ENOIOCTLCMD;
  523. #ifdef BAYCOM_DEBUG
  524. case BAYCOMCTL_GETDEBUG:
  525. bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
  526. bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
  527. bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
  528. break;
  529. #endif /* BAYCOM_DEBUG */
  530. }
  531. if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
  532. return -EFAULT;
  533. return 0;
  534. }
  535. /* --------------------------------------------------------------------- */
  536. /*
  537. * command line settable parameters
  538. */
  539. static char *mode[NR_PORTS] = { "ser12*", };
  540. static int iobase[NR_PORTS] = { 0x3f8, };
  541. static int irq[NR_PORTS] = { 4, };
  542. static int baud[NR_PORTS] = { [0 ... NR_PORTS-1] = 1200 };
  543. module_param_array(mode, charp, NULL, 0);
  544. MODULE_PARM_DESC(mode, "baycom operating mode; * for software DCD");
  545. module_param_array(iobase, int, NULL, 0);
  546. MODULE_PARM_DESC(iobase, "baycom io base address");
  547. module_param_array(irq, int, NULL, 0);
  548. MODULE_PARM_DESC(irq, "baycom irq number");
  549. module_param_array(baud, int, NULL, 0);
  550. MODULE_PARM_DESC(baud, "baycom baud rate (300 to 4800)");
  551. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  552. MODULE_DESCRIPTION("Baycom ser12 full duplex amateur radio modem driver");
  553. MODULE_LICENSE("GPL");
  554. /* --------------------------------------------------------------------- */
  555. static int __init init_baycomserfdx(void)
  556. {
  557. int i, found = 0;
  558. char set_hw = 1;
  559. printk(bc_drvinfo);
  560. /*
  561. * register net devices
  562. */
  563. for (i = 0; i < NR_PORTS; i++) {
  564. struct net_device *dev;
  565. struct baycom_state *bc;
  566. char ifname[IFNAMSIZ];
  567. sprintf(ifname, "bcsf%d", i);
  568. if (!mode[i])
  569. set_hw = 0;
  570. if (!set_hw)
  571. iobase[i] = irq[i] = 0;
  572. dev = hdlcdrv_register(&ser12_ops,
  573. sizeof(struct baycom_state),
  574. ifname, iobase[i], irq[i], 0);
  575. if (IS_ERR(dev))
  576. break;
  577. bc = netdev_priv(dev);
  578. if (set_hw && baycom_setmode(bc, mode[i]))
  579. set_hw = 0;
  580. bc->baud = baud[i];
  581. found++;
  582. baycom_device[i] = dev;
  583. }
  584. if (!found)
  585. return -ENXIO;
  586. return 0;
  587. }
  588. static void __exit cleanup_baycomserfdx(void)
  589. {
  590. int i;
  591. for(i = 0; i < NR_PORTS; i++) {
  592. struct net_device *dev = baycom_device[i];
  593. if (dev)
  594. hdlcdrv_unregister(dev);
  595. }
  596. }
  597. module_init(init_baycomserfdx);
  598. module_exit(cleanup_baycomserfdx);
  599. /* --------------------------------------------------------------------- */
  600. #ifndef MODULE
  601. /*
  602. * format: baycom_ser_fdx=io,irq,mode
  603. * mode: ser# hardware DCD
  604. * ser#* software DCD
  605. * ser#+ hardware DCD, inverted signal at DCD pin
  606. * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
  607. */
  608. static int __init baycom_ser_fdx_setup(char *str)
  609. {
  610. static unsigned nr_dev;
  611. int ints[4];
  612. if (nr_dev >= NR_PORTS)
  613. return 0;
  614. str = get_options(str, 4, ints);
  615. if (ints[0] < 2)
  616. return 0;
  617. mode[nr_dev] = str;
  618. iobase[nr_dev] = ints[1];
  619. irq[nr_dev] = ints[2];
  620. if (ints[0] >= 3)
  621. baud[nr_dev] = ints[3];
  622. nr_dev++;
  623. return 1;
  624. }
  625. __setup("baycom_ser_fdx=", baycom_ser_fdx_setup);
  626. #endif /* MODULE */
  627. /* --------------------------------------------------------------------- */