mxser.c 82 KB

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