moxa.c 52 KB

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