mxser_new.c 71 KB

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