baycom_epp.c 34 KB

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