baycom_epp.c 35 KB

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