scc.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. #define RCS_ID "$Id: scc.c,v 1.75 1998/11/04 15:15:01 jreuter Exp jreuter $"
  2. #define VERSION "3.0"
  3. /*
  4. * Please use z8530drv-utils-3.0 with this version.
  5. * ------------------
  6. *
  7. * You can find a subset of the documentation in
  8. * Documentation/networking/z8530drv.txt.
  9. */
  10. /*
  11. ********************************************************************
  12. * SCC.C - Linux driver for Z8530 based HDLC cards for AX.25 *
  13. ********************************************************************
  14. ********************************************************************
  15. Copyright (c) 1993, 2000 Joerg Reuter DL1BKE
  16. portions (c) 1993 Guido ten Dolle PE1NNZ
  17. ********************************************************************
  18. The driver and the programs in the archive are UNDER CONSTRUCTION.
  19. The code is likely to fail, and so your kernel could --- even
  20. a whole network.
  21. This driver is intended for Amateur Radio use. If you are running it
  22. for commercial purposes, please drop me a note. I am nosy...
  23. ...BUT:
  24. ! You m u s t recognize the appropriate legislations of your country !
  25. ! before you connect a radio to the SCC board and start to transmit or !
  26. ! receive. The GPL allows you to use the d r i v e r, NOT the RADIO! !
  27. For non-Amateur-Radio use please note that you might need a special
  28. allowance/licence from the designer of the SCC Board and/or the
  29. MODEM.
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the (modified) GNU General Public License
  32. delivered with the Linux kernel source.
  33. This program is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. GNU General Public License for more details.
  37. You should find a copy of the GNU General Public License in
  38. /usr/src/linux/COPYING;
  39. ********************************************************************
  40. Incomplete history of z8530drv:
  41. -------------------------------
  42. 1994-09-13 started to write the driver, rescued most of my own
  43. code (and Hans Alblas' memory buffer pool concept) from
  44. an earlier project "sccdrv" which was initiated by
  45. Guido ten Dolle. Not much of the old driver survived,
  46. though. The first version I put my hands on was sccdrv1.3
  47. from August 1993. The memory buffer pool concept
  48. appeared in an unauthorized sccdrv version (1.5) from
  49. August 1994.
  50. 1995-01-31 changed copyright notice to GPL without limitations.
  51. .
  52. . <SNIP>
  53. .
  54. 1996-10-05 New semester, new driver...
  55. * KISS TNC emulator removed (TTY driver)
  56. * Source moved to drivers/net/
  57. * Includes Z8530 defines from drivers/net/z8530.h
  58. * Uses sk_buffer memory management
  59. * Reduced overhead of /proc/net/z8530drv output
  60. * Streamlined quite a lot things
  61. * Invents brand new bugs... ;-)
  62. The move to version number 3.0 reflects theses changes.
  63. You can use 'kissbridge' if you need a KISS TNC emulator.
  64. 1996-12-13 Fixed for Linux networking changes. (G4KLX)
  65. 1997-01-08 Fixed the remaining problems.
  66. 1997-04-02 Hopefully fixed the problems with the new *_timer()
  67. routines, added calibration code.
  68. 1997-10-12 Made SCC_DELAY a CONFIG option, added CONFIG_SCC_TRXECHO
  69. 1998-01-29 Small fix to avoid lock-up on initialization
  70. 1998-09-29 Fixed the "grouping" bugs, tx_inhibit works again,
  71. using dev->tx_queue_len now instead of MAXQUEUE now.
  72. 1998-10-21 Postponed the spinlock changes, would need a lot of
  73. testing I currently don't have the time to. Softdcd doesn't
  74. work.
  75. 1998-11-04 Softdcd does not work correctly in DPLL mode, in fact it
  76. never did. The DPLL locks on noise, the SYNC unit sees
  77. flags that aren't... Restarting the DPLL does not help
  78. either, it resynchronizes too slow and the first received
  79. frame gets lost.
  80. 2000-02-13 Fixed for new network driver interface changes, still
  81. does TX timeouts itself since it uses its own queue
  82. scheme.
  83. Thanks to all who contributed to this driver with ideas and bug
  84. reports!
  85. NB -- if you find errors, change something, please let me know
  86. first before you distribute it... And please don't touch
  87. the version number. Just replace my callsign in
  88. "v3.0.dl1bke" with your own. Just to avoid confusion...
  89. If you want to add your modification to the linux distribution
  90. please (!) contact me first.
  91. New versions of the driver will be announced on the linux-hams
  92. mailing list on vger.kernel.org. To subscribe send an e-mail
  93. to majordomo@vger.kernel.org with the following line in
  94. the body of the mail:
  95. subscribe linux-hams
  96. The content of the "Subject" field will be ignored.
  97. vy 73,
  98. Joerg Reuter ampr-net: dl1bke@db0pra.ampr.org
  99. AX-25 : DL1BKE @ DB0ABH.#BAY.DEU.EU
  100. Internet: jreuter@yaina.de
  101. www : http://yaina.de/jreuter
  102. */
  103. /* ----------------------------------------------------------------------- */
  104. #undef SCC_LDELAY /* slow it even a bit more down */
  105. #undef SCC_DONT_CHECK /* don't look if the SCCs you specified are available */
  106. #define SCC_MAXCHIPS 4 /* number of max. supported chips */
  107. #define SCC_BUFSIZE 384 /* must not exceed 4096 */
  108. #undef SCC_DEBUG
  109. #define SCC_DEFAULT_CLOCK 4915200
  110. /* default pclock if nothing is specified */
  111. /* ----------------------------------------------------------------------- */
  112. #include <linux/module.h>
  113. #include <linux/errno.h>
  114. #include <linux/signal.h>
  115. #include <linux/timer.h>
  116. #include <linux/interrupt.h>
  117. #include <linux/ioport.h>
  118. #include <linux/string.h>
  119. #include <linux/in.h>
  120. #include <linux/fcntl.h>
  121. #include <linux/ptrace.h>
  122. #include <linux/delay.h>
  123. #include <linux/skbuff.h>
  124. #include <linux/netdevice.h>
  125. #include <linux/rtnetlink.h>
  126. #include <linux/if_ether.h>
  127. #include <linux/if_arp.h>
  128. #include <linux/socket.h>
  129. #include <linux/init.h>
  130. #include <linux/scc.h>
  131. #include <linux/ctype.h>
  132. #include <linux/kernel.h>
  133. #include <linux/proc_fs.h>
  134. #include <linux/seq_file.h>
  135. #include <linux/bitops.h>
  136. #include <net/net_namespace.h>
  137. #include <net/ax25.h>
  138. #include <asm/irq.h>
  139. #include <asm/io.h>
  140. #include <linux/uaccess.h>
  141. #include "z8530.h"
  142. static const char banner[] __initconst = KERN_INFO \
  143. "AX.25: Z8530 SCC driver version "VERSION".dl1bke\n";
  144. static void t_dwait(struct timer_list *t);
  145. static void t_txdelay(struct timer_list *t);
  146. static void t_tail(struct timer_list *t);
  147. static void t_busy(struct timer_list *);
  148. static void t_maxkeyup(struct timer_list *);
  149. static void t_idle(struct timer_list *t);
  150. static void scc_tx_done(struct scc_channel *);
  151. static void scc_start_tx_timer(struct scc_channel *,
  152. void (*)(struct timer_list *), unsigned long);
  153. static void scc_start_maxkeyup(struct scc_channel *);
  154. static void scc_start_defer(struct scc_channel *);
  155. static void z8530_init(void);
  156. static void init_channel(struct scc_channel *scc);
  157. static void scc_key_trx (struct scc_channel *scc, char tx);
  158. static void scc_init_timer(struct scc_channel *scc);
  159. static int scc_net_alloc(const char *name, struct scc_channel *scc);
  160. static void scc_net_setup(struct net_device *dev);
  161. static int scc_net_open(struct net_device *dev);
  162. static int scc_net_close(struct net_device *dev);
  163. static void scc_net_rx(struct scc_channel *scc, struct sk_buff *skb);
  164. static netdev_tx_t scc_net_tx(struct sk_buff *skb,
  165. struct net_device *dev);
  166. static int scc_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  167. static int scc_net_set_mac_address(struct net_device *dev, void *addr);
  168. static struct net_device_stats * scc_net_get_stats(struct net_device *dev);
  169. static unsigned char SCC_DriverName[] = "scc";
  170. static struct irqflags { unsigned char used : 1; } Ivec[NR_IRQS];
  171. static struct scc_channel SCC_Info[2 * SCC_MAXCHIPS]; /* information per channel */
  172. static struct scc_ctrl {
  173. io_port chan_A;
  174. io_port chan_B;
  175. int irq;
  176. } SCC_ctrl[SCC_MAXCHIPS+1];
  177. static unsigned char Driver_Initialized;
  178. static int Nchips;
  179. static io_port Vector_Latch;
  180. /* ******************************************************************** */
  181. /* * Port Access Functions * */
  182. /* ******************************************************************** */
  183. /* These provide interrupt save 2-step access to the Z8530 registers */
  184. static DEFINE_SPINLOCK(iolock); /* Guards paired accesses */
  185. static inline unsigned char InReg(io_port port, unsigned char reg)
  186. {
  187. unsigned long flags;
  188. unsigned char r;
  189. spin_lock_irqsave(&iolock, flags);
  190. #ifdef SCC_LDELAY
  191. Outb(port, reg);
  192. udelay(SCC_LDELAY);
  193. r=Inb(port);
  194. udelay(SCC_LDELAY);
  195. #else
  196. Outb(port, reg);
  197. r=Inb(port);
  198. #endif
  199. spin_unlock_irqrestore(&iolock, flags);
  200. return r;
  201. }
  202. static inline void OutReg(io_port port, unsigned char reg, unsigned char val)
  203. {
  204. unsigned long flags;
  205. spin_lock_irqsave(&iolock, flags);
  206. #ifdef SCC_LDELAY
  207. Outb(port, reg); udelay(SCC_LDELAY);
  208. Outb(port, val); udelay(SCC_LDELAY);
  209. #else
  210. Outb(port, reg);
  211. Outb(port, val);
  212. #endif
  213. spin_unlock_irqrestore(&iolock, flags);
  214. }
  215. static inline void wr(struct scc_channel *scc, unsigned char reg,
  216. unsigned char val)
  217. {
  218. OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));
  219. }
  220. static inline void or(struct scc_channel *scc, unsigned char reg, unsigned char val)
  221. {
  222. OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));
  223. }
  224. static inline void cl(struct scc_channel *scc, unsigned char reg, unsigned char val)
  225. {
  226. OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));
  227. }
  228. /* ******************************************************************** */
  229. /* * Some useful macros * */
  230. /* ******************************************************************** */
  231. static inline void scc_discard_buffers(struct scc_channel *scc)
  232. {
  233. unsigned long flags;
  234. spin_lock_irqsave(&scc->lock, flags);
  235. if (scc->tx_buff != NULL)
  236. {
  237. dev_kfree_skb(scc->tx_buff);
  238. scc->tx_buff = NULL;
  239. }
  240. while (!skb_queue_empty(&scc->tx_queue))
  241. dev_kfree_skb(skb_dequeue(&scc->tx_queue));
  242. spin_unlock_irqrestore(&scc->lock, flags);
  243. }
  244. /* ******************************************************************** */
  245. /* * Interrupt Service Routines * */
  246. /* ******************************************************************** */
  247. /* ----> subroutines for the interrupt handlers <---- */
  248. static inline void scc_notify(struct scc_channel *scc, int event)
  249. {
  250. struct sk_buff *skb;
  251. char *bp;
  252. if (scc->kiss.fulldup != KISS_DUPLEX_OPTIMA)
  253. return;
  254. skb = dev_alloc_skb(2);
  255. if (skb != NULL)
  256. {
  257. bp = skb_put(skb, 2);
  258. *bp++ = PARAM_HWEVENT;
  259. *bp++ = event;
  260. scc_net_rx(scc, skb);
  261. } else
  262. scc->stat.nospace++;
  263. }
  264. static inline void flush_rx_FIFO(struct scc_channel *scc)
  265. {
  266. int k;
  267. for (k=0; k<3; k++)
  268. Inb(scc->data);
  269. if(scc->rx_buff != NULL) /* did we receive something? */
  270. {
  271. scc->stat.rxerrs++; /* then count it as an error */
  272. dev_kfree_skb_irq(scc->rx_buff);
  273. scc->rx_buff = NULL;
  274. }
  275. }
  276. static void start_hunt(struct scc_channel *scc)
  277. {
  278. if ((scc->modem.clocksrc != CLK_EXTERNAL))
  279. OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
  280. or(scc,R3,ENT_HM|RxENABLE); /* enable the receiver, hunt mode */
  281. }
  282. /* ----> four different interrupt handlers for Tx, Rx, changing of */
  283. /* DCD/CTS and Rx/Tx errors */
  284. /* Transmitter interrupt handler */
  285. static inline void scc_txint(struct scc_channel *scc)
  286. {
  287. struct sk_buff *skb;
  288. scc->stat.txints++;
  289. skb = scc->tx_buff;
  290. /* send first octet */
  291. if (skb == NULL)
  292. {
  293. skb = skb_dequeue(&scc->tx_queue);
  294. scc->tx_buff = skb;
  295. netif_wake_queue(scc->dev);
  296. if (skb == NULL)
  297. {
  298. scc_tx_done(scc);
  299. Outb(scc->ctrl, RES_Tx_P);
  300. return;
  301. }
  302. if (skb->len == 0) /* Paranoia... */
  303. {
  304. dev_kfree_skb_irq(skb);
  305. scc->tx_buff = NULL;
  306. scc_tx_done(scc);
  307. Outb(scc->ctrl, RES_Tx_P);
  308. return;
  309. }
  310. scc->stat.tx_state = TXS_ACTIVE;
  311. OutReg(scc->ctrl, R0, RES_Tx_CRC);
  312. /* reset CRC generator */
  313. or(scc,R10,ABUNDER); /* re-install underrun protection */
  314. Outb(scc->data,*skb->data); /* send byte */
  315. skb_pull(skb, 1);
  316. if (!scc->enhanced) /* reset EOM latch */
  317. Outb(scc->ctrl,RES_EOM_L);
  318. return;
  319. }
  320. /* End Of Frame... */
  321. if (skb->len == 0)
  322. {
  323. Outb(scc->ctrl, RES_Tx_P); /* reset pending int */
  324. cl(scc, R10, ABUNDER); /* send CRC */
  325. dev_kfree_skb_irq(skb);
  326. scc->tx_buff = NULL;
  327. scc->stat.tx_state = TXS_NEWFRAME; /* next frame... */
  328. return;
  329. }
  330. /* send octet */
  331. Outb(scc->data,*skb->data);
  332. skb_pull(skb, 1);
  333. }
  334. /* External/Status interrupt handler */
  335. static inline void scc_exint(struct scc_channel *scc)
  336. {
  337. unsigned char status,changes,chg_and_stat;
  338. scc->stat.exints++;
  339. status = InReg(scc->ctrl,R0);
  340. changes = status ^ scc->status;
  341. chg_and_stat = changes & status;
  342. /* ABORT: generated whenever DCD drops while receiving */
  343. if (chg_and_stat & BRK_ABRT) /* Received an ABORT */
  344. flush_rx_FIFO(scc);
  345. /* HUNT: software DCD; on = waiting for SYNC, off = receiving frame */
  346. if ((changes & SYNC_HUNT) && scc->kiss.softdcd)
  347. {
  348. if (status & SYNC_HUNT)
  349. {
  350. scc->dcd = 0;
  351. flush_rx_FIFO(scc);
  352. if ((scc->modem.clocksrc != CLK_EXTERNAL))
  353. OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
  354. } else {
  355. scc->dcd = 1;
  356. }
  357. scc_notify(scc, scc->dcd? HWEV_DCD_OFF:HWEV_DCD_ON);
  358. }
  359. /* DCD: on = start to receive packet, off = ABORT condition */
  360. /* (a successfully received packet generates a special condition int) */
  361. if((changes & DCD) && !scc->kiss.softdcd) /* DCD input changed state */
  362. {
  363. if(status & DCD) /* DCD is now ON */
  364. {
  365. start_hunt(scc);
  366. scc->dcd = 1;
  367. } else { /* DCD is now OFF */
  368. cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
  369. flush_rx_FIFO(scc);
  370. scc->dcd = 0;
  371. }
  372. scc_notify(scc, scc->dcd? HWEV_DCD_ON:HWEV_DCD_OFF);
  373. }
  374. #ifdef notdef
  375. /* CTS: use external TxDelay (what's that good for?!)
  376. * Anyway: If we _could_ use it (BayCom USCC uses CTS for
  377. * own purposes) we _should_ use the "autoenable" feature
  378. * of the Z8530 and not this interrupt...
  379. */
  380. if (chg_and_stat & CTS) /* CTS is now ON */
  381. {
  382. if (scc->kiss.txdelay == 0) /* zero TXDELAY = wait for CTS */
  383. scc_start_tx_timer(scc, t_txdelay, 0);
  384. }
  385. #endif
  386. if (scc->stat.tx_state == TXS_ACTIVE && (status & TxEOM))
  387. {
  388. scc->stat.tx_under++; /* oops, an underrun! count 'em */
  389. Outb(scc->ctrl, RES_EXT_INT); /* reset ext/status interrupts */
  390. if (scc->tx_buff != NULL)
  391. {
  392. dev_kfree_skb_irq(scc->tx_buff);
  393. scc->tx_buff = NULL;
  394. }
  395. or(scc,R10,ABUNDER);
  396. scc_start_tx_timer(scc, t_txdelay, 0); /* restart transmission */
  397. }
  398. scc->status = status;
  399. Outb(scc->ctrl,RES_EXT_INT);
  400. }
  401. /* Receiver interrupt handler */
  402. static inline void scc_rxint(struct scc_channel *scc)
  403. {
  404. struct sk_buff *skb;
  405. scc->stat.rxints++;
  406. if((scc->wreg[5] & RTS) && scc->kiss.fulldup == KISS_DUPLEX_HALF)
  407. {
  408. Inb(scc->data); /* discard char */
  409. or(scc,R3,ENT_HM); /* enter hunt mode for next flag */
  410. return;
  411. }
  412. skb = scc->rx_buff;
  413. if (skb == NULL)
  414. {
  415. skb = dev_alloc_skb(scc->stat.bufsize);
  416. if (skb == NULL)
  417. {
  418. scc->dev_stat.rx_dropped++;
  419. scc->stat.nospace++;
  420. Inb(scc->data);
  421. or(scc, R3, ENT_HM);
  422. return;
  423. }
  424. scc->rx_buff = skb;
  425. skb_put_u8(skb, 0); /* KISS data */
  426. }
  427. if (skb->len >= scc->stat.bufsize)
  428. {
  429. #ifdef notdef
  430. printk(KERN_DEBUG "z8530drv: oops, scc_rxint() received huge frame...\n");
  431. #endif
  432. dev_kfree_skb_irq(skb);
  433. scc->rx_buff = NULL;
  434. Inb(scc->data);
  435. or(scc, R3, ENT_HM);
  436. return;
  437. }
  438. skb_put_u8(skb, Inb(scc->data));
  439. }
  440. /* Receive Special Condition interrupt handler */
  441. static inline void scc_spint(struct scc_channel *scc)
  442. {
  443. unsigned char status;
  444. struct sk_buff *skb;
  445. scc->stat.spints++;
  446. status = InReg(scc->ctrl,R1); /* read receiver status */
  447. Inb(scc->data); /* throw away Rx byte */
  448. skb = scc->rx_buff;
  449. if(status & Rx_OVR) /* receiver overrun */
  450. {
  451. scc->stat.rx_over++; /* count them */
  452. or(scc,R3,ENT_HM); /* enter hunt mode for next flag */
  453. if (skb != NULL)
  454. dev_kfree_skb_irq(skb);
  455. scc->rx_buff = skb = NULL;
  456. }
  457. if(status & END_FR && skb != NULL) /* end of frame */
  458. {
  459. /* CRC okay, frame ends on 8 bit boundary and received something ? */
  460. if (!(status & CRC_ERR) && (status & 0xe) == RES8 && skb->len > 0)
  461. {
  462. /* ignore last received byte (first of the CRC bytes) */
  463. skb_trim(skb, skb->len-1);
  464. scc_net_rx(scc, skb);
  465. scc->rx_buff = NULL;
  466. scc->stat.rxframes++;
  467. } else { /* a bad frame */
  468. dev_kfree_skb_irq(skb);
  469. scc->rx_buff = NULL;
  470. scc->stat.rxerrs++;
  471. }
  472. }
  473. Outb(scc->ctrl,ERR_RES);
  474. }
  475. /* ----> interrupt service routine for the Z8530 <---- */
  476. static void scc_isr_dispatch(struct scc_channel *scc, int vector)
  477. {
  478. spin_lock(&scc->lock);
  479. switch (vector & VECTOR_MASK)
  480. {
  481. case TXINT: scc_txint(scc); break;
  482. case EXINT: scc_exint(scc); break;
  483. case RXINT: scc_rxint(scc); break;
  484. case SPINT: scc_spint(scc); break;
  485. }
  486. spin_unlock(&scc->lock);
  487. }
  488. /* If the card has a latch for the interrupt vector (like the PA0HZP card)
  489. use it to get the number of the chip that generated the int.
  490. If not: poll all defined chips.
  491. */
  492. #define SCC_IRQTIMEOUT 30000
  493. static irqreturn_t scc_isr(int irq, void *dev_id)
  494. {
  495. int chip_irq = (long) dev_id;
  496. unsigned char vector;
  497. struct scc_channel *scc;
  498. struct scc_ctrl *ctrl;
  499. int k;
  500. if (Vector_Latch)
  501. {
  502. for(k=0; k < SCC_IRQTIMEOUT; k++)
  503. {
  504. Outb(Vector_Latch, 0); /* Generate INTACK */
  505. /* Read the vector */
  506. if((vector=Inb(Vector_Latch)) >= 16 * Nchips) break;
  507. if (vector & 0x01) break;
  508. scc=&SCC_Info[vector >> 3 ^ 0x01];
  509. if (!scc->dev) break;
  510. scc_isr_dispatch(scc, vector);
  511. OutReg(scc->ctrl,R0,RES_H_IUS); /* Reset Highest IUS */
  512. }
  513. if (k == SCC_IRQTIMEOUT)
  514. printk(KERN_WARNING "z8530drv: endless loop in scc_isr()?\n");
  515. return IRQ_HANDLED;
  516. }
  517. /* Find the SCC generating the interrupt by polling all attached SCCs
  518. * reading RR3A (the interrupt pending register)
  519. */
  520. ctrl = SCC_ctrl;
  521. while (ctrl->chan_A)
  522. {
  523. if (ctrl->irq != chip_irq)
  524. {
  525. ctrl++;
  526. continue;
  527. }
  528. scc = NULL;
  529. for (k = 0; InReg(ctrl->chan_A,R3) && k < SCC_IRQTIMEOUT; k++)
  530. {
  531. vector=InReg(ctrl->chan_B,R2); /* Read the vector */
  532. if (vector & 0x01) break;
  533. scc = &SCC_Info[vector >> 3 ^ 0x01];
  534. if (!scc->dev) break;
  535. scc_isr_dispatch(scc, vector);
  536. }
  537. if (k == SCC_IRQTIMEOUT)
  538. {
  539. printk(KERN_WARNING "z8530drv: endless loop in scc_isr()?!\n");
  540. break;
  541. }
  542. /* This looks weird and it is. At least the BayCom USCC doesn't
  543. * use the Interrupt Daisy Chain, thus we'll have to start
  544. * all over again to be sure not to miss an interrupt from
  545. * (any of) the other chip(s)...
  546. * Honestly, the situation *is* braindamaged...
  547. */
  548. if (scc != NULL)
  549. {
  550. OutReg(scc->ctrl,R0,RES_H_IUS);
  551. ctrl = SCC_ctrl;
  552. } else
  553. ctrl++;
  554. }
  555. return IRQ_HANDLED;
  556. }
  557. /* ******************************************************************** */
  558. /* * Init Channel */
  559. /* ******************************************************************** */
  560. /* ----> set SCC channel speed <---- */
  561. static inline void set_brg(struct scc_channel *scc, unsigned int tc)
  562. {
  563. cl(scc,R14,BRENABL); /* disable baudrate generator */
  564. wr(scc,R12,tc & 255); /* brg rate LOW */
  565. wr(scc,R13,tc >> 8); /* brg rate HIGH */
  566. or(scc,R14,BRENABL); /* enable baudrate generator */
  567. }
  568. static inline void set_speed(struct scc_channel *scc)
  569. {
  570. unsigned long flags;
  571. spin_lock_irqsave(&scc->lock, flags);
  572. if (scc->modem.speed > 0) /* paranoia... */
  573. set_brg(scc, (unsigned) (scc->clock / (scc->modem.speed * 64)) - 2);
  574. spin_unlock_irqrestore(&scc->lock, flags);
  575. }
  576. /* ----> initialize a SCC channel <---- */
  577. static inline void init_brg(struct scc_channel *scc)
  578. {
  579. wr(scc, R14, BRSRC); /* BRG source = PCLK */
  580. OutReg(scc->ctrl, R14, SSBR|scc->wreg[R14]); /* DPLL source = BRG */
  581. OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]); /* DPLL NRZI mode */
  582. }
  583. /*
  584. * Initialization according to the Z8530 manual (SGS-Thomson's version):
  585. *
  586. * 1. Modes and constants
  587. *
  588. * WR9 11000000 chip reset
  589. * WR4 XXXXXXXX Tx/Rx control, async or sync mode
  590. * WR1 0XX00X00 select W/REQ (optional)
  591. * WR2 XXXXXXXX program interrupt vector
  592. * WR3 XXXXXXX0 select Rx control
  593. * WR5 XXXX0XXX select Tx control
  594. * WR6 XXXXXXXX sync character
  595. * WR7 XXXXXXXX sync character
  596. * WR9 000X0XXX select interrupt control
  597. * WR10 XXXXXXXX miscellaneous control (optional)
  598. * WR11 XXXXXXXX clock control
  599. * WR12 XXXXXXXX time constant lower byte (optional)
  600. * WR13 XXXXXXXX time constant upper byte (optional)
  601. * WR14 XXXXXXX0 miscellaneous control
  602. * WR14 XXXSSSSS commands (optional)
  603. *
  604. * 2. Enables
  605. *
  606. * WR14 000SSSS1 baud rate enable
  607. * WR3 SSSSSSS1 Rx enable
  608. * WR5 SSSS1SSS Tx enable
  609. * WR0 10000000 reset Tx CRG (optional)
  610. * WR1 XSS00S00 DMA enable (optional)
  611. *
  612. * 3. Interrupt status
  613. *
  614. * WR15 XXXXXXXX enable external/status
  615. * WR0 00010000 reset external status
  616. * WR0 00010000 reset external status twice
  617. * WR1 SSSXXSXX enable Rx, Tx and Ext/status
  618. * WR9 000SXSSS enable master interrupt enable
  619. *
  620. * 1 = set to one, 0 = reset to zero
  621. * X = user defined, S = same as previous init
  622. *
  623. *
  624. * Note that the implementation differs in some points from above scheme.
  625. *
  626. */
  627. static void init_channel(struct scc_channel *scc)
  628. {
  629. del_timer(&scc->tx_t);
  630. del_timer(&scc->tx_wdog);
  631. disable_irq(scc->irq);
  632. wr(scc,R4,X1CLK|SDLC); /* *1 clock, SDLC mode */
  633. wr(scc,R1,0); /* no W/REQ operation */
  634. wr(scc,R3,Rx8|RxCRC_ENAB); /* RX 8 bits/char, CRC, disabled */
  635. wr(scc,R5,Tx8|DTR|TxCRC_ENAB); /* TX 8 bits/char, disabled, DTR */
  636. wr(scc,R6,0); /* SDLC address zero (not used) */
  637. wr(scc,R7,FLAG); /* SDLC flag value */
  638. wr(scc,R9,VIS); /* vector includes status */
  639. wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER); /* abort on underrun, preset CRC generator, NRZ(I) */
  640. wr(scc,R14, 0);
  641. /* set clock sources:
  642. CLK_DPLL: normal halfduplex operation
  643. RxClk: use DPLL
  644. TxClk: use DPLL
  645. TRxC mode DPLL output
  646. CLK_EXTERNAL: external clocking (G3RUH or DF9IC modem)
  647. BayCom: others:
  648. TxClk = pin RTxC TxClk = pin TRxC
  649. RxClk = pin TRxC RxClk = pin RTxC
  650. CLK_DIVIDER:
  651. RxClk = use DPLL
  652. TxClk = pin RTxC
  653. BayCom: others:
  654. pin TRxC = DPLL pin TRxC = BRG
  655. (RxClk * 1) (RxClk * 32)
  656. */
  657. switch(scc->modem.clocksrc)
  658. {
  659. case CLK_DPLL:
  660. wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
  661. init_brg(scc);
  662. break;
  663. case CLK_DIVIDER:
  664. wr(scc, R11, ((scc->brand & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
  665. init_brg(scc);
  666. break;
  667. case CLK_EXTERNAL:
  668. wr(scc, R11, (scc->brand & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
  669. OutReg(scc->ctrl, R14, DISDPLL);
  670. break;
  671. }
  672. set_speed(scc); /* set baudrate */
  673. if(scc->enhanced)
  674. {
  675. or(scc,R15,SHDLCE|FIFOE); /* enable FIFO, SDLC/HDLC Enhancements (From now R7 is R7') */
  676. wr(scc,R7,AUTOEOM);
  677. }
  678. if(scc->kiss.softdcd || (InReg(scc->ctrl,R0) & DCD))
  679. /* DCD is now ON */
  680. {
  681. start_hunt(scc);
  682. }
  683. /* enable ABORT, DCD & SYNC/HUNT interrupts */
  684. wr(scc,R15, BRKIE|TxUIE|(scc->kiss.softdcd? SYNCIE:DCDIE));
  685. Outb(scc->ctrl,RES_EXT_INT); /* reset ext/status interrupts */
  686. Outb(scc->ctrl,RES_EXT_INT); /* must be done twice */
  687. or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
  688. scc->status = InReg(scc->ctrl,R0); /* read initial status */
  689. or(scc,R9,MIE); /* master interrupt enable */
  690. scc_init_timer(scc);
  691. enable_irq(scc->irq);
  692. }
  693. /* ******************************************************************** */
  694. /* * SCC timer functions * */
  695. /* ******************************************************************** */
  696. /* ----> scc_key_trx sets the time constant for the baudrate
  697. generator and keys the transmitter <---- */
  698. static void scc_key_trx(struct scc_channel *scc, char tx)
  699. {
  700. unsigned int time_const;
  701. if (scc->brand & PRIMUS)
  702. Outb(scc->ctrl + 4, scc->option | (tx? 0x80 : 0));
  703. if (scc->modem.speed < 300)
  704. scc->modem.speed = 1200;
  705. time_const = (unsigned) (scc->clock / (scc->modem.speed * (tx? 2:64))) - 2;
  706. disable_irq(scc->irq);
  707. if (tx)
  708. {
  709. or(scc, R1, TxINT_ENAB); /* t_maxkeyup may have reset these */
  710. or(scc, R15, TxUIE);
  711. }
  712. if (scc->modem.clocksrc == CLK_DPLL)
  713. { /* force simplex operation */
  714. if (tx)
  715. {
  716. #ifdef CONFIG_SCC_TRXECHO
  717. cl(scc, R3, RxENABLE|ENT_HM); /* switch off receiver */
  718. cl(scc, R15, DCDIE|SYNCIE); /* No DCD changes, please */
  719. #endif
  720. set_brg(scc, time_const); /* reprogram baudrate generator */
  721. /* DPLL -> Rx clk, BRG -> Tx CLK, TRxC mode output, TRxC = BRG */
  722. wr(scc, R11, RCDPLL|TCBR|TRxCOI|TRxCBR);
  723. /* By popular demand: tx_inhibit */
  724. if (scc->kiss.tx_inhibit)
  725. {
  726. or(scc,R5, TxENAB);
  727. scc->wreg[R5] |= RTS;
  728. } else {
  729. or(scc,R5,RTS|TxENAB); /* set the RTS line and enable TX */
  730. }
  731. } else {
  732. cl(scc,R5,RTS|TxENAB);
  733. set_brg(scc, time_const); /* reprogram baudrate generator */
  734. /* DPLL -> Rx clk, DPLL -> Tx CLK, TRxC mode output, TRxC = DPLL */
  735. wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
  736. #ifndef CONFIG_SCC_TRXECHO
  737. if (scc->kiss.softdcd)
  738. #endif
  739. {
  740. or(scc,R15, scc->kiss.softdcd? SYNCIE:DCDIE);
  741. start_hunt(scc);
  742. }
  743. }
  744. } else {
  745. if (tx)
  746. {
  747. #ifdef CONFIG_SCC_TRXECHO
  748. if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
  749. {
  750. cl(scc, R3, RxENABLE);
  751. cl(scc, R15, DCDIE|SYNCIE);
  752. }
  753. #endif
  754. if (scc->kiss.tx_inhibit)
  755. {
  756. or(scc,R5, TxENAB);
  757. scc->wreg[R5] |= RTS;
  758. } else {
  759. or(scc,R5,RTS|TxENAB); /* enable tx */
  760. }
  761. } else {
  762. cl(scc,R5,RTS|TxENAB); /* disable tx */
  763. if ((scc->kiss.fulldup == KISS_DUPLEX_HALF) &&
  764. #ifndef CONFIG_SCC_TRXECHO
  765. scc->kiss.softdcd)
  766. #else
  767. 1)
  768. #endif
  769. {
  770. or(scc, R15, scc->kiss.softdcd? SYNCIE:DCDIE);
  771. start_hunt(scc);
  772. }
  773. }
  774. }
  775. enable_irq(scc->irq);
  776. }
  777. /* ----> SCC timer interrupt handler and friends. <---- */
  778. static void __scc_start_tx_timer(struct scc_channel *scc,
  779. void (*handler)(struct timer_list *t),
  780. unsigned long when)
  781. {
  782. del_timer(&scc->tx_t);
  783. if (when == 0)
  784. {
  785. handler(&scc->tx_t);
  786. } else
  787. if (when != TIMER_OFF)
  788. {
  789. scc->tx_t.function = handler;
  790. scc->tx_t.expires = jiffies + (when*HZ)/100;
  791. add_timer(&scc->tx_t);
  792. }
  793. }
  794. static void scc_start_tx_timer(struct scc_channel *scc,
  795. void (*handler)(struct timer_list *t),
  796. unsigned long when)
  797. {
  798. unsigned long flags;
  799. spin_lock_irqsave(&scc->lock, flags);
  800. __scc_start_tx_timer(scc, handler, when);
  801. spin_unlock_irqrestore(&scc->lock, flags);
  802. }
  803. static void scc_start_defer(struct scc_channel *scc)
  804. {
  805. unsigned long flags;
  806. spin_lock_irqsave(&scc->lock, flags);
  807. del_timer(&scc->tx_wdog);
  808. if (scc->kiss.maxdefer != 0 && scc->kiss.maxdefer != TIMER_OFF)
  809. {
  810. scc->tx_wdog.function = t_busy;
  811. scc->tx_wdog.expires = jiffies + HZ*scc->kiss.maxdefer;
  812. add_timer(&scc->tx_wdog);
  813. }
  814. spin_unlock_irqrestore(&scc->lock, flags);
  815. }
  816. static void scc_start_maxkeyup(struct scc_channel *scc)
  817. {
  818. unsigned long flags;
  819. spin_lock_irqsave(&scc->lock, flags);
  820. del_timer(&scc->tx_wdog);
  821. if (scc->kiss.maxkeyup != 0 && scc->kiss.maxkeyup != TIMER_OFF)
  822. {
  823. scc->tx_wdog.function = t_maxkeyup;
  824. scc->tx_wdog.expires = jiffies + HZ*scc->kiss.maxkeyup;
  825. add_timer(&scc->tx_wdog);
  826. }
  827. spin_unlock_irqrestore(&scc->lock, flags);
  828. }
  829. /*
  830. * This is called from scc_txint() when there are no more frames to send.
  831. * Not exactly a timer function, but it is a close friend of the family...
  832. */
  833. static void scc_tx_done(struct scc_channel *scc)
  834. {
  835. /*
  836. * trx remains keyed in fulldup mode 2 until t_idle expires.
  837. */
  838. switch (scc->kiss.fulldup)
  839. {
  840. case KISS_DUPLEX_LINK:
  841. scc->stat.tx_state = TXS_IDLE2;
  842. if (scc->kiss.idletime != TIMER_OFF)
  843. scc_start_tx_timer(scc, t_idle,
  844. scc->kiss.idletime*100);
  845. break;
  846. case KISS_DUPLEX_OPTIMA:
  847. scc_notify(scc, HWEV_ALL_SENT);
  848. break;
  849. default:
  850. scc->stat.tx_state = TXS_BUSY;
  851. scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
  852. }
  853. netif_wake_queue(scc->dev);
  854. }
  855. static unsigned char Rand = 17;
  856. static inline int is_grouped(struct scc_channel *scc)
  857. {
  858. int k;
  859. struct scc_channel *scc2;
  860. unsigned char grp1, grp2;
  861. grp1 = scc->kiss.group;
  862. for (k = 0; k < (Nchips * 2); k++)
  863. {
  864. scc2 = &SCC_Info[k];
  865. grp2 = scc2->kiss.group;
  866. if (scc2 == scc || !(scc2->dev && grp2))
  867. continue;
  868. if ((grp1 & 0x3f) == (grp2 & 0x3f))
  869. {
  870. if ( (grp1 & TXGROUP) && (scc2->wreg[R5] & RTS) )
  871. return 1;
  872. if ( (grp1 & RXGROUP) && scc2->dcd )
  873. return 1;
  874. }
  875. }
  876. return 0;
  877. }
  878. /* DWAIT and SLOTTIME expired
  879. *
  880. * fulldup == 0: DCD is active or Rand > P-persistence: start t_busy timer
  881. * else key trx and start txdelay
  882. * fulldup == 1: key trx and start txdelay
  883. * fulldup == 2: mintime expired, reset status or key trx and start txdelay
  884. */
  885. static void t_dwait(struct timer_list *t)
  886. {
  887. struct scc_channel *scc = from_timer(scc, t, tx_t);
  888. if (scc->stat.tx_state == TXS_WAIT) /* maxkeyup or idle timeout */
  889. {
  890. if (skb_queue_empty(&scc->tx_queue)) { /* nothing to send */
  891. scc->stat.tx_state = TXS_IDLE;
  892. netif_wake_queue(scc->dev); /* t_maxkeyup locked it. */
  893. return;
  894. }
  895. scc->stat.tx_state = TXS_BUSY;
  896. }
  897. if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
  898. {
  899. Rand = Rand * 17 + 31;
  900. if (scc->dcd || (scc->kiss.persist) < Rand || (scc->kiss.group && is_grouped(scc)) )
  901. {
  902. scc_start_defer(scc);
  903. scc_start_tx_timer(scc, t_dwait, scc->kiss.slottime);
  904. return ;
  905. }
  906. }
  907. if ( !(scc->wreg[R5] & RTS) )
  908. {
  909. scc_key_trx(scc, TX_ON);
  910. scc_start_tx_timer(scc, t_txdelay, scc->kiss.txdelay);
  911. } else {
  912. scc_start_tx_timer(scc, t_txdelay, 0);
  913. }
  914. }
  915. /* TXDELAY expired
  916. *
  917. * kick transmission by a fake scc_txint(scc), start 'maxkeyup' watchdog.
  918. */
  919. static void t_txdelay(struct timer_list *t)
  920. {
  921. struct scc_channel *scc = from_timer(scc, t, tx_t);
  922. scc_start_maxkeyup(scc);
  923. if (scc->tx_buff == NULL)
  924. {
  925. disable_irq(scc->irq);
  926. scc_txint(scc);
  927. enable_irq(scc->irq);
  928. }
  929. }
  930. /* TAILTIME expired
  931. *
  932. * switch off transmitter. If we were stopped by Maxkeyup restart
  933. * transmission after 'mintime' seconds
  934. */
  935. static void t_tail(struct timer_list *t)
  936. {
  937. struct scc_channel *scc = from_timer(scc, t, tx_t);
  938. unsigned long flags;
  939. spin_lock_irqsave(&scc->lock, flags);
  940. del_timer(&scc->tx_wdog);
  941. scc_key_trx(scc, TX_OFF);
  942. spin_unlock_irqrestore(&scc->lock, flags);
  943. if (scc->stat.tx_state == TXS_TIMEOUT) /* we had a timeout? */
  944. {
  945. scc->stat.tx_state = TXS_WAIT;
  946. scc_start_tx_timer(scc, t_dwait, scc->kiss.mintime*100);
  947. return;
  948. }
  949. scc->stat.tx_state = TXS_IDLE;
  950. netif_wake_queue(scc->dev);
  951. }
  952. /* BUSY timeout
  953. *
  954. * throw away send buffers if DCD remains active too long.
  955. */
  956. static void t_busy(struct timer_list *t)
  957. {
  958. struct scc_channel *scc = from_timer(scc, t, tx_wdog);
  959. del_timer(&scc->tx_t);
  960. netif_stop_queue(scc->dev); /* don't pile on the wabbit! */
  961. scc_discard_buffers(scc);
  962. scc->stat.txerrs++;
  963. scc->stat.tx_state = TXS_IDLE;
  964. netif_wake_queue(scc->dev);
  965. }
  966. /* MAXKEYUP timeout
  967. *
  968. * this is our watchdog.
  969. */
  970. static void t_maxkeyup(struct timer_list *t)
  971. {
  972. struct scc_channel *scc = from_timer(scc, t, tx_wdog);
  973. unsigned long flags;
  974. spin_lock_irqsave(&scc->lock, flags);
  975. /*
  976. * let things settle down before we start to
  977. * accept new data.
  978. */
  979. netif_stop_queue(scc->dev);
  980. scc_discard_buffers(scc);
  981. del_timer(&scc->tx_t);
  982. cl(scc, R1, TxINT_ENAB); /* force an ABORT, but don't */
  983. cl(scc, R15, TxUIE); /* count it. */
  984. OutReg(scc->ctrl, R0, RES_Tx_P);
  985. spin_unlock_irqrestore(&scc->lock, flags);
  986. scc->stat.txerrs++;
  987. scc->stat.tx_state = TXS_TIMEOUT;
  988. scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
  989. }
  990. /* IDLE timeout
  991. *
  992. * in fulldup mode 2 it keys down the transmitter after 'idle' seconds
  993. * of inactivity. We will not restart transmission before 'mintime'
  994. * expires.
  995. */
  996. static void t_idle(struct timer_list *t)
  997. {
  998. struct scc_channel *scc = from_timer(scc, t, tx_t);
  999. del_timer(&scc->tx_wdog);
  1000. scc_key_trx(scc, TX_OFF);
  1001. if(scc->kiss.mintime)
  1002. scc_start_tx_timer(scc, t_dwait, scc->kiss.mintime*100);
  1003. scc->stat.tx_state = TXS_WAIT;
  1004. }
  1005. static void scc_init_timer(struct scc_channel *scc)
  1006. {
  1007. unsigned long flags;
  1008. spin_lock_irqsave(&scc->lock, flags);
  1009. scc->stat.tx_state = TXS_IDLE;
  1010. spin_unlock_irqrestore(&scc->lock, flags);
  1011. }
  1012. /* ******************************************************************** */
  1013. /* * Set/get L1 parameters * */
  1014. /* ******************************************************************** */
  1015. /*
  1016. * this will set the "hardware" parameters through KISS commands or ioctl()
  1017. */
  1018. #define CAST(x) (unsigned long)(x)
  1019. static unsigned int scc_set_param(struct scc_channel *scc, unsigned int cmd, unsigned int arg)
  1020. {
  1021. switch (cmd)
  1022. {
  1023. case PARAM_TXDELAY: scc->kiss.txdelay=arg; break;
  1024. case PARAM_PERSIST: scc->kiss.persist=arg; break;
  1025. case PARAM_SLOTTIME: scc->kiss.slottime=arg; break;
  1026. case PARAM_TXTAIL: scc->kiss.tailtime=arg; break;
  1027. case PARAM_FULLDUP: scc->kiss.fulldup=arg; break;
  1028. case PARAM_DTR: break; /* does someone need this? */
  1029. case PARAM_GROUP: scc->kiss.group=arg; break;
  1030. case PARAM_IDLE: scc->kiss.idletime=arg; break;
  1031. case PARAM_MIN: scc->kiss.mintime=arg; break;
  1032. case PARAM_MAXKEY: scc->kiss.maxkeyup=arg; break;
  1033. case PARAM_WAIT: scc->kiss.waittime=arg; break;
  1034. case PARAM_MAXDEFER: scc->kiss.maxdefer=arg; break;
  1035. case PARAM_TX: scc->kiss.tx_inhibit=arg; break;
  1036. case PARAM_SOFTDCD:
  1037. scc->kiss.softdcd=arg;
  1038. if (arg)
  1039. {
  1040. or(scc, R15, SYNCIE);
  1041. cl(scc, R15, DCDIE);
  1042. start_hunt(scc);
  1043. } else {
  1044. or(scc, R15, DCDIE);
  1045. cl(scc, R15, SYNCIE);
  1046. }
  1047. break;
  1048. case PARAM_SPEED:
  1049. if (arg < 256)
  1050. scc->modem.speed=arg*100;
  1051. else
  1052. scc->modem.speed=arg;
  1053. if (scc->stat.tx_state == 0) /* only switch baudrate on rx... ;-) */
  1054. set_speed(scc);
  1055. break;
  1056. case PARAM_RTS:
  1057. if ( !(scc->wreg[R5] & RTS) )
  1058. {
  1059. if (arg != TX_OFF) {
  1060. scc_key_trx(scc, TX_ON);
  1061. scc_start_tx_timer(scc, t_txdelay, scc->kiss.txdelay);
  1062. }
  1063. } else {
  1064. if (arg == TX_OFF)
  1065. {
  1066. scc->stat.tx_state = TXS_BUSY;
  1067. scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
  1068. }
  1069. }
  1070. break;
  1071. case PARAM_HWEVENT:
  1072. scc_notify(scc, scc->dcd? HWEV_DCD_ON:HWEV_DCD_OFF);
  1073. break;
  1074. default: return -EINVAL;
  1075. }
  1076. return 0;
  1077. }
  1078. static unsigned long scc_get_param(struct scc_channel *scc, unsigned int cmd)
  1079. {
  1080. switch (cmd)
  1081. {
  1082. case PARAM_TXDELAY: return CAST(scc->kiss.txdelay);
  1083. case PARAM_PERSIST: return CAST(scc->kiss.persist);
  1084. case PARAM_SLOTTIME: return CAST(scc->kiss.slottime);
  1085. case PARAM_TXTAIL: return CAST(scc->kiss.tailtime);
  1086. case PARAM_FULLDUP: return CAST(scc->kiss.fulldup);
  1087. case PARAM_SOFTDCD: return CAST(scc->kiss.softdcd);
  1088. case PARAM_DTR: return CAST((scc->wreg[R5] & DTR)? 1:0);
  1089. case PARAM_RTS: return CAST((scc->wreg[R5] & RTS)? 1:0);
  1090. case PARAM_SPEED: return CAST(scc->modem.speed);
  1091. case PARAM_GROUP: return CAST(scc->kiss.group);
  1092. case PARAM_IDLE: return CAST(scc->kiss.idletime);
  1093. case PARAM_MIN: return CAST(scc->kiss.mintime);
  1094. case PARAM_MAXKEY: return CAST(scc->kiss.maxkeyup);
  1095. case PARAM_WAIT: return CAST(scc->kiss.waittime);
  1096. case PARAM_MAXDEFER: return CAST(scc->kiss.maxdefer);
  1097. case PARAM_TX: return CAST(scc->kiss.tx_inhibit);
  1098. default: return NO_SUCH_PARAM;
  1099. }
  1100. }
  1101. #undef CAST
  1102. /* ******************************************************************* */
  1103. /* * Send calibration pattern * */
  1104. /* ******************************************************************* */
  1105. static void scc_stop_calibrate(struct timer_list *t)
  1106. {
  1107. struct scc_channel *scc = from_timer(scc, t, tx_wdog);
  1108. unsigned long flags;
  1109. spin_lock_irqsave(&scc->lock, flags);
  1110. del_timer(&scc->tx_wdog);
  1111. scc_key_trx(scc, TX_OFF);
  1112. wr(scc, R6, 0);
  1113. wr(scc, R7, FLAG);
  1114. Outb(scc->ctrl,RES_EXT_INT); /* reset ext/status interrupts */
  1115. Outb(scc->ctrl,RES_EXT_INT);
  1116. netif_wake_queue(scc->dev);
  1117. spin_unlock_irqrestore(&scc->lock, flags);
  1118. }
  1119. static void
  1120. scc_start_calibrate(struct scc_channel *scc, int duration, unsigned char pattern)
  1121. {
  1122. unsigned long flags;
  1123. spin_lock_irqsave(&scc->lock, flags);
  1124. netif_stop_queue(scc->dev);
  1125. scc_discard_buffers(scc);
  1126. del_timer(&scc->tx_wdog);
  1127. scc->tx_wdog.function = scc_stop_calibrate;
  1128. scc->tx_wdog.expires = jiffies + HZ*duration;
  1129. add_timer(&scc->tx_wdog);
  1130. /* This doesn't seem to work. Why not? */
  1131. wr(scc, R6, 0);
  1132. wr(scc, R7, pattern);
  1133. /*
  1134. * Don't know if this works.
  1135. * Damn, where is my Z8530 programming manual...?
  1136. */
  1137. Outb(scc->ctrl,RES_EXT_INT); /* reset ext/status interrupts */
  1138. Outb(scc->ctrl,RES_EXT_INT);
  1139. scc_key_trx(scc, TX_ON);
  1140. spin_unlock_irqrestore(&scc->lock, flags);
  1141. }
  1142. /* ******************************************************************* */
  1143. /* * Init channel structures, special HW, etc... * */
  1144. /* ******************************************************************* */
  1145. /*
  1146. * Reset the Z8530s and setup special hardware
  1147. */
  1148. static void z8530_init(void)
  1149. {
  1150. struct scc_channel *scc;
  1151. int chip, k;
  1152. unsigned long flags;
  1153. char *flag;
  1154. printk(KERN_INFO "Init Z8530 driver: %u channels, IRQ", Nchips*2);
  1155. flag=" ";
  1156. for (k = 0; k < nr_irqs; k++)
  1157. if (Ivec[k].used)
  1158. {
  1159. printk("%s%d", flag, k);
  1160. flag=",";
  1161. }
  1162. printk("\n");
  1163. /* reset and pre-init all chips in the system */
  1164. for (chip = 0; chip < Nchips; chip++)
  1165. {
  1166. scc=&SCC_Info[2*chip];
  1167. if (!scc->ctrl) continue;
  1168. /* Special SCC cards */
  1169. if(scc->brand & EAGLE) /* this is an EAGLE card */
  1170. Outb(scc->special,0x08); /* enable interrupt on the board */
  1171. if(scc->brand & (PC100 | PRIMUS)) /* this is a PC100/PRIMUS card */
  1172. Outb(scc->special,scc->option); /* set the MODEM mode (0x22) */
  1173. /* Reset and pre-init Z8530 */
  1174. spin_lock_irqsave(&scc->lock, flags);
  1175. Outb(scc->ctrl, 0);
  1176. OutReg(scc->ctrl,R9,FHWRES); /* force hardware reset */
  1177. udelay(100); /* give it 'a bit' more time than required */
  1178. wr(scc, R2, chip*16); /* interrupt vector */
  1179. wr(scc, R9, VIS); /* vector includes status */
  1180. spin_unlock_irqrestore(&scc->lock, flags);
  1181. }
  1182. Driver_Initialized = 1;
  1183. }
  1184. /*
  1185. * Allocate device structure, err, instance, and register driver
  1186. */
  1187. static int scc_net_alloc(const char *name, struct scc_channel *scc)
  1188. {
  1189. int err;
  1190. struct net_device *dev;
  1191. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, scc_net_setup);
  1192. if (!dev)
  1193. return -ENOMEM;
  1194. dev->ml_priv = scc;
  1195. scc->dev = dev;
  1196. spin_lock_init(&scc->lock);
  1197. timer_setup(&scc->tx_t, NULL, 0);
  1198. timer_setup(&scc->tx_wdog, NULL, 0);
  1199. err = register_netdevice(dev);
  1200. if (err) {
  1201. printk(KERN_ERR "%s: can't register network device (%d)\n",
  1202. name, err);
  1203. free_netdev(dev);
  1204. scc->dev = NULL;
  1205. return err;
  1206. }
  1207. return 0;
  1208. }
  1209. /* ******************************************************************** */
  1210. /* * Network driver methods * */
  1211. /* ******************************************************************** */
  1212. static const struct net_device_ops scc_netdev_ops = {
  1213. .ndo_open = scc_net_open,
  1214. .ndo_stop = scc_net_close,
  1215. .ndo_start_xmit = scc_net_tx,
  1216. .ndo_set_mac_address = scc_net_set_mac_address,
  1217. .ndo_get_stats = scc_net_get_stats,
  1218. .ndo_do_ioctl = scc_net_ioctl,
  1219. };
  1220. /* ----> Initialize device <----- */
  1221. static void scc_net_setup(struct net_device *dev)
  1222. {
  1223. dev->tx_queue_len = 16; /* should be enough... */
  1224. dev->netdev_ops = &scc_netdev_ops;
  1225. dev->header_ops = &ax25_header_ops;
  1226. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  1227. memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
  1228. dev->flags = 0;
  1229. dev->type = ARPHRD_AX25;
  1230. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  1231. dev->mtu = AX25_DEF_PACLEN;
  1232. dev->addr_len = AX25_ADDR_LEN;
  1233. }
  1234. /* ----> open network device <---- */
  1235. static int scc_net_open(struct net_device *dev)
  1236. {
  1237. struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
  1238. if (!scc->init)
  1239. return -EINVAL;
  1240. scc->tx_buff = NULL;
  1241. skb_queue_head_init(&scc->tx_queue);
  1242. init_channel(scc);
  1243. netif_start_queue(dev);
  1244. return 0;
  1245. }
  1246. /* ----> close network device <---- */
  1247. static int scc_net_close(struct net_device *dev)
  1248. {
  1249. struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
  1250. unsigned long flags;
  1251. netif_stop_queue(dev);
  1252. spin_lock_irqsave(&scc->lock, flags);
  1253. Outb(scc->ctrl,0); /* Make sure pointer is written */
  1254. wr(scc,R1,0); /* disable interrupts */
  1255. wr(scc,R3,0);
  1256. spin_unlock_irqrestore(&scc->lock, flags);
  1257. del_timer_sync(&scc->tx_t);
  1258. del_timer_sync(&scc->tx_wdog);
  1259. scc_discard_buffers(scc);
  1260. return 0;
  1261. }
  1262. /* ----> receive frame, called from scc_rxint() <---- */
  1263. static void scc_net_rx(struct scc_channel *scc, struct sk_buff *skb)
  1264. {
  1265. if (skb->len == 0) {
  1266. dev_kfree_skb_irq(skb);
  1267. return;
  1268. }
  1269. scc->dev_stat.rx_packets++;
  1270. scc->dev_stat.rx_bytes += skb->len;
  1271. skb->protocol = ax25_type_trans(skb, scc->dev);
  1272. netif_rx(skb);
  1273. }
  1274. /* ----> transmit frame <---- */
  1275. static netdev_tx_t scc_net_tx(struct sk_buff *skb, struct net_device *dev)
  1276. {
  1277. struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
  1278. unsigned long flags;
  1279. char kisscmd;
  1280. if (skb->protocol == htons(ETH_P_IP))
  1281. return ax25_ip_xmit(skb);
  1282. if (skb->len > scc->stat.bufsize || skb->len < 2) {
  1283. scc->dev_stat.tx_dropped++; /* bogus frame */
  1284. dev_kfree_skb(skb);
  1285. return NETDEV_TX_OK;
  1286. }
  1287. scc->dev_stat.tx_packets++;
  1288. scc->dev_stat.tx_bytes += skb->len;
  1289. scc->stat.txframes++;
  1290. kisscmd = *skb->data & 0x1f;
  1291. skb_pull(skb, 1);
  1292. if (kisscmd) {
  1293. scc_set_param(scc, kisscmd, *skb->data);
  1294. dev_kfree_skb(skb);
  1295. return NETDEV_TX_OK;
  1296. }
  1297. spin_lock_irqsave(&scc->lock, flags);
  1298. if (skb_queue_len(&scc->tx_queue) > scc->dev->tx_queue_len) {
  1299. struct sk_buff *skb_del;
  1300. skb_del = skb_dequeue(&scc->tx_queue);
  1301. dev_kfree_skb(skb_del);
  1302. }
  1303. skb_queue_tail(&scc->tx_queue, skb);
  1304. netif_trans_update(dev);
  1305. /*
  1306. * Start transmission if the trx state is idle or
  1307. * t_idle hasn't expired yet. Use dwait/persistence/slottime
  1308. * algorithm for normal halfduplex operation.
  1309. */
  1310. if(scc->stat.tx_state == TXS_IDLE || scc->stat.tx_state == TXS_IDLE2) {
  1311. scc->stat.tx_state = TXS_BUSY;
  1312. if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
  1313. __scc_start_tx_timer(scc, t_dwait, scc->kiss.waittime);
  1314. else
  1315. __scc_start_tx_timer(scc, t_dwait, 0);
  1316. }
  1317. spin_unlock_irqrestore(&scc->lock, flags);
  1318. return NETDEV_TX_OK;
  1319. }
  1320. /* ----> ioctl functions <---- */
  1321. /*
  1322. * SIOCSCCCFG - configure driver arg: (struct scc_hw_config *) arg
  1323. * SIOCSCCINI - initialize driver arg: ---
  1324. * SIOCSCCCHANINI - initialize channel arg: (struct scc_modem *) arg
  1325. * SIOCSCCSMEM - set memory arg: (struct scc_mem_config *) arg
  1326. * SIOCSCCGKISS - get level 1 parameter arg: (struct scc_kiss_cmd *) arg
  1327. * SIOCSCCSKISS - set level 1 parameter arg: (struct scc_kiss_cmd *) arg
  1328. * SIOCSCCGSTAT - get driver status arg: (struct scc_stat *) arg
  1329. * SIOCSCCCAL - send calib. pattern arg: (struct scc_calibrate *) arg
  1330. */
  1331. static int scc_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1332. {
  1333. struct scc_kiss_cmd kiss_cmd;
  1334. struct scc_mem_config memcfg;
  1335. struct scc_hw_config hwcfg;
  1336. struct scc_calibrate cal;
  1337. struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
  1338. int chan;
  1339. unsigned char device_name[IFNAMSIZ];
  1340. void __user *arg = ifr->ifr_data;
  1341. if (!Driver_Initialized)
  1342. {
  1343. if (cmd == SIOCSCCCFG)
  1344. {
  1345. int found = 1;
  1346. if (!capable(CAP_SYS_RAWIO)) return -EPERM;
  1347. if (!arg) return -EFAULT;
  1348. if (Nchips >= SCC_MAXCHIPS)
  1349. return -EINVAL;
  1350. if (copy_from_user(&hwcfg, arg, sizeof(hwcfg)))
  1351. return -EFAULT;
  1352. if (hwcfg.irq == 2) hwcfg.irq = 9;
  1353. if (hwcfg.irq < 0 || hwcfg.irq >= nr_irqs)
  1354. return -EINVAL;
  1355. if (!Ivec[hwcfg.irq].used && hwcfg.irq)
  1356. {
  1357. if (request_irq(hwcfg.irq, scc_isr,
  1358. 0, "AX.25 SCC",
  1359. (void *)(long) hwcfg.irq))
  1360. printk(KERN_WARNING "z8530drv: warning, cannot get IRQ %d\n", hwcfg.irq);
  1361. else
  1362. Ivec[hwcfg.irq].used = 1;
  1363. }
  1364. if (hwcfg.vector_latch && !Vector_Latch) {
  1365. if (!request_region(hwcfg.vector_latch, 1, "scc vector latch"))
  1366. printk(KERN_WARNING "z8530drv: warning, cannot reserve vector latch port 0x%lx\n, disabled.", hwcfg.vector_latch);
  1367. else
  1368. Vector_Latch = hwcfg.vector_latch;
  1369. }
  1370. if (hwcfg.clock == 0)
  1371. hwcfg.clock = SCC_DEFAULT_CLOCK;
  1372. #ifndef SCC_DONT_CHECK
  1373. if(request_region(hwcfg.ctrl_a, 1, "scc-probe"))
  1374. {
  1375. disable_irq(hwcfg.irq);
  1376. Outb(hwcfg.ctrl_a, 0);
  1377. OutReg(hwcfg.ctrl_a, R9, FHWRES);
  1378. udelay(100);
  1379. OutReg(hwcfg.ctrl_a,R13,0x55); /* is this chip really there? */
  1380. udelay(5);
  1381. if (InReg(hwcfg.ctrl_a,R13) != 0x55)
  1382. found = 0;
  1383. enable_irq(hwcfg.irq);
  1384. release_region(hwcfg.ctrl_a, 1);
  1385. }
  1386. else
  1387. found = 0;
  1388. #endif
  1389. if (found)
  1390. {
  1391. SCC_Info[2*Nchips ].ctrl = hwcfg.ctrl_a;
  1392. SCC_Info[2*Nchips ].data = hwcfg.data_a;
  1393. SCC_Info[2*Nchips ].irq = hwcfg.irq;
  1394. SCC_Info[2*Nchips+1].ctrl = hwcfg.ctrl_b;
  1395. SCC_Info[2*Nchips+1].data = hwcfg.data_b;
  1396. SCC_Info[2*Nchips+1].irq = hwcfg.irq;
  1397. SCC_ctrl[Nchips].chan_A = hwcfg.ctrl_a;
  1398. SCC_ctrl[Nchips].chan_B = hwcfg.ctrl_b;
  1399. SCC_ctrl[Nchips].irq = hwcfg.irq;
  1400. }
  1401. for (chan = 0; chan < 2; chan++)
  1402. {
  1403. sprintf(device_name, "%s%i", SCC_DriverName, 2*Nchips+chan);
  1404. SCC_Info[2*Nchips+chan].special = hwcfg.special;
  1405. SCC_Info[2*Nchips+chan].clock = hwcfg.clock;
  1406. SCC_Info[2*Nchips+chan].brand = hwcfg.brand;
  1407. SCC_Info[2*Nchips+chan].option = hwcfg.option;
  1408. SCC_Info[2*Nchips+chan].enhanced = hwcfg.escc;
  1409. #ifdef SCC_DONT_CHECK
  1410. printk(KERN_INFO "%s: data port = 0x%3.3x control port = 0x%3.3x\n",
  1411. device_name,
  1412. SCC_Info[2*Nchips+chan].data,
  1413. SCC_Info[2*Nchips+chan].ctrl);
  1414. #else
  1415. printk(KERN_INFO "%s: data port = 0x%3.3lx control port = 0x%3.3lx -- %s\n",
  1416. device_name,
  1417. chan? hwcfg.data_b : hwcfg.data_a,
  1418. chan? hwcfg.ctrl_b : hwcfg.ctrl_a,
  1419. found? "found" : "missing");
  1420. #endif
  1421. if (found)
  1422. {
  1423. request_region(SCC_Info[2*Nchips+chan].ctrl, 1, "scc ctrl");
  1424. request_region(SCC_Info[2*Nchips+chan].data, 1, "scc data");
  1425. if (Nchips+chan != 0 &&
  1426. scc_net_alloc(device_name,
  1427. &SCC_Info[2*Nchips+chan]))
  1428. return -EINVAL;
  1429. }
  1430. }
  1431. if (found) Nchips++;
  1432. return 0;
  1433. }
  1434. if (cmd == SIOCSCCINI)
  1435. {
  1436. if (!capable(CAP_SYS_RAWIO))
  1437. return -EPERM;
  1438. if (Nchips == 0)
  1439. return -EINVAL;
  1440. z8530_init();
  1441. return 0;
  1442. }
  1443. return -EINVAL; /* confuse the user */
  1444. }
  1445. if (!scc->init)
  1446. {
  1447. if (cmd == SIOCSCCCHANINI)
  1448. {
  1449. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  1450. if (!arg) return -EINVAL;
  1451. scc->stat.bufsize = SCC_BUFSIZE;
  1452. if (copy_from_user(&scc->modem, arg, sizeof(struct scc_modem)))
  1453. return -EINVAL;
  1454. /* default KISS Params */
  1455. if (scc->modem.speed < 4800)
  1456. {
  1457. scc->kiss.txdelay = 36; /* 360 ms */
  1458. scc->kiss.persist = 42; /* 25% persistence */ /* was 25 */
  1459. scc->kiss.slottime = 16; /* 160 ms */
  1460. scc->kiss.tailtime = 4; /* minimal reasonable value */
  1461. scc->kiss.fulldup = 0; /* CSMA */
  1462. scc->kiss.waittime = 50; /* 500 ms */
  1463. scc->kiss.maxkeyup = 10; /* 10 s */
  1464. scc->kiss.mintime = 3; /* 3 s */
  1465. scc->kiss.idletime = 30; /* 30 s */
  1466. scc->kiss.maxdefer = 120; /* 2 min */
  1467. scc->kiss.softdcd = 0; /* hardware dcd */
  1468. } else {
  1469. scc->kiss.txdelay = 10; /* 100 ms */
  1470. scc->kiss.persist = 64; /* 25% persistence */ /* was 25 */
  1471. scc->kiss.slottime = 8; /* 160 ms */
  1472. scc->kiss.tailtime = 1; /* minimal reasonable value */
  1473. scc->kiss.fulldup = 0; /* CSMA */
  1474. scc->kiss.waittime = 50; /* 500 ms */
  1475. scc->kiss.maxkeyup = 7; /* 7 s */
  1476. scc->kiss.mintime = 3; /* 3 s */
  1477. scc->kiss.idletime = 30; /* 30 s */
  1478. scc->kiss.maxdefer = 120; /* 2 min */
  1479. scc->kiss.softdcd = 0; /* hardware dcd */
  1480. }
  1481. scc->tx_buff = NULL;
  1482. skb_queue_head_init(&scc->tx_queue);
  1483. scc->init = 1;
  1484. return 0;
  1485. }
  1486. return -EINVAL;
  1487. }
  1488. switch(cmd)
  1489. {
  1490. case SIOCSCCRESERVED:
  1491. return -ENOIOCTLCMD;
  1492. case SIOCSCCSMEM:
  1493. if (!capable(CAP_SYS_RAWIO)) return -EPERM;
  1494. if (!arg || copy_from_user(&memcfg, arg, sizeof(memcfg)))
  1495. return -EINVAL;
  1496. scc->stat.bufsize = memcfg.bufsize;
  1497. return 0;
  1498. case SIOCSCCGSTAT:
  1499. if (!arg || copy_to_user(arg, &scc->stat, sizeof(scc->stat)))
  1500. return -EINVAL;
  1501. return 0;
  1502. case SIOCSCCGKISS:
  1503. if (!arg || copy_from_user(&kiss_cmd, arg, sizeof(kiss_cmd)))
  1504. return -EINVAL;
  1505. kiss_cmd.param = scc_get_param(scc, kiss_cmd.command);
  1506. if (copy_to_user(arg, &kiss_cmd, sizeof(kiss_cmd)))
  1507. return -EINVAL;
  1508. return 0;
  1509. case SIOCSCCSKISS:
  1510. if (!capable(CAP_NET_ADMIN)) return -EPERM;
  1511. if (!arg || copy_from_user(&kiss_cmd, arg, sizeof(kiss_cmd)))
  1512. return -EINVAL;
  1513. return scc_set_param(scc, kiss_cmd.command, kiss_cmd.param);
  1514. case SIOCSCCCAL:
  1515. if (!capable(CAP_SYS_RAWIO)) return -EPERM;
  1516. if (!arg || copy_from_user(&cal, arg, sizeof(cal)) || cal.time == 0)
  1517. return -EINVAL;
  1518. scc_start_calibrate(scc, cal.time, cal.pattern);
  1519. return 0;
  1520. default:
  1521. return -ENOIOCTLCMD;
  1522. }
  1523. return -EINVAL;
  1524. }
  1525. /* ----> set interface callsign <---- */
  1526. static int scc_net_set_mac_address(struct net_device *dev, void *addr)
  1527. {
  1528. struct sockaddr *sa = (struct sockaddr *) addr;
  1529. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  1530. return 0;
  1531. }
  1532. /* ----> get statistics <---- */
  1533. static struct net_device_stats *scc_net_get_stats(struct net_device *dev)
  1534. {
  1535. struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
  1536. scc->dev_stat.rx_errors = scc->stat.rxerrs + scc->stat.rx_over;
  1537. scc->dev_stat.tx_errors = scc->stat.txerrs + scc->stat.tx_under;
  1538. scc->dev_stat.rx_fifo_errors = scc->stat.rx_over;
  1539. scc->dev_stat.tx_fifo_errors = scc->stat.tx_under;
  1540. return &scc->dev_stat;
  1541. }
  1542. /* ******************************************************************** */
  1543. /* * dump statistics to /proc/net/z8530drv * */
  1544. /* ******************************************************************** */
  1545. #ifdef CONFIG_PROC_FS
  1546. static inline struct scc_channel *scc_net_seq_idx(loff_t pos)
  1547. {
  1548. int k;
  1549. for (k = 0; k < Nchips*2; ++k) {
  1550. if (!SCC_Info[k].init)
  1551. continue;
  1552. if (pos-- == 0)
  1553. return &SCC_Info[k];
  1554. }
  1555. return NULL;
  1556. }
  1557. static void *scc_net_seq_start(struct seq_file *seq, loff_t *pos)
  1558. {
  1559. return *pos ? scc_net_seq_idx(*pos - 1) : SEQ_START_TOKEN;
  1560. }
  1561. static void *scc_net_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1562. {
  1563. unsigned k;
  1564. struct scc_channel *scc = v;
  1565. ++*pos;
  1566. for (k = (v == SEQ_START_TOKEN) ? 0 : (scc - SCC_Info)+1;
  1567. k < Nchips*2; ++k) {
  1568. if (SCC_Info[k].init)
  1569. return &SCC_Info[k];
  1570. }
  1571. return NULL;
  1572. }
  1573. static void scc_net_seq_stop(struct seq_file *seq, void *v)
  1574. {
  1575. }
  1576. static int scc_net_seq_show(struct seq_file *seq, void *v)
  1577. {
  1578. if (v == SEQ_START_TOKEN) {
  1579. seq_puts(seq, "z8530drv-"VERSION"\n");
  1580. } else if (!Driver_Initialized) {
  1581. seq_puts(seq, "not initialized\n");
  1582. } else if (!Nchips) {
  1583. seq_puts(seq, "chips missing\n");
  1584. } else {
  1585. const struct scc_channel *scc = v;
  1586. const struct scc_stat *stat = &scc->stat;
  1587. const struct scc_kiss *kiss = &scc->kiss;
  1588. /* dev data ctrl irq clock brand enh vector special option
  1589. * baud nrz clocksrc softdcd bufsize
  1590. * rxints txints exints spints
  1591. * rcvd rxerrs over / xmit txerrs under / nospace bufsize
  1592. * txd pers slot tail ful wait min maxk idl defr txof grp
  1593. * W ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  1594. * R ## ## XX ## ## ## ## ## XX ## ## ## ## ## ## ##
  1595. */
  1596. seq_printf(seq, "%s\t%3.3lx %3.3lx %d %lu %2.2x %d %3.3lx %3.3lx %d\n",
  1597. scc->dev->name,
  1598. scc->data, scc->ctrl, scc->irq, scc->clock, scc->brand,
  1599. scc->enhanced, Vector_Latch, scc->special,
  1600. scc->option);
  1601. seq_printf(seq, "\t%lu %d %d %d %d\n",
  1602. scc->modem.speed, scc->modem.nrz,
  1603. scc->modem.clocksrc, kiss->softdcd,
  1604. stat->bufsize);
  1605. seq_printf(seq, "\t%lu %lu %lu %lu\n",
  1606. stat->rxints, stat->txints, stat->exints, stat->spints);
  1607. seq_printf(seq, "\t%lu %lu %d / %lu %lu %d / %d %d\n",
  1608. stat->rxframes, stat->rxerrs, stat->rx_over,
  1609. stat->txframes, stat->txerrs, stat->tx_under,
  1610. stat->nospace, stat->tx_state);
  1611. #define K(x) kiss->x
  1612. seq_printf(seq, "\t%d %d %d %d %d %d %d %d %d %d %d %d\n",
  1613. K(txdelay), K(persist), K(slottime), K(tailtime),
  1614. K(fulldup), K(waittime), K(mintime), K(maxkeyup),
  1615. K(idletime), K(maxdefer), K(tx_inhibit), K(group));
  1616. #undef K
  1617. #ifdef SCC_DEBUG
  1618. {
  1619. int reg;
  1620. seq_printf(seq, "\tW ");
  1621. for (reg = 0; reg < 16; reg++)
  1622. seq_printf(seq, "%2.2x ", scc->wreg[reg]);
  1623. seq_printf(seq, "\n");
  1624. seq_printf(seq, "\tR %2.2x %2.2x XX ", InReg(scc->ctrl,R0), InReg(scc->ctrl,R1));
  1625. for (reg = 3; reg < 8; reg++)
  1626. seq_printf(seq, "%2.2x ", InReg(scc->ctrl, reg));
  1627. seq_printf(seq, "XX ");
  1628. for (reg = 9; reg < 16; reg++)
  1629. seq_printf(seq, "%2.2x ", InReg(scc->ctrl, reg));
  1630. seq_printf(seq, "\n");
  1631. }
  1632. #endif
  1633. seq_putc(seq, '\n');
  1634. }
  1635. return 0;
  1636. }
  1637. static const struct seq_operations scc_net_seq_ops = {
  1638. .start = scc_net_seq_start,
  1639. .next = scc_net_seq_next,
  1640. .stop = scc_net_seq_stop,
  1641. .show = scc_net_seq_show,
  1642. };
  1643. #endif /* CONFIG_PROC_FS */
  1644. /* ******************************************************************** */
  1645. /* * Init SCC driver * */
  1646. /* ******************************************************************** */
  1647. static int __init scc_init_driver (void)
  1648. {
  1649. char devname[IFNAMSIZ];
  1650. printk(banner);
  1651. sprintf(devname,"%s0", SCC_DriverName);
  1652. rtnl_lock();
  1653. if (scc_net_alloc(devname, SCC_Info)) {
  1654. rtnl_unlock();
  1655. printk(KERN_ERR "z8530drv: cannot initialize module\n");
  1656. return -EIO;
  1657. }
  1658. rtnl_unlock();
  1659. proc_create_seq("z8530drv", 0, init_net.proc_net, &scc_net_seq_ops);
  1660. return 0;
  1661. }
  1662. static void __exit scc_cleanup_driver(void)
  1663. {
  1664. io_port ctrl;
  1665. int k;
  1666. struct scc_channel *scc;
  1667. struct net_device *dev;
  1668. if (Nchips == 0 && (dev = SCC_Info[0].dev))
  1669. {
  1670. unregister_netdev(dev);
  1671. free_netdev(dev);
  1672. }
  1673. /* Guard against chip prattle */
  1674. local_irq_disable();
  1675. for (k = 0; k < Nchips; k++)
  1676. if ( (ctrl = SCC_ctrl[k].chan_A) )
  1677. {
  1678. Outb(ctrl, 0);
  1679. OutReg(ctrl,R9,FHWRES); /* force hardware reset */
  1680. udelay(50);
  1681. }
  1682. /* To unload the port must be closed so no real IRQ pending */
  1683. for (k = 0; k < nr_irqs ; k++)
  1684. if (Ivec[k].used) free_irq(k, NULL);
  1685. local_irq_enable();
  1686. /* Now clean up */
  1687. for (k = 0; k < Nchips*2; k++)
  1688. {
  1689. scc = &SCC_Info[k];
  1690. if (scc->ctrl)
  1691. {
  1692. release_region(scc->ctrl, 1);
  1693. release_region(scc->data, 1);
  1694. }
  1695. if (scc->dev)
  1696. {
  1697. unregister_netdev(scc->dev);
  1698. free_netdev(scc->dev);
  1699. }
  1700. }
  1701. if (Vector_Latch)
  1702. release_region(Vector_Latch, 1);
  1703. remove_proc_entry("z8530drv", init_net.proc_net);
  1704. }
  1705. MODULE_AUTHOR("Joerg Reuter <jreuter@yaina.de>");
  1706. MODULE_DESCRIPTION("AX.25 Device Driver for Z8530 based HDLC cards");
  1707. MODULE_SUPPORTED_DEVICE("Z8530 based SCC cards for Amateur Radio");
  1708. MODULE_LICENSE("GPL");
  1709. module_init(scc_init_driver);
  1710. module_exit(scc_cleanup_driver);