moxa.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*****************************************************************************/
  3. /*
  4. * moxa.c -- MOXA Intellio family multiport serial driver.
  5. *
  6. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
  7. * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  8. *
  9. * This code is loosely based on the Linux serial driver, written by
  10. * Linus Torvalds, Theodore T'so and others.
  11. */
  12. /*
  13. * MOXA Intellio Series Driver
  14. * for : LINUX
  15. * date : 1999/1/7
  16. * version : 5.1
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/mm.h>
  21. #include <linux/ioport.h>
  22. #include <linux/errno.h>
  23. #include <linux/firmware.h>
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/timer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/major.h>
  31. #include <linux/string.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/serial.h>
  35. #include <linux/tty_driver.h>
  36. #include <linux/delay.h>
  37. #include <linux/pci.h>
  38. #include <linux/init.h>
  39. #include <linux/bitops.h>
  40. #include <linux/slab.h>
  41. #include <linux/ratelimit.h>
  42. #include <asm/io.h>
  43. #include <linux/uaccess.h>
  44. #include "moxa.h"
  45. #define MOXA_VERSION "6.0k"
  46. #define MOXA_FW_HDRLEN 32
  47. #define MOXAMAJOR 172
  48. #define MAX_BOARDS 4 /* Don't change this value */
  49. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  50. #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
  51. #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
  52. (brd)->boardType == MOXA_BOARD_C320_PCI)
  53. /*
  54. * Define the Moxa PCI vendor and device IDs.
  55. */
  56. #define MOXA_BUS_TYPE_ISA 0
  57. #define MOXA_BUS_TYPE_PCI 1
  58. enum {
  59. MOXA_BOARD_C218_PCI = 1,
  60. MOXA_BOARD_C218_ISA,
  61. MOXA_BOARD_C320_PCI,
  62. MOXA_BOARD_C320_ISA,
  63. MOXA_BOARD_CP204J,
  64. };
  65. static char *moxa_brdname[] =
  66. {
  67. "C218 Turbo PCI series",
  68. "C218 Turbo ISA series",
  69. "C320 Turbo PCI series",
  70. "C320 Turbo ISA series",
  71. "CP-204J series",
  72. };
  73. #ifdef CONFIG_PCI
  74. static const struct pci_device_id moxa_pcibrds[] = {
  75. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
  76. .driver_data = MOXA_BOARD_C218_PCI },
  77. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
  78. .driver_data = MOXA_BOARD_C320_PCI },
  79. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
  80. .driver_data = MOXA_BOARD_CP204J },
  81. { 0 }
  82. };
  83. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  84. #endif /* CONFIG_PCI */
  85. struct moxa_port;
  86. static struct moxa_board_conf {
  87. int boardType;
  88. int numPorts;
  89. int busType;
  90. unsigned int ready;
  91. struct moxa_port *ports;
  92. void __iomem *basemem;
  93. void __iomem *intNdx;
  94. void __iomem *intPend;
  95. void __iomem *intTable;
  96. } moxa_boards[MAX_BOARDS];
  97. struct mxser_mstatus {
  98. tcflag_t cflag;
  99. int cts;
  100. int dsr;
  101. int ri;
  102. int dcd;
  103. };
  104. struct moxaq_str {
  105. int inq;
  106. int outq;
  107. };
  108. struct moxa_port {
  109. struct tty_port port;
  110. struct moxa_board_conf *board;
  111. void __iomem *tableAddr;
  112. int type;
  113. int cflag;
  114. unsigned long statusflags;
  115. u8 DCDState; /* Protected by the port lock */
  116. u8 lineCtrl;
  117. u8 lowChkFlag;
  118. };
  119. struct mon_str {
  120. int tick;
  121. int rxcnt[MAX_PORTS];
  122. int txcnt[MAX_PORTS];
  123. };
  124. /* statusflags */
  125. #define TXSTOPPED 1
  126. #define LOWWAIT 2
  127. #define EMPTYWAIT 3
  128. #define WAKEUP_CHARS 256
  129. static int ttymajor = MOXAMAJOR;
  130. static struct mon_str moxaLog;
  131. static unsigned int moxaFuncTout = HZ / 2;
  132. static unsigned int moxaLowWaterChk;
  133. static DEFINE_MUTEX(moxa_openlock);
  134. static DEFINE_SPINLOCK(moxa_lock);
  135. static unsigned long baseaddr[MAX_BOARDS];
  136. static unsigned int type[MAX_BOARDS];
  137. static unsigned int numports[MAX_BOARDS];
  138. static struct tty_port moxa_service_port;
  139. MODULE_AUTHOR("William Chen");
  140. MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
  141. MODULE_LICENSE("GPL");
  142. MODULE_FIRMWARE("c218tunx.cod");
  143. MODULE_FIRMWARE("cp204unx.cod");
  144. MODULE_FIRMWARE("c320tunx.cod");
  145. module_param_array(type, uint, NULL, 0);
  146. MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
  147. module_param_hw_array(baseaddr, ulong, ioport, 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);
  166. static int moxa_tiocmset(struct tty_struct *tty,
  167. unsigned int set, unsigned int clear);
  168. static void moxa_poll(struct timer_list *);
  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)
  205. printk_ratelimited(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,
  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)
  317. tmp.cflag = p->cflag;
  318. else
  319. tmp.cflag = ttyp->termios.c_cflag;
  320. tty_kref_put(ttyp);
  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);
  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, first_idx;
  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. first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
  779. for (i = 0; i < brd->numPorts; i++)
  780. tty_port_register_device(&brd->ports[i].port, moxaDriver,
  781. first_idx + i, dev);
  782. return 0;
  783. err_free:
  784. for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
  785. tty_port_destroy(&brd->ports[i].port);
  786. kfree(brd->ports);
  787. err:
  788. return ret;
  789. }
  790. static void moxa_board_deinit(struct moxa_board_conf *brd)
  791. {
  792. unsigned int a, opened, first_idx;
  793. mutex_lock(&moxa_openlock);
  794. spin_lock_bh(&moxa_lock);
  795. brd->ready = 0;
  796. spin_unlock_bh(&moxa_lock);
  797. /* pci hot-un-plug support */
  798. for (a = 0; a < brd->numPorts; a++)
  799. if (tty_port_initialized(&brd->ports[a].port))
  800. tty_port_tty_hangup(&brd->ports[a].port, false);
  801. for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
  802. tty_port_destroy(&brd->ports[a].port);
  803. while (1) {
  804. opened = 0;
  805. for (a = 0; a < brd->numPorts; a++)
  806. if (tty_port_initialized(&brd->ports[a].port))
  807. opened++;
  808. mutex_unlock(&moxa_openlock);
  809. if (!opened)
  810. break;
  811. msleep(50);
  812. mutex_lock(&moxa_openlock);
  813. }
  814. first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
  815. for (a = 0; a < brd->numPorts; a++)
  816. tty_unregister_device(moxaDriver, first_idx + a);
  817. iounmap(brd->basemem);
  818. brd->basemem = NULL;
  819. kfree(brd->ports);
  820. }
  821. #ifdef CONFIG_PCI
  822. static int moxa_pci_probe(struct pci_dev *pdev,
  823. const struct pci_device_id *ent)
  824. {
  825. struct moxa_board_conf *board;
  826. unsigned int i;
  827. int board_type = ent->driver_data;
  828. int retval;
  829. retval = pci_enable_device(pdev);
  830. if (retval) {
  831. dev_err(&pdev->dev, "can't enable pci device\n");
  832. goto err;
  833. }
  834. for (i = 0; i < MAX_BOARDS; i++)
  835. if (moxa_boards[i].basemem == NULL)
  836. break;
  837. retval = -ENODEV;
  838. if (i >= MAX_BOARDS) {
  839. dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
  840. "found. Board is ignored.\n", MAX_BOARDS);
  841. goto err;
  842. }
  843. board = &moxa_boards[i];
  844. retval = pci_request_region(pdev, 2, "moxa-base");
  845. if (retval) {
  846. dev_err(&pdev->dev, "can't request pci region 2\n");
  847. goto err;
  848. }
  849. board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
  850. if (board->basemem == NULL) {
  851. dev_err(&pdev->dev, "can't remap io space 2\n");
  852. retval = -ENOMEM;
  853. goto err_reg;
  854. }
  855. board->boardType = board_type;
  856. switch (board_type) {
  857. case MOXA_BOARD_C218_ISA:
  858. case MOXA_BOARD_C218_PCI:
  859. board->numPorts = 8;
  860. break;
  861. case MOXA_BOARD_CP204J:
  862. board->numPorts = 4;
  863. break;
  864. default:
  865. board->numPorts = 0;
  866. break;
  867. }
  868. board->busType = MOXA_BUS_TYPE_PCI;
  869. retval = moxa_init_board(board, &pdev->dev);
  870. if (retval)
  871. goto err_base;
  872. pci_set_drvdata(pdev, board);
  873. dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
  874. moxa_brdname[board_type - 1], board->numPorts);
  875. return 0;
  876. err_base:
  877. iounmap(board->basemem);
  878. board->basemem = NULL;
  879. err_reg:
  880. pci_release_region(pdev, 2);
  881. err:
  882. return retval;
  883. }
  884. static void moxa_pci_remove(struct pci_dev *pdev)
  885. {
  886. struct moxa_board_conf *brd = pci_get_drvdata(pdev);
  887. moxa_board_deinit(brd);
  888. pci_release_region(pdev, 2);
  889. }
  890. static struct pci_driver moxa_pci_driver = {
  891. .name = "moxa",
  892. .id_table = moxa_pcibrds,
  893. .probe = moxa_pci_probe,
  894. .remove = moxa_pci_remove
  895. };
  896. #endif /* CONFIG_PCI */
  897. static int __init moxa_init(void)
  898. {
  899. unsigned int isabrds = 0;
  900. int retval = 0;
  901. struct moxa_board_conf *brd = moxa_boards;
  902. unsigned int i;
  903. printk(KERN_INFO "MOXA Intellio family driver version %s\n",
  904. MOXA_VERSION);
  905. tty_port_init(&moxa_service_port);
  906. moxaDriver = tty_alloc_driver(MAX_PORTS + 1,
  907. TTY_DRIVER_REAL_RAW |
  908. TTY_DRIVER_DYNAMIC_DEV);
  909. if (IS_ERR(moxaDriver))
  910. return PTR_ERR(moxaDriver);
  911. moxaDriver->name = "ttyMX";
  912. moxaDriver->major = ttymajor;
  913. moxaDriver->minor_start = 0;
  914. moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
  915. moxaDriver->subtype = SERIAL_TYPE_NORMAL;
  916. moxaDriver->init_termios = tty_std_termios;
  917. moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  918. moxaDriver->init_termios.c_ispeed = 9600;
  919. moxaDriver->init_termios.c_ospeed = 9600;
  920. tty_set_operations(moxaDriver, &moxa_ops);
  921. /* Having one more port only for ioctls is ugly */
  922. tty_port_link_device(&moxa_service_port, moxaDriver, MAX_PORTS);
  923. if (tty_register_driver(moxaDriver)) {
  924. printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
  925. put_tty_driver(moxaDriver);
  926. return -1;
  927. }
  928. /* Find the boards defined from module args. */
  929. for (i = 0; i < MAX_BOARDS; i++) {
  930. if (!baseaddr[i])
  931. break;
  932. if (type[i] == MOXA_BOARD_C218_ISA ||
  933. type[i] == MOXA_BOARD_C320_ISA) {
  934. pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
  935. isabrds + 1, moxa_brdname[type[i] - 1],
  936. baseaddr[i]);
  937. brd->boardType = type[i];
  938. brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
  939. numports[i];
  940. brd->busType = MOXA_BUS_TYPE_ISA;
  941. brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
  942. if (!brd->basemem) {
  943. printk(KERN_ERR "MOXA: can't remap %lx\n",
  944. baseaddr[i]);
  945. continue;
  946. }
  947. if (moxa_init_board(brd, NULL)) {
  948. iounmap(brd->basemem);
  949. brd->basemem = NULL;
  950. continue;
  951. }
  952. printk(KERN_INFO "MOXA isa board found at 0x%.8lx and "
  953. "ready (%u ports, firmware loaded)\n",
  954. baseaddr[i], brd->numPorts);
  955. brd++;
  956. isabrds++;
  957. }
  958. }
  959. #ifdef CONFIG_PCI
  960. retval = pci_register_driver(&moxa_pci_driver);
  961. if (retval) {
  962. printk(KERN_ERR "Can't register MOXA pci driver!\n");
  963. if (isabrds)
  964. retval = 0;
  965. }
  966. #endif
  967. return retval;
  968. }
  969. static void __exit moxa_exit(void)
  970. {
  971. unsigned int i;
  972. #ifdef CONFIG_PCI
  973. pci_unregister_driver(&moxa_pci_driver);
  974. #endif
  975. for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
  976. if (moxa_boards[i].ready)
  977. moxa_board_deinit(&moxa_boards[i]);
  978. del_timer_sync(&moxaTimer);
  979. if (tty_unregister_driver(moxaDriver))
  980. printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
  981. "serial driver\n");
  982. put_tty_driver(moxaDriver);
  983. }
  984. module_init(moxa_init);
  985. module_exit(moxa_exit);
  986. static void moxa_shutdown(struct tty_port *port)
  987. {
  988. struct moxa_port *ch = container_of(port, struct moxa_port, port);
  989. MoxaPortDisable(ch);
  990. MoxaPortFlushData(ch, 2);
  991. }
  992. static int moxa_carrier_raised(struct tty_port *port)
  993. {
  994. struct moxa_port *ch = container_of(port, struct moxa_port, port);
  995. int dcd;
  996. spin_lock_irq(&port->lock);
  997. dcd = ch->DCDState;
  998. spin_unlock_irq(&port->lock);
  999. return dcd;
  1000. }
  1001. static void moxa_dtr_rts(struct tty_port *port, int onoff)
  1002. {
  1003. struct moxa_port *ch = container_of(port, struct moxa_port, port);
  1004. MoxaPortLineCtrl(ch, onoff, onoff);
  1005. }
  1006. static int moxa_open(struct tty_struct *tty, struct file *filp)
  1007. {
  1008. struct moxa_board_conf *brd;
  1009. struct moxa_port *ch;
  1010. int port;
  1011. port = tty->index;
  1012. if (port == MAX_PORTS) {
  1013. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  1014. }
  1015. if (mutex_lock_interruptible(&moxa_openlock))
  1016. return -ERESTARTSYS;
  1017. brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
  1018. if (!brd->ready) {
  1019. mutex_unlock(&moxa_openlock);
  1020. return -ENODEV;
  1021. }
  1022. if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
  1023. mutex_unlock(&moxa_openlock);
  1024. return -ENODEV;
  1025. }
  1026. ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
  1027. ch->port.count++;
  1028. tty->driver_data = ch;
  1029. tty_port_tty_set(&ch->port, tty);
  1030. mutex_lock(&ch->port.mutex);
  1031. if (!tty_port_initialized(&ch->port)) {
  1032. ch->statusflags = 0;
  1033. moxa_set_tty_param(tty, &tty->termios);
  1034. MoxaPortLineCtrl(ch, 1, 1);
  1035. MoxaPortEnable(ch);
  1036. MoxaSetFifo(ch, ch->type == PORT_16550A);
  1037. tty_port_set_initialized(&ch->port, 1);
  1038. }
  1039. mutex_unlock(&ch->port.mutex);
  1040. mutex_unlock(&moxa_openlock);
  1041. return tty_port_block_til_ready(&ch->port, tty, filp);
  1042. }
  1043. static void moxa_close(struct tty_struct *tty, struct file *filp)
  1044. {
  1045. struct moxa_port *ch = tty->driver_data;
  1046. ch->cflag = tty->termios.c_cflag;
  1047. tty_port_close(&ch->port, tty, filp);
  1048. }
  1049. static int moxa_write(struct tty_struct *tty,
  1050. const unsigned char *buf, int count)
  1051. {
  1052. struct moxa_port *ch = tty->driver_data;
  1053. unsigned long flags;
  1054. int len;
  1055. if (ch == NULL)
  1056. return 0;
  1057. spin_lock_irqsave(&moxa_lock, flags);
  1058. len = MoxaPortWriteData(tty, buf, count);
  1059. spin_unlock_irqrestore(&moxa_lock, flags);
  1060. set_bit(LOWWAIT, &ch->statusflags);
  1061. return len;
  1062. }
  1063. static int moxa_write_room(struct tty_struct *tty)
  1064. {
  1065. struct moxa_port *ch;
  1066. if (tty->stopped)
  1067. return 0;
  1068. ch = tty->driver_data;
  1069. if (ch == NULL)
  1070. return 0;
  1071. return MoxaPortTxFree(ch);
  1072. }
  1073. static void moxa_flush_buffer(struct tty_struct *tty)
  1074. {
  1075. struct moxa_port *ch = tty->driver_data;
  1076. if (ch == NULL)
  1077. return;
  1078. MoxaPortFlushData(ch, 1);
  1079. tty_wakeup(tty);
  1080. }
  1081. static int moxa_chars_in_buffer(struct tty_struct *tty)
  1082. {
  1083. struct moxa_port *ch = tty->driver_data;
  1084. int chars;
  1085. chars = MoxaPortTxQueue(ch);
  1086. if (chars)
  1087. /*
  1088. * Make it possible to wakeup anything waiting for output
  1089. * in tty_ioctl.c, etc.
  1090. */
  1091. set_bit(EMPTYWAIT, &ch->statusflags);
  1092. return chars;
  1093. }
  1094. static int moxa_tiocmget(struct tty_struct *tty)
  1095. {
  1096. struct moxa_port *ch = tty->driver_data;
  1097. int flag = 0, dtr, rts;
  1098. MoxaPortGetLineOut(ch, &dtr, &rts);
  1099. if (dtr)
  1100. flag |= TIOCM_DTR;
  1101. if (rts)
  1102. flag |= TIOCM_RTS;
  1103. dtr = MoxaPortLineStatus(ch);
  1104. if (dtr & 1)
  1105. flag |= TIOCM_CTS;
  1106. if (dtr & 2)
  1107. flag |= TIOCM_DSR;
  1108. if (dtr & 4)
  1109. flag |= TIOCM_CD;
  1110. return flag;
  1111. }
  1112. static int moxa_tiocmset(struct tty_struct *tty,
  1113. unsigned int set, unsigned int clear)
  1114. {
  1115. struct moxa_port *ch;
  1116. int dtr, rts;
  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 (!test_bit(TXSTOPPED, &ch->statusflags))
  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. unsigned long flags;
  1172. dcd = !!dcd;
  1173. spin_lock_irqsave(&p->port.lock, flags);
  1174. if (dcd != p->DCDState) {
  1175. p->DCDState = dcd;
  1176. spin_unlock_irqrestore(&p->port.lock, flags);
  1177. if (!dcd)
  1178. tty_port_tty_hangup(&p->port, true);
  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 = tty_port_initialized(&p->port);
  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 && !tty_throttled(tty) &&
  1202. MoxaPortRxQueue(p) > 0) { /* RX */
  1203. MoxaPortReadData(p);
  1204. tty_schedule_flip(&p->port);
  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(&p->port, 0, TTY_BREAK);
  1224. tty_schedule_flip(&p->port);
  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(struct timer_list *unused)
  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. MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
  1280. baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
  1281. if (baud == -1)
  1282. baud = tty_termios_baud_rate(old_termios);
  1283. /* Not put the baud rate into the termios data */
  1284. tty_encode_baud_rate(tty, baud, baud);
  1285. }
  1286. /*****************************************************************************
  1287. * Driver level functions: *
  1288. *****************************************************************************/
  1289. static void MoxaPortFlushData(struct moxa_port *port, int mode)
  1290. {
  1291. void __iomem *ofsAddr;
  1292. if (mode < 0 || mode > 2)
  1293. return;
  1294. ofsAddr = port->tableAddr;
  1295. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1296. if (mode != 1) {
  1297. port->lowChkFlag = 0;
  1298. moxa_low_water_check(ofsAddr);
  1299. }
  1300. }
  1301. /*
  1302. * Moxa Port Number Description:
  1303. *
  1304. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1305. * the port number using in MOXA driver functions will be 0 to 31 for
  1306. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1307. * to 127 for fourth. For example, if you setup three MOXA boards,
  1308. * first board is C218, second board is C320-16 and third board is
  1309. * C320-32. The port number of first board (C218 - 8 ports) is from
  1310. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1311. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1312. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1313. * 127 will be invalid.
  1314. *
  1315. *
  1316. * Moxa Functions Description:
  1317. *
  1318. * Function 1: Driver initialization routine, this routine must be
  1319. * called when initialized driver.
  1320. * Syntax:
  1321. * void MoxaDriverInit();
  1322. *
  1323. *
  1324. * Function 2: Moxa driver private IOCTL command processing.
  1325. * Syntax:
  1326. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1327. *
  1328. * unsigned int cmd : IOCTL command
  1329. * unsigned long arg : IOCTL argument
  1330. * int port : port number (0 - 127)
  1331. *
  1332. * return: 0 (OK)
  1333. * -EINVAL
  1334. * -ENOIOCTLCMD
  1335. *
  1336. *
  1337. * Function 6: Enable this port to start Tx/Rx data.
  1338. * Syntax:
  1339. * void MoxaPortEnable(int port);
  1340. * int port : port number (0 - 127)
  1341. *
  1342. *
  1343. * Function 7: Disable this port
  1344. * Syntax:
  1345. * void MoxaPortDisable(int port);
  1346. * int port : port number (0 - 127)
  1347. *
  1348. *
  1349. * Function 10: Setting baud rate of this port.
  1350. * Syntax:
  1351. * speed_t MoxaPortSetBaud(int port, speed_t baud);
  1352. * int port : port number (0 - 127)
  1353. * long baud : baud rate (50 - 115200)
  1354. *
  1355. * return: 0 : this port is invalid or baud < 50
  1356. * 50 - 115200 : the real baud rate set to the port, if
  1357. * the argument baud is large than maximun
  1358. * available baud rate, the real setting
  1359. * baud rate will be the maximun baud rate.
  1360. *
  1361. *
  1362. * Function 12: Configure the port.
  1363. * Syntax:
  1364. * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
  1365. * int port : port number (0 - 127)
  1366. * struct ktermios * termio : termio structure pointer
  1367. * speed_t baud : baud rate
  1368. *
  1369. * return: -1 : this port is invalid or termio == NULL
  1370. * 0 : setting O.K.
  1371. *
  1372. *
  1373. * Function 13: Get the DTR/RTS state of this port.
  1374. * Syntax:
  1375. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1376. * int port : port number (0 - 127)
  1377. * int * dtrState : pointer to INT to receive the current DTR
  1378. * state. (if NULL, this function will not
  1379. * write to this address)
  1380. * int * rtsState : pointer to INT to receive the current RTS
  1381. * state. (if NULL, this function will not
  1382. * write to this address)
  1383. *
  1384. * return: -1 : this port is invalid
  1385. * 0 : O.K.
  1386. *
  1387. *
  1388. * Function 14: Setting the DTR/RTS output state of this port.
  1389. * Syntax:
  1390. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1391. * int port : port number (0 - 127)
  1392. * int dtrState : DTR output state (0: off, 1: on)
  1393. * int rtsState : RTS output state (0: off, 1: on)
  1394. *
  1395. *
  1396. * Function 15: Setting the flow control of this port.
  1397. * Syntax:
  1398. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1399. * int txFlow,int xany);
  1400. * int port : port number (0 - 127)
  1401. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1402. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1403. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1404. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1405. * int xany : S/W XANY flow control (0: no, 1: yes)
  1406. *
  1407. *
  1408. * Function 16: Get ths line status of this port
  1409. * Syntax:
  1410. * int MoxaPortLineStatus(int port);
  1411. * int port : port number (0 - 127)
  1412. *
  1413. * return: Bit 0 - CTS state (0: off, 1: on)
  1414. * Bit 1 - DSR state (0: off, 1: on)
  1415. * Bit 2 - DCD state (0: off, 1: on)
  1416. *
  1417. *
  1418. * Function 19: Flush the Rx/Tx buffer data of this port.
  1419. * Syntax:
  1420. * void MoxaPortFlushData(int port, int mode);
  1421. * int port : port number (0 - 127)
  1422. * int mode
  1423. * 0 : flush the Rx buffer
  1424. * 1 : flush the Tx buffer
  1425. * 2 : flush the Rx and Tx buffer
  1426. *
  1427. *
  1428. * Function 20: Write data.
  1429. * Syntax:
  1430. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1431. * int port : port number (0 - 127)
  1432. * unsigned char * buffer : pointer to write data buffer.
  1433. * int length : write data length
  1434. *
  1435. * return: 0 - length : real write data length
  1436. *
  1437. *
  1438. * Function 21: Read data.
  1439. * Syntax:
  1440. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1441. * int port : port number (0 - 127)
  1442. * struct tty_struct *tty : tty for data
  1443. *
  1444. * return: 0 - length : real read data length
  1445. *
  1446. *
  1447. * Function 24: Get the Tx buffer current queued data bytes
  1448. * Syntax:
  1449. * int MoxaPortTxQueue(int port);
  1450. * int port : port number (0 - 127)
  1451. *
  1452. * return: .. : Tx buffer current queued data bytes
  1453. *
  1454. *
  1455. * Function 25: Get the Tx buffer current free space
  1456. * Syntax:
  1457. * int MoxaPortTxFree(int port);
  1458. * int port : port number (0 - 127)
  1459. *
  1460. * return: .. : Tx buffer current free space
  1461. *
  1462. *
  1463. * Function 26: Get the Rx buffer current queued data bytes
  1464. * Syntax:
  1465. * int MoxaPortRxQueue(int port);
  1466. * int port : port number (0 - 127)
  1467. *
  1468. * return: .. : Rx buffer current queued data bytes
  1469. *
  1470. *
  1471. * Function 28: Disable port data transmission.
  1472. * Syntax:
  1473. * void MoxaPortTxDisable(int port);
  1474. * int port : port number (0 - 127)
  1475. *
  1476. *
  1477. * Function 29: Enable port data transmission.
  1478. * Syntax:
  1479. * void MoxaPortTxEnable(int port);
  1480. * int port : port number (0 - 127)
  1481. *
  1482. *
  1483. * Function 31: Get the received BREAK signal count and reset it.
  1484. * Syntax:
  1485. * int MoxaPortResetBrkCnt(int port);
  1486. * int port : port number (0 - 127)
  1487. *
  1488. * return: 0 - .. : BREAK signal count
  1489. *
  1490. *
  1491. */
  1492. static void MoxaPortEnable(struct moxa_port *port)
  1493. {
  1494. void __iomem *ofsAddr;
  1495. u16 lowwater = 512;
  1496. ofsAddr = port->tableAddr;
  1497. writew(lowwater, ofsAddr + Low_water);
  1498. if (MOXA_IS_320(port->board))
  1499. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1500. else
  1501. writew(readw(ofsAddr + HostStat) | WakeupBreak,
  1502. ofsAddr + HostStat);
  1503. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1504. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1505. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1506. MoxaPortLineStatus(port);
  1507. }
  1508. static void MoxaPortDisable(struct moxa_port *port)
  1509. {
  1510. void __iomem *ofsAddr = port->tableAddr;
  1511. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1512. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1513. writew(0, ofsAddr + HostStat);
  1514. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1515. }
  1516. static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
  1517. {
  1518. void __iomem *ofsAddr = port->tableAddr;
  1519. unsigned int clock, val;
  1520. speed_t max;
  1521. max = MOXA_IS_320(port->board) ? 460800 : 921600;
  1522. if (baud < 50)
  1523. return 0;
  1524. if (baud > max)
  1525. baud = max;
  1526. clock = 921600;
  1527. val = clock / baud;
  1528. moxafunc(ofsAddr, FC_SetBaud, val);
  1529. baud = clock / val;
  1530. return baud;
  1531. }
  1532. static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
  1533. speed_t baud)
  1534. {
  1535. void __iomem *ofsAddr;
  1536. tcflag_t mode = 0;
  1537. ofsAddr = port->tableAddr;
  1538. mode = termio->c_cflag & CSIZE;
  1539. if (mode == CS5)
  1540. mode = MX_CS5;
  1541. else if (mode == CS6)
  1542. mode = MX_CS6;
  1543. else if (mode == CS7)
  1544. mode = MX_CS7;
  1545. else if (mode == CS8)
  1546. mode = MX_CS8;
  1547. if (termio->c_cflag & CSTOPB) {
  1548. if (mode == MX_CS5)
  1549. mode |= MX_STOP15;
  1550. else
  1551. mode |= MX_STOP2;
  1552. } else
  1553. mode |= MX_STOP1;
  1554. if (termio->c_cflag & PARENB) {
  1555. if (termio->c_cflag & PARODD) {
  1556. if (termio->c_cflag & CMSPAR)
  1557. mode |= MX_PARMARK;
  1558. else
  1559. mode |= MX_PARODD;
  1560. } else {
  1561. if (termio->c_cflag & CMSPAR)
  1562. mode |= MX_PARSPACE;
  1563. else
  1564. mode |= MX_PAREVEN;
  1565. }
  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(&port->port, &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(&port->port, &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. }