baycom_epp.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. static void encode_hdlc(struct baycom_state *bc)
  314. {
  315. struct sk_buff *skb;
  316. unsigned char *wp, *bp;
  317. int pkt_len;
  318. unsigned bitstream, notbitstream, bitbuf, numbit, crc;
  319. unsigned char crcarr[2];
  320. int j;
  321. if (bc->hdlctx.bufcnt > 0)
  322. return;
  323. skb = bc->skb;
  324. if (!skb)
  325. return;
  326. bc->skb = NULL;
  327. pkt_len = skb->len-1; /* strip KISS byte */
  328. wp = bc->hdlctx.buf;
  329. bp = skb->data+1;
  330. crc = calc_crc_ccitt(bp, pkt_len);
  331. crcarr[0] = crc;
  332. crcarr[1] = crc >> 8;
  333. *wp++ = 0x7e;
  334. bitstream = bitbuf = numbit = 0;
  335. while (pkt_len > -2) {
  336. bitstream >>= 8;
  337. bitstream |= ((unsigned int)*bp) << 8;
  338. bitbuf |= ((unsigned int)*bp) << numbit;
  339. notbitstream = ~bitstream;
  340. bp++;
  341. pkt_len--;
  342. if (!pkt_len)
  343. bp = crcarr;
  344. for (j = 0; j < 8; j++)
  345. if (unlikely(!(notbitstream & (0x1f0 << j)))) {
  346. bitstream &= ~(0x100 << j);
  347. bitbuf = (bitbuf & (((2 << j) << numbit) - 1)) |
  348. ((bitbuf & ~(((2 << j) << numbit) - 1)) << 1);
  349. numbit++;
  350. notbitstream = ~bitstream;
  351. }
  352. numbit += 8;
  353. while (numbit >= 8) {
  354. *wp++ = bitbuf;
  355. bitbuf >>= 8;
  356. numbit -= 8;
  357. }
  358. }
  359. bitbuf |= 0x7e7e << numbit;
  360. numbit += 16;
  361. while (numbit >= 8) {
  362. *wp++ = bitbuf;
  363. bitbuf >>= 8;
  364. numbit -= 8;
  365. }
  366. bc->hdlctx.bufptr = bc->hdlctx.buf;
  367. bc->hdlctx.bufcnt = wp - bc->hdlctx.buf;
  368. dev_kfree_skb(skb);
  369. bc->stats.tx_packets++;
  370. }
  371. /* ---------------------------------------------------------------------- */
  372. static unsigned short random_seed;
  373. static inline unsigned short random_num(void)
  374. {
  375. random_seed = 28629 * random_seed + 157;
  376. return random_seed;
  377. }
  378. /* ---------------------------------------------------------------------- */
  379. static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
  380. {
  381. struct parport *pp = bc->pdev->port;
  382. unsigned char tmp[128];
  383. int i, j;
  384. if (bc->hdlctx.state == tx_tail && !(stat & EPP_PTTBIT))
  385. bc->hdlctx.state = tx_idle;
  386. if (bc->hdlctx.state == tx_idle && bc->hdlctx.calibrate <= 0) {
  387. if (bc->hdlctx.bufcnt <= 0)
  388. encode_hdlc(bc);
  389. if (bc->hdlctx.bufcnt <= 0)
  390. return 0;
  391. if (!bc->ch_params.fulldup) {
  392. if (!(stat & EPP_DCDBIT)) {
  393. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  394. return 0;
  395. }
  396. if ((--bc->hdlctx.slotcnt) > 0)
  397. return 0;
  398. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  399. if ((random_num() % 256) > bc->ch_params.ppersist)
  400. return 0;
  401. }
  402. }
  403. if (bc->hdlctx.state == tx_idle && bc->hdlctx.bufcnt > 0) {
  404. bc->hdlctx.state = tx_keyup;
  405. bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_delay);
  406. bc->ptt_keyed++;
  407. }
  408. while (cnt > 0) {
  409. switch (bc->hdlctx.state) {
  410. case tx_keyup:
  411. i = min_t(int, cnt, bc->hdlctx.flags);
  412. cnt -= i;
  413. bc->hdlctx.flags -= i;
  414. if (bc->hdlctx.flags <= 0)
  415. bc->hdlctx.state = tx_data;
  416. memset(tmp, 0x7e, sizeof(tmp));
  417. while (i > 0) {
  418. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  419. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  420. return -1;
  421. i -= j;
  422. }
  423. break;
  424. case tx_data:
  425. if (bc->hdlctx.bufcnt <= 0) {
  426. encode_hdlc(bc);
  427. if (bc->hdlctx.bufcnt <= 0) {
  428. bc->hdlctx.state = tx_tail;
  429. bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_tail);
  430. break;
  431. }
  432. }
  433. i = min_t(int, cnt, bc->hdlctx.bufcnt);
  434. bc->hdlctx.bufcnt -= i;
  435. cnt -= i;
  436. if (i != pp->ops->epp_write_data(pp, bc->hdlctx.bufptr, i, 0))
  437. return -1;
  438. bc->hdlctx.bufptr += i;
  439. break;
  440. case tx_tail:
  441. encode_hdlc(bc);
  442. if (bc->hdlctx.bufcnt > 0) {
  443. bc->hdlctx.state = tx_data;
  444. break;
  445. }
  446. i = min_t(int, cnt, bc->hdlctx.flags);
  447. if (i) {
  448. cnt -= i;
  449. bc->hdlctx.flags -= i;
  450. memset(tmp, 0x7e, sizeof(tmp));
  451. while (i > 0) {
  452. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  453. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  454. return -1;
  455. i -= j;
  456. }
  457. break;
  458. }
  459. default: /* fall through */
  460. if (bc->hdlctx.calibrate <= 0)
  461. return 0;
  462. i = min_t(int, cnt, bc->hdlctx.calibrate);
  463. cnt -= i;
  464. bc->hdlctx.calibrate -= i;
  465. memset(tmp, 0, sizeof(tmp));
  466. while (i > 0) {
  467. j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
  468. if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
  469. return -1;
  470. i -= j;
  471. }
  472. break;
  473. }
  474. }
  475. return 0;
  476. }
  477. /* ---------------------------------------------------------------------- */
  478. static void do_rxpacket(struct net_device *dev)
  479. {
  480. struct baycom_state *bc = netdev_priv(dev);
  481. struct sk_buff *skb;
  482. unsigned char *cp;
  483. unsigned pktlen;
  484. if (bc->hdlcrx.bufcnt < 4)
  485. return;
  486. if (!check_crc_ccitt(bc->hdlcrx.buf, bc->hdlcrx.bufcnt))
  487. return;
  488. pktlen = bc->hdlcrx.bufcnt-2+1; /* KISS kludge */
  489. if (!(skb = dev_alloc_skb(pktlen))) {
  490. printk("%s: memory squeeze, dropping packet\n", dev->name);
  491. bc->stats.rx_dropped++;
  492. return;
  493. }
  494. cp = skb_put(skb, pktlen);
  495. *cp++ = 0; /* KISS kludge */
  496. memcpy(cp, bc->hdlcrx.buf, pktlen - 1);
  497. skb->protocol = ax25_type_trans(skb, dev);
  498. netif_rx(skb);
  499. dev->last_rx = jiffies;
  500. bc->stats.rx_packets++;
  501. }
  502. static int receive(struct net_device *dev, int cnt)
  503. {
  504. struct baycom_state *bc = netdev_priv(dev);
  505. struct parport *pp = bc->pdev->port;
  506. unsigned int bitbuf, notbitstream, bitstream, numbits, state;
  507. unsigned char tmp[128];
  508. unsigned char *cp;
  509. int cnt2, ret = 0;
  510. int j;
  511. numbits = bc->hdlcrx.numbits;
  512. state = bc->hdlcrx.state;
  513. bitstream = bc->hdlcrx.bitstream;
  514. bitbuf = bc->hdlcrx.bitbuf;
  515. while (cnt > 0) {
  516. cnt2 = (cnt > sizeof(tmp)) ? sizeof(tmp) : cnt;
  517. cnt -= cnt2;
  518. if (cnt2 != pp->ops->epp_read_data(pp, tmp, cnt2, 0)) {
  519. ret = -1;
  520. break;
  521. }
  522. cp = tmp;
  523. for (; cnt2 > 0; cnt2--, cp++) {
  524. bitstream >>= 8;
  525. bitstream |= (*cp) << 8;
  526. bitbuf >>= 8;
  527. bitbuf |= (*cp) << 8;
  528. numbits += 8;
  529. notbitstream = ~bitstream;
  530. for (j = 0; j < 8; j++) {
  531. /* flag or abort */
  532. if (unlikely(!(notbitstream & (0x0fc << j)))) {
  533. /* abort received */
  534. if (!(notbitstream & (0x1fc << j)))
  535. state = 0;
  536. /* not flag received */
  537. else if (!(bitstream & (0x1fe << j)) != (0x0fc << j)) {
  538. if (state)
  539. do_rxpacket(dev);
  540. bc->hdlcrx.bufcnt = 0;
  541. bc->hdlcrx.bufptr = bc->hdlcrx.buf;
  542. state = 1;
  543. numbits = 7-j;
  544. }
  545. }
  546. /* stuffed bit */
  547. else if (unlikely((bitstream & (0x1f8 << j)) == (0xf8 << j))) {
  548. numbits--;
  549. bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) << j)) << 1);
  550. }
  551. }
  552. while (state && numbits >= 8) {
  553. if (bc->hdlcrx.bufcnt >= TXBUFFER_SIZE) {
  554. state = 0;
  555. } else {
  556. *(bc->hdlcrx.bufptr)++ = bitbuf >> (16-numbits);
  557. bc->hdlcrx.bufcnt++;
  558. numbits -= 8;
  559. }
  560. }
  561. }
  562. }
  563. bc->hdlcrx.numbits = numbits;
  564. bc->hdlcrx.state = state;
  565. bc->hdlcrx.bitstream = bitstream;
  566. bc->hdlcrx.bitbuf = bitbuf;
  567. return ret;
  568. }
  569. /* --------------------------------------------------------------------- */
  570. #ifdef __i386__
  571. #include <asm/msr.h>
  572. #define GETTICK(x) \
  573. ({ \
  574. if (cpu_has_tsc) \
  575. rdtscl(x); \
  576. })
  577. #else /* __i386__ */
  578. #define GETTICK(x)
  579. #endif /* __i386__ */
  580. static void epp_bh(struct net_device *dev)
  581. {
  582. struct baycom_state *bc;
  583. struct parport *pp;
  584. unsigned char stat;
  585. unsigned char tmp[2];
  586. unsigned int time1 = 0, time2 = 0, time3 = 0;
  587. int cnt, cnt2;
  588. bc = netdev_priv(dev);
  589. if (!bc->work_running)
  590. return;
  591. baycom_int_freq(bc);
  592. pp = bc->pdev->port;
  593. /* update status */
  594. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  595. goto epptimeout;
  596. bc->stat = stat;
  597. bc->debug_vals.last_pllcorr = stat;
  598. GETTICK(time1);
  599. if (bc->modem == EPP_FPGAEXTSTATUS) {
  600. /* get input count */
  601. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|1;
  602. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  603. goto epptimeout;
  604. if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
  605. goto epptimeout;
  606. cnt = tmp[0] | (tmp[1] << 8);
  607. cnt &= 0x7fff;
  608. /* get output count */
  609. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|2;
  610. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  611. goto epptimeout;
  612. if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
  613. goto epptimeout;
  614. cnt2 = tmp[0] | (tmp[1] << 8);
  615. cnt2 = 16384 - (cnt2 & 0x7fff);
  616. /* return to normal */
  617. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
  618. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  619. goto epptimeout;
  620. if (transmit(bc, cnt2, stat))
  621. goto epptimeout;
  622. GETTICK(time2);
  623. if (receive(dev, cnt))
  624. goto epptimeout;
  625. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  626. goto epptimeout;
  627. bc->stat = stat;
  628. } else {
  629. /* try to tx */
  630. switch (stat & (EPP_NTAEF|EPP_NTHF)) {
  631. case EPP_NTHF:
  632. cnt = 2048 - 256;
  633. break;
  634. case EPP_NTAEF:
  635. cnt = 2048 - 1793;
  636. break;
  637. case 0:
  638. cnt = 0;
  639. break;
  640. default:
  641. cnt = 2048 - 1025;
  642. break;
  643. }
  644. if (transmit(bc, cnt, stat))
  645. goto epptimeout;
  646. GETTICK(time2);
  647. /* do receiver */
  648. while ((stat & (EPP_NRAEF|EPP_NRHF)) != EPP_NRHF) {
  649. switch (stat & (EPP_NRAEF|EPP_NRHF)) {
  650. case EPP_NRAEF:
  651. cnt = 1025;
  652. break;
  653. case 0:
  654. cnt = 1793;
  655. break;
  656. default:
  657. cnt = 256;
  658. break;
  659. }
  660. if (receive(dev, cnt))
  661. goto epptimeout;
  662. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  663. goto epptimeout;
  664. }
  665. cnt = 0;
  666. if (bc->bitrate < 50000)
  667. cnt = 256;
  668. else if (bc->bitrate < 100000)
  669. cnt = 128;
  670. while (cnt > 0 && stat & EPP_NREF) {
  671. if (receive(dev, 1))
  672. goto epptimeout;
  673. cnt--;
  674. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  675. goto epptimeout;
  676. }
  677. }
  678. GETTICK(time3);
  679. #ifdef BAYCOM_DEBUG
  680. bc->debug_vals.mod_cycles = time2 - time1;
  681. bc->debug_vals.demod_cycles = time3 - time2;
  682. #endif /* BAYCOM_DEBUG */
  683. schedule_delayed_work(&bc->run_work, 1);
  684. if (!bc->skb)
  685. netif_wake_queue(dev);
  686. return;
  687. epptimeout:
  688. printk(KERN_ERR "%s: EPP timeout!\n", bc_drvname);
  689. }
  690. /* ---------------------------------------------------------------------- */
  691. /*
  692. * ===================== network driver interface =========================
  693. */
  694. static int baycom_send_packet(struct sk_buff *skb, struct net_device *dev)
  695. {
  696. struct baycom_state *bc = netdev_priv(dev);
  697. if (skb->data[0] != 0) {
  698. do_kiss_params(bc, skb->data, skb->len);
  699. dev_kfree_skb(skb);
  700. return 0;
  701. }
  702. if (bc->skb)
  703. return -1;
  704. /* strip KISS byte */
  705. if (skb->len >= HDLCDRV_MAXFLEN+1 || skb->len < 3) {
  706. dev_kfree_skb(skb);
  707. return 0;
  708. }
  709. netif_stop_queue(dev);
  710. bc->skb = skb;
  711. return 0;
  712. }
  713. /* --------------------------------------------------------------------- */
  714. static int baycom_set_mac_address(struct net_device *dev, void *addr)
  715. {
  716. struct sockaddr *sa = (struct sockaddr *)addr;
  717. /* addr is an AX.25 shifted ASCII mac address */
  718. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  719. return 0;
  720. }
  721. /* --------------------------------------------------------------------- */
  722. static struct net_device_stats *baycom_get_stats(struct net_device *dev)
  723. {
  724. struct baycom_state *bc = netdev_priv(dev);
  725. /*
  726. * Get the current statistics. This may be called with the
  727. * card open or closed.
  728. */
  729. return &bc->stats;
  730. }
  731. /* --------------------------------------------------------------------- */
  732. static void epp_wakeup(void *handle)
  733. {
  734. struct net_device *dev = (struct net_device *)handle;
  735. struct baycom_state *bc = netdev_priv(dev);
  736. printk(KERN_DEBUG "baycom_epp: %s: why am I being woken up?\n", dev->name);
  737. if (!parport_claim(bc->pdev))
  738. printk(KERN_DEBUG "baycom_epp: %s: I'm broken.\n", dev->name);
  739. }
  740. /* --------------------------------------------------------------------- */
  741. /*
  742. * Open/initialize the board. This is called (in the current kernel)
  743. * sometime after booting when the 'ifconfig' program is run.
  744. *
  745. * This routine should set everything up anew at each open, even
  746. * registers that "should" only need to be set once at boot, so that
  747. * there is non-reboot way to recover if something goes wrong.
  748. */
  749. static int epp_open(struct net_device *dev)
  750. {
  751. struct baycom_state *bc = netdev_priv(dev);
  752. struct parport *pp = parport_find_base(dev->base_addr);
  753. unsigned int i, j;
  754. unsigned char tmp[128];
  755. unsigned char stat;
  756. unsigned long tstart;
  757. if (!pp) {
  758. printk(KERN_ERR "%s: parport at 0x%lx unknown\n", bc_drvname, dev->base_addr);
  759. return -ENXIO;
  760. }
  761. #if 0
  762. if (pp->irq < 0) {
  763. printk(KERN_ERR "%s: parport at 0x%lx has no irq\n", bc_drvname, pp->base);
  764. parport_put_port(pp);
  765. return -ENXIO;
  766. }
  767. #endif
  768. if ((~pp->modes) & (PARPORT_MODE_TRISTATE | PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
  769. printk(KERN_ERR "%s: parport at 0x%lx cannot be used\n",
  770. bc_drvname, pp->base);
  771. parport_put_port(pp);
  772. return -EIO;
  773. }
  774. memset(&bc->modem, 0, sizeof(bc->modem));
  775. bc->pdev = parport_register_device(pp, dev->name, NULL, epp_wakeup,
  776. epp_interrupt, PARPORT_DEV_EXCL, dev);
  777. parport_put_port(pp);
  778. if (!bc->pdev) {
  779. printk(KERN_ERR "%s: cannot register parport at 0x%lx\n", bc_drvname, pp->base);
  780. return -ENXIO;
  781. }
  782. if (parport_claim(bc->pdev)) {
  783. printk(KERN_ERR "%s: parport at 0x%lx busy\n", bc_drvname, pp->base);
  784. parport_unregister_device(bc->pdev);
  785. return -EBUSY;
  786. }
  787. dev->irq = /*pp->irq*/ 0;
  788. INIT_WORK(&bc->run_work, (void *)(void *)epp_bh, dev);
  789. bc->work_running = 1;
  790. bc->modem = EPP_CONVENTIONAL;
  791. if (eppconfig(bc))
  792. printk(KERN_INFO "%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname);
  793. else
  794. bc->modem = /*EPP_FPGA*/ EPP_FPGAEXTSTATUS;
  795. parport_write_control(pp, LPTCTRL_PROGRAM); /* prepare EPP mode; we aren't using interrupts */
  796. /* reset the modem */
  797. tmp[0] = 0;
  798. tmp[1] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
  799. if (pp->ops->epp_write_addr(pp, tmp, 2, 0) != 2)
  800. goto epptimeout;
  801. /* autoprobe baud rate */
  802. tstart = jiffies;
  803. i = 0;
  804. while ((signed)(jiffies-tstart-HZ/3) < 0) {
  805. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  806. goto epptimeout;
  807. if ((stat & (EPP_NRAEF|EPP_NRHF)) == EPP_NRHF) {
  808. schedule();
  809. continue;
  810. }
  811. if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
  812. goto epptimeout;
  813. if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
  814. goto epptimeout;
  815. i += 256;
  816. }
  817. for (j = 0; j < 256; j++) {
  818. if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
  819. goto epptimeout;
  820. if (!(stat & EPP_NREF))
  821. break;
  822. if (pp->ops->epp_read_data(pp, tmp, 1, 0) != 1)
  823. goto epptimeout;
  824. i++;
  825. }
  826. tstart = jiffies - tstart;
  827. bc->bitrate = i * (8 * HZ) / tstart;
  828. j = 1;
  829. i = bc->bitrate >> 3;
  830. while (j < 7 && i > 150) {
  831. j++;
  832. i >>= 1;
  833. }
  834. printk(KERN_INFO "%s: autoprobed bitrate: %d int divider: %d int rate: %d\n",
  835. bc_drvname, bc->bitrate, j, bc->bitrate >> (j+2));
  836. tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE/*|j*/;
  837. if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
  838. goto epptimeout;
  839. /*
  840. * initialise hdlc variables
  841. */
  842. bc->hdlcrx.state = 0;
  843. bc->hdlcrx.numbits = 0;
  844. bc->hdlctx.state = tx_idle;
  845. bc->hdlctx.bufcnt = 0;
  846. bc->hdlctx.slotcnt = bc->ch_params.slottime;
  847. bc->hdlctx.calibrate = 0;
  848. /* start the bottom half stuff */
  849. schedule_delayed_work(&bc->run_work, 1);
  850. netif_start_queue(dev);
  851. return 0;
  852. epptimeout:
  853. printk(KERN_ERR "%s: epp timeout during bitrate probe\n", bc_drvname);
  854. parport_write_control(pp, 0); /* reset the adapter */
  855. parport_release(bc->pdev);
  856. parport_unregister_device(bc->pdev);
  857. return -EIO;
  858. }
  859. /* --------------------------------------------------------------------- */
  860. static int epp_close(struct net_device *dev)
  861. {
  862. struct baycom_state *bc = netdev_priv(dev);
  863. struct parport *pp = bc->pdev->port;
  864. unsigned char tmp[1];
  865. bc->work_running = 0;
  866. flush_scheduled_work();
  867. bc->stat = EPP_DCDBIT;
  868. tmp[0] = 0;
  869. pp->ops->epp_write_addr(pp, tmp, 1, 0);
  870. parport_write_control(pp, 0); /* reset the adapter */
  871. parport_release(bc->pdev);
  872. parport_unregister_device(bc->pdev);
  873. if (bc->skb)
  874. dev_kfree_skb(bc->skb);
  875. bc->skb = NULL;
  876. printk(KERN_INFO "%s: close epp at iobase 0x%lx irq %u\n",
  877. bc_drvname, dev->base_addr, dev->irq);
  878. return 0;
  879. }
  880. /* --------------------------------------------------------------------- */
  881. static int baycom_setmode(struct baycom_state *bc, const char *modestr)
  882. {
  883. const char *cp;
  884. if (strstr(modestr,"intclk"))
  885. bc->cfg.intclk = 1;
  886. if (strstr(modestr,"extclk"))
  887. bc->cfg.intclk = 0;
  888. if (strstr(modestr,"intmodem"))
  889. bc->cfg.extmodem = 0;
  890. if (strstr(modestr,"extmodem"))
  891. bc->cfg.extmodem = 1;
  892. if (strstr(modestr,"noloopback"))
  893. bc->cfg.loopback = 0;
  894. if (strstr(modestr,"loopback"))
  895. bc->cfg.loopback = 1;
  896. if ((cp = strstr(modestr,"fclk="))) {
  897. bc->cfg.fclk = simple_strtoul(cp+5, NULL, 0);
  898. if (bc->cfg.fclk < 1000000)
  899. bc->cfg.fclk = 1000000;
  900. if (bc->cfg.fclk > 25000000)
  901. bc->cfg.fclk = 25000000;
  902. }
  903. if ((cp = strstr(modestr,"bps="))) {
  904. bc->cfg.bps = simple_strtoul(cp+4, NULL, 0);
  905. if (bc->cfg.bps < 1000)
  906. bc->cfg.bps = 1000;
  907. if (bc->cfg.bps > 1500000)
  908. bc->cfg.bps = 1500000;
  909. }
  910. return 0;
  911. }
  912. /* --------------------------------------------------------------------- */
  913. static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  914. {
  915. struct baycom_state *bc = netdev_priv(dev);
  916. struct hdlcdrv_ioctl hi;
  917. if (cmd != SIOCDEVPRIVATE)
  918. return -ENOIOCTLCMD;
  919. if (copy_from_user(&hi, ifr->ifr_data, sizeof(hi)))
  920. return -EFAULT;
  921. switch (hi.cmd) {
  922. default:
  923. return -ENOIOCTLCMD;
  924. case HDLCDRVCTL_GETCHANNELPAR:
  925. hi.data.cp.tx_delay = bc->ch_params.tx_delay;
  926. hi.data.cp.tx_tail = bc->ch_params.tx_tail;
  927. hi.data.cp.slottime = bc->ch_params.slottime;
  928. hi.data.cp.ppersist = bc->ch_params.ppersist;
  929. hi.data.cp.fulldup = bc->ch_params.fulldup;
  930. break;
  931. case HDLCDRVCTL_SETCHANNELPAR:
  932. if (!capable(CAP_NET_ADMIN))
  933. return -EACCES;
  934. bc->ch_params.tx_delay = hi.data.cp.tx_delay;
  935. bc->ch_params.tx_tail = hi.data.cp.tx_tail;
  936. bc->ch_params.slottime = hi.data.cp.slottime;
  937. bc->ch_params.ppersist = hi.data.cp.ppersist;
  938. bc->ch_params.fulldup = hi.data.cp.fulldup;
  939. bc->hdlctx.slotcnt = 1;
  940. return 0;
  941. case HDLCDRVCTL_GETMODEMPAR:
  942. hi.data.mp.iobase = dev->base_addr;
  943. hi.data.mp.irq = dev->irq;
  944. hi.data.mp.dma = dev->dma;
  945. hi.data.mp.dma2 = 0;
  946. hi.data.mp.seriobase = 0;
  947. hi.data.mp.pariobase = 0;
  948. hi.data.mp.midiiobase = 0;
  949. break;
  950. case HDLCDRVCTL_SETMODEMPAR:
  951. if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
  952. return -EACCES;
  953. dev->base_addr = hi.data.mp.iobase;
  954. dev->irq = /*hi.data.mp.irq*/0;
  955. dev->dma = /*hi.data.mp.dma*/0;
  956. return 0;
  957. case HDLCDRVCTL_GETSTAT:
  958. hi.data.cs.ptt = !!(bc->stat & EPP_PTTBIT);
  959. hi.data.cs.dcd = !(bc->stat & EPP_DCDBIT);
  960. hi.data.cs.ptt_keyed = bc->ptt_keyed;
  961. hi.data.cs.tx_packets = bc->stats.tx_packets;
  962. hi.data.cs.tx_errors = bc->stats.tx_errors;
  963. hi.data.cs.rx_packets = bc->stats.rx_packets;
  964. hi.data.cs.rx_errors = bc->stats.rx_errors;
  965. break;
  966. case HDLCDRVCTL_OLDGETSTAT:
  967. hi.data.ocs.ptt = !!(bc->stat & EPP_PTTBIT);
  968. hi.data.ocs.dcd = !(bc->stat & EPP_DCDBIT);
  969. hi.data.ocs.ptt_keyed = bc->ptt_keyed;
  970. break;
  971. case HDLCDRVCTL_CALIBRATE:
  972. if (!capable(CAP_SYS_RAWIO))
  973. return -EACCES;
  974. bc->hdlctx.calibrate = hi.data.calibrate * bc->bitrate / 8;
  975. return 0;
  976. case HDLCDRVCTL_DRIVERNAME:
  977. strncpy(hi.data.drivername, "baycom_epp", sizeof(hi.data.drivername));
  978. break;
  979. case HDLCDRVCTL_GETMODE:
  980. sprintf(hi.data.modename, "%sclk,%smodem,fclk=%d,bps=%d%s",
  981. bc->cfg.intclk ? "int" : "ext",
  982. bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
  983. bc->cfg.loopback ? ",loopback" : "");
  984. break;
  985. case HDLCDRVCTL_SETMODE:
  986. if (!capable(CAP_NET_ADMIN) || netif_running(dev))
  987. return -EACCES;
  988. hi.data.modename[sizeof(hi.data.modename)-1] = '\0';
  989. return baycom_setmode(bc, hi.data.modename);
  990. case HDLCDRVCTL_MODELIST:
  991. strncpy(hi.data.modename, "intclk,extclk,intmodem,extmodem,divider=x",
  992. sizeof(hi.data.modename));
  993. break;
  994. case HDLCDRVCTL_MODEMPARMASK:
  995. return HDLCDRV_PARMASK_IOBASE;
  996. }
  997. if (copy_to_user(ifr->ifr_data, &hi, sizeof(hi)))
  998. return -EFAULT;
  999. return 0;
  1000. }
  1001. /* --------------------------------------------------------------------- */
  1002. /*
  1003. * Check for a network adaptor of this type, and return '0' if one exists.
  1004. * If dev->base_addr == 0, probe all likely locations.
  1005. * If dev->base_addr == 1, always return failure.
  1006. * If dev->base_addr == 2, allocate space for the device and return success
  1007. * (detachable devices only).
  1008. */
  1009. static void baycom_probe(struct net_device *dev)
  1010. {
  1011. static char ax25_bcast[AX25_ADDR_LEN] = {
  1012. 'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1
  1013. };
  1014. static char ax25_nocall[AX25_ADDR_LEN] = {
  1015. 'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1
  1016. };
  1017. const struct hdlcdrv_channel_params dflt_ch_params = {
  1018. 20, 2, 10, 40, 0
  1019. };
  1020. struct baycom_state *bc;
  1021. /*
  1022. * not a real probe! only initialize data structures
  1023. */
  1024. bc = netdev_priv(dev);
  1025. /*
  1026. * initialize the baycom_state struct
  1027. */
  1028. bc->ch_params = dflt_ch_params;
  1029. bc->ptt_keyed = 0;
  1030. /*
  1031. * initialize the device struct
  1032. */
  1033. dev->open = epp_open;
  1034. dev->stop = epp_close;
  1035. dev->do_ioctl = baycom_ioctl;
  1036. dev->hard_start_xmit = baycom_send_packet;
  1037. dev->get_stats = baycom_get_stats;
  1038. /* Fill in the fields of the device structure */
  1039. bc->skb = NULL;
  1040. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  1041. dev->hard_header = ax25_encapsulate;
  1042. dev->rebuild_header = ax25_rebuild_header;
  1043. #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  1044. dev->hard_header = NULL;
  1045. dev->rebuild_header = NULL;
  1046. #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
  1047. dev->set_mac_address = baycom_set_mac_address;
  1048. dev->type = ARPHRD_AX25; /* AF_AX25 device */
  1049. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  1050. dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */
  1051. dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */
  1052. memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
  1053. memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
  1054. dev->tx_queue_len = 16;
  1055. /* New style flags */
  1056. dev->flags = 0;
  1057. }
  1058. /* --------------------------------------------------------------------- */
  1059. /*
  1060. * command line settable parameters
  1061. */
  1062. static const char *mode[NR_PORTS] = { "", };
  1063. static int iobase[NR_PORTS] = { 0x378, };
  1064. module_param_array(mode, charp, NULL, 0);
  1065. MODULE_PARM_DESC(mode, "baycom operating mode");
  1066. module_param_array(iobase, int, NULL, 0);
  1067. MODULE_PARM_DESC(iobase, "baycom io base address");
  1068. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  1069. MODULE_DESCRIPTION("Baycom epp amateur radio modem driver");
  1070. MODULE_LICENSE("GPL");
  1071. /* --------------------------------------------------------------------- */
  1072. static void __init baycom_epp_dev_setup(struct net_device *dev)
  1073. {
  1074. struct baycom_state *bc = netdev_priv(dev);
  1075. /*
  1076. * initialize part of the baycom_state struct
  1077. */
  1078. bc->magic = BAYCOM_MAGIC;
  1079. bc->cfg.fclk = 19666600;
  1080. bc->cfg.bps = 9600;
  1081. /*
  1082. * initialize part of the device struct
  1083. */
  1084. baycom_probe(dev);
  1085. }
  1086. static int __init init_baycomepp(void)
  1087. {
  1088. int i, found = 0;
  1089. char set_hw = 1;
  1090. printk(bc_drvinfo);
  1091. /*
  1092. * register net devices
  1093. */
  1094. for (i = 0; i < NR_PORTS; i++) {
  1095. struct net_device *dev;
  1096. dev = alloc_netdev(sizeof(struct baycom_state), "bce%d",
  1097. baycom_epp_dev_setup);
  1098. if (!dev) {
  1099. printk(KERN_WARNING "bce%d : out of memory\n", i);
  1100. return found ? 0 : -ENOMEM;
  1101. }
  1102. sprintf(dev->name, "bce%d", i);
  1103. dev->base_addr = iobase[i];
  1104. if (!mode[i])
  1105. set_hw = 0;
  1106. if (!set_hw)
  1107. iobase[i] = 0;
  1108. if (register_netdev(dev)) {
  1109. printk(KERN_WARNING "%s: cannot register net device %s\n", bc_drvname, dev->name);
  1110. free_netdev(dev);
  1111. break;
  1112. }
  1113. if (set_hw && baycom_setmode(netdev_priv(dev), mode[i]))
  1114. set_hw = 0;
  1115. baycom_device[i] = dev;
  1116. found++;
  1117. }
  1118. return found ? 0 : -ENXIO;
  1119. }
  1120. static void __exit cleanup_baycomepp(void)
  1121. {
  1122. int i;
  1123. for(i = 0; i < NR_PORTS; i++) {
  1124. struct net_device *dev = baycom_device[i];
  1125. if (dev) {
  1126. struct baycom_state *bc = netdev_priv(dev);
  1127. if (bc->magic == BAYCOM_MAGIC) {
  1128. unregister_netdev(dev);
  1129. free_netdev(dev);
  1130. } else
  1131. printk(paranoia_str, "cleanup_module");
  1132. }
  1133. }
  1134. }
  1135. module_init(init_baycomepp);
  1136. module_exit(cleanup_baycomepp);
  1137. /* --------------------------------------------------------------------- */
  1138. #ifndef MODULE
  1139. /*
  1140. * format: baycom_epp=io,mode
  1141. * mode: fpga config options
  1142. */
  1143. static int __init baycom_epp_setup(char *str)
  1144. {
  1145. static unsigned __initdata nr_dev = 0;
  1146. int ints[2];
  1147. if (nr_dev >= NR_PORTS)
  1148. return 0;
  1149. str = get_options(str, 2, ints);
  1150. if (ints[0] < 1)
  1151. return 0;
  1152. mode[nr_dev] = str;
  1153. iobase[nr_dev] = ints[1];
  1154. nr_dev++;
  1155. return 1;
  1156. }
  1157. __setup("baycom_epp=", baycom_epp_setup);
  1158. #endif /* MODULE */
  1159. /* --------------------------------------------------------------------- */