moxa.c 75 KB

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