moxa.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234
  1. /*****************************************************************************/
  2. /*
  3. * moxa.c -- MOXA Intellio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
  6. *
  7. * This code is loosely based on the Linux serial driver, written by
  8. * Linus Torvalds, Theodore T'so and others.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. /*
  25. * MOXA Intellio Series Driver
  26. * for : LINUX
  27. * date : 1999/1/7
  28. * version : 5.1
  29. */
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/mm.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/signal.h>
  36. #include <linux/sched.h>
  37. #include <linux/timer.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/tty.h>
  40. #include <linux/tty_flip.h>
  41. #include <linux/major.h>
  42. #include <linux/string.h>
  43. #include <linux/fcntl.h>
  44. #include <linux/ptrace.h>
  45. #include <linux/serial.h>
  46. #include <linux/tty_driver.h>
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/init.h>
  50. #include <linux/bitops.h>
  51. #include <asm/system.h>
  52. #include <asm/io.h>
  53. #include <asm/uaccess.h>
  54. #define MOXA_VERSION "5.1k"
  55. #define MOXAMAJOR 172
  56. #define MOXACUMAJOR 173
  57. #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
  58. #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
  59. #define MAX_BOARDS 4 /* Don't change this value */
  60. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  61. #define MAX_PORTS 128 /* Don't change this value */
  62. /*
  63. * Define the Moxa PCI vendor and device IDs.
  64. */
  65. #define MOXA_BUS_TYPE_ISA 0
  66. #define MOXA_BUS_TYPE_PCI 1
  67. #ifndef PCI_VENDOR_ID_MOXA
  68. #define PCI_VENDOR_ID_MOXA 0x1393
  69. #endif
  70. #ifndef PCI_DEVICE_ID_CP204J
  71. #define PCI_DEVICE_ID_CP204J 0x2040
  72. #endif
  73. #ifndef PCI_DEVICE_ID_C218
  74. #define PCI_DEVICE_ID_C218 0x2180
  75. #endif
  76. #ifndef PCI_DEVICE_ID_C320
  77. #define PCI_DEVICE_ID_C320 0x3200
  78. #endif
  79. enum {
  80. MOXA_BOARD_C218_PCI = 1,
  81. MOXA_BOARD_C218_ISA,
  82. MOXA_BOARD_C320_PCI,
  83. MOXA_BOARD_C320_ISA,
  84. MOXA_BOARD_CP204J,
  85. };
  86. static char *moxa_brdname[] =
  87. {
  88. "C218 Turbo PCI series",
  89. "C218 Turbo ISA series",
  90. "C320 Turbo PCI series",
  91. "C320 Turbo ISA series",
  92. "CP-204J series",
  93. };
  94. #ifdef CONFIG_PCI
  95. static struct pci_device_id moxa_pcibrds[] = {
  96. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
  97. 0, 0, MOXA_BOARD_C218_PCI },
  98. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
  99. 0, 0, MOXA_BOARD_C320_PCI },
  100. { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
  101. 0, 0, MOXA_BOARD_CP204J },
  102. { 0 }
  103. };
  104. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  105. #endif /* CONFIG_PCI */
  106. typedef struct _moxa_isa_board_conf {
  107. int boardType;
  108. int numPorts;
  109. unsigned long baseAddr;
  110. } moxa_isa_board_conf;
  111. static moxa_isa_board_conf moxa_isa_boards[] =
  112. {
  113. /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
  114. };
  115. typedef struct _moxa_pci_devinfo {
  116. ushort busNum;
  117. ushort devNum;
  118. } moxa_pci_devinfo;
  119. typedef struct _moxa_board_conf {
  120. int boardType;
  121. int numPorts;
  122. unsigned long baseAddr;
  123. int busType;
  124. moxa_pci_devinfo pciInfo;
  125. } moxa_board_conf;
  126. static moxa_board_conf moxa_boards[MAX_BOARDS];
  127. static void __iomem *moxaBaseAddr[MAX_BOARDS];
  128. static int loadstat[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 (loadstat[card] == 0)
  1508. continue;
  1509. if ((ports = moxa_boards[card].numPorts) == 0)
  1510. continue;
  1511. if (readb(moxaIntPend[card]) == 0xff) {
  1512. ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
  1513. p = card * MAX_PORTS_PER_BOARD;
  1514. ports <<= 1;
  1515. for (port = 0; port < ports; port += 2, p++) {
  1516. if ((temp = readw(ip + port)) != 0) {
  1517. writew(0, ip + port);
  1518. ofsAddr = moxaTableAddr[p];
  1519. if (temp & IntrTx)
  1520. writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
  1521. if (temp & IntrBreak) {
  1522. moxaBreakCnt[p]++;
  1523. }
  1524. if (temp & IntrLine) {
  1525. if (readb(ofsAddr + FlagStat) & DCD_state) {
  1526. if ((moxaDCDState[p] & DCD_oldstate) == 0)
  1527. moxaDCDState[p] = (DCD_oldstate |
  1528. DCD_changed);
  1529. } else {
  1530. if (moxaDCDState[p] & DCD_oldstate)
  1531. moxaDCDState[p] = DCD_changed;
  1532. }
  1533. }
  1534. }
  1535. }
  1536. writeb(0, moxaIntPend[card]);
  1537. }
  1538. if (moxaLowWaterChk) {
  1539. p = card * MAX_PORTS_PER_BOARD;
  1540. for (port = 0; port < ports; port++, p++) {
  1541. if (moxaLowChkFlag[p]) {
  1542. moxaLowChkFlag[p] = 0;
  1543. ofsAddr = moxaTableAddr[p];
  1544. low_water_check(ofsAddr);
  1545. }
  1546. }
  1547. }
  1548. }
  1549. moxaLowWaterChk = 0;
  1550. return (0);
  1551. }
  1552. /*****************************************************************************
  1553. * Card level function: *
  1554. * 1. MoxaPortsOfCard(int cardno); *
  1555. *****************************************************************************/
  1556. int MoxaPortsOfCard(int cardno)
  1557. {
  1558. if (moxa_boards[cardno].boardType == 0)
  1559. return (0);
  1560. return (moxa_boards[cardno].numPorts);
  1561. }
  1562. /*****************************************************************************
  1563. * Port level functions: *
  1564. * 1. MoxaPortIsValid(int port); *
  1565. * 2. MoxaPortEnable(int port); *
  1566. * 3. MoxaPortDisable(int port); *
  1567. * 4. MoxaPortGetMaxBaud(int port); *
  1568. * 5. MoxaPortGetCurBaud(int port); *
  1569. * 6. MoxaPortSetBaud(int port, long baud); *
  1570. * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
  1571. * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
  1572. * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
  1573. * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
  1574. * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
  1575. * 12. MoxaPortLineStatus(int port); *
  1576. * 13. MoxaPortDCDChange(int port); *
  1577. * 14. MoxaPortDCDON(int port); *
  1578. * 15. MoxaPortFlushData(int port, int mode); *
  1579. * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
  1580. * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
  1581. * 18. MoxaPortTxBufSize(int port); *
  1582. * 19. MoxaPortRxBufSize(int port); *
  1583. * 20. MoxaPortTxQueue(int port); *
  1584. * 21. MoxaPortTxFree(int port); *
  1585. * 22. MoxaPortRxQueue(int port); *
  1586. * 23. MoxaPortRxFree(int port); *
  1587. * 24. MoxaPortTxDisable(int port); *
  1588. * 25. MoxaPortTxEnable(int port); *
  1589. * 26. MoxaPortGetBrkCnt(int port); *
  1590. * 27. MoxaPortResetBrkCnt(int port); *
  1591. * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
  1592. * 29. MoxaPortIsTxHold(int port); *
  1593. * 30. MoxaPortSendBreak(int port, int ticks); *
  1594. *****************************************************************************/
  1595. /*
  1596. * Moxa Port Number Description:
  1597. *
  1598. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1599. * the port number using in MOXA driver functions will be 0 to 31 for
  1600. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1601. * to 127 for fourth. For example, if you setup three MOXA boards,
  1602. * first board is C218, second board is C320-16 and third board is
  1603. * C320-32. The port number of first board (C218 - 8 ports) is from
  1604. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1605. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1606. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1607. * 127 will be invalid.
  1608. *
  1609. *
  1610. * Moxa Functions Description:
  1611. *
  1612. * Function 1: Driver initialization routine, this routine must be
  1613. * called when initialized driver.
  1614. * Syntax:
  1615. * void MoxaDriverInit();
  1616. *
  1617. *
  1618. * Function 2: Moxa driver private IOCTL command processing.
  1619. * Syntax:
  1620. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1621. *
  1622. * unsigned int cmd : IOCTL command
  1623. * unsigned long arg : IOCTL argument
  1624. * int port : port number (0 - 127)
  1625. *
  1626. * return: 0 (OK)
  1627. * -EINVAL
  1628. * -ENOIOCTLCMD
  1629. *
  1630. *
  1631. * Function 3: Moxa driver polling process routine.
  1632. * Syntax:
  1633. * int MoxaDriverPoll(void);
  1634. *
  1635. * return: 0 ; polling O.K.
  1636. * -1 : no any Moxa card.
  1637. *
  1638. *
  1639. * Function 4: Get the ports of this card.
  1640. * Syntax:
  1641. * int MoxaPortsOfCard(int cardno);
  1642. *
  1643. * int cardno : card number (0 - 3)
  1644. *
  1645. * return: 0 : this card is invalid
  1646. * 8/16/24/32
  1647. *
  1648. *
  1649. * Function 5: Check this port is valid or invalid
  1650. * Syntax:
  1651. * int MoxaPortIsValid(int port);
  1652. * int port : port number (0 - 127, ref port description)
  1653. *
  1654. * return: 0 : this port is invalid
  1655. * 1 : this port is valid
  1656. *
  1657. *
  1658. * Function 6: Enable this port to start Tx/Rx data.
  1659. * Syntax:
  1660. * void MoxaPortEnable(int port);
  1661. * int port : port number (0 - 127)
  1662. *
  1663. *
  1664. * Function 7: Disable this port
  1665. * Syntax:
  1666. * void MoxaPortDisable(int port);
  1667. * int port : port number (0 - 127)
  1668. *
  1669. *
  1670. * Function 8: Get the maximun available baud rate of this port.
  1671. * Syntax:
  1672. * long MoxaPortGetMaxBaud(int port);
  1673. * int port : port number (0 - 127)
  1674. *
  1675. * return: 0 : this port is invalid
  1676. * 38400/57600/115200 bps
  1677. *
  1678. *
  1679. * Function 9: Get the current baud rate of this port.
  1680. * Syntax:
  1681. * long MoxaPortGetCurBaud(int port);
  1682. * int port : port number (0 - 127)
  1683. *
  1684. * return: 0 : this port is invalid
  1685. * 50 - 115200 bps
  1686. *
  1687. *
  1688. * Function 10: Setting baud rate of this port.
  1689. * Syntax:
  1690. * long MoxaPortSetBaud(int port, long baud);
  1691. * int port : port number (0 - 127)
  1692. * long baud : baud rate (50 - 115200)
  1693. *
  1694. * return: 0 : this port is invalid or baud < 50
  1695. * 50 - 115200 : the real baud rate set to the port, if
  1696. * the argument baud is large than maximun
  1697. * available baud rate, the real setting
  1698. * baud rate will be the maximun baud rate.
  1699. *
  1700. *
  1701. * Function 11: Setting the data-bits/stop-bits/parity of this port
  1702. * Syntax:
  1703. * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
  1704. * int port : port number (0 - 127)
  1705. * int databits : data bits (8/7/6/5)
  1706. * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
  1707. int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
  1708. *
  1709. * return: -1 : invalid parameter
  1710. * 0 : setting O.K.
  1711. *
  1712. *
  1713. * Function 12: Configure the port.
  1714. * Syntax:
  1715. * int MoxaPortSetTermio(int port, struct termios *termio);
  1716. * int port : port number (0 - 127)
  1717. * struct termios * termio : termio structure pointer
  1718. *
  1719. * return: -1 : this port is invalid or termio == NULL
  1720. * 0 : setting O.K.
  1721. *
  1722. *
  1723. * Function 13: Get the DTR/RTS state of this port.
  1724. * Syntax:
  1725. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1726. * int port : port number (0 - 127)
  1727. * int * dtrState : pointer to INT to receive the current DTR
  1728. * state. (if NULL, this function will not
  1729. * write to this address)
  1730. * int * rtsState : pointer to INT to receive the current RTS
  1731. * state. (if NULL, this function will not
  1732. * write to this address)
  1733. *
  1734. * return: -1 : this port is invalid
  1735. * 0 : O.K.
  1736. *
  1737. *
  1738. * Function 14: Setting the DTR/RTS output state of this port.
  1739. * Syntax:
  1740. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1741. * int port : port number (0 - 127)
  1742. * int dtrState : DTR output state (0: off, 1: on)
  1743. * int rtsState : RTS output state (0: off, 1: on)
  1744. *
  1745. *
  1746. * Function 15: Setting the flow control of this port.
  1747. * Syntax:
  1748. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1749. * int txFlow,int xany);
  1750. * int port : port number (0 - 127)
  1751. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1752. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1753. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1754. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1755. * int xany : S/W XANY flow control (0: no, 1: yes)
  1756. *
  1757. *
  1758. * Function 16: Get ths line status of this port
  1759. * Syntax:
  1760. * int MoxaPortLineStatus(int port);
  1761. * int port : port number (0 - 127)
  1762. *
  1763. * return: Bit 0 - CTS state (0: off, 1: on)
  1764. * Bit 1 - DSR state (0: off, 1: on)
  1765. * Bit 2 - DCD state (0: off, 1: on)
  1766. *
  1767. *
  1768. * Function 17: Check the DCD state has changed since the last read
  1769. * of this function.
  1770. * Syntax:
  1771. * int MoxaPortDCDChange(int port);
  1772. * int port : port number (0 - 127)
  1773. *
  1774. * return: 0 : no changed
  1775. * 1 : DCD has changed
  1776. *
  1777. *
  1778. * Function 18: Check ths current DCD state is ON or not.
  1779. * Syntax:
  1780. * int MoxaPortDCDON(int port);
  1781. * int port : port number (0 - 127)
  1782. *
  1783. * return: 0 : DCD off
  1784. * 1 : DCD on
  1785. *
  1786. *
  1787. * Function 19: Flush the Rx/Tx buffer data of this port.
  1788. * Syntax:
  1789. * void MoxaPortFlushData(int port, int mode);
  1790. * int port : port number (0 - 127)
  1791. * int mode
  1792. * 0 : flush the Rx buffer
  1793. * 1 : flush the Tx buffer
  1794. * 2 : flush the Rx and Tx buffer
  1795. *
  1796. *
  1797. * Function 20: Write data.
  1798. * Syntax:
  1799. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1800. * int port : port number (0 - 127)
  1801. * unsigned char * buffer : pointer to write data buffer.
  1802. * int length : write data length
  1803. *
  1804. * return: 0 - length : real write data length
  1805. *
  1806. *
  1807. * Function 21: Read data.
  1808. * Syntax:
  1809. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1810. * int port : port number (0 - 127)
  1811. * struct tty_struct *tty : tty for data
  1812. *
  1813. * return: 0 - length : real read data length
  1814. *
  1815. *
  1816. * Function 22: Get the Tx buffer size of this port
  1817. * Syntax:
  1818. * int MoxaPortTxBufSize(int port);
  1819. * int port : port number (0 - 127)
  1820. *
  1821. * return: .. : Tx buffer size
  1822. *
  1823. *
  1824. * Function 23: Get the Rx buffer size of this port
  1825. * Syntax:
  1826. * int MoxaPortRxBufSize(int port);
  1827. * int port : port number (0 - 127)
  1828. *
  1829. * return: .. : Rx buffer size
  1830. *
  1831. *
  1832. * Function 24: Get the Tx buffer current queued data bytes
  1833. * Syntax:
  1834. * int MoxaPortTxQueue(int port);
  1835. * int port : port number (0 - 127)
  1836. *
  1837. * return: .. : Tx buffer current queued data bytes
  1838. *
  1839. *
  1840. * Function 25: Get the Tx buffer current free space
  1841. * Syntax:
  1842. * int MoxaPortTxFree(int port);
  1843. * int port : port number (0 - 127)
  1844. *
  1845. * return: .. : Tx buffer current free space
  1846. *
  1847. *
  1848. * Function 26: Get the Rx buffer current queued data bytes
  1849. * Syntax:
  1850. * int MoxaPortRxQueue(int port);
  1851. * int port : port number (0 - 127)
  1852. *
  1853. * return: .. : Rx buffer current queued data bytes
  1854. *
  1855. *
  1856. * Function 27: Get the Rx buffer current free space
  1857. * Syntax:
  1858. * int MoxaPortRxFree(int port);
  1859. * int port : port number (0 - 127)
  1860. *
  1861. * return: .. : Rx buffer current free space
  1862. *
  1863. *
  1864. * Function 28: Disable port data transmission.
  1865. * Syntax:
  1866. * void MoxaPortTxDisable(int port);
  1867. * int port : port number (0 - 127)
  1868. *
  1869. *
  1870. * Function 29: Enable port data transmission.
  1871. * Syntax:
  1872. * void MoxaPortTxEnable(int port);
  1873. * int port : port number (0 - 127)
  1874. *
  1875. *
  1876. * Function 30: Get the received BREAK signal count.
  1877. * Syntax:
  1878. * int MoxaPortGetBrkCnt(int port);
  1879. * int port : port number (0 - 127)
  1880. *
  1881. * return: 0 - .. : BREAK signal count
  1882. *
  1883. *
  1884. * Function 31: Get the received BREAK signal count and reset it.
  1885. * Syntax:
  1886. * int MoxaPortResetBrkCnt(int port);
  1887. * int port : port number (0 - 127)
  1888. *
  1889. * return: 0 - .. : BREAK signal count
  1890. *
  1891. *
  1892. * Function 32: Set the S/W flow control new XON/XOFF value, default
  1893. * XON is 0x11 & XOFF is 0x13.
  1894. * Syntax:
  1895. * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
  1896. * int port : port number (0 - 127)
  1897. * int xonValue : new XON value (0 - 255)
  1898. * int xoffValue : new XOFF value (0 - 255)
  1899. *
  1900. *
  1901. * Function 33: Check this port's transmission is hold by remote site
  1902. * because the flow control.
  1903. * Syntax:
  1904. * int MoxaPortIsTxHold(int port);
  1905. * int port : port number (0 - 127)
  1906. *
  1907. * return: 0 : normal
  1908. * 1 : hold by remote site
  1909. *
  1910. *
  1911. * Function 34: Send out a BREAK signal.
  1912. * Syntax:
  1913. * void MoxaPortSendBreak(int port, int ms100);
  1914. * int port : port number (0 - 127)
  1915. * int ms100 : break signal time interval.
  1916. * unit: 100 mini-second. if ms100 == 0, it will
  1917. * send out a about 250 ms BREAK signal.
  1918. *
  1919. */
  1920. int MoxaPortIsValid(int port)
  1921. {
  1922. if (moxaCard == 0)
  1923. return (0);
  1924. if (moxaChkPort[port] == 0)
  1925. return (0);
  1926. return (1);
  1927. }
  1928. void MoxaPortEnable(int port)
  1929. {
  1930. void __iomem *ofsAddr;
  1931. int MoxaPortLineStatus(int);
  1932. short lowwater = 512;
  1933. ofsAddr = moxaTableAddr[port];
  1934. writew(lowwater, ofsAddr + Low_water);
  1935. moxaBreakCnt[port] = 0;
  1936. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1937. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1938. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1939. } else {
  1940. writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
  1941. }
  1942. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1943. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1944. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1945. MoxaPortLineStatus(port);
  1946. }
  1947. void MoxaPortDisable(int port)
  1948. {
  1949. void __iomem *ofsAddr = moxaTableAddr[port];
  1950. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1951. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1952. writew(0, ofsAddr + HostStat);
  1953. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1954. }
  1955. long MoxaPortGetMaxBaud(int port)
  1956. {
  1957. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1958. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
  1959. return (460800L);
  1960. else
  1961. return (921600L);
  1962. }
  1963. long MoxaPortSetBaud(int port, long baud)
  1964. {
  1965. void __iomem *ofsAddr;
  1966. long max, clock;
  1967. unsigned int val;
  1968. if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
  1969. return (0);
  1970. ofsAddr = moxaTableAddr[port];
  1971. if (baud > max)
  1972. baud = max;
  1973. if (max == 38400L)
  1974. clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
  1975. else if (max == 57600L)
  1976. clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
  1977. else
  1978. clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
  1979. val = clock / baud;
  1980. moxafunc(ofsAddr, FC_SetBaud, val);
  1981. baud = clock / val;
  1982. moxaCurBaud[port] = baud;
  1983. return (baud);
  1984. }
  1985. int MoxaPortSetTermio(int port, struct termios *termio)
  1986. {
  1987. void __iomem *ofsAddr;
  1988. tcflag_t cflag;
  1989. long baud;
  1990. tcflag_t mode = 0;
  1991. if (moxaChkPort[port] == 0 || termio == 0)
  1992. return (-1);
  1993. ofsAddr = moxaTableAddr[port];
  1994. cflag = termio->c_cflag; /* termio->c_cflag */
  1995. mode = termio->c_cflag & CSIZE;
  1996. if (mode == CS5)
  1997. mode = MX_CS5;
  1998. else if (mode == CS6)
  1999. mode = MX_CS6;
  2000. else if (mode == CS7)
  2001. mode = MX_CS7;
  2002. else if (mode == CS8)
  2003. mode = MX_CS8;
  2004. if (termio->c_cflag & CSTOPB) {
  2005. if (mode == MX_CS5)
  2006. mode |= MX_STOP15;
  2007. else
  2008. mode |= MX_STOP2;
  2009. } else
  2010. mode |= MX_STOP1;
  2011. if (termio->c_cflag & PARENB) {
  2012. if (termio->c_cflag & PARODD)
  2013. mode |= MX_PARODD;
  2014. else
  2015. mode |= MX_PAREVEN;
  2016. } else
  2017. mode |= MX_PARNONE;
  2018. moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
  2019. cflag &= (CBAUD | CBAUDEX);
  2020. #ifndef B921600
  2021. #define B921600 (B460800+1)
  2022. #endif
  2023. switch (cflag) {
  2024. case B921600:
  2025. baud = 921600L;
  2026. break;
  2027. case B460800:
  2028. baud = 460800L;
  2029. break;
  2030. case B230400:
  2031. baud = 230400L;
  2032. break;
  2033. case B115200:
  2034. baud = 115200L;
  2035. break;
  2036. case B57600:
  2037. baud = 57600L;
  2038. break;
  2039. case B38400:
  2040. baud = 38400L;
  2041. break;
  2042. case B19200:
  2043. baud = 19200L;
  2044. break;
  2045. case B9600:
  2046. baud = 9600L;
  2047. break;
  2048. case B4800:
  2049. baud = 4800L;
  2050. break;
  2051. case B2400:
  2052. baud = 2400L;
  2053. break;
  2054. case B1800:
  2055. baud = 1800L;
  2056. break;
  2057. case B1200:
  2058. baud = 1200L;
  2059. break;
  2060. case B600:
  2061. baud = 600L;
  2062. break;
  2063. case B300:
  2064. baud = 300L;
  2065. break;
  2066. case B200:
  2067. baud = 200L;
  2068. break;
  2069. case B150:
  2070. baud = 150L;
  2071. break;
  2072. case B134:
  2073. baud = 134L;
  2074. break;
  2075. case B110:
  2076. baud = 110L;
  2077. break;
  2078. case B75:
  2079. baud = 75L;
  2080. break;
  2081. case B50:
  2082. baud = 50L;
  2083. break;
  2084. default:
  2085. baud = 0;
  2086. }
  2087. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  2088. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  2089. if (baud == 921600L)
  2090. return (-1);
  2091. }
  2092. MoxaPortSetBaud(port, baud);
  2093. if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
  2094. writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
  2095. writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
  2096. writeb(FC_SetXonXoff, ofsAddr + FuncCode);
  2097. wait_finish(ofsAddr);
  2098. }
  2099. return (0);
  2100. }
  2101. int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
  2102. {
  2103. if (!MoxaPortIsValid(port))
  2104. return (-1);
  2105. if (dtrState) {
  2106. if (moxaLineCtrl[port] & DTR_ON)
  2107. *dtrState = 1;
  2108. else
  2109. *dtrState = 0;
  2110. }
  2111. if (rtsState) {
  2112. if (moxaLineCtrl[port] & RTS_ON)
  2113. *rtsState = 1;
  2114. else
  2115. *rtsState = 0;
  2116. }
  2117. return (0);
  2118. }
  2119. void MoxaPortLineCtrl(int port, int dtr, int rts)
  2120. {
  2121. void __iomem *ofsAddr;
  2122. int mode;
  2123. ofsAddr = moxaTableAddr[port];
  2124. mode = 0;
  2125. if (dtr)
  2126. mode |= DTR_ON;
  2127. if (rts)
  2128. mode |= RTS_ON;
  2129. moxaLineCtrl[port] = mode;
  2130. moxafunc(ofsAddr, FC_LineControl, mode);
  2131. }
  2132. void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
  2133. {
  2134. void __iomem *ofsAddr;
  2135. int mode;
  2136. ofsAddr = moxaTableAddr[port];
  2137. mode = 0;
  2138. if (rts)
  2139. mode |= RTS_FlowCtl;
  2140. if (cts)
  2141. mode |= CTS_FlowCtl;
  2142. if (txflow)
  2143. mode |= Tx_FlowCtl;
  2144. if (rxflow)
  2145. mode |= Rx_FlowCtl;
  2146. if (txany)
  2147. mode |= IXM_IXANY;
  2148. moxafunc(ofsAddr, FC_SetFlowCtl, mode);
  2149. }
  2150. int MoxaPortLineStatus(int port)
  2151. {
  2152. void __iomem *ofsAddr;
  2153. int val;
  2154. ofsAddr = moxaTableAddr[port];
  2155. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  2156. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  2157. moxafunc(ofsAddr, FC_LineStatus, 0);
  2158. val = readw(ofsAddr + FuncArg);
  2159. } else {
  2160. val = readw(ofsAddr + FlagStat) >> 4;
  2161. }
  2162. val &= 0x0B;
  2163. if (val & 8) {
  2164. val |= 4;
  2165. if ((moxaDCDState[port] & DCD_oldstate) == 0)
  2166. moxaDCDState[port] = (DCD_oldstate | DCD_changed);
  2167. } else {
  2168. if (moxaDCDState[port] & DCD_oldstate)
  2169. moxaDCDState[port] = DCD_changed;
  2170. }
  2171. val &= 7;
  2172. return (val);
  2173. }
  2174. int MoxaPortDCDChange(int port)
  2175. {
  2176. int n;
  2177. if (moxaChkPort[port] == 0)
  2178. return (0);
  2179. n = moxaDCDState[port];
  2180. moxaDCDState[port] &= ~DCD_changed;
  2181. n &= DCD_changed;
  2182. return (n);
  2183. }
  2184. int MoxaPortDCDON(int port)
  2185. {
  2186. int n;
  2187. if (moxaChkPort[port] == 0)
  2188. return (0);
  2189. if (moxaDCDState[port] & DCD_oldstate)
  2190. n = 1;
  2191. else
  2192. n = 0;
  2193. return (n);
  2194. }
  2195. /*
  2196. int MoxaDumpMem(int port, unsigned char * buffer, int len)
  2197. {
  2198. int i;
  2199. unsigned long baseAddr,ofsAddr,ofs;
  2200. baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
  2201. ofs = baseAddr + DynPage_addr + pageofs;
  2202. if (len > 0x2000L)
  2203. len = 0x2000L;
  2204. for (i = 0; i < len; i++)
  2205. buffer[i] = readb(ofs+i);
  2206. }
  2207. */
  2208. int MoxaPortWriteData(int port, unsigned char * buffer, int len)
  2209. {
  2210. int c, total, i;
  2211. ushort tail;
  2212. int cnt;
  2213. ushort head, tx_mask, spage, epage;
  2214. ushort pageno, pageofs, bufhead;
  2215. void __iomem *baseAddr, *ofsAddr, *ofs;
  2216. ofsAddr = moxaTableAddr[port];
  2217. baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
  2218. tx_mask = readw(ofsAddr + TX_mask);
  2219. spage = readw(ofsAddr + Page_txb);
  2220. epage = readw(ofsAddr + EndPage_txb);
  2221. tail = readw(ofsAddr + TXwptr);
  2222. head = readw(ofsAddr + TXrptr);
  2223. c = (head > tail) ? (head - tail - 1)
  2224. : (head - tail + tx_mask);
  2225. if (c > len)
  2226. c = len;
  2227. moxaLog.txcnt[port] += c;
  2228. total = c;
  2229. if (spage == epage) {
  2230. bufhead = readw(ofsAddr + Ofs_txb);
  2231. writew(spage, baseAddr + Control_reg);
  2232. while (c > 0) {
  2233. if (head > tail)
  2234. len = head - tail - 1;
  2235. else
  2236. len = tx_mask + 1 - tail;
  2237. len = (c > len) ? len : c;
  2238. ofs = baseAddr + DynPage_addr + bufhead + tail;
  2239. for (i = 0; i < len; i++)
  2240. writeb(*buffer++, ofs + i);
  2241. tail = (tail + len) & tx_mask;
  2242. c -= len;
  2243. }
  2244. writew(tail, ofsAddr + TXwptr);
  2245. } else {
  2246. len = c;
  2247. pageno = spage + (tail >> 13);
  2248. pageofs = tail & Page_mask;
  2249. do {
  2250. cnt = Page_size - pageofs;
  2251. if (cnt > c)
  2252. cnt = c;
  2253. c -= cnt;
  2254. writeb(pageno, baseAddr + Control_reg);
  2255. ofs = baseAddr + DynPage_addr + pageofs;
  2256. for (i = 0; i < cnt; i++)
  2257. writeb(*buffer++, ofs + i);
  2258. if (c == 0) {
  2259. writew((tail + len) & tx_mask, ofsAddr + TXwptr);
  2260. break;
  2261. }
  2262. if (++pageno == epage)
  2263. pageno = spage;
  2264. pageofs = 0;
  2265. } while (1);
  2266. }
  2267. writeb(1, ofsAddr + CD180TXirq); /* start to send */
  2268. return (total);
  2269. }
  2270. int MoxaPortReadData(int port, struct tty_struct *tty)
  2271. {
  2272. register ushort head, pageofs;
  2273. int i, count, cnt, len, total, remain;
  2274. ushort tail, rx_mask, spage, epage;
  2275. ushort pageno, bufhead;
  2276. void __iomem *baseAddr, *ofsAddr, *ofs;
  2277. ofsAddr = moxaTableAddr[port];
  2278. baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
  2279. head = readw(ofsAddr + RXrptr);
  2280. tail = readw(ofsAddr + RXwptr);
  2281. rx_mask = readw(ofsAddr + RX_mask);
  2282. spage = readw(ofsAddr + Page_rxb);
  2283. epage = readw(ofsAddr + EndPage_rxb);
  2284. count = (tail >= head) ? (tail - head)
  2285. : (tail - head + rx_mask + 1);
  2286. if (count == 0)
  2287. return 0;
  2288. total = count;
  2289. remain = count - total;
  2290. moxaLog.rxcnt[port] += total;
  2291. count = total;
  2292. if (spage == epage) {
  2293. bufhead = readw(ofsAddr + Ofs_rxb);
  2294. writew(spage, baseAddr + Control_reg);
  2295. while (count > 0) {
  2296. if (tail >= head)
  2297. len = tail - head;
  2298. else
  2299. len = rx_mask + 1 - head;
  2300. len = (count > len) ? len : count;
  2301. ofs = baseAddr + DynPage_addr + bufhead + head;
  2302. for (i = 0; i < len; i++)
  2303. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2304. head = (head + len) & rx_mask;
  2305. count -= len;
  2306. }
  2307. writew(head, ofsAddr + RXrptr);
  2308. } else {
  2309. len = count;
  2310. pageno = spage + (head >> 13);
  2311. pageofs = head & Page_mask;
  2312. do {
  2313. cnt = Page_size - pageofs;
  2314. if (cnt > count)
  2315. cnt = count;
  2316. count -= cnt;
  2317. writew(pageno, baseAddr + Control_reg);
  2318. ofs = baseAddr + DynPage_addr + pageofs;
  2319. for (i = 0; i < cnt; i++)
  2320. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2321. if (count == 0) {
  2322. writew((head + len) & rx_mask, ofsAddr + RXrptr);
  2323. break;
  2324. }
  2325. if (++pageno == epage)
  2326. pageno = spage;
  2327. pageofs = 0;
  2328. } while (1);
  2329. }
  2330. if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
  2331. moxaLowWaterChk = 1;
  2332. moxaLowChkFlag[port] = 1;
  2333. }
  2334. return (total);
  2335. }
  2336. int MoxaPortTxQueue(int port)
  2337. {
  2338. void __iomem *ofsAddr;
  2339. ushort rptr, wptr, mask;
  2340. int len;
  2341. ofsAddr = moxaTableAddr[port];
  2342. rptr = readw(ofsAddr + TXrptr);
  2343. wptr = readw(ofsAddr + TXwptr);
  2344. mask = readw(ofsAddr + TX_mask);
  2345. len = (wptr - rptr) & mask;
  2346. return (len);
  2347. }
  2348. int MoxaPortTxFree(int port)
  2349. {
  2350. void __iomem *ofsAddr;
  2351. ushort rptr, wptr, mask;
  2352. int len;
  2353. ofsAddr = moxaTableAddr[port];
  2354. rptr = readw(ofsAddr + TXrptr);
  2355. wptr = readw(ofsAddr + TXwptr);
  2356. mask = readw(ofsAddr + TX_mask);
  2357. len = mask - ((wptr - rptr) & mask);
  2358. return (len);
  2359. }
  2360. int MoxaPortRxQueue(int port)
  2361. {
  2362. void __iomem *ofsAddr;
  2363. ushort rptr, wptr, mask;
  2364. int len;
  2365. ofsAddr = moxaTableAddr[port];
  2366. rptr = readw(ofsAddr + RXrptr);
  2367. wptr = readw(ofsAddr + RXwptr);
  2368. mask = readw(ofsAddr + RX_mask);
  2369. len = (wptr - rptr) & mask;
  2370. return (len);
  2371. }
  2372. void MoxaPortTxDisable(int port)
  2373. {
  2374. void __iomem *ofsAddr;
  2375. ofsAddr = moxaTableAddr[port];
  2376. moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
  2377. }
  2378. void MoxaPortTxEnable(int port)
  2379. {
  2380. void __iomem *ofsAddr;
  2381. ofsAddr = moxaTableAddr[port];
  2382. moxafunc(ofsAddr, FC_SetXonState, Magic_code);
  2383. }
  2384. int MoxaPortResetBrkCnt(int port)
  2385. {
  2386. ushort cnt;
  2387. cnt = moxaBreakCnt[port];
  2388. moxaBreakCnt[port] = 0;
  2389. return (cnt);
  2390. }
  2391. void MoxaPortSendBreak(int port, int ms100)
  2392. {
  2393. void __iomem *ofsAddr;
  2394. ofsAddr = moxaTableAddr[port];
  2395. if (ms100) {
  2396. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2397. moxadelay(ms100 * (HZ / 10));
  2398. } else {
  2399. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2400. moxadelay(HZ / 4); /* 250 ms */
  2401. }
  2402. moxafunc(ofsAddr, FC_StopBreak, Magic_code);
  2403. }
  2404. static int moxa_get_serial_info(struct moxa_str *info,
  2405. struct serial_struct __user *retinfo)
  2406. {
  2407. struct serial_struct tmp;
  2408. memset(&tmp, 0, sizeof(tmp));
  2409. tmp.type = info->type;
  2410. tmp.line = info->port;
  2411. tmp.port = 0;
  2412. tmp.irq = 0;
  2413. tmp.flags = info->asyncflags;
  2414. tmp.baud_base = 921600;
  2415. tmp.close_delay = info->close_delay;
  2416. tmp.closing_wait = info->closing_wait;
  2417. tmp.custom_divisor = 0;
  2418. tmp.hub6 = 0;
  2419. if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2420. return -EFAULT;
  2421. return (0);
  2422. }
  2423. static int moxa_set_serial_info(struct moxa_str *info,
  2424. struct serial_struct __user *new_info)
  2425. {
  2426. struct serial_struct new_serial;
  2427. if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2428. return -EFAULT;
  2429. if ((new_serial.irq != 0) ||
  2430. (new_serial.port != 0) ||
  2431. // (new_serial.type != info->type) ||
  2432. (new_serial.custom_divisor != 0) ||
  2433. (new_serial.baud_base != 921600))
  2434. return (-EPERM);
  2435. if (!capable(CAP_SYS_ADMIN)) {
  2436. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  2437. (info->asyncflags & ~ASYNC_USR_MASK)))
  2438. return (-EPERM);
  2439. } else {
  2440. info->close_delay = new_serial.close_delay * HZ / 100;
  2441. info->closing_wait = new_serial.closing_wait * HZ / 100;
  2442. }
  2443. new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
  2444. new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
  2445. if (new_serial.type == PORT_16550A) {
  2446. MoxaSetFifo(info->port, 1);
  2447. } else {
  2448. MoxaSetFifo(info->port, 0);
  2449. }
  2450. info->type = new_serial.type;
  2451. return (0);
  2452. }
  2453. /*****************************************************************************
  2454. * Static local functions: *
  2455. *****************************************************************************/
  2456. /*
  2457. * moxadelay - delays a specified number ticks
  2458. */
  2459. static void moxadelay(int tick)
  2460. {
  2461. unsigned long st, et;
  2462. st = jiffies;
  2463. et = st + tick;
  2464. while (time_before(jiffies, et));
  2465. }
  2466. static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
  2467. {
  2468. writew(arg, ofsAddr + FuncArg);
  2469. writew(cmd, ofsAddr + FuncCode);
  2470. wait_finish(ofsAddr);
  2471. }
  2472. static void wait_finish(void __iomem *ofsAddr)
  2473. {
  2474. unsigned long i, j;
  2475. i = jiffies;
  2476. while (readw(ofsAddr + FuncCode) != 0) {
  2477. j = jiffies;
  2478. if ((j - i) > moxaFuncTout) {
  2479. return;
  2480. }
  2481. }
  2482. }
  2483. static void low_water_check(void __iomem *ofsAddr)
  2484. {
  2485. int len;
  2486. ushort rptr, wptr, mask;
  2487. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  2488. rptr = readw(ofsAddr + RXrptr);
  2489. wptr = readw(ofsAddr + RXwptr);
  2490. mask = readw(ofsAddr + RX_mask);
  2491. len = (wptr - rptr) & mask;
  2492. if (len <= Low_water)
  2493. moxafunc(ofsAddr, FC_SendXon, 0);
  2494. }
  2495. }
  2496. static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
  2497. {
  2498. void __iomem *baseAddr;
  2499. int i;
  2500. if(copy_from_user(moxaBuff, tmp, len))
  2501. return -EFAULT;
  2502. baseAddr = moxaBaseAddr[cardno];
  2503. writeb(HW_reset, baseAddr + Control_reg); /* reset */
  2504. moxadelay(1); /* delay 10 ms */
  2505. for (i = 0; i < 4096; i++)
  2506. writeb(0, baseAddr + i); /* clear fix page */
  2507. for (i = 0; i < len; i++)
  2508. writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
  2509. writeb(0, baseAddr + Control_reg); /* restart */
  2510. return (0);
  2511. }
  2512. static int moxafindcard(int cardno)
  2513. {
  2514. void __iomem *baseAddr;
  2515. ushort tmp;
  2516. baseAddr = moxaBaseAddr[cardno];
  2517. switch (moxa_boards[cardno].boardType) {
  2518. case MOXA_BOARD_C218_ISA:
  2519. case MOXA_BOARD_C218_PCI:
  2520. if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
  2521. return (-1);
  2522. }
  2523. break;
  2524. case MOXA_BOARD_CP204J:
  2525. if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
  2526. return (-1);
  2527. }
  2528. break;
  2529. default:
  2530. if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
  2531. return (-1);
  2532. }
  2533. if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
  2534. return (-2);
  2535. }
  2536. }
  2537. return (0);
  2538. }
  2539. static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
  2540. {
  2541. void __iomem *baseAddr;
  2542. int i;
  2543. if(len > sizeof(moxaBuff))
  2544. return -EINVAL;
  2545. if(copy_from_user(moxaBuff, tmp, len))
  2546. return -EFAULT;
  2547. baseAddr = moxaBaseAddr[cardno];
  2548. writew(len - 7168 - 2, baseAddr + C320bapi_len);
  2549. writeb(1, baseAddr + Control_reg); /* Select Page 1 */
  2550. for (i = 0; i < 7168; i++)
  2551. writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
  2552. writeb(2, baseAddr + Control_reg); /* Select Page 2 */
  2553. for (i = 0; i < (len - 7168); i++)
  2554. writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
  2555. return (0);
  2556. }
  2557. static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
  2558. {
  2559. void __iomem *baseAddr, *ofsAddr;
  2560. int retval, port, i;
  2561. if(copy_from_user(moxaBuff, tmp, len))
  2562. return -EFAULT;
  2563. baseAddr = moxaBaseAddr[cardno];
  2564. switch (moxa_boards[cardno].boardType) {
  2565. case MOXA_BOARD_C218_ISA:
  2566. case MOXA_BOARD_C218_PCI:
  2567. case MOXA_BOARD_CP204J:
  2568. retval = moxaloadc218(cardno, baseAddr, len);
  2569. if (retval)
  2570. return (retval);
  2571. port = cardno * MAX_PORTS_PER_BOARD;
  2572. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2573. moxaChkPort[port] = 1;
  2574. moxaCurBaud[port] = 9600L;
  2575. moxaDCDState[port] = 0;
  2576. moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
  2577. ofsAddr = moxaTableAddr[port];
  2578. writew(C218rx_mask, ofsAddr + RX_mask);
  2579. writew(C218tx_mask, ofsAddr + TX_mask);
  2580. writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
  2581. writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
  2582. writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
  2583. writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
  2584. }
  2585. break;
  2586. default:
  2587. retval = moxaloadc320(cardno, baseAddr, len,
  2588. &moxa_boards[cardno].numPorts);
  2589. if (retval)
  2590. return (retval);
  2591. port = cardno * MAX_PORTS_PER_BOARD;
  2592. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2593. moxaChkPort[port] = 1;
  2594. moxaCurBaud[port] = 9600L;
  2595. moxaDCDState[port] = 0;
  2596. moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
  2597. ofsAddr = moxaTableAddr[port];
  2598. if (moxa_boards[cardno].numPorts == 8) {
  2599. writew(C320p8rx_mask, ofsAddr + RX_mask);
  2600. writew(C320p8tx_mask, ofsAddr + TX_mask);
  2601. writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
  2602. writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
  2603. writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
  2604. writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
  2605. } else if (moxa_boards[cardno].numPorts == 16) {
  2606. writew(C320p16rx_mask, ofsAddr + RX_mask);
  2607. writew(C320p16tx_mask, ofsAddr + TX_mask);
  2608. writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
  2609. writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
  2610. writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
  2611. writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
  2612. } else if (moxa_boards[cardno].numPorts == 24) {
  2613. writew(C320p24rx_mask, ofsAddr + RX_mask);
  2614. writew(C320p24tx_mask, ofsAddr + TX_mask);
  2615. writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
  2616. writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
  2617. writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
  2618. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2619. } else if (moxa_boards[cardno].numPorts == 32) {
  2620. writew(C320p32rx_mask, ofsAddr + RX_mask);
  2621. writew(C320p32tx_mask, ofsAddr + TX_mask);
  2622. writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
  2623. writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
  2624. writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
  2625. writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
  2626. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2627. }
  2628. }
  2629. break;
  2630. }
  2631. loadstat[cardno] = 1;
  2632. return (0);
  2633. }
  2634. static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
  2635. {
  2636. char retry;
  2637. int i, j, len1, len2;
  2638. ushort usum, *ptr, keycode;
  2639. if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
  2640. keycode = CP204J_KeyCode;
  2641. else
  2642. keycode = C218_KeyCode;
  2643. usum = 0;
  2644. len1 = len >> 1;
  2645. ptr = (ushort *) moxaBuff;
  2646. for (i = 0; i < len1; i++)
  2647. usum += le16_to_cpu(*(ptr + i));
  2648. retry = 0;
  2649. do {
  2650. len1 = len >> 1;
  2651. j = 0;
  2652. while (len1) {
  2653. len2 = (len1 > 2048) ? 2048 : len1;
  2654. len1 -= len2;
  2655. for (i = 0; i < len2 << 1; i++)
  2656. writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
  2657. j += i;
  2658. writew(len2, baseAddr + C218DLoad_len);
  2659. writew(0, baseAddr + C218_key);
  2660. for (i = 0; i < 100; i++) {
  2661. if (readw(baseAddr + C218_key) == keycode)
  2662. break;
  2663. moxadelay(1); /* delay 10 ms */
  2664. }
  2665. if (readw(baseAddr + C218_key) != keycode) {
  2666. return (-1);
  2667. }
  2668. }
  2669. writew(0, baseAddr + C218DLoad_len);
  2670. writew(usum, baseAddr + C218check_sum);
  2671. writew(0, baseAddr + C218_key);
  2672. for (i = 0; i < 100; i++) {
  2673. if (readw(baseAddr + C218_key) == keycode)
  2674. break;
  2675. moxadelay(1); /* delay 10 ms */
  2676. }
  2677. retry++;
  2678. } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
  2679. if (readb(baseAddr + C218chksum_ok) != 1) {
  2680. return (-1);
  2681. }
  2682. writew(0, baseAddr + C218_key);
  2683. for (i = 0; i < 100; i++) {
  2684. if (readw(baseAddr + Magic_no) == Magic_code)
  2685. break;
  2686. moxadelay(1); /* delay 10 ms */
  2687. }
  2688. if (readw(baseAddr + Magic_no) != Magic_code) {
  2689. return (-1);
  2690. }
  2691. writew(1, baseAddr + Disable_IRQ);
  2692. writew(0, baseAddr + Magic_no);
  2693. for (i = 0; i < 100; i++) {
  2694. if (readw(baseAddr + Magic_no) == Magic_code)
  2695. break;
  2696. moxadelay(1); /* delay 10 ms */
  2697. }
  2698. if (readw(baseAddr + Magic_no) != Magic_code) {
  2699. return (-1);
  2700. }
  2701. moxaCard = 1;
  2702. moxaIntNdx[cardno] = baseAddr + IRQindex;
  2703. moxaIntPend[cardno] = baseAddr + IRQpending;
  2704. moxaIntTable[cardno] = baseAddr + IRQtable;
  2705. return (0);
  2706. }
  2707. static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
  2708. {
  2709. ushort usum;
  2710. int i, j, wlen, len2, retry;
  2711. ushort *uptr;
  2712. usum = 0;
  2713. wlen = len >> 1;
  2714. uptr = (ushort *) moxaBuff;
  2715. for (i = 0; i < wlen; i++)
  2716. usum += le16_to_cpu(uptr[i]);
  2717. retry = 0;
  2718. j = 0;
  2719. do {
  2720. while (wlen) {
  2721. if (wlen > 2048)
  2722. len2 = 2048;
  2723. else
  2724. len2 = wlen;
  2725. wlen -= len2;
  2726. len2 <<= 1;
  2727. for (i = 0; i < len2; i++)
  2728. writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
  2729. len2 >>= 1;
  2730. j += i;
  2731. writew(len2, baseAddr + C320DLoad_len);
  2732. writew(0, baseAddr + C320_key);
  2733. for (i = 0; i < 10; i++) {
  2734. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2735. break;
  2736. moxadelay(1);
  2737. }
  2738. if (readw(baseAddr + C320_key) != C320_KeyCode)
  2739. return (-1);
  2740. }
  2741. writew(0, baseAddr + C320DLoad_len);
  2742. writew(usum, baseAddr + C320check_sum);
  2743. writew(0, baseAddr + C320_key);
  2744. for (i = 0; i < 10; i++) {
  2745. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2746. break;
  2747. moxadelay(1);
  2748. }
  2749. retry++;
  2750. } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
  2751. if (readb(baseAddr + C320chksum_ok) != 1)
  2752. return (-1);
  2753. writew(0, baseAddr + C320_key);
  2754. for (i = 0; i < 600; i++) {
  2755. if (readw(baseAddr + Magic_no) == Magic_code)
  2756. break;
  2757. moxadelay(1);
  2758. }
  2759. if (readw(baseAddr + Magic_no) != Magic_code)
  2760. return (-100);
  2761. if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
  2762. writew(0x3800, baseAddr + TMS320_PORT1);
  2763. writew(0x3900, baseAddr + TMS320_PORT2);
  2764. writew(28499, baseAddr + TMS320_CLOCK);
  2765. } else {
  2766. writew(0x3200, baseAddr + TMS320_PORT1);
  2767. writew(0x3400, baseAddr + TMS320_PORT2);
  2768. writew(19999, baseAddr + TMS320_CLOCK);
  2769. }
  2770. writew(1, baseAddr + Disable_IRQ);
  2771. writew(0, baseAddr + Magic_no);
  2772. for (i = 0; i < 500; i++) {
  2773. if (readw(baseAddr + Magic_no) == Magic_code)
  2774. break;
  2775. moxadelay(1);
  2776. }
  2777. if (readw(baseAddr + Magic_no) != Magic_code)
  2778. return (-102);
  2779. j = readw(baseAddr + Module_cnt);
  2780. if (j <= 0)
  2781. return (-101);
  2782. *numPorts = j * 8;
  2783. writew(j, baseAddr + Module_no);
  2784. writew(0, baseAddr + Magic_no);
  2785. for (i = 0; i < 600; i++) {
  2786. if (readw(baseAddr + Magic_no) == Magic_code)
  2787. break;
  2788. moxadelay(1);
  2789. }
  2790. if (readw(baseAddr + Magic_no) != Magic_code)
  2791. return (-102);
  2792. moxaCard = 1;
  2793. moxaIntNdx[cardno] = baseAddr + IRQindex;
  2794. moxaIntPend[cardno] = baseAddr + IRQpending;
  2795. moxaIntTable[cardno] = baseAddr + IRQtable;
  2796. return (0);
  2797. }
  2798. #if 0
  2799. long MoxaPortGetCurBaud(int port)
  2800. {
  2801. if (moxaChkPort[port] == 0)
  2802. return (0);
  2803. return (moxaCurBaud[port]);
  2804. }
  2805. #endif /* 0 */
  2806. static void MoxaSetFifo(int port, int enable)
  2807. {
  2808. void __iomem *ofsAddr = moxaTableAddr[port];
  2809. if (!enable) {
  2810. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
  2811. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
  2812. } else {
  2813. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
  2814. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
  2815. }
  2816. }
  2817. #if 0
  2818. int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
  2819. {
  2820. void __iomem *ofsAddr;
  2821. int val;
  2822. val = 0;
  2823. switch (databits) {
  2824. case 5:
  2825. val |= 0;
  2826. break;
  2827. case 6:
  2828. val |= 1;
  2829. break;
  2830. case 7:
  2831. val |= 2;
  2832. break;
  2833. case 8:
  2834. val |= 3;
  2835. break;
  2836. default:
  2837. return (-1);
  2838. }
  2839. switch (stopbits) {
  2840. case 0:
  2841. val |= 0;
  2842. break; /* stop bits 1.5 */
  2843. case 1:
  2844. val |= 0;
  2845. break;
  2846. case 2:
  2847. val |= 4;
  2848. break;
  2849. default:
  2850. return (-1);
  2851. }
  2852. switch (parity) {
  2853. case 0:
  2854. val |= 0x00;
  2855. break; /* None */
  2856. case 1:
  2857. val |= 0x08;
  2858. break; /* Odd */
  2859. case 2:
  2860. val |= 0x18;
  2861. break; /* Even */
  2862. case 3:
  2863. val |= 0x28;
  2864. break; /* Mark */
  2865. case 4:
  2866. val |= 0x38;
  2867. break; /* Space */
  2868. default:
  2869. return (-1);
  2870. }
  2871. ofsAddr = moxaTableAddr[port];
  2872. moxafunc(ofsAddr, FC_SetMode, val);
  2873. return (0);
  2874. }
  2875. int MoxaPortTxBufSize(int port)
  2876. {
  2877. void __iomem *ofsAddr;
  2878. int size;
  2879. ofsAddr = moxaTableAddr[port];
  2880. size = readw(ofsAddr + TX_mask);
  2881. return (size);
  2882. }
  2883. int MoxaPortRxBufSize(int port)
  2884. {
  2885. void __iomem *ofsAddr;
  2886. int size;
  2887. ofsAddr = moxaTableAddr[port];
  2888. size = readw(ofsAddr + RX_mask);
  2889. return (size);
  2890. }
  2891. int MoxaPortRxFree(int port)
  2892. {
  2893. void __iomem *ofsAddr;
  2894. ushort rptr, wptr, mask;
  2895. int len;
  2896. ofsAddr = moxaTableAddr[port];
  2897. rptr = readw(ofsAddr + RXrptr);
  2898. wptr = readw(ofsAddr + RXwptr);
  2899. mask = readw(ofsAddr + RX_mask);
  2900. len = mask - ((wptr - rptr) & mask);
  2901. return (len);
  2902. }
  2903. int MoxaPortGetBrkCnt(int port)
  2904. {
  2905. return (moxaBreakCnt[port]);
  2906. }
  2907. void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
  2908. {
  2909. void __iomem *ofsAddr;
  2910. ofsAddr = moxaTableAddr[port];
  2911. writew(xonValue, ofsAddr + FuncArg);
  2912. writew(xoffValue, ofsAddr + FuncArg1);
  2913. writew(FC_SetXonXoff, ofsAddr + FuncCode);
  2914. wait_finish(ofsAddr);
  2915. }
  2916. int MoxaPortIsTxHold(int port)
  2917. {
  2918. void __iomem *ofsAddr;
  2919. int val;
  2920. ofsAddr = moxaTableAddr[port];
  2921. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  2922. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  2923. moxafunc(ofsAddr, FC_GetCCSR, 0);
  2924. val = readw(ofsAddr + FuncArg);
  2925. if (val & 0x04)
  2926. return (1);
  2927. } else {
  2928. if (readw(ofsAddr + FlagStat) & Tx_flowOff)
  2929. return (1);
  2930. }
  2931. return (0);
  2932. }
  2933. #endif