moxa.c 52 KB

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