moxa.c 52 KB

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