moxa.c 52 KB

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