mxser.c 69 KB

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