mxser.c 69 KB

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