mxser.c 83 KB

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