mxser_new.c 74 KB

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