mxser_new.c 70 KB

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