moxa.c 53 KB

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