moxa.c 53 KB

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