moxa.c 83 KB

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