moxa.c 75 KB

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