moxa.c 75 KB

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