specialix.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. /*
  2. * specialix.c -- specialix IO8+ multiport serial driver.
  3. *
  4. * Copyright (C) 1997 Roger Wolff (R.E.Wolff@BitWizard.nl)
  5. * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
  6. *
  7. * Specialix pays for the development and support of this driver.
  8. * Please DO contact io8-linux@specialix.co.uk if you require
  9. * support. But please read the documentation (specialix.txt)
  10. * first.
  11. *
  12. * This driver was developed in the BitWizard linux device
  13. * driver service. If you require a linux device driver for your
  14. * product, please contact devices@BitWizard.nl for a quote.
  15. *
  16. * This code is firmly based on the riscom/8 serial driver,
  17. * written by Dmitry Gorodchanin. The specialix IO8+ card
  18. * programming information was obtained from the CL-CD1865 Data
  19. * Book, and Specialix document number 6200059: IO8+ Hardware
  20. * Functional Specification.
  21. *
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License as
  24. * published by the Free Software Foundation; either version 2 of
  25. * the License, or (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be
  28. * useful, but WITHOUT ANY WARRANTY; without even the implied
  29. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  30. * PURPOSE. See the GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public
  33. * License along with this program; if not, write to the Free
  34. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  35. * USA.
  36. *
  37. * Revision history:
  38. *
  39. * Revision 1.0: April 1st 1997.
  40. * Initial release for alpha testing.
  41. * Revision 1.1: April 14th 1997.
  42. * Incorporated Richard Hudsons suggestions,
  43. * removed some debugging printk's.
  44. * Revision 1.2: April 15th 1997.
  45. * Ported to 2.1.x kernels.
  46. * Revision 1.3: April 17th 1997
  47. * Backported to 2.0. (Compatibility macros).
  48. * Revision 1.4: April 18th 1997
  49. * Fixed DTR/RTS bug that caused the card to indicate
  50. * "don't send data" to a modem after the password prompt.
  51. * Fixed bug for premature (fake) interrupts.
  52. * Revision 1.5: April 19th 1997
  53. * fixed a minor typo in the header file, cleanup a little.
  54. * performance warnings are now MAXed at once per minute.
  55. * Revision 1.6: May 23 1997
  56. * Changed the specialix=... format to include interrupt.
  57. * Revision 1.7: May 27 1997
  58. * Made many more debug printk's a compile time option.
  59. * Revision 1.8: Jul 1 1997
  60. * port to linux-2.1.43 kernel.
  61. * Revision 1.9: Oct 9 1998
  62. * Added stuff for the IO8+/PCI version.
  63. * Revision 1.10: Oct 22 1999 / Jan 21 2000.
  64. * Added stuff for setserial.
  65. * Nicolas Mailhot (Nicolas.Mailhot@email.enst.fr)
  66. *
  67. */
  68. #define VERSION "1.11"
  69. /*
  70. * There is a bunch of documentation about the card, jumpers, config
  71. * settings, restrictions, cables, device names and numbers in
  72. * Documentation/serial/specialix.txt
  73. */
  74. #include <linux/module.h>
  75. #include <linux/io.h>
  76. #include <linux/kernel.h>
  77. #include <linux/sched.h>
  78. #include <linux/ioport.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/errno.h>
  81. #include <linux/tty.h>
  82. #include <linux/tty_flip.h>
  83. #include <linux/mm.h>
  84. #include <linux/serial.h>
  85. #include <linux/fcntl.h>
  86. #include <linux/major.h>
  87. #include <linux/delay.h>
  88. #include <linux/pci.h>
  89. #include <linux/init.h>
  90. #include <linux/uaccess.h>
  91. #include <linux/gfp.h>
  92. #include "specialix_io8.h"
  93. #include "cd1865.h"
  94. /*
  95. This driver can spew a whole lot of debugging output at you. If you
  96. need maximum performance, you should disable the DEBUG define. To
  97. aid in debugging in the field, I'm leaving the compile-time debug
  98. features enabled, and disable them "runtime". That allows me to
  99. instruct people with problems to enable debugging without requiring
  100. them to recompile...
  101. */
  102. #define DEBUG
  103. static int sx_debug;
  104. static int sx_rxfifo = SPECIALIX_RXFIFO;
  105. static int sx_rtscts;
  106. #ifdef DEBUG
  107. #define dprintk(f, str...) if (sx_debug & f) printk(str)
  108. #else
  109. #define dprintk(f, str...) /* nothing */
  110. #endif
  111. #define SX_DEBUG_FLOW 0x0001
  112. #define SX_DEBUG_DATA 0x0002
  113. #define SX_DEBUG_PROBE 0x0004
  114. #define SX_DEBUG_CHAN 0x0008
  115. #define SX_DEBUG_INIT 0x0010
  116. #define SX_DEBUG_RX 0x0020
  117. #define SX_DEBUG_TX 0x0040
  118. #define SX_DEBUG_IRQ 0x0080
  119. #define SX_DEBUG_OPEN 0x0100
  120. #define SX_DEBUG_TERMIOS 0x0200
  121. #define SX_DEBUG_SIGNALS 0x0400
  122. #define SX_DEBUG_FIFO 0x0800
  123. #define func_enter() dprintk(SX_DEBUG_FLOW, "io8: enter %s\n", __func__)
  124. #define func_exit() dprintk(SX_DEBUG_FLOW, "io8: exit %s\n", __func__)
  125. /* Configurable options: */
  126. /* Am I paranoid or not ? ;-) */
  127. #define SPECIALIX_PARANOIA_CHECK
  128. /*
  129. * The following defines are mostly for testing purposes. But if you need
  130. * some nice reporting in your syslog, you can define them also.
  131. */
  132. #undef SX_REPORT_FIFO
  133. #undef SX_REPORT_OVERRUN
  134. #define SPECIALIX_LEGAL_FLAGS \
  135. (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
  136. ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
  137. ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
  138. static struct tty_driver *specialix_driver;
  139. static struct specialix_board sx_board[SX_NBOARD] = {
  140. { 0, SX_IOBASE1, 9, },
  141. { 0, SX_IOBASE2, 11, },
  142. { 0, SX_IOBASE3, 12, },
  143. { 0, SX_IOBASE4, 15, },
  144. };
  145. static struct specialix_port sx_port[SX_NBOARD * SX_NPORT];
  146. static int sx_paranoia_check(struct specialix_port const *port,
  147. char *name, const char *routine)
  148. {
  149. #ifdef SPECIALIX_PARANOIA_CHECK
  150. static const char *badmagic = KERN_ERR
  151. "sx: Warning: bad specialix port magic number for device %s in %s\n";
  152. static const char *badinfo = KERN_ERR
  153. "sx: Warning: null specialix port for device %s in %s\n";
  154. if (!port) {
  155. printk(badinfo, name, routine);
  156. return 1;
  157. }
  158. if (port->magic != SPECIALIX_MAGIC) {
  159. printk(badmagic, name, routine);
  160. return 1;
  161. }
  162. #endif
  163. return 0;
  164. }
  165. /*
  166. *
  167. * Service functions for specialix IO8+ driver.
  168. *
  169. */
  170. /* Get board number from pointer */
  171. static inline int board_No(struct specialix_board *bp)
  172. {
  173. return bp - sx_board;
  174. }
  175. /* Get port number from pointer */
  176. static inline int port_No(struct specialix_port const *port)
  177. {
  178. return SX_PORT(port - sx_port);
  179. }
  180. /* Get pointer to board from pointer to port */
  181. static inline struct specialix_board *port_Board(
  182. struct specialix_port const *port)
  183. {
  184. return &sx_board[SX_BOARD(port - sx_port)];
  185. }
  186. /* Input Byte from CL CD186x register */
  187. static inline unsigned char sx_in(struct specialix_board *bp,
  188. unsigned short reg)
  189. {
  190. bp->reg = reg | 0x80;
  191. outb(reg | 0x80, bp->base + SX_ADDR_REG);
  192. return inb(bp->base + SX_DATA_REG);
  193. }
  194. /* Output Byte to CL CD186x register */
  195. static inline void sx_out(struct specialix_board *bp, unsigned short reg,
  196. unsigned char val)
  197. {
  198. bp->reg = reg | 0x80;
  199. outb(reg | 0x80, bp->base + SX_ADDR_REG);
  200. outb(val, bp->base + SX_DATA_REG);
  201. }
  202. /* Input Byte from CL CD186x register */
  203. static inline unsigned char sx_in_off(struct specialix_board *bp,
  204. unsigned short reg)
  205. {
  206. bp->reg = reg;
  207. outb(reg, bp->base + SX_ADDR_REG);
  208. return inb(bp->base + SX_DATA_REG);
  209. }
  210. /* Output Byte to CL CD186x register */
  211. static inline void sx_out_off(struct specialix_board *bp,
  212. unsigned short reg, unsigned char val)
  213. {
  214. bp->reg = reg;
  215. outb(reg, bp->base + SX_ADDR_REG);
  216. outb(val, bp->base + SX_DATA_REG);
  217. }
  218. /* Wait for Channel Command Register ready */
  219. static void sx_wait_CCR(struct specialix_board *bp)
  220. {
  221. unsigned long delay, flags;
  222. unsigned char ccr;
  223. for (delay = SX_CCR_TIMEOUT; delay; delay--) {
  224. spin_lock_irqsave(&bp->lock, flags);
  225. ccr = sx_in(bp, CD186x_CCR);
  226. spin_unlock_irqrestore(&bp->lock, flags);
  227. if (!ccr)
  228. return;
  229. udelay(1);
  230. }
  231. printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
  232. }
  233. /* Wait for Channel Command Register ready */
  234. static void sx_wait_CCR_off(struct specialix_board *bp)
  235. {
  236. unsigned long delay;
  237. unsigned char crr;
  238. unsigned long flags;
  239. for (delay = SX_CCR_TIMEOUT; delay; delay--) {
  240. spin_lock_irqsave(&bp->lock, flags);
  241. crr = sx_in_off(bp, CD186x_CCR);
  242. spin_unlock_irqrestore(&bp->lock, flags);
  243. if (!crr)
  244. return;
  245. udelay(1);
  246. }
  247. printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
  248. }
  249. /*
  250. * specialix IO8+ IO range functions.
  251. */
  252. static int sx_request_io_range(struct specialix_board *bp)
  253. {
  254. return request_region(bp->base,
  255. bp->flags & SX_BOARD_IS_PCI ? SX_PCI_IO_SPACE : SX_IO_SPACE,
  256. "specialix IO8+") == NULL;
  257. }
  258. static void sx_release_io_range(struct specialix_board *bp)
  259. {
  260. release_region(bp->base, bp->flags & SX_BOARD_IS_PCI ?
  261. SX_PCI_IO_SPACE : SX_IO_SPACE);
  262. }
  263. /* Set the IRQ using the RTS lines that run to the PAL on the board.... */
  264. static int sx_set_irq(struct specialix_board *bp)
  265. {
  266. int virq;
  267. int i;
  268. unsigned long flags;
  269. if (bp->flags & SX_BOARD_IS_PCI)
  270. return 1;
  271. switch (bp->irq) {
  272. /* In the same order as in the docs... */
  273. case 15:
  274. virq = 0;
  275. break;
  276. case 12:
  277. virq = 1;
  278. break;
  279. case 11:
  280. virq = 2;
  281. break;
  282. case 9:
  283. virq = 3;
  284. break;
  285. default:printk(KERN_ERR
  286. "Speclialix: cannot set irq to %d.\n", bp->irq);
  287. return 0;
  288. }
  289. spin_lock_irqsave(&bp->lock, flags);
  290. for (i = 0; i < 2; i++) {
  291. sx_out(bp, CD186x_CAR, i);
  292. sx_out(bp, CD186x_MSVRTS, ((virq >> i) & 0x1)? MSVR_RTS:0);
  293. }
  294. spin_unlock_irqrestore(&bp->lock, flags);
  295. return 1;
  296. }
  297. /* Reset and setup CD186x chip */
  298. static int sx_init_CD186x(struct specialix_board *bp)
  299. {
  300. unsigned long flags;
  301. int scaler;
  302. int rv = 1;
  303. func_enter();
  304. sx_wait_CCR_off(bp); /* Wait for CCR ready */
  305. spin_lock_irqsave(&bp->lock, flags);
  306. sx_out_off(bp, CD186x_CCR, CCR_HARDRESET); /* Reset CD186x chip */
  307. spin_unlock_irqrestore(&bp->lock, flags);
  308. msleep(50); /* Delay 0.05 sec */
  309. spin_lock_irqsave(&bp->lock, flags);
  310. sx_out_off(bp, CD186x_GIVR, SX_ID); /* Set ID for this chip */
  311. sx_out_off(bp, CD186x_GICR, 0); /* Clear all bits */
  312. sx_out_off(bp, CD186x_PILR1, SX_ACK_MINT); /* Prio for modem intr */
  313. sx_out_off(bp, CD186x_PILR2, SX_ACK_TINT); /* Prio for transmitter intr */
  314. sx_out_off(bp, CD186x_PILR3, SX_ACK_RINT); /* Prio for receiver intr */
  315. /* Set RegAckEn */
  316. sx_out_off(bp, CD186x_SRCR, sx_in(bp, CD186x_SRCR) | SRCR_REGACKEN);
  317. /* Setting up prescaler. We need 4 ticks per 1 ms */
  318. scaler = SX_OSCFREQ/SPECIALIX_TPS;
  319. sx_out_off(bp, CD186x_PPRH, scaler >> 8);
  320. sx_out_off(bp, CD186x_PPRL, scaler & 0xff);
  321. spin_unlock_irqrestore(&bp->lock, flags);
  322. if (!sx_set_irq(bp)) {
  323. /* Figure out how to pass this along... */
  324. printk(KERN_ERR "Cannot set irq to %d.\n", bp->irq);
  325. rv = 0;
  326. }
  327. func_exit();
  328. return rv;
  329. }
  330. static int read_cross_byte(struct specialix_board *bp, int reg, int bit)
  331. {
  332. int i;
  333. int t;
  334. unsigned long flags;
  335. spin_lock_irqsave(&bp->lock, flags);
  336. for (i = 0, t = 0; i < 8; i++) {
  337. sx_out_off(bp, CD186x_CAR, i);
  338. if (sx_in_off(bp, reg) & bit)
  339. t |= 1 << i;
  340. }
  341. spin_unlock_irqrestore(&bp->lock, flags);
  342. return t;
  343. }
  344. /* Main probing routine, also sets irq. */
  345. static int sx_probe(struct specialix_board *bp)
  346. {
  347. unsigned char val1, val2;
  348. int rev;
  349. int chip;
  350. func_enter();
  351. if (sx_request_io_range(bp)) {
  352. func_exit();
  353. return 1;
  354. }
  355. /* Are the I/O ports here ? */
  356. sx_out_off(bp, CD186x_PPRL, 0x5a);
  357. udelay(1);
  358. val1 = sx_in_off(bp, CD186x_PPRL);
  359. sx_out_off(bp, CD186x_PPRL, 0xa5);
  360. udelay(1);
  361. val2 = sx_in_off(bp, CD186x_PPRL);
  362. if (val1 != 0x5a || val2 != 0xa5) {
  363. printk(KERN_INFO
  364. "sx%d: specialix IO8+ Board at 0x%03x not found.\n",
  365. board_No(bp), bp->base);
  366. sx_release_io_range(bp);
  367. func_exit();
  368. return 1;
  369. }
  370. /* Check the DSR lines that Specialix uses as board
  371. identification */
  372. val1 = read_cross_byte(bp, CD186x_MSVR, MSVR_DSR);
  373. val2 = read_cross_byte(bp, CD186x_MSVR, MSVR_RTS);
  374. dprintk(SX_DEBUG_INIT,
  375. "sx%d: DSR lines are: %02x, rts lines are: %02x\n",
  376. board_No(bp), val1, val2);
  377. /* They managed to switch the bit order between the docs and
  378. the IO8+ card. The new PCI card now conforms to old docs.
  379. They changed the PCI docs to reflect the situation on the
  380. old card. */
  381. val2 = (bp->flags & SX_BOARD_IS_PCI)?0x4d : 0xb2;
  382. if (val1 != val2) {
  383. printk(KERN_INFO
  384. "sx%d: specialix IO8+ ID %02x at 0x%03x not found (%02x).\n",
  385. board_No(bp), val2, bp->base, val1);
  386. sx_release_io_range(bp);
  387. func_exit();
  388. return 1;
  389. }
  390. /* Reset CD186x again */
  391. if (!sx_init_CD186x(bp)) {
  392. sx_release_io_range(bp);
  393. func_exit();
  394. return 1;
  395. }
  396. sx_request_io_range(bp);
  397. bp->flags |= SX_BOARD_PRESENT;
  398. /* Chip revcode pkgtype
  399. GFRCR SRCR bit 7
  400. CD180 rev B 0x81 0
  401. CD180 rev C 0x82 0
  402. CD1864 rev A 0x82 1
  403. CD1865 rev A 0x83 1 -- Do not use!!! Does not work.
  404. CD1865 rev B 0x84 1
  405. -- Thanks to Gwen Wang, Cirrus Logic.
  406. */
  407. switch (sx_in_off(bp, CD186x_GFRCR)) {
  408. case 0x82:
  409. chip = 1864;
  410. rev = 'A';
  411. break;
  412. case 0x83:
  413. chip = 1865;
  414. rev = 'A';
  415. break;
  416. case 0x84:
  417. chip = 1865;
  418. rev = 'B';
  419. break;
  420. case 0x85:
  421. chip = 1865;
  422. rev = 'C';
  423. break; /* Does not exist at this time */
  424. default:
  425. chip = -1;
  426. rev = 'x';
  427. }
  428. dprintk(SX_DEBUG_INIT, " GFCR = 0x%02x\n", sx_in_off(bp, CD186x_GFRCR));
  429. printk(KERN_INFO
  430. "sx%d: specialix IO8+ board detected at 0x%03x, IRQ %d, CD%d Rev. %c.\n",
  431. board_No(bp), bp->base, bp->irq, chip, rev);
  432. func_exit();
  433. return 0;
  434. }
  435. /*
  436. *
  437. * Interrupt processing routines.
  438. * */
  439. static struct specialix_port *sx_get_port(struct specialix_board *bp,
  440. unsigned char const *what)
  441. {
  442. unsigned char channel;
  443. struct specialix_port *port = NULL;
  444. channel = sx_in(bp, CD186x_GICR) >> GICR_CHAN_OFF;
  445. dprintk(SX_DEBUG_CHAN, "channel: %d\n", channel);
  446. if (channel < CD186x_NCH) {
  447. port = &sx_port[board_No(bp) * SX_NPORT + channel];
  448. dprintk(SX_DEBUG_CHAN, "port: %d %p flags: 0x%lx\n",
  449. board_No(bp) * SX_NPORT + channel, port,
  450. port->port.flags & ASYNC_INITIALIZED);
  451. if (port->port.flags & ASYNC_INITIALIZED) {
  452. dprintk(SX_DEBUG_CHAN, "port: %d %p\n", channel, port);
  453. func_exit();
  454. return port;
  455. }
  456. }
  457. printk(KERN_INFO "sx%d: %s interrupt from invalid port %d\n",
  458. board_No(bp), what, channel);
  459. return NULL;
  460. }
  461. static void sx_receive_exc(struct specialix_board *bp)
  462. {
  463. struct specialix_port *port;
  464. struct tty_struct *tty;
  465. unsigned char status;
  466. unsigned char ch, flag;
  467. func_enter();
  468. port = sx_get_port(bp, "Receive");
  469. if (!port) {
  470. dprintk(SX_DEBUG_RX, "Hmm, couldn't find port.\n");
  471. func_exit();
  472. return;
  473. }
  474. tty = port->port.tty;
  475. status = sx_in(bp, CD186x_RCSR);
  476. dprintk(SX_DEBUG_RX, "status: 0x%x\n", status);
  477. if (status & RCSR_OE) {
  478. port->overrun++;
  479. dprintk(SX_DEBUG_FIFO,
  480. "sx%d: port %d: Overrun. Total %ld overruns.\n",
  481. board_No(bp), port_No(port), port->overrun);
  482. }
  483. status &= port->mark_mask;
  484. /* This flip buffer check needs to be below the reading of the
  485. status register to reset the chip's IRQ.... */
  486. if (tty_buffer_request_room(tty, 1) == 0) {
  487. dprintk(SX_DEBUG_FIFO,
  488. "sx%d: port %d: Working around flip buffer overflow.\n",
  489. board_No(bp), port_No(port));
  490. func_exit();
  491. return;
  492. }
  493. ch = sx_in(bp, CD186x_RDR);
  494. if (!status) {
  495. func_exit();
  496. return;
  497. }
  498. if (status & RCSR_TOUT) {
  499. printk(KERN_INFO
  500. "sx%d: port %d: Receiver timeout. Hardware problems ?\n",
  501. board_No(bp), port_No(port));
  502. func_exit();
  503. return;
  504. } else if (status & RCSR_BREAK) {
  505. dprintk(SX_DEBUG_RX, "sx%d: port %d: Handling break...\n",
  506. board_No(bp), port_No(port));
  507. flag = TTY_BREAK;
  508. if (port->port.flags & ASYNC_SAK)
  509. do_SAK(tty);
  510. } else if (status & RCSR_PE)
  511. flag = TTY_PARITY;
  512. else if (status & RCSR_FE)
  513. flag = TTY_FRAME;
  514. else if (status & RCSR_OE)
  515. flag = TTY_OVERRUN;
  516. else
  517. flag = TTY_NORMAL;
  518. if (tty_insert_flip_char(tty, ch, flag))
  519. tty_flip_buffer_push(tty);
  520. func_exit();
  521. }
  522. static void sx_receive(struct specialix_board *bp)
  523. {
  524. struct specialix_port *port;
  525. struct tty_struct *tty;
  526. unsigned char count;
  527. func_enter();
  528. port = sx_get_port(bp, "Receive");
  529. if (port == NULL) {
  530. dprintk(SX_DEBUG_RX, "Hmm, couldn't find port.\n");
  531. func_exit();
  532. return;
  533. }
  534. tty = port->port.tty;
  535. count = sx_in(bp, CD186x_RDCR);
  536. dprintk(SX_DEBUG_RX, "port: %p: count: %d\n", port, count);
  537. port->hits[count > 8 ? 9 : count]++;
  538. while (count--)
  539. tty_insert_flip_char(tty, sx_in(bp, CD186x_RDR), TTY_NORMAL);
  540. tty_flip_buffer_push(tty);
  541. func_exit();
  542. }
  543. static void sx_transmit(struct specialix_board *bp)
  544. {
  545. struct specialix_port *port;
  546. struct tty_struct *tty;
  547. unsigned char count;
  548. func_enter();
  549. port = sx_get_port(bp, "Transmit");
  550. if (port == NULL) {
  551. func_exit();
  552. return;
  553. }
  554. dprintk(SX_DEBUG_TX, "port: %p\n", port);
  555. tty = port->port.tty;
  556. if (port->IER & IER_TXEMPTY) {
  557. /* FIFO drained */
  558. sx_out(bp, CD186x_CAR, port_No(port));
  559. port->IER &= ~IER_TXEMPTY;
  560. sx_out(bp, CD186x_IER, port->IER);
  561. func_exit();
  562. return;
  563. }
  564. if ((port->xmit_cnt <= 0 && !port->break_length)
  565. || tty->stopped || tty->hw_stopped) {
  566. sx_out(bp, CD186x_CAR, port_No(port));
  567. port->IER &= ~IER_TXRDY;
  568. sx_out(bp, CD186x_IER, port->IER);
  569. func_exit();
  570. return;
  571. }
  572. if (port->break_length) {
  573. if (port->break_length > 0) {
  574. if (port->COR2 & COR2_ETC) {
  575. sx_out(bp, CD186x_TDR, CD186x_C_ESC);
  576. sx_out(bp, CD186x_TDR, CD186x_C_SBRK);
  577. port->COR2 &= ~COR2_ETC;
  578. }
  579. count = min_t(int, port->break_length, 0xff);
  580. sx_out(bp, CD186x_TDR, CD186x_C_ESC);
  581. sx_out(bp, CD186x_TDR, CD186x_C_DELAY);
  582. sx_out(bp, CD186x_TDR, count);
  583. port->break_length -= count;
  584. if (port->break_length == 0)
  585. port->break_length--;
  586. } else {
  587. sx_out(bp, CD186x_TDR, CD186x_C_ESC);
  588. sx_out(bp, CD186x_TDR, CD186x_C_EBRK);
  589. sx_out(bp, CD186x_COR2, port->COR2);
  590. sx_wait_CCR(bp);
  591. sx_out(bp, CD186x_CCR, CCR_CORCHG2);
  592. port->break_length = 0;
  593. }
  594. func_exit();
  595. return;
  596. }
  597. count = CD186x_NFIFO;
  598. do {
  599. sx_out(bp, CD186x_TDR, port->xmit_buf[port->xmit_tail++]);
  600. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
  601. if (--port->xmit_cnt <= 0)
  602. break;
  603. } while (--count > 0);
  604. if (port->xmit_cnt <= 0) {
  605. sx_out(bp, CD186x_CAR, port_No(port));
  606. port->IER &= ~IER_TXRDY;
  607. sx_out(bp, CD186x_IER, port->IER);
  608. }
  609. if (port->xmit_cnt <= port->wakeup_chars)
  610. tty_wakeup(tty);
  611. func_exit();
  612. }
  613. static void sx_check_modem(struct specialix_board *bp)
  614. {
  615. struct specialix_port *port;
  616. struct tty_struct *tty;
  617. unsigned char mcr;
  618. int msvr_cd;
  619. dprintk(SX_DEBUG_SIGNALS, "Modem intr. ");
  620. port = sx_get_port(bp, "Modem");
  621. if (port == NULL)
  622. return;
  623. tty = port->port.tty;
  624. mcr = sx_in(bp, CD186x_MCR);
  625. if ((mcr & MCR_CDCHG)) {
  626. dprintk(SX_DEBUG_SIGNALS, "CD just changed... ");
  627. msvr_cd = sx_in(bp, CD186x_MSVR) & MSVR_CD;
  628. if (msvr_cd) {
  629. dprintk(SX_DEBUG_SIGNALS, "Waking up guys in open.\n");
  630. wake_up_interruptible(&port->port.open_wait);
  631. } else {
  632. dprintk(SX_DEBUG_SIGNALS, "Sending HUP.\n");
  633. tty_hangup(tty);
  634. }
  635. }
  636. #ifdef SPECIALIX_BRAIN_DAMAGED_CTS
  637. if (mcr & MCR_CTSCHG) {
  638. if (sx_in(bp, CD186x_MSVR) & MSVR_CTS) {
  639. tty->hw_stopped = 0;
  640. port->IER |= IER_TXRDY;
  641. if (port->xmit_cnt <= port->wakeup_chars)
  642. tty_wakeup(tty);
  643. } else {
  644. tty->hw_stopped = 1;
  645. port->IER &= ~IER_TXRDY;
  646. }
  647. sx_out(bp, CD186x_IER, port->IER);
  648. }
  649. if (mcr & MCR_DSSXHG) {
  650. if (sx_in(bp, CD186x_MSVR) & MSVR_DSR) {
  651. tty->hw_stopped = 0;
  652. port->IER |= IER_TXRDY;
  653. if (port->xmit_cnt <= port->wakeup_chars)
  654. tty_wakeup(tty);
  655. } else {
  656. tty->hw_stopped = 1;
  657. port->IER &= ~IER_TXRDY;
  658. }
  659. sx_out(bp, CD186x_IER, port->IER);
  660. }
  661. #endif /* SPECIALIX_BRAIN_DAMAGED_CTS */
  662. /* Clear change bits */
  663. sx_out(bp, CD186x_MCR, 0);
  664. }
  665. /* The main interrupt processing routine */
  666. static irqreturn_t sx_interrupt(int dummy, void *dev_id)
  667. {
  668. unsigned char status;
  669. unsigned char ack;
  670. struct specialix_board *bp = dev_id;
  671. unsigned long loop = 0;
  672. int saved_reg;
  673. unsigned long flags;
  674. func_enter();
  675. spin_lock_irqsave(&bp->lock, flags);
  676. dprintk(SX_DEBUG_FLOW, "enter %s port %d room: %ld\n", __func__,
  677. port_No(sx_get_port(bp, "INT")),
  678. SERIAL_XMIT_SIZE - sx_get_port(bp, "ITN")->xmit_cnt - 1);
  679. if (!(bp->flags & SX_BOARD_ACTIVE)) {
  680. dprintk(SX_DEBUG_IRQ, "sx: False interrupt. irq %d.\n",
  681. bp->irq);
  682. spin_unlock_irqrestore(&bp->lock, flags);
  683. func_exit();
  684. return IRQ_NONE;
  685. }
  686. saved_reg = bp->reg;
  687. while (++loop < 16) {
  688. status = sx_in(bp, CD186x_SRSR) &
  689. (SRSR_RREQint | SRSR_TREQint | SRSR_MREQint);
  690. if (status == 0)
  691. break;
  692. if (status & SRSR_RREQint) {
  693. ack = sx_in(bp, CD186x_RRAR);
  694. if (ack == (SX_ID | GIVR_IT_RCV))
  695. sx_receive(bp);
  696. else if (ack == (SX_ID | GIVR_IT_REXC))
  697. sx_receive_exc(bp);
  698. else
  699. printk(KERN_ERR
  700. "sx%d: status: 0x%x Bad receive ack 0x%02x.\n",
  701. board_No(bp), status, ack);
  702. } else if (status & SRSR_TREQint) {
  703. ack = sx_in(bp, CD186x_TRAR);
  704. if (ack == (SX_ID | GIVR_IT_TX))
  705. sx_transmit(bp);
  706. else
  707. printk(KERN_ERR "sx%d: status: 0x%x Bad transmit ack 0x%02x. port: %d\n",
  708. board_No(bp), status, ack,
  709. port_No(sx_get_port(bp, "Int")));
  710. } else if (status & SRSR_MREQint) {
  711. ack = sx_in(bp, CD186x_MRAR);
  712. if (ack == (SX_ID | GIVR_IT_MODEM))
  713. sx_check_modem(bp);
  714. else
  715. printk(KERN_ERR
  716. "sx%d: status: 0x%x Bad modem ack 0x%02x.\n",
  717. board_No(bp), status, ack);
  718. }
  719. sx_out(bp, CD186x_EOIR, 0); /* Mark end of interrupt */
  720. }
  721. bp->reg = saved_reg;
  722. outb(bp->reg, bp->base + SX_ADDR_REG);
  723. spin_unlock_irqrestore(&bp->lock, flags);
  724. func_exit();
  725. return IRQ_HANDLED;
  726. }
  727. /*
  728. * Routines for open & close processing.
  729. */
  730. static void turn_ints_off(struct specialix_board *bp)
  731. {
  732. unsigned long flags;
  733. func_enter();
  734. spin_lock_irqsave(&bp->lock, flags);
  735. (void) sx_in_off(bp, 0); /* Turn off interrupts. */
  736. spin_unlock_irqrestore(&bp->lock, flags);
  737. func_exit();
  738. }
  739. static void turn_ints_on(struct specialix_board *bp)
  740. {
  741. unsigned long flags;
  742. func_enter();
  743. spin_lock_irqsave(&bp->lock, flags);
  744. (void) sx_in(bp, 0); /* Turn ON interrupts. */
  745. spin_unlock_irqrestore(&bp->lock, flags);
  746. func_exit();
  747. }
  748. /* Called with disabled interrupts */
  749. static int sx_setup_board(struct specialix_board *bp)
  750. {
  751. int error;
  752. if (bp->flags & SX_BOARD_ACTIVE)
  753. return 0;
  754. if (bp->flags & SX_BOARD_IS_PCI)
  755. error = request_irq(bp->irq, sx_interrupt,
  756. IRQF_DISABLED | IRQF_SHARED, "specialix IO8+", bp);
  757. else
  758. error = request_irq(bp->irq, sx_interrupt,
  759. IRQF_DISABLED, "specialix IO8+", bp);
  760. if (error)
  761. return error;
  762. turn_ints_on(bp);
  763. bp->flags |= SX_BOARD_ACTIVE;
  764. return 0;
  765. }
  766. /* Called with disabled interrupts */
  767. static void sx_shutdown_board(struct specialix_board *bp)
  768. {
  769. func_enter();
  770. if (!(bp->flags & SX_BOARD_ACTIVE)) {
  771. func_exit();
  772. return;
  773. }
  774. bp->flags &= ~SX_BOARD_ACTIVE;
  775. dprintk(SX_DEBUG_IRQ, "Freeing IRQ%d for board %d.\n",
  776. bp->irq, board_No(bp));
  777. free_irq(bp->irq, bp);
  778. turn_ints_off(bp);
  779. func_exit();
  780. }
  781. static unsigned int sx_crtscts(struct tty_struct *tty)
  782. {
  783. if (sx_rtscts)
  784. return C_CRTSCTS(tty);
  785. return 1;
  786. }
  787. /*
  788. * Setting up port characteristics.
  789. * Must be called with disabled interrupts
  790. */
  791. static void sx_change_speed(struct specialix_board *bp,
  792. struct specialix_port *port)
  793. {
  794. struct tty_struct *tty;
  795. unsigned long baud;
  796. long tmp;
  797. unsigned char cor1 = 0, cor3 = 0;
  798. unsigned char mcor1 = 0, mcor2 = 0;
  799. static unsigned long again;
  800. unsigned long flags;
  801. func_enter();
  802. tty = port->port.tty;
  803. if (!tty || !tty->termios) {
  804. func_exit();
  805. return;
  806. }
  807. port->IER = 0;
  808. port->COR2 = 0;
  809. /* Select port on the board */
  810. spin_lock_irqsave(&bp->lock, flags);
  811. sx_out(bp, CD186x_CAR, port_No(port));
  812. /* The Specialix board doesn't implement the RTS lines.
  813. They are used to set the IRQ level. Don't touch them. */
  814. if (sx_crtscts(tty))
  815. port->MSVR = MSVR_DTR | (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
  816. else
  817. port->MSVR = (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
  818. spin_unlock_irqrestore(&bp->lock, flags);
  819. dprintk(SX_DEBUG_TERMIOS, "sx: got MSVR=%02x.\n", port->MSVR);
  820. baud = tty_get_baud_rate(tty);
  821. if (baud == 38400) {
  822. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  823. baud = 57600;
  824. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  825. baud = 115200;
  826. }
  827. if (!baud) {
  828. /* Drop DTR & exit */
  829. dprintk(SX_DEBUG_TERMIOS, "Dropping DTR... Hmm....\n");
  830. if (!sx_crtscts(tty)) {
  831. port->MSVR &= ~MSVR_DTR;
  832. spin_lock_irqsave(&bp->lock, flags);
  833. sx_out(bp, CD186x_MSVR, port->MSVR);
  834. spin_unlock_irqrestore(&bp->lock, flags);
  835. } else
  836. dprintk(SX_DEBUG_TERMIOS, "Can't drop DTR: no DTR.\n");
  837. return;
  838. } else {
  839. /* Set DTR on */
  840. if (!sx_crtscts(tty))
  841. port->MSVR |= MSVR_DTR;
  842. }
  843. /*
  844. * Now we must calculate some speed depended things
  845. */
  846. /* Set baud rate for port */
  847. tmp = port->custom_divisor ;
  848. if (tmp)
  849. printk(KERN_INFO
  850. "sx%d: Using custom baud rate divisor %ld. \n"
  851. "This is an untested option, please be careful.\n",
  852. port_No(port), tmp);
  853. else
  854. tmp = (((SX_OSCFREQ + baud/2) / baud + CD186x_TPC/2) /
  855. CD186x_TPC);
  856. if (tmp < 0x10 && time_before(again, jiffies)) {
  857. again = jiffies + HZ * 60;
  858. /* Page 48 of version 2.0 of the CL-CD1865 databook */
  859. if (tmp >= 12) {
  860. printk(KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
  861. "Performance degradation is possible.\n"
  862. "Read specialix.txt for more info.\n",
  863. port_No(port), tmp);
  864. } else {
  865. printk(KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
  866. "Warning: overstressing Cirrus chip. This might not work.\n"
  867. "Read specialix.txt for more info.\n", port_No(port), tmp);
  868. }
  869. }
  870. spin_lock_irqsave(&bp->lock, flags);
  871. sx_out(bp, CD186x_RBPRH, (tmp >> 8) & 0xff);
  872. sx_out(bp, CD186x_TBPRH, (tmp >> 8) & 0xff);
  873. sx_out(bp, CD186x_RBPRL, tmp & 0xff);
  874. sx_out(bp, CD186x_TBPRL, tmp & 0xff);
  875. spin_unlock_irqrestore(&bp->lock, flags);
  876. if (port->custom_divisor)
  877. baud = (SX_OSCFREQ + port->custom_divisor/2) /
  878. port->custom_divisor;
  879. baud = (baud + 5) / 10; /* Estimated CPS */
  880. /* Two timer ticks seems enough to wakeup something like SLIP driver */
  881. tmp = ((baud + HZ/2) / HZ) * 2 - CD186x_NFIFO;
  882. port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
  883. SERIAL_XMIT_SIZE - 1 : tmp);
  884. /* Receiver timeout will be transmission time for 1.5 chars */
  885. tmp = (SPECIALIX_TPS + SPECIALIX_TPS/2 + baud/2) / baud;
  886. tmp = (tmp > 0xff) ? 0xff : tmp;
  887. spin_lock_irqsave(&bp->lock, flags);
  888. sx_out(bp, CD186x_RTPR, tmp);
  889. spin_unlock_irqrestore(&bp->lock, flags);
  890. switch (C_CSIZE(tty)) {
  891. case CS5:
  892. cor1 |= COR1_5BITS;
  893. break;
  894. case CS6:
  895. cor1 |= COR1_6BITS;
  896. break;
  897. case CS7:
  898. cor1 |= COR1_7BITS;
  899. break;
  900. case CS8:
  901. cor1 |= COR1_8BITS;
  902. break;
  903. }
  904. if (C_CSTOPB(tty))
  905. cor1 |= COR1_2SB;
  906. cor1 |= COR1_IGNORE;
  907. if (C_PARENB(tty)) {
  908. cor1 |= COR1_NORMPAR;
  909. if (C_PARODD(tty))
  910. cor1 |= COR1_ODDP;
  911. if (I_INPCK(tty))
  912. cor1 &= ~COR1_IGNORE;
  913. }
  914. /* Set marking of some errors */
  915. port->mark_mask = RCSR_OE | RCSR_TOUT;
  916. if (I_INPCK(tty))
  917. port->mark_mask |= RCSR_FE | RCSR_PE;
  918. if (I_BRKINT(tty) || I_PARMRK(tty))
  919. port->mark_mask |= RCSR_BREAK;
  920. if (I_IGNPAR(tty))
  921. port->mark_mask &= ~(RCSR_FE | RCSR_PE);
  922. if (I_IGNBRK(tty)) {
  923. port->mark_mask &= ~RCSR_BREAK;
  924. if (I_IGNPAR(tty))
  925. /* Real raw mode. Ignore all */
  926. port->mark_mask &= ~RCSR_OE;
  927. }
  928. /* Enable Hardware Flow Control */
  929. if (C_CRTSCTS(tty)) {
  930. #ifdef SPECIALIX_BRAIN_DAMAGED_CTS
  931. port->IER |= IER_DSR | IER_CTS;
  932. mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
  933. mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
  934. spin_lock_irqsave(&bp->lock, flags);
  935. tty->hw_stopped = !(sx_in(bp, CD186x_MSVR) &
  936. (MSVR_CTS|MSVR_DSR));
  937. spin_unlock_irqrestore(&bp->lock, flags);
  938. #else
  939. port->COR2 |= COR2_CTSAE;
  940. #endif
  941. }
  942. /* Enable Software Flow Control. FIXME: I'm not sure about this */
  943. /* Some people reported that it works, but I still doubt it */
  944. if (I_IXON(tty)) {
  945. port->COR2 |= COR2_TXIBE;
  946. cor3 |= (COR3_FCT | COR3_SCDE);
  947. if (I_IXANY(tty))
  948. port->COR2 |= COR2_IXM;
  949. spin_lock_irqsave(&bp->lock, flags);
  950. sx_out(bp, CD186x_SCHR1, START_CHAR(tty));
  951. sx_out(bp, CD186x_SCHR2, STOP_CHAR(tty));
  952. sx_out(bp, CD186x_SCHR3, START_CHAR(tty));
  953. sx_out(bp, CD186x_SCHR4, STOP_CHAR(tty));
  954. spin_unlock_irqrestore(&bp->lock, flags);
  955. }
  956. if (!C_CLOCAL(tty)) {
  957. /* Enable CD check */
  958. port->IER |= IER_CD;
  959. mcor1 |= MCOR1_CDZD;
  960. mcor2 |= MCOR2_CDOD;
  961. }
  962. if (C_CREAD(tty))
  963. /* Enable receiver */
  964. port->IER |= IER_RXD;
  965. /* Set input FIFO size (1-8 bytes) */
  966. cor3 |= sx_rxfifo;
  967. /* Setting up CD186x channel registers */
  968. spin_lock_irqsave(&bp->lock, flags);
  969. sx_out(bp, CD186x_COR1, cor1);
  970. sx_out(bp, CD186x_COR2, port->COR2);
  971. sx_out(bp, CD186x_COR3, cor3);
  972. spin_unlock_irqrestore(&bp->lock, flags);
  973. /* Make CD186x know about registers change */
  974. sx_wait_CCR(bp);
  975. spin_lock_irqsave(&bp->lock, flags);
  976. sx_out(bp, CD186x_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
  977. /* Setting up modem option registers */
  978. dprintk(SX_DEBUG_TERMIOS, "Mcor1 = %02x, mcor2 = %02x.\n",
  979. mcor1, mcor2);
  980. sx_out(bp, CD186x_MCOR1, mcor1);
  981. sx_out(bp, CD186x_MCOR2, mcor2);
  982. spin_unlock_irqrestore(&bp->lock, flags);
  983. /* Enable CD186x transmitter & receiver */
  984. sx_wait_CCR(bp);
  985. spin_lock_irqsave(&bp->lock, flags);
  986. sx_out(bp, CD186x_CCR, CCR_TXEN | CCR_RXEN);
  987. /* Enable interrupts */
  988. sx_out(bp, CD186x_IER, port->IER);
  989. /* And finally set the modem lines... */
  990. sx_out(bp, CD186x_MSVR, port->MSVR);
  991. spin_unlock_irqrestore(&bp->lock, flags);
  992. func_exit();
  993. }
  994. /* Must be called with interrupts enabled */
  995. static int sx_setup_port(struct specialix_board *bp,
  996. struct specialix_port *port)
  997. {
  998. unsigned long flags;
  999. func_enter();
  1000. if (port->port.flags & ASYNC_INITIALIZED) {
  1001. func_exit();
  1002. return 0;
  1003. }
  1004. if (!port->xmit_buf) {
  1005. /* We may sleep in get_zeroed_page() */
  1006. unsigned long tmp;
  1007. tmp = get_zeroed_page(GFP_KERNEL);
  1008. if (tmp == 0L) {
  1009. func_exit();
  1010. return -ENOMEM;
  1011. }
  1012. if (port->xmit_buf) {
  1013. free_page(tmp);
  1014. func_exit();
  1015. return -ERESTARTSYS;
  1016. }
  1017. port->xmit_buf = (unsigned char *) tmp;
  1018. }
  1019. spin_lock_irqsave(&port->lock, flags);
  1020. if (port->port.tty)
  1021. clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
  1022. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  1023. sx_change_speed(bp, port);
  1024. port->port.flags |= ASYNC_INITIALIZED;
  1025. spin_unlock_irqrestore(&port->lock, flags);
  1026. func_exit();
  1027. return 0;
  1028. }
  1029. /* Must be called with interrupts disabled */
  1030. static void sx_shutdown_port(struct specialix_board *bp,
  1031. struct specialix_port *port)
  1032. {
  1033. struct tty_struct *tty;
  1034. int i;
  1035. unsigned long flags;
  1036. func_enter();
  1037. if (!(port->port.flags & ASYNC_INITIALIZED)) {
  1038. func_exit();
  1039. return;
  1040. }
  1041. if (sx_debug & SX_DEBUG_FIFO) {
  1042. dprintk(SX_DEBUG_FIFO,
  1043. "sx%d: port %d: %ld overruns, FIFO hits [ ",
  1044. board_No(bp), port_No(port), port->overrun);
  1045. for (i = 0; i < 10; i++)
  1046. dprintk(SX_DEBUG_FIFO, "%ld ", port->hits[i]);
  1047. dprintk(SX_DEBUG_FIFO, "].\n");
  1048. }
  1049. if (port->xmit_buf) {
  1050. free_page((unsigned long) port->xmit_buf);
  1051. port->xmit_buf = NULL;
  1052. }
  1053. /* Select port */
  1054. spin_lock_irqsave(&bp->lock, flags);
  1055. sx_out(bp, CD186x_CAR, port_No(port));
  1056. tty = port->port.tty;
  1057. if (tty == NULL || C_HUPCL(tty)) {
  1058. /* Drop DTR */
  1059. sx_out(bp, CD186x_MSVDTR, 0);
  1060. }
  1061. spin_unlock_irqrestore(&bp->lock, flags);
  1062. /* Reset port */
  1063. sx_wait_CCR(bp);
  1064. spin_lock_irqsave(&bp->lock, flags);
  1065. sx_out(bp, CD186x_CCR, CCR_SOFTRESET);
  1066. /* Disable all interrupts from this port */
  1067. port->IER = 0;
  1068. sx_out(bp, CD186x_IER, port->IER);
  1069. spin_unlock_irqrestore(&bp->lock, flags);
  1070. if (tty)
  1071. set_bit(TTY_IO_ERROR, &tty->flags);
  1072. port->port.flags &= ~ASYNC_INITIALIZED;
  1073. if (!bp->count)
  1074. sx_shutdown_board(bp);
  1075. func_exit();
  1076. }
  1077. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  1078. struct specialix_port *port)
  1079. {
  1080. DECLARE_WAITQUEUE(wait, current);
  1081. struct specialix_board *bp = port_Board(port);
  1082. int retval;
  1083. int do_clocal = 0;
  1084. int CD;
  1085. unsigned long flags;
  1086. func_enter();
  1087. /*
  1088. * If the device is in the middle of being closed, then block
  1089. * until it's done, and then try again.
  1090. */
  1091. if (tty_hung_up_p(filp) || port->port.flags & ASYNC_CLOSING) {
  1092. interruptible_sleep_on(&port->port.close_wait);
  1093. if (port->port.flags & ASYNC_HUP_NOTIFY) {
  1094. func_exit();
  1095. return -EAGAIN;
  1096. } else {
  1097. func_exit();
  1098. return -ERESTARTSYS;
  1099. }
  1100. }
  1101. /*
  1102. * If non-blocking mode is set, or the port is not enabled,
  1103. * then make the check up front and then exit.
  1104. */
  1105. if ((filp->f_flags & O_NONBLOCK) ||
  1106. (tty->flags & (1 << TTY_IO_ERROR))) {
  1107. port->port.flags |= ASYNC_NORMAL_ACTIVE;
  1108. func_exit();
  1109. return 0;
  1110. }
  1111. if (C_CLOCAL(tty))
  1112. do_clocal = 1;
  1113. /*
  1114. * Block waiting for the carrier detect and the line to become
  1115. * free (i.e., not in use by the callout). While we are in
  1116. * this loop, info->count is dropped by one, so that
  1117. * rs_close() knows when to free things. We restore it upon
  1118. * exit, either normal or abnormal.
  1119. */
  1120. retval = 0;
  1121. add_wait_queue(&port->port.open_wait, &wait);
  1122. spin_lock_irqsave(&port->lock, flags);
  1123. if (!tty_hung_up_p(filp))
  1124. port->port.count--;
  1125. spin_unlock_irqrestore(&port->lock, flags);
  1126. port->port.blocked_open++;
  1127. while (1) {
  1128. spin_lock_irqsave(&bp->lock, flags);
  1129. sx_out(bp, CD186x_CAR, port_No(port));
  1130. CD = sx_in(bp, CD186x_MSVR) & MSVR_CD;
  1131. if (sx_crtscts(tty)) {
  1132. /* Activate RTS */
  1133. port->MSVR |= MSVR_DTR; /* WTF? */
  1134. sx_out(bp, CD186x_MSVR, port->MSVR);
  1135. } else {
  1136. /* Activate DTR */
  1137. port->MSVR |= MSVR_DTR;
  1138. sx_out(bp, CD186x_MSVR, port->MSVR);
  1139. }
  1140. spin_unlock_irqrestore(&bp->lock, flags);
  1141. set_current_state(TASK_INTERRUPTIBLE);
  1142. if (tty_hung_up_p(filp) ||
  1143. !(port->port.flags & ASYNC_INITIALIZED)) {
  1144. if (port->port.flags & ASYNC_HUP_NOTIFY)
  1145. retval = -EAGAIN;
  1146. else
  1147. retval = -ERESTARTSYS;
  1148. break;
  1149. }
  1150. if (!(port->port.flags & ASYNC_CLOSING) &&
  1151. (do_clocal || CD))
  1152. break;
  1153. if (signal_pending(current)) {
  1154. retval = -ERESTARTSYS;
  1155. break;
  1156. }
  1157. tty_unlock();
  1158. schedule();
  1159. tty_lock();
  1160. }
  1161. set_current_state(TASK_RUNNING);
  1162. remove_wait_queue(&port->port.open_wait, &wait);
  1163. spin_lock_irqsave(&port->lock, flags);
  1164. if (!tty_hung_up_p(filp))
  1165. port->port.count++;
  1166. port->port.blocked_open--;
  1167. spin_unlock_irqrestore(&port->lock, flags);
  1168. if (retval) {
  1169. func_exit();
  1170. return retval;
  1171. }
  1172. port->port.flags |= ASYNC_NORMAL_ACTIVE;
  1173. func_exit();
  1174. return 0;
  1175. }
  1176. static int sx_open(struct tty_struct *tty, struct file *filp)
  1177. {
  1178. int board;
  1179. int error;
  1180. struct specialix_port *port;
  1181. struct specialix_board *bp;
  1182. int i;
  1183. unsigned long flags;
  1184. func_enter();
  1185. board = SX_BOARD(tty->index);
  1186. if (board >= SX_NBOARD || !(sx_board[board].flags & SX_BOARD_PRESENT)) {
  1187. func_exit();
  1188. return -ENODEV;
  1189. }
  1190. bp = &sx_board[board];
  1191. port = sx_port + board * SX_NPORT + SX_PORT(tty->index);
  1192. port->overrun = 0;
  1193. for (i = 0; i < 10; i++)
  1194. port->hits[i] = 0;
  1195. dprintk(SX_DEBUG_OPEN,
  1196. "Board = %d, bp = %p, port = %p, portno = %d.\n",
  1197. board, bp, port, SX_PORT(tty->index));
  1198. if (sx_paranoia_check(port, tty->name, "sx_open")) {
  1199. func_exit();
  1200. return -ENODEV;
  1201. }
  1202. error = sx_setup_board(bp);
  1203. if (error) {
  1204. func_exit();
  1205. return error;
  1206. }
  1207. spin_lock_irqsave(&bp->lock, flags);
  1208. port->port.count++;
  1209. bp->count++;
  1210. tty->driver_data = port;
  1211. port->port.tty = tty;
  1212. spin_unlock_irqrestore(&bp->lock, flags);
  1213. error = sx_setup_port(bp, port);
  1214. if (error) {
  1215. func_exit();
  1216. return error;
  1217. }
  1218. error = block_til_ready(tty, filp, port);
  1219. if (error) {
  1220. func_exit();
  1221. return error;
  1222. }
  1223. func_exit();
  1224. return 0;
  1225. }
  1226. static void sx_flush_buffer(struct tty_struct *tty)
  1227. {
  1228. struct specialix_port *port = tty->driver_data;
  1229. unsigned long flags;
  1230. struct specialix_board *bp;
  1231. func_enter();
  1232. if (sx_paranoia_check(port, tty->name, "sx_flush_buffer")) {
  1233. func_exit();
  1234. return;
  1235. }
  1236. bp = port_Board(port);
  1237. spin_lock_irqsave(&port->lock, flags);
  1238. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  1239. spin_unlock_irqrestore(&port->lock, flags);
  1240. tty_wakeup(tty);
  1241. func_exit();
  1242. }
  1243. static void sx_close(struct tty_struct *tty, struct file *filp)
  1244. {
  1245. struct specialix_port *port = tty->driver_data;
  1246. struct specialix_board *bp;
  1247. unsigned long flags;
  1248. unsigned long timeout;
  1249. func_enter();
  1250. if (!port || sx_paranoia_check(port, tty->name, "close")) {
  1251. func_exit();
  1252. return;
  1253. }
  1254. spin_lock_irqsave(&port->lock, flags);
  1255. if (tty_hung_up_p(filp)) {
  1256. spin_unlock_irqrestore(&port->lock, flags);
  1257. func_exit();
  1258. return;
  1259. }
  1260. bp = port_Board(port);
  1261. if (tty->count == 1 && port->port.count != 1) {
  1262. printk(KERN_ERR "sx%d: sx_close: bad port count;"
  1263. " tty->count is 1, port count is %d\n",
  1264. board_No(bp), port->port.count);
  1265. port->port.count = 1;
  1266. }
  1267. if (port->port.count > 1) {
  1268. port->port.count--;
  1269. bp->count--;
  1270. spin_unlock_irqrestore(&port->lock, flags);
  1271. func_exit();
  1272. return;
  1273. }
  1274. port->port.flags |= ASYNC_CLOSING;
  1275. /*
  1276. * Now we wait for the transmit buffer to clear; and we notify
  1277. * the line discipline to only process XON/XOFF characters.
  1278. */
  1279. tty->closing = 1;
  1280. spin_unlock_irqrestore(&port->lock, flags);
  1281. dprintk(SX_DEBUG_OPEN, "Closing\n");
  1282. if (port->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1283. tty_wait_until_sent(tty, port->port.closing_wait);
  1284. /*
  1285. * At this point we stop accepting input. To do this, we
  1286. * disable the receive line status interrupts, and tell the
  1287. * interrupt driver to stop checking the data ready bit in the
  1288. * line status register.
  1289. */
  1290. dprintk(SX_DEBUG_OPEN, "Closed\n");
  1291. port->IER &= ~IER_RXD;
  1292. if (port->port.flags & ASYNC_INITIALIZED) {
  1293. port->IER &= ~IER_TXRDY;
  1294. port->IER |= IER_TXEMPTY;
  1295. spin_lock_irqsave(&bp->lock, flags);
  1296. sx_out(bp, CD186x_CAR, port_No(port));
  1297. sx_out(bp, CD186x_IER, port->IER);
  1298. spin_unlock_irqrestore(&bp->lock, flags);
  1299. /*
  1300. * Before we drop DTR, make sure the UART transmitter
  1301. * has completely drained; this is especially
  1302. * important if there is a transmit FIFO!
  1303. */
  1304. timeout = jiffies+HZ;
  1305. while (port->IER & IER_TXEMPTY) {
  1306. set_current_state(TASK_INTERRUPTIBLE);
  1307. msleep_interruptible(jiffies_to_msecs(port->timeout));
  1308. if (time_after(jiffies, timeout)) {
  1309. printk(KERN_INFO "Timeout waiting for close\n");
  1310. break;
  1311. }
  1312. }
  1313. }
  1314. if (--bp->count < 0) {
  1315. printk(KERN_ERR
  1316. "sx%d: sx_shutdown_port: bad board count: %d port: %d\n",
  1317. board_No(bp), bp->count, tty->index);
  1318. bp->count = 0;
  1319. }
  1320. if (--port->port.count < 0) {
  1321. printk(KERN_ERR
  1322. "sx%d: sx_close: bad port count for tty%d: %d\n",
  1323. board_No(bp), port_No(port), port->port.count);
  1324. port->port.count = 0;
  1325. }
  1326. sx_shutdown_port(bp, port);
  1327. sx_flush_buffer(tty);
  1328. tty_ldisc_flush(tty);
  1329. spin_lock_irqsave(&port->lock, flags);
  1330. tty->closing = 0;
  1331. port->port.tty = NULL;
  1332. spin_unlock_irqrestore(&port->lock, flags);
  1333. if (port->port.blocked_open) {
  1334. if (port->port.close_delay)
  1335. msleep_interruptible(
  1336. jiffies_to_msecs(port->port.close_delay));
  1337. wake_up_interruptible(&port->port.open_wait);
  1338. }
  1339. port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1340. wake_up_interruptible(&port->port.close_wait);
  1341. func_exit();
  1342. }
  1343. static int sx_write(struct tty_struct *tty,
  1344. const unsigned char *buf, int count)
  1345. {
  1346. struct specialix_port *port = tty->driver_data;
  1347. struct specialix_board *bp;
  1348. int c, total = 0;
  1349. unsigned long flags;
  1350. func_enter();
  1351. if (sx_paranoia_check(port, tty->name, "sx_write")) {
  1352. func_exit();
  1353. return 0;
  1354. }
  1355. bp = port_Board(port);
  1356. if (!port->xmit_buf) {
  1357. func_exit();
  1358. return 0;
  1359. }
  1360. while (1) {
  1361. spin_lock_irqsave(&port->lock, flags);
  1362. c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
  1363. SERIAL_XMIT_SIZE - port->xmit_head));
  1364. if (c <= 0) {
  1365. spin_unlock_irqrestore(&port->lock, flags);
  1366. break;
  1367. }
  1368. memcpy(port->xmit_buf + port->xmit_head, buf, c);
  1369. port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  1370. port->xmit_cnt += c;
  1371. spin_unlock_irqrestore(&port->lock, flags);
  1372. buf += c;
  1373. count -= c;
  1374. total += c;
  1375. }
  1376. spin_lock_irqsave(&bp->lock, flags);
  1377. if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
  1378. !(port->IER & IER_TXRDY)) {
  1379. port->IER |= IER_TXRDY;
  1380. sx_out(bp, CD186x_CAR, port_No(port));
  1381. sx_out(bp, CD186x_IER, port->IER);
  1382. }
  1383. spin_unlock_irqrestore(&bp->lock, flags);
  1384. func_exit();
  1385. return total;
  1386. }
  1387. static int sx_put_char(struct tty_struct *tty, unsigned char ch)
  1388. {
  1389. struct specialix_port *port = tty->driver_data;
  1390. unsigned long flags;
  1391. struct specialix_board *bp;
  1392. func_enter();
  1393. if (sx_paranoia_check(port, tty->name, "sx_put_char")) {
  1394. func_exit();
  1395. return 0;
  1396. }
  1397. dprintk(SX_DEBUG_TX, "check tty: %p %p\n", tty, port->xmit_buf);
  1398. if (!port->xmit_buf) {
  1399. func_exit();
  1400. return 0;
  1401. }
  1402. bp = port_Board(port);
  1403. spin_lock_irqsave(&port->lock, flags);
  1404. dprintk(SX_DEBUG_TX, "xmit_cnt: %d xmit_buf: %p\n",
  1405. port->xmit_cnt, port->xmit_buf);
  1406. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1 || !port->xmit_buf) {
  1407. spin_unlock_irqrestore(&port->lock, flags);
  1408. dprintk(SX_DEBUG_TX, "Exit size\n");
  1409. func_exit();
  1410. return 0;
  1411. }
  1412. dprintk(SX_DEBUG_TX, "Handle xmit: %p %p\n", port, port->xmit_buf);
  1413. port->xmit_buf[port->xmit_head++] = ch;
  1414. port->xmit_head &= SERIAL_XMIT_SIZE - 1;
  1415. port->xmit_cnt++;
  1416. spin_unlock_irqrestore(&port->lock, flags);
  1417. func_exit();
  1418. return 1;
  1419. }
  1420. static void sx_flush_chars(struct tty_struct *tty)
  1421. {
  1422. struct specialix_port *port = tty->driver_data;
  1423. unsigned long flags;
  1424. struct specialix_board *bp = port_Board(port);
  1425. func_enter();
  1426. if (sx_paranoia_check(port, tty->name, "sx_flush_chars")) {
  1427. func_exit();
  1428. return;
  1429. }
  1430. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  1431. !port->xmit_buf) {
  1432. func_exit();
  1433. return;
  1434. }
  1435. spin_lock_irqsave(&bp->lock, flags);
  1436. port->IER |= IER_TXRDY;
  1437. sx_out(port_Board(port), CD186x_CAR, port_No(port));
  1438. sx_out(port_Board(port), CD186x_IER, port->IER);
  1439. spin_unlock_irqrestore(&bp->lock, flags);
  1440. func_exit();
  1441. }
  1442. static int sx_write_room(struct tty_struct *tty)
  1443. {
  1444. struct specialix_port *port = tty->driver_data;
  1445. int ret;
  1446. func_enter();
  1447. if (sx_paranoia_check(port, tty->name, "sx_write_room")) {
  1448. func_exit();
  1449. return 0;
  1450. }
  1451. ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  1452. if (ret < 0)
  1453. ret = 0;
  1454. func_exit();
  1455. return ret;
  1456. }
  1457. static int sx_chars_in_buffer(struct tty_struct *tty)
  1458. {
  1459. struct specialix_port *port = tty->driver_data;
  1460. func_enter();
  1461. if (sx_paranoia_check(port, tty->name, "sx_chars_in_buffer")) {
  1462. func_exit();
  1463. return 0;
  1464. }
  1465. func_exit();
  1466. return port->xmit_cnt;
  1467. }
  1468. static int sx_tiocmget(struct tty_struct *tty)
  1469. {
  1470. struct specialix_port *port = tty->driver_data;
  1471. struct specialix_board *bp;
  1472. unsigned char status;
  1473. unsigned int result;
  1474. unsigned long flags;
  1475. func_enter();
  1476. if (sx_paranoia_check(port, tty->name, __func__)) {
  1477. func_exit();
  1478. return -ENODEV;
  1479. }
  1480. bp = port_Board(port);
  1481. spin_lock_irqsave(&bp->lock, flags);
  1482. sx_out(bp, CD186x_CAR, port_No(port));
  1483. status = sx_in(bp, CD186x_MSVR);
  1484. spin_unlock_irqrestore(&bp->lock, flags);
  1485. dprintk(SX_DEBUG_INIT, "Got msvr[%d] = %02x, car = %d.\n",
  1486. port_No(port), status, sx_in(bp, CD186x_CAR));
  1487. dprintk(SX_DEBUG_INIT, "sx_port = %p, port = %p\n", sx_port, port);
  1488. if (sx_crtscts(port->port.tty)) {
  1489. result = TIOCM_DTR | TIOCM_DSR
  1490. | ((status & MSVR_DTR) ? TIOCM_RTS : 0)
  1491. | ((status & MSVR_CD) ? TIOCM_CAR : 0)
  1492. | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
  1493. } else {
  1494. result = TIOCM_RTS | TIOCM_DSR
  1495. | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
  1496. | ((status & MSVR_CD) ? TIOCM_CAR : 0)
  1497. | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
  1498. }
  1499. func_exit();
  1500. return result;
  1501. }
  1502. static int sx_tiocmset(struct tty_struct *tty,
  1503. unsigned int set, unsigned int clear)
  1504. {
  1505. struct specialix_port *port = tty->driver_data;
  1506. unsigned long flags;
  1507. struct specialix_board *bp;
  1508. func_enter();
  1509. if (sx_paranoia_check(port, tty->name, __func__)) {
  1510. func_exit();
  1511. return -ENODEV;
  1512. }
  1513. bp = port_Board(port);
  1514. spin_lock_irqsave(&port->lock, flags);
  1515. if (sx_crtscts(port->port.tty)) {
  1516. if (set & TIOCM_RTS)
  1517. port->MSVR |= MSVR_DTR;
  1518. } else {
  1519. if (set & TIOCM_DTR)
  1520. port->MSVR |= MSVR_DTR;
  1521. }
  1522. if (sx_crtscts(port->port.tty)) {
  1523. if (clear & TIOCM_RTS)
  1524. port->MSVR &= ~MSVR_DTR;
  1525. } else {
  1526. if (clear & TIOCM_DTR)
  1527. port->MSVR &= ~MSVR_DTR;
  1528. }
  1529. spin_lock(&bp->lock);
  1530. sx_out(bp, CD186x_CAR, port_No(port));
  1531. sx_out(bp, CD186x_MSVR, port->MSVR);
  1532. spin_unlock(&bp->lock);
  1533. spin_unlock_irqrestore(&port->lock, flags);
  1534. func_exit();
  1535. return 0;
  1536. }
  1537. static int sx_send_break(struct tty_struct *tty, int length)
  1538. {
  1539. struct specialix_port *port = tty->driver_data;
  1540. struct specialix_board *bp = port_Board(port);
  1541. unsigned long flags;
  1542. func_enter();
  1543. if (length == 0 || length == -1)
  1544. return -EOPNOTSUPP;
  1545. spin_lock_irqsave(&port->lock, flags);
  1546. port->break_length = SPECIALIX_TPS / HZ * length;
  1547. port->COR2 |= COR2_ETC;
  1548. port->IER |= IER_TXRDY;
  1549. spin_lock(&bp->lock);
  1550. sx_out(bp, CD186x_CAR, port_No(port));
  1551. sx_out(bp, CD186x_COR2, port->COR2);
  1552. sx_out(bp, CD186x_IER, port->IER);
  1553. spin_unlock(&bp->lock);
  1554. spin_unlock_irqrestore(&port->lock, flags);
  1555. sx_wait_CCR(bp);
  1556. spin_lock_irqsave(&bp->lock, flags);
  1557. sx_out(bp, CD186x_CCR, CCR_CORCHG2);
  1558. spin_unlock_irqrestore(&bp->lock, flags);
  1559. sx_wait_CCR(bp);
  1560. func_exit();
  1561. return 0;
  1562. }
  1563. static int sx_set_serial_info(struct specialix_port *port,
  1564. struct serial_struct __user *newinfo)
  1565. {
  1566. struct serial_struct tmp;
  1567. struct specialix_board *bp = port_Board(port);
  1568. int change_speed;
  1569. func_enter();
  1570. if (copy_from_user(&tmp, newinfo, sizeof(tmp))) {
  1571. func_exit();
  1572. return -EFAULT;
  1573. }
  1574. mutex_lock(&port->port.mutex);
  1575. change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
  1576. (tmp.flags & ASYNC_SPD_MASK));
  1577. change_speed |= (tmp.custom_divisor != port->custom_divisor);
  1578. if (!capable(CAP_SYS_ADMIN)) {
  1579. if ((tmp.close_delay != port->port.close_delay) ||
  1580. (tmp.closing_wait != port->port.closing_wait) ||
  1581. ((tmp.flags & ~ASYNC_USR_MASK) !=
  1582. (port->port.flags & ~ASYNC_USR_MASK))) {
  1583. func_exit();
  1584. mutex_unlock(&port->port.mutex);
  1585. return -EPERM;
  1586. }
  1587. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  1588. (tmp.flags & ASYNC_USR_MASK));
  1589. port->custom_divisor = tmp.custom_divisor;
  1590. } else {
  1591. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  1592. (tmp.flags & ASYNC_FLAGS));
  1593. port->port.close_delay = tmp.close_delay;
  1594. port->port.closing_wait = tmp.closing_wait;
  1595. port->custom_divisor = tmp.custom_divisor;
  1596. }
  1597. if (change_speed)
  1598. sx_change_speed(bp, port);
  1599. func_exit();
  1600. mutex_unlock(&port->port.mutex);
  1601. return 0;
  1602. }
  1603. static int sx_get_serial_info(struct specialix_port *port,
  1604. struct serial_struct __user *retinfo)
  1605. {
  1606. struct serial_struct tmp;
  1607. struct specialix_board *bp = port_Board(port);
  1608. func_enter();
  1609. memset(&tmp, 0, sizeof(tmp));
  1610. mutex_lock(&port->port.mutex);
  1611. tmp.type = PORT_CIRRUS;
  1612. tmp.line = port - sx_port;
  1613. tmp.port = bp->base;
  1614. tmp.irq = bp->irq;
  1615. tmp.flags = port->port.flags;
  1616. tmp.baud_base = (SX_OSCFREQ + CD186x_TPC/2) / CD186x_TPC;
  1617. tmp.close_delay = port->port.close_delay * HZ/100;
  1618. tmp.closing_wait = port->port.closing_wait * HZ/100;
  1619. tmp.custom_divisor = port->custom_divisor;
  1620. tmp.xmit_fifo_size = CD186x_NFIFO;
  1621. mutex_unlock(&port->port.mutex);
  1622. if (copy_to_user(retinfo, &tmp, sizeof(tmp))) {
  1623. func_exit();
  1624. return -EFAULT;
  1625. }
  1626. func_exit();
  1627. return 0;
  1628. }
  1629. static int sx_ioctl(struct tty_struct *tty,
  1630. unsigned int cmd, unsigned long arg)
  1631. {
  1632. struct specialix_port *port = tty->driver_data;
  1633. void __user *argp = (void __user *)arg;
  1634. func_enter();
  1635. if (sx_paranoia_check(port, tty->name, "sx_ioctl")) {
  1636. func_exit();
  1637. return -ENODEV;
  1638. }
  1639. switch (cmd) {
  1640. case TIOCGSERIAL:
  1641. func_exit();
  1642. return sx_get_serial_info(port, argp);
  1643. case TIOCSSERIAL:
  1644. func_exit();
  1645. return sx_set_serial_info(port, argp);
  1646. default:
  1647. func_exit();
  1648. return -ENOIOCTLCMD;
  1649. }
  1650. func_exit();
  1651. return 0;
  1652. }
  1653. static void sx_throttle(struct tty_struct *tty)
  1654. {
  1655. struct specialix_port *port = tty->driver_data;
  1656. struct specialix_board *bp;
  1657. unsigned long flags;
  1658. func_enter();
  1659. if (sx_paranoia_check(port, tty->name, "sx_throttle")) {
  1660. func_exit();
  1661. return;
  1662. }
  1663. bp = port_Board(port);
  1664. /* Use DTR instead of RTS ! */
  1665. if (sx_crtscts(tty))
  1666. port->MSVR &= ~MSVR_DTR;
  1667. else {
  1668. /* Auch!!! I think the system shouldn't call this then. */
  1669. /* Or maybe we're supposed (allowed?) to do our side of hw
  1670. handshake anyway, even when hardware handshake is off.
  1671. When you see this in your logs, please report.... */
  1672. printk(KERN_ERR
  1673. "sx%d: Need to throttle, but can't (hardware hs is off)\n",
  1674. port_No(port));
  1675. }
  1676. spin_lock_irqsave(&bp->lock, flags);
  1677. sx_out(bp, CD186x_CAR, port_No(port));
  1678. spin_unlock_irqrestore(&bp->lock, flags);
  1679. if (I_IXOFF(tty)) {
  1680. sx_wait_CCR(bp);
  1681. spin_lock_irqsave(&bp->lock, flags);
  1682. sx_out(bp, CD186x_CCR, CCR_SSCH2);
  1683. spin_unlock_irqrestore(&bp->lock, flags);
  1684. sx_wait_CCR(bp);
  1685. }
  1686. spin_lock_irqsave(&bp->lock, flags);
  1687. sx_out(bp, CD186x_MSVR, port->MSVR);
  1688. spin_unlock_irqrestore(&bp->lock, flags);
  1689. func_exit();
  1690. }
  1691. static void sx_unthrottle(struct tty_struct *tty)
  1692. {
  1693. struct specialix_port *port = tty->driver_data;
  1694. struct specialix_board *bp;
  1695. unsigned long flags;
  1696. func_enter();
  1697. if (sx_paranoia_check(port, tty->name, "sx_unthrottle")) {
  1698. func_exit();
  1699. return;
  1700. }
  1701. bp = port_Board(port);
  1702. spin_lock_irqsave(&port->lock, flags);
  1703. /* XXXX Use DTR INSTEAD???? */
  1704. if (sx_crtscts(tty))
  1705. port->MSVR |= MSVR_DTR;
  1706. /* Else clause: see remark in "sx_throttle"... */
  1707. spin_lock(&bp->lock);
  1708. sx_out(bp, CD186x_CAR, port_No(port));
  1709. spin_unlock(&bp->lock);
  1710. if (I_IXOFF(tty)) {
  1711. spin_unlock_irqrestore(&port->lock, flags);
  1712. sx_wait_CCR(bp);
  1713. spin_lock_irqsave(&bp->lock, flags);
  1714. sx_out(bp, CD186x_CCR, CCR_SSCH1);
  1715. spin_unlock_irqrestore(&bp->lock, flags);
  1716. sx_wait_CCR(bp);
  1717. spin_lock_irqsave(&port->lock, flags);
  1718. }
  1719. spin_lock(&bp->lock);
  1720. sx_out(bp, CD186x_MSVR, port->MSVR);
  1721. spin_unlock(&bp->lock);
  1722. spin_unlock_irqrestore(&port->lock, flags);
  1723. func_exit();
  1724. }
  1725. static void sx_stop(struct tty_struct *tty)
  1726. {
  1727. struct specialix_port *port = tty->driver_data;
  1728. struct specialix_board *bp;
  1729. unsigned long flags;
  1730. func_enter();
  1731. if (sx_paranoia_check(port, tty->name, "sx_stop")) {
  1732. func_exit();
  1733. return;
  1734. }
  1735. bp = port_Board(port);
  1736. spin_lock_irqsave(&port->lock, flags);
  1737. port->IER &= ~IER_TXRDY;
  1738. spin_lock(&bp->lock);
  1739. sx_out(bp, CD186x_CAR, port_No(port));
  1740. sx_out(bp, CD186x_IER, port->IER);
  1741. spin_unlock(&bp->lock);
  1742. spin_unlock_irqrestore(&port->lock, flags);
  1743. func_exit();
  1744. }
  1745. static void sx_start(struct tty_struct *tty)
  1746. {
  1747. struct specialix_port *port = tty->driver_data;
  1748. struct specialix_board *bp;
  1749. unsigned long flags;
  1750. func_enter();
  1751. if (sx_paranoia_check(port, tty->name, "sx_start")) {
  1752. func_exit();
  1753. return;
  1754. }
  1755. bp = port_Board(port);
  1756. spin_lock_irqsave(&port->lock, flags);
  1757. if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
  1758. port->IER |= IER_TXRDY;
  1759. spin_lock(&bp->lock);
  1760. sx_out(bp, CD186x_CAR, port_No(port));
  1761. sx_out(bp, CD186x_IER, port->IER);
  1762. spin_unlock(&bp->lock);
  1763. }
  1764. spin_unlock_irqrestore(&port->lock, flags);
  1765. func_exit();
  1766. }
  1767. static void sx_hangup(struct tty_struct *tty)
  1768. {
  1769. struct specialix_port *port = tty->driver_data;
  1770. struct specialix_board *bp;
  1771. unsigned long flags;
  1772. func_enter();
  1773. if (sx_paranoia_check(port, tty->name, "sx_hangup")) {
  1774. func_exit();
  1775. return;
  1776. }
  1777. bp = port_Board(port);
  1778. sx_shutdown_port(bp, port);
  1779. spin_lock_irqsave(&port->lock, flags);
  1780. bp->count -= port->port.count;
  1781. if (bp->count < 0) {
  1782. printk(KERN_ERR
  1783. "sx%d: sx_hangup: bad board count: %d port: %d\n",
  1784. board_No(bp), bp->count, tty->index);
  1785. bp->count = 0;
  1786. }
  1787. port->port.count = 0;
  1788. port->port.flags &= ~ASYNC_NORMAL_ACTIVE;
  1789. port->port.tty = NULL;
  1790. spin_unlock_irqrestore(&port->lock, flags);
  1791. wake_up_interruptible(&port->port.open_wait);
  1792. func_exit();
  1793. }
  1794. static void sx_set_termios(struct tty_struct *tty,
  1795. struct ktermios *old_termios)
  1796. {
  1797. struct specialix_port *port = tty->driver_data;
  1798. unsigned long flags;
  1799. struct specialix_board *bp;
  1800. if (sx_paranoia_check(port, tty->name, "sx_set_termios"))
  1801. return;
  1802. bp = port_Board(port);
  1803. spin_lock_irqsave(&port->lock, flags);
  1804. sx_change_speed(port_Board(port), port);
  1805. spin_unlock_irqrestore(&port->lock, flags);
  1806. if ((old_termios->c_cflag & CRTSCTS) &&
  1807. !(tty->termios->c_cflag & CRTSCTS)) {
  1808. tty->hw_stopped = 0;
  1809. sx_start(tty);
  1810. }
  1811. }
  1812. static const struct tty_operations sx_ops = {
  1813. .open = sx_open,
  1814. .close = sx_close,
  1815. .write = sx_write,
  1816. .put_char = sx_put_char,
  1817. .flush_chars = sx_flush_chars,
  1818. .write_room = sx_write_room,
  1819. .chars_in_buffer = sx_chars_in_buffer,
  1820. .flush_buffer = sx_flush_buffer,
  1821. .ioctl = sx_ioctl,
  1822. .throttle = sx_throttle,
  1823. .unthrottle = sx_unthrottle,
  1824. .set_termios = sx_set_termios,
  1825. .stop = sx_stop,
  1826. .start = sx_start,
  1827. .hangup = sx_hangup,
  1828. .tiocmget = sx_tiocmget,
  1829. .tiocmset = sx_tiocmset,
  1830. .break_ctl = sx_send_break,
  1831. };
  1832. static int sx_init_drivers(void)
  1833. {
  1834. int error;
  1835. int i;
  1836. func_enter();
  1837. specialix_driver = alloc_tty_driver(SX_NBOARD * SX_NPORT);
  1838. if (!specialix_driver) {
  1839. printk(KERN_ERR "sx: Couldn't allocate tty_driver.\n");
  1840. func_exit();
  1841. return 1;
  1842. }
  1843. specialix_driver->owner = THIS_MODULE;
  1844. specialix_driver->name = "ttyW";
  1845. specialix_driver->major = SPECIALIX_NORMAL_MAJOR;
  1846. specialix_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1847. specialix_driver->subtype = SERIAL_TYPE_NORMAL;
  1848. specialix_driver->init_termios = tty_std_termios;
  1849. specialix_driver->init_termios.c_cflag =
  1850. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1851. specialix_driver->init_termios.c_ispeed = 9600;
  1852. specialix_driver->init_termios.c_ospeed = 9600;
  1853. specialix_driver->flags = TTY_DRIVER_REAL_RAW |
  1854. TTY_DRIVER_HARDWARE_BREAK;
  1855. tty_set_operations(specialix_driver, &sx_ops);
  1856. error = tty_register_driver(specialix_driver);
  1857. if (error) {
  1858. put_tty_driver(specialix_driver);
  1859. printk(KERN_ERR
  1860. "sx: Couldn't register specialix IO8+ driver, error = %d\n",
  1861. error);
  1862. func_exit();
  1863. return 1;
  1864. }
  1865. memset(sx_port, 0, sizeof(sx_port));
  1866. for (i = 0; i < SX_NPORT * SX_NBOARD; i++) {
  1867. sx_port[i].magic = SPECIALIX_MAGIC;
  1868. tty_port_init(&sx_port[i].port);
  1869. spin_lock_init(&sx_port[i].lock);
  1870. }
  1871. func_exit();
  1872. return 0;
  1873. }
  1874. static void sx_release_drivers(void)
  1875. {
  1876. func_enter();
  1877. tty_unregister_driver(specialix_driver);
  1878. put_tty_driver(specialix_driver);
  1879. func_exit();
  1880. }
  1881. /*
  1882. * This routine must be called by kernel at boot time
  1883. */
  1884. static int __init specialix_init(void)
  1885. {
  1886. int i;
  1887. int found = 0;
  1888. func_enter();
  1889. printk(KERN_INFO "sx: Specialix IO8+ driver v" VERSION ", (c) R.E.Wolff 1997/1998.\n");
  1890. printk(KERN_INFO "sx: derived from work (c) D.Gorodchanin 1994-1996.\n");
  1891. if (sx_rtscts)
  1892. printk(KERN_INFO
  1893. "sx: DTR/RTS pin is RTS when CRTSCTS is on.\n");
  1894. else
  1895. printk(KERN_INFO "sx: DTR/RTS pin is always RTS.\n");
  1896. for (i = 0; i < SX_NBOARD; i++)
  1897. spin_lock_init(&sx_board[i].lock);
  1898. if (sx_init_drivers()) {
  1899. func_exit();
  1900. return -EIO;
  1901. }
  1902. for (i = 0; i < SX_NBOARD; i++)
  1903. if (sx_board[i].base && !sx_probe(&sx_board[i]))
  1904. found++;
  1905. #ifdef CONFIG_PCI
  1906. {
  1907. struct pci_dev *pdev = NULL;
  1908. i = 0;
  1909. while (i < SX_NBOARD) {
  1910. if (sx_board[i].flags & SX_BOARD_PRESENT) {
  1911. i++;
  1912. continue;
  1913. }
  1914. pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX,
  1915. PCI_DEVICE_ID_SPECIALIX_IO8, pdev);
  1916. if (!pdev)
  1917. break;
  1918. if (pci_enable_device(pdev))
  1919. continue;
  1920. sx_board[i].irq = pdev->irq;
  1921. sx_board[i].base = pci_resource_start(pdev, 2);
  1922. sx_board[i].flags |= SX_BOARD_IS_PCI;
  1923. if (!sx_probe(&sx_board[i]))
  1924. found++;
  1925. }
  1926. /* May exit pci_get sequence early with lots of boards */
  1927. if (pdev != NULL)
  1928. pci_dev_put(pdev);
  1929. }
  1930. #endif
  1931. if (!found) {
  1932. sx_release_drivers();
  1933. printk(KERN_INFO "sx: No specialix IO8+ boards detected.\n");
  1934. func_exit();
  1935. return -EIO;
  1936. }
  1937. func_exit();
  1938. return 0;
  1939. }
  1940. static int iobase[SX_NBOARD] = {0,};
  1941. static int irq[SX_NBOARD] = {0,};
  1942. module_param_array(iobase, int, NULL, 0);
  1943. module_param_array(irq, int, NULL, 0);
  1944. module_param(sx_debug, int, 0);
  1945. module_param(sx_rtscts, int, 0);
  1946. module_param(sx_rxfifo, int, 0);
  1947. /*
  1948. * You can setup up to 4 boards.
  1949. * by specifying "iobase=0xXXX,0xXXX ..." as insmod parameter.
  1950. * You should specify the IRQs too in that case "irq=....,...".
  1951. *
  1952. * More than 4 boards in one computer is not possible, as the card can
  1953. * only use 4 different interrupts.
  1954. *
  1955. */
  1956. static int __init specialix_init_module(void)
  1957. {
  1958. int i;
  1959. func_enter();
  1960. if (iobase[0] || iobase[1] || iobase[2] || iobase[3]) {
  1961. for (i = 0; i < SX_NBOARD; i++) {
  1962. sx_board[i].base = iobase[i];
  1963. sx_board[i].irq = irq[i];
  1964. sx_board[i].count = 0;
  1965. }
  1966. }
  1967. func_exit();
  1968. return specialix_init();
  1969. }
  1970. static void __exit specialix_exit_module(void)
  1971. {
  1972. int i;
  1973. func_enter();
  1974. sx_release_drivers();
  1975. for (i = 0; i < SX_NBOARD; i++)
  1976. if (sx_board[i].flags & SX_BOARD_PRESENT)
  1977. sx_release_io_range(&sx_board[i]);
  1978. func_exit();
  1979. }
  1980. static struct pci_device_id specialx_pci_tbl[] __devinitdata __used = {
  1981. { PCI_DEVICE(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_IO8) },
  1982. { }
  1983. };
  1984. MODULE_DEVICE_TABLE(pci, specialx_pci_tbl);
  1985. module_init(specialix_init_module);
  1986. module_exit(specialix_exit_module);
  1987. MODULE_LICENSE("GPL");
  1988. MODULE_ALIAS_CHARDEV_MAJOR(SPECIALIX_NORMAL_MAJOR);