moxa.c 54 KB

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