mxser.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753
  1. /*
  2. * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
  3. *
  4. * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
  5. * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
  6. *
  7. * This code is loosely based on the 1.8 moxa driver which is based on
  8. * Linux serial driver, written by Linus Torvalds, Theodore T'so and
  9. * others.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
  17. * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
  18. * www.moxa.com.
  19. * - Fixed x86_64 cleanness
  20. */
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/signal.h>
  24. #include <linux/sched.h>
  25. #include <linux/timer.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/serial.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/major.h>
  32. #include <linux/string.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/ptrace.h>
  35. #include <linux/ioport.h>
  36. #include <linux/mm.h>
  37. #include <linux/delay.h>
  38. #include <linux/pci.h>
  39. #include <linux/bitops.h>
  40. #include <linux/slab.h>
  41. #include <asm/system.h>
  42. #include <asm/io.h>
  43. #include <asm/irq.h>
  44. #include <asm/uaccess.h>
  45. #include "mxser.h"
  46. #define MXSER_VERSION "2.0.5" /* 1.14 */
  47. #define MXSERMAJOR 174
  48. #define MXSER_BOARDS 4 /* Max. boards */
  49. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  50. #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
  51. #define MXSER_ISR_PASS_LIMIT 100
  52. /*CheckIsMoxaMust return value*/
  53. #define MOXA_OTHER_UART 0x00
  54. #define MOXA_MUST_MU150_HWID 0x01
  55. #define MOXA_MUST_MU860_HWID 0x02
  56. #define WAKEUP_CHARS 256
  57. #define UART_MCR_AFE 0x20
  58. #define UART_LSR_SPECIAL 0x1E
  59. #define PCI_DEVICE_ID_POS104UL 0x1044
  60. #define PCI_DEVICE_ID_CB108 0x1080
  61. #define PCI_DEVICE_ID_CP102UF 0x1023
  62. #define PCI_DEVICE_ID_CP112UL 0x1120
  63. #define PCI_DEVICE_ID_CB114 0x1142
  64. #define PCI_DEVICE_ID_CP114UL 0x1143
  65. #define PCI_DEVICE_ID_CB134I 0x1341
  66. #define PCI_DEVICE_ID_CP138U 0x1380
  67. #define C168_ASIC_ID 1
  68. #define C104_ASIC_ID 2
  69. #define C102_ASIC_ID 0xB
  70. #define CI132_ASIC_ID 4
  71. #define CI134_ASIC_ID 3
  72. #define CI104J_ASIC_ID 5
  73. #define MXSER_HIGHBAUD 1
  74. #define MXSER_HAS2 2
  75. /* This is only for PCI */
  76. static const struct {
  77. int type;
  78. int tx_fifo;
  79. int rx_fifo;
  80. int xmit_fifo_size;
  81. int rx_high_water;
  82. int rx_trigger;
  83. int rx_low_water;
  84. long max_baud;
  85. } Gpci_uart_info[] = {
  86. {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
  87. {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
  88. {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
  89. };
  90. #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
  91. struct mxser_cardinfo {
  92. char *name;
  93. unsigned int nports;
  94. unsigned int flags;
  95. };
  96. static const struct mxser_cardinfo mxser_cards[] = {
  97. /* 0*/ { "C168 series", 8, },
  98. { "C104 series", 4, },
  99. { "CI-104J series", 4, },
  100. { "C168H/PCI series", 8, },
  101. { "C104H/PCI series", 4, },
  102. /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
  103. { "CI-132 series", 4, MXSER_HAS2 },
  104. { "CI-134 series", 4, },
  105. { "CP-132 series", 2, },
  106. { "CP-114 series", 4, },
  107. /*10*/ { "CT-114 series", 4, },
  108. { "CP-102 series", 2, MXSER_HIGHBAUD },
  109. { "CP-104U series", 4, },
  110. { "CP-168U series", 8, },
  111. { "CP-132U series", 2, },
  112. /*15*/ { "CP-134U series", 4, },
  113. { "CP-104JU series", 4, },
  114. { "Moxa UC7000 Serial", 8, }, /* RC7000 */
  115. { "CP-118U series", 8, },
  116. { "CP-102UL series", 2, },
  117. /*20*/ { "CP-102U series", 2, },
  118. { "CP-118EL series", 8, },
  119. { "CP-168EL series", 8, },
  120. { "CP-104EL series", 4, },
  121. { "CB-108 series", 8, },
  122. /*25*/ { "CB-114 series", 4, },
  123. { "CB-134I series", 4, },
  124. { "CP-138U series", 8, },
  125. { "POS-104UL series", 4, },
  126. { "CP-114UL series", 4, },
  127. /*30*/ { "CP-102UF series", 2, },
  128. { "CP-112UL series", 2, },
  129. };
  130. /* driver_data correspond to the lines in the structure above
  131. see also ISA probe function before you change something */
  132. static struct pci_device_id mxser_pcibrds[] = {
  133. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
  134. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
  135. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
  136. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
  137. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
  138. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
  139. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
  140. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
  141. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
  142. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
  143. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
  144. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
  145. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
  146. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
  147. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
  148. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
  149. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
  150. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
  151. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
  152. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
  153. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
  154. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
  155. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
  156. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
  157. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
  158. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 31 },
  159. { }
  160. };
  161. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  162. static unsigned long ioaddr[MXSER_BOARDS];
  163. static int ttymajor = MXSERMAJOR;
  164. /* Variables for insmod */
  165. MODULE_AUTHOR("Casper Yang");
  166. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  167. module_param_array(ioaddr, ulong, NULL, 0);
  168. MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
  169. module_param(ttymajor, int, 0);
  170. MODULE_LICENSE("GPL");
  171. struct mxser_log {
  172. int tick;
  173. unsigned long rxcnt[MXSER_PORTS];
  174. unsigned long txcnt[MXSER_PORTS];
  175. };
  176. struct mxser_mon {
  177. unsigned long rxcnt;
  178. unsigned long txcnt;
  179. unsigned long up_rxcnt;
  180. unsigned long up_txcnt;
  181. int modem_status;
  182. unsigned char hold_reason;
  183. };
  184. struct mxser_mon_ext {
  185. unsigned long rx_cnt[32];
  186. unsigned long tx_cnt[32];
  187. unsigned long up_rxcnt[32];
  188. unsigned long up_txcnt[32];
  189. int modem_status[32];
  190. long baudrate[32];
  191. int databits[32];
  192. int stopbits[32];
  193. int parity[32];
  194. int flowctrl[32];
  195. int fifo[32];
  196. int iftype[32];
  197. };
  198. struct mxser_board;
  199. struct mxser_port {
  200. struct tty_port port;
  201. struct mxser_board *board;
  202. unsigned long ioaddr;
  203. unsigned long opmode_ioaddr;
  204. int max_baud;
  205. int rx_high_water;
  206. int rx_trigger; /* Rx fifo trigger level */
  207. int rx_low_water;
  208. int baud_base; /* max. speed */
  209. int type; /* UART type */
  210. int x_char; /* xon/xoff character */
  211. int IER; /* Interrupt Enable Register */
  212. int MCR; /* Modem control register */
  213. unsigned char stop_rx;
  214. unsigned char ldisc_stop_rx;
  215. int custom_divisor;
  216. unsigned char err_shadow;
  217. struct async_icount icount; /* kernel counters for 4 input interrupts */
  218. int timeout;
  219. int read_status_mask;
  220. int ignore_status_mask;
  221. int xmit_fifo_size;
  222. int xmit_head;
  223. int xmit_tail;
  224. int xmit_cnt;
  225. struct ktermios normal_termios;
  226. struct mxser_mon mon_data;
  227. spinlock_t slock;
  228. };
  229. struct mxser_board {
  230. unsigned int idx;
  231. int irq;
  232. const struct mxser_cardinfo *info;
  233. unsigned long vector;
  234. unsigned long vector_mask;
  235. int chip_flag;
  236. int uart_type;
  237. struct mxser_port ports[MXSER_PORTS_PER_BOARD];
  238. };
  239. struct mxser_mstatus {
  240. tcflag_t cflag;
  241. int cts;
  242. int dsr;
  243. int ri;
  244. int dcd;
  245. };
  246. static struct mxser_board mxser_boards[MXSER_BOARDS];
  247. static struct tty_driver *mxvar_sdriver;
  248. static struct mxser_log mxvar_log;
  249. static int mxser_set_baud_method[MXSER_PORTS + 1];
  250. static void mxser_enable_must_enchance_mode(unsigned long baseio)
  251. {
  252. u8 oldlcr;
  253. u8 efr;
  254. oldlcr = inb(baseio + UART_LCR);
  255. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  256. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  257. efr |= MOXA_MUST_EFR_EFRB_ENABLE;
  258. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  259. outb(oldlcr, baseio + UART_LCR);
  260. }
  261. static void mxser_disable_must_enchance_mode(unsigned long baseio)
  262. {
  263. u8 oldlcr;
  264. u8 efr;
  265. oldlcr = inb(baseio + UART_LCR);
  266. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  267. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  268. efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
  269. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  270. outb(oldlcr, baseio + UART_LCR);
  271. }
  272. static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
  273. {
  274. u8 oldlcr;
  275. u8 efr;
  276. oldlcr = inb(baseio + UART_LCR);
  277. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  278. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  279. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  280. efr |= MOXA_MUST_EFR_BANK0;
  281. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  282. outb(value, baseio + MOXA_MUST_XON1_REGISTER);
  283. outb(oldlcr, baseio + UART_LCR);
  284. }
  285. static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
  286. {
  287. u8 oldlcr;
  288. u8 efr;
  289. oldlcr = inb(baseio + UART_LCR);
  290. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  291. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  292. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  293. efr |= MOXA_MUST_EFR_BANK0;
  294. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  295. outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
  296. outb(oldlcr, baseio + UART_LCR);
  297. }
  298. static void mxser_set_must_fifo_value(struct mxser_port *info)
  299. {
  300. u8 oldlcr;
  301. u8 efr;
  302. oldlcr = inb(info->ioaddr + UART_LCR);
  303. outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
  304. efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
  305. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  306. efr |= MOXA_MUST_EFR_BANK1;
  307. outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
  308. outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
  309. outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
  310. outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
  311. outb(oldlcr, info->ioaddr + UART_LCR);
  312. }
  313. static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
  314. {
  315. u8 oldlcr;
  316. u8 efr;
  317. oldlcr = inb(baseio + UART_LCR);
  318. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  319. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  320. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  321. efr |= MOXA_MUST_EFR_BANK2;
  322. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  323. outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
  324. outb(oldlcr, baseio + UART_LCR);
  325. }
  326. static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
  327. {
  328. u8 oldlcr;
  329. u8 efr;
  330. oldlcr = inb(baseio + UART_LCR);
  331. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  332. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  333. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  334. efr |= MOXA_MUST_EFR_BANK2;
  335. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  336. *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
  337. outb(oldlcr, baseio + UART_LCR);
  338. }
  339. static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
  340. {
  341. u8 oldlcr;
  342. u8 efr;
  343. oldlcr = inb(baseio + UART_LCR);
  344. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  345. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  346. efr &= ~MOXA_MUST_EFR_SF_MASK;
  347. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  348. outb(oldlcr, baseio + UART_LCR);
  349. }
  350. static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
  351. {
  352. u8 oldlcr;
  353. u8 efr;
  354. oldlcr = inb(baseio + UART_LCR);
  355. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  356. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  357. efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
  358. efr |= MOXA_MUST_EFR_SF_TX1;
  359. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  360. outb(oldlcr, baseio + UART_LCR);
  361. }
  362. static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
  363. {
  364. u8 oldlcr;
  365. u8 efr;
  366. oldlcr = inb(baseio + UART_LCR);
  367. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  368. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  369. efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
  370. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  371. outb(oldlcr, baseio + UART_LCR);
  372. }
  373. static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
  374. {
  375. u8 oldlcr;
  376. u8 efr;
  377. oldlcr = inb(baseio + UART_LCR);
  378. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  379. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  380. efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
  381. efr |= MOXA_MUST_EFR_SF_RX1;
  382. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  383. outb(oldlcr, baseio + UART_LCR);
  384. }
  385. static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
  386. {
  387. u8 oldlcr;
  388. u8 efr;
  389. oldlcr = inb(baseio + UART_LCR);
  390. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  391. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  392. efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
  393. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  394. outb(oldlcr, baseio + UART_LCR);
  395. }
  396. #ifdef CONFIG_PCI
  397. static int __devinit CheckIsMoxaMust(unsigned long io)
  398. {
  399. u8 oldmcr, hwid;
  400. int i;
  401. outb(0, io + UART_LCR);
  402. mxser_disable_must_enchance_mode(io);
  403. oldmcr = inb(io + UART_MCR);
  404. outb(0, io + UART_MCR);
  405. mxser_set_must_xon1_value(io, 0x11);
  406. if ((hwid = inb(io + UART_MCR)) != 0) {
  407. outb(oldmcr, io + UART_MCR);
  408. return MOXA_OTHER_UART;
  409. }
  410. mxser_get_must_hardware_id(io, &hwid);
  411. for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
  412. if (hwid == Gpci_uart_info[i].type)
  413. return (int)hwid;
  414. }
  415. return MOXA_OTHER_UART;
  416. }
  417. #endif
  418. static void process_txrx_fifo(struct mxser_port *info)
  419. {
  420. int i;
  421. if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
  422. info->rx_trigger = 1;
  423. info->rx_high_water = 1;
  424. info->rx_low_water = 1;
  425. info->xmit_fifo_size = 1;
  426. } else
  427. for (i = 0; i < UART_INFO_NUM; i++)
  428. if (info->board->chip_flag == Gpci_uart_info[i].type) {
  429. info->rx_trigger = Gpci_uart_info[i].rx_trigger;
  430. info->rx_low_water = Gpci_uart_info[i].rx_low_water;
  431. info->rx_high_water = Gpci_uart_info[i].rx_high_water;
  432. info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
  433. break;
  434. }
  435. }
  436. static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
  437. {
  438. static unsigned char mxser_msr[MXSER_PORTS + 1];
  439. unsigned char status = 0;
  440. status = inb(baseaddr + UART_MSR);
  441. mxser_msr[port] &= 0x0F;
  442. mxser_msr[port] |= status;
  443. status = mxser_msr[port];
  444. if (mode)
  445. mxser_msr[port] = 0;
  446. return status;
  447. }
  448. static int mxser_carrier_raised(struct tty_port *port)
  449. {
  450. struct mxser_port *mp = container_of(port, struct mxser_port, port);
  451. return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
  452. }
  453. static void mxser_dtr_rts(struct tty_port *port, int on)
  454. {
  455. struct mxser_port *mp = container_of(port, struct mxser_port, port);
  456. unsigned long flags;
  457. spin_lock_irqsave(&mp->slock, flags);
  458. if (on)
  459. outb(inb(mp->ioaddr + UART_MCR) |
  460. UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
  461. else
  462. outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
  463. mp->ioaddr + UART_MCR);
  464. spin_unlock_irqrestore(&mp->slock, flags);
  465. }
  466. static int mxser_set_baud(struct tty_struct *tty, long newspd)
  467. {
  468. struct mxser_port *info = tty->driver_data;
  469. int quot = 0, baud;
  470. unsigned char cval;
  471. if (!info->ioaddr)
  472. return -1;
  473. if (newspd > info->max_baud)
  474. return -1;
  475. if (newspd == 134) {
  476. quot = 2 * info->baud_base / 269;
  477. tty_encode_baud_rate(tty, 134, 134);
  478. } else if (newspd) {
  479. quot = info->baud_base / newspd;
  480. if (quot == 0)
  481. quot = 1;
  482. baud = info->baud_base/quot;
  483. tty_encode_baud_rate(tty, baud, baud);
  484. } else {
  485. quot = 0;
  486. }
  487. info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
  488. info->timeout += HZ / 50; /* Add .02 seconds of slop */
  489. if (quot) {
  490. info->MCR |= UART_MCR_DTR;
  491. outb(info->MCR, info->ioaddr + UART_MCR);
  492. } else {
  493. info->MCR &= ~UART_MCR_DTR;
  494. outb(info->MCR, info->ioaddr + UART_MCR);
  495. return 0;
  496. }
  497. cval = inb(info->ioaddr + UART_LCR);
  498. outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
  499. outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
  500. outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
  501. outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
  502. #ifdef BOTHER
  503. if (C_BAUD(tty) == BOTHER) {
  504. quot = info->baud_base % newspd;
  505. quot *= 8;
  506. if (quot % newspd > newspd / 2) {
  507. quot /= newspd;
  508. quot++;
  509. } else
  510. quot /= newspd;
  511. mxser_set_must_enum_value(info->ioaddr, quot);
  512. } else
  513. #endif
  514. mxser_set_must_enum_value(info->ioaddr, 0);
  515. return 0;
  516. }
  517. /*
  518. * This routine is called to set the UART divisor registers to match
  519. * the specified baud rate for a serial port.
  520. */
  521. static int mxser_change_speed(struct tty_struct *tty,
  522. struct ktermios *old_termios)
  523. {
  524. struct mxser_port *info = tty->driver_data;
  525. unsigned cflag, cval, fcr;
  526. int ret = 0;
  527. unsigned char status;
  528. cflag = tty->termios->c_cflag;
  529. if (!info->ioaddr)
  530. return ret;
  531. if (mxser_set_baud_method[tty->index] == 0)
  532. mxser_set_baud(tty, tty_get_baud_rate(tty));
  533. /* byte size and parity */
  534. switch (cflag & CSIZE) {
  535. case CS5:
  536. cval = 0x00;
  537. break;
  538. case CS6:
  539. cval = 0x01;
  540. break;
  541. case CS7:
  542. cval = 0x02;
  543. break;
  544. case CS8:
  545. cval = 0x03;
  546. break;
  547. default:
  548. cval = 0x00;
  549. break; /* too keep GCC shut... */
  550. }
  551. if (cflag & CSTOPB)
  552. cval |= 0x04;
  553. if (cflag & PARENB)
  554. cval |= UART_LCR_PARITY;
  555. if (!(cflag & PARODD))
  556. cval |= UART_LCR_EPAR;
  557. if (cflag & CMSPAR)
  558. cval |= UART_LCR_SPAR;
  559. if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
  560. if (info->board->chip_flag) {
  561. fcr = UART_FCR_ENABLE_FIFO;
  562. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  563. mxser_set_must_fifo_value(info);
  564. } else
  565. fcr = 0;
  566. } else {
  567. fcr = UART_FCR_ENABLE_FIFO;
  568. if (info->board->chip_flag) {
  569. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  570. mxser_set_must_fifo_value(info);
  571. } else {
  572. switch (info->rx_trigger) {
  573. case 1:
  574. fcr |= UART_FCR_TRIGGER_1;
  575. break;
  576. case 4:
  577. fcr |= UART_FCR_TRIGGER_4;
  578. break;
  579. case 8:
  580. fcr |= UART_FCR_TRIGGER_8;
  581. break;
  582. default:
  583. fcr |= UART_FCR_TRIGGER_14;
  584. break;
  585. }
  586. }
  587. }
  588. /* CTS flow control flag and modem status interrupts */
  589. info->IER &= ~UART_IER_MSI;
  590. info->MCR &= ~UART_MCR_AFE;
  591. if (cflag & CRTSCTS) {
  592. info->port.flags |= ASYNC_CTS_FLOW;
  593. info->IER |= UART_IER_MSI;
  594. if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
  595. info->MCR |= UART_MCR_AFE;
  596. } else {
  597. status = inb(info->ioaddr + UART_MSR);
  598. if (tty->hw_stopped) {
  599. if (status & UART_MSR_CTS) {
  600. tty->hw_stopped = 0;
  601. if (info->type != PORT_16550A &&
  602. !info->board->chip_flag) {
  603. outb(info->IER & ~UART_IER_THRI,
  604. info->ioaddr +
  605. UART_IER);
  606. info->IER |= UART_IER_THRI;
  607. outb(info->IER, info->ioaddr +
  608. UART_IER);
  609. }
  610. tty_wakeup(tty);
  611. }
  612. } else {
  613. if (!(status & UART_MSR_CTS)) {
  614. tty->hw_stopped = 1;
  615. if ((info->type != PORT_16550A) &&
  616. (!info->board->chip_flag)) {
  617. info->IER &= ~UART_IER_THRI;
  618. outb(info->IER, info->ioaddr +
  619. UART_IER);
  620. }
  621. }
  622. }
  623. }
  624. } else {
  625. info->port.flags &= ~ASYNC_CTS_FLOW;
  626. }
  627. outb(info->MCR, info->ioaddr + UART_MCR);
  628. if (cflag & CLOCAL) {
  629. info->port.flags &= ~ASYNC_CHECK_CD;
  630. } else {
  631. info->port.flags |= ASYNC_CHECK_CD;
  632. info->IER |= UART_IER_MSI;
  633. }
  634. outb(info->IER, info->ioaddr + UART_IER);
  635. /*
  636. * Set up parity check flag
  637. */
  638. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  639. if (I_INPCK(tty))
  640. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  641. if (I_BRKINT(tty) || I_PARMRK(tty))
  642. info->read_status_mask |= UART_LSR_BI;
  643. info->ignore_status_mask = 0;
  644. if (I_IGNBRK(tty)) {
  645. info->ignore_status_mask |= UART_LSR_BI;
  646. info->read_status_mask |= UART_LSR_BI;
  647. /*
  648. * If we're ignore parity and break indicators, ignore
  649. * overruns too. (For real raw support).
  650. */
  651. if (I_IGNPAR(tty)) {
  652. info->ignore_status_mask |=
  653. UART_LSR_OE |
  654. UART_LSR_PE |
  655. UART_LSR_FE;
  656. info->read_status_mask |=
  657. UART_LSR_OE |
  658. UART_LSR_PE |
  659. UART_LSR_FE;
  660. }
  661. }
  662. if (info->board->chip_flag) {
  663. mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
  664. mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
  665. if (I_IXON(tty)) {
  666. mxser_enable_must_rx_software_flow_control(
  667. info->ioaddr);
  668. } else {
  669. mxser_disable_must_rx_software_flow_control(
  670. info->ioaddr);
  671. }
  672. if (I_IXOFF(tty)) {
  673. mxser_enable_must_tx_software_flow_control(
  674. info->ioaddr);
  675. } else {
  676. mxser_disable_must_tx_software_flow_control(
  677. info->ioaddr);
  678. }
  679. }
  680. outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
  681. outb(cval, info->ioaddr + UART_LCR);
  682. return ret;
  683. }
  684. static void mxser_check_modem_status(struct tty_struct *tty,
  685. struct mxser_port *port, int status)
  686. {
  687. /* update input line counters */
  688. if (status & UART_MSR_TERI)
  689. port->icount.rng++;
  690. if (status & UART_MSR_DDSR)
  691. port->icount.dsr++;
  692. if (status & UART_MSR_DDCD)
  693. port->icount.dcd++;
  694. if (status & UART_MSR_DCTS)
  695. port->icount.cts++;
  696. port->mon_data.modem_status = status;
  697. wake_up_interruptible(&port->port.delta_msr_wait);
  698. if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
  699. if (status & UART_MSR_DCD)
  700. wake_up_interruptible(&port->port.open_wait);
  701. }
  702. if (port->port.flags & ASYNC_CTS_FLOW) {
  703. if (tty->hw_stopped) {
  704. if (status & UART_MSR_CTS) {
  705. tty->hw_stopped = 0;
  706. if ((port->type != PORT_16550A) &&
  707. (!port->board->chip_flag)) {
  708. outb(port->IER & ~UART_IER_THRI,
  709. port->ioaddr + UART_IER);
  710. port->IER |= UART_IER_THRI;
  711. outb(port->IER, port->ioaddr +
  712. UART_IER);
  713. }
  714. tty_wakeup(tty);
  715. }
  716. } else {
  717. if (!(status & UART_MSR_CTS)) {
  718. tty->hw_stopped = 1;
  719. if (port->type != PORT_16550A &&
  720. !port->board->chip_flag) {
  721. port->IER &= ~UART_IER_THRI;
  722. outb(port->IER, port->ioaddr +
  723. UART_IER);
  724. }
  725. }
  726. }
  727. }
  728. }
  729. static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
  730. {
  731. struct mxser_port *info = container_of(port, struct mxser_port, port);
  732. unsigned long page;
  733. unsigned long flags;
  734. page = __get_free_page(GFP_KERNEL);
  735. if (!page)
  736. return -ENOMEM;
  737. spin_lock_irqsave(&info->slock, flags);
  738. if (!info->ioaddr || !info->type) {
  739. set_bit(TTY_IO_ERROR, &tty->flags);
  740. free_page(page);
  741. spin_unlock_irqrestore(&info->slock, flags);
  742. return 0;
  743. }
  744. info->port.xmit_buf = (unsigned char *) page;
  745. /*
  746. * Clear the FIFO buffers and disable them
  747. * (they will be reenabled in mxser_change_speed())
  748. */
  749. if (info->board->chip_flag)
  750. outb((UART_FCR_CLEAR_RCVR |
  751. UART_FCR_CLEAR_XMIT |
  752. MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
  753. else
  754. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  755. info->ioaddr + UART_FCR);
  756. /*
  757. * At this point there's no way the LSR could still be 0xFF;
  758. * if it is, then bail out, because there's likely no UART
  759. * here.
  760. */
  761. if (inb(info->ioaddr + UART_LSR) == 0xff) {
  762. spin_unlock_irqrestore(&info->slock, flags);
  763. if (capable(CAP_SYS_ADMIN)) {
  764. set_bit(TTY_IO_ERROR, &tty->flags);
  765. return 0;
  766. } else
  767. return -ENODEV;
  768. }
  769. /*
  770. * Clear the interrupt registers.
  771. */
  772. (void) inb(info->ioaddr + UART_LSR);
  773. (void) inb(info->ioaddr + UART_RX);
  774. (void) inb(info->ioaddr + UART_IIR);
  775. (void) inb(info->ioaddr + UART_MSR);
  776. /*
  777. * Now, initialize the UART
  778. */
  779. outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
  780. info->MCR = UART_MCR_DTR | UART_MCR_RTS;
  781. outb(info->MCR, info->ioaddr + UART_MCR);
  782. /*
  783. * Finally, enable interrupts
  784. */
  785. info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  786. if (info->board->chip_flag)
  787. info->IER |= MOXA_MUST_IER_EGDAI;
  788. outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
  789. /*
  790. * And clear the interrupt registers again for luck.
  791. */
  792. (void) inb(info->ioaddr + UART_LSR);
  793. (void) inb(info->ioaddr + UART_RX);
  794. (void) inb(info->ioaddr + UART_IIR);
  795. (void) inb(info->ioaddr + UART_MSR);
  796. clear_bit(TTY_IO_ERROR, &tty->flags);
  797. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  798. /*
  799. * and set the speed of the serial port
  800. */
  801. mxser_change_speed(tty, NULL);
  802. spin_unlock_irqrestore(&info->slock, flags);
  803. return 0;
  804. }
  805. /*
  806. * This routine will shutdown a serial port
  807. */
  808. static void mxser_shutdown_port(struct tty_port *port)
  809. {
  810. struct mxser_port *info = container_of(port, struct mxser_port, port);
  811. unsigned long flags;
  812. spin_lock_irqsave(&info->slock, flags);
  813. /*
  814. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  815. * here so the queue might never be waken up
  816. */
  817. wake_up_interruptible(&info->port.delta_msr_wait);
  818. /*
  819. * Free the xmit buffer, if necessary
  820. */
  821. if (info->port.xmit_buf) {
  822. free_page((unsigned long) info->port.xmit_buf);
  823. info->port.xmit_buf = NULL;
  824. }
  825. info->IER = 0;
  826. outb(0x00, info->ioaddr + UART_IER);
  827. /* clear Rx/Tx FIFO's */
  828. if (info->board->chip_flag)
  829. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
  830. MOXA_MUST_FCR_GDA_MODE_ENABLE,
  831. info->ioaddr + UART_FCR);
  832. else
  833. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  834. info->ioaddr + UART_FCR);
  835. /* read data port to reset things */
  836. (void) inb(info->ioaddr + UART_RX);
  837. if (info->board->chip_flag)
  838. SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  839. spin_unlock_irqrestore(&info->slock, flags);
  840. }
  841. /*
  842. * This routine is called whenever a serial port is opened. It
  843. * enables interrupts for a serial port, linking in its async structure into
  844. * the IRQ chain. It also performs the serial-specific
  845. * initialization for the tty structure.
  846. */
  847. static int mxser_open(struct tty_struct *tty, struct file *filp)
  848. {
  849. struct mxser_port *info;
  850. int line;
  851. line = tty->index;
  852. if (line == MXSER_PORTS)
  853. return 0;
  854. if (line < 0 || line > MXSER_PORTS)
  855. return -ENODEV;
  856. info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
  857. if (!info->ioaddr)
  858. return -ENODEV;
  859. tty->driver_data = info;
  860. return tty_port_open(&info->port, tty, filp);
  861. }
  862. static void mxser_flush_buffer(struct tty_struct *tty)
  863. {
  864. struct mxser_port *info = tty->driver_data;
  865. char fcr;
  866. unsigned long flags;
  867. spin_lock_irqsave(&info->slock, flags);
  868. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  869. fcr = inb(info->ioaddr + UART_FCR);
  870. outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  871. info->ioaddr + UART_FCR);
  872. outb(fcr, info->ioaddr + UART_FCR);
  873. spin_unlock_irqrestore(&info->slock, flags);
  874. tty_wakeup(tty);
  875. }
  876. static void mxser_close_port(struct tty_port *port)
  877. {
  878. struct mxser_port *info = container_of(port, struct mxser_port, port);
  879. unsigned long timeout;
  880. /*
  881. * At this point we stop accepting input. To do this, we
  882. * disable the receive line status interrupts, and tell the
  883. * interrupt driver to stop checking the data ready bit in the
  884. * line status register.
  885. */
  886. info->IER &= ~UART_IER_RLSI;
  887. if (info->board->chip_flag)
  888. info->IER &= ~MOXA_MUST_RECV_ISR;
  889. outb(info->IER, info->ioaddr + UART_IER);
  890. /*
  891. * Before we drop DTR, make sure the UART transmitter
  892. * has completely drained; this is especially
  893. * important if there is a transmit FIFO!
  894. */
  895. timeout = jiffies + HZ;
  896. while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
  897. schedule_timeout_interruptible(5);
  898. if (time_after(jiffies, timeout))
  899. break;
  900. }
  901. }
  902. /*
  903. * This routine is called when the serial port gets closed. First, we
  904. * wait for the last remaining data to be sent. Then, we unlink its
  905. * async structure from the interrupt chain if necessary, and we free
  906. * that IRQ if nothing is left in the chain.
  907. */
  908. static void mxser_close(struct tty_struct *tty, struct file *filp)
  909. {
  910. struct mxser_port *info = tty->driver_data;
  911. struct tty_port *port = &info->port;
  912. if (tty->index == MXSER_PORTS || info == NULL)
  913. return;
  914. if (tty_port_close_start(port, tty, filp) == 0)
  915. return;
  916. mutex_lock(&port->mutex);
  917. mxser_close_port(port);
  918. mxser_flush_buffer(tty);
  919. mxser_shutdown_port(port);
  920. clear_bit(ASYNCB_INITIALIZED, &port->flags);
  921. mutex_unlock(&port->mutex);
  922. /* Right now the tty_port set is done outside of the close_end helper
  923. as we don't yet have everyone using refcounts */
  924. tty_port_close_end(port, tty);
  925. tty_port_tty_set(port, NULL);
  926. }
  927. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  928. {
  929. int c, total = 0;
  930. struct mxser_port *info = tty->driver_data;
  931. unsigned long flags;
  932. if (!info->port.xmit_buf)
  933. return 0;
  934. while (1) {
  935. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  936. SERIAL_XMIT_SIZE - info->xmit_head));
  937. if (c <= 0)
  938. break;
  939. memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
  940. spin_lock_irqsave(&info->slock, flags);
  941. info->xmit_head = (info->xmit_head + c) &
  942. (SERIAL_XMIT_SIZE - 1);
  943. info->xmit_cnt += c;
  944. spin_unlock_irqrestore(&info->slock, flags);
  945. buf += c;
  946. count -= c;
  947. total += c;
  948. }
  949. if (info->xmit_cnt && !tty->stopped) {
  950. if (!tty->hw_stopped ||
  951. (info->type == PORT_16550A) ||
  952. (info->board->chip_flag)) {
  953. spin_lock_irqsave(&info->slock, flags);
  954. outb(info->IER & ~UART_IER_THRI, info->ioaddr +
  955. UART_IER);
  956. info->IER |= UART_IER_THRI;
  957. outb(info->IER, info->ioaddr + UART_IER);
  958. spin_unlock_irqrestore(&info->slock, flags);
  959. }
  960. }
  961. return total;
  962. }
  963. static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
  964. {
  965. struct mxser_port *info = tty->driver_data;
  966. unsigned long flags;
  967. if (!info->port.xmit_buf)
  968. return 0;
  969. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  970. return 0;
  971. spin_lock_irqsave(&info->slock, flags);
  972. info->port.xmit_buf[info->xmit_head++] = ch;
  973. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  974. info->xmit_cnt++;
  975. spin_unlock_irqrestore(&info->slock, flags);
  976. if (!tty->stopped) {
  977. if (!tty->hw_stopped ||
  978. (info->type == PORT_16550A) ||
  979. info->board->chip_flag) {
  980. spin_lock_irqsave(&info->slock, flags);
  981. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  982. info->IER |= UART_IER_THRI;
  983. outb(info->IER, info->ioaddr + UART_IER);
  984. spin_unlock_irqrestore(&info->slock, flags);
  985. }
  986. }
  987. return 1;
  988. }
  989. static void mxser_flush_chars(struct tty_struct *tty)
  990. {
  991. struct mxser_port *info = tty->driver_data;
  992. unsigned long flags;
  993. if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
  994. (tty->hw_stopped && info->type != PORT_16550A &&
  995. !info->board->chip_flag))
  996. return;
  997. spin_lock_irqsave(&info->slock, flags);
  998. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  999. info->IER |= UART_IER_THRI;
  1000. outb(info->IER, info->ioaddr + UART_IER);
  1001. spin_unlock_irqrestore(&info->slock, flags);
  1002. }
  1003. static int mxser_write_room(struct tty_struct *tty)
  1004. {
  1005. struct mxser_port *info = tty->driver_data;
  1006. int ret;
  1007. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1008. return ret < 0 ? 0 : ret;
  1009. }
  1010. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1011. {
  1012. struct mxser_port *info = tty->driver_data;
  1013. return info->xmit_cnt;
  1014. }
  1015. /*
  1016. * ------------------------------------------------------------
  1017. * friends of mxser_ioctl()
  1018. * ------------------------------------------------------------
  1019. */
  1020. static int mxser_get_serial_info(struct tty_struct *tty,
  1021. struct serial_struct __user *retinfo)
  1022. {
  1023. struct mxser_port *info = tty->driver_data;
  1024. struct serial_struct tmp = {
  1025. .type = info->type,
  1026. .line = tty->index,
  1027. .port = info->ioaddr,
  1028. .irq = info->board->irq,
  1029. .flags = info->port.flags,
  1030. .baud_base = info->baud_base,
  1031. .close_delay = info->port.close_delay,
  1032. .closing_wait = info->port.closing_wait,
  1033. .custom_divisor = info->custom_divisor,
  1034. .hub6 = 0
  1035. };
  1036. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1037. return -EFAULT;
  1038. return 0;
  1039. }
  1040. static int mxser_set_serial_info(struct tty_struct *tty,
  1041. struct serial_struct __user *new_info)
  1042. {
  1043. struct mxser_port *info = tty->driver_data;
  1044. struct tty_port *port = &info->port;
  1045. struct serial_struct new_serial;
  1046. speed_t baud;
  1047. unsigned long sl_flags;
  1048. unsigned int flags;
  1049. int retval = 0;
  1050. if (!new_info || !info->ioaddr)
  1051. return -ENODEV;
  1052. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1053. return -EFAULT;
  1054. if (new_serial.irq != info->board->irq ||
  1055. new_serial.port != info->ioaddr)
  1056. return -EINVAL;
  1057. flags = port->flags & ASYNC_SPD_MASK;
  1058. if (!capable(CAP_SYS_ADMIN)) {
  1059. if ((new_serial.baud_base != info->baud_base) ||
  1060. (new_serial.close_delay != info->port.close_delay) ||
  1061. ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
  1062. return -EPERM;
  1063. info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
  1064. (new_serial.flags & ASYNC_USR_MASK));
  1065. } else {
  1066. /*
  1067. * OK, past this point, all the error checking has been done.
  1068. * At this point, we start making changes.....
  1069. */
  1070. port->flags = ((port->flags & ~ASYNC_FLAGS) |
  1071. (new_serial.flags & ASYNC_FLAGS));
  1072. port->close_delay = new_serial.close_delay * HZ / 100;
  1073. port->closing_wait = new_serial.closing_wait * HZ / 100;
  1074. tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1075. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
  1076. (new_serial.baud_base != info->baud_base ||
  1077. new_serial.custom_divisor !=
  1078. info->custom_divisor)) {
  1079. if (new_serial.custom_divisor == 0)
  1080. return -EINVAL;
  1081. baud = new_serial.baud_base / new_serial.custom_divisor;
  1082. tty_encode_baud_rate(tty, baud, baud);
  1083. }
  1084. }
  1085. info->type = new_serial.type;
  1086. process_txrx_fifo(info);
  1087. if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  1088. if (flags != (port->flags & ASYNC_SPD_MASK)) {
  1089. spin_lock_irqsave(&info->slock, sl_flags);
  1090. mxser_change_speed(tty, NULL);
  1091. spin_unlock_irqrestore(&info->slock, sl_flags);
  1092. }
  1093. } else {
  1094. retval = mxser_activate(port, tty);
  1095. if (retval == 0)
  1096. set_bit(ASYNCB_INITIALIZED, &port->flags);
  1097. }
  1098. return retval;
  1099. }
  1100. /*
  1101. * mxser_get_lsr_info - get line status register info
  1102. *
  1103. * Purpose: Let user call ioctl() to get info when the UART physically
  1104. * is emptied. On bus types like RS485, the transmitter must
  1105. * release the bus after transmitting. This must be done when
  1106. * the transmit shift register is empty, not be done when the
  1107. * transmit holding register is empty. This functionality
  1108. * allows an RS485 driver to be written in user space.
  1109. */
  1110. static int mxser_get_lsr_info(struct mxser_port *info,
  1111. unsigned int __user *value)
  1112. {
  1113. unsigned char status;
  1114. unsigned int result;
  1115. unsigned long flags;
  1116. spin_lock_irqsave(&info->slock, flags);
  1117. status = inb(info->ioaddr + UART_LSR);
  1118. spin_unlock_irqrestore(&info->slock, flags);
  1119. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1120. return put_user(result, value);
  1121. }
  1122. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  1123. {
  1124. struct mxser_port *info = tty->driver_data;
  1125. unsigned char control, status;
  1126. unsigned long flags;
  1127. if (tty->index == MXSER_PORTS)
  1128. return -ENOIOCTLCMD;
  1129. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1130. return -EIO;
  1131. control = info->MCR;
  1132. spin_lock_irqsave(&info->slock, flags);
  1133. status = inb(info->ioaddr + UART_MSR);
  1134. if (status & UART_MSR_ANY_DELTA)
  1135. mxser_check_modem_status(tty, info, status);
  1136. spin_unlock_irqrestore(&info->slock, flags);
  1137. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  1138. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  1139. ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  1140. ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
  1141. ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  1142. ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1143. }
  1144. static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
  1145. unsigned int set, unsigned int clear)
  1146. {
  1147. struct mxser_port *info = tty->driver_data;
  1148. unsigned long flags;
  1149. if (tty->index == MXSER_PORTS)
  1150. return -ENOIOCTLCMD;
  1151. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1152. return -EIO;
  1153. spin_lock_irqsave(&info->slock, flags);
  1154. if (set & TIOCM_RTS)
  1155. info->MCR |= UART_MCR_RTS;
  1156. if (set & TIOCM_DTR)
  1157. info->MCR |= UART_MCR_DTR;
  1158. if (clear & TIOCM_RTS)
  1159. info->MCR &= ~UART_MCR_RTS;
  1160. if (clear & TIOCM_DTR)
  1161. info->MCR &= ~UART_MCR_DTR;
  1162. outb(info->MCR, info->ioaddr + UART_MCR);
  1163. spin_unlock_irqrestore(&info->slock, flags);
  1164. return 0;
  1165. }
  1166. static int __init mxser_program_mode(int port)
  1167. {
  1168. int id, i, j, n;
  1169. outb(0, port);
  1170. outb(0, port);
  1171. outb(0, port);
  1172. (void)inb(port);
  1173. (void)inb(port);
  1174. outb(0, port);
  1175. (void)inb(port);
  1176. id = inb(port + 1) & 0x1F;
  1177. if ((id != C168_ASIC_ID) &&
  1178. (id != C104_ASIC_ID) &&
  1179. (id != C102_ASIC_ID) &&
  1180. (id != CI132_ASIC_ID) &&
  1181. (id != CI134_ASIC_ID) &&
  1182. (id != CI104J_ASIC_ID))
  1183. return -1;
  1184. for (i = 0, j = 0; i < 4; i++) {
  1185. n = inb(port + 2);
  1186. if (n == 'M') {
  1187. j = 1;
  1188. } else if ((j == 1) && (n == 1)) {
  1189. j = 2;
  1190. break;
  1191. } else
  1192. j = 0;
  1193. }
  1194. if (j != 2)
  1195. id = -2;
  1196. return id;
  1197. }
  1198. static void __init mxser_normal_mode(int port)
  1199. {
  1200. int i, n;
  1201. outb(0xA5, port + 1);
  1202. outb(0x80, port + 3);
  1203. outb(12, port + 0); /* 9600 bps */
  1204. outb(0, port + 1);
  1205. outb(0x03, port + 3); /* 8 data bits */
  1206. outb(0x13, port + 4); /* loop back mode */
  1207. for (i = 0; i < 16; i++) {
  1208. n = inb(port + 5);
  1209. if ((n & 0x61) == 0x60)
  1210. break;
  1211. if ((n & 1) == 1)
  1212. (void)inb(port);
  1213. }
  1214. outb(0x00, port + 4);
  1215. }
  1216. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  1217. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  1218. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  1219. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  1220. #define EN_CCMD 0x000 /* Chip's command register */
  1221. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  1222. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  1223. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  1224. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  1225. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  1226. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  1227. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  1228. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  1229. static int __init mxser_read_register(int port, unsigned short *regs)
  1230. {
  1231. int i, k, value, id;
  1232. unsigned int j;
  1233. id = mxser_program_mode(port);
  1234. if (id < 0)
  1235. return id;
  1236. for (i = 0; i < 14; i++) {
  1237. k = (i & 0x3F) | 0x180;
  1238. for (j = 0x100; j > 0; j >>= 1) {
  1239. outb(CHIP_CS, port);
  1240. if (k & j) {
  1241. outb(CHIP_CS | CHIP_DO, port);
  1242. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  1243. } else {
  1244. outb(CHIP_CS, port);
  1245. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  1246. }
  1247. }
  1248. (void)inb(port);
  1249. value = 0;
  1250. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  1251. outb(CHIP_CS, port);
  1252. outb(CHIP_CS | CHIP_SK, port);
  1253. if (inb(port) & CHIP_DI)
  1254. value |= j;
  1255. }
  1256. regs[i] = value;
  1257. outb(0, port);
  1258. }
  1259. mxser_normal_mode(port);
  1260. return id;
  1261. }
  1262. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1263. {
  1264. struct mxser_port *ip;
  1265. struct tty_port *port;
  1266. struct tty_struct *tty;
  1267. int result, status;
  1268. unsigned int i, j;
  1269. int ret = 0;
  1270. switch (cmd) {
  1271. case MOXA_GET_MAJOR:
  1272. if (printk_ratelimit())
  1273. printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
  1274. "%x (GET_MAJOR), fix your userspace\n",
  1275. current->comm, cmd);
  1276. return put_user(ttymajor, (int __user *)argp);
  1277. case MOXA_CHKPORTENABLE:
  1278. result = 0;
  1279. for (i = 0; i < MXSER_BOARDS; i++)
  1280. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
  1281. if (mxser_boards[i].ports[j].ioaddr)
  1282. result |= (1 << i);
  1283. return put_user(result, (unsigned long __user *)argp);
  1284. case MOXA_GETDATACOUNT:
  1285. /* The receive side is locked by port->slock but it isn't
  1286. clear that an exact snapshot is worth copying here */
  1287. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1288. ret = -EFAULT;
  1289. return ret;
  1290. case MOXA_GETMSTATUS: {
  1291. struct mxser_mstatus ms, __user *msu = argp;
  1292. for (i = 0; i < MXSER_BOARDS; i++)
  1293. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1294. ip = &mxser_boards[i].ports[j];
  1295. port = &ip->port;
  1296. memset(&ms, 0, sizeof(ms));
  1297. mutex_lock(&port->mutex);
  1298. if (!ip->ioaddr)
  1299. goto copy;
  1300. tty = tty_port_tty_get(port);
  1301. if (!tty || !tty->termios)
  1302. ms.cflag = ip->normal_termios.c_cflag;
  1303. else
  1304. ms.cflag = tty->termios->c_cflag;
  1305. tty_kref_put(tty);
  1306. spin_lock_irq(&ip->slock);
  1307. status = inb(ip->ioaddr + UART_MSR);
  1308. spin_unlock_irq(&ip->slock);
  1309. if (status & UART_MSR_DCD)
  1310. ms.dcd = 1;
  1311. if (status & UART_MSR_DSR)
  1312. ms.dsr = 1;
  1313. if (status & UART_MSR_CTS)
  1314. ms.cts = 1;
  1315. copy:
  1316. mutex_unlock(&port->mutex);
  1317. if (copy_to_user(msu, &ms, sizeof(ms)))
  1318. return -EFAULT;
  1319. msu++;
  1320. }
  1321. return 0;
  1322. }
  1323. case MOXA_ASPP_MON_EXT: {
  1324. struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
  1325. unsigned int cflag, iflag, p;
  1326. u8 opmode;
  1327. me = kzalloc(sizeof(*me), GFP_KERNEL);
  1328. if (!me)
  1329. return -ENOMEM;
  1330. for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
  1331. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
  1332. if (p >= ARRAY_SIZE(me->rx_cnt)) {
  1333. i = MXSER_BOARDS;
  1334. break;
  1335. }
  1336. ip = &mxser_boards[i].ports[j];
  1337. port = &ip->port;
  1338. mutex_lock(&port->mutex);
  1339. if (!ip->ioaddr) {
  1340. mutex_unlock(&port->mutex);
  1341. continue;
  1342. }
  1343. spin_lock_irq(&ip->slock);
  1344. status = mxser_get_msr(ip->ioaddr, 0, p);
  1345. if (status & UART_MSR_TERI)
  1346. ip->icount.rng++;
  1347. if (status & UART_MSR_DDSR)
  1348. ip->icount.dsr++;
  1349. if (status & UART_MSR_DDCD)
  1350. ip->icount.dcd++;
  1351. if (status & UART_MSR_DCTS)
  1352. ip->icount.cts++;
  1353. ip->mon_data.modem_status = status;
  1354. me->rx_cnt[p] = ip->mon_data.rxcnt;
  1355. me->tx_cnt[p] = ip->mon_data.txcnt;
  1356. me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
  1357. me->up_txcnt[p] = ip->mon_data.up_txcnt;
  1358. me->modem_status[p] =
  1359. ip->mon_data.modem_status;
  1360. spin_unlock_irq(&ip->slock);
  1361. tty = tty_port_tty_get(&ip->port);
  1362. if (!tty || !tty->termios) {
  1363. cflag = ip->normal_termios.c_cflag;
  1364. iflag = ip->normal_termios.c_iflag;
  1365. me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
  1366. } else {
  1367. cflag = tty->termios->c_cflag;
  1368. iflag = tty->termios->c_iflag;
  1369. me->baudrate[p] = tty_get_baud_rate(tty);
  1370. }
  1371. tty_kref_put(tty);
  1372. me->databits[p] = cflag & CSIZE;
  1373. me->stopbits[p] = cflag & CSTOPB;
  1374. me->parity[p] = cflag & (PARENB | PARODD |
  1375. CMSPAR);
  1376. if (cflag & CRTSCTS)
  1377. me->flowctrl[p] |= 0x03;
  1378. if (iflag & (IXON | IXOFF))
  1379. me->flowctrl[p] |= 0x0C;
  1380. if (ip->type == PORT_16550A)
  1381. me->fifo[p] = 1;
  1382. opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
  1383. opmode &= OP_MODE_MASK;
  1384. me->iftype[p] = opmode;
  1385. mutex_unlock(&port->mutex);
  1386. }
  1387. }
  1388. if (copy_to_user(argp, me, sizeof(*me)))
  1389. ret = -EFAULT;
  1390. kfree(me);
  1391. return ret;
  1392. }
  1393. default:
  1394. return -ENOIOCTLCMD;
  1395. }
  1396. return 0;
  1397. }
  1398. static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
  1399. struct async_icount *cprev)
  1400. {
  1401. struct async_icount cnow;
  1402. unsigned long flags;
  1403. int ret;
  1404. spin_lock_irqsave(&info->slock, flags);
  1405. cnow = info->icount; /* atomic copy */
  1406. spin_unlock_irqrestore(&info->slock, flags);
  1407. ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  1408. ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  1409. ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  1410. ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1411. *cprev = cnow;
  1412. return ret;
  1413. }
  1414. static int mxser_ioctl(struct tty_struct *tty, struct file *file,
  1415. unsigned int cmd, unsigned long arg)
  1416. {
  1417. struct mxser_port *info = tty->driver_data;
  1418. struct tty_port *port = &info->port;
  1419. struct async_icount cnow;
  1420. unsigned long flags;
  1421. void __user *argp = (void __user *)arg;
  1422. int retval;
  1423. if (tty->index == MXSER_PORTS)
  1424. return mxser_ioctl_special(cmd, argp);
  1425. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1426. int p;
  1427. unsigned long opmode;
  1428. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1429. int shiftbit;
  1430. unsigned char val, mask;
  1431. p = tty->index % 4;
  1432. if (cmd == MOXA_SET_OP_MODE) {
  1433. if (get_user(opmode, (int __user *) argp))
  1434. return -EFAULT;
  1435. if (opmode != RS232_MODE &&
  1436. opmode != RS485_2WIRE_MODE &&
  1437. opmode != RS422_MODE &&
  1438. opmode != RS485_4WIRE_MODE)
  1439. return -EFAULT;
  1440. mask = ModeMask[p];
  1441. shiftbit = p * 2;
  1442. spin_lock_irq(&info->slock);
  1443. val = inb(info->opmode_ioaddr);
  1444. val &= mask;
  1445. val |= (opmode << shiftbit);
  1446. outb(val, info->opmode_ioaddr);
  1447. spin_unlock_irq(&info->slock);
  1448. } else {
  1449. shiftbit = p * 2;
  1450. spin_lock_irq(&info->slock);
  1451. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1452. spin_unlock_irq(&info->slock);
  1453. opmode &= OP_MODE_MASK;
  1454. if (put_user(opmode, (int __user *)argp))
  1455. return -EFAULT;
  1456. }
  1457. return 0;
  1458. }
  1459. if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT &&
  1460. test_bit(TTY_IO_ERROR, &tty->flags))
  1461. return -EIO;
  1462. switch (cmd) {
  1463. case TIOCGSERIAL:
  1464. mutex_lock(&port->mutex);
  1465. retval = mxser_get_serial_info(tty, argp);
  1466. mutex_unlock(&port->mutex);
  1467. return retval;
  1468. case TIOCSSERIAL:
  1469. mutex_lock(&port->mutex);
  1470. retval = mxser_set_serial_info(tty, argp);
  1471. mutex_unlock(&port->mutex);
  1472. return retval;
  1473. case TIOCSERGETLSR: /* Get line status register */
  1474. return mxser_get_lsr_info(info, argp);
  1475. /*
  1476. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1477. * - mask passed in arg for lines of interest
  1478. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1479. * Caller should use TIOCGICOUNT to see which one it was
  1480. */
  1481. case TIOCMIWAIT:
  1482. spin_lock_irqsave(&info->slock, flags);
  1483. cnow = info->icount; /* note the counters on entry */
  1484. spin_unlock_irqrestore(&info->slock, flags);
  1485. return wait_event_interruptible(info->port.delta_msr_wait,
  1486. mxser_cflags_changed(info, arg, &cnow));
  1487. case MOXA_HighSpeedOn:
  1488. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
  1489. case MOXA_SDS_RSTICOUNTER:
  1490. spin_lock_irq(&info->slock);
  1491. info->mon_data.rxcnt = 0;
  1492. info->mon_data.txcnt = 0;
  1493. spin_unlock_irq(&info->slock);
  1494. return 0;
  1495. case MOXA_ASPP_OQUEUE:{
  1496. int len, lsr;
  1497. len = mxser_chars_in_buffer(tty);
  1498. spin_lock_irq(&info->slock);
  1499. lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
  1500. spin_unlock_irq(&info->slock);
  1501. len += (lsr ? 0 : 1);
  1502. return put_user(len, (int __user *)argp);
  1503. }
  1504. case MOXA_ASPP_MON: {
  1505. int mcr, status;
  1506. spin_lock_irq(&info->slock);
  1507. status = mxser_get_msr(info->ioaddr, 1, tty->index);
  1508. mxser_check_modem_status(tty, info, status);
  1509. mcr = inb(info->ioaddr + UART_MCR);
  1510. spin_unlock_irq(&info->slock);
  1511. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1512. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1513. else
  1514. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1515. if (mcr & MOXA_MUST_MCR_TX_XON)
  1516. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1517. else
  1518. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1519. if (tty->hw_stopped)
  1520. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1521. else
  1522. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1523. if (copy_to_user(argp, &info->mon_data,
  1524. sizeof(struct mxser_mon)))
  1525. return -EFAULT;
  1526. return 0;
  1527. }
  1528. case MOXA_ASPP_LSTATUS: {
  1529. if (put_user(info->err_shadow, (unsigned char __user *)argp))
  1530. return -EFAULT;
  1531. info->err_shadow = 0;
  1532. return 0;
  1533. }
  1534. case MOXA_SET_BAUD_METHOD: {
  1535. int method;
  1536. if (get_user(method, (int __user *)argp))
  1537. return -EFAULT;
  1538. mxser_set_baud_method[tty->index] = method;
  1539. return put_user(method, (int __user *)argp);
  1540. }
  1541. default:
  1542. return -ENOIOCTLCMD;
  1543. }
  1544. return 0;
  1545. }
  1546. /*
  1547. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1548. * Return: write counters to the user passed counter struct
  1549. * NB: both 1->0 and 0->1 transitions are counted except for
  1550. * RI where only 0->1 is counted.
  1551. */
  1552. static int mxser_get_icount(struct tty_struct *tty,
  1553. struct serial_icounter_struct *icount)
  1554. {
  1555. struct mxser_port *info = tty->driver_data;
  1556. struct async_icount cnow;
  1557. unsigned long flags;
  1558. spin_lock_irqsave(&info->slock, flags);
  1559. cnow = info->icount;
  1560. spin_unlock_irqrestore(&info->slock, flags);
  1561. icount->frame = cnow.frame;
  1562. icount->brk = cnow.brk;
  1563. icount->overrun = cnow.overrun;
  1564. icount->buf_overrun = cnow.buf_overrun;
  1565. icount->parity = cnow.parity;
  1566. icount->rx = cnow.rx;
  1567. icount->tx = cnow.tx;
  1568. icount->cts = cnow.cts;
  1569. icount->dsr = cnow.dsr;
  1570. icount->rng = cnow.rng;
  1571. icount->dcd = cnow.dcd;
  1572. return 0;
  1573. }
  1574. static void mxser_stoprx(struct tty_struct *tty)
  1575. {
  1576. struct mxser_port *info = tty->driver_data;
  1577. info->ldisc_stop_rx = 1;
  1578. if (I_IXOFF(tty)) {
  1579. if (info->board->chip_flag) {
  1580. info->IER &= ~MOXA_MUST_RECV_ISR;
  1581. outb(info->IER, info->ioaddr + UART_IER);
  1582. } else {
  1583. info->x_char = STOP_CHAR(tty);
  1584. outb(0, info->ioaddr + UART_IER);
  1585. info->IER |= UART_IER_THRI;
  1586. outb(info->IER, info->ioaddr + UART_IER);
  1587. }
  1588. }
  1589. if (tty->termios->c_cflag & CRTSCTS) {
  1590. info->MCR &= ~UART_MCR_RTS;
  1591. outb(info->MCR, info->ioaddr + UART_MCR);
  1592. }
  1593. }
  1594. /*
  1595. * This routine is called by the upper-layer tty layer to signal that
  1596. * incoming characters should be throttled.
  1597. */
  1598. static void mxser_throttle(struct tty_struct *tty)
  1599. {
  1600. mxser_stoprx(tty);
  1601. }
  1602. static void mxser_unthrottle(struct tty_struct *tty)
  1603. {
  1604. struct mxser_port *info = tty->driver_data;
  1605. /* startrx */
  1606. info->ldisc_stop_rx = 0;
  1607. if (I_IXOFF(tty)) {
  1608. if (info->x_char)
  1609. info->x_char = 0;
  1610. else {
  1611. if (info->board->chip_flag) {
  1612. info->IER |= MOXA_MUST_RECV_ISR;
  1613. outb(info->IER, info->ioaddr + UART_IER);
  1614. } else {
  1615. info->x_char = START_CHAR(tty);
  1616. outb(0, info->ioaddr + UART_IER);
  1617. info->IER |= UART_IER_THRI;
  1618. outb(info->IER, info->ioaddr + UART_IER);
  1619. }
  1620. }
  1621. }
  1622. if (tty->termios->c_cflag & CRTSCTS) {
  1623. info->MCR |= UART_MCR_RTS;
  1624. outb(info->MCR, info->ioaddr + UART_MCR);
  1625. }
  1626. }
  1627. /*
  1628. * mxser_stop() and mxser_start()
  1629. *
  1630. * This routines are called before setting or resetting tty->stopped.
  1631. * They enable or disable transmitter interrupts, as necessary.
  1632. */
  1633. static void mxser_stop(struct tty_struct *tty)
  1634. {
  1635. struct mxser_port *info = tty->driver_data;
  1636. unsigned long flags;
  1637. spin_lock_irqsave(&info->slock, flags);
  1638. if (info->IER & UART_IER_THRI) {
  1639. info->IER &= ~UART_IER_THRI;
  1640. outb(info->IER, info->ioaddr + UART_IER);
  1641. }
  1642. spin_unlock_irqrestore(&info->slock, flags);
  1643. }
  1644. static void mxser_start(struct tty_struct *tty)
  1645. {
  1646. struct mxser_port *info = tty->driver_data;
  1647. unsigned long flags;
  1648. spin_lock_irqsave(&info->slock, flags);
  1649. if (info->xmit_cnt && info->port.xmit_buf) {
  1650. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1651. info->IER |= UART_IER_THRI;
  1652. outb(info->IER, info->ioaddr + UART_IER);
  1653. }
  1654. spin_unlock_irqrestore(&info->slock, flags);
  1655. }
  1656. static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1657. {
  1658. struct mxser_port *info = tty->driver_data;
  1659. unsigned long flags;
  1660. spin_lock_irqsave(&info->slock, flags);
  1661. mxser_change_speed(tty, old_termios);
  1662. spin_unlock_irqrestore(&info->slock, flags);
  1663. if ((old_termios->c_cflag & CRTSCTS) &&
  1664. !(tty->termios->c_cflag & CRTSCTS)) {
  1665. tty->hw_stopped = 0;
  1666. mxser_start(tty);
  1667. }
  1668. /* Handle sw stopped */
  1669. if ((old_termios->c_iflag & IXON) &&
  1670. !(tty->termios->c_iflag & IXON)) {
  1671. tty->stopped = 0;
  1672. if (info->board->chip_flag) {
  1673. spin_lock_irqsave(&info->slock, flags);
  1674. mxser_disable_must_rx_software_flow_control(
  1675. info->ioaddr);
  1676. spin_unlock_irqrestore(&info->slock, flags);
  1677. }
  1678. mxser_start(tty);
  1679. }
  1680. }
  1681. /*
  1682. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1683. */
  1684. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1685. {
  1686. struct mxser_port *info = tty->driver_data;
  1687. unsigned long orig_jiffies, char_time;
  1688. unsigned long flags;
  1689. int lsr;
  1690. if (info->type == PORT_UNKNOWN)
  1691. return;
  1692. if (info->xmit_fifo_size == 0)
  1693. return; /* Just in case.... */
  1694. orig_jiffies = jiffies;
  1695. /*
  1696. * Set the check interval to be 1/5 of the estimated time to
  1697. * send a single character, and make it at least 1. The check
  1698. * interval should also be less than the timeout.
  1699. *
  1700. * Note: we have to use pretty tight timings here to satisfy
  1701. * the NIST-PCTS.
  1702. */
  1703. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1704. char_time = char_time / 5;
  1705. if (char_time == 0)
  1706. char_time = 1;
  1707. if (timeout && timeout < char_time)
  1708. char_time = timeout;
  1709. /*
  1710. * If the transmitter hasn't cleared in twice the approximate
  1711. * amount of time to send the entire FIFO, it probably won't
  1712. * ever clear. This assumes the UART isn't doing flow
  1713. * control, which is currently the case. Hence, if it ever
  1714. * takes longer than info->timeout, this is probably due to a
  1715. * UART bug of some kind. So, we clamp the timeout parameter at
  1716. * 2*info->timeout.
  1717. */
  1718. if (!timeout || timeout > 2 * info->timeout)
  1719. timeout = 2 * info->timeout;
  1720. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1721. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
  1722. timeout, char_time);
  1723. printk("jiff=%lu...", jiffies);
  1724. #endif
  1725. spin_lock_irqsave(&info->slock, flags);
  1726. while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
  1727. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1728. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1729. #endif
  1730. spin_unlock_irqrestore(&info->slock, flags);
  1731. schedule_timeout_interruptible(char_time);
  1732. spin_lock_irqsave(&info->slock, flags);
  1733. if (signal_pending(current))
  1734. break;
  1735. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1736. break;
  1737. }
  1738. spin_unlock_irqrestore(&info->slock, flags);
  1739. set_current_state(TASK_RUNNING);
  1740. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1741. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1742. #endif
  1743. }
  1744. /*
  1745. * This routine is called by tty_hangup() when a hangup is signaled.
  1746. */
  1747. static void mxser_hangup(struct tty_struct *tty)
  1748. {
  1749. struct mxser_port *info = tty->driver_data;
  1750. mxser_flush_buffer(tty);
  1751. tty_port_hangup(&info->port);
  1752. }
  1753. /*
  1754. * mxser_rs_break() --- routine which turns the break handling on or off
  1755. */
  1756. static int mxser_rs_break(struct tty_struct *tty, int break_state)
  1757. {
  1758. struct mxser_port *info = tty->driver_data;
  1759. unsigned long flags;
  1760. spin_lock_irqsave(&info->slock, flags);
  1761. if (break_state == -1)
  1762. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1763. info->ioaddr + UART_LCR);
  1764. else
  1765. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1766. info->ioaddr + UART_LCR);
  1767. spin_unlock_irqrestore(&info->slock, flags);
  1768. return 0;
  1769. }
  1770. static void mxser_receive_chars(struct tty_struct *tty,
  1771. struct mxser_port *port, int *status)
  1772. {
  1773. unsigned char ch, gdl;
  1774. int ignored = 0;
  1775. int cnt = 0;
  1776. int recv_room;
  1777. int max = 256;
  1778. recv_room = tty->receive_room;
  1779. if (recv_room == 0 && !port->ldisc_stop_rx)
  1780. mxser_stoprx(tty);
  1781. if (port->board->chip_flag != MOXA_OTHER_UART) {
  1782. if (*status & UART_LSR_SPECIAL)
  1783. goto intr_old;
  1784. if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
  1785. (*status & MOXA_MUST_LSR_RERR))
  1786. goto intr_old;
  1787. if (*status & MOXA_MUST_LSR_RERR)
  1788. goto intr_old;
  1789. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1790. if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
  1791. gdl &= MOXA_MUST_GDL_MASK;
  1792. if (gdl >= recv_room) {
  1793. if (!port->ldisc_stop_rx)
  1794. mxser_stoprx(tty);
  1795. }
  1796. while (gdl--) {
  1797. ch = inb(port->ioaddr + UART_RX);
  1798. tty_insert_flip_char(tty, ch, 0);
  1799. cnt++;
  1800. }
  1801. goto end_intr;
  1802. }
  1803. intr_old:
  1804. do {
  1805. if (max-- < 0)
  1806. break;
  1807. ch = inb(port->ioaddr + UART_RX);
  1808. if (port->board->chip_flag && (*status & UART_LSR_OE))
  1809. outb(0x23, port->ioaddr + UART_FCR);
  1810. *status &= port->read_status_mask;
  1811. if (*status & port->ignore_status_mask) {
  1812. if (++ignored > 100)
  1813. break;
  1814. } else {
  1815. char flag = 0;
  1816. if (*status & UART_LSR_SPECIAL) {
  1817. if (*status & UART_LSR_BI) {
  1818. flag = TTY_BREAK;
  1819. port->icount.brk++;
  1820. if (port->port.flags & ASYNC_SAK)
  1821. do_SAK(tty);
  1822. } else if (*status & UART_LSR_PE) {
  1823. flag = TTY_PARITY;
  1824. port->icount.parity++;
  1825. } else if (*status & UART_LSR_FE) {
  1826. flag = TTY_FRAME;
  1827. port->icount.frame++;
  1828. } else if (*status & UART_LSR_OE) {
  1829. flag = TTY_OVERRUN;
  1830. port->icount.overrun++;
  1831. } else
  1832. flag = TTY_BREAK;
  1833. }
  1834. tty_insert_flip_char(tty, ch, flag);
  1835. cnt++;
  1836. if (cnt >= recv_room) {
  1837. if (!port->ldisc_stop_rx)
  1838. mxser_stoprx(tty);
  1839. break;
  1840. }
  1841. }
  1842. if (port->board->chip_flag)
  1843. break;
  1844. *status = inb(port->ioaddr + UART_LSR);
  1845. } while (*status & UART_LSR_DR);
  1846. end_intr:
  1847. mxvar_log.rxcnt[tty->index] += cnt;
  1848. port->mon_data.rxcnt += cnt;
  1849. port->mon_data.up_rxcnt += cnt;
  1850. /*
  1851. * We are called from an interrupt context with &port->slock
  1852. * being held. Drop it temporarily in order to prevent
  1853. * recursive locking.
  1854. */
  1855. spin_unlock(&port->slock);
  1856. tty_flip_buffer_push(tty);
  1857. spin_lock(&port->slock);
  1858. }
  1859. static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
  1860. {
  1861. int count, cnt;
  1862. if (port->x_char) {
  1863. outb(port->x_char, port->ioaddr + UART_TX);
  1864. port->x_char = 0;
  1865. mxvar_log.txcnt[tty->index]++;
  1866. port->mon_data.txcnt++;
  1867. port->mon_data.up_txcnt++;
  1868. port->icount.tx++;
  1869. return;
  1870. }
  1871. if (port->port.xmit_buf == NULL)
  1872. return;
  1873. if (port->xmit_cnt <= 0 || tty->stopped ||
  1874. (tty->hw_stopped &&
  1875. (port->type != PORT_16550A) &&
  1876. (!port->board->chip_flag))) {
  1877. port->IER &= ~UART_IER_THRI;
  1878. outb(port->IER, port->ioaddr + UART_IER);
  1879. return;
  1880. }
  1881. cnt = port->xmit_cnt;
  1882. count = port->xmit_fifo_size;
  1883. do {
  1884. outb(port->port.xmit_buf[port->xmit_tail++],
  1885. port->ioaddr + UART_TX);
  1886. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  1887. if (--port->xmit_cnt <= 0)
  1888. break;
  1889. } while (--count > 0);
  1890. mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
  1891. port->mon_data.txcnt += (cnt - port->xmit_cnt);
  1892. port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
  1893. port->icount.tx += (cnt - port->xmit_cnt);
  1894. if (port->xmit_cnt < WAKEUP_CHARS)
  1895. tty_wakeup(tty);
  1896. if (port->xmit_cnt <= 0) {
  1897. port->IER &= ~UART_IER_THRI;
  1898. outb(port->IER, port->ioaddr + UART_IER);
  1899. }
  1900. }
  1901. /*
  1902. * This is the serial driver's generic interrupt routine
  1903. */
  1904. static irqreturn_t mxser_interrupt(int irq, void *dev_id)
  1905. {
  1906. int status, iir, i;
  1907. struct mxser_board *brd = NULL;
  1908. struct mxser_port *port;
  1909. int max, irqbits, bits, msr;
  1910. unsigned int int_cnt, pass_counter = 0;
  1911. int handled = IRQ_NONE;
  1912. struct tty_struct *tty;
  1913. for (i = 0; i < MXSER_BOARDS; i++)
  1914. if (dev_id == &mxser_boards[i]) {
  1915. brd = dev_id;
  1916. break;
  1917. }
  1918. if (i == MXSER_BOARDS)
  1919. goto irq_stop;
  1920. if (brd == NULL)
  1921. goto irq_stop;
  1922. max = brd->info->nports;
  1923. while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
  1924. irqbits = inb(brd->vector) & brd->vector_mask;
  1925. if (irqbits == brd->vector_mask)
  1926. break;
  1927. handled = IRQ_HANDLED;
  1928. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  1929. if (irqbits == brd->vector_mask)
  1930. break;
  1931. if (bits & irqbits)
  1932. continue;
  1933. port = &brd->ports[i];
  1934. int_cnt = 0;
  1935. spin_lock(&port->slock);
  1936. do {
  1937. iir = inb(port->ioaddr + UART_IIR);
  1938. if (iir & UART_IIR_NO_INT)
  1939. break;
  1940. iir &= MOXA_MUST_IIR_MASK;
  1941. tty = tty_port_tty_get(&port->port);
  1942. if (!tty ||
  1943. (port->port.flags & ASYNC_CLOSING) ||
  1944. !(port->port.flags &
  1945. ASYNC_INITIALIZED)) {
  1946. status = inb(port->ioaddr + UART_LSR);
  1947. outb(0x27, port->ioaddr + UART_FCR);
  1948. inb(port->ioaddr + UART_MSR);
  1949. tty_kref_put(tty);
  1950. break;
  1951. }
  1952. status = inb(port->ioaddr + UART_LSR);
  1953. if (status & UART_LSR_PE)
  1954. port->err_shadow |= NPPI_NOTIFY_PARITY;
  1955. if (status & UART_LSR_FE)
  1956. port->err_shadow |= NPPI_NOTIFY_FRAMING;
  1957. if (status & UART_LSR_OE)
  1958. port->err_shadow |=
  1959. NPPI_NOTIFY_HW_OVERRUN;
  1960. if (status & UART_LSR_BI)
  1961. port->err_shadow |= NPPI_NOTIFY_BREAK;
  1962. if (port->board->chip_flag) {
  1963. if (iir == MOXA_MUST_IIR_GDA ||
  1964. iir == MOXA_MUST_IIR_RDA ||
  1965. iir == MOXA_MUST_IIR_RTO ||
  1966. iir == MOXA_MUST_IIR_LSR)
  1967. mxser_receive_chars(tty, port,
  1968. &status);
  1969. } else {
  1970. status &= port->read_status_mask;
  1971. if (status & UART_LSR_DR)
  1972. mxser_receive_chars(tty, port,
  1973. &status);
  1974. }
  1975. msr = inb(port->ioaddr + UART_MSR);
  1976. if (msr & UART_MSR_ANY_DELTA)
  1977. mxser_check_modem_status(tty, port, msr);
  1978. if (port->board->chip_flag) {
  1979. if (iir == 0x02 && (status &
  1980. UART_LSR_THRE))
  1981. mxser_transmit_chars(tty, port);
  1982. } else {
  1983. if (status & UART_LSR_THRE)
  1984. mxser_transmit_chars(tty, port);
  1985. }
  1986. tty_kref_put(tty);
  1987. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  1988. spin_unlock(&port->slock);
  1989. }
  1990. }
  1991. irq_stop:
  1992. return handled;
  1993. }
  1994. static const struct tty_operations mxser_ops = {
  1995. .open = mxser_open,
  1996. .close = mxser_close,
  1997. .write = mxser_write,
  1998. .put_char = mxser_put_char,
  1999. .flush_chars = mxser_flush_chars,
  2000. .write_room = mxser_write_room,
  2001. .chars_in_buffer = mxser_chars_in_buffer,
  2002. .flush_buffer = mxser_flush_buffer,
  2003. .ioctl = mxser_ioctl,
  2004. .throttle = mxser_throttle,
  2005. .unthrottle = mxser_unthrottle,
  2006. .set_termios = mxser_set_termios,
  2007. .stop = mxser_stop,
  2008. .start = mxser_start,
  2009. .hangup = mxser_hangup,
  2010. .break_ctl = mxser_rs_break,
  2011. .wait_until_sent = mxser_wait_until_sent,
  2012. .tiocmget = mxser_tiocmget,
  2013. .tiocmset = mxser_tiocmset,
  2014. .get_icount = mxser_get_icount,
  2015. };
  2016. struct tty_port_operations mxser_port_ops = {
  2017. .carrier_raised = mxser_carrier_raised,
  2018. .dtr_rts = mxser_dtr_rts,
  2019. .activate = mxser_activate,
  2020. .shutdown = mxser_shutdown_port,
  2021. };
  2022. /*
  2023. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  2024. */
  2025. static void mxser_release_ISA_res(struct mxser_board *brd)
  2026. {
  2027. free_irq(brd->irq, brd);
  2028. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2029. release_region(brd->vector, 1);
  2030. }
  2031. static int __devinit mxser_initbrd(struct mxser_board *brd,
  2032. struct pci_dev *pdev)
  2033. {
  2034. struct mxser_port *info;
  2035. unsigned int i;
  2036. int retval;
  2037. printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
  2038. brd->ports[0].max_baud);
  2039. for (i = 0; i < brd->info->nports; i++) {
  2040. info = &brd->ports[i];
  2041. tty_port_init(&info->port);
  2042. info->port.ops = &mxser_port_ops;
  2043. info->board = brd;
  2044. info->stop_rx = 0;
  2045. info->ldisc_stop_rx = 0;
  2046. /* Enhance mode enabled here */
  2047. if (brd->chip_flag != MOXA_OTHER_UART)
  2048. mxser_enable_must_enchance_mode(info->ioaddr);
  2049. info->port.flags = ASYNC_SHARE_IRQ;
  2050. info->type = brd->uart_type;
  2051. process_txrx_fifo(info);
  2052. info->custom_divisor = info->baud_base * 16;
  2053. info->port.close_delay = 5 * HZ / 10;
  2054. info->port.closing_wait = 30 * HZ;
  2055. info->normal_termios = mxvar_sdriver->init_termios;
  2056. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  2057. info->err_shadow = 0;
  2058. spin_lock_init(&info->slock);
  2059. /* before set INT ISR, disable all int */
  2060. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  2061. info->ioaddr + UART_IER);
  2062. }
  2063. retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
  2064. brd);
  2065. if (retval)
  2066. printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
  2067. "conflict with another device.\n",
  2068. brd->info->name, brd->irq);
  2069. return retval;
  2070. }
  2071. static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
  2072. {
  2073. int id, i, bits;
  2074. unsigned short regs[16], irq;
  2075. unsigned char scratch, scratch2;
  2076. brd->chip_flag = MOXA_OTHER_UART;
  2077. id = mxser_read_register(cap, regs);
  2078. switch (id) {
  2079. case C168_ASIC_ID:
  2080. brd->info = &mxser_cards[0];
  2081. break;
  2082. case C104_ASIC_ID:
  2083. brd->info = &mxser_cards[1];
  2084. break;
  2085. case CI104J_ASIC_ID:
  2086. brd->info = &mxser_cards[2];
  2087. break;
  2088. case C102_ASIC_ID:
  2089. brd->info = &mxser_cards[5];
  2090. break;
  2091. case CI132_ASIC_ID:
  2092. brd->info = &mxser_cards[6];
  2093. break;
  2094. case CI134_ASIC_ID:
  2095. brd->info = &mxser_cards[7];
  2096. break;
  2097. default:
  2098. return 0;
  2099. }
  2100. irq = 0;
  2101. /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
  2102. Flag-hack checks if configuration should be read as 2-port here. */
  2103. if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
  2104. irq = regs[9] & 0xF000;
  2105. irq = irq | (irq >> 4);
  2106. if (irq != (regs[9] & 0xFF00))
  2107. goto err_irqconflict;
  2108. } else if (brd->info->nports == 4) {
  2109. irq = regs[9] & 0xF000;
  2110. irq = irq | (irq >> 4);
  2111. irq = irq | (irq >> 8);
  2112. if (irq != regs[9])
  2113. goto err_irqconflict;
  2114. } else if (brd->info->nports == 8) {
  2115. irq = regs[9] & 0xF000;
  2116. irq = irq | (irq >> 4);
  2117. irq = irq | (irq >> 8);
  2118. if ((irq != regs[9]) || (irq != regs[10]))
  2119. goto err_irqconflict;
  2120. }
  2121. if (!irq) {
  2122. printk(KERN_ERR "mxser: interrupt number unset\n");
  2123. return -EIO;
  2124. }
  2125. brd->irq = ((int)(irq & 0xF000) >> 12);
  2126. for (i = 0; i < 8; i++)
  2127. brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
  2128. if ((regs[12] & 0x80) == 0) {
  2129. printk(KERN_ERR "mxser: invalid interrupt vector\n");
  2130. return -EIO;
  2131. }
  2132. brd->vector = (int)regs[11]; /* interrupt vector */
  2133. if (id == 1)
  2134. brd->vector_mask = 0x00FF;
  2135. else
  2136. brd->vector_mask = 0x000F;
  2137. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2138. if (regs[12] & bits) {
  2139. brd->ports[i].baud_base = 921600;
  2140. brd->ports[i].max_baud = 921600;
  2141. } else {
  2142. brd->ports[i].baud_base = 115200;
  2143. brd->ports[i].max_baud = 115200;
  2144. }
  2145. }
  2146. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2147. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2148. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2149. outb(scratch2, cap + UART_LCR);
  2150. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2151. scratch = inb(cap + UART_IIR);
  2152. if (scratch & 0xC0)
  2153. brd->uart_type = PORT_16550A;
  2154. else
  2155. brd->uart_type = PORT_16450;
  2156. if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
  2157. "mxser(IO)")) {
  2158. printk(KERN_ERR "mxser: can't request ports I/O region: "
  2159. "0x%.8lx-0x%.8lx\n",
  2160. brd->ports[0].ioaddr, brd->ports[0].ioaddr +
  2161. 8 * brd->info->nports - 1);
  2162. return -EIO;
  2163. }
  2164. if (!request_region(brd->vector, 1, "mxser(vector)")) {
  2165. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2166. printk(KERN_ERR "mxser: can't request interrupt vector region: "
  2167. "0x%.8lx-0x%.8lx\n",
  2168. brd->ports[0].ioaddr, brd->ports[0].ioaddr +
  2169. 8 * brd->info->nports - 1);
  2170. return -EIO;
  2171. }
  2172. return brd->info->nports;
  2173. err_irqconflict:
  2174. printk(KERN_ERR "mxser: invalid interrupt number\n");
  2175. return -EIO;
  2176. }
  2177. static int __devinit mxser_probe(struct pci_dev *pdev,
  2178. const struct pci_device_id *ent)
  2179. {
  2180. #ifdef CONFIG_PCI
  2181. struct mxser_board *brd;
  2182. unsigned int i, j;
  2183. unsigned long ioaddress;
  2184. int retval = -EINVAL;
  2185. for (i = 0; i < MXSER_BOARDS; i++)
  2186. if (mxser_boards[i].info == NULL)
  2187. break;
  2188. if (i >= MXSER_BOARDS) {
  2189. dev_err(&pdev->dev, "too many boards found (maximum %d), board "
  2190. "not configured\n", MXSER_BOARDS);
  2191. goto err;
  2192. }
  2193. brd = &mxser_boards[i];
  2194. brd->idx = i * MXSER_PORTS_PER_BOARD;
  2195. dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
  2196. mxser_cards[ent->driver_data].name,
  2197. pdev->bus->number, PCI_SLOT(pdev->devfn));
  2198. retval = pci_enable_device(pdev);
  2199. if (retval) {
  2200. dev_err(&pdev->dev, "PCI enable failed\n");
  2201. goto err;
  2202. }
  2203. /* io address */
  2204. ioaddress = pci_resource_start(pdev, 2);
  2205. retval = pci_request_region(pdev, 2, "mxser(IO)");
  2206. if (retval)
  2207. goto err_dis;
  2208. brd->info = &mxser_cards[ent->driver_data];
  2209. for (i = 0; i < brd->info->nports; i++)
  2210. brd->ports[i].ioaddr = ioaddress + 8 * i;
  2211. /* vector */
  2212. ioaddress = pci_resource_start(pdev, 3);
  2213. retval = pci_request_region(pdev, 3, "mxser(vector)");
  2214. if (retval)
  2215. goto err_zero;
  2216. brd->vector = ioaddress;
  2217. /* irq */
  2218. brd->irq = pdev->irq;
  2219. brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
  2220. brd->uart_type = PORT_16550A;
  2221. brd->vector_mask = 0;
  2222. for (i = 0; i < brd->info->nports; i++) {
  2223. for (j = 0; j < UART_INFO_NUM; j++) {
  2224. if (Gpci_uart_info[j].type == brd->chip_flag) {
  2225. brd->ports[i].max_baud =
  2226. Gpci_uart_info[j].max_baud;
  2227. /* exception....CP-102 */
  2228. if (brd->info->flags & MXSER_HIGHBAUD)
  2229. brd->ports[i].max_baud = 921600;
  2230. break;
  2231. }
  2232. }
  2233. }
  2234. if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
  2235. for (i = 0; i < brd->info->nports; i++) {
  2236. if (i < 4)
  2237. brd->ports[i].opmode_ioaddr = ioaddress + 4;
  2238. else
  2239. brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
  2240. }
  2241. outb(0, ioaddress + 4); /* default set to RS232 mode */
  2242. outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
  2243. }
  2244. for (i = 0; i < brd->info->nports; i++) {
  2245. brd->vector_mask |= (1 << i);
  2246. brd->ports[i].baud_base = 921600;
  2247. }
  2248. /* mxser_initbrd will hook ISR. */
  2249. retval = mxser_initbrd(brd, pdev);
  2250. if (retval)
  2251. goto err_rel3;
  2252. for (i = 0; i < brd->info->nports; i++)
  2253. tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
  2254. pci_set_drvdata(pdev, brd);
  2255. return 0;
  2256. err_rel3:
  2257. pci_release_region(pdev, 3);
  2258. err_zero:
  2259. brd->info = NULL;
  2260. pci_release_region(pdev, 2);
  2261. err_dis:
  2262. pci_disable_device(pdev);
  2263. err:
  2264. return retval;
  2265. #else
  2266. return -ENODEV;
  2267. #endif
  2268. }
  2269. static void __devexit mxser_remove(struct pci_dev *pdev)
  2270. {
  2271. #ifdef CONFIG_PCI
  2272. struct mxser_board *brd = pci_get_drvdata(pdev);
  2273. unsigned int i;
  2274. for (i = 0; i < brd->info->nports; i++)
  2275. tty_unregister_device(mxvar_sdriver, brd->idx + i);
  2276. free_irq(pdev->irq, brd);
  2277. pci_release_region(pdev, 2);
  2278. pci_release_region(pdev, 3);
  2279. pci_disable_device(pdev);
  2280. brd->info = NULL;
  2281. #endif
  2282. }
  2283. static struct pci_driver mxser_driver = {
  2284. .name = "mxser",
  2285. .id_table = mxser_pcibrds,
  2286. .probe = mxser_probe,
  2287. .remove = __devexit_p(mxser_remove)
  2288. };
  2289. static int __init mxser_module_init(void)
  2290. {
  2291. struct mxser_board *brd;
  2292. unsigned int b, i, m;
  2293. int retval;
  2294. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  2295. if (!mxvar_sdriver)
  2296. return -ENOMEM;
  2297. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
  2298. MXSER_VERSION);
  2299. /* Initialize the tty_driver structure */
  2300. mxvar_sdriver->owner = THIS_MODULE;
  2301. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  2302. mxvar_sdriver->name = "ttyMI";
  2303. mxvar_sdriver->major = ttymajor;
  2304. mxvar_sdriver->minor_start = 0;
  2305. mxvar_sdriver->num = MXSER_PORTS + 1;
  2306. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  2307. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  2308. mxvar_sdriver->init_termios = tty_std_termios;
  2309. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  2310. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
  2311. tty_set_operations(mxvar_sdriver, &mxser_ops);
  2312. retval = tty_register_driver(mxvar_sdriver);
  2313. if (retval) {
  2314. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
  2315. "tty driver !\n");
  2316. goto err_put;
  2317. }
  2318. /* Start finding ISA boards here */
  2319. for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
  2320. if (!ioaddr[b])
  2321. continue;
  2322. brd = &mxser_boards[m];
  2323. retval = mxser_get_ISA_conf(ioaddr[b], brd);
  2324. if (retval <= 0) {
  2325. brd->info = NULL;
  2326. continue;
  2327. }
  2328. printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
  2329. brd->info->name, ioaddr[b]);
  2330. /* mxser_initbrd will hook ISR. */
  2331. if (mxser_initbrd(brd, NULL) < 0) {
  2332. brd->info = NULL;
  2333. continue;
  2334. }
  2335. brd->idx = m * MXSER_PORTS_PER_BOARD;
  2336. for (i = 0; i < brd->info->nports; i++)
  2337. tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
  2338. m++;
  2339. }
  2340. retval = pci_register_driver(&mxser_driver);
  2341. if (retval) {
  2342. printk(KERN_ERR "mxser: can't register pci driver\n");
  2343. if (!m) {
  2344. retval = -ENODEV;
  2345. goto err_unr;
  2346. } /* else: we have some ISA cards under control */
  2347. }
  2348. return 0;
  2349. err_unr:
  2350. tty_unregister_driver(mxvar_sdriver);
  2351. err_put:
  2352. put_tty_driver(mxvar_sdriver);
  2353. return retval;
  2354. }
  2355. static void __exit mxser_module_exit(void)
  2356. {
  2357. unsigned int i, j;
  2358. pci_unregister_driver(&mxser_driver);
  2359. for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
  2360. if (mxser_boards[i].info != NULL)
  2361. for (j = 0; j < mxser_boards[i].info->nports; j++)
  2362. tty_unregister_device(mxvar_sdriver,
  2363. mxser_boards[i].idx + j);
  2364. tty_unregister_driver(mxvar_sdriver);
  2365. put_tty_driver(mxvar_sdriver);
  2366. for (i = 0; i < MXSER_BOARDS; i++)
  2367. if (mxser_boards[i].info != NULL)
  2368. mxser_release_ISA_res(&mxser_boards[i]);
  2369. }
  2370. module_init(mxser_module_init);
  2371. module_exit(mxser_module_exit);