baycom_epp.c 38 KB

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