moxa.c 83 KB

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