mxser_new.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119
  1. /*
  2. * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
  3. *
  4. * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
  5. *
  6. * This code is loosely based on the Linux serial driver, written by
  7. * Linus Torvalds, Theodore T'so and others.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
  24. * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
  25. * - Fixed x86_64 cleanness
  26. * - Fixed sleep with spinlock held in mxser_send_break
  27. */
  28. #include <linux/module.h>
  29. #include <linux/autoconf.h>
  30. #include <linux/errno.h>
  31. #include <linux/signal.h>
  32. #include <linux/sched.h>
  33. #include <linux/timer.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/tty.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/serial.h>
  38. #include <linux/serial_reg.h>
  39. #include <linux/major.h>
  40. #include <linux/string.h>
  41. #include <linux/fcntl.h>
  42. #include <linux/ptrace.h>
  43. #include <linux/gfp.h>
  44. #include <linux/ioport.h>
  45. #include <linux/mm.h>
  46. #include <linux/smp_lock.h>
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <asm/system.h>
  50. #include <asm/io.h>
  51. #include <asm/irq.h>
  52. #include <asm/bitops.h>
  53. #include <asm/uaccess.h>
  54. #include "mxser_new.h"
  55. #define MXSER_VERSION "2.0"
  56. #define MXSERMAJOR 174
  57. #define MXSERCUMAJOR 175
  58. #define MXSER_EVENT_TXLOW 1
  59. #define MXSER_EVENT_HANGUP 2
  60. #define MXSER_BOARDS 4 /* Max. boards */
  61. #define MXSER_PORTS 32 /* Max. ports */
  62. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  63. #define MXSER_ISR_PASS_LIMIT 99999L
  64. #define MXSER_ERR_IOADDR -1
  65. #define MXSER_ERR_IRQ -2
  66. #define MXSER_ERR_IRQ_CONFLIT -3
  67. #define MXSER_ERR_VECTOR -4
  68. #define SERIAL_TYPE_NORMAL 1
  69. #define SERIAL_TYPE_CALLOUT 2
  70. #define WAKEUP_CHARS 256
  71. #define UART_MCR_AFE 0x20
  72. #define UART_LSR_SPECIAL 0x1E
  73. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
  74. IXON|IXOFF))
  75. #define C168_ASIC_ID 1
  76. #define C104_ASIC_ID 2
  77. #define C102_ASIC_ID 0xB
  78. #define CI132_ASIC_ID 4
  79. #define CI134_ASIC_ID 3
  80. #define CI104J_ASIC_ID 5
  81. enum {
  82. MXSER_BOARD_C168_ISA = 1,
  83. MXSER_BOARD_C104_ISA,
  84. MXSER_BOARD_CI104J,
  85. MXSER_BOARD_C168_PCI,
  86. MXSER_BOARD_C104_PCI,
  87. MXSER_BOARD_C102_ISA,
  88. MXSER_BOARD_CI132,
  89. MXSER_BOARD_CI134,
  90. MXSER_BOARD_CP132,
  91. MXSER_BOARD_CP114,
  92. MXSER_BOARD_CT114,
  93. MXSER_BOARD_CP102,
  94. MXSER_BOARD_CP104U,
  95. MXSER_BOARD_CP168U,
  96. MXSER_BOARD_CP132U,
  97. MXSER_BOARD_CP134U,
  98. MXSER_BOARD_CP104JU,
  99. MXSER_BOARD_RC7000,
  100. MXSER_BOARD_CP118U,
  101. MXSER_BOARD_CP102UL,
  102. MXSER_BOARD_CP102U,
  103. MXSER_BOARD_CP118EL,
  104. MXSER_BOARD_CP168EL,
  105. MXSER_BOARD_CP104EL
  106. };
  107. static char *mxser_brdname[] = {
  108. "C168 series",
  109. "C104 series",
  110. "CI-104J series",
  111. "C168H/PCI series",
  112. "C104H/PCI series",
  113. "C102 series",
  114. "CI-132 series",
  115. "CI-134 series",
  116. "CP-132 series",
  117. "CP-114 series",
  118. "CT-114 series",
  119. "CP-102 series",
  120. "CP-104U series",
  121. "CP-168U series",
  122. "CP-132U series",
  123. "CP-134U series",
  124. "CP-104JU series",
  125. "Moxa UC7000 Serial",
  126. "CP-118U series",
  127. "CP-102UL series",
  128. "CP-102U series",
  129. "CP-118EL series",
  130. "CP-168EL series",
  131. "CP-104EL series"
  132. };
  133. static int mxser_numports[] = {
  134. 8, /* C168-ISA */
  135. 4, /* C104-ISA */
  136. 4, /* CI104J */
  137. 8, /* C168-PCI */
  138. 4, /* C104-PCI */
  139. 2, /* C102-ISA */
  140. 2, /* CI132 */
  141. 4, /* CI134 */
  142. 2, /* CP132 */
  143. 4, /* CP114 */
  144. 4, /* CT114 */
  145. 2, /* CP102 */
  146. 4, /* CP104U */
  147. 8, /* CP168U */
  148. 2, /* CP132U */
  149. 4, /* CP134U */
  150. 4, /* CP104JU */
  151. 8, /* RC7000 */
  152. 8, /* CP118U */
  153. 2, /* CP102UL */
  154. 2, /* CP102U */
  155. 8, /* CP118EL */
  156. 8, /* CP168EL */
  157. 4 /* CP104EL */
  158. };
  159. #define UART_TYPE_NUM 2
  160. static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
  161. MOXA_MUST_MU150_HWID,
  162. MOXA_MUST_MU860_HWID
  163. };
  164. /* This is only for PCI */
  165. #define UART_INFO_NUM 3
  166. struct mxpciuart_info {
  167. int type;
  168. int tx_fifo;
  169. int rx_fifo;
  170. int xmit_fifo_size;
  171. int rx_high_water;
  172. int rx_trigger;
  173. int rx_low_water;
  174. long max_baud;
  175. };
  176. static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
  177. {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
  178. {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
  179. {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
  180. };
  181. static struct pci_device_id mxser_pcibrds[] = {
  182. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
  183. .driver_data = MXSER_BOARD_C168_PCI },
  184. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
  185. .driver_data = MXSER_BOARD_C104_PCI },
  186. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
  187. .driver_data = MXSER_BOARD_CP132 },
  188. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
  189. .driver_data = MXSER_BOARD_CP114 },
  190. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
  191. .driver_data = MXSER_BOARD_CT114 },
  192. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
  193. .driver_data = MXSER_BOARD_CP102 },
  194. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
  195. .driver_data = MXSER_BOARD_CP104U },
  196. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
  197. .driver_data = MXSER_BOARD_CP168U },
  198. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
  199. .driver_data = MXSER_BOARD_CP132U },
  200. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
  201. .driver_data = MXSER_BOARD_CP134U },
  202. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
  203. .driver_data = MXSER_BOARD_CP104JU },
  204. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
  205. .driver_data = MXSER_BOARD_RC7000 },
  206. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
  207. .driver_data = MXSER_BOARD_CP118U },
  208. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
  209. .driver_data = MXSER_BOARD_CP102UL },
  210. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
  211. .driver_data = MXSER_BOARD_CP102U },
  212. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
  213. .driver_data = MXSER_BOARD_CP118EL },
  214. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
  215. .driver_data = MXSER_BOARD_CP168EL },
  216. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
  217. .driver_data = MXSER_BOARD_CP104EL },
  218. { }
  219. };
  220. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  221. static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
  222. static int ttymajor = MXSERMAJOR;
  223. static int calloutmajor = MXSERCUMAJOR;
  224. static int verbose = 0;
  225. /* Variables for insmod */
  226. MODULE_AUTHOR("Casper Yang");
  227. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  228. module_param_array(ioaddr, int, NULL, 0);
  229. module_param(ttymajor, int, 0);
  230. module_param(verbose, bool, 0);
  231. MODULE_LICENSE("GPL");
  232. struct mxser_log {
  233. int tick;
  234. unsigned long rxcnt[MXSER_PORTS];
  235. unsigned long txcnt[MXSER_PORTS];
  236. };
  237. struct mxser_mon {
  238. unsigned long rxcnt;
  239. unsigned long txcnt;
  240. unsigned long up_rxcnt;
  241. unsigned long up_txcnt;
  242. int modem_status;
  243. unsigned char hold_reason;
  244. };
  245. struct mxser_mon_ext {
  246. unsigned long rx_cnt[32];
  247. unsigned long tx_cnt[32];
  248. unsigned long up_rxcnt[32];
  249. unsigned long up_txcnt[32];
  250. int modem_status[32];
  251. long baudrate[32];
  252. int databits[32];
  253. int stopbits[32];
  254. int parity[32];
  255. int flowctrl[32];
  256. int fifo[32];
  257. int iftype[32];
  258. };
  259. struct mxser_board;
  260. struct mxser_port {
  261. struct mxser_board *board;
  262. struct tty_struct *tty;
  263. unsigned long ioaddr;
  264. unsigned long opmode_ioaddr;
  265. int max_baud;
  266. int rx_high_water;
  267. int rx_trigger; /* Rx fifo trigger level */
  268. int rx_low_water;
  269. int baud_base; /* max. speed */
  270. long realbaud;
  271. int type; /* UART type */
  272. int flags; /* defined in tty.h */
  273. long session; /* Session of opening process */
  274. long pgrp; /* pgrp of opening process */
  275. int x_char; /* xon/xoff character */
  276. int IER; /* Interrupt Enable Register */
  277. int MCR; /* Modem control register */
  278. unsigned char stop_rx;
  279. unsigned char ldisc_stop_rx;
  280. int custom_divisor;
  281. int close_delay;
  282. unsigned short closing_wait;
  283. unsigned char err_shadow;
  284. unsigned long event;
  285. int count; /* # of fd on device */
  286. int blocked_open; /* # of blocked opens */
  287. struct async_icount icount; /* kernel counters for 4 input interrupts */
  288. int timeout;
  289. int read_status_mask;
  290. int ignore_status_mask;
  291. int xmit_fifo_size;
  292. unsigned char *xmit_buf;
  293. int xmit_head;
  294. int xmit_tail;
  295. int xmit_cnt;
  296. struct termios normal_termios;
  297. struct termios callout_termios;
  298. struct mxser_mon mon_data;
  299. spinlock_t slock;
  300. struct work_struct tqueue;
  301. wait_queue_head_t open_wait;
  302. wait_queue_head_t close_wait;
  303. wait_queue_head_t delta_msr_wait;
  304. };
  305. struct mxser_board {
  306. struct pci_dev *pdev; /* temporary (until pci probing) */
  307. int irq;
  308. int board_type;
  309. unsigned int nports;
  310. unsigned long vector;
  311. unsigned long vector_mask;
  312. int chip_flag;
  313. int uart_type;
  314. struct mxser_port ports[MXSER_PORTS_PER_BOARD];
  315. };
  316. struct mxser_mstatus {
  317. tcflag_t cflag;
  318. int cts;
  319. int dsr;
  320. int ri;
  321. int dcd;
  322. };
  323. static struct mxser_mstatus GMStatus[MXSER_PORTS];
  324. static int mxserBoardCAP[MXSER_BOARDS] = {
  325. 0, 0, 0, 0
  326. /* 0x180, 0x280, 0x200, 0x320 */
  327. };
  328. static struct mxser_board mxser_boards[MXSER_BOARDS];
  329. static struct tty_driver *mxvar_sdriver;
  330. static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
  331. static struct termios *mxvar_termios[MXSER_PORTS + 1];
  332. static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
  333. static struct mxser_log mxvar_log;
  334. static int mxvar_diagflag;
  335. static unsigned char mxser_msr[MXSER_PORTS + 1];
  336. static struct mxser_mon_ext mon_data_ext;
  337. static int mxser_set_baud_method[MXSER_PORTS + 1];
  338. static spinlock_t gm_lock;
  339. /*
  340. * static functions:
  341. */
  342. static int mxser_init(void);
  343. /* static void mxser_poll(unsigned long); */
  344. static int mxser_get_ISA_conf(int, struct mxser_board *);
  345. static void mxser_do_softint(void *);
  346. static int mxser_open(struct tty_struct *, struct file *);
  347. static void mxser_close(struct tty_struct *, struct file *);
  348. static int mxser_write(struct tty_struct *, const unsigned char *, int);
  349. static int mxser_write_room(struct tty_struct *);
  350. static void mxser_flush_buffer(struct tty_struct *);
  351. static int mxser_chars_in_buffer(struct tty_struct *);
  352. static void mxser_flush_chars(struct tty_struct *);
  353. static void mxser_put_char(struct tty_struct *, unsigned char);
  354. static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
  355. static int mxser_ioctl_special(unsigned int, void __user *);
  356. static void mxser_throttle(struct tty_struct *);
  357. static void mxser_unthrottle(struct tty_struct *);
  358. static void mxser_set_termios(struct tty_struct *, struct termios *);
  359. static void mxser_stop(struct tty_struct *);
  360. static void mxser_start(struct tty_struct *);
  361. static void mxser_hangup(struct tty_struct *);
  362. static void mxser_rs_break(struct tty_struct *, int);
  363. static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
  364. static void mxser_receive_chars(struct mxser_port *, int *);
  365. static void mxser_transmit_chars(struct mxser_port *);
  366. static void mxser_check_modem_status(struct mxser_port *, int);
  367. static int mxser_block_til_ready(struct tty_struct *, struct file *,
  368. struct mxser_port *);
  369. static int mxser_startup(struct mxser_port *);
  370. static void mxser_shutdown(struct mxser_port *);
  371. static int mxser_change_speed(struct mxser_port *, struct termios *);
  372. static int mxser_get_serial_info(struct mxser_port *,
  373. struct serial_struct __user *);
  374. static int mxser_set_serial_info(struct mxser_port *,
  375. struct serial_struct __user *);
  376. static int mxser_get_lsr_info(struct mxser_port *, unsigned int __user *);
  377. static void mxser_send_break(struct mxser_port *, int);
  378. static int mxser_tiocmget(struct tty_struct *, struct file *);
  379. static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int,
  380. unsigned int);
  381. static int mxser_set_baud(struct mxser_port *, long);
  382. static void mxser_wait_until_sent(struct tty_struct *, int);
  383. static void mxser_startrx(struct tty_struct *);
  384. static void mxser_stoprx(struct tty_struct *);
  385. static int CheckIsMoxaMust(int io)
  386. {
  387. u8 oldmcr, hwid;
  388. int i;
  389. outb(0, io + UART_LCR);
  390. DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
  391. oldmcr = inb(io + UART_MCR);
  392. outb(0, io + UART_MCR);
  393. SET_MOXA_MUST_XON1_VALUE(io, 0x11);
  394. if ((hwid = inb(io + UART_MCR)) != 0) {
  395. outb(oldmcr, io + UART_MCR);
  396. return MOXA_OTHER_UART;
  397. }
  398. GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
  399. for (i = 0; i < UART_TYPE_NUM; i++) {
  400. if (hwid == Gmoxa_uart_id[i])
  401. return (int)hwid;
  402. }
  403. return MOXA_OTHER_UART;
  404. }
  405. /* above is modified by Victor Yu. 08-15-2002 */
  406. static const struct tty_operations mxser_ops = {
  407. .open = mxser_open,
  408. .close = mxser_close,
  409. .write = mxser_write,
  410. .put_char = mxser_put_char,
  411. .flush_chars = mxser_flush_chars,
  412. .write_room = mxser_write_room,
  413. .chars_in_buffer = mxser_chars_in_buffer,
  414. .flush_buffer = mxser_flush_buffer,
  415. .ioctl = mxser_ioctl,
  416. .throttle = mxser_throttle,
  417. .unthrottle = mxser_unthrottle,
  418. .set_termios = mxser_set_termios,
  419. .stop = mxser_stop,
  420. .start = mxser_start,
  421. .hangup = mxser_hangup,
  422. .break_ctl = mxser_rs_break,
  423. .wait_until_sent = mxser_wait_until_sent,
  424. .tiocmget = mxser_tiocmget,
  425. .tiocmset = mxser_tiocmset,
  426. };
  427. /*
  428. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  429. */
  430. static int __init mxser_module_init(void)
  431. {
  432. int ret;
  433. if (verbose)
  434. printk(KERN_DEBUG "Loading module mxser ...\n");
  435. printk(KERN_INFO "This is mxser driver version 1.9.1 and needs TESTING."
  436. "If your are willing to test this driver, please report to "
  437. "jirislaby@gmail.com. Thanks.\n");
  438. ret = mxser_init();
  439. if (verbose)
  440. printk(KERN_DEBUG "Done.\n");
  441. return ret;
  442. }
  443. static void __exit mxser_module_exit(void)
  444. {
  445. int i, err;
  446. if (verbose)
  447. printk(KERN_DEBUG "Unloading module mxser ...\n");
  448. err = tty_unregister_driver(mxvar_sdriver);
  449. if (!err)
  450. put_tty_driver(mxvar_sdriver);
  451. else
  452. printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
  453. for (i = 0; i < MXSER_BOARDS; i++) {
  454. struct pci_dev *pdev;
  455. if (mxser_boards[i].board_type == -1)
  456. continue;
  457. else {
  458. pdev = mxser_boards[i].pdev;
  459. free_irq(mxser_boards[i].irq, &mxser_boards[i]);
  460. if (pdev != NULL) { /* PCI */
  461. pci_release_region(pdev, 2);
  462. pci_release_region(pdev, 3);
  463. pci_dev_put(pdev);
  464. } else {
  465. release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
  466. release_region(mxser_boards[i].vector, 1);
  467. }
  468. }
  469. }
  470. if (verbose)
  471. printk(KERN_DEBUG "Done.\n");
  472. }
  473. static void process_txrx_fifo(struct mxser_port *info)
  474. {
  475. int i;
  476. if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
  477. info->rx_trigger = 1;
  478. info->rx_high_water = 1;
  479. info->rx_low_water = 1;
  480. info->xmit_fifo_size = 1;
  481. } else
  482. for (i = 0; i < UART_INFO_NUM; i++)
  483. if (info->board->chip_flag == Gpci_uart_info[i].type) {
  484. info->rx_trigger = Gpci_uart_info[i].rx_trigger;
  485. info->rx_low_water = Gpci_uart_info[i].rx_low_water;
  486. info->rx_high_water = Gpci_uart_info[i].rx_high_water;
  487. info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
  488. break;
  489. }
  490. }
  491. static int __devinit mxser_initbrd(struct mxser_board *brd)
  492. {
  493. struct mxser_port *info;
  494. unsigned int i;
  495. int retval;
  496. /*if (verbose) */ {
  497. printk(" max. baud rate = %d bps.\n",
  498. brd->ports[0].max_baud);
  499. }
  500. for (i = 0; i < brd->nports; i++) {
  501. info = &brd->ports[i];
  502. info->board = brd;
  503. info->stop_rx = 0;
  504. info->ldisc_stop_rx = 0;
  505. /* Enhance mode enabled here */
  506. if (brd->chip_flag != MOXA_OTHER_UART)
  507. ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
  508. info->flags = ASYNC_SHARE_IRQ;
  509. info->type = brd->uart_type;
  510. process_txrx_fifo(info);
  511. info->custom_divisor = info->baud_base * 16;
  512. info->close_delay = 5 * HZ / 10;
  513. info->closing_wait = 30 * HZ;
  514. INIT_WORK(&info->tqueue, mxser_do_softint, info);
  515. info->normal_termios = mxvar_sdriver->init_termios;
  516. init_waitqueue_head(&info->open_wait);
  517. init_waitqueue_head(&info->close_wait);
  518. init_waitqueue_head(&info->delta_msr_wait);
  519. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  520. info->err_shadow = 0;
  521. spin_lock_init(&info->slock);
  522. /* before set INT ISR, disable all int */
  523. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  524. info->ioaddr + UART_IER);
  525. }
  526. /*
  527. * Allocate the IRQ if necessary
  528. */
  529. retval = request_irq(brd->irq, mxser_interrupt,
  530. (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
  531. IRQF_DISABLED, "mxser", brd);
  532. if (retval) {
  533. printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
  534. "conflict with another device.\n",
  535. mxser_brdname[brd->board_type - 1], brd->irq);
  536. return retval;
  537. }
  538. return 0;
  539. }
  540. static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
  541. struct pci_dev *pdev)
  542. {
  543. unsigned int i, j;
  544. unsigned long ioaddress;
  545. int retval;
  546. /* io address */
  547. brd->board_type = board_type;
  548. brd->nports = mxser_numports[board_type - 1];
  549. ioaddress = pci_resource_start(pdev, 2);
  550. retval = pci_request_region(pdev, 2, "mxser(IO)");
  551. if (retval)
  552. goto err;
  553. for (i = 0; i < brd->nports; i++)
  554. brd->ports[i].ioaddr = ioaddress + 8 * i;
  555. /* vector */
  556. ioaddress = pci_resource_start(pdev, 3);
  557. retval = pci_request_region(pdev, 3, "mxser(vector)");
  558. if (retval)
  559. goto err_relio;
  560. brd->vector = ioaddress;
  561. /* irq */
  562. brd->irq = pdev->irq;
  563. brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
  564. brd->uart_type = PORT_16550A;
  565. brd->vector_mask = 0;
  566. for (i = 0; i < brd->nports; i++) {
  567. for (j = 0; j < UART_INFO_NUM; j++) {
  568. if (Gpci_uart_info[j].type == brd->chip_flag) {
  569. brd->ports[i].max_baud =
  570. Gpci_uart_info[j].max_baud;
  571. /* exception....CP-102 */
  572. if (board_type == MXSER_BOARD_CP102)
  573. brd->ports[i].max_baud = 921600;
  574. break;
  575. }
  576. }
  577. }
  578. if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
  579. for (i = 0; i < brd->nports; i++) {
  580. if (i < 4)
  581. brd->ports[i].opmode_ioaddr = ioaddress + 4;
  582. else
  583. brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
  584. }
  585. outb(0, ioaddress + 4); /* default set to RS232 mode */
  586. outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
  587. }
  588. for (i = 0; i < brd->nports; i++) {
  589. brd->vector_mask |= (1 << i);
  590. brd->ports[i].baud_base = 921600;
  591. }
  592. return 0;
  593. err_relio:
  594. pci_release_region(pdev, 2);
  595. err:
  596. return retval;
  597. }
  598. static int __init mxser_init(void)
  599. {
  600. struct pci_dev *pdev = NULL;
  601. struct mxser_board *brd;
  602. unsigned int i, m;
  603. int retval, b, n;
  604. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  605. if (!mxvar_sdriver)
  606. return -ENOMEM;
  607. spin_lock_init(&gm_lock);
  608. for (i = 0; i < MXSER_BOARDS; i++)
  609. mxser_boards[i].board_type = -1;
  610. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
  611. MXSER_VERSION);
  612. /* Initialize the tty_driver structure */
  613. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  614. mxvar_sdriver->name = "ttyM";
  615. mxvar_sdriver->major = ttymajor;
  616. mxvar_sdriver->minor_start = 0;
  617. mxvar_sdriver->num = MXSER_PORTS + 1;
  618. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  619. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  620. mxvar_sdriver->init_termios = tty_std_termios;
  621. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  622. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
  623. tty_set_operations(mxvar_sdriver, &mxser_ops);
  624. mxvar_sdriver->ttys = mxvar_tty;
  625. mxvar_sdriver->termios = mxvar_termios;
  626. mxvar_sdriver->termios_locked = mxvar_termios_locked;
  627. mxvar_diagflag = 0;
  628. m = 0;
  629. /* Start finding ISA boards here */
  630. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  631. int cap;
  632. if (!(cap = mxserBoardCAP[b]))
  633. continue;
  634. brd = &mxser_boards[m];
  635. retval = mxser_get_ISA_conf(cap, brd);
  636. if (retval != 0)
  637. printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
  638. mxser_brdname[brd->board_type - 1], ioaddr[b]);
  639. if (retval <= 0) {
  640. if (retval == MXSER_ERR_IRQ)
  641. printk(KERN_ERR "Invalid interrupt number, "
  642. "board not configured\n");
  643. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  644. printk(KERN_ERR "Invalid interrupt number, "
  645. "board not configured\n");
  646. else if (retval == MXSER_ERR_VECTOR)
  647. printk(KERN_ERR "Invalid interrupt vector, "
  648. "board not configured\n");
  649. else if (retval == MXSER_ERR_IOADDR)
  650. printk(KERN_ERR "Invalid I/O address, "
  651. "board not configured\n");
  652. continue;
  653. }
  654. brd->pdev = NULL;
  655. /* mxser_initbrd will hook ISR. */
  656. if (mxser_initbrd(brd) < 0)
  657. continue;
  658. m++;
  659. }
  660. /* Start finding ISA boards from module arg */
  661. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  662. unsigned long cap;
  663. if (!(cap = ioaddr[b]))
  664. continue;
  665. brd = &mxser_boards[m];
  666. retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
  667. if (retval != 0)
  668. printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
  669. mxser_brdname[brd->board_type - 1], ioaddr[b]);
  670. if (retval <= 0) {
  671. if (retval == MXSER_ERR_IRQ)
  672. printk(KERN_ERR "Invalid interrupt number, "
  673. "board not configured\n");
  674. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  675. printk(KERN_ERR "Invalid interrupt number, "
  676. "board not configured\n");
  677. else if (retval == MXSER_ERR_VECTOR)
  678. printk(KERN_ERR "Invalid interrupt vector, "
  679. "board not configured\n");
  680. else if (retval == MXSER_ERR_IOADDR)
  681. printk(KERN_ERR "Invalid I/O address, "
  682. "board not configured\n");
  683. continue;
  684. }
  685. brd->pdev = NULL;
  686. /* mxser_initbrd will hook ISR. */
  687. if (mxser_initbrd(brd) < 0)
  688. continue;
  689. m++;
  690. }
  691. /* start finding PCI board here */
  692. n = ARRAY_SIZE(mxser_pcibrds) - 1;
  693. b = 0;
  694. while (b < n) {
  695. pdev = pci_get_device(mxser_pcibrds[b].vendor,
  696. mxser_pcibrds[b].device, pdev);
  697. if (pdev == NULL) {
  698. b++;
  699. continue;
  700. }
  701. printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
  702. mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
  703. pdev->bus->number, PCI_SLOT(pdev->devfn));
  704. if (m >= MXSER_BOARDS)
  705. printk(KERN_ERR
  706. "Too many Smartio/Industio family boards find "
  707. "(maximum %d), board not configured\n",
  708. MXSER_BOARDS);
  709. else {
  710. if (pci_enable_device(pdev)) {
  711. printk(KERN_ERR "Moxa SmartI/O PCI enable "
  712. "fail !\n");
  713. continue;
  714. }
  715. brd = &mxser_boards[m];
  716. brd->pdev = pdev;
  717. retval = mxser_get_PCI_conf(
  718. (int)mxser_pcibrds[b].driver_data,
  719. brd, pdev);
  720. if (retval < 0) {
  721. if (retval == MXSER_ERR_IRQ)
  722. printk(KERN_ERR
  723. "Invalid interrupt number, "
  724. "board not configured\n");
  725. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  726. printk(KERN_ERR
  727. "Invalid interrupt number, "
  728. "board not configured\n");
  729. else if (retval == MXSER_ERR_VECTOR)
  730. printk(KERN_ERR
  731. "Invalid interrupt vector, "
  732. "board not configured\n");
  733. else if (retval == MXSER_ERR_IOADDR)
  734. printk(KERN_ERR
  735. "Invalid I/O address, "
  736. "board not configured\n");
  737. continue;
  738. }
  739. /* mxser_initbrd will hook ISR. */
  740. if (mxser_initbrd(brd) < 0)
  741. continue;
  742. m++;
  743. /* Keep an extra reference if we succeeded. It will
  744. be returned at unload time */
  745. pci_dev_get(pdev);
  746. }
  747. }
  748. retval = tty_register_driver(mxvar_sdriver);
  749. if (retval) {
  750. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
  751. " driver !\n");
  752. put_tty_driver(mxvar_sdriver);
  753. for (i = 0; i < MXSER_BOARDS; i++) {
  754. if (mxser_boards[i].board_type == -1)
  755. continue;
  756. else {
  757. free_irq(mxser_boards[i].irq, &mxser_boards[i]);
  758. /* todo: release io, vector */
  759. }
  760. }
  761. return retval;
  762. }
  763. return 0;
  764. }
  765. static void mxser_do_softint(void *private_)
  766. {
  767. struct mxser_port *info = private_;
  768. struct tty_struct *tty;
  769. tty = info->tty;
  770. if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
  771. tty_wakeup(tty);
  772. if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
  773. tty_hangup(tty);
  774. }
  775. static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
  776. {
  777. unsigned char status = 0;
  778. status = inb(baseaddr + UART_MSR);
  779. mxser_msr[port] &= 0x0F;
  780. mxser_msr[port] |= status;
  781. status = mxser_msr[port];
  782. if (mode)
  783. mxser_msr[port] = 0;
  784. return status;
  785. }
  786. /*
  787. * This routine is called whenever a serial port is opened. It
  788. * enables interrupts for a serial port, linking in its async structure into
  789. * the IRQ chain. It also performs the serial-specific
  790. * initialization for the tty structure.
  791. */
  792. static int mxser_open(struct tty_struct *tty, struct file *filp)
  793. {
  794. struct mxser_port *info;
  795. int retval, line;
  796. /* initialize driver_data in case something fails */
  797. tty->driver_data = NULL;
  798. line = tty->index;
  799. if (line == MXSER_PORTS)
  800. return 0;
  801. if (line < 0 || line > MXSER_PORTS)
  802. return -ENODEV;
  803. info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
  804. if (!info->ioaddr)
  805. return -ENODEV;
  806. tty->driver_data = info;
  807. info->tty = tty;
  808. /*
  809. * Start up serial port
  810. */
  811. info->count++;
  812. retval = mxser_startup(info);
  813. if (retval)
  814. return retval;
  815. retval = mxser_block_til_ready(tty, filp, info);
  816. if (retval)
  817. return retval;
  818. if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
  819. if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
  820. *tty->termios = info->normal_termios;
  821. else
  822. *tty->termios = info->callout_termios;
  823. mxser_change_speed(info, NULL);
  824. }
  825. info->session = process_session(current);
  826. info->pgrp = process_group(current);
  827. /*
  828. status = mxser_get_msr(info->base, 0, info->port);
  829. mxser_check_modem_status(info, status);
  830. */
  831. /* unmark here for very high baud rate (ex. 921600 bps) used */
  832. tty->low_latency = 1;
  833. return 0;
  834. }
  835. /*
  836. * This routine is called when the serial port gets closed. First, we
  837. * wait for the last remaining data to be sent. Then, we unlink its
  838. * async structure from the interrupt chain if necessary, and we free
  839. * that IRQ if nothing is left in the chain.
  840. */
  841. static void mxser_close(struct tty_struct *tty, struct file *filp)
  842. {
  843. struct mxser_port *info = tty->driver_data;
  844. unsigned long timeout;
  845. unsigned long flags;
  846. struct tty_ldisc *ld;
  847. if (tty->index == MXSER_PORTS)
  848. return;
  849. if (!info)
  850. return;
  851. spin_lock_irqsave(&info->slock, flags);
  852. if (tty_hung_up_p(filp)) {
  853. spin_unlock_irqrestore(&info->slock, flags);
  854. return;
  855. }
  856. if ((tty->count == 1) && (info->count != 1)) {
  857. /*
  858. * Uh, oh. tty->count is 1, which means that the tty
  859. * structure will be freed. Info->count should always
  860. * be one in these conditions. If it's greater than
  861. * one, we've got real problems, since it means the
  862. * serial port won't be shutdown.
  863. */
  864. printk(KERN_ERR "mxser_close: bad serial port count; "
  865. "tty->count is 1, info->count is %d\n", info->count);
  866. info->count = 1;
  867. }
  868. if (--info->count < 0) {
  869. printk(KERN_ERR "mxser_close: bad serial port count for "
  870. "ttys%d: %d\n", tty->index, info->count);
  871. info->count = 0;
  872. }
  873. if (info->count) {
  874. spin_unlock_irqrestore(&info->slock, flags);
  875. return;
  876. }
  877. info->flags |= ASYNC_CLOSING;
  878. spin_unlock_irqrestore(&info->slock, flags);
  879. /*
  880. * Save the termios structure, since this port may have
  881. * separate termios for callout and dialin.
  882. */
  883. if (info->flags & ASYNC_NORMAL_ACTIVE)
  884. info->normal_termios = *tty->termios;
  885. /*
  886. * Now we wait for the transmit buffer to clear; and we notify
  887. * the line discipline to only process XON/XOFF characters.
  888. */
  889. tty->closing = 1;
  890. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  891. tty_wait_until_sent(tty, info->closing_wait);
  892. /*
  893. * At this point we stop accepting input. To do this, we
  894. * disable the receive line status interrupts, and tell the
  895. * interrupt driver to stop checking the data ready bit in the
  896. * line status register.
  897. */
  898. info->IER &= ~UART_IER_RLSI;
  899. if (info->board->chip_flag)
  900. info->IER &= ~MOXA_MUST_RECV_ISR;
  901. /* by William
  902. info->read_status_mask &= ~UART_LSR_DR;
  903. */
  904. if (info->flags & ASYNC_INITIALIZED) {
  905. outb(info->IER, info->ioaddr + UART_IER);
  906. /*
  907. * Before we drop DTR, make sure the UART transmitter
  908. * has completely drained; this is especially
  909. * important if there is a transmit FIFO!
  910. */
  911. timeout = jiffies + HZ;
  912. while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
  913. schedule_timeout_interruptible(5);
  914. if (time_after(jiffies, timeout))
  915. break;
  916. }
  917. }
  918. mxser_shutdown(info);
  919. if (tty->driver->flush_buffer)
  920. tty->driver->flush_buffer(tty);
  921. ld = tty_ldisc_ref(tty);
  922. if (ld) {
  923. if (ld->flush_buffer)
  924. ld->flush_buffer(tty);
  925. tty_ldisc_deref(ld);
  926. }
  927. tty->closing = 0;
  928. info->event = 0;
  929. info->tty = NULL;
  930. if (info->blocked_open) {
  931. if (info->close_delay)
  932. schedule_timeout_interruptible(info->close_delay);
  933. wake_up_interruptible(&info->open_wait);
  934. }
  935. info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  936. wake_up_interruptible(&info->close_wait);
  937. }
  938. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  939. {
  940. int c, total = 0;
  941. struct mxser_port *info = tty->driver_data;
  942. unsigned long flags;
  943. if (!info->xmit_buf)
  944. return 0;
  945. while (1) {
  946. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  947. SERIAL_XMIT_SIZE - info->xmit_head));
  948. if (c <= 0)
  949. break;
  950. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  951. spin_lock_irqsave(&info->slock, flags);
  952. info->xmit_head = (info->xmit_head + c) &
  953. (SERIAL_XMIT_SIZE - 1);
  954. info->xmit_cnt += c;
  955. spin_unlock_irqrestore(&info->slock, flags);
  956. buf += c;
  957. count -= c;
  958. total += c;
  959. }
  960. if (info->xmit_cnt && !tty->stopped
  961. /*&& !(info->IER & UART_IER_THRI)*/) {
  962. if (!tty->hw_stopped ||
  963. (info->type == PORT_16550A) ||
  964. (info->board->chip_flag)) {
  965. spin_lock_irqsave(&info->slock, flags);
  966. outb(info->IER & ~UART_IER_THRI, info->ioaddr +
  967. UART_IER);
  968. info->IER |= UART_IER_THRI;
  969. outb(info->IER, info->ioaddr + UART_IER);
  970. spin_unlock_irqrestore(&info->slock, flags);
  971. }
  972. }
  973. return total;
  974. }
  975. static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
  976. {
  977. struct mxser_port *info = tty->driver_data;
  978. unsigned long flags;
  979. if (!info->xmit_buf)
  980. return;
  981. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  982. return;
  983. spin_lock_irqsave(&info->slock, flags);
  984. info->xmit_buf[info->xmit_head++] = ch;
  985. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  986. info->xmit_cnt++;
  987. spin_unlock_irqrestore(&info->slock, flags);
  988. if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
  989. if (!tty->hw_stopped ||
  990. (info->type == PORT_16550A) ||
  991. info->board->chip_flag) {
  992. spin_lock_irqsave(&info->slock, flags);
  993. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  994. info->IER |= UART_IER_THRI;
  995. outb(info->IER, info->ioaddr + UART_IER);
  996. spin_unlock_irqrestore(&info->slock, flags);
  997. }
  998. }
  999. }
  1000. static void mxser_flush_chars(struct tty_struct *tty)
  1001. {
  1002. struct mxser_port *info = tty->driver_data;
  1003. unsigned long flags;
  1004. if (info->xmit_cnt <= 0 ||
  1005. tty->stopped ||
  1006. !info->xmit_buf ||
  1007. (tty->hw_stopped &&
  1008. (info->type != PORT_16550A) &&
  1009. (!info->board->chip_flag)
  1010. ))
  1011. return;
  1012. spin_lock_irqsave(&info->slock, flags);
  1013. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1014. info->IER |= UART_IER_THRI;
  1015. outb(info->IER, info->ioaddr + UART_IER);
  1016. spin_unlock_irqrestore(&info->slock, flags);
  1017. }
  1018. static int mxser_write_room(struct tty_struct *tty)
  1019. {
  1020. struct mxser_port *info = tty->driver_data;
  1021. int ret;
  1022. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1023. if (ret < 0)
  1024. ret = 0;
  1025. return ret;
  1026. }
  1027. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1028. {
  1029. struct mxser_port *info = tty->driver_data;
  1030. int len = info->xmit_cnt;
  1031. if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
  1032. len++;
  1033. return len;
  1034. }
  1035. static void mxser_flush_buffer(struct tty_struct *tty)
  1036. {
  1037. struct mxser_port *info = tty->driver_data;
  1038. char fcr;
  1039. unsigned long flags;
  1040. spin_lock_irqsave(&info->slock, flags);
  1041. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1042. /* below added by shinhay */
  1043. fcr = inb(info->ioaddr + UART_FCR);
  1044. outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  1045. info->ioaddr + UART_FCR);
  1046. outb(fcr, info->ioaddr + UART_FCR);
  1047. spin_unlock_irqrestore(&info->slock, flags);
  1048. /* above added by shinhay */
  1049. wake_up_interruptible(&tty->write_wait);
  1050. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
  1051. (tty->ldisc.write_wakeup) (tty);
  1052. }
  1053. static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  1054. {
  1055. struct mxser_port *info = tty->driver_data;
  1056. int retval;
  1057. struct async_icount cprev, cnow; /* kernel counter temps */
  1058. struct serial_icounter_struct __user *p_cuser;
  1059. unsigned long templ;
  1060. unsigned long flags;
  1061. void __user *argp = (void __user *)arg;
  1062. if (tty->index == MXSER_PORTS)
  1063. return mxser_ioctl_special(cmd, argp);
  1064. /* following add by Victor Yu. 01-05-2004 */
  1065. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1066. int p;
  1067. unsigned long opmode;
  1068. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1069. int shiftbit;
  1070. unsigned char val, mask;
  1071. p = tty->index % 4;
  1072. if (cmd == MOXA_SET_OP_MODE) {
  1073. if (get_user(opmode, (int __user *) argp))
  1074. return -EFAULT;
  1075. if (opmode != RS232_MODE &&
  1076. opmode != RS485_2WIRE_MODE &&
  1077. opmode != RS422_MODE &&
  1078. opmode != RS485_4WIRE_MODE)
  1079. return -EFAULT;
  1080. mask = ModeMask[p];
  1081. shiftbit = p * 2;
  1082. val = inb(info->opmode_ioaddr);
  1083. val &= mask;
  1084. val |= (opmode << shiftbit);
  1085. outb(val, info->opmode_ioaddr);
  1086. } else {
  1087. shiftbit = p * 2;
  1088. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1089. opmode &= OP_MODE_MASK;
  1090. if (copy_to_user(argp, &opmode, sizeof(int)))
  1091. return -EFAULT;
  1092. }
  1093. return 0;
  1094. }
  1095. /* above add by Victor Yu. 01-05-2004 */
  1096. if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1097. if (tty->flags & (1 << TTY_IO_ERROR))
  1098. return -EIO;
  1099. }
  1100. switch (cmd) {
  1101. case TCSBRK: /* SVID version: non-zero arg --> no break */
  1102. retval = tty_check_change(tty);
  1103. if (retval)
  1104. return retval;
  1105. tty_wait_until_sent(tty, 0);
  1106. if (!arg)
  1107. mxser_send_break(info, HZ / 4); /* 1/4 second */
  1108. return 0;
  1109. case TCSBRKP: /* support for POSIX tcsendbreak() */
  1110. retval = tty_check_change(tty);
  1111. if (retval)
  1112. return retval;
  1113. tty_wait_until_sent(tty, 0);
  1114. mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
  1115. return 0;
  1116. case TIOCGSOFTCAR:
  1117. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
  1118. case TIOCSSOFTCAR:
  1119. if (get_user(templ, (unsigned long __user *) argp))
  1120. return -EFAULT;
  1121. arg = templ;
  1122. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
  1123. return 0;
  1124. case TIOCGSERIAL:
  1125. return mxser_get_serial_info(info, argp);
  1126. case TIOCSSERIAL:
  1127. return mxser_set_serial_info(info, argp);
  1128. case TIOCSERGETLSR: /* Get line status register */
  1129. return mxser_get_lsr_info(info, argp);
  1130. /*
  1131. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1132. * - mask passed in arg for lines of interest
  1133. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1134. * Caller should use TIOCGICOUNT to see which one it was
  1135. */
  1136. case TIOCMIWAIT: {
  1137. DECLARE_WAITQUEUE(wait, current);
  1138. int ret;
  1139. spin_lock_irqsave(&info->slock, flags);
  1140. cprev = info->icount; /* note the counters on entry */
  1141. spin_unlock_irqrestore(&info->slock, flags);
  1142. add_wait_queue(&info->delta_msr_wait, &wait);
  1143. while (1) {
  1144. spin_lock_irqsave(&info->slock, flags);
  1145. cnow = info->icount; /* atomic copy */
  1146. spin_unlock_irqrestore(&info->slock, flags);
  1147. set_current_state(TASK_INTERRUPTIBLE);
  1148. if (((arg & TIOCM_RNG) &&
  1149. (cnow.rng != cprev.rng)) ||
  1150. ((arg & TIOCM_DSR) &&
  1151. (cnow.dsr != cprev.dsr)) ||
  1152. ((arg & TIOCM_CD) &&
  1153. (cnow.dcd != cprev.dcd)) ||
  1154. ((arg & TIOCM_CTS) &&
  1155. (cnow.cts != cprev.cts))) {
  1156. ret = 0;
  1157. break;
  1158. }
  1159. /* see if a signal did it */
  1160. if (signal_pending(current)) {
  1161. ret = -ERESTARTSYS;
  1162. break;
  1163. }
  1164. cprev = cnow;
  1165. }
  1166. current->state = TASK_RUNNING;
  1167. remove_wait_queue(&info->delta_msr_wait, &wait);
  1168. break;
  1169. }
  1170. /* NOTREACHED */
  1171. /*
  1172. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1173. * Return: write counters to the user passed counter struct
  1174. * NB: both 1->0 and 0->1 transitions are counted except for
  1175. * RI where only 0->1 is counted.
  1176. */
  1177. case TIOCGICOUNT:
  1178. spin_lock_irqsave(&info->slock, flags);
  1179. cnow = info->icount;
  1180. spin_unlock_irqrestore(&info->slock, flags);
  1181. p_cuser = argp;
  1182. /* modified by casper 1/11/2000 */
  1183. if (put_user(cnow.frame, &p_cuser->frame))
  1184. return -EFAULT;
  1185. if (put_user(cnow.brk, &p_cuser->brk))
  1186. return -EFAULT;
  1187. if (put_user(cnow.overrun, &p_cuser->overrun))
  1188. return -EFAULT;
  1189. if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  1190. return -EFAULT;
  1191. if (put_user(cnow.parity, &p_cuser->parity))
  1192. return -EFAULT;
  1193. if (put_user(cnow.rx, &p_cuser->rx))
  1194. return -EFAULT;
  1195. if (put_user(cnow.tx, &p_cuser->tx))
  1196. return -EFAULT;
  1197. put_user(cnow.cts, &p_cuser->cts);
  1198. put_user(cnow.dsr, &p_cuser->dsr);
  1199. put_user(cnow.rng, &p_cuser->rng);
  1200. put_user(cnow.dcd, &p_cuser->dcd);
  1201. return 0;
  1202. case MOXA_HighSpeedOn:
  1203. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
  1204. case MOXA_SDS_RSTICOUNTER: {
  1205. info->mon_data.rxcnt = 0;
  1206. info->mon_data.txcnt = 0;
  1207. return 0;
  1208. }
  1209. /* (above) added by James. */
  1210. case MOXA_ASPP_SETBAUD:{
  1211. long baud;
  1212. if (get_user(baud, (long __user *)argp))
  1213. return -EFAULT;
  1214. mxser_set_baud(info, baud);
  1215. return 0;
  1216. }
  1217. case MOXA_ASPP_GETBAUD:
  1218. if (copy_to_user(argp, &info->realbaud, sizeof(long)))
  1219. return -EFAULT;
  1220. return 0;
  1221. case MOXA_ASPP_OQUEUE:{
  1222. int len, lsr;
  1223. len = mxser_chars_in_buffer(tty);
  1224. lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
  1225. len += (lsr ? 0 : 1);
  1226. if (copy_to_user(argp, &len, sizeof(int)))
  1227. return -EFAULT;
  1228. return 0;
  1229. }
  1230. case MOXA_ASPP_MON: {
  1231. int mcr, status;
  1232. /* info->mon_data.ser_param = tty->termios->c_cflag; */
  1233. status = mxser_get_msr(info->ioaddr, 1, tty->index);
  1234. mxser_check_modem_status(info, status);
  1235. mcr = inb(info->ioaddr + UART_MCR);
  1236. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1237. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1238. else
  1239. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1240. if (mcr & MOXA_MUST_MCR_TX_XON)
  1241. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1242. else
  1243. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1244. if (info->tty->hw_stopped)
  1245. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1246. else
  1247. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1248. if (copy_to_user(argp, &info->mon_data,
  1249. sizeof(struct mxser_mon)))
  1250. return -EFAULT;
  1251. return 0;
  1252. }
  1253. case MOXA_ASPP_LSTATUS: {
  1254. if (copy_to_user(argp, &info->err_shadow,
  1255. sizeof(unsigned char)))
  1256. return -EFAULT;
  1257. info->err_shadow = 0;
  1258. return 0;
  1259. }
  1260. case MOXA_SET_BAUD_METHOD: {
  1261. int method;
  1262. if (get_user(method, (int __user *)argp))
  1263. return -EFAULT;
  1264. mxser_set_baud_method[tty->index] = method;
  1265. if (copy_to_user(argp, &method, sizeof(int)))
  1266. return -EFAULT;
  1267. return 0;
  1268. }
  1269. default:
  1270. return -ENOIOCTLCMD;
  1271. }
  1272. return 0;
  1273. }
  1274. #ifndef CMSPAR
  1275. #define CMSPAR 010000000000
  1276. #endif
  1277. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1278. {
  1279. struct mxser_port *port;
  1280. int result, status;
  1281. unsigned int i, j;
  1282. switch (cmd) {
  1283. case MOXA_GET_CONF:
  1284. /* if (copy_to_user(argp, mxsercfg,
  1285. sizeof(struct mxser_hwconf) * 4))
  1286. return -EFAULT;
  1287. return 0;*/
  1288. return -ENXIO;
  1289. case MOXA_GET_MAJOR:
  1290. if (copy_to_user(argp, &ttymajor, sizeof(int)))
  1291. return -EFAULT;
  1292. return 0;
  1293. case MOXA_GET_CUMAJOR:
  1294. if (copy_to_user(argp, &calloutmajor, sizeof(int)))
  1295. return -EFAULT;
  1296. return 0;
  1297. case MOXA_CHKPORTENABLE:
  1298. result = 0;
  1299. for (i = 0; i < MXSER_BOARDS; i++)
  1300. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
  1301. if (mxser_boards[i].ports[j].ioaddr)
  1302. result |= (1 << i);
  1303. return put_user(result, (unsigned long __user *)argp);
  1304. case MOXA_GETDATACOUNT:
  1305. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1306. return -EFAULT;
  1307. return 0;
  1308. case MOXA_GETMSTATUS:
  1309. for (i = 0; i < MXSER_BOARDS; i++)
  1310. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1311. port = &mxser_boards[i].ports[j];
  1312. GMStatus[i].ri = 0;
  1313. if (!port->ioaddr) {
  1314. GMStatus[i].dcd = 0;
  1315. GMStatus[i].dsr = 0;
  1316. GMStatus[i].cts = 0;
  1317. continue;
  1318. }
  1319. if (!port->tty || !port->tty->termios)
  1320. GMStatus[i].cflag =
  1321. port->normal_termios.c_cflag;
  1322. else
  1323. GMStatus[i].cflag =
  1324. port->tty->termios->c_cflag;
  1325. status = inb(port->ioaddr + UART_MSR);
  1326. if (status & 0x80 /*UART_MSR_DCD */ )
  1327. GMStatus[i].dcd = 1;
  1328. else
  1329. GMStatus[i].dcd = 0;
  1330. if (status & 0x20 /*UART_MSR_DSR */ )
  1331. GMStatus[i].dsr = 1;
  1332. else
  1333. GMStatus[i].dsr = 0;
  1334. if (status & 0x10 /*UART_MSR_CTS */ )
  1335. GMStatus[i].cts = 1;
  1336. else
  1337. GMStatus[i].cts = 0;
  1338. }
  1339. if (copy_to_user(argp, GMStatus,
  1340. sizeof(struct mxser_mstatus) * MXSER_PORTS))
  1341. return -EFAULT;
  1342. return 0;
  1343. case MOXA_ASPP_MON_EXT: {
  1344. int status, p, shiftbit;
  1345. unsigned long opmode;
  1346. unsigned cflag, iflag;
  1347. for (i = 0; i < MXSER_BOARDS; i++)
  1348. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1349. port = &mxser_boards[i].ports[j];
  1350. if (!port->ioaddr)
  1351. continue;
  1352. status = mxser_get_msr(port->ioaddr, 0, i);
  1353. /* mxser_check_modem_status(port, status); */
  1354. if (status & UART_MSR_TERI)
  1355. port->icount.rng++;
  1356. if (status & UART_MSR_DDSR)
  1357. port->icount.dsr++;
  1358. if (status & UART_MSR_DDCD)
  1359. port->icount.dcd++;
  1360. if (status & UART_MSR_DCTS)
  1361. port->icount.cts++;
  1362. port->mon_data.modem_status = status;
  1363. mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
  1364. mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
  1365. mon_data_ext.up_rxcnt[i] =
  1366. port->mon_data.up_rxcnt;
  1367. mon_data_ext.up_txcnt[i] =
  1368. port->mon_data.up_txcnt;
  1369. mon_data_ext.modem_status[i] =
  1370. port->mon_data.modem_status;
  1371. mon_data_ext.baudrate[i] = port->realbaud;
  1372. if (!port->tty || !port->tty->termios) {
  1373. cflag = port->normal_termios.c_cflag;
  1374. iflag = port->normal_termios.c_iflag;
  1375. } else {
  1376. cflag = port->tty->termios->c_cflag;
  1377. iflag = port->tty->termios->c_iflag;
  1378. }
  1379. mon_data_ext.databits[i] = cflag & CSIZE;
  1380. mon_data_ext.stopbits[i] = cflag & CSTOPB;
  1381. mon_data_ext.parity[i] =
  1382. cflag & (PARENB | PARODD | CMSPAR);
  1383. mon_data_ext.flowctrl[i] = 0x00;
  1384. if (cflag & CRTSCTS)
  1385. mon_data_ext.flowctrl[i] |= 0x03;
  1386. if (iflag & (IXON | IXOFF))
  1387. mon_data_ext.flowctrl[i] |= 0x0C;
  1388. if (port->type == PORT_16550A)
  1389. mon_data_ext.fifo[i] = 1;
  1390. else
  1391. mon_data_ext.fifo[i] = 0;
  1392. p = i % 4;
  1393. shiftbit = p * 2;
  1394. opmode = inb(port->opmode_ioaddr) >> shiftbit;
  1395. opmode &= OP_MODE_MASK;
  1396. mon_data_ext.iftype[i] = opmode;
  1397. }
  1398. if (copy_to_user(argp, &mon_data_ext,
  1399. sizeof(mon_data_ext)))
  1400. return -EFAULT;
  1401. return 0;
  1402. } default:
  1403. return -ENOIOCTLCMD;
  1404. }
  1405. return 0;
  1406. }
  1407. static void mxser_stoprx(struct tty_struct *tty)
  1408. {
  1409. struct mxser_port *info = tty->driver_data;
  1410. info->ldisc_stop_rx = 1;
  1411. if (I_IXOFF(tty)) {
  1412. /* following add by Victor Yu. 09-02-2002 */
  1413. if (info->board->chip_flag) {
  1414. info->IER &= ~MOXA_MUST_RECV_ISR;
  1415. outb(info->IER, info->ioaddr + UART_IER);
  1416. } else if (!(info->flags & ASYNC_CLOSING)) {
  1417. info->x_char = STOP_CHAR(tty);
  1418. outb(0, info->ioaddr + UART_IER);
  1419. info->IER |= UART_IER_THRI;
  1420. outb(info->IER, info->ioaddr + UART_IER);
  1421. }
  1422. }
  1423. if (info->tty->termios->c_cflag & CRTSCTS) {
  1424. info->MCR &= ~UART_MCR_RTS;
  1425. outb(info->MCR, info->ioaddr + UART_MCR);
  1426. }
  1427. }
  1428. static void mxser_startrx(struct tty_struct *tty)
  1429. {
  1430. struct mxser_port *info = tty->driver_data;
  1431. info->ldisc_stop_rx = 0;
  1432. if (I_IXOFF(tty)) {
  1433. if (info->x_char)
  1434. info->x_char = 0;
  1435. else {
  1436. /* following add by Victor Yu. 09-02-2002 */
  1437. if (info->board->chip_flag) {
  1438. info->IER |= MOXA_MUST_RECV_ISR;
  1439. outb(info->IER, info->ioaddr + UART_IER);
  1440. } else if (!(info->flags & ASYNC_CLOSING)) {
  1441. info->x_char = START_CHAR(tty);
  1442. outb(0, info->ioaddr + UART_IER);
  1443. info->IER |= UART_IER_THRI;
  1444. outb(info->IER, info->ioaddr + UART_IER);
  1445. }
  1446. }
  1447. }
  1448. if (info->tty->termios->c_cflag & CRTSCTS) {
  1449. info->MCR |= UART_MCR_RTS;
  1450. outb(info->MCR, info->ioaddr + UART_MCR);
  1451. }
  1452. }
  1453. /*
  1454. * This routine is called by the upper-layer tty layer to signal that
  1455. * incoming characters should be throttled.
  1456. */
  1457. static void mxser_throttle(struct tty_struct *tty)
  1458. {
  1459. mxser_stoprx(tty);
  1460. }
  1461. static void mxser_unthrottle(struct tty_struct *tty)
  1462. {
  1463. mxser_startrx(tty);
  1464. }
  1465. static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1466. {
  1467. struct mxser_port *info = tty->driver_data;
  1468. unsigned long flags;
  1469. if ((tty->termios->c_cflag != old_termios->c_cflag) ||
  1470. (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
  1471. mxser_change_speed(info, old_termios);
  1472. if ((old_termios->c_cflag & CRTSCTS) &&
  1473. !(tty->termios->c_cflag & CRTSCTS)) {
  1474. tty->hw_stopped = 0;
  1475. mxser_start(tty);
  1476. }
  1477. }
  1478. /* Handle sw stopped */
  1479. if ((old_termios->c_iflag & IXON) &&
  1480. !(tty->termios->c_iflag & IXON)) {
  1481. tty->stopped = 0;
  1482. /* following add by Victor Yu. 09-02-2002 */
  1483. if (info->board->chip_flag) {
  1484. spin_lock_irqsave(&info->slock, flags);
  1485. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  1486. spin_unlock_irqrestore(&info->slock, flags);
  1487. }
  1488. /* above add by Victor Yu. 09-02-2002 */
  1489. mxser_start(tty);
  1490. }
  1491. }
  1492. /*
  1493. * mxser_stop() and mxser_start()
  1494. *
  1495. * This routines are called before setting or resetting tty->stopped.
  1496. * They enable or disable transmitter interrupts, as necessary.
  1497. */
  1498. static void mxser_stop(struct tty_struct *tty)
  1499. {
  1500. struct mxser_port *info = tty->driver_data;
  1501. unsigned long flags;
  1502. spin_lock_irqsave(&info->slock, flags);
  1503. if (info->IER & UART_IER_THRI) {
  1504. info->IER &= ~UART_IER_THRI;
  1505. outb(info->IER, info->ioaddr + UART_IER);
  1506. }
  1507. spin_unlock_irqrestore(&info->slock, flags);
  1508. }
  1509. static void mxser_start(struct tty_struct *tty)
  1510. {
  1511. struct mxser_port *info = tty->driver_data;
  1512. unsigned long flags;
  1513. spin_lock_irqsave(&info->slock, flags);
  1514. if (info->xmit_cnt && info->xmit_buf
  1515. /* && !(info->IER & UART_IER_THRI) */) {
  1516. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1517. info->IER |= UART_IER_THRI;
  1518. outb(info->IER, info->ioaddr + UART_IER);
  1519. }
  1520. spin_unlock_irqrestore(&info->slock, flags);
  1521. }
  1522. /*
  1523. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1524. */
  1525. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1526. {
  1527. struct mxser_port *info = tty->driver_data;
  1528. unsigned long orig_jiffies, char_time;
  1529. int lsr;
  1530. if (info->type == PORT_UNKNOWN)
  1531. return;
  1532. if (info->xmit_fifo_size == 0)
  1533. return; /* Just in case.... */
  1534. orig_jiffies = jiffies;
  1535. /*
  1536. * Set the check interval to be 1/5 of the estimated time to
  1537. * send a single character, and make it at least 1. The check
  1538. * interval should also be less than the timeout.
  1539. *
  1540. * Note: we have to use pretty tight timings here to satisfy
  1541. * the NIST-PCTS.
  1542. */
  1543. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1544. char_time = char_time / 5;
  1545. if (char_time == 0)
  1546. char_time = 1;
  1547. if (timeout && timeout < char_time)
  1548. char_time = timeout;
  1549. /*
  1550. * If the transmitter hasn't cleared in twice the approximate
  1551. * amount of time to send the entire FIFO, it probably won't
  1552. * ever clear. This assumes the UART isn't doing flow
  1553. * control, which is currently the case. Hence, if it ever
  1554. * takes longer than info->timeout, this is probably due to a
  1555. * UART bug of some kind. So, we clamp the timeout parameter at
  1556. * 2*info->timeout.
  1557. */
  1558. if (!timeout || timeout > 2 * info->timeout)
  1559. timeout = 2 * info->timeout;
  1560. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1561. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
  1562. timeout, char_time);
  1563. printk("jiff=%lu...", jiffies);
  1564. #endif
  1565. while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
  1566. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1567. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1568. #endif
  1569. schedule_timeout_interruptible(char_time);
  1570. if (signal_pending(current))
  1571. break;
  1572. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1573. break;
  1574. }
  1575. set_current_state(TASK_RUNNING);
  1576. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1577. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1578. #endif
  1579. }
  1580. /*
  1581. * This routine is called by tty_hangup() when a hangup is signaled.
  1582. */
  1583. void mxser_hangup(struct tty_struct *tty)
  1584. {
  1585. struct mxser_port *info = tty->driver_data;
  1586. mxser_flush_buffer(tty);
  1587. mxser_shutdown(info);
  1588. info->event = 0;
  1589. info->count = 0;
  1590. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1591. info->tty = NULL;
  1592. wake_up_interruptible(&info->open_wait);
  1593. }
  1594. /* added by James 03-12-2004. */
  1595. /*
  1596. * mxser_rs_break() --- routine which turns the break handling on or off
  1597. */
  1598. static void mxser_rs_break(struct tty_struct *tty, int break_state)
  1599. {
  1600. struct mxser_port *info = tty->driver_data;
  1601. unsigned long flags;
  1602. spin_lock_irqsave(&info->slock, flags);
  1603. if (break_state == -1)
  1604. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1605. info->ioaddr + UART_LCR);
  1606. else
  1607. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1608. info->ioaddr + UART_LCR);
  1609. spin_unlock_irqrestore(&info->slock, flags);
  1610. }
  1611. /* (above) added by James. */
  1612. /*
  1613. * This is the serial driver's generic interrupt routine
  1614. */
  1615. static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1616. {
  1617. int status, iir, i;
  1618. struct mxser_board *brd = NULL;
  1619. struct mxser_port *port;
  1620. int max, irqbits, bits, msr;
  1621. int pass_counter = 0;
  1622. unsigned int int_cnt;
  1623. int handled = IRQ_NONE;
  1624. /* spin_lock(&gm_lock); */
  1625. for (i = 0; i < MXSER_BOARDS; i++)
  1626. if (dev_id == &mxser_boards[i]) {
  1627. brd = dev_id;
  1628. break;
  1629. }
  1630. if (i == MXSER_BOARDS)
  1631. goto irq_stop;
  1632. if (brd == NULL)
  1633. goto irq_stop;
  1634. max = mxser_numports[brd->board_type - 1];
  1635. while (1) {
  1636. irqbits = inb(brd->vector) & brd->vector_mask;
  1637. if (irqbits == brd->vector_mask)
  1638. break;
  1639. handled = IRQ_HANDLED;
  1640. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  1641. if (irqbits == brd->vector_mask)
  1642. break;
  1643. if (bits & irqbits)
  1644. continue;
  1645. port = &brd->ports[i];
  1646. int_cnt = 0;
  1647. do {
  1648. /* following add by Victor Yu. 09-13-2002 */
  1649. iir = inb(port->ioaddr + UART_IIR);
  1650. if (iir & UART_IIR_NO_INT)
  1651. break;
  1652. iir &= MOXA_MUST_IIR_MASK;
  1653. if (!port->tty) {
  1654. status = inb(port->ioaddr + UART_LSR);
  1655. outb(0x27, port->ioaddr + UART_FCR);
  1656. inb(port->ioaddr + UART_MSR);
  1657. break;
  1658. }
  1659. /* above add by Victor Yu. 09-13-2002 */
  1660. /* following add by Victor Yu. 09-02-2002 */
  1661. status = inb(port->ioaddr + UART_LSR);
  1662. if (status & UART_LSR_PE)
  1663. port->err_shadow |= NPPI_NOTIFY_PARITY;
  1664. if (status & UART_LSR_FE)
  1665. port->err_shadow |= NPPI_NOTIFY_FRAMING;
  1666. if (status & UART_LSR_OE)
  1667. port->err_shadow |=
  1668. NPPI_NOTIFY_HW_OVERRUN;
  1669. if (status & UART_LSR_BI)
  1670. port->err_shadow |= NPPI_NOTIFY_BREAK;
  1671. if (port->board->chip_flag) {
  1672. /*
  1673. if ( (status & 0x02) && !(status & 0x01) ) {
  1674. outb(port->ioaddr+UART_FCR, 0x23);
  1675. continue;
  1676. }
  1677. */
  1678. if (iir == MOXA_MUST_IIR_GDA ||
  1679. iir == MOXA_MUST_IIR_RDA ||
  1680. iir == MOXA_MUST_IIR_RTO ||
  1681. iir == MOXA_MUST_IIR_LSR)
  1682. mxser_receive_chars(port,
  1683. &status);
  1684. } else {
  1685. /* above add by Victor Yu. 09-02-2002 */
  1686. status &= port->read_status_mask;
  1687. if (status & UART_LSR_DR)
  1688. mxser_receive_chars(port,
  1689. &status);
  1690. }
  1691. msr = inb(port->ioaddr + UART_MSR);
  1692. if (msr & UART_MSR_ANY_DELTA)
  1693. mxser_check_modem_status(port, msr);
  1694. /* following add by Victor Yu. 09-13-2002 */
  1695. if (port->board->chip_flag) {
  1696. if (iir == 0x02 && (status &
  1697. UART_LSR_THRE))
  1698. mxser_transmit_chars(port);
  1699. } else {
  1700. /* above add by Victor Yu. 09-13-2002 */
  1701. if (status & UART_LSR_THRE)
  1702. mxser_transmit_chars(port);
  1703. }
  1704. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  1705. }
  1706. if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
  1707. break; /* Prevent infinite loops */
  1708. }
  1709. irq_stop:
  1710. /* spin_unlock(&gm_lock); */
  1711. return handled;
  1712. }
  1713. static void mxser_receive_chars(struct mxser_port *port, int *status)
  1714. {
  1715. struct tty_struct *tty = port->tty;
  1716. unsigned char ch, gdl;
  1717. int ignored = 0;
  1718. int cnt = 0;
  1719. int recv_room;
  1720. int max = 256;
  1721. unsigned long flags;
  1722. spin_lock_irqsave(&port->slock, flags);
  1723. recv_room = tty->receive_room;
  1724. if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
  1725. /* mxser_throttle(tty); */
  1726. mxser_stoprx(tty);
  1727. /* return; */
  1728. }
  1729. /* following add by Victor Yu. 09-02-2002 */
  1730. if (port->board->chip_flag != MOXA_OTHER_UART) {
  1731. if (*status & UART_LSR_SPECIAL)
  1732. goto intr_old;
  1733. /* following add by Victor Yu. 02-11-2004 */
  1734. if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
  1735. (*status & MOXA_MUST_LSR_RERR))
  1736. goto intr_old;
  1737. /* above add by Victor Yu. 02-14-2004 */
  1738. if (*status & MOXA_MUST_LSR_RERR)
  1739. goto intr_old;
  1740. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1741. /* add by Victor Yu. 02-11-2004 */
  1742. if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
  1743. gdl &= MOXA_MUST_GDL_MASK;
  1744. if (gdl >= recv_room) {
  1745. if (!port->ldisc_stop_rx) {
  1746. /* mxser_throttle(tty); */
  1747. mxser_stoprx(tty);
  1748. }
  1749. /* return; */
  1750. }
  1751. while (gdl--) {
  1752. ch = inb(port->ioaddr + UART_RX);
  1753. tty_insert_flip_char(tty, ch, 0);
  1754. cnt++;
  1755. }
  1756. goto end_intr;
  1757. }
  1758. intr_old:
  1759. /* above add by Victor Yu. 09-02-2002 */
  1760. do {
  1761. if (max-- < 0)
  1762. break;
  1763. ch = inb(port->ioaddr + UART_RX);
  1764. /* following add by Victor Yu. 09-02-2002 */
  1765. if (port->board->chip_flag && (*status & UART_LSR_OE)
  1766. /*&& !(*status&UART_LSR_DR) */)
  1767. outb(0x23, port->ioaddr + UART_FCR);
  1768. *status &= port->read_status_mask;
  1769. /* above add by Victor Yu. 09-02-2002 */
  1770. if (*status & port->ignore_status_mask) {
  1771. if (++ignored > 100)
  1772. break;
  1773. } else {
  1774. char flag = 0;
  1775. if (*status & UART_LSR_SPECIAL) {
  1776. if (*status & UART_LSR_BI) {
  1777. flag = TTY_BREAK;
  1778. /* added by casper 1/11/2000 */
  1779. port->icount.brk++;
  1780. if (port->flags & ASYNC_SAK)
  1781. do_SAK(tty);
  1782. } else if (*status & UART_LSR_PE) {
  1783. flag = TTY_PARITY;
  1784. /* added by casper 1/11/2000 */
  1785. port->icount.parity++;
  1786. } else if (*status & UART_LSR_FE) {
  1787. flag = TTY_FRAME;
  1788. /* added by casper 1/11/2000 */
  1789. port->icount.frame++;
  1790. } else if (*status & UART_LSR_OE) {
  1791. flag = TTY_OVERRUN;
  1792. /* added by casper 1/11/2000 */
  1793. port->icount.overrun++;
  1794. } else
  1795. flags = TTY_BREAK;
  1796. } else
  1797. flags = 0;
  1798. tty_insert_flip_char(tty, ch, flag);
  1799. cnt++;
  1800. if (cnt >= recv_room) {
  1801. if (!port->ldisc_stop_rx) {
  1802. /* mxser_throttle(tty); */
  1803. mxser_stoprx(tty);
  1804. }
  1805. break;
  1806. }
  1807. }
  1808. /* following add by Victor Yu. 09-02-2002 */
  1809. if (port->board->chip_flag)
  1810. break;
  1811. /* mask by Victor Yu. 09-02-2002
  1812. *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
  1813. */
  1814. /* following add by Victor Yu. 09-02-2002 */
  1815. *status = inb(port->ioaddr + UART_LSR);
  1816. /* above add by Victor Yu. 09-02-2002 */
  1817. } while (*status & UART_LSR_DR);
  1818. end_intr: /* add by Victor Yu. 09-02-2002 */
  1819. mxvar_log.rxcnt[port->tty->index] += cnt;
  1820. port->mon_data.rxcnt += cnt;
  1821. port->mon_data.up_rxcnt += cnt;
  1822. spin_unlock_irqrestore(&port->slock, flags);
  1823. tty_flip_buffer_push(tty);
  1824. }
  1825. static void mxser_transmit_chars(struct mxser_port *port)
  1826. {
  1827. int count, cnt;
  1828. unsigned long flags;
  1829. spin_lock_irqsave(&port->slock, flags);
  1830. if (port->x_char) {
  1831. outb(port->x_char, port->ioaddr + UART_TX);
  1832. port->x_char = 0;
  1833. mxvar_log.txcnt[port->tty->index]++;
  1834. port->mon_data.txcnt++;
  1835. port->mon_data.up_txcnt++;
  1836. /* added by casper 1/11/2000 */
  1837. port->icount.tx++;
  1838. goto unlock;
  1839. }
  1840. if (port->xmit_buf == 0)
  1841. goto unlock;
  1842. if (port->xmit_cnt == 0) {
  1843. if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
  1844. set_bit(MXSER_EVENT_TXLOW, &port->event);
  1845. schedule_work(&port->tqueue);
  1846. }
  1847. goto unlock;
  1848. }
  1849. if (port->tty->stopped || (port->tty->hw_stopped &&
  1850. (port->type != PORT_16550A) &&
  1851. (!port->board->chip_flag))) {
  1852. port->IER &= ~UART_IER_THRI;
  1853. outb(port->IER, port->ioaddr + UART_IER);
  1854. goto unlock;
  1855. }
  1856. cnt = port->xmit_cnt;
  1857. count = port->xmit_fifo_size;
  1858. do {
  1859. outb(port->xmit_buf[port->xmit_tail++],
  1860. port->ioaddr + UART_TX);
  1861. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  1862. if (--port->xmit_cnt <= 0)
  1863. break;
  1864. } while (--count > 0);
  1865. mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
  1866. /* added by James 03-12-2004. */
  1867. port->mon_data.txcnt += (cnt - port->xmit_cnt);
  1868. port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
  1869. /* added by casper 1/11/2000 */
  1870. port->icount.tx += (cnt - port->xmit_cnt);
  1871. if (port->xmit_cnt < WAKEUP_CHARS) {
  1872. set_bit(MXSER_EVENT_TXLOW, &port->event);
  1873. schedule_work(&port->tqueue);
  1874. }
  1875. if (port->xmit_cnt <= 0) {
  1876. port->IER &= ~UART_IER_THRI;
  1877. outb(port->IER, port->ioaddr + UART_IER);
  1878. }
  1879. unlock:
  1880. spin_unlock_irqrestore(&port->slock, flags);
  1881. }
  1882. static void mxser_check_modem_status(struct mxser_port *port, int status)
  1883. {
  1884. /* update input line counters */
  1885. if (status & UART_MSR_TERI)
  1886. port->icount.rng++;
  1887. if (status & UART_MSR_DDSR)
  1888. port->icount.dsr++;
  1889. if (status & UART_MSR_DDCD)
  1890. port->icount.dcd++;
  1891. if (status & UART_MSR_DCTS)
  1892. port->icount.cts++;
  1893. port->mon_data.modem_status = status;
  1894. wake_up_interruptible(&port->delta_msr_wait);
  1895. if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
  1896. if (status & UART_MSR_DCD)
  1897. wake_up_interruptible(&port->open_wait);
  1898. schedule_work(&port->tqueue);
  1899. }
  1900. if (port->flags & ASYNC_CTS_FLOW) {
  1901. if (port->tty->hw_stopped) {
  1902. if (status & UART_MSR_CTS) {
  1903. port->tty->hw_stopped = 0;
  1904. if ((port->type != PORT_16550A) &&
  1905. (!port->board->chip_flag)) {
  1906. outb(port->IER & ~UART_IER_THRI,
  1907. port->ioaddr + UART_IER);
  1908. port->IER |= UART_IER_THRI;
  1909. outb(port->IER, port->ioaddr +
  1910. UART_IER);
  1911. }
  1912. set_bit(MXSER_EVENT_TXLOW, &port->event);
  1913. schedule_work(&port->tqueue);
  1914. }
  1915. } else {
  1916. if (!(status & UART_MSR_CTS)) {
  1917. port->tty->hw_stopped = 1;
  1918. if (port->type != PORT_16550A &&
  1919. !port->board->chip_flag) {
  1920. port->IER &= ~UART_IER_THRI;
  1921. outb(port->IER, port->ioaddr +
  1922. UART_IER);
  1923. }
  1924. }
  1925. }
  1926. }
  1927. }
  1928. static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
  1929. {
  1930. DECLARE_WAITQUEUE(wait, current);
  1931. int retval;
  1932. int do_clocal = 0;
  1933. unsigned long flags;
  1934. /*
  1935. * If non-blocking mode is set, or the port is not enabled,
  1936. * then make the check up front and then exit.
  1937. */
  1938. if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
  1939. port->flags |= ASYNC_NORMAL_ACTIVE;
  1940. return 0;
  1941. }
  1942. if (tty->termios->c_cflag & CLOCAL)
  1943. do_clocal = 1;
  1944. /*
  1945. * Block waiting for the carrier detect and the line to become
  1946. * free (i.e., not in use by the callout). While we are in
  1947. * this loop, port->count is dropped by one, so that
  1948. * mxser_close() knows when to free things. We restore it upon
  1949. * exit, either normal or abnormal.
  1950. */
  1951. retval = 0;
  1952. add_wait_queue(&port->open_wait, &wait);
  1953. spin_lock_irqsave(&port->slock, flags);
  1954. if (!tty_hung_up_p(filp))
  1955. port->count--;
  1956. spin_unlock_irqrestore(&port->slock, flags);
  1957. port->blocked_open++;
  1958. while (1) {
  1959. spin_lock_irqsave(&port->slock, flags);
  1960. outb(inb(port->ioaddr + UART_MCR) |
  1961. UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
  1962. spin_unlock_irqrestore(&port->slock, flags);
  1963. set_current_state(TASK_INTERRUPTIBLE);
  1964. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  1965. if (port->flags & ASYNC_HUP_NOTIFY)
  1966. retval = -EAGAIN;
  1967. else
  1968. retval = -ERESTARTSYS;
  1969. break;
  1970. }
  1971. if (!(port->flags & ASYNC_CLOSING) &&
  1972. (do_clocal ||
  1973. (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
  1974. break;
  1975. if (signal_pending(current)) {
  1976. retval = -ERESTARTSYS;
  1977. break;
  1978. }
  1979. schedule();
  1980. }
  1981. set_current_state(TASK_RUNNING);
  1982. remove_wait_queue(&port->open_wait, &wait);
  1983. if (!tty_hung_up_p(filp))
  1984. port->count++;
  1985. port->blocked_open--;
  1986. if (retval)
  1987. return retval;
  1988. port->flags |= ASYNC_NORMAL_ACTIVE;
  1989. return 0;
  1990. }
  1991. static int mxser_startup(struct mxser_port *info)
  1992. {
  1993. unsigned long page;
  1994. unsigned long flags;
  1995. page = __get_free_page(GFP_KERNEL);
  1996. if (!page)
  1997. return -ENOMEM;
  1998. spin_lock_irqsave(&info->slock, flags);
  1999. if (info->flags & ASYNC_INITIALIZED) {
  2000. free_page(page);
  2001. spin_unlock_irqrestore(&info->slock, flags);
  2002. return 0;
  2003. }
  2004. if (!info->ioaddr || !info->type) {
  2005. if (info->tty)
  2006. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2007. free_page(page);
  2008. spin_unlock_irqrestore(&info->slock, flags);
  2009. return 0;
  2010. }
  2011. if (info->xmit_buf)
  2012. free_page(page);
  2013. else
  2014. info->xmit_buf = (unsigned char *) page;
  2015. /*
  2016. * Clear the FIFO buffers and disable them
  2017. * (they will be reenabled in mxser_change_speed())
  2018. */
  2019. if (info->board->chip_flag)
  2020. outb((UART_FCR_CLEAR_RCVR |
  2021. UART_FCR_CLEAR_XMIT |
  2022. MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
  2023. else
  2024. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  2025. info->ioaddr + UART_FCR);
  2026. /*
  2027. * At this point there's no way the LSR could still be 0xFF;
  2028. * if it is, then bail out, because there's likely no UART
  2029. * here.
  2030. */
  2031. if (inb(info->ioaddr + UART_LSR) == 0xff) {
  2032. spin_unlock_irqrestore(&info->slock, flags);
  2033. if (capable(CAP_SYS_ADMIN)) {
  2034. if (info->tty)
  2035. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2036. return 0;
  2037. } else
  2038. return -ENODEV;
  2039. }
  2040. /*
  2041. * Clear the interrupt registers.
  2042. */
  2043. (void) inb(info->ioaddr + UART_LSR);
  2044. (void) inb(info->ioaddr + UART_RX);
  2045. (void) inb(info->ioaddr + UART_IIR);
  2046. (void) inb(info->ioaddr + UART_MSR);
  2047. /*
  2048. * Now, initialize the UART
  2049. */
  2050. outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
  2051. info->MCR = UART_MCR_DTR | UART_MCR_RTS;
  2052. outb(info->MCR, info->ioaddr + UART_MCR);
  2053. /*
  2054. * Finally, enable interrupts
  2055. */
  2056. info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  2057. /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
  2058. /* following add by Victor Yu. 08-30-2002 */
  2059. if (info->board->chip_flag)
  2060. info->IER |= MOXA_MUST_IER_EGDAI;
  2061. /* above add by Victor Yu. 08-30-2002 */
  2062. outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
  2063. /*
  2064. * And clear the interrupt registers again for luck.
  2065. */
  2066. (void) inb(info->ioaddr + UART_LSR);
  2067. (void) inb(info->ioaddr + UART_RX);
  2068. (void) inb(info->ioaddr + UART_IIR);
  2069. (void) inb(info->ioaddr + UART_MSR);
  2070. if (info->tty)
  2071. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  2072. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  2073. /*
  2074. * and set the speed of the serial port
  2075. */
  2076. spin_unlock_irqrestore(&info->slock, flags);
  2077. mxser_change_speed(info, NULL);
  2078. info->flags |= ASYNC_INITIALIZED;
  2079. return 0;
  2080. }
  2081. /*
  2082. * This routine will shutdown a serial port; interrupts maybe disabled, and
  2083. * DTR is dropped if the hangup on close termio flag is on.
  2084. */
  2085. static void mxser_shutdown(struct mxser_port *info)
  2086. {
  2087. unsigned long flags;
  2088. if (!(info->flags & ASYNC_INITIALIZED))
  2089. return;
  2090. spin_lock_irqsave(&info->slock, flags);
  2091. /*
  2092. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  2093. * here so the queue might never be waken up
  2094. */
  2095. wake_up_interruptible(&info->delta_msr_wait);
  2096. /*
  2097. * Free the IRQ, if necessary
  2098. */
  2099. if (info->xmit_buf) {
  2100. free_page((unsigned long) info->xmit_buf);
  2101. info->xmit_buf = NULL;
  2102. }
  2103. info->IER = 0;
  2104. outb(0x00, info->ioaddr + UART_IER);
  2105. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  2106. info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
  2107. outb(info->MCR, info->ioaddr + UART_MCR);
  2108. /* clear Rx/Tx FIFO's */
  2109. /* following add by Victor Yu. 08-30-2002 */
  2110. if (info->board->chip_flag)
  2111. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
  2112. MOXA_MUST_FCR_GDA_MODE_ENABLE,
  2113. info->ioaddr + UART_FCR);
  2114. else
  2115. /* above add by Victor Yu. 08-30-2002 */
  2116. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  2117. info->ioaddr + UART_FCR);
  2118. /* read data port to reset things */
  2119. (void) inb(info->ioaddr + UART_RX);
  2120. if (info->tty)
  2121. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2122. info->flags &= ~ASYNC_INITIALIZED;
  2123. /* following add by Victor Yu. 09-23-2002 */
  2124. if (info->board->chip_flag)
  2125. SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  2126. /* above add by Victor Yu. 09-23-2002 */
  2127. spin_unlock_irqrestore(&info->slock, flags);
  2128. }
  2129. /*
  2130. * This routine is called to set the UART divisor registers to match
  2131. * the specified baud rate for a serial port.
  2132. */
  2133. static int mxser_change_speed(struct mxser_port *info,
  2134. struct termios *old_termios)
  2135. {
  2136. unsigned cflag, cval, fcr;
  2137. int ret = 0;
  2138. unsigned char status;
  2139. long baud;
  2140. unsigned long flags;
  2141. if (!info->tty || !info->tty->termios)
  2142. return ret;
  2143. cflag = info->tty->termios->c_cflag;
  2144. if (!(info->ioaddr))
  2145. return ret;
  2146. #ifndef B921600
  2147. #define B921600 (B460800 +1)
  2148. #endif
  2149. if (mxser_set_baud_method[info->tty->index] == 0) {
  2150. baud = tty_get_baud_rate(info->tty);
  2151. mxser_set_baud(info, baud);
  2152. }
  2153. /* byte size and parity */
  2154. switch (cflag & CSIZE) {
  2155. case CS5:
  2156. cval = 0x00;
  2157. break;
  2158. case CS6:
  2159. cval = 0x01;
  2160. break;
  2161. case CS7:
  2162. cval = 0x02;
  2163. break;
  2164. case CS8:
  2165. cval = 0x03;
  2166. break;
  2167. default:
  2168. cval = 0x00;
  2169. break; /* too keep GCC shut... */
  2170. }
  2171. if (cflag & CSTOPB)
  2172. cval |= 0x04;
  2173. if (cflag & PARENB)
  2174. cval |= UART_LCR_PARITY;
  2175. if (!(cflag & PARODD))
  2176. cval |= UART_LCR_EPAR;
  2177. if (cflag & CMSPAR)
  2178. cval |= UART_LCR_SPAR;
  2179. if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
  2180. if (info->board->chip_flag) {
  2181. fcr = UART_FCR_ENABLE_FIFO;
  2182. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  2183. SET_MOXA_MUST_FIFO_VALUE(info);
  2184. } else
  2185. fcr = 0;
  2186. } else {
  2187. fcr = UART_FCR_ENABLE_FIFO;
  2188. /* following add by Victor Yu. 08-30-2002 */
  2189. if (info->board->chip_flag) {
  2190. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  2191. SET_MOXA_MUST_FIFO_VALUE(info);
  2192. } else {
  2193. /* above add by Victor Yu. 08-30-2002 */
  2194. switch (info->rx_trigger) {
  2195. case 1:
  2196. fcr |= UART_FCR_TRIGGER_1;
  2197. break;
  2198. case 4:
  2199. fcr |= UART_FCR_TRIGGER_4;
  2200. break;
  2201. case 8:
  2202. fcr |= UART_FCR_TRIGGER_8;
  2203. break;
  2204. default:
  2205. fcr |= UART_FCR_TRIGGER_14;
  2206. break;
  2207. }
  2208. }
  2209. }
  2210. /* CTS flow control flag and modem status interrupts */
  2211. info->IER &= ~UART_IER_MSI;
  2212. info->MCR &= ~UART_MCR_AFE;
  2213. if (cflag & CRTSCTS) {
  2214. info->flags |= ASYNC_CTS_FLOW;
  2215. info->IER |= UART_IER_MSI;
  2216. if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
  2217. info->MCR |= UART_MCR_AFE;
  2218. /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
  2219. /*
  2220. save_flags(flags);
  2221. cli();
  2222. status = inb(baseaddr + UART_MSR);
  2223. restore_flags(flags);
  2224. */
  2225. /* mxser_check_modem_status(info, status); */
  2226. } else {
  2227. /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
  2228. /* MX_LOCK(&info->slock); */
  2229. status = inb(info->ioaddr + UART_MSR);
  2230. /* MX_UNLOCK(&info->slock); */
  2231. if (info->tty->hw_stopped) {
  2232. if (status & UART_MSR_CTS) {
  2233. info->tty->hw_stopped = 0;
  2234. if (info->type != PORT_16550A &&
  2235. !info->board->chip_flag) {
  2236. outb(info->IER & ~UART_IER_THRI,
  2237. info->ioaddr +
  2238. UART_IER);
  2239. info->IER |= UART_IER_THRI;
  2240. outb(info->IER, info->ioaddr +
  2241. UART_IER);
  2242. }
  2243. set_bit(MXSER_EVENT_TXLOW, &info->event);
  2244. schedule_work(&info->tqueue); }
  2245. } else {
  2246. if (!(status & UART_MSR_CTS)) {
  2247. info->tty->hw_stopped = 1;
  2248. if ((info->type != PORT_16550A) &&
  2249. (!info->board->chip_flag)) {
  2250. info->IER &= ~UART_IER_THRI;
  2251. outb(info->IER, info->ioaddr +
  2252. UART_IER);
  2253. }
  2254. }
  2255. }
  2256. }
  2257. } else {
  2258. info->flags &= ~ASYNC_CTS_FLOW;
  2259. }
  2260. outb(info->MCR, info->ioaddr + UART_MCR);
  2261. if (cflag & CLOCAL) {
  2262. info->flags &= ~ASYNC_CHECK_CD;
  2263. } else {
  2264. info->flags |= ASYNC_CHECK_CD;
  2265. info->IER |= UART_IER_MSI;
  2266. }
  2267. outb(info->IER, info->ioaddr + UART_IER);
  2268. /*
  2269. * Set up parity check flag
  2270. */
  2271. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  2272. if (I_INPCK(info->tty))
  2273. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  2274. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  2275. info->read_status_mask |= UART_LSR_BI;
  2276. info->ignore_status_mask = 0;
  2277. if (I_IGNBRK(info->tty)) {
  2278. info->ignore_status_mask |= UART_LSR_BI;
  2279. info->read_status_mask |= UART_LSR_BI;
  2280. /*
  2281. * If we're ignore parity and break indicators, ignore
  2282. * overruns too. (For real raw support).
  2283. */
  2284. if (I_IGNPAR(info->tty)) {
  2285. info->ignore_status_mask |=
  2286. UART_LSR_OE |
  2287. UART_LSR_PE |
  2288. UART_LSR_FE;
  2289. info->read_status_mask |=
  2290. UART_LSR_OE |
  2291. UART_LSR_PE |
  2292. UART_LSR_FE;
  2293. }
  2294. }
  2295. /* following add by Victor Yu. 09-02-2002 */
  2296. if (info->board->chip_flag) {
  2297. spin_lock_irqsave(&info->slock, flags);
  2298. SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
  2299. SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
  2300. if (I_IXON(info->tty)) {
  2301. ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  2302. } else {
  2303. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  2304. }
  2305. if (I_IXOFF(info->tty)) {
  2306. ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  2307. } else {
  2308. DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  2309. }
  2310. /*
  2311. if ( I_IXANY(info->tty) ) {
  2312. info->MCR |= MOXA_MUST_MCR_XON_ANY;
  2313. ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
  2314. } else {
  2315. info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
  2316. DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
  2317. }
  2318. */
  2319. spin_unlock_irqrestore(&info->slock, flags);
  2320. }
  2321. /* above add by Victor Yu. 09-02-2002 */
  2322. outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
  2323. outb(cval, info->ioaddr + UART_LCR);
  2324. return ret;
  2325. }
  2326. static int mxser_set_baud(struct mxser_port *info, long newspd)
  2327. {
  2328. int quot = 0;
  2329. unsigned char cval;
  2330. int ret = 0;
  2331. unsigned long flags;
  2332. if (!info->tty || !info->tty->termios)
  2333. return ret;
  2334. if (!(info->ioaddr))
  2335. return ret;
  2336. if (newspd > info->max_baud)
  2337. return 0;
  2338. info->realbaud = newspd;
  2339. if (newspd == 134) {
  2340. quot = (2 * info->baud_base / 269);
  2341. } else if (newspd) {
  2342. quot = info->baud_base / newspd;
  2343. if (quot == 0)
  2344. quot = 1;
  2345. } else {
  2346. quot = 0;
  2347. }
  2348. info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
  2349. info->timeout += HZ / 50; /* Add .02 seconds of slop */
  2350. if (quot) {
  2351. spin_lock_irqsave(&info->slock, flags);
  2352. info->MCR |= UART_MCR_DTR;
  2353. outb(info->MCR, info->ioaddr + UART_MCR);
  2354. spin_unlock_irqrestore(&info->slock, flags);
  2355. } else {
  2356. spin_lock_irqsave(&info->slock, flags);
  2357. info->MCR &= ~UART_MCR_DTR;
  2358. outb(info->MCR, info->ioaddr + UART_MCR);
  2359. spin_unlock_irqrestore(&info->slock, flags);
  2360. return ret;
  2361. }
  2362. cval = inb(info->ioaddr + UART_LCR);
  2363. outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
  2364. outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
  2365. outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
  2366. outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
  2367. return ret;
  2368. }
  2369. /*
  2370. * ------------------------------------------------------------
  2371. * friends of mxser_ioctl()
  2372. * ------------------------------------------------------------
  2373. */
  2374. static int mxser_get_serial_info(struct mxser_port *info,
  2375. struct serial_struct __user *retinfo)
  2376. {
  2377. struct serial_struct tmp;
  2378. if (!retinfo)
  2379. return -EFAULT;
  2380. memset(&tmp, 0, sizeof(tmp));
  2381. tmp.type = info->type;
  2382. tmp.line = info->tty->index;
  2383. tmp.port = info->ioaddr;
  2384. tmp.irq = info->board->irq;
  2385. tmp.flags = info->flags;
  2386. tmp.baud_base = info->baud_base;
  2387. tmp.close_delay = info->close_delay;
  2388. tmp.closing_wait = info->closing_wait;
  2389. tmp.custom_divisor = info->custom_divisor;
  2390. tmp.hub6 = 0;
  2391. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2392. return -EFAULT;
  2393. return 0;
  2394. }
  2395. static int mxser_set_serial_info(struct mxser_port *info,
  2396. struct serial_struct __user *new_info)
  2397. {
  2398. struct serial_struct new_serial;
  2399. unsigned int flags;
  2400. int retval = 0;
  2401. if (!new_info || !info->ioaddr)
  2402. return -EFAULT;
  2403. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2404. return -EFAULT;
  2405. if ((new_serial.irq != info->board->irq) ||
  2406. (new_serial.port != info->ioaddr) ||
  2407. (new_serial.custom_divisor != info->custom_divisor) ||
  2408. (new_serial.baud_base != info->baud_base))
  2409. return -EPERM;
  2410. flags = info->flags & ASYNC_SPD_MASK;
  2411. if (!capable(CAP_SYS_ADMIN)) {
  2412. if ((new_serial.baud_base != info->baud_base) ||
  2413. (new_serial.close_delay != info->close_delay) ||
  2414. ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
  2415. return -EPERM;
  2416. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  2417. (new_serial.flags & ASYNC_USR_MASK));
  2418. } else {
  2419. /*
  2420. * OK, past this point, all the error checking has been done.
  2421. * At this point, we start making changes.....
  2422. */
  2423. info->flags = ((info->flags & ~ASYNC_FLAGS) |
  2424. (new_serial.flags & ASYNC_FLAGS));
  2425. info->close_delay = new_serial.close_delay * HZ / 100;
  2426. info->closing_wait = new_serial.closing_wait * HZ / 100;
  2427. info->tty->low_latency =
  2428. (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2429. info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
  2430. }
  2431. /* added by casper, 3/17/2000, for mouse */
  2432. info->type = new_serial.type;
  2433. process_txrx_fifo(info);
  2434. if (info->flags & ASYNC_INITIALIZED) {
  2435. if (flags != (info->flags & ASYNC_SPD_MASK))
  2436. mxser_change_speed(info, NULL);
  2437. } else
  2438. retval = mxser_startup(info);
  2439. return retval;
  2440. }
  2441. /*
  2442. * mxser_get_lsr_info - get line status register info
  2443. *
  2444. * Purpose: Let user call ioctl() to get info when the UART physically
  2445. * is emptied. On bus types like RS485, the transmitter must
  2446. * release the bus after transmitting. This must be done when
  2447. * the transmit shift register is empty, not be done when the
  2448. * transmit holding register is empty. This functionality
  2449. * allows an RS485 driver to be written in user space.
  2450. */
  2451. static int mxser_get_lsr_info(struct mxser_port *info,
  2452. unsigned int __user *value)
  2453. {
  2454. unsigned char status;
  2455. unsigned int result;
  2456. unsigned long flags;
  2457. spin_lock_irqsave(&info->slock, flags);
  2458. status = inb(info->ioaddr + UART_LSR);
  2459. spin_unlock_irqrestore(&info->slock, flags);
  2460. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  2461. return put_user(result, value);
  2462. }
  2463. /*
  2464. * This routine sends a break character out the serial port.
  2465. */
  2466. static void mxser_send_break(struct mxser_port *info, int duration)
  2467. {
  2468. unsigned long flags;
  2469. if (!info->ioaddr)
  2470. return;
  2471. set_current_state(TASK_INTERRUPTIBLE);
  2472. spin_lock_irqsave(&info->slock, flags);
  2473. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  2474. info->ioaddr + UART_LCR);
  2475. spin_unlock_irqrestore(&info->slock, flags);
  2476. schedule_timeout(duration);
  2477. spin_lock_irqsave(&info->slock, flags);
  2478. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  2479. info->ioaddr + UART_LCR);
  2480. spin_unlock_irqrestore(&info->slock, flags);
  2481. }
  2482. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  2483. {
  2484. struct mxser_port *info = tty->driver_data;
  2485. unsigned char control, status;
  2486. unsigned long flags;
  2487. if (tty->index == MXSER_PORTS)
  2488. return -ENOIOCTLCMD;
  2489. if (tty->flags & (1 << TTY_IO_ERROR))
  2490. return -EIO;
  2491. control = info->MCR;
  2492. spin_lock_irqsave(&info->slock, flags);
  2493. status = inb(info->ioaddr + UART_MSR);
  2494. if (status & UART_MSR_ANY_DELTA)
  2495. mxser_check_modem_status(info, status);
  2496. spin_unlock_irqrestore(&info->slock, flags);
  2497. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  2498. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  2499. ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  2500. ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
  2501. ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  2502. ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  2503. }
  2504. static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
  2505. unsigned int set, unsigned int clear)
  2506. {
  2507. struct mxser_port *info = tty->driver_data;
  2508. unsigned long flags;
  2509. if (tty->index == MXSER_PORTS)
  2510. return -ENOIOCTLCMD;
  2511. if (tty->flags & (1 << TTY_IO_ERROR))
  2512. return -EIO;
  2513. spin_lock_irqsave(&info->slock, flags);
  2514. if (set & TIOCM_RTS)
  2515. info->MCR |= UART_MCR_RTS;
  2516. if (set & TIOCM_DTR)
  2517. info->MCR |= UART_MCR_DTR;
  2518. if (clear & TIOCM_RTS)
  2519. info->MCR &= ~UART_MCR_RTS;
  2520. if (clear & TIOCM_DTR)
  2521. info->MCR &= ~UART_MCR_DTR;
  2522. outb(info->MCR, info->ioaddr + UART_MCR);
  2523. spin_unlock_irqrestore(&info->slock, flags);
  2524. return 0;
  2525. }
  2526. static int mxser_read_register(int, unsigned short *);
  2527. static int mxser_program_mode(int);
  2528. static void mxser_normal_mode(int);
  2529. static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
  2530. {
  2531. int id, i, bits;
  2532. unsigned short regs[16], irq;
  2533. unsigned char scratch, scratch2;
  2534. brd->chip_flag = MOXA_OTHER_UART;
  2535. id = mxser_read_register(cap, regs);
  2536. if (id == C168_ASIC_ID) {
  2537. brd->board_type = MXSER_BOARD_C168_ISA;
  2538. brd->nports = 8;
  2539. } else if (id == C104_ASIC_ID) {
  2540. brd->board_type = MXSER_BOARD_C104_ISA;
  2541. brd->nports = 4;
  2542. } else if (id == C102_ASIC_ID) {
  2543. brd->board_type = MXSER_BOARD_C102_ISA;
  2544. brd->nports = 2;
  2545. } else if (id == CI132_ASIC_ID) {
  2546. brd->board_type = MXSER_BOARD_CI132;
  2547. brd->nports = 2;
  2548. } else if (id == CI134_ASIC_ID) {
  2549. brd->board_type = MXSER_BOARD_CI134;
  2550. brd->nports = 4;
  2551. } else if (id == CI104J_ASIC_ID) {
  2552. brd->board_type = MXSER_BOARD_CI104J;
  2553. brd->nports = 4;
  2554. } else
  2555. return 0;
  2556. irq = 0;
  2557. if (brd->nports == 2) {
  2558. irq = regs[9] & 0xF000;
  2559. irq = irq | (irq >> 4);
  2560. if (irq != (regs[9] & 0xFF00))
  2561. return MXSER_ERR_IRQ_CONFLIT;
  2562. } else if (brd->nports == 4) {
  2563. irq = regs[9] & 0xF000;
  2564. irq = irq | (irq >> 4);
  2565. irq = irq | (irq >> 8);
  2566. if (irq != regs[9])
  2567. return MXSER_ERR_IRQ_CONFLIT;
  2568. } else if (brd->nports == 8) {
  2569. irq = regs[9] & 0xF000;
  2570. irq = irq | (irq >> 4);
  2571. irq = irq | (irq >> 8);
  2572. if ((irq != regs[9]) || (irq != regs[10]))
  2573. return MXSER_ERR_IRQ_CONFLIT;
  2574. }
  2575. if (!irq)
  2576. return MXSER_ERR_IRQ;
  2577. brd->irq = ((int)(irq & 0xF000) >> 12);
  2578. for (i = 0; i < 8; i++)
  2579. brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
  2580. if ((regs[12] & 0x80) == 0)
  2581. return MXSER_ERR_VECTOR;
  2582. brd->vector = (int)regs[11]; /* interrupt vector */
  2583. if (id == 1)
  2584. brd->vector_mask = 0x00FF;
  2585. else
  2586. brd->vector_mask = 0x000F;
  2587. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2588. if (regs[12] & bits) {
  2589. brd->ports[i].baud_base = 921600;
  2590. brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
  2591. } else {
  2592. brd->ports[i].baud_base = 115200;
  2593. brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
  2594. }
  2595. }
  2596. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2597. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2598. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2599. outb(scratch2, cap + UART_LCR);
  2600. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2601. scratch = inb(cap + UART_IIR);
  2602. if (scratch & 0xC0)
  2603. brd->uart_type = PORT_16550A;
  2604. else
  2605. brd->uart_type = PORT_16450;
  2606. if (id == 1)
  2607. brd->nports = 8;
  2608. else
  2609. brd->nports = 4;
  2610. if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
  2611. return MXSER_ERR_IOADDR;
  2612. if (!request_region(brd->vector, 1, "mxser(vector)")) {
  2613. release_region(brd->ports[0].ioaddr, 8 * brd->nports);
  2614. return MXSER_ERR_VECTOR;
  2615. }
  2616. return brd->nports;
  2617. }
  2618. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  2619. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  2620. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  2621. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  2622. #define EN_CCMD 0x000 /* Chip's command register */
  2623. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  2624. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  2625. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  2626. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  2627. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  2628. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  2629. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  2630. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  2631. static int mxser_read_register(int port, unsigned short *regs)
  2632. {
  2633. int i, k, value, id;
  2634. unsigned int j;
  2635. id = mxser_program_mode(port);
  2636. if (id < 0)
  2637. return id;
  2638. for (i = 0; i < 14; i++) {
  2639. k = (i & 0x3F) | 0x180;
  2640. for (j = 0x100; j > 0; j >>= 1) {
  2641. outb(CHIP_CS, port);
  2642. if (k & j) {
  2643. outb(CHIP_CS | CHIP_DO, port);
  2644. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  2645. } else {
  2646. outb(CHIP_CS, port);
  2647. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  2648. }
  2649. }
  2650. (void)inb(port);
  2651. value = 0;
  2652. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  2653. outb(CHIP_CS, port);
  2654. outb(CHIP_CS | CHIP_SK, port);
  2655. if (inb(port) & CHIP_DI)
  2656. value |= j;
  2657. }
  2658. regs[i] = value;
  2659. outb(0, port);
  2660. }
  2661. mxser_normal_mode(port);
  2662. return id;
  2663. }
  2664. static int mxser_program_mode(int port)
  2665. {
  2666. int id, i, j, n;
  2667. /* unsigned long flags; */
  2668. spin_lock(&gm_lock);
  2669. outb(0, port);
  2670. outb(0, port);
  2671. outb(0, port);
  2672. (void)inb(port);
  2673. (void)inb(port);
  2674. outb(0, port);
  2675. (void)inb(port);
  2676. /* restore_flags(flags); */
  2677. spin_unlock(&gm_lock);
  2678. id = inb(port + 1) & 0x1F;
  2679. if ((id != C168_ASIC_ID) &&
  2680. (id != C104_ASIC_ID) &&
  2681. (id != C102_ASIC_ID) &&
  2682. (id != CI132_ASIC_ID) &&
  2683. (id != CI134_ASIC_ID) &&
  2684. (id != CI104J_ASIC_ID))
  2685. return -1;
  2686. for (i = 0, j = 0; i < 4; i++) {
  2687. n = inb(port + 2);
  2688. if (n == 'M') {
  2689. j = 1;
  2690. } else if ((j == 1) && (n == 1)) {
  2691. j = 2;
  2692. break;
  2693. } else
  2694. j = 0;
  2695. }
  2696. if (j != 2)
  2697. id = -2;
  2698. return id;
  2699. }
  2700. static void mxser_normal_mode(int port)
  2701. {
  2702. int i, n;
  2703. outb(0xA5, port + 1);
  2704. outb(0x80, port + 3);
  2705. outb(12, port + 0); /* 9600 bps */
  2706. outb(0, port + 1);
  2707. outb(0x03, port + 3); /* 8 data bits */
  2708. outb(0x13, port + 4); /* loop back mode */
  2709. for (i = 0; i < 16; i++) {
  2710. n = inb(port + 5);
  2711. if ((n & 0x61) == 0x60)
  2712. break;
  2713. if ((n & 1) == 1)
  2714. (void)inb(port);
  2715. }
  2716. outb(0x00, port + 4);
  2717. }
  2718. module_init(mxser_module_init);
  2719. module_exit(mxser_module_exit);