mxser.c 69 KB

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