moxa.c 52 KB

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