mxser_new.c 70 KB

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