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/module.h>
  31. #include <linux/types.h>
  32. #include <linux/mm.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/signal.h>
  36. #include <linux/sched.h>
  37. #include <linux/timer.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/tty.h>
  40. #include <linux/tty_flip.h>
  41. #include <linux/major.h>
  42. #include <linux/string.h>
  43. #include <linux/fcntl.h>
  44. #include <linux/ptrace.h>
  45. #include <linux/serial.h>
  46. #include <linux/tty_driver.h>
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/init.h>
  50. #include <linux/bitops.h>
  51. #include <asm/system.h>
  52. #include <asm/io.h>
  53. #include <asm/uaccess.h>
  54. #define MOXA_VERSION "5.1k"
  55. #define MOXAMAJOR 172
  56. #define MOXACUMAJOR 173
  57. #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
  58. #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
  59. #define MAX_BOARDS 4 /* Don't change this value */
  60. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  61. #define MAX_PORTS 128 /* Don't change this value */
  62. /*
  63. * Define the Moxa PCI vendor and device IDs.
  64. */
  65. #define MOXA_BUS_TYPE_ISA 0
  66. #define MOXA_BUS_TYPE_PCI 1
  67. #ifndef PCI_VENDOR_ID_MOXA
  68. #define PCI_VENDOR_ID_MOXA 0x1393
  69. #endif
  70. #ifndef PCI_DEVICE_ID_CP204J
  71. #define PCI_DEVICE_ID_CP204J 0x2040
  72. #endif
  73. #ifndef PCI_DEVICE_ID_C218
  74. #define PCI_DEVICE_ID_C218 0x2180
  75. #endif
  76. #ifndef PCI_DEVICE_ID_C320
  77. #define PCI_DEVICE_ID_C320 0x3200
  78. #endif
  79. enum {
  80. MOXA_BOARD_C218_PCI = 1,
  81. MOXA_BOARD_C218_ISA,
  82. MOXA_BOARD_C320_PCI,
  83. MOXA_BOARD_C320_ISA,
  84. MOXA_BOARD_CP204J,
  85. };
  86. static char *moxa_brdname[] =
  87. {
  88. "C218 Turbo PCI series",
  89. "C218 Turbo ISA series",
  90. "C320 Turbo PCI series",
  91. "C320 Turbo ISA series",
  92. "CP-204J series",
  93. };
  94. #ifdef CONFIG_PCI
  95. static struct pci_device_id moxa_pcibrds[] = {
  96. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
  97. 0, 0, MOXA_BOARD_C218_PCI },
  98. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
  99. 0, 0, MOXA_BOARD_C320_PCI },
  100. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
  101. 0, 0, MOXA_BOARD_CP204J },
  102. { 0 }
  103. };
  104. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  105. #endif /* CONFIG_PCI */
  106. typedef struct _moxa_isa_board_conf {
  107. int boardType;
  108. int numPorts;
  109. unsigned long baseAddr;
  110. } moxa_isa_board_conf;
  111. static moxa_isa_board_conf moxa_isa_boards[] =
  112. {
  113. /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
  114. };
  115. typedef struct _moxa_pci_devinfo {
  116. ushort busNum;
  117. ushort devNum;
  118. } moxa_pci_devinfo;
  119. typedef struct _moxa_board_conf {
  120. int boardType;
  121. int numPorts;
  122. unsigned long baseAddr;
  123. int busType;
  124. moxa_pci_devinfo pciInfo;
  125. } moxa_board_conf;
  126. static moxa_board_conf moxa_boards[MAX_BOARDS];
  127. static void __iomem *moxaBaseAddr[MAX_BOARDS];
  128. struct moxa_str {
  129. int type;
  130. int port;
  131. int close_delay;
  132. unsigned short closing_wait;
  133. int count;
  134. int blocked_open;
  135. long event; /* long req'd for set_bit --RR */
  136. int asyncflags;
  137. unsigned long statusflags;
  138. struct tty_struct *tty;
  139. int cflag;
  140. wait_queue_head_t open_wait;
  141. wait_queue_head_t close_wait;
  142. struct work_struct tqueue;
  143. };
  144. struct mxser_mstatus {
  145. tcflag_t cflag;
  146. int cts;
  147. int dsr;
  148. int ri;
  149. int dcd;
  150. };
  151. static struct mxser_mstatus GMStatus[MAX_PORTS];
  152. /* statusflags */
  153. #define TXSTOPPED 0x1
  154. #define LOWWAIT 0x2
  155. #define EMPTYWAIT 0x4
  156. #define THROTTLE 0x8
  157. /* event */
  158. #define MOXA_EVENT_HANGUP 1
  159. #define SERIAL_DO_RESTART
  160. #define SERIAL_TYPE_NORMAL 1
  161. #define WAKEUP_CHARS 256
  162. #define PORTNO(x) ((x)->index)
  163. static int verbose = 0;
  164. static int ttymajor = MOXAMAJOR;
  165. /* Variables for insmod */
  166. #ifdef MODULE
  167. static int baseaddr[] = {0, 0, 0, 0};
  168. static int type[] = {0, 0, 0, 0};
  169. static int numports[] = {0, 0, 0, 0};
  170. #endif
  171. MODULE_AUTHOR("William Chen");
  172. MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
  173. MODULE_LICENSE("GPL");
  174. #ifdef MODULE
  175. module_param_array(type, int, NULL, 0);
  176. module_param_array(baseaddr, int, NULL, 0);
  177. module_param_array(numports, int, NULL, 0);
  178. #endif
  179. module_param(ttymajor, int, 0);
  180. module_param(verbose, bool, 0644);
  181. static struct tty_driver *moxaDriver;
  182. static struct moxa_str moxaChannels[MAX_PORTS];
  183. static unsigned char *moxaXmitBuff;
  184. static int moxaTimer_on;
  185. static struct timer_list moxaTimer;
  186. static int moxaEmptyTimer_on[MAX_PORTS];
  187. static struct timer_list moxaEmptyTimer[MAX_PORTS];
  188. static struct semaphore moxaBuffSem;
  189. /*
  190. * static functions:
  191. */
  192. static void do_moxa_softint(void *);
  193. static int moxa_open(struct tty_struct *, struct file *);
  194. static void moxa_close(struct tty_struct *, struct file *);
  195. static int moxa_write(struct tty_struct *, const unsigned char *, int);
  196. static int moxa_write_room(struct tty_struct *);
  197. static void moxa_flush_buffer(struct tty_struct *);
  198. static int moxa_chars_in_buffer(struct tty_struct *);
  199. static void moxa_flush_chars(struct tty_struct *);
  200. static void moxa_put_char(struct tty_struct *, unsigned char);
  201. static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
  202. static void moxa_throttle(struct tty_struct *);
  203. static void moxa_unthrottle(struct tty_struct *);
  204. static void moxa_set_termios(struct tty_struct *, struct termios *);
  205. static void moxa_stop(struct tty_struct *);
  206. static void moxa_start(struct tty_struct *);
  207. static void moxa_hangup(struct tty_struct *);
  208. static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
  209. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  210. unsigned int set, unsigned int clear);
  211. static void moxa_poll(unsigned long);
  212. static void set_tty_param(struct tty_struct *);
  213. static int block_till_ready(struct tty_struct *, struct file *,
  214. struct moxa_str *);
  215. static void setup_empty_event(struct tty_struct *);
  216. static void check_xmit_empty(unsigned long);
  217. static void shut_down(struct moxa_str *);
  218. static void receive_data(struct moxa_str *);
  219. /*
  220. * moxa board interface functions:
  221. */
  222. static void MoxaDriverInit(void);
  223. static int MoxaDriverIoctl(unsigned int, unsigned long, int);
  224. static int MoxaDriverPoll(void);
  225. static int MoxaPortsOfCard(int);
  226. static int MoxaPortIsValid(int);
  227. static void MoxaPortEnable(int);
  228. static void MoxaPortDisable(int);
  229. static long MoxaPortGetMaxBaud(int);
  230. static long MoxaPortSetBaud(int, long);
  231. static int MoxaPortSetTermio(int, struct termios *);
  232. static int MoxaPortGetLineOut(int, int *, int *);
  233. static void MoxaPortLineCtrl(int, int, int);
  234. static void MoxaPortFlowCtrl(int, int, int, int, int, int);
  235. static int MoxaPortLineStatus(int);
  236. static int MoxaPortDCDChange(int);
  237. static int MoxaPortDCDON(int);
  238. static void MoxaPortFlushData(int, int);
  239. static int MoxaPortWriteData(int, unsigned char *, int);
  240. static int MoxaPortReadData(int, struct tty_struct *tty);
  241. static int MoxaPortTxQueue(int);
  242. static int MoxaPortRxQueue(int);
  243. static int MoxaPortTxFree(int);
  244. static void MoxaPortTxDisable(int);
  245. static void MoxaPortTxEnable(int);
  246. static int MoxaPortResetBrkCnt(int);
  247. static void MoxaPortSendBreak(int, int);
  248. static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
  249. static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
  250. static void MoxaSetFifo(int port, int enable);
  251. static struct tty_operations moxa_ops = {
  252. .open = moxa_open,
  253. .close = moxa_close,
  254. .write = moxa_write,
  255. .write_room = moxa_write_room,
  256. .flush_buffer = moxa_flush_buffer,
  257. .chars_in_buffer = moxa_chars_in_buffer,
  258. .flush_chars = moxa_flush_chars,
  259. .put_char = moxa_put_char,
  260. .ioctl = moxa_ioctl,
  261. .throttle = moxa_throttle,
  262. .unthrottle = moxa_unthrottle,
  263. .set_termios = moxa_set_termios,
  264. .stop = moxa_stop,
  265. .start = moxa_start,
  266. .hangup = moxa_hangup,
  267. .tiocmget = moxa_tiocmget,
  268. .tiocmset = moxa_tiocmset,
  269. };
  270. static DEFINE_SPINLOCK(moxa_lock);
  271. #ifdef CONFIG_PCI
  272. static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
  273. {
  274. board->baseAddr = pci_resource_start (p, 2);
  275. board->boardType = board_type;
  276. switch (board_type) {
  277. case MOXA_BOARD_C218_ISA:
  278. case MOXA_BOARD_C218_PCI:
  279. board->numPorts = 8;
  280. break;
  281. case MOXA_BOARD_CP204J:
  282. board->numPorts = 4;
  283. break;
  284. default:
  285. board->numPorts = 0;
  286. break;
  287. }
  288. board->busType = MOXA_BUS_TYPE_PCI;
  289. board->pciInfo.busNum = p->bus->number;
  290. board->pciInfo.devNum = p->devfn >> 3;
  291. return (0);
  292. }
  293. #endif /* CONFIG_PCI */
  294. static int __init moxa_init(void)
  295. {
  296. int i, numBoards;
  297. struct moxa_str *ch;
  298. printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
  299. moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
  300. if (!moxaDriver)
  301. return -ENOMEM;
  302. init_MUTEX(&moxaBuffSem);
  303. moxaDriver->owner = THIS_MODULE;
  304. moxaDriver->name = "ttyMX";
  305. moxaDriver->major = ttymajor;
  306. moxaDriver->minor_start = 0;
  307. moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
  308. moxaDriver->subtype = SERIAL_TYPE_NORMAL;
  309. moxaDriver->init_termios = tty_std_termios;
  310. moxaDriver->init_termios.c_iflag = 0;
  311. moxaDriver->init_termios.c_oflag = 0;
  312. moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  313. moxaDriver->init_termios.c_lflag = 0;
  314. moxaDriver->flags = TTY_DRIVER_REAL_RAW;
  315. tty_set_operations(moxaDriver, &moxa_ops);
  316. moxaXmitBuff = NULL;
  317. for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
  318. ch->type = PORT_16550A;
  319. ch->port = i;
  320. INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
  321. ch->tty = NULL;
  322. ch->close_delay = 5 * HZ / 10;
  323. ch->closing_wait = 30 * HZ;
  324. ch->count = 0;
  325. ch->blocked_open = 0;
  326. ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  327. init_waitqueue_head(&ch->open_wait);
  328. init_waitqueue_head(&ch->close_wait);
  329. }
  330. for (i = 0; i < MAX_BOARDS; i++) {
  331. moxa_boards[i].boardType = 0;
  332. moxa_boards[i].numPorts = 0;
  333. moxa_boards[i].baseAddr = 0;
  334. moxa_boards[i].busType = 0;
  335. moxa_boards[i].pciInfo.busNum = 0;
  336. moxa_boards[i].pciInfo.devNum = 0;
  337. }
  338. MoxaDriverInit();
  339. printk("Tty devices major number = %d\n", ttymajor);
  340. if (tty_register_driver(moxaDriver)) {
  341. printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
  342. put_tty_driver(moxaDriver);
  343. return -1;
  344. }
  345. for (i = 0; i < MAX_PORTS; i++) {
  346. init_timer(&moxaEmptyTimer[i]);
  347. moxaEmptyTimer[i].function = check_xmit_empty;
  348. moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
  349. moxaEmptyTimer_on[i] = 0;
  350. }
  351. init_timer(&moxaTimer);
  352. moxaTimer.function = moxa_poll;
  353. moxaTimer.expires = jiffies + (HZ / 50);
  354. moxaTimer_on = 1;
  355. add_timer(&moxaTimer);
  356. /* Find the boards defined in source code */
  357. numBoards = 0;
  358. for (i = 0; i < MAX_BOARDS; i++) {
  359. if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
  360. (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
  361. moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
  362. if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
  363. moxa_boards[numBoards].numPorts = 8;
  364. else
  365. moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
  366. moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
  367. moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
  368. if (verbose)
  369. printk("Board %2d: %s board(baseAddr=%lx)\n",
  370. numBoards + 1,
  371. moxa_brdname[moxa_boards[numBoards].boardType - 1],
  372. moxa_boards[numBoards].baseAddr);
  373. numBoards++;
  374. }
  375. }
  376. /* Find the boards defined form module args. */
  377. #ifdef MODULE
  378. for (i = 0; i < MAX_BOARDS; i++) {
  379. if ((type[i] == MOXA_BOARD_C218_ISA) ||
  380. (type[i] == MOXA_BOARD_C320_ISA)) {
  381. if (verbose)
  382. printk("Board %2d: %s board(baseAddr=%lx)\n",
  383. numBoards + 1,
  384. moxa_brdname[type[i] - 1],
  385. (unsigned long) baseaddr[i]);
  386. if (numBoards >= MAX_BOARDS) {
  387. if (verbose)
  388. printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
  389. continue;
  390. }
  391. moxa_boards[numBoards].boardType = type[i];
  392. if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
  393. moxa_boards[numBoards].numPorts = 8;
  394. else
  395. moxa_boards[numBoards].numPorts = numports[i];
  396. moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
  397. moxa_boards[numBoards].baseAddr = baseaddr[i];
  398. numBoards++;
  399. }
  400. }
  401. #endif
  402. /* Find PCI boards here */
  403. #ifdef CONFIG_PCI
  404. {
  405. struct pci_dev *p = NULL;
  406. int n = ARRAY_SIZE(moxa_pcibrds) - 1;
  407. i = 0;
  408. while (i < n) {
  409. while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
  410. {
  411. if (pci_enable_device(p))
  412. continue;
  413. if (numBoards >= MAX_BOARDS) {
  414. if (verbose)
  415. printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
  416. } else {
  417. moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
  418. &moxa_boards[numBoards]);
  419. numBoards++;
  420. }
  421. }
  422. i++;
  423. }
  424. }
  425. #endif
  426. for (i = 0; i < numBoards; i++) {
  427. moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
  428. }
  429. return (0);
  430. }
  431. static void __exit moxa_exit(void)
  432. {
  433. int i;
  434. if (verbose)
  435. printk("Unloading module moxa ...\n");
  436. if (moxaTimer_on)
  437. del_timer(&moxaTimer);
  438. for (i = 0; i < MAX_PORTS; i++)
  439. if (moxaEmptyTimer_on[i])
  440. del_timer(&moxaEmptyTimer[i]);
  441. if (tty_unregister_driver(moxaDriver))
  442. printk("Couldn't unregister MOXA Intellio family serial driver\n");
  443. put_tty_driver(moxaDriver);
  444. if (verbose)
  445. printk("Done\n");
  446. }
  447. module_init(moxa_init);
  448. module_exit(moxa_exit);
  449. static void do_moxa_softint(void *private_)
  450. {
  451. struct moxa_str *ch = (struct moxa_str *) private_;
  452. struct tty_struct *tty;
  453. if (ch && (tty = ch->tty)) {
  454. if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
  455. tty_hangup(tty); /* FIXME: module removal race here - AKPM */
  456. wake_up_interruptible(&ch->open_wait);
  457. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  458. }
  459. }
  460. }
  461. static int moxa_open(struct tty_struct *tty, struct file *filp)
  462. {
  463. struct moxa_str *ch;
  464. int port;
  465. int retval;
  466. unsigned long page;
  467. port = PORTNO(tty);
  468. if (port == MAX_PORTS) {
  469. return (0);
  470. }
  471. if (!MoxaPortIsValid(port)) {
  472. tty->driver_data = NULL;
  473. return (-ENODEV);
  474. }
  475. down(&moxaBuffSem);
  476. if (!moxaXmitBuff) {
  477. page = get_zeroed_page(GFP_KERNEL);
  478. if (!page) {
  479. up(&moxaBuffSem);
  480. return (-ENOMEM);
  481. }
  482. /* This test is guarded by the BuffSem so no longer needed
  483. delete me in 2.5 */
  484. if (moxaXmitBuff)
  485. free_page(page);
  486. else
  487. moxaXmitBuff = (unsigned char *) page;
  488. }
  489. up(&moxaBuffSem);
  490. ch = &moxaChannels[port];
  491. ch->count++;
  492. tty->driver_data = ch;
  493. ch->tty = tty;
  494. if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
  495. ch->statusflags = 0;
  496. set_tty_param(tty);
  497. MoxaPortLineCtrl(ch->port, 1, 1);
  498. MoxaPortEnable(ch->port);
  499. ch->asyncflags |= ASYNC_INITIALIZED;
  500. }
  501. retval = block_till_ready(tty, filp, ch);
  502. moxa_unthrottle(tty);
  503. if (ch->type == PORT_16550A) {
  504. MoxaSetFifo(ch->port, 1);
  505. } else {
  506. MoxaSetFifo(ch->port, 0);
  507. }
  508. return (retval);
  509. }
  510. static void moxa_close(struct tty_struct *tty, struct file *filp)
  511. {
  512. struct moxa_str *ch;
  513. int port;
  514. port = PORTNO(tty);
  515. if (port == MAX_PORTS) {
  516. return;
  517. }
  518. if (!MoxaPortIsValid(port)) {
  519. #ifdef SERIAL_DEBUG_CLOSE
  520. printk("Invalid portno in moxa_close\n");
  521. #endif
  522. tty->driver_data = NULL;
  523. return;
  524. }
  525. if (tty->driver_data == NULL) {
  526. return;
  527. }
  528. if (tty_hung_up_p(filp)) {
  529. return;
  530. }
  531. ch = (struct moxa_str *) tty->driver_data;
  532. if ((tty->count == 1) && (ch->count != 1)) {
  533. printk("moxa_close: bad serial port count; tty->count is 1, "
  534. "ch->count is %d\n", ch->count);
  535. ch->count = 1;
  536. }
  537. if (--ch->count < 0) {
  538. printk("moxa_close: bad serial port count, device=%s\n",
  539. tty->name);
  540. ch->count = 0;
  541. }
  542. if (ch->count) {
  543. return;
  544. }
  545. ch->asyncflags |= ASYNC_CLOSING;
  546. ch->cflag = tty->termios->c_cflag;
  547. if (ch->asyncflags & ASYNC_INITIALIZED) {
  548. setup_empty_event(tty);
  549. tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
  550. moxaEmptyTimer_on[ch->port] = 0;
  551. del_timer(&moxaEmptyTimer[ch->port]);
  552. }
  553. shut_down(ch);
  554. MoxaPortFlushData(port, 2);
  555. if (tty->driver->flush_buffer)
  556. tty->driver->flush_buffer(tty);
  557. tty_ldisc_flush(tty);
  558. tty->closing = 0;
  559. ch->event = 0;
  560. ch->tty = NULL;
  561. if (ch->blocked_open) {
  562. if (ch->close_delay) {
  563. msleep_interruptible(jiffies_to_msecs(ch->close_delay));
  564. }
  565. wake_up_interruptible(&ch->open_wait);
  566. }
  567. ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  568. wake_up_interruptible(&ch->close_wait);
  569. }
  570. static int moxa_write(struct tty_struct *tty,
  571. const unsigned char *buf, int count)
  572. {
  573. struct moxa_str *ch;
  574. int len, port;
  575. unsigned long flags;
  576. ch = (struct moxa_str *) tty->driver_data;
  577. if (ch == NULL)
  578. return (0);
  579. port = ch->port;
  580. spin_lock_irqsave(&moxa_lock, flags);
  581. len = MoxaPortWriteData(port, (unsigned char *) buf, count);
  582. spin_unlock_irqrestore(&moxa_lock, flags);
  583. /*********************************************
  584. if ( !(ch->statusflags & LOWWAIT) &&
  585. ((len != count) || (MoxaPortTxFree(port) <= 100)) )
  586. ************************************************/
  587. ch->statusflags |= LOWWAIT;
  588. return (len);
  589. }
  590. static int moxa_write_room(struct tty_struct *tty)
  591. {
  592. struct moxa_str *ch;
  593. if (tty->stopped)
  594. return (0);
  595. ch = (struct moxa_str *) tty->driver_data;
  596. if (ch == NULL)
  597. return (0);
  598. return (MoxaPortTxFree(ch->port));
  599. }
  600. static void moxa_flush_buffer(struct tty_struct *tty)
  601. {
  602. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  603. if (ch == NULL)
  604. return;
  605. MoxaPortFlushData(ch->port, 1);
  606. tty_wakeup(tty);
  607. }
  608. static int moxa_chars_in_buffer(struct tty_struct *tty)
  609. {
  610. int chars;
  611. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  612. /*
  613. * Sigh...I have to check if driver_data is NULL here, because
  614. * if an open() fails, the TTY subsystem eventually calls
  615. * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
  616. * routine. And since the open() failed, we return 0 here. TDJ
  617. */
  618. if (ch == NULL)
  619. return (0);
  620. chars = MoxaPortTxQueue(ch->port);
  621. if (chars) {
  622. /*
  623. * Make it possible to wakeup anything waiting for output
  624. * in tty_ioctl.c, etc.
  625. */
  626. if (!(ch->statusflags & EMPTYWAIT))
  627. setup_empty_event(tty);
  628. }
  629. return (chars);
  630. }
  631. static void moxa_flush_chars(struct tty_struct *tty)
  632. {
  633. /*
  634. * Don't think I need this, because this is called to empty the TX
  635. * buffer for the 16450, 16550, etc.
  636. */
  637. }
  638. static void moxa_put_char(struct tty_struct *tty, unsigned char c)
  639. {
  640. struct moxa_str *ch;
  641. int port;
  642. unsigned long flags;
  643. ch = (struct moxa_str *) tty->driver_data;
  644. if (ch == NULL)
  645. return;
  646. port = ch->port;
  647. spin_lock_irqsave(&moxa_lock, flags);
  648. moxaXmitBuff[0] = c;
  649. MoxaPortWriteData(port, moxaXmitBuff, 1);
  650. spin_unlock_irqrestore(&moxa_lock, flags);
  651. /************************************************
  652. if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
  653. *************************************************/
  654. ch->statusflags |= LOWWAIT;
  655. }
  656. static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
  657. {
  658. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  659. int port;
  660. int flag = 0, dtr, rts;
  661. port = PORTNO(tty);
  662. if ((port != MAX_PORTS) && (!ch))
  663. return (-EINVAL);
  664. MoxaPortGetLineOut(ch->port, &dtr, &rts);
  665. if (dtr)
  666. flag |= TIOCM_DTR;
  667. if (rts)
  668. flag |= TIOCM_RTS;
  669. dtr = MoxaPortLineStatus(ch->port);
  670. if (dtr & 1)
  671. flag |= TIOCM_CTS;
  672. if (dtr & 2)
  673. flag |= TIOCM_DSR;
  674. if (dtr & 4)
  675. flag |= TIOCM_CD;
  676. return flag;
  677. }
  678. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  679. unsigned int set, unsigned int clear)
  680. {
  681. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  682. int port;
  683. int dtr, rts;
  684. port = PORTNO(tty);
  685. if ((port != MAX_PORTS) && (!ch))
  686. return (-EINVAL);
  687. MoxaPortGetLineOut(ch->port, &dtr, &rts);
  688. if (set & TIOCM_RTS)
  689. rts = 1;
  690. if (set & TIOCM_DTR)
  691. dtr = 1;
  692. if (clear & TIOCM_RTS)
  693. rts = 0;
  694. if (clear & TIOCM_DTR)
  695. dtr = 0;
  696. MoxaPortLineCtrl(ch->port, dtr, rts);
  697. return 0;
  698. }
  699. static int moxa_ioctl(struct tty_struct *tty, struct file *file,
  700. unsigned int cmd, unsigned long arg)
  701. {
  702. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  703. register int port;
  704. void __user *argp = (void __user *)arg;
  705. int retval;
  706. port = PORTNO(tty);
  707. if ((port != MAX_PORTS) && (!ch))
  708. return (-EINVAL);
  709. switch (cmd) {
  710. case TCSBRK: /* SVID version: non-zero arg --> no break */
  711. retval = tty_check_change(tty);
  712. if (retval)
  713. return (retval);
  714. setup_empty_event(tty);
  715. tty_wait_until_sent(tty, 0);
  716. if (!arg)
  717. MoxaPortSendBreak(ch->port, 0);
  718. return (0);
  719. case TCSBRKP: /* support for POSIX tcsendbreak() */
  720. retval = tty_check_change(tty);
  721. if (retval)
  722. return (retval);
  723. setup_empty_event(tty);
  724. tty_wait_until_sent(tty, 0);
  725. MoxaPortSendBreak(ch->port, arg);
  726. return (0);
  727. case TIOCGSOFTCAR:
  728. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
  729. case TIOCSSOFTCAR:
  730. if(get_user(retval, (unsigned long __user *) argp))
  731. return -EFAULT;
  732. arg = retval;
  733. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
  734. (arg ? CLOCAL : 0));
  735. if (C_CLOCAL(tty))
  736. ch->asyncflags &= ~ASYNC_CHECK_CD;
  737. else
  738. ch->asyncflags |= ASYNC_CHECK_CD;
  739. return (0);
  740. case TIOCGSERIAL:
  741. return moxa_get_serial_info(ch, argp);
  742. case TIOCSSERIAL:
  743. return moxa_set_serial_info(ch, argp);
  744. default:
  745. retval = MoxaDriverIoctl(cmd, arg, port);
  746. }
  747. return (retval);
  748. }
  749. static void moxa_throttle(struct tty_struct *tty)
  750. {
  751. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  752. ch->statusflags |= THROTTLE;
  753. }
  754. static void moxa_unthrottle(struct tty_struct *tty)
  755. {
  756. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  757. ch->statusflags &= ~THROTTLE;
  758. }
  759. static void moxa_set_termios(struct tty_struct *tty,
  760. struct termios *old_termios)
  761. {
  762. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  763. if (ch == NULL)
  764. return;
  765. set_tty_param(tty);
  766. if (!(old_termios->c_cflag & CLOCAL) &&
  767. (tty->termios->c_cflag & CLOCAL))
  768. wake_up_interruptible(&ch->open_wait);
  769. }
  770. static void moxa_stop(struct tty_struct *tty)
  771. {
  772. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  773. if (ch == NULL)
  774. return;
  775. MoxaPortTxDisable(ch->port);
  776. ch->statusflags |= TXSTOPPED;
  777. }
  778. static void moxa_start(struct tty_struct *tty)
  779. {
  780. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  781. if (ch == NULL)
  782. return;
  783. if (!(ch->statusflags & TXSTOPPED))
  784. return;
  785. MoxaPortTxEnable(ch->port);
  786. ch->statusflags &= ~TXSTOPPED;
  787. }
  788. static void moxa_hangup(struct tty_struct *tty)
  789. {
  790. struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
  791. moxa_flush_buffer(tty);
  792. shut_down(ch);
  793. ch->event = 0;
  794. ch->count = 0;
  795. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  796. ch->tty = NULL;
  797. wake_up_interruptible(&ch->open_wait);
  798. }
  799. static void moxa_poll(unsigned long ignored)
  800. {
  801. register int card;
  802. struct moxa_str *ch;
  803. struct tty_struct *tp;
  804. int i, ports;
  805. moxaTimer_on = 0;
  806. del_timer(&moxaTimer);
  807. if (MoxaDriverPoll() < 0) {
  808. moxaTimer.function = moxa_poll;
  809. moxaTimer.expires = jiffies + (HZ / 50);
  810. moxaTimer_on = 1;
  811. add_timer(&moxaTimer);
  812. return;
  813. }
  814. for (card = 0; card < MAX_BOARDS; card++) {
  815. if ((ports = MoxaPortsOfCard(card)) <= 0)
  816. continue;
  817. ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
  818. for (i = 0; i < ports; i++, ch++) {
  819. if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
  820. continue;
  821. if (!(ch->statusflags & THROTTLE) &&
  822. (MoxaPortRxQueue(ch->port) > 0))
  823. receive_data(ch);
  824. if ((tp = ch->tty) == 0)
  825. continue;
  826. if (ch->statusflags & LOWWAIT) {
  827. if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
  828. if (!tp->stopped) {
  829. ch->statusflags &= ~LOWWAIT;
  830. tty_wakeup(tp);
  831. }
  832. }
  833. }
  834. if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
  835. tty_insert_flip_char(tp, 0, TTY_BREAK);
  836. tty_schedule_flip(tp);
  837. }
  838. if (MoxaPortDCDChange(ch->port)) {
  839. if (ch->asyncflags & ASYNC_CHECK_CD) {
  840. if (MoxaPortDCDON(ch->port))
  841. wake_up_interruptible(&ch->open_wait);
  842. else {
  843. set_bit(MOXA_EVENT_HANGUP, &ch->event);
  844. schedule_work(&ch->tqueue);
  845. }
  846. }
  847. }
  848. }
  849. }
  850. moxaTimer.function = moxa_poll;
  851. moxaTimer.expires = jiffies + (HZ / 50);
  852. moxaTimer_on = 1;
  853. add_timer(&moxaTimer);
  854. }
  855. /******************************************************************************/
  856. static void set_tty_param(struct tty_struct *tty)
  857. {
  858. register struct termios *ts;
  859. struct moxa_str *ch;
  860. int rts, cts, txflow, rxflow, xany;
  861. ch = (struct moxa_str *) tty->driver_data;
  862. ts = tty->termios;
  863. if (ts->c_cflag & CLOCAL)
  864. ch->asyncflags &= ~ASYNC_CHECK_CD;
  865. else
  866. ch->asyncflags |= ASYNC_CHECK_CD;
  867. rts = cts = txflow = rxflow = xany = 0;
  868. if (ts->c_cflag & CRTSCTS)
  869. rts = cts = 1;
  870. if (ts->c_iflag & IXON)
  871. txflow = 1;
  872. if (ts->c_iflag & IXOFF)
  873. rxflow = 1;
  874. if (ts->c_iflag & IXANY)
  875. xany = 1;
  876. MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
  877. MoxaPortSetTermio(ch->port, ts);
  878. }
  879. static int block_till_ready(struct tty_struct *tty, struct file *filp,
  880. struct moxa_str *ch)
  881. {
  882. DECLARE_WAITQUEUE(wait,current);
  883. unsigned long flags;
  884. int retval;
  885. int do_clocal = C_CLOCAL(tty);
  886. /*
  887. * If the device is in the middle of being closed, then block
  888. * until it's done, and then try again.
  889. */
  890. if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
  891. if (ch->asyncflags & ASYNC_CLOSING)
  892. interruptible_sleep_on(&ch->close_wait);
  893. #ifdef SERIAL_DO_RESTART
  894. if (ch->asyncflags & ASYNC_HUP_NOTIFY)
  895. return (-EAGAIN);
  896. else
  897. return (-ERESTARTSYS);
  898. #else
  899. return (-EAGAIN);
  900. #endif
  901. }
  902. /*
  903. * If non-blocking mode is set, then make the check up front
  904. * and then exit.
  905. */
  906. if (filp->f_flags & O_NONBLOCK) {
  907. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  908. return (0);
  909. }
  910. /*
  911. * Block waiting for the carrier detect and the line to become free
  912. */
  913. retval = 0;
  914. add_wait_queue(&ch->open_wait, &wait);
  915. #ifdef SERIAL_DEBUG_OPEN
  916. printk("block_til_ready before block: ttys%d, count = %d\n",
  917. ch->line, ch->count);
  918. #endif
  919. spin_lock_irqsave(&moxa_lock, flags);
  920. if (!tty_hung_up_p(filp))
  921. ch->count--;
  922. ch->blocked_open++;
  923. spin_unlock_irqrestore(&moxa_lock, flags);
  924. while (1) {
  925. set_current_state(TASK_INTERRUPTIBLE);
  926. if (tty_hung_up_p(filp) ||
  927. !(ch->asyncflags & ASYNC_INITIALIZED)) {
  928. #ifdef SERIAL_DO_RESTART
  929. if (ch->asyncflags & ASYNC_HUP_NOTIFY)
  930. retval = -EAGAIN;
  931. else
  932. retval = -ERESTARTSYS;
  933. #else
  934. retval = -EAGAIN;
  935. #endif
  936. break;
  937. }
  938. if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
  939. MoxaPortDCDON(ch->port)))
  940. break;
  941. if (signal_pending(current)) {
  942. retval = -ERESTARTSYS;
  943. break;
  944. }
  945. schedule();
  946. }
  947. set_current_state(TASK_RUNNING);
  948. remove_wait_queue(&ch->open_wait, &wait);
  949. spin_lock_irqsave(&moxa_lock, flags);
  950. if (!tty_hung_up_p(filp))
  951. ch->count++;
  952. ch->blocked_open--;
  953. spin_unlock_irqrestore(&moxa_lock, flags);
  954. #ifdef SERIAL_DEBUG_OPEN
  955. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  956. ch->line, ch->count);
  957. #endif
  958. if (retval)
  959. return (retval);
  960. /* FIXME: review to see if we need to use set_bit on these */
  961. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  962. return 0;
  963. }
  964. static void setup_empty_event(struct tty_struct *tty)
  965. {
  966. struct moxa_str *ch = tty->driver_data;
  967. unsigned long flags;
  968. spin_lock_irqsave(&moxa_lock, flags);
  969. ch->statusflags |= EMPTYWAIT;
  970. moxaEmptyTimer_on[ch->port] = 0;
  971. del_timer(&moxaEmptyTimer[ch->port]);
  972. moxaEmptyTimer[ch->port].expires = jiffies + HZ;
  973. moxaEmptyTimer_on[ch->port] = 1;
  974. add_timer(&moxaEmptyTimer[ch->port]);
  975. spin_unlock_irqrestore(&moxa_lock, flags);
  976. }
  977. static void check_xmit_empty(unsigned long data)
  978. {
  979. struct moxa_str *ch;
  980. ch = (struct moxa_str *) data;
  981. moxaEmptyTimer_on[ch->port] = 0;
  982. del_timer(&moxaEmptyTimer[ch->port]);
  983. if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
  984. if (MoxaPortTxQueue(ch->port) == 0) {
  985. ch->statusflags &= ~EMPTYWAIT;
  986. tty_wakeup(ch->tty);
  987. return;
  988. }
  989. moxaEmptyTimer[ch->port].expires = jiffies + HZ;
  990. moxaEmptyTimer_on[ch->port] = 1;
  991. add_timer(&moxaEmptyTimer[ch->port]);
  992. } else
  993. ch->statusflags &= ~EMPTYWAIT;
  994. }
  995. static void shut_down(struct moxa_str *ch)
  996. {
  997. struct tty_struct *tp;
  998. if (!(ch->asyncflags & ASYNC_INITIALIZED))
  999. return;
  1000. tp = ch->tty;
  1001. MoxaPortDisable(ch->port);
  1002. /*
  1003. * If we're a modem control device and HUPCL is on, drop RTS & DTR.
  1004. */
  1005. if (tp->termios->c_cflag & HUPCL)
  1006. MoxaPortLineCtrl(ch->port, 0, 0);
  1007. ch->asyncflags &= ~ASYNC_INITIALIZED;
  1008. }
  1009. static void receive_data(struct moxa_str *ch)
  1010. {
  1011. struct tty_struct *tp;
  1012. struct termios *ts;
  1013. unsigned long flags;
  1014. ts = NULL;
  1015. tp = ch->tty;
  1016. if (tp)
  1017. ts = tp->termios;
  1018. /**************************************************
  1019. if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
  1020. *****************************************************/
  1021. if (!tp || !ts) {
  1022. MoxaPortFlushData(ch->port, 0);
  1023. return;
  1024. }
  1025. spin_lock_irqsave(&moxa_lock, flags);
  1026. MoxaPortReadData(ch->port, tp);
  1027. spin_unlock_irqrestore(&moxa_lock, flags);
  1028. tty_schedule_flip(tp);
  1029. }
  1030. #define Magic_code 0x404
  1031. /*
  1032. * System Configuration
  1033. */
  1034. /*
  1035. * for C218 BIOS initialization
  1036. */
  1037. #define C218_ConfBase 0x800
  1038. #define C218_status (C218_ConfBase + 0) /* BIOS running status */
  1039. #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
  1040. #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
  1041. #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
  1042. #define C218check_sum (C218_ConfBase + 8) /* BYTE */
  1043. #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
  1044. #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
  1045. #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
  1046. #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
  1047. #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
  1048. #define C218_LoadBuf 0x0F00
  1049. #define C218_KeyCode 0x218
  1050. #define CP204J_KeyCode 0x204
  1051. /*
  1052. * for C320 BIOS initialization
  1053. */
  1054. #define C320_ConfBase 0x800
  1055. #define C320_LoadBuf 0x0f00
  1056. #define STS_init 0x05 /* for C320_status */
  1057. #define C320_status C320_ConfBase + 0 /* BIOS running status */
  1058. #define C320_diag C320_ConfBase + 2 /* diagnostic status */
  1059. #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
  1060. #define C320DLoad_len C320_ConfBase + 6 /* WORD */
  1061. #define C320check_sum C320_ConfBase + 8 /* WORD */
  1062. #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
  1063. #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
  1064. #define C320UART_no C320_ConfBase + 0x0e /* WORD */
  1065. #define C320_KeyCode 0x320
  1066. #define FixPage_addr 0x0000 /* starting addr of static page */
  1067. #define DynPage_addr 0x2000 /* starting addr of dynamic page */
  1068. #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
  1069. #define Control_reg 0x1ff0 /* select page and reset control */
  1070. #define HW_reset 0x80
  1071. /*
  1072. * Function Codes
  1073. */
  1074. #define FC_CardReset 0x80
  1075. #define FC_ChannelReset 1 /* C320 firmware not supported */
  1076. #define FC_EnableCH 2
  1077. #define FC_DisableCH 3
  1078. #define FC_SetParam 4
  1079. #define FC_SetMode 5
  1080. #define FC_SetRate 6
  1081. #define FC_LineControl 7
  1082. #define FC_LineStatus 8
  1083. #define FC_XmitControl 9
  1084. #define FC_FlushQueue 10
  1085. #define FC_SendBreak 11
  1086. #define FC_StopBreak 12
  1087. #define FC_LoopbackON 13
  1088. #define FC_LoopbackOFF 14
  1089. #define FC_ClrIrqTable 15
  1090. #define FC_SendXon 16
  1091. #define FC_SetTermIrq 17 /* C320 firmware not supported */
  1092. #define FC_SetCntIrq 18 /* C320 firmware not supported */
  1093. #define FC_SetBreakIrq 19
  1094. #define FC_SetLineIrq 20
  1095. #define FC_SetFlowCtl 21
  1096. #define FC_GenIrq 22
  1097. #define FC_InCD180 23
  1098. #define FC_OutCD180 24
  1099. #define FC_InUARTreg 23
  1100. #define FC_OutUARTreg 24
  1101. #define FC_SetXonXoff 25
  1102. #define FC_OutCD180CCR 26
  1103. #define FC_ExtIQueue 27
  1104. #define FC_ExtOQueue 28
  1105. #define FC_ClrLineIrq 29
  1106. #define FC_HWFlowCtl 30
  1107. #define FC_GetClockRate 35
  1108. #define FC_SetBaud 36
  1109. #define FC_SetDataMode 41
  1110. #define FC_GetCCSR 43
  1111. #define FC_GetDataError 45
  1112. #define FC_RxControl 50
  1113. #define FC_ImmSend 51
  1114. #define FC_SetXonState 52
  1115. #define FC_SetXoffState 53
  1116. #define FC_SetRxFIFOTrig 54
  1117. #define FC_SetTxFIFOCnt 55
  1118. #define FC_UnixRate 56
  1119. #define FC_UnixResetTimer 57
  1120. #define RxFIFOTrig1 0
  1121. #define RxFIFOTrig4 1
  1122. #define RxFIFOTrig8 2
  1123. #define RxFIFOTrig14 3
  1124. /*
  1125. * Dual-Ported RAM
  1126. */
  1127. #define DRAM_global 0
  1128. #define INT_data (DRAM_global + 0)
  1129. #define Config_base (DRAM_global + 0x108)
  1130. #define IRQindex (INT_data + 0)
  1131. #define IRQpending (INT_data + 4)
  1132. #define IRQtable (INT_data + 8)
  1133. /*
  1134. * Interrupt Status
  1135. */
  1136. #define IntrRx 0x01 /* receiver data O.K. */
  1137. #define IntrTx 0x02 /* transmit buffer empty */
  1138. #define IntrFunc 0x04 /* function complete */
  1139. #define IntrBreak 0x08 /* received break */
  1140. #define IntrLine 0x10 /* line status change
  1141. for transmitter */
  1142. #define IntrIntr 0x20 /* received INTR code */
  1143. #define IntrQuit 0x40 /* received QUIT code */
  1144. #define IntrEOF 0x80 /* received EOF code */
  1145. #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
  1146. #define IntrTxTrigger 0x200 /* tx data count below trigger value */
  1147. #define Magic_no (Config_base + 0)
  1148. #define Card_model_no (Config_base + 2)
  1149. #define Total_ports (Config_base + 4)
  1150. #define Module_cnt (Config_base + 8)
  1151. #define Module_no (Config_base + 10)
  1152. #define Timer_10ms (Config_base + 14)
  1153. #define Disable_IRQ (Config_base + 20)
  1154. #define TMS320_PORT1 (Config_base + 22)
  1155. #define TMS320_PORT2 (Config_base + 24)
  1156. #define TMS320_CLOCK (Config_base + 26)
  1157. /*
  1158. * DATA BUFFER in DRAM
  1159. */
  1160. #define Extern_table 0x400 /* Base address of the external table
  1161. (24 words * 64) total 3K bytes
  1162. (24 words * 128) total 6K bytes */
  1163. #define Extern_size 0x60 /* 96 bytes */
  1164. #define RXrptr 0x00 /* read pointer for RX buffer */
  1165. #define RXwptr 0x02 /* write pointer for RX buffer */
  1166. #define TXrptr 0x04 /* read pointer for TX buffer */
  1167. #define TXwptr 0x06 /* write pointer for TX buffer */
  1168. #define HostStat 0x08 /* IRQ flag and general flag */
  1169. #define FlagStat 0x0A
  1170. #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
  1171. /* x x x x | | | | */
  1172. /* | | | + CTS flow */
  1173. /* | | +--- RTS flow */
  1174. /* | +------ TX Xon/Xoff */
  1175. /* +--------- RX Xon/Xoff */
  1176. #define Break_cnt 0x0E /* received break count */
  1177. #define CD180TXirq 0x10 /* if non-0: enable TX irq */
  1178. #define RX_mask 0x12
  1179. #define TX_mask 0x14
  1180. #define Ofs_rxb 0x16
  1181. #define Ofs_txb 0x18
  1182. #define Page_rxb 0x1A
  1183. #define Page_txb 0x1C
  1184. #define EndPage_rxb 0x1E
  1185. #define EndPage_txb 0x20
  1186. #define Data_error 0x22
  1187. #define RxTrigger 0x28
  1188. #define TxTrigger 0x2a
  1189. #define rRXwptr 0x34
  1190. #define Low_water 0x36
  1191. #define FuncCode 0x40
  1192. #define FuncArg 0x42
  1193. #define FuncArg1 0x44
  1194. #define C218rx_size 0x2000 /* 8K bytes */
  1195. #define C218tx_size 0x8000 /* 32K bytes */
  1196. #define C218rx_mask (C218rx_size - 1)
  1197. #define C218tx_mask (C218tx_size - 1)
  1198. #define C320p8rx_size 0x2000
  1199. #define C320p8tx_size 0x8000
  1200. #define C320p8rx_mask (C320p8rx_size - 1)
  1201. #define C320p8tx_mask (C320p8tx_size - 1)
  1202. #define C320p16rx_size 0x2000
  1203. #define C320p16tx_size 0x4000
  1204. #define C320p16rx_mask (C320p16rx_size - 1)
  1205. #define C320p16tx_mask (C320p16tx_size - 1)
  1206. #define C320p24rx_size 0x2000
  1207. #define C320p24tx_size 0x2000
  1208. #define C320p24rx_mask (C320p24rx_size - 1)
  1209. #define C320p24tx_mask (C320p24tx_size - 1)
  1210. #define C320p32rx_size 0x1000
  1211. #define C320p32tx_size 0x1000
  1212. #define C320p32rx_mask (C320p32rx_size - 1)
  1213. #define C320p32tx_mask (C320p32tx_size - 1)
  1214. #define Page_size 0x2000
  1215. #define Page_mask (Page_size - 1)
  1216. #define C218rx_spage 3
  1217. #define C218tx_spage 4
  1218. #define C218rx_pageno 1
  1219. #define C218tx_pageno 4
  1220. #define C218buf_pageno 5
  1221. #define C320p8rx_spage 3
  1222. #define C320p8tx_spage 4
  1223. #define C320p8rx_pgno 1
  1224. #define C320p8tx_pgno 4
  1225. #define C320p8buf_pgno 5
  1226. #define C320p16rx_spage 3
  1227. #define C320p16tx_spage 4
  1228. #define C320p16rx_pgno 1
  1229. #define C320p16tx_pgno 2
  1230. #define C320p16buf_pgno 3
  1231. #define C320p24rx_spage 3
  1232. #define C320p24tx_spage 4
  1233. #define C320p24rx_pgno 1
  1234. #define C320p24tx_pgno 1
  1235. #define C320p24buf_pgno 2
  1236. #define C320p32rx_spage 3
  1237. #define C320p32tx_ofs C320p32rx_size
  1238. #define C320p32tx_spage 3
  1239. #define C320p32buf_pgno 1
  1240. /*
  1241. * Host Status
  1242. */
  1243. #define WakeupRx 0x01
  1244. #define WakeupTx 0x02
  1245. #define WakeupBreak 0x08
  1246. #define WakeupLine 0x10
  1247. #define WakeupIntr 0x20
  1248. #define WakeupQuit 0x40
  1249. #define WakeupEOF 0x80 /* used in VTIME control */
  1250. #define WakeupRxTrigger 0x100
  1251. #define WakeupTxTrigger 0x200
  1252. /*
  1253. * Flag status
  1254. */
  1255. #define Rx_over 0x01
  1256. #define Xoff_state 0x02
  1257. #define Tx_flowOff 0x04
  1258. #define Tx_enable 0x08
  1259. #define CTS_state 0x10
  1260. #define DSR_state 0x20
  1261. #define DCD_state 0x80
  1262. /*
  1263. * FlowControl
  1264. */
  1265. #define CTS_FlowCtl 1
  1266. #define RTS_FlowCtl 2
  1267. #define Tx_FlowCtl 4
  1268. #define Rx_FlowCtl 8
  1269. #define IXM_IXANY 0x10
  1270. #define LowWater 128
  1271. #define DTR_ON 1
  1272. #define RTS_ON 2
  1273. #define CTS_ON 1
  1274. #define DSR_ON 2
  1275. #define DCD_ON 8
  1276. /* mode definition */
  1277. #define MX_CS8 0x03
  1278. #define MX_CS7 0x02
  1279. #define MX_CS6 0x01
  1280. #define MX_CS5 0x00
  1281. #define MX_STOP1 0x00
  1282. #define MX_STOP15 0x04
  1283. #define MX_STOP2 0x08
  1284. #define MX_PARNONE 0x00
  1285. #define MX_PAREVEN 0x40
  1286. #define MX_PARODD 0xC0
  1287. /*
  1288. * Query
  1289. */
  1290. #define QueryPort MAX_PORTS
  1291. struct mon_str {
  1292. int tick;
  1293. int rxcnt[MAX_PORTS];
  1294. int txcnt[MAX_PORTS];
  1295. };
  1296. typedef struct mon_str mon_st;
  1297. #define DCD_changed 0x01
  1298. #define DCD_oldstate 0x80
  1299. static unsigned char moxaBuff[10240];
  1300. static void __iomem *moxaIntNdx[MAX_BOARDS];
  1301. static void __iomem *moxaIntPend[MAX_BOARDS];
  1302. static void __iomem *moxaIntTable[MAX_BOARDS];
  1303. static char moxaChkPort[MAX_PORTS];
  1304. static char moxaLineCtrl[MAX_PORTS];
  1305. static void __iomem *moxaTableAddr[MAX_PORTS];
  1306. static long moxaCurBaud[MAX_PORTS];
  1307. static char moxaDCDState[MAX_PORTS];
  1308. static char moxaLowChkFlag[MAX_PORTS];
  1309. static int moxaLowWaterChk;
  1310. static int moxaCard;
  1311. static mon_st moxaLog;
  1312. static int moxaFuncTout;
  1313. static ushort moxaBreakCnt[MAX_PORTS];
  1314. static void moxadelay(int);
  1315. static void moxafunc(void __iomem *, int, ushort);
  1316. static void wait_finish(void __iomem *);
  1317. static void low_water_check(void __iomem *);
  1318. static int moxaloadbios(int, unsigned char __user *, int);
  1319. static int moxafindcard(int);
  1320. static int moxaload320b(int, unsigned char __user *, int);
  1321. static int moxaloadcode(int, unsigned char __user *, int);
  1322. static int moxaloadc218(int, void __iomem *, int);
  1323. static int moxaloadc320(int, void __iomem *, int, int *);
  1324. /*****************************************************************************
  1325. * Driver level functions: *
  1326. * 1. MoxaDriverInit(void); *
  1327. * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
  1328. * 3. MoxaDriverPoll(void); *
  1329. *****************************************************************************/
  1330. void MoxaDriverInit(void)
  1331. {
  1332. int i;
  1333. moxaFuncTout = HZ / 2; /* 500 mini-seconds */
  1334. moxaCard = 0;
  1335. moxaLog.tick = 0;
  1336. moxaLowWaterChk = 0;
  1337. for (i = 0; i < MAX_PORTS; i++) {
  1338. moxaChkPort[i] = 0;
  1339. moxaLowChkFlag[i] = 0;
  1340. moxaLineCtrl[i] = 0;
  1341. moxaLog.rxcnt[i] = 0;
  1342. moxaLog.txcnt[i] = 0;
  1343. }
  1344. }
  1345. #define MOXA 0x400
  1346. #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
  1347. #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
  1348. #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
  1349. #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
  1350. #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
  1351. #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
  1352. #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
  1353. #define MOXA_GETDATACOUNT (MOXA + 23)
  1354. #define MOXA_GET_IOQUEUE (MOXA + 27)
  1355. #define MOXA_FLUSH_QUEUE (MOXA + 28)
  1356. #define MOXA_GET_CONF (MOXA + 35) /* configuration */
  1357. #define MOXA_GET_MAJOR (MOXA + 63)
  1358. #define MOXA_GET_CUMAJOR (MOXA + 64)
  1359. #define MOXA_GETMSTATUS (MOXA + 65)
  1360. struct moxaq_str {
  1361. int inq;
  1362. int outq;
  1363. };
  1364. struct dl_str {
  1365. char __user *buf;
  1366. int len;
  1367. int cardno;
  1368. };
  1369. static struct moxaq_str temp_queue[MAX_PORTS];
  1370. static struct dl_str dltmp;
  1371. void MoxaPortFlushData(int port, int mode)
  1372. {
  1373. void __iomem *ofsAddr;
  1374. if ((mode < 0) || (mode > 2))
  1375. return;
  1376. ofsAddr = moxaTableAddr[port];
  1377. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1378. if (mode != 1) {
  1379. moxaLowChkFlag[port] = 0;
  1380. low_water_check(ofsAddr);
  1381. }
  1382. }
  1383. int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
  1384. {
  1385. int i;
  1386. int status;
  1387. int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
  1388. void __user *argp = (void __user *)arg;
  1389. if (port == QueryPort) {
  1390. if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
  1391. (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
  1392. (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
  1393. (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
  1394. (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
  1395. return (-EINVAL);
  1396. }
  1397. switch (cmd) {
  1398. case MOXA_GET_CONF:
  1399. if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
  1400. return -EFAULT;
  1401. return (0);
  1402. case MOXA_INIT_DRIVER:
  1403. if ((int) arg == 0x404)
  1404. MoxaDriverInit();
  1405. return (0);
  1406. case MOXA_GETDATACOUNT:
  1407. moxaLog.tick = jiffies;
  1408. if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
  1409. return -EFAULT;
  1410. return (0);
  1411. case MOXA_FLUSH_QUEUE:
  1412. MoxaPortFlushData(port, arg);
  1413. return (0);
  1414. case MOXA_GET_IOQUEUE:
  1415. for (i = 0; i < MAX_PORTS; i++) {
  1416. if (moxaChkPort[i]) {
  1417. temp_queue[i].inq = MoxaPortRxQueue(i);
  1418. temp_queue[i].outq = MoxaPortTxQueue(i);
  1419. }
  1420. }
  1421. if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
  1422. return -EFAULT;
  1423. return (0);
  1424. case MOXA_GET_OQUEUE:
  1425. i = MoxaPortTxQueue(port);
  1426. return put_user(i, (unsigned long __user *)argp);
  1427. case MOXA_GET_IQUEUE:
  1428. i = MoxaPortRxQueue(port);
  1429. return put_user(i, (unsigned long __user *)argp);
  1430. case MOXA_GET_MAJOR:
  1431. if(copy_to_user(argp, &ttymajor, sizeof(int)))
  1432. return -EFAULT;
  1433. return 0;
  1434. case MOXA_GET_CUMAJOR:
  1435. i = 0;
  1436. if(copy_to_user(argp, &i, sizeof(int)))
  1437. return -EFAULT;
  1438. return 0;
  1439. case MOXA_GETMSTATUS:
  1440. for (i = 0; i < MAX_PORTS; i++) {
  1441. GMStatus[i].ri = 0;
  1442. GMStatus[i].dcd = 0;
  1443. GMStatus[i].dsr = 0;
  1444. GMStatus[i].cts = 0;
  1445. if (!moxaChkPort[i]) {
  1446. continue;
  1447. } else {
  1448. status = MoxaPortLineStatus(moxaChannels[i].port);
  1449. if (status & 1)
  1450. GMStatus[i].cts = 1;
  1451. if (status & 2)
  1452. GMStatus[i].dsr = 1;
  1453. if (status & 4)
  1454. GMStatus[i].dcd = 1;
  1455. }
  1456. if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
  1457. GMStatus[i].cflag = moxaChannels[i].cflag;
  1458. else
  1459. GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
  1460. }
  1461. if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
  1462. return -EFAULT;
  1463. return 0;
  1464. default:
  1465. return (-ENOIOCTLCMD);
  1466. case MOXA_LOAD_BIOS:
  1467. case MOXA_FIND_BOARD:
  1468. case MOXA_LOAD_C320B:
  1469. case MOXA_LOAD_CODE:
  1470. if (!capable(CAP_SYS_RAWIO))
  1471. return -EPERM;
  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