mxser_new.c 79 KB

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