moxa.c 84 KB

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