moxa.c 52 KB

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