moxa.c 53 KB

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