mxser_new.c 71 KB

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