mxser.c 69 KB

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