mxser.c 69 KB

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