mxser_new.c 69 KB

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