moxa.c 52 KB

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