moxa.c 54 KB

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