mxser.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. /*
  2. * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
  3. *
  4. * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
  5. * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
  6. *
  7. * This code is loosely based on the 1.8 moxa driver which is based on
  8. * Linux serial driver, written by Linus Torvalds, Theodore T'so and
  9. * others.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
  17. * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
  18. * www.moxa.com.
  19. * - Fixed x86_64 cleanness
  20. */
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/signal.h>
  24. #include <linux/sched.h>
  25. #include <linux/timer.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/serial.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/major.h>
  32. #include <linux/string.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/ptrace.h>
  35. #include <linux/gfp.h>
  36. #include <linux/ioport.h>
  37. #include <linux/mm.h>
  38. #include <linux/delay.h>
  39. #include <linux/pci.h>
  40. #include <linux/bitops.h>
  41. #include <asm/system.h>
  42. #include <asm/io.h>
  43. #include <asm/irq.h>
  44. #include <asm/uaccess.h>
  45. #include "mxser.h"
  46. #define MXSER_VERSION "2.0.4" /* 1.12 */
  47. #define MXSERMAJOR 174
  48. #define MXSER_BOARDS 4 /* Max. boards */
  49. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  50. #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
  51. #define MXSER_ISR_PASS_LIMIT 100
  52. /*CheckIsMoxaMust return value*/
  53. #define MOXA_OTHER_UART 0x00
  54. #define MOXA_MUST_MU150_HWID 0x01
  55. #define MOXA_MUST_MU860_HWID 0x02
  56. #define WAKEUP_CHARS 256
  57. #define UART_MCR_AFE 0x20
  58. #define UART_LSR_SPECIAL 0x1E
  59. #define PCI_DEVICE_ID_POS104UL 0x1044
  60. #define PCI_DEVICE_ID_CB108 0x1080
  61. #define PCI_DEVICE_ID_CP102UF 0x1023
  62. #define PCI_DEVICE_ID_CB114 0x1142
  63. #define PCI_DEVICE_ID_CP114UL 0x1143
  64. #define PCI_DEVICE_ID_CB134I 0x1341
  65. #define PCI_DEVICE_ID_CP138U 0x1380
  66. #define C168_ASIC_ID 1
  67. #define C104_ASIC_ID 2
  68. #define C102_ASIC_ID 0xB
  69. #define CI132_ASIC_ID 4
  70. #define CI134_ASIC_ID 3
  71. #define CI104J_ASIC_ID 5
  72. #define MXSER_HIGHBAUD 1
  73. #define MXSER_HAS2 2
  74. /* This is only for PCI */
  75. static const struct {
  76. int type;
  77. int tx_fifo;
  78. int rx_fifo;
  79. int xmit_fifo_size;
  80. int rx_high_water;
  81. int rx_trigger;
  82. int rx_low_water;
  83. long max_baud;
  84. } Gpci_uart_info[] = {
  85. {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
  86. {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
  87. {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
  88. };
  89. #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
  90. struct mxser_cardinfo {
  91. char *name;
  92. unsigned int nports;
  93. unsigned int flags;
  94. };
  95. static const struct mxser_cardinfo mxser_cards[] = {
  96. /* 0*/ { "C168 series", 8, },
  97. { "C104 series", 4, },
  98. { "CI-104J series", 4, },
  99. { "C168H/PCI series", 8, },
  100. { "C104H/PCI series", 4, },
  101. /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
  102. { "CI-132 series", 4, MXSER_HAS2 },
  103. { "CI-134 series", 4, },
  104. { "CP-132 series", 2, },
  105. { "CP-114 series", 4, },
  106. /*10*/ { "CT-114 series", 4, },
  107. { "CP-102 series", 2, MXSER_HIGHBAUD },
  108. { "CP-104U series", 4, },
  109. { "CP-168U series", 8, },
  110. { "CP-132U series", 2, },
  111. /*15*/ { "CP-134U series", 4, },
  112. { "CP-104JU series", 4, },
  113. { "Moxa UC7000 Serial", 8, }, /* RC7000 */
  114. { "CP-118U series", 8, },
  115. { "CP-102UL series", 2, },
  116. /*20*/ { "CP-102U series", 2, },
  117. { "CP-118EL series", 8, },
  118. { "CP-168EL series", 8, },
  119. { "CP-104EL series", 4, },
  120. { "CB-108 series", 8, },
  121. /*25*/ { "CB-114 series", 4, },
  122. { "CB-134I series", 4, },
  123. { "CP-138U series", 8, },
  124. { "POS-104UL series", 4, },
  125. { "CP-114UL series", 4, },
  126. /*30*/ { "CP-102UF series", 2, }
  127. };
  128. /* driver_data correspond to the lines in the structure above
  129. see also ISA probe function before you change something */
  130. static struct pci_device_id mxser_pcibrds[] = {
  131. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
  132. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
  133. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
  134. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
  135. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
  136. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
  137. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
  138. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
  139. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
  140. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
  141. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
  142. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
  143. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
  144. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
  145. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
  146. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
  147. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
  148. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
  149. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
  150. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
  151. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
  152. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
  153. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
  154. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
  155. { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
  156. { }
  157. };
  158. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  159. static unsigned long ioaddr[MXSER_BOARDS];
  160. static int ttymajor = MXSERMAJOR;
  161. /* Variables for insmod */
  162. MODULE_AUTHOR("Casper Yang");
  163. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  164. module_param_array(ioaddr, ulong, NULL, 0);
  165. MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
  166. module_param(ttymajor, int, 0);
  167. MODULE_LICENSE("GPL");
  168. struct mxser_log {
  169. int tick;
  170. unsigned long rxcnt[MXSER_PORTS];
  171. unsigned long txcnt[MXSER_PORTS];
  172. };
  173. struct mxser_mon {
  174. unsigned long rxcnt;
  175. unsigned long txcnt;
  176. unsigned long up_rxcnt;
  177. unsigned long up_txcnt;
  178. int modem_status;
  179. unsigned char hold_reason;
  180. };
  181. struct mxser_mon_ext {
  182. unsigned long rx_cnt[32];
  183. unsigned long tx_cnt[32];
  184. unsigned long up_rxcnt[32];
  185. unsigned long up_txcnt[32];
  186. int modem_status[32];
  187. long baudrate[32];
  188. int databits[32];
  189. int stopbits[32];
  190. int parity[32];
  191. int flowctrl[32];
  192. int fifo[32];
  193. int iftype[32];
  194. };
  195. struct mxser_board;
  196. struct mxser_port {
  197. struct tty_port port;
  198. struct mxser_board *board;
  199. unsigned long ioaddr;
  200. unsigned long opmode_ioaddr;
  201. int max_baud;
  202. int rx_high_water;
  203. int rx_trigger; /* Rx fifo trigger level */
  204. int rx_low_water;
  205. int baud_base; /* max. speed */
  206. int type; /* UART type */
  207. int x_char; /* xon/xoff character */
  208. int IER; /* Interrupt Enable Register */
  209. int MCR; /* Modem control register */
  210. unsigned char stop_rx;
  211. unsigned char ldisc_stop_rx;
  212. int custom_divisor;
  213. unsigned char err_shadow;
  214. struct async_icount icount; /* kernel counters for 4 input interrupts */
  215. int timeout;
  216. int read_status_mask;
  217. int ignore_status_mask;
  218. int xmit_fifo_size;
  219. int xmit_head;
  220. int xmit_tail;
  221. int xmit_cnt;
  222. struct ktermios normal_termios;
  223. struct mxser_mon mon_data;
  224. spinlock_t slock;
  225. wait_queue_head_t delta_msr_wait;
  226. };
  227. struct mxser_board {
  228. unsigned int idx;
  229. int irq;
  230. const struct mxser_cardinfo *info;
  231. unsigned long vector;
  232. unsigned long vector_mask;
  233. int chip_flag;
  234. int uart_type;
  235. struct mxser_port ports[MXSER_PORTS_PER_BOARD];
  236. };
  237. struct mxser_mstatus {
  238. tcflag_t cflag;
  239. int cts;
  240. int dsr;
  241. int ri;
  242. int dcd;
  243. };
  244. static struct mxser_board mxser_boards[MXSER_BOARDS];
  245. static struct tty_driver *mxvar_sdriver;
  246. static struct mxser_log mxvar_log;
  247. static int mxser_set_baud_method[MXSER_PORTS + 1];
  248. static void mxser_enable_must_enchance_mode(unsigned long baseio)
  249. {
  250. u8 oldlcr;
  251. u8 efr;
  252. oldlcr = inb(baseio + UART_LCR);
  253. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  254. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  255. efr |= MOXA_MUST_EFR_EFRB_ENABLE;
  256. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  257. outb(oldlcr, baseio + UART_LCR);
  258. }
  259. static void mxser_disable_must_enchance_mode(unsigned long baseio)
  260. {
  261. u8 oldlcr;
  262. u8 efr;
  263. oldlcr = inb(baseio + UART_LCR);
  264. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  265. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  266. efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
  267. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  268. outb(oldlcr, baseio + UART_LCR);
  269. }
  270. static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
  271. {
  272. u8 oldlcr;
  273. u8 efr;
  274. oldlcr = inb(baseio + UART_LCR);
  275. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  276. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  277. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  278. efr |= MOXA_MUST_EFR_BANK0;
  279. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  280. outb(value, baseio + MOXA_MUST_XON1_REGISTER);
  281. outb(oldlcr, baseio + UART_LCR);
  282. }
  283. static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
  284. {
  285. u8 oldlcr;
  286. u8 efr;
  287. oldlcr = inb(baseio + UART_LCR);
  288. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  289. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  290. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  291. efr |= MOXA_MUST_EFR_BANK0;
  292. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  293. outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
  294. outb(oldlcr, baseio + UART_LCR);
  295. }
  296. static void mxser_set_must_fifo_value(struct mxser_port *info)
  297. {
  298. u8 oldlcr;
  299. u8 efr;
  300. oldlcr = inb(info->ioaddr + UART_LCR);
  301. outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
  302. efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
  303. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  304. efr |= MOXA_MUST_EFR_BANK1;
  305. outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
  306. outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
  307. outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
  308. outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
  309. outb(oldlcr, info->ioaddr + UART_LCR);
  310. }
  311. static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
  312. {
  313. u8 oldlcr;
  314. u8 efr;
  315. oldlcr = inb(baseio + UART_LCR);
  316. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  317. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  318. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  319. efr |= MOXA_MUST_EFR_BANK2;
  320. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  321. outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
  322. outb(oldlcr, baseio + UART_LCR);
  323. }
  324. static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
  325. {
  326. u8 oldlcr;
  327. u8 efr;
  328. oldlcr = inb(baseio + UART_LCR);
  329. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  330. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  331. efr &= ~MOXA_MUST_EFR_BANK_MASK;
  332. efr |= MOXA_MUST_EFR_BANK2;
  333. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  334. *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
  335. outb(oldlcr, baseio + UART_LCR);
  336. }
  337. static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
  338. {
  339. u8 oldlcr;
  340. u8 efr;
  341. oldlcr = inb(baseio + UART_LCR);
  342. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  343. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  344. efr &= ~MOXA_MUST_EFR_SF_MASK;
  345. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  346. outb(oldlcr, baseio + UART_LCR);
  347. }
  348. static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
  349. {
  350. u8 oldlcr;
  351. u8 efr;
  352. oldlcr = inb(baseio + UART_LCR);
  353. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  354. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  355. efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
  356. efr |= MOXA_MUST_EFR_SF_TX1;
  357. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  358. outb(oldlcr, baseio + UART_LCR);
  359. }
  360. static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
  361. {
  362. u8 oldlcr;
  363. u8 efr;
  364. oldlcr = inb(baseio + UART_LCR);
  365. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  366. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  367. efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
  368. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  369. outb(oldlcr, baseio + UART_LCR);
  370. }
  371. static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
  372. {
  373. u8 oldlcr;
  374. u8 efr;
  375. oldlcr = inb(baseio + UART_LCR);
  376. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  377. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  378. efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
  379. efr |= MOXA_MUST_EFR_SF_RX1;
  380. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  381. outb(oldlcr, baseio + UART_LCR);
  382. }
  383. static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
  384. {
  385. u8 oldlcr;
  386. u8 efr;
  387. oldlcr = inb(baseio + UART_LCR);
  388. outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
  389. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  390. efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
  391. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  392. outb(oldlcr, baseio + UART_LCR);
  393. }
  394. #ifdef CONFIG_PCI
  395. static int __devinit CheckIsMoxaMust(unsigned long io)
  396. {
  397. u8 oldmcr, hwid;
  398. int i;
  399. outb(0, io + UART_LCR);
  400. mxser_disable_must_enchance_mode(io);
  401. oldmcr = inb(io + UART_MCR);
  402. outb(0, io + UART_MCR);
  403. mxser_set_must_xon1_value(io, 0x11);
  404. if ((hwid = inb(io + UART_MCR)) != 0) {
  405. outb(oldmcr, io + UART_MCR);
  406. return MOXA_OTHER_UART;
  407. }
  408. mxser_get_must_hardware_id(io, &hwid);
  409. for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
  410. if (hwid == Gpci_uart_info[i].type)
  411. return (int)hwid;
  412. }
  413. return MOXA_OTHER_UART;
  414. }
  415. #endif
  416. static void process_txrx_fifo(struct mxser_port *info)
  417. {
  418. int i;
  419. if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
  420. info->rx_trigger = 1;
  421. info->rx_high_water = 1;
  422. info->rx_low_water = 1;
  423. info->xmit_fifo_size = 1;
  424. } else
  425. for (i = 0; i < UART_INFO_NUM; i++)
  426. if (info->board->chip_flag == Gpci_uart_info[i].type) {
  427. info->rx_trigger = Gpci_uart_info[i].rx_trigger;
  428. info->rx_low_water = Gpci_uart_info[i].rx_low_water;
  429. info->rx_high_water = Gpci_uart_info[i].rx_high_water;
  430. info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
  431. break;
  432. }
  433. }
  434. static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
  435. {
  436. static unsigned char mxser_msr[MXSER_PORTS + 1];
  437. unsigned char status = 0;
  438. status = inb(baseaddr + UART_MSR);
  439. mxser_msr[port] &= 0x0F;
  440. mxser_msr[port] |= status;
  441. status = mxser_msr[port];
  442. if (mode)
  443. mxser_msr[port] = 0;
  444. return status;
  445. }
  446. static int mxser_carrier_raised(struct tty_port *port)
  447. {
  448. struct mxser_port *mp = container_of(port, struct mxser_port, port);
  449. return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
  450. }
  451. static void mxser_raise_dtr_rts(struct tty_port *port)
  452. {
  453. struct mxser_port *mp = container_of(port, struct mxser_port, port);
  454. unsigned long flags;
  455. spin_lock_irqsave(&mp->slock, flags);
  456. outb(inb(mp->ioaddr + UART_MCR) |
  457. UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
  458. spin_unlock_irqrestore(&mp->slock, flags);
  459. }
  460. static int mxser_set_baud(struct tty_struct *tty, long newspd)
  461. {
  462. struct mxser_port *info = tty->driver_data;
  463. int quot = 0, baud;
  464. unsigned char cval;
  465. if (!info->ioaddr)
  466. return -1;
  467. if (newspd > info->max_baud)
  468. return -1;
  469. if (newspd == 134) {
  470. quot = 2 * info->baud_base / 269;
  471. tty_encode_baud_rate(tty, 134, 134);
  472. } else if (newspd) {
  473. quot = info->baud_base / newspd;
  474. if (quot == 0)
  475. quot = 1;
  476. baud = info->baud_base/quot;
  477. tty_encode_baud_rate(tty, baud, baud);
  478. } else {
  479. quot = 0;
  480. }
  481. info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
  482. info->timeout += HZ / 50; /* Add .02 seconds of slop */
  483. if (quot) {
  484. info->MCR |= UART_MCR_DTR;
  485. outb(info->MCR, info->ioaddr + UART_MCR);
  486. } else {
  487. info->MCR &= ~UART_MCR_DTR;
  488. outb(info->MCR, info->ioaddr + UART_MCR);
  489. return 0;
  490. }
  491. cval = inb(info->ioaddr + UART_LCR);
  492. outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
  493. outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
  494. outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
  495. outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
  496. #ifdef BOTHER
  497. if (C_BAUD(tty) == BOTHER) {
  498. quot = info->baud_base % newspd;
  499. quot *= 8;
  500. if (quot % newspd > newspd / 2) {
  501. quot /= newspd;
  502. quot++;
  503. } else
  504. quot /= newspd;
  505. mxser_set_must_enum_value(info->ioaddr, quot);
  506. } else
  507. #endif
  508. mxser_set_must_enum_value(info->ioaddr, 0);
  509. return 0;
  510. }
  511. /*
  512. * This routine is called to set the UART divisor registers to match
  513. * the specified baud rate for a serial port.
  514. */
  515. static int mxser_change_speed(struct tty_struct *tty,
  516. struct ktermios *old_termios)
  517. {
  518. struct mxser_port *info = tty->driver_data;
  519. unsigned cflag, cval, fcr;
  520. int ret = 0;
  521. unsigned char status;
  522. cflag = tty->termios->c_cflag;
  523. if (!info->ioaddr)
  524. return ret;
  525. if (mxser_set_baud_method[tty->index] == 0)
  526. mxser_set_baud(tty, tty_get_baud_rate(tty));
  527. /* byte size and parity */
  528. switch (cflag & CSIZE) {
  529. case CS5:
  530. cval = 0x00;
  531. break;
  532. case CS6:
  533. cval = 0x01;
  534. break;
  535. case CS7:
  536. cval = 0x02;
  537. break;
  538. case CS8:
  539. cval = 0x03;
  540. break;
  541. default:
  542. cval = 0x00;
  543. break; /* too keep GCC shut... */
  544. }
  545. if (cflag & CSTOPB)
  546. cval |= 0x04;
  547. if (cflag & PARENB)
  548. cval |= UART_LCR_PARITY;
  549. if (!(cflag & PARODD))
  550. cval |= UART_LCR_EPAR;
  551. if (cflag & CMSPAR)
  552. cval |= UART_LCR_SPAR;
  553. if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
  554. if (info->board->chip_flag) {
  555. fcr = UART_FCR_ENABLE_FIFO;
  556. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  557. mxser_set_must_fifo_value(info);
  558. } else
  559. fcr = 0;
  560. } else {
  561. fcr = UART_FCR_ENABLE_FIFO;
  562. if (info->board->chip_flag) {
  563. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  564. mxser_set_must_fifo_value(info);
  565. } else {
  566. switch (info->rx_trigger) {
  567. case 1:
  568. fcr |= UART_FCR_TRIGGER_1;
  569. break;
  570. case 4:
  571. fcr |= UART_FCR_TRIGGER_4;
  572. break;
  573. case 8:
  574. fcr |= UART_FCR_TRIGGER_8;
  575. break;
  576. default:
  577. fcr |= UART_FCR_TRIGGER_14;
  578. break;
  579. }
  580. }
  581. }
  582. /* CTS flow control flag and modem status interrupts */
  583. info->IER &= ~UART_IER_MSI;
  584. info->MCR &= ~UART_MCR_AFE;
  585. if (cflag & CRTSCTS) {
  586. info->port.flags |= ASYNC_CTS_FLOW;
  587. info->IER |= UART_IER_MSI;
  588. if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
  589. info->MCR |= UART_MCR_AFE;
  590. } else {
  591. status = inb(info->ioaddr + UART_MSR);
  592. if (tty->hw_stopped) {
  593. if (status & UART_MSR_CTS) {
  594. tty->hw_stopped = 0;
  595. if (info->type != PORT_16550A &&
  596. !info->board->chip_flag) {
  597. outb(info->IER & ~UART_IER_THRI,
  598. info->ioaddr +
  599. UART_IER);
  600. info->IER |= UART_IER_THRI;
  601. outb(info->IER, info->ioaddr +
  602. UART_IER);
  603. }
  604. tty_wakeup(tty);
  605. }
  606. } else {
  607. if (!(status & UART_MSR_CTS)) {
  608. tty->hw_stopped = 1;
  609. if ((info->type != PORT_16550A) &&
  610. (!info->board->chip_flag)) {
  611. info->IER &= ~UART_IER_THRI;
  612. outb(info->IER, info->ioaddr +
  613. UART_IER);
  614. }
  615. }
  616. }
  617. }
  618. } else {
  619. info->port.flags &= ~ASYNC_CTS_FLOW;
  620. }
  621. outb(info->MCR, info->ioaddr + UART_MCR);
  622. if (cflag & CLOCAL) {
  623. info->port.flags &= ~ASYNC_CHECK_CD;
  624. } else {
  625. info->port.flags |= ASYNC_CHECK_CD;
  626. info->IER |= UART_IER_MSI;
  627. }
  628. outb(info->IER, info->ioaddr + UART_IER);
  629. /*
  630. * Set up parity check flag
  631. */
  632. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  633. if (I_INPCK(tty))
  634. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  635. if (I_BRKINT(tty) || I_PARMRK(tty))
  636. info->read_status_mask |= UART_LSR_BI;
  637. info->ignore_status_mask = 0;
  638. if (I_IGNBRK(tty)) {
  639. info->ignore_status_mask |= UART_LSR_BI;
  640. info->read_status_mask |= UART_LSR_BI;
  641. /*
  642. * If we're ignore parity and break indicators, ignore
  643. * overruns too. (For real raw support).
  644. */
  645. if (I_IGNPAR(tty)) {
  646. info->ignore_status_mask |=
  647. UART_LSR_OE |
  648. UART_LSR_PE |
  649. UART_LSR_FE;
  650. info->read_status_mask |=
  651. UART_LSR_OE |
  652. UART_LSR_PE |
  653. UART_LSR_FE;
  654. }
  655. }
  656. if (info->board->chip_flag) {
  657. mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
  658. mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
  659. if (I_IXON(tty)) {
  660. mxser_enable_must_rx_software_flow_control(
  661. info->ioaddr);
  662. } else {
  663. mxser_disable_must_rx_software_flow_control(
  664. info->ioaddr);
  665. }
  666. if (I_IXOFF(tty)) {
  667. mxser_enable_must_tx_software_flow_control(
  668. info->ioaddr);
  669. } else {
  670. mxser_disable_must_tx_software_flow_control(
  671. info->ioaddr);
  672. }
  673. }
  674. outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
  675. outb(cval, info->ioaddr + UART_LCR);
  676. return ret;
  677. }
  678. static void mxser_check_modem_status(struct tty_struct *tty,
  679. struct mxser_port *port, int status)
  680. {
  681. /* update input line counters */
  682. if (status & UART_MSR_TERI)
  683. port->icount.rng++;
  684. if (status & UART_MSR_DDSR)
  685. port->icount.dsr++;
  686. if (status & UART_MSR_DDCD)
  687. port->icount.dcd++;
  688. if (status & UART_MSR_DCTS)
  689. port->icount.cts++;
  690. port->mon_data.modem_status = status;
  691. wake_up_interruptible(&port->delta_msr_wait);
  692. if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
  693. if (status & UART_MSR_DCD)
  694. wake_up_interruptible(&port->port.open_wait);
  695. }
  696. if (port->port.flags & ASYNC_CTS_FLOW) {
  697. if (tty->hw_stopped) {
  698. if (status & UART_MSR_CTS) {
  699. tty->hw_stopped = 0;
  700. if ((port->type != PORT_16550A) &&
  701. (!port->board->chip_flag)) {
  702. outb(port->IER & ~UART_IER_THRI,
  703. port->ioaddr + UART_IER);
  704. port->IER |= UART_IER_THRI;
  705. outb(port->IER, port->ioaddr +
  706. UART_IER);
  707. }
  708. tty_wakeup(tty);
  709. }
  710. } else {
  711. if (!(status & UART_MSR_CTS)) {
  712. tty->hw_stopped = 1;
  713. if (port->type != PORT_16550A &&
  714. !port->board->chip_flag) {
  715. port->IER &= ~UART_IER_THRI;
  716. outb(port->IER, port->ioaddr +
  717. UART_IER);
  718. }
  719. }
  720. }
  721. }
  722. }
  723. static int mxser_startup(struct tty_struct *tty)
  724. {
  725. struct mxser_port *info = tty->driver_data;
  726. unsigned long page;
  727. unsigned long flags;
  728. page = __get_free_page(GFP_KERNEL);
  729. if (!page)
  730. return -ENOMEM;
  731. spin_lock_irqsave(&info->slock, flags);
  732. if (info->port.flags & ASYNC_INITIALIZED) {
  733. free_page(page);
  734. spin_unlock_irqrestore(&info->slock, flags);
  735. return 0;
  736. }
  737. if (!info->ioaddr || !info->type) {
  738. set_bit(TTY_IO_ERROR, &tty->flags);
  739. free_page(page);
  740. spin_unlock_irqrestore(&info->slock, flags);
  741. return 0;
  742. }
  743. if (info->port.xmit_buf)
  744. free_page(page);
  745. else
  746. info->port.xmit_buf = (unsigned char *) page;
  747. /*
  748. * Clear the FIFO buffers and disable them
  749. * (they will be reenabled in mxser_change_speed())
  750. */
  751. if (info->board->chip_flag)
  752. outb((UART_FCR_CLEAR_RCVR |
  753. UART_FCR_CLEAR_XMIT |
  754. MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
  755. else
  756. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  757. info->ioaddr + UART_FCR);
  758. /*
  759. * At this point there's no way the LSR could still be 0xFF;
  760. * if it is, then bail out, because there's likely no UART
  761. * here.
  762. */
  763. if (inb(info->ioaddr + UART_LSR) == 0xff) {
  764. spin_unlock_irqrestore(&info->slock, flags);
  765. if (capable(CAP_SYS_ADMIN)) {
  766. if (tty)
  767. set_bit(TTY_IO_ERROR, &tty->flags);
  768. return 0;
  769. } else
  770. return -ENODEV;
  771. }
  772. /*
  773. * Clear the interrupt registers.
  774. */
  775. (void) inb(info->ioaddr + UART_LSR);
  776. (void) inb(info->ioaddr + UART_RX);
  777. (void) inb(info->ioaddr + UART_IIR);
  778. (void) inb(info->ioaddr + UART_MSR);
  779. /*
  780. * Now, initialize the UART
  781. */
  782. outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
  783. info->MCR = UART_MCR_DTR | UART_MCR_RTS;
  784. outb(info->MCR, info->ioaddr + UART_MCR);
  785. /*
  786. * Finally, enable interrupts
  787. */
  788. info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  789. if (info->board->chip_flag)
  790. info->IER |= MOXA_MUST_IER_EGDAI;
  791. outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
  792. /*
  793. * And clear the interrupt registers again for luck.
  794. */
  795. (void) inb(info->ioaddr + UART_LSR);
  796. (void) inb(info->ioaddr + UART_RX);
  797. (void) inb(info->ioaddr + UART_IIR);
  798. (void) inb(info->ioaddr + UART_MSR);
  799. clear_bit(TTY_IO_ERROR, &tty->flags);
  800. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  801. /*
  802. * and set the speed of the serial port
  803. */
  804. mxser_change_speed(tty, NULL);
  805. info->port.flags |= ASYNC_INITIALIZED;
  806. spin_unlock_irqrestore(&info->slock, flags);
  807. return 0;
  808. }
  809. /*
  810. * This routine will shutdown a serial port; interrupts maybe disabled, and
  811. * DTR is dropped if the hangup on close termio flag is on.
  812. */
  813. static void mxser_shutdown(struct tty_struct *tty)
  814. {
  815. struct mxser_port *info = tty->driver_data;
  816. unsigned long flags;
  817. if (!(info->port.flags & ASYNC_INITIALIZED))
  818. return;
  819. spin_lock_irqsave(&info->slock, flags);
  820. /*
  821. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  822. * here so the queue might never be waken up
  823. */
  824. wake_up_interruptible(&info->delta_msr_wait);
  825. /*
  826. * Free the IRQ, if necessary
  827. */
  828. if (info->port.xmit_buf) {
  829. free_page((unsigned long) info->port.xmit_buf);
  830. info->port.xmit_buf = NULL;
  831. }
  832. info->IER = 0;
  833. outb(0x00, info->ioaddr + UART_IER);
  834. if (tty->termios->c_cflag & HUPCL)
  835. info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
  836. outb(info->MCR, info->ioaddr + UART_MCR);
  837. /* clear Rx/Tx FIFO's */
  838. if (info->board->chip_flag)
  839. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
  840. MOXA_MUST_FCR_GDA_MODE_ENABLE,
  841. info->ioaddr + UART_FCR);
  842. else
  843. outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  844. info->ioaddr + UART_FCR);
  845. /* read data port to reset things */
  846. (void) inb(info->ioaddr + UART_RX);
  847. set_bit(TTY_IO_ERROR, &tty->flags);
  848. info->port.flags &= ~ASYNC_INITIALIZED;
  849. if (info->board->chip_flag)
  850. SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  851. spin_unlock_irqrestore(&info->slock, flags);
  852. }
  853. /*
  854. * This routine is called whenever a serial port is opened. It
  855. * enables interrupts for a serial port, linking in its async structure into
  856. * the IRQ chain. It also performs the serial-specific
  857. * initialization for the tty structure.
  858. */
  859. static int mxser_open(struct tty_struct *tty, struct file *filp)
  860. {
  861. struct mxser_port *info;
  862. unsigned long flags;
  863. int retval, line;
  864. line = tty->index;
  865. if (line == MXSER_PORTS)
  866. return 0;
  867. if (line < 0 || line > MXSER_PORTS)
  868. return -ENODEV;
  869. info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
  870. if (!info->ioaddr)
  871. return -ENODEV;
  872. tty->driver_data = info;
  873. tty_port_tty_set(&info->port, tty);
  874. /*
  875. * Start up serial port
  876. */
  877. spin_lock_irqsave(&info->port.lock, flags);
  878. info->port.count++;
  879. spin_unlock_irqrestore(&info->port.lock, flags);
  880. retval = mxser_startup(tty);
  881. if (retval)
  882. return retval;
  883. retval = tty_port_block_til_ready(&info->port, tty, filp);
  884. if (retval)
  885. return retval;
  886. /* unmark here for very high baud rate (ex. 921600 bps) used */
  887. tty->low_latency = 1;
  888. return 0;
  889. }
  890. static void mxser_flush_buffer(struct tty_struct *tty)
  891. {
  892. struct mxser_port *info = tty->driver_data;
  893. char fcr;
  894. unsigned long flags;
  895. spin_lock_irqsave(&info->slock, flags);
  896. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  897. fcr = inb(info->ioaddr + UART_FCR);
  898. outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  899. info->ioaddr + UART_FCR);
  900. outb(fcr, info->ioaddr + UART_FCR);
  901. spin_unlock_irqrestore(&info->slock, flags);
  902. tty_wakeup(tty);
  903. }
  904. /*
  905. * This routine is called when the serial port gets closed. First, we
  906. * wait for the last remaining data to be sent. Then, we unlink its
  907. * async structure from the interrupt chain if necessary, and we free
  908. * that IRQ if nothing is left in the chain.
  909. */
  910. static void mxser_close(struct tty_struct *tty, struct file *filp)
  911. {
  912. struct mxser_port *info = tty->driver_data;
  913. struct tty_port *port = &info->port;
  914. unsigned long timeout;
  915. if (tty->index == MXSER_PORTS)
  916. return;
  917. if (!info)
  918. return;
  919. if (tty_port_close_start(port, tty, filp) == 0)
  920. return;
  921. /*
  922. * Save the termios structure, since this port may have
  923. * separate termios for callout and dialin.
  924. *
  925. * FIXME: Can this go ?
  926. */
  927. if (info->port.flags & ASYNC_NORMAL_ACTIVE)
  928. info->normal_termios = *tty->termios;
  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->board->chip_flag)
  937. info->IER &= ~MOXA_MUST_RECV_ISR;
  938. if (info->port.flags & ASYNC_INITIALIZED) {
  939. outb(info->IER, info->ioaddr + UART_IER);
  940. /*
  941. * Before we drop DTR, make sure the UART transmitter
  942. * has completely drained; this is especially
  943. * important if there is a transmit FIFO!
  944. */
  945. timeout = jiffies + HZ;
  946. while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
  947. schedule_timeout_interruptible(5);
  948. if (time_after(jiffies, timeout))
  949. break;
  950. }
  951. }
  952. mxser_shutdown(tty);
  953. mxser_flush_buffer(tty);
  954. /* Right now the tty_port set is done outside of the close_end helper
  955. as we don't yet have everyone using refcounts */
  956. tty_port_close_end(port, tty);
  957. tty_port_tty_set(port, NULL);
  958. }
  959. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  960. {
  961. int c, total = 0;
  962. struct mxser_port *info = tty->driver_data;
  963. unsigned long flags;
  964. if (!info->port.xmit_buf)
  965. return 0;
  966. while (1) {
  967. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  968. SERIAL_XMIT_SIZE - info->xmit_head));
  969. if (c <= 0)
  970. break;
  971. memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
  972. spin_lock_irqsave(&info->slock, flags);
  973. info->xmit_head = (info->xmit_head + c) &
  974. (SERIAL_XMIT_SIZE - 1);
  975. info->xmit_cnt += c;
  976. spin_unlock_irqrestore(&info->slock, flags);
  977. buf += c;
  978. count -= c;
  979. total += c;
  980. }
  981. if (info->xmit_cnt && !tty->stopped) {
  982. if (!tty->hw_stopped ||
  983. (info->type == PORT_16550A) ||
  984. (info->board->chip_flag)) {
  985. spin_lock_irqsave(&info->slock, flags);
  986. outb(info->IER & ~UART_IER_THRI, info->ioaddr +
  987. UART_IER);
  988. info->IER |= UART_IER_THRI;
  989. outb(info->IER, info->ioaddr + UART_IER);
  990. spin_unlock_irqrestore(&info->slock, flags);
  991. }
  992. }
  993. return total;
  994. }
  995. static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
  996. {
  997. struct mxser_port *info = tty->driver_data;
  998. unsigned long flags;
  999. if (!info->port.xmit_buf)
  1000. return 0;
  1001. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  1002. return 0;
  1003. spin_lock_irqsave(&info->slock, flags);
  1004. info->port.xmit_buf[info->xmit_head++] = ch;
  1005. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  1006. info->xmit_cnt++;
  1007. spin_unlock_irqrestore(&info->slock, flags);
  1008. if (!tty->stopped) {
  1009. if (!tty->hw_stopped ||
  1010. (info->type == PORT_16550A) ||
  1011. info->board->chip_flag) {
  1012. spin_lock_irqsave(&info->slock, flags);
  1013. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1014. info->IER |= UART_IER_THRI;
  1015. outb(info->IER, info->ioaddr + UART_IER);
  1016. spin_unlock_irqrestore(&info->slock, flags);
  1017. }
  1018. }
  1019. return 1;
  1020. }
  1021. static void mxser_flush_chars(struct tty_struct *tty)
  1022. {
  1023. struct mxser_port *info = tty->driver_data;
  1024. unsigned long flags;
  1025. if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
  1026. (tty->hw_stopped && info->type != PORT_16550A &&
  1027. !info->board->chip_flag))
  1028. return;
  1029. spin_lock_irqsave(&info->slock, flags);
  1030. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1031. info->IER |= UART_IER_THRI;
  1032. outb(info->IER, info->ioaddr + UART_IER);
  1033. spin_unlock_irqrestore(&info->slock, flags);
  1034. }
  1035. static int mxser_write_room(struct tty_struct *tty)
  1036. {
  1037. struct mxser_port *info = tty->driver_data;
  1038. int ret;
  1039. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1040. return ret < 0 ? 0 : ret;
  1041. }
  1042. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1043. {
  1044. struct mxser_port *info = tty->driver_data;
  1045. return info->xmit_cnt;
  1046. }
  1047. /*
  1048. * ------------------------------------------------------------
  1049. * friends of mxser_ioctl()
  1050. * ------------------------------------------------------------
  1051. */
  1052. static int mxser_get_serial_info(struct tty_struct *tty,
  1053. struct serial_struct __user *retinfo)
  1054. {
  1055. struct mxser_port *info = tty->driver_data;
  1056. struct serial_struct tmp = {
  1057. .type = info->type,
  1058. .line = tty->index,
  1059. .port = info->ioaddr,
  1060. .irq = info->board->irq,
  1061. .flags = info->port.flags,
  1062. .baud_base = info->baud_base,
  1063. .close_delay = info->port.close_delay,
  1064. .closing_wait = info->port.closing_wait,
  1065. .custom_divisor = info->custom_divisor,
  1066. .hub6 = 0
  1067. };
  1068. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1069. return -EFAULT;
  1070. return 0;
  1071. }
  1072. static int mxser_set_serial_info(struct tty_struct *tty,
  1073. struct serial_struct __user *new_info)
  1074. {
  1075. struct mxser_port *info = tty->driver_data;
  1076. struct serial_struct new_serial;
  1077. speed_t baud;
  1078. unsigned long sl_flags;
  1079. unsigned int flags;
  1080. int retval = 0;
  1081. if (!new_info || !info->ioaddr)
  1082. return -ENODEV;
  1083. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1084. return -EFAULT;
  1085. if (new_serial.irq != info->board->irq ||
  1086. new_serial.port != info->ioaddr)
  1087. return -EINVAL;
  1088. flags = info->port.flags & ASYNC_SPD_MASK;
  1089. if (!capable(CAP_SYS_ADMIN)) {
  1090. if ((new_serial.baud_base != info->baud_base) ||
  1091. (new_serial.close_delay != info->port.close_delay) ||
  1092. ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
  1093. return -EPERM;
  1094. info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
  1095. (new_serial.flags & ASYNC_USR_MASK));
  1096. } else {
  1097. /*
  1098. * OK, past this point, all the error checking has been done.
  1099. * At this point, we start making changes.....
  1100. */
  1101. info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
  1102. (new_serial.flags & ASYNC_FLAGS));
  1103. info->port.close_delay = new_serial.close_delay * HZ / 100;
  1104. info->port.closing_wait = new_serial.closing_wait * HZ / 100;
  1105. tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY)
  1106. ? 1 : 0;
  1107. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
  1108. (new_serial.baud_base != info->baud_base ||
  1109. new_serial.custom_divisor !=
  1110. info->custom_divisor)) {
  1111. baud = new_serial.baud_base / new_serial.custom_divisor;
  1112. tty_encode_baud_rate(tty, baud, baud);
  1113. }
  1114. }
  1115. info->type = new_serial.type;
  1116. process_txrx_fifo(info);
  1117. if (info->port.flags & ASYNC_INITIALIZED) {
  1118. if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
  1119. spin_lock_irqsave(&info->slock, sl_flags);
  1120. mxser_change_speed(tty, NULL);
  1121. spin_unlock_irqrestore(&info->slock, sl_flags);
  1122. }
  1123. } else
  1124. retval = mxser_startup(tty);
  1125. return retval;
  1126. }
  1127. /*
  1128. * mxser_get_lsr_info - get line status register info
  1129. *
  1130. * Purpose: Let user call ioctl() to get info when the UART physically
  1131. * is emptied. On bus types like RS485, the transmitter must
  1132. * release the bus after transmitting. This must be done when
  1133. * the transmit shift register is empty, not be done when the
  1134. * transmit holding register is empty. This functionality
  1135. * allows an RS485 driver to be written in user space.
  1136. */
  1137. static int mxser_get_lsr_info(struct mxser_port *info,
  1138. unsigned int __user *value)
  1139. {
  1140. unsigned char status;
  1141. unsigned int result;
  1142. unsigned long flags;
  1143. spin_lock_irqsave(&info->slock, flags);
  1144. status = inb(info->ioaddr + UART_LSR);
  1145. spin_unlock_irqrestore(&info->slock, flags);
  1146. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1147. return put_user(result, value);
  1148. }
  1149. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  1150. {
  1151. struct mxser_port *info = tty->driver_data;
  1152. unsigned char control, status;
  1153. unsigned long flags;
  1154. if (tty->index == MXSER_PORTS)
  1155. return -ENOIOCTLCMD;
  1156. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1157. return -EIO;
  1158. control = info->MCR;
  1159. spin_lock_irqsave(&info->slock, flags);
  1160. status = inb(info->ioaddr + UART_MSR);
  1161. if (status & UART_MSR_ANY_DELTA)
  1162. mxser_check_modem_status(tty, info, status);
  1163. spin_unlock_irqrestore(&info->slock, flags);
  1164. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  1165. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  1166. ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  1167. ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
  1168. ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  1169. ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1170. }
  1171. static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
  1172. unsigned int set, unsigned int clear)
  1173. {
  1174. struct mxser_port *info = tty->driver_data;
  1175. unsigned long flags;
  1176. if (tty->index == MXSER_PORTS)
  1177. return -ENOIOCTLCMD;
  1178. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1179. return -EIO;
  1180. spin_lock_irqsave(&info->slock, flags);
  1181. if (set & TIOCM_RTS)
  1182. info->MCR |= UART_MCR_RTS;
  1183. if (set & TIOCM_DTR)
  1184. info->MCR |= UART_MCR_DTR;
  1185. if (clear & TIOCM_RTS)
  1186. info->MCR &= ~UART_MCR_RTS;
  1187. if (clear & TIOCM_DTR)
  1188. info->MCR &= ~UART_MCR_DTR;
  1189. outb(info->MCR, info->ioaddr + UART_MCR);
  1190. spin_unlock_irqrestore(&info->slock, flags);
  1191. return 0;
  1192. }
  1193. static int __init mxser_program_mode(int port)
  1194. {
  1195. int id, i, j, n;
  1196. outb(0, port);
  1197. outb(0, port);
  1198. outb(0, port);
  1199. (void)inb(port);
  1200. (void)inb(port);
  1201. outb(0, port);
  1202. (void)inb(port);
  1203. id = inb(port + 1) & 0x1F;
  1204. if ((id != C168_ASIC_ID) &&
  1205. (id != C104_ASIC_ID) &&
  1206. (id != C102_ASIC_ID) &&
  1207. (id != CI132_ASIC_ID) &&
  1208. (id != CI134_ASIC_ID) &&
  1209. (id != CI104J_ASIC_ID))
  1210. return -1;
  1211. for (i = 0, j = 0; i < 4; i++) {
  1212. n = inb(port + 2);
  1213. if (n == 'M') {
  1214. j = 1;
  1215. } else if ((j == 1) && (n == 1)) {
  1216. j = 2;
  1217. break;
  1218. } else
  1219. j = 0;
  1220. }
  1221. if (j != 2)
  1222. id = -2;
  1223. return id;
  1224. }
  1225. static void __init mxser_normal_mode(int port)
  1226. {
  1227. int i, n;
  1228. outb(0xA5, port + 1);
  1229. outb(0x80, port + 3);
  1230. outb(12, port + 0); /* 9600 bps */
  1231. outb(0, port + 1);
  1232. outb(0x03, port + 3); /* 8 data bits */
  1233. outb(0x13, port + 4); /* loop back mode */
  1234. for (i = 0; i < 16; i++) {
  1235. n = inb(port + 5);
  1236. if ((n & 0x61) == 0x60)
  1237. break;
  1238. if ((n & 1) == 1)
  1239. (void)inb(port);
  1240. }
  1241. outb(0x00, port + 4);
  1242. }
  1243. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  1244. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  1245. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  1246. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  1247. #define EN_CCMD 0x000 /* Chip's command register */
  1248. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  1249. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  1250. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  1251. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  1252. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  1253. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  1254. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  1255. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  1256. static int __init mxser_read_register(int port, unsigned short *regs)
  1257. {
  1258. int i, k, value, id;
  1259. unsigned int j;
  1260. id = mxser_program_mode(port);
  1261. if (id < 0)
  1262. return id;
  1263. for (i = 0; i < 14; i++) {
  1264. k = (i & 0x3F) | 0x180;
  1265. for (j = 0x100; j > 0; j >>= 1) {
  1266. outb(CHIP_CS, port);
  1267. if (k & j) {
  1268. outb(CHIP_CS | CHIP_DO, port);
  1269. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  1270. } else {
  1271. outb(CHIP_CS, port);
  1272. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  1273. }
  1274. }
  1275. (void)inb(port);
  1276. value = 0;
  1277. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  1278. outb(CHIP_CS, port);
  1279. outb(CHIP_CS | CHIP_SK, port);
  1280. if (inb(port) & CHIP_DI)
  1281. value |= j;
  1282. }
  1283. regs[i] = value;
  1284. outb(0, port);
  1285. }
  1286. mxser_normal_mode(port);
  1287. return id;
  1288. }
  1289. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1290. {
  1291. struct mxser_port *port;
  1292. struct tty_struct *tty;
  1293. int result, status;
  1294. unsigned int i, j;
  1295. int ret = 0;
  1296. switch (cmd) {
  1297. case MOXA_GET_MAJOR:
  1298. if (printk_ratelimit())
  1299. printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
  1300. "%x (GET_MAJOR), fix your userspace\n",
  1301. current->comm, cmd);
  1302. return put_user(ttymajor, (int __user *)argp);
  1303. case MOXA_CHKPORTENABLE:
  1304. result = 0;
  1305. lock_kernel();
  1306. for (i = 0; i < MXSER_BOARDS; i++)
  1307. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
  1308. if (mxser_boards[i].ports[j].ioaddr)
  1309. result |= (1 << i);
  1310. unlock_kernel();
  1311. return put_user(result, (unsigned long __user *)argp);
  1312. case MOXA_GETDATACOUNT:
  1313. lock_kernel();
  1314. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1315. ret = -EFAULT;
  1316. unlock_kernel();
  1317. return ret;
  1318. case MOXA_GETMSTATUS: {
  1319. struct mxser_mstatus ms, __user *msu = argp;
  1320. lock_kernel();
  1321. for (i = 0; i < MXSER_BOARDS; i++)
  1322. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1323. port = &mxser_boards[i].ports[j];
  1324. memset(&ms, 0, sizeof(ms));
  1325. if (!port->ioaddr)
  1326. goto copy;
  1327. tty = tty_port_tty_get(&port->port);
  1328. if (!tty || !tty->termios)
  1329. ms.cflag = port->normal_termios.c_cflag;
  1330. else
  1331. ms.cflag = tty->termios->c_cflag;
  1332. tty_kref_put(tty);
  1333. status = inb(port->ioaddr + UART_MSR);
  1334. if (status & UART_MSR_DCD)
  1335. ms.dcd = 1;
  1336. if (status & UART_MSR_DSR)
  1337. ms.dsr = 1;
  1338. if (status & UART_MSR_CTS)
  1339. ms.cts = 1;
  1340. copy:
  1341. if (copy_to_user(msu, &ms, sizeof(ms))) {
  1342. unlock_kernel();
  1343. return -EFAULT;
  1344. }
  1345. msu++;
  1346. }
  1347. unlock_kernel();
  1348. return 0;
  1349. }
  1350. case MOXA_ASPP_MON_EXT: {
  1351. struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
  1352. unsigned int cflag, iflag, p;
  1353. u8 opmode;
  1354. me = kzalloc(sizeof(*me), GFP_KERNEL);
  1355. if (!me)
  1356. return -ENOMEM;
  1357. lock_kernel();
  1358. for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
  1359. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
  1360. if (p >= ARRAY_SIZE(me->rx_cnt)) {
  1361. i = MXSER_BOARDS;
  1362. break;
  1363. }
  1364. port = &mxser_boards[i].ports[j];
  1365. if (!port->ioaddr)
  1366. continue;
  1367. status = mxser_get_msr(port->ioaddr, 0, p);
  1368. if (status & UART_MSR_TERI)
  1369. port->icount.rng++;
  1370. if (status & UART_MSR_DDSR)
  1371. port->icount.dsr++;
  1372. if (status & UART_MSR_DDCD)
  1373. port->icount.dcd++;
  1374. if (status & UART_MSR_DCTS)
  1375. port->icount.cts++;
  1376. port->mon_data.modem_status = status;
  1377. me->rx_cnt[p] = port->mon_data.rxcnt;
  1378. me->tx_cnt[p] = port->mon_data.txcnt;
  1379. me->up_rxcnt[p] = port->mon_data.up_rxcnt;
  1380. me->up_txcnt[p] = port->mon_data.up_txcnt;
  1381. me->modem_status[p] =
  1382. port->mon_data.modem_status;
  1383. tty = tty_port_tty_get(&port->port);
  1384. if (!tty || !tty->termios) {
  1385. cflag = port->normal_termios.c_cflag;
  1386. iflag = port->normal_termios.c_iflag;
  1387. me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios);
  1388. } else {
  1389. cflag = tty->termios->c_cflag;
  1390. iflag = tty->termios->c_iflag;
  1391. me->baudrate[p] = tty_get_baud_rate(tty);
  1392. }
  1393. tty_kref_put(tty);
  1394. me->databits[p] = cflag & CSIZE;
  1395. me->stopbits[p] = cflag & CSTOPB;
  1396. me->parity[p] = cflag & (PARENB | PARODD |
  1397. CMSPAR);
  1398. if (cflag & CRTSCTS)
  1399. me->flowctrl[p] |= 0x03;
  1400. if (iflag & (IXON | IXOFF))
  1401. me->flowctrl[p] |= 0x0C;
  1402. if (port->type == PORT_16550A)
  1403. me->fifo[p] = 1;
  1404. opmode = inb(port->opmode_ioaddr) >>
  1405. ((p % 4) * 2);
  1406. opmode &= OP_MODE_MASK;
  1407. me->iftype[p] = opmode;
  1408. }
  1409. }
  1410. unlock_kernel();
  1411. if (copy_to_user(argp, me, sizeof(*me)))
  1412. ret = -EFAULT;
  1413. kfree(me);
  1414. return ret;
  1415. }
  1416. default:
  1417. return -ENOIOCTLCMD;
  1418. }
  1419. return 0;
  1420. }
  1421. static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
  1422. struct async_icount *cprev)
  1423. {
  1424. struct async_icount cnow;
  1425. unsigned long flags;
  1426. int ret;
  1427. spin_lock_irqsave(&info->slock, flags);
  1428. cnow = info->icount; /* atomic copy */
  1429. spin_unlock_irqrestore(&info->slock, flags);
  1430. ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  1431. ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  1432. ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  1433. ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1434. *cprev = cnow;
  1435. return ret;
  1436. }
  1437. static int mxser_ioctl(struct tty_struct *tty, struct file *file,
  1438. unsigned int cmd, unsigned long arg)
  1439. {
  1440. struct mxser_port *info = tty->driver_data;
  1441. struct async_icount cnow;
  1442. unsigned long flags;
  1443. void __user *argp = (void __user *)arg;
  1444. int retval;
  1445. if (tty->index == MXSER_PORTS)
  1446. return mxser_ioctl_special(cmd, argp);
  1447. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1448. int p;
  1449. unsigned long opmode;
  1450. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1451. int shiftbit;
  1452. unsigned char val, mask;
  1453. p = tty->index % 4;
  1454. if (cmd == MOXA_SET_OP_MODE) {
  1455. if (get_user(opmode, (int __user *) argp))
  1456. return -EFAULT;
  1457. if (opmode != RS232_MODE &&
  1458. opmode != RS485_2WIRE_MODE &&
  1459. opmode != RS422_MODE &&
  1460. opmode != RS485_4WIRE_MODE)
  1461. return -EFAULT;
  1462. lock_kernel();
  1463. mask = ModeMask[p];
  1464. shiftbit = p * 2;
  1465. val = inb(info->opmode_ioaddr);
  1466. val &= mask;
  1467. val |= (opmode << shiftbit);
  1468. outb(val, info->opmode_ioaddr);
  1469. unlock_kernel();
  1470. } else {
  1471. lock_kernel();
  1472. shiftbit = p * 2;
  1473. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1474. opmode &= OP_MODE_MASK;
  1475. unlock_kernel();
  1476. if (put_user(opmode, (int __user *)argp))
  1477. return -EFAULT;
  1478. }
  1479. return 0;
  1480. }
  1481. if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
  1482. test_bit(TTY_IO_ERROR, &tty->flags))
  1483. return -EIO;
  1484. switch (cmd) {
  1485. case TIOCGSERIAL:
  1486. lock_kernel();
  1487. retval = mxser_get_serial_info(tty, argp);
  1488. unlock_kernel();
  1489. return retval;
  1490. case TIOCSSERIAL:
  1491. lock_kernel();
  1492. retval = mxser_set_serial_info(tty, argp);
  1493. unlock_kernel();
  1494. return retval;
  1495. case TIOCSERGETLSR: /* Get line status register */
  1496. return mxser_get_lsr_info(info, argp);
  1497. /*
  1498. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1499. * - mask passed in arg for lines of interest
  1500. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1501. * Caller should use TIOCGICOUNT to see which one it was
  1502. */
  1503. case TIOCMIWAIT:
  1504. spin_lock_irqsave(&info->slock, flags);
  1505. cnow = info->icount; /* note the counters on entry */
  1506. spin_unlock_irqrestore(&info->slock, flags);
  1507. return wait_event_interruptible(info->delta_msr_wait,
  1508. mxser_cflags_changed(info, arg, &cnow));
  1509. /*
  1510. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1511. * Return: write counters to the user passed counter struct
  1512. * NB: both 1->0 and 0->1 transitions are counted except for
  1513. * RI where only 0->1 is counted.
  1514. */
  1515. case TIOCGICOUNT: {
  1516. struct serial_icounter_struct icnt = { 0 };
  1517. spin_lock_irqsave(&info->slock, flags);
  1518. cnow = info->icount;
  1519. spin_unlock_irqrestore(&info->slock, flags);
  1520. icnt.frame = cnow.frame;
  1521. icnt.brk = cnow.brk;
  1522. icnt.overrun = cnow.overrun;
  1523. icnt.buf_overrun = cnow.buf_overrun;
  1524. icnt.parity = cnow.parity;
  1525. icnt.rx = cnow.rx;
  1526. icnt.tx = cnow.tx;
  1527. icnt.cts = cnow.cts;
  1528. icnt.dsr = cnow.dsr;
  1529. icnt.rng = cnow.rng;
  1530. icnt.dcd = cnow.dcd;
  1531. return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
  1532. }
  1533. case MOXA_HighSpeedOn:
  1534. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
  1535. case MOXA_SDS_RSTICOUNTER:
  1536. lock_kernel();
  1537. info->mon_data.rxcnt = 0;
  1538. info->mon_data.txcnt = 0;
  1539. unlock_kernel();
  1540. return 0;
  1541. case MOXA_ASPP_OQUEUE:{
  1542. int len, lsr;
  1543. lock_kernel();
  1544. len = mxser_chars_in_buffer(tty);
  1545. lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
  1546. len += (lsr ? 0 : 1);
  1547. unlock_kernel();
  1548. return put_user(len, (int __user *)argp);
  1549. }
  1550. case MOXA_ASPP_MON: {
  1551. int mcr, status;
  1552. lock_kernel();
  1553. status = mxser_get_msr(info->ioaddr, 1, tty->index);
  1554. mxser_check_modem_status(tty, info, status);
  1555. mcr = inb(info->ioaddr + UART_MCR);
  1556. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1557. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1558. else
  1559. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1560. if (mcr & MOXA_MUST_MCR_TX_XON)
  1561. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1562. else
  1563. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1564. if (tty->hw_stopped)
  1565. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1566. else
  1567. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1568. unlock_kernel();
  1569. if (copy_to_user(argp, &info->mon_data,
  1570. sizeof(struct mxser_mon)))
  1571. return -EFAULT;
  1572. return 0;
  1573. }
  1574. case MOXA_ASPP_LSTATUS: {
  1575. if (put_user(info->err_shadow, (unsigned char __user *)argp))
  1576. return -EFAULT;
  1577. info->err_shadow = 0;
  1578. return 0;
  1579. }
  1580. case MOXA_SET_BAUD_METHOD: {
  1581. int method;
  1582. if (get_user(method, (int __user *)argp))
  1583. return -EFAULT;
  1584. mxser_set_baud_method[tty->index] = method;
  1585. return put_user(method, (int __user *)argp);
  1586. }
  1587. default:
  1588. return -ENOIOCTLCMD;
  1589. }
  1590. return 0;
  1591. }
  1592. static void mxser_stoprx(struct tty_struct *tty)
  1593. {
  1594. struct mxser_port *info = tty->driver_data;
  1595. info->ldisc_stop_rx = 1;
  1596. if (I_IXOFF(tty)) {
  1597. if (info->board->chip_flag) {
  1598. info->IER &= ~MOXA_MUST_RECV_ISR;
  1599. outb(info->IER, info->ioaddr + UART_IER);
  1600. } else {
  1601. info->x_char = STOP_CHAR(tty);
  1602. outb(0, info->ioaddr + UART_IER);
  1603. info->IER |= UART_IER_THRI;
  1604. outb(info->IER, info->ioaddr + UART_IER);
  1605. }
  1606. }
  1607. if (tty->termios->c_cflag & CRTSCTS) {
  1608. info->MCR &= ~UART_MCR_RTS;
  1609. outb(info->MCR, info->ioaddr + UART_MCR);
  1610. }
  1611. }
  1612. /*
  1613. * This routine is called by the upper-layer tty layer to signal that
  1614. * incoming characters should be throttled.
  1615. */
  1616. static void mxser_throttle(struct tty_struct *tty)
  1617. {
  1618. mxser_stoprx(tty);
  1619. }
  1620. static void mxser_unthrottle(struct tty_struct *tty)
  1621. {
  1622. struct mxser_port *info = tty->driver_data;
  1623. /* startrx */
  1624. info->ldisc_stop_rx = 0;
  1625. if (I_IXOFF(tty)) {
  1626. if (info->x_char)
  1627. info->x_char = 0;
  1628. else {
  1629. if (info->board->chip_flag) {
  1630. info->IER |= MOXA_MUST_RECV_ISR;
  1631. outb(info->IER, info->ioaddr + UART_IER);
  1632. } else {
  1633. info->x_char = START_CHAR(tty);
  1634. outb(0, info->ioaddr + UART_IER);
  1635. info->IER |= UART_IER_THRI;
  1636. outb(info->IER, info->ioaddr + UART_IER);
  1637. }
  1638. }
  1639. }
  1640. if (tty->termios->c_cflag & CRTSCTS) {
  1641. info->MCR |= UART_MCR_RTS;
  1642. outb(info->MCR, info->ioaddr + UART_MCR);
  1643. }
  1644. }
  1645. /*
  1646. * mxser_stop() and mxser_start()
  1647. *
  1648. * This routines are called before setting or resetting tty->stopped.
  1649. * They enable or disable transmitter interrupts, as necessary.
  1650. */
  1651. static void mxser_stop(struct tty_struct *tty)
  1652. {
  1653. struct mxser_port *info = tty->driver_data;
  1654. unsigned long flags;
  1655. spin_lock_irqsave(&info->slock, flags);
  1656. if (info->IER & UART_IER_THRI) {
  1657. info->IER &= ~UART_IER_THRI;
  1658. outb(info->IER, info->ioaddr + UART_IER);
  1659. }
  1660. spin_unlock_irqrestore(&info->slock, flags);
  1661. }
  1662. static void mxser_start(struct tty_struct *tty)
  1663. {
  1664. struct mxser_port *info = tty->driver_data;
  1665. unsigned long flags;
  1666. spin_lock_irqsave(&info->slock, flags);
  1667. if (info->xmit_cnt && info->port.xmit_buf) {
  1668. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1669. info->IER |= UART_IER_THRI;
  1670. outb(info->IER, info->ioaddr + UART_IER);
  1671. }
  1672. spin_unlock_irqrestore(&info->slock, flags);
  1673. }
  1674. static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1675. {
  1676. struct mxser_port *info = tty->driver_data;
  1677. unsigned long flags;
  1678. spin_lock_irqsave(&info->slock, flags);
  1679. mxser_change_speed(tty, old_termios);
  1680. spin_unlock_irqrestore(&info->slock, flags);
  1681. if ((old_termios->c_cflag & CRTSCTS) &&
  1682. !(tty->termios->c_cflag & CRTSCTS)) {
  1683. tty->hw_stopped = 0;
  1684. mxser_start(tty);
  1685. }
  1686. /* Handle sw stopped */
  1687. if ((old_termios->c_iflag & IXON) &&
  1688. !(tty->termios->c_iflag & IXON)) {
  1689. tty->stopped = 0;
  1690. if (info->board->chip_flag) {
  1691. spin_lock_irqsave(&info->slock, flags);
  1692. mxser_disable_must_rx_software_flow_control(
  1693. info->ioaddr);
  1694. spin_unlock_irqrestore(&info->slock, flags);
  1695. }
  1696. mxser_start(tty);
  1697. }
  1698. }
  1699. /*
  1700. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1701. */
  1702. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1703. {
  1704. struct mxser_port *info = tty->driver_data;
  1705. unsigned long orig_jiffies, char_time;
  1706. int lsr;
  1707. if (info->type == PORT_UNKNOWN)
  1708. return;
  1709. if (info->xmit_fifo_size == 0)
  1710. return; /* Just in case.... */
  1711. orig_jiffies = jiffies;
  1712. /*
  1713. * Set the check interval to be 1/5 of the estimated time to
  1714. * send a single character, and make it at least 1. The check
  1715. * interval should also be less than the timeout.
  1716. *
  1717. * Note: we have to use pretty tight timings here to satisfy
  1718. * the NIST-PCTS.
  1719. */
  1720. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1721. char_time = char_time / 5;
  1722. if (char_time == 0)
  1723. char_time = 1;
  1724. if (timeout && timeout < char_time)
  1725. char_time = timeout;
  1726. /*
  1727. * If the transmitter hasn't cleared in twice the approximate
  1728. * amount of time to send the entire FIFO, it probably won't
  1729. * ever clear. This assumes the UART isn't doing flow
  1730. * control, which is currently the case. Hence, if it ever
  1731. * takes longer than info->timeout, this is probably due to a
  1732. * UART bug of some kind. So, we clamp the timeout parameter at
  1733. * 2*info->timeout.
  1734. */
  1735. if (!timeout || timeout > 2 * info->timeout)
  1736. timeout = 2 * info->timeout;
  1737. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1738. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
  1739. timeout, char_time);
  1740. printk("jiff=%lu...", jiffies);
  1741. #endif
  1742. lock_kernel();
  1743. while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
  1744. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1745. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1746. #endif
  1747. schedule_timeout_interruptible(char_time);
  1748. if (signal_pending(current))
  1749. break;
  1750. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1751. break;
  1752. }
  1753. set_current_state(TASK_RUNNING);
  1754. unlock_kernel();
  1755. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1756. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1757. #endif
  1758. }
  1759. /*
  1760. * This routine is called by tty_hangup() when a hangup is signaled.
  1761. */
  1762. static void mxser_hangup(struct tty_struct *tty)
  1763. {
  1764. struct mxser_port *info = tty->driver_data;
  1765. mxser_flush_buffer(tty);
  1766. mxser_shutdown(tty);
  1767. tty_port_hangup(&info->port);
  1768. }
  1769. /*
  1770. * mxser_rs_break() --- routine which turns the break handling on or off
  1771. */
  1772. static int mxser_rs_break(struct tty_struct *tty, int break_state)
  1773. {
  1774. struct mxser_port *info = tty->driver_data;
  1775. unsigned long flags;
  1776. spin_lock_irqsave(&info->slock, flags);
  1777. if (break_state == -1)
  1778. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1779. info->ioaddr + UART_LCR);
  1780. else
  1781. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1782. info->ioaddr + UART_LCR);
  1783. spin_unlock_irqrestore(&info->slock, flags);
  1784. return 0;
  1785. }
  1786. static void mxser_receive_chars(struct tty_struct *tty,
  1787. struct mxser_port *port, int *status)
  1788. {
  1789. unsigned char ch, gdl;
  1790. int ignored = 0;
  1791. int cnt = 0;
  1792. int recv_room;
  1793. int max = 256;
  1794. recv_room = tty->receive_room;
  1795. if (recv_room == 0 && !port->ldisc_stop_rx)
  1796. mxser_stoprx(tty);
  1797. if (port->board->chip_flag != MOXA_OTHER_UART) {
  1798. if (*status & UART_LSR_SPECIAL)
  1799. goto intr_old;
  1800. if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
  1801. (*status & MOXA_MUST_LSR_RERR))
  1802. goto intr_old;
  1803. if (*status & MOXA_MUST_LSR_RERR)
  1804. goto intr_old;
  1805. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1806. if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
  1807. gdl &= MOXA_MUST_GDL_MASK;
  1808. if (gdl >= recv_room) {
  1809. if (!port->ldisc_stop_rx)
  1810. mxser_stoprx(tty);
  1811. }
  1812. while (gdl--) {
  1813. ch = inb(port->ioaddr + UART_RX);
  1814. tty_insert_flip_char(tty, ch, 0);
  1815. cnt++;
  1816. }
  1817. goto end_intr;
  1818. }
  1819. intr_old:
  1820. do {
  1821. if (max-- < 0)
  1822. break;
  1823. ch = inb(port->ioaddr + UART_RX);
  1824. if (port->board->chip_flag && (*status & UART_LSR_OE))
  1825. outb(0x23, port->ioaddr + UART_FCR);
  1826. *status &= port->read_status_mask;
  1827. if (*status & port->ignore_status_mask) {
  1828. if (++ignored > 100)
  1829. break;
  1830. } else {
  1831. char flag = 0;
  1832. if (*status & UART_LSR_SPECIAL) {
  1833. if (*status & UART_LSR_BI) {
  1834. flag = TTY_BREAK;
  1835. port->icount.brk++;
  1836. if (port->port.flags & ASYNC_SAK)
  1837. do_SAK(tty);
  1838. } else if (*status & UART_LSR_PE) {
  1839. flag = TTY_PARITY;
  1840. port->icount.parity++;
  1841. } else if (*status & UART_LSR_FE) {
  1842. flag = TTY_FRAME;
  1843. port->icount.frame++;
  1844. } else if (*status & UART_LSR_OE) {
  1845. flag = TTY_OVERRUN;
  1846. port->icount.overrun++;
  1847. } else
  1848. flag = TTY_BREAK;
  1849. }
  1850. tty_insert_flip_char(tty, ch, flag);
  1851. cnt++;
  1852. if (cnt >= recv_room) {
  1853. if (!port->ldisc_stop_rx)
  1854. mxser_stoprx(tty);
  1855. break;
  1856. }
  1857. }
  1858. if (port->board->chip_flag)
  1859. break;
  1860. *status = inb(port->ioaddr + UART_LSR);
  1861. } while (*status & UART_LSR_DR);
  1862. end_intr:
  1863. mxvar_log.rxcnt[tty->index] += cnt;
  1864. port->mon_data.rxcnt += cnt;
  1865. port->mon_data.up_rxcnt += cnt;
  1866. /*
  1867. * We are called from an interrupt context with &port->slock
  1868. * being held. Drop it temporarily in order to prevent
  1869. * recursive locking.
  1870. */
  1871. spin_unlock(&port->slock);
  1872. tty_flip_buffer_push(tty);
  1873. spin_lock(&port->slock);
  1874. }
  1875. static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
  1876. {
  1877. int count, cnt;
  1878. if (port->x_char) {
  1879. outb(port->x_char, port->ioaddr + UART_TX);
  1880. port->x_char = 0;
  1881. mxvar_log.txcnt[tty->index]++;
  1882. port->mon_data.txcnt++;
  1883. port->mon_data.up_txcnt++;
  1884. port->icount.tx++;
  1885. return;
  1886. }
  1887. if (port->port.xmit_buf == NULL)
  1888. return;
  1889. if (port->xmit_cnt <= 0 || tty->stopped ||
  1890. (tty->hw_stopped &&
  1891. (port->type != PORT_16550A) &&
  1892. (!port->board->chip_flag))) {
  1893. port->IER &= ~UART_IER_THRI;
  1894. outb(port->IER, port->ioaddr + UART_IER);
  1895. return;
  1896. }
  1897. cnt = port->xmit_cnt;
  1898. count = port->xmit_fifo_size;
  1899. do {
  1900. outb(port->port.xmit_buf[port->xmit_tail++],
  1901. port->ioaddr + UART_TX);
  1902. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  1903. if (--port->xmit_cnt <= 0)
  1904. break;
  1905. } while (--count > 0);
  1906. mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
  1907. port->mon_data.txcnt += (cnt - port->xmit_cnt);
  1908. port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
  1909. port->icount.tx += (cnt - port->xmit_cnt);
  1910. if (port->xmit_cnt < WAKEUP_CHARS && tty)
  1911. tty_wakeup(tty);
  1912. if (port->xmit_cnt <= 0) {
  1913. port->IER &= ~UART_IER_THRI;
  1914. outb(port->IER, port->ioaddr + UART_IER);
  1915. }
  1916. }
  1917. /*
  1918. * This is the serial driver's generic interrupt routine
  1919. */
  1920. static irqreturn_t mxser_interrupt(int irq, void *dev_id)
  1921. {
  1922. int status, iir, i;
  1923. struct mxser_board *brd = NULL;
  1924. struct mxser_port *port;
  1925. int max, irqbits, bits, msr;
  1926. unsigned int int_cnt, pass_counter = 0;
  1927. int handled = IRQ_NONE;
  1928. struct tty_struct *tty;
  1929. for (i = 0; i < MXSER_BOARDS; i++)
  1930. if (dev_id == &mxser_boards[i]) {
  1931. brd = dev_id;
  1932. break;
  1933. }
  1934. if (i == MXSER_BOARDS)
  1935. goto irq_stop;
  1936. if (brd == NULL)
  1937. goto irq_stop;
  1938. max = brd->info->nports;
  1939. while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
  1940. irqbits = inb(brd->vector) & brd->vector_mask;
  1941. if (irqbits == brd->vector_mask)
  1942. break;
  1943. handled = IRQ_HANDLED;
  1944. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  1945. if (irqbits == brd->vector_mask)
  1946. break;
  1947. if (bits & irqbits)
  1948. continue;
  1949. port = &brd->ports[i];
  1950. int_cnt = 0;
  1951. spin_lock(&port->slock);
  1952. do {
  1953. iir = inb(port->ioaddr + UART_IIR);
  1954. if (iir & UART_IIR_NO_INT)
  1955. break;
  1956. iir &= MOXA_MUST_IIR_MASK;
  1957. tty = tty_port_tty_get(&port->port);
  1958. if (!tty ||
  1959. (port->port.flags & ASYNC_CLOSING) ||
  1960. !(port->port.flags &
  1961. ASYNC_INITIALIZED)) {
  1962. status = inb(port->ioaddr + UART_LSR);
  1963. outb(0x27, port->ioaddr + UART_FCR);
  1964. inb(port->ioaddr + UART_MSR);
  1965. tty_kref_put(tty);
  1966. break;
  1967. }
  1968. status = inb(port->ioaddr + UART_LSR);
  1969. if (status & UART_LSR_PE)
  1970. port->err_shadow |= NPPI_NOTIFY_PARITY;
  1971. if (status & UART_LSR_FE)
  1972. port->err_shadow |= NPPI_NOTIFY_FRAMING;
  1973. if (status & UART_LSR_OE)
  1974. port->err_shadow |=
  1975. NPPI_NOTIFY_HW_OVERRUN;
  1976. if (status & UART_LSR_BI)
  1977. port->err_shadow |= NPPI_NOTIFY_BREAK;
  1978. if (port->board->chip_flag) {
  1979. if (iir == MOXA_MUST_IIR_GDA ||
  1980. iir == MOXA_MUST_IIR_RDA ||
  1981. iir == MOXA_MUST_IIR_RTO ||
  1982. iir == MOXA_MUST_IIR_LSR)
  1983. mxser_receive_chars(tty, port,
  1984. &status);
  1985. } else {
  1986. status &= port->read_status_mask;
  1987. if (status & UART_LSR_DR)
  1988. mxser_receive_chars(tty, port,
  1989. &status);
  1990. }
  1991. msr = inb(port->ioaddr + UART_MSR);
  1992. if (msr & UART_MSR_ANY_DELTA)
  1993. mxser_check_modem_status(tty, port, msr);
  1994. if (port->board->chip_flag) {
  1995. if (iir == 0x02 && (status &
  1996. UART_LSR_THRE))
  1997. mxser_transmit_chars(tty, port);
  1998. } else {
  1999. if (status & UART_LSR_THRE)
  2000. mxser_transmit_chars(tty, port);
  2001. }
  2002. tty_kref_put(tty);
  2003. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  2004. spin_unlock(&port->slock);
  2005. }
  2006. }
  2007. irq_stop:
  2008. return handled;
  2009. }
  2010. static const struct tty_operations mxser_ops = {
  2011. .open = mxser_open,
  2012. .close = mxser_close,
  2013. .write = mxser_write,
  2014. .put_char = mxser_put_char,
  2015. .flush_chars = mxser_flush_chars,
  2016. .write_room = mxser_write_room,
  2017. .chars_in_buffer = mxser_chars_in_buffer,
  2018. .flush_buffer = mxser_flush_buffer,
  2019. .ioctl = mxser_ioctl,
  2020. .throttle = mxser_throttle,
  2021. .unthrottle = mxser_unthrottle,
  2022. .set_termios = mxser_set_termios,
  2023. .stop = mxser_stop,
  2024. .start = mxser_start,
  2025. .hangup = mxser_hangup,
  2026. .break_ctl = mxser_rs_break,
  2027. .wait_until_sent = mxser_wait_until_sent,
  2028. .tiocmget = mxser_tiocmget,
  2029. .tiocmset = mxser_tiocmset,
  2030. };
  2031. struct tty_port_operations mxser_port_ops = {
  2032. .carrier_raised = mxser_carrier_raised,
  2033. .raise_dtr_rts = mxser_raise_dtr_rts,
  2034. };
  2035. /*
  2036. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  2037. */
  2038. static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
  2039. unsigned int irq)
  2040. {
  2041. if (irq)
  2042. free_irq(brd->irq, brd);
  2043. if (pdev != NULL) { /* PCI */
  2044. #ifdef CONFIG_PCI
  2045. pci_release_region(pdev, 2);
  2046. pci_release_region(pdev, 3);
  2047. #endif
  2048. } else {
  2049. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2050. release_region(brd->vector, 1);
  2051. }
  2052. }
  2053. static int __devinit mxser_initbrd(struct mxser_board *brd,
  2054. struct pci_dev *pdev)
  2055. {
  2056. struct mxser_port *info;
  2057. unsigned int i;
  2058. int retval;
  2059. printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
  2060. brd->ports[0].max_baud);
  2061. for (i = 0; i < brd->info->nports; i++) {
  2062. info = &brd->ports[i];
  2063. tty_port_init(&info->port);
  2064. info->port.ops = &mxser_port_ops;
  2065. info->board = brd;
  2066. info->stop_rx = 0;
  2067. info->ldisc_stop_rx = 0;
  2068. /* Enhance mode enabled here */
  2069. if (brd->chip_flag != MOXA_OTHER_UART)
  2070. mxser_enable_must_enchance_mode(info->ioaddr);
  2071. info->port.flags = ASYNC_SHARE_IRQ;
  2072. info->type = brd->uart_type;
  2073. process_txrx_fifo(info);
  2074. info->custom_divisor = info->baud_base * 16;
  2075. info->port.close_delay = 5 * HZ / 10;
  2076. info->port.closing_wait = 30 * HZ;
  2077. info->normal_termios = mxvar_sdriver->init_termios;
  2078. init_waitqueue_head(&info->delta_msr_wait);
  2079. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  2080. info->err_shadow = 0;
  2081. spin_lock_init(&info->slock);
  2082. /* before set INT ISR, disable all int */
  2083. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  2084. info->ioaddr + UART_IER);
  2085. }
  2086. retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
  2087. brd);
  2088. if (retval) {
  2089. printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
  2090. "conflict with another device.\n",
  2091. brd->info->name, brd->irq);
  2092. /* We hold resources, we need to release them. */
  2093. mxser_release_res(brd, pdev, 0);
  2094. }
  2095. return retval;
  2096. }
  2097. static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
  2098. {
  2099. int id, i, bits;
  2100. unsigned short regs[16], irq;
  2101. unsigned char scratch, scratch2;
  2102. brd->chip_flag = MOXA_OTHER_UART;
  2103. id = mxser_read_register(cap, regs);
  2104. switch (id) {
  2105. case C168_ASIC_ID:
  2106. brd->info = &mxser_cards[0];
  2107. break;
  2108. case C104_ASIC_ID:
  2109. brd->info = &mxser_cards[1];
  2110. break;
  2111. case CI104J_ASIC_ID:
  2112. brd->info = &mxser_cards[2];
  2113. break;
  2114. case C102_ASIC_ID:
  2115. brd->info = &mxser_cards[5];
  2116. break;
  2117. case CI132_ASIC_ID:
  2118. brd->info = &mxser_cards[6];
  2119. break;
  2120. case CI134_ASIC_ID:
  2121. brd->info = &mxser_cards[7];
  2122. break;
  2123. default:
  2124. return 0;
  2125. }
  2126. irq = 0;
  2127. /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
  2128. Flag-hack checks if configuration should be read as 2-port here. */
  2129. if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
  2130. irq = regs[9] & 0xF000;
  2131. irq = irq | (irq >> 4);
  2132. if (irq != (regs[9] & 0xFF00))
  2133. goto err_irqconflict;
  2134. } else if (brd->info->nports == 4) {
  2135. irq = regs[9] & 0xF000;
  2136. irq = irq | (irq >> 4);
  2137. irq = irq | (irq >> 8);
  2138. if (irq != regs[9])
  2139. goto err_irqconflict;
  2140. } else if (brd->info->nports == 8) {
  2141. irq = regs[9] & 0xF000;
  2142. irq = irq | (irq >> 4);
  2143. irq = irq | (irq >> 8);
  2144. if ((irq != regs[9]) || (irq != regs[10]))
  2145. goto err_irqconflict;
  2146. }
  2147. if (!irq) {
  2148. printk(KERN_ERR "mxser: interrupt number unset\n");
  2149. return -EIO;
  2150. }
  2151. brd->irq = ((int)(irq & 0xF000) >> 12);
  2152. for (i = 0; i < 8; i++)
  2153. brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
  2154. if ((regs[12] & 0x80) == 0) {
  2155. printk(KERN_ERR "mxser: invalid interrupt vector\n");
  2156. return -EIO;
  2157. }
  2158. brd->vector = (int)regs[11]; /* interrupt vector */
  2159. if (id == 1)
  2160. brd->vector_mask = 0x00FF;
  2161. else
  2162. brd->vector_mask = 0x000F;
  2163. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2164. if (regs[12] & bits) {
  2165. brd->ports[i].baud_base = 921600;
  2166. brd->ports[i].max_baud = 921600;
  2167. } else {
  2168. brd->ports[i].baud_base = 115200;
  2169. brd->ports[i].max_baud = 115200;
  2170. }
  2171. }
  2172. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2173. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2174. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2175. outb(scratch2, cap + UART_LCR);
  2176. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2177. scratch = inb(cap + UART_IIR);
  2178. if (scratch & 0xC0)
  2179. brd->uart_type = PORT_16550A;
  2180. else
  2181. brd->uart_type = PORT_16450;
  2182. if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
  2183. "mxser(IO)")) {
  2184. printk(KERN_ERR "mxser: can't request ports I/O region: "
  2185. "0x%.8lx-0x%.8lx\n",
  2186. brd->ports[0].ioaddr, brd->ports[0].ioaddr +
  2187. 8 * brd->info->nports - 1);
  2188. return -EIO;
  2189. }
  2190. if (!request_region(brd->vector, 1, "mxser(vector)")) {
  2191. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2192. printk(KERN_ERR "mxser: can't request interrupt vector region: "
  2193. "0x%.8lx-0x%.8lx\n",
  2194. brd->ports[0].ioaddr, brd->ports[0].ioaddr +
  2195. 8 * brd->info->nports - 1);
  2196. return -EIO;
  2197. }
  2198. return brd->info->nports;
  2199. err_irqconflict:
  2200. printk(KERN_ERR "mxser: invalid interrupt number\n");
  2201. return -EIO;
  2202. }
  2203. static int __devinit mxser_probe(struct pci_dev *pdev,
  2204. const struct pci_device_id *ent)
  2205. {
  2206. #ifdef CONFIG_PCI
  2207. struct mxser_board *brd;
  2208. unsigned int i, j;
  2209. unsigned long ioaddress;
  2210. int retval = -EINVAL;
  2211. for (i = 0; i < MXSER_BOARDS; i++)
  2212. if (mxser_boards[i].info == NULL)
  2213. break;
  2214. if (i >= MXSER_BOARDS) {
  2215. dev_err(&pdev->dev, "too many boards found (maximum %d), board "
  2216. "not configured\n", MXSER_BOARDS);
  2217. goto err;
  2218. }
  2219. brd = &mxser_boards[i];
  2220. brd->idx = i * MXSER_PORTS_PER_BOARD;
  2221. dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
  2222. mxser_cards[ent->driver_data].name,
  2223. pdev->bus->number, PCI_SLOT(pdev->devfn));
  2224. retval = pci_enable_device(pdev);
  2225. if (retval) {
  2226. dev_err(&pdev->dev, "PCI enable failed\n");
  2227. goto err;
  2228. }
  2229. /* io address */
  2230. ioaddress = pci_resource_start(pdev, 2);
  2231. retval = pci_request_region(pdev, 2, "mxser(IO)");
  2232. if (retval)
  2233. goto err;
  2234. brd->info = &mxser_cards[ent->driver_data];
  2235. for (i = 0; i < brd->info->nports; i++)
  2236. brd->ports[i].ioaddr = ioaddress + 8 * i;
  2237. /* vector */
  2238. ioaddress = pci_resource_start(pdev, 3);
  2239. retval = pci_request_region(pdev, 3, "mxser(vector)");
  2240. if (retval)
  2241. goto err_relio;
  2242. brd->vector = ioaddress;
  2243. /* irq */
  2244. brd->irq = pdev->irq;
  2245. brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
  2246. brd->uart_type = PORT_16550A;
  2247. brd->vector_mask = 0;
  2248. for (i = 0; i < brd->info->nports; i++) {
  2249. for (j = 0; j < UART_INFO_NUM; j++) {
  2250. if (Gpci_uart_info[j].type == brd->chip_flag) {
  2251. brd->ports[i].max_baud =
  2252. Gpci_uart_info[j].max_baud;
  2253. /* exception....CP-102 */
  2254. if (brd->info->flags & MXSER_HIGHBAUD)
  2255. brd->ports[i].max_baud = 921600;
  2256. break;
  2257. }
  2258. }
  2259. }
  2260. if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
  2261. for (i = 0; i < brd->info->nports; i++) {
  2262. if (i < 4)
  2263. brd->ports[i].opmode_ioaddr = ioaddress + 4;
  2264. else
  2265. brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
  2266. }
  2267. outb(0, ioaddress + 4); /* default set to RS232 mode */
  2268. outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
  2269. }
  2270. for (i = 0; i < brd->info->nports; i++) {
  2271. brd->vector_mask |= (1 << i);
  2272. brd->ports[i].baud_base = 921600;
  2273. }
  2274. /* mxser_initbrd will hook ISR. */
  2275. retval = mxser_initbrd(brd, pdev);
  2276. if (retval)
  2277. goto err_null;
  2278. for (i = 0; i < brd->info->nports; i++)
  2279. tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
  2280. pci_set_drvdata(pdev, brd);
  2281. return 0;
  2282. err_relio:
  2283. pci_release_region(pdev, 2);
  2284. err_null:
  2285. brd->info = NULL;
  2286. err:
  2287. return retval;
  2288. #else
  2289. return -ENODEV;
  2290. #endif
  2291. }
  2292. static void __devexit mxser_remove(struct pci_dev *pdev)
  2293. {
  2294. struct mxser_board *brd = pci_get_drvdata(pdev);
  2295. unsigned int i;
  2296. for (i = 0; i < brd->info->nports; i++)
  2297. tty_unregister_device(mxvar_sdriver, brd->idx + i);
  2298. mxser_release_res(brd, pdev, 1);
  2299. brd->info = NULL;
  2300. }
  2301. static struct pci_driver mxser_driver = {
  2302. .name = "mxser",
  2303. .id_table = mxser_pcibrds,
  2304. .probe = mxser_probe,
  2305. .remove = __devexit_p(mxser_remove)
  2306. };
  2307. static int __init mxser_module_init(void)
  2308. {
  2309. struct mxser_board *brd;
  2310. unsigned int b, i, m;
  2311. int retval;
  2312. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  2313. if (!mxvar_sdriver)
  2314. return -ENOMEM;
  2315. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
  2316. MXSER_VERSION);
  2317. /* Initialize the tty_driver structure */
  2318. mxvar_sdriver->owner = THIS_MODULE;
  2319. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  2320. mxvar_sdriver->name = "ttyMI";
  2321. mxvar_sdriver->major = ttymajor;
  2322. mxvar_sdriver->minor_start = 0;
  2323. mxvar_sdriver->num = MXSER_PORTS + 1;
  2324. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  2325. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  2326. mxvar_sdriver->init_termios = tty_std_termios;
  2327. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  2328. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
  2329. tty_set_operations(mxvar_sdriver, &mxser_ops);
  2330. retval = tty_register_driver(mxvar_sdriver);
  2331. if (retval) {
  2332. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
  2333. "tty driver !\n");
  2334. goto err_put;
  2335. }
  2336. /* Start finding ISA boards here */
  2337. for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
  2338. if (!ioaddr[b])
  2339. continue;
  2340. brd = &mxser_boards[m];
  2341. retval = mxser_get_ISA_conf(!ioaddr[b], brd);
  2342. if (retval <= 0) {
  2343. brd->info = NULL;
  2344. continue;
  2345. }
  2346. printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
  2347. brd->info->name, ioaddr[b]);
  2348. /* mxser_initbrd will hook ISR. */
  2349. if (mxser_initbrd(brd, NULL) < 0) {
  2350. brd->info = NULL;
  2351. continue;
  2352. }
  2353. brd->idx = m * MXSER_PORTS_PER_BOARD;
  2354. for (i = 0; i < brd->info->nports; i++)
  2355. tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
  2356. m++;
  2357. }
  2358. retval = pci_register_driver(&mxser_driver);
  2359. if (retval) {
  2360. printk(KERN_ERR "mxser: can't register pci driver\n");
  2361. if (!m) {
  2362. retval = -ENODEV;
  2363. goto err_unr;
  2364. } /* else: we have some ISA cards under control */
  2365. }
  2366. return 0;
  2367. err_unr:
  2368. tty_unregister_driver(mxvar_sdriver);
  2369. err_put:
  2370. put_tty_driver(mxvar_sdriver);
  2371. return retval;
  2372. }
  2373. static void __exit mxser_module_exit(void)
  2374. {
  2375. unsigned int i, j;
  2376. pci_unregister_driver(&mxser_driver);
  2377. for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
  2378. if (mxser_boards[i].info != NULL)
  2379. for (j = 0; j < mxser_boards[i].info->nports; j++)
  2380. tty_unregister_device(mxvar_sdriver,
  2381. mxser_boards[i].idx + j);
  2382. tty_unregister_driver(mxvar_sdriver);
  2383. put_tty_driver(mxvar_sdriver);
  2384. for (i = 0; i < MXSER_BOARDS; i++)
  2385. if (mxser_boards[i].info != NULL)
  2386. mxser_release_res(&mxser_boards[i], NULL, 1);
  2387. }
  2388. module_init(mxser_module_init);
  2389. module_exit(mxser_module_exit);