mxser_new.c 80 KB

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