moxa.c 53 KB

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