moxa.c 52 KB

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