moxa.c 52 KB

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