mxser.c 82 KB

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