moxa.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. /*****************************************************************************/
  2. /*
  3. * moxa.c -- MOXA Intellio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
  6. *
  7. * This code is loosely based on the Linux serial driver, written by
  8. * Linus Torvalds, Theodore T'so and others.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. /*
  16. * MOXA Intellio Series Driver
  17. * for : LINUX
  18. * date : 1999/1/7
  19. * version : 5.1
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/mm.h>
  24. #include <linux/ioport.h>
  25. #include <linux/errno.h>
  26. #include <linux/firmware.h>
  27. #include <linux/signal.h>
  28. #include <linux/sched.h>
  29. #include <linux/timer.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/major.h>
  34. #include <linux/string.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/serial.h>
  38. #include <linux/tty_driver.h>
  39. #include <linux/delay.h>
  40. #include <linux/pci.h>
  41. #include <linux/init.h>
  42. #include <linux/bitops.h>
  43. #include <asm/system.h>
  44. #include <asm/io.h>
  45. #include <asm/uaccess.h>
  46. #include "moxa.h"
  47. #define MOXA_VERSION "5.1k"
  48. #define MOXA_FW_HDRLEN 32
  49. #define MOXAMAJOR 172
  50. #define MAX_BOARDS 4 /* Don't change this value */
  51. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  52. #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
  53. /*
  54. * Define the Moxa PCI vendor and device IDs.
  55. */
  56. #define MOXA_BUS_TYPE_ISA 0
  57. #define MOXA_BUS_TYPE_PCI 1
  58. enum {
  59. MOXA_BOARD_C218_PCI = 1,
  60. MOXA_BOARD_C218_ISA,
  61. MOXA_BOARD_C320_PCI,
  62. MOXA_BOARD_C320_ISA,
  63. MOXA_BOARD_CP204J,
  64. };
  65. static char *moxa_brdname[] =
  66. {
  67. "C218 Turbo PCI series",
  68. "C218 Turbo ISA series",
  69. "C320 Turbo PCI series",
  70. "C320 Turbo ISA series",
  71. "CP-204J series",
  72. };
  73. #ifdef CONFIG_PCI
  74. static struct pci_device_id moxa_pcibrds[] = {
  75. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
  76. .driver_data = MOXA_BOARD_C218_PCI },
  77. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
  78. .driver_data = MOXA_BOARD_C320_PCI },
  79. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
  80. .driver_data = MOXA_BOARD_CP204J },
  81. { 0 }
  82. };
  83. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  84. #endif /* CONFIG_PCI */
  85. struct moxa_port;
  86. static struct moxa_board_conf {
  87. int boardType;
  88. int numPorts;
  89. int busType;
  90. unsigned int ready;
  91. struct moxa_port *ports;
  92. void __iomem *basemem;
  93. void __iomem *intNdx;
  94. void __iomem *intPend;
  95. void __iomem *intTable;
  96. } moxa_boards[MAX_BOARDS];
  97. struct mxser_mstatus {
  98. tcflag_t cflag;
  99. int cts;
  100. int dsr;
  101. int ri;
  102. int dcd;
  103. };
  104. struct moxaq_str {
  105. int inq;
  106. int outq;
  107. };
  108. struct moxa_port {
  109. struct moxa_board_conf *board;
  110. struct tty_struct *tty;
  111. void __iomem *tableAddr;
  112. int type;
  113. int close_delay;
  114. unsigned int count;
  115. int asyncflags;
  116. int cflag;
  117. unsigned long statusflags;
  118. wait_queue_head_t open_wait;
  119. u8 DCDState;
  120. u8 lineCtrl;
  121. u8 lowChkFlag;
  122. };
  123. struct mon_str {
  124. int tick;
  125. int rxcnt[MAX_PORTS];
  126. int txcnt[MAX_PORTS];
  127. };
  128. /* statusflags */
  129. #define TXSTOPPED 0x1
  130. #define LOWWAIT 0x2
  131. #define EMPTYWAIT 0x4
  132. #define THROTTLE 0x8
  133. #define SERIAL_DO_RESTART
  134. #define WAKEUP_CHARS 256
  135. static int ttymajor = MOXAMAJOR;
  136. static struct mon_str moxaLog;
  137. static unsigned int moxaFuncTout = HZ / 2;
  138. static unsigned int moxaLowWaterChk;
  139. static DEFINE_MUTEX(moxa_openlock);
  140. /* Variables for insmod */
  141. #ifdef MODULE
  142. static unsigned long baseaddr[MAX_BOARDS];
  143. static unsigned int type[MAX_BOARDS];
  144. static unsigned int numports[MAX_BOARDS];
  145. #endif
  146. MODULE_AUTHOR("William Chen");
  147. MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
  148. MODULE_LICENSE("GPL");
  149. #ifdef MODULE
  150. module_param_array(type, uint, NULL, 0);
  151. MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
  152. module_param_array(baseaddr, ulong, NULL, 0);
  153. MODULE_PARM_DESC(baseaddr, "base address");
  154. module_param_array(numports, uint, NULL, 0);
  155. MODULE_PARM_DESC(numports, "numports (ignored for C218)");
  156. #endif
  157. module_param(ttymajor, int, 0);
  158. /*
  159. * static functions:
  160. */
  161. static int moxa_open(struct tty_struct *, struct file *);
  162. static void moxa_close(struct tty_struct *, struct file *);
  163. static int moxa_write(struct tty_struct *, const unsigned char *, int);
  164. static int moxa_write_room(struct tty_struct *);
  165. static void moxa_flush_buffer(struct tty_struct *);
  166. static int moxa_chars_in_buffer(struct tty_struct *);
  167. static void moxa_throttle(struct tty_struct *);
  168. static void moxa_unthrottle(struct tty_struct *);
  169. static void moxa_set_termios(struct tty_struct *, struct ktermios *);
  170. static void moxa_stop(struct tty_struct *);
  171. static void moxa_start(struct tty_struct *);
  172. static void moxa_hangup(struct tty_struct *);
  173. static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
  174. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  175. unsigned int set, unsigned int clear);
  176. static void moxa_poll(unsigned long);
  177. static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
  178. static void moxa_setup_empty_event(struct tty_struct *);
  179. static void moxa_shut_down(struct moxa_port *);
  180. /*
  181. * moxa board interface functions:
  182. */
  183. static void MoxaPortEnable(struct moxa_port *);
  184. static void MoxaPortDisable(struct moxa_port *);
  185. static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
  186. static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
  187. static void MoxaPortLineCtrl(struct moxa_port *, int, int);
  188. static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
  189. static int MoxaPortLineStatus(struct moxa_port *);
  190. static void MoxaPortFlushData(struct moxa_port *, int);
  191. static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
  192. static int MoxaPortReadData(struct moxa_port *);
  193. static int MoxaPortTxQueue(struct moxa_port *);
  194. static int MoxaPortRxQueue(struct moxa_port *);
  195. static int MoxaPortTxFree(struct moxa_port *);
  196. static void MoxaPortTxDisable(struct moxa_port *);
  197. static void MoxaPortTxEnable(struct moxa_port *);
  198. static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
  199. static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
  200. static void MoxaSetFifo(struct moxa_port *port, int enable);
  201. /*
  202. * I/O functions
  203. */
  204. static void moxa_wait_finish(void __iomem *ofsAddr)
  205. {
  206. unsigned long end = jiffies + moxaFuncTout;
  207. while (readw(ofsAddr + FuncCode) != 0)
  208. if (time_after(jiffies, end))
  209. return;
  210. if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
  211. printk(KERN_WARNING "moxa function expired\n");
  212. }
  213. static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
  214. {
  215. writew(arg, ofsAddr + FuncArg);
  216. writew(cmd, ofsAddr + FuncCode);
  217. moxa_wait_finish(ofsAddr);
  218. }
  219. static void moxa_low_water_check(void __iomem *ofsAddr)
  220. {
  221. u16 rptr, wptr, mask, len;
  222. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  223. rptr = readw(ofsAddr + RXrptr);
  224. wptr = readw(ofsAddr + RXwptr);
  225. mask = readw(ofsAddr + RX_mask);
  226. len = (wptr - rptr) & mask;
  227. if (len <= Low_water)
  228. moxafunc(ofsAddr, FC_SendXon, 0);
  229. }
  230. }
  231. /*
  232. * TTY operations
  233. */
  234. static int moxa_ioctl(struct tty_struct *tty, struct file *file,
  235. unsigned int cmd, unsigned long arg)
  236. {
  237. struct moxa_port *ch = tty->driver_data;
  238. void __user *argp = (void __user *)arg;
  239. int status, ret = 0;
  240. if (tty->index == MAX_PORTS) {
  241. if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
  242. cmd != MOXA_GETMSTATUS)
  243. return -EINVAL;
  244. } else if (!ch)
  245. return -ENODEV;
  246. switch (cmd) {
  247. case MOXA_GETDATACOUNT:
  248. moxaLog.tick = jiffies;
  249. if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
  250. ret = -EFAULT;
  251. break;
  252. case MOXA_FLUSH_QUEUE:
  253. MoxaPortFlushData(ch, arg);
  254. break;
  255. case MOXA_GET_IOQUEUE: {
  256. struct moxaq_str __user *argm = argp;
  257. struct moxaq_str tmp;
  258. struct moxa_port *p;
  259. unsigned int i, j;
  260. mutex_lock(&moxa_openlock);
  261. for (i = 0; i < MAX_BOARDS; i++) {
  262. p = moxa_boards[i].ports;
  263. for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
  264. memset(&tmp, 0, sizeof(tmp));
  265. if (moxa_boards[i].ready) {
  266. tmp.inq = MoxaPortRxQueue(p);
  267. tmp.outq = MoxaPortTxQueue(p);
  268. }
  269. if (copy_to_user(argm, &tmp, sizeof(tmp))) {
  270. mutex_unlock(&moxa_openlock);
  271. return -EFAULT;
  272. }
  273. }
  274. }
  275. mutex_unlock(&moxa_openlock);
  276. break;
  277. } case MOXA_GET_OQUEUE:
  278. status = MoxaPortTxQueue(ch);
  279. ret = put_user(status, (unsigned long __user *)argp);
  280. break;
  281. case MOXA_GET_IQUEUE:
  282. status = MoxaPortRxQueue(ch);
  283. ret = put_user(status, (unsigned long __user *)argp);
  284. break;
  285. case MOXA_GETMSTATUS: {
  286. struct mxser_mstatus __user *argm = argp;
  287. struct mxser_mstatus tmp;
  288. struct moxa_port *p;
  289. unsigned int i, j;
  290. mutex_lock(&moxa_openlock);
  291. for (i = 0; i < MAX_BOARDS; i++) {
  292. p = moxa_boards[i].ports;
  293. for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
  294. memset(&tmp, 0, sizeof(tmp));
  295. if (!moxa_boards[i].ready)
  296. goto copy;
  297. status = MoxaPortLineStatus(p);
  298. if (status & 1)
  299. tmp.cts = 1;
  300. if (status & 2)
  301. tmp.dsr = 1;
  302. if (status & 4)
  303. tmp.dcd = 1;
  304. if (!p->tty || !p->tty->termios)
  305. tmp.cflag = p->cflag;
  306. else
  307. tmp.cflag = p->tty->termios->c_cflag;
  308. copy:
  309. if (copy_to_user(argm, &tmp, sizeof(tmp))) {
  310. mutex_unlock(&moxa_openlock);
  311. return -EFAULT;
  312. }
  313. }
  314. }
  315. mutex_unlock(&moxa_openlock);
  316. break;
  317. }
  318. case TIOCGSERIAL:
  319. mutex_lock(&moxa_openlock);
  320. ret = moxa_get_serial_info(ch, argp);
  321. mutex_unlock(&moxa_openlock);
  322. break;
  323. case TIOCSSERIAL:
  324. mutex_lock(&moxa_openlock);
  325. ret = moxa_set_serial_info(ch, argp);
  326. mutex_unlock(&moxa_openlock);
  327. break;
  328. default:
  329. ret = -ENOIOCTLCMD;
  330. }
  331. return ret;
  332. }
  333. static void moxa_break_ctl(struct tty_struct *tty, int state)
  334. {
  335. struct moxa_port *port = tty->driver_data;
  336. moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
  337. Magic_code);
  338. }
  339. static const struct tty_operations moxa_ops = {
  340. .open = moxa_open,
  341. .close = moxa_close,
  342. .write = moxa_write,
  343. .write_room = moxa_write_room,
  344. .flush_buffer = moxa_flush_buffer,
  345. .chars_in_buffer = moxa_chars_in_buffer,
  346. .ioctl = moxa_ioctl,
  347. .throttle = moxa_throttle,
  348. .unthrottle = moxa_unthrottle,
  349. .set_termios = moxa_set_termios,
  350. .stop = moxa_stop,
  351. .start = moxa_start,
  352. .hangup = moxa_hangup,
  353. .break_ctl = moxa_break_ctl,
  354. .tiocmget = moxa_tiocmget,
  355. .tiocmset = moxa_tiocmset,
  356. };
  357. static struct tty_driver *moxaDriver;
  358. static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
  359. static DEFINE_SPINLOCK(moxa_lock);
  360. /*
  361. * HW init
  362. */
  363. static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
  364. {
  365. switch (brd->boardType) {
  366. case MOXA_BOARD_C218_ISA:
  367. case MOXA_BOARD_C218_PCI:
  368. if (model != 1)
  369. goto err;
  370. break;
  371. case MOXA_BOARD_CP204J:
  372. if (model != 3)
  373. goto err;
  374. break;
  375. default:
  376. if (model != 2)
  377. goto err;
  378. break;
  379. }
  380. return 0;
  381. err:
  382. return -EINVAL;
  383. }
  384. static int moxa_check_fw(const void *ptr)
  385. {
  386. const __le16 *lptr = ptr;
  387. if (*lptr != cpu_to_le16(0x7980))
  388. return -EINVAL;
  389. return 0;
  390. }
  391. static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
  392. size_t len)
  393. {
  394. void __iomem *baseAddr = brd->basemem;
  395. u16 tmp;
  396. writeb(HW_reset, baseAddr + Control_reg); /* reset */
  397. msleep(10);
  398. memset_io(baseAddr, 0, 4096);
  399. memcpy_toio(baseAddr, buf, len); /* download BIOS */
  400. writeb(0, baseAddr + Control_reg); /* restart */
  401. msleep(2000);
  402. switch (brd->boardType) {
  403. case MOXA_BOARD_C218_ISA:
  404. case MOXA_BOARD_C218_PCI:
  405. tmp = readw(baseAddr + C218_key);
  406. if (tmp != C218_KeyCode)
  407. goto err;
  408. break;
  409. case MOXA_BOARD_CP204J:
  410. tmp = readw(baseAddr + C218_key);
  411. if (tmp != CP204J_KeyCode)
  412. goto err;
  413. break;
  414. default:
  415. tmp = readw(baseAddr + C320_key);
  416. if (tmp != C320_KeyCode)
  417. goto err;
  418. tmp = readw(baseAddr + C320_status);
  419. if (tmp != STS_init) {
  420. printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
  421. "module not found\n");
  422. return -EIO;
  423. }
  424. break;
  425. }
  426. return 0;
  427. err:
  428. printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
  429. return -EIO;
  430. }
  431. static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
  432. size_t len)
  433. {
  434. void __iomem *baseAddr = brd->basemem;
  435. if (len < 7168) {
  436. printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
  437. return -EINVAL;
  438. }
  439. writew(len - 7168 - 2, baseAddr + C320bapi_len);
  440. writeb(1, baseAddr + Control_reg); /* Select Page 1 */
  441. memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
  442. writeb(2, baseAddr + Control_reg); /* Select Page 2 */
  443. memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
  444. return 0;
  445. }
  446. static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
  447. size_t len)
  448. {
  449. void __iomem *baseAddr = brd->basemem;
  450. const u16 *uptr = ptr;
  451. size_t wlen, len2, j;
  452. unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
  453. unsigned int i, retry, c320;
  454. u16 usum, keycode;
  455. c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
  456. brd->boardType == MOXA_BOARD_C320_ISA;
  457. keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
  458. C218_KeyCode;
  459. switch (brd->boardType) {
  460. case MOXA_BOARD_CP204J:
  461. case MOXA_BOARD_C218_ISA:
  462. case MOXA_BOARD_C218_PCI:
  463. key = C218_key;
  464. loadbuf = C218_LoadBuf;
  465. loadlen = C218DLoad_len;
  466. checksum = C218check_sum;
  467. checksum_ok = C218chksum_ok;
  468. break;
  469. default:
  470. key = C320_key;
  471. keycode = C320_KeyCode;
  472. loadbuf = C320_LoadBuf;
  473. loadlen = C320DLoad_len;
  474. checksum = C320check_sum;
  475. checksum_ok = C320chksum_ok;
  476. break;
  477. }
  478. usum = 0;
  479. wlen = len >> 1;
  480. for (i = 0; i < wlen; i++)
  481. usum += le16_to_cpu(uptr[i]);
  482. retry = 0;
  483. do {
  484. wlen = len >> 1;
  485. j = 0;
  486. while (wlen) {
  487. len2 = (wlen > 2048) ? 2048 : wlen;
  488. wlen -= len2;
  489. memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
  490. j += len2 << 1;
  491. writew(len2, baseAddr + loadlen);
  492. writew(0, baseAddr + key);
  493. for (i = 0; i < 100; i++) {
  494. if (readw(baseAddr + key) == keycode)
  495. break;
  496. msleep(10);
  497. }
  498. if (readw(baseAddr + key) != keycode)
  499. return -EIO;
  500. }
  501. writew(0, baseAddr + loadlen);
  502. writew(usum, baseAddr + checksum);
  503. writew(0, baseAddr + key);
  504. for (i = 0; i < 100; i++) {
  505. if (readw(baseAddr + key) == keycode)
  506. break;
  507. msleep(10);
  508. }
  509. retry++;
  510. } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
  511. if (readb(baseAddr + checksum_ok) != 1)
  512. return -EIO;
  513. writew(0, baseAddr + key);
  514. for (i = 0; i < 600; i++) {
  515. if (readw(baseAddr + Magic_no) == Magic_code)
  516. break;
  517. msleep(10);
  518. }
  519. if (readw(baseAddr + Magic_no) != Magic_code)
  520. return -EIO;
  521. if (c320) {
  522. if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
  523. writew(0x3800, baseAddr + TMS320_PORT1);
  524. writew(0x3900, baseAddr + TMS320_PORT2);
  525. writew(28499, baseAddr + TMS320_CLOCK);
  526. } else {
  527. writew(0x3200, baseAddr + TMS320_PORT1);
  528. writew(0x3400, baseAddr + TMS320_PORT2);
  529. writew(19999, baseAddr + TMS320_CLOCK);
  530. }
  531. }
  532. writew(1, baseAddr + Disable_IRQ);
  533. writew(0, baseAddr + Magic_no);
  534. for (i = 0; i < 500; i++) {
  535. if (readw(baseAddr + Magic_no) == Magic_code)
  536. break;
  537. msleep(10);
  538. }
  539. if (readw(baseAddr + Magic_no) != Magic_code)
  540. return -EIO;
  541. if (c320) {
  542. j = readw(baseAddr + Module_cnt);
  543. if (j <= 0)
  544. return -EIO;
  545. brd->numPorts = j * 8;
  546. writew(j, baseAddr + Module_no);
  547. writew(0, baseAddr + Magic_no);
  548. for (i = 0; i < 600; i++) {
  549. if (readw(baseAddr + Magic_no) == Magic_code)
  550. break;
  551. msleep(10);
  552. }
  553. if (readw(baseAddr + Magic_no) != Magic_code)
  554. return -EIO;
  555. }
  556. brd->intNdx = baseAddr + IRQindex;
  557. brd->intPend = baseAddr + IRQpending;
  558. brd->intTable = baseAddr + IRQtable;
  559. return 0;
  560. }
  561. static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
  562. size_t len)
  563. {
  564. void __iomem *ofsAddr, *baseAddr = brd->basemem;
  565. struct moxa_port *port;
  566. int retval, i;
  567. if (len % 2) {
  568. printk(KERN_ERR "MOXA: bios length is not even\n");
  569. return -EINVAL;
  570. }
  571. retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
  572. if (retval)
  573. return retval;
  574. switch (brd->boardType) {
  575. case MOXA_BOARD_C218_ISA:
  576. case MOXA_BOARD_C218_PCI:
  577. case MOXA_BOARD_CP204J:
  578. port = brd->ports;
  579. for (i = 0; i < brd->numPorts; i++, port++) {
  580. port->board = brd;
  581. port->DCDState = 0;
  582. port->tableAddr = baseAddr + Extern_table +
  583. Extern_size * i;
  584. ofsAddr = port->tableAddr;
  585. writew(C218rx_mask, ofsAddr + RX_mask);
  586. writew(C218tx_mask, ofsAddr + TX_mask);
  587. writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
  588. writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
  589. writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
  590. writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
  591. }
  592. break;
  593. default:
  594. port = brd->ports;
  595. for (i = 0; i < brd->numPorts; i++, port++) {
  596. port->board = brd;
  597. port->DCDState = 0;
  598. port->tableAddr = baseAddr + Extern_table +
  599. Extern_size * i;
  600. ofsAddr = port->tableAddr;
  601. switch (brd->numPorts) {
  602. case 8:
  603. writew(C320p8rx_mask, ofsAddr + RX_mask);
  604. writew(C320p8tx_mask, ofsAddr + TX_mask);
  605. writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
  606. writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
  607. writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
  608. writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
  609. break;
  610. case 16:
  611. writew(C320p16rx_mask, ofsAddr + RX_mask);
  612. writew(C320p16tx_mask, ofsAddr + TX_mask);
  613. writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
  614. writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
  615. writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
  616. writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
  617. break;
  618. case 24:
  619. writew(C320p24rx_mask, ofsAddr + RX_mask);
  620. writew(C320p24tx_mask, ofsAddr + TX_mask);
  621. writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
  622. writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
  623. writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
  624. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  625. break;
  626. case 32:
  627. writew(C320p32rx_mask, ofsAddr + RX_mask);
  628. writew(C320p32tx_mask, ofsAddr + TX_mask);
  629. writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
  630. writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
  631. writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
  632. writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
  633. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  634. break;
  635. }
  636. }
  637. break;
  638. }
  639. return 0;
  640. }
  641. static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
  642. {
  643. void *ptr = fw->data;
  644. char rsn[64];
  645. u16 lens[5];
  646. size_t len;
  647. unsigned int a, lenp, lencnt;
  648. int ret = -EINVAL;
  649. struct {
  650. __le32 magic; /* 0x34303430 */
  651. u8 reserved1[2];
  652. u8 type; /* UNIX = 3 */
  653. u8 model; /* C218T=1, C320T=2, CP204=3 */
  654. u8 reserved2[8];
  655. __le16 len[5];
  656. } *hdr = ptr;
  657. BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
  658. if (fw->size < MOXA_FW_HDRLEN) {
  659. strcpy(rsn, "too short (even header won't fit)");
  660. goto err;
  661. }
  662. if (hdr->magic != cpu_to_le32(0x30343034)) {
  663. sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
  664. goto err;
  665. }
  666. if (hdr->type != 3) {
  667. sprintf(rsn, "not for linux, type is %u", hdr->type);
  668. goto err;
  669. }
  670. if (moxa_check_fw_model(brd, hdr->model)) {
  671. sprintf(rsn, "not for this card, model is %u", hdr->model);
  672. goto err;
  673. }
  674. len = MOXA_FW_HDRLEN;
  675. lencnt = hdr->model == 2 ? 5 : 3;
  676. for (a = 0; a < ARRAY_SIZE(lens); a++) {
  677. lens[a] = le16_to_cpu(hdr->len[a]);
  678. if (lens[a] && len + lens[a] <= fw->size &&
  679. moxa_check_fw(&fw->data[len]))
  680. printk(KERN_WARNING "MOXA firmware: unexpected input "
  681. "at offset %u, but going on\n", (u32)len);
  682. if (!lens[a] && a < lencnt) {
  683. sprintf(rsn, "too few entries in fw file");
  684. goto err;
  685. }
  686. len += lens[a];
  687. }
  688. if (len != fw->size) {
  689. sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
  690. (u32)len);
  691. goto err;
  692. }
  693. ptr += MOXA_FW_HDRLEN;
  694. lenp = 0; /* bios */
  695. strcpy(rsn, "read above");
  696. ret = moxa_load_bios(brd, ptr, lens[lenp]);
  697. if (ret)
  698. goto err;
  699. /* we skip the tty section (lens[1]), since we don't need it */
  700. ptr += lens[lenp] + lens[lenp + 1];
  701. lenp += 2; /* comm */
  702. if (hdr->model == 2) {
  703. ret = moxa_load_320b(brd, ptr, lens[lenp]);
  704. if (ret)
  705. goto err;
  706. /* skip another tty */
  707. ptr += lens[lenp] + lens[lenp + 1];
  708. lenp += 2;
  709. }
  710. ret = moxa_load_code(brd, ptr, lens[lenp]);
  711. if (ret)
  712. goto err;
  713. return 0;
  714. err:
  715. printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
  716. return ret;
  717. }
  718. static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
  719. {
  720. const struct firmware *fw;
  721. const char *file;
  722. struct moxa_port *p;
  723. unsigned int i;
  724. int ret;
  725. brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
  726. GFP_KERNEL);
  727. if (brd->ports == NULL) {
  728. printk(KERN_ERR "cannot allocate memory for ports\n");
  729. ret = -ENOMEM;
  730. goto err;
  731. }
  732. for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
  733. p->type = PORT_16550A;
  734. p->close_delay = 5 * HZ / 10;
  735. p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  736. init_waitqueue_head(&p->open_wait);
  737. }
  738. switch (brd->boardType) {
  739. case MOXA_BOARD_C218_ISA:
  740. case MOXA_BOARD_C218_PCI:
  741. file = "c218tunx.cod";
  742. break;
  743. case MOXA_BOARD_CP204J:
  744. file = "cp204unx.cod";
  745. break;
  746. default:
  747. file = "c320tunx.cod";
  748. break;
  749. }
  750. ret = request_firmware(&fw, file, dev);
  751. if (ret) {
  752. printk(KERN_ERR "request_firmware failed\n");
  753. goto err_free;
  754. }
  755. ret = moxa_load_fw(brd, fw);
  756. release_firmware(fw);
  757. if (ret)
  758. goto err_free;
  759. spin_lock_bh(&moxa_lock);
  760. brd->ready = 1;
  761. if (!timer_pending(&moxaTimer))
  762. mod_timer(&moxaTimer, jiffies + HZ / 50);
  763. spin_unlock_bh(&moxa_lock);
  764. return 0;
  765. err_free:
  766. kfree(brd->ports);
  767. err:
  768. return ret;
  769. }
  770. static void moxa_board_deinit(struct moxa_board_conf *brd)
  771. {
  772. unsigned int a, opened;
  773. mutex_lock(&moxa_openlock);
  774. spin_lock_bh(&moxa_lock);
  775. brd->ready = 0;
  776. spin_unlock_bh(&moxa_lock);
  777. /* pci hot-un-plug support */
  778. for (a = 0; a < brd->numPorts; a++)
  779. if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
  780. tty_hangup(brd->ports[a].tty);
  781. while (1) {
  782. opened = 0;
  783. for (a = 0; a < brd->numPorts; a++)
  784. if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
  785. opened++;
  786. mutex_unlock(&moxa_openlock);
  787. if (!opened)
  788. break;
  789. msleep(50);
  790. mutex_lock(&moxa_openlock);
  791. }
  792. iounmap(brd->basemem);
  793. brd->basemem = NULL;
  794. kfree(brd->ports);
  795. }
  796. #ifdef CONFIG_PCI
  797. static int __devinit moxa_pci_probe(struct pci_dev *pdev,
  798. const struct pci_device_id *ent)
  799. {
  800. struct moxa_board_conf *board;
  801. unsigned int i;
  802. int board_type = ent->driver_data;
  803. int retval;
  804. retval = pci_enable_device(pdev);
  805. if (retval) {
  806. dev_err(&pdev->dev, "can't enable pci device\n");
  807. goto err;
  808. }
  809. for (i = 0; i < MAX_BOARDS; i++)
  810. if (moxa_boards[i].basemem == NULL)
  811. break;
  812. retval = -ENODEV;
  813. if (i >= MAX_BOARDS) {
  814. dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
  815. "found. Board is ignored.\n", MAX_BOARDS);
  816. goto err;
  817. }
  818. board = &moxa_boards[i];
  819. retval = pci_request_region(pdev, 2, "moxa-base");
  820. if (retval) {
  821. dev_err(&pdev->dev, "can't request pci region 2\n");
  822. goto err;
  823. }
  824. board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
  825. if (board->basemem == NULL) {
  826. dev_err(&pdev->dev, "can't remap io space 2\n");
  827. goto err_reg;
  828. }
  829. board->boardType = board_type;
  830. switch (board_type) {
  831. case MOXA_BOARD_C218_ISA:
  832. case MOXA_BOARD_C218_PCI:
  833. board->numPorts = 8;
  834. break;
  835. case MOXA_BOARD_CP204J:
  836. board->numPorts = 4;
  837. break;
  838. default:
  839. board->numPorts = 0;
  840. break;
  841. }
  842. board->busType = MOXA_BUS_TYPE_PCI;
  843. retval = moxa_init_board(board, &pdev->dev);
  844. if (retval)
  845. goto err_base;
  846. pci_set_drvdata(pdev, board);
  847. return 0;
  848. err_base:
  849. iounmap(board->basemem);
  850. board->basemem = NULL;
  851. err_reg:
  852. pci_release_region(pdev, 2);
  853. err:
  854. return retval;
  855. }
  856. static void __devexit moxa_pci_remove(struct pci_dev *pdev)
  857. {
  858. struct moxa_board_conf *brd = pci_get_drvdata(pdev);
  859. moxa_board_deinit(brd);
  860. pci_release_region(pdev, 2);
  861. }
  862. static struct pci_driver moxa_pci_driver = {
  863. .name = "moxa",
  864. .id_table = moxa_pcibrds,
  865. .probe = moxa_pci_probe,
  866. .remove = __devexit_p(moxa_pci_remove)
  867. };
  868. #endif /* CONFIG_PCI */
  869. static int __init moxa_init(void)
  870. {
  871. unsigned int isabrds = 0;
  872. int retval = 0;
  873. printk(KERN_INFO "MOXA Intellio family driver version %s\n",
  874. MOXA_VERSION);
  875. moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
  876. if (!moxaDriver)
  877. return -ENOMEM;
  878. moxaDriver->owner = THIS_MODULE;
  879. moxaDriver->name = "ttyMX";
  880. moxaDriver->major = ttymajor;
  881. moxaDriver->minor_start = 0;
  882. moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
  883. moxaDriver->subtype = SERIAL_TYPE_NORMAL;
  884. moxaDriver->init_termios = tty_std_termios;
  885. moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  886. moxaDriver->init_termios.c_ispeed = 9600;
  887. moxaDriver->init_termios.c_ospeed = 9600;
  888. moxaDriver->flags = TTY_DRIVER_REAL_RAW;
  889. tty_set_operations(moxaDriver, &moxa_ops);
  890. if (tty_register_driver(moxaDriver)) {
  891. printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
  892. put_tty_driver(moxaDriver);
  893. return -1;
  894. }
  895. /* Find the boards defined from module args. */
  896. #ifdef MODULE
  897. {
  898. struct moxa_board_conf *brd = moxa_boards;
  899. unsigned int i;
  900. for (i = 0; i < MAX_BOARDS; i++) {
  901. if (!baseaddr[i])
  902. break;
  903. if (type[i] == MOXA_BOARD_C218_ISA ||
  904. type[i] == MOXA_BOARD_C320_ISA) {
  905. pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
  906. isabrds + 1, moxa_brdname[type[i] - 1],
  907. baseaddr[i]);
  908. brd->boardType = type[i];
  909. brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
  910. numports[i];
  911. brd->busType = MOXA_BUS_TYPE_ISA;
  912. brd->basemem = ioremap(baseaddr[i], 0x4000);
  913. if (!brd->basemem) {
  914. printk(KERN_ERR "MOXA: can't remap %lx\n",
  915. baseaddr[i]);
  916. continue;
  917. }
  918. if (moxa_init_board(brd, NULL)) {
  919. iounmap(brd->basemem);
  920. brd->basemem = NULL;
  921. continue;
  922. }
  923. brd++;
  924. isabrds++;
  925. }
  926. }
  927. }
  928. #endif
  929. #ifdef CONFIG_PCI
  930. retval = pci_register_driver(&moxa_pci_driver);
  931. if (retval) {
  932. printk(KERN_ERR "Can't register MOXA pci driver!\n");
  933. if (isabrds)
  934. retval = 0;
  935. }
  936. #endif
  937. return retval;
  938. }
  939. static void __exit moxa_exit(void)
  940. {
  941. unsigned int i;
  942. #ifdef CONFIG_PCI
  943. pci_unregister_driver(&moxa_pci_driver);
  944. #endif
  945. for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
  946. if (moxa_boards[i].ready)
  947. moxa_board_deinit(&moxa_boards[i]);
  948. del_timer_sync(&moxaTimer);
  949. if (tty_unregister_driver(moxaDriver))
  950. printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
  951. "serial driver\n");
  952. put_tty_driver(moxaDriver);
  953. }
  954. module_init(moxa_init);
  955. module_exit(moxa_exit);
  956. static void moxa_close_port(struct moxa_port *ch)
  957. {
  958. moxa_shut_down(ch);
  959. MoxaPortFlushData(ch, 2);
  960. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  961. ch->tty->driver_data = NULL;
  962. ch->tty = NULL;
  963. }
  964. static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
  965. struct moxa_port *ch)
  966. {
  967. DEFINE_WAIT(wait);
  968. int retval = 0;
  969. u8 dcd;
  970. while (1) {
  971. prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
  972. if (tty_hung_up_p(filp)) {
  973. #ifdef SERIAL_DO_RESTART
  974. retval = -ERESTARTSYS;
  975. #else
  976. retval = -EAGAIN;
  977. #endif
  978. break;
  979. }
  980. spin_lock_bh(&moxa_lock);
  981. dcd = ch->DCDState;
  982. spin_unlock_bh(&moxa_lock);
  983. if (dcd)
  984. break;
  985. if (signal_pending(current)) {
  986. retval = -ERESTARTSYS;
  987. break;
  988. }
  989. schedule();
  990. }
  991. finish_wait(&ch->open_wait, &wait);
  992. return retval;
  993. }
  994. static int moxa_open(struct tty_struct *tty, struct file *filp)
  995. {
  996. struct moxa_board_conf *brd;
  997. struct moxa_port *ch;
  998. int port;
  999. int retval;
  1000. port = tty->index;
  1001. if (port == MAX_PORTS) {
  1002. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  1003. }
  1004. if (mutex_lock_interruptible(&moxa_openlock))
  1005. return -ERESTARTSYS;
  1006. brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
  1007. if (!brd->ready) {
  1008. mutex_unlock(&moxa_openlock);
  1009. return -ENODEV;
  1010. }
  1011. ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
  1012. ch->count++;
  1013. tty->driver_data = ch;
  1014. ch->tty = tty;
  1015. if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
  1016. ch->statusflags = 0;
  1017. moxa_set_tty_param(tty, tty->termios);
  1018. MoxaPortLineCtrl(ch, 1, 1);
  1019. MoxaPortEnable(ch);
  1020. MoxaSetFifo(ch, ch->type == PORT_16550A);
  1021. ch->asyncflags |= ASYNC_INITIALIZED;
  1022. }
  1023. mutex_unlock(&moxa_openlock);
  1024. retval = 0;
  1025. if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
  1026. retval = moxa_block_till_ready(tty, filp, ch);
  1027. mutex_lock(&moxa_openlock);
  1028. if (retval) {
  1029. if (ch->count) /* 0 means already hung up... */
  1030. if (--ch->count == 0)
  1031. moxa_close_port(ch);
  1032. } else
  1033. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  1034. mutex_unlock(&moxa_openlock);
  1035. return retval;
  1036. }
  1037. static void moxa_close(struct tty_struct *tty, struct file *filp)
  1038. {
  1039. struct moxa_port *ch;
  1040. int port;
  1041. port = tty->index;
  1042. if (port == MAX_PORTS || tty_hung_up_p(filp))
  1043. return;
  1044. mutex_lock(&moxa_openlock);
  1045. ch = tty->driver_data;
  1046. if (ch == NULL)
  1047. goto unlock;
  1048. if (tty->count == 1 && ch->count != 1) {
  1049. printk(KERN_WARNING "moxa_close: bad serial port count; "
  1050. "tty->count is 1, ch->count is %d\n", ch->count);
  1051. ch->count = 1;
  1052. }
  1053. if (--ch->count < 0) {
  1054. printk(KERN_WARNING "moxa_close: bad serial port count, "
  1055. "device=%s\n", tty->name);
  1056. ch->count = 0;
  1057. }
  1058. if (ch->count)
  1059. goto unlock;
  1060. ch->cflag = tty->termios->c_cflag;
  1061. if (ch->asyncflags & ASYNC_INITIALIZED) {
  1062. moxa_setup_empty_event(tty);
  1063. tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
  1064. }
  1065. moxa_close_port(ch);
  1066. unlock:
  1067. mutex_unlock(&moxa_openlock);
  1068. }
  1069. static int moxa_write(struct tty_struct *tty,
  1070. const unsigned char *buf, int count)
  1071. {
  1072. struct moxa_port *ch = tty->driver_data;
  1073. int len;
  1074. if (ch == NULL)
  1075. return 0;
  1076. spin_lock_bh(&moxa_lock);
  1077. len = MoxaPortWriteData(ch, buf, count);
  1078. spin_unlock_bh(&moxa_lock);
  1079. ch->statusflags |= LOWWAIT;
  1080. return len;
  1081. }
  1082. static int moxa_write_room(struct tty_struct *tty)
  1083. {
  1084. struct moxa_port *ch;
  1085. if (tty->stopped)
  1086. return 0;
  1087. ch = tty->driver_data;
  1088. if (ch == NULL)
  1089. return 0;
  1090. return MoxaPortTxFree(ch);
  1091. }
  1092. static void moxa_flush_buffer(struct tty_struct *tty)
  1093. {
  1094. struct moxa_port *ch = tty->driver_data;
  1095. if (ch == NULL)
  1096. return;
  1097. MoxaPortFlushData(ch, 1);
  1098. tty_wakeup(tty);
  1099. }
  1100. static int moxa_chars_in_buffer(struct tty_struct *tty)
  1101. {
  1102. struct moxa_port *ch = tty->driver_data;
  1103. int chars;
  1104. /*
  1105. * Sigh...I have to check if driver_data is NULL here, because
  1106. * if an open() fails, the TTY subsystem eventually calls
  1107. * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
  1108. * routine. And since the open() failed, we return 0 here. TDJ
  1109. */
  1110. if (ch == NULL)
  1111. return 0;
  1112. chars = MoxaPortTxQueue(ch);
  1113. if (chars) {
  1114. /*
  1115. * Make it possible to wakeup anything waiting for output
  1116. * in tty_ioctl.c, etc.
  1117. */
  1118. if (!(ch->statusflags & EMPTYWAIT))
  1119. moxa_setup_empty_event(tty);
  1120. }
  1121. return chars;
  1122. }
  1123. static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
  1124. {
  1125. struct moxa_port *ch;
  1126. int flag = 0, dtr, rts;
  1127. mutex_lock(&moxa_openlock);
  1128. ch = tty->driver_data;
  1129. if (!ch) {
  1130. mutex_unlock(&moxa_openlock);
  1131. return -EINVAL;
  1132. }
  1133. MoxaPortGetLineOut(ch, &dtr, &rts);
  1134. if (dtr)
  1135. flag |= TIOCM_DTR;
  1136. if (rts)
  1137. flag |= TIOCM_RTS;
  1138. dtr = MoxaPortLineStatus(ch);
  1139. if (dtr & 1)
  1140. flag |= TIOCM_CTS;
  1141. if (dtr & 2)
  1142. flag |= TIOCM_DSR;
  1143. if (dtr & 4)
  1144. flag |= TIOCM_CD;
  1145. mutex_unlock(&moxa_openlock);
  1146. return flag;
  1147. }
  1148. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  1149. unsigned int set, unsigned int clear)
  1150. {
  1151. struct moxa_port *ch;
  1152. int port;
  1153. int dtr, rts;
  1154. port = tty->index;
  1155. mutex_lock(&moxa_openlock);
  1156. ch = tty->driver_data;
  1157. if (!ch) {
  1158. mutex_unlock(&moxa_openlock);
  1159. return -EINVAL;
  1160. }
  1161. MoxaPortGetLineOut(ch, &dtr, &rts);
  1162. if (set & TIOCM_RTS)
  1163. rts = 1;
  1164. if (set & TIOCM_DTR)
  1165. dtr = 1;
  1166. if (clear & TIOCM_RTS)
  1167. rts = 0;
  1168. if (clear & TIOCM_DTR)
  1169. dtr = 0;
  1170. MoxaPortLineCtrl(ch, dtr, rts);
  1171. mutex_unlock(&moxa_openlock);
  1172. return 0;
  1173. }
  1174. static void moxa_throttle(struct tty_struct *tty)
  1175. {
  1176. struct moxa_port *ch = tty->driver_data;
  1177. ch->statusflags |= THROTTLE;
  1178. }
  1179. static void moxa_unthrottle(struct tty_struct *tty)
  1180. {
  1181. struct moxa_port *ch = tty->driver_data;
  1182. ch->statusflags &= ~THROTTLE;
  1183. }
  1184. static void moxa_set_termios(struct tty_struct *tty,
  1185. struct ktermios *old_termios)
  1186. {
  1187. struct moxa_port *ch = tty->driver_data;
  1188. if (ch == NULL)
  1189. return;
  1190. moxa_set_tty_param(tty, old_termios);
  1191. if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
  1192. wake_up_interruptible(&ch->open_wait);
  1193. }
  1194. static void moxa_stop(struct tty_struct *tty)
  1195. {
  1196. struct moxa_port *ch = tty->driver_data;
  1197. if (ch == NULL)
  1198. return;
  1199. MoxaPortTxDisable(ch);
  1200. ch->statusflags |= TXSTOPPED;
  1201. }
  1202. static void moxa_start(struct tty_struct *tty)
  1203. {
  1204. struct moxa_port *ch = tty->driver_data;
  1205. if (ch == NULL)
  1206. return;
  1207. if (!(ch->statusflags & TXSTOPPED))
  1208. return;
  1209. MoxaPortTxEnable(ch);
  1210. ch->statusflags &= ~TXSTOPPED;
  1211. }
  1212. static void moxa_hangup(struct tty_struct *tty)
  1213. {
  1214. struct moxa_port *ch;
  1215. mutex_lock(&moxa_openlock);
  1216. ch = tty->driver_data;
  1217. if (ch == NULL) {
  1218. mutex_unlock(&moxa_openlock);
  1219. return;
  1220. }
  1221. ch->count = 0;
  1222. moxa_close_port(ch);
  1223. mutex_unlock(&moxa_openlock);
  1224. wake_up_interruptible(&ch->open_wait);
  1225. }
  1226. static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
  1227. {
  1228. dcd = !!dcd;
  1229. if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
  1230. if (!dcd)
  1231. tty_hangup(p->tty);
  1232. }
  1233. p->DCDState = dcd;
  1234. }
  1235. static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
  1236. u16 __iomem *ip)
  1237. {
  1238. struct tty_struct *tty = p->tty;
  1239. void __iomem *ofsAddr;
  1240. unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
  1241. u16 intr;
  1242. if (tty) {
  1243. if ((p->statusflags & EMPTYWAIT) &&
  1244. MoxaPortTxQueue(p) == 0) {
  1245. p->statusflags &= ~EMPTYWAIT;
  1246. tty_wakeup(tty);
  1247. }
  1248. if ((p->statusflags & LOWWAIT) && !tty->stopped &&
  1249. MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
  1250. p->statusflags &= ~LOWWAIT;
  1251. tty_wakeup(tty);
  1252. }
  1253. if (inited && !(p->statusflags & THROTTLE) &&
  1254. MoxaPortRxQueue(p) > 0) { /* RX */
  1255. MoxaPortReadData(p);
  1256. tty_schedule_flip(tty);
  1257. }
  1258. } else {
  1259. p->statusflags &= ~EMPTYWAIT;
  1260. MoxaPortFlushData(p, 0); /* flush RX */
  1261. }
  1262. if (!handle) /* nothing else to do */
  1263. return 0;
  1264. intr = readw(ip); /* port irq status */
  1265. if (intr == 0)
  1266. return 0;
  1267. writew(0, ip); /* ACK port */
  1268. ofsAddr = p->tableAddr;
  1269. if (intr & IntrTx) /* disable tx intr */
  1270. writew(readw(ofsAddr + HostStat) & ~WakeupTx,
  1271. ofsAddr + HostStat);
  1272. if (!inited)
  1273. return 0;
  1274. if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
  1275. tty_insert_flip_char(tty, 0, TTY_BREAK);
  1276. tty_schedule_flip(tty);
  1277. }
  1278. if (intr & IntrLine)
  1279. moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
  1280. return 0;
  1281. }
  1282. static void moxa_poll(unsigned long ignored)
  1283. {
  1284. struct moxa_board_conf *brd;
  1285. u16 __iomem *ip;
  1286. unsigned int card, port, served = 0;
  1287. spin_lock(&moxa_lock);
  1288. for (card = 0; card < MAX_BOARDS; card++) {
  1289. brd = &moxa_boards[card];
  1290. if (!brd->ready)
  1291. continue;
  1292. served++;
  1293. ip = NULL;
  1294. if (readb(brd->intPend) == 0xff)
  1295. ip = brd->intTable + readb(brd->intNdx);
  1296. for (port = 0; port < brd->numPorts; port++)
  1297. moxa_poll_port(&brd->ports[port], !!ip, ip + port);
  1298. if (ip)
  1299. writeb(0, brd->intPend); /* ACK */
  1300. if (moxaLowWaterChk) {
  1301. struct moxa_port *p = brd->ports;
  1302. for (port = 0; port < brd->numPorts; port++, p++)
  1303. if (p->lowChkFlag) {
  1304. p->lowChkFlag = 0;
  1305. moxa_low_water_check(p->tableAddr);
  1306. }
  1307. }
  1308. }
  1309. moxaLowWaterChk = 0;
  1310. if (served)
  1311. mod_timer(&moxaTimer, jiffies + HZ / 50);
  1312. spin_unlock(&moxa_lock);
  1313. }
  1314. /******************************************************************************/
  1315. static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
  1316. {
  1317. register struct ktermios *ts = tty->termios;
  1318. struct moxa_port *ch = tty->driver_data;
  1319. int rts, cts, txflow, rxflow, xany, baud;
  1320. rts = cts = txflow = rxflow = xany = 0;
  1321. if (ts->c_cflag & CRTSCTS)
  1322. rts = cts = 1;
  1323. if (ts->c_iflag & IXON)
  1324. txflow = 1;
  1325. if (ts->c_iflag & IXOFF)
  1326. rxflow = 1;
  1327. if (ts->c_iflag & IXANY)
  1328. xany = 1;
  1329. /* Clear the features we don't support */
  1330. ts->c_cflag &= ~CMSPAR;
  1331. MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
  1332. baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
  1333. if (baud == -1)
  1334. baud = tty_termios_baud_rate(old_termios);
  1335. /* Not put the baud rate into the termios data */
  1336. tty_encode_baud_rate(tty, baud, baud);
  1337. }
  1338. static void moxa_setup_empty_event(struct tty_struct *tty)
  1339. {
  1340. struct moxa_port *ch = tty->driver_data;
  1341. spin_lock_bh(&moxa_lock);
  1342. ch->statusflags |= EMPTYWAIT;
  1343. spin_unlock_bh(&moxa_lock);
  1344. }
  1345. static void moxa_shut_down(struct moxa_port *ch)
  1346. {
  1347. struct tty_struct *tp = ch->tty;
  1348. if (!(ch->asyncflags & ASYNC_INITIALIZED))
  1349. return;
  1350. MoxaPortDisable(ch);
  1351. /*
  1352. * If we're a modem control device and HUPCL is on, drop RTS & DTR.
  1353. */
  1354. if (C_HUPCL(tp))
  1355. MoxaPortLineCtrl(ch, 0, 0);
  1356. spin_lock_bh(&moxa_lock);
  1357. ch->asyncflags &= ~ASYNC_INITIALIZED;
  1358. spin_unlock_bh(&moxa_lock);
  1359. }
  1360. /*****************************************************************************
  1361. * Driver level functions: *
  1362. *****************************************************************************/
  1363. static void MoxaPortFlushData(struct moxa_port *port, int mode)
  1364. {
  1365. void __iomem *ofsAddr;
  1366. if (mode < 0 || mode > 2)
  1367. return;
  1368. ofsAddr = port->tableAddr;
  1369. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1370. if (mode != 1) {
  1371. port->lowChkFlag = 0;
  1372. moxa_low_water_check(ofsAddr);
  1373. }
  1374. }
  1375. /*
  1376. * Moxa Port Number Description:
  1377. *
  1378. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1379. * the port number using in MOXA driver functions will be 0 to 31 for
  1380. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1381. * to 127 for fourth. For example, if you setup three MOXA boards,
  1382. * first board is C218, second board is C320-16 and third board is
  1383. * C320-32. The port number of first board (C218 - 8 ports) is from
  1384. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1385. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1386. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1387. * 127 will be invalid.
  1388. *
  1389. *
  1390. * Moxa Functions Description:
  1391. *
  1392. * Function 1: Driver initialization routine, this routine must be
  1393. * called when initialized driver.
  1394. * Syntax:
  1395. * void MoxaDriverInit();
  1396. *
  1397. *
  1398. * Function 2: Moxa driver private IOCTL command processing.
  1399. * Syntax:
  1400. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1401. *
  1402. * unsigned int cmd : IOCTL command
  1403. * unsigned long arg : IOCTL argument
  1404. * int port : port number (0 - 127)
  1405. *
  1406. * return: 0 (OK)
  1407. * -EINVAL
  1408. * -ENOIOCTLCMD
  1409. *
  1410. *
  1411. * Function 6: Enable this port to start Tx/Rx data.
  1412. * Syntax:
  1413. * void MoxaPortEnable(int port);
  1414. * int port : port number (0 - 127)
  1415. *
  1416. *
  1417. * Function 7: Disable this port
  1418. * Syntax:
  1419. * void MoxaPortDisable(int port);
  1420. * int port : port number (0 - 127)
  1421. *
  1422. *
  1423. * Function 8: Get the maximun available baud rate of this port.
  1424. * Syntax:
  1425. * long MoxaPortGetMaxBaud(int port);
  1426. * int port : port number (0 - 127)
  1427. *
  1428. * return: 0 : this port is invalid
  1429. * 38400/57600/115200 bps
  1430. *
  1431. *
  1432. * Function 10: Setting baud rate of this port.
  1433. * Syntax:
  1434. * long MoxaPortSetBaud(int port, long baud);
  1435. * int port : port number (0 - 127)
  1436. * long baud : baud rate (50 - 115200)
  1437. *
  1438. * return: 0 : this port is invalid or baud < 50
  1439. * 50 - 115200 : the real baud rate set to the port, if
  1440. * the argument baud is large than maximun
  1441. * available baud rate, the real setting
  1442. * baud rate will be the maximun baud rate.
  1443. *
  1444. *
  1445. * Function 12: Configure the port.
  1446. * Syntax:
  1447. * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
  1448. * int port : port number (0 - 127)
  1449. * struct ktermios * termio : termio structure pointer
  1450. * speed_t baud : baud rate
  1451. *
  1452. * return: -1 : this port is invalid or termio == NULL
  1453. * 0 : setting O.K.
  1454. *
  1455. *
  1456. * Function 13: Get the DTR/RTS state of this port.
  1457. * Syntax:
  1458. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1459. * int port : port number (0 - 127)
  1460. * int * dtrState : pointer to INT to receive the current DTR
  1461. * state. (if NULL, this function will not
  1462. * write to this address)
  1463. * int * rtsState : pointer to INT to receive the current RTS
  1464. * state. (if NULL, this function will not
  1465. * write to this address)
  1466. *
  1467. * return: -1 : this port is invalid
  1468. * 0 : O.K.
  1469. *
  1470. *
  1471. * Function 14: Setting the DTR/RTS output state of this port.
  1472. * Syntax:
  1473. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1474. * int port : port number (0 - 127)
  1475. * int dtrState : DTR output state (0: off, 1: on)
  1476. * int rtsState : RTS output state (0: off, 1: on)
  1477. *
  1478. *
  1479. * Function 15: Setting the flow control of this port.
  1480. * Syntax:
  1481. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1482. * int txFlow,int xany);
  1483. * int port : port number (0 - 127)
  1484. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1485. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1486. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1487. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1488. * int xany : S/W XANY flow control (0: no, 1: yes)
  1489. *
  1490. *
  1491. * Function 16: Get ths line status of this port
  1492. * Syntax:
  1493. * int MoxaPortLineStatus(int port);
  1494. * int port : port number (0 - 127)
  1495. *
  1496. * return: Bit 0 - CTS state (0: off, 1: on)
  1497. * Bit 1 - DSR state (0: off, 1: on)
  1498. * Bit 2 - DCD state (0: off, 1: on)
  1499. *
  1500. *
  1501. * Function 19: Flush the Rx/Tx buffer data of this port.
  1502. * Syntax:
  1503. * void MoxaPortFlushData(int port, int mode);
  1504. * int port : port number (0 - 127)
  1505. * int mode
  1506. * 0 : flush the Rx buffer
  1507. * 1 : flush the Tx buffer
  1508. * 2 : flush the Rx and Tx buffer
  1509. *
  1510. *
  1511. * Function 20: Write data.
  1512. * Syntax:
  1513. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1514. * int port : port number (0 - 127)
  1515. * unsigned char * buffer : pointer to write data buffer.
  1516. * int length : write data length
  1517. *
  1518. * return: 0 - length : real write data length
  1519. *
  1520. *
  1521. * Function 21: Read data.
  1522. * Syntax:
  1523. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1524. * int port : port number (0 - 127)
  1525. * struct tty_struct *tty : tty for data
  1526. *
  1527. * return: 0 - length : real read data length
  1528. *
  1529. *
  1530. * Function 24: Get the Tx buffer current queued data bytes
  1531. * Syntax:
  1532. * int MoxaPortTxQueue(int port);
  1533. * int port : port number (0 - 127)
  1534. *
  1535. * return: .. : Tx buffer current queued data bytes
  1536. *
  1537. *
  1538. * Function 25: Get the Tx buffer current free space
  1539. * Syntax:
  1540. * int MoxaPortTxFree(int port);
  1541. * int port : port number (0 - 127)
  1542. *
  1543. * return: .. : Tx buffer current free space
  1544. *
  1545. *
  1546. * Function 26: Get the Rx buffer current queued data bytes
  1547. * Syntax:
  1548. * int MoxaPortRxQueue(int port);
  1549. * int port : port number (0 - 127)
  1550. *
  1551. * return: .. : Rx buffer current queued data bytes
  1552. *
  1553. *
  1554. * Function 28: Disable port data transmission.
  1555. * Syntax:
  1556. * void MoxaPortTxDisable(int port);
  1557. * int port : port number (0 - 127)
  1558. *
  1559. *
  1560. * Function 29: Enable port data transmission.
  1561. * Syntax:
  1562. * void MoxaPortTxEnable(int port);
  1563. * int port : port number (0 - 127)
  1564. *
  1565. *
  1566. * Function 31: Get the received BREAK signal count and reset it.
  1567. * Syntax:
  1568. * int MoxaPortResetBrkCnt(int port);
  1569. * int port : port number (0 - 127)
  1570. *
  1571. * return: 0 - .. : BREAK signal count
  1572. *
  1573. *
  1574. */
  1575. static void MoxaPortEnable(struct moxa_port *port)
  1576. {
  1577. void __iomem *ofsAddr;
  1578. u16 lowwater = 512;
  1579. ofsAddr = port->tableAddr;
  1580. writew(lowwater, ofsAddr + Low_water);
  1581. if (port->board->boardType == MOXA_BOARD_C320_ISA ||
  1582. port->board->boardType == MOXA_BOARD_C320_PCI)
  1583. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1584. else
  1585. writew(readw(ofsAddr + HostStat) | WakeupBreak,
  1586. ofsAddr + HostStat);
  1587. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1588. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1589. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1590. MoxaPortLineStatus(port);
  1591. }
  1592. static void MoxaPortDisable(struct moxa_port *port)
  1593. {
  1594. void __iomem *ofsAddr = port->tableAddr;
  1595. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1596. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1597. writew(0, ofsAddr + HostStat);
  1598. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1599. }
  1600. static long MoxaPortGetMaxBaud(struct moxa_port *port)
  1601. {
  1602. if (port->board->boardType == MOXA_BOARD_C320_ISA ||
  1603. port->board->boardType == MOXA_BOARD_C320_PCI)
  1604. return 460800L;
  1605. else
  1606. return 921600L;
  1607. }
  1608. static long MoxaPortSetBaud(struct moxa_port *port, long baud)
  1609. {
  1610. void __iomem *ofsAddr;
  1611. long max, clock;
  1612. unsigned int val;
  1613. if (baud < 50L || (max = MoxaPortGetMaxBaud(port)) == 0)
  1614. return 0;
  1615. ofsAddr = port->tableAddr;
  1616. if (baud > max)
  1617. baud = max;
  1618. if (max == 38400L)
  1619. clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
  1620. else if (max == 57600L)
  1621. clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
  1622. else
  1623. clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
  1624. val = clock / baud;
  1625. moxafunc(ofsAddr, FC_SetBaud, val);
  1626. baud = clock / val;
  1627. return baud;
  1628. }
  1629. static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
  1630. speed_t baud)
  1631. {
  1632. void __iomem *ofsAddr;
  1633. tcflag_t cflag;
  1634. tcflag_t mode = 0;
  1635. ofsAddr = port->tableAddr;
  1636. cflag = termio->c_cflag; /* termio->c_cflag */
  1637. mode = termio->c_cflag & CSIZE;
  1638. if (mode == CS5)
  1639. mode = MX_CS5;
  1640. else if (mode == CS6)
  1641. mode = MX_CS6;
  1642. else if (mode == CS7)
  1643. mode = MX_CS7;
  1644. else if (mode == CS8)
  1645. mode = MX_CS8;
  1646. if (termio->c_cflag & CSTOPB) {
  1647. if (mode == MX_CS5)
  1648. mode |= MX_STOP15;
  1649. else
  1650. mode |= MX_STOP2;
  1651. } else
  1652. mode |= MX_STOP1;
  1653. if (termio->c_cflag & PARENB) {
  1654. if (termio->c_cflag & PARODD)
  1655. mode |= MX_PARODD;
  1656. else
  1657. mode |= MX_PAREVEN;
  1658. } else
  1659. mode |= MX_PARNONE;
  1660. moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
  1661. if (port->board->boardType == MOXA_BOARD_C320_ISA ||
  1662. port->board->boardType == MOXA_BOARD_C320_PCI) {
  1663. if (baud >= 921600L)
  1664. return -1;
  1665. }
  1666. baud = MoxaPortSetBaud(port, baud);
  1667. if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
  1668. writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
  1669. writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
  1670. writeb(FC_SetXonXoff, ofsAddr + FuncCode);
  1671. moxa_wait_finish(ofsAddr);
  1672. }
  1673. return baud;
  1674. }
  1675. static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
  1676. int *rtsState)
  1677. {
  1678. if (dtrState)
  1679. *dtrState = !!(port->lineCtrl & DTR_ON);
  1680. if (rtsState)
  1681. *rtsState = !!(port->lineCtrl & RTS_ON);
  1682. return 0;
  1683. }
  1684. static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
  1685. {
  1686. u8 mode = 0;
  1687. if (dtr)
  1688. mode |= DTR_ON;
  1689. if (rts)
  1690. mode |= RTS_ON;
  1691. port->lineCtrl = mode;
  1692. moxafunc(port->tableAddr, FC_LineControl, mode);
  1693. }
  1694. static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
  1695. int txflow, int rxflow, int txany)
  1696. {
  1697. int mode = 0;
  1698. if (rts)
  1699. mode |= RTS_FlowCtl;
  1700. if (cts)
  1701. mode |= CTS_FlowCtl;
  1702. if (txflow)
  1703. mode |= Tx_FlowCtl;
  1704. if (rxflow)
  1705. mode |= Rx_FlowCtl;
  1706. if (txany)
  1707. mode |= IXM_IXANY;
  1708. moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
  1709. }
  1710. static int MoxaPortLineStatus(struct moxa_port *port)
  1711. {
  1712. void __iomem *ofsAddr;
  1713. int val;
  1714. ofsAddr = port->tableAddr;
  1715. if (port->board->boardType == MOXA_BOARD_C320_ISA ||
  1716. port->board->boardType == MOXA_BOARD_C320_PCI) {
  1717. moxafunc(ofsAddr, FC_LineStatus, 0);
  1718. val = readw(ofsAddr + FuncArg);
  1719. } else {
  1720. val = readw(ofsAddr + FlagStat) >> 4;
  1721. }
  1722. val &= 0x0B;
  1723. if (val & 8)
  1724. val |= 4;
  1725. spin_lock_bh(&moxa_lock);
  1726. moxa_new_dcdstate(port, val & 8);
  1727. spin_unlock_bh(&moxa_lock);
  1728. val &= 7;
  1729. return val;
  1730. }
  1731. static int MoxaPortWriteData(struct moxa_port *port,
  1732. const unsigned char *buffer, int len)
  1733. {
  1734. void __iomem *baseAddr, *ofsAddr, *ofs;
  1735. unsigned int c, total;
  1736. u16 head, tail, tx_mask, spage, epage;
  1737. u16 pageno, pageofs, bufhead;
  1738. ofsAddr = port->tableAddr;
  1739. baseAddr = port->board->basemem;
  1740. tx_mask = readw(ofsAddr + TX_mask);
  1741. spage = readw(ofsAddr + Page_txb);
  1742. epage = readw(ofsAddr + EndPage_txb);
  1743. tail = readw(ofsAddr + TXwptr);
  1744. head = readw(ofsAddr + TXrptr);
  1745. c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
  1746. if (c > len)
  1747. c = len;
  1748. moxaLog.txcnt[port->tty->index] += c;
  1749. total = c;
  1750. if (spage == epage) {
  1751. bufhead = readw(ofsAddr + Ofs_txb);
  1752. writew(spage, baseAddr + Control_reg);
  1753. while (c > 0) {
  1754. if (head > tail)
  1755. len = head - tail - 1;
  1756. else
  1757. len = tx_mask + 1 - tail;
  1758. len = (c > len) ? len : c;
  1759. ofs = baseAddr + DynPage_addr + bufhead + tail;
  1760. memcpy_toio(ofs, buffer, len);
  1761. buffer += len;
  1762. tail = (tail + len) & tx_mask;
  1763. c -= len;
  1764. }
  1765. } else {
  1766. pageno = spage + (tail >> 13);
  1767. pageofs = tail & Page_mask;
  1768. while (c > 0) {
  1769. len = Page_size - pageofs;
  1770. if (len > c)
  1771. len = c;
  1772. writeb(pageno, baseAddr + Control_reg);
  1773. ofs = baseAddr + DynPage_addr + pageofs;
  1774. memcpy_toio(ofs, buffer, len);
  1775. buffer += len;
  1776. if (++pageno == epage)
  1777. pageno = spage;
  1778. pageofs = 0;
  1779. c -= len;
  1780. }
  1781. tail = (tail + total) & tx_mask;
  1782. }
  1783. writew(tail, ofsAddr + TXwptr);
  1784. writeb(1, ofsAddr + CD180TXirq); /* start to send */
  1785. return total;
  1786. }
  1787. static int MoxaPortReadData(struct moxa_port *port)
  1788. {
  1789. struct tty_struct *tty = port->tty;
  1790. unsigned char *dst;
  1791. void __iomem *baseAddr, *ofsAddr, *ofs;
  1792. unsigned int count, len, total;
  1793. u16 tail, rx_mask, spage, epage;
  1794. u16 pageno, pageofs, bufhead, head;
  1795. ofsAddr = port->tableAddr;
  1796. baseAddr = port->board->basemem;
  1797. head = readw(ofsAddr + RXrptr);
  1798. tail = readw(ofsAddr + RXwptr);
  1799. rx_mask = readw(ofsAddr + RX_mask);
  1800. spage = readw(ofsAddr + Page_rxb);
  1801. epage = readw(ofsAddr + EndPage_rxb);
  1802. count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
  1803. if (count == 0)
  1804. return 0;
  1805. total = count;
  1806. moxaLog.rxcnt[tty->index] += total;
  1807. if (spage == epage) {
  1808. bufhead = readw(ofsAddr + Ofs_rxb);
  1809. writew(spage, baseAddr + Control_reg);
  1810. while (count > 0) {
  1811. ofs = baseAddr + DynPage_addr + bufhead + head;
  1812. len = (tail >= head) ? (tail - head) :
  1813. (rx_mask + 1 - head);
  1814. len = tty_prepare_flip_string(tty, &dst,
  1815. min(len, count));
  1816. memcpy_fromio(dst, ofs, len);
  1817. head = (head + len) & rx_mask;
  1818. count -= len;
  1819. }
  1820. } else {
  1821. pageno = spage + (head >> 13);
  1822. pageofs = head & Page_mask;
  1823. while (count > 0) {
  1824. writew(pageno, baseAddr + Control_reg);
  1825. ofs = baseAddr + DynPage_addr + pageofs;
  1826. len = tty_prepare_flip_string(tty, &dst,
  1827. min(Page_size - pageofs, count));
  1828. memcpy_fromio(dst, ofs, len);
  1829. count -= len;
  1830. pageofs = (pageofs + len) & Page_mask;
  1831. if (pageofs == 0 && ++pageno == epage)
  1832. pageno = spage;
  1833. }
  1834. head = (head + total) & rx_mask;
  1835. }
  1836. writew(head, ofsAddr + RXrptr);
  1837. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  1838. moxaLowWaterChk = 1;
  1839. port->lowChkFlag = 1;
  1840. }
  1841. return total;
  1842. }
  1843. static int MoxaPortTxQueue(struct moxa_port *port)
  1844. {
  1845. void __iomem *ofsAddr = port->tableAddr;
  1846. u16 rptr, wptr, mask;
  1847. rptr = readw(ofsAddr + TXrptr);
  1848. wptr = readw(ofsAddr + TXwptr);
  1849. mask = readw(ofsAddr + TX_mask);
  1850. return (wptr - rptr) & mask;
  1851. }
  1852. static int MoxaPortTxFree(struct moxa_port *port)
  1853. {
  1854. void __iomem *ofsAddr = port->tableAddr;
  1855. u16 rptr, wptr, mask;
  1856. rptr = readw(ofsAddr + TXrptr);
  1857. wptr = readw(ofsAddr + TXwptr);
  1858. mask = readw(ofsAddr + TX_mask);
  1859. return mask - ((wptr - rptr) & mask);
  1860. }
  1861. static int MoxaPortRxQueue(struct moxa_port *port)
  1862. {
  1863. void __iomem *ofsAddr = port->tableAddr;
  1864. u16 rptr, wptr, mask;
  1865. rptr = readw(ofsAddr + RXrptr);
  1866. wptr = readw(ofsAddr + RXwptr);
  1867. mask = readw(ofsAddr + RX_mask);
  1868. return (wptr - rptr) & mask;
  1869. }
  1870. static void MoxaPortTxDisable(struct moxa_port *port)
  1871. {
  1872. moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
  1873. }
  1874. static void MoxaPortTxEnable(struct moxa_port *port)
  1875. {
  1876. moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
  1877. }
  1878. static int moxa_get_serial_info(struct moxa_port *info,
  1879. struct serial_struct __user *retinfo)
  1880. {
  1881. struct serial_struct tmp = {
  1882. .type = info->type,
  1883. .line = info->tty->index,
  1884. .flags = info->asyncflags,
  1885. .baud_base = 921600,
  1886. .close_delay = info->close_delay
  1887. };
  1888. return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
  1889. }
  1890. static int moxa_set_serial_info(struct moxa_port *info,
  1891. struct serial_struct __user *new_info)
  1892. {
  1893. struct serial_struct new_serial;
  1894. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1895. return -EFAULT;
  1896. if (new_serial.irq != 0 || new_serial.port != 0 ||
  1897. new_serial.custom_divisor != 0 ||
  1898. new_serial.baud_base != 921600)
  1899. return -EPERM;
  1900. if (!capable(CAP_SYS_ADMIN)) {
  1901. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  1902. (info->asyncflags & ~ASYNC_USR_MASK)))
  1903. return -EPERM;
  1904. } else
  1905. info->close_delay = new_serial.close_delay * HZ / 100;
  1906. new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
  1907. new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
  1908. MoxaSetFifo(info, new_serial.type == PORT_16550A);
  1909. info->type = new_serial.type;
  1910. return 0;
  1911. }
  1912. /*****************************************************************************
  1913. * Static local functions: *
  1914. *****************************************************************************/
  1915. static void MoxaSetFifo(struct moxa_port *port, int enable)
  1916. {
  1917. void __iomem *ofsAddr = port->tableAddr;
  1918. if (!enable) {
  1919. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
  1920. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
  1921. } else {
  1922. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
  1923. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
  1924. }
  1925. }