mxser.c 71 KB

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