mxser_new.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016
  1. /*
  2. * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
  3. *
  4. * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
  5. * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
  6. *
  7. * This code is loosely based on the 1.8 moxa driver which is based on
  8. * Linux serial driver, written by Linus Torvalds, Theodore T'so and
  9. * others.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
  17. * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
  18. * - Fixed x86_64 cleanness
  19. * - Fixed sleep with spinlock held in mxser_send_break
  20. */
  21. #include <linux/module.h>
  22. #include <linux/autoconf.h>
  23. #include <linux/errno.h>
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/timer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_reg.h>
  32. #include <linux/major.h>
  33. #include <linux/string.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/gfp.h>
  37. #include <linux/ioport.h>
  38. #include <linux/mm.h>
  39. #include <linux/smp_lock.h>
  40. #include <linux/delay.h>
  41. #include <linux/pci.h>
  42. #include <asm/system.h>
  43. #include <asm/io.h>
  44. #include <asm/irq.h>
  45. #include <asm/bitops.h>
  46. #include <asm/uaccess.h>
  47. #include "mxser_new.h"
  48. #define MXSER_VERSION "2.0"
  49. #define MXSERMAJOR 174
  50. #define MXSERCUMAJOR 175
  51. #define MXSER_EVENT_TXLOW 1
  52. #define MXSER_EVENT_HANGUP 2
  53. #define MXSER_BOARDS 4 /* Max. boards */
  54. #define MXSER_PORTS 32 /* Max. ports */
  55. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  56. #define MXSER_ISR_PASS_LIMIT 99999L
  57. #define MXSER_ERR_IOADDR -1
  58. #define MXSER_ERR_IRQ -2
  59. #define MXSER_ERR_IRQ_CONFLIT -3
  60. #define MXSER_ERR_VECTOR -4
  61. #define SERIAL_TYPE_NORMAL 1
  62. #define SERIAL_TYPE_CALLOUT 2
  63. #define WAKEUP_CHARS 256
  64. #define UART_MCR_AFE 0x20
  65. #define UART_LSR_SPECIAL 0x1E
  66. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
  67. IXON|IXOFF))
  68. #define C168_ASIC_ID 1
  69. #define C104_ASIC_ID 2
  70. #define C102_ASIC_ID 0xB
  71. #define CI132_ASIC_ID 4
  72. #define CI134_ASIC_ID 3
  73. #define CI104J_ASIC_ID 5
  74. enum {
  75. MXSER_BOARD_C168_ISA = 1,
  76. MXSER_BOARD_C104_ISA,
  77. MXSER_BOARD_CI104J,
  78. MXSER_BOARD_C168_PCI,
  79. MXSER_BOARD_C104_PCI,
  80. MXSER_BOARD_C102_ISA,
  81. MXSER_BOARD_CI132,
  82. MXSER_BOARD_CI134,
  83. MXSER_BOARD_CP132,
  84. MXSER_BOARD_CP114,
  85. MXSER_BOARD_CT114,
  86. MXSER_BOARD_CP102,
  87. MXSER_BOARD_CP104U,
  88. MXSER_BOARD_CP168U,
  89. MXSER_BOARD_CP132U,
  90. MXSER_BOARD_CP134U,
  91. MXSER_BOARD_CP104JU,
  92. MXSER_BOARD_RC7000,
  93. MXSER_BOARD_CP118U,
  94. MXSER_BOARD_CP102UL,
  95. MXSER_BOARD_CP102U,
  96. MXSER_BOARD_CP118EL,
  97. MXSER_BOARD_CP168EL,
  98. MXSER_BOARD_CP104EL
  99. };
  100. static char *mxser_brdname[] = {
  101. "C168 series",
  102. "C104 series",
  103. "CI-104J series",
  104. "C168H/PCI series",
  105. "C104H/PCI series",
  106. "C102 series",
  107. "CI-132 series",
  108. "CI-134 series",
  109. "CP-132 series",
  110. "CP-114 series",
  111. "CT-114 series",
  112. "CP-102 series",
  113. "CP-104U series",
  114. "CP-168U series",
  115. "CP-132U series",
  116. "CP-134U series",
  117. "CP-104JU series",
  118. "Moxa UC7000 Serial",
  119. "CP-118U series",
  120. "CP-102UL series",
  121. "CP-102U series",
  122. "CP-118EL series",
  123. "CP-168EL series",
  124. "CP-104EL series"
  125. };
  126. static int mxser_numports[] = {
  127. 8, /* C168-ISA */
  128. 4, /* C104-ISA */
  129. 4, /* CI104J */
  130. 8, /* C168-PCI */
  131. 4, /* C104-PCI */
  132. 2, /* C102-ISA */
  133. 2, /* CI132 */
  134. 4, /* CI134 */
  135. 2, /* CP132 */
  136. 4, /* CP114 */
  137. 4, /* CT114 */
  138. 2, /* CP102 */
  139. 4, /* CP104U */
  140. 8, /* CP168U */
  141. 2, /* CP132U */
  142. 4, /* CP134U */
  143. 4, /* CP104JU */
  144. 8, /* RC7000 */
  145. 8, /* CP118U */
  146. 2, /* CP102UL */
  147. 2, /* CP102U */
  148. 8, /* CP118EL */
  149. 8, /* CP168EL */
  150. 4 /* CP104EL */
  151. };
  152. #define UART_TYPE_NUM 2
  153. static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
  154. MOXA_MUST_MU150_HWID,
  155. MOXA_MUST_MU860_HWID
  156. };
  157. /* This is only for PCI */
  158. #define UART_INFO_NUM 3
  159. struct mxpciuart_info {
  160. int type;
  161. int tx_fifo;
  162. int rx_fifo;
  163. int xmit_fifo_size;
  164. int rx_high_water;
  165. int rx_trigger;
  166. int rx_low_water;
  167. long max_baud;
  168. };
  169. static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
  170. {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
  171. {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
  172. {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
  173. };
  174. static struct pci_device_id mxser_pcibrds[] = {
  175. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
  176. .driver_data = MXSER_BOARD_C168_PCI },
  177. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
  178. .driver_data = MXSER_BOARD_C104_PCI },
  179. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
  180. .driver_data = MXSER_BOARD_CP132 },
  181. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
  182. .driver_data = MXSER_BOARD_CP114 },
  183. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
  184. .driver_data = MXSER_BOARD_CT114 },
  185. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
  186. .driver_data = MXSER_BOARD_CP102 },
  187. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
  188. .driver_data = MXSER_BOARD_CP104U },
  189. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
  190. .driver_data = MXSER_BOARD_CP168U },
  191. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
  192. .driver_data = MXSER_BOARD_CP132U },
  193. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
  194. .driver_data = MXSER_BOARD_CP134U },
  195. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
  196. .driver_data = MXSER_BOARD_CP104JU },
  197. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
  198. .driver_data = MXSER_BOARD_RC7000 },
  199. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
  200. .driver_data = MXSER_BOARD_CP118U },
  201. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
  202. .driver_data = MXSER_BOARD_CP102UL },
  203. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
  204. .driver_data = MXSER_BOARD_CP102U },
  205. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
  206. .driver_data = MXSER_BOARD_CP118EL },
  207. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
  208. .driver_data = MXSER_BOARD_CP168EL },
  209. { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
  210. .driver_data = MXSER_BOARD_CP104EL },
  211. { }
  212. };
  213. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  214. static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
  215. static int ttymajor = MXSERMAJOR;
  216. static int calloutmajor = MXSERCUMAJOR;
  217. /* Variables for insmod */
  218. MODULE_AUTHOR("Casper Yang");
  219. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  220. module_param_array(ioaddr, int, NULL, 0);
  221. module_param(ttymajor, int, 0);
  222. MODULE_LICENSE("GPL");
  223. struct mxser_log {
  224. int tick;
  225. unsigned long rxcnt[MXSER_PORTS];
  226. unsigned long txcnt[MXSER_PORTS];
  227. };
  228. struct mxser_mon {
  229. unsigned long rxcnt;
  230. unsigned long txcnt;
  231. unsigned long up_rxcnt;
  232. unsigned long up_txcnt;
  233. int modem_status;
  234. unsigned char hold_reason;
  235. };
  236. struct mxser_mon_ext {
  237. unsigned long rx_cnt[32];
  238. unsigned long tx_cnt[32];
  239. unsigned long up_rxcnt[32];
  240. unsigned long up_txcnt[32];
  241. int modem_status[32];
  242. long baudrate[32];
  243. int databits[32];
  244. int stopbits[32];
  245. int parity[32];
  246. int flowctrl[32];
  247. int fifo[32];
  248. int iftype[32];
  249. };
  250. struct mxser_board;
  251. struct mxser_port {
  252. struct mxser_board *board;
  253. struct tty_struct *tty;
  254. unsigned long ioaddr;
  255. unsigned long opmode_ioaddr;
  256. int max_baud;
  257. int rx_high_water;
  258. int rx_trigger; /* Rx fifo trigger level */
  259. int rx_low_water;
  260. int baud_base; /* max. speed */
  261. long realbaud;
  262. int type; /* UART type */
  263. int flags; /* defined in tty.h */
  264. long session; /* Session of opening process */
  265. long pgrp; /* pgrp of opening process */
  266. int x_char; /* xon/xoff character */
  267. int IER; /* Interrupt Enable Register */
  268. int MCR; /* Modem control register */
  269. unsigned char stop_rx;
  270. unsigned char ldisc_stop_rx;
  271. int custom_divisor;
  272. int close_delay;
  273. unsigned short closing_wait;
  274. unsigned char err_shadow;
  275. unsigned long event;
  276. int count; /* # of fd on device */
  277. int blocked_open; /* # of blocked opens */
  278. struct async_icount icount; /* kernel counters for 4 input interrupts */
  279. int timeout;
  280. int read_status_mask;
  281. int ignore_status_mask;
  282. int xmit_fifo_size;
  283. unsigned char *xmit_buf;
  284. int xmit_head;
  285. int xmit_tail;
  286. int xmit_cnt;
  287. struct termios normal_termios;
  288. struct termios callout_termios;
  289. struct mxser_mon mon_data;
  290. spinlock_t slock;
  291. struct work_struct tqueue;
  292. wait_queue_head_t open_wait;
  293. wait_queue_head_t close_wait;
  294. wait_queue_head_t delta_msr_wait;
  295. };
  296. struct mxser_board {
  297. struct pci_dev *pdev; /* temporary (until pci probing) */
  298. int irq;
  299. int board_type;
  300. unsigned int nports;
  301. unsigned long vector;
  302. unsigned long vector_mask;
  303. int chip_flag;
  304. int uart_type;
  305. struct mxser_port ports[MXSER_PORTS_PER_BOARD];
  306. };
  307. struct mxser_mstatus {
  308. tcflag_t cflag;
  309. int cts;
  310. int dsr;
  311. int ri;
  312. int dcd;
  313. };
  314. static struct mxser_mstatus GMStatus[MXSER_PORTS];
  315. static int mxserBoardCAP[MXSER_BOARDS] = {
  316. 0, 0, 0, 0
  317. /* 0x180, 0x280, 0x200, 0x320 */
  318. };
  319. static struct mxser_board mxser_boards[MXSER_BOARDS];
  320. static struct tty_driver *mxvar_sdriver;
  321. static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
  322. static struct termios *mxvar_termios[MXSER_PORTS + 1];
  323. static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
  324. static struct mxser_log mxvar_log;
  325. static int mxvar_diagflag;
  326. static unsigned char mxser_msr[MXSER_PORTS + 1];
  327. static struct mxser_mon_ext mon_data_ext;
  328. static int mxser_set_baud_method[MXSER_PORTS + 1];
  329. static spinlock_t gm_lock;
  330. static int CheckIsMoxaMust(int io)
  331. {
  332. u8 oldmcr, hwid;
  333. int i;
  334. outb(0, io + UART_LCR);
  335. DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
  336. oldmcr = inb(io + UART_MCR);
  337. outb(0, io + UART_MCR);
  338. SET_MOXA_MUST_XON1_VALUE(io, 0x11);
  339. if ((hwid = inb(io + UART_MCR)) != 0) {
  340. outb(oldmcr, io + UART_MCR);
  341. return MOXA_OTHER_UART;
  342. }
  343. GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
  344. for (i = 0; i < UART_TYPE_NUM; i++) {
  345. if (hwid == Gmoxa_uart_id[i])
  346. return (int)hwid;
  347. }
  348. return MOXA_OTHER_UART;
  349. }
  350. /* above is modified by Victor Yu. 08-15-2002 */
  351. static void process_txrx_fifo(struct mxser_port *info)
  352. {
  353. int i;
  354. if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
  355. info->rx_trigger = 1;
  356. info->rx_high_water = 1;
  357. info->rx_low_water = 1;
  358. info->xmit_fifo_size = 1;
  359. } else
  360. for (i = 0; i < UART_INFO_NUM; i++)
  361. if (info->board->chip_flag == Gpci_uart_info[i].type) {
  362. info->rx_trigger = Gpci_uart_info[i].rx_trigger;
  363. info->rx_low_water = Gpci_uart_info[i].rx_low_water;
  364. info->rx_high_water = Gpci_uart_info[i].rx_high_water;
  365. info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
  366. break;
  367. }
  368. }
  369. static void mxser_do_softint(void *private_)
  370. {
  371. struct mxser_port *info = private_;
  372. struct tty_struct *tty;
  373. tty = info->tty;
  374. if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
  375. tty_wakeup(tty);
  376. if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
  377. tty_hangup(tty);
  378. }
  379. static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
  380. {
  381. unsigned char status = 0;
  382. status = inb(baseaddr + UART_MSR);
  383. mxser_msr[port] &= 0x0F;
  384. mxser_msr[port] |= status;
  385. status = mxser_msr[port];
  386. if (mode)
  387. mxser_msr[port] = 0;
  388. return status;
  389. }
  390. static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
  391. struct mxser_port *port)
  392. {
  393. DECLARE_WAITQUEUE(wait, current);
  394. int retval;
  395. int do_clocal = 0;
  396. unsigned long flags;
  397. /*
  398. * If non-blocking mode is set, or the port is not enabled,
  399. * then make the check up front and then exit.
  400. */
  401. if ((filp->f_flags & O_NONBLOCK) ||
  402. test_bit(TTY_IO_ERROR, &tty->flags)) {
  403. port->flags |= ASYNC_NORMAL_ACTIVE;
  404. return 0;
  405. }
  406. if (tty->termios->c_cflag & CLOCAL)
  407. do_clocal = 1;
  408. /*
  409. * Block waiting for the carrier detect and the line to become
  410. * free (i.e., not in use by the callout). While we are in
  411. * this loop, port->count is dropped by one, so that
  412. * mxser_close() knows when to free things. We restore it upon
  413. * exit, either normal or abnormal.
  414. */
  415. retval = 0;
  416. add_wait_queue(&port->open_wait, &wait);
  417. spin_lock_irqsave(&port->slock, flags);
  418. if (!tty_hung_up_p(filp))
  419. port->count--;
  420. spin_unlock_irqrestore(&port->slock, flags);
  421. port->blocked_open++;
  422. while (1) {
  423. spin_lock_irqsave(&port->slock, flags);
  424. outb(inb(port->ioaddr + UART_MCR) |
  425. UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
  426. spin_unlock_irqrestore(&port->slock, flags);
  427. set_current_state(TASK_INTERRUPTIBLE);
  428. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  429. if (port->flags & ASYNC_HUP_NOTIFY)
  430. retval = -EAGAIN;
  431. else
  432. retval = -ERESTARTSYS;
  433. break;
  434. }
  435. if (!(port->flags & ASYNC_CLOSING) &&
  436. (do_clocal ||
  437. (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
  438. break;
  439. if (signal_pending(current)) {
  440. retval = -ERESTARTSYS;
  441. break;
  442. }
  443. schedule();
  444. }
  445. set_current_state(TASK_RUNNING);
  446. remove_wait_queue(&port->open_wait, &wait);
  447. if (!tty_hung_up_p(filp))
  448. port->count++;
  449. port->blocked_open--;
  450. if (retval)
  451. return retval;
  452. port->flags |= ASYNC_NORMAL_ACTIVE;
  453. return 0;
  454. }
  455. static int mxser_set_baud(struct mxser_port *info, long newspd)
  456. {
  457. int quot = 0;
  458. unsigned char cval;
  459. int ret = 0;
  460. unsigned long flags;
  461. if (!info->tty || !info->tty->termios)
  462. return ret;
  463. if (!(info->ioaddr))
  464. return ret;
  465. if (newspd > info->max_baud)
  466. return 0;
  467. info->realbaud = newspd;
  468. if (newspd == 134) {
  469. quot = (2 * info->baud_base / 269);
  470. } else if (newspd) {
  471. quot = info->baud_base / newspd;
  472. if (quot == 0)
  473. quot = 1;
  474. } else {
  475. quot = 0;
  476. }
  477. info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
  478. info->timeout += HZ / 50; /* Add .02 seconds of slop */
  479. if (quot) {
  480. spin_lock_irqsave(&info->slock, flags);
  481. info->MCR |= UART_MCR_DTR;
  482. outb(info->MCR, info->ioaddr + UART_MCR);
  483. spin_unlock_irqrestore(&info->slock, flags);
  484. } else {
  485. spin_lock_irqsave(&info->slock, flags);
  486. info->MCR &= ~UART_MCR_DTR;
  487. outb(info->MCR, info->ioaddr + UART_MCR);
  488. spin_unlock_irqrestore(&info->slock, flags);
  489. return ret;
  490. }
  491. cval = inb(info->ioaddr + UART_LCR);
  492. outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
  493. outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
  494. outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
  495. outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
  496. return ret;
  497. }
  498. /*
  499. * This routine is called to set the UART divisor registers to match
  500. * the specified baud rate for a serial port.
  501. */
  502. static int mxser_change_speed(struct mxser_port *info,
  503. struct termios *old_termios)
  504. {
  505. unsigned cflag, cval, fcr;
  506. int ret = 0;
  507. unsigned char status;
  508. long baud;
  509. unsigned long flags;
  510. if (!info->tty || !info->tty->termios)
  511. return ret;
  512. cflag = info->tty->termios->c_cflag;
  513. if (!(info->ioaddr))
  514. return ret;
  515. if (mxser_set_baud_method[info->tty->index] == 0) {
  516. baud = tty_get_baud_rate(info->tty);
  517. mxser_set_baud(info, baud);
  518. }
  519. /* byte size and parity */
  520. switch (cflag & CSIZE) {
  521. case CS5:
  522. cval = 0x00;
  523. break;
  524. case CS6:
  525. cval = 0x01;
  526. break;
  527. case CS7:
  528. cval = 0x02;
  529. break;
  530. case CS8:
  531. cval = 0x03;
  532. break;
  533. default:
  534. cval = 0x00;
  535. break; /* too keep GCC shut... */
  536. }
  537. if (cflag & CSTOPB)
  538. cval |= 0x04;
  539. if (cflag & PARENB)
  540. cval |= UART_LCR_PARITY;
  541. if (!(cflag & PARODD))
  542. cval |= UART_LCR_EPAR;
  543. if (cflag & CMSPAR)
  544. cval |= UART_LCR_SPAR;
  545. if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
  546. if (info->board->chip_flag) {
  547. fcr = UART_FCR_ENABLE_FIFO;
  548. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  549. SET_MOXA_MUST_FIFO_VALUE(info);
  550. } else
  551. fcr = 0;
  552. } else {
  553. fcr = UART_FCR_ENABLE_FIFO;
  554. /* following add by Victor Yu. 08-30-2002 */
  555. if (info->board->chip_flag) {
  556. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  557. SET_MOXA_MUST_FIFO_VALUE(info);
  558. } else {
  559. /* above add by Victor Yu. 08-30-2002 */
  560. switch (info->rx_trigger) {
  561. case 1:
  562. fcr |= UART_FCR_TRIGGER_1;
  563. break;
  564. case 4:
  565. fcr |= UART_FCR_TRIGGER_4;
  566. break;
  567. case 8:
  568. fcr |= UART_FCR_TRIGGER_8;
  569. break;
  570. default:
  571. fcr |= UART_FCR_TRIGGER_14;
  572. break;
  573. }
  574. }
  575. }
  576. /* CTS flow control flag and modem status interrupts */
  577. info->IER &= ~UART_IER_MSI;
  578. info->MCR &= ~UART_MCR_AFE;
  579. if (cflag & CRTSCTS) {
  580. info->flags |= ASYNC_CTS_FLOW;
  581. info->IER |= UART_IER_MSI;
  582. if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
  583. info->MCR |= UART_MCR_AFE;
  584. /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
  585. /*
  586. save_flags(flags);
  587. cli();
  588. status = inb(baseaddr + UART_MSR);
  589. restore_flags(flags);
  590. */
  591. /* mxser_check_modem_status(info, status); */
  592. } else {
  593. /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
  594. /* MX_LOCK(&info->slock); */
  595. status = inb(info->ioaddr + UART_MSR);
  596. /* MX_UNLOCK(&info->slock); */
  597. if (info->tty->hw_stopped) {
  598. if (status & UART_MSR_CTS) {
  599. info->tty->hw_stopped = 0;
  600. if (info->type != PORT_16550A &&
  601. !info->board->chip_flag) {
  602. outb(info->IER & ~UART_IER_THRI,
  603. info->ioaddr +
  604. UART_IER);
  605. info->IER |= UART_IER_THRI;
  606. outb(info->IER, info->ioaddr +
  607. UART_IER);
  608. }
  609. set_bit(MXSER_EVENT_TXLOW, &info->event);
  610. schedule_work(&info->tqueue); }
  611. } else {
  612. if (!(status & UART_MSR_CTS)) {
  613. info->tty->hw_stopped = 1;
  614. if ((info->type != PORT_16550A) &&
  615. (!info->board->chip_flag)) {
  616. info->IER &= ~UART_IER_THRI;
  617. outb(info->IER, info->ioaddr +
  618. UART_IER);
  619. }
  620. }
  621. }
  622. }
  623. } else {
  624. info->flags &= ~ASYNC_CTS_FLOW;
  625. }
  626. outb(info->MCR, info->ioaddr + UART_MCR);
  627. if (cflag & CLOCAL) {
  628. info->flags &= ~ASYNC_CHECK_CD;
  629. } else {
  630. info->flags |= ASYNC_CHECK_CD;
  631. info->IER |= UART_IER_MSI;
  632. }
  633. outb(info->IER, info->ioaddr + UART_IER);
  634. /*
  635. * Set up parity check flag
  636. */
  637. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  638. if (I_INPCK(info->tty))
  639. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  640. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  641. info->read_status_mask |= UART_LSR_BI;
  642. info->ignore_status_mask = 0;
  643. if (I_IGNBRK(info->tty)) {
  644. info->ignore_status_mask |= UART_LSR_BI;
  645. info->read_status_mask |= UART_LSR_BI;
  646. /*
  647. * If we're ignore parity and break indicators, ignore
  648. * overruns too. (For real raw support).
  649. */
  650. if (I_IGNPAR(info->tty)) {
  651. info->ignore_status_mask |=
  652. UART_LSR_OE |
  653. UART_LSR_PE |
  654. UART_LSR_FE;
  655. info->read_status_mask |=
  656. UART_LSR_OE |
  657. UART_LSR_PE |
  658. UART_LSR_FE;
  659. }
  660. }
  661. /* following add by Victor Yu. 09-02-2002 */
  662. if (info->board->chip_flag) {
  663. spin_lock_irqsave(&info->slock, flags);
  664. SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
  665. SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
  666. if (I_IXON(info->tty)) {
  667. ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  668. } else {
  669. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  670. }
  671. if (I_IXOFF(info->tty)) {
  672. ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  673. } else {
  674. DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  675. }
  676. /*
  677. if ( I_IXANY(info->tty) ) {
  678. info->MCR |= MOXA_MUST_MCR_XON_ANY;
  679. ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
  680. } else {
  681. info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
  682. DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
  683. }
  684. */
  685. spin_unlock_irqrestore(&info->slock, flags);
  686. }
  687. /* above add by Victor Yu. 09-02-2002 */
  688. outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
  689. outb(cval, info->ioaddr + UART_LCR);
  690. return ret;
  691. }
  692. static void mxser_check_modem_status(struct mxser_port *port, int status)
  693. {
  694. /* update input line counters */
  695. if (status & UART_MSR_TERI)
  696. port->icount.rng++;
  697. if (status & UART_MSR_DDSR)
  698. port->icount.dsr++;
  699. if (status & UART_MSR_DDCD)
  700. port->icount.dcd++;
  701. if (status & UART_MSR_DCTS)
  702. port->icount.cts++;
  703. port->mon_data.modem_status = status;
  704. wake_up_interruptible(&port->delta_msr_wait);
  705. if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
  706. if (status & UART_MSR_DCD)
  707. wake_up_interruptible(&port->open_wait);
  708. schedule_work(&port->tqueue);
  709. }
  710. if (port->flags & ASYNC_CTS_FLOW) {
  711. if (port->tty->hw_stopped) {
  712. if (status & UART_MSR_CTS) {
  713. port->tty->hw_stopped = 0;
  714. if ((port->type != PORT_16550A) &&
  715. (!port->board->chip_flag)) {
  716. outb(port->IER & ~UART_IER_THRI,
  717. port->ioaddr + UART_IER);
  718. port->IER |= UART_IER_THRI;
  719. outb(port->IER, port->ioaddr +
  720. UART_IER);
  721. }
  722. set_bit(MXSER_EVENT_TXLOW, &port->event);
  723. schedule_work(&port->tqueue);
  724. }
  725. } else {
  726. if (!(status & UART_MSR_CTS)) {
  727. port->tty->hw_stopped = 1;
  728. if (port->type != PORT_16550A &&
  729. !port->board->chip_flag) {
  730. port->IER &= ~UART_IER_THRI;
  731. outb(port->IER, port->ioaddr +
  732. UART_IER);
  733. }
  734. }
  735. }
  736. }
  737. }
  738. static int mxser_startup(struct mxser_port *info)
  739. {
  740. unsigned long page;
  741. unsigned long flags;
  742. page = __get_free_page(GFP_KERNEL);
  743. if (!page)
  744. return -ENOMEM;
  745. spin_lock_irqsave(&info->slock, flags);
  746. if (info->flags & ASYNC_INITIALIZED) {
  747. free_page(page);
  748. spin_unlock_irqrestore(&info->slock, flags);
  749. return 0;
  750. }
  751. if (!info->ioaddr || !info->type) {
  752. if (info->tty)
  753. set_bit(TTY_IO_ERROR, &info->tty->flags);
  754. free_page(page);
  755. spin_unlock_irqrestore(&info->slock, flags);
  756. return 0;
  757. }
  758. if (info->xmit_buf)
  759. free_page(page);
  760. else
  761. info->xmit_buf = (unsigned char *) page;
  762. /*
  763. * Clear the FIFO buffers and disable them
  764. * (they will be reenabled in mxser_change_speed())
  765. */
  766. if (info->board->chip_flag)
  767. outb((UART_FCR_CLEAR_RCVR |
  768. UART_FCR_CLEAR_XMIT |
  769. MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
  770. else
  771. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  772. info->ioaddr + UART_FCR);
  773. /*
  774. * At this point there's no way the LSR could still be 0xFF;
  775. * if it is, then bail out, because there's likely no UART
  776. * here.
  777. */
  778. if (inb(info->ioaddr + UART_LSR) == 0xff) {
  779. spin_unlock_irqrestore(&info->slock, flags);
  780. if (capable(CAP_SYS_ADMIN)) {
  781. if (info->tty)
  782. set_bit(TTY_IO_ERROR, &info->tty->flags);
  783. return 0;
  784. } else
  785. return -ENODEV;
  786. }
  787. /*
  788. * Clear the interrupt registers.
  789. */
  790. (void) inb(info->ioaddr + UART_LSR);
  791. (void) inb(info->ioaddr + UART_RX);
  792. (void) inb(info->ioaddr + UART_IIR);
  793. (void) inb(info->ioaddr + UART_MSR);
  794. /*
  795. * Now, initialize the UART
  796. */
  797. outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
  798. info->MCR = UART_MCR_DTR | UART_MCR_RTS;
  799. outb(info->MCR, info->ioaddr + UART_MCR);
  800. /*
  801. * Finally, enable interrupts
  802. */
  803. info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  804. /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
  805. /* following add by Victor Yu. 08-30-2002 */
  806. if (info->board->chip_flag)
  807. info->IER |= MOXA_MUST_IER_EGDAI;
  808. /* above add by Victor Yu. 08-30-2002 */
  809. outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
  810. /*
  811. * And clear the interrupt registers again for luck.
  812. */
  813. (void) inb(info->ioaddr + UART_LSR);
  814. (void) inb(info->ioaddr + UART_RX);
  815. (void) inb(info->ioaddr + UART_IIR);
  816. (void) inb(info->ioaddr + UART_MSR);
  817. if (info->tty)
  818. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  819. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  820. /*
  821. * and set the speed of the serial port
  822. */
  823. spin_unlock_irqrestore(&info->slock, flags);
  824. mxser_change_speed(info, NULL);
  825. info->flags |= ASYNC_INITIALIZED;
  826. return 0;
  827. }
  828. /*
  829. * This routine will shutdown a serial port; interrupts maybe disabled, and
  830. * DTR is dropped if the hangup on close termio flag is on.
  831. */
  832. static void mxser_shutdown(struct mxser_port *info)
  833. {
  834. unsigned long flags;
  835. if (!(info->flags & ASYNC_INITIALIZED))
  836. return;
  837. spin_lock_irqsave(&info->slock, flags);
  838. /*
  839. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  840. * here so the queue might never be waken up
  841. */
  842. wake_up_interruptible(&info->delta_msr_wait);
  843. /*
  844. * Free the IRQ, if necessary
  845. */
  846. if (info->xmit_buf) {
  847. free_page((unsigned long) info->xmit_buf);
  848. info->xmit_buf = NULL;
  849. }
  850. info->IER = 0;
  851. outb(0x00, info->ioaddr + UART_IER);
  852. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  853. info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
  854. outb(info->MCR, info->ioaddr + UART_MCR);
  855. /* clear Rx/Tx FIFO's */
  856. /* following add by Victor Yu. 08-30-2002 */
  857. if (info->board->chip_flag)
  858. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
  859. MOXA_MUST_FCR_GDA_MODE_ENABLE,
  860. info->ioaddr + UART_FCR);
  861. else
  862. /* above add by Victor Yu. 08-30-2002 */
  863. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  864. info->ioaddr + UART_FCR);
  865. /* read data port to reset things */
  866. (void) inb(info->ioaddr + UART_RX);
  867. if (info->tty)
  868. set_bit(TTY_IO_ERROR, &info->tty->flags);
  869. info->flags &= ~ASYNC_INITIALIZED;
  870. /* following add by Victor Yu. 09-23-2002 */
  871. if (info->board->chip_flag)
  872. SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  873. /* above add by Victor Yu. 09-23-2002 */
  874. spin_unlock_irqrestore(&info->slock, flags);
  875. }
  876. /*
  877. * This routine is called whenever a serial port is opened. It
  878. * enables interrupts for a serial port, linking in its async structure into
  879. * the IRQ chain. It also performs the serial-specific
  880. * initialization for the tty structure.
  881. */
  882. static int mxser_open(struct tty_struct *tty, struct file *filp)
  883. {
  884. struct mxser_port *info;
  885. int retval, line;
  886. /* initialize driver_data in case something fails */
  887. tty->driver_data = NULL;
  888. line = tty->index;
  889. if (line == MXSER_PORTS)
  890. return 0;
  891. if (line < 0 || line > MXSER_PORTS)
  892. return -ENODEV;
  893. info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
  894. if (!info->ioaddr)
  895. return -ENODEV;
  896. tty->driver_data = info;
  897. info->tty = tty;
  898. /*
  899. * Start up serial port
  900. */
  901. info->count++;
  902. retval = mxser_startup(info);
  903. if (retval)
  904. return retval;
  905. retval = mxser_block_til_ready(tty, filp, info);
  906. if (retval)
  907. return retval;
  908. if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
  909. if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
  910. *tty->termios = info->normal_termios;
  911. else
  912. *tty->termios = info->callout_termios;
  913. mxser_change_speed(info, NULL);
  914. }
  915. info->session = process_session(current);
  916. info->pgrp = process_group(current);
  917. /*
  918. status = mxser_get_msr(info->base, 0, info->port);
  919. mxser_check_modem_status(info, status);
  920. */
  921. /* unmark here for very high baud rate (ex. 921600 bps) used */
  922. tty->low_latency = 1;
  923. return 0;
  924. }
  925. /*
  926. * This routine is called when the serial port gets closed. First, we
  927. * wait for the last remaining data to be sent. Then, we unlink its
  928. * async structure from the interrupt chain if necessary, and we free
  929. * that IRQ if nothing is left in the chain.
  930. */
  931. static void mxser_close(struct tty_struct *tty, struct file *filp)
  932. {
  933. struct mxser_port *info = tty->driver_data;
  934. unsigned long timeout;
  935. unsigned long flags;
  936. if (tty->index == MXSER_PORTS)
  937. return;
  938. if (!info)
  939. return;
  940. spin_lock_irqsave(&info->slock, flags);
  941. if (tty_hung_up_p(filp)) {
  942. spin_unlock_irqrestore(&info->slock, flags);
  943. return;
  944. }
  945. if ((tty->count == 1) && (info->count != 1)) {
  946. /*
  947. * Uh, oh. tty->count is 1, which means that the tty
  948. * structure will be freed. Info->count should always
  949. * be one in these conditions. If it's greater than
  950. * one, we've got real problems, since it means the
  951. * serial port won't be shutdown.
  952. */
  953. printk(KERN_ERR "mxser_close: bad serial port count; "
  954. "tty->count is 1, info->count is %d\n", info->count);
  955. info->count = 1;
  956. }
  957. if (--info->count < 0) {
  958. printk(KERN_ERR "mxser_close: bad serial port count for "
  959. "ttys%d: %d\n", tty->index, info->count);
  960. info->count = 0;
  961. }
  962. if (info->count) {
  963. spin_unlock_irqrestore(&info->slock, flags);
  964. return;
  965. }
  966. info->flags |= ASYNC_CLOSING;
  967. spin_unlock_irqrestore(&info->slock, flags);
  968. /*
  969. * Save the termios structure, since this port may have
  970. * separate termios for callout and dialin.
  971. */
  972. if (info->flags & ASYNC_NORMAL_ACTIVE)
  973. info->normal_termios = *tty->termios;
  974. /*
  975. * Now we wait for the transmit buffer to clear; and we notify
  976. * the line discipline to only process XON/XOFF characters.
  977. */
  978. tty->closing = 1;
  979. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  980. tty_wait_until_sent(tty, info->closing_wait);
  981. /*
  982. * At this point we stop accepting input. To do this, we
  983. * disable the receive line status interrupts, and tell the
  984. * interrupt driver to stop checking the data ready bit in the
  985. * line status register.
  986. */
  987. info->IER &= ~UART_IER_RLSI;
  988. if (info->board->chip_flag)
  989. info->IER &= ~MOXA_MUST_RECV_ISR;
  990. /* by William
  991. info->read_status_mask &= ~UART_LSR_DR;
  992. */
  993. if (info->flags & ASYNC_INITIALIZED) {
  994. outb(info->IER, info->ioaddr + UART_IER);
  995. /*
  996. * Before we drop DTR, make sure the UART transmitter
  997. * has completely drained; this is especially
  998. * important if there is a transmit FIFO!
  999. */
  1000. timeout = jiffies + HZ;
  1001. while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
  1002. schedule_timeout_interruptible(5);
  1003. if (time_after(jiffies, timeout))
  1004. break;
  1005. }
  1006. }
  1007. mxser_shutdown(info);
  1008. if (tty->driver->flush_buffer)
  1009. tty->driver->flush_buffer(tty);
  1010. tty_ldisc_flush(tty);
  1011. tty->closing = 0;
  1012. info->event = 0;
  1013. info->tty = NULL;
  1014. if (info->blocked_open) {
  1015. if (info->close_delay)
  1016. schedule_timeout_interruptible(info->close_delay);
  1017. wake_up_interruptible(&info->open_wait);
  1018. }
  1019. info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  1020. wake_up_interruptible(&info->close_wait);
  1021. }
  1022. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1023. {
  1024. int c, total = 0;
  1025. struct mxser_port *info = tty->driver_data;
  1026. unsigned long flags;
  1027. if (!info->xmit_buf)
  1028. return 0;
  1029. while (1) {
  1030. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  1031. SERIAL_XMIT_SIZE - info->xmit_head));
  1032. if (c <= 0)
  1033. break;
  1034. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  1035. spin_lock_irqsave(&info->slock, flags);
  1036. info->xmit_head = (info->xmit_head + c) &
  1037. (SERIAL_XMIT_SIZE - 1);
  1038. info->xmit_cnt += c;
  1039. spin_unlock_irqrestore(&info->slock, flags);
  1040. buf += c;
  1041. count -= c;
  1042. total += c;
  1043. }
  1044. if (info->xmit_cnt && !tty->stopped
  1045. /*&& !(info->IER & UART_IER_THRI)*/) {
  1046. if (!tty->hw_stopped ||
  1047. (info->type == PORT_16550A) ||
  1048. (info->board->chip_flag)) {
  1049. spin_lock_irqsave(&info->slock, flags);
  1050. outb(info->IER & ~UART_IER_THRI, info->ioaddr +
  1051. UART_IER);
  1052. info->IER |= UART_IER_THRI;
  1053. outb(info->IER, info->ioaddr + UART_IER);
  1054. spin_unlock_irqrestore(&info->slock, flags);
  1055. }
  1056. }
  1057. return total;
  1058. }
  1059. static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
  1060. {
  1061. struct mxser_port *info = tty->driver_data;
  1062. unsigned long flags;
  1063. if (!info->xmit_buf)
  1064. return;
  1065. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  1066. return;
  1067. spin_lock_irqsave(&info->slock, flags);
  1068. info->xmit_buf[info->xmit_head++] = ch;
  1069. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  1070. info->xmit_cnt++;
  1071. spin_unlock_irqrestore(&info->slock, flags);
  1072. if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
  1073. if (!tty->hw_stopped ||
  1074. (info->type == PORT_16550A) ||
  1075. info->board->chip_flag) {
  1076. spin_lock_irqsave(&info->slock, flags);
  1077. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1078. info->IER |= UART_IER_THRI;
  1079. outb(info->IER, info->ioaddr + UART_IER);
  1080. spin_unlock_irqrestore(&info->slock, flags);
  1081. }
  1082. }
  1083. }
  1084. static void mxser_flush_chars(struct tty_struct *tty)
  1085. {
  1086. struct mxser_port *info = tty->driver_data;
  1087. unsigned long flags;
  1088. if (info->xmit_cnt <= 0 ||
  1089. tty->stopped ||
  1090. !info->xmit_buf ||
  1091. (tty->hw_stopped &&
  1092. (info->type != PORT_16550A) &&
  1093. (!info->board->chip_flag)
  1094. ))
  1095. return;
  1096. spin_lock_irqsave(&info->slock, flags);
  1097. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1098. info->IER |= UART_IER_THRI;
  1099. outb(info->IER, info->ioaddr + UART_IER);
  1100. spin_unlock_irqrestore(&info->slock, flags);
  1101. }
  1102. static int mxser_write_room(struct tty_struct *tty)
  1103. {
  1104. struct mxser_port *info = tty->driver_data;
  1105. int ret;
  1106. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1107. if (ret < 0)
  1108. ret = 0;
  1109. return ret;
  1110. }
  1111. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1112. {
  1113. struct mxser_port *info = tty->driver_data;
  1114. int len = info->xmit_cnt;
  1115. if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
  1116. len++;
  1117. return len;
  1118. }
  1119. static void mxser_flush_buffer(struct tty_struct *tty)
  1120. {
  1121. struct mxser_port *info = tty->driver_data;
  1122. char fcr;
  1123. unsigned long flags;
  1124. spin_lock_irqsave(&info->slock, flags);
  1125. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1126. /* below added by shinhay */
  1127. fcr = inb(info->ioaddr + UART_FCR);
  1128. outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  1129. info->ioaddr + UART_FCR);
  1130. outb(fcr, info->ioaddr + UART_FCR);
  1131. spin_unlock_irqrestore(&info->slock, flags);
  1132. /* above added by shinhay */
  1133. tty_wakeup(tty);
  1134. }
  1135. /*
  1136. * ------------------------------------------------------------
  1137. * friends of mxser_ioctl()
  1138. * ------------------------------------------------------------
  1139. */
  1140. static int mxser_get_serial_info(struct mxser_port *info,
  1141. struct serial_struct __user *retinfo)
  1142. {
  1143. struct serial_struct tmp;
  1144. if (!retinfo)
  1145. return -EFAULT;
  1146. memset(&tmp, 0, sizeof(tmp));
  1147. tmp.type = info->type;
  1148. tmp.line = info->tty->index;
  1149. tmp.port = info->ioaddr;
  1150. tmp.irq = info->board->irq;
  1151. tmp.flags = info->flags;
  1152. tmp.baud_base = info->baud_base;
  1153. tmp.close_delay = info->close_delay;
  1154. tmp.closing_wait = info->closing_wait;
  1155. tmp.custom_divisor = info->custom_divisor;
  1156. tmp.hub6 = 0;
  1157. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1158. return -EFAULT;
  1159. return 0;
  1160. }
  1161. static int mxser_set_serial_info(struct mxser_port *info,
  1162. struct serial_struct __user *new_info)
  1163. {
  1164. struct serial_struct new_serial;
  1165. unsigned int flags;
  1166. int retval = 0;
  1167. if (!new_info || !info->ioaddr)
  1168. return -EFAULT;
  1169. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1170. return -EFAULT;
  1171. if ((new_serial.irq != info->board->irq) ||
  1172. (new_serial.port != info->ioaddr) ||
  1173. (new_serial.custom_divisor != info->custom_divisor) ||
  1174. (new_serial.baud_base != info->baud_base))
  1175. return -EPERM;
  1176. flags = info->flags & ASYNC_SPD_MASK;
  1177. if (!capable(CAP_SYS_ADMIN)) {
  1178. if ((new_serial.baud_base != info->baud_base) ||
  1179. (new_serial.close_delay != info->close_delay) ||
  1180. ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
  1181. return -EPERM;
  1182. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  1183. (new_serial.flags & ASYNC_USR_MASK));
  1184. } else {
  1185. /*
  1186. * OK, past this point, all the error checking has been done.
  1187. * At this point, we start making changes.....
  1188. */
  1189. info->flags = ((info->flags & ~ASYNC_FLAGS) |
  1190. (new_serial.flags & ASYNC_FLAGS));
  1191. info->close_delay = new_serial.close_delay * HZ / 100;
  1192. info->closing_wait = new_serial.closing_wait * HZ / 100;
  1193. info->tty->low_latency =
  1194. (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1195. info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
  1196. }
  1197. /* added by casper, 3/17/2000, for mouse */
  1198. info->type = new_serial.type;
  1199. process_txrx_fifo(info);
  1200. if (info->flags & ASYNC_INITIALIZED) {
  1201. if (flags != (info->flags & ASYNC_SPD_MASK))
  1202. mxser_change_speed(info, NULL);
  1203. } else
  1204. retval = mxser_startup(info);
  1205. return retval;
  1206. }
  1207. /*
  1208. * mxser_get_lsr_info - get line status register info
  1209. *
  1210. * Purpose: Let user call ioctl() to get info when the UART physically
  1211. * is emptied. On bus types like RS485, the transmitter must
  1212. * release the bus after transmitting. This must be done when
  1213. * the transmit shift register is empty, not be done when the
  1214. * transmit holding register is empty. This functionality
  1215. * allows an RS485 driver to be written in user space.
  1216. */
  1217. static int mxser_get_lsr_info(struct mxser_port *info,
  1218. unsigned int __user *value)
  1219. {
  1220. unsigned char status;
  1221. unsigned int result;
  1222. unsigned long flags;
  1223. spin_lock_irqsave(&info->slock, flags);
  1224. status = inb(info->ioaddr + UART_LSR);
  1225. spin_unlock_irqrestore(&info->slock, flags);
  1226. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1227. return put_user(result, value);
  1228. }
  1229. /*
  1230. * This routine sends a break character out the serial port.
  1231. */
  1232. static void mxser_send_break(struct mxser_port *info, int duration)
  1233. {
  1234. unsigned long flags;
  1235. if (!info->ioaddr)
  1236. return;
  1237. set_current_state(TASK_INTERRUPTIBLE);
  1238. spin_lock_irqsave(&info->slock, flags);
  1239. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1240. info->ioaddr + UART_LCR);
  1241. spin_unlock_irqrestore(&info->slock, flags);
  1242. schedule_timeout(duration);
  1243. spin_lock_irqsave(&info->slock, flags);
  1244. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1245. info->ioaddr + UART_LCR);
  1246. spin_unlock_irqrestore(&info->slock, flags);
  1247. }
  1248. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  1249. {
  1250. struct mxser_port *info = tty->driver_data;
  1251. unsigned char control, status;
  1252. unsigned long flags;
  1253. if (tty->index == MXSER_PORTS)
  1254. return -ENOIOCTLCMD;
  1255. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1256. return -EIO;
  1257. control = info->MCR;
  1258. spin_lock_irqsave(&info->slock, flags);
  1259. status = inb(info->ioaddr + UART_MSR);
  1260. if (status & UART_MSR_ANY_DELTA)
  1261. mxser_check_modem_status(info, status);
  1262. spin_unlock_irqrestore(&info->slock, flags);
  1263. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  1264. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  1265. ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  1266. ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
  1267. ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  1268. ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1269. }
  1270. static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
  1271. unsigned int set, unsigned int clear)
  1272. {
  1273. struct mxser_port *info = tty->driver_data;
  1274. unsigned long flags;
  1275. if (tty->index == MXSER_PORTS)
  1276. return -ENOIOCTLCMD;
  1277. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1278. return -EIO;
  1279. spin_lock_irqsave(&info->slock, flags);
  1280. if (set & TIOCM_RTS)
  1281. info->MCR |= UART_MCR_RTS;
  1282. if (set & TIOCM_DTR)
  1283. info->MCR |= UART_MCR_DTR;
  1284. if (clear & TIOCM_RTS)
  1285. info->MCR &= ~UART_MCR_RTS;
  1286. if (clear & TIOCM_DTR)
  1287. info->MCR &= ~UART_MCR_DTR;
  1288. outb(info->MCR, info->ioaddr + UART_MCR);
  1289. spin_unlock_irqrestore(&info->slock, flags);
  1290. return 0;
  1291. }
  1292. static int mxser_program_mode(int port)
  1293. {
  1294. int id, i, j, n;
  1295. /* unsigned long flags; */
  1296. spin_lock(&gm_lock);
  1297. outb(0, port);
  1298. outb(0, port);
  1299. outb(0, port);
  1300. (void)inb(port);
  1301. (void)inb(port);
  1302. outb(0, port);
  1303. (void)inb(port);
  1304. /* restore_flags(flags); */
  1305. spin_unlock(&gm_lock);
  1306. id = inb(port + 1) & 0x1F;
  1307. if ((id != C168_ASIC_ID) &&
  1308. (id != C104_ASIC_ID) &&
  1309. (id != C102_ASIC_ID) &&
  1310. (id != CI132_ASIC_ID) &&
  1311. (id != CI134_ASIC_ID) &&
  1312. (id != CI104J_ASIC_ID))
  1313. return -1;
  1314. for (i = 0, j = 0; i < 4; i++) {
  1315. n = inb(port + 2);
  1316. if (n == 'M') {
  1317. j = 1;
  1318. } else if ((j == 1) && (n == 1)) {
  1319. j = 2;
  1320. break;
  1321. } else
  1322. j = 0;
  1323. }
  1324. if (j != 2)
  1325. id = -2;
  1326. return id;
  1327. }
  1328. static void mxser_normal_mode(int port)
  1329. {
  1330. int i, n;
  1331. outb(0xA5, port + 1);
  1332. outb(0x80, port + 3);
  1333. outb(12, port + 0); /* 9600 bps */
  1334. outb(0, port + 1);
  1335. outb(0x03, port + 3); /* 8 data bits */
  1336. outb(0x13, port + 4); /* loop back mode */
  1337. for (i = 0; i < 16; i++) {
  1338. n = inb(port + 5);
  1339. if ((n & 0x61) == 0x60)
  1340. break;
  1341. if ((n & 1) == 1)
  1342. (void)inb(port);
  1343. }
  1344. outb(0x00, port + 4);
  1345. }
  1346. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  1347. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  1348. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  1349. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  1350. #define EN_CCMD 0x000 /* Chip's command register */
  1351. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  1352. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  1353. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  1354. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  1355. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  1356. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  1357. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  1358. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  1359. static int mxser_read_register(int port, unsigned short *regs)
  1360. {
  1361. int i, k, value, id;
  1362. unsigned int j;
  1363. id = mxser_program_mode(port);
  1364. if (id < 0)
  1365. return id;
  1366. for (i = 0; i < 14; i++) {
  1367. k = (i & 0x3F) | 0x180;
  1368. for (j = 0x100; j > 0; j >>= 1) {
  1369. outb(CHIP_CS, port);
  1370. if (k & j) {
  1371. outb(CHIP_CS | CHIP_DO, port);
  1372. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  1373. } else {
  1374. outb(CHIP_CS, port);
  1375. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  1376. }
  1377. }
  1378. (void)inb(port);
  1379. value = 0;
  1380. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  1381. outb(CHIP_CS, port);
  1382. outb(CHIP_CS | CHIP_SK, port);
  1383. if (inb(port) & CHIP_DI)
  1384. value |= j;
  1385. }
  1386. regs[i] = value;
  1387. outb(0, port);
  1388. }
  1389. mxser_normal_mode(port);
  1390. return id;
  1391. }
  1392. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1393. {
  1394. struct mxser_port *port;
  1395. int result, status;
  1396. unsigned int i, j;
  1397. switch (cmd) {
  1398. case MOXA_GET_CONF:
  1399. /* if (copy_to_user(argp, mxsercfg,
  1400. sizeof(struct mxser_hwconf) * 4))
  1401. return -EFAULT;
  1402. return 0;*/
  1403. return -ENXIO;
  1404. case MOXA_GET_MAJOR:
  1405. if (copy_to_user(argp, &ttymajor, sizeof(int)))
  1406. return -EFAULT;
  1407. return 0;
  1408. case MOXA_GET_CUMAJOR:
  1409. if (copy_to_user(argp, &calloutmajor, sizeof(int)))
  1410. return -EFAULT;
  1411. return 0;
  1412. case MOXA_CHKPORTENABLE:
  1413. result = 0;
  1414. for (i = 0; i < MXSER_BOARDS; i++)
  1415. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
  1416. if (mxser_boards[i].ports[j].ioaddr)
  1417. result |= (1 << i);
  1418. return put_user(result, (unsigned long __user *)argp);
  1419. case MOXA_GETDATACOUNT:
  1420. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1421. return -EFAULT;
  1422. return 0;
  1423. case MOXA_GETMSTATUS:
  1424. for (i = 0; i < MXSER_BOARDS; i++)
  1425. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1426. port = &mxser_boards[i].ports[j];
  1427. GMStatus[i].ri = 0;
  1428. if (!port->ioaddr) {
  1429. GMStatus[i].dcd = 0;
  1430. GMStatus[i].dsr = 0;
  1431. GMStatus[i].cts = 0;
  1432. continue;
  1433. }
  1434. if (!port->tty || !port->tty->termios)
  1435. GMStatus[i].cflag =
  1436. port->normal_termios.c_cflag;
  1437. else
  1438. GMStatus[i].cflag =
  1439. port->tty->termios->c_cflag;
  1440. status = inb(port->ioaddr + UART_MSR);
  1441. if (status & 0x80 /*UART_MSR_DCD */ )
  1442. GMStatus[i].dcd = 1;
  1443. else
  1444. GMStatus[i].dcd = 0;
  1445. if (status & 0x20 /*UART_MSR_DSR */ )
  1446. GMStatus[i].dsr = 1;
  1447. else
  1448. GMStatus[i].dsr = 0;
  1449. if (status & 0x10 /*UART_MSR_CTS */ )
  1450. GMStatus[i].cts = 1;
  1451. else
  1452. GMStatus[i].cts = 0;
  1453. }
  1454. if (copy_to_user(argp, GMStatus,
  1455. sizeof(struct mxser_mstatus) * MXSER_PORTS))
  1456. return -EFAULT;
  1457. return 0;
  1458. case MOXA_ASPP_MON_EXT: {
  1459. int status, p, shiftbit;
  1460. unsigned long opmode;
  1461. unsigned cflag, iflag;
  1462. for (i = 0; i < MXSER_BOARDS; i++)
  1463. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1464. port = &mxser_boards[i].ports[j];
  1465. if (!port->ioaddr)
  1466. continue;
  1467. status = mxser_get_msr(port->ioaddr, 0, i);
  1468. /* mxser_check_modem_status(port, status); */
  1469. if (status & UART_MSR_TERI)
  1470. port->icount.rng++;
  1471. if (status & UART_MSR_DDSR)
  1472. port->icount.dsr++;
  1473. if (status & UART_MSR_DDCD)
  1474. port->icount.dcd++;
  1475. if (status & UART_MSR_DCTS)
  1476. port->icount.cts++;
  1477. port->mon_data.modem_status = status;
  1478. mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
  1479. mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
  1480. mon_data_ext.up_rxcnt[i] =
  1481. port->mon_data.up_rxcnt;
  1482. mon_data_ext.up_txcnt[i] =
  1483. port->mon_data.up_txcnt;
  1484. mon_data_ext.modem_status[i] =
  1485. port->mon_data.modem_status;
  1486. mon_data_ext.baudrate[i] = port->realbaud;
  1487. if (!port->tty || !port->tty->termios) {
  1488. cflag = port->normal_termios.c_cflag;
  1489. iflag = port->normal_termios.c_iflag;
  1490. } else {
  1491. cflag = port->tty->termios->c_cflag;
  1492. iflag = port->tty->termios->c_iflag;
  1493. }
  1494. mon_data_ext.databits[i] = cflag & CSIZE;
  1495. mon_data_ext.stopbits[i] = cflag & CSTOPB;
  1496. mon_data_ext.parity[i] =
  1497. cflag & (PARENB | PARODD | CMSPAR);
  1498. mon_data_ext.flowctrl[i] = 0x00;
  1499. if (cflag & CRTSCTS)
  1500. mon_data_ext.flowctrl[i] |= 0x03;
  1501. if (iflag & (IXON | IXOFF))
  1502. mon_data_ext.flowctrl[i] |= 0x0C;
  1503. if (port->type == PORT_16550A)
  1504. mon_data_ext.fifo[i] = 1;
  1505. else
  1506. mon_data_ext.fifo[i] = 0;
  1507. p = i % 4;
  1508. shiftbit = p * 2;
  1509. opmode = inb(port->opmode_ioaddr) >> shiftbit;
  1510. opmode &= OP_MODE_MASK;
  1511. mon_data_ext.iftype[i] = opmode;
  1512. }
  1513. if (copy_to_user(argp, &mon_data_ext,
  1514. sizeof(mon_data_ext)))
  1515. return -EFAULT;
  1516. return 0;
  1517. } default:
  1518. return -ENOIOCTLCMD;
  1519. }
  1520. return 0;
  1521. }
  1522. static int mxser_ioctl(struct tty_struct *tty, struct file *file,
  1523. unsigned int cmd, unsigned long arg)
  1524. {
  1525. struct mxser_port *info = tty->driver_data;
  1526. struct async_icount cprev, cnow; /* kernel counter temps */
  1527. struct serial_icounter_struct __user *p_cuser;
  1528. unsigned long templ;
  1529. unsigned long flags;
  1530. void __user *argp = (void __user *)arg;
  1531. int retval;
  1532. if (tty->index == MXSER_PORTS)
  1533. return mxser_ioctl_special(cmd, argp);
  1534. /* following add by Victor Yu. 01-05-2004 */
  1535. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1536. int p;
  1537. unsigned long opmode;
  1538. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1539. int shiftbit;
  1540. unsigned char val, mask;
  1541. p = tty->index % 4;
  1542. if (cmd == MOXA_SET_OP_MODE) {
  1543. if (get_user(opmode, (int __user *) argp))
  1544. return -EFAULT;
  1545. if (opmode != RS232_MODE &&
  1546. opmode != RS485_2WIRE_MODE &&
  1547. opmode != RS422_MODE &&
  1548. opmode != RS485_4WIRE_MODE)
  1549. return -EFAULT;
  1550. mask = ModeMask[p];
  1551. shiftbit = p * 2;
  1552. val = inb(info->opmode_ioaddr);
  1553. val &= mask;
  1554. val |= (opmode << shiftbit);
  1555. outb(val, info->opmode_ioaddr);
  1556. } else {
  1557. shiftbit = p * 2;
  1558. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1559. opmode &= OP_MODE_MASK;
  1560. if (copy_to_user(argp, &opmode, sizeof(int)))
  1561. return -EFAULT;
  1562. }
  1563. return 0;
  1564. }
  1565. /* above add by Victor Yu. 01-05-2004 */
  1566. if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
  1567. test_bit(TTY_IO_ERROR, &tty->flags))
  1568. return -EIO;
  1569. switch (cmd) {
  1570. case TCSBRK: /* SVID version: non-zero arg --> no break */
  1571. retval = tty_check_change(tty);
  1572. if (retval)
  1573. return retval;
  1574. tty_wait_until_sent(tty, 0);
  1575. if (!arg)
  1576. mxser_send_break(info, HZ / 4); /* 1/4 second */
  1577. return 0;
  1578. case TCSBRKP: /* support for POSIX tcsendbreak() */
  1579. retval = tty_check_change(tty);
  1580. if (retval)
  1581. return retval;
  1582. tty_wait_until_sent(tty, 0);
  1583. mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
  1584. return 0;
  1585. case TIOCGSOFTCAR:
  1586. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
  1587. case TIOCSSOFTCAR:
  1588. if (get_user(templ, (unsigned long __user *) argp))
  1589. return -EFAULT;
  1590. arg = templ;
  1591. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
  1592. return 0;
  1593. case TIOCGSERIAL:
  1594. return mxser_get_serial_info(info, argp);
  1595. case TIOCSSERIAL:
  1596. return mxser_set_serial_info(info, argp);
  1597. case TIOCSERGETLSR: /* Get line status register */
  1598. return mxser_get_lsr_info(info, argp);
  1599. /*
  1600. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1601. * - mask passed in arg for lines of interest
  1602. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1603. * Caller should use TIOCGICOUNT to see which one it was
  1604. */
  1605. case TIOCMIWAIT: {
  1606. DECLARE_WAITQUEUE(wait, current);
  1607. int ret;
  1608. spin_lock_irqsave(&info->slock, flags);
  1609. cprev = info->icount; /* note the counters on entry */
  1610. spin_unlock_irqrestore(&info->slock, flags);
  1611. add_wait_queue(&info->delta_msr_wait, &wait);
  1612. while (1) {
  1613. spin_lock_irqsave(&info->slock, flags);
  1614. cnow = info->icount; /* atomic copy */
  1615. spin_unlock_irqrestore(&info->slock, flags);
  1616. set_current_state(TASK_INTERRUPTIBLE);
  1617. if (((arg & TIOCM_RNG) &&
  1618. (cnow.rng != cprev.rng)) ||
  1619. ((arg & TIOCM_DSR) &&
  1620. (cnow.dsr != cprev.dsr)) ||
  1621. ((arg & TIOCM_CD) &&
  1622. (cnow.dcd != cprev.dcd)) ||
  1623. ((arg & TIOCM_CTS) &&
  1624. (cnow.cts != cprev.cts))) {
  1625. ret = 0;
  1626. break;
  1627. }
  1628. /* see if a signal did it */
  1629. if (signal_pending(current)) {
  1630. ret = -ERESTARTSYS;
  1631. break;
  1632. }
  1633. cprev = cnow;
  1634. }
  1635. current->state = TASK_RUNNING;
  1636. remove_wait_queue(&info->delta_msr_wait, &wait);
  1637. break;
  1638. }
  1639. /* NOTREACHED */
  1640. /*
  1641. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1642. * Return: write counters to the user passed counter struct
  1643. * NB: both 1->0 and 0->1 transitions are counted except for
  1644. * RI where only 0->1 is counted.
  1645. */
  1646. case TIOCGICOUNT:
  1647. spin_lock_irqsave(&info->slock, flags);
  1648. cnow = info->icount;
  1649. spin_unlock_irqrestore(&info->slock, flags);
  1650. p_cuser = argp;
  1651. /* modified by casper 1/11/2000 */
  1652. if (put_user(cnow.frame, &p_cuser->frame))
  1653. return -EFAULT;
  1654. if (put_user(cnow.brk, &p_cuser->brk))
  1655. return -EFAULT;
  1656. if (put_user(cnow.overrun, &p_cuser->overrun))
  1657. return -EFAULT;
  1658. if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  1659. return -EFAULT;
  1660. if (put_user(cnow.parity, &p_cuser->parity))
  1661. return -EFAULT;
  1662. if (put_user(cnow.rx, &p_cuser->rx))
  1663. return -EFAULT;
  1664. if (put_user(cnow.tx, &p_cuser->tx))
  1665. return -EFAULT;
  1666. put_user(cnow.cts, &p_cuser->cts);
  1667. put_user(cnow.dsr, &p_cuser->dsr);
  1668. put_user(cnow.rng, &p_cuser->rng);
  1669. put_user(cnow.dcd, &p_cuser->dcd);
  1670. return 0;
  1671. case MOXA_HighSpeedOn:
  1672. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
  1673. case MOXA_SDS_RSTICOUNTER:
  1674. info->mon_data.rxcnt = 0;
  1675. info->mon_data.txcnt = 0;
  1676. return 0;
  1677. /* (above) added by James. */
  1678. case MOXA_ASPP_SETBAUD:{
  1679. long baud;
  1680. if (get_user(baud, (long __user *)argp))
  1681. return -EFAULT;
  1682. mxser_set_baud(info, baud);
  1683. return 0;
  1684. }
  1685. case MOXA_ASPP_GETBAUD:
  1686. if (copy_to_user(argp, &info->realbaud, sizeof(long)))
  1687. return -EFAULT;
  1688. return 0;
  1689. case MOXA_ASPP_OQUEUE:{
  1690. int len, lsr;
  1691. len = mxser_chars_in_buffer(tty);
  1692. lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
  1693. len += (lsr ? 0 : 1);
  1694. if (copy_to_user(argp, &len, sizeof(int)))
  1695. return -EFAULT;
  1696. return 0;
  1697. }
  1698. case MOXA_ASPP_MON: {
  1699. int mcr, status;
  1700. /* info->mon_data.ser_param = tty->termios->c_cflag; */
  1701. status = mxser_get_msr(info->ioaddr, 1, tty->index);
  1702. mxser_check_modem_status(info, status);
  1703. mcr = inb(info->ioaddr + UART_MCR);
  1704. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1705. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1706. else
  1707. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1708. if (mcr & MOXA_MUST_MCR_TX_XON)
  1709. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1710. else
  1711. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1712. if (info->tty->hw_stopped)
  1713. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1714. else
  1715. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1716. if (copy_to_user(argp, &info->mon_data,
  1717. sizeof(struct mxser_mon)))
  1718. return -EFAULT;
  1719. return 0;
  1720. }
  1721. case MOXA_ASPP_LSTATUS: {
  1722. if (copy_to_user(argp, &info->err_shadow,
  1723. sizeof(unsigned char)))
  1724. return -EFAULT;
  1725. info->err_shadow = 0;
  1726. return 0;
  1727. }
  1728. case MOXA_SET_BAUD_METHOD: {
  1729. int method;
  1730. if (get_user(method, (int __user *)argp))
  1731. return -EFAULT;
  1732. mxser_set_baud_method[tty->index] = method;
  1733. if (copy_to_user(argp, &method, sizeof(int)))
  1734. return -EFAULT;
  1735. return 0;
  1736. }
  1737. default:
  1738. return -ENOIOCTLCMD;
  1739. }
  1740. return 0;
  1741. }
  1742. static void mxser_stoprx(struct tty_struct *tty)
  1743. {
  1744. struct mxser_port *info = tty->driver_data;
  1745. info->ldisc_stop_rx = 1;
  1746. if (I_IXOFF(tty)) {
  1747. /* following add by Victor Yu. 09-02-2002 */
  1748. if (info->board->chip_flag) {
  1749. info->IER &= ~MOXA_MUST_RECV_ISR;
  1750. outb(info->IER, info->ioaddr + UART_IER);
  1751. } else if (!(info->flags & ASYNC_CLOSING)) {
  1752. info->x_char = STOP_CHAR(tty);
  1753. outb(0, info->ioaddr + UART_IER);
  1754. info->IER |= UART_IER_THRI;
  1755. outb(info->IER, info->ioaddr + UART_IER);
  1756. }
  1757. }
  1758. if (info->tty->termios->c_cflag & CRTSCTS) {
  1759. info->MCR &= ~UART_MCR_RTS;
  1760. outb(info->MCR, info->ioaddr + UART_MCR);
  1761. }
  1762. }
  1763. /*
  1764. * This routine is called by the upper-layer tty layer to signal that
  1765. * incoming characters should be throttled.
  1766. */
  1767. static void mxser_throttle(struct tty_struct *tty)
  1768. {
  1769. mxser_stoprx(tty);
  1770. }
  1771. static void mxser_unthrottle(struct tty_struct *tty)
  1772. {
  1773. struct mxser_port *info = tty->driver_data;
  1774. /* startrx */
  1775. info->ldisc_stop_rx = 0;
  1776. if (I_IXOFF(tty)) {
  1777. if (info->x_char)
  1778. info->x_char = 0;
  1779. else {
  1780. /* following add by Victor Yu. 09-02-2002 */
  1781. if (info->board->chip_flag) {
  1782. info->IER |= MOXA_MUST_RECV_ISR;
  1783. outb(info->IER, info->ioaddr + UART_IER);
  1784. } else if (!(info->flags & ASYNC_CLOSING)) {
  1785. info->x_char = START_CHAR(tty);
  1786. outb(0, info->ioaddr + UART_IER);
  1787. info->IER |= UART_IER_THRI;
  1788. outb(info->IER, info->ioaddr + UART_IER);
  1789. }
  1790. }
  1791. }
  1792. if (info->tty->termios->c_cflag & CRTSCTS) {
  1793. info->MCR |= UART_MCR_RTS;
  1794. outb(info->MCR, info->ioaddr + UART_MCR);
  1795. }
  1796. }
  1797. /*
  1798. * mxser_stop() and mxser_start()
  1799. *
  1800. * This routines are called before setting or resetting tty->stopped.
  1801. * They enable or disable transmitter interrupts, as necessary.
  1802. */
  1803. static void mxser_stop(struct tty_struct *tty)
  1804. {
  1805. struct mxser_port *info = tty->driver_data;
  1806. unsigned long flags;
  1807. spin_lock_irqsave(&info->slock, flags);
  1808. if (info->IER & UART_IER_THRI) {
  1809. info->IER &= ~UART_IER_THRI;
  1810. outb(info->IER, info->ioaddr + UART_IER);
  1811. }
  1812. spin_unlock_irqrestore(&info->slock, flags);
  1813. }
  1814. static void mxser_start(struct tty_struct *tty)
  1815. {
  1816. struct mxser_port *info = tty->driver_data;
  1817. unsigned long flags;
  1818. spin_lock_irqsave(&info->slock, flags);
  1819. if (info->xmit_cnt && info->xmit_buf
  1820. /* && !(info->IER & UART_IER_THRI) */) {
  1821. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1822. info->IER |= UART_IER_THRI;
  1823. outb(info->IER, info->ioaddr + UART_IER);
  1824. }
  1825. spin_unlock_irqrestore(&info->slock, flags);
  1826. }
  1827. static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1828. {
  1829. struct mxser_port *info = tty->driver_data;
  1830. unsigned long flags;
  1831. if ((tty->termios->c_cflag != old_termios->c_cflag) ||
  1832. (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
  1833. mxser_change_speed(info, old_termios);
  1834. if ((old_termios->c_cflag & CRTSCTS) &&
  1835. !(tty->termios->c_cflag & CRTSCTS)) {
  1836. tty->hw_stopped = 0;
  1837. mxser_start(tty);
  1838. }
  1839. }
  1840. /* Handle sw stopped */
  1841. if ((old_termios->c_iflag & IXON) &&
  1842. !(tty->termios->c_iflag & IXON)) {
  1843. tty->stopped = 0;
  1844. /* following add by Victor Yu. 09-02-2002 */
  1845. if (info->board->chip_flag) {
  1846. spin_lock_irqsave(&info->slock, flags);
  1847. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  1848. spin_unlock_irqrestore(&info->slock, flags);
  1849. }
  1850. /* above add by Victor Yu. 09-02-2002 */
  1851. mxser_start(tty);
  1852. }
  1853. }
  1854. /*
  1855. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1856. */
  1857. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1858. {
  1859. struct mxser_port *info = tty->driver_data;
  1860. unsigned long orig_jiffies, char_time;
  1861. int lsr;
  1862. if (info->type == PORT_UNKNOWN)
  1863. return;
  1864. if (info->xmit_fifo_size == 0)
  1865. return; /* Just in case.... */
  1866. orig_jiffies = jiffies;
  1867. /*
  1868. * Set the check interval to be 1/5 of the estimated time to
  1869. * send a single character, and make it at least 1. The check
  1870. * interval should also be less than the timeout.
  1871. *
  1872. * Note: we have to use pretty tight timings here to satisfy
  1873. * the NIST-PCTS.
  1874. */
  1875. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1876. char_time = char_time / 5;
  1877. if (char_time == 0)
  1878. char_time = 1;
  1879. if (timeout && timeout < char_time)
  1880. char_time = timeout;
  1881. /*
  1882. * If the transmitter hasn't cleared in twice the approximate
  1883. * amount of time to send the entire FIFO, it probably won't
  1884. * ever clear. This assumes the UART isn't doing flow
  1885. * control, which is currently the case. Hence, if it ever
  1886. * takes longer than info->timeout, this is probably due to a
  1887. * UART bug of some kind. So, we clamp the timeout parameter at
  1888. * 2*info->timeout.
  1889. */
  1890. if (!timeout || timeout > 2 * info->timeout)
  1891. timeout = 2 * info->timeout;
  1892. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1893. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
  1894. timeout, char_time);
  1895. printk("jiff=%lu...", jiffies);
  1896. #endif
  1897. while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
  1898. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1899. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1900. #endif
  1901. schedule_timeout_interruptible(char_time);
  1902. if (signal_pending(current))
  1903. break;
  1904. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1905. break;
  1906. }
  1907. set_current_state(TASK_RUNNING);
  1908. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1909. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1910. #endif
  1911. }
  1912. /*
  1913. * This routine is called by tty_hangup() when a hangup is signaled.
  1914. */
  1915. void mxser_hangup(struct tty_struct *tty)
  1916. {
  1917. struct mxser_port *info = tty->driver_data;
  1918. mxser_flush_buffer(tty);
  1919. mxser_shutdown(info);
  1920. info->event = 0;
  1921. info->count = 0;
  1922. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1923. info->tty = NULL;
  1924. wake_up_interruptible(&info->open_wait);
  1925. }
  1926. /* added by James 03-12-2004. */
  1927. /*
  1928. * mxser_rs_break() --- routine which turns the break handling on or off
  1929. */
  1930. static void mxser_rs_break(struct tty_struct *tty, int break_state)
  1931. {
  1932. struct mxser_port *info = tty->driver_data;
  1933. unsigned long flags;
  1934. spin_lock_irqsave(&info->slock, flags);
  1935. if (break_state == -1)
  1936. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1937. info->ioaddr + UART_LCR);
  1938. else
  1939. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1940. info->ioaddr + UART_LCR);
  1941. spin_unlock_irqrestore(&info->slock, flags);
  1942. }
  1943. /* (above) added by James. */
  1944. static void mxser_receive_chars(struct mxser_port *port, int *status)
  1945. {
  1946. struct tty_struct *tty = port->tty;
  1947. unsigned char ch, gdl;
  1948. int ignored = 0;
  1949. int cnt = 0;
  1950. int recv_room;
  1951. int max = 256;
  1952. unsigned long flags;
  1953. spin_lock_irqsave(&port->slock, flags);
  1954. recv_room = tty->receive_room;
  1955. if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
  1956. /* mxser_throttle(tty); */
  1957. mxser_stoprx(tty);
  1958. /* return; */
  1959. }
  1960. /* following add by Victor Yu. 09-02-2002 */
  1961. if (port->board->chip_flag != MOXA_OTHER_UART) {
  1962. if (*status & UART_LSR_SPECIAL)
  1963. goto intr_old;
  1964. /* following add by Victor Yu. 02-11-2004 */
  1965. if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
  1966. (*status & MOXA_MUST_LSR_RERR))
  1967. goto intr_old;
  1968. /* above add by Victor Yu. 02-14-2004 */
  1969. if (*status & MOXA_MUST_LSR_RERR)
  1970. goto intr_old;
  1971. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1972. /* add by Victor Yu. 02-11-2004 */
  1973. if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
  1974. gdl &= MOXA_MUST_GDL_MASK;
  1975. if (gdl >= recv_room) {
  1976. if (!port->ldisc_stop_rx) {
  1977. /* mxser_throttle(tty); */
  1978. mxser_stoprx(tty);
  1979. }
  1980. /* return; */
  1981. }
  1982. while (gdl--) {
  1983. ch = inb(port->ioaddr + UART_RX);
  1984. tty_insert_flip_char(tty, ch, 0);
  1985. cnt++;
  1986. }
  1987. goto end_intr;
  1988. }
  1989. intr_old:
  1990. /* above add by Victor Yu. 09-02-2002 */
  1991. do {
  1992. if (max-- < 0)
  1993. break;
  1994. ch = inb(port->ioaddr + UART_RX);
  1995. /* following add by Victor Yu. 09-02-2002 */
  1996. if (port->board->chip_flag && (*status & UART_LSR_OE)
  1997. /*&& !(*status&UART_LSR_DR) */)
  1998. outb(0x23, port->ioaddr + UART_FCR);
  1999. *status &= port->read_status_mask;
  2000. /* above add by Victor Yu. 09-02-2002 */
  2001. if (*status & port->ignore_status_mask) {
  2002. if (++ignored > 100)
  2003. break;
  2004. } else {
  2005. char flag = 0;
  2006. if (*status & UART_LSR_SPECIAL) {
  2007. if (*status & UART_LSR_BI) {
  2008. flag = TTY_BREAK;
  2009. /* added by casper 1/11/2000 */
  2010. port->icount.brk++;
  2011. if (port->flags & ASYNC_SAK)
  2012. do_SAK(tty);
  2013. } else if (*status & UART_LSR_PE) {
  2014. flag = TTY_PARITY;
  2015. /* added by casper 1/11/2000 */
  2016. port->icount.parity++;
  2017. } else if (*status & UART_LSR_FE) {
  2018. flag = TTY_FRAME;
  2019. /* added by casper 1/11/2000 */
  2020. port->icount.frame++;
  2021. } else if (*status & UART_LSR_OE) {
  2022. flag = TTY_OVERRUN;
  2023. /* added by casper 1/11/2000 */
  2024. port->icount.overrun++;
  2025. } else
  2026. flags = TTY_BREAK;
  2027. } else
  2028. flags = 0;
  2029. tty_insert_flip_char(tty, ch, flag);
  2030. cnt++;
  2031. if (cnt >= recv_room) {
  2032. if (!port->ldisc_stop_rx) {
  2033. /* mxser_throttle(tty); */
  2034. mxser_stoprx(tty);
  2035. }
  2036. break;
  2037. }
  2038. }
  2039. /* following add by Victor Yu. 09-02-2002 */
  2040. if (port->board->chip_flag)
  2041. break;
  2042. /* mask by Victor Yu. 09-02-2002
  2043. *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
  2044. */
  2045. /* following add by Victor Yu. 09-02-2002 */
  2046. *status = inb(port->ioaddr + UART_LSR);
  2047. /* above add by Victor Yu. 09-02-2002 */
  2048. } while (*status & UART_LSR_DR);
  2049. end_intr: /* add by Victor Yu. 09-02-2002 */
  2050. mxvar_log.rxcnt[port->tty->index] += cnt;
  2051. port->mon_data.rxcnt += cnt;
  2052. port->mon_data.up_rxcnt += cnt;
  2053. spin_unlock_irqrestore(&port->slock, flags);
  2054. tty_flip_buffer_push(tty);
  2055. }
  2056. static void mxser_transmit_chars(struct mxser_port *port)
  2057. {
  2058. int count, cnt;
  2059. unsigned long flags;
  2060. spin_lock_irqsave(&port->slock, flags);
  2061. if (port->x_char) {
  2062. outb(port->x_char, port->ioaddr + UART_TX);
  2063. port->x_char = 0;
  2064. mxvar_log.txcnt[port->tty->index]++;
  2065. port->mon_data.txcnt++;
  2066. port->mon_data.up_txcnt++;
  2067. /* added by casper 1/11/2000 */
  2068. port->icount.tx++;
  2069. goto unlock;
  2070. }
  2071. if (port->xmit_buf == 0)
  2072. goto unlock;
  2073. if (port->xmit_cnt == 0) {
  2074. if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
  2075. set_bit(MXSER_EVENT_TXLOW, &port->event);
  2076. schedule_work(&port->tqueue);
  2077. }
  2078. goto unlock;
  2079. }
  2080. if (port->tty->stopped || (port->tty->hw_stopped &&
  2081. (port->type != PORT_16550A) &&
  2082. (!port->board->chip_flag))) {
  2083. port->IER &= ~UART_IER_THRI;
  2084. outb(port->IER, port->ioaddr + UART_IER);
  2085. goto unlock;
  2086. }
  2087. cnt = port->xmit_cnt;
  2088. count = port->xmit_fifo_size;
  2089. do {
  2090. outb(port->xmit_buf[port->xmit_tail++],
  2091. port->ioaddr + UART_TX);
  2092. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  2093. if (--port->xmit_cnt <= 0)
  2094. break;
  2095. } while (--count > 0);
  2096. mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
  2097. /* added by James 03-12-2004. */
  2098. port->mon_data.txcnt += (cnt - port->xmit_cnt);
  2099. port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
  2100. /* added by casper 1/11/2000 */
  2101. port->icount.tx += (cnt - port->xmit_cnt);
  2102. if (port->xmit_cnt < WAKEUP_CHARS) {
  2103. set_bit(MXSER_EVENT_TXLOW, &port->event);
  2104. schedule_work(&port->tqueue);
  2105. }
  2106. if (port->xmit_cnt <= 0) {
  2107. port->IER &= ~UART_IER_THRI;
  2108. outb(port->IER, port->ioaddr + UART_IER);
  2109. }
  2110. unlock:
  2111. spin_unlock_irqrestore(&port->slock, flags);
  2112. }
  2113. /*
  2114. * This is the serial driver's generic interrupt routine
  2115. */
  2116. static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  2117. {
  2118. int status, iir, i;
  2119. struct mxser_board *brd = NULL;
  2120. struct mxser_port *port;
  2121. int max, irqbits, bits, msr;
  2122. int pass_counter = 0;
  2123. unsigned int int_cnt;
  2124. int handled = IRQ_NONE;
  2125. /* spin_lock(&gm_lock); */
  2126. for (i = 0; i < MXSER_BOARDS; i++)
  2127. if (dev_id == &mxser_boards[i]) {
  2128. brd = dev_id;
  2129. break;
  2130. }
  2131. if (i == MXSER_BOARDS)
  2132. goto irq_stop;
  2133. if (brd == NULL)
  2134. goto irq_stop;
  2135. max = mxser_numports[brd->board_type - 1];
  2136. while (1) {
  2137. irqbits = inb(brd->vector) & brd->vector_mask;
  2138. if (irqbits == brd->vector_mask)
  2139. break;
  2140. handled = IRQ_HANDLED;
  2141. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  2142. if (irqbits == brd->vector_mask)
  2143. break;
  2144. if (bits & irqbits)
  2145. continue;
  2146. port = &brd->ports[i];
  2147. int_cnt = 0;
  2148. do {
  2149. /* following add by Victor Yu. 09-13-2002 */
  2150. iir = inb(port->ioaddr + UART_IIR);
  2151. if (iir & UART_IIR_NO_INT)
  2152. break;
  2153. iir &= MOXA_MUST_IIR_MASK;
  2154. if (!port->tty) {
  2155. status = inb(port->ioaddr + UART_LSR);
  2156. outb(0x27, port->ioaddr + UART_FCR);
  2157. inb(port->ioaddr + UART_MSR);
  2158. break;
  2159. }
  2160. /* above add by Victor Yu. 09-13-2002 */
  2161. /* following add by Victor Yu. 09-02-2002 */
  2162. status = inb(port->ioaddr + UART_LSR);
  2163. if (status & UART_LSR_PE)
  2164. port->err_shadow |= NPPI_NOTIFY_PARITY;
  2165. if (status & UART_LSR_FE)
  2166. port->err_shadow |= NPPI_NOTIFY_FRAMING;
  2167. if (status & UART_LSR_OE)
  2168. port->err_shadow |=
  2169. NPPI_NOTIFY_HW_OVERRUN;
  2170. if (status & UART_LSR_BI)
  2171. port->err_shadow |= NPPI_NOTIFY_BREAK;
  2172. if (port->board->chip_flag) {
  2173. /*
  2174. if ( (status & 0x02) && !(status & 0x01) ) {
  2175. outb(port->ioaddr+UART_FCR, 0x23);
  2176. continue;
  2177. }
  2178. */
  2179. if (iir == MOXA_MUST_IIR_GDA ||
  2180. iir == MOXA_MUST_IIR_RDA ||
  2181. iir == MOXA_MUST_IIR_RTO ||
  2182. iir == MOXA_MUST_IIR_LSR)
  2183. mxser_receive_chars(port,
  2184. &status);
  2185. } else {
  2186. /* above add by Victor Yu. 09-02-2002 */
  2187. status &= port->read_status_mask;
  2188. if (status & UART_LSR_DR)
  2189. mxser_receive_chars(port,
  2190. &status);
  2191. }
  2192. msr = inb(port->ioaddr + UART_MSR);
  2193. if (msr & UART_MSR_ANY_DELTA)
  2194. mxser_check_modem_status(port, msr);
  2195. /* following add by Victor Yu. 09-13-2002 */
  2196. if (port->board->chip_flag) {
  2197. if (iir == 0x02 && (status &
  2198. UART_LSR_THRE))
  2199. mxser_transmit_chars(port);
  2200. } else {
  2201. /* above add by Victor Yu. 09-13-2002 */
  2202. if (status & UART_LSR_THRE)
  2203. mxser_transmit_chars(port);
  2204. }
  2205. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  2206. }
  2207. if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
  2208. break; /* Prevent infinite loops */
  2209. }
  2210. irq_stop:
  2211. /* spin_unlock(&gm_lock); */
  2212. return handled;
  2213. }
  2214. static const struct tty_operations mxser_ops = {
  2215. .open = mxser_open,
  2216. .close = mxser_close,
  2217. .write = mxser_write,
  2218. .put_char = mxser_put_char,
  2219. .flush_chars = mxser_flush_chars,
  2220. .write_room = mxser_write_room,
  2221. .chars_in_buffer = mxser_chars_in_buffer,
  2222. .flush_buffer = mxser_flush_buffer,
  2223. .ioctl = mxser_ioctl,
  2224. .throttle = mxser_throttle,
  2225. .unthrottle = mxser_unthrottle,
  2226. .set_termios = mxser_set_termios,
  2227. .stop = mxser_stop,
  2228. .start = mxser_start,
  2229. .hangup = mxser_hangup,
  2230. .break_ctl = mxser_rs_break,
  2231. .wait_until_sent = mxser_wait_until_sent,
  2232. .tiocmget = mxser_tiocmget,
  2233. .tiocmset = mxser_tiocmset,
  2234. };
  2235. /*
  2236. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  2237. */
  2238. static int __devinit mxser_initbrd(struct mxser_board *brd)
  2239. {
  2240. struct mxser_port *info;
  2241. unsigned int i;
  2242. int retval;
  2243. printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
  2244. for (i = 0; i < brd->nports; i++) {
  2245. info = &brd->ports[i];
  2246. info->board = brd;
  2247. info->stop_rx = 0;
  2248. info->ldisc_stop_rx = 0;
  2249. /* Enhance mode enabled here */
  2250. if (brd->chip_flag != MOXA_OTHER_UART)
  2251. ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
  2252. info->flags = ASYNC_SHARE_IRQ;
  2253. info->type = brd->uart_type;
  2254. process_txrx_fifo(info);
  2255. info->custom_divisor = info->baud_base * 16;
  2256. info->close_delay = 5 * HZ / 10;
  2257. info->closing_wait = 30 * HZ;
  2258. INIT_WORK(&info->tqueue, mxser_do_softint, info);
  2259. info->normal_termios = mxvar_sdriver->init_termios;
  2260. init_waitqueue_head(&info->open_wait);
  2261. init_waitqueue_head(&info->close_wait);
  2262. init_waitqueue_head(&info->delta_msr_wait);
  2263. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  2264. info->err_shadow = 0;
  2265. spin_lock_init(&info->slock);
  2266. /* before set INT ISR, disable all int */
  2267. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  2268. info->ioaddr + UART_IER);
  2269. }
  2270. /*
  2271. * Allocate the IRQ if necessary
  2272. */
  2273. retval = request_irq(brd->irq, mxser_interrupt,
  2274. (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
  2275. IRQF_DISABLED, "mxser", brd);
  2276. if (retval) {
  2277. printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
  2278. "conflict with another device.\n",
  2279. mxser_brdname[brd->board_type - 1], brd->irq);
  2280. return retval;
  2281. }
  2282. return 0;
  2283. }
  2284. static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
  2285. {
  2286. int id, i, bits;
  2287. unsigned short regs[16], irq;
  2288. unsigned char scratch, scratch2;
  2289. brd->chip_flag = MOXA_OTHER_UART;
  2290. id = mxser_read_register(cap, regs);
  2291. if (id == C168_ASIC_ID) {
  2292. brd->board_type = MXSER_BOARD_C168_ISA;
  2293. brd->nports = 8;
  2294. } else if (id == C104_ASIC_ID) {
  2295. brd->board_type = MXSER_BOARD_C104_ISA;
  2296. brd->nports = 4;
  2297. } else if (id == C102_ASIC_ID) {
  2298. brd->board_type = MXSER_BOARD_C102_ISA;
  2299. brd->nports = 2;
  2300. } else if (id == CI132_ASIC_ID) {
  2301. brd->board_type = MXSER_BOARD_CI132;
  2302. brd->nports = 2;
  2303. } else if (id == CI134_ASIC_ID) {
  2304. brd->board_type = MXSER_BOARD_CI134;
  2305. brd->nports = 4;
  2306. } else if (id == CI104J_ASIC_ID) {
  2307. brd->board_type = MXSER_BOARD_CI104J;
  2308. brd->nports = 4;
  2309. } else
  2310. return 0;
  2311. irq = 0;
  2312. if (brd->nports == 2) {
  2313. irq = regs[9] & 0xF000;
  2314. irq = irq | (irq >> 4);
  2315. if (irq != (regs[9] & 0xFF00))
  2316. return MXSER_ERR_IRQ_CONFLIT;
  2317. } else if (brd->nports == 4) {
  2318. irq = regs[9] & 0xF000;
  2319. irq = irq | (irq >> 4);
  2320. irq = irq | (irq >> 8);
  2321. if (irq != regs[9])
  2322. return MXSER_ERR_IRQ_CONFLIT;
  2323. } else if (brd->nports == 8) {
  2324. irq = regs[9] & 0xF000;
  2325. irq = irq | (irq >> 4);
  2326. irq = irq | (irq >> 8);
  2327. if ((irq != regs[9]) || (irq != regs[10]))
  2328. return MXSER_ERR_IRQ_CONFLIT;
  2329. }
  2330. if (!irq)
  2331. return MXSER_ERR_IRQ;
  2332. brd->irq = ((int)(irq & 0xF000) >> 12);
  2333. for (i = 0; i < 8; i++)
  2334. brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
  2335. if ((regs[12] & 0x80) == 0)
  2336. return MXSER_ERR_VECTOR;
  2337. brd->vector = (int)regs[11]; /* interrupt vector */
  2338. if (id == 1)
  2339. brd->vector_mask = 0x00FF;
  2340. else
  2341. brd->vector_mask = 0x000F;
  2342. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2343. if (regs[12] & bits) {
  2344. brd->ports[i].baud_base = 921600;
  2345. brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
  2346. } else {
  2347. brd->ports[i].baud_base = 115200;
  2348. brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
  2349. }
  2350. }
  2351. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2352. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2353. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2354. outb(scratch2, cap + UART_LCR);
  2355. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2356. scratch = inb(cap + UART_IIR);
  2357. if (scratch & 0xC0)
  2358. brd->uart_type = PORT_16550A;
  2359. else
  2360. brd->uart_type = PORT_16450;
  2361. if (id == 1)
  2362. brd->nports = 8;
  2363. else
  2364. brd->nports = 4;
  2365. if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
  2366. return MXSER_ERR_IOADDR;
  2367. if (!request_region(brd->vector, 1, "mxser(vector)")) {
  2368. release_region(brd->ports[0].ioaddr, 8 * brd->nports);
  2369. return MXSER_ERR_VECTOR;
  2370. }
  2371. return brd->nports;
  2372. }
  2373. static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
  2374. struct pci_dev *pdev)
  2375. {
  2376. unsigned int i, j;
  2377. unsigned long ioaddress;
  2378. int retval;
  2379. /* io address */
  2380. brd->board_type = board_type;
  2381. brd->nports = mxser_numports[board_type - 1];
  2382. ioaddress = pci_resource_start(pdev, 2);
  2383. retval = pci_request_region(pdev, 2, "mxser(IO)");
  2384. if (retval)
  2385. goto err;
  2386. for (i = 0; i < brd->nports; i++)
  2387. brd->ports[i].ioaddr = ioaddress + 8 * i;
  2388. /* vector */
  2389. ioaddress = pci_resource_start(pdev, 3);
  2390. retval = pci_request_region(pdev, 3, "mxser(vector)");
  2391. if (retval)
  2392. goto err_relio;
  2393. brd->vector = ioaddress;
  2394. /* irq */
  2395. brd->irq = pdev->irq;
  2396. brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
  2397. brd->uart_type = PORT_16550A;
  2398. brd->vector_mask = 0;
  2399. for (i = 0; i < brd->nports; i++) {
  2400. for (j = 0; j < UART_INFO_NUM; j++) {
  2401. if (Gpci_uart_info[j].type == brd->chip_flag) {
  2402. brd->ports[i].max_baud =
  2403. Gpci_uart_info[j].max_baud;
  2404. /* exception....CP-102 */
  2405. if (board_type == MXSER_BOARD_CP102)
  2406. brd->ports[i].max_baud = 921600;
  2407. break;
  2408. }
  2409. }
  2410. }
  2411. if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
  2412. for (i = 0; i < brd->nports; i++) {
  2413. if (i < 4)
  2414. brd->ports[i].opmode_ioaddr = ioaddress + 4;
  2415. else
  2416. brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
  2417. }
  2418. outb(0, ioaddress + 4); /* default set to RS232 mode */
  2419. outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
  2420. }
  2421. for (i = 0; i < brd->nports; i++) {
  2422. brd->vector_mask |= (1 << i);
  2423. brd->ports[i].baud_base = 921600;
  2424. }
  2425. return 0;
  2426. err_relio:
  2427. pci_release_region(pdev, 2);
  2428. err:
  2429. return retval;
  2430. }
  2431. static int __init mxser_module_init(void)
  2432. {
  2433. struct pci_dev *pdev = NULL;
  2434. struct mxser_board *brd;
  2435. unsigned int i, m;
  2436. int retval, b, n;
  2437. pr_debug("Loading module mxser ...\n");
  2438. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  2439. if (!mxvar_sdriver)
  2440. return -ENOMEM;
  2441. spin_lock_init(&gm_lock);
  2442. for (i = 0; i < MXSER_BOARDS; i++)
  2443. mxser_boards[i].board_type = -1;
  2444. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
  2445. MXSER_VERSION);
  2446. /* Initialize the tty_driver structure */
  2447. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  2448. mxvar_sdriver->name = "ttyM";
  2449. mxvar_sdriver->major = ttymajor;
  2450. mxvar_sdriver->minor_start = 0;
  2451. mxvar_sdriver->num = MXSER_PORTS + 1;
  2452. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  2453. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  2454. mxvar_sdriver->init_termios = tty_std_termios;
  2455. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  2456. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
  2457. tty_set_operations(mxvar_sdriver, &mxser_ops);
  2458. mxvar_sdriver->ttys = mxvar_tty;
  2459. mxvar_sdriver->termios = mxvar_termios;
  2460. mxvar_sdriver->termios_locked = mxvar_termios_locked;
  2461. mxvar_diagflag = 0;
  2462. m = 0;
  2463. /* Start finding ISA boards here */
  2464. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  2465. int cap;
  2466. if (!(cap = mxserBoardCAP[b]))
  2467. continue;
  2468. brd = &mxser_boards[m];
  2469. retval = mxser_get_ISA_conf(cap, brd);
  2470. if (retval != 0)
  2471. printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
  2472. mxser_brdname[brd->board_type - 1], ioaddr[b]);
  2473. if (retval <= 0) {
  2474. if (retval == MXSER_ERR_IRQ)
  2475. printk(KERN_ERR "Invalid interrupt number, "
  2476. "board not configured\n");
  2477. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  2478. printk(KERN_ERR "Invalid interrupt number, "
  2479. "board not configured\n");
  2480. else if (retval == MXSER_ERR_VECTOR)
  2481. printk(KERN_ERR "Invalid interrupt vector, "
  2482. "board not configured\n");
  2483. else if (retval == MXSER_ERR_IOADDR)
  2484. printk(KERN_ERR "Invalid I/O address, "
  2485. "board not configured\n");
  2486. continue;
  2487. }
  2488. brd->pdev = NULL;
  2489. /* mxser_initbrd will hook ISR. */
  2490. if (mxser_initbrd(brd) < 0)
  2491. continue;
  2492. m++;
  2493. }
  2494. /* Start finding ISA boards from module arg */
  2495. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  2496. unsigned long cap;
  2497. if (!(cap = ioaddr[b]))
  2498. continue;
  2499. brd = &mxser_boards[m];
  2500. retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
  2501. if (retval != 0)
  2502. printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
  2503. mxser_brdname[brd->board_type - 1], ioaddr[b]);
  2504. if (retval <= 0) {
  2505. if (retval == MXSER_ERR_IRQ)
  2506. printk(KERN_ERR "Invalid interrupt number, "
  2507. "board not configured\n");
  2508. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  2509. printk(KERN_ERR "Invalid interrupt number, "
  2510. "board not configured\n");
  2511. else if (retval == MXSER_ERR_VECTOR)
  2512. printk(KERN_ERR "Invalid interrupt vector, "
  2513. "board not configured\n");
  2514. else if (retval == MXSER_ERR_IOADDR)
  2515. printk(KERN_ERR "Invalid I/O address, "
  2516. "board not configured\n");
  2517. continue;
  2518. }
  2519. brd->pdev = NULL;
  2520. /* mxser_initbrd will hook ISR. */
  2521. if (mxser_initbrd(brd) < 0)
  2522. continue;
  2523. m++;
  2524. }
  2525. /* start finding PCI board here */
  2526. n = ARRAY_SIZE(mxser_pcibrds) - 1;
  2527. b = 0;
  2528. while (b < n) {
  2529. pdev = pci_get_device(mxser_pcibrds[b].vendor,
  2530. mxser_pcibrds[b].device, pdev);
  2531. if (pdev == NULL) {
  2532. b++;
  2533. continue;
  2534. }
  2535. printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
  2536. mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
  2537. pdev->bus->number, PCI_SLOT(pdev->devfn));
  2538. if (m >= MXSER_BOARDS)
  2539. printk(KERN_ERR
  2540. "Too many Smartio/Industio family boards find "
  2541. "(maximum %d), board not configured\n",
  2542. MXSER_BOARDS);
  2543. else {
  2544. if (pci_enable_device(pdev)) {
  2545. printk(KERN_ERR "Moxa SmartI/O PCI enable "
  2546. "fail !\n");
  2547. continue;
  2548. }
  2549. brd = &mxser_boards[m];
  2550. brd->pdev = pdev;
  2551. retval = mxser_get_PCI_conf(
  2552. (int)mxser_pcibrds[b].driver_data,
  2553. brd, pdev);
  2554. if (retval < 0) {
  2555. if (retval == MXSER_ERR_IRQ)
  2556. printk(KERN_ERR
  2557. "Invalid interrupt number, "
  2558. "board not configured\n");
  2559. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  2560. printk(KERN_ERR
  2561. "Invalid interrupt number, "
  2562. "board not configured\n");
  2563. else if (retval == MXSER_ERR_VECTOR)
  2564. printk(KERN_ERR
  2565. "Invalid interrupt vector, "
  2566. "board not configured\n");
  2567. else if (retval == MXSER_ERR_IOADDR)
  2568. printk(KERN_ERR
  2569. "Invalid I/O address, "
  2570. "board not configured\n");
  2571. continue;
  2572. }
  2573. /* mxser_initbrd will hook ISR. */
  2574. if (mxser_initbrd(brd) < 0)
  2575. continue;
  2576. m++;
  2577. /* Keep an extra reference if we succeeded. It will
  2578. be returned at unload time */
  2579. pci_dev_get(pdev);
  2580. }
  2581. }
  2582. retval = tty_register_driver(mxvar_sdriver);
  2583. if (retval) {
  2584. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
  2585. " driver !\n");
  2586. put_tty_driver(mxvar_sdriver);
  2587. for (i = 0; i < MXSER_BOARDS; i++) {
  2588. if (mxser_boards[i].board_type == -1)
  2589. continue;
  2590. else {
  2591. free_irq(mxser_boards[i].irq, &mxser_boards[i]);
  2592. /* todo: release io, vector */
  2593. }
  2594. }
  2595. return retval;
  2596. }
  2597. pr_debug("Done.\n");
  2598. return retval;
  2599. }
  2600. static void __exit mxser_module_exit(void)
  2601. {
  2602. int i, err;
  2603. pr_debug("Unloading module mxser ...\n");
  2604. err = tty_unregister_driver(mxvar_sdriver);
  2605. if (!err)
  2606. put_tty_driver(mxvar_sdriver);
  2607. else
  2608. printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
  2609. for (i = 0; i < MXSER_BOARDS; i++) {
  2610. struct pci_dev *pdev;
  2611. if (mxser_boards[i].board_type == -1)
  2612. continue;
  2613. else {
  2614. pdev = mxser_boards[i].pdev;
  2615. free_irq(mxser_boards[i].irq, &mxser_boards[i]);
  2616. if (pdev != NULL) { /* PCI */
  2617. pci_release_region(pdev, 2);
  2618. pci_release_region(pdev, 3);
  2619. pci_dev_put(pdev);
  2620. } else {
  2621. release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
  2622. release_region(mxser_boards[i].vector, 1);
  2623. }
  2624. }
  2625. }
  2626. pr_debug("Done.\n");
  2627. }
  2628. module_init(mxser_module_init);
  2629. module_exit(mxser_module_exit);