mxser.c 83 KB

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