baycom_epp.c 35 KB

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