mxser_new.c 71 KB

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