mxser.c 82 KB

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