moxa.c 83 KB

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