moxa.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. /*****************************************************************************/
  2. /*
  3. * moxa.c -- MOXA Intellio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
  6. * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  7. *
  8. * This code is loosely based on the Linux serial driver, written by
  9. * Linus Torvalds, Theodore T'so and others.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. /*
  17. * MOXA Intellio Series Driver
  18. * for : LINUX
  19. * date : 1999/1/7
  20. * version : 5.1
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/mm.h>
  25. #include <linux/ioport.h>
  26. #include <linux/errno.h>
  27. #include <linux/firmware.h>
  28. #include <linux/signal.h>
  29. #include <linux/sched.h>
  30. #include <linux/timer.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/tty.h>
  33. #include <linux/tty_flip.h>
  34. #include <linux/major.h>
  35. #include <linux/string.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/serial.h>
  39. #include <linux/tty_driver.h>
  40. #include <linux/delay.h>
  41. #include <linux/pci.h>
  42. #include <linux/init.h>
  43. #include <linux/bitops.h>
  44. #include <asm/system.h>
  45. #include <asm/io.h>
  46. #include <asm/uaccess.h>
  47. #include "moxa.h"
  48. #define MOXA_VERSION "6.0k"
  49. #define MOXA_FW_HDRLEN 32
  50. #define MOXAMAJOR 172
  51. #define MAX_BOARDS 4 /* Don't change this value */
  52. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  53. #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
  54. #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
  55. (brd)->boardType == MOXA_BOARD_C320_PCI)
  56. /*
  57. * Define the Moxa PCI vendor and device IDs.
  58. */
  59. #define MOXA_BUS_TYPE_ISA 0
  60. #define MOXA_BUS_TYPE_PCI 1
  61. enum {
  62. MOXA_BOARD_C218_PCI = 1,
  63. MOXA_BOARD_C218_ISA,
  64. MOXA_BOARD_C320_PCI,
  65. MOXA_BOARD_C320_ISA,
  66. MOXA_BOARD_CP204J,
  67. };
  68. static char *moxa_brdname[] =
  69. {
  70. "C218 Turbo PCI series",
  71. "C218 Turbo ISA series",
  72. "C320 Turbo PCI series",
  73. "C320 Turbo ISA series",
  74. "CP-204J series",
  75. };
  76. #ifdef CONFIG_PCI
  77. static struct pci_device_id moxa_pcibrds[] = {
  78. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
  79. .driver_data = MOXA_BOARD_C218_PCI },
  80. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
  81. .driver_data = MOXA_BOARD_C320_PCI },
  82. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
  83. .driver_data = MOXA_BOARD_CP204J },
  84. { 0 }
  85. };
  86. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  87. #endif /* CONFIG_PCI */
  88. struct moxa_port;
  89. static struct moxa_board_conf {
  90. int boardType;
  91. int numPorts;
  92. int busType;
  93. unsigned int ready;
  94. struct moxa_port *ports;
  95. void __iomem *basemem;
  96. void __iomem *intNdx;
  97. void __iomem *intPend;
  98. void __iomem *intTable;
  99. } moxa_boards[MAX_BOARDS];
  100. struct mxser_mstatus {
  101. tcflag_t cflag;
  102. int cts;
  103. int dsr;
  104. int ri;
  105. int dcd;
  106. };
  107. struct moxaq_str {
  108. int inq;
  109. int outq;
  110. };
  111. struct moxa_port {
  112. struct tty_port port;
  113. struct moxa_board_conf *board;
  114. void __iomem *tableAddr;
  115. int type;
  116. int cflag;
  117. unsigned long statusflags;
  118. u8 DCDState; /* Protected by the port lock */
  119. u8 lineCtrl;
  120. u8 lowChkFlag;
  121. };
  122. struct mon_str {
  123. int tick;
  124. int rxcnt[MAX_PORTS];
  125. int txcnt[MAX_PORTS];
  126. };
  127. /* statusflags */
  128. #define TXSTOPPED 1
  129. #define LOWWAIT 2
  130. #define EMPTYWAIT 3
  131. #define SERIAL_DO_RESTART
  132. #define WAKEUP_CHARS 256
  133. static int ttymajor = MOXAMAJOR;
  134. static struct mon_str moxaLog;
  135. static unsigned int moxaFuncTout = HZ / 2;
  136. static unsigned int moxaLowWaterChk;
  137. static DEFINE_MUTEX(moxa_openlock);
  138. static DEFINE_SPINLOCK(moxa_lock);
  139. /* Variables for insmod */
  140. #ifdef MODULE
  141. static unsigned long baseaddr[MAX_BOARDS];
  142. static unsigned int type[MAX_BOARDS];
  143. static unsigned int numports[MAX_BOARDS];
  144. #endif
  145. MODULE_AUTHOR("William Chen");
  146. MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
  147. MODULE_LICENSE("GPL");
  148. #ifdef MODULE
  149. module_param_array(type, uint, NULL, 0);
  150. MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
  151. module_param_array(baseaddr, ulong, NULL, 0);
  152. MODULE_PARM_DESC(baseaddr, "base address");
  153. module_param_array(numports, uint, NULL, 0);
  154. MODULE_PARM_DESC(numports, "numports (ignored for C218)");
  155. #endif
  156. module_param(ttymajor, int, 0);
  157. /*
  158. * static functions:
  159. */
  160. static int moxa_open(struct tty_struct *, struct file *);
  161. static void moxa_close(struct tty_struct *, struct file *);
  162. static int moxa_write(struct tty_struct *, const unsigned char *, int);
  163. static int moxa_write_room(struct tty_struct *);
  164. static void moxa_flush_buffer(struct tty_struct *);
  165. static int moxa_chars_in_buffer(struct tty_struct *);
  166. static void moxa_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, struct file *file);
  171. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  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 && printk_ratelimit())
  210. printk(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, struct file *file,
  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(tty);
  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. printk(KERN_INFO "MOXA Intellio family driver version %s\n",
  901. MOXA_VERSION);
  902. moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
  903. if (!moxaDriver)
  904. return -ENOMEM;
  905. moxaDriver->owner = THIS_MODULE;
  906. moxaDriver->name = "ttyMX";
  907. moxaDriver->major = ttymajor;
  908. moxaDriver->minor_start = 0;
  909. moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
  910. moxaDriver->subtype = SERIAL_TYPE_NORMAL;
  911. moxaDriver->init_termios = tty_std_termios;
  912. moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  913. moxaDriver->init_termios.c_ispeed = 9600;
  914. moxaDriver->init_termios.c_ospeed = 9600;
  915. moxaDriver->flags = TTY_DRIVER_REAL_RAW;
  916. tty_set_operations(moxaDriver, &moxa_ops);
  917. if (tty_register_driver(moxaDriver)) {
  918. printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
  919. put_tty_driver(moxaDriver);
  920. return -1;
  921. }
  922. /* Find the boards defined from module args. */
  923. #ifdef MODULE
  924. {
  925. struct moxa_board_conf *brd = moxa_boards;
  926. unsigned int i;
  927. for (i = 0; i < MAX_BOARDS; i++) {
  928. if (!baseaddr[i])
  929. break;
  930. if (type[i] == MOXA_BOARD_C218_ISA ||
  931. type[i] == MOXA_BOARD_C320_ISA) {
  932. pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
  933. isabrds + 1, moxa_brdname[type[i] - 1],
  934. baseaddr[i]);
  935. brd->boardType = type[i];
  936. brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
  937. numports[i];
  938. brd->busType = MOXA_BUS_TYPE_ISA;
  939. brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
  940. if (!brd->basemem) {
  941. printk(KERN_ERR "MOXA: can't remap %lx\n",
  942. baseaddr[i]);
  943. continue;
  944. }
  945. if (moxa_init_board(brd, NULL)) {
  946. iounmap(brd->basemem);
  947. brd->basemem = NULL;
  948. continue;
  949. }
  950. printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
  951. "ready (%u ports, firmware loaded)\n",
  952. baseaddr[i], brd->numPorts);
  953. brd++;
  954. isabrds++;
  955. }
  956. }
  957. }
  958. #endif
  959. #ifdef CONFIG_PCI
  960. retval = pci_register_driver(&moxa_pci_driver);
  961. if (retval) {
  962. printk(KERN_ERR "Can't register MOXA pci driver!\n");
  963. if (isabrds)
  964. retval = 0;
  965. }
  966. #endif
  967. return retval;
  968. }
  969. static void __exit moxa_exit(void)
  970. {
  971. unsigned int i;
  972. #ifdef CONFIG_PCI
  973. pci_unregister_driver(&moxa_pci_driver);
  974. #endif
  975. for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
  976. if (moxa_boards[i].ready)
  977. moxa_board_deinit(&moxa_boards[i]);
  978. del_timer_sync(&moxaTimer);
  979. if (tty_unregister_driver(moxaDriver))
  980. printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
  981. "serial driver\n");
  982. put_tty_driver(moxaDriver);
  983. }
  984. module_init(moxa_init);
  985. module_exit(moxa_exit);
  986. static void moxa_shutdown(struct tty_port *port)
  987. {
  988. struct moxa_port *ch = container_of(port, struct moxa_port, port);
  989. MoxaPortDisable(ch);
  990. MoxaPortFlushData(ch, 2);
  991. clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
  992. }
  993. static int moxa_carrier_raised(struct tty_port *port)
  994. {
  995. struct moxa_port *ch = container_of(port, struct moxa_port, port);
  996. int dcd;
  997. spin_lock_irq(&port->lock);
  998. dcd = ch->DCDState;
  999. spin_unlock_irq(&port->lock);
  1000. return dcd;
  1001. }
  1002. static void moxa_dtr_rts(struct tty_port *port, int onoff)
  1003. {
  1004. struct moxa_port *ch = container_of(port, struct moxa_port, port);
  1005. MoxaPortLineCtrl(ch, onoff, onoff);
  1006. }
  1007. static int moxa_open(struct tty_struct *tty, struct file *filp)
  1008. {
  1009. struct moxa_board_conf *brd;
  1010. struct moxa_port *ch;
  1011. int port;
  1012. int retval;
  1013. port = tty->index;
  1014. if (port == MAX_PORTS) {
  1015. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  1016. }
  1017. if (mutex_lock_interruptible(&moxa_openlock))
  1018. return -ERESTARTSYS;
  1019. brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
  1020. if (!brd->ready) {
  1021. mutex_unlock(&moxa_openlock);
  1022. return -ENODEV;
  1023. }
  1024. if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
  1025. mutex_unlock(&moxa_openlock);
  1026. return -ENODEV;
  1027. }
  1028. ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
  1029. ch->port.count++;
  1030. tty->driver_data = ch;
  1031. tty_port_tty_set(&ch->port, tty);
  1032. mutex_lock(&ch->port.mutex);
  1033. if (!(ch->port.flags & ASYNC_INITIALIZED)) {
  1034. ch->statusflags = 0;
  1035. moxa_set_tty_param(tty, tty->termios);
  1036. MoxaPortLineCtrl(ch, 1, 1);
  1037. MoxaPortEnable(ch);
  1038. MoxaSetFifo(ch, ch->type == PORT_16550A);
  1039. ch->port.flags |= ASYNC_INITIALIZED;
  1040. }
  1041. mutex_unlock(&ch->port.mutex);
  1042. mutex_unlock(&moxa_openlock);
  1043. retval = tty_port_block_til_ready(&ch->port, tty, filp);
  1044. if (retval == 0)
  1045. set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
  1046. return retval;
  1047. }
  1048. static void moxa_close(struct tty_struct *tty, struct file *filp)
  1049. {
  1050. struct moxa_port *ch = tty->driver_data;
  1051. ch->cflag = tty->termios->c_cflag;
  1052. tty_port_close(&ch->port, tty, filp);
  1053. }
  1054. static int moxa_write(struct tty_struct *tty,
  1055. const unsigned char *buf, int count)
  1056. {
  1057. struct moxa_port *ch = tty->driver_data;
  1058. int len;
  1059. if (ch == NULL)
  1060. return 0;
  1061. spin_lock_bh(&moxa_lock);
  1062. len = MoxaPortWriteData(tty, buf, count);
  1063. spin_unlock_bh(&moxa_lock);
  1064. set_bit(LOWWAIT, &ch->statusflags);
  1065. return len;
  1066. }
  1067. static int moxa_write_room(struct tty_struct *tty)
  1068. {
  1069. struct moxa_port *ch;
  1070. if (tty->stopped)
  1071. return 0;
  1072. ch = tty->driver_data;
  1073. if (ch == NULL)
  1074. return 0;
  1075. return MoxaPortTxFree(ch);
  1076. }
  1077. static void moxa_flush_buffer(struct tty_struct *tty)
  1078. {
  1079. struct moxa_port *ch = tty->driver_data;
  1080. if (ch == NULL)
  1081. return;
  1082. MoxaPortFlushData(ch, 1);
  1083. tty_wakeup(tty);
  1084. }
  1085. static int moxa_chars_in_buffer(struct tty_struct *tty)
  1086. {
  1087. struct moxa_port *ch = tty->driver_data;
  1088. int chars;
  1089. chars = MoxaPortTxQueue(ch);
  1090. if (chars)
  1091. /*
  1092. * Make it possible to wakeup anything waiting for output
  1093. * in tty_ioctl.c, etc.
  1094. */
  1095. set_bit(EMPTYWAIT, &ch->statusflags);
  1096. return chars;
  1097. }
  1098. static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
  1099. {
  1100. struct moxa_port *ch = tty->driver_data;
  1101. int flag = 0, dtr, rts;
  1102. MoxaPortGetLineOut(ch, &dtr, &rts);
  1103. if (dtr)
  1104. flag |= TIOCM_DTR;
  1105. if (rts)
  1106. flag |= TIOCM_RTS;
  1107. dtr = MoxaPortLineStatus(ch);
  1108. if (dtr & 1)
  1109. flag |= TIOCM_CTS;
  1110. if (dtr & 2)
  1111. flag |= TIOCM_DSR;
  1112. if (dtr & 4)
  1113. flag |= TIOCM_CD;
  1114. return flag;
  1115. }
  1116. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  1117. unsigned int set, unsigned int clear)
  1118. {
  1119. struct moxa_port *ch;
  1120. int port;
  1121. int dtr, rts;
  1122. port = tty->index;
  1123. mutex_lock(&moxa_openlock);
  1124. ch = tty->driver_data;
  1125. if (!ch) {
  1126. mutex_unlock(&moxa_openlock);
  1127. return -EINVAL;
  1128. }
  1129. MoxaPortGetLineOut(ch, &dtr, &rts);
  1130. if (set & TIOCM_RTS)
  1131. rts = 1;
  1132. if (set & TIOCM_DTR)
  1133. dtr = 1;
  1134. if (clear & TIOCM_RTS)
  1135. rts = 0;
  1136. if (clear & TIOCM_DTR)
  1137. dtr = 0;
  1138. MoxaPortLineCtrl(ch, dtr, rts);
  1139. mutex_unlock(&moxa_openlock);
  1140. return 0;
  1141. }
  1142. static void moxa_set_termios(struct tty_struct *tty,
  1143. struct ktermios *old_termios)
  1144. {
  1145. struct moxa_port *ch = tty->driver_data;
  1146. if (ch == NULL)
  1147. return;
  1148. moxa_set_tty_param(tty, old_termios);
  1149. if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
  1150. wake_up_interruptible(&ch->port.open_wait);
  1151. }
  1152. static void moxa_stop(struct tty_struct *tty)
  1153. {
  1154. struct moxa_port *ch = tty->driver_data;
  1155. if (ch == NULL)
  1156. return;
  1157. MoxaPortTxDisable(ch);
  1158. set_bit(TXSTOPPED, &ch->statusflags);
  1159. }
  1160. static void moxa_start(struct tty_struct *tty)
  1161. {
  1162. struct moxa_port *ch = tty->driver_data;
  1163. if (ch == NULL)
  1164. return;
  1165. if (!(ch->statusflags & TXSTOPPED))
  1166. return;
  1167. MoxaPortTxEnable(ch);
  1168. clear_bit(TXSTOPPED, &ch->statusflags);
  1169. }
  1170. static void moxa_hangup(struct tty_struct *tty)
  1171. {
  1172. struct moxa_port *ch = tty->driver_data;
  1173. tty_port_hangup(&ch->port);
  1174. }
  1175. static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
  1176. {
  1177. struct tty_struct *tty;
  1178. unsigned long flags;
  1179. dcd = !!dcd;
  1180. spin_lock_irqsave(&p->port.lock, flags);
  1181. if (dcd != p->DCDState) {
  1182. p->DCDState = dcd;
  1183. spin_unlock_irqrestore(&p->port.lock, flags);
  1184. tty = tty_port_tty_get(&p->port);
  1185. if (tty && C_CLOCAL(tty) && !dcd)
  1186. tty_hangup(tty);
  1187. tty_kref_put(tty);
  1188. }
  1189. else
  1190. spin_unlock_irqrestore(&p->port.lock, flags);
  1191. }
  1192. static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
  1193. u16 __iomem *ip)
  1194. {
  1195. struct tty_struct *tty = tty_port_tty_get(&p->port);
  1196. void __iomem *ofsAddr;
  1197. unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
  1198. u16 intr;
  1199. if (tty) {
  1200. if (test_bit(EMPTYWAIT, &p->statusflags) &&
  1201. MoxaPortTxQueue(p) == 0) {
  1202. clear_bit(EMPTYWAIT, &p->statusflags);
  1203. tty_wakeup(tty);
  1204. }
  1205. if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
  1206. MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
  1207. clear_bit(LOWWAIT, &p->statusflags);
  1208. tty_wakeup(tty);
  1209. }
  1210. if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
  1211. MoxaPortRxQueue(p) > 0) { /* RX */
  1212. MoxaPortReadData(p);
  1213. tty_schedule_flip(tty);
  1214. }
  1215. } else {
  1216. clear_bit(EMPTYWAIT, &p->statusflags);
  1217. MoxaPortFlushData(p, 0); /* flush RX */
  1218. }
  1219. if (!handle) /* nothing else to do */
  1220. goto put;
  1221. intr = readw(ip); /* port irq status */
  1222. if (intr == 0)
  1223. goto put;
  1224. writew(0, ip); /* ACK port */
  1225. ofsAddr = p->tableAddr;
  1226. if (intr & IntrTx) /* disable tx intr */
  1227. writew(readw(ofsAddr + HostStat) & ~WakeupTx,
  1228. ofsAddr + HostStat);
  1229. if (!inited)
  1230. goto put;
  1231. if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
  1232. tty_insert_flip_char(tty, 0, TTY_BREAK);
  1233. tty_schedule_flip(tty);
  1234. }
  1235. if (intr & IntrLine)
  1236. moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
  1237. put:
  1238. tty_kref_put(tty);
  1239. return 0;
  1240. }
  1241. static void moxa_poll(unsigned long ignored)
  1242. {
  1243. struct moxa_board_conf *brd;
  1244. u16 __iomem *ip;
  1245. unsigned int card, port, served = 0;
  1246. spin_lock(&moxa_lock);
  1247. for (card = 0; card < MAX_BOARDS; card++) {
  1248. brd = &moxa_boards[card];
  1249. if (!brd->ready)
  1250. continue;
  1251. served++;
  1252. ip = NULL;
  1253. if (readb(brd->intPend) == 0xff)
  1254. ip = brd->intTable + readb(brd->intNdx);
  1255. for (port = 0; port < brd->numPorts; port++)
  1256. moxa_poll_port(&brd->ports[port], !!ip, ip + port);
  1257. if (ip)
  1258. writeb(0, brd->intPend); /* ACK */
  1259. if (moxaLowWaterChk) {
  1260. struct moxa_port *p = brd->ports;
  1261. for (port = 0; port < brd->numPorts; port++, p++)
  1262. if (p->lowChkFlag) {
  1263. p->lowChkFlag = 0;
  1264. moxa_low_water_check(p->tableAddr);
  1265. }
  1266. }
  1267. }
  1268. moxaLowWaterChk = 0;
  1269. if (served)
  1270. mod_timer(&moxaTimer, jiffies + HZ / 50);
  1271. spin_unlock(&moxa_lock);
  1272. }
  1273. /******************************************************************************/
  1274. static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
  1275. {
  1276. register struct ktermios *ts = tty->termios;
  1277. struct moxa_port *ch = tty->driver_data;
  1278. int rts, cts, txflow, rxflow, xany, baud;
  1279. rts = cts = txflow = rxflow = xany = 0;
  1280. if (ts->c_cflag & CRTSCTS)
  1281. rts = cts = 1;
  1282. if (ts->c_iflag & IXON)
  1283. txflow = 1;
  1284. if (ts->c_iflag & IXOFF)
  1285. rxflow = 1;
  1286. if (ts->c_iflag & IXANY)
  1287. xany = 1;
  1288. /* Clear the features we don't support */
  1289. ts->c_cflag &= ~CMSPAR;
  1290. MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
  1291. baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
  1292. if (baud == -1)
  1293. baud = tty_termios_baud_rate(old_termios);
  1294. /* Not put the baud rate into the termios data */
  1295. tty_encode_baud_rate(tty, baud, baud);
  1296. }
  1297. /*****************************************************************************
  1298. * Driver level functions: *
  1299. *****************************************************************************/
  1300. static void MoxaPortFlushData(struct moxa_port *port, int mode)
  1301. {
  1302. void __iomem *ofsAddr;
  1303. if (mode < 0 || mode > 2)
  1304. return;
  1305. ofsAddr = port->tableAddr;
  1306. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1307. if (mode != 1) {
  1308. port->lowChkFlag = 0;
  1309. moxa_low_water_check(ofsAddr);
  1310. }
  1311. }
  1312. /*
  1313. * Moxa Port Number Description:
  1314. *
  1315. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1316. * the port number using in MOXA driver functions will be 0 to 31 for
  1317. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1318. * to 127 for fourth. For example, if you setup three MOXA boards,
  1319. * first board is C218, second board is C320-16 and third board is
  1320. * C320-32. The port number of first board (C218 - 8 ports) is from
  1321. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1322. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1323. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1324. * 127 will be invalid.
  1325. *
  1326. *
  1327. * Moxa Functions Description:
  1328. *
  1329. * Function 1: Driver initialization routine, this routine must be
  1330. * called when initialized driver.
  1331. * Syntax:
  1332. * void MoxaDriverInit();
  1333. *
  1334. *
  1335. * Function 2: Moxa driver private IOCTL command processing.
  1336. * Syntax:
  1337. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1338. *
  1339. * unsigned int cmd : IOCTL command
  1340. * unsigned long arg : IOCTL argument
  1341. * int port : port number (0 - 127)
  1342. *
  1343. * return: 0 (OK)
  1344. * -EINVAL
  1345. * -ENOIOCTLCMD
  1346. *
  1347. *
  1348. * Function 6: Enable this port to start Tx/Rx data.
  1349. * Syntax:
  1350. * void MoxaPortEnable(int port);
  1351. * int port : port number (0 - 127)
  1352. *
  1353. *
  1354. * Function 7: Disable this port
  1355. * Syntax:
  1356. * void MoxaPortDisable(int port);
  1357. * int port : port number (0 - 127)
  1358. *
  1359. *
  1360. * Function 10: Setting baud rate of this port.
  1361. * Syntax:
  1362. * speed_t MoxaPortSetBaud(int port, speed_t baud);
  1363. * int port : port number (0 - 127)
  1364. * long baud : baud rate (50 - 115200)
  1365. *
  1366. * return: 0 : this port is invalid or baud < 50
  1367. * 50 - 115200 : the real baud rate set to the port, if
  1368. * the argument baud is large than maximun
  1369. * available baud rate, the real setting
  1370. * baud rate will be the maximun baud rate.
  1371. *
  1372. *
  1373. * Function 12: Configure the port.
  1374. * Syntax:
  1375. * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
  1376. * int port : port number (0 - 127)
  1377. * struct ktermios * termio : termio structure pointer
  1378. * speed_t baud : baud rate
  1379. *
  1380. * return: -1 : this port is invalid or termio == NULL
  1381. * 0 : setting O.K.
  1382. *
  1383. *
  1384. * Function 13: Get the DTR/RTS state of this port.
  1385. * Syntax:
  1386. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1387. * int port : port number (0 - 127)
  1388. * int * dtrState : pointer to INT to receive the current DTR
  1389. * state. (if NULL, this function will not
  1390. * write to this address)
  1391. * int * rtsState : pointer to INT to receive the current RTS
  1392. * state. (if NULL, this function will not
  1393. * write to this address)
  1394. *
  1395. * return: -1 : this port is invalid
  1396. * 0 : O.K.
  1397. *
  1398. *
  1399. * Function 14: Setting the DTR/RTS output state of this port.
  1400. * Syntax:
  1401. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1402. * int port : port number (0 - 127)
  1403. * int dtrState : DTR output state (0: off, 1: on)
  1404. * int rtsState : RTS output state (0: off, 1: on)
  1405. *
  1406. *
  1407. * Function 15: Setting the flow control of this port.
  1408. * Syntax:
  1409. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1410. * int txFlow,int xany);
  1411. * int port : port number (0 - 127)
  1412. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1413. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1414. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1415. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1416. * int xany : S/W XANY flow control (0: no, 1: yes)
  1417. *
  1418. *
  1419. * Function 16: Get ths line status of this port
  1420. * Syntax:
  1421. * int MoxaPortLineStatus(int port);
  1422. * int port : port number (0 - 127)
  1423. *
  1424. * return: Bit 0 - CTS state (0: off, 1: on)
  1425. * Bit 1 - DSR state (0: off, 1: on)
  1426. * Bit 2 - DCD state (0: off, 1: on)
  1427. *
  1428. *
  1429. * Function 19: Flush the Rx/Tx buffer data of this port.
  1430. * Syntax:
  1431. * void MoxaPortFlushData(int port, int mode);
  1432. * int port : port number (0 - 127)
  1433. * int mode
  1434. * 0 : flush the Rx buffer
  1435. * 1 : flush the Tx buffer
  1436. * 2 : flush the Rx and Tx buffer
  1437. *
  1438. *
  1439. * Function 20: Write data.
  1440. * Syntax:
  1441. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1442. * int port : port number (0 - 127)
  1443. * unsigned char * buffer : pointer to write data buffer.
  1444. * int length : write data length
  1445. *
  1446. * return: 0 - length : real write data length
  1447. *
  1448. *
  1449. * Function 21: Read data.
  1450. * Syntax:
  1451. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1452. * int port : port number (0 - 127)
  1453. * struct tty_struct *tty : tty for data
  1454. *
  1455. * return: 0 - length : real read data length
  1456. *
  1457. *
  1458. * Function 24: Get the Tx buffer current queued data bytes
  1459. * Syntax:
  1460. * int MoxaPortTxQueue(int port);
  1461. * int port : port number (0 - 127)
  1462. *
  1463. * return: .. : Tx buffer current queued data bytes
  1464. *
  1465. *
  1466. * Function 25: Get the Tx buffer current free space
  1467. * Syntax:
  1468. * int MoxaPortTxFree(int port);
  1469. * int port : port number (0 - 127)
  1470. *
  1471. * return: .. : Tx buffer current free space
  1472. *
  1473. *
  1474. * Function 26: Get the Rx buffer current queued data bytes
  1475. * Syntax:
  1476. * int MoxaPortRxQueue(int port);
  1477. * int port : port number (0 - 127)
  1478. *
  1479. * return: .. : Rx buffer current queued data bytes
  1480. *
  1481. *
  1482. * Function 28: Disable port data transmission.
  1483. * Syntax:
  1484. * void MoxaPortTxDisable(int port);
  1485. * int port : port number (0 - 127)
  1486. *
  1487. *
  1488. * Function 29: Enable port data transmission.
  1489. * Syntax:
  1490. * void MoxaPortTxEnable(int port);
  1491. * int port : port number (0 - 127)
  1492. *
  1493. *
  1494. * Function 31: Get the received BREAK signal count and reset it.
  1495. * Syntax:
  1496. * int MoxaPortResetBrkCnt(int port);
  1497. * int port : port number (0 - 127)
  1498. *
  1499. * return: 0 - .. : BREAK signal count
  1500. *
  1501. *
  1502. */
  1503. static void MoxaPortEnable(struct moxa_port *port)
  1504. {
  1505. void __iomem *ofsAddr;
  1506. u16 lowwater = 512;
  1507. ofsAddr = port->tableAddr;
  1508. writew(lowwater, ofsAddr + Low_water);
  1509. if (MOXA_IS_320(port->board))
  1510. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1511. else
  1512. writew(readw(ofsAddr + HostStat) | WakeupBreak,
  1513. ofsAddr + HostStat);
  1514. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1515. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1516. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1517. MoxaPortLineStatus(port);
  1518. }
  1519. static void MoxaPortDisable(struct moxa_port *port)
  1520. {
  1521. void __iomem *ofsAddr = port->tableAddr;
  1522. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1523. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1524. writew(0, ofsAddr + HostStat);
  1525. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1526. }
  1527. static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
  1528. {
  1529. void __iomem *ofsAddr = port->tableAddr;
  1530. unsigned int clock, val;
  1531. speed_t max;
  1532. max = MOXA_IS_320(port->board) ? 460800 : 921600;
  1533. if (baud < 50)
  1534. return 0;
  1535. if (baud > max)
  1536. baud = max;
  1537. clock = 921600;
  1538. val = clock / baud;
  1539. moxafunc(ofsAddr, FC_SetBaud, val);
  1540. baud = clock / val;
  1541. return baud;
  1542. }
  1543. static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
  1544. speed_t baud)
  1545. {
  1546. void __iomem *ofsAddr;
  1547. tcflag_t cflag;
  1548. tcflag_t mode = 0;
  1549. ofsAddr = port->tableAddr;
  1550. cflag = termio->c_cflag; /* termio->c_cflag */
  1551. mode = termio->c_cflag & CSIZE;
  1552. if (mode == CS5)
  1553. mode = MX_CS5;
  1554. else if (mode == CS6)
  1555. mode = MX_CS6;
  1556. else if (mode == CS7)
  1557. mode = MX_CS7;
  1558. else if (mode == CS8)
  1559. mode = MX_CS8;
  1560. if (termio->c_cflag & CSTOPB) {
  1561. if (mode == MX_CS5)
  1562. mode |= MX_STOP15;
  1563. else
  1564. mode |= MX_STOP2;
  1565. } else
  1566. mode |= MX_STOP1;
  1567. if (termio->c_cflag & PARENB) {
  1568. if (termio->c_cflag & PARODD)
  1569. mode |= MX_PARODD;
  1570. else
  1571. mode |= MX_PAREVEN;
  1572. } else
  1573. mode |= MX_PARNONE;
  1574. moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
  1575. if (MOXA_IS_320(port->board) && baud >= 921600)
  1576. return -1;
  1577. baud = MoxaPortSetBaud(port, baud);
  1578. if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
  1579. spin_lock_irq(&moxafunc_lock);
  1580. writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
  1581. writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
  1582. writeb(FC_SetXonXoff, ofsAddr + FuncCode);
  1583. moxa_wait_finish(ofsAddr);
  1584. spin_unlock_irq(&moxafunc_lock);
  1585. }
  1586. return baud;
  1587. }
  1588. static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
  1589. int *rtsState)
  1590. {
  1591. if (dtrState)
  1592. *dtrState = !!(port->lineCtrl & DTR_ON);
  1593. if (rtsState)
  1594. *rtsState = !!(port->lineCtrl & RTS_ON);
  1595. return 0;
  1596. }
  1597. static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
  1598. {
  1599. u8 mode = 0;
  1600. if (dtr)
  1601. mode |= DTR_ON;
  1602. if (rts)
  1603. mode |= RTS_ON;
  1604. port->lineCtrl = mode;
  1605. moxafunc(port->tableAddr, FC_LineControl, mode);
  1606. }
  1607. static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
  1608. int txflow, int rxflow, int txany)
  1609. {
  1610. int mode = 0;
  1611. if (rts)
  1612. mode |= RTS_FlowCtl;
  1613. if (cts)
  1614. mode |= CTS_FlowCtl;
  1615. if (txflow)
  1616. mode |= Tx_FlowCtl;
  1617. if (rxflow)
  1618. mode |= Rx_FlowCtl;
  1619. if (txany)
  1620. mode |= IXM_IXANY;
  1621. moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
  1622. }
  1623. static int MoxaPortLineStatus(struct moxa_port *port)
  1624. {
  1625. void __iomem *ofsAddr;
  1626. int val;
  1627. ofsAddr = port->tableAddr;
  1628. if (MOXA_IS_320(port->board))
  1629. val = moxafuncret(ofsAddr, FC_LineStatus, 0);
  1630. else
  1631. val = readw(ofsAddr + FlagStat) >> 4;
  1632. val &= 0x0B;
  1633. if (val & 8)
  1634. val |= 4;
  1635. moxa_new_dcdstate(port, val & 8);
  1636. val &= 7;
  1637. return val;
  1638. }
  1639. static int MoxaPortWriteData(struct tty_struct *tty,
  1640. const unsigned char *buffer, int len)
  1641. {
  1642. struct moxa_port *port = tty->driver_data;
  1643. void __iomem *baseAddr, *ofsAddr, *ofs;
  1644. unsigned int c, total;
  1645. u16 head, tail, tx_mask, spage, epage;
  1646. u16 pageno, pageofs, bufhead;
  1647. ofsAddr = port->tableAddr;
  1648. baseAddr = port->board->basemem;
  1649. tx_mask = readw(ofsAddr + TX_mask);
  1650. spage = readw(ofsAddr + Page_txb);
  1651. epage = readw(ofsAddr + EndPage_txb);
  1652. tail = readw(ofsAddr + TXwptr);
  1653. head = readw(ofsAddr + TXrptr);
  1654. c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
  1655. if (c > len)
  1656. c = len;
  1657. moxaLog.txcnt[port->port.tty->index] += c;
  1658. total = c;
  1659. if (spage == epage) {
  1660. bufhead = readw(ofsAddr + Ofs_txb);
  1661. writew(spage, baseAddr + Control_reg);
  1662. while (c > 0) {
  1663. if (head > tail)
  1664. len = head - tail - 1;
  1665. else
  1666. len = tx_mask + 1 - tail;
  1667. len = (c > len) ? len : c;
  1668. ofs = baseAddr + DynPage_addr + bufhead + tail;
  1669. memcpy_toio(ofs, buffer, len);
  1670. buffer += len;
  1671. tail = (tail + len) & tx_mask;
  1672. c -= len;
  1673. }
  1674. } else {
  1675. pageno = spage + (tail >> 13);
  1676. pageofs = tail & Page_mask;
  1677. while (c > 0) {
  1678. len = Page_size - pageofs;
  1679. if (len > c)
  1680. len = c;
  1681. writeb(pageno, baseAddr + Control_reg);
  1682. ofs = baseAddr + DynPage_addr + pageofs;
  1683. memcpy_toio(ofs, buffer, len);
  1684. buffer += len;
  1685. if (++pageno == epage)
  1686. pageno = spage;
  1687. pageofs = 0;
  1688. c -= len;
  1689. }
  1690. tail = (tail + total) & tx_mask;
  1691. }
  1692. writew(tail, ofsAddr + TXwptr);
  1693. writeb(1, ofsAddr + CD180TXirq); /* start to send */
  1694. return total;
  1695. }
  1696. static int MoxaPortReadData(struct moxa_port *port)
  1697. {
  1698. struct tty_struct *tty = port->port.tty;
  1699. unsigned char *dst;
  1700. void __iomem *baseAddr, *ofsAddr, *ofs;
  1701. unsigned int count, len, total;
  1702. u16 tail, rx_mask, spage, epage;
  1703. u16 pageno, pageofs, bufhead, head;
  1704. ofsAddr = port->tableAddr;
  1705. baseAddr = port->board->basemem;
  1706. head = readw(ofsAddr + RXrptr);
  1707. tail = readw(ofsAddr + RXwptr);
  1708. rx_mask = readw(ofsAddr + RX_mask);
  1709. spage = readw(ofsAddr + Page_rxb);
  1710. epage = readw(ofsAddr + EndPage_rxb);
  1711. count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
  1712. if (count == 0)
  1713. return 0;
  1714. total = count;
  1715. moxaLog.rxcnt[tty->index] += total;
  1716. if (spage == epage) {
  1717. bufhead = readw(ofsAddr + Ofs_rxb);
  1718. writew(spage, baseAddr + Control_reg);
  1719. while (count > 0) {
  1720. ofs = baseAddr + DynPage_addr + bufhead + head;
  1721. len = (tail >= head) ? (tail - head) :
  1722. (rx_mask + 1 - head);
  1723. len = tty_prepare_flip_string(tty, &dst,
  1724. min(len, count));
  1725. memcpy_fromio(dst, ofs, len);
  1726. head = (head + len) & rx_mask;
  1727. count -= len;
  1728. }
  1729. } else {
  1730. pageno = spage + (head >> 13);
  1731. pageofs = head & Page_mask;
  1732. while (count > 0) {
  1733. writew(pageno, baseAddr + Control_reg);
  1734. ofs = baseAddr + DynPage_addr + pageofs;
  1735. len = tty_prepare_flip_string(tty, &dst,
  1736. min(Page_size - pageofs, count));
  1737. memcpy_fromio(dst, ofs, len);
  1738. count -= len;
  1739. pageofs = (pageofs + len) & Page_mask;
  1740. if (pageofs == 0 && ++pageno == epage)
  1741. pageno = spage;
  1742. }
  1743. head = (head + total) & rx_mask;
  1744. }
  1745. writew(head, ofsAddr + RXrptr);
  1746. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  1747. moxaLowWaterChk = 1;
  1748. port->lowChkFlag = 1;
  1749. }
  1750. return total;
  1751. }
  1752. static int MoxaPortTxQueue(struct moxa_port *port)
  1753. {
  1754. void __iomem *ofsAddr = port->tableAddr;
  1755. u16 rptr, wptr, mask;
  1756. rptr = readw(ofsAddr + TXrptr);
  1757. wptr = readw(ofsAddr + TXwptr);
  1758. mask = readw(ofsAddr + TX_mask);
  1759. return (wptr - rptr) & mask;
  1760. }
  1761. static int MoxaPortTxFree(struct moxa_port *port)
  1762. {
  1763. void __iomem *ofsAddr = port->tableAddr;
  1764. u16 rptr, wptr, mask;
  1765. rptr = readw(ofsAddr + TXrptr);
  1766. wptr = readw(ofsAddr + TXwptr);
  1767. mask = readw(ofsAddr + TX_mask);
  1768. return mask - ((wptr - rptr) & mask);
  1769. }
  1770. static int MoxaPortRxQueue(struct moxa_port *port)
  1771. {
  1772. void __iomem *ofsAddr = port->tableAddr;
  1773. u16 rptr, wptr, mask;
  1774. rptr = readw(ofsAddr + RXrptr);
  1775. wptr = readw(ofsAddr + RXwptr);
  1776. mask = readw(ofsAddr + RX_mask);
  1777. return (wptr - rptr) & mask;
  1778. }
  1779. static void MoxaPortTxDisable(struct moxa_port *port)
  1780. {
  1781. moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
  1782. }
  1783. static void MoxaPortTxEnable(struct moxa_port *port)
  1784. {
  1785. moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
  1786. }
  1787. static int moxa_get_serial_info(struct moxa_port *info,
  1788. struct serial_struct __user *retinfo)
  1789. {
  1790. struct serial_struct tmp = {
  1791. .type = info->type,
  1792. .line = info->port.tty->index,
  1793. .flags = info->port.flags,
  1794. .baud_base = 921600,
  1795. .close_delay = info->port.close_delay
  1796. };
  1797. return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
  1798. }
  1799. static int moxa_set_serial_info(struct moxa_port *info,
  1800. struct serial_struct __user *new_info)
  1801. {
  1802. struct serial_struct new_serial;
  1803. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1804. return -EFAULT;
  1805. if (new_serial.irq != 0 || new_serial.port != 0 ||
  1806. new_serial.custom_divisor != 0 ||
  1807. new_serial.baud_base != 921600)
  1808. return -EPERM;
  1809. if (!capable(CAP_SYS_ADMIN)) {
  1810. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  1811. (info->port.flags & ~ASYNC_USR_MASK)))
  1812. return -EPERM;
  1813. } else
  1814. info->port.close_delay = new_serial.close_delay * HZ / 100;
  1815. new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
  1816. new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
  1817. MoxaSetFifo(info, new_serial.type == PORT_16550A);
  1818. info->type = new_serial.type;
  1819. return 0;
  1820. }
  1821. /*****************************************************************************
  1822. * Static local functions: *
  1823. *****************************************************************************/
  1824. static void MoxaSetFifo(struct moxa_port *port, int enable)
  1825. {
  1826. void __iomem *ofsAddr = port->tableAddr;
  1827. if (!enable) {
  1828. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
  1829. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
  1830. } else {
  1831. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
  1832. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
  1833. }
  1834. }