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