mxser_new.c 71 KB

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