mxser.c 84 KB

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