baycom_epp.c 34 KB

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