mxser_new.c 69 KB

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