moxa.c 54 KB

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