mxser_new.c 71 KB

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