baycom_ser_fdx.c 21 KB

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