mxser_new.c 76 KB

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