mxser.c 81 KB

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