baycom_epp.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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/workqueue.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/jiffies.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. #include <linux/crc-ccitt.h>
  60. /* --------------------------------------------------------------------- */
  61. #define BAYCOM_DEBUG
  62. #define BAYCOM_MAGIC 19730510
  63. /* --------------------------------------------------------------------- */
  64. static const char paranoia_str[] = KERN_ERR
  65. "baycom_epp: bad magic number for hdlcdrv_state struct in routine %s\n";
  66. static const char bc_drvname[] = "baycom_epp";
  67. static const char bc_drvinfo[] = KERN_INFO "baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WA\n"
  68. KERN_INFO "baycom_epp: version 0.7 compiled " __TIME__ " " __DATE__ "\n";
  69. /* --------------------------------------------------------------------- */
  70. #define NR_PORTS 4
  71. static struct net_device *baycom_device[NR_PORTS];
  72. /* --------------------------------------------------------------------- */
  73. /* EPP status register */
  74. #define EPP_DCDBIT 0x80
  75. #define EPP_PTTBIT 0x08
  76. #define EPP_NREF 0x01
  77. #define EPP_NRAEF 0x02
  78. #define EPP_NRHF 0x04
  79. #define EPP_NTHF 0x20
  80. #define EPP_NTAEF 0x10
  81. #define EPP_NTEF EPP_PTTBIT
  82. /* EPP control register */
  83. #define EPP_TX_FIFO_ENABLE 0x10
  84. #define EPP_RX_FIFO_ENABLE 0x08
  85. #define EPP_MODEM_ENABLE 0x20
  86. #define EPP_LEDS 0xC0
  87. #define EPP_IRQ_ENABLE 0x10
  88. /* LPT registers */
  89. #define LPTREG_ECONTROL 0x402
  90. #define LPTREG_CONFIGB 0x401
  91. #define LPTREG_CONFIGA 0x400
  92. #define LPTREG_EPPDATA 0x004
  93. #define LPTREG_EPPADDR 0x003
  94. #define LPTREG_CONTROL 0x002
  95. #define LPTREG_STATUS 0x001
  96. #define LPTREG_DATA 0x000
  97. /* LPT control register */
  98. #define LPTCTRL_PROGRAM 0x04 /* 0 to reprogram */
  99. #define LPTCTRL_WRITE 0x01
  100. #define LPTCTRL_ADDRSTB 0x08
  101. #define LPTCTRL_DATASTB 0x02
  102. #define LPTCTRL_INTEN 0x10
  103. /* LPT status register */
  104. #define LPTSTAT_SHIFT_NINTR 6
  105. #define LPTSTAT_WAIT 0x80
  106. #define LPTSTAT_NINTR (1<<LPTSTAT_SHIFT_NINTR)
  107. #define LPTSTAT_PE 0x20
  108. #define LPTSTAT_DONE 0x10
  109. #define LPTSTAT_NERROR 0x08
  110. #define LPTSTAT_EPPTIMEOUT 0x01
  111. /* LPT data register */
  112. #define LPTDATA_SHIFT_TDI 0
  113. #define LPTDATA_SHIFT_TMS 2
  114. #define LPTDATA_TDI (1<<LPTDATA_SHIFT_TDI)
  115. #define LPTDATA_TCK 0x02
  116. #define LPTDATA_TMS (1<<LPTDATA_SHIFT_TMS)
  117. #define LPTDATA_INITBIAS 0x80
  118. /* EPP modem config/status bits */
  119. #define EPP_DCDBIT 0x80
  120. #define EPP_PTTBIT 0x08
  121. #define EPP_RXEBIT 0x01
  122. #define EPP_RXAEBIT 0x02
  123. #define EPP_RXHFULL 0x04
  124. #define EPP_NTHF 0x20
  125. #define EPP_NTAEF 0x10
  126. #define EPP_NTEF EPP_PTTBIT
  127. #define EPP_TX_FIFO_ENABLE 0x10
  128. #define EPP_RX_FIFO_ENABLE 0x08
  129. #define EPP_MODEM_ENABLE 0x20
  130. #define EPP_LEDS 0xC0
  131. #define EPP_IRQ_ENABLE 0x10
  132. /* Xilinx 4k JTAG instructions */
  133. #define XC4K_IRLENGTH 3
  134. #define XC4K_EXTEST 0
  135. #define XC4K_PRELOAD 1
  136. #define XC4K_CONFIGURE 5
  137. #define XC4K_BYPASS 7
  138. #define EPP_CONVENTIONAL 0
  139. #define EPP_FPGA 1
  140. #define EPP_FPGAEXTSTATUS 2
  141. #define TXBUFFER_SIZE ((HDLCDRV_MAXFLEN*6/5)+8)
  142. /* ---------------------------------------------------------------------- */
  143. /*
  144. * Information that need to be kept for each board.
  145. */
  146. struct baycom_state {
  147. int magic;
  148. struct pardevice *pdev;
  149. unsigned int work_running;
  150. struct work_struct run_work;
  151. unsigned int modem;
  152. unsigned int bitrate;
  153. unsigned char stat;
  154. struct {
  155. unsigned int intclk;
  156. unsigned int fclk;
  157. unsigned int bps;
  158. unsigned int extmodem;
  159. unsigned int loopback;
  160. } cfg;
  161. struct hdlcdrv_channel_params ch_params;
  162. struct {
  163. unsigned int bitbuf, bitstream, numbits, state;
  164. unsigned char *bufptr;
  165. int bufcnt;
  166. unsigned char buf[TXBUFFER_SIZE];
  167. } hdlcrx;
  168. struct {
  169. int calibrate;
  170. int slotcnt;
  171. int flags;
  172. enum { tx_idle = 0, tx_keyup, tx_data, tx_tail } state;
  173. unsigned char *bufptr;
  174. int bufcnt;
  175. unsigned char buf[TXBUFFER_SIZE];
  176. } hdlctx;
  177. struct net_device_stats stats;
  178. unsigned int ptt_keyed;
  179. struct sk_buff *skb; /* next transmit packet */
  180. #ifdef BAYCOM_DEBUG
  181. struct debug_vals {
  182. unsigned long last_jiffies;
  183. unsigned cur_intcnt;
  184. unsigned last_intcnt;
  185. int cur_pllcorr;
  186. int last_pllcorr;
  187. unsigned int mod_cycles;
  188. unsigned int demod_cycles;
  189. } debug_vals;
  190. #endif /* BAYCOM_DEBUG */
  191. };
  192. /* --------------------------------------------------------------------- */
  193. #define KISS_VERBOSE
  194. /* --------------------------------------------------------------------- */
  195. #define PARAM_TXDELAY 1
  196. #define PARAM_PERSIST 2
  197. #define PARAM_SLOTTIME 3
  198. #define PARAM_TXTAIL 4
  199. #define PARAM_FULLDUP 5
  200. #define PARAM_HARDWARE 6
  201. #define PARAM_RETURN 255
  202. /* --------------------------------------------------------------------- */
  203. /*
  204. * the CRC routines are stolen from WAMPES
  205. * by Dieter Deyke
  206. */
  207. /*---------------------------------------------------------------------------*/
  208. #if 0
  209. static inline void append_crc_ccitt(unsigned char *buffer, int len)
  210. {
  211. unsigned int crc = 0xffff;
  212. for (;len>0;len--)
  213. crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buffer++) & 0xff];
  214. crc ^= 0xffff;
  215. *buffer++ = crc;
  216. *buffer++ = crc >> 8;
  217. }
  218. #endif
  219. /*---------------------------------------------------------------------------*/
  220. static inline int check_crc_ccitt(const unsigned char *buf, int cnt)
  221. {
  222. return (crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8;
  223. }
  224. /*---------------------------------------------------------------------------*/
  225. static inline int calc_crc_ccitt(const unsigned char *buf, int cnt)
  226. {
  227. return (crc_ccitt(0xffff, buf, cnt) ^ 0xffff) & 0xffff;
  228. }
  229. /* ---------------------------------------------------------------------- */
  230. #define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800)
  231. /* --------------------------------------------------------------------- */
  232. static inline void baycom_int_freq(struct baycom_state *bc)
  233. {
  234. #ifdef BAYCOM_DEBUG
  235. unsigned long cur_jiffies = jiffies;
  236. /*
  237. * measure the interrupt frequency
  238. */
  239. bc->debug_vals.cur_intcnt++;
  240. if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
  241. bc->debug_vals.last_jiffies = cur_jiffies;
  242. bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
  243. bc->debug_vals.cur_intcnt = 0;
  244. bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
  245. bc->debug_vals.cur_pllcorr = 0;
  246. }
  247. #endif /* BAYCOM_DEBUG */
  248. }
  249. /* ---------------------------------------------------------------------- */
  250. /*
  251. * eppconfig_path should be setable via /proc/sys.
  252. */
  253. static char eppconfig_path[256] = "/usr/sbin/eppfpga";
  254. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL };
  255. /* eppconfig: called during ifconfig up to configure the modem */
  256. static int eppconfig(struct baycom_state *bc)
  257. {
  258. char modearg[256];
  259. char portarg[16];
  260. char *argv[] = { eppconfig_path, "-s", "-p", portarg, "-m", modearg,
  261. NULL };
  262. /* set up arguments */
  263. sprintf(modearg, "%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat",
  264. bc->cfg.intclk ? "int" : "ext",
  265. bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
  266. (bc->cfg.fclk + 8 * bc->cfg.bps) / (16 * bc->cfg.bps),
  267. bc->cfg.loopback ? ",loopback" : "");
  268. sprintf(portarg, "%ld", bc->pdev->port->base);
  269. printk(KERN_DEBUG "%s: %s -s -p %s -m %s\n", bc_drvname, eppconfig_path, portarg, modearg);
  270. return call_usermodehelper(eppconfig_path, argv, envp, 1);
  271. }
  272. /* ---------------------------------------------------------------------- */
  273. static void epp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  274. {
  275. }
  276. /* ---------------------------------------------------------------------- */
  277. static inline void do_kiss_params(struct baycom_state *bc,
  278. unsigned char *data, unsigned long len)
  279. {
  280. #ifdef KISS_VERBOSE
  281. #define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "\n", b)
  282. #else /* KISS_VERBOSE */
  283. #define PKP(a,b)
  284. #endif /* KISS_VERBOSE */
  285. if (len < 2)
  286. return;
  287. switch(data[0]) {
  288. case PARAM_TXDELAY:
  289. bc->ch_params.tx_delay = data[1];
  290. PKP("TX delay = %ums", 10 * bc->ch_params.tx_delay);
  291. break;
  292. case PARAM_PERSIST:
  293. bc->ch_params.ppersist = data[1];
  294. PKP("p persistence = %u", bc->ch_params.ppersist);
  295. break;
  296. case PARAM_SLOTTIME:
  297. bc->ch_params.slottime = data[1];
  298. PKP("slot time = %ums", bc->ch_params.slottime);
  299. break;
  300. case PARAM_TXTAIL:
  301. bc->ch_params.tx_tail = data[1];
  302. PKP("TX tail = %ums", bc->ch_params.tx_tail);
  303. break;
  304. case PARAM_FULLDUP:
  305. bc->ch_params.fulldup = !!data[1];
  306. PKP("%s duplex", bc->ch_params.fulldup ? "full" : "half");
  307. break;
  308. default:
  309. break;
  310. }
  311. #undef PKP
  312. }
  313. /* --------------------------------------------------------------------- */
  314. static void encode_hdlc(struct baycom_state *bc)
  315. {
  316. struct sk_buff *skb;
  317. unsigned char *wp, *bp;
  318. int pkt_len;
  319. unsigned bitstream, notbitstream, bitbuf, numbit, crc;
  320. unsigned char crcarr[2];
  321. int j;
  322. if (bc->hdlctx.bufcnt > 0)
  323. return;
  324. skb = bc->skb;
  325. if (!skb)
  326. return;
  327. bc->skb = NULL;
  328. pkt_len = skb->len-1; /* strip KISS byte */
  329. wp = bc->hdlctx.buf;
  330. bp = skb->data+1;
  331. crc = calc_crc_ccitt(bp, pkt_len);
  332. crcarr[0] = crc;
  333. crcarr[1] = crc >> 8;
  334. *wp++ = 0x7e;
  335. bitstream = bitbuf = numbit = 0;
  336. while (pkt_len > -2) {
  337. bitstream >>= 8;
  338. bitstream |= ((unsigned int)*bp) << 8;
  339. bitbuf |= ((unsigned int)*bp) << numbit;
  340. notbitstream = ~bitstream;
  341. bp++;
  342. pkt_len--;
  343. if (!pkt_len)
  344. bp = crcarr;
  345. for (j = 0; j < 8; j++)
  346. if (unlikely(!(notbitstream & (0x1f0 << j)))) {
  347. bitstream &= ~(0x100 << j);
  348. bitbuf = (bitbuf & (((2 << j) << numbit) - 1)) |
  349. ((bitbuf & ~(((2 << j) << numbit) - 1)) << 1);
  350. numbit++;
  351. notbitstream = ~bitstream;
  352. }
  353. numbit += 8;
  354. while (numbit >= 8) {
  355. *wp++ = bitbuf;
  356. bitbuf >>= 8;
  357. numbit -= 8;
  358. }
  359. }
  360. bitbuf |= 0x7e7e << numbit;
  361. numbit += 16;
  362. while (numbit >= 8) {
  363. *wp++ = bitbuf;
  364. bitbuf >>= 8;
  365. numbit -= 8;
  366. }
  367. bc->hdlctx.bufptr = bc->hdlctx.buf;
  368. bc->hdlctx.bufcnt = wp - bc->hdlctx.buf;
  369. dev_kfree_skb(skb);
  370. bc->stats.tx_packets++;
  371. }
  372. /* ---------------------------------------------------------------------- */
  373. static unsigned short random_seed;
  374. static inline unsigned short random_num(void)
  375. {
  376. random_seed = 28629 * random_seed + 157;
  377. return random_seed;
  378. }
  379. /* ---------------------------------------------------------------------- */
  380. static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
  381. {
  382. struct parport *pp = bc->pdev->port;
  383. unsigned char tmp[128];
  384. int i, j;
  385. if (bc->hdlctx.state == tx_tail && !(stat & EPP_PTTBIT))
  386. bc->hdlctx.state = tx_idle;
  387. if (bc->hdlctx.state == tx_idle && bc->hdlctx.calibrate <= 0) {
  388. if (bc->hdlctx.bufcnt <= 0)
  389. encode_hdlc(bc);
  390. if (bc->hdlctx.bufcnt <= 0)
  391. return 0;
  392. if (!bc->ch_params.fulldup) {
  393. if (!(stat & EPP_DCDBIT)) {
  394. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  395. return 0;
  396. }
  397. if ((--bc->hdlctx.slotcnt) > 0)
  398. return 0;
  399. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  400. if ((random_num() % 256) > bc->ch_params.ppersist)
  401. return 0;
  402. }
  403. }
  404. if (bc->hdlctx.state == tx_idle && bc->hdlctx.bufcnt > 0) {
  405. bc->hdlctx.state = tx_keyup;
  406. bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_delay);
  407. bc->ptt_keyed++;
  408. }
  409. while (cnt > 0) {
  410. switch (bc->hdlctx.state) {
  411. case tx_keyup:
  412. i = min_t(int, cnt, bc->hdlctx.flags);
  413. cnt -= i;
  414. bc->hdlctx.flags -= i;
  415. if (bc->hdlctx.flags <= 0)
  416. bc->hdlctx.state = tx_data;
  417. memset(tmp, 0x7e, sizeof(tmp));
  418. while (i > 0) {
  419. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  420. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  421. return -1;
  422. i -= j;
  423. }
  424. break;
  425. case tx_data:
  426. if (bc->hdlctx.bufcnt <= 0) {
  427. encode_hdlc(bc);
  428. if (bc->hdlctx.bufcnt <= 0) {
  429. bc->hdlctx.state = tx_tail;
  430. bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_tail);
  431. break;
  432. }
  433. }
  434. i = min_t(int, cnt, bc->hdlctx.bufcnt);
  435. bc->hdlctx.bufcnt -= i;
  436. cnt -= i;
  437. if (i != pp->ops->epp_write_data(pp, bc->hdlctx.bufptr, i, 0))
  438. return -1;
  439. bc->hdlctx.bufptr += i;
  440. break;
  441. case tx_tail:
  442. encode_hdlc(bc);
  443. if (bc->hdlctx.bufcnt > 0) {
  444. bc->hdlctx.state = tx_data;
  445. break;
  446. }
  447. i = min_t(int, cnt, bc->hdlctx.flags);
  448. if (i) {
  449. cnt -= i;
  450. bc->hdlctx.flags -= i;
  451. memset(tmp, 0x7e, sizeof(tmp));
  452. while (i > 0) {
  453. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  454. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  455. return -1;
  456. i -= j;
  457. }
  458. break;
  459. }
  460. default: /* fall through */
  461. if (bc->hdlctx.calibrate <= 0)
  462. return 0;
  463. i = min_t(int, cnt, bc->hdlctx.calibrate);
  464. cnt -= i;
  465. bc->hdlctx.calibrate -= i;
  466. memset(tmp, 0, sizeof(tmp));
  467. while (i > 0) {
  468. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  469. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  470. return -1;
  471. i -= j;
  472. }
  473. break;
  474. }
  475. }
  476. return 0;
  477. }
  478. /* ---------------------------------------------------------------------- */
  479. static void do_rxpacket(struct net_device *dev)
  480. {
  481. struct baycom_state *bc = netdev_priv(dev);
  482. struct sk_buff *skb;
  483. unsigned char *cp;
  484. unsigned pktlen;
  485. if (bc->hdlcrx.bufcnt < 4)
  486. return;
  487. if (!check_crc_ccitt(bc->hdlcrx.buf, bc->hdlcrx.bufcnt))
  488. return;
  489. pktlen = bc->hdlcrx.bufcnt-2+1; /* KISS kludge */
  490. if (!(skb = dev_alloc_skb(pktlen))) {
  491. printk("%s: memory squeeze, dropping packet\n", dev->name);
  492. bc->stats.rx_dropped++;
  493. return;
  494. }
  495. cp = skb_put(skb, pktlen);
  496. *cp++ = 0; /* KISS kludge */
  497. memcpy(cp, bc->hdlcrx.buf, pktlen - 1);
  498. skb->protocol = ax25_type_trans(skb, dev);
  499. netif_rx(skb);
  500. dev->last_rx = jiffies;
  501. bc->stats.rx_packets++;
  502. }
  503. static int receive(struct net_device *dev, int cnt)
  504. {
  505. struct baycom_state *bc = netdev_priv(dev);
  506. struct parport *pp = bc->pdev->port;
  507. unsigned int bitbuf, notbitstream, bitstream, numbits, state;
  508. unsigned char tmp[128];
  509. unsigned char *cp;
  510. int cnt2, ret = 0;
  511. int j;
  512. numbits = bc->hdlcrx.numbits;
  513. state = bc->hdlcrx.state;
  514. bitstream = bc->hdlcrx.bitstream;
  515. bitbuf = bc->hdlcrx.bitbuf;
  516. while (cnt > 0) {
  517. cnt2 = (cnt > sizeof(tmp)) ? sizeof(tmp) : cnt;
  518. cnt -= cnt2;
  519. if (cnt2 != pp->ops->epp_read_data(pp, tmp, cnt2, 0)) {
  520. ret = -1;
  521. break;
  522. }
  523. cp = tmp;
  524. for (; cnt2 > 0; cnt2--, cp++) {
  525. bitstream >>= 8;
  526. bitstream |= (*cp) << 8;
  527. bitbuf >>= 8;
  528. bitbuf |= (*cp) << 8;
  529. numbits += 8;
  530. notbitstream = ~bitstream;
  531. for (j = 0; j < 8; j++) {
  532. /* flag or abort */
  533. if (unlikely(!(notbitstream & (0x0fc << j)))) {
  534. /* abort received */
  535. if (!(notbitstream & (0x1fc << j)))
  536. state = 0;
  537. /* not flag received */
  538. else if (!(bitstream & (0x1fe << j)) != (0x0fc << j)) {
  539. if (state)
  540. do_rxpacket(dev);
  541. bc->hdlcrx.bufcnt = 0;
  542. bc->hdlcrx.bufptr = bc->hdlcrx.buf;
  543. state = 1;
  544. numbits = 7-j;
  545. }
  546. }
  547. /* stuffed bit */
  548. else if (unlikely((bitstream & (0x1f8 << j)) == (0xf8 << j))) {
  549. numbits--;
  550. bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) << j)) << 1);
  551. }
  552. }
  553. while (state && numbits >= 8) {
  554. if (bc->hdlcrx.bufcnt >= TXBUFFER_SIZE) {
  555. state = 0;
  556. } else {
  557. *(bc->hdlcrx.bufptr)++ = bitbuf >> (16-numbits);
  558. bc->hdlcrx.bufcnt++;
  559. numbits -= 8;
  560. }
  561. }
  562. }
  563. }
  564. bc->hdlcrx.numbits = numbits;
  565. bc->hdlcrx.state = state;
  566. bc->hdlcrx.bitstream = bitstream;
  567. bc->hdlcrx.bitbuf = bitbuf;
  568. return ret;
  569. }
  570. /* --------------------------------------------------------------------- */
  571. #ifdef __i386__
  572. #include <asm/msr.h>
  573. #define GETTICK(x) \
  574. ({ \
  575. if (cpu_has_tsc) \
  576. rdtscl(x); \
  577. })
  578. #else /* __i386__ */
  579. #define GETTICK(x)
  580. #endif /* __i386__ */
  581. static void epp_bh(struct net_device *dev)
  582. {
  583. struct baycom_state *bc;
  584. struct parport *pp;
  585. unsigned char stat;
  586. unsigned char tmp[2];
  587. unsigned int time1 = 0, time2 = 0, time3 = 0;
  588. int cnt, cnt2;
  589. bc = netdev_priv(dev);
  590. if (!bc->work_running)
  591. return;
  592. baycom_int_freq(bc);
  593. pp = bc->pdev->port;
  594. /* update status */
  595. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  596. goto epptimeout;
  597. bc->stat = stat;
  598. bc->debug_vals.last_pllcorr = stat;
  599. GETTICK(time1);
  600. if (bc->modem == EPP_FPGAEXTSTATUS) {
  601. /* get input count */
  602. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|1;
  603. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  604. goto epptimeout;
  605. if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
  606. goto epptimeout;
  607. cnt = tmp[0] | (tmp[1] << 8);
  608. cnt &= 0x7fff;
  609. /* get output count */
  610. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|2;
  611. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  612. goto epptimeout;
  613. if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
  614. goto epptimeout;
  615. cnt2 = tmp[0] | (tmp[1] << 8);
  616. cnt2 = 16384 - (cnt2 & 0x7fff);
  617. /* return to normal */
  618. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
  619. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  620. goto epptimeout;
  621. if (transmit(bc, cnt2, stat))
  622. goto epptimeout;
  623. GETTICK(time2);
  624. if (receive(dev, cnt))
  625. goto epptimeout;
  626. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  627. goto epptimeout;
  628. bc->stat = stat;
  629. } else {
  630. /* try to tx */
  631. switch (stat & (EPP_NTAEF|EPP_NTHF)) {
  632. case EPP_NTHF:
  633. cnt = 2048 - 256;
  634. break;
  635. case EPP_NTAEF:
  636. cnt = 2048 - 1793;
  637. break;
  638. case 0:
  639. cnt = 0;
  640. break;
  641. default:
  642. cnt = 2048 - 1025;
  643. break;
  644. }
  645. if (transmit(bc, cnt, stat))
  646. goto epptimeout;
  647. GETTICK(time2);
  648. /* do receiver */
  649. while ((stat & (EPP_NRAEF|EPP_NRHF)) != EPP_NRHF) {
  650. switch (stat & (EPP_NRAEF|EPP_NRHF)) {
  651. case EPP_NRAEF:
  652. cnt = 1025;
  653. break;
  654. case 0:
  655. cnt = 1793;
  656. break;
  657. default:
  658. cnt = 256;
  659. break;
  660. }
  661. if (receive(dev, cnt))
  662. goto epptimeout;
  663. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  664. goto epptimeout;
  665. }
  666. cnt = 0;
  667. if (bc->bitrate < 50000)
  668. cnt = 256;
  669. else if (bc->bitrate < 100000)
  670. cnt = 128;
  671. while (cnt > 0 && stat & EPP_NREF) {
  672. if (receive(dev, 1))
  673. goto epptimeout;
  674. cnt--;
  675. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  676. goto epptimeout;
  677. }
  678. }
  679. GETTICK(time3);
  680. #ifdef BAYCOM_DEBUG
  681. bc->debug_vals.mod_cycles = time2 - time1;
  682. bc->debug_vals.demod_cycles = time3 - time2;
  683. #endif /* BAYCOM_DEBUG */
  684. schedule_delayed_work(&bc->run_work, 1);
  685. if (!bc->skb)
  686. netif_wake_queue(dev);
  687. return;
  688. epptimeout:
  689. printk(KERN_ERR "%s: EPP timeout!\n", bc_drvname);
  690. }
  691. /* ---------------------------------------------------------------------- */
  692. /*
  693. * ===================== network driver interface =========================
  694. */
  695. static int baycom_send_packet(struct sk_buff *skb, struct net_device *dev)
  696. {
  697. struct baycom_state *bc = netdev_priv(dev);
  698. if (skb->data[0] != 0) {
  699. do_kiss_params(bc, skb->data, skb->len);
  700. dev_kfree_skb(skb);
  701. return 0;
  702. }
  703. if (bc->skb)
  704. return -1;
  705. /* strip KISS byte */
  706. if (skb->len >= HDLCDRV_MAXFLEN+1 || skb->len < 3) {
  707. dev_kfree_skb(skb);
  708. return 0;
  709. }
  710. netif_stop_queue(dev);
  711. bc->skb = skb;
  712. return 0;
  713. }
  714. /* --------------------------------------------------------------------- */
  715. static int baycom_set_mac_address(struct net_device *dev, void *addr)
  716. {
  717. struct sockaddr *sa = (struct sockaddr *)addr;
  718. /* addr is an AX.25 shifted ASCII mac address */
  719. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  720. return 0;
  721. }
  722. /* --------------------------------------------------------------------- */
  723. static struct net_device_stats *baycom_get_stats(struct net_device *dev)
  724. {
  725. struct baycom_state *bc = netdev_priv(dev);
  726. /*
  727. * Get the current statistics. This may be called with the
  728. * card open or closed.
  729. */
  730. return &bc->stats;
  731. }
  732. /* --------------------------------------------------------------------- */
  733. static void epp_wakeup(void *handle)
  734. {
  735. struct net_device *dev = (struct net_device *)handle;
  736. struct baycom_state *bc = netdev_priv(dev);
  737. printk(KERN_DEBUG "baycom_epp: %s: why am I being woken up?\n", dev->name);
  738. if (!parport_claim(bc->pdev))
  739. printk(KERN_DEBUG "baycom_epp: %s: I'm broken.\n", dev->name);
  740. }
  741. /* --------------------------------------------------------------------- */
  742. /*
  743. * Open/initialize the board. This is called (in the current kernel)
  744. * sometime after booting when the 'ifconfig' program is run.
  745. *
  746. * This routine should set everything up anew at each open, even
  747. * registers that "should" only need to be set once at boot, so that
  748. * there is non-reboot way to recover if something goes wrong.
  749. */
  750. static int epp_open(struct net_device *dev)
  751. {
  752. struct baycom_state *bc = netdev_priv(dev);
  753. struct parport *pp = parport_find_base(dev->base_addr);
  754. unsigned int i, j;
  755. unsigned char tmp[128];
  756. unsigned char stat;
  757. unsigned long tstart;
  758. if (!pp) {
  759. printk(KERN_ERR "%s: parport at 0x%lx unknown\n", bc_drvname, dev->base_addr);
  760. return -ENXIO;
  761. }
  762. #if 0
  763. if (pp->irq < 0) {
  764. printk(KERN_ERR "%s: parport at 0x%lx has no irq\n", bc_drvname, pp->base);
  765. parport_put_port(pp);
  766. return -ENXIO;
  767. }
  768. #endif
  769. if ((~pp->modes) & (PARPORT_MODE_TRISTATE | PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
  770. printk(KERN_ERR "%s: parport at 0x%lx cannot be used\n",
  771. bc_drvname, pp->base);
  772. parport_put_port(pp);
  773. return -EIO;
  774. }
  775. memset(&bc->modem, 0, sizeof(bc->modem));
  776. bc->pdev = parport_register_device(pp, dev->name, NULL, epp_wakeup,
  777. epp_interrupt, PARPORT_DEV_EXCL, dev);
  778. parport_put_port(pp);
  779. if (!bc->pdev) {
  780. printk(KERN_ERR "%s: cannot register parport at 0x%lx\n", bc_drvname, pp->base);
  781. return -ENXIO;
  782. }
  783. if (parport_claim(bc->pdev)) {
  784. printk(KERN_ERR "%s: parport at 0x%lx busy\n", bc_drvname, pp->base);
  785. parport_unregister_device(bc->pdev);
  786. return -EBUSY;
  787. }
  788. dev->irq = /*pp->irq*/ 0;
  789. INIT_WORK(&bc->run_work, (void *)(void *)epp_bh, dev);
  790. bc->work_running = 1;
  791. bc->modem = EPP_CONVENTIONAL;
  792. if (eppconfig(bc))
  793. printk(KERN_INFO "%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname);
  794. else
  795. bc->modem = /*EPP_FPGA*/ EPP_FPGAEXTSTATUS;
  796. parport_write_control(pp, LPTCTRL_PROGRAM); /* prepare EPP mode; we aren't using interrupts */
  797. /* reset the modem */
  798. tmp[0] = 0;
  799. tmp[1] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
  800. if (pp->ops->epp_write_addr(pp, tmp, 2, 0) != 2)
  801. goto epptimeout;
  802. /* autoprobe baud rate */
  803. tstart = jiffies;
  804. i = 0;
  805. while ((signed)(jiffies-tstart-HZ/3) < 0) {
  806. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  807. goto epptimeout;
  808. if ((stat & (EPP_NRAEF|EPP_NRHF)) == EPP_NRHF) {
  809. schedule();
  810. continue;
  811. }
  812. if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
  813. goto epptimeout;
  814. if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
  815. goto epptimeout;
  816. i += 256;
  817. }
  818. for (j = 0; j < 256; j++) {
  819. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  820. goto epptimeout;
  821. if (!(stat & EPP_NREF))
  822. break;
  823. if (pp->ops->epp_read_data(pp, tmp, 1, 0) != 1)
  824. goto epptimeout;
  825. i++;
  826. }
  827. tstart = jiffies - tstart;
  828. bc->bitrate = i * (8 * HZ) / tstart;
  829. j = 1;
  830. i = bc->bitrate >> 3;
  831. while (j < 7 && i > 150) {
  832. j++;
  833. i >>= 1;
  834. }
  835. printk(KERN_INFO "%s: autoprobed bitrate: %d int divider: %d int rate: %d\n",
  836. bc_drvname, bc->bitrate, j, bc->bitrate >> (j+2));
  837. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE/*|j*/;
  838. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  839. goto epptimeout;
  840. /*
  841. * initialise hdlc variables
  842. */
  843. bc->hdlcrx.state = 0;
  844. bc->hdlcrx.numbits = 0;
  845. bc->hdlctx.state = tx_idle;
  846. bc->hdlctx.bufcnt = 0;
  847. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  848. bc->hdlctx.calibrate = 0;
  849. /* start the bottom half stuff */
  850. schedule_delayed_work(&bc->run_work, 1);
  851. netif_start_queue(dev);
  852. return 0;
  853. epptimeout:
  854. printk(KERN_ERR "%s: epp timeout during bitrate probe\n", bc_drvname);
  855. parport_write_control(pp, 0); /* reset the adapter */
  856. parport_release(bc->pdev);
  857. parport_unregister_device(bc->pdev);
  858. return -EIO;
  859. }
  860. /* --------------------------------------------------------------------- */
  861. static int epp_close(struct net_device *dev)
  862. {
  863. struct baycom_state *bc = netdev_priv(dev);
  864. struct parport *pp = bc->pdev->port;
  865. unsigned char tmp[1];
  866. bc->work_running = 0;
  867. flush_scheduled_work();
  868. bc->stat = EPP_DCDBIT;
  869. tmp[0] = 0;
  870. pp->ops->epp_write_addr(pp, tmp, 1, 0);
  871. parport_write_control(pp, 0); /* reset the adapter */
  872. parport_release(bc->pdev);
  873. parport_unregister_device(bc->pdev);
  874. if (bc->skb)
  875. dev_kfree_skb(bc->skb);
  876. bc->skb = NULL;
  877. printk(KERN_INFO "%s: close epp at iobase 0x%lx irq %u\n",
  878. bc_drvname, dev->base_addr, dev->irq);
  879. return 0;
  880. }
  881. /* --------------------------------------------------------------------- */
  882. static int baycom_setmode(struct baycom_state *bc, const char *modestr)
  883. {
  884. const char *cp;
  885. if (strstr(modestr,"intclk"))
  886. bc->cfg.intclk = 1;
  887. if (strstr(modestr,"extclk"))
  888. bc->cfg.intclk = 0;
  889. if (strstr(modestr,"intmodem"))
  890. bc->cfg.extmodem = 0;
  891. if (strstr(modestr,"extmodem"))
  892. bc->cfg.extmodem = 1;
  893. if (strstr(modestr,"noloopback"))
  894. bc->cfg.loopback = 0;
  895. if (strstr(modestr,"loopback"))
  896. bc->cfg.loopback = 1;
  897. if ((cp = strstr(modestr,"fclk="))) {
  898. bc->cfg.fclk = simple_strtoul(cp+5, NULL, 0);
  899. if (bc->cfg.fclk < 1000000)
  900. bc->cfg.fclk = 1000000;
  901. if (bc->cfg.fclk > 25000000)
  902. bc->cfg.fclk = 25000000;
  903. }
  904. if ((cp = strstr(modestr,"bps="))) {
  905. bc->cfg.bps = simple_strtoul(cp+4, NULL, 0);
  906. if (bc->cfg.bps < 1000)
  907. bc->cfg.bps = 1000;
  908. if (bc->cfg.bps > 1500000)
  909. bc->cfg.bps = 1500000;
  910. }
  911. return 0;
  912. }
  913. /* --------------------------------------------------------------------- */
  914. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  915. {
  916. struct baycom_state *bc = netdev_priv(dev);
  917. struct hdlcdrv_ioctl hi;
  918. if (cmd != SIOCDEVPRIVATE)
  919. return -ENOIOCTLCMD;
  920. if (copy_from_user(&hi, ifr->ifr_data, sizeof(hi)))
  921. return -EFAULT;
  922. switch (hi.cmd) {
  923. default:
  924. return -ENOIOCTLCMD;
  925. case HDLCDRVCTL_GETCHANNELPAR:
  926. hi.data.cp.tx_delay = bc->ch_params.tx_delay;
  927. hi.data.cp.tx_tail = bc->ch_params.tx_tail;
  928. hi.data.cp.slottime = bc->ch_params.slottime;
  929. hi.data.cp.ppersist = bc->ch_params.ppersist;
  930. hi.data.cp.fulldup = bc->ch_params.fulldup;
  931. break;
  932. case HDLCDRVCTL_SETCHANNELPAR:
  933. if (!capable(CAP_NET_ADMIN))
  934. return -EACCES;
  935. bc->ch_params.tx_delay = hi.data.cp.tx_delay;
  936. bc->ch_params.tx_tail = hi.data.cp.tx_tail;
  937. bc->ch_params.slottime = hi.data.cp.slottime;
  938. bc->ch_params.ppersist = hi.data.cp.ppersist;
  939. bc->ch_params.fulldup = hi.data.cp.fulldup;
  940. bc->hdlctx.slotcnt = 1;
  941. return 0;
  942. case HDLCDRVCTL_GETMODEMPAR:
  943. hi.data.mp.iobase = dev->base_addr;
  944. hi.data.mp.irq = dev->irq;
  945. hi.data.mp.dma = dev->dma;
  946. hi.data.mp.dma2 = 0;
  947. hi.data.mp.seriobase = 0;
  948. hi.data.mp.pariobase = 0;
  949. hi.data.mp.midiiobase = 0;
  950. break;
  951. case HDLCDRVCTL_SETMODEMPAR:
  952. if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
  953. return -EACCES;
  954. dev->base_addr = hi.data.mp.iobase;
  955. dev->irq = /*hi.data.mp.irq*/0;
  956. dev->dma = /*hi.data.mp.dma*/0;
  957. return 0;
  958. case HDLCDRVCTL_GETSTAT:
  959. hi.data.cs.ptt = !!(bc->stat & EPP_PTTBIT);
  960. hi.data.cs.dcd = !(bc->stat & EPP_DCDBIT);
  961. hi.data.cs.ptt_keyed = bc->ptt_keyed;
  962. hi.data.cs.tx_packets = bc->stats.tx_packets;
  963. hi.data.cs.tx_errors = bc->stats.tx_errors;
  964. hi.data.cs.rx_packets = bc->stats.rx_packets;
  965. hi.data.cs.rx_errors = bc->stats.rx_errors;
  966. break;
  967. case HDLCDRVCTL_OLDGETSTAT:
  968. hi.data.ocs.ptt = !!(bc->stat & EPP_PTTBIT);
  969. hi.data.ocs.dcd = !(bc->stat & EPP_DCDBIT);
  970. hi.data.ocs.ptt_keyed = bc->ptt_keyed;
  971. break;
  972. case HDLCDRVCTL_CALIBRATE:
  973. if (!capable(CAP_SYS_RAWIO))
  974. return -EACCES;
  975. bc->hdlctx.calibrate = hi.data.calibrate * bc->bitrate / 8;
  976. return 0;
  977. case HDLCDRVCTL_DRIVERNAME:
  978. strncpy(hi.data.drivername, "baycom_epp", sizeof(hi.data.drivername));
  979. break;
  980. case HDLCDRVCTL_GETMODE:
  981. sprintf(hi.data.modename, "%sclk,%smodem,fclk=%d,bps=%d%s",
  982. bc->cfg.intclk ? "int" : "ext",
  983. bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
  984. bc->cfg.loopback ? ",loopback" : "");
  985. break;
  986. case HDLCDRVCTL_SETMODE:
  987. if (!capable(CAP_NET_ADMIN) || netif_running(dev))
  988. return -EACCES;
  989. hi.data.modename[sizeof(hi.data.modename)-1] = '\0';
  990. return baycom_setmode(bc, hi.data.modename);
  991. case HDLCDRVCTL_MODELIST:
  992. strncpy(hi.data.modename, "intclk,extclk,intmodem,extmodem,divider=x",
  993. sizeof(hi.data.modename));
  994. break;
  995. case HDLCDRVCTL_MODEMPARMASK:
  996. return HDLCDRV_PARMASK_IOBASE;
  997. }
  998. if (copy_to_user(ifr->ifr_data, &hi, sizeof(hi)))
  999. return -EFAULT;
  1000. return 0;
  1001. }
  1002. /* --------------------------------------------------------------------- */
  1003. /*
  1004. * Check for a network adaptor of this type, and return '0' if one exists.
  1005. * If dev->base_addr == 0, probe all likely locations.
  1006. * If dev->base_addr == 1, always return failure.
  1007. * If dev->base_addr == 2, allocate space for the device and return success
  1008. * (detachable devices only).
  1009. */
  1010. static void baycom_probe(struct net_device *dev)
  1011. {
  1012. static char ax25_bcast[AX25_ADDR_LEN] = {
  1013. 'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1
  1014. };
  1015. static char ax25_nocall[AX25_ADDR_LEN] = {
  1016. 'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1
  1017. };
  1018. const struct hdlcdrv_channel_params dflt_ch_params = {
  1019. 20, 2, 10, 40, 0
  1020. };
  1021. struct baycom_state *bc;
  1022. /*
  1023. * not a real probe! only initialize data structures
  1024. */
  1025. bc = netdev_priv(dev);
  1026. /*
  1027. * initialize the baycom_state struct
  1028. */
  1029. bc->ch_params = dflt_ch_params;
  1030. bc->ptt_keyed = 0;
  1031. /*
  1032. * initialize the device struct
  1033. */
  1034. dev->open = epp_open;
  1035. dev->stop = epp_close;
  1036. dev->do_ioctl = baycom_ioctl;
  1037. dev->hard_start_xmit = baycom_send_packet;
  1038. dev->get_stats = baycom_get_stats;
  1039. /* Fill in the fields of the device structure */
  1040. bc->skb = NULL;
  1041. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  1042. dev->hard_header = ax25_encapsulate;
  1043. dev->rebuild_header = ax25_rebuild_header;
  1044. #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  1045. dev->hard_header = NULL;
  1046. dev->rebuild_header = NULL;
  1047. #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  1048. dev->set_mac_address = baycom_set_mac_address;
  1049. dev->type = ARPHRD_AX25; /* AF_AX25 device */
  1050. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  1051. dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */
  1052. dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */
  1053. memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
  1054. memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
  1055. dev->tx_queue_len = 16;
  1056. /* New style flags */
  1057. dev->flags = 0;
  1058. }
  1059. /* --------------------------------------------------------------------- */
  1060. /*
  1061. * command line settable parameters
  1062. */
  1063. static const char *mode[NR_PORTS] = { "", };
  1064. static int iobase[NR_PORTS] = { 0x378, };
  1065. module_param_array(mode, charp, NULL, 0);
  1066. MODULE_PARM_DESC(mode, "baycom operating mode");
  1067. module_param_array(iobase, int, NULL, 0);
  1068. MODULE_PARM_DESC(iobase, "baycom io base address");
  1069. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  1070. MODULE_DESCRIPTION("Baycom epp amateur radio modem driver");
  1071. MODULE_LICENSE("GPL");
  1072. /* --------------------------------------------------------------------- */
  1073. static void __init baycom_epp_dev_setup(struct net_device *dev)
  1074. {
  1075. struct baycom_state *bc = netdev_priv(dev);
  1076. /*
  1077. * initialize part of the baycom_state struct
  1078. */
  1079. bc->magic = BAYCOM_MAGIC;
  1080. bc->cfg.fclk = 19666600;
  1081. bc->cfg.bps = 9600;
  1082. /*
  1083. * initialize part of the device struct
  1084. */
  1085. baycom_probe(dev);
  1086. }
  1087. static int __init init_baycomepp(void)
  1088. {
  1089. int i, found = 0;
  1090. char set_hw = 1;
  1091. printk(bc_drvinfo);
  1092. /*
  1093. * register net devices
  1094. */
  1095. for (i = 0; i < NR_PORTS; i++) {
  1096. struct net_device *dev;
  1097. dev = alloc_netdev(sizeof(struct baycom_state), "bce%d",
  1098. baycom_epp_dev_setup);
  1099. if (!dev) {
  1100. printk(KERN_WARNING "bce%d : out of memory\n", i);
  1101. return found ? 0 : -ENOMEM;
  1102. }
  1103. sprintf(dev->name, "bce%d", i);
  1104. dev->base_addr = iobase[i];
  1105. if (!mode[i])
  1106. set_hw = 0;
  1107. if (!set_hw)
  1108. iobase[i] = 0;
  1109. if (register_netdev(dev)) {
  1110. printk(KERN_WARNING "%s: cannot register net device %s\n", bc_drvname, dev->name);
  1111. free_netdev(dev);
  1112. break;
  1113. }
  1114. if (set_hw && baycom_setmode(netdev_priv(dev), mode[i]))
  1115. set_hw = 0;
  1116. baycom_device[i] = dev;
  1117. found++;
  1118. }
  1119. return found ? 0 : -ENXIO;
  1120. }
  1121. static void __exit cleanup_baycomepp(void)
  1122. {
  1123. int i;
  1124. for(i = 0; i < NR_PORTS; i++) {
  1125. struct net_device *dev = baycom_device[i];
  1126. if (dev) {
  1127. struct baycom_state *bc = netdev_priv(dev);
  1128. if (bc->magic == BAYCOM_MAGIC) {
  1129. unregister_netdev(dev);
  1130. free_netdev(dev);
  1131. } else
  1132. printk(paranoia_str, "cleanup_module");
  1133. }
  1134. }
  1135. }
  1136. module_init(init_baycomepp);
  1137. module_exit(cleanup_baycomepp);
  1138. /* --------------------------------------------------------------------- */
  1139. #ifndef MODULE
  1140. /*
  1141. * format: baycom_epp=io,mode
  1142. * mode: fpga config options
  1143. */
  1144. static int __init baycom_epp_setup(char *str)
  1145. {
  1146. static unsigned __initdata nr_dev = 0;
  1147. int ints[2];
  1148. if (nr_dev >= NR_PORTS)
  1149. return 0;
  1150. str = get_options(str, 2, ints);
  1151. if (ints[0] < 1)
  1152. return 0;
  1153. mode[nr_dev] = str;
  1154. iobase[nr_dev] = ints[1];
  1155. nr_dev++;
  1156. return 1;
  1157. }
  1158. __setup("baycom_epp=", baycom_epp_setup);
  1159. #endif /* MODULE */
  1160. /* --------------------------------------------------------------------- */