moxa.c 75 KB

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