moxa.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  1. /*****************************************************************************/
  2. /*
  3. * moxa.c -- MOXA Intellio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
  6. *
  7. * This code is loosely based on the Linux serial driver, written by
  8. * Linus Torvalds, Theodore T'so and others.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. /*
  16. * MOXA Intellio Series Driver
  17. * for : LINUX
  18. * date : 1999/1/7
  19. * version : 5.1
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/mm.h>
  24. #include <linux/ioport.h>
  25. #include <linux/errno.h>
  26. #include <linux/signal.h>
  27. #include <linux/sched.h>
  28. #include <linux/timer.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/tty.h>
  31. #include <linux/tty_flip.h>
  32. #include <linux/major.h>
  33. #include <linux/string.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/serial.h>
  37. #include <linux/tty_driver.h>
  38. #include <linux/delay.h>
  39. #include <linux/pci.h>
  40. #include <linux/init.h>
  41. #include <linux/bitops.h>
  42. #include <asm/system.h>
  43. #include <asm/io.h>
  44. #include <asm/uaccess.h>
  45. #define MOXA_VERSION "5.1k"
  46. #define MOXAMAJOR 172
  47. #define MOXACUMAJOR 173
  48. #define MAX_BOARDS 4 /* Don't change this value */
  49. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  50. #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
  51. /*
  52. * Define the Moxa PCI vendor and device IDs.
  53. */
  54. #define MOXA_BUS_TYPE_ISA 0
  55. #define MOXA_BUS_TYPE_PCI 1
  56. enum {
  57. MOXA_BOARD_C218_PCI = 1,
  58. MOXA_BOARD_C218_ISA,
  59. MOXA_BOARD_C320_PCI,
  60. MOXA_BOARD_C320_ISA,
  61. MOXA_BOARD_CP204J,
  62. };
  63. static char *moxa_brdname[] =
  64. {
  65. "C218 Turbo PCI series",
  66. "C218 Turbo ISA series",
  67. "C320 Turbo PCI series",
  68. "C320 Turbo ISA series",
  69. "CP-204J series",
  70. };
  71. #ifdef CONFIG_PCI
  72. static struct pci_device_id moxa_pcibrds[] = {
  73. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
  74. .driver_data = MOXA_BOARD_C218_PCI },
  75. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
  76. .driver_data = MOXA_BOARD_C320_PCI },
  77. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
  78. .driver_data = MOXA_BOARD_CP204J },
  79. { 0 }
  80. };
  81. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  82. #endif /* CONFIG_PCI */
  83. struct moxa_isa_board_conf {
  84. int boardType;
  85. int numPorts;
  86. unsigned long baseAddr;
  87. };
  88. static struct moxa_isa_board_conf moxa_isa_boards[] =
  89. {
  90. /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
  91. };
  92. static struct moxa_board_conf {
  93. int boardType;
  94. int numPorts;
  95. unsigned long baseAddr;
  96. int busType;
  97. struct pci_dev *pdev;
  98. int loadstat;
  99. void __iomem *basemem;
  100. void __iomem *intNdx;
  101. void __iomem *intPend;
  102. void __iomem *intTable;
  103. } moxa_boards[MAX_BOARDS];
  104. struct mxser_mstatus {
  105. tcflag_t cflag;
  106. int cts;
  107. int dsr;
  108. int ri;
  109. int dcd;
  110. };
  111. struct moxaq_str {
  112. int inq;
  113. int outq;
  114. };
  115. struct moxa_port {
  116. int type;
  117. int port;
  118. int close_delay;
  119. unsigned short closing_wait;
  120. int count;
  121. int blocked_open;
  122. long event; /* long req'd for set_bit --RR */
  123. int asyncflags;
  124. unsigned long statusflags;
  125. struct tty_struct *tty;
  126. int cflag;
  127. wait_queue_head_t open_wait;
  128. wait_queue_head_t close_wait;
  129. struct timer_list emptyTimer;
  130. char chkPort;
  131. char lineCtrl;
  132. void __iomem *tableAddr;
  133. long curBaud;
  134. char DCDState;
  135. char lowChkFlag;
  136. ushort breakCnt;
  137. };
  138. /* statusflags */
  139. #define TXSTOPPED 0x1
  140. #define LOWWAIT 0x2
  141. #define EMPTYWAIT 0x4
  142. #define THROTTLE 0x8
  143. #define SERIAL_DO_RESTART
  144. #define WAKEUP_CHARS 256
  145. static int verbose = 0;
  146. static int ttymajor = MOXAMAJOR;
  147. /* Variables for insmod */
  148. #ifdef MODULE
  149. static int baseaddr[4];
  150. static int type[4];
  151. static int numports[4];
  152. #endif
  153. MODULE_AUTHOR("William Chen");
  154. MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
  155. MODULE_LICENSE("GPL");
  156. #ifdef MODULE
  157. module_param_array(type, int, NULL, 0);
  158. module_param_array(baseaddr, int, NULL, 0);
  159. module_param_array(numports, int, NULL, 0);
  160. #endif
  161. module_param(ttymajor, int, 0);
  162. module_param(verbose, bool, 0644);
  163. /*
  164. * static functions:
  165. */
  166. static int moxa_open(struct tty_struct *, struct file *);
  167. static void moxa_close(struct tty_struct *, struct file *);
  168. static int moxa_write(struct tty_struct *, const unsigned char *, int);
  169. static int moxa_write_room(struct tty_struct *);
  170. static void moxa_flush_buffer(struct tty_struct *);
  171. static int moxa_chars_in_buffer(struct tty_struct *);
  172. static void moxa_flush_chars(struct tty_struct *);
  173. static void moxa_put_char(struct tty_struct *, unsigned char);
  174. static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
  175. static void moxa_throttle(struct tty_struct *);
  176. static void moxa_unthrottle(struct tty_struct *);
  177. static void moxa_set_termios(struct tty_struct *, struct ktermios *);
  178. static void moxa_stop(struct tty_struct *);
  179. static void moxa_start(struct tty_struct *);
  180. static void moxa_hangup(struct tty_struct *);
  181. static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
  182. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  183. unsigned int set, unsigned int clear);
  184. static void moxa_poll(unsigned long);
  185. static void set_tty_param(struct tty_struct *);
  186. static int block_till_ready(struct tty_struct *, struct file *,
  187. struct moxa_port *);
  188. static void setup_empty_event(struct tty_struct *);
  189. static void check_xmit_empty(unsigned long);
  190. static void shut_down(struct moxa_port *);
  191. static void receive_data(struct moxa_port *);
  192. /*
  193. * moxa board interface functions:
  194. */
  195. static void MoxaDriverInit(void);
  196. static int MoxaDriverIoctl(unsigned int, unsigned long, int);
  197. static int MoxaDriverPoll(void);
  198. static int MoxaPortsOfCard(int);
  199. static int MoxaPortIsValid(int);
  200. static void MoxaPortEnable(int);
  201. static void MoxaPortDisable(int);
  202. static long MoxaPortGetMaxBaud(int);
  203. static long MoxaPortSetBaud(int, long);
  204. static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
  205. static int MoxaPortGetLineOut(int, int *, int *);
  206. static void MoxaPortLineCtrl(int, int, int);
  207. static void MoxaPortFlowCtrl(int, int, int, int, int, int);
  208. static int MoxaPortLineStatus(int);
  209. static int MoxaPortDCDChange(int);
  210. static int MoxaPortDCDON(int);
  211. static void MoxaPortFlushData(int, int);
  212. static int MoxaPortWriteData(int, unsigned char *, int);
  213. static int MoxaPortReadData(int, struct tty_struct *tty);
  214. static int MoxaPortTxQueue(int);
  215. static int MoxaPortRxQueue(int);
  216. static int MoxaPortTxFree(int);
  217. static void MoxaPortTxDisable(int);
  218. static void MoxaPortTxEnable(int);
  219. static int MoxaPortResetBrkCnt(int);
  220. static void MoxaPortSendBreak(int, int);
  221. static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
  222. static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
  223. static void MoxaSetFifo(int port, int enable);
  224. static const struct tty_operations moxa_ops = {
  225. .open = moxa_open,
  226. .close = moxa_close,
  227. .write = moxa_write,
  228. .write_room = moxa_write_room,
  229. .flush_buffer = moxa_flush_buffer,
  230. .chars_in_buffer = moxa_chars_in_buffer,
  231. .flush_chars = moxa_flush_chars,
  232. .put_char = moxa_put_char,
  233. .ioctl = moxa_ioctl,
  234. .throttle = moxa_throttle,
  235. .unthrottle = moxa_unthrottle,
  236. .set_termios = moxa_set_termios,
  237. .stop = moxa_stop,
  238. .start = moxa_start,
  239. .hangup = moxa_hangup,
  240. .tiocmget = moxa_tiocmget,
  241. .tiocmset = moxa_tiocmset,
  242. };
  243. static struct tty_driver *moxaDriver;
  244. static struct moxa_port moxa_ports[MAX_PORTS];
  245. static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
  246. static DEFINE_SPINLOCK(moxa_lock);
  247. #ifdef CONFIG_PCI
  248. static int __devinit moxa_pci_probe(struct pci_dev *pdev,
  249. const struct pci_device_id *ent)
  250. {
  251. struct moxa_board_conf *board;
  252. unsigned int i;
  253. int board_type = ent->driver_data;
  254. int retval;
  255. retval = pci_enable_device(pdev);
  256. if (retval)
  257. goto err;
  258. for (i = 0; i < MAX_BOARDS; i++)
  259. if (moxa_boards[i].basemem == NULL)
  260. break;
  261. retval = -ENODEV;
  262. if (i >= MAX_BOARDS) {
  263. if (verbose)
  264. printk("More than %d MOXA Intellio family boards "
  265. "found. Board is ignored.\n", MAX_BOARDS);
  266. goto err;
  267. }
  268. board = &moxa_boards[i];
  269. board->basemem = pci_iomap(pdev, 2, 0x4000);
  270. if (board->basemem == NULL)
  271. goto err;
  272. board->boardType = board_type;
  273. switch (board_type) {
  274. case MOXA_BOARD_C218_ISA:
  275. case MOXA_BOARD_C218_PCI:
  276. board->numPorts = 8;
  277. break;
  278. case MOXA_BOARD_CP204J:
  279. board->numPorts = 4;
  280. break;
  281. default:
  282. board->numPorts = 0;
  283. break;
  284. }
  285. board->busType = MOXA_BUS_TYPE_PCI;
  286. /* don't lose the reference in the next pci_get_device iteration */
  287. board->pdev = pci_dev_get(pdev);
  288. pci_set_drvdata(pdev, board);
  289. return (0);
  290. err:
  291. return retval;
  292. }
  293. static void __devexit moxa_pci_remove(struct pci_dev *pdev)
  294. {
  295. struct moxa_board_conf *brd = pci_get_drvdata(pdev);
  296. pci_iounmap(pdev, brd->basemem);
  297. brd->basemem = NULL;
  298. pci_dev_put(pdev);
  299. }
  300. #endif /* CONFIG_PCI */
  301. static int __init moxa_init(void)
  302. {
  303. int i, numBoards;
  304. struct moxa_port *ch;
  305. printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
  306. moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
  307. if (!moxaDriver)
  308. return -ENOMEM;
  309. moxaDriver->owner = THIS_MODULE;
  310. moxaDriver->name = "ttyMX";
  311. moxaDriver->major = ttymajor;
  312. moxaDriver->minor_start = 0;
  313. moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
  314. moxaDriver->subtype = SERIAL_TYPE_NORMAL;
  315. moxaDriver->init_termios = tty_std_termios;
  316. moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  317. moxaDriver->init_termios.c_ispeed = 9600;
  318. moxaDriver->init_termios.c_ospeed = 9600;
  319. moxaDriver->flags = TTY_DRIVER_REAL_RAW;
  320. tty_set_operations(moxaDriver, &moxa_ops);
  321. for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
  322. ch->type = PORT_16550A;
  323. ch->port = i;
  324. ch->close_delay = 5 * HZ / 10;
  325. ch->closing_wait = 30 * HZ;
  326. ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
  327. init_waitqueue_head(&ch->open_wait);
  328. init_waitqueue_head(&ch->close_wait);
  329. setup_timer(&ch->emptyTimer, check_xmit_empty,
  330. (unsigned long)ch);
  331. }
  332. printk("Tty devices major number = %d\n", ttymajor);
  333. if (tty_register_driver(moxaDriver)) {
  334. printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
  335. put_tty_driver(moxaDriver);
  336. return -1;
  337. }
  338. mod_timer(&moxaTimer, jiffies + HZ / 50);
  339. /* Find the boards defined in source code */
  340. numBoards = 0;
  341. for (i = 0; i < MAX_BOARDS; i++) {
  342. if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
  343. (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
  344. moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
  345. if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
  346. moxa_boards[numBoards].numPorts = 8;
  347. else
  348. moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
  349. moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
  350. moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
  351. if (verbose)
  352. printk("Board %2d: %s board(baseAddr=%lx)\n",
  353. numBoards + 1,
  354. moxa_brdname[moxa_boards[numBoards].boardType - 1],
  355. moxa_boards[numBoards].baseAddr);
  356. numBoards++;
  357. }
  358. }
  359. /* Find the boards defined form module args. */
  360. #ifdef MODULE
  361. for (i = 0; i < MAX_BOARDS; i++) {
  362. if ((type[i] == MOXA_BOARD_C218_ISA) ||
  363. (type[i] == MOXA_BOARD_C320_ISA)) {
  364. if (verbose)
  365. printk("Board %2d: %s board(baseAddr=%lx)\n",
  366. numBoards + 1,
  367. moxa_brdname[type[i] - 1],
  368. (unsigned long) baseaddr[i]);
  369. if (numBoards >= MAX_BOARDS) {
  370. if (verbose)
  371. printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
  372. continue;
  373. }
  374. moxa_boards[numBoards].boardType = type[i];
  375. if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
  376. moxa_boards[numBoards].numPorts = 8;
  377. else
  378. moxa_boards[numBoards].numPorts = numports[i];
  379. moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
  380. moxa_boards[numBoards].baseAddr = baseaddr[i];
  381. numBoards++;
  382. }
  383. }
  384. #endif
  385. /* Find PCI boards here */
  386. #ifdef CONFIG_PCI
  387. {
  388. struct pci_dev *p = NULL;
  389. int n = ARRAY_SIZE(moxa_pcibrds) - 1;
  390. i = 0;
  391. while (i < n) {
  392. while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
  393. moxa_pci_probe(p, &moxa_pcibrds[i]);
  394. i++;
  395. }
  396. }
  397. #endif
  398. for (i = 0; i < numBoards; i++) {
  399. moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
  400. 0x4000);
  401. }
  402. return (0);
  403. }
  404. static void __exit moxa_exit(void)
  405. {
  406. int i;
  407. if (verbose)
  408. printk("Unloading module moxa ...\n");
  409. del_timer_sync(&moxaTimer);
  410. for (i = 0; i < MAX_PORTS; i++)
  411. del_timer_sync(&moxa_ports[i].emptyTimer);
  412. if (tty_unregister_driver(moxaDriver))
  413. printk("Couldn't unregister MOXA Intellio family serial driver\n");
  414. put_tty_driver(moxaDriver);
  415. for (i = 0; i < MAX_BOARDS; i++) {
  416. #ifdef CONFIG_PCI
  417. if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
  418. moxa_pci_remove(moxa_boards[i].pdev);
  419. #endif
  420. if (moxa_boards[i].basemem)
  421. iounmap(moxa_boards[i].basemem);
  422. }
  423. if (verbose)
  424. printk("Done\n");
  425. }
  426. module_init(moxa_init);
  427. module_exit(moxa_exit);
  428. static int moxa_open(struct tty_struct *tty, struct file *filp)
  429. {
  430. struct moxa_port *ch;
  431. int port;
  432. int retval;
  433. port = tty->index;
  434. if (port == MAX_PORTS) {
  435. return (0);
  436. }
  437. if (!MoxaPortIsValid(port)) {
  438. tty->driver_data = NULL;
  439. return (-ENODEV);
  440. }
  441. ch = &moxa_ports[port];
  442. ch->count++;
  443. tty->driver_data = ch;
  444. ch->tty = tty;
  445. if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
  446. ch->statusflags = 0;
  447. set_tty_param(tty);
  448. MoxaPortLineCtrl(ch->port, 1, 1);
  449. MoxaPortEnable(ch->port);
  450. ch->asyncflags |= ASYNC_INITIALIZED;
  451. }
  452. retval = block_till_ready(tty, filp, ch);
  453. moxa_unthrottle(tty);
  454. if (ch->type == PORT_16550A) {
  455. MoxaSetFifo(ch->port, 1);
  456. } else {
  457. MoxaSetFifo(ch->port, 0);
  458. }
  459. return (retval);
  460. }
  461. static void moxa_close(struct tty_struct *tty, struct file *filp)
  462. {
  463. struct moxa_port *ch;
  464. int port;
  465. port = tty->index;
  466. if (port == MAX_PORTS) {
  467. return;
  468. }
  469. if (!MoxaPortIsValid(port)) {
  470. #ifdef SERIAL_DEBUG_CLOSE
  471. printk("Invalid portno in moxa_close\n");
  472. #endif
  473. tty->driver_data = NULL;
  474. return;
  475. }
  476. if (tty->driver_data == NULL) {
  477. return;
  478. }
  479. if (tty_hung_up_p(filp)) {
  480. return;
  481. }
  482. ch = (struct moxa_port *) tty->driver_data;
  483. if ((tty->count == 1) && (ch->count != 1)) {
  484. printk("moxa_close: bad serial port count; tty->count is 1, "
  485. "ch->count is %d\n", ch->count);
  486. ch->count = 1;
  487. }
  488. if (--ch->count < 0) {
  489. printk("moxa_close: bad serial port count, device=%s\n",
  490. tty->name);
  491. ch->count = 0;
  492. }
  493. if (ch->count) {
  494. return;
  495. }
  496. ch->asyncflags |= ASYNC_CLOSING;
  497. ch->cflag = tty->termios->c_cflag;
  498. if (ch->asyncflags & ASYNC_INITIALIZED) {
  499. setup_empty_event(tty);
  500. tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
  501. del_timer_sync(&moxa_ports[ch->port].emptyTimer);
  502. }
  503. shut_down(ch);
  504. MoxaPortFlushData(port, 2);
  505. if (tty->driver->flush_buffer)
  506. tty->driver->flush_buffer(tty);
  507. tty_ldisc_flush(tty);
  508. tty->closing = 0;
  509. ch->event = 0;
  510. ch->tty = NULL;
  511. if (ch->blocked_open) {
  512. if (ch->close_delay) {
  513. msleep_interruptible(jiffies_to_msecs(ch->close_delay));
  514. }
  515. wake_up_interruptible(&ch->open_wait);
  516. }
  517. ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  518. wake_up_interruptible(&ch->close_wait);
  519. }
  520. static int moxa_write(struct tty_struct *tty,
  521. const unsigned char *buf, int count)
  522. {
  523. struct moxa_port *ch;
  524. int len, port;
  525. unsigned long flags;
  526. ch = (struct moxa_port *) tty->driver_data;
  527. if (ch == NULL)
  528. return (0);
  529. port = ch->port;
  530. spin_lock_irqsave(&moxa_lock, flags);
  531. len = MoxaPortWriteData(port, (unsigned char *) buf, count);
  532. spin_unlock_irqrestore(&moxa_lock, flags);
  533. /*********************************************
  534. if ( !(ch->statusflags & LOWWAIT) &&
  535. ((len != count) || (MoxaPortTxFree(port) <= 100)) )
  536. ************************************************/
  537. ch->statusflags |= LOWWAIT;
  538. return (len);
  539. }
  540. static int moxa_write_room(struct tty_struct *tty)
  541. {
  542. struct moxa_port *ch;
  543. if (tty->stopped)
  544. return (0);
  545. ch = (struct moxa_port *) tty->driver_data;
  546. if (ch == NULL)
  547. return (0);
  548. return (MoxaPortTxFree(ch->port));
  549. }
  550. static void moxa_flush_buffer(struct tty_struct *tty)
  551. {
  552. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  553. if (ch == NULL)
  554. return;
  555. MoxaPortFlushData(ch->port, 1);
  556. tty_wakeup(tty);
  557. }
  558. static int moxa_chars_in_buffer(struct tty_struct *tty)
  559. {
  560. int chars;
  561. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  562. /*
  563. * Sigh...I have to check if driver_data is NULL here, because
  564. * if an open() fails, the TTY subsystem eventually calls
  565. * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
  566. * routine. And since the open() failed, we return 0 here. TDJ
  567. */
  568. if (ch == NULL)
  569. return (0);
  570. chars = MoxaPortTxQueue(ch->port);
  571. if (chars) {
  572. /*
  573. * Make it possible to wakeup anything waiting for output
  574. * in tty_ioctl.c, etc.
  575. */
  576. if (!(ch->statusflags & EMPTYWAIT))
  577. setup_empty_event(tty);
  578. }
  579. return (chars);
  580. }
  581. static void moxa_flush_chars(struct tty_struct *tty)
  582. {
  583. /*
  584. * Don't think I need this, because this is called to empty the TX
  585. * buffer for the 16450, 16550, etc.
  586. */
  587. }
  588. static void moxa_put_char(struct tty_struct *tty, unsigned char c)
  589. {
  590. struct moxa_port *ch;
  591. int port;
  592. unsigned long flags;
  593. ch = (struct moxa_port *) tty->driver_data;
  594. if (ch == NULL)
  595. return;
  596. port = ch->port;
  597. spin_lock_irqsave(&moxa_lock, flags);
  598. MoxaPortWriteData(port, &c, 1);
  599. spin_unlock_irqrestore(&moxa_lock, flags);
  600. /************************************************
  601. if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
  602. *************************************************/
  603. ch->statusflags |= LOWWAIT;
  604. }
  605. static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
  606. {
  607. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  608. int port;
  609. int flag = 0, dtr, rts;
  610. port = tty->index;
  611. if ((port != MAX_PORTS) && (!ch))
  612. return (-EINVAL);
  613. MoxaPortGetLineOut(ch->port, &dtr, &rts);
  614. if (dtr)
  615. flag |= TIOCM_DTR;
  616. if (rts)
  617. flag |= TIOCM_RTS;
  618. dtr = MoxaPortLineStatus(ch->port);
  619. if (dtr & 1)
  620. flag |= TIOCM_CTS;
  621. if (dtr & 2)
  622. flag |= TIOCM_DSR;
  623. if (dtr & 4)
  624. flag |= TIOCM_CD;
  625. return flag;
  626. }
  627. static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
  628. unsigned int set, unsigned int clear)
  629. {
  630. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  631. int port;
  632. int dtr, rts;
  633. port = tty->index;
  634. if ((port != MAX_PORTS) && (!ch))
  635. return (-EINVAL);
  636. MoxaPortGetLineOut(ch->port, &dtr, &rts);
  637. if (set & TIOCM_RTS)
  638. rts = 1;
  639. if (set & TIOCM_DTR)
  640. dtr = 1;
  641. if (clear & TIOCM_RTS)
  642. rts = 0;
  643. if (clear & TIOCM_DTR)
  644. dtr = 0;
  645. MoxaPortLineCtrl(ch->port, dtr, rts);
  646. return 0;
  647. }
  648. static int moxa_ioctl(struct tty_struct *tty, struct file *file,
  649. unsigned int cmd, unsigned long arg)
  650. {
  651. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  652. register int port;
  653. void __user *argp = (void __user *)arg;
  654. int retval;
  655. port = tty->index;
  656. if ((port != MAX_PORTS) && (!ch))
  657. return (-EINVAL);
  658. switch (cmd) {
  659. case TCSBRK: /* SVID version: non-zero arg --> no break */
  660. retval = tty_check_change(tty);
  661. if (retval)
  662. return (retval);
  663. setup_empty_event(tty);
  664. tty_wait_until_sent(tty, 0);
  665. if (!arg)
  666. MoxaPortSendBreak(ch->port, 0);
  667. return (0);
  668. case TCSBRKP: /* support for POSIX tcsendbreak() */
  669. retval = tty_check_change(tty);
  670. if (retval)
  671. return (retval);
  672. setup_empty_event(tty);
  673. tty_wait_until_sent(tty, 0);
  674. MoxaPortSendBreak(ch->port, arg);
  675. return (0);
  676. case TIOCGSOFTCAR:
  677. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
  678. case TIOCSSOFTCAR:
  679. if(get_user(retval, (unsigned long __user *) argp))
  680. return -EFAULT;
  681. arg = retval;
  682. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
  683. (arg ? CLOCAL : 0));
  684. if (C_CLOCAL(tty))
  685. ch->asyncflags &= ~ASYNC_CHECK_CD;
  686. else
  687. ch->asyncflags |= ASYNC_CHECK_CD;
  688. return (0);
  689. case TIOCGSERIAL:
  690. return moxa_get_serial_info(ch, argp);
  691. case TIOCSSERIAL:
  692. return moxa_set_serial_info(ch, argp);
  693. default:
  694. retval = MoxaDriverIoctl(cmd, arg, port);
  695. }
  696. return (retval);
  697. }
  698. static void moxa_throttle(struct tty_struct *tty)
  699. {
  700. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  701. ch->statusflags |= THROTTLE;
  702. }
  703. static void moxa_unthrottle(struct tty_struct *tty)
  704. {
  705. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  706. ch->statusflags &= ~THROTTLE;
  707. }
  708. static void moxa_set_termios(struct tty_struct *tty,
  709. struct ktermios *old_termios)
  710. {
  711. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  712. if (ch == NULL)
  713. return;
  714. set_tty_param(tty);
  715. if (!(old_termios->c_cflag & CLOCAL) &&
  716. (tty->termios->c_cflag & CLOCAL))
  717. wake_up_interruptible(&ch->open_wait);
  718. }
  719. static void moxa_stop(struct tty_struct *tty)
  720. {
  721. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  722. if (ch == NULL)
  723. return;
  724. MoxaPortTxDisable(ch->port);
  725. ch->statusflags |= TXSTOPPED;
  726. }
  727. static void moxa_start(struct tty_struct *tty)
  728. {
  729. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  730. if (ch == NULL)
  731. return;
  732. if (!(ch->statusflags & TXSTOPPED))
  733. return;
  734. MoxaPortTxEnable(ch->port);
  735. ch->statusflags &= ~TXSTOPPED;
  736. }
  737. static void moxa_hangup(struct tty_struct *tty)
  738. {
  739. struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
  740. moxa_flush_buffer(tty);
  741. shut_down(ch);
  742. ch->event = 0;
  743. ch->count = 0;
  744. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  745. ch->tty = NULL;
  746. wake_up_interruptible(&ch->open_wait);
  747. }
  748. static void moxa_poll(unsigned long ignored)
  749. {
  750. register int card;
  751. struct moxa_port *ch;
  752. struct tty_struct *tp;
  753. int i, ports;
  754. del_timer(&moxaTimer);
  755. if (MoxaDriverPoll() < 0) {
  756. mod_timer(&moxaTimer, jiffies + HZ / 50);
  757. return;
  758. }
  759. for (card = 0; card < MAX_BOARDS; card++) {
  760. if ((ports = MoxaPortsOfCard(card)) <= 0)
  761. continue;
  762. ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
  763. for (i = 0; i < ports; i++, ch++) {
  764. if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
  765. continue;
  766. if (!(ch->statusflags & THROTTLE) &&
  767. (MoxaPortRxQueue(ch->port) > 0))
  768. receive_data(ch);
  769. if ((tp = ch->tty) == 0)
  770. continue;
  771. if (ch->statusflags & LOWWAIT) {
  772. if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
  773. if (!tp->stopped) {
  774. ch->statusflags &= ~LOWWAIT;
  775. tty_wakeup(tp);
  776. }
  777. }
  778. }
  779. if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
  780. tty_insert_flip_char(tp, 0, TTY_BREAK);
  781. tty_schedule_flip(tp);
  782. }
  783. if (MoxaPortDCDChange(ch->port)) {
  784. if (ch->asyncflags & ASYNC_CHECK_CD) {
  785. if (MoxaPortDCDON(ch->port))
  786. wake_up_interruptible(&ch->open_wait);
  787. else {
  788. tty_hangup(tp);
  789. wake_up_interruptible(&ch->open_wait);
  790. ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
  791. }
  792. }
  793. }
  794. }
  795. }
  796. mod_timer(&moxaTimer, jiffies + HZ / 50);
  797. }
  798. /******************************************************************************/
  799. static void set_tty_param(struct tty_struct *tty)
  800. {
  801. register struct ktermios *ts;
  802. struct moxa_port *ch;
  803. int rts, cts, txflow, rxflow, xany;
  804. ch = (struct moxa_port *) tty->driver_data;
  805. ts = tty->termios;
  806. if (ts->c_cflag & CLOCAL)
  807. ch->asyncflags &= ~ASYNC_CHECK_CD;
  808. else
  809. ch->asyncflags |= ASYNC_CHECK_CD;
  810. rts = cts = txflow = rxflow = xany = 0;
  811. if (ts->c_cflag & CRTSCTS)
  812. rts = cts = 1;
  813. if (ts->c_iflag & IXON)
  814. txflow = 1;
  815. if (ts->c_iflag & IXOFF)
  816. rxflow = 1;
  817. if (ts->c_iflag & IXANY)
  818. xany = 1;
  819. MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
  820. MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
  821. }
  822. static int block_till_ready(struct tty_struct *tty, struct file *filp,
  823. struct moxa_port *ch)
  824. {
  825. DECLARE_WAITQUEUE(wait,current);
  826. unsigned long flags;
  827. int retval;
  828. int do_clocal = C_CLOCAL(tty);
  829. /*
  830. * If the device is in the middle of being closed, then block
  831. * until it's done, and then try again.
  832. */
  833. if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
  834. if (ch->asyncflags & ASYNC_CLOSING)
  835. interruptible_sleep_on(&ch->close_wait);
  836. #ifdef SERIAL_DO_RESTART
  837. if (ch->asyncflags & ASYNC_HUP_NOTIFY)
  838. return (-EAGAIN);
  839. else
  840. return (-ERESTARTSYS);
  841. #else
  842. return (-EAGAIN);
  843. #endif
  844. }
  845. /*
  846. * If non-blocking mode is set, then make the check up front
  847. * and then exit.
  848. */
  849. if (filp->f_flags & O_NONBLOCK) {
  850. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  851. return (0);
  852. }
  853. /*
  854. * Block waiting for the carrier detect and the line to become free
  855. */
  856. retval = 0;
  857. add_wait_queue(&ch->open_wait, &wait);
  858. #ifdef SERIAL_DEBUG_OPEN
  859. printk("block_til_ready before block: ttys%d, count = %d\n",
  860. ch->line, ch->count);
  861. #endif
  862. spin_lock_irqsave(&moxa_lock, flags);
  863. if (!tty_hung_up_p(filp))
  864. ch->count--;
  865. ch->blocked_open++;
  866. spin_unlock_irqrestore(&moxa_lock, flags);
  867. while (1) {
  868. set_current_state(TASK_INTERRUPTIBLE);
  869. if (tty_hung_up_p(filp) ||
  870. !(ch->asyncflags & ASYNC_INITIALIZED)) {
  871. #ifdef SERIAL_DO_RESTART
  872. if (ch->asyncflags & ASYNC_HUP_NOTIFY)
  873. retval = -EAGAIN;
  874. else
  875. retval = -ERESTARTSYS;
  876. #else
  877. retval = -EAGAIN;
  878. #endif
  879. break;
  880. }
  881. if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
  882. MoxaPortDCDON(ch->port)))
  883. break;
  884. if (signal_pending(current)) {
  885. retval = -ERESTARTSYS;
  886. break;
  887. }
  888. schedule();
  889. }
  890. set_current_state(TASK_RUNNING);
  891. remove_wait_queue(&ch->open_wait, &wait);
  892. spin_lock_irqsave(&moxa_lock, flags);
  893. if (!tty_hung_up_p(filp))
  894. ch->count++;
  895. ch->blocked_open--;
  896. spin_unlock_irqrestore(&moxa_lock, flags);
  897. #ifdef SERIAL_DEBUG_OPEN
  898. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  899. ch->line, ch->count);
  900. #endif
  901. if (retval)
  902. return (retval);
  903. /* FIXME: review to see if we need to use set_bit on these */
  904. ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
  905. return 0;
  906. }
  907. static void setup_empty_event(struct tty_struct *tty)
  908. {
  909. struct moxa_port *ch = tty->driver_data;
  910. unsigned long flags;
  911. spin_lock_irqsave(&moxa_lock, flags);
  912. ch->statusflags |= EMPTYWAIT;
  913. mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
  914. spin_unlock_irqrestore(&moxa_lock, flags);
  915. }
  916. static void check_xmit_empty(unsigned long data)
  917. {
  918. struct moxa_port *ch;
  919. ch = (struct moxa_port *) data;
  920. del_timer_sync(&moxa_ports[ch->port].emptyTimer);
  921. if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
  922. if (MoxaPortTxQueue(ch->port) == 0) {
  923. ch->statusflags &= ~EMPTYWAIT;
  924. tty_wakeup(ch->tty);
  925. return;
  926. }
  927. mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
  928. } else
  929. ch->statusflags &= ~EMPTYWAIT;
  930. }
  931. static void shut_down(struct moxa_port *ch)
  932. {
  933. struct tty_struct *tp;
  934. if (!(ch->asyncflags & ASYNC_INITIALIZED))
  935. return;
  936. tp = ch->tty;
  937. MoxaPortDisable(ch->port);
  938. /*
  939. * If we're a modem control device and HUPCL is on, drop RTS & DTR.
  940. */
  941. if (tp->termios->c_cflag & HUPCL)
  942. MoxaPortLineCtrl(ch->port, 0, 0);
  943. ch->asyncflags &= ~ASYNC_INITIALIZED;
  944. }
  945. static void receive_data(struct moxa_port *ch)
  946. {
  947. struct tty_struct *tp;
  948. struct ktermios *ts;
  949. unsigned long flags;
  950. ts = NULL;
  951. tp = ch->tty;
  952. if (tp)
  953. ts = tp->termios;
  954. /**************************************************
  955. if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
  956. *****************************************************/
  957. if (!tp || !ts) {
  958. MoxaPortFlushData(ch->port, 0);
  959. return;
  960. }
  961. spin_lock_irqsave(&moxa_lock, flags);
  962. MoxaPortReadData(ch->port, tp);
  963. spin_unlock_irqrestore(&moxa_lock, flags);
  964. tty_schedule_flip(tp);
  965. }
  966. #define Magic_code 0x404
  967. /*
  968. * System Configuration
  969. */
  970. /*
  971. * for C218 BIOS initialization
  972. */
  973. #define C218_ConfBase 0x800
  974. #define C218_status (C218_ConfBase + 0) /* BIOS running status */
  975. #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
  976. #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
  977. #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
  978. #define C218check_sum (C218_ConfBase + 8) /* BYTE */
  979. #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
  980. #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
  981. #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
  982. #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
  983. #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
  984. #define C218_LoadBuf 0x0F00
  985. #define C218_KeyCode 0x218
  986. #define CP204J_KeyCode 0x204
  987. /*
  988. * for C320 BIOS initialization
  989. */
  990. #define C320_ConfBase 0x800
  991. #define C320_LoadBuf 0x0f00
  992. #define STS_init 0x05 /* for C320_status */
  993. #define C320_status C320_ConfBase + 0 /* BIOS running status */
  994. #define C320_diag C320_ConfBase + 2 /* diagnostic status */
  995. #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
  996. #define C320DLoad_len C320_ConfBase + 6 /* WORD */
  997. #define C320check_sum C320_ConfBase + 8 /* WORD */
  998. #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
  999. #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
  1000. #define C320UART_no C320_ConfBase + 0x0e /* WORD */
  1001. #define C320_KeyCode 0x320
  1002. #define FixPage_addr 0x0000 /* starting addr of static page */
  1003. #define DynPage_addr 0x2000 /* starting addr of dynamic page */
  1004. #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
  1005. #define Control_reg 0x1ff0 /* select page and reset control */
  1006. #define HW_reset 0x80
  1007. /*
  1008. * Function Codes
  1009. */
  1010. #define FC_CardReset 0x80
  1011. #define FC_ChannelReset 1 /* C320 firmware not supported */
  1012. #define FC_EnableCH 2
  1013. #define FC_DisableCH 3
  1014. #define FC_SetParam 4
  1015. #define FC_SetMode 5
  1016. #define FC_SetRate 6
  1017. #define FC_LineControl 7
  1018. #define FC_LineStatus 8
  1019. #define FC_XmitControl 9
  1020. #define FC_FlushQueue 10
  1021. #define FC_SendBreak 11
  1022. #define FC_StopBreak 12
  1023. #define FC_LoopbackON 13
  1024. #define FC_LoopbackOFF 14
  1025. #define FC_ClrIrqTable 15
  1026. #define FC_SendXon 16
  1027. #define FC_SetTermIrq 17 /* C320 firmware not supported */
  1028. #define FC_SetCntIrq 18 /* C320 firmware not supported */
  1029. #define FC_SetBreakIrq 19
  1030. #define FC_SetLineIrq 20
  1031. #define FC_SetFlowCtl 21
  1032. #define FC_GenIrq 22
  1033. #define FC_InCD180 23
  1034. #define FC_OutCD180 24
  1035. #define FC_InUARTreg 23
  1036. #define FC_OutUARTreg 24
  1037. #define FC_SetXonXoff 25
  1038. #define FC_OutCD180CCR 26
  1039. #define FC_ExtIQueue 27
  1040. #define FC_ExtOQueue 28
  1041. #define FC_ClrLineIrq 29
  1042. #define FC_HWFlowCtl 30
  1043. #define FC_GetClockRate 35
  1044. #define FC_SetBaud 36
  1045. #define FC_SetDataMode 41
  1046. #define FC_GetCCSR 43
  1047. #define FC_GetDataError 45
  1048. #define FC_RxControl 50
  1049. #define FC_ImmSend 51
  1050. #define FC_SetXonState 52
  1051. #define FC_SetXoffState 53
  1052. #define FC_SetRxFIFOTrig 54
  1053. #define FC_SetTxFIFOCnt 55
  1054. #define FC_UnixRate 56
  1055. #define FC_UnixResetTimer 57
  1056. #define RxFIFOTrig1 0
  1057. #define RxFIFOTrig4 1
  1058. #define RxFIFOTrig8 2
  1059. #define RxFIFOTrig14 3
  1060. /*
  1061. * Dual-Ported RAM
  1062. */
  1063. #define DRAM_global 0
  1064. #define INT_data (DRAM_global + 0)
  1065. #define Config_base (DRAM_global + 0x108)
  1066. #define IRQindex (INT_data + 0)
  1067. #define IRQpending (INT_data + 4)
  1068. #define IRQtable (INT_data + 8)
  1069. /*
  1070. * Interrupt Status
  1071. */
  1072. #define IntrRx 0x01 /* receiver data O.K. */
  1073. #define IntrTx 0x02 /* transmit buffer empty */
  1074. #define IntrFunc 0x04 /* function complete */
  1075. #define IntrBreak 0x08 /* received break */
  1076. #define IntrLine 0x10 /* line status change
  1077. for transmitter */
  1078. #define IntrIntr 0x20 /* received INTR code */
  1079. #define IntrQuit 0x40 /* received QUIT code */
  1080. #define IntrEOF 0x80 /* received EOF code */
  1081. #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
  1082. #define IntrTxTrigger 0x200 /* tx data count below trigger value */
  1083. #define Magic_no (Config_base + 0)
  1084. #define Card_model_no (Config_base + 2)
  1085. #define Total_ports (Config_base + 4)
  1086. #define Module_cnt (Config_base + 8)
  1087. #define Module_no (Config_base + 10)
  1088. #define Timer_10ms (Config_base + 14)
  1089. #define Disable_IRQ (Config_base + 20)
  1090. #define TMS320_PORT1 (Config_base + 22)
  1091. #define TMS320_PORT2 (Config_base + 24)
  1092. #define TMS320_CLOCK (Config_base + 26)
  1093. /*
  1094. * DATA BUFFER in DRAM
  1095. */
  1096. #define Extern_table 0x400 /* Base address of the external table
  1097. (24 words * 64) total 3K bytes
  1098. (24 words * 128) total 6K bytes */
  1099. #define Extern_size 0x60 /* 96 bytes */
  1100. #define RXrptr 0x00 /* read pointer for RX buffer */
  1101. #define RXwptr 0x02 /* write pointer for RX buffer */
  1102. #define TXrptr 0x04 /* read pointer for TX buffer */
  1103. #define TXwptr 0x06 /* write pointer for TX buffer */
  1104. #define HostStat 0x08 /* IRQ flag and general flag */
  1105. #define FlagStat 0x0A
  1106. #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
  1107. /* x x x x | | | | */
  1108. /* | | | + CTS flow */
  1109. /* | | +--- RTS flow */
  1110. /* | +------ TX Xon/Xoff */
  1111. /* +--------- RX Xon/Xoff */
  1112. #define Break_cnt 0x0E /* received break count */
  1113. #define CD180TXirq 0x10 /* if non-0: enable TX irq */
  1114. #define RX_mask 0x12
  1115. #define TX_mask 0x14
  1116. #define Ofs_rxb 0x16
  1117. #define Ofs_txb 0x18
  1118. #define Page_rxb 0x1A
  1119. #define Page_txb 0x1C
  1120. #define EndPage_rxb 0x1E
  1121. #define EndPage_txb 0x20
  1122. #define Data_error 0x22
  1123. #define RxTrigger 0x28
  1124. #define TxTrigger 0x2a
  1125. #define rRXwptr 0x34
  1126. #define Low_water 0x36
  1127. #define FuncCode 0x40
  1128. #define FuncArg 0x42
  1129. #define FuncArg1 0x44
  1130. #define C218rx_size 0x2000 /* 8K bytes */
  1131. #define C218tx_size 0x8000 /* 32K bytes */
  1132. #define C218rx_mask (C218rx_size - 1)
  1133. #define C218tx_mask (C218tx_size - 1)
  1134. #define C320p8rx_size 0x2000
  1135. #define C320p8tx_size 0x8000
  1136. #define C320p8rx_mask (C320p8rx_size - 1)
  1137. #define C320p8tx_mask (C320p8tx_size - 1)
  1138. #define C320p16rx_size 0x2000
  1139. #define C320p16tx_size 0x4000
  1140. #define C320p16rx_mask (C320p16rx_size - 1)
  1141. #define C320p16tx_mask (C320p16tx_size - 1)
  1142. #define C320p24rx_size 0x2000
  1143. #define C320p24tx_size 0x2000
  1144. #define C320p24rx_mask (C320p24rx_size - 1)
  1145. #define C320p24tx_mask (C320p24tx_size - 1)
  1146. #define C320p32rx_size 0x1000
  1147. #define C320p32tx_size 0x1000
  1148. #define C320p32rx_mask (C320p32rx_size - 1)
  1149. #define C320p32tx_mask (C320p32tx_size - 1)
  1150. #define Page_size 0x2000
  1151. #define Page_mask (Page_size - 1)
  1152. #define C218rx_spage 3
  1153. #define C218tx_spage 4
  1154. #define C218rx_pageno 1
  1155. #define C218tx_pageno 4
  1156. #define C218buf_pageno 5
  1157. #define C320p8rx_spage 3
  1158. #define C320p8tx_spage 4
  1159. #define C320p8rx_pgno 1
  1160. #define C320p8tx_pgno 4
  1161. #define C320p8buf_pgno 5
  1162. #define C320p16rx_spage 3
  1163. #define C320p16tx_spage 4
  1164. #define C320p16rx_pgno 1
  1165. #define C320p16tx_pgno 2
  1166. #define C320p16buf_pgno 3
  1167. #define C320p24rx_spage 3
  1168. #define C320p24tx_spage 4
  1169. #define C320p24rx_pgno 1
  1170. #define C320p24tx_pgno 1
  1171. #define C320p24buf_pgno 2
  1172. #define C320p32rx_spage 3
  1173. #define C320p32tx_ofs C320p32rx_size
  1174. #define C320p32tx_spage 3
  1175. #define C320p32buf_pgno 1
  1176. /*
  1177. * Host Status
  1178. */
  1179. #define WakeupRx 0x01
  1180. #define WakeupTx 0x02
  1181. #define WakeupBreak 0x08
  1182. #define WakeupLine 0x10
  1183. #define WakeupIntr 0x20
  1184. #define WakeupQuit 0x40
  1185. #define WakeupEOF 0x80 /* used in VTIME control */
  1186. #define WakeupRxTrigger 0x100
  1187. #define WakeupTxTrigger 0x200
  1188. /*
  1189. * Flag status
  1190. */
  1191. #define Rx_over 0x01
  1192. #define Xoff_state 0x02
  1193. #define Tx_flowOff 0x04
  1194. #define Tx_enable 0x08
  1195. #define CTS_state 0x10
  1196. #define DSR_state 0x20
  1197. #define DCD_state 0x80
  1198. /*
  1199. * FlowControl
  1200. */
  1201. #define CTS_FlowCtl 1
  1202. #define RTS_FlowCtl 2
  1203. #define Tx_FlowCtl 4
  1204. #define Rx_FlowCtl 8
  1205. #define IXM_IXANY 0x10
  1206. #define LowWater 128
  1207. #define DTR_ON 1
  1208. #define RTS_ON 2
  1209. #define CTS_ON 1
  1210. #define DSR_ON 2
  1211. #define DCD_ON 8
  1212. /* mode definition */
  1213. #define MX_CS8 0x03
  1214. #define MX_CS7 0x02
  1215. #define MX_CS6 0x01
  1216. #define MX_CS5 0x00
  1217. #define MX_STOP1 0x00
  1218. #define MX_STOP15 0x04
  1219. #define MX_STOP2 0x08
  1220. #define MX_PARNONE 0x00
  1221. #define MX_PAREVEN 0x40
  1222. #define MX_PARODD 0xC0
  1223. /*
  1224. * Query
  1225. */
  1226. struct mon_str {
  1227. int tick;
  1228. int rxcnt[MAX_PORTS];
  1229. int txcnt[MAX_PORTS];
  1230. };
  1231. #define DCD_changed 0x01
  1232. #define DCD_oldstate 0x80
  1233. static unsigned char moxaBuff[10240];
  1234. static int moxaLowWaterChk;
  1235. static int moxaCard;
  1236. static struct mon_str moxaLog;
  1237. static int moxaFuncTout = HZ / 2;
  1238. static void moxadelay(int);
  1239. static void moxafunc(void __iomem *, int, ushort);
  1240. static void wait_finish(void __iomem *);
  1241. static void low_water_check(void __iomem *);
  1242. static int moxaloadbios(int, unsigned char __user *, int);
  1243. static int moxafindcard(int);
  1244. static int moxaload320b(int, unsigned char __user *, int);
  1245. static int moxaloadcode(int, unsigned char __user *, int);
  1246. static int moxaloadc218(int, void __iomem *, int);
  1247. static int moxaloadc320(int, void __iomem *, int, int *);
  1248. /*****************************************************************************
  1249. * Driver level functions: *
  1250. * 1. MoxaDriverInit(void); *
  1251. * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
  1252. * 3. MoxaDriverPoll(void); *
  1253. *****************************************************************************/
  1254. void MoxaDriverInit(void)
  1255. {
  1256. struct moxa_port *p;
  1257. unsigned int i;
  1258. moxaFuncTout = HZ / 2; /* 500 mini-seconds */
  1259. moxaCard = 0;
  1260. moxaLog.tick = 0;
  1261. moxaLowWaterChk = 0;
  1262. for (i = 0; i < MAX_PORTS; i++) {
  1263. p = &moxa_ports[i];
  1264. p->chkPort = 0;
  1265. p->lowChkFlag = 0;
  1266. p->lineCtrl = 0;
  1267. moxaLog.rxcnt[i] = 0;
  1268. moxaLog.txcnt[i] = 0;
  1269. }
  1270. }
  1271. #define MOXA 0x400
  1272. #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
  1273. #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
  1274. #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
  1275. #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
  1276. #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
  1277. #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
  1278. #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
  1279. #define MOXA_GETDATACOUNT (MOXA + 23)
  1280. #define MOXA_GET_IOQUEUE (MOXA + 27)
  1281. #define MOXA_FLUSH_QUEUE (MOXA + 28)
  1282. #define MOXA_GET_CONF (MOXA + 35) /* configuration */
  1283. #define MOXA_GET_MAJOR (MOXA + 63)
  1284. #define MOXA_GET_CUMAJOR (MOXA + 64)
  1285. #define MOXA_GETMSTATUS (MOXA + 65)
  1286. struct dl_str {
  1287. char __user *buf;
  1288. int len;
  1289. int cardno;
  1290. };
  1291. static struct dl_str dltmp;
  1292. void MoxaPortFlushData(int port, int mode)
  1293. {
  1294. void __iomem *ofsAddr;
  1295. if ((mode < 0) || (mode > 2))
  1296. return;
  1297. ofsAddr = moxa_ports[port].tableAddr;
  1298. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1299. if (mode != 1) {
  1300. moxa_ports[port].lowChkFlag = 0;
  1301. low_water_check(ofsAddr);
  1302. }
  1303. }
  1304. int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
  1305. {
  1306. int i;
  1307. int status;
  1308. int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
  1309. void __user *argp = (void __user *)arg;
  1310. if (port == MAX_PORTS) {
  1311. if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
  1312. (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
  1313. (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
  1314. (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
  1315. (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
  1316. return (-EINVAL);
  1317. }
  1318. switch (cmd) {
  1319. case MOXA_GET_CONF:
  1320. if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
  1321. sizeof(struct moxa_board_conf)))
  1322. return -EFAULT;
  1323. return (0);
  1324. case MOXA_INIT_DRIVER:
  1325. if ((int) arg == 0x404)
  1326. MoxaDriverInit();
  1327. return (0);
  1328. case MOXA_GETDATACOUNT:
  1329. moxaLog.tick = jiffies;
  1330. if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
  1331. return -EFAULT;
  1332. return (0);
  1333. case MOXA_FLUSH_QUEUE:
  1334. MoxaPortFlushData(port, arg);
  1335. return (0);
  1336. case MOXA_GET_IOQUEUE: {
  1337. struct moxaq_str __user *argm = argp;
  1338. struct moxaq_str tmp;
  1339. for (i = 0; i < MAX_PORTS; i++, argm++) {
  1340. memset(&tmp, 0, sizeof(tmp));
  1341. if (moxa_ports[i].chkPort) {
  1342. tmp.inq = MoxaPortRxQueue(i);
  1343. tmp.outq = MoxaPortTxQueue(i);
  1344. }
  1345. if (copy_to_user(argm, &tmp, sizeof(tmp)))
  1346. return -EFAULT;
  1347. }
  1348. return (0);
  1349. } case MOXA_GET_OQUEUE:
  1350. i = MoxaPortTxQueue(port);
  1351. return put_user(i, (unsigned long __user *)argp);
  1352. case MOXA_GET_IQUEUE:
  1353. i = MoxaPortRxQueue(port);
  1354. return put_user(i, (unsigned long __user *)argp);
  1355. case MOXA_GET_MAJOR:
  1356. if(copy_to_user(argp, &ttymajor, sizeof(int)))
  1357. return -EFAULT;
  1358. return 0;
  1359. case MOXA_GET_CUMAJOR:
  1360. i = 0;
  1361. if(copy_to_user(argp, &i, sizeof(int)))
  1362. return -EFAULT;
  1363. return 0;
  1364. case MOXA_GETMSTATUS: {
  1365. struct mxser_mstatus __user *argm = argp;
  1366. struct mxser_mstatus tmp;
  1367. struct moxa_port *p;
  1368. for (i = 0; i < MAX_PORTS; i++, argm++) {
  1369. p = &moxa_ports[i];
  1370. memset(&tmp, 0, sizeof(tmp));
  1371. if (!p->chkPort) {
  1372. goto copy;
  1373. } else {
  1374. status = MoxaPortLineStatus(p->port);
  1375. if (status & 1)
  1376. tmp.cts = 1;
  1377. if (status & 2)
  1378. tmp.dsr = 1;
  1379. if (status & 4)
  1380. tmp.dcd = 1;
  1381. }
  1382. if (!p->tty || !p->tty->termios)
  1383. tmp.cflag = p->cflag;
  1384. else
  1385. tmp.cflag = p->tty->termios->c_cflag;
  1386. copy:
  1387. if (copy_to_user(argm, &tmp, sizeof(tmp)))
  1388. return -EFAULT;
  1389. }
  1390. return 0;
  1391. } default:
  1392. return (-ENOIOCTLCMD);
  1393. case MOXA_LOAD_BIOS:
  1394. case MOXA_FIND_BOARD:
  1395. case MOXA_LOAD_C320B:
  1396. case MOXA_LOAD_CODE:
  1397. if (!capable(CAP_SYS_RAWIO))
  1398. return -EPERM;
  1399. break;
  1400. }
  1401. if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
  1402. return -EFAULT;
  1403. if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
  1404. return -EINVAL;
  1405. switch(cmd)
  1406. {
  1407. case MOXA_LOAD_BIOS:
  1408. i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
  1409. return (i);
  1410. case MOXA_FIND_BOARD:
  1411. return moxafindcard(dltmp.cardno);
  1412. case MOXA_LOAD_C320B:
  1413. moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
  1414. default: /* to keep gcc happy */
  1415. return (0);
  1416. case MOXA_LOAD_CODE:
  1417. i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
  1418. if (i == -1)
  1419. return (-EFAULT);
  1420. return (i);
  1421. }
  1422. }
  1423. int MoxaDriverPoll(void)
  1424. {
  1425. struct moxa_board_conf *brd;
  1426. register ushort temp;
  1427. register int card;
  1428. void __iomem *ofsAddr;
  1429. void __iomem *ip;
  1430. int port, p, ports;
  1431. if (moxaCard == 0)
  1432. return (-1);
  1433. for (card = 0; card < MAX_BOARDS; card++) {
  1434. brd = &moxa_boards[card];
  1435. if (brd->loadstat == 0)
  1436. continue;
  1437. if ((ports = brd->numPorts) == 0)
  1438. continue;
  1439. if (readb(brd->intPend) == 0xff) {
  1440. ip = brd->intTable + readb(brd->intNdx);
  1441. p = card * MAX_PORTS_PER_BOARD;
  1442. ports <<= 1;
  1443. for (port = 0; port < ports; port += 2, p++) {
  1444. if ((temp = readw(ip + port)) != 0) {
  1445. writew(0, ip + port);
  1446. ofsAddr = moxa_ports[p].tableAddr;
  1447. if (temp & IntrTx)
  1448. writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
  1449. if (temp & IntrBreak) {
  1450. moxa_ports[p].breakCnt++;
  1451. }
  1452. if (temp & IntrLine) {
  1453. if (readb(ofsAddr + FlagStat) & DCD_state) {
  1454. if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
  1455. moxa_ports[p].DCDState = (DCD_oldstate |
  1456. DCD_changed);
  1457. } else {
  1458. if (moxa_ports[p].DCDState & DCD_oldstate)
  1459. moxa_ports[p].DCDState = DCD_changed;
  1460. }
  1461. }
  1462. }
  1463. }
  1464. writeb(0, brd->intPend);
  1465. }
  1466. if (moxaLowWaterChk) {
  1467. p = card * MAX_PORTS_PER_BOARD;
  1468. for (port = 0; port < ports; port++, p++) {
  1469. if (moxa_ports[p].lowChkFlag) {
  1470. moxa_ports[p].lowChkFlag = 0;
  1471. ofsAddr = moxa_ports[p].tableAddr;
  1472. low_water_check(ofsAddr);
  1473. }
  1474. }
  1475. }
  1476. }
  1477. moxaLowWaterChk = 0;
  1478. return (0);
  1479. }
  1480. /*****************************************************************************
  1481. * Card level function: *
  1482. * 1. MoxaPortsOfCard(int cardno); *
  1483. *****************************************************************************/
  1484. int MoxaPortsOfCard(int cardno)
  1485. {
  1486. if (moxa_boards[cardno].boardType == 0)
  1487. return (0);
  1488. return (moxa_boards[cardno].numPorts);
  1489. }
  1490. /*****************************************************************************
  1491. * Port level functions: *
  1492. * 1. MoxaPortIsValid(int port); *
  1493. * 2. MoxaPortEnable(int port); *
  1494. * 3. MoxaPortDisable(int port); *
  1495. * 4. MoxaPortGetMaxBaud(int port); *
  1496. * 6. MoxaPortSetBaud(int port, long baud); *
  1497. * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
  1498. * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
  1499. * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
  1500. * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
  1501. * 12. MoxaPortLineStatus(int port); *
  1502. * 13. MoxaPortDCDChange(int port); *
  1503. * 14. MoxaPortDCDON(int port); *
  1504. * 15. MoxaPortFlushData(int port, int mode); *
  1505. * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
  1506. * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
  1507. * 20. MoxaPortTxQueue(int port); *
  1508. * 21. MoxaPortTxFree(int port); *
  1509. * 22. MoxaPortRxQueue(int port); *
  1510. * 24. MoxaPortTxDisable(int port); *
  1511. * 25. MoxaPortTxEnable(int port); *
  1512. * 27. MoxaPortResetBrkCnt(int port); *
  1513. * 30. MoxaPortSendBreak(int port, int ticks); *
  1514. *****************************************************************************/
  1515. /*
  1516. * Moxa Port Number Description:
  1517. *
  1518. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1519. * the port number using in MOXA driver functions will be 0 to 31 for
  1520. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1521. * to 127 for fourth. For example, if you setup three MOXA boards,
  1522. * first board is C218, second board is C320-16 and third board is
  1523. * C320-32. The port number of first board (C218 - 8 ports) is from
  1524. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1525. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1526. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1527. * 127 will be invalid.
  1528. *
  1529. *
  1530. * Moxa Functions Description:
  1531. *
  1532. * Function 1: Driver initialization routine, this routine must be
  1533. * called when initialized driver.
  1534. * Syntax:
  1535. * void MoxaDriverInit();
  1536. *
  1537. *
  1538. * Function 2: Moxa driver private IOCTL command processing.
  1539. * Syntax:
  1540. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1541. *
  1542. * unsigned int cmd : IOCTL command
  1543. * unsigned long arg : IOCTL argument
  1544. * int port : port number (0 - 127)
  1545. *
  1546. * return: 0 (OK)
  1547. * -EINVAL
  1548. * -ENOIOCTLCMD
  1549. *
  1550. *
  1551. * Function 3: Moxa driver polling process routine.
  1552. * Syntax:
  1553. * int MoxaDriverPoll(void);
  1554. *
  1555. * return: 0 ; polling O.K.
  1556. * -1 : no any Moxa card.
  1557. *
  1558. *
  1559. * Function 4: Get the ports of this card.
  1560. * Syntax:
  1561. * int MoxaPortsOfCard(int cardno);
  1562. *
  1563. * int cardno : card number (0 - 3)
  1564. *
  1565. * return: 0 : this card is invalid
  1566. * 8/16/24/32
  1567. *
  1568. *
  1569. * Function 5: Check this port is valid or invalid
  1570. * Syntax:
  1571. * int MoxaPortIsValid(int port);
  1572. * int port : port number (0 - 127, ref port description)
  1573. *
  1574. * return: 0 : this port is invalid
  1575. * 1 : this port is valid
  1576. *
  1577. *
  1578. * Function 6: Enable this port to start Tx/Rx data.
  1579. * Syntax:
  1580. * void MoxaPortEnable(int port);
  1581. * int port : port number (0 - 127)
  1582. *
  1583. *
  1584. * Function 7: Disable this port
  1585. * Syntax:
  1586. * void MoxaPortDisable(int port);
  1587. * int port : port number (0 - 127)
  1588. *
  1589. *
  1590. * Function 8: Get the maximun available baud rate of this port.
  1591. * Syntax:
  1592. * long MoxaPortGetMaxBaud(int port);
  1593. * int port : port number (0 - 127)
  1594. *
  1595. * return: 0 : this port is invalid
  1596. * 38400/57600/115200 bps
  1597. *
  1598. *
  1599. * Function 10: Setting baud rate of this port.
  1600. * Syntax:
  1601. * long MoxaPortSetBaud(int port, long baud);
  1602. * int port : port number (0 - 127)
  1603. * long baud : baud rate (50 - 115200)
  1604. *
  1605. * return: 0 : this port is invalid or baud < 50
  1606. * 50 - 115200 : the real baud rate set to the port, if
  1607. * the argument baud is large than maximun
  1608. * available baud rate, the real setting
  1609. * baud rate will be the maximun baud rate.
  1610. *
  1611. *
  1612. * Function 12: Configure the port.
  1613. * Syntax:
  1614. * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
  1615. * int port : port number (0 - 127)
  1616. * struct ktermios * termio : termio structure pointer
  1617. * speed_t baud : baud rate
  1618. *
  1619. * return: -1 : this port is invalid or termio == NULL
  1620. * 0 : setting O.K.
  1621. *
  1622. *
  1623. * Function 13: Get the DTR/RTS state of this port.
  1624. * Syntax:
  1625. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1626. * int port : port number (0 - 127)
  1627. * int * dtrState : pointer to INT to receive the current DTR
  1628. * state. (if NULL, this function will not
  1629. * write to this address)
  1630. * int * rtsState : pointer to INT to receive the current RTS
  1631. * state. (if NULL, this function will not
  1632. * write to this address)
  1633. *
  1634. * return: -1 : this port is invalid
  1635. * 0 : O.K.
  1636. *
  1637. *
  1638. * Function 14: Setting the DTR/RTS output state of this port.
  1639. * Syntax:
  1640. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1641. * int port : port number (0 - 127)
  1642. * int dtrState : DTR output state (0: off, 1: on)
  1643. * int rtsState : RTS output state (0: off, 1: on)
  1644. *
  1645. *
  1646. * Function 15: Setting the flow control of this port.
  1647. * Syntax:
  1648. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1649. * int txFlow,int xany);
  1650. * int port : port number (0 - 127)
  1651. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1652. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1653. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1654. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1655. * int xany : S/W XANY flow control (0: no, 1: yes)
  1656. *
  1657. *
  1658. * Function 16: Get ths line status of this port
  1659. * Syntax:
  1660. * int MoxaPortLineStatus(int port);
  1661. * int port : port number (0 - 127)
  1662. *
  1663. * return: Bit 0 - CTS state (0: off, 1: on)
  1664. * Bit 1 - DSR state (0: off, 1: on)
  1665. * Bit 2 - DCD state (0: off, 1: on)
  1666. *
  1667. *
  1668. * Function 17: Check the DCD state has changed since the last read
  1669. * of this function.
  1670. * Syntax:
  1671. * int MoxaPortDCDChange(int port);
  1672. * int port : port number (0 - 127)
  1673. *
  1674. * return: 0 : no changed
  1675. * 1 : DCD has changed
  1676. *
  1677. *
  1678. * Function 18: Check ths current DCD state is ON or not.
  1679. * Syntax:
  1680. * int MoxaPortDCDON(int port);
  1681. * int port : port number (0 - 127)
  1682. *
  1683. * return: 0 : DCD off
  1684. * 1 : DCD on
  1685. *
  1686. *
  1687. * Function 19: Flush the Rx/Tx buffer data of this port.
  1688. * Syntax:
  1689. * void MoxaPortFlushData(int port, int mode);
  1690. * int port : port number (0 - 127)
  1691. * int mode
  1692. * 0 : flush the Rx buffer
  1693. * 1 : flush the Tx buffer
  1694. * 2 : flush the Rx and Tx buffer
  1695. *
  1696. *
  1697. * Function 20: Write data.
  1698. * Syntax:
  1699. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1700. * int port : port number (0 - 127)
  1701. * unsigned char * buffer : pointer to write data buffer.
  1702. * int length : write data length
  1703. *
  1704. * return: 0 - length : real write data length
  1705. *
  1706. *
  1707. * Function 21: Read data.
  1708. * Syntax:
  1709. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1710. * int port : port number (0 - 127)
  1711. * struct tty_struct *tty : tty for data
  1712. *
  1713. * return: 0 - length : real read data length
  1714. *
  1715. *
  1716. * Function 24: Get the Tx buffer current queued data bytes
  1717. * Syntax:
  1718. * int MoxaPortTxQueue(int port);
  1719. * int port : port number (0 - 127)
  1720. *
  1721. * return: .. : Tx buffer current queued data bytes
  1722. *
  1723. *
  1724. * Function 25: Get the Tx buffer current free space
  1725. * Syntax:
  1726. * int MoxaPortTxFree(int port);
  1727. * int port : port number (0 - 127)
  1728. *
  1729. * return: .. : Tx buffer current free space
  1730. *
  1731. *
  1732. * Function 26: Get the Rx buffer current queued data bytes
  1733. * Syntax:
  1734. * int MoxaPortRxQueue(int port);
  1735. * int port : port number (0 - 127)
  1736. *
  1737. * return: .. : Rx buffer current queued data bytes
  1738. *
  1739. *
  1740. * Function 28: Disable port data transmission.
  1741. * Syntax:
  1742. * void MoxaPortTxDisable(int port);
  1743. * int port : port number (0 - 127)
  1744. *
  1745. *
  1746. * Function 29: Enable port data transmission.
  1747. * Syntax:
  1748. * void MoxaPortTxEnable(int port);
  1749. * int port : port number (0 - 127)
  1750. *
  1751. *
  1752. * Function 31: Get the received BREAK signal count and reset it.
  1753. * Syntax:
  1754. * int MoxaPortResetBrkCnt(int port);
  1755. * int port : port number (0 - 127)
  1756. *
  1757. * return: 0 - .. : BREAK signal count
  1758. *
  1759. *
  1760. * Function 34: Send out a BREAK signal.
  1761. * Syntax:
  1762. * void MoxaPortSendBreak(int port, int ms100);
  1763. * int port : port number (0 - 127)
  1764. * int ms100 : break signal time interval.
  1765. * unit: 100 mini-second. if ms100 == 0, it will
  1766. * send out a about 250 ms BREAK signal.
  1767. *
  1768. */
  1769. int MoxaPortIsValid(int port)
  1770. {
  1771. if (moxaCard == 0)
  1772. return (0);
  1773. if (moxa_ports[port].chkPort == 0)
  1774. return (0);
  1775. return (1);
  1776. }
  1777. void MoxaPortEnable(int port)
  1778. {
  1779. void __iomem *ofsAddr;
  1780. int MoxaPortLineStatus(int);
  1781. short lowwater = 512;
  1782. ofsAddr = moxa_ports[port].tableAddr;
  1783. writew(lowwater, ofsAddr + Low_water);
  1784. moxa_ports[port].breakCnt = 0;
  1785. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1786. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1787. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1788. } else {
  1789. writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
  1790. }
  1791. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1792. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1793. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1794. MoxaPortLineStatus(port);
  1795. }
  1796. void MoxaPortDisable(int port)
  1797. {
  1798. void __iomem *ofsAddr = moxa_ports[port].tableAddr;
  1799. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1800. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1801. writew(0, ofsAddr + HostStat);
  1802. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1803. }
  1804. long MoxaPortGetMaxBaud(int port)
  1805. {
  1806. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1807. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
  1808. return (460800L);
  1809. else
  1810. return (921600L);
  1811. }
  1812. long MoxaPortSetBaud(int port, long baud)
  1813. {
  1814. void __iomem *ofsAddr;
  1815. long max, clock;
  1816. unsigned int val;
  1817. if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
  1818. return (0);
  1819. ofsAddr = moxa_ports[port].tableAddr;
  1820. if (baud > max)
  1821. baud = max;
  1822. if (max == 38400L)
  1823. clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
  1824. else if (max == 57600L)
  1825. clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
  1826. else
  1827. clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
  1828. val = clock / baud;
  1829. moxafunc(ofsAddr, FC_SetBaud, val);
  1830. baud = clock / val;
  1831. moxa_ports[port].curBaud = baud;
  1832. return (baud);
  1833. }
  1834. int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
  1835. {
  1836. void __iomem *ofsAddr;
  1837. tcflag_t cflag;
  1838. tcflag_t mode = 0;
  1839. if (moxa_ports[port].chkPort == 0 || termio == 0)
  1840. return (-1);
  1841. ofsAddr = moxa_ports[port].tableAddr;
  1842. cflag = termio->c_cflag; /* termio->c_cflag */
  1843. mode = termio->c_cflag & CSIZE;
  1844. if (mode == CS5)
  1845. mode = MX_CS5;
  1846. else if (mode == CS6)
  1847. mode = MX_CS6;
  1848. else if (mode == CS7)
  1849. mode = MX_CS7;
  1850. else if (mode == CS8)
  1851. mode = MX_CS8;
  1852. if (termio->c_cflag & CSTOPB) {
  1853. if (mode == MX_CS5)
  1854. mode |= MX_STOP15;
  1855. else
  1856. mode |= MX_STOP2;
  1857. } else
  1858. mode |= MX_STOP1;
  1859. if (termio->c_cflag & PARENB) {
  1860. if (termio->c_cflag & PARODD)
  1861. mode |= MX_PARODD;
  1862. else
  1863. mode |= MX_PAREVEN;
  1864. } else
  1865. mode |= MX_PARNONE;
  1866. moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
  1867. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1868. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1869. if (baud >= 921600L)
  1870. return (-1);
  1871. }
  1872. MoxaPortSetBaud(port, baud);
  1873. if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
  1874. writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
  1875. writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
  1876. writeb(FC_SetXonXoff, ofsAddr + FuncCode);
  1877. wait_finish(ofsAddr);
  1878. }
  1879. return (0);
  1880. }
  1881. int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
  1882. {
  1883. if (!MoxaPortIsValid(port))
  1884. return (-1);
  1885. if (dtrState) {
  1886. if (moxa_ports[port].lineCtrl & DTR_ON)
  1887. *dtrState = 1;
  1888. else
  1889. *dtrState = 0;
  1890. }
  1891. if (rtsState) {
  1892. if (moxa_ports[port].lineCtrl & RTS_ON)
  1893. *rtsState = 1;
  1894. else
  1895. *rtsState = 0;
  1896. }
  1897. return (0);
  1898. }
  1899. void MoxaPortLineCtrl(int port, int dtr, int rts)
  1900. {
  1901. void __iomem *ofsAddr;
  1902. int mode;
  1903. ofsAddr = moxa_ports[port].tableAddr;
  1904. mode = 0;
  1905. if (dtr)
  1906. mode |= DTR_ON;
  1907. if (rts)
  1908. mode |= RTS_ON;
  1909. moxa_ports[port].lineCtrl = mode;
  1910. moxafunc(ofsAddr, FC_LineControl, mode);
  1911. }
  1912. void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
  1913. {
  1914. void __iomem *ofsAddr;
  1915. int mode;
  1916. ofsAddr = moxa_ports[port].tableAddr;
  1917. mode = 0;
  1918. if (rts)
  1919. mode |= RTS_FlowCtl;
  1920. if (cts)
  1921. mode |= CTS_FlowCtl;
  1922. if (txflow)
  1923. mode |= Tx_FlowCtl;
  1924. if (rxflow)
  1925. mode |= Rx_FlowCtl;
  1926. if (txany)
  1927. mode |= IXM_IXANY;
  1928. moxafunc(ofsAddr, FC_SetFlowCtl, mode);
  1929. }
  1930. int MoxaPortLineStatus(int port)
  1931. {
  1932. void __iomem *ofsAddr;
  1933. int val;
  1934. ofsAddr = moxa_ports[port].tableAddr;
  1935. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1936. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1937. moxafunc(ofsAddr, FC_LineStatus, 0);
  1938. val = readw(ofsAddr + FuncArg);
  1939. } else {
  1940. val = readw(ofsAddr + FlagStat) >> 4;
  1941. }
  1942. val &= 0x0B;
  1943. if (val & 8) {
  1944. val |= 4;
  1945. if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
  1946. moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
  1947. } else {
  1948. if (moxa_ports[port].DCDState & DCD_oldstate)
  1949. moxa_ports[port].DCDState = DCD_changed;
  1950. }
  1951. val &= 7;
  1952. return (val);
  1953. }
  1954. int MoxaPortDCDChange(int port)
  1955. {
  1956. int n;
  1957. if (moxa_ports[port].chkPort == 0)
  1958. return (0);
  1959. n = moxa_ports[port].DCDState;
  1960. moxa_ports[port].DCDState &= ~DCD_changed;
  1961. n &= DCD_changed;
  1962. return (n);
  1963. }
  1964. int MoxaPortDCDON(int port)
  1965. {
  1966. int n;
  1967. if (moxa_ports[port].chkPort == 0)
  1968. return (0);
  1969. if (moxa_ports[port].DCDState & DCD_oldstate)
  1970. n = 1;
  1971. else
  1972. n = 0;
  1973. return (n);
  1974. }
  1975. int MoxaPortWriteData(int port, unsigned char * buffer, int len)
  1976. {
  1977. int c, total, i;
  1978. ushort tail;
  1979. int cnt;
  1980. ushort head, tx_mask, spage, epage;
  1981. ushort pageno, pageofs, bufhead;
  1982. void __iomem *baseAddr, *ofsAddr, *ofs;
  1983. ofsAddr = moxa_ports[port].tableAddr;
  1984. baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
  1985. tx_mask = readw(ofsAddr + TX_mask);
  1986. spage = readw(ofsAddr + Page_txb);
  1987. epage = readw(ofsAddr + EndPage_txb);
  1988. tail = readw(ofsAddr + TXwptr);
  1989. head = readw(ofsAddr + TXrptr);
  1990. c = (head > tail) ? (head - tail - 1)
  1991. : (head - tail + tx_mask);
  1992. if (c > len)
  1993. c = len;
  1994. moxaLog.txcnt[port] += c;
  1995. total = c;
  1996. if (spage == epage) {
  1997. bufhead = readw(ofsAddr + Ofs_txb);
  1998. writew(spage, baseAddr + Control_reg);
  1999. while (c > 0) {
  2000. if (head > tail)
  2001. len = head - tail - 1;
  2002. else
  2003. len = tx_mask + 1 - tail;
  2004. len = (c > len) ? len : c;
  2005. ofs = baseAddr + DynPage_addr + bufhead + tail;
  2006. for (i = 0; i < len; i++)
  2007. writeb(*buffer++, ofs + i);
  2008. tail = (tail + len) & tx_mask;
  2009. c -= len;
  2010. }
  2011. writew(tail, ofsAddr + TXwptr);
  2012. } else {
  2013. len = c;
  2014. pageno = spage + (tail >> 13);
  2015. pageofs = tail & Page_mask;
  2016. do {
  2017. cnt = Page_size - pageofs;
  2018. if (cnt > c)
  2019. cnt = c;
  2020. c -= cnt;
  2021. writeb(pageno, baseAddr + Control_reg);
  2022. ofs = baseAddr + DynPage_addr + pageofs;
  2023. for (i = 0; i < cnt; i++)
  2024. writeb(*buffer++, ofs + i);
  2025. if (c == 0) {
  2026. writew((tail + len) & tx_mask, ofsAddr + TXwptr);
  2027. break;
  2028. }
  2029. if (++pageno == epage)
  2030. pageno = spage;
  2031. pageofs = 0;
  2032. } while (1);
  2033. }
  2034. writeb(1, ofsAddr + CD180TXirq); /* start to send */
  2035. return (total);
  2036. }
  2037. int MoxaPortReadData(int port, struct tty_struct *tty)
  2038. {
  2039. register ushort head, pageofs;
  2040. int i, count, cnt, len, total, remain;
  2041. ushort tail, rx_mask, spage, epage;
  2042. ushort pageno, bufhead;
  2043. void __iomem *baseAddr, *ofsAddr, *ofs;
  2044. ofsAddr = moxa_ports[port].tableAddr;
  2045. baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
  2046. head = readw(ofsAddr + RXrptr);
  2047. tail = readw(ofsAddr + RXwptr);
  2048. rx_mask = readw(ofsAddr + RX_mask);
  2049. spage = readw(ofsAddr + Page_rxb);
  2050. epage = readw(ofsAddr + EndPage_rxb);
  2051. count = (tail >= head) ? (tail - head)
  2052. : (tail - head + rx_mask + 1);
  2053. if (count == 0)
  2054. return 0;
  2055. total = count;
  2056. remain = count - total;
  2057. moxaLog.rxcnt[port] += total;
  2058. count = total;
  2059. if (spage == epage) {
  2060. bufhead = readw(ofsAddr + Ofs_rxb);
  2061. writew(spage, baseAddr + Control_reg);
  2062. while (count > 0) {
  2063. if (tail >= head)
  2064. len = tail - head;
  2065. else
  2066. len = rx_mask + 1 - head;
  2067. len = (count > len) ? len : count;
  2068. ofs = baseAddr + DynPage_addr + bufhead + head;
  2069. for (i = 0; i < len; i++)
  2070. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2071. head = (head + len) & rx_mask;
  2072. count -= len;
  2073. }
  2074. writew(head, ofsAddr + RXrptr);
  2075. } else {
  2076. len = count;
  2077. pageno = spage + (head >> 13);
  2078. pageofs = head & Page_mask;
  2079. do {
  2080. cnt = Page_size - pageofs;
  2081. if (cnt > count)
  2082. cnt = count;
  2083. count -= cnt;
  2084. writew(pageno, baseAddr + Control_reg);
  2085. ofs = baseAddr + DynPage_addr + pageofs;
  2086. for (i = 0; i < cnt; i++)
  2087. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2088. if (count == 0) {
  2089. writew((head + len) & rx_mask, ofsAddr + RXrptr);
  2090. break;
  2091. }
  2092. if (++pageno == epage)
  2093. pageno = spage;
  2094. pageofs = 0;
  2095. } while (1);
  2096. }
  2097. if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
  2098. moxaLowWaterChk = 1;
  2099. moxa_ports[port].lowChkFlag = 1;
  2100. }
  2101. return (total);
  2102. }
  2103. int MoxaPortTxQueue(int port)
  2104. {
  2105. void __iomem *ofsAddr;
  2106. ushort rptr, wptr, mask;
  2107. int len;
  2108. ofsAddr = moxa_ports[port].tableAddr;
  2109. rptr = readw(ofsAddr + TXrptr);
  2110. wptr = readw(ofsAddr + TXwptr);
  2111. mask = readw(ofsAddr + TX_mask);
  2112. len = (wptr - rptr) & mask;
  2113. return (len);
  2114. }
  2115. int MoxaPortTxFree(int port)
  2116. {
  2117. void __iomem *ofsAddr;
  2118. ushort rptr, wptr, mask;
  2119. int len;
  2120. ofsAddr = moxa_ports[port].tableAddr;
  2121. rptr = readw(ofsAddr + TXrptr);
  2122. wptr = readw(ofsAddr + TXwptr);
  2123. mask = readw(ofsAddr + TX_mask);
  2124. len = mask - ((wptr - rptr) & mask);
  2125. return (len);
  2126. }
  2127. int MoxaPortRxQueue(int port)
  2128. {
  2129. void __iomem *ofsAddr;
  2130. ushort rptr, wptr, mask;
  2131. int len;
  2132. ofsAddr = moxa_ports[port].tableAddr;
  2133. rptr = readw(ofsAddr + RXrptr);
  2134. wptr = readw(ofsAddr + RXwptr);
  2135. mask = readw(ofsAddr + RX_mask);
  2136. len = (wptr - rptr) & mask;
  2137. return (len);
  2138. }
  2139. void MoxaPortTxDisable(int port)
  2140. {
  2141. void __iomem *ofsAddr;
  2142. ofsAddr = moxa_ports[port].tableAddr;
  2143. moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
  2144. }
  2145. void MoxaPortTxEnable(int port)
  2146. {
  2147. void __iomem *ofsAddr;
  2148. ofsAddr = moxa_ports[port].tableAddr;
  2149. moxafunc(ofsAddr, FC_SetXonState, Magic_code);
  2150. }
  2151. int MoxaPortResetBrkCnt(int port)
  2152. {
  2153. ushort cnt;
  2154. cnt = moxa_ports[port].breakCnt;
  2155. moxa_ports[port].breakCnt = 0;
  2156. return (cnt);
  2157. }
  2158. void MoxaPortSendBreak(int port, int ms100)
  2159. {
  2160. void __iomem *ofsAddr;
  2161. ofsAddr = moxa_ports[port].tableAddr;
  2162. if (ms100) {
  2163. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2164. moxadelay(ms100 * (HZ / 10));
  2165. } else {
  2166. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2167. moxadelay(HZ / 4); /* 250 ms */
  2168. }
  2169. moxafunc(ofsAddr, FC_StopBreak, Magic_code);
  2170. }
  2171. static int moxa_get_serial_info(struct moxa_port *info,
  2172. struct serial_struct __user *retinfo)
  2173. {
  2174. struct serial_struct tmp;
  2175. memset(&tmp, 0, sizeof(tmp));
  2176. tmp.type = info->type;
  2177. tmp.line = info->port;
  2178. tmp.port = 0;
  2179. tmp.irq = 0;
  2180. tmp.flags = info->asyncflags;
  2181. tmp.baud_base = 921600;
  2182. tmp.close_delay = info->close_delay;
  2183. tmp.closing_wait = info->closing_wait;
  2184. tmp.custom_divisor = 0;
  2185. tmp.hub6 = 0;
  2186. if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2187. return -EFAULT;
  2188. return (0);
  2189. }
  2190. static int moxa_set_serial_info(struct moxa_port *info,
  2191. struct serial_struct __user *new_info)
  2192. {
  2193. struct serial_struct new_serial;
  2194. if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2195. return -EFAULT;
  2196. if ((new_serial.irq != 0) ||
  2197. (new_serial.port != 0) ||
  2198. // (new_serial.type != info->type) ||
  2199. (new_serial.custom_divisor != 0) ||
  2200. (new_serial.baud_base != 921600))
  2201. return (-EPERM);
  2202. if (!capable(CAP_SYS_ADMIN)) {
  2203. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  2204. (info->asyncflags & ~ASYNC_USR_MASK)))
  2205. return (-EPERM);
  2206. } else {
  2207. info->close_delay = new_serial.close_delay * HZ / 100;
  2208. info->closing_wait = new_serial.closing_wait * HZ / 100;
  2209. }
  2210. new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
  2211. new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
  2212. if (new_serial.type == PORT_16550A) {
  2213. MoxaSetFifo(info->port, 1);
  2214. } else {
  2215. MoxaSetFifo(info->port, 0);
  2216. }
  2217. info->type = new_serial.type;
  2218. return (0);
  2219. }
  2220. /*****************************************************************************
  2221. * Static local functions: *
  2222. *****************************************************************************/
  2223. /*
  2224. * moxadelay - delays a specified number ticks
  2225. */
  2226. static void moxadelay(int tick)
  2227. {
  2228. unsigned long st, et;
  2229. st = jiffies;
  2230. et = st + tick;
  2231. while (time_before(jiffies, et));
  2232. }
  2233. static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
  2234. {
  2235. writew(arg, ofsAddr + FuncArg);
  2236. writew(cmd, ofsAddr + FuncCode);
  2237. wait_finish(ofsAddr);
  2238. }
  2239. static void wait_finish(void __iomem *ofsAddr)
  2240. {
  2241. unsigned long i, j;
  2242. i = jiffies;
  2243. while (readw(ofsAddr + FuncCode) != 0) {
  2244. j = jiffies;
  2245. if ((j - i) > moxaFuncTout) {
  2246. return;
  2247. }
  2248. }
  2249. }
  2250. static void low_water_check(void __iomem *ofsAddr)
  2251. {
  2252. int len;
  2253. ushort rptr, wptr, mask;
  2254. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  2255. rptr = readw(ofsAddr + RXrptr);
  2256. wptr = readw(ofsAddr + RXwptr);
  2257. mask = readw(ofsAddr + RX_mask);
  2258. len = (wptr - rptr) & mask;
  2259. if (len <= Low_water)
  2260. moxafunc(ofsAddr, FC_SendXon, 0);
  2261. }
  2262. }
  2263. static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
  2264. {
  2265. void __iomem *baseAddr;
  2266. int i;
  2267. if(copy_from_user(moxaBuff, tmp, len))
  2268. return -EFAULT;
  2269. baseAddr = moxa_boards[cardno].basemem;
  2270. writeb(HW_reset, baseAddr + Control_reg); /* reset */
  2271. moxadelay(1); /* delay 10 ms */
  2272. for (i = 0; i < 4096; i++)
  2273. writeb(0, baseAddr + i); /* clear fix page */
  2274. for (i = 0; i < len; i++)
  2275. writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
  2276. writeb(0, baseAddr + Control_reg); /* restart */
  2277. return (0);
  2278. }
  2279. static int moxafindcard(int cardno)
  2280. {
  2281. void __iomem *baseAddr;
  2282. ushort tmp;
  2283. baseAddr = moxa_boards[cardno].basemem;
  2284. switch (moxa_boards[cardno].boardType) {
  2285. case MOXA_BOARD_C218_ISA:
  2286. case MOXA_BOARD_C218_PCI:
  2287. if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
  2288. return (-1);
  2289. }
  2290. break;
  2291. case MOXA_BOARD_CP204J:
  2292. if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
  2293. return (-1);
  2294. }
  2295. break;
  2296. default:
  2297. if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
  2298. return (-1);
  2299. }
  2300. if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
  2301. return (-2);
  2302. }
  2303. }
  2304. return (0);
  2305. }
  2306. static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
  2307. {
  2308. void __iomem *baseAddr;
  2309. int i;
  2310. if(len > sizeof(moxaBuff))
  2311. return -EINVAL;
  2312. if(copy_from_user(moxaBuff, tmp, len))
  2313. return -EFAULT;
  2314. baseAddr = moxa_boards[cardno].basemem;
  2315. writew(len - 7168 - 2, baseAddr + C320bapi_len);
  2316. writeb(1, baseAddr + Control_reg); /* Select Page 1 */
  2317. for (i = 0; i < 7168; i++)
  2318. writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
  2319. writeb(2, baseAddr + Control_reg); /* Select Page 2 */
  2320. for (i = 0; i < (len - 7168); i++)
  2321. writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
  2322. return (0);
  2323. }
  2324. static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
  2325. {
  2326. void __iomem *baseAddr, *ofsAddr;
  2327. int retval, port, i;
  2328. if(copy_from_user(moxaBuff, tmp, len))
  2329. return -EFAULT;
  2330. baseAddr = moxa_boards[cardno].basemem;
  2331. switch (moxa_boards[cardno].boardType) {
  2332. case MOXA_BOARD_C218_ISA:
  2333. case MOXA_BOARD_C218_PCI:
  2334. case MOXA_BOARD_CP204J:
  2335. retval = moxaloadc218(cardno, baseAddr, len);
  2336. if (retval)
  2337. return (retval);
  2338. port = cardno * MAX_PORTS_PER_BOARD;
  2339. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2340. struct moxa_port *p = &moxa_ports[port];
  2341. p->chkPort = 1;
  2342. p->curBaud = 9600L;
  2343. p->DCDState = 0;
  2344. p->tableAddr = baseAddr + Extern_table + Extern_size * i;
  2345. ofsAddr = p->tableAddr;
  2346. writew(C218rx_mask, ofsAddr + RX_mask);
  2347. writew(C218tx_mask, ofsAddr + TX_mask);
  2348. writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
  2349. writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
  2350. writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
  2351. writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
  2352. }
  2353. break;
  2354. default:
  2355. retval = moxaloadc320(cardno, baseAddr, len,
  2356. &moxa_boards[cardno].numPorts);
  2357. if (retval)
  2358. return (retval);
  2359. port = cardno * MAX_PORTS_PER_BOARD;
  2360. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2361. struct moxa_port *p = &moxa_ports[port];
  2362. p->chkPort = 1;
  2363. p->curBaud = 9600L;
  2364. p->DCDState = 0;
  2365. p->tableAddr = baseAddr + Extern_table + Extern_size * i;
  2366. ofsAddr = p->tableAddr;
  2367. if (moxa_boards[cardno].numPorts == 8) {
  2368. writew(C320p8rx_mask, ofsAddr + RX_mask);
  2369. writew(C320p8tx_mask, ofsAddr + TX_mask);
  2370. writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
  2371. writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
  2372. writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
  2373. writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
  2374. } else if (moxa_boards[cardno].numPorts == 16) {
  2375. writew(C320p16rx_mask, ofsAddr + RX_mask);
  2376. writew(C320p16tx_mask, ofsAddr + TX_mask);
  2377. writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
  2378. writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
  2379. writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
  2380. writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
  2381. } else if (moxa_boards[cardno].numPorts == 24) {
  2382. writew(C320p24rx_mask, ofsAddr + RX_mask);
  2383. writew(C320p24tx_mask, ofsAddr + TX_mask);
  2384. writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
  2385. writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
  2386. writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
  2387. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2388. } else if (moxa_boards[cardno].numPorts == 32) {
  2389. writew(C320p32rx_mask, ofsAddr + RX_mask);
  2390. writew(C320p32tx_mask, ofsAddr + TX_mask);
  2391. writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
  2392. writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
  2393. writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
  2394. writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
  2395. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2396. }
  2397. }
  2398. break;
  2399. }
  2400. moxa_boards[cardno].loadstat = 1;
  2401. return (0);
  2402. }
  2403. static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
  2404. {
  2405. char retry;
  2406. int i, j, len1, len2;
  2407. ushort usum, *ptr, keycode;
  2408. if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
  2409. keycode = CP204J_KeyCode;
  2410. else
  2411. keycode = C218_KeyCode;
  2412. usum = 0;
  2413. len1 = len >> 1;
  2414. ptr = (ushort *) moxaBuff;
  2415. for (i = 0; i < len1; i++)
  2416. usum += le16_to_cpu(*(ptr + i));
  2417. retry = 0;
  2418. do {
  2419. len1 = len >> 1;
  2420. j = 0;
  2421. while (len1) {
  2422. len2 = (len1 > 2048) ? 2048 : len1;
  2423. len1 -= len2;
  2424. for (i = 0; i < len2 << 1; i++)
  2425. writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
  2426. j += i;
  2427. writew(len2, baseAddr + C218DLoad_len);
  2428. writew(0, baseAddr + C218_key);
  2429. for (i = 0; i < 100; i++) {
  2430. if (readw(baseAddr + C218_key) == keycode)
  2431. break;
  2432. moxadelay(1); /* delay 10 ms */
  2433. }
  2434. if (readw(baseAddr + C218_key) != keycode) {
  2435. return (-1);
  2436. }
  2437. }
  2438. writew(0, baseAddr + C218DLoad_len);
  2439. writew(usum, baseAddr + C218check_sum);
  2440. writew(0, baseAddr + C218_key);
  2441. for (i = 0; i < 100; i++) {
  2442. if (readw(baseAddr + C218_key) == keycode)
  2443. break;
  2444. moxadelay(1); /* delay 10 ms */
  2445. }
  2446. retry++;
  2447. } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
  2448. if (readb(baseAddr + C218chksum_ok) != 1) {
  2449. return (-1);
  2450. }
  2451. writew(0, baseAddr + C218_key);
  2452. for (i = 0; i < 100; i++) {
  2453. if (readw(baseAddr + Magic_no) == Magic_code)
  2454. break;
  2455. moxadelay(1); /* delay 10 ms */
  2456. }
  2457. if (readw(baseAddr + Magic_no) != Magic_code) {
  2458. return (-1);
  2459. }
  2460. writew(1, baseAddr + Disable_IRQ);
  2461. writew(0, baseAddr + Magic_no);
  2462. for (i = 0; i < 100; i++) {
  2463. if (readw(baseAddr + Magic_no) == Magic_code)
  2464. break;
  2465. moxadelay(1); /* delay 10 ms */
  2466. }
  2467. if (readw(baseAddr + Magic_no) != Magic_code) {
  2468. return (-1);
  2469. }
  2470. moxaCard = 1;
  2471. moxa_boards[cardno].intNdx = baseAddr + IRQindex;
  2472. moxa_boards[cardno].intPend = baseAddr + IRQpending;
  2473. moxa_boards[cardno].intTable = baseAddr + IRQtable;
  2474. return (0);
  2475. }
  2476. static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
  2477. {
  2478. ushort usum;
  2479. int i, j, wlen, len2, retry;
  2480. ushort *uptr;
  2481. usum = 0;
  2482. wlen = len >> 1;
  2483. uptr = (ushort *) moxaBuff;
  2484. for (i = 0; i < wlen; i++)
  2485. usum += le16_to_cpu(uptr[i]);
  2486. retry = 0;
  2487. j = 0;
  2488. do {
  2489. while (wlen) {
  2490. if (wlen > 2048)
  2491. len2 = 2048;
  2492. else
  2493. len2 = wlen;
  2494. wlen -= len2;
  2495. len2 <<= 1;
  2496. for (i = 0; i < len2; i++)
  2497. writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
  2498. len2 >>= 1;
  2499. j += i;
  2500. writew(len2, baseAddr + C320DLoad_len);
  2501. writew(0, baseAddr + C320_key);
  2502. for (i = 0; i < 10; i++) {
  2503. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2504. break;
  2505. moxadelay(1);
  2506. }
  2507. if (readw(baseAddr + C320_key) != C320_KeyCode)
  2508. return (-1);
  2509. }
  2510. writew(0, baseAddr + C320DLoad_len);
  2511. writew(usum, baseAddr + C320check_sum);
  2512. writew(0, baseAddr + C320_key);
  2513. for (i = 0; i < 10; i++) {
  2514. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2515. break;
  2516. moxadelay(1);
  2517. }
  2518. retry++;
  2519. } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
  2520. if (readb(baseAddr + C320chksum_ok) != 1)
  2521. return (-1);
  2522. writew(0, baseAddr + C320_key);
  2523. for (i = 0; i < 600; i++) {
  2524. if (readw(baseAddr + Magic_no) == Magic_code)
  2525. break;
  2526. moxadelay(1);
  2527. }
  2528. if (readw(baseAddr + Magic_no) != Magic_code)
  2529. return (-100);
  2530. if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
  2531. writew(0x3800, baseAddr + TMS320_PORT1);
  2532. writew(0x3900, baseAddr + TMS320_PORT2);
  2533. writew(28499, baseAddr + TMS320_CLOCK);
  2534. } else {
  2535. writew(0x3200, baseAddr + TMS320_PORT1);
  2536. writew(0x3400, baseAddr + TMS320_PORT2);
  2537. writew(19999, baseAddr + TMS320_CLOCK);
  2538. }
  2539. writew(1, baseAddr + Disable_IRQ);
  2540. writew(0, baseAddr + Magic_no);
  2541. for (i = 0; i < 500; i++) {
  2542. if (readw(baseAddr + Magic_no) == Magic_code)
  2543. break;
  2544. moxadelay(1);
  2545. }
  2546. if (readw(baseAddr + Magic_no) != Magic_code)
  2547. return (-102);
  2548. j = readw(baseAddr + Module_cnt);
  2549. if (j <= 0)
  2550. return (-101);
  2551. *numPorts = j * 8;
  2552. writew(j, baseAddr + Module_no);
  2553. writew(0, baseAddr + Magic_no);
  2554. for (i = 0; i < 600; i++) {
  2555. if (readw(baseAddr + Magic_no) == Magic_code)
  2556. break;
  2557. moxadelay(1);
  2558. }
  2559. if (readw(baseAddr + Magic_no) != Magic_code)
  2560. return (-102);
  2561. moxaCard = 1;
  2562. moxa_boards[cardno].intNdx = baseAddr + IRQindex;
  2563. moxa_boards[cardno].intPend = baseAddr + IRQpending;
  2564. moxa_boards[cardno].intTable = baseAddr + IRQtable;
  2565. return (0);
  2566. }
  2567. static void MoxaSetFifo(int port, int enable)
  2568. {
  2569. void __iomem *ofsAddr = moxa_ports[port].tableAddr;
  2570. if (!enable) {
  2571. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
  2572. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
  2573. } else {
  2574. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
  2575. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
  2576. }
  2577. }