moxa.c 52 KB

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