baycom_epp.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:44k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*****************************************************************************/
  2. /*
  3.  * baycom_epp.c  -- baycom epp radio modem driver.
  4.  *
  5.  * Copyright (C) 1998-2000
  6.  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *  Please note that the GPL allows you to use the driver, NOT the radio.
  23.  *  In order to use the radio, you need a license from the communications
  24.  *  authority of your country.
  25.  *
  26.  *
  27.  *  History:
  28.  *   0.1  xx.xx.1998  Initial version by Matthias Welwarsky (dg2fef)
  29.  *   0.2  21.04.1998  Massive rework by Thomas Sailer
  30.  *                    Integrated FPGA EPP modem configuration routines
  31.  *   0.3  11.05.1998  Took FPGA config out and moved it into a separate program
  32.  *   0.4  26.07.1999  Adapted to new lowlevel parport driver interface
  33.  *   0.5  03.08.1999  adapt to Linus' new __setup/__initcall
  34.  *                    removed some pre-2.2 kernel compatibility cruft
  35.  *   0.6  10.08.1999  Check if parport can do SPP and is safe to access during interrupt contexts
  36.  *   0.7  12.02.2000  adapted to softnet driver interface
  37.  *
  38.  */
  39. /*****************************************************************************/
  40. #include <linux/config.h>
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/string.h>
  45. #include <linux/tqueue.h>
  46. #include <linux/fs.h>
  47. #include <linux/parport.h>
  48. #include <linux/smp_lock.h>
  49. #include <asm/uaccess.h>
  50. #include <linux/if_arp.h>
  51. #include <linux/kmod.h>
  52. #include <linux/hdlcdrv.h>
  53. #include <linux/baycom.h>
  54. #include <linux/soundmodem.h>
  55. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  56. /* prototypes for ax25_encapsulate and ax25_rebuild_header */
  57. #include <net/ax25.h> 
  58. #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  59. #define __KERNEL_SYSCALLS__
  60. #include <linux/unistd.h>
  61. /* --------------------------------------------------------------------- */
  62. #define BAYCOM_DEBUG
  63. #define BAYCOM_MAGIC 19730510
  64. /* --------------------------------------------------------------------- */
  65. static const char paranoia_str[] = KERN_ERR 
  66. "baycom_epp: bad magic number for hdlcdrv_state struct in routine %sn";
  67. #define baycom_paranoia_check(dev,routine,retval)                                              
  68. ({                                                                                             
  69. if (!dev || !dev->priv || ((struct baycom_state *)dev->priv)->magic != BAYCOM_MAGIC) { 
  70. printk(paranoia_str, routine);                                                 
  71. return retval;                                                                 
  72. }                                                                                      
  73. })
  74. #define baycom_paranoia_check_void(dev,routine)                                                
  75. ({                                                                                             
  76. if (!dev || !dev->priv || ((struct baycom_state *)dev->priv)->magic != BAYCOM_MAGIC) { 
  77. printk(paranoia_str, routine);                                                 
  78. return;                                                                        
  79. }                                                                                      
  80. })
  81. /* --------------------------------------------------------------------- */
  82. static const char bc_drvname[] = "baycom_epp";
  83. static const char bc_drvinfo[] = KERN_INFO "baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WAn"
  84. KERN_INFO "baycom_epp: version 0.7 compiled " __TIME__ " " __DATE__ "n";
  85. /* --------------------------------------------------------------------- */
  86. #define NR_PORTS 4
  87. static struct net_device baycom_device[NR_PORTS];
  88. /* --------------------------------------------------------------------- */
  89. /* EPP status register */
  90. #define EPP_DCDBIT      0x80
  91. #define EPP_PTTBIT      0x08
  92. #define EPP_NREF        0x01
  93. #define EPP_NRAEF       0x02
  94. #define EPP_NRHF        0x04
  95. #define EPP_NTHF        0x20
  96. #define EPP_NTAEF       0x10
  97. #define EPP_NTEF        EPP_PTTBIT
  98. /* EPP control register */
  99. #define EPP_TX_FIFO_ENABLE 0x10
  100. #define EPP_RX_FIFO_ENABLE 0x08
  101. #define EPP_MODEM_ENABLE   0x20
  102. #define EPP_LEDS           0xC0
  103. #define EPP_IRQ_ENABLE     0x10
  104. /* LPT registers */
  105. #define LPTREG_ECONTROL       0x402
  106. #define LPTREG_CONFIGB        0x401
  107. #define LPTREG_CONFIGA        0x400
  108. #define LPTREG_EPPDATA        0x004
  109. #define LPTREG_EPPADDR        0x003
  110. #define LPTREG_CONTROL        0x002
  111. #define LPTREG_STATUS         0x001
  112. #define LPTREG_DATA           0x000
  113. /* LPT control register */
  114. #define LPTCTRL_PROGRAM       0x04   /* 0 to reprogram */
  115. #define LPTCTRL_WRITE         0x01
  116. #define LPTCTRL_ADDRSTB       0x08
  117. #define LPTCTRL_DATASTB       0x02
  118. #define LPTCTRL_INTEN         0x10
  119. /* LPT status register */
  120. #define LPTSTAT_SHIFT_NINTR   6
  121. #define LPTSTAT_WAIT          0x80
  122. #define LPTSTAT_NINTR         (1<<LPTSTAT_SHIFT_NINTR)
  123. #define LPTSTAT_PE            0x20
  124. #define LPTSTAT_DONE          0x10
  125. #define LPTSTAT_NERROR        0x08
  126. #define LPTSTAT_EPPTIMEOUT    0x01
  127. /* LPT data register */
  128. #define LPTDATA_SHIFT_TDI     0
  129. #define LPTDATA_SHIFT_TMS     2
  130. #define LPTDATA_TDI           (1<<LPTDATA_SHIFT_TDI)
  131. #define LPTDATA_TCK           0x02
  132. #define LPTDATA_TMS           (1<<LPTDATA_SHIFT_TMS)
  133. #define LPTDATA_INITBIAS      0x80
  134. /* EPP modem config/status bits */
  135. #define EPP_DCDBIT            0x80
  136. #define EPP_PTTBIT            0x08
  137. #define EPP_RXEBIT            0x01
  138. #define EPP_RXAEBIT           0x02
  139. #define EPP_RXHFULL           0x04
  140. #define EPP_NTHF              0x20
  141. #define EPP_NTAEF             0x10
  142. #define EPP_NTEF              EPP_PTTBIT
  143. #define EPP_TX_FIFO_ENABLE    0x10
  144. #define EPP_RX_FIFO_ENABLE    0x08
  145. #define EPP_MODEM_ENABLE      0x20
  146. #define EPP_LEDS              0xC0
  147. #define EPP_IRQ_ENABLE        0x10
  148. /* Xilinx 4k JTAG instructions */
  149. #define XC4K_IRLENGTH   3
  150. #define XC4K_EXTEST     0
  151. #define XC4K_PRELOAD    1
  152. #define XC4K_CONFIGURE  5
  153. #define XC4K_BYPASS     7
  154. #define EPP_CONVENTIONAL  0
  155. #define EPP_FPGA          1
  156. #define EPP_FPGAEXTSTATUS 2
  157. #define TXBUFFER_SIZE     ((HDLCDRV_MAXFLEN*6/5)+8)
  158. /* ---------------------------------------------------------------------- */
  159. /*
  160.  * Information that need to be kept for each board.
  161.  */
  162. struct baycom_state {
  163. int magic;
  164.         struct pardevice *pdev;
  165. unsigned int bh_running;
  166. struct tq_struct run_bh;
  167. unsigned int modem;
  168. unsigned int bitrate;
  169. unsigned char stat;
  170. struct {
  171. unsigned int intclk;
  172. unsigned int fclk;
  173. unsigned int bps;
  174. unsigned int extmodem;
  175. unsigned int loopback;
  176. } cfg;
  177.         struct hdlcdrv_channel_params ch_params;
  178.         struct {
  179. unsigned int bitbuf, bitstream, numbits, state;
  180. unsigned char *bufptr;
  181. int bufcnt;
  182. unsigned char buf[TXBUFFER_SIZE];
  183.         } hdlcrx;
  184.         struct {
  185. int calibrate;
  186.                 int slotcnt;
  187. int flags;
  188. enum { tx_idle = 0, tx_keyup, tx_data, tx_tail } state;
  189. unsigned char *bufptr;
  190. int bufcnt;
  191. unsigned char buf[TXBUFFER_SIZE];
  192.         } hdlctx;
  193.         struct net_device_stats stats;
  194. unsigned int ptt_keyed;
  195. struct sk_buff *skb;  /* next transmit packet  */
  196. #ifdef BAYCOM_DEBUG
  197. struct debug_vals {
  198. unsigned long last_jiffies;
  199. unsigned cur_intcnt;
  200. unsigned last_intcnt;
  201. int cur_pllcorr;
  202. int last_pllcorr;
  203. unsigned int mod_cycles;
  204. unsigned int demod_cycles;
  205. } debug_vals;
  206. #endif /* BAYCOM_DEBUG */
  207. };
  208. /* --------------------------------------------------------------------- */
  209. #define KISS_VERBOSE
  210. /* --------------------------------------------------------------------- */
  211. #define PARAM_TXDELAY   1
  212. #define PARAM_PERSIST   2
  213. #define PARAM_SLOTTIME  3
  214. #define PARAM_TXTAIL    4
  215. #define PARAM_FULLDUP   5
  216. #define PARAM_HARDWARE  6
  217. #define PARAM_RETURN    255
  218. /* --------------------------------------------------------------------- */
  219. /*
  220.  * the CRC routines are stolen from WAMPES
  221.  * by Dieter Deyke
  222.  */
  223. static const unsigned short crc_ccitt_table[] = {
  224. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  225. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  226. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  227. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  228. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  229. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  230. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  231. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  232. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  233. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  234. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  235. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  236. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  237. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  238. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  239. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  240. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  241. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  242. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  243. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  244. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  245. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  246. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  247. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  248. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  249. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  250. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  251. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  252. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  253. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  254. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  255. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  256. };
  257. /*---------------------------------------------------------------------------*/
  258. #if 0
  259. extern inline void append_crc_ccitt(unsigned char *buffer, int len)
  260. {
  261.   unsigned int crc = 0xffff;
  262. for (;len>0;len--)
  263. crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buffer++) & 0xff];
  264. crc ^= 0xffff;
  265. *buffer++ = crc;
  266. *buffer++ = crc >> 8;
  267. }
  268. #endif
  269. /*---------------------------------------------------------------------------*/
  270. extern inline int check_crc_ccitt(const unsigned char *buf, int cnt)
  271. {
  272. unsigned int crc = 0xffff;
  273. for (; cnt > 0; cnt--)
  274. crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff];
  275. return (crc & 0xffff) == 0xf0b8;
  276. }
  277. /*---------------------------------------------------------------------------*/
  278. extern inline int calc_crc_ccitt(const unsigned char *buf, int cnt)
  279. {
  280. unsigned int crc = 0xffff;
  281. for (; cnt > 0; cnt--)
  282. crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff];
  283. crc ^= 0xffff;
  284. return (crc & 0xffff);
  285. }
  286. /* ---------------------------------------------------------------------- */
  287. #define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800)
  288. /* --------------------------------------------------------------------- */
  289. static void inline baycom_int_freq(struct baycom_state *bc)
  290. {
  291. #ifdef BAYCOM_DEBUG
  292. unsigned long cur_jiffies = jiffies;
  293. /*
  294.  * measure the interrupt frequency
  295.  */
  296. bc->debug_vals.cur_intcnt++;
  297. if ((cur_jiffies - bc->debug_vals.last_jiffies) >= HZ) {
  298. bc->debug_vals.last_jiffies = cur_jiffies;
  299. bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
  300. bc->debug_vals.cur_intcnt = 0;
  301. bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
  302. bc->debug_vals.cur_pllcorr = 0;
  303. }
  304. #endif /* BAYCOM_DEBUG */
  305. }
  306. /* ---------------------------------------------------------------------- */
  307. /*
  308.  *    eppconfig_path should be setable  via /proc/sys.
  309.  */
  310. static char eppconfig_path[256] = "/usr/sbin/eppfpga";
  311. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL };
  312. static int errno;
  313. static int exec_eppfpga(void *b)
  314. {
  315. struct baycom_state *bc = (struct baycom_state *)b;
  316. char modearg[256];
  317. char portarg[16];
  318.         char *argv[] = { eppconfig_path, "-s", "-p", portarg, "-m", modearg, NULL};
  319.         int i;
  320. /* set up arguments */
  321. sprintf(modearg, "%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat",
  322. bc->cfg.intclk ? "int" : "ext",
  323. bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
  324. (bc->cfg.fclk + 8 * bc->cfg.bps) / (16 * bc->cfg.bps),
  325. bc->cfg.loopback ? ",loopback" : "");
  326. sprintf(portarg, "%ld", bc->pdev->port->base);
  327. printk(KERN_DEBUG "%s: %s -s -p %s -m %sn", bc_drvname, eppconfig_path, portarg, modearg);
  328. i = exec_usermodehelper(eppconfig_path, argv, envp);
  329. if (i < 0) {
  330.                 printk(KERN_ERR "%s: failed to exec %s -s -p %s -m %s, errno = %dn",
  331.                        bc_drvname, eppconfig_path, portarg, modearg, i);
  332.                 return i;
  333.         }
  334.         return 0;
  335. }
  336. /* eppconfig: called during ifconfig up to configure the modem */
  337. static int eppconfig(struct baycom_state *bc)
  338. {
  339.         int i, pid, r;
  340. mm_segment_t fs;
  341.         pid = kernel_thread(exec_eppfpga, bc, CLONE_FS);
  342.         if (pid < 0) {
  343.                 printk(KERN_ERR "%s: fork failed, errno %dn", bc_drvname, -pid);
  344.                 return pid;
  345.         }
  346. fs = get_fs();
  347.         set_fs(KERNEL_DS);      /* Allow i to be in kernel space. */
  348. r = waitpid(pid, &i, __WCLONE);
  349. set_fs(fs);
  350.         if (r != pid) {
  351.                 printk(KERN_ERR "%s: waitpid(%d) failed, returning %dn",
  352.        bc_drvname, pid, r);
  353. return -1;
  354.         }
  355. printk(KERN_DEBUG "%s: eppfpga returned %dn", bc_drvname, i);
  356. return i;
  357. }
  358. /* ---------------------------------------------------------------------- */
  359. static void epp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  360. {
  361. }
  362. /* ---------------------------------------------------------------------- */
  363. static void inline do_kiss_params(struct baycom_state *bc,
  364.   unsigned char *data, unsigned long len)
  365. {
  366. #ifdef KISS_VERBOSE
  367. #define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "n", b)
  368. #else /* KISS_VERBOSE */       
  369. #define PKP(a,b) 
  370. #endif /* KISS_VERBOSE */       
  371. if (len < 2)
  372. return;
  373. switch(data[0]) {
  374. case PARAM_TXDELAY:
  375. bc->ch_params.tx_delay = data[1];
  376. PKP("TX delay = %ums", 10 * bc->ch_params.tx_delay);
  377. break;
  378. case PARAM_PERSIST:   
  379. bc->ch_params.ppersist = data[1];
  380. PKP("p persistence = %u", bc->ch_params.ppersist);
  381. break;
  382. case PARAM_SLOTTIME:  
  383. bc->ch_params.slottime = data[1];
  384. PKP("slot time = %ums", bc->ch_params.slottime);
  385. break;
  386. case PARAM_TXTAIL:    
  387. bc->ch_params.tx_tail = data[1];
  388. PKP("TX tail = %ums", bc->ch_params.tx_tail);
  389. break;
  390. case PARAM_FULLDUP:   
  391. bc->ch_params.fulldup = !!data[1];
  392. PKP("%s duplex", bc->ch_params.fulldup ? "full" : "half");
  393. break;
  394. default:
  395. break;
  396. }
  397. #undef PKP
  398. }
  399. /* --------------------------------------------------------------------- */
  400. /*
  401.  * high performance HDLC encoder
  402.  * yes, it's ugly, but generates pretty good code
  403.  */
  404. #define ENCODEITERA(j)                         
  405. ({                                             
  406.         if (!(notbitstream & (0x1f0 << j)))    
  407.                 goto stuff##j;                 
  408.   encodeend##j:                                
  409. })
  410. #define ENCODEITERB(j)                                          
  411. ({                                                              
  412.   stuff##j:                                                     
  413.         bitstream &= ~(0x100 << j);                             
  414.         bitbuf = (bitbuf & (((2 << j) << numbit) - 1)) |        
  415.                 ((bitbuf & ~(((2 << j) << numbit) - 1)) << 1);  
  416.         numbit++;                                               
  417.         notbitstream = ~bitstream;                              
  418.         goto encodeend##j;                                      
  419. })
  420. static void encode_hdlc(struct baycom_state *bc)
  421. {
  422. struct sk_buff *skb;
  423. unsigned char *wp, *bp;
  424. int pkt_len;
  425.         unsigned bitstream, notbitstream, bitbuf, numbit, crc;
  426. unsigned char crcarr[2];
  427. if (bc->hdlctx.bufcnt > 0)
  428. return;
  429. skb = bc->skb;
  430. if (!skb)
  431. return;
  432. bc->skb = NULL;
  433. pkt_len = skb->len-1; /* strip KISS byte */
  434. wp = bc->hdlctx.buf;
  435. bp = skb->data+1;
  436. crc = calc_crc_ccitt(bp, pkt_len);
  437. crcarr[0] = crc;
  438. crcarr[1] = crc >> 8;
  439. *wp++ = 0x7e;
  440. bitstream = bitbuf = numbit = 0;
  441. while (pkt_len > -2) {
  442. bitstream >>= 8;
  443. bitstream |= ((unsigned int)*bp) << 8;
  444. bitbuf |= ((unsigned int)*bp) << numbit;
  445. notbitstream = ~bitstream;
  446. bp++;
  447. pkt_len--;
  448. if (!pkt_len)
  449. bp = crcarr;
  450. ENCODEITERA(0);
  451. ENCODEITERA(1);
  452. ENCODEITERA(2);
  453. ENCODEITERA(3);
  454. ENCODEITERA(4);
  455. ENCODEITERA(5);
  456. ENCODEITERA(6);
  457. ENCODEITERA(7);
  458. goto enditer;
  459. ENCODEITERB(0);
  460. ENCODEITERB(1);
  461. ENCODEITERB(2);
  462. ENCODEITERB(3);
  463. ENCODEITERB(4);
  464. ENCODEITERB(5);
  465. ENCODEITERB(6);
  466. ENCODEITERB(7);
  467. enditer:
  468. numbit += 8;
  469. while (numbit >= 8) {
  470. *wp++ = bitbuf;
  471. bitbuf >>= 8;
  472. numbit -= 8;
  473. }
  474. }
  475. bitbuf |= 0x7e7e << numbit;
  476. numbit += 16;
  477. while (numbit >= 8) {
  478. *wp++ = bitbuf;
  479. bitbuf >>= 8;
  480. numbit -= 8;
  481. }
  482. bc->hdlctx.bufptr = bc->hdlctx.buf;
  483. bc->hdlctx.bufcnt = wp - bc->hdlctx.buf;
  484. dev_kfree_skb(skb);
  485. bc->stats.tx_packets++;
  486. }
  487. /* ---------------------------------------------------------------------- */
  488. static unsigned short random_seed;
  489. static inline unsigned short random_num(void)
  490. {
  491. random_seed = 28629 * random_seed + 157;
  492. return random_seed;
  493. }
  494. /* ---------------------------------------------------------------------- */
  495. static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
  496. {
  497. struct parport *pp = bc->pdev->port;
  498. unsigned char tmp[128];
  499. int i, j;
  500. if (bc->hdlctx.state == tx_tail && !(stat & EPP_PTTBIT))
  501. bc->hdlctx.state = tx_idle;
  502. if (bc->hdlctx.state == tx_idle && bc->hdlctx.calibrate <= 0) {
  503. if (bc->hdlctx.bufcnt <= 0)
  504. encode_hdlc(bc);
  505. if (bc->hdlctx.bufcnt <= 0)
  506. return 0;
  507. if (!bc->ch_params.fulldup) {
  508. if (!(stat & EPP_DCDBIT)) {
  509. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  510. return 0;
  511. }
  512. if ((--bc->hdlctx.slotcnt) > 0)
  513. return 0;
  514. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  515. if ((random_num() % 256) > bc->ch_params.ppersist)
  516. return 0;
  517. }
  518. }
  519. if (bc->hdlctx.state == tx_idle && bc->hdlctx.bufcnt > 0) {
  520. bc->hdlctx.state = tx_keyup;
  521. bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_delay);
  522. bc->ptt_keyed++;
  523. }
  524. while (cnt > 0) {
  525. switch (bc->hdlctx.state) {
  526. case tx_keyup:
  527. i = min_t(int, cnt, bc->hdlctx.flags);
  528. cnt -= i;
  529. bc->hdlctx.flags -= i;
  530. if (bc->hdlctx.flags <= 0)
  531. bc->hdlctx.state = tx_data;
  532. memset(tmp, 0x7e, sizeof(tmp));
  533. while (i > 0) {
  534. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  535. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  536. return -1;
  537. i -= j;
  538. }
  539. break;
  540. case tx_data:
  541. if (bc->hdlctx.bufcnt <= 0) {
  542. encode_hdlc(bc);
  543. if (bc->hdlctx.bufcnt <= 0) {
  544. bc->hdlctx.state = tx_tail;
  545. bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_tail);
  546. break;
  547. }
  548. }
  549. i = min_t(int, cnt, bc->hdlctx.bufcnt);
  550. bc->hdlctx.bufcnt -= i;
  551. cnt -= i;
  552. if (i != pp->ops->epp_write_data(pp, bc->hdlctx.bufptr, i, 0))
  553. return -1;
  554. bc->hdlctx.bufptr += i;
  555. break;
  556. case tx_tail:
  557. encode_hdlc(bc);
  558. if (bc->hdlctx.bufcnt > 0) {
  559. bc->hdlctx.state = tx_data;
  560. break;
  561. }
  562. i = min_t(int, cnt, bc->hdlctx.flags);
  563. if (i) {
  564. cnt -= i;
  565. bc->hdlctx.flags -= i;
  566. memset(tmp, 0x7e, sizeof(tmp));
  567. while (i > 0) {
  568. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  569. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  570. return -1;
  571. i -= j;
  572. }
  573. break;
  574. }
  575. default:  /* fall through */
  576. if (bc->hdlctx.calibrate <= 0)
  577. return 0;
  578. i = min_t(int, cnt, bc->hdlctx.calibrate);
  579. cnt -= i;
  580. bc->hdlctx.calibrate -= i;
  581. memset(tmp, 0, sizeof(tmp));
  582. while (i > 0) {
  583. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  584. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  585. return -1;
  586. i -= j;
  587. }
  588. break;
  589. }
  590. }
  591. return 0;
  592. }
  593. /* ---------------------------------------------------------------------- */
  594. static void do_rxpacket(struct net_device *dev)
  595. {
  596. struct baycom_state *bc = (struct baycom_state *)dev->priv;
  597. struct sk_buff *skb;
  598. unsigned char *cp;
  599. unsigned pktlen;
  600. if (bc->hdlcrx.bufcnt < 4) 
  601. return;
  602. if (!check_crc_ccitt(bc->hdlcrx.buf, bc->hdlcrx.bufcnt)) 
  603. return;
  604. pktlen = bc->hdlcrx.bufcnt-2+1; /* KISS kludge */
  605. if (!(skb = dev_alloc_skb(pktlen))) {
  606. printk("%s: memory squeeze, dropping packetn", dev->name);
  607. bc->stats.rx_dropped++;
  608. return;
  609. }
  610. skb->dev = dev;
  611. cp = skb_put(skb, pktlen);
  612. *cp++ = 0; /* KISS kludge */
  613. memcpy(cp, bc->hdlcrx.buf, pktlen - 1);
  614. skb->protocol = htons(ETH_P_AX25);
  615. skb->mac.raw = skb->data;
  616. netif_rx(skb);
  617. bc->stats.rx_packets++;
  618. }
  619. #define DECODEITERA(j)                                                        
  620. ({                                                                            
  621.         if (!(notbitstream & (0x0fc << j)))              /* flag or abort */  
  622.                 goto flgabrt##j;                                              
  623.         if ((bitstream & (0x1f8 << j)) == (0xf8 << j))   /* stuffed bit */    
  624.                 goto stuff##j;                                                
  625.   enditer##j:                                                                 
  626. })
  627. #define DECODEITERB(j)                                                                 
  628. ({                                                                                     
  629.   flgabrt##j:                                                                          
  630.         if (!(notbitstream & (0x1fc << j))) {              /* abort received */        
  631.                 state = 0;                                                             
  632.                 goto enditer##j;                                                       
  633.         }                                                                              
  634.         if ((bitstream & (0x1fe << j)) != (0x0fc << j))   /* flag received */          
  635.                 goto enditer##j;                                                       
  636.         if (state)                                                                     
  637.                 do_rxpacket(dev);                                                      
  638.         bc->hdlcrx.bufcnt = 0;                                                         
  639.         bc->hdlcrx.bufptr = bc->hdlcrx.buf;                                            
  640.         state = 1;                                                                     
  641.         numbits = 7-j;                                                                 
  642.         goto enditer##j;                                                               
  643.   stuff##j:                                                                            
  644.         numbits--;                                                                     
  645.         bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) << j)) << 1);        
  646.         goto enditer##j;                                                               
  647. })
  648.         
  649. static int receive(struct net_device *dev, int cnt)
  650. {
  651. struct baycom_state *bc = (struct baycom_state *)dev->priv;
  652. struct parport *pp = bc->pdev->port;
  653.         unsigned int bitbuf, notbitstream, bitstream, numbits, state;
  654. unsigned char tmp[128];
  655.         unsigned char *cp;
  656. int cnt2, ret = 0;
  657.         
  658.         numbits = bc->hdlcrx.numbits;
  659. state = bc->hdlcrx.state;
  660. bitstream = bc->hdlcrx.bitstream;
  661. bitbuf = bc->hdlcrx.bitbuf;
  662. while (cnt > 0) {
  663. cnt2 = (cnt > sizeof(tmp)) ? sizeof(tmp) : cnt;
  664. cnt -= cnt2;
  665. if (cnt2 != pp->ops->epp_read_data(pp, tmp, cnt2, 0)) {
  666. ret = -1;
  667. break;
  668. }
  669. cp = tmp;
  670. for (; cnt2 > 0; cnt2--, cp++) {
  671. bitstream >>= 8;
  672. bitstream |= (*cp) << 8;
  673. bitbuf >>= 8;
  674. bitbuf |= (*cp) << 8;
  675. numbits += 8;
  676. notbitstream = ~bitstream;
  677. DECODEITERA(0);
  678. DECODEITERA(1);
  679. DECODEITERA(2);
  680. DECODEITERA(3);
  681. DECODEITERA(4);
  682. DECODEITERA(5);
  683. DECODEITERA(6);
  684. DECODEITERA(7);
  685. goto enddec;
  686. DECODEITERB(0);
  687. DECODEITERB(1);
  688. DECODEITERB(2);
  689. DECODEITERB(3);
  690. DECODEITERB(4);
  691. DECODEITERB(5);
  692. DECODEITERB(6);
  693. DECODEITERB(7);
  694. enddec:
  695. while (state && numbits >= 8) {
  696. if (bc->hdlcrx.bufcnt >= TXBUFFER_SIZE) {
  697. state = 0;
  698. } else {
  699. *(bc->hdlcrx.bufptr)++ = bitbuf >> (16-numbits);
  700. bc->hdlcrx.bufcnt++;
  701. numbits -= 8;
  702. }
  703. }
  704. }
  705. }
  706.         bc->hdlcrx.numbits = numbits;
  707. bc->hdlcrx.state = state;
  708. bc->hdlcrx.bitstream = bitstream;
  709. bc->hdlcrx.bitbuf = bitbuf;
  710. return ret;
  711. }
  712. /* --------------------------------------------------------------------- */
  713. #ifdef __i386__
  714. #define GETTICK(x)                                                
  715. ({                                                                
  716. if (cpu_has_tsc)                                          
  717. __asm__ __volatile__("rdtsc" : "=a" (x) : : "dx");
  718. })
  719. #else /* __i386__ */
  720. #define GETTICK(x)
  721. #endif /* __i386__ */
  722. static void epp_bh(struct net_device *dev)
  723. {
  724. struct baycom_state *bc;
  725. struct parport *pp;
  726. unsigned char stat;
  727. unsigned char tmp[2];
  728. unsigned int time1 = 0, time2 = 0, time3 = 0;
  729. int cnt, cnt2;
  730. baycom_paranoia_check_void(dev, "epp_bh");
  731. bc = (struct baycom_state *)dev->priv;
  732. if (!bc->bh_running)
  733. return;
  734. baycom_int_freq(bc);
  735. pp = bc->pdev->port;
  736. /* update status */
  737. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  738. goto epptimeout;
  739. bc->stat = stat;
  740. bc->debug_vals.last_pllcorr = stat;
  741. GETTICK(time1);
  742. if (bc->modem == EPP_FPGAEXTSTATUS) {
  743. /* get input count */
  744. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|1;
  745. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  746. goto epptimeout;
  747. if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
  748. goto epptimeout;
  749. cnt = tmp[0] | (tmp[1] << 8);
  750. cnt &= 0x7fff;
  751. /* get output count */
  752. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|2;
  753. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  754. goto epptimeout;
  755. if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
  756. goto epptimeout;
  757. cnt2 = tmp[0] | (tmp[1] << 8);
  758. cnt2 = 16384 - (cnt2 & 0x7fff);
  759. /* return to normal */
  760. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
  761. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  762. goto epptimeout;
  763. if (transmit(bc, cnt2, stat))
  764. goto epptimeout;
  765. GETTICK(time2);
  766. if (receive(dev, cnt))
  767. goto epptimeout;
  768. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  769. goto epptimeout;
  770. bc->stat = stat;
  771. } else {
  772. /* try to tx */
  773. switch (stat & (EPP_NTAEF|EPP_NTHF)) {
  774. case EPP_NTHF:
  775. cnt = 2048 - 256;
  776. break;
  777. case EPP_NTAEF:
  778. cnt = 2048 - 1793;
  779. break;
  780. case 0:
  781. cnt = 0;
  782. break;
  783. default:
  784. cnt = 2048 - 1025;
  785. break;
  786. }
  787. if (transmit(bc, cnt, stat))
  788. goto epptimeout;
  789. GETTICK(time2);
  790. /* do receiver */
  791. while ((stat & (EPP_NRAEF|EPP_NRHF)) != EPP_NRHF) {
  792. switch (stat & (EPP_NRAEF|EPP_NRHF)) {
  793. case EPP_NRAEF:
  794. cnt = 1025;
  795. break;
  796. case 0:
  797. cnt = 1793;
  798. break;
  799. default:
  800. cnt = 256;
  801. break;
  802. }
  803. if (receive(dev, cnt))
  804. goto epptimeout;
  805. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  806. goto epptimeout;
  807. }
  808. cnt = 0;
  809. if (bc->bitrate < 50000)
  810. cnt = 256;
  811. else if (bc->bitrate < 100000)
  812. cnt = 128;
  813. while (cnt > 0 && stat & EPP_NREF) {
  814. if (receive(dev, 1))
  815. goto epptimeout;
  816. cnt--;
  817. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  818. goto epptimeout;
  819. }
  820. }
  821. GETTICK(time3);
  822. #ifdef BAYCOM_DEBUG
  823. bc->debug_vals.mod_cycles = time2 - time1;
  824. bc->debug_vals.demod_cycles = time3 - time2;
  825. #endif /* BAYCOM_DEBUG */
  826. queue_task(&bc->run_bh, &tq_timer);
  827. if (!bc->skb)
  828. netif_wake_queue(dev);
  829. return;
  830.  epptimeout:
  831. printk(KERN_ERR "%s: EPP timeout!n", bc_drvname);
  832. }
  833. /* ---------------------------------------------------------------------- */
  834. /*
  835.  * ===================== network driver interface =========================
  836.  */
  837. static int baycom_send_packet(struct sk_buff *skb, struct net_device *dev)
  838. {
  839. struct baycom_state *bc;
  840. baycom_paranoia_check(dev, "baycom_send_packet", 0);
  841. bc = (struct baycom_state *)dev->priv;
  842. if (skb->data[0] != 0) {
  843. do_kiss_params(bc, skb->data, skb->len);
  844. dev_kfree_skb(skb);
  845. return 0;
  846. }
  847. if (bc->skb)
  848. return -1;
  849. /* strip KISS byte */
  850. if (skb->len >= HDLCDRV_MAXFLEN+1 || skb->len < 3) {
  851. dev_kfree_skb(skb);
  852. return 0;
  853. }
  854. netif_stop_queue(dev);
  855. bc->skb = skb;
  856. return 0;
  857. }
  858. /* --------------------------------------------------------------------- */
  859. static int baycom_set_mac_address(struct net_device *dev, void *addr)
  860. {
  861. struct sockaddr *sa = (struct sockaddr *)addr;
  862. /* addr is an AX.25 shifted ASCII mac address */
  863. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); 
  864. return 0;                                         
  865. }
  866. /* --------------------------------------------------------------------- */
  867. static struct net_device_stats *baycom_get_stats(struct net_device *dev)
  868. {
  869. struct baycom_state *bc;
  870. baycom_paranoia_check(dev, "baycom_get_stats", NULL);
  871. bc = (struct baycom_state *)dev->priv;
  872. /* 
  873.  * Get the current statistics.  This may be called with the
  874.  * card open or closed. 
  875.  */
  876. return &bc->stats;
  877. }
  878. /* --------------------------------------------------------------------- */
  879. static void epp_wakeup(void *handle)
  880. {
  881.         struct net_device *dev = (struct net_device *)handle;
  882.         struct baycom_state *bc;
  883. baycom_paranoia_check_void(dev, "epp_wakeup");
  884.         bc = (struct baycom_state *)dev->priv;
  885.         printk(KERN_DEBUG "baycom_epp: %s: why am I being woken up?n", dev->name);
  886.         if (!parport_claim(bc->pdev))
  887.                 printk(KERN_DEBUG "baycom_epp: %s: I'm broken.n", dev->name);
  888. }
  889. /* --------------------------------------------------------------------- */
  890. /*
  891.  * Open/initialize the board. This is called (in the current kernel)
  892.  * sometime after booting when the 'ifconfig' program is run.
  893.  *
  894.  * This routine should set everything up anew at each open, even
  895.  * registers that "should" only need to be set once at boot, so that
  896.  * there is non-reboot way to recover if something goes wrong.
  897.  */
  898. static int epp_open(struct net_device *dev)
  899. {
  900. struct baycom_state *bc;
  901.         struct parport *pp;
  902. const struct tq_struct run_bh = {
  903. routine: (void *)(void *)epp_bh,
  904. data: dev
  905. };
  906. unsigned int i, j;
  907. unsigned char tmp[128];
  908. unsigned char stat;
  909. unsigned long tstart;
  910. baycom_paranoia_check(dev, "epp_open", -ENXIO);
  911. bc = (struct baycom_state *)dev->priv;
  912.         pp = parport_enumerate();
  913.         while (pp && pp->base != dev->base_addr) 
  914.                 pp = pp->next;
  915.         if (!pp) {
  916.                 printk(KERN_ERR "%s: parport at 0x%lx unknownn", bc_drvname, dev->base_addr);
  917.                 return -ENXIO;
  918.         }
  919. #if 0
  920.         if (pp->irq < 0) {
  921.                 printk(KERN_ERR "%s: parport at 0x%lx has no irqn", bc_drvname, pp->base);
  922.                 return -ENXIO;
  923.         }
  924. #endif
  925. if ((~pp->modes) & (PARPORT_MODE_TRISTATE | PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
  926.                 printk(KERN_ERR "%s: parport at 0x%lx cannot be usedn",
  927.        bc_drvname, pp->base);
  928.                 return -EIO;
  929. }
  930. memset(&bc->modem, 0, sizeof(bc->modem));
  931.         if (!(bc->pdev = parport_register_device(pp, dev->name, NULL, epp_wakeup, 
  932.                                                  epp_interrupt, PARPORT_DEV_EXCL, dev))) {
  933.                 printk(KERN_ERR "%s: cannot register parport at 0x%lxn", bc_drvname, pp->base);
  934.                 return -ENXIO;
  935.         }
  936.         if (parport_claim(bc->pdev)) {
  937.                 printk(KERN_ERR "%s: parport at 0x%lx busyn", bc_drvname, pp->base);
  938.                 parport_unregister_device(bc->pdev);
  939.                 return -EBUSY;
  940.         }
  941.         dev->irq = /*pp->irq*/ 0;
  942. bc->run_bh = run_bh;
  943. bc->bh_running = 1;
  944. bc->modem = EPP_CONVENTIONAL;
  945. if (eppconfig(bc))
  946. printk(KERN_INFO "%s: no FPGA detected, assuming conventional EPP modemn", bc_drvname);
  947. else
  948. bc->modem = /*EPP_FPGA*/ EPP_FPGAEXTSTATUS;
  949. parport_write_control(pp, LPTCTRL_PROGRAM); /* prepare EPP mode; we aren't using interrupts */
  950. /* reset the modem */
  951. tmp[0] = 0;
  952. tmp[1] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
  953. if (pp->ops->epp_write_addr(pp, tmp, 2, 0) != 2)
  954. goto epptimeout;
  955. /* autoprobe baud rate */
  956. tstart = jiffies;
  957. i = 0;
  958. while ((signed)(jiffies-tstart-HZ/3) < 0) {
  959. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  960. goto epptimeout;
  961. if ((stat & (EPP_NRAEF|EPP_NRHF)) == EPP_NRHF) {
  962. schedule();
  963. continue;
  964. }
  965. if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
  966. goto epptimeout;
  967. if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
  968. goto epptimeout;
  969. i += 256;
  970. }
  971. for (j = 0; j < 256; j++) {
  972. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  973. goto epptimeout;
  974. if (!(stat & EPP_NREF))
  975. break;
  976. if (pp->ops->epp_read_data(pp, tmp, 1, 0) != 1)
  977. goto epptimeout;
  978. i++;
  979. }
  980. tstart = jiffies - tstart;
  981. bc->bitrate = i * (8 * HZ) / tstart;
  982. j = 1;
  983. i = bc->bitrate >> 3;
  984. while (j < 7 && i > 150) {
  985. j++;
  986. i >>= 1;
  987. }
  988. printk(KERN_INFO "%s: autoprobed bitrate: %d  int divider: %d  int rate: %dn", 
  989.        bc_drvname, bc->bitrate, j, bc->bitrate >> (j+2));
  990. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE/*|j*/;
  991. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  992. goto epptimeout;
  993. /*
  994.  * initialise hdlc variables
  995.  */
  996. bc->hdlcrx.state = 0;
  997. bc->hdlcrx.numbits = 0;
  998. bc->hdlctx.state = tx_idle;
  999. bc->hdlctx.bufcnt = 0;
  1000. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  1001. bc->hdlctx.calibrate = 0;
  1002. /* start the bottom half stuff */
  1003. queue_task(&bc->run_bh, &tq_timer);
  1004. netif_start_queue(dev);
  1005. MOD_INC_USE_COUNT;
  1006. return 0;
  1007.  epptimeout:
  1008. printk(KERN_ERR "%s: epp timeout during bitrate proben", bc_drvname);
  1009. parport_write_control(pp, 0); /* reset the adapter */
  1010.         parport_release(bc->pdev);
  1011.         parport_unregister_device(bc->pdev);
  1012. return -EIO;
  1013. }
  1014. /* --------------------------------------------------------------------- */
  1015. static int epp_close(struct net_device *dev)
  1016. {
  1017. struct baycom_state *bc;
  1018. struct parport *pp;
  1019. unsigned char tmp[1];
  1020. baycom_paranoia_check(dev, "epp_close", -EINVAL);
  1021. bc = (struct baycom_state *)dev->priv;
  1022. pp = bc->pdev->port;
  1023. bc->bh_running = 0;
  1024. run_task_queue(&tq_timer);  /* dequeue bottom half */
  1025. bc->stat = EPP_DCDBIT;
  1026. tmp[0] = 0;
  1027. pp->ops->epp_write_addr(pp, tmp, 1, 0);
  1028. parport_write_control(pp, 0); /* reset the adapter */
  1029.         parport_release(bc->pdev);
  1030.         parport_unregister_device(bc->pdev);
  1031. if (bc->skb)
  1032. dev_kfree_skb(bc->skb);
  1033. bc->skb = NULL;
  1034. printk(KERN_INFO "%s: close epp at iobase 0x%lx irq %un",
  1035.        bc_drvname, dev->base_addr, dev->irq);
  1036. MOD_DEC_USE_COUNT;
  1037. return 0;
  1038. }
  1039. /* --------------------------------------------------------------------- */
  1040. static int baycom_setmode(struct baycom_state *bc, const char *modestr)
  1041. {
  1042. const char *cp;
  1043. if (strstr(modestr,"intclk"))
  1044. bc->cfg.intclk = 1;
  1045. if (strstr(modestr,"extclk"))
  1046. bc->cfg.intclk = 0;
  1047. if (strstr(modestr,"intmodem"))
  1048. bc->cfg.extmodem = 0;
  1049. if (strstr(modestr,"extmodem"))
  1050. bc->cfg.extmodem = 1;
  1051. if (strstr(modestr,"noloopback"))
  1052. bc->cfg.loopback = 0;
  1053. if (strstr(modestr,"loopback"))
  1054. bc->cfg.loopback = 1;
  1055. if ((cp = strstr(modestr,"fclk="))) {
  1056. bc->cfg.fclk = simple_strtoul(cp+5, NULL, 0);
  1057. if (bc->cfg.fclk < 1000000)
  1058. bc->cfg.fclk = 1000000;
  1059. if (bc->cfg.fclk > 25000000)
  1060. bc->cfg.fclk = 25000000;
  1061. }
  1062. if ((cp = strstr(modestr,"bps="))) {
  1063. bc->cfg.bps = simple_strtoul(cp+4, NULL, 0);
  1064. if (bc->cfg.bps < 1000)
  1065. bc->cfg.bps = 1000;
  1066. if (bc->cfg.bps > 1500000)
  1067. bc->cfg.bps = 1500000;
  1068. }
  1069. return 0;
  1070. }
  1071. /* --------------------------------------------------------------------- */
  1072. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1073. {
  1074. struct baycom_state *bc;
  1075. struct baycom_ioctl bi;
  1076. struct hdlcdrv_ioctl hi;
  1077. struct sm_ioctl si;
  1078. baycom_paranoia_check(dev, "baycom_ioctl", -EINVAL);
  1079. bc = (struct baycom_state *)dev->priv;
  1080. if (cmd != SIOCDEVPRIVATE)
  1081. return -ENOIOCTLCMD;
  1082. if (get_user(cmd, (int *)ifr->ifr_data))
  1083. return -EFAULT;
  1084. #ifdef BAYCOM_DEBUG
  1085. if (cmd == BAYCOMCTL_GETDEBUG) {
  1086. bi.data.dbg.debug1 = bc->ptt_keyed;
  1087. bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
  1088. bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
  1089. bc->debug_vals.last_intcnt = 0;
  1090. if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
  1091. return -EFAULT;
  1092. return 0;
  1093. }
  1094. if (cmd == SMCTL_GETDEBUG) {
  1095.                 si.data.dbg.int_rate = bc->debug_vals.last_intcnt;
  1096.                 si.data.dbg.mod_cycles = bc->debug_vals.mod_cycles;
  1097.                 si.data.dbg.demod_cycles = bc->debug_vals.demod_cycles;
  1098.                 si.data.dbg.dma_residue = 0;
  1099.                 bc->debug_vals.mod_cycles = bc->debug_vals.demod_cycles = 0;
  1100. bc->debug_vals.last_intcnt = 0;
  1101.                 if (copy_to_user(ifr->ifr_data, &si, sizeof(si)))
  1102.                         return -EFAULT;
  1103.                 return 0;
  1104. }
  1105. #endif /* BAYCOM_DEBUG */
  1106. if (copy_from_user(&hi, ifr->ifr_data, sizeof(hi)))
  1107. return -EFAULT;
  1108. switch (hi.cmd) {
  1109. default:
  1110. return -ENOIOCTLCMD;
  1111. case HDLCDRVCTL_GETCHANNELPAR:
  1112. hi.data.cp.tx_delay = bc->ch_params.tx_delay;
  1113. hi.data.cp.tx_tail = bc->ch_params.tx_tail;
  1114. hi.data.cp.slottime = bc->ch_params.slottime;
  1115. hi.data.cp.ppersist = bc->ch_params.ppersist;
  1116. hi.data.cp.fulldup = bc->ch_params.fulldup;
  1117. break;
  1118. case HDLCDRVCTL_SETCHANNELPAR:
  1119. if (!capable(CAP_NET_ADMIN))
  1120. return -EACCES;
  1121. bc->ch_params.tx_delay = hi.data.cp.tx_delay;
  1122. bc->ch_params.tx_tail = hi.data.cp.tx_tail;
  1123. bc->ch_params.slottime = hi.data.cp.slottime;
  1124. bc->ch_params.ppersist = hi.data.cp.ppersist;
  1125. bc->ch_params.fulldup = hi.data.cp.fulldup;
  1126. bc->hdlctx.slotcnt = 1;
  1127. return 0;
  1128. case HDLCDRVCTL_GETMODEMPAR:
  1129. hi.data.mp.iobase = dev->base_addr;
  1130. hi.data.mp.irq = dev->irq;
  1131. hi.data.mp.dma = dev->dma;
  1132. hi.data.mp.dma2 = 0;
  1133. hi.data.mp.seriobase = 0;
  1134. hi.data.mp.pariobase = 0;
  1135. hi.data.mp.midiiobase = 0;
  1136. break;
  1137. case HDLCDRVCTL_SETMODEMPAR:
  1138. if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
  1139. return -EACCES;
  1140. dev->base_addr = hi.data.mp.iobase;
  1141. dev->irq = /*hi.data.mp.irq*/0;
  1142. dev->dma = /*hi.data.mp.dma*/0;
  1143. return 0;
  1144. case HDLCDRVCTL_GETSTAT:
  1145. hi.data.cs.ptt = !!(bc->stat & EPP_PTTBIT);
  1146. hi.data.cs.dcd = !(bc->stat & EPP_DCDBIT);
  1147. hi.data.cs.ptt_keyed = bc->ptt_keyed;
  1148. hi.data.cs.tx_packets = bc->stats.tx_packets;
  1149. hi.data.cs.tx_errors = bc->stats.tx_errors;
  1150. hi.data.cs.rx_packets = bc->stats.rx_packets;
  1151. hi.data.cs.rx_errors = bc->stats.rx_errors;
  1152. break;
  1153. case HDLCDRVCTL_OLDGETSTAT:
  1154. hi.data.ocs.ptt = !!(bc->stat & EPP_PTTBIT);
  1155. hi.data.ocs.dcd = !(bc->stat & EPP_DCDBIT);
  1156. hi.data.ocs.ptt_keyed = bc->ptt_keyed;
  1157. break;
  1158. case HDLCDRVCTL_CALIBRATE:
  1159. if (!capable(CAP_SYS_RAWIO))
  1160. return -EACCES;
  1161. bc->hdlctx.calibrate = hi.data.calibrate * bc->bitrate / 8;
  1162. return 0;
  1163. case HDLCDRVCTL_DRIVERNAME:
  1164. strncpy(hi.data.drivername, "baycom_epp", sizeof(hi.data.drivername));
  1165. break;
  1166. case HDLCDRVCTL_GETMODE:
  1167. sprintf(hi.data.modename, "%sclk,%smodem,fclk=%d,bps=%d%s", 
  1168. bc->cfg.intclk ? "int" : "ext",
  1169. bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
  1170. bc->cfg.loopback ? ",loopback" : "");
  1171. break;
  1172. case HDLCDRVCTL_SETMODE:
  1173. if (!capable(CAP_NET_ADMIN) || netif_running(dev))
  1174. return -EACCES;
  1175. hi.data.modename[sizeof(hi.data.modename)-1] = '';
  1176. return baycom_setmode(bc, hi.data.modename);
  1177. case HDLCDRVCTL_MODELIST:
  1178. strncpy(hi.data.modename, "intclk,extclk,intmodem,extmodem,divider=x",
  1179. sizeof(hi.data.modename));
  1180. break;
  1181. case HDLCDRVCTL_MODEMPARMASK:
  1182. return HDLCDRV_PARMASK_IOBASE;
  1183. }
  1184. if (copy_to_user(ifr->ifr_data, &hi, sizeof(hi)))
  1185. return -EFAULT;
  1186. return 0;
  1187. }
  1188. /* --------------------------------------------------------------------- */
  1189. /*
  1190.  * Check for a network adaptor of this type, and return '0' if one exists.
  1191.  * If dev->base_addr == 0, probe all likely locations.
  1192.  * If dev->base_addr == 1, always return failure.
  1193.  * If dev->base_addr == 2, allocate space for the device and return success
  1194.  * (detachable devices only).
  1195.  */
  1196. static int baycom_probe(struct net_device *dev)
  1197. {
  1198. static char ax25_bcast[AX25_ADDR_LEN] = {
  1199. 'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1
  1200. };
  1201. static char ax25_nocall[AX25_ADDR_LEN] = {
  1202. 'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1
  1203. };
  1204. const struct hdlcdrv_channel_params dflt_ch_params = { 
  1205. 20, 2, 10, 40, 0 
  1206. };
  1207. struct baycom_state *bc;
  1208. if (!dev)
  1209. return -ENXIO;
  1210. baycom_paranoia_check(dev, "baycom_probe", -ENXIO);
  1211. /*
  1212.  * not a real probe! only initialize data structures
  1213.  */
  1214. bc = (struct baycom_state *)dev->priv;
  1215. /*
  1216.  * initialize the baycom_state struct
  1217.  */
  1218. bc->ch_params = dflt_ch_params;
  1219. bc->ptt_keyed = 0;
  1220. /*
  1221.  * initialize the device struct
  1222.  */
  1223. dev->open = epp_open;
  1224. dev->stop = epp_close;
  1225. dev->do_ioctl = baycom_ioctl;
  1226. dev->hard_start_xmit = baycom_send_packet;
  1227. dev->get_stats = baycom_get_stats;
  1228. /* Fill in the fields of the device structure */
  1229. bc->skb = NULL;
  1230. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  1231. dev->hard_header = ax25_encapsulate;
  1232. dev->rebuild_header = ax25_rebuild_header;
  1233. #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  1234. dev->hard_header = NULL;
  1235. dev->rebuild_header = NULL;
  1236. #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  1237. dev->set_mac_address = baycom_set_mac_address;
  1238. dev->type = ARPHRD_AX25;           /* AF_AX25 device */
  1239. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  1240. dev->mtu = AX25_DEF_PACLEN;        /* eth_mtu is the default */
  1241. dev->addr_len = AX25_ADDR_LEN;     /* sizeof an ax.25 address */
  1242. memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
  1243. memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
  1244. dev->tx_queue_len = 16;
  1245. /* New style flags */
  1246. dev->flags = 0;
  1247. return 0;
  1248. }
  1249. /* --------------------------------------------------------------------- */
  1250. /*
  1251.  * command line settable parameters
  1252.  */
  1253. static const char *mode[NR_PORTS] = { "", };
  1254. static int iobase[NR_PORTS] = { 0x378, };
  1255. MODULE_PARM(mode, "1-" __MODULE_STRING(NR_PORTS) "s");
  1256. MODULE_PARM_DESC(mode, "baycom operating mode");
  1257. MODULE_PARM(iobase, "1-" __MODULE_STRING(NR_PORTS) "i");
  1258. MODULE_PARM_DESC(iobase, "baycom io base address");
  1259. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  1260. MODULE_DESCRIPTION("Baycom epp amateur radio modem driver");
  1261. MODULE_LICENSE("GPL");
  1262. /* --------------------------------------------------------------------- */
  1263. static int __init init_baycomepp(void)
  1264. {
  1265. struct net_device *dev;
  1266. int i, found = 0;
  1267. char set_hw = 1;
  1268. struct baycom_state *bc;
  1269. printk(bc_drvinfo);
  1270. /*
  1271.  * register net devices
  1272.  */
  1273. for (i = 0; i < NR_PORTS; i++) {
  1274. dev = baycom_device+i;
  1275. if (!mode[i])
  1276. set_hw = 0;
  1277. if (!set_hw)
  1278. iobase[i] = 0;
  1279. memset(dev, 0, sizeof(struct net_device));
  1280. if (!(bc = dev->priv = kmalloc(sizeof(struct baycom_state), GFP_KERNEL)))
  1281. return -ENOMEM;
  1282. /*
  1283.  * initialize part of the baycom_state struct
  1284.  */
  1285. memset(bc, 0, sizeof(struct baycom_state));
  1286. bc->magic = BAYCOM_MAGIC;
  1287. sprintf(dev->name, "bce%d", i);
  1288. bc->cfg.fclk = 19666600;
  1289. bc->cfg.bps = 9600;
  1290. /*
  1291.  * initialize part of the device struct
  1292.  */
  1293. dev->if_port = 0;
  1294. dev->init = baycom_probe;
  1295. dev->base_addr = iobase[i];
  1296. dev->irq = 0;
  1297. dev->dma = 0;
  1298. if (register_netdev(dev)) {
  1299. printk(KERN_WARNING "%s: cannot register net device %sn", bc_drvname, dev->name);
  1300. kfree(dev->priv);
  1301. return -ENXIO;
  1302. }
  1303. if (set_hw && baycom_setmode(bc, mode[i]))
  1304. set_hw = 0;
  1305. found++;
  1306. }
  1307. if (!found)
  1308. return -ENXIO;
  1309. return 0;
  1310. }
  1311. static void __exit cleanup_baycomepp(void)
  1312. {
  1313. struct net_device *dev;
  1314. struct baycom_state *bc;
  1315. int i;
  1316. for(i = 0; i < NR_PORTS; i++) {
  1317. dev = baycom_device+i;
  1318. bc = (struct baycom_state *)dev->priv;
  1319. if (bc) {
  1320. if (bc->magic == BAYCOM_MAGIC) {
  1321. unregister_netdev(dev);
  1322. kfree(dev->priv);
  1323. } else
  1324. printk(paranoia_str, "cleanup_module");
  1325. }
  1326. }
  1327. }
  1328. module_init(init_baycomepp);
  1329. module_exit(cleanup_baycomepp);
  1330. /* --------------------------------------------------------------------- */
  1331. #ifndef MODULE
  1332. /*
  1333.  * format: baycom_epp=io,mode
  1334.  * mode: fpga config options
  1335.  */
  1336. static int __init baycom_epp_setup(char *str)
  1337. {
  1338.         static unsigned __initdata nr_dev = 0;
  1339. int ints[2];
  1340.         if (nr_dev >= NR_PORTS)
  1341.                 return 0;
  1342. str = get_options(str, 2, ints);
  1343. if (ints[0] < 1)
  1344. return 0;
  1345. mode[nr_dev] = str;
  1346. iobase[nr_dev] = ints[1];
  1347. nr_dev++;
  1348. return 1;
  1349. }
  1350. __setup("baycom_epp=", baycom_epp_setup);
  1351. #endif /* MODULE */
  1352. /* --------------------------------------------------------------------- */