moxa.c 84 KB

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