mxser.c 83 KB

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