moxa.c 52 KB

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