mxser.c 83 KB

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