moxa.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230
  1. /*****************************************************************************/
  2. /*
  3. * moxa.c -- MOXA Intellio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
  6. *
  7. * This code is loosely based on the Linux serial driver, written by
  8. * Linus Torvalds, Theodore T'so and others.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. /*
  25. * MOXA Intellio Series Driver
  26. * for : LINUX
  27. * date : 1999/1/7
  28. * version : 5.1
  29. */
  30. #include <linux/config.h>
  31. #include <linux/module.h>
  32. #include <linux/types.h>
  33. #include <linux/mm.h>
  34. #include <linux/ioport.h>
  35. #include <linux/errno.h>
  36. #include <linux/signal.h>
  37. #include <linux/sched.h>
  38. #include <linux/timer.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/major.h>
  43. #include <linux/string.h>
  44. #include <linux/fcntl.h>
  45. #include <linux/ptrace.h>
  46. #include <linux/serial.h>
  47. #include <linux/tty_driver.h>
  48. #include <linux/delay.h>
  49. #include <linux/pci.h>
  50. #include <linux/init.h>
  51. #include <linux/bitops.h>
  52. #include <asm/system.h>
  53. #include <asm/io.h>
  54. #include <asm/uaccess.h>
  55. #define MOXA_VERSION "5.1k"
  56. #define MOXAMAJOR 172
  57. #define MOXACUMAJOR 173
  58. #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
  59. #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
  60. #define MAX_BOARDS 4 /* Don't change this value */
  61. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  62. #define MAX_PORTS 128 /* Don't change this value */
  63. /*
  64. * Define the Moxa PCI vendor and device IDs.
  65. */
  66. #define MOXA_BUS_TYPE_ISA 0
  67. #define MOXA_BUS_TYPE_PCI 1
  68. #ifndef PCI_VENDOR_ID_MOXA
  69. #define PCI_VENDOR_ID_MOXA 0x1393
  70. #endif
  71. #ifndef PCI_DEVICE_ID_CP204J
  72. #define PCI_DEVICE_ID_CP204J 0x2040
  73. #endif
  74. #ifndef PCI_DEVICE_ID_C218
  75. #define PCI_DEVICE_ID_C218 0x2180
  76. #endif
  77. #ifndef PCI_DEVICE_ID_C320
  78. #define PCI_DEVICE_ID_C320 0x3200
  79. #endif
  80. enum {
  81. MOXA_BOARD_C218_PCI = 1,
  82. MOXA_BOARD_C218_ISA,
  83. MOXA_BOARD_C320_PCI,
  84. MOXA_BOARD_C320_ISA,
  85. MOXA_BOARD_CP204J,
  86. };
  87. static char *moxa_brdname[] =
  88. {
  89. "C218 Turbo PCI series",
  90. "C218 Turbo ISA series",
  91. "C320 Turbo PCI series",
  92. "C320 Turbo ISA series",
  93. "CP-204J series",
  94. };
  95. #ifdef CONFIG_PCI
  96. static struct pci_device_id moxa_pcibrds[] = {
  97. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
  98. 0, 0, MOXA_BOARD_C218_PCI },
  99. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
  100. 0, 0, MOXA_BOARD_C320_PCI },
  101. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
  102. 0, 0, MOXA_BOARD_CP204J },
  103. { 0 }
  104. };
  105. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  106. #endif /* CONFIG_PCI */
  107. typedef struct _moxa_isa_board_conf {
  108. int boardType;
  109. int numPorts;
  110. unsigned long baseAddr;
  111. } moxa_isa_board_conf;
  112. static moxa_isa_board_conf moxa_isa_boards[] =
  113. {
  114. /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
  115. };
  116. typedef struct _moxa_pci_devinfo {
  117. ushort busNum;
  118. ushort devNum;
  119. } moxa_pci_devinfo;
  120. typedef struct _moxa_board_conf {
  121. int boardType;
  122. int numPorts;
  123. unsigned long baseAddr;
  124. int busType;
  125. moxa_pci_devinfo pciInfo;
  126. } moxa_board_conf;
  127. static moxa_board_conf moxa_boards[MAX_BOARDS];
  128. static void __iomem *moxaBaseAddr[MAX_BOARDS];
  129. struct moxa_str {
  130. int type;
  131. int port;
  132. int close_delay;
  133. unsigned short closing_wait;
  134. int count;
  135. int blocked_open;
  136. long event; /* long req'd for set_bit --RR */
  137. int asyncflags;
  138. unsigned long statusflags;
  139. struct tty_struct *tty;
  140. int cflag;
  141. wait_queue_head_t open_wait;
  142. wait_queue_head_t close_wait;
  143. struct work_struct tqueue;
  144. };
  145. struct mxser_mstatus {
  146. tcflag_t cflag;
  147. int cts;
  148. int dsr;
  149. int ri;
  150. int dcd;
  151. };
  152. static struct mxser_mstatus GMStatus[MAX_PORTS];
  153. /* statusflags */
  154. #define TXSTOPPED 0x1
  155. #define LOWWAIT 0x2
  156. #define EMPTYWAIT 0x4
  157. #define THROTTLE 0x8
  158. /* event */
  159. #define MOXA_EVENT_HANGUP 1
  160. #define SERIAL_DO_RESTART
  161. #define SERIAL_TYPE_NORMAL 1
  162. #define WAKEUP_CHARS 256
  163. #define PORTNO(x) ((x)->index)
  164. static int verbose = 0;
  165. static int ttymajor = MOXAMAJOR;
  166. /* Variables for insmod */
  167. #ifdef MODULE
  168. static int baseaddr[] = {0, 0, 0, 0};
  169. static int type[] = {0, 0, 0, 0};
  170. static int numports[] = {0, 0, 0, 0};
  171. #endif
  172. MODULE_AUTHOR("William Chen");
  173. MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
  174. MODULE_LICENSE("GPL");
  175. #ifdef MODULE
  176. module_param_array(type, int, NULL, 0);
  177. module_param_array(baseaddr, int, NULL, 0);
  178. module_param_array(numports, int, NULL, 0);
  179. #endif
  180. module_param(ttymajor, int, 0);
  181. module_param(verbose, bool, 0644);
  182. static struct tty_driver *moxaDriver;
  183. static struct moxa_str moxaChannels[MAX_PORTS];
  184. static unsigned char *moxaXmitBuff;
  185. static int moxaTimer_on;
  186. static struct timer_list moxaTimer;
  187. static int moxaEmptyTimer_on[MAX_PORTS];
  188. static struct timer_list moxaEmptyTimer[MAX_PORTS];
  189. static struct semaphore moxaBuffSem;
  190. /*
  191. * static functions:
  192. */
  193. static void do_moxa_softint(void *);
  194. static int moxa_open(struct tty_struct *, struct file *);
  195. static void moxa_close(struct tty_struct *, struct file *);
  196. static int moxa_write(struct tty_struct *, const unsigned char *, int);
  197. static int moxa_write_room(struct tty_struct *);
  198. static void moxa_flush_buffer(struct tty_struct *);
  199. static int moxa_chars_in_buffer(struct tty_struct *);
  200. static void moxa_flush_chars(struct tty_struct *);
  201. static void moxa_put_char(struct tty_struct *, unsigned char);
  202. static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
  203. static void moxa_throttle(struct tty_struct *);
  204. static void moxa_unthrottle(struct tty_struct *);
  205. static void moxa_set_termios(struct tty_struct *, struct termios *);
  206. static void moxa_stop(struct tty_struct *);
  207. static void moxa_start(struct tty_struct *);
  208. static void moxa_hangup(struct tty_struct *);
  209. static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
  210. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  211. unsigned int set, unsigned int clear);
  212. static void moxa_poll(unsigned long);
  213. static void set_tty_param(struct tty_struct *);
  214. static int block_till_ready(struct tty_struct *, struct file *,
  215. struct moxa_str *);
  216. static void setup_empty_event(struct tty_struct *);
  217. static void check_xmit_empty(unsigned long);
  218. static void shut_down(struct moxa_str *);
  219. static void receive_data(struct moxa_str *);
  220. /*
  221. * moxa board interface functions:
  222. */
  223. static void MoxaDriverInit(void);
  224. static int MoxaDriverIoctl(unsigned int, unsigned long, int);
  225. static int MoxaDriverPoll(void);
  226. static int MoxaPortsOfCard(int);
  227. static int MoxaPortIsValid(int);
  228. static void MoxaPortEnable(int);
  229. static void MoxaPortDisable(int);
  230. static long MoxaPortGetMaxBaud(int);
  231. static long MoxaPortSetBaud(int, long);
  232. static int MoxaPortSetTermio(int, struct termios *);
  233. static int MoxaPortGetLineOut(int, int *, int *);
  234. static void MoxaPortLineCtrl(int, int, int);
  235. static void MoxaPortFlowCtrl(int, int, int, int, int, int);
  236. static int MoxaPortLineStatus(int);
  237. static int MoxaPortDCDChange(int);
  238. static int MoxaPortDCDON(int);
  239. static void MoxaPortFlushData(int, int);
  240. static int MoxaPortWriteData(int, unsigned char *, int);
  241. static int MoxaPortReadData(int, struct tty_struct *tty);
  242. static int MoxaPortTxQueue(int);
  243. static int MoxaPortRxQueue(int);
  244. static int MoxaPortTxFree(int);
  245. static void MoxaPortTxDisable(int);
  246. static void MoxaPortTxEnable(int);
  247. static int MoxaPortResetBrkCnt(int);
  248. static void MoxaPortSendBreak(int, int);
  249. static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
  250. static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
  251. static void MoxaSetFifo(int port, int enable);
  252. static struct tty_operations moxa_ops = {
  253. .open = moxa_open,
  254. .close = moxa_close,
  255. .write = moxa_write,
  256. .write_room = moxa_write_room,
  257. .flush_buffer = moxa_flush_buffer,
  258. .chars_in_buffer = moxa_chars_in_buffer,
  259. .flush_chars = moxa_flush_chars,
  260. .put_char = moxa_put_char,
  261. .ioctl = moxa_ioctl,
  262. .throttle = moxa_throttle,
  263. .unthrottle = moxa_unthrottle,
  264. .set_termios = moxa_set_termios,
  265. .stop = moxa_stop,
  266. .start = moxa_start,
  267. .hangup = moxa_hangup,
  268. .tiocmget = moxa_tiocmget,
  269. .tiocmset = moxa_tiocmset,
  270. };
  271. static spinlock_t moxa_lock = SPIN_LOCK_UNLOCKED;
  272. #ifdef CONFIG_PCI
  273. static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
  274. {
  275. board->baseAddr = pci_resource_start (p, 2);
  276. board->boardType = board_type;
  277. switch (board_type) {
  278. case MOXA_BOARD_C218_ISA:
  279. case MOXA_BOARD_C218_PCI:
  280. board->numPorts = 8;
  281. break;
  282. case MOXA_BOARD_CP204J:
  283. board->numPorts = 4;
  284. break;
  285. default:
  286. board->numPorts = 0;
  287. break;
  288. }
  289. board->busType = MOXA_BUS_TYPE_PCI;
  290. board->pciInfo.busNum = p->bus->number;
  291. board->pciInfo.devNum = p->devfn >> 3;
  292. return (0);
  293. }
  294. #endif /* CONFIG_PCI */
  295. static int __init moxa_init(void)
  296. {
  297. int i, numBoards;
  298. struct moxa_str *ch;
  299. printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
  300. moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
  301. if (!moxaDriver)
  302. return -ENOMEM;
  303. init_MUTEX(&moxaBuffSem);
  304. moxaDriver->owner = THIS_MODULE;
  305. moxaDriver->name = "ttyMX";
  306. moxaDriver->devfs_name = "tts/a";
  307. moxaDriver->major = ttymajor;
  308. moxaDriver->minor_start = 0;
  309. moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
  310. moxaDriver->subtype = SERIAL_TYPE_NORMAL;
  311. moxaDriver->init_termios = tty_std_termios;
  312. moxaDriver->init_termios.c_iflag = 0;
  313. moxaDriver->init_termios.c_oflag = 0;
  314. moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  315. moxaDriver->init_termios.c_lflag = 0;
  316. moxaDriver->flags = TTY_DRIVER_REAL_RAW;
  317. tty_set_operations(moxaDriver, &moxa_ops);
  318. moxaXmitBuff = NULL;
  319. for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
  320. ch->type = PORT_16550A;
  321. ch->port = i;
  322. INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
  323. ch->tty = NULL;
  324. ch->close_delay = 5 * HZ / 10;
  325. ch->closing_wait = 30 * HZ;
  326. ch->count = 0;
  327. ch->blocked_open = 0;
  328. ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  329. init_waitqueue_head(&ch->open_wait);
  330. init_waitqueue_head(&ch->close_wait);
  331. }
  332. for (i = 0; i < MAX_BOARDS; i++) {
  333. moxa_boards[i].boardType = 0;
  334. moxa_boards[i].numPorts = 0;
  335. moxa_boards[i].baseAddr = 0;
  336. moxa_boards[i].busType = 0;
  337. moxa_boards[i].pciInfo.busNum = 0;
  338. moxa_boards[i].pciInfo.devNum = 0;
  339. }
  340. MoxaDriverInit();
  341. printk("Tty devices major number = %d\n", ttymajor);
  342. if (tty_register_driver(moxaDriver)) {
  343. printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
  344. put_tty_driver(moxaDriver);
  345. return -1;
  346. }
  347. for (i = 0; i < MAX_PORTS; i++) {
  348. init_timer(&moxaEmptyTimer[i]);
  349. moxaEmptyTimer[i].function = check_xmit_empty;
  350. moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
  351. moxaEmptyTimer_on[i] = 0;
  352. }
  353. init_timer(&moxaTimer);
  354. moxaTimer.function = moxa_poll;
  355. moxaTimer.expires = jiffies + (HZ / 50);
  356. moxaTimer_on = 1;
  357. add_timer(&moxaTimer);
  358. /* Find the boards defined in source code */
  359. numBoards = 0;
  360. for (i = 0; i < MAX_BOARDS; i++) {
  361. if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
  362. (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
  363. moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
  364. if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
  365. moxa_boards[numBoards].numPorts = 8;
  366. else
  367. moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
  368. moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
  369. moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
  370. if (verbose)
  371. printk("Board %2d: %s board(baseAddr=%lx)\n",
  372. numBoards + 1,
  373. moxa_brdname[moxa_boards[numBoards].boardType - 1],
  374. moxa_boards[numBoards].baseAddr);
  375. numBoards++;
  376. }
  377. }
  378. /* Find the boards defined form module args. */
  379. #ifdef MODULE
  380. for (i = 0; i < MAX_BOARDS; i++) {
  381. if ((type[i] == MOXA_BOARD_C218_ISA) ||
  382. (type[i] == MOXA_BOARD_C320_ISA)) {
  383. if (verbose)
  384. printk("Board %2d: %s board(baseAddr=%lx)\n",
  385. numBoards + 1,
  386. moxa_brdname[type[i] - 1],
  387. (unsigned long) baseaddr[i]);
  388. if (numBoards >= MAX_BOARDS) {
  389. if (verbose)
  390. printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
  391. continue;
  392. }
  393. moxa_boards[numBoards].boardType = type[i];
  394. if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
  395. moxa_boards[numBoards].numPorts = 8;
  396. else
  397. moxa_boards[numBoards].numPorts = numports[i];
  398. moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
  399. moxa_boards[numBoards].baseAddr = baseaddr[i];
  400. numBoards++;
  401. }
  402. }
  403. #endif
  404. /* Find PCI boards here */
  405. #ifdef CONFIG_PCI
  406. {
  407. struct pci_dev *p = NULL;
  408. int n = ARRAY_SIZE(moxa_pcibrds) - 1;
  409. i = 0;
  410. while (i < n) {
  411. while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
  412. {
  413. if (pci_enable_device(p))
  414. continue;
  415. if (numBoards >= MAX_BOARDS) {
  416. if (verbose)
  417. printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
  418. } else {
  419. moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
  420. &moxa_boards[numBoards]);
  421. numBoards++;
  422. }
  423. }
  424. i++;
  425. }
  426. }
  427. #endif
  428. for (i = 0; i < numBoards; i++) {
  429. moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
  430. }
  431. return (0);
  432. }
  433. static void __exit moxa_exit(void)
  434. {
  435. int i;
  436. if (verbose)
  437. printk("Unloading module moxa ...\n");
  438. if (moxaTimer_on)
  439. del_timer(&moxaTimer);
  440. for (i = 0; i < MAX_PORTS; i++)
  441. if (moxaEmptyTimer_on[i])
  442. del_timer(&moxaEmptyTimer[i]);
  443. if (tty_unregister_driver(moxaDriver))
  444. printk("Couldn't unregister MOXA Intellio family serial driver\n");
  445. put_tty_driver(moxaDriver);
  446. if (verbose)
  447. printk("Done\n");
  448. }
  449. module_init(moxa_init);
  450. module_exit(moxa_exit);
  451. static void do_moxa_softint(void *private_)
  452. {
  453. struct moxa_str *ch = (struct moxa_str *) private_;
  454. struct tty_struct *tty;
  455. if (ch && (tty = ch->tty)) {
  456. if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
  457. tty_hangup(tty); /* FIXME: module removal race here - AKPM */
  458. wake_up_interruptible(&ch->open_wait);
  459. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  460. }
  461. }
  462. }
  463. static int moxa_open(struct tty_struct *tty, struct file *filp)
  464. {
  465. struct moxa_str *ch;
  466. int port;
  467. int retval;
  468. unsigned long page;
  469. port = PORTNO(tty);
  470. if (port == MAX_PORTS) {
  471. return (0);
  472. }
  473. if (!MoxaPortIsValid(port)) {
  474. tty->driver_data = NULL;
  475. return (-ENODEV);
  476. }
  477. down(&moxaBuffSem);
  478. if (!moxaXmitBuff) {
  479. page = get_zeroed_page(GFP_KERNEL);
  480. if (!page) {
  481. up(&moxaBuffSem);
  482. return (-ENOMEM);
  483. }
  484. /* This test is guarded by the BuffSem so no longer needed
  485. delete me in 2.5 */
  486. if (moxaXmitBuff)
  487. free_page(page);
  488. else
  489. moxaXmitBuff = (unsigned char *) page;
  490. }
  491. up(&moxaBuffSem);
  492. ch = &moxaChannels[port];
  493. ch->count++;
  494. tty->driver_data = ch;
  495. ch->tty = tty;
  496. if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
  497. ch->statusflags = 0;
  498. set_tty_param(tty);
  499. MoxaPortLineCtrl(ch->port, 1, 1);
  500. MoxaPortEnable(ch->port);
  501. ch->asyncflags |= ASYNC_INITIALIZED;
  502. }
  503. retval = block_till_ready(tty, filp, ch);
  504. moxa_unthrottle(tty);
  505. if (ch->type == PORT_16550A) {
  506. MoxaSetFifo(ch->port, 1);
  507. } else {
  508. MoxaSetFifo(ch->port, 0);
  509. }
  510. return (retval);
  511. }
  512. static void moxa_close(struct tty_struct *tty, struct file *filp)
  513. {
  514. struct moxa_str *ch;
  515. int port;
  516. port = PORTNO(tty);
  517. if (port == MAX_PORTS) {
  518. return;
  519. }
  520. if (!MoxaPortIsValid(port)) {
  521. #ifdef SERIAL_DEBUG_CLOSE
  522. printk("Invalid portno in moxa_close\n");
  523. #endif
  524. tty->driver_data = NULL;
  525. return;
  526. }
  527. if (tty->driver_data == NULL) {
  528. return;
  529. }
  530. if (tty_hung_up_p(filp)) {
  531. return;
  532. }
  533. ch = (struct moxa_str *) tty->driver_data;
  534. if ((tty->count == 1) && (ch->count != 1)) {
  535. printk("moxa_close: bad serial port count; tty->count is 1, "
  536. "ch->count is %d\n", ch->count);
  537. ch->count = 1;
  538. }
  539. if (--ch->count < 0) {
  540. printk("moxa_close: bad serial port count, device=%s\n",
  541. tty->name);
  542. ch->count = 0;
  543. }
  544. if (ch->count) {
  545. return;
  546. }
  547. ch->asyncflags |= ASYNC_CLOSING;
  548. ch->cflag = tty->termios->c_cflag;
  549. if (ch->asyncflags & ASYNC_INITIALIZED) {
  550. setup_empty_event(tty);
  551. tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
  552. moxaEmptyTimer_on[ch->port] = 0;
  553. del_timer(&moxaEmptyTimer[ch->port]);
  554. }
  555. shut_down(ch);
  556. MoxaPortFlushData(port, 2);
  557. if (tty->driver->flush_buffer)
  558. tty->driver->flush_buffer(tty);
  559. tty_ldisc_flush(tty);
  560. tty->closing = 0;
  561. ch->event = 0;
  562. ch->tty = NULL;
  563. if (ch->blocked_open) {
  564. if (ch->close_delay) {
  565. msleep_interruptible(jiffies_to_msecs(ch->close_delay));
  566. }
  567. wake_up_interruptible(&ch->open_wait);
  568. }
  569. ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  570. wake_up_interruptible(&ch->close_wait);
  571. }
  572. static int moxa_write(struct tty_struct *tty,
  573. const unsigned char *buf, int count)
  574. {
  575. struct moxa_str *ch;
  576. int len, port;
  577. unsigned long flags;
  578. ch = (struct moxa_str *) tty->driver_data;
  579. if (ch == NULL)
  580. return (0);
  581. port = ch->port;
  582. spin_lock_irqsave(&moxa_lock, flags);
  583. len = MoxaPortWriteData(port, (unsigned char *) buf, count);
  584. spin_unlock_irqrestore(&moxa_lock, flags);
  585. /*********************************************
  586. if ( !(ch->statusflags & LOWWAIT) &&
  587. ((len != count) || (MoxaPortTxFree(port) <= 100)) )
  588. ************************************************/
  589. ch->statusflags |= LOWWAIT;
  590. return (len);
  591. }
  592. static int moxa_write_room(struct tty_struct *tty)
  593. {
  594. struct moxa_str *ch;
  595. if (tty->stopped)
  596. return (0);
  597. ch = (struct moxa_str *) tty->driver_data;
  598. if (ch == NULL)
  599. return (0);
  600. return (MoxaPortTxFree(ch->port));
  601. }
  602. static void moxa_flush_buffer(struct tty_struct *tty)
  603. {
  604. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  605. if (ch == NULL)
  606. return;
  607. MoxaPortFlushData(ch->port, 1);
  608. tty_wakeup(tty);
  609. }
  610. static int moxa_chars_in_buffer(struct tty_struct *tty)
  611. {
  612. int chars;
  613. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  614. /*
  615. * Sigh...I have to check if driver_data is NULL here, because
  616. * if an open() fails, the TTY subsystem eventually calls
  617. * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
  618. * routine. And since the open() failed, we return 0 here. TDJ
  619. */
  620. if (ch == NULL)
  621. return (0);
  622. chars = MoxaPortTxQueue(ch->port);
  623. if (chars) {
  624. /*
  625. * Make it possible to wakeup anything waiting for output
  626. * in tty_ioctl.c, etc.
  627. */
  628. if (!(ch->statusflags & EMPTYWAIT))
  629. setup_empty_event(tty);
  630. }
  631. return (chars);
  632. }
  633. static void moxa_flush_chars(struct tty_struct *tty)
  634. {
  635. /*
  636. * Don't think I need this, because this is called to empty the TX
  637. * buffer for the 16450, 16550, etc.
  638. */
  639. }
  640. static void moxa_put_char(struct tty_struct *tty, unsigned char c)
  641. {
  642. struct moxa_str *ch;
  643. int port;
  644. unsigned long flags;
  645. ch = (struct moxa_str *) tty->driver_data;
  646. if (ch == NULL)
  647. return;
  648. port = ch->port;
  649. spin_lock_irqsave(&moxa_lock, flags);
  650. moxaXmitBuff[0] = c;
  651. MoxaPortWriteData(port, moxaXmitBuff, 1);
  652. spin_unlock_irqrestore(&moxa_lock, flags);
  653. /************************************************
  654. if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
  655. *************************************************/
  656. ch->statusflags |= LOWWAIT;
  657. }
  658. static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
  659. {
  660. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  661. int port;
  662. int flag = 0, dtr, rts;
  663. port = PORTNO(tty);
  664. if ((port != MAX_PORTS) && (!ch))
  665. return (-EINVAL);
  666. MoxaPortGetLineOut(ch->port, &dtr, &rts);
  667. if (dtr)
  668. flag |= TIOCM_DTR;
  669. if (rts)
  670. flag |= TIOCM_RTS;
  671. dtr = MoxaPortLineStatus(ch->port);
  672. if (dtr & 1)
  673. flag |= TIOCM_CTS;
  674. if (dtr & 2)
  675. flag |= TIOCM_DSR;
  676. if (dtr & 4)
  677. flag |= TIOCM_CD;
  678. return flag;
  679. }
  680. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  681. unsigned int set, unsigned int clear)
  682. {
  683. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  684. int port;
  685. int dtr, rts;
  686. port = PORTNO(tty);
  687. if ((port != MAX_PORTS) && (!ch))
  688. return (-EINVAL);
  689. MoxaPortGetLineOut(ch->port, &dtr, &rts);
  690. if (set & TIOCM_RTS)
  691. rts = 1;
  692. if (set & TIOCM_DTR)
  693. dtr = 1;
  694. if (clear & TIOCM_RTS)
  695. rts = 0;
  696. if (clear & TIOCM_DTR)
  697. dtr = 0;
  698. MoxaPortLineCtrl(ch->port, dtr, rts);
  699. return 0;
  700. }
  701. static int moxa_ioctl(struct tty_struct *tty, struct file *file,
  702. unsigned int cmd, unsigned long arg)
  703. {
  704. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  705. register int port;
  706. void __user *argp = (void __user *)arg;
  707. int retval;
  708. port = PORTNO(tty);
  709. if ((port != MAX_PORTS) && (!ch))
  710. return (-EINVAL);
  711. switch (cmd) {
  712. case TCSBRK: /* SVID version: non-zero arg --> no break */
  713. retval = tty_check_change(tty);
  714. if (retval)
  715. return (retval);
  716. setup_empty_event(tty);
  717. tty_wait_until_sent(tty, 0);
  718. if (!arg)
  719. MoxaPortSendBreak(ch->port, 0);
  720. return (0);
  721. case TCSBRKP: /* support for POSIX tcsendbreak() */
  722. retval = tty_check_change(tty);
  723. if (retval)
  724. return (retval);
  725. setup_empty_event(tty);
  726. tty_wait_until_sent(tty, 0);
  727. MoxaPortSendBreak(ch->port, arg);
  728. return (0);
  729. case TIOCGSOFTCAR:
  730. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
  731. case TIOCSSOFTCAR:
  732. if(get_user(retval, (unsigned long __user *) argp))
  733. return -EFAULT;
  734. arg = retval;
  735. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
  736. (arg ? CLOCAL : 0));
  737. if (C_CLOCAL(tty))
  738. ch->asyncflags &= ~ASYNC_CHECK_CD;
  739. else
  740. ch->asyncflags |= ASYNC_CHECK_CD;
  741. return (0);
  742. case TIOCGSERIAL:
  743. return moxa_get_serial_info(ch, argp);
  744. case TIOCSSERIAL:
  745. return moxa_set_serial_info(ch, argp);
  746. default:
  747. retval = MoxaDriverIoctl(cmd, arg, port);
  748. }
  749. return (retval);
  750. }
  751. static void moxa_throttle(struct tty_struct *tty)
  752. {
  753. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  754. ch->statusflags |= THROTTLE;
  755. }
  756. static void moxa_unthrottle(struct tty_struct *tty)
  757. {
  758. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  759. ch->statusflags &= ~THROTTLE;
  760. }
  761. static void moxa_set_termios(struct tty_struct *tty,
  762. struct termios *old_termios)
  763. {
  764. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  765. if (ch == NULL)
  766. return;
  767. set_tty_param(tty);
  768. if (!(old_termios->c_cflag & CLOCAL) &&
  769. (tty->termios->c_cflag & CLOCAL))
  770. wake_up_interruptible(&ch->open_wait);
  771. }
  772. static void moxa_stop(struct tty_struct *tty)
  773. {
  774. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  775. if (ch == NULL)
  776. return;
  777. MoxaPortTxDisable(ch->port);
  778. ch->statusflags |= TXSTOPPED;
  779. }
  780. static void moxa_start(struct tty_struct *tty)
  781. {
  782. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  783. if (ch == NULL)
  784. return;
  785. if (!(ch->statusflags & TXSTOPPED))
  786. return;
  787. MoxaPortTxEnable(ch->port);
  788. ch->statusflags &= ~TXSTOPPED;
  789. }
  790. static void moxa_hangup(struct tty_struct *tty)
  791. {
  792. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  793. moxa_flush_buffer(tty);
  794. shut_down(ch);
  795. ch->event = 0;
  796. ch->count = 0;
  797. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  798. ch->tty = NULL;
  799. wake_up_interruptible(&ch->open_wait);
  800. }
  801. static void moxa_poll(unsigned long ignored)
  802. {
  803. register int card;
  804. struct moxa_str *ch;
  805. struct tty_struct *tp;
  806. int i, ports;
  807. moxaTimer_on = 0;
  808. del_timer(&moxaTimer);
  809. if (MoxaDriverPoll() < 0) {
  810. moxaTimer.function = moxa_poll;
  811. moxaTimer.expires = jiffies + (HZ / 50);
  812. moxaTimer_on = 1;
  813. add_timer(&moxaTimer);
  814. return;
  815. }
  816. for (card = 0; card < MAX_BOARDS; card++) {
  817. if ((ports = MoxaPortsOfCard(card)) <= 0)
  818. continue;
  819. ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
  820. for (i = 0; i < ports; i++, ch++) {
  821. if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
  822. continue;
  823. if (!(ch->statusflags & THROTTLE) &&
  824. (MoxaPortRxQueue(ch->port) > 0))
  825. receive_data(ch);
  826. if ((tp = ch->tty) == 0)
  827. continue;
  828. if (ch->statusflags & LOWWAIT) {
  829. if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
  830. if (!tp->stopped) {
  831. ch->statusflags &= ~LOWWAIT;
  832. tty_wakeup(tp);
  833. }
  834. }
  835. }
  836. if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
  837. tty_insert_flip_char(tp, 0, TTY_BREAK);
  838. tty_schedule_flip(tp);
  839. }
  840. if (MoxaPortDCDChange(ch->port)) {
  841. if (ch->asyncflags & ASYNC_CHECK_CD) {
  842. if (MoxaPortDCDON(ch->port))
  843. wake_up_interruptible(&ch->open_wait);
  844. else {
  845. set_bit(MOXA_EVENT_HANGUP, &ch->event);
  846. schedule_work(&ch->tqueue);
  847. }
  848. }
  849. }
  850. }
  851. }
  852. moxaTimer.function = moxa_poll;
  853. moxaTimer.expires = jiffies + (HZ / 50);
  854. moxaTimer_on = 1;
  855. add_timer(&moxaTimer);
  856. }
  857. /******************************************************************************/
  858. static void set_tty_param(struct tty_struct *tty)
  859. {
  860. register struct termios *ts;
  861. struct moxa_str *ch;
  862. int rts, cts, txflow, rxflow, xany;
  863. ch = (struct moxa_str *) tty->driver_data;
  864. ts = tty->termios;
  865. if (ts->c_cflag & CLOCAL)
  866. ch->asyncflags &= ~ASYNC_CHECK_CD;
  867. else
  868. ch->asyncflags |= ASYNC_CHECK_CD;
  869. rts = cts = txflow = rxflow = xany = 0;
  870. if (ts->c_cflag & CRTSCTS)
  871. rts = cts = 1;
  872. if (ts->c_iflag & IXON)
  873. txflow = 1;
  874. if (ts->c_iflag & IXOFF)
  875. rxflow = 1;
  876. if (ts->c_iflag & IXANY)
  877. xany = 1;
  878. MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
  879. MoxaPortSetTermio(ch->port, ts);
  880. }
  881. static int block_till_ready(struct tty_struct *tty, struct file *filp,
  882. struct moxa_str *ch)
  883. {
  884. DECLARE_WAITQUEUE(wait,current);
  885. unsigned long flags;
  886. int retval;
  887. int do_clocal = C_CLOCAL(tty);
  888. /*
  889. * If the device is in the middle of being closed, then block
  890. * until it's done, and then try again.
  891. */
  892. if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
  893. if (ch->asyncflags & ASYNC_CLOSING)
  894. interruptible_sleep_on(&ch->close_wait);
  895. #ifdef SERIAL_DO_RESTART
  896. if (ch->asyncflags & ASYNC_HUP_NOTIFY)
  897. return (-EAGAIN);
  898. else
  899. return (-ERESTARTSYS);
  900. #else
  901. return (-EAGAIN);
  902. #endif
  903. }
  904. /*
  905. * If non-blocking mode is set, then make the check up front
  906. * and then exit.
  907. */
  908. if (filp->f_flags & O_NONBLOCK) {
  909. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  910. return (0);
  911. }
  912. /*
  913. * Block waiting for the carrier detect and the line to become free
  914. */
  915. retval = 0;
  916. add_wait_queue(&ch->open_wait, &wait);
  917. #ifdef SERIAL_DEBUG_OPEN
  918. printk("block_til_ready before block: ttys%d, count = %d\n",
  919. ch->line, ch->count);
  920. #endif
  921. spin_lock_irqsave(&moxa_lock, flags);
  922. if (!tty_hung_up_p(filp))
  923. ch->count--;
  924. ch->blocked_open++;
  925. spin_unlock_irqrestore(&moxa_lock, flags);
  926. while (1) {
  927. set_current_state(TASK_INTERRUPTIBLE);
  928. if (tty_hung_up_p(filp) ||
  929. !(ch->asyncflags & ASYNC_INITIALIZED)) {
  930. #ifdef SERIAL_DO_RESTART
  931. if (ch->asyncflags & ASYNC_HUP_NOTIFY)
  932. retval = -EAGAIN;
  933. else
  934. retval = -ERESTARTSYS;
  935. #else
  936. retval = -EAGAIN;
  937. #endif
  938. break;
  939. }
  940. if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
  941. MoxaPortDCDON(ch->port)))
  942. break;
  943. if (signal_pending(current)) {
  944. retval = -ERESTARTSYS;
  945. break;
  946. }
  947. schedule();
  948. }
  949. set_current_state(TASK_RUNNING);
  950. remove_wait_queue(&ch->open_wait, &wait);
  951. spin_lock_irqsave(&moxa_lock, flags);
  952. if (!tty_hung_up_p(filp))
  953. ch->count++;
  954. ch->blocked_open--;
  955. spin_unlock_irqrestore(&moxa_lock, flags);
  956. #ifdef SERIAL_DEBUG_OPEN
  957. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  958. ch->line, ch->count);
  959. #endif
  960. if (retval)
  961. return (retval);
  962. /* FIXME: review to see if we need to use set_bit on these */
  963. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  964. return 0;
  965. }
  966. static void setup_empty_event(struct tty_struct *tty)
  967. {
  968. struct moxa_str *ch = tty->driver_data;
  969. unsigned long flags;
  970. spin_lock_irqsave(&moxa_lock, flags);
  971. ch->statusflags |= EMPTYWAIT;
  972. moxaEmptyTimer_on[ch->port] = 0;
  973. del_timer(&moxaEmptyTimer[ch->port]);
  974. moxaEmptyTimer[ch->port].expires = jiffies + HZ;
  975. moxaEmptyTimer_on[ch->port] = 1;
  976. add_timer(&moxaEmptyTimer[ch->port]);
  977. spin_unlock_irqrestore(&moxa_lock, flags);
  978. }
  979. static void check_xmit_empty(unsigned long data)
  980. {
  981. struct moxa_str *ch;
  982. ch = (struct moxa_str *) data;
  983. moxaEmptyTimer_on[ch->port] = 0;
  984. del_timer(&moxaEmptyTimer[ch->port]);
  985. if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
  986. if (MoxaPortTxQueue(ch->port) == 0) {
  987. ch->statusflags &= ~EMPTYWAIT;
  988. tty_wakeup(ch->tty);
  989. return;
  990. }
  991. moxaEmptyTimer[ch->port].expires = jiffies + HZ;
  992. moxaEmptyTimer_on[ch->port] = 1;
  993. add_timer(&moxaEmptyTimer[ch->port]);
  994. } else
  995. ch->statusflags &= ~EMPTYWAIT;
  996. }
  997. static void shut_down(struct moxa_str *ch)
  998. {
  999. struct tty_struct *tp;
  1000. if (!(ch->asyncflags & ASYNC_INITIALIZED))
  1001. return;
  1002. tp = ch->tty;
  1003. MoxaPortDisable(ch->port);
  1004. /*
  1005. * If we're a modem control device and HUPCL is on, drop RTS & DTR.
  1006. */
  1007. if (tp->termios->c_cflag & HUPCL)
  1008. MoxaPortLineCtrl(ch->port, 0, 0);
  1009. ch->asyncflags &= ~ASYNC_INITIALIZED;
  1010. }
  1011. static void receive_data(struct moxa_str *ch)
  1012. {
  1013. struct tty_struct *tp;
  1014. struct termios *ts;
  1015. unsigned long flags;
  1016. ts = NULL;
  1017. tp = ch->tty;
  1018. if (tp)
  1019. ts = tp->termios;
  1020. /**************************************************
  1021. if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
  1022. *****************************************************/
  1023. if (!tp || !ts) {
  1024. MoxaPortFlushData(ch->port, 0);
  1025. return;
  1026. }
  1027. spin_lock_irqsave(&moxa_lock, flags);
  1028. MoxaPortReadData(ch->port, tp);
  1029. spin_unlock_irqrestore(&moxa_lock, flags);
  1030. tty_schedule_flip(tp);
  1031. }
  1032. #define Magic_code 0x404
  1033. /*
  1034. * System Configuration
  1035. */
  1036. /*
  1037. * for C218 BIOS initialization
  1038. */
  1039. #define C218_ConfBase 0x800
  1040. #define C218_status (C218_ConfBase + 0) /* BIOS running status */
  1041. #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
  1042. #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
  1043. #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
  1044. #define C218check_sum (C218_ConfBase + 8) /* BYTE */
  1045. #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
  1046. #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
  1047. #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
  1048. #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
  1049. #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
  1050. #define C218_LoadBuf 0x0F00
  1051. #define C218_KeyCode 0x218
  1052. #define CP204J_KeyCode 0x204
  1053. /*
  1054. * for C320 BIOS initialization
  1055. */
  1056. #define C320_ConfBase 0x800
  1057. #define C320_LoadBuf 0x0f00
  1058. #define STS_init 0x05 /* for C320_status */
  1059. #define C320_status C320_ConfBase + 0 /* BIOS running status */
  1060. #define C320_diag C320_ConfBase + 2 /* diagnostic status */
  1061. #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
  1062. #define C320DLoad_len C320_ConfBase + 6 /* WORD */
  1063. #define C320check_sum C320_ConfBase + 8 /* WORD */
  1064. #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
  1065. #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
  1066. #define C320UART_no C320_ConfBase + 0x0e /* WORD */
  1067. #define C320_KeyCode 0x320
  1068. #define FixPage_addr 0x0000 /* starting addr of static page */
  1069. #define DynPage_addr 0x2000 /* starting addr of dynamic page */
  1070. #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
  1071. #define Control_reg 0x1ff0 /* select page and reset control */
  1072. #define HW_reset 0x80
  1073. /*
  1074. * Function Codes
  1075. */
  1076. #define FC_CardReset 0x80
  1077. #define FC_ChannelReset 1 /* C320 firmware not supported */
  1078. #define FC_EnableCH 2
  1079. #define FC_DisableCH 3
  1080. #define FC_SetParam 4
  1081. #define FC_SetMode 5
  1082. #define FC_SetRate 6
  1083. #define FC_LineControl 7
  1084. #define FC_LineStatus 8
  1085. #define FC_XmitControl 9
  1086. #define FC_FlushQueue 10
  1087. #define FC_SendBreak 11
  1088. #define FC_StopBreak 12
  1089. #define FC_LoopbackON 13
  1090. #define FC_LoopbackOFF 14
  1091. #define FC_ClrIrqTable 15
  1092. #define FC_SendXon 16
  1093. #define FC_SetTermIrq 17 /* C320 firmware not supported */
  1094. #define FC_SetCntIrq 18 /* C320 firmware not supported */
  1095. #define FC_SetBreakIrq 19
  1096. #define FC_SetLineIrq 20
  1097. #define FC_SetFlowCtl 21
  1098. #define FC_GenIrq 22
  1099. #define FC_InCD180 23
  1100. #define FC_OutCD180 24
  1101. #define FC_InUARTreg 23
  1102. #define FC_OutUARTreg 24
  1103. #define FC_SetXonXoff 25
  1104. #define FC_OutCD180CCR 26
  1105. #define FC_ExtIQueue 27
  1106. #define FC_ExtOQueue 28
  1107. #define FC_ClrLineIrq 29
  1108. #define FC_HWFlowCtl 30
  1109. #define FC_GetClockRate 35
  1110. #define FC_SetBaud 36
  1111. #define FC_SetDataMode 41
  1112. #define FC_GetCCSR 43
  1113. #define FC_GetDataError 45
  1114. #define FC_RxControl 50
  1115. #define FC_ImmSend 51
  1116. #define FC_SetXonState 52
  1117. #define FC_SetXoffState 53
  1118. #define FC_SetRxFIFOTrig 54
  1119. #define FC_SetTxFIFOCnt 55
  1120. #define FC_UnixRate 56
  1121. #define FC_UnixResetTimer 57
  1122. #define RxFIFOTrig1 0
  1123. #define RxFIFOTrig4 1
  1124. #define RxFIFOTrig8 2
  1125. #define RxFIFOTrig14 3
  1126. /*
  1127. * Dual-Ported RAM
  1128. */
  1129. #define DRAM_global 0
  1130. #define INT_data (DRAM_global + 0)
  1131. #define Config_base (DRAM_global + 0x108)
  1132. #define IRQindex (INT_data + 0)
  1133. #define IRQpending (INT_data + 4)
  1134. #define IRQtable (INT_data + 8)
  1135. /*
  1136. * Interrupt Status
  1137. */
  1138. #define IntrRx 0x01 /* receiver data O.K. */
  1139. #define IntrTx 0x02 /* transmit buffer empty */
  1140. #define IntrFunc 0x04 /* function complete */
  1141. #define IntrBreak 0x08 /* received break */
  1142. #define IntrLine 0x10 /* line status change
  1143. for transmitter */
  1144. #define IntrIntr 0x20 /* received INTR code */
  1145. #define IntrQuit 0x40 /* received QUIT code */
  1146. #define IntrEOF 0x80 /* received EOF code */
  1147. #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
  1148. #define IntrTxTrigger 0x200 /* tx data count below trigger value */
  1149. #define Magic_no (Config_base + 0)
  1150. #define Card_model_no (Config_base + 2)
  1151. #define Total_ports (Config_base + 4)
  1152. #define Module_cnt (Config_base + 8)
  1153. #define Module_no (Config_base + 10)
  1154. #define Timer_10ms (Config_base + 14)
  1155. #define Disable_IRQ (Config_base + 20)
  1156. #define TMS320_PORT1 (Config_base + 22)
  1157. #define TMS320_PORT2 (Config_base + 24)
  1158. #define TMS320_CLOCK (Config_base + 26)
  1159. /*
  1160. * DATA BUFFER in DRAM
  1161. */
  1162. #define Extern_table 0x400 /* Base address of the external table
  1163. (24 words * 64) total 3K bytes
  1164. (24 words * 128) total 6K bytes */
  1165. #define Extern_size 0x60 /* 96 bytes */
  1166. #define RXrptr 0x00 /* read pointer for RX buffer */
  1167. #define RXwptr 0x02 /* write pointer for RX buffer */
  1168. #define TXrptr 0x04 /* read pointer for TX buffer */
  1169. #define TXwptr 0x06 /* write pointer for TX buffer */
  1170. #define HostStat 0x08 /* IRQ flag and general flag */
  1171. #define FlagStat 0x0A
  1172. #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
  1173. /* x x x x | | | | */
  1174. /* | | | + CTS flow */
  1175. /* | | +--- RTS flow */
  1176. /* | +------ TX Xon/Xoff */
  1177. /* +--------- RX Xon/Xoff */
  1178. #define Break_cnt 0x0E /* received break count */
  1179. #define CD180TXirq 0x10 /* if non-0: enable TX irq */
  1180. #define RX_mask 0x12
  1181. #define TX_mask 0x14
  1182. #define Ofs_rxb 0x16
  1183. #define Ofs_txb 0x18
  1184. #define Page_rxb 0x1A
  1185. #define Page_txb 0x1C
  1186. #define EndPage_rxb 0x1E
  1187. #define EndPage_txb 0x20
  1188. #define Data_error 0x22
  1189. #define RxTrigger 0x28
  1190. #define TxTrigger 0x2a
  1191. #define rRXwptr 0x34
  1192. #define Low_water 0x36
  1193. #define FuncCode 0x40
  1194. #define FuncArg 0x42
  1195. #define FuncArg1 0x44
  1196. #define C218rx_size 0x2000 /* 8K bytes */
  1197. #define C218tx_size 0x8000 /* 32K bytes */
  1198. #define C218rx_mask (C218rx_size - 1)
  1199. #define C218tx_mask (C218tx_size - 1)
  1200. #define C320p8rx_size 0x2000
  1201. #define C320p8tx_size 0x8000
  1202. #define C320p8rx_mask (C320p8rx_size - 1)
  1203. #define C320p8tx_mask (C320p8tx_size - 1)
  1204. #define C320p16rx_size 0x2000
  1205. #define C320p16tx_size 0x4000
  1206. #define C320p16rx_mask (C320p16rx_size - 1)
  1207. #define C320p16tx_mask (C320p16tx_size - 1)
  1208. #define C320p24rx_size 0x2000
  1209. #define C320p24tx_size 0x2000
  1210. #define C320p24rx_mask (C320p24rx_size - 1)
  1211. #define C320p24tx_mask (C320p24tx_size - 1)
  1212. #define C320p32rx_size 0x1000
  1213. #define C320p32tx_size 0x1000
  1214. #define C320p32rx_mask (C320p32rx_size - 1)
  1215. #define C320p32tx_mask (C320p32tx_size - 1)
  1216. #define Page_size 0x2000
  1217. #define Page_mask (Page_size - 1)
  1218. #define C218rx_spage 3
  1219. #define C218tx_spage 4
  1220. #define C218rx_pageno 1
  1221. #define C218tx_pageno 4
  1222. #define C218buf_pageno 5
  1223. #define C320p8rx_spage 3
  1224. #define C320p8tx_spage 4
  1225. #define C320p8rx_pgno 1
  1226. #define C320p8tx_pgno 4
  1227. #define C320p8buf_pgno 5
  1228. #define C320p16rx_spage 3
  1229. #define C320p16tx_spage 4
  1230. #define C320p16rx_pgno 1
  1231. #define C320p16tx_pgno 2
  1232. #define C320p16buf_pgno 3
  1233. #define C320p24rx_spage 3
  1234. #define C320p24tx_spage 4
  1235. #define C320p24rx_pgno 1
  1236. #define C320p24tx_pgno 1
  1237. #define C320p24buf_pgno 2
  1238. #define C320p32rx_spage 3
  1239. #define C320p32tx_ofs C320p32rx_size
  1240. #define C320p32tx_spage 3
  1241. #define C320p32buf_pgno 1
  1242. /*
  1243. * Host Status
  1244. */
  1245. #define WakeupRx 0x01
  1246. #define WakeupTx 0x02
  1247. #define WakeupBreak 0x08
  1248. #define WakeupLine 0x10
  1249. #define WakeupIntr 0x20
  1250. #define WakeupQuit 0x40
  1251. #define WakeupEOF 0x80 /* used in VTIME control */
  1252. #define WakeupRxTrigger 0x100
  1253. #define WakeupTxTrigger 0x200
  1254. /*
  1255. * Flag status
  1256. */
  1257. #define Rx_over 0x01
  1258. #define Xoff_state 0x02
  1259. #define Tx_flowOff 0x04
  1260. #define Tx_enable 0x08
  1261. #define CTS_state 0x10
  1262. #define DSR_state 0x20
  1263. #define DCD_state 0x80
  1264. /*
  1265. * FlowControl
  1266. */
  1267. #define CTS_FlowCtl 1
  1268. #define RTS_FlowCtl 2
  1269. #define Tx_FlowCtl 4
  1270. #define Rx_FlowCtl 8
  1271. #define IXM_IXANY 0x10
  1272. #define LowWater 128
  1273. #define DTR_ON 1
  1274. #define RTS_ON 2
  1275. #define CTS_ON 1
  1276. #define DSR_ON 2
  1277. #define DCD_ON 8
  1278. /* mode definition */
  1279. #define MX_CS8 0x03
  1280. #define MX_CS7 0x02
  1281. #define MX_CS6 0x01
  1282. #define MX_CS5 0x00
  1283. #define MX_STOP1 0x00
  1284. #define MX_STOP15 0x04
  1285. #define MX_STOP2 0x08
  1286. #define MX_PARNONE 0x00
  1287. #define MX_PAREVEN 0x40
  1288. #define MX_PARODD 0xC0
  1289. /*
  1290. * Query
  1291. */
  1292. #define QueryPort MAX_PORTS
  1293. struct mon_str {
  1294. int tick;
  1295. int rxcnt[MAX_PORTS];
  1296. int txcnt[MAX_PORTS];
  1297. };
  1298. typedef struct mon_str mon_st;
  1299. #define DCD_changed 0x01
  1300. #define DCD_oldstate 0x80
  1301. static unsigned char moxaBuff[10240];
  1302. static void __iomem *moxaIntNdx[MAX_BOARDS];
  1303. static void __iomem *moxaIntPend[MAX_BOARDS];
  1304. static void __iomem *moxaIntTable[MAX_BOARDS];
  1305. static char moxaChkPort[MAX_PORTS];
  1306. static char moxaLineCtrl[MAX_PORTS];
  1307. static void __iomem *moxaTableAddr[MAX_PORTS];
  1308. static long moxaCurBaud[MAX_PORTS];
  1309. static char moxaDCDState[MAX_PORTS];
  1310. static char moxaLowChkFlag[MAX_PORTS];
  1311. static int moxaLowWaterChk;
  1312. static int moxaCard;
  1313. static mon_st moxaLog;
  1314. static int moxaFuncTout;
  1315. static ushort moxaBreakCnt[MAX_PORTS];
  1316. static void moxadelay(int);
  1317. static void moxafunc(void __iomem *, int, ushort);
  1318. static void wait_finish(void __iomem *);
  1319. static void low_water_check(void __iomem *);
  1320. static int moxaloadbios(int, unsigned char __user *, int);
  1321. static int moxafindcard(int);
  1322. static int moxaload320b(int, unsigned char __user *, int);
  1323. static int moxaloadcode(int, unsigned char __user *, int);
  1324. static int moxaloadc218(int, void __iomem *, int);
  1325. static int moxaloadc320(int, void __iomem *, int, int *);
  1326. /*****************************************************************************
  1327. * Driver level functions: *
  1328. * 1. MoxaDriverInit(void); *
  1329. * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
  1330. * 3. MoxaDriverPoll(void); *
  1331. *****************************************************************************/
  1332. void MoxaDriverInit(void)
  1333. {
  1334. int i;
  1335. moxaFuncTout = HZ / 2; /* 500 mini-seconds */
  1336. moxaCard = 0;
  1337. moxaLog.tick = 0;
  1338. moxaLowWaterChk = 0;
  1339. for (i = 0; i < MAX_PORTS; i++) {
  1340. moxaChkPort[i] = 0;
  1341. moxaLowChkFlag[i] = 0;
  1342. moxaLineCtrl[i] = 0;
  1343. moxaLog.rxcnt[i] = 0;
  1344. moxaLog.txcnt[i] = 0;
  1345. }
  1346. }
  1347. #define MOXA 0x400
  1348. #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
  1349. #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
  1350. #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
  1351. #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
  1352. #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
  1353. #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
  1354. #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
  1355. #define MOXA_GETDATACOUNT (MOXA + 23)
  1356. #define MOXA_GET_IOQUEUE (MOXA + 27)
  1357. #define MOXA_FLUSH_QUEUE (MOXA + 28)
  1358. #define MOXA_GET_CONF (MOXA + 35) /* configuration */
  1359. #define MOXA_GET_MAJOR (MOXA + 63)
  1360. #define MOXA_GET_CUMAJOR (MOXA + 64)
  1361. #define MOXA_GETMSTATUS (MOXA + 65)
  1362. struct moxaq_str {
  1363. int inq;
  1364. int outq;
  1365. };
  1366. struct dl_str {
  1367. char __user *buf;
  1368. int len;
  1369. int cardno;
  1370. };
  1371. static struct moxaq_str temp_queue[MAX_PORTS];
  1372. static struct dl_str dltmp;
  1373. void MoxaPortFlushData(int port, int mode)
  1374. {
  1375. void __iomem *ofsAddr;
  1376. if ((mode < 0) || (mode > 2))
  1377. return;
  1378. ofsAddr = moxaTableAddr[port];
  1379. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1380. if (mode != 1) {
  1381. moxaLowChkFlag[port] = 0;
  1382. low_water_check(ofsAddr);
  1383. }
  1384. }
  1385. int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
  1386. {
  1387. int i;
  1388. int status;
  1389. int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
  1390. void __user *argp = (void __user *)arg;
  1391. if (port == QueryPort) {
  1392. if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
  1393. (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
  1394. (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
  1395. (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
  1396. (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
  1397. return (-EINVAL);
  1398. }
  1399. switch (cmd) {
  1400. case MOXA_GET_CONF:
  1401. if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
  1402. return -EFAULT;
  1403. return (0);
  1404. case MOXA_INIT_DRIVER:
  1405. if ((int) arg == 0x404)
  1406. MoxaDriverInit();
  1407. return (0);
  1408. case MOXA_GETDATACOUNT:
  1409. moxaLog.tick = jiffies;
  1410. if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
  1411. return -EFAULT;
  1412. return (0);
  1413. case MOXA_FLUSH_QUEUE:
  1414. MoxaPortFlushData(port, arg);
  1415. return (0);
  1416. case MOXA_GET_IOQUEUE:
  1417. for (i = 0; i < MAX_PORTS; i++) {
  1418. if (moxaChkPort[i]) {
  1419. temp_queue[i].inq = MoxaPortRxQueue(i);
  1420. temp_queue[i].outq = MoxaPortTxQueue(i);
  1421. }
  1422. }
  1423. if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
  1424. return -EFAULT;
  1425. return (0);
  1426. case MOXA_GET_OQUEUE:
  1427. i = MoxaPortTxQueue(port);
  1428. return put_user(i, (unsigned long __user *)argp);
  1429. case MOXA_GET_IQUEUE:
  1430. i = MoxaPortRxQueue(port);
  1431. return put_user(i, (unsigned long __user *)argp);
  1432. case MOXA_GET_MAJOR:
  1433. if(copy_to_user(argp, &ttymajor, sizeof(int)))
  1434. return -EFAULT;
  1435. return 0;
  1436. case MOXA_GET_CUMAJOR:
  1437. i = 0;
  1438. if(copy_to_user(argp, &i, sizeof(int)))
  1439. return -EFAULT;
  1440. return 0;
  1441. case MOXA_GETMSTATUS:
  1442. for (i = 0; i < MAX_PORTS; i++) {
  1443. GMStatus[i].ri = 0;
  1444. GMStatus[i].dcd = 0;
  1445. GMStatus[i].dsr = 0;
  1446. GMStatus[i].cts = 0;
  1447. if (!moxaChkPort[i]) {
  1448. continue;
  1449. } else {
  1450. status = MoxaPortLineStatus(moxaChannels[i].port);
  1451. if (status & 1)
  1452. GMStatus[i].cts = 1;
  1453. if (status & 2)
  1454. GMStatus[i].dsr = 1;
  1455. if (status & 4)
  1456. GMStatus[i].dcd = 1;
  1457. }
  1458. if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
  1459. GMStatus[i].cflag = moxaChannels[i].cflag;
  1460. else
  1461. GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
  1462. }
  1463. if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
  1464. return -EFAULT;
  1465. return 0;
  1466. default:
  1467. return (-ENOIOCTLCMD);
  1468. case MOXA_LOAD_BIOS:
  1469. case MOXA_FIND_BOARD:
  1470. case MOXA_LOAD_C320B:
  1471. case MOXA_LOAD_CODE:
  1472. break;
  1473. }
  1474. if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
  1475. return -EFAULT;
  1476. if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
  1477. return -EINVAL;
  1478. switch(cmd)
  1479. {
  1480. case MOXA_LOAD_BIOS:
  1481. i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
  1482. return (i);
  1483. case MOXA_FIND_BOARD:
  1484. return moxafindcard(dltmp.cardno);
  1485. case MOXA_LOAD_C320B:
  1486. moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
  1487. default: /* to keep gcc happy */
  1488. return (0);
  1489. case MOXA_LOAD_CODE:
  1490. i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
  1491. if (i == -1)
  1492. return (-EFAULT);
  1493. return (i);
  1494. }
  1495. }
  1496. int MoxaDriverPoll(void)
  1497. {
  1498. register ushort temp;
  1499. register int card;
  1500. void __iomem *ofsAddr;
  1501. void __iomem *ip;
  1502. int port, p, ports;
  1503. if (moxaCard == 0)
  1504. return (-1);
  1505. for (card = 0; card < MAX_BOARDS; card++) {
  1506. if ((ports = moxa_boards[card].numPorts) == 0)
  1507. continue;
  1508. if (readb(moxaIntPend[card]) == 0xff) {
  1509. ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
  1510. p = card * MAX_PORTS_PER_BOARD;
  1511. ports <<= 1;
  1512. for (port = 0; port < ports; port += 2, p++) {
  1513. if ((temp = readw(ip + port)) != 0) {
  1514. writew(0, ip + port);
  1515. ofsAddr = moxaTableAddr[p];
  1516. if (temp & IntrTx)
  1517. writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
  1518. if (temp & IntrBreak) {
  1519. moxaBreakCnt[p]++;
  1520. }
  1521. if (temp & IntrLine) {
  1522. if (readb(ofsAddr + FlagStat) & DCD_state) {
  1523. if ((moxaDCDState[p] & DCD_oldstate) == 0)
  1524. moxaDCDState[p] = (DCD_oldstate |
  1525. DCD_changed);
  1526. } else {
  1527. if (moxaDCDState[p] & DCD_oldstate)
  1528. moxaDCDState[p] = DCD_changed;
  1529. }
  1530. }
  1531. }
  1532. }
  1533. writeb(0, moxaIntPend[card]);
  1534. }
  1535. if (moxaLowWaterChk) {
  1536. p = card * MAX_PORTS_PER_BOARD;
  1537. for (port = 0; port < ports; port++, p++) {
  1538. if (moxaLowChkFlag[p]) {
  1539. moxaLowChkFlag[p] = 0;
  1540. ofsAddr = moxaTableAddr[p];
  1541. low_water_check(ofsAddr);
  1542. }
  1543. }
  1544. }
  1545. }
  1546. moxaLowWaterChk = 0;
  1547. return (0);
  1548. }
  1549. /*****************************************************************************
  1550. * Card level function: *
  1551. * 1. MoxaPortsOfCard(int cardno); *
  1552. *****************************************************************************/
  1553. int MoxaPortsOfCard(int cardno)
  1554. {
  1555. if (moxa_boards[cardno].boardType == 0)
  1556. return (0);
  1557. return (moxa_boards[cardno].numPorts);
  1558. }
  1559. /*****************************************************************************
  1560. * Port level functions: *
  1561. * 1. MoxaPortIsValid(int port); *
  1562. * 2. MoxaPortEnable(int port); *
  1563. * 3. MoxaPortDisable(int port); *
  1564. * 4. MoxaPortGetMaxBaud(int port); *
  1565. * 5. MoxaPortGetCurBaud(int port); *
  1566. * 6. MoxaPortSetBaud(int port, long baud); *
  1567. * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
  1568. * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
  1569. * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
  1570. * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
  1571. * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
  1572. * 12. MoxaPortLineStatus(int port); *
  1573. * 13. MoxaPortDCDChange(int port); *
  1574. * 14. MoxaPortDCDON(int port); *
  1575. * 15. MoxaPortFlushData(int port, int mode); *
  1576. * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
  1577. * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
  1578. * 18. MoxaPortTxBufSize(int port); *
  1579. * 19. MoxaPortRxBufSize(int port); *
  1580. * 20. MoxaPortTxQueue(int port); *
  1581. * 21. MoxaPortTxFree(int port); *
  1582. * 22. MoxaPortRxQueue(int port); *
  1583. * 23. MoxaPortRxFree(int port); *
  1584. * 24. MoxaPortTxDisable(int port); *
  1585. * 25. MoxaPortTxEnable(int port); *
  1586. * 26. MoxaPortGetBrkCnt(int port); *
  1587. * 27. MoxaPortResetBrkCnt(int port); *
  1588. * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
  1589. * 29. MoxaPortIsTxHold(int port); *
  1590. * 30. MoxaPortSendBreak(int port, int ticks); *
  1591. *****************************************************************************/
  1592. /*
  1593. * Moxa Port Number Description:
  1594. *
  1595. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1596. * the port number using in MOXA driver functions will be 0 to 31 for
  1597. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1598. * to 127 for fourth. For example, if you setup three MOXA boards,
  1599. * first board is C218, second board is C320-16 and third board is
  1600. * C320-32. The port number of first board (C218 - 8 ports) is from
  1601. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1602. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1603. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1604. * 127 will be invalid.
  1605. *
  1606. *
  1607. * Moxa Functions Description:
  1608. *
  1609. * Function 1: Driver initialization routine, this routine must be
  1610. * called when initialized driver.
  1611. * Syntax:
  1612. * void MoxaDriverInit();
  1613. *
  1614. *
  1615. * Function 2: Moxa driver private IOCTL command processing.
  1616. * Syntax:
  1617. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1618. *
  1619. * unsigned int cmd : IOCTL command
  1620. * unsigned long arg : IOCTL argument
  1621. * int port : port number (0 - 127)
  1622. *
  1623. * return: 0 (OK)
  1624. * -EINVAL
  1625. * -ENOIOCTLCMD
  1626. *
  1627. *
  1628. * Function 3: Moxa driver polling process routine.
  1629. * Syntax:
  1630. * int MoxaDriverPoll(void);
  1631. *
  1632. * return: 0 ; polling O.K.
  1633. * -1 : no any Moxa card.
  1634. *
  1635. *
  1636. * Function 4: Get the ports of this card.
  1637. * Syntax:
  1638. * int MoxaPortsOfCard(int cardno);
  1639. *
  1640. * int cardno : card number (0 - 3)
  1641. *
  1642. * return: 0 : this card is invalid
  1643. * 8/16/24/32
  1644. *
  1645. *
  1646. * Function 5: Check this port is valid or invalid
  1647. * Syntax:
  1648. * int MoxaPortIsValid(int port);
  1649. * int port : port number (0 - 127, ref port description)
  1650. *
  1651. * return: 0 : this port is invalid
  1652. * 1 : this port is valid
  1653. *
  1654. *
  1655. * Function 6: Enable this port to start Tx/Rx data.
  1656. * Syntax:
  1657. * void MoxaPortEnable(int port);
  1658. * int port : port number (0 - 127)
  1659. *
  1660. *
  1661. * Function 7: Disable this port
  1662. * Syntax:
  1663. * void MoxaPortDisable(int port);
  1664. * int port : port number (0 - 127)
  1665. *
  1666. *
  1667. * Function 8: Get the maximun available baud rate of this port.
  1668. * Syntax:
  1669. * long MoxaPortGetMaxBaud(int port);
  1670. * int port : port number (0 - 127)
  1671. *
  1672. * return: 0 : this port is invalid
  1673. * 38400/57600/115200 bps
  1674. *
  1675. *
  1676. * Function 9: Get the current baud rate of this port.
  1677. * Syntax:
  1678. * long MoxaPortGetCurBaud(int port);
  1679. * int port : port number (0 - 127)
  1680. *
  1681. * return: 0 : this port is invalid
  1682. * 50 - 115200 bps
  1683. *
  1684. *
  1685. * Function 10: Setting baud rate of this port.
  1686. * Syntax:
  1687. * long MoxaPortSetBaud(int port, long baud);
  1688. * int port : port number (0 - 127)
  1689. * long baud : baud rate (50 - 115200)
  1690. *
  1691. * return: 0 : this port is invalid or baud < 50
  1692. * 50 - 115200 : the real baud rate set to the port, if
  1693. * the argument baud is large than maximun
  1694. * available baud rate, the real setting
  1695. * baud rate will be the maximun baud rate.
  1696. *
  1697. *
  1698. * Function 11: Setting the data-bits/stop-bits/parity of this port
  1699. * Syntax:
  1700. * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
  1701. * int port : port number (0 - 127)
  1702. * int databits : data bits (8/7/6/5)
  1703. * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
  1704. int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
  1705. *
  1706. * return: -1 : invalid parameter
  1707. * 0 : setting O.K.
  1708. *
  1709. *
  1710. * Function 12: Configure the port.
  1711. * Syntax:
  1712. * int MoxaPortSetTermio(int port, struct termios *termio);
  1713. * int port : port number (0 - 127)
  1714. * struct termios * termio : termio structure pointer
  1715. *
  1716. * return: -1 : this port is invalid or termio == NULL
  1717. * 0 : setting O.K.
  1718. *
  1719. *
  1720. * Function 13: Get the DTR/RTS state of this port.
  1721. * Syntax:
  1722. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1723. * int port : port number (0 - 127)
  1724. * int * dtrState : pointer to INT to receive the current DTR
  1725. * state. (if NULL, this function will not
  1726. * write to this address)
  1727. * int * rtsState : pointer to INT to receive the current RTS
  1728. * state. (if NULL, this function will not
  1729. * write to this address)
  1730. *
  1731. * return: -1 : this port is invalid
  1732. * 0 : O.K.
  1733. *
  1734. *
  1735. * Function 14: Setting the DTR/RTS output state of this port.
  1736. * Syntax:
  1737. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1738. * int port : port number (0 - 127)
  1739. * int dtrState : DTR output state (0: off, 1: on)
  1740. * int rtsState : RTS output state (0: off, 1: on)
  1741. *
  1742. *
  1743. * Function 15: Setting the flow control of this port.
  1744. * Syntax:
  1745. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1746. * int txFlow,int xany);
  1747. * int port : port number (0 - 127)
  1748. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1749. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1750. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1751. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1752. * int xany : S/W XANY flow control (0: no, 1: yes)
  1753. *
  1754. *
  1755. * Function 16: Get ths line status of this port
  1756. * Syntax:
  1757. * int MoxaPortLineStatus(int port);
  1758. * int port : port number (0 - 127)
  1759. *
  1760. * return: Bit 0 - CTS state (0: off, 1: on)
  1761. * Bit 1 - DSR state (0: off, 1: on)
  1762. * Bit 2 - DCD state (0: off, 1: on)
  1763. *
  1764. *
  1765. * Function 17: Check the DCD state has changed since the last read
  1766. * of this function.
  1767. * Syntax:
  1768. * int MoxaPortDCDChange(int port);
  1769. * int port : port number (0 - 127)
  1770. *
  1771. * return: 0 : no changed
  1772. * 1 : DCD has changed
  1773. *
  1774. *
  1775. * Function 18: Check ths current DCD state is ON or not.
  1776. * Syntax:
  1777. * int MoxaPortDCDON(int port);
  1778. * int port : port number (0 - 127)
  1779. *
  1780. * return: 0 : DCD off
  1781. * 1 : DCD on
  1782. *
  1783. *
  1784. * Function 19: Flush the Rx/Tx buffer data of this port.
  1785. * Syntax:
  1786. * void MoxaPortFlushData(int port, int mode);
  1787. * int port : port number (0 - 127)
  1788. * int mode
  1789. * 0 : flush the Rx buffer
  1790. * 1 : flush the Tx buffer
  1791. * 2 : flush the Rx and Tx buffer
  1792. *
  1793. *
  1794. * Function 20: Write data.
  1795. * Syntax:
  1796. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1797. * int port : port number (0 - 127)
  1798. * unsigned char * buffer : pointer to write data buffer.
  1799. * int length : write data length
  1800. *
  1801. * return: 0 - length : real write data length
  1802. *
  1803. *
  1804. * Function 21: Read data.
  1805. * Syntax:
  1806. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1807. * int port : port number (0 - 127)
  1808. * struct tty_struct *tty : tty for data
  1809. *
  1810. * return: 0 - length : real read data length
  1811. *
  1812. *
  1813. * Function 22: Get the Tx buffer size of this port
  1814. * Syntax:
  1815. * int MoxaPortTxBufSize(int port);
  1816. * int port : port number (0 - 127)
  1817. *
  1818. * return: .. : Tx buffer size
  1819. *
  1820. *
  1821. * Function 23: Get the Rx buffer size of this port
  1822. * Syntax:
  1823. * int MoxaPortRxBufSize(int port);
  1824. * int port : port number (0 - 127)
  1825. *
  1826. * return: .. : Rx buffer size
  1827. *
  1828. *
  1829. * Function 24: Get the Tx buffer current queued data bytes
  1830. * Syntax:
  1831. * int MoxaPortTxQueue(int port);
  1832. * int port : port number (0 - 127)
  1833. *
  1834. * return: .. : Tx buffer current queued data bytes
  1835. *
  1836. *
  1837. * Function 25: Get the Tx buffer current free space
  1838. * Syntax:
  1839. * int MoxaPortTxFree(int port);
  1840. * int port : port number (0 - 127)
  1841. *
  1842. * return: .. : Tx buffer current free space
  1843. *
  1844. *
  1845. * Function 26: Get the Rx buffer current queued data bytes
  1846. * Syntax:
  1847. * int MoxaPortRxQueue(int port);
  1848. * int port : port number (0 - 127)
  1849. *
  1850. * return: .. : Rx buffer current queued data bytes
  1851. *
  1852. *
  1853. * Function 27: Get the Rx buffer current free space
  1854. * Syntax:
  1855. * int MoxaPortRxFree(int port);
  1856. * int port : port number (0 - 127)
  1857. *
  1858. * return: .. : Rx buffer current free space
  1859. *
  1860. *
  1861. * Function 28: Disable port data transmission.
  1862. * Syntax:
  1863. * void MoxaPortTxDisable(int port);
  1864. * int port : port number (0 - 127)
  1865. *
  1866. *
  1867. * Function 29: Enable port data transmission.
  1868. * Syntax:
  1869. * void MoxaPortTxEnable(int port);
  1870. * int port : port number (0 - 127)
  1871. *
  1872. *
  1873. * Function 30: Get the received BREAK signal count.
  1874. * Syntax:
  1875. * int MoxaPortGetBrkCnt(int port);
  1876. * int port : port number (0 - 127)
  1877. *
  1878. * return: 0 - .. : BREAK signal count
  1879. *
  1880. *
  1881. * Function 31: Get the received BREAK signal count and reset it.
  1882. * Syntax:
  1883. * int MoxaPortResetBrkCnt(int port);
  1884. * int port : port number (0 - 127)
  1885. *
  1886. * return: 0 - .. : BREAK signal count
  1887. *
  1888. *
  1889. * Function 32: Set the S/W flow control new XON/XOFF value, default
  1890. * XON is 0x11 & XOFF is 0x13.
  1891. * Syntax:
  1892. * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
  1893. * int port : port number (0 - 127)
  1894. * int xonValue : new XON value (0 - 255)
  1895. * int xoffValue : new XOFF value (0 - 255)
  1896. *
  1897. *
  1898. * Function 33: Check this port's transmission is hold by remote site
  1899. * because the flow control.
  1900. * Syntax:
  1901. * int MoxaPortIsTxHold(int port);
  1902. * int port : port number (0 - 127)
  1903. *
  1904. * return: 0 : normal
  1905. * 1 : hold by remote site
  1906. *
  1907. *
  1908. * Function 34: Send out a BREAK signal.
  1909. * Syntax:
  1910. * void MoxaPortSendBreak(int port, int ms100);
  1911. * int port : port number (0 - 127)
  1912. * int ms100 : break signal time interval.
  1913. * unit: 100 mini-second. if ms100 == 0, it will
  1914. * send out a about 250 ms BREAK signal.
  1915. *
  1916. */
  1917. int MoxaPortIsValid(int port)
  1918. {
  1919. if (moxaCard == 0)
  1920. return (0);
  1921. if (moxaChkPort[port] == 0)
  1922. return (0);
  1923. return (1);
  1924. }
  1925. void MoxaPortEnable(int port)
  1926. {
  1927. void __iomem *ofsAddr;
  1928. int MoxaPortLineStatus(int);
  1929. short lowwater = 512;
  1930. ofsAddr = moxaTableAddr[port];
  1931. writew(lowwater, ofsAddr + Low_water);
  1932. moxaBreakCnt[port] = 0;
  1933. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1934. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1935. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1936. } else {
  1937. writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
  1938. }
  1939. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1940. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1941. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1942. MoxaPortLineStatus(port);
  1943. }
  1944. void MoxaPortDisable(int port)
  1945. {
  1946. void __iomem *ofsAddr = moxaTableAddr[port];
  1947. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1948. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1949. writew(0, ofsAddr + HostStat);
  1950. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1951. }
  1952. long MoxaPortGetMaxBaud(int port)
  1953. {
  1954. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1955. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
  1956. return (460800L);
  1957. else
  1958. return (921600L);
  1959. }
  1960. long MoxaPortSetBaud(int port, long baud)
  1961. {
  1962. void __iomem *ofsAddr;
  1963. long max, clock;
  1964. unsigned int val;
  1965. if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
  1966. return (0);
  1967. ofsAddr = moxaTableAddr[port];
  1968. if (baud > max)
  1969. baud = max;
  1970. if (max == 38400L)
  1971. clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
  1972. else if (max == 57600L)
  1973. clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
  1974. else
  1975. clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
  1976. val = clock / baud;
  1977. moxafunc(ofsAddr, FC_SetBaud, val);
  1978. baud = clock / val;
  1979. moxaCurBaud[port] = baud;
  1980. return (baud);
  1981. }
  1982. int MoxaPortSetTermio(int port, struct termios *termio)
  1983. {
  1984. void __iomem *ofsAddr;
  1985. tcflag_t cflag;
  1986. long baud;
  1987. tcflag_t mode = 0;
  1988. if (moxaChkPort[port] == 0 || termio == 0)
  1989. return (-1);
  1990. ofsAddr = moxaTableAddr[port];
  1991. cflag = termio->c_cflag; /* termio->c_cflag */
  1992. mode = termio->c_cflag & CSIZE;
  1993. if (mode == CS5)
  1994. mode = MX_CS5;
  1995. else if (mode == CS6)
  1996. mode = MX_CS6;
  1997. else if (mode == CS7)
  1998. mode = MX_CS7;
  1999. else if (mode == CS8)
  2000. mode = MX_CS8;
  2001. if (termio->c_cflag & CSTOPB) {
  2002. if (mode == MX_CS5)
  2003. mode |= MX_STOP15;
  2004. else
  2005. mode |= MX_STOP2;
  2006. } else
  2007. mode |= MX_STOP1;
  2008. if (termio->c_cflag & PARENB) {
  2009. if (termio->c_cflag & PARODD)
  2010. mode |= MX_PARODD;
  2011. else
  2012. mode |= MX_PAREVEN;
  2013. } else
  2014. mode |= MX_PARNONE;
  2015. moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
  2016. cflag &= (CBAUD | CBAUDEX);
  2017. #ifndef B921600
  2018. #define B921600 (B460800+1)
  2019. #endif
  2020. switch (cflag) {
  2021. case B921600:
  2022. baud = 921600L;
  2023. break;
  2024. case B460800:
  2025. baud = 460800L;
  2026. break;
  2027. case B230400:
  2028. baud = 230400L;
  2029. break;
  2030. case B115200:
  2031. baud = 115200L;
  2032. break;
  2033. case B57600:
  2034. baud = 57600L;
  2035. break;
  2036. case B38400:
  2037. baud = 38400L;
  2038. break;
  2039. case B19200:
  2040. baud = 19200L;
  2041. break;
  2042. case B9600:
  2043. baud = 9600L;
  2044. break;
  2045. case B4800:
  2046. baud = 4800L;
  2047. break;
  2048. case B2400:
  2049. baud = 2400L;
  2050. break;
  2051. case B1800:
  2052. baud = 1800L;
  2053. break;
  2054. case B1200:
  2055. baud = 1200L;
  2056. break;
  2057. case B600:
  2058. baud = 600L;
  2059. break;
  2060. case B300:
  2061. baud = 300L;
  2062. break;
  2063. case B200:
  2064. baud = 200L;
  2065. break;
  2066. case B150:
  2067. baud = 150L;
  2068. break;
  2069. case B134:
  2070. baud = 134L;
  2071. break;
  2072. case B110:
  2073. baud = 110L;
  2074. break;
  2075. case B75:
  2076. baud = 75L;
  2077. break;
  2078. case B50:
  2079. baud = 50L;
  2080. break;
  2081. default:
  2082. baud = 0;
  2083. }
  2084. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  2085. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  2086. if (baud == 921600L)
  2087. return (-1);
  2088. }
  2089. MoxaPortSetBaud(port, baud);
  2090. if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
  2091. writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
  2092. writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
  2093. writeb(FC_SetXonXoff, ofsAddr + FuncCode);
  2094. wait_finish(ofsAddr);
  2095. }
  2096. return (0);
  2097. }
  2098. int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
  2099. {
  2100. if (!MoxaPortIsValid(port))
  2101. return (-1);
  2102. if (dtrState) {
  2103. if (moxaLineCtrl[port] & DTR_ON)
  2104. *dtrState = 1;
  2105. else
  2106. *dtrState = 0;
  2107. }
  2108. if (rtsState) {
  2109. if (moxaLineCtrl[port] & RTS_ON)
  2110. *rtsState = 1;
  2111. else
  2112. *rtsState = 0;
  2113. }
  2114. return (0);
  2115. }
  2116. void MoxaPortLineCtrl(int port, int dtr, int rts)
  2117. {
  2118. void __iomem *ofsAddr;
  2119. int mode;
  2120. ofsAddr = moxaTableAddr[port];
  2121. mode = 0;
  2122. if (dtr)
  2123. mode |= DTR_ON;
  2124. if (rts)
  2125. mode |= RTS_ON;
  2126. moxaLineCtrl[port] = mode;
  2127. moxafunc(ofsAddr, FC_LineControl, mode);
  2128. }
  2129. void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
  2130. {
  2131. void __iomem *ofsAddr;
  2132. int mode;
  2133. ofsAddr = moxaTableAddr[port];
  2134. mode = 0;
  2135. if (rts)
  2136. mode |= RTS_FlowCtl;
  2137. if (cts)
  2138. mode |= CTS_FlowCtl;
  2139. if (txflow)
  2140. mode |= Tx_FlowCtl;
  2141. if (rxflow)
  2142. mode |= Rx_FlowCtl;
  2143. if (txany)
  2144. mode |= IXM_IXANY;
  2145. moxafunc(ofsAddr, FC_SetFlowCtl, mode);
  2146. }
  2147. int MoxaPortLineStatus(int port)
  2148. {
  2149. void __iomem *ofsAddr;
  2150. int val;
  2151. ofsAddr = moxaTableAddr[port];
  2152. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  2153. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  2154. moxafunc(ofsAddr, FC_LineStatus, 0);
  2155. val = readw(ofsAddr + FuncArg);
  2156. } else {
  2157. val = readw(ofsAddr + FlagStat) >> 4;
  2158. }
  2159. val &= 0x0B;
  2160. if (val & 8) {
  2161. val |= 4;
  2162. if ((moxaDCDState[port] & DCD_oldstate) == 0)
  2163. moxaDCDState[port] = (DCD_oldstate | DCD_changed);
  2164. } else {
  2165. if (moxaDCDState[port] & DCD_oldstate)
  2166. moxaDCDState[port] = DCD_changed;
  2167. }
  2168. val &= 7;
  2169. return (val);
  2170. }
  2171. int MoxaPortDCDChange(int port)
  2172. {
  2173. int n;
  2174. if (moxaChkPort[port] == 0)
  2175. return (0);
  2176. n = moxaDCDState[port];
  2177. moxaDCDState[port] &= ~DCD_changed;
  2178. n &= DCD_changed;
  2179. return (n);
  2180. }
  2181. int MoxaPortDCDON(int port)
  2182. {
  2183. int n;
  2184. if (moxaChkPort[port] == 0)
  2185. return (0);
  2186. if (moxaDCDState[port] & DCD_oldstate)
  2187. n = 1;
  2188. else
  2189. n = 0;
  2190. return (n);
  2191. }
  2192. /*
  2193. int MoxaDumpMem(int port, unsigned char * buffer, int len)
  2194. {
  2195. int i;
  2196. unsigned long baseAddr,ofsAddr,ofs;
  2197. baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
  2198. ofs = baseAddr + DynPage_addr + pageofs;
  2199. if (len > 0x2000L)
  2200. len = 0x2000L;
  2201. for (i = 0; i < len; i++)
  2202. buffer[i] = readb(ofs+i);
  2203. }
  2204. */
  2205. int MoxaPortWriteData(int port, unsigned char * buffer, int len)
  2206. {
  2207. int c, total, i;
  2208. ushort tail;
  2209. int cnt;
  2210. ushort head, tx_mask, spage, epage;
  2211. ushort pageno, pageofs, bufhead;
  2212. void __iomem *baseAddr, *ofsAddr, *ofs;
  2213. ofsAddr = moxaTableAddr[port];
  2214. baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
  2215. tx_mask = readw(ofsAddr + TX_mask);
  2216. spage = readw(ofsAddr + Page_txb);
  2217. epage = readw(ofsAddr + EndPage_txb);
  2218. tail = readw(ofsAddr + TXwptr);
  2219. head = readw(ofsAddr + TXrptr);
  2220. c = (head > tail) ? (head - tail - 1)
  2221. : (head - tail + tx_mask);
  2222. if (c > len)
  2223. c = len;
  2224. moxaLog.txcnt[port] += c;
  2225. total = c;
  2226. if (spage == epage) {
  2227. bufhead = readw(ofsAddr + Ofs_txb);
  2228. writew(spage, baseAddr + Control_reg);
  2229. while (c > 0) {
  2230. if (head > tail)
  2231. len = head - tail - 1;
  2232. else
  2233. len = tx_mask + 1 - tail;
  2234. len = (c > len) ? len : c;
  2235. ofs = baseAddr + DynPage_addr + bufhead + tail;
  2236. for (i = 0; i < len; i++)
  2237. writeb(*buffer++, ofs + i);
  2238. tail = (tail + len) & tx_mask;
  2239. c -= len;
  2240. }
  2241. writew(tail, ofsAddr + TXwptr);
  2242. } else {
  2243. len = c;
  2244. pageno = spage + (tail >> 13);
  2245. pageofs = tail & Page_mask;
  2246. do {
  2247. cnt = Page_size - pageofs;
  2248. if (cnt > c)
  2249. cnt = c;
  2250. c -= cnt;
  2251. writeb(pageno, baseAddr + Control_reg);
  2252. ofs = baseAddr + DynPage_addr + pageofs;
  2253. for (i = 0; i < cnt; i++)
  2254. writeb(*buffer++, ofs + i);
  2255. if (c == 0) {
  2256. writew((tail + len) & tx_mask, ofsAddr + TXwptr);
  2257. break;
  2258. }
  2259. if (++pageno == epage)
  2260. pageno = spage;
  2261. pageofs = 0;
  2262. } while (1);
  2263. }
  2264. writeb(1, ofsAddr + CD180TXirq); /* start to send */
  2265. return (total);
  2266. }
  2267. int MoxaPortReadData(int port, struct tty_struct *tty)
  2268. {
  2269. register ushort head, pageofs;
  2270. int i, count, cnt, len, total, remain;
  2271. ushort tail, rx_mask, spage, epage;
  2272. ushort pageno, bufhead;
  2273. void __iomem *baseAddr, *ofsAddr, *ofs;
  2274. ofsAddr = moxaTableAddr[port];
  2275. baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
  2276. head = readw(ofsAddr + RXrptr);
  2277. tail = readw(ofsAddr + RXwptr);
  2278. rx_mask = readw(ofsAddr + RX_mask);
  2279. spage = readw(ofsAddr + Page_rxb);
  2280. epage = readw(ofsAddr + EndPage_rxb);
  2281. count = (tail >= head) ? (tail - head)
  2282. : (tail - head + rx_mask + 1);
  2283. if (count == 0)
  2284. return 0;
  2285. total = count;
  2286. remain = count - total;
  2287. moxaLog.rxcnt[port] += total;
  2288. count = total;
  2289. if (spage == epage) {
  2290. bufhead = readw(ofsAddr + Ofs_rxb);
  2291. writew(spage, baseAddr + Control_reg);
  2292. while (count > 0) {
  2293. if (tail >= head)
  2294. len = tail - head;
  2295. else
  2296. len = rx_mask + 1 - head;
  2297. len = (count > len) ? len : count;
  2298. ofs = baseAddr + DynPage_addr + bufhead + head;
  2299. for (i = 0; i < len; i++)
  2300. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2301. head = (head + len) & rx_mask;
  2302. count -= len;
  2303. }
  2304. writew(head, ofsAddr + RXrptr);
  2305. } else {
  2306. len = count;
  2307. pageno = spage + (head >> 13);
  2308. pageofs = head & Page_mask;
  2309. do {
  2310. cnt = Page_size - pageofs;
  2311. if (cnt > count)
  2312. cnt = count;
  2313. count -= cnt;
  2314. writew(pageno, baseAddr + Control_reg);
  2315. ofs = baseAddr + DynPage_addr + pageofs;
  2316. for (i = 0; i < cnt; i++)
  2317. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2318. if (count == 0) {
  2319. writew((head + len) & rx_mask, ofsAddr + RXrptr);
  2320. break;
  2321. }
  2322. if (++pageno == epage)
  2323. pageno = spage;
  2324. pageofs = 0;
  2325. } while (1);
  2326. }
  2327. if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
  2328. moxaLowWaterChk = 1;
  2329. moxaLowChkFlag[port] = 1;
  2330. }
  2331. return (total);
  2332. }
  2333. int MoxaPortTxQueue(int port)
  2334. {
  2335. void __iomem *ofsAddr;
  2336. ushort rptr, wptr, mask;
  2337. int len;
  2338. ofsAddr = moxaTableAddr[port];
  2339. rptr = readw(ofsAddr + TXrptr);
  2340. wptr = readw(ofsAddr + TXwptr);
  2341. mask = readw(ofsAddr + TX_mask);
  2342. len = (wptr - rptr) & mask;
  2343. return (len);
  2344. }
  2345. int MoxaPortTxFree(int port)
  2346. {
  2347. void __iomem *ofsAddr;
  2348. ushort rptr, wptr, mask;
  2349. int len;
  2350. ofsAddr = moxaTableAddr[port];
  2351. rptr = readw(ofsAddr + TXrptr);
  2352. wptr = readw(ofsAddr + TXwptr);
  2353. mask = readw(ofsAddr + TX_mask);
  2354. len = mask - ((wptr - rptr) & mask);
  2355. return (len);
  2356. }
  2357. int MoxaPortRxQueue(int port)
  2358. {
  2359. void __iomem *ofsAddr;
  2360. ushort rptr, wptr, mask;
  2361. int len;
  2362. ofsAddr = moxaTableAddr[port];
  2363. rptr = readw(ofsAddr + RXrptr);
  2364. wptr = readw(ofsAddr + RXwptr);
  2365. mask = readw(ofsAddr + RX_mask);
  2366. len = (wptr - rptr) & mask;
  2367. return (len);
  2368. }
  2369. void MoxaPortTxDisable(int port)
  2370. {
  2371. void __iomem *ofsAddr;
  2372. ofsAddr = moxaTableAddr[port];
  2373. moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
  2374. }
  2375. void MoxaPortTxEnable(int port)
  2376. {
  2377. void __iomem *ofsAddr;
  2378. ofsAddr = moxaTableAddr[port];
  2379. moxafunc(ofsAddr, FC_SetXonState, Magic_code);
  2380. }
  2381. int MoxaPortResetBrkCnt(int port)
  2382. {
  2383. ushort cnt;
  2384. cnt = moxaBreakCnt[port];
  2385. moxaBreakCnt[port] = 0;
  2386. return (cnt);
  2387. }
  2388. void MoxaPortSendBreak(int port, int ms100)
  2389. {
  2390. void __iomem *ofsAddr;
  2391. ofsAddr = moxaTableAddr[port];
  2392. if (ms100) {
  2393. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2394. moxadelay(ms100 * (HZ / 10));
  2395. } else {
  2396. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2397. moxadelay(HZ / 4); /* 250 ms */
  2398. }
  2399. moxafunc(ofsAddr, FC_StopBreak, Magic_code);
  2400. }
  2401. static int moxa_get_serial_info(struct moxa_str *info,
  2402. struct serial_struct __user *retinfo)
  2403. {
  2404. struct serial_struct tmp;
  2405. memset(&tmp, 0, sizeof(tmp));
  2406. tmp.type = info->type;
  2407. tmp.line = info->port;
  2408. tmp.port = 0;
  2409. tmp.irq = 0;
  2410. tmp.flags = info->asyncflags;
  2411. tmp.baud_base = 921600;
  2412. tmp.close_delay = info->close_delay;
  2413. tmp.closing_wait = info->closing_wait;
  2414. tmp.custom_divisor = 0;
  2415. tmp.hub6 = 0;
  2416. if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2417. return -EFAULT;
  2418. return (0);
  2419. }
  2420. static int moxa_set_serial_info(struct moxa_str *info,
  2421. struct serial_struct __user *new_info)
  2422. {
  2423. struct serial_struct new_serial;
  2424. if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2425. return -EFAULT;
  2426. if ((new_serial.irq != 0) ||
  2427. (new_serial.port != 0) ||
  2428. // (new_serial.type != info->type) ||
  2429. (new_serial.custom_divisor != 0) ||
  2430. (new_serial.baud_base != 921600))
  2431. return (-EPERM);
  2432. if (!capable(CAP_SYS_ADMIN)) {
  2433. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  2434. (info->asyncflags & ~ASYNC_USR_MASK)))
  2435. return (-EPERM);
  2436. } else {
  2437. info->close_delay = new_serial.close_delay * HZ / 100;
  2438. info->closing_wait = new_serial.closing_wait * HZ / 100;
  2439. }
  2440. new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
  2441. new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
  2442. if (new_serial.type == PORT_16550A) {
  2443. MoxaSetFifo(info->port, 1);
  2444. } else {
  2445. MoxaSetFifo(info->port, 0);
  2446. }
  2447. info->type = new_serial.type;
  2448. return (0);
  2449. }
  2450. /*****************************************************************************
  2451. * Static local functions: *
  2452. *****************************************************************************/
  2453. /*
  2454. * moxadelay - delays a specified number ticks
  2455. */
  2456. static void moxadelay(int tick)
  2457. {
  2458. unsigned long st, et;
  2459. st = jiffies;
  2460. et = st + tick;
  2461. while (time_before(jiffies, et));
  2462. }
  2463. static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
  2464. {
  2465. writew(arg, ofsAddr + FuncArg);
  2466. writew(cmd, ofsAddr + FuncCode);
  2467. wait_finish(ofsAddr);
  2468. }
  2469. static void wait_finish(void __iomem *ofsAddr)
  2470. {
  2471. unsigned long i, j;
  2472. i = jiffies;
  2473. while (readw(ofsAddr + FuncCode) != 0) {
  2474. j = jiffies;
  2475. if ((j - i) > moxaFuncTout) {
  2476. return;
  2477. }
  2478. }
  2479. }
  2480. static void low_water_check(void __iomem *ofsAddr)
  2481. {
  2482. int len;
  2483. ushort rptr, wptr, mask;
  2484. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  2485. rptr = readw(ofsAddr + RXrptr);
  2486. wptr = readw(ofsAddr + RXwptr);
  2487. mask = readw(ofsAddr + RX_mask);
  2488. len = (wptr - rptr) & mask;
  2489. if (len <= Low_water)
  2490. moxafunc(ofsAddr, FC_SendXon, 0);
  2491. }
  2492. }
  2493. static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
  2494. {
  2495. void __iomem *baseAddr;
  2496. int i;
  2497. if(copy_from_user(moxaBuff, tmp, len))
  2498. return -EFAULT;
  2499. baseAddr = moxaBaseAddr[cardno];
  2500. writeb(HW_reset, baseAddr + Control_reg); /* reset */
  2501. moxadelay(1); /* delay 10 ms */
  2502. for (i = 0; i < 4096; i++)
  2503. writeb(0, baseAddr + i); /* clear fix page */
  2504. for (i = 0; i < len; i++)
  2505. writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
  2506. writeb(0, baseAddr + Control_reg); /* restart */
  2507. return (0);
  2508. }
  2509. static int moxafindcard(int cardno)
  2510. {
  2511. void __iomem *baseAddr;
  2512. ushort tmp;
  2513. baseAddr = moxaBaseAddr[cardno];
  2514. switch (moxa_boards[cardno].boardType) {
  2515. case MOXA_BOARD_C218_ISA:
  2516. case MOXA_BOARD_C218_PCI:
  2517. if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
  2518. return (-1);
  2519. }
  2520. break;
  2521. case MOXA_BOARD_CP204J:
  2522. if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
  2523. return (-1);
  2524. }
  2525. break;
  2526. default:
  2527. if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
  2528. return (-1);
  2529. }
  2530. if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
  2531. return (-2);
  2532. }
  2533. }
  2534. return (0);
  2535. }
  2536. static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
  2537. {
  2538. void __iomem *baseAddr;
  2539. int i;
  2540. if(len > sizeof(moxaBuff))
  2541. return -EINVAL;
  2542. if(copy_from_user(moxaBuff, tmp, len))
  2543. return -EFAULT;
  2544. baseAddr = moxaBaseAddr[cardno];
  2545. writew(len - 7168 - 2, baseAddr + C320bapi_len);
  2546. writeb(1, baseAddr + Control_reg); /* Select Page 1 */
  2547. for (i = 0; i < 7168; i++)
  2548. writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
  2549. writeb(2, baseAddr + Control_reg); /* Select Page 2 */
  2550. for (i = 0; i < (len - 7168); i++)
  2551. writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
  2552. return (0);
  2553. }
  2554. static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
  2555. {
  2556. void __iomem *baseAddr, *ofsAddr;
  2557. int retval, port, i;
  2558. if(copy_from_user(moxaBuff, tmp, len))
  2559. return -EFAULT;
  2560. baseAddr = moxaBaseAddr[cardno];
  2561. switch (moxa_boards[cardno].boardType) {
  2562. case MOXA_BOARD_C218_ISA:
  2563. case MOXA_BOARD_C218_PCI:
  2564. case MOXA_BOARD_CP204J:
  2565. retval = moxaloadc218(cardno, baseAddr, len);
  2566. if (retval)
  2567. return (retval);
  2568. port = cardno * MAX_PORTS_PER_BOARD;
  2569. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2570. moxaChkPort[port] = 1;
  2571. moxaCurBaud[port] = 9600L;
  2572. moxaDCDState[port] = 0;
  2573. moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
  2574. ofsAddr = moxaTableAddr[port];
  2575. writew(C218rx_mask, ofsAddr + RX_mask);
  2576. writew(C218tx_mask, ofsAddr + TX_mask);
  2577. writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
  2578. writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
  2579. writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
  2580. writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
  2581. }
  2582. break;
  2583. default:
  2584. retval = moxaloadc320(cardno, baseAddr, len,
  2585. &moxa_boards[cardno].numPorts);
  2586. if (retval)
  2587. return (retval);
  2588. port = cardno * MAX_PORTS_PER_BOARD;
  2589. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2590. moxaChkPort[port] = 1;
  2591. moxaCurBaud[port] = 9600L;
  2592. moxaDCDState[port] = 0;
  2593. moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
  2594. ofsAddr = moxaTableAddr[port];
  2595. if (moxa_boards[cardno].numPorts == 8) {
  2596. writew(C320p8rx_mask, ofsAddr + RX_mask);
  2597. writew(C320p8tx_mask, ofsAddr + TX_mask);
  2598. writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
  2599. writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
  2600. writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
  2601. writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
  2602. } else if (moxa_boards[cardno].numPorts == 16) {
  2603. writew(C320p16rx_mask, ofsAddr + RX_mask);
  2604. writew(C320p16tx_mask, ofsAddr + TX_mask);
  2605. writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
  2606. writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
  2607. writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
  2608. writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
  2609. } else if (moxa_boards[cardno].numPorts == 24) {
  2610. writew(C320p24rx_mask, ofsAddr + RX_mask);
  2611. writew(C320p24tx_mask, ofsAddr + TX_mask);
  2612. writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
  2613. writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
  2614. writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
  2615. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2616. } else if (moxa_boards[cardno].numPorts == 32) {
  2617. writew(C320p32rx_mask, ofsAddr + RX_mask);
  2618. writew(C320p32tx_mask, ofsAddr + TX_mask);
  2619. writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
  2620. writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
  2621. writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
  2622. writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
  2623. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2624. }
  2625. }
  2626. break;
  2627. }
  2628. return (0);
  2629. }
  2630. static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
  2631. {
  2632. char retry;
  2633. int i, j, len1, len2;
  2634. ushort usum, *ptr, keycode;
  2635. if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
  2636. keycode = CP204J_KeyCode;
  2637. else
  2638. keycode = C218_KeyCode;
  2639. usum = 0;
  2640. len1 = len >> 1;
  2641. ptr = (ushort *) moxaBuff;
  2642. for (i = 0; i < len1; i++)
  2643. usum += *(ptr + i);
  2644. retry = 0;
  2645. do {
  2646. len1 = len >> 1;
  2647. j = 0;
  2648. while (len1) {
  2649. len2 = (len1 > 2048) ? 2048 : len1;
  2650. len1 -= len2;
  2651. for (i = 0; i < len2 << 1; i++)
  2652. writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
  2653. j += i;
  2654. writew(len2, baseAddr + C218DLoad_len);
  2655. writew(0, baseAddr + C218_key);
  2656. for (i = 0; i < 100; i++) {
  2657. if (readw(baseAddr + C218_key) == keycode)
  2658. break;
  2659. moxadelay(1); /* delay 10 ms */
  2660. }
  2661. if (readw(baseAddr + C218_key) != keycode) {
  2662. return (-1);
  2663. }
  2664. }
  2665. writew(0, baseAddr + C218DLoad_len);
  2666. writew(usum, baseAddr + C218check_sum);
  2667. writew(0, baseAddr + C218_key);
  2668. for (i = 0; i < 100; i++) {
  2669. if (readw(baseAddr + C218_key) == keycode)
  2670. break;
  2671. moxadelay(1); /* delay 10 ms */
  2672. }
  2673. retry++;
  2674. } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
  2675. if (readb(baseAddr + C218chksum_ok) != 1) {
  2676. return (-1);
  2677. }
  2678. writew(0, baseAddr + C218_key);
  2679. for (i = 0; i < 100; i++) {
  2680. if (readw(baseAddr + Magic_no) == Magic_code)
  2681. break;
  2682. moxadelay(1); /* delay 10 ms */
  2683. }
  2684. if (readw(baseAddr + Magic_no) != Magic_code) {
  2685. return (-1);
  2686. }
  2687. writew(1, baseAddr + Disable_IRQ);
  2688. writew(0, baseAddr + Magic_no);
  2689. for (i = 0; i < 100; i++) {
  2690. if (readw(baseAddr + Magic_no) == Magic_code)
  2691. break;
  2692. moxadelay(1); /* delay 10 ms */
  2693. }
  2694. if (readw(baseAddr + Magic_no) != Magic_code) {
  2695. return (-1);
  2696. }
  2697. moxaCard = 1;
  2698. moxaIntNdx[cardno] = baseAddr + IRQindex;
  2699. moxaIntPend[cardno] = baseAddr + IRQpending;
  2700. moxaIntTable[cardno] = baseAddr + IRQtable;
  2701. return (0);
  2702. }
  2703. static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
  2704. {
  2705. ushort usum;
  2706. int i, j, wlen, len2, retry;
  2707. ushort *uptr;
  2708. usum = 0;
  2709. wlen = len >> 1;
  2710. uptr = (ushort *) moxaBuff;
  2711. for (i = 0; i < wlen; i++)
  2712. usum += uptr[i];
  2713. retry = 0;
  2714. j = 0;
  2715. do {
  2716. while (wlen) {
  2717. if (wlen > 2048)
  2718. len2 = 2048;
  2719. else
  2720. len2 = wlen;
  2721. wlen -= len2;
  2722. len2 <<= 1;
  2723. for (i = 0; i < len2; i++)
  2724. writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
  2725. len2 >>= 1;
  2726. j += i;
  2727. writew(len2, baseAddr + C320DLoad_len);
  2728. writew(0, baseAddr + C320_key);
  2729. for (i = 0; i < 10; i++) {
  2730. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2731. break;
  2732. moxadelay(1);
  2733. }
  2734. if (readw(baseAddr + C320_key) != C320_KeyCode)
  2735. return (-1);
  2736. }
  2737. writew(0, baseAddr + C320DLoad_len);
  2738. writew(usum, baseAddr + C320check_sum);
  2739. writew(0, baseAddr + C320_key);
  2740. for (i = 0; i < 10; i++) {
  2741. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2742. break;
  2743. moxadelay(1);
  2744. }
  2745. retry++;
  2746. } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
  2747. if (readb(baseAddr + C320chksum_ok) != 1)
  2748. return (-1);
  2749. writew(0, baseAddr + C320_key);
  2750. for (i = 0; i < 600; i++) {
  2751. if (readw(baseAddr + Magic_no) == Magic_code)
  2752. break;
  2753. moxadelay(1);
  2754. }
  2755. if (readw(baseAddr + Magic_no) != Magic_code)
  2756. return (-100);
  2757. if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
  2758. writew(0x3800, baseAddr + TMS320_PORT1);
  2759. writew(0x3900, baseAddr + TMS320_PORT2);
  2760. writew(28499, baseAddr + TMS320_CLOCK);
  2761. } else {
  2762. writew(0x3200, baseAddr + TMS320_PORT1);
  2763. writew(0x3400, baseAddr + TMS320_PORT2);
  2764. writew(19999, baseAddr + TMS320_CLOCK);
  2765. }
  2766. writew(1, baseAddr + Disable_IRQ);
  2767. writew(0, baseAddr + Magic_no);
  2768. for (i = 0; i < 500; i++) {
  2769. if (readw(baseAddr + Magic_no) == Magic_code)
  2770. break;
  2771. moxadelay(1);
  2772. }
  2773. if (readw(baseAddr + Magic_no) != Magic_code)
  2774. return (-102);
  2775. j = readw(baseAddr + Module_cnt);
  2776. if (j <= 0)
  2777. return (-101);
  2778. *numPorts = j * 8;
  2779. writew(j, baseAddr + Module_no);
  2780. writew(0, baseAddr + Magic_no);
  2781. for (i = 0; i < 600; i++) {
  2782. if (readw(baseAddr + Magic_no) == Magic_code)
  2783. break;
  2784. moxadelay(1);
  2785. }
  2786. if (readw(baseAddr + Magic_no) != Magic_code)
  2787. return (-102);
  2788. moxaCard = 1;
  2789. moxaIntNdx[cardno] = baseAddr + IRQindex;
  2790. moxaIntPend[cardno] = baseAddr + IRQpending;
  2791. moxaIntTable[cardno] = baseAddr + IRQtable;
  2792. return (0);
  2793. }
  2794. #if 0
  2795. long MoxaPortGetCurBaud(int port)
  2796. {
  2797. if (moxaChkPort[port] == 0)
  2798. return (0);
  2799. return (moxaCurBaud[port]);
  2800. }
  2801. #endif /* 0 */
  2802. static void MoxaSetFifo(int port, int enable)
  2803. {
  2804. void __iomem *ofsAddr = moxaTableAddr[port];
  2805. if (!enable) {
  2806. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
  2807. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
  2808. } else {
  2809. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
  2810. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
  2811. }
  2812. }
  2813. #if 0
  2814. int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
  2815. {
  2816. void __iomem *ofsAddr;
  2817. int val;
  2818. val = 0;
  2819. switch (databits) {
  2820. case 5:
  2821. val |= 0;
  2822. break;
  2823. case 6:
  2824. val |= 1;
  2825. break;
  2826. case 7:
  2827. val |= 2;
  2828. break;
  2829. case 8:
  2830. val |= 3;
  2831. break;
  2832. default:
  2833. return (-1);
  2834. }
  2835. switch (stopbits) {
  2836. case 0:
  2837. val |= 0;
  2838. break; /* stop bits 1.5 */
  2839. case 1:
  2840. val |= 0;
  2841. break;
  2842. case 2:
  2843. val |= 4;
  2844. break;
  2845. default:
  2846. return (-1);
  2847. }
  2848. switch (parity) {
  2849. case 0:
  2850. val |= 0x00;
  2851. break; /* None */
  2852. case 1:
  2853. val |= 0x08;
  2854. break; /* Odd */
  2855. case 2:
  2856. val |= 0x18;
  2857. break; /* Even */
  2858. case 3:
  2859. val |= 0x28;
  2860. break; /* Mark */
  2861. case 4:
  2862. val |= 0x38;
  2863. break; /* Space */
  2864. default:
  2865. return (-1);
  2866. }
  2867. ofsAddr = moxaTableAddr[port];
  2868. moxafunc(ofsAddr, FC_SetMode, val);
  2869. return (0);
  2870. }
  2871. int MoxaPortTxBufSize(int port)
  2872. {
  2873. void __iomem *ofsAddr;
  2874. int size;
  2875. ofsAddr = moxaTableAddr[port];
  2876. size = readw(ofsAddr + TX_mask);
  2877. return (size);
  2878. }
  2879. int MoxaPortRxBufSize(int port)
  2880. {
  2881. void __iomem *ofsAddr;
  2882. int size;
  2883. ofsAddr = moxaTableAddr[port];
  2884. size = readw(ofsAddr + RX_mask);
  2885. return (size);
  2886. }
  2887. int MoxaPortRxFree(int port)
  2888. {
  2889. void __iomem *ofsAddr;
  2890. ushort rptr, wptr, mask;
  2891. int len;
  2892. ofsAddr = moxaTableAddr[port];
  2893. rptr = readw(ofsAddr + RXrptr);
  2894. wptr = readw(ofsAddr + RXwptr);
  2895. mask = readw(ofsAddr + RX_mask);
  2896. len = mask - ((wptr - rptr) & mask);
  2897. return (len);
  2898. }
  2899. int MoxaPortGetBrkCnt(int port)
  2900. {
  2901. return (moxaBreakCnt[port]);
  2902. }
  2903. void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
  2904. {
  2905. void __iomem *ofsAddr;
  2906. ofsAddr = moxaTableAddr[port];
  2907. writew(xonValue, ofsAddr + FuncArg);
  2908. writew(xoffValue, ofsAddr + FuncArg1);
  2909. writew(FC_SetXonXoff, ofsAddr + FuncCode);
  2910. wait_finish(ofsAddr);
  2911. }
  2912. int MoxaPortIsTxHold(int port)
  2913. {
  2914. void __iomem *ofsAddr;
  2915. int val;
  2916. ofsAddr = moxaTableAddr[port];
  2917. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  2918. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  2919. moxafunc(ofsAddr, FC_GetCCSR, 0);
  2920. val = readw(ofsAddr + FuncArg);
  2921. if (val & 0x04)
  2922. return (1);
  2923. } else {
  2924. if (readw(ofsAddr + FlagStat) & Tx_flowOff)
  2925. return (1);
  2926. }
  2927. return (0);
  2928. }
  2929. #endif