mxser.c 84 KB

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