moxa.c 54 KB

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