mxser.c 70 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/smp_lock.h>
  26. #include <linux/timer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_reg.h>
  32. #include <linux/major.h>
  33. #include <linux/string.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/gfp.h>
  37. #include <linux/ioport.h>
  38. #include <linux/mm.h>
  39. #include <linux/delay.h>
  40. #include <linux/pci.h>
  41. #include <linux/bitops.h>
  42. #include <asm/system.h>
  43. #include <asm/io.h>
  44. #include <asm/irq.h>
  45. #include <asm/uaccess.h>
  46. #include "mxser.h"
  47. #define MXSER_VERSION "2.0.4" /* 1.12 */
  48. #define MXSERMAJOR 174
  49. #define MXSER_BOARDS 4 /* Max. boards */
  50. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  51. #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
  52. #define MXSER_ISR_PASS_LIMIT 100
  53. /*CheckIsMoxaMust return value*/
  54. #define MOXA_OTHER_UART 0x00
  55. #define MOXA_MUST_MU150_HWID 0x01
  56. #define MOXA_MUST_MU860_HWID 0x02
  57. #define WAKEUP_CHARS 256
  58. #define UART_MCR_AFE 0x20
  59. #define UART_LSR_SPECIAL 0x1E
  60. #define PCI_DEVICE_ID_POS104UL 0x1044
  61. #define PCI_DEVICE_ID_CB108 0x1080
  62. #define PCI_DEVICE_ID_CP102UF 0x1023
  63. #define PCI_DEVICE_ID_CB114 0x1142
  64. #define PCI_DEVICE_ID_CP114UL 0x1143
  65. #define PCI_DEVICE_ID_CB134I 0x1341
  66. #define PCI_DEVICE_ID_CP138U 0x1380
  67. #define C168_ASIC_ID 1
  68. #define C104_ASIC_ID 2
  69. #define C102_ASIC_ID 0xB
  70. #define CI132_ASIC_ID 4
  71. #define CI134_ASIC_ID 3
  72. #define CI104J_ASIC_ID 5
  73. #define MXSER_HIGHBAUD 1
  74. #define MXSER_HAS2 2
  75. /* This is only for PCI */
  76. static const struct {
  77. int type;
  78. int tx_fifo;
  79. int rx_fifo;
  80. int xmit_fifo_size;
  81. int rx_high_water;
  82. int rx_trigger;
  83. int rx_low_water;
  84. long max_baud;
  85. } Gpci_uart_info[] = {
  86. {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
  87. {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
  88. {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
  89. };
  90. #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
  91. struct mxser_cardinfo {
  92. char *name;
  93. unsigned int nports;
  94. unsigned int flags;
  95. };
  96. static const struct mxser_cardinfo mxser_cards[] = {
  97. /* 0*/ { "C168 series", 8, },
  98. { "C104 series", 4, },
  99. { "CI-104J series", 4, },
  100. { "C168H/PCI series", 8, },
  101. { "C104H/PCI series", 4, },
  102. /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
  103. { "CI-132 series", 4, MXSER_HAS2 },
  104. { "CI-134 series", 4, },
  105. { "CP-132 series", 2, },
  106. { "CP-114 series", 4, },
  107. /*10*/ { "CT-114 series", 4, },
  108. { "CP-102 series", 2, MXSER_HIGHBAUD },
  109. { "CP-104U series", 4, },
  110. { "CP-168U series", 8, },
  111. { "CP-132U series", 2, },
  112. /*15*/ { "CP-134U series", 4, },
  113. { "CP-104JU series", 4, },
  114. { "Moxa UC7000 Serial", 8, }, /* RC7000 */
  115. { "CP-118U series", 8, },
  116. { "CP-102UL series", 2, },
  117. /*20*/ { "CP-102U series", 2, },
  118. { "CP-118EL series", 8, },
  119. { "CP-168EL series", 8, },
  120. { "CP-104EL series", 4, },
  121. { "CB-108 series", 8, },
  122. /*25*/ { "CB-114 series", 4, },
  123. { "CB-134I series", 4, },
  124. { "CP-138U series", 8, },
  125. { "POS-104UL series", 4, },
  126. { "CP-114UL series", 4, },
  127. /*30*/ { "CP-102UF series", 2, }
  128. };
  129. /* driver_data correspond to the lines in the structure above
  130. see also ISA probe function before you change something */
  131. static struct pci_device_id mxser_pcibrds[] = {
  132. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
  133. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
  134. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
  135. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
  136. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
  137. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
  138. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
  139. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
  140. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
  141. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
  142. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
  143. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
  144. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
  145. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
  146. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
  147. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
  148. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
  149. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
  150. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
  151. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
  152. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
  153. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
  154. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
  155. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
  156. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  160. static unsigned long ioaddr[MXSER_BOARDS];
  161. static int ttymajor = MXSERMAJOR;
  162. /* Variables for insmod */
  163. MODULE_AUTHOR("Casper Yang");
  164. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  165. module_param_array(ioaddr, ulong, NULL, 0);
  166. MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
  167. module_param(ttymajor, int, 0);
  168. MODULE_LICENSE("GPL");
  169. struct mxser_log {
  170. int tick;
  171. unsigned long rxcnt[MXSER_PORTS];
  172. unsigned long txcnt[MXSER_PORTS];
  173. };
  174. struct mxser_mon {
  175. unsigned long rxcnt;
  176. unsigned long txcnt;
  177. unsigned long up_rxcnt;
  178. unsigned long up_txcnt;
  179. int modem_status;
  180. unsigned char hold_reason;
  181. };
  182. struct mxser_mon_ext {
  183. unsigned long rx_cnt[32];
  184. unsigned long tx_cnt[32];
  185. unsigned long up_rxcnt[32];
  186. unsigned long up_txcnt[32];
  187. int modem_status[32];
  188. long baudrate[32];
  189. int databits[32];
  190. int stopbits[32];
  191. int parity[32];
  192. int flowctrl[32];
  193. int fifo[32];
  194. int iftype[32];
  195. };
  196. struct mxser_board;
  197. struct mxser_port {
  198. struct tty_port port;
  199. struct mxser_board *board;
  200. unsigned long ioaddr;
  201. unsigned long opmode_ioaddr;
  202. int max_baud;
  203. int rx_high_water;
  204. int rx_trigger; /* Rx fifo trigger level */
  205. int rx_low_water;
  206. int baud_base; /* max. speed */
  207. int type; /* UART type */
  208. int x_char; /* xon/xoff character */
  209. int IER; /* Interrupt Enable Register */
  210. int MCR; /* Modem control register */
  211. unsigned char stop_rx;
  212. unsigned char ldisc_stop_rx;
  213. int custom_divisor;
  214. unsigned char err_shadow;
  215. struct async_icount icount; /* kernel counters for 4 input interrupts */
  216. int timeout;
  217. int read_status_mask;
  218. int ignore_status_mask;
  219. int xmit_fifo_size;
  220. int xmit_head;
  221. int xmit_tail;
  222. int xmit_cnt;
  223. struct ktermios normal_termios;
  224. struct mxser_mon mon_data;
  225. spinlock_t slock;
  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->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->port.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. static void mxser_close_port(struct tty_struct *tty, struct tty_port *port)
  907. {
  908. struct mxser_port *info = container_of(port, struct mxser_port, port);
  909. unsigned long timeout;
  910. /*
  911. * Save the termios structure, since this port may have
  912. * separate termios for callout and dialin.
  913. *
  914. * FIXME: Can this go ?
  915. */
  916. if (port->flags & ASYNC_NORMAL_ACTIVE)
  917. info->normal_termios = *tty->termios;
  918. /*
  919. * At this point we stop accepting input. To do this, we
  920. * disable the receive line status interrupts, and tell the
  921. * interrupt driver to stop checking the data ready bit in the
  922. * line status register.
  923. */
  924. info->IER &= ~UART_IER_RLSI;
  925. if (info->board->chip_flag)
  926. info->IER &= ~MOXA_MUST_RECV_ISR;
  927. if (port->flags & ASYNC_INITIALIZED) {
  928. outb(info->IER, info->ioaddr + UART_IER);
  929. /*
  930. * Before we drop DTR, make sure the UART transmitter
  931. * has completely drained; this is especially
  932. * important if there is a transmit FIFO!
  933. */
  934. timeout = jiffies + HZ;
  935. while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
  936. schedule_timeout_interruptible(5);
  937. if (time_after(jiffies, timeout))
  938. break;
  939. }
  940. }
  941. mxser_shutdown(tty);
  942. }
  943. /*
  944. * This routine is called when the serial port gets closed. First, we
  945. * wait for the last remaining data to be sent. Then, we unlink its
  946. * async structure from the interrupt chain if necessary, and we free
  947. * that IRQ if nothing is left in the chain.
  948. */
  949. static void mxser_close(struct tty_struct *tty, struct file *filp)
  950. {
  951. struct mxser_port *info = tty->driver_data;
  952. struct tty_port *port = &info->port;
  953. if (tty->index == MXSER_PORTS)
  954. return;
  955. if (tty_port_close_start(port, tty, filp) == 0)
  956. return;
  957. mxser_close_port(tty, port);
  958. mxser_flush_buffer(tty);
  959. /* Right now the tty_port set is done outside of the close_end helper
  960. as we don't yet have everyone using refcounts */
  961. tty_port_close_end(port, tty);
  962. tty_port_tty_set(port, NULL);
  963. }
  964. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  965. {
  966. int c, total = 0;
  967. struct mxser_port *info = tty->driver_data;
  968. unsigned long flags;
  969. if (!info->port.xmit_buf)
  970. return 0;
  971. while (1) {
  972. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  973. SERIAL_XMIT_SIZE - info->xmit_head));
  974. if (c <= 0)
  975. break;
  976. memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
  977. spin_lock_irqsave(&info->slock, flags);
  978. info->xmit_head = (info->xmit_head + c) &
  979. (SERIAL_XMIT_SIZE - 1);
  980. info->xmit_cnt += c;
  981. spin_unlock_irqrestore(&info->slock, flags);
  982. buf += c;
  983. count -= c;
  984. total += c;
  985. }
  986. if (info->xmit_cnt && !tty->stopped) {
  987. if (!tty->hw_stopped ||
  988. (info->type == PORT_16550A) ||
  989. (info->board->chip_flag)) {
  990. spin_lock_irqsave(&info->slock, flags);
  991. outb(info->IER & ~UART_IER_THRI, info->ioaddr +
  992. UART_IER);
  993. info->IER |= UART_IER_THRI;
  994. outb(info->IER, info->ioaddr + UART_IER);
  995. spin_unlock_irqrestore(&info->slock, flags);
  996. }
  997. }
  998. return total;
  999. }
  1000. static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
  1001. {
  1002. struct mxser_port *info = tty->driver_data;
  1003. unsigned long flags;
  1004. if (!info->port.xmit_buf)
  1005. return 0;
  1006. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  1007. return 0;
  1008. spin_lock_irqsave(&info->slock, flags);
  1009. info->port.xmit_buf[info->xmit_head++] = ch;
  1010. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  1011. info->xmit_cnt++;
  1012. spin_unlock_irqrestore(&info->slock, flags);
  1013. if (!tty->stopped) {
  1014. if (!tty->hw_stopped ||
  1015. (info->type == PORT_16550A) ||
  1016. info->board->chip_flag) {
  1017. spin_lock_irqsave(&info->slock, flags);
  1018. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1019. info->IER |= UART_IER_THRI;
  1020. outb(info->IER, info->ioaddr + UART_IER);
  1021. spin_unlock_irqrestore(&info->slock, flags);
  1022. }
  1023. }
  1024. return 1;
  1025. }
  1026. static void mxser_flush_chars(struct tty_struct *tty)
  1027. {
  1028. struct mxser_port *info = tty->driver_data;
  1029. unsigned long flags;
  1030. if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
  1031. (tty->hw_stopped && info->type != PORT_16550A &&
  1032. !info->board->chip_flag))
  1033. return;
  1034. spin_lock_irqsave(&info->slock, flags);
  1035. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1036. info->IER |= UART_IER_THRI;
  1037. outb(info->IER, info->ioaddr + UART_IER);
  1038. spin_unlock_irqrestore(&info->slock, flags);
  1039. }
  1040. static int mxser_write_room(struct tty_struct *tty)
  1041. {
  1042. struct mxser_port *info = tty->driver_data;
  1043. int ret;
  1044. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1045. return ret < 0 ? 0 : ret;
  1046. }
  1047. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1048. {
  1049. struct mxser_port *info = tty->driver_data;
  1050. return info->xmit_cnt;
  1051. }
  1052. /*
  1053. * ------------------------------------------------------------
  1054. * friends of mxser_ioctl()
  1055. * ------------------------------------------------------------
  1056. */
  1057. static int mxser_get_serial_info(struct tty_struct *tty,
  1058. struct serial_struct __user *retinfo)
  1059. {
  1060. struct mxser_port *info = tty->driver_data;
  1061. struct serial_struct tmp = {
  1062. .type = info->type,
  1063. .line = tty->index,
  1064. .port = info->ioaddr,
  1065. .irq = info->board->irq,
  1066. .flags = info->port.flags,
  1067. .baud_base = info->baud_base,
  1068. .close_delay = info->port.close_delay,
  1069. .closing_wait = info->port.closing_wait,
  1070. .custom_divisor = info->custom_divisor,
  1071. .hub6 = 0
  1072. };
  1073. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1074. return -EFAULT;
  1075. return 0;
  1076. }
  1077. static int mxser_set_serial_info(struct tty_struct *tty,
  1078. struct serial_struct __user *new_info)
  1079. {
  1080. struct mxser_port *info = tty->driver_data;
  1081. struct serial_struct new_serial;
  1082. speed_t baud;
  1083. unsigned long sl_flags;
  1084. unsigned int flags;
  1085. int retval = 0;
  1086. if (!new_info || !info->ioaddr)
  1087. return -ENODEV;
  1088. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1089. return -EFAULT;
  1090. if (new_serial.irq != info->board->irq ||
  1091. new_serial.port != info->ioaddr)
  1092. return -EINVAL;
  1093. flags = info->port.flags & ASYNC_SPD_MASK;
  1094. if (!capable(CAP_SYS_ADMIN)) {
  1095. if ((new_serial.baud_base != info->baud_base) ||
  1096. (new_serial.close_delay != info->port.close_delay) ||
  1097. ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
  1098. return -EPERM;
  1099. info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
  1100. (new_serial.flags & ASYNC_USR_MASK));
  1101. } else {
  1102. /*
  1103. * OK, past this point, all the error checking has been done.
  1104. * At this point, we start making changes.....
  1105. */
  1106. info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
  1107. (new_serial.flags & ASYNC_FLAGS));
  1108. info->port.close_delay = new_serial.close_delay * HZ / 100;
  1109. info->port.closing_wait = new_serial.closing_wait * HZ / 100;
  1110. tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY)
  1111. ? 1 : 0;
  1112. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
  1113. (new_serial.baud_base != info->baud_base ||
  1114. new_serial.custom_divisor !=
  1115. info->custom_divisor)) {
  1116. baud = new_serial.baud_base / new_serial.custom_divisor;
  1117. tty_encode_baud_rate(tty, baud, baud);
  1118. }
  1119. }
  1120. info->type = new_serial.type;
  1121. process_txrx_fifo(info);
  1122. if (info->port.flags & ASYNC_INITIALIZED) {
  1123. if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
  1124. spin_lock_irqsave(&info->slock, sl_flags);
  1125. mxser_change_speed(tty, NULL);
  1126. spin_unlock_irqrestore(&info->slock, sl_flags);
  1127. }
  1128. } else
  1129. retval = mxser_startup(tty);
  1130. return retval;
  1131. }
  1132. /*
  1133. * mxser_get_lsr_info - get line status register info
  1134. *
  1135. * Purpose: Let user call ioctl() to get info when the UART physically
  1136. * is emptied. On bus types like RS485, the transmitter must
  1137. * release the bus after transmitting. This must be done when
  1138. * the transmit shift register is empty, not be done when the
  1139. * transmit holding register is empty. This functionality
  1140. * allows an RS485 driver to be written in user space.
  1141. */
  1142. static int mxser_get_lsr_info(struct mxser_port *info,
  1143. unsigned int __user *value)
  1144. {
  1145. unsigned char status;
  1146. unsigned int result;
  1147. unsigned long flags;
  1148. spin_lock_irqsave(&info->slock, flags);
  1149. status = inb(info->ioaddr + UART_LSR);
  1150. spin_unlock_irqrestore(&info->slock, flags);
  1151. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1152. return put_user(result, value);
  1153. }
  1154. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  1155. {
  1156. struct mxser_port *info = tty->driver_data;
  1157. unsigned char control, status;
  1158. unsigned long flags;
  1159. if (tty->index == MXSER_PORTS)
  1160. return -ENOIOCTLCMD;
  1161. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1162. return -EIO;
  1163. control = info->MCR;
  1164. spin_lock_irqsave(&info->slock, flags);
  1165. status = inb(info->ioaddr + UART_MSR);
  1166. if (status & UART_MSR_ANY_DELTA)
  1167. mxser_check_modem_status(tty, info, status);
  1168. spin_unlock_irqrestore(&info->slock, flags);
  1169. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  1170. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  1171. ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  1172. ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
  1173. ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  1174. ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1175. }
  1176. static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
  1177. unsigned int set, unsigned int clear)
  1178. {
  1179. struct mxser_port *info = tty->driver_data;
  1180. unsigned long flags;
  1181. if (tty->index == MXSER_PORTS)
  1182. return -ENOIOCTLCMD;
  1183. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1184. return -EIO;
  1185. spin_lock_irqsave(&info->slock, flags);
  1186. if (set & TIOCM_RTS)
  1187. info->MCR |= UART_MCR_RTS;
  1188. if (set & TIOCM_DTR)
  1189. info->MCR |= UART_MCR_DTR;
  1190. if (clear & TIOCM_RTS)
  1191. info->MCR &= ~UART_MCR_RTS;
  1192. if (clear & TIOCM_DTR)
  1193. info->MCR &= ~UART_MCR_DTR;
  1194. outb(info->MCR, info->ioaddr + UART_MCR);
  1195. spin_unlock_irqrestore(&info->slock, flags);
  1196. return 0;
  1197. }
  1198. static int __init mxser_program_mode(int port)
  1199. {
  1200. int id, i, j, n;
  1201. outb(0, port);
  1202. outb(0, port);
  1203. outb(0, port);
  1204. (void)inb(port);
  1205. (void)inb(port);
  1206. outb(0, port);
  1207. (void)inb(port);
  1208. id = inb(port + 1) & 0x1F;
  1209. if ((id != C168_ASIC_ID) &&
  1210. (id != C104_ASIC_ID) &&
  1211. (id != C102_ASIC_ID) &&
  1212. (id != CI132_ASIC_ID) &&
  1213. (id != CI134_ASIC_ID) &&
  1214. (id != CI104J_ASIC_ID))
  1215. return -1;
  1216. for (i = 0, j = 0; i < 4; i++) {
  1217. n = inb(port + 2);
  1218. if (n == 'M') {
  1219. j = 1;
  1220. } else if ((j == 1) && (n == 1)) {
  1221. j = 2;
  1222. break;
  1223. } else
  1224. j = 0;
  1225. }
  1226. if (j != 2)
  1227. id = -2;
  1228. return id;
  1229. }
  1230. static void __init mxser_normal_mode(int port)
  1231. {
  1232. int i, n;
  1233. outb(0xA5, port + 1);
  1234. outb(0x80, port + 3);
  1235. outb(12, port + 0); /* 9600 bps */
  1236. outb(0, port + 1);
  1237. outb(0x03, port + 3); /* 8 data bits */
  1238. outb(0x13, port + 4); /* loop back mode */
  1239. for (i = 0; i < 16; i++) {
  1240. n = inb(port + 5);
  1241. if ((n & 0x61) == 0x60)
  1242. break;
  1243. if ((n & 1) == 1)
  1244. (void)inb(port);
  1245. }
  1246. outb(0x00, port + 4);
  1247. }
  1248. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  1249. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  1250. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  1251. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  1252. #define EN_CCMD 0x000 /* Chip's command register */
  1253. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  1254. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  1255. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  1256. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  1257. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  1258. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  1259. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  1260. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  1261. static int __init mxser_read_register(int port, unsigned short *regs)
  1262. {
  1263. int i, k, value, id;
  1264. unsigned int j;
  1265. id = mxser_program_mode(port);
  1266. if (id < 0)
  1267. return id;
  1268. for (i = 0; i < 14; i++) {
  1269. k = (i & 0x3F) | 0x180;
  1270. for (j = 0x100; j > 0; j >>= 1) {
  1271. outb(CHIP_CS, port);
  1272. if (k & j) {
  1273. outb(CHIP_CS | CHIP_DO, port);
  1274. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  1275. } else {
  1276. outb(CHIP_CS, port);
  1277. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  1278. }
  1279. }
  1280. (void)inb(port);
  1281. value = 0;
  1282. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  1283. outb(CHIP_CS, port);
  1284. outb(CHIP_CS | CHIP_SK, port);
  1285. if (inb(port) & CHIP_DI)
  1286. value |= j;
  1287. }
  1288. regs[i] = value;
  1289. outb(0, port);
  1290. }
  1291. mxser_normal_mode(port);
  1292. return id;
  1293. }
  1294. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1295. {
  1296. struct mxser_port *port;
  1297. struct tty_struct *tty;
  1298. int result, status;
  1299. unsigned int i, j;
  1300. int ret = 0;
  1301. switch (cmd) {
  1302. case MOXA_GET_MAJOR:
  1303. if (printk_ratelimit())
  1304. printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
  1305. "%x (GET_MAJOR), fix your userspace\n",
  1306. current->comm, cmd);
  1307. return put_user(ttymajor, (int __user *)argp);
  1308. case MOXA_CHKPORTENABLE:
  1309. result = 0;
  1310. lock_kernel();
  1311. for (i = 0; i < MXSER_BOARDS; i++)
  1312. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
  1313. if (mxser_boards[i].ports[j].ioaddr)
  1314. result |= (1 << i);
  1315. unlock_kernel();
  1316. return put_user(result, (unsigned long __user *)argp);
  1317. case MOXA_GETDATACOUNT:
  1318. lock_kernel();
  1319. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1320. ret = -EFAULT;
  1321. unlock_kernel();
  1322. return ret;
  1323. case MOXA_GETMSTATUS: {
  1324. struct mxser_mstatus ms, __user *msu = argp;
  1325. lock_kernel();
  1326. for (i = 0; i < MXSER_BOARDS; i++)
  1327. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1328. port = &mxser_boards[i].ports[j];
  1329. memset(&ms, 0, sizeof(ms));
  1330. if (!port->ioaddr)
  1331. goto copy;
  1332. tty = tty_port_tty_get(&port->port);
  1333. if (!tty || !tty->termios)
  1334. ms.cflag = port->normal_termios.c_cflag;
  1335. else
  1336. ms.cflag = tty->termios->c_cflag;
  1337. tty_kref_put(tty);
  1338. status = inb(port->ioaddr + UART_MSR);
  1339. if (status & UART_MSR_DCD)
  1340. ms.dcd = 1;
  1341. if (status & UART_MSR_DSR)
  1342. ms.dsr = 1;
  1343. if (status & UART_MSR_CTS)
  1344. ms.cts = 1;
  1345. copy:
  1346. if (copy_to_user(msu, &ms, sizeof(ms))) {
  1347. unlock_kernel();
  1348. return -EFAULT;
  1349. }
  1350. msu++;
  1351. }
  1352. unlock_kernel();
  1353. return 0;
  1354. }
  1355. case MOXA_ASPP_MON_EXT: {
  1356. struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
  1357. unsigned int cflag, iflag, p;
  1358. u8 opmode;
  1359. me = kzalloc(sizeof(*me), GFP_KERNEL);
  1360. if (!me)
  1361. return -ENOMEM;
  1362. lock_kernel();
  1363. for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
  1364. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
  1365. if (p >= ARRAY_SIZE(me->rx_cnt)) {
  1366. i = MXSER_BOARDS;
  1367. break;
  1368. }
  1369. port = &mxser_boards[i].ports[j];
  1370. if (!port->ioaddr)
  1371. continue;
  1372. status = mxser_get_msr(port->ioaddr, 0, p);
  1373. if (status & UART_MSR_TERI)
  1374. port->icount.rng++;
  1375. if (status & UART_MSR_DDSR)
  1376. port->icount.dsr++;
  1377. if (status & UART_MSR_DDCD)
  1378. port->icount.dcd++;
  1379. if (status & UART_MSR_DCTS)
  1380. port->icount.cts++;
  1381. port->mon_data.modem_status = status;
  1382. me->rx_cnt[p] = port->mon_data.rxcnt;
  1383. me->tx_cnt[p] = port->mon_data.txcnt;
  1384. me->up_rxcnt[p] = port->mon_data.up_rxcnt;
  1385. me->up_txcnt[p] = port->mon_data.up_txcnt;
  1386. me->modem_status[p] =
  1387. port->mon_data.modem_status;
  1388. tty = tty_port_tty_get(&port->port);
  1389. if (!tty || !tty->termios) {
  1390. cflag = port->normal_termios.c_cflag;
  1391. iflag = port->normal_termios.c_iflag;
  1392. me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios);
  1393. } else {
  1394. cflag = tty->termios->c_cflag;
  1395. iflag = tty->termios->c_iflag;
  1396. me->baudrate[p] = tty_get_baud_rate(tty);
  1397. }
  1398. tty_kref_put(tty);
  1399. me->databits[p] = cflag & CSIZE;
  1400. me->stopbits[p] = cflag & CSTOPB;
  1401. me->parity[p] = cflag & (PARENB | PARODD |
  1402. CMSPAR);
  1403. if (cflag & CRTSCTS)
  1404. me->flowctrl[p] |= 0x03;
  1405. if (iflag & (IXON | IXOFF))
  1406. me->flowctrl[p] |= 0x0C;
  1407. if (port->type == PORT_16550A)
  1408. me->fifo[p] = 1;
  1409. opmode = inb(port->opmode_ioaddr) >>
  1410. ((p % 4) * 2);
  1411. opmode &= OP_MODE_MASK;
  1412. me->iftype[p] = opmode;
  1413. }
  1414. }
  1415. unlock_kernel();
  1416. if (copy_to_user(argp, me, sizeof(*me)))
  1417. ret = -EFAULT;
  1418. kfree(me);
  1419. return ret;
  1420. }
  1421. default:
  1422. return -ENOIOCTLCMD;
  1423. }
  1424. return 0;
  1425. }
  1426. static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
  1427. struct async_icount *cprev)
  1428. {
  1429. struct async_icount cnow;
  1430. unsigned long flags;
  1431. int ret;
  1432. spin_lock_irqsave(&info->slock, flags);
  1433. cnow = info->icount; /* atomic copy */
  1434. spin_unlock_irqrestore(&info->slock, flags);
  1435. ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  1436. ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  1437. ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  1438. ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1439. *cprev = cnow;
  1440. return ret;
  1441. }
  1442. static int mxser_ioctl(struct tty_struct *tty, struct file *file,
  1443. unsigned int cmd, unsigned long arg)
  1444. {
  1445. struct mxser_port *info = tty->driver_data;
  1446. struct async_icount cnow;
  1447. unsigned long flags;
  1448. void __user *argp = (void __user *)arg;
  1449. int retval;
  1450. if (tty->index == MXSER_PORTS)
  1451. return mxser_ioctl_special(cmd, argp);
  1452. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1453. int p;
  1454. unsigned long opmode;
  1455. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1456. int shiftbit;
  1457. unsigned char val, mask;
  1458. p = tty->index % 4;
  1459. if (cmd == MOXA_SET_OP_MODE) {
  1460. if (get_user(opmode, (int __user *) argp))
  1461. return -EFAULT;
  1462. if (opmode != RS232_MODE &&
  1463. opmode != RS485_2WIRE_MODE &&
  1464. opmode != RS422_MODE &&
  1465. opmode != RS485_4WIRE_MODE)
  1466. return -EFAULT;
  1467. lock_kernel();
  1468. mask = ModeMask[p];
  1469. shiftbit = p * 2;
  1470. val = inb(info->opmode_ioaddr);
  1471. val &= mask;
  1472. val |= (opmode << shiftbit);
  1473. outb(val, info->opmode_ioaddr);
  1474. unlock_kernel();
  1475. } else {
  1476. lock_kernel();
  1477. shiftbit = p * 2;
  1478. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1479. opmode &= OP_MODE_MASK;
  1480. unlock_kernel();
  1481. if (put_user(opmode, (int __user *)argp))
  1482. return -EFAULT;
  1483. }
  1484. return 0;
  1485. }
  1486. if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
  1487. test_bit(TTY_IO_ERROR, &tty->flags))
  1488. return -EIO;
  1489. switch (cmd) {
  1490. case TIOCGSERIAL:
  1491. lock_kernel();
  1492. retval = mxser_get_serial_info(tty, argp);
  1493. unlock_kernel();
  1494. return retval;
  1495. case TIOCSSERIAL:
  1496. lock_kernel();
  1497. retval = mxser_set_serial_info(tty, argp);
  1498. unlock_kernel();
  1499. return retval;
  1500. case TIOCSERGETLSR: /* Get line status register */
  1501. return mxser_get_lsr_info(info, argp);
  1502. /*
  1503. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1504. * - mask passed in arg for lines of interest
  1505. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1506. * Caller should use TIOCGICOUNT to see which one it was
  1507. */
  1508. case TIOCMIWAIT:
  1509. spin_lock_irqsave(&info->slock, flags);
  1510. cnow = info->icount; /* note the counters on entry */
  1511. spin_unlock_irqrestore(&info->slock, flags);
  1512. return wait_event_interruptible(info->port.delta_msr_wait,
  1513. mxser_cflags_changed(info, arg, &cnow));
  1514. /*
  1515. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1516. * Return: write counters to the user passed counter struct
  1517. * NB: both 1->0 and 0->1 transitions are counted except for
  1518. * RI where only 0->1 is counted.
  1519. */
  1520. case TIOCGICOUNT: {
  1521. struct serial_icounter_struct icnt = { 0 };
  1522. spin_lock_irqsave(&info->slock, flags);
  1523. cnow = info->icount;
  1524. spin_unlock_irqrestore(&info->slock, flags);
  1525. icnt.frame = cnow.frame;
  1526. icnt.brk = cnow.brk;
  1527. icnt.overrun = cnow.overrun;
  1528. icnt.buf_overrun = cnow.buf_overrun;
  1529. icnt.parity = cnow.parity;
  1530. icnt.rx = cnow.rx;
  1531. icnt.tx = cnow.tx;
  1532. icnt.cts = cnow.cts;
  1533. icnt.dsr = cnow.dsr;
  1534. icnt.rng = cnow.rng;
  1535. icnt.dcd = cnow.dcd;
  1536. return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
  1537. }
  1538. case MOXA_HighSpeedOn:
  1539. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
  1540. case MOXA_SDS_RSTICOUNTER:
  1541. lock_kernel();
  1542. info->mon_data.rxcnt = 0;
  1543. info->mon_data.txcnt = 0;
  1544. unlock_kernel();
  1545. return 0;
  1546. case MOXA_ASPP_OQUEUE:{
  1547. int len, lsr;
  1548. lock_kernel();
  1549. len = mxser_chars_in_buffer(tty);
  1550. lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
  1551. len += (lsr ? 0 : 1);
  1552. unlock_kernel();
  1553. return put_user(len, (int __user *)argp);
  1554. }
  1555. case MOXA_ASPP_MON: {
  1556. int mcr, status;
  1557. lock_kernel();
  1558. status = mxser_get_msr(info->ioaddr, 1, tty->index);
  1559. mxser_check_modem_status(tty, info, status);
  1560. mcr = inb(info->ioaddr + UART_MCR);
  1561. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1562. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1563. else
  1564. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1565. if (mcr & MOXA_MUST_MCR_TX_XON)
  1566. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1567. else
  1568. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1569. if (tty->hw_stopped)
  1570. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1571. else
  1572. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1573. unlock_kernel();
  1574. if (copy_to_user(argp, &info->mon_data,
  1575. sizeof(struct mxser_mon)))
  1576. return -EFAULT;
  1577. return 0;
  1578. }
  1579. case MOXA_ASPP_LSTATUS: {
  1580. if (put_user(info->err_shadow, (unsigned char __user *)argp))
  1581. return -EFAULT;
  1582. info->err_shadow = 0;
  1583. return 0;
  1584. }
  1585. case MOXA_SET_BAUD_METHOD: {
  1586. int method;
  1587. if (get_user(method, (int __user *)argp))
  1588. return -EFAULT;
  1589. mxser_set_baud_method[tty->index] = method;
  1590. return put_user(method, (int __user *)argp);
  1591. }
  1592. default:
  1593. return -ENOIOCTLCMD;
  1594. }
  1595. return 0;
  1596. }
  1597. static void mxser_stoprx(struct tty_struct *tty)
  1598. {
  1599. struct mxser_port *info = tty->driver_data;
  1600. info->ldisc_stop_rx = 1;
  1601. if (I_IXOFF(tty)) {
  1602. if (info->board->chip_flag) {
  1603. info->IER &= ~MOXA_MUST_RECV_ISR;
  1604. outb(info->IER, info->ioaddr + UART_IER);
  1605. } else {
  1606. info->x_char = STOP_CHAR(tty);
  1607. outb(0, info->ioaddr + UART_IER);
  1608. info->IER |= UART_IER_THRI;
  1609. outb(info->IER, info->ioaddr + UART_IER);
  1610. }
  1611. }
  1612. if (tty->termios->c_cflag & CRTSCTS) {
  1613. info->MCR &= ~UART_MCR_RTS;
  1614. outb(info->MCR, info->ioaddr + UART_MCR);
  1615. }
  1616. }
  1617. /*
  1618. * This routine is called by the upper-layer tty layer to signal that
  1619. * incoming characters should be throttled.
  1620. */
  1621. static void mxser_throttle(struct tty_struct *tty)
  1622. {
  1623. mxser_stoprx(tty);
  1624. }
  1625. static void mxser_unthrottle(struct tty_struct *tty)
  1626. {
  1627. struct mxser_port *info = tty->driver_data;
  1628. /* startrx */
  1629. info->ldisc_stop_rx = 0;
  1630. if (I_IXOFF(tty)) {
  1631. if (info->x_char)
  1632. info->x_char = 0;
  1633. else {
  1634. if (info->board->chip_flag) {
  1635. info->IER |= MOXA_MUST_RECV_ISR;
  1636. outb(info->IER, info->ioaddr + UART_IER);
  1637. } else {
  1638. info->x_char = START_CHAR(tty);
  1639. outb(0, info->ioaddr + UART_IER);
  1640. info->IER |= UART_IER_THRI;
  1641. outb(info->IER, info->ioaddr + UART_IER);
  1642. }
  1643. }
  1644. }
  1645. if (tty->termios->c_cflag & CRTSCTS) {
  1646. info->MCR |= UART_MCR_RTS;
  1647. outb(info->MCR, info->ioaddr + UART_MCR);
  1648. }
  1649. }
  1650. /*
  1651. * mxser_stop() and mxser_start()
  1652. *
  1653. * This routines are called before setting or resetting tty->stopped.
  1654. * They enable or disable transmitter interrupts, as necessary.
  1655. */
  1656. static void mxser_stop(struct tty_struct *tty)
  1657. {
  1658. struct mxser_port *info = tty->driver_data;
  1659. unsigned long flags;
  1660. spin_lock_irqsave(&info->slock, flags);
  1661. if (info->IER & UART_IER_THRI) {
  1662. info->IER &= ~UART_IER_THRI;
  1663. outb(info->IER, info->ioaddr + UART_IER);
  1664. }
  1665. spin_unlock_irqrestore(&info->slock, flags);
  1666. }
  1667. static void mxser_start(struct tty_struct *tty)
  1668. {
  1669. struct mxser_port *info = tty->driver_data;
  1670. unsigned long flags;
  1671. spin_lock_irqsave(&info->slock, flags);
  1672. if (info->xmit_cnt && info->port.xmit_buf) {
  1673. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1674. info->IER |= UART_IER_THRI;
  1675. outb(info->IER, info->ioaddr + UART_IER);
  1676. }
  1677. spin_unlock_irqrestore(&info->slock, flags);
  1678. }
  1679. static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1680. {
  1681. struct mxser_port *info = tty->driver_data;
  1682. unsigned long flags;
  1683. spin_lock_irqsave(&info->slock, flags);
  1684. mxser_change_speed(tty, old_termios);
  1685. spin_unlock_irqrestore(&info->slock, flags);
  1686. if ((old_termios->c_cflag & CRTSCTS) &&
  1687. !(tty->termios->c_cflag & CRTSCTS)) {
  1688. tty->hw_stopped = 0;
  1689. mxser_start(tty);
  1690. }
  1691. /* Handle sw stopped */
  1692. if ((old_termios->c_iflag & IXON) &&
  1693. !(tty->termios->c_iflag & IXON)) {
  1694. tty->stopped = 0;
  1695. if (info->board->chip_flag) {
  1696. spin_lock_irqsave(&info->slock, flags);
  1697. mxser_disable_must_rx_software_flow_control(
  1698. info->ioaddr);
  1699. spin_unlock_irqrestore(&info->slock, flags);
  1700. }
  1701. mxser_start(tty);
  1702. }
  1703. }
  1704. /*
  1705. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1706. */
  1707. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1708. {
  1709. struct mxser_port *info = tty->driver_data;
  1710. unsigned long orig_jiffies, char_time;
  1711. int lsr;
  1712. if (info->type == PORT_UNKNOWN)
  1713. return;
  1714. if (info->xmit_fifo_size == 0)
  1715. return; /* Just in case.... */
  1716. orig_jiffies = jiffies;
  1717. /*
  1718. * Set the check interval to be 1/5 of the estimated time to
  1719. * send a single character, and make it at least 1. The check
  1720. * interval should also be less than the timeout.
  1721. *
  1722. * Note: we have to use pretty tight timings here to satisfy
  1723. * the NIST-PCTS.
  1724. */
  1725. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1726. char_time = char_time / 5;
  1727. if (char_time == 0)
  1728. char_time = 1;
  1729. if (timeout && timeout < char_time)
  1730. char_time = timeout;
  1731. /*
  1732. * If the transmitter hasn't cleared in twice the approximate
  1733. * amount of time to send the entire FIFO, it probably won't
  1734. * ever clear. This assumes the UART isn't doing flow
  1735. * control, which is currently the case. Hence, if it ever
  1736. * takes longer than info->timeout, this is probably due to a
  1737. * UART bug of some kind. So, we clamp the timeout parameter at
  1738. * 2*info->timeout.
  1739. */
  1740. if (!timeout || timeout > 2 * info->timeout)
  1741. timeout = 2 * info->timeout;
  1742. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1743. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
  1744. timeout, char_time);
  1745. printk("jiff=%lu...", jiffies);
  1746. #endif
  1747. lock_kernel();
  1748. while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
  1749. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1750. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1751. #endif
  1752. schedule_timeout_interruptible(char_time);
  1753. if (signal_pending(current))
  1754. break;
  1755. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1756. break;
  1757. }
  1758. set_current_state(TASK_RUNNING);
  1759. unlock_kernel();
  1760. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1761. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1762. #endif
  1763. }
  1764. /*
  1765. * This routine is called by tty_hangup() when a hangup is signaled.
  1766. */
  1767. static void mxser_hangup(struct tty_struct *tty)
  1768. {
  1769. struct mxser_port *info = tty->driver_data;
  1770. mxser_flush_buffer(tty);
  1771. mxser_shutdown(tty);
  1772. tty_port_hangup(&info->port);
  1773. }
  1774. /*
  1775. * mxser_rs_break() --- routine which turns the break handling on or off
  1776. */
  1777. static int mxser_rs_break(struct tty_struct *tty, int break_state)
  1778. {
  1779. struct mxser_port *info = tty->driver_data;
  1780. unsigned long flags;
  1781. spin_lock_irqsave(&info->slock, flags);
  1782. if (break_state == -1)
  1783. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1784. info->ioaddr + UART_LCR);
  1785. else
  1786. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1787. info->ioaddr + UART_LCR);
  1788. spin_unlock_irqrestore(&info->slock, flags);
  1789. return 0;
  1790. }
  1791. static void mxser_receive_chars(struct tty_struct *tty,
  1792. struct mxser_port *port, int *status)
  1793. {
  1794. unsigned char ch, gdl;
  1795. int ignored = 0;
  1796. int cnt = 0;
  1797. int recv_room;
  1798. int max = 256;
  1799. recv_room = tty->receive_room;
  1800. if (recv_room == 0 && !port->ldisc_stop_rx)
  1801. mxser_stoprx(tty);
  1802. if (port->board->chip_flag != MOXA_OTHER_UART) {
  1803. if (*status & UART_LSR_SPECIAL)
  1804. goto intr_old;
  1805. if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
  1806. (*status & MOXA_MUST_LSR_RERR))
  1807. goto intr_old;
  1808. if (*status & MOXA_MUST_LSR_RERR)
  1809. goto intr_old;
  1810. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1811. if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
  1812. gdl &= MOXA_MUST_GDL_MASK;
  1813. if (gdl >= recv_room) {
  1814. if (!port->ldisc_stop_rx)
  1815. mxser_stoprx(tty);
  1816. }
  1817. while (gdl--) {
  1818. ch = inb(port->ioaddr + UART_RX);
  1819. tty_insert_flip_char(tty, ch, 0);
  1820. cnt++;
  1821. }
  1822. goto end_intr;
  1823. }
  1824. intr_old:
  1825. do {
  1826. if (max-- < 0)
  1827. break;
  1828. ch = inb(port->ioaddr + UART_RX);
  1829. if (port->board->chip_flag && (*status & UART_LSR_OE))
  1830. outb(0x23, port->ioaddr + UART_FCR);
  1831. *status &= port->read_status_mask;
  1832. if (*status & port->ignore_status_mask) {
  1833. if (++ignored > 100)
  1834. break;
  1835. } else {
  1836. char flag = 0;
  1837. if (*status & UART_LSR_SPECIAL) {
  1838. if (*status & UART_LSR_BI) {
  1839. flag = TTY_BREAK;
  1840. port->icount.brk++;
  1841. if (port->port.flags & ASYNC_SAK)
  1842. do_SAK(tty);
  1843. } else if (*status & UART_LSR_PE) {
  1844. flag = TTY_PARITY;
  1845. port->icount.parity++;
  1846. } else if (*status & UART_LSR_FE) {
  1847. flag = TTY_FRAME;
  1848. port->icount.frame++;
  1849. } else if (*status & UART_LSR_OE) {
  1850. flag = TTY_OVERRUN;
  1851. port->icount.overrun++;
  1852. } else
  1853. flag = TTY_BREAK;
  1854. }
  1855. tty_insert_flip_char(tty, ch, flag);
  1856. cnt++;
  1857. if (cnt >= recv_room) {
  1858. if (!port->ldisc_stop_rx)
  1859. mxser_stoprx(tty);
  1860. break;
  1861. }
  1862. }
  1863. if (port->board->chip_flag)
  1864. break;
  1865. *status = inb(port->ioaddr + UART_LSR);
  1866. } while (*status & UART_LSR_DR);
  1867. end_intr:
  1868. mxvar_log.rxcnt[tty->index] += cnt;
  1869. port->mon_data.rxcnt += cnt;
  1870. port->mon_data.up_rxcnt += cnt;
  1871. /*
  1872. * We are called from an interrupt context with &port->slock
  1873. * being held. Drop it temporarily in order to prevent
  1874. * recursive locking.
  1875. */
  1876. spin_unlock(&port->slock);
  1877. tty_flip_buffer_push(tty);
  1878. spin_lock(&port->slock);
  1879. }
  1880. static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
  1881. {
  1882. int count, cnt;
  1883. if (port->x_char) {
  1884. outb(port->x_char, port->ioaddr + UART_TX);
  1885. port->x_char = 0;
  1886. mxvar_log.txcnt[tty->index]++;
  1887. port->mon_data.txcnt++;
  1888. port->mon_data.up_txcnt++;
  1889. port->icount.tx++;
  1890. return;
  1891. }
  1892. if (port->port.xmit_buf == NULL)
  1893. return;
  1894. if (port->xmit_cnt <= 0 || tty->stopped ||
  1895. (tty->hw_stopped &&
  1896. (port->type != PORT_16550A) &&
  1897. (!port->board->chip_flag))) {
  1898. port->IER &= ~UART_IER_THRI;
  1899. outb(port->IER, port->ioaddr + UART_IER);
  1900. return;
  1901. }
  1902. cnt = port->xmit_cnt;
  1903. count = port->xmit_fifo_size;
  1904. do {
  1905. outb(port->port.xmit_buf[port->xmit_tail++],
  1906. port->ioaddr + UART_TX);
  1907. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  1908. if (--port->xmit_cnt <= 0)
  1909. break;
  1910. } while (--count > 0);
  1911. mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
  1912. port->mon_data.txcnt += (cnt - port->xmit_cnt);
  1913. port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
  1914. port->icount.tx += (cnt - port->xmit_cnt);
  1915. if (port->xmit_cnt < WAKEUP_CHARS && tty)
  1916. tty_wakeup(tty);
  1917. if (port->xmit_cnt <= 0) {
  1918. port->IER &= ~UART_IER_THRI;
  1919. outb(port->IER, port->ioaddr + UART_IER);
  1920. }
  1921. }
  1922. /*
  1923. * This is the serial driver's generic interrupt routine
  1924. */
  1925. static irqreturn_t mxser_interrupt(int irq, void *dev_id)
  1926. {
  1927. int status, iir, i;
  1928. struct mxser_board *brd = NULL;
  1929. struct mxser_port *port;
  1930. int max, irqbits, bits, msr;
  1931. unsigned int int_cnt, pass_counter = 0;
  1932. int handled = IRQ_NONE;
  1933. struct tty_struct *tty;
  1934. for (i = 0; i < MXSER_BOARDS; i++)
  1935. if (dev_id == &mxser_boards[i]) {
  1936. brd = dev_id;
  1937. break;
  1938. }
  1939. if (i == MXSER_BOARDS)
  1940. goto irq_stop;
  1941. if (brd == NULL)
  1942. goto irq_stop;
  1943. max = brd->info->nports;
  1944. while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
  1945. irqbits = inb(brd->vector) & brd->vector_mask;
  1946. if (irqbits == brd->vector_mask)
  1947. break;
  1948. handled = IRQ_HANDLED;
  1949. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  1950. if (irqbits == brd->vector_mask)
  1951. break;
  1952. if (bits & irqbits)
  1953. continue;
  1954. port = &brd->ports[i];
  1955. int_cnt = 0;
  1956. spin_lock(&port->slock);
  1957. do {
  1958. iir = inb(port->ioaddr + UART_IIR);
  1959. if (iir & UART_IIR_NO_INT)
  1960. break;
  1961. iir &= MOXA_MUST_IIR_MASK;
  1962. tty = tty_port_tty_get(&port->port);
  1963. if (!tty ||
  1964. (port->port.flags & ASYNC_CLOSING) ||
  1965. !(port->port.flags &
  1966. ASYNC_INITIALIZED)) {
  1967. status = inb(port->ioaddr + UART_LSR);
  1968. outb(0x27, port->ioaddr + UART_FCR);
  1969. inb(port->ioaddr + UART_MSR);
  1970. tty_kref_put(tty);
  1971. break;
  1972. }
  1973. status = inb(port->ioaddr + UART_LSR);
  1974. if (status & UART_LSR_PE)
  1975. port->err_shadow |= NPPI_NOTIFY_PARITY;
  1976. if (status & UART_LSR_FE)
  1977. port->err_shadow |= NPPI_NOTIFY_FRAMING;
  1978. if (status & UART_LSR_OE)
  1979. port->err_shadow |=
  1980. NPPI_NOTIFY_HW_OVERRUN;
  1981. if (status & UART_LSR_BI)
  1982. port->err_shadow |= NPPI_NOTIFY_BREAK;
  1983. if (port->board->chip_flag) {
  1984. if (iir == MOXA_MUST_IIR_GDA ||
  1985. iir == MOXA_MUST_IIR_RDA ||
  1986. iir == MOXA_MUST_IIR_RTO ||
  1987. iir == MOXA_MUST_IIR_LSR)
  1988. mxser_receive_chars(tty, port,
  1989. &status);
  1990. } else {
  1991. status &= port->read_status_mask;
  1992. if (status & UART_LSR_DR)
  1993. mxser_receive_chars(tty, port,
  1994. &status);
  1995. }
  1996. msr = inb(port->ioaddr + UART_MSR);
  1997. if (msr & UART_MSR_ANY_DELTA)
  1998. mxser_check_modem_status(tty, port, msr);
  1999. if (port->board->chip_flag) {
  2000. if (iir == 0x02 && (status &
  2001. UART_LSR_THRE))
  2002. mxser_transmit_chars(tty, port);
  2003. } else {
  2004. if (status & UART_LSR_THRE)
  2005. mxser_transmit_chars(tty, port);
  2006. }
  2007. tty_kref_put(tty);
  2008. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  2009. spin_unlock(&port->slock);
  2010. }
  2011. }
  2012. irq_stop:
  2013. return handled;
  2014. }
  2015. static const struct tty_operations mxser_ops = {
  2016. .open = mxser_open,
  2017. .close = mxser_close,
  2018. .write = mxser_write,
  2019. .put_char = mxser_put_char,
  2020. .flush_chars = mxser_flush_chars,
  2021. .write_room = mxser_write_room,
  2022. .chars_in_buffer = mxser_chars_in_buffer,
  2023. .flush_buffer = mxser_flush_buffer,
  2024. .ioctl = mxser_ioctl,
  2025. .throttle = mxser_throttle,
  2026. .unthrottle = mxser_unthrottle,
  2027. .set_termios = mxser_set_termios,
  2028. .stop = mxser_stop,
  2029. .start = mxser_start,
  2030. .hangup = mxser_hangup,
  2031. .break_ctl = mxser_rs_break,
  2032. .wait_until_sent = mxser_wait_until_sent,
  2033. .tiocmget = mxser_tiocmget,
  2034. .tiocmset = mxser_tiocmset,
  2035. };
  2036. struct tty_port_operations mxser_port_ops = {
  2037. .carrier_raised = mxser_carrier_raised,
  2038. .dtr_rts = mxser_dtr_rts,
  2039. };
  2040. /*
  2041. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  2042. */
  2043. static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
  2044. unsigned int irq)
  2045. {
  2046. if (irq)
  2047. free_irq(brd->irq, brd);
  2048. if (pdev != NULL) { /* PCI */
  2049. #ifdef CONFIG_PCI
  2050. pci_release_region(pdev, 2);
  2051. pci_release_region(pdev, 3);
  2052. #endif
  2053. } else {
  2054. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2055. release_region(brd->vector, 1);
  2056. }
  2057. }
  2058. static int __devinit mxser_initbrd(struct mxser_board *brd,
  2059. struct pci_dev *pdev)
  2060. {
  2061. struct mxser_port *info;
  2062. unsigned int i;
  2063. int retval;
  2064. printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
  2065. brd->ports[0].max_baud);
  2066. for (i = 0; i < brd->info->nports; i++) {
  2067. info = &brd->ports[i];
  2068. tty_port_init(&info->port);
  2069. info->port.ops = &mxser_port_ops;
  2070. info->board = brd;
  2071. info->stop_rx = 0;
  2072. info->ldisc_stop_rx = 0;
  2073. /* Enhance mode enabled here */
  2074. if (brd->chip_flag != MOXA_OTHER_UART)
  2075. mxser_enable_must_enchance_mode(info->ioaddr);
  2076. info->port.flags = ASYNC_SHARE_IRQ;
  2077. info->type = brd->uart_type;
  2078. process_txrx_fifo(info);
  2079. info->custom_divisor = info->baud_base * 16;
  2080. info->port.close_delay = 5 * HZ / 10;
  2081. info->port.closing_wait = 30 * HZ;
  2082. info->normal_termios = mxvar_sdriver->init_termios;
  2083. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  2084. info->err_shadow = 0;
  2085. spin_lock_init(&info->slock);
  2086. /* before set INT ISR, disable all int */
  2087. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  2088. info->ioaddr + UART_IER);
  2089. }
  2090. retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
  2091. brd);
  2092. if (retval) {
  2093. printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
  2094. "conflict with another device.\n",
  2095. brd->info->name, brd->irq);
  2096. /* We hold resources, we need to release them. */
  2097. mxser_release_res(brd, pdev, 0);
  2098. }
  2099. return retval;
  2100. }
  2101. static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
  2102. {
  2103. int id, i, bits;
  2104. unsigned short regs[16], irq;
  2105. unsigned char scratch, scratch2;
  2106. brd->chip_flag = MOXA_OTHER_UART;
  2107. id = mxser_read_register(cap, regs);
  2108. switch (id) {
  2109. case C168_ASIC_ID:
  2110. brd->info = &mxser_cards[0];
  2111. break;
  2112. case C104_ASIC_ID:
  2113. brd->info = &mxser_cards[1];
  2114. break;
  2115. case CI104J_ASIC_ID:
  2116. brd->info = &mxser_cards[2];
  2117. break;
  2118. case C102_ASIC_ID:
  2119. brd->info = &mxser_cards[5];
  2120. break;
  2121. case CI132_ASIC_ID:
  2122. brd->info = &mxser_cards[6];
  2123. break;
  2124. case CI134_ASIC_ID:
  2125. brd->info = &mxser_cards[7];
  2126. break;
  2127. default:
  2128. return 0;
  2129. }
  2130. irq = 0;
  2131. /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
  2132. Flag-hack checks if configuration should be read as 2-port here. */
  2133. if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
  2134. irq = regs[9] & 0xF000;
  2135. irq = irq | (irq >> 4);
  2136. if (irq != (regs[9] & 0xFF00))
  2137. goto err_irqconflict;
  2138. } else if (brd->info->nports == 4) {
  2139. irq = regs[9] & 0xF000;
  2140. irq = irq | (irq >> 4);
  2141. irq = irq | (irq >> 8);
  2142. if (irq != regs[9])
  2143. goto err_irqconflict;
  2144. } else if (brd->info->nports == 8) {
  2145. irq = regs[9] & 0xF000;
  2146. irq = irq | (irq >> 4);
  2147. irq = irq | (irq >> 8);
  2148. if ((irq != regs[9]) || (irq != regs[10]))
  2149. goto err_irqconflict;
  2150. }
  2151. if (!irq) {
  2152. printk(KERN_ERR "mxser: interrupt number unset\n");
  2153. return -EIO;
  2154. }
  2155. brd->irq = ((int)(irq & 0xF000) >> 12);
  2156. for (i = 0; i < 8; i++)
  2157. brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
  2158. if ((regs[12] & 0x80) == 0) {
  2159. printk(KERN_ERR "mxser: invalid interrupt vector\n");
  2160. return -EIO;
  2161. }
  2162. brd->vector = (int)regs[11]; /* interrupt vector */
  2163. if (id == 1)
  2164. brd->vector_mask = 0x00FF;
  2165. else
  2166. brd->vector_mask = 0x000F;
  2167. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2168. if (regs[12] & bits) {
  2169. brd->ports[i].baud_base = 921600;
  2170. brd->ports[i].max_baud = 921600;
  2171. } else {
  2172. brd->ports[i].baud_base = 115200;
  2173. brd->ports[i].max_baud = 115200;
  2174. }
  2175. }
  2176. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2177. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2178. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2179. outb(scratch2, cap + UART_LCR);
  2180. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2181. scratch = inb(cap + UART_IIR);
  2182. if (scratch & 0xC0)
  2183. brd->uart_type = PORT_16550A;
  2184. else
  2185. brd->uart_type = PORT_16450;
  2186. if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
  2187. "mxser(IO)")) {
  2188. printk(KERN_ERR "mxser: can't request ports I/O region: "
  2189. "0x%.8lx-0x%.8lx\n",
  2190. brd->ports[0].ioaddr, brd->ports[0].ioaddr +
  2191. 8 * brd->info->nports - 1);
  2192. return -EIO;
  2193. }
  2194. if (!request_region(brd->vector, 1, "mxser(vector)")) {
  2195. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2196. printk(KERN_ERR "mxser: can't request interrupt vector region: "
  2197. "0x%.8lx-0x%.8lx\n",
  2198. brd->ports[0].ioaddr, brd->ports[0].ioaddr +
  2199. 8 * brd->info->nports - 1);
  2200. return -EIO;
  2201. }
  2202. return brd->info->nports;
  2203. err_irqconflict:
  2204. printk(KERN_ERR "mxser: invalid interrupt number\n");
  2205. return -EIO;
  2206. }
  2207. static int __devinit mxser_probe(struct pci_dev *pdev,
  2208. const struct pci_device_id *ent)
  2209. {
  2210. #ifdef CONFIG_PCI
  2211. struct mxser_board *brd;
  2212. unsigned int i, j;
  2213. unsigned long ioaddress;
  2214. int retval = -EINVAL;
  2215. for (i = 0; i < MXSER_BOARDS; i++)
  2216. if (mxser_boards[i].info == NULL)
  2217. break;
  2218. if (i >= MXSER_BOARDS) {
  2219. dev_err(&pdev->dev, "too many boards found (maximum %d), board "
  2220. "not configured\n", MXSER_BOARDS);
  2221. goto err;
  2222. }
  2223. brd = &mxser_boards[i];
  2224. brd->idx = i * MXSER_PORTS_PER_BOARD;
  2225. dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
  2226. mxser_cards[ent->driver_data].name,
  2227. pdev->bus->number, PCI_SLOT(pdev->devfn));
  2228. retval = pci_enable_device(pdev);
  2229. if (retval) {
  2230. dev_err(&pdev->dev, "PCI enable failed\n");
  2231. goto err;
  2232. }
  2233. /* io address */
  2234. ioaddress = pci_resource_start(pdev, 2);
  2235. retval = pci_request_region(pdev, 2, "mxser(IO)");
  2236. if (retval)
  2237. goto err;
  2238. brd->info = &mxser_cards[ent->driver_data];
  2239. for (i = 0; i < brd->info->nports; i++)
  2240. brd->ports[i].ioaddr = ioaddress + 8 * i;
  2241. /* vector */
  2242. ioaddress = pci_resource_start(pdev, 3);
  2243. retval = pci_request_region(pdev, 3, "mxser(vector)");
  2244. if (retval)
  2245. goto err_relio;
  2246. brd->vector = ioaddress;
  2247. /* irq */
  2248. brd->irq = pdev->irq;
  2249. brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
  2250. brd->uart_type = PORT_16550A;
  2251. brd->vector_mask = 0;
  2252. for (i = 0; i < brd->info->nports; i++) {
  2253. for (j = 0; j < UART_INFO_NUM; j++) {
  2254. if (Gpci_uart_info[j].type == brd->chip_flag) {
  2255. brd->ports[i].max_baud =
  2256. Gpci_uart_info[j].max_baud;
  2257. /* exception....CP-102 */
  2258. if (brd->info->flags & MXSER_HIGHBAUD)
  2259. brd->ports[i].max_baud = 921600;
  2260. break;
  2261. }
  2262. }
  2263. }
  2264. if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
  2265. for (i = 0; i < brd->info->nports; i++) {
  2266. if (i < 4)
  2267. brd->ports[i].opmode_ioaddr = ioaddress + 4;
  2268. else
  2269. brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
  2270. }
  2271. outb(0, ioaddress + 4); /* default set to RS232 mode */
  2272. outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
  2273. }
  2274. for (i = 0; i < brd->info->nports; i++) {
  2275. brd->vector_mask |= (1 << i);
  2276. brd->ports[i].baud_base = 921600;
  2277. }
  2278. /* mxser_initbrd will hook ISR. */
  2279. retval = mxser_initbrd(brd, pdev);
  2280. if (retval)
  2281. goto err_null;
  2282. for (i = 0; i < brd->info->nports; i++)
  2283. tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
  2284. pci_set_drvdata(pdev, brd);
  2285. return 0;
  2286. err_relio:
  2287. pci_release_region(pdev, 2);
  2288. err_null:
  2289. brd->info = NULL;
  2290. err:
  2291. return retval;
  2292. #else
  2293. return -ENODEV;
  2294. #endif
  2295. }
  2296. static void __devexit mxser_remove(struct pci_dev *pdev)
  2297. {
  2298. struct mxser_board *brd = pci_get_drvdata(pdev);
  2299. unsigned int i;
  2300. for (i = 0; i < brd->info->nports; i++)
  2301. tty_unregister_device(mxvar_sdriver, brd->idx + i);
  2302. mxser_release_res(brd, pdev, 1);
  2303. brd->info = NULL;
  2304. }
  2305. static struct pci_driver mxser_driver = {
  2306. .name = "mxser",
  2307. .id_table = mxser_pcibrds,
  2308. .probe = mxser_probe,
  2309. .remove = __devexit_p(mxser_remove)
  2310. };
  2311. static int __init mxser_module_init(void)
  2312. {
  2313. struct mxser_board *brd;
  2314. unsigned int b, i, m;
  2315. int retval;
  2316. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  2317. if (!mxvar_sdriver)
  2318. return -ENOMEM;
  2319. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
  2320. MXSER_VERSION);
  2321. /* Initialize the tty_driver structure */
  2322. mxvar_sdriver->owner = THIS_MODULE;
  2323. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  2324. mxvar_sdriver->name = "ttyMI";
  2325. mxvar_sdriver->major = ttymajor;
  2326. mxvar_sdriver->minor_start = 0;
  2327. mxvar_sdriver->num = MXSER_PORTS + 1;
  2328. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  2329. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  2330. mxvar_sdriver->init_termios = tty_std_termios;
  2331. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  2332. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
  2333. tty_set_operations(mxvar_sdriver, &mxser_ops);
  2334. retval = tty_register_driver(mxvar_sdriver);
  2335. if (retval) {
  2336. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
  2337. "tty driver !\n");
  2338. goto err_put;
  2339. }
  2340. /* Start finding ISA boards here */
  2341. for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
  2342. if (!ioaddr[b])
  2343. continue;
  2344. brd = &mxser_boards[m];
  2345. retval = mxser_get_ISA_conf(ioaddr[b], brd);
  2346. if (retval <= 0) {
  2347. brd->info = NULL;
  2348. continue;
  2349. }
  2350. printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
  2351. brd->info->name, ioaddr[b]);
  2352. /* mxser_initbrd will hook ISR. */
  2353. if (mxser_initbrd(brd, NULL) < 0) {
  2354. brd->info = NULL;
  2355. continue;
  2356. }
  2357. brd->idx = m * MXSER_PORTS_PER_BOARD;
  2358. for (i = 0; i < brd->info->nports; i++)
  2359. tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
  2360. m++;
  2361. }
  2362. retval = pci_register_driver(&mxser_driver);
  2363. if (retval) {
  2364. printk(KERN_ERR "mxser: can't register pci driver\n");
  2365. if (!m) {
  2366. retval = -ENODEV;
  2367. goto err_unr;
  2368. } /* else: we have some ISA cards under control */
  2369. }
  2370. return 0;
  2371. err_unr:
  2372. tty_unregister_driver(mxvar_sdriver);
  2373. err_put:
  2374. put_tty_driver(mxvar_sdriver);
  2375. return retval;
  2376. }
  2377. static void __exit mxser_module_exit(void)
  2378. {
  2379. unsigned int i, j;
  2380. pci_unregister_driver(&mxser_driver);
  2381. for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
  2382. if (mxser_boards[i].info != NULL)
  2383. for (j = 0; j < mxser_boards[i].info->nports; j++)
  2384. tty_unregister_device(mxvar_sdriver,
  2385. mxser_boards[i].idx + j);
  2386. tty_unregister_driver(mxvar_sdriver);
  2387. put_tty_driver(mxvar_sdriver);
  2388. for (i = 0; i < MXSER_BOARDS; i++)
  2389. if (mxser_boards[i].info != NULL)
  2390. mxser_release_res(&mxser_boards[i], NULL, 1);
  2391. }
  2392. module_init(mxser_module_init);
  2393. module_exit(mxser_module_exit);