moxa.c 83 KB

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