moxa.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  1. /*****************************************************************************/
  2. /*
  3. * moxa.c -- MOXA Intellio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
  6. *
  7. * This code is loosely based on the Linux serial driver, written by
  8. * Linus Torvalds, Theodore T'so and others.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. /*
  16. * MOXA Intellio Series Driver
  17. * for : LINUX
  18. * date : 1999/1/7
  19. * version : 5.1
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/mm.h>
  24. #include <linux/ioport.h>
  25. #include <linux/errno.h>
  26. #include <linux/signal.h>
  27. #include <linux/sched.h>
  28. #include <linux/timer.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/tty.h>
  31. #include <linux/tty_flip.h>
  32. #include <linux/major.h>
  33. #include <linux/string.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/serial.h>
  37. #include <linux/tty_driver.h>
  38. #include <linux/delay.h>
  39. #include <linux/pci.h>
  40. #include <linux/init.h>
  41. #include <linux/bitops.h>
  42. #include <asm/system.h>
  43. #include <asm/io.h>
  44. #include <asm/uaccess.h>
  45. #define MOXA_VERSION "5.1k"
  46. #define MOXAMAJOR 172
  47. #define MOXACUMAJOR 173
  48. #define MAX_BOARDS 4 /* Don't change this value */
  49. #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
  50. #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
  51. /*
  52. * Define the Moxa PCI vendor and device IDs.
  53. */
  54. #define MOXA_BUS_TYPE_ISA 0
  55. #define MOXA_BUS_TYPE_PCI 1
  56. enum {
  57. MOXA_BOARD_C218_PCI = 1,
  58. MOXA_BOARD_C218_ISA,
  59. MOXA_BOARD_C320_PCI,
  60. MOXA_BOARD_C320_ISA,
  61. MOXA_BOARD_CP204J,
  62. };
  63. static char *moxa_brdname[] =
  64. {
  65. "C218 Turbo PCI series",
  66. "C218 Turbo ISA series",
  67. "C320 Turbo PCI series",
  68. "C320 Turbo ISA series",
  69. "CP-204J series",
  70. };
  71. #ifdef CONFIG_PCI
  72. static struct pci_device_id moxa_pcibrds[] = {
  73. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
  74. .driver_data = MOXA_BOARD_C218_PCI },
  75. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
  76. .driver_data = MOXA_BOARD_C320_PCI },
  77. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
  78. .driver_data = MOXA_BOARD_CP204J },
  79. { 0 }
  80. };
  81. MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
  82. #endif /* CONFIG_PCI */
  83. struct moxa_isa_board_conf {
  84. int boardType;
  85. int numPorts;
  86. unsigned long baseAddr;
  87. };
  88. static struct moxa_isa_board_conf moxa_isa_boards[] =
  89. {
  90. /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
  91. };
  92. static struct moxa_board_conf {
  93. int boardType;
  94. int numPorts;
  95. unsigned long baseAddr;
  96. int busType;
  97. 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 moxadelay(int);
  1234. static void moxafunc(void __iomem *, int, ushort);
  1235. static void wait_finish(void __iomem *);
  1236. static void low_water_check(void __iomem *);
  1237. static int moxaloadbios(int, unsigned char __user *, int);
  1238. static int moxafindcard(int);
  1239. static int moxaload320b(int, unsigned char __user *, int);
  1240. static int moxaloadcode(int, unsigned char __user *, int);
  1241. static int moxaloadc218(int, void __iomem *, int);
  1242. static int moxaloadc320(int, void __iomem *, int, int *);
  1243. /*****************************************************************************
  1244. * Driver level functions: *
  1245. * 1. MoxaDriverInit(void); *
  1246. * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
  1247. * 3. MoxaDriverPoll(void); *
  1248. *****************************************************************************/
  1249. void MoxaDriverInit(void)
  1250. {
  1251. struct moxa_port *p;
  1252. unsigned int i;
  1253. moxaFuncTout = HZ / 2; /* 500 mini-seconds */
  1254. moxaCard = 0;
  1255. moxaLog.tick = 0;
  1256. moxaLowWaterChk = 0;
  1257. for (i = 0; i < MAX_PORTS; i++) {
  1258. p = &moxa_ports[i];
  1259. p->chkPort = 0;
  1260. p->lowChkFlag = 0;
  1261. p->lineCtrl = 0;
  1262. moxaLog.rxcnt[i] = 0;
  1263. moxaLog.txcnt[i] = 0;
  1264. }
  1265. }
  1266. #define MOXA 0x400
  1267. #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
  1268. #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
  1269. #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
  1270. #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
  1271. #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
  1272. #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
  1273. #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
  1274. #define MOXA_GETDATACOUNT (MOXA + 23)
  1275. #define MOXA_GET_IOQUEUE (MOXA + 27)
  1276. #define MOXA_FLUSH_QUEUE (MOXA + 28)
  1277. #define MOXA_GET_CONF (MOXA + 35) /* configuration */
  1278. #define MOXA_GET_MAJOR (MOXA + 63)
  1279. #define MOXA_GET_CUMAJOR (MOXA + 64)
  1280. #define MOXA_GETMSTATUS (MOXA + 65)
  1281. struct dl_str {
  1282. char __user *buf;
  1283. int len;
  1284. int cardno;
  1285. };
  1286. static struct dl_str dltmp;
  1287. void MoxaPortFlushData(int port, int mode)
  1288. {
  1289. void __iomem *ofsAddr;
  1290. if ((mode < 0) || (mode > 2))
  1291. return;
  1292. ofsAddr = moxa_ports[port].tableAddr;
  1293. moxafunc(ofsAddr, FC_FlushQueue, mode);
  1294. if (mode != 1) {
  1295. moxa_ports[port].lowChkFlag = 0;
  1296. low_water_check(ofsAddr);
  1297. }
  1298. }
  1299. int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
  1300. {
  1301. int i;
  1302. int status;
  1303. int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
  1304. void __user *argp = (void __user *)arg;
  1305. if (port == MAX_PORTS) {
  1306. if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
  1307. (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
  1308. (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
  1309. (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
  1310. (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
  1311. return (-EINVAL);
  1312. }
  1313. switch (cmd) {
  1314. case MOXA_GET_CONF:
  1315. if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
  1316. sizeof(struct moxa_board_conf)))
  1317. return -EFAULT;
  1318. return (0);
  1319. case MOXA_INIT_DRIVER:
  1320. if ((int) arg == 0x404)
  1321. MoxaDriverInit();
  1322. return (0);
  1323. case MOXA_GETDATACOUNT:
  1324. moxaLog.tick = jiffies;
  1325. if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
  1326. return -EFAULT;
  1327. return (0);
  1328. case MOXA_FLUSH_QUEUE:
  1329. MoxaPortFlushData(port, arg);
  1330. return (0);
  1331. case MOXA_GET_IOQUEUE: {
  1332. struct moxaq_str __user *argm = argp;
  1333. struct moxaq_str tmp;
  1334. for (i = 0; i < MAX_PORTS; i++, argm++) {
  1335. memset(&tmp, 0, sizeof(tmp));
  1336. if (moxa_ports[i].chkPort) {
  1337. tmp.inq = MoxaPortRxQueue(i);
  1338. tmp.outq = MoxaPortTxQueue(i);
  1339. }
  1340. if (copy_to_user(argm, &tmp, sizeof(tmp)))
  1341. return -EFAULT;
  1342. }
  1343. return (0);
  1344. } case MOXA_GET_OQUEUE:
  1345. i = MoxaPortTxQueue(port);
  1346. return put_user(i, (unsigned long __user *)argp);
  1347. case MOXA_GET_IQUEUE:
  1348. i = MoxaPortRxQueue(port);
  1349. return put_user(i, (unsigned long __user *)argp);
  1350. case MOXA_GET_MAJOR:
  1351. if(copy_to_user(argp, &ttymajor, sizeof(int)))
  1352. return -EFAULT;
  1353. return 0;
  1354. case MOXA_GET_CUMAJOR:
  1355. i = 0;
  1356. if(copy_to_user(argp, &i, sizeof(int)))
  1357. return -EFAULT;
  1358. return 0;
  1359. case MOXA_GETMSTATUS: {
  1360. struct mxser_mstatus __user *argm = argp;
  1361. struct mxser_mstatus tmp;
  1362. struct moxa_port *p;
  1363. for (i = 0; i < MAX_PORTS; i++, argm++) {
  1364. p = &moxa_ports[i];
  1365. memset(&tmp, 0, sizeof(tmp));
  1366. if (!p->chkPort) {
  1367. goto copy;
  1368. } else {
  1369. status = MoxaPortLineStatus(p->port);
  1370. if (status & 1)
  1371. tmp.cts = 1;
  1372. if (status & 2)
  1373. tmp.dsr = 1;
  1374. if (status & 4)
  1375. tmp.dcd = 1;
  1376. }
  1377. if (!p->tty || !p->tty->termios)
  1378. tmp.cflag = p->cflag;
  1379. else
  1380. tmp.cflag = p->tty->termios->c_cflag;
  1381. copy:
  1382. if (copy_to_user(argm, &tmp, sizeof(tmp)))
  1383. return -EFAULT;
  1384. }
  1385. return 0;
  1386. } default:
  1387. return (-ENOIOCTLCMD);
  1388. case MOXA_LOAD_BIOS:
  1389. case MOXA_FIND_BOARD:
  1390. case MOXA_LOAD_C320B:
  1391. case MOXA_LOAD_CODE:
  1392. if (!capable(CAP_SYS_RAWIO))
  1393. return -EPERM;
  1394. break;
  1395. }
  1396. if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
  1397. return -EFAULT;
  1398. if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
  1399. return -EINVAL;
  1400. switch(cmd)
  1401. {
  1402. case MOXA_LOAD_BIOS:
  1403. i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
  1404. return (i);
  1405. case MOXA_FIND_BOARD:
  1406. return moxafindcard(dltmp.cardno);
  1407. case MOXA_LOAD_C320B:
  1408. moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
  1409. default: /* to keep gcc happy */
  1410. return (0);
  1411. case MOXA_LOAD_CODE:
  1412. i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
  1413. if (i == -1)
  1414. return (-EFAULT);
  1415. return (i);
  1416. }
  1417. }
  1418. int MoxaDriverPoll(void)
  1419. {
  1420. struct moxa_board_conf *brd;
  1421. register ushort temp;
  1422. register int card;
  1423. void __iomem *ofsAddr;
  1424. void __iomem *ip;
  1425. int port, p, ports;
  1426. if (moxaCard == 0)
  1427. return (-1);
  1428. for (card = 0; card < MAX_BOARDS; card++) {
  1429. brd = &moxa_boards[card];
  1430. if (brd->loadstat == 0)
  1431. continue;
  1432. if ((ports = brd->numPorts) == 0)
  1433. continue;
  1434. if (readb(brd->intPend) == 0xff) {
  1435. ip = brd->intTable + readb(brd->intNdx);
  1436. p = card * MAX_PORTS_PER_BOARD;
  1437. ports <<= 1;
  1438. for (port = 0; port < ports; port += 2, p++) {
  1439. if ((temp = readw(ip + port)) != 0) {
  1440. writew(0, ip + port);
  1441. ofsAddr = moxa_ports[p].tableAddr;
  1442. if (temp & IntrTx)
  1443. writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
  1444. if (temp & IntrBreak) {
  1445. moxa_ports[p].breakCnt++;
  1446. }
  1447. if (temp & IntrLine) {
  1448. if (readb(ofsAddr + FlagStat) & DCD_state) {
  1449. if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
  1450. moxa_ports[p].DCDState = (DCD_oldstate |
  1451. DCD_changed);
  1452. } else {
  1453. if (moxa_ports[p].DCDState & DCD_oldstate)
  1454. moxa_ports[p].DCDState = DCD_changed;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. writeb(0, brd->intPend);
  1460. }
  1461. if (moxaLowWaterChk) {
  1462. p = card * MAX_PORTS_PER_BOARD;
  1463. for (port = 0; port < ports; port++, p++) {
  1464. if (moxa_ports[p].lowChkFlag) {
  1465. moxa_ports[p].lowChkFlag = 0;
  1466. ofsAddr = moxa_ports[p].tableAddr;
  1467. low_water_check(ofsAddr);
  1468. }
  1469. }
  1470. }
  1471. }
  1472. moxaLowWaterChk = 0;
  1473. return (0);
  1474. }
  1475. /*****************************************************************************
  1476. * Card level function: *
  1477. * 1. MoxaPortsOfCard(int cardno); *
  1478. *****************************************************************************/
  1479. int MoxaPortsOfCard(int cardno)
  1480. {
  1481. if (moxa_boards[cardno].boardType == 0)
  1482. return (0);
  1483. return (moxa_boards[cardno].numPorts);
  1484. }
  1485. /*****************************************************************************
  1486. * Port level functions: *
  1487. * 1. MoxaPortIsValid(int port); *
  1488. * 2. MoxaPortEnable(int port); *
  1489. * 3. MoxaPortDisable(int port); *
  1490. * 4. MoxaPortGetMaxBaud(int port); *
  1491. * 6. MoxaPortSetBaud(int port, long baud); *
  1492. * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
  1493. * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
  1494. * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
  1495. * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
  1496. * 12. MoxaPortLineStatus(int port); *
  1497. * 13. MoxaPortDCDChange(int port); *
  1498. * 14. MoxaPortDCDON(int port); *
  1499. * 15. MoxaPortFlushData(int port, int mode); *
  1500. * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
  1501. * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
  1502. * 20. MoxaPortTxQueue(int port); *
  1503. * 21. MoxaPortTxFree(int port); *
  1504. * 22. MoxaPortRxQueue(int port); *
  1505. * 24. MoxaPortTxDisable(int port); *
  1506. * 25. MoxaPortTxEnable(int port); *
  1507. * 27. MoxaPortResetBrkCnt(int port); *
  1508. * 30. MoxaPortSendBreak(int port, int ticks); *
  1509. *****************************************************************************/
  1510. /*
  1511. * Moxa Port Number Description:
  1512. *
  1513. * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
  1514. * the port number using in MOXA driver functions will be 0 to 31 for
  1515. * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
  1516. * to 127 for fourth. For example, if you setup three MOXA boards,
  1517. * first board is C218, second board is C320-16 and third board is
  1518. * C320-32. The port number of first board (C218 - 8 ports) is from
  1519. * 0 to 7. The port number of second board (C320 - 16 ports) is form
  1520. * 32 to 47. The port number of third board (C320 - 32 ports) is from
  1521. * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
  1522. * 127 will be invalid.
  1523. *
  1524. *
  1525. * Moxa Functions Description:
  1526. *
  1527. * Function 1: Driver initialization routine, this routine must be
  1528. * called when initialized driver.
  1529. * Syntax:
  1530. * void MoxaDriverInit();
  1531. *
  1532. *
  1533. * Function 2: Moxa driver private IOCTL command processing.
  1534. * Syntax:
  1535. * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
  1536. *
  1537. * unsigned int cmd : IOCTL command
  1538. * unsigned long arg : IOCTL argument
  1539. * int port : port number (0 - 127)
  1540. *
  1541. * return: 0 (OK)
  1542. * -EINVAL
  1543. * -ENOIOCTLCMD
  1544. *
  1545. *
  1546. * Function 3: Moxa driver polling process routine.
  1547. * Syntax:
  1548. * int MoxaDriverPoll(void);
  1549. *
  1550. * return: 0 ; polling O.K.
  1551. * -1 : no any Moxa card.
  1552. *
  1553. *
  1554. * Function 4: Get the ports of this card.
  1555. * Syntax:
  1556. * int MoxaPortsOfCard(int cardno);
  1557. *
  1558. * int cardno : card number (0 - 3)
  1559. *
  1560. * return: 0 : this card is invalid
  1561. * 8/16/24/32
  1562. *
  1563. *
  1564. * Function 5: Check this port is valid or invalid
  1565. * Syntax:
  1566. * int MoxaPortIsValid(int port);
  1567. * int port : port number (0 - 127, ref port description)
  1568. *
  1569. * return: 0 : this port is invalid
  1570. * 1 : this port is valid
  1571. *
  1572. *
  1573. * Function 6: Enable this port to start Tx/Rx data.
  1574. * Syntax:
  1575. * void MoxaPortEnable(int port);
  1576. * int port : port number (0 - 127)
  1577. *
  1578. *
  1579. * Function 7: Disable this port
  1580. * Syntax:
  1581. * void MoxaPortDisable(int port);
  1582. * int port : port number (0 - 127)
  1583. *
  1584. *
  1585. * Function 8: Get the maximun available baud rate of this port.
  1586. * Syntax:
  1587. * long MoxaPortGetMaxBaud(int port);
  1588. * int port : port number (0 - 127)
  1589. *
  1590. * return: 0 : this port is invalid
  1591. * 38400/57600/115200 bps
  1592. *
  1593. *
  1594. * Function 10: Setting baud rate of this port.
  1595. * Syntax:
  1596. * long MoxaPortSetBaud(int port, long baud);
  1597. * int port : port number (0 - 127)
  1598. * long baud : baud rate (50 - 115200)
  1599. *
  1600. * return: 0 : this port is invalid or baud < 50
  1601. * 50 - 115200 : the real baud rate set to the port, if
  1602. * the argument baud is large than maximun
  1603. * available baud rate, the real setting
  1604. * baud rate will be the maximun baud rate.
  1605. *
  1606. *
  1607. * Function 12: Configure the port.
  1608. * Syntax:
  1609. * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
  1610. * int port : port number (0 - 127)
  1611. * struct ktermios * termio : termio structure pointer
  1612. * speed_t baud : baud rate
  1613. *
  1614. * return: -1 : this port is invalid or termio == NULL
  1615. * 0 : setting O.K.
  1616. *
  1617. *
  1618. * Function 13: Get the DTR/RTS state of this port.
  1619. * Syntax:
  1620. * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
  1621. * int port : port number (0 - 127)
  1622. * int * dtrState : pointer to INT to receive the current DTR
  1623. * state. (if NULL, this function will not
  1624. * write to this address)
  1625. * int * rtsState : pointer to INT to receive the current RTS
  1626. * state. (if NULL, this function will not
  1627. * write to this address)
  1628. *
  1629. * return: -1 : this port is invalid
  1630. * 0 : O.K.
  1631. *
  1632. *
  1633. * Function 14: Setting the DTR/RTS output state of this port.
  1634. * Syntax:
  1635. * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
  1636. * int port : port number (0 - 127)
  1637. * int dtrState : DTR output state (0: off, 1: on)
  1638. * int rtsState : RTS output state (0: off, 1: on)
  1639. *
  1640. *
  1641. * Function 15: Setting the flow control of this port.
  1642. * Syntax:
  1643. * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
  1644. * int txFlow,int xany);
  1645. * int port : port number (0 - 127)
  1646. * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
  1647. * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
  1648. * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
  1649. * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
  1650. * int xany : S/W XANY flow control (0: no, 1: yes)
  1651. *
  1652. *
  1653. * Function 16: Get ths line status of this port
  1654. * Syntax:
  1655. * int MoxaPortLineStatus(int port);
  1656. * int port : port number (0 - 127)
  1657. *
  1658. * return: Bit 0 - CTS state (0: off, 1: on)
  1659. * Bit 1 - DSR state (0: off, 1: on)
  1660. * Bit 2 - DCD state (0: off, 1: on)
  1661. *
  1662. *
  1663. * Function 17: Check the DCD state has changed since the last read
  1664. * of this function.
  1665. * Syntax:
  1666. * int MoxaPortDCDChange(int port);
  1667. * int port : port number (0 - 127)
  1668. *
  1669. * return: 0 : no changed
  1670. * 1 : DCD has changed
  1671. *
  1672. *
  1673. * Function 18: Check ths current DCD state is ON or not.
  1674. * Syntax:
  1675. * int MoxaPortDCDON(int port);
  1676. * int port : port number (0 - 127)
  1677. *
  1678. * return: 0 : DCD off
  1679. * 1 : DCD on
  1680. *
  1681. *
  1682. * Function 19: Flush the Rx/Tx buffer data of this port.
  1683. * Syntax:
  1684. * void MoxaPortFlushData(int port, int mode);
  1685. * int port : port number (0 - 127)
  1686. * int mode
  1687. * 0 : flush the Rx buffer
  1688. * 1 : flush the Tx buffer
  1689. * 2 : flush the Rx and Tx buffer
  1690. *
  1691. *
  1692. * Function 20: Write data.
  1693. * Syntax:
  1694. * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
  1695. * int port : port number (0 - 127)
  1696. * unsigned char * buffer : pointer to write data buffer.
  1697. * int length : write data length
  1698. *
  1699. * return: 0 - length : real write data length
  1700. *
  1701. *
  1702. * Function 21: Read data.
  1703. * Syntax:
  1704. * int MoxaPortReadData(int port, struct tty_struct *tty);
  1705. * int port : port number (0 - 127)
  1706. * struct tty_struct *tty : tty for data
  1707. *
  1708. * return: 0 - length : real read data length
  1709. *
  1710. *
  1711. * Function 24: Get the Tx buffer current queued data bytes
  1712. * Syntax:
  1713. * int MoxaPortTxQueue(int port);
  1714. * int port : port number (0 - 127)
  1715. *
  1716. * return: .. : Tx buffer current queued data bytes
  1717. *
  1718. *
  1719. * Function 25: Get the Tx buffer current free space
  1720. * Syntax:
  1721. * int MoxaPortTxFree(int port);
  1722. * int port : port number (0 - 127)
  1723. *
  1724. * return: .. : Tx buffer current free space
  1725. *
  1726. *
  1727. * Function 26: Get the Rx buffer current queued data bytes
  1728. * Syntax:
  1729. * int MoxaPortRxQueue(int port);
  1730. * int port : port number (0 - 127)
  1731. *
  1732. * return: .. : Rx buffer current queued data bytes
  1733. *
  1734. *
  1735. * Function 28: Disable port data transmission.
  1736. * Syntax:
  1737. * void MoxaPortTxDisable(int port);
  1738. * int port : port number (0 - 127)
  1739. *
  1740. *
  1741. * Function 29: Enable port data transmission.
  1742. * Syntax:
  1743. * void MoxaPortTxEnable(int port);
  1744. * int port : port number (0 - 127)
  1745. *
  1746. *
  1747. * Function 31: Get the received BREAK signal count and reset it.
  1748. * Syntax:
  1749. * int MoxaPortResetBrkCnt(int port);
  1750. * int port : port number (0 - 127)
  1751. *
  1752. * return: 0 - .. : BREAK signal count
  1753. *
  1754. *
  1755. * Function 34: Send out a BREAK signal.
  1756. * Syntax:
  1757. * void MoxaPortSendBreak(int port, int ms100);
  1758. * int port : port number (0 - 127)
  1759. * int ms100 : break signal time interval.
  1760. * unit: 100 mini-second. if ms100 == 0, it will
  1761. * send out a about 250 ms BREAK signal.
  1762. *
  1763. */
  1764. int MoxaPortIsValid(int port)
  1765. {
  1766. if (moxaCard == 0)
  1767. return (0);
  1768. if (moxa_ports[port].chkPort == 0)
  1769. return (0);
  1770. return (1);
  1771. }
  1772. void MoxaPortEnable(int port)
  1773. {
  1774. void __iomem *ofsAddr;
  1775. int MoxaPortLineStatus(int);
  1776. short lowwater = 512;
  1777. ofsAddr = moxa_ports[port].tableAddr;
  1778. writew(lowwater, ofsAddr + Low_water);
  1779. moxa_ports[port].breakCnt = 0;
  1780. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1781. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1782. moxafunc(ofsAddr, FC_SetBreakIrq, 0);
  1783. } else {
  1784. writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
  1785. }
  1786. moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
  1787. moxafunc(ofsAddr, FC_FlushQueue, 2);
  1788. moxafunc(ofsAddr, FC_EnableCH, Magic_code);
  1789. MoxaPortLineStatus(port);
  1790. }
  1791. void MoxaPortDisable(int port)
  1792. {
  1793. void __iomem *ofsAddr = moxa_ports[port].tableAddr;
  1794. moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
  1795. moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
  1796. writew(0, ofsAddr + HostStat);
  1797. moxafunc(ofsAddr, FC_DisableCH, Magic_code);
  1798. }
  1799. long MoxaPortGetMaxBaud(int port)
  1800. {
  1801. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1802. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
  1803. return (460800L);
  1804. else
  1805. return (921600L);
  1806. }
  1807. long MoxaPortSetBaud(int port, long baud)
  1808. {
  1809. void __iomem *ofsAddr;
  1810. long max, clock;
  1811. unsigned int val;
  1812. if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
  1813. return (0);
  1814. ofsAddr = moxa_ports[port].tableAddr;
  1815. if (baud > max)
  1816. baud = max;
  1817. if (max == 38400L)
  1818. clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
  1819. else if (max == 57600L)
  1820. clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
  1821. else
  1822. clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
  1823. val = clock / baud;
  1824. moxafunc(ofsAddr, FC_SetBaud, val);
  1825. baud = clock / val;
  1826. moxa_ports[port].curBaud = baud;
  1827. return (baud);
  1828. }
  1829. int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
  1830. {
  1831. void __iomem *ofsAddr;
  1832. tcflag_t cflag;
  1833. tcflag_t mode = 0;
  1834. if (moxa_ports[port].chkPort == 0 || termio == 0)
  1835. return (-1);
  1836. ofsAddr = moxa_ports[port].tableAddr;
  1837. cflag = termio->c_cflag; /* termio->c_cflag */
  1838. mode = termio->c_cflag & CSIZE;
  1839. if (mode == CS5)
  1840. mode = MX_CS5;
  1841. else if (mode == CS6)
  1842. mode = MX_CS6;
  1843. else if (mode == CS7)
  1844. mode = MX_CS7;
  1845. else if (mode == CS8)
  1846. mode = MX_CS8;
  1847. if (termio->c_cflag & CSTOPB) {
  1848. if (mode == MX_CS5)
  1849. mode |= MX_STOP15;
  1850. else
  1851. mode |= MX_STOP2;
  1852. } else
  1853. mode |= MX_STOP1;
  1854. if (termio->c_cflag & PARENB) {
  1855. if (termio->c_cflag & PARODD)
  1856. mode |= MX_PARODD;
  1857. else
  1858. mode |= MX_PAREVEN;
  1859. } else
  1860. mode |= MX_PARNONE;
  1861. moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
  1862. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1863. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1864. if (baud >= 921600L)
  1865. return (-1);
  1866. }
  1867. MoxaPortSetBaud(port, baud);
  1868. if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
  1869. writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
  1870. writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
  1871. writeb(FC_SetXonXoff, ofsAddr + FuncCode);
  1872. wait_finish(ofsAddr);
  1873. }
  1874. return (0);
  1875. }
  1876. int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
  1877. {
  1878. if (!MoxaPortIsValid(port))
  1879. return (-1);
  1880. if (dtrState) {
  1881. if (moxa_ports[port].lineCtrl & DTR_ON)
  1882. *dtrState = 1;
  1883. else
  1884. *dtrState = 0;
  1885. }
  1886. if (rtsState) {
  1887. if (moxa_ports[port].lineCtrl & RTS_ON)
  1888. *rtsState = 1;
  1889. else
  1890. *rtsState = 0;
  1891. }
  1892. return (0);
  1893. }
  1894. void MoxaPortLineCtrl(int port, int dtr, int rts)
  1895. {
  1896. void __iomem *ofsAddr;
  1897. int mode;
  1898. ofsAddr = moxa_ports[port].tableAddr;
  1899. mode = 0;
  1900. if (dtr)
  1901. mode |= DTR_ON;
  1902. if (rts)
  1903. mode |= RTS_ON;
  1904. moxa_ports[port].lineCtrl = mode;
  1905. moxafunc(ofsAddr, FC_LineControl, mode);
  1906. }
  1907. void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
  1908. {
  1909. void __iomem *ofsAddr;
  1910. int mode;
  1911. ofsAddr = moxa_ports[port].tableAddr;
  1912. mode = 0;
  1913. if (rts)
  1914. mode |= RTS_FlowCtl;
  1915. if (cts)
  1916. mode |= CTS_FlowCtl;
  1917. if (txflow)
  1918. mode |= Tx_FlowCtl;
  1919. if (rxflow)
  1920. mode |= Rx_FlowCtl;
  1921. if (txany)
  1922. mode |= IXM_IXANY;
  1923. moxafunc(ofsAddr, FC_SetFlowCtl, mode);
  1924. }
  1925. int MoxaPortLineStatus(int port)
  1926. {
  1927. void __iomem *ofsAddr;
  1928. int val;
  1929. ofsAddr = moxa_ports[port].tableAddr;
  1930. if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
  1931. (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
  1932. moxafunc(ofsAddr, FC_LineStatus, 0);
  1933. val = readw(ofsAddr + FuncArg);
  1934. } else {
  1935. val = readw(ofsAddr + FlagStat) >> 4;
  1936. }
  1937. val &= 0x0B;
  1938. if (val & 8) {
  1939. val |= 4;
  1940. if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
  1941. moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
  1942. } else {
  1943. if (moxa_ports[port].DCDState & DCD_oldstate)
  1944. moxa_ports[port].DCDState = DCD_changed;
  1945. }
  1946. val &= 7;
  1947. return (val);
  1948. }
  1949. int MoxaPortDCDChange(int port)
  1950. {
  1951. int n;
  1952. if (moxa_ports[port].chkPort == 0)
  1953. return (0);
  1954. n = moxa_ports[port].DCDState;
  1955. moxa_ports[port].DCDState &= ~DCD_changed;
  1956. n &= DCD_changed;
  1957. return (n);
  1958. }
  1959. int MoxaPortDCDON(int port)
  1960. {
  1961. int n;
  1962. if (moxa_ports[port].chkPort == 0)
  1963. return (0);
  1964. if (moxa_ports[port].DCDState & DCD_oldstate)
  1965. n = 1;
  1966. else
  1967. n = 0;
  1968. return (n);
  1969. }
  1970. int MoxaPortWriteData(int port, unsigned char * buffer, int len)
  1971. {
  1972. int c, total, i;
  1973. ushort tail;
  1974. int cnt;
  1975. ushort head, tx_mask, spage, epage;
  1976. ushort pageno, pageofs, bufhead;
  1977. void __iomem *baseAddr, *ofsAddr, *ofs;
  1978. ofsAddr = moxa_ports[port].tableAddr;
  1979. baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
  1980. tx_mask = readw(ofsAddr + TX_mask);
  1981. spage = readw(ofsAddr + Page_txb);
  1982. epage = readw(ofsAddr + EndPage_txb);
  1983. tail = readw(ofsAddr + TXwptr);
  1984. head = readw(ofsAddr + TXrptr);
  1985. c = (head > tail) ? (head - tail - 1)
  1986. : (head - tail + tx_mask);
  1987. if (c > len)
  1988. c = len;
  1989. moxaLog.txcnt[port] += c;
  1990. total = c;
  1991. if (spage == epage) {
  1992. bufhead = readw(ofsAddr + Ofs_txb);
  1993. writew(spage, baseAddr + Control_reg);
  1994. while (c > 0) {
  1995. if (head > tail)
  1996. len = head - tail - 1;
  1997. else
  1998. len = tx_mask + 1 - tail;
  1999. len = (c > len) ? len : c;
  2000. ofs = baseAddr + DynPage_addr + bufhead + tail;
  2001. for (i = 0; i < len; i++)
  2002. writeb(*buffer++, ofs + i);
  2003. tail = (tail + len) & tx_mask;
  2004. c -= len;
  2005. }
  2006. writew(tail, ofsAddr + TXwptr);
  2007. } else {
  2008. len = c;
  2009. pageno = spage + (tail >> 13);
  2010. pageofs = tail & Page_mask;
  2011. do {
  2012. cnt = Page_size - pageofs;
  2013. if (cnt > c)
  2014. cnt = c;
  2015. c -= cnt;
  2016. writeb(pageno, baseAddr + Control_reg);
  2017. ofs = baseAddr + DynPage_addr + pageofs;
  2018. for (i = 0; i < cnt; i++)
  2019. writeb(*buffer++, ofs + i);
  2020. if (c == 0) {
  2021. writew((tail + len) & tx_mask, ofsAddr + TXwptr);
  2022. break;
  2023. }
  2024. if (++pageno == epage)
  2025. pageno = spage;
  2026. pageofs = 0;
  2027. } while (1);
  2028. }
  2029. writeb(1, ofsAddr + CD180TXirq); /* start to send */
  2030. return (total);
  2031. }
  2032. int MoxaPortReadData(int port, struct tty_struct *tty)
  2033. {
  2034. register ushort head, pageofs;
  2035. int i, count, cnt, len, total, remain;
  2036. ushort tail, rx_mask, spage, epage;
  2037. ushort pageno, bufhead;
  2038. void __iomem *baseAddr, *ofsAddr, *ofs;
  2039. ofsAddr = moxa_ports[port].tableAddr;
  2040. baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
  2041. head = readw(ofsAddr + RXrptr);
  2042. tail = readw(ofsAddr + RXwptr);
  2043. rx_mask = readw(ofsAddr + RX_mask);
  2044. spage = readw(ofsAddr + Page_rxb);
  2045. epage = readw(ofsAddr + EndPage_rxb);
  2046. count = (tail >= head) ? (tail - head)
  2047. : (tail - head + rx_mask + 1);
  2048. if (count == 0)
  2049. return 0;
  2050. total = count;
  2051. remain = count - total;
  2052. moxaLog.rxcnt[port] += total;
  2053. count = total;
  2054. if (spage == epage) {
  2055. bufhead = readw(ofsAddr + Ofs_rxb);
  2056. writew(spage, baseAddr + Control_reg);
  2057. while (count > 0) {
  2058. if (tail >= head)
  2059. len = tail - head;
  2060. else
  2061. len = rx_mask + 1 - head;
  2062. len = (count > len) ? len : count;
  2063. ofs = baseAddr + DynPage_addr + bufhead + head;
  2064. for (i = 0; i < len; i++)
  2065. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2066. head = (head + len) & rx_mask;
  2067. count -= len;
  2068. }
  2069. writew(head, ofsAddr + RXrptr);
  2070. } else {
  2071. len = count;
  2072. pageno = spage + (head >> 13);
  2073. pageofs = head & Page_mask;
  2074. do {
  2075. cnt = Page_size - pageofs;
  2076. if (cnt > count)
  2077. cnt = count;
  2078. count -= cnt;
  2079. writew(pageno, baseAddr + Control_reg);
  2080. ofs = baseAddr + DynPage_addr + pageofs;
  2081. for (i = 0; i < cnt; i++)
  2082. tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
  2083. if (count == 0) {
  2084. writew((head + len) & rx_mask, ofsAddr + RXrptr);
  2085. break;
  2086. }
  2087. if (++pageno == epage)
  2088. pageno = spage;
  2089. pageofs = 0;
  2090. } while (1);
  2091. }
  2092. if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
  2093. moxaLowWaterChk = 1;
  2094. moxa_ports[port].lowChkFlag = 1;
  2095. }
  2096. return (total);
  2097. }
  2098. int MoxaPortTxQueue(int port)
  2099. {
  2100. void __iomem *ofsAddr;
  2101. ushort rptr, wptr, mask;
  2102. int len;
  2103. ofsAddr = moxa_ports[port].tableAddr;
  2104. rptr = readw(ofsAddr + TXrptr);
  2105. wptr = readw(ofsAddr + TXwptr);
  2106. mask = readw(ofsAddr + TX_mask);
  2107. len = (wptr - rptr) & mask;
  2108. return (len);
  2109. }
  2110. int MoxaPortTxFree(int port)
  2111. {
  2112. void __iomem *ofsAddr;
  2113. ushort rptr, wptr, mask;
  2114. int len;
  2115. ofsAddr = moxa_ports[port].tableAddr;
  2116. rptr = readw(ofsAddr + TXrptr);
  2117. wptr = readw(ofsAddr + TXwptr);
  2118. mask = readw(ofsAddr + TX_mask);
  2119. len = mask - ((wptr - rptr) & mask);
  2120. return (len);
  2121. }
  2122. int MoxaPortRxQueue(int port)
  2123. {
  2124. void __iomem *ofsAddr;
  2125. ushort rptr, wptr, mask;
  2126. int len;
  2127. ofsAddr = moxa_ports[port].tableAddr;
  2128. rptr = readw(ofsAddr + RXrptr);
  2129. wptr = readw(ofsAddr + RXwptr);
  2130. mask = readw(ofsAddr + RX_mask);
  2131. len = (wptr - rptr) & mask;
  2132. return (len);
  2133. }
  2134. void MoxaPortTxDisable(int port)
  2135. {
  2136. void __iomem *ofsAddr;
  2137. ofsAddr = moxa_ports[port].tableAddr;
  2138. moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
  2139. }
  2140. void MoxaPortTxEnable(int port)
  2141. {
  2142. void __iomem *ofsAddr;
  2143. ofsAddr = moxa_ports[port].tableAddr;
  2144. moxafunc(ofsAddr, FC_SetXonState, Magic_code);
  2145. }
  2146. int MoxaPortResetBrkCnt(int port)
  2147. {
  2148. ushort cnt;
  2149. cnt = moxa_ports[port].breakCnt;
  2150. moxa_ports[port].breakCnt = 0;
  2151. return (cnt);
  2152. }
  2153. void MoxaPortSendBreak(int port, int ms100)
  2154. {
  2155. void __iomem *ofsAddr;
  2156. ofsAddr = moxa_ports[port].tableAddr;
  2157. if (ms100) {
  2158. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2159. moxadelay(ms100 * (HZ / 10));
  2160. } else {
  2161. moxafunc(ofsAddr, FC_SendBreak, Magic_code);
  2162. moxadelay(HZ / 4); /* 250 ms */
  2163. }
  2164. moxafunc(ofsAddr, FC_StopBreak, Magic_code);
  2165. }
  2166. static int moxa_get_serial_info(struct moxa_port *info,
  2167. struct serial_struct __user *retinfo)
  2168. {
  2169. struct serial_struct tmp;
  2170. memset(&tmp, 0, sizeof(tmp));
  2171. tmp.type = info->type;
  2172. tmp.line = info->port;
  2173. tmp.port = 0;
  2174. tmp.irq = 0;
  2175. tmp.flags = info->asyncflags;
  2176. tmp.baud_base = 921600;
  2177. tmp.close_delay = info->close_delay;
  2178. tmp.closing_wait = info->closing_wait;
  2179. tmp.custom_divisor = 0;
  2180. tmp.hub6 = 0;
  2181. if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2182. return -EFAULT;
  2183. return (0);
  2184. }
  2185. static int moxa_set_serial_info(struct moxa_port *info,
  2186. struct serial_struct __user *new_info)
  2187. {
  2188. struct serial_struct new_serial;
  2189. if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2190. return -EFAULT;
  2191. if ((new_serial.irq != 0) ||
  2192. (new_serial.port != 0) ||
  2193. // (new_serial.type != info->type) ||
  2194. (new_serial.custom_divisor != 0) ||
  2195. (new_serial.baud_base != 921600))
  2196. return (-EPERM);
  2197. if (!capable(CAP_SYS_ADMIN)) {
  2198. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  2199. (info->asyncflags & ~ASYNC_USR_MASK)))
  2200. return (-EPERM);
  2201. } else {
  2202. info->close_delay = new_serial.close_delay * HZ / 100;
  2203. info->closing_wait = new_serial.closing_wait * HZ / 100;
  2204. }
  2205. new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
  2206. new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
  2207. if (new_serial.type == PORT_16550A) {
  2208. MoxaSetFifo(info->port, 1);
  2209. } else {
  2210. MoxaSetFifo(info->port, 0);
  2211. }
  2212. info->type = new_serial.type;
  2213. return (0);
  2214. }
  2215. /*****************************************************************************
  2216. * Static local functions: *
  2217. *****************************************************************************/
  2218. /*
  2219. * moxadelay - delays a specified number ticks
  2220. */
  2221. static void moxadelay(int tick)
  2222. {
  2223. unsigned long st, et;
  2224. st = jiffies;
  2225. et = st + tick;
  2226. while (time_before(jiffies, et));
  2227. }
  2228. static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
  2229. {
  2230. writew(arg, ofsAddr + FuncArg);
  2231. writew(cmd, ofsAddr + FuncCode);
  2232. wait_finish(ofsAddr);
  2233. }
  2234. static void wait_finish(void __iomem *ofsAddr)
  2235. {
  2236. unsigned long i, j;
  2237. i = jiffies;
  2238. while (readw(ofsAddr + FuncCode) != 0) {
  2239. j = jiffies;
  2240. if ((j - i) > moxaFuncTout) {
  2241. return;
  2242. }
  2243. }
  2244. }
  2245. static void low_water_check(void __iomem *ofsAddr)
  2246. {
  2247. int len;
  2248. ushort rptr, wptr, mask;
  2249. if (readb(ofsAddr + FlagStat) & Xoff_state) {
  2250. rptr = readw(ofsAddr + RXrptr);
  2251. wptr = readw(ofsAddr + RXwptr);
  2252. mask = readw(ofsAddr + RX_mask);
  2253. len = (wptr - rptr) & mask;
  2254. if (len <= Low_water)
  2255. moxafunc(ofsAddr, FC_SendXon, 0);
  2256. }
  2257. }
  2258. static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
  2259. {
  2260. void __iomem *baseAddr;
  2261. int i;
  2262. if(copy_from_user(moxaBuff, tmp, len))
  2263. return -EFAULT;
  2264. baseAddr = moxa_boards[cardno].basemem;
  2265. writeb(HW_reset, baseAddr + Control_reg); /* reset */
  2266. moxadelay(1); /* delay 10 ms */
  2267. for (i = 0; i < 4096; i++)
  2268. writeb(0, baseAddr + i); /* clear fix page */
  2269. for (i = 0; i < len; i++)
  2270. writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
  2271. writeb(0, baseAddr + Control_reg); /* restart */
  2272. return (0);
  2273. }
  2274. static int moxafindcard(int cardno)
  2275. {
  2276. void __iomem *baseAddr;
  2277. ushort tmp;
  2278. baseAddr = moxa_boards[cardno].basemem;
  2279. switch (moxa_boards[cardno].boardType) {
  2280. case MOXA_BOARD_C218_ISA:
  2281. case MOXA_BOARD_C218_PCI:
  2282. if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
  2283. return (-1);
  2284. }
  2285. break;
  2286. case MOXA_BOARD_CP204J:
  2287. if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
  2288. return (-1);
  2289. }
  2290. break;
  2291. default:
  2292. if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
  2293. return (-1);
  2294. }
  2295. if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
  2296. return (-2);
  2297. }
  2298. }
  2299. return (0);
  2300. }
  2301. static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
  2302. {
  2303. void __iomem *baseAddr;
  2304. int i;
  2305. if(len > sizeof(moxaBuff))
  2306. return -EINVAL;
  2307. if(copy_from_user(moxaBuff, tmp, len))
  2308. return -EFAULT;
  2309. baseAddr = moxa_boards[cardno].basemem;
  2310. writew(len - 7168 - 2, baseAddr + C320bapi_len);
  2311. writeb(1, baseAddr + Control_reg); /* Select Page 1 */
  2312. for (i = 0; i < 7168; i++)
  2313. writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
  2314. writeb(2, baseAddr + Control_reg); /* Select Page 2 */
  2315. for (i = 0; i < (len - 7168); i++)
  2316. writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
  2317. return (0);
  2318. }
  2319. static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
  2320. {
  2321. void __iomem *baseAddr, *ofsAddr;
  2322. int retval, port, i;
  2323. if(copy_from_user(moxaBuff, tmp, len))
  2324. return -EFAULT;
  2325. baseAddr = moxa_boards[cardno].basemem;
  2326. switch (moxa_boards[cardno].boardType) {
  2327. case MOXA_BOARD_C218_ISA:
  2328. case MOXA_BOARD_C218_PCI:
  2329. case MOXA_BOARD_CP204J:
  2330. retval = moxaloadc218(cardno, baseAddr, len);
  2331. if (retval)
  2332. return (retval);
  2333. port = cardno * MAX_PORTS_PER_BOARD;
  2334. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2335. struct moxa_port *p = &moxa_ports[port];
  2336. p->chkPort = 1;
  2337. p->curBaud = 9600L;
  2338. p->DCDState = 0;
  2339. p->tableAddr = baseAddr + Extern_table + Extern_size * i;
  2340. ofsAddr = p->tableAddr;
  2341. writew(C218rx_mask, ofsAddr + RX_mask);
  2342. writew(C218tx_mask, ofsAddr + TX_mask);
  2343. writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
  2344. writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
  2345. writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
  2346. writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
  2347. }
  2348. break;
  2349. default:
  2350. retval = moxaloadc320(cardno, baseAddr, len,
  2351. &moxa_boards[cardno].numPorts);
  2352. if (retval)
  2353. return (retval);
  2354. port = cardno * MAX_PORTS_PER_BOARD;
  2355. for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
  2356. struct moxa_port *p = &moxa_ports[port];
  2357. p->chkPort = 1;
  2358. p->curBaud = 9600L;
  2359. p->DCDState = 0;
  2360. p->tableAddr = baseAddr + Extern_table + Extern_size * i;
  2361. ofsAddr = p->tableAddr;
  2362. if (moxa_boards[cardno].numPorts == 8) {
  2363. writew(C320p8rx_mask, ofsAddr + RX_mask);
  2364. writew(C320p8tx_mask, ofsAddr + TX_mask);
  2365. writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
  2366. writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
  2367. writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
  2368. writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
  2369. } else if (moxa_boards[cardno].numPorts == 16) {
  2370. writew(C320p16rx_mask, ofsAddr + RX_mask);
  2371. writew(C320p16tx_mask, ofsAddr + TX_mask);
  2372. writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
  2373. writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
  2374. writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
  2375. writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
  2376. } else if (moxa_boards[cardno].numPorts == 24) {
  2377. writew(C320p24rx_mask, ofsAddr + RX_mask);
  2378. writew(C320p24tx_mask, ofsAddr + TX_mask);
  2379. writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
  2380. writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
  2381. writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
  2382. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2383. } else if (moxa_boards[cardno].numPorts == 32) {
  2384. writew(C320p32rx_mask, ofsAddr + RX_mask);
  2385. writew(C320p32tx_mask, ofsAddr + TX_mask);
  2386. writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
  2387. writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
  2388. writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
  2389. writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
  2390. writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
  2391. }
  2392. }
  2393. break;
  2394. }
  2395. moxa_boards[cardno].loadstat = 1;
  2396. return (0);
  2397. }
  2398. static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
  2399. {
  2400. char retry;
  2401. int i, j, len1, len2;
  2402. ushort usum, *ptr, keycode;
  2403. if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
  2404. keycode = CP204J_KeyCode;
  2405. else
  2406. keycode = C218_KeyCode;
  2407. usum = 0;
  2408. len1 = len >> 1;
  2409. ptr = (ushort *) moxaBuff;
  2410. for (i = 0; i < len1; i++)
  2411. usum += le16_to_cpu(*(ptr + i));
  2412. retry = 0;
  2413. do {
  2414. len1 = len >> 1;
  2415. j = 0;
  2416. while (len1) {
  2417. len2 = (len1 > 2048) ? 2048 : len1;
  2418. len1 -= len2;
  2419. for (i = 0; i < len2 << 1; i++)
  2420. writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
  2421. j += i;
  2422. writew(len2, baseAddr + C218DLoad_len);
  2423. writew(0, baseAddr + C218_key);
  2424. for (i = 0; i < 100; i++) {
  2425. if (readw(baseAddr + C218_key) == keycode)
  2426. break;
  2427. moxadelay(1); /* delay 10 ms */
  2428. }
  2429. if (readw(baseAddr + C218_key) != keycode) {
  2430. return (-1);
  2431. }
  2432. }
  2433. writew(0, baseAddr + C218DLoad_len);
  2434. writew(usum, baseAddr + C218check_sum);
  2435. writew(0, baseAddr + C218_key);
  2436. for (i = 0; i < 100; i++) {
  2437. if (readw(baseAddr + C218_key) == keycode)
  2438. break;
  2439. moxadelay(1); /* delay 10 ms */
  2440. }
  2441. retry++;
  2442. } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
  2443. if (readb(baseAddr + C218chksum_ok) != 1) {
  2444. return (-1);
  2445. }
  2446. writew(0, baseAddr + C218_key);
  2447. for (i = 0; i < 100; i++) {
  2448. if (readw(baseAddr + Magic_no) == Magic_code)
  2449. break;
  2450. moxadelay(1); /* delay 10 ms */
  2451. }
  2452. if (readw(baseAddr + Magic_no) != Magic_code) {
  2453. return (-1);
  2454. }
  2455. writew(1, baseAddr + Disable_IRQ);
  2456. writew(0, baseAddr + Magic_no);
  2457. for (i = 0; i < 100; i++) {
  2458. if (readw(baseAddr + Magic_no) == Magic_code)
  2459. break;
  2460. moxadelay(1); /* delay 10 ms */
  2461. }
  2462. if (readw(baseAddr + Magic_no) != Magic_code) {
  2463. return (-1);
  2464. }
  2465. moxaCard = 1;
  2466. moxa_boards[cardno].intNdx = baseAddr + IRQindex;
  2467. moxa_boards[cardno].intPend = baseAddr + IRQpending;
  2468. moxa_boards[cardno].intTable = baseAddr + IRQtable;
  2469. return (0);
  2470. }
  2471. static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
  2472. {
  2473. ushort usum;
  2474. int i, j, wlen, len2, retry;
  2475. ushort *uptr;
  2476. usum = 0;
  2477. wlen = len >> 1;
  2478. uptr = (ushort *) moxaBuff;
  2479. for (i = 0; i < wlen; i++)
  2480. usum += le16_to_cpu(uptr[i]);
  2481. retry = 0;
  2482. j = 0;
  2483. do {
  2484. while (wlen) {
  2485. if (wlen > 2048)
  2486. len2 = 2048;
  2487. else
  2488. len2 = wlen;
  2489. wlen -= len2;
  2490. len2 <<= 1;
  2491. for (i = 0; i < len2; i++)
  2492. writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
  2493. len2 >>= 1;
  2494. j += i;
  2495. writew(len2, baseAddr + C320DLoad_len);
  2496. writew(0, baseAddr + C320_key);
  2497. for (i = 0; i < 10; i++) {
  2498. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2499. break;
  2500. moxadelay(1);
  2501. }
  2502. if (readw(baseAddr + C320_key) != C320_KeyCode)
  2503. return (-1);
  2504. }
  2505. writew(0, baseAddr + C320DLoad_len);
  2506. writew(usum, baseAddr + C320check_sum);
  2507. writew(0, baseAddr + C320_key);
  2508. for (i = 0; i < 10; i++) {
  2509. if (readw(baseAddr + C320_key) == C320_KeyCode)
  2510. break;
  2511. moxadelay(1);
  2512. }
  2513. retry++;
  2514. } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
  2515. if (readb(baseAddr + C320chksum_ok) != 1)
  2516. return (-1);
  2517. writew(0, baseAddr + C320_key);
  2518. for (i = 0; i < 600; i++) {
  2519. if (readw(baseAddr + Magic_no) == Magic_code)
  2520. break;
  2521. moxadelay(1);
  2522. }
  2523. if (readw(baseAddr + Magic_no) != Magic_code)
  2524. return (-100);
  2525. if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
  2526. writew(0x3800, baseAddr + TMS320_PORT1);
  2527. writew(0x3900, baseAddr + TMS320_PORT2);
  2528. writew(28499, baseAddr + TMS320_CLOCK);
  2529. } else {
  2530. writew(0x3200, baseAddr + TMS320_PORT1);
  2531. writew(0x3400, baseAddr + TMS320_PORT2);
  2532. writew(19999, baseAddr + TMS320_CLOCK);
  2533. }
  2534. writew(1, baseAddr + Disable_IRQ);
  2535. writew(0, baseAddr + Magic_no);
  2536. for (i = 0; i < 500; i++) {
  2537. if (readw(baseAddr + Magic_no) == Magic_code)
  2538. break;
  2539. moxadelay(1);
  2540. }
  2541. if (readw(baseAddr + Magic_no) != Magic_code)
  2542. return (-102);
  2543. j = readw(baseAddr + Module_cnt);
  2544. if (j <= 0)
  2545. return (-101);
  2546. *numPorts = j * 8;
  2547. writew(j, baseAddr + Module_no);
  2548. writew(0, baseAddr + Magic_no);
  2549. for (i = 0; i < 600; i++) {
  2550. if (readw(baseAddr + Magic_no) == Magic_code)
  2551. break;
  2552. moxadelay(1);
  2553. }
  2554. if (readw(baseAddr + Magic_no) != Magic_code)
  2555. return (-102);
  2556. moxaCard = 1;
  2557. moxa_boards[cardno].intNdx = baseAddr + IRQindex;
  2558. moxa_boards[cardno].intPend = baseAddr + IRQpending;
  2559. moxa_boards[cardno].intTable = baseAddr + IRQtable;
  2560. return (0);
  2561. }
  2562. static void MoxaSetFifo(int port, int enable)
  2563. {
  2564. void __iomem *ofsAddr = moxa_ports[port].tableAddr;
  2565. if (!enable) {
  2566. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
  2567. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
  2568. } else {
  2569. moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
  2570. moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
  2571. }
  2572. }