mxser_new.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839
  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. info->flags |= ASYNC_INITIALIZED;
  756. spin_unlock_irqrestore(&info->slock, flags);
  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. unsigned long flags;
  813. int retval, line;
  814. line = tty->index;
  815. if (line == MXSER_PORTS)
  816. return 0;
  817. if (line < 0 || line > MXSER_PORTS)
  818. return -ENODEV;
  819. info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
  820. if (!info->ioaddr)
  821. return -ENODEV;
  822. tty->driver_data = info;
  823. info->tty = tty;
  824. /*
  825. * Start up serial port
  826. */
  827. spin_lock_irqsave(&info->slock, flags);
  828. info->count++;
  829. spin_unlock_irqrestore(&info->slock, flags);
  830. retval = mxser_startup(info);
  831. if (retval)
  832. return retval;
  833. retval = mxser_block_til_ready(tty, filp, info);
  834. if (retval)
  835. return retval;
  836. /* unmark here for very high baud rate (ex. 921600 bps) used */
  837. tty->low_latency = 1;
  838. return 0;
  839. }
  840. /*
  841. * This routine is called when the serial port gets closed. First, we
  842. * wait for the last remaining data to be sent. Then, we unlink its
  843. * async structure from the interrupt chain if necessary, and we free
  844. * that IRQ if nothing is left in the chain.
  845. */
  846. static void mxser_close(struct tty_struct *tty, struct file *filp)
  847. {
  848. struct mxser_port *info = tty->driver_data;
  849. unsigned long timeout;
  850. unsigned long flags;
  851. if (tty->index == MXSER_PORTS)
  852. return;
  853. if (!info)
  854. return;
  855. spin_lock_irqsave(&info->slock, flags);
  856. if (tty_hung_up_p(filp)) {
  857. spin_unlock_irqrestore(&info->slock, flags);
  858. return;
  859. }
  860. if ((tty->count == 1) && (info->count != 1)) {
  861. /*
  862. * Uh, oh. tty->count is 1, which means that the tty
  863. * structure will be freed. Info->count should always
  864. * be one in these conditions. If it's greater than
  865. * one, we've got real problems, since it means the
  866. * serial port won't be shutdown.
  867. */
  868. printk(KERN_ERR "mxser_close: bad serial port count; "
  869. "tty->count is 1, info->count is %d\n", info->count);
  870. info->count = 1;
  871. }
  872. if (--info->count < 0) {
  873. printk(KERN_ERR "mxser_close: bad serial port count for "
  874. "ttys%d: %d\n", tty->index, info->count);
  875. info->count = 0;
  876. }
  877. if (info->count) {
  878. spin_unlock_irqrestore(&info->slock, flags);
  879. return;
  880. }
  881. info->flags |= ASYNC_CLOSING;
  882. spin_unlock_irqrestore(&info->slock, flags);
  883. /*
  884. * Save the termios structure, since this port may have
  885. * separate termios for callout and dialin.
  886. */
  887. if (info->flags & ASYNC_NORMAL_ACTIVE)
  888. info->normal_termios = *tty->termios;
  889. /*
  890. * Now we wait for the transmit buffer to clear; and we notify
  891. * the line discipline to only process XON/XOFF characters.
  892. */
  893. tty->closing = 1;
  894. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  895. tty_wait_until_sent(tty, info->closing_wait);
  896. /*
  897. * At this point we stop accepting input. To do this, we
  898. * disable the receive line status interrupts, and tell the
  899. * interrupt driver to stop checking the data ready bit in the
  900. * line status register.
  901. */
  902. info->IER &= ~UART_IER_RLSI;
  903. if (info->board->chip_flag)
  904. info->IER &= ~MOXA_MUST_RECV_ISR;
  905. if (info->flags & ASYNC_INITIALIZED) {
  906. outb(info->IER, info->ioaddr + UART_IER);
  907. /*
  908. * Before we drop DTR, make sure the UART transmitter
  909. * has completely drained; this is especially
  910. * important if there is a transmit FIFO!
  911. */
  912. timeout = jiffies + HZ;
  913. while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
  914. schedule_timeout_interruptible(5);
  915. if (time_after(jiffies, timeout))
  916. break;
  917. }
  918. }
  919. mxser_shutdown(info);
  920. if (tty->driver->flush_buffer)
  921. tty->driver->flush_buffer(tty);
  922. tty_ldisc_flush(tty);
  923. tty->closing = 0;
  924. info->event = 0;
  925. info->tty = NULL;
  926. if (info->blocked_open) {
  927. if (info->close_delay)
  928. schedule_timeout_interruptible(info->close_delay);
  929. wake_up_interruptible(&info->open_wait);
  930. }
  931. info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  932. }
  933. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  934. {
  935. int c, total = 0;
  936. struct mxser_port *info = tty->driver_data;
  937. unsigned long flags;
  938. if (!info->xmit_buf)
  939. return 0;
  940. while (1) {
  941. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  942. SERIAL_XMIT_SIZE - info->xmit_head));
  943. if (c <= 0)
  944. break;
  945. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  946. spin_lock_irqsave(&info->slock, flags);
  947. info->xmit_head = (info->xmit_head + c) &
  948. (SERIAL_XMIT_SIZE - 1);
  949. info->xmit_cnt += c;
  950. spin_unlock_irqrestore(&info->slock, flags);
  951. buf += c;
  952. count -= c;
  953. total += c;
  954. }
  955. if (info->xmit_cnt && !tty->stopped) {
  956. if (!tty->hw_stopped ||
  957. (info->type == PORT_16550A) ||
  958. (info->board->chip_flag)) {
  959. spin_lock_irqsave(&info->slock, flags);
  960. outb(info->IER & ~UART_IER_THRI, info->ioaddr +
  961. UART_IER);
  962. info->IER |= UART_IER_THRI;
  963. outb(info->IER, info->ioaddr + UART_IER);
  964. spin_unlock_irqrestore(&info->slock, flags);
  965. }
  966. }
  967. return total;
  968. }
  969. static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
  970. {
  971. struct mxser_port *info = tty->driver_data;
  972. unsigned long flags;
  973. if (!info->xmit_buf)
  974. return;
  975. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  976. return;
  977. spin_lock_irqsave(&info->slock, flags);
  978. info->xmit_buf[info->xmit_head++] = ch;
  979. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  980. info->xmit_cnt++;
  981. spin_unlock_irqrestore(&info->slock, flags);
  982. if (!tty->stopped) {
  983. if (!tty->hw_stopped ||
  984. (info->type == PORT_16550A) ||
  985. info->board->chip_flag) {
  986. spin_lock_irqsave(&info->slock, flags);
  987. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  988. info->IER |= UART_IER_THRI;
  989. outb(info->IER, info->ioaddr + UART_IER);
  990. spin_unlock_irqrestore(&info->slock, flags);
  991. }
  992. }
  993. }
  994. static void mxser_flush_chars(struct tty_struct *tty)
  995. {
  996. struct mxser_port *info = tty->driver_data;
  997. unsigned long flags;
  998. if (info->xmit_cnt <= 0 ||
  999. tty->stopped ||
  1000. !info->xmit_buf ||
  1001. (tty->hw_stopped &&
  1002. (info->type != PORT_16550A) &&
  1003. (!info->board->chip_flag)
  1004. ))
  1005. return;
  1006. spin_lock_irqsave(&info->slock, flags);
  1007. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1008. info->IER |= UART_IER_THRI;
  1009. outb(info->IER, info->ioaddr + UART_IER);
  1010. spin_unlock_irqrestore(&info->slock, flags);
  1011. }
  1012. static int mxser_write_room(struct tty_struct *tty)
  1013. {
  1014. struct mxser_port *info = tty->driver_data;
  1015. int ret;
  1016. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1017. if (ret < 0)
  1018. ret = 0;
  1019. return ret;
  1020. }
  1021. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1022. {
  1023. struct mxser_port *info = tty->driver_data;
  1024. return info->xmit_cnt;
  1025. }
  1026. static void mxser_flush_buffer(struct tty_struct *tty)
  1027. {
  1028. struct mxser_port *info = tty->driver_data;
  1029. char fcr;
  1030. unsigned long flags;
  1031. spin_lock_irqsave(&info->slock, flags);
  1032. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1033. fcr = inb(info->ioaddr + UART_FCR);
  1034. outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
  1035. info->ioaddr + UART_FCR);
  1036. outb(fcr, info->ioaddr + UART_FCR);
  1037. spin_unlock_irqrestore(&info->slock, flags);
  1038. tty_wakeup(tty);
  1039. }
  1040. /*
  1041. * ------------------------------------------------------------
  1042. * friends of mxser_ioctl()
  1043. * ------------------------------------------------------------
  1044. */
  1045. static int mxser_get_serial_info(struct mxser_port *info,
  1046. struct serial_struct __user *retinfo)
  1047. {
  1048. struct serial_struct tmp;
  1049. if (!retinfo)
  1050. return -EFAULT;
  1051. memset(&tmp, 0, sizeof(tmp));
  1052. tmp.type = info->type;
  1053. tmp.line = info->tty->index;
  1054. tmp.port = info->ioaddr;
  1055. tmp.irq = info->board->irq;
  1056. tmp.flags = info->flags;
  1057. tmp.baud_base = info->baud_base;
  1058. tmp.close_delay = info->close_delay;
  1059. tmp.closing_wait = info->closing_wait;
  1060. tmp.custom_divisor = info->custom_divisor;
  1061. tmp.hub6 = 0;
  1062. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1063. return -EFAULT;
  1064. return 0;
  1065. }
  1066. static int mxser_set_serial_info(struct mxser_port *info,
  1067. struct serial_struct __user *new_info)
  1068. {
  1069. struct serial_struct new_serial;
  1070. unsigned long sl_flags;
  1071. unsigned int flags;
  1072. int retval = 0;
  1073. if (!new_info || !info->ioaddr)
  1074. return -EFAULT;
  1075. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  1076. return -EFAULT;
  1077. if ((new_serial.irq != info->board->irq) ||
  1078. (new_serial.port != info->ioaddr) ||
  1079. (new_serial.custom_divisor != info->custom_divisor) ||
  1080. (new_serial.baud_base != info->baud_base))
  1081. return -EPERM;
  1082. flags = info->flags & ASYNC_SPD_MASK;
  1083. if (!capable(CAP_SYS_ADMIN)) {
  1084. if ((new_serial.baud_base != info->baud_base) ||
  1085. (new_serial.close_delay != info->close_delay) ||
  1086. ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
  1087. return -EPERM;
  1088. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  1089. (new_serial.flags & ASYNC_USR_MASK));
  1090. } else {
  1091. /*
  1092. * OK, past this point, all the error checking has been done.
  1093. * At this point, we start making changes.....
  1094. */
  1095. info->flags = ((info->flags & ~ASYNC_FLAGS) |
  1096. (new_serial.flags & ASYNC_FLAGS));
  1097. info->close_delay = new_serial.close_delay * HZ / 100;
  1098. info->closing_wait = new_serial.closing_wait * HZ / 100;
  1099. info->tty->low_latency =
  1100. (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1101. info->tty->low_latency = 0;
  1102. }
  1103. info->type = new_serial.type;
  1104. process_txrx_fifo(info);
  1105. if (info->flags & ASYNC_INITIALIZED) {
  1106. if (flags != (info->flags & ASYNC_SPD_MASK)) {
  1107. spin_lock_irqsave(&info->slock, sl_flags);
  1108. mxser_change_speed(info, NULL);
  1109. spin_unlock_irqrestore(&info->slock, sl_flags);
  1110. }
  1111. } else
  1112. retval = mxser_startup(info);
  1113. return retval;
  1114. }
  1115. /*
  1116. * mxser_get_lsr_info - get line status register info
  1117. *
  1118. * Purpose: Let user call ioctl() to get info when the UART physically
  1119. * is emptied. On bus types like RS485, the transmitter must
  1120. * release the bus after transmitting. This must be done when
  1121. * the transmit shift register is empty, not be done when the
  1122. * transmit holding register is empty. This functionality
  1123. * allows an RS485 driver to be written in user space.
  1124. */
  1125. static int mxser_get_lsr_info(struct mxser_port *info,
  1126. unsigned int __user *value)
  1127. {
  1128. unsigned char status;
  1129. unsigned int result;
  1130. unsigned long flags;
  1131. spin_lock_irqsave(&info->slock, flags);
  1132. status = inb(info->ioaddr + UART_LSR);
  1133. spin_unlock_irqrestore(&info->slock, flags);
  1134. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1135. return put_user(result, value);
  1136. }
  1137. /*
  1138. * This routine sends a break character out the serial port.
  1139. */
  1140. static void mxser_send_break(struct mxser_port *info, int duration)
  1141. {
  1142. unsigned long flags;
  1143. if (!info->ioaddr)
  1144. return;
  1145. set_current_state(TASK_INTERRUPTIBLE);
  1146. spin_lock_irqsave(&info->slock, flags);
  1147. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1148. info->ioaddr + UART_LCR);
  1149. spin_unlock_irqrestore(&info->slock, flags);
  1150. schedule_timeout(duration);
  1151. spin_lock_irqsave(&info->slock, flags);
  1152. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1153. info->ioaddr + UART_LCR);
  1154. spin_unlock_irqrestore(&info->slock, flags);
  1155. }
  1156. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  1157. {
  1158. struct mxser_port *info = tty->driver_data;
  1159. unsigned char control, status;
  1160. unsigned long flags;
  1161. if (tty->index == MXSER_PORTS)
  1162. return -ENOIOCTLCMD;
  1163. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1164. return -EIO;
  1165. control = info->MCR;
  1166. spin_lock_irqsave(&info->slock, flags);
  1167. status = inb(info->ioaddr + UART_MSR);
  1168. if (status & UART_MSR_ANY_DELTA)
  1169. mxser_check_modem_status(info, status);
  1170. spin_unlock_irqrestore(&info->slock, flags);
  1171. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  1172. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  1173. ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  1174. ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
  1175. ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  1176. ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1177. }
  1178. static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
  1179. unsigned int set, unsigned int clear)
  1180. {
  1181. struct mxser_port *info = tty->driver_data;
  1182. unsigned long flags;
  1183. if (tty->index == MXSER_PORTS)
  1184. return -ENOIOCTLCMD;
  1185. if (test_bit(TTY_IO_ERROR, &tty->flags))
  1186. return -EIO;
  1187. spin_lock_irqsave(&info->slock, flags);
  1188. if (set & TIOCM_RTS)
  1189. info->MCR |= UART_MCR_RTS;
  1190. if (set & TIOCM_DTR)
  1191. info->MCR |= UART_MCR_DTR;
  1192. if (clear & TIOCM_RTS)
  1193. info->MCR &= ~UART_MCR_RTS;
  1194. if (clear & TIOCM_DTR)
  1195. info->MCR &= ~UART_MCR_DTR;
  1196. outb(info->MCR, info->ioaddr + UART_MCR);
  1197. spin_unlock_irqrestore(&info->slock, flags);
  1198. return 0;
  1199. }
  1200. static int __init mxser_program_mode(int port)
  1201. {
  1202. int id, i, j, n;
  1203. outb(0, port);
  1204. outb(0, port);
  1205. outb(0, port);
  1206. (void)inb(port);
  1207. (void)inb(port);
  1208. outb(0, port);
  1209. (void)inb(port);
  1210. id = inb(port + 1) & 0x1F;
  1211. if ((id != C168_ASIC_ID) &&
  1212. (id != C104_ASIC_ID) &&
  1213. (id != C102_ASIC_ID) &&
  1214. (id != CI132_ASIC_ID) &&
  1215. (id != CI134_ASIC_ID) &&
  1216. (id != CI104J_ASIC_ID))
  1217. return -1;
  1218. for (i = 0, j = 0; i < 4; i++) {
  1219. n = inb(port + 2);
  1220. if (n == 'M') {
  1221. j = 1;
  1222. } else if ((j == 1) && (n == 1)) {
  1223. j = 2;
  1224. break;
  1225. } else
  1226. j = 0;
  1227. }
  1228. if (j != 2)
  1229. id = -2;
  1230. return id;
  1231. }
  1232. static void __init mxser_normal_mode(int port)
  1233. {
  1234. int i, n;
  1235. outb(0xA5, port + 1);
  1236. outb(0x80, port + 3);
  1237. outb(12, port + 0); /* 9600 bps */
  1238. outb(0, port + 1);
  1239. outb(0x03, port + 3); /* 8 data bits */
  1240. outb(0x13, port + 4); /* loop back mode */
  1241. for (i = 0; i < 16; i++) {
  1242. n = inb(port + 5);
  1243. if ((n & 0x61) == 0x60)
  1244. break;
  1245. if ((n & 1) == 1)
  1246. (void)inb(port);
  1247. }
  1248. outb(0x00, port + 4);
  1249. }
  1250. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  1251. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  1252. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  1253. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  1254. #define EN_CCMD 0x000 /* Chip's command register */
  1255. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  1256. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  1257. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  1258. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  1259. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  1260. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  1261. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  1262. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  1263. static int __init mxser_read_register(int port, unsigned short *regs)
  1264. {
  1265. int i, k, value, id;
  1266. unsigned int j;
  1267. id = mxser_program_mode(port);
  1268. if (id < 0)
  1269. return id;
  1270. for (i = 0; i < 14; i++) {
  1271. k = (i & 0x3F) | 0x180;
  1272. for (j = 0x100; j > 0; j >>= 1) {
  1273. outb(CHIP_CS, port);
  1274. if (k & j) {
  1275. outb(CHIP_CS | CHIP_DO, port);
  1276. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  1277. } else {
  1278. outb(CHIP_CS, port);
  1279. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  1280. }
  1281. }
  1282. (void)inb(port);
  1283. value = 0;
  1284. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  1285. outb(CHIP_CS, port);
  1286. outb(CHIP_CS | CHIP_SK, port);
  1287. if (inb(port) & CHIP_DI)
  1288. value |= j;
  1289. }
  1290. regs[i] = value;
  1291. outb(0, port);
  1292. }
  1293. mxser_normal_mode(port);
  1294. return id;
  1295. }
  1296. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1297. {
  1298. struct mxser_port *port;
  1299. int result, status;
  1300. unsigned int i, j;
  1301. switch (cmd) {
  1302. case MOXA_GET_CONF:
  1303. /* if (copy_to_user(argp, mxsercfg,
  1304. sizeof(struct mxser_hwconf) * 4))
  1305. return -EFAULT;
  1306. return 0;*/
  1307. return -ENXIO;
  1308. case MOXA_GET_MAJOR:
  1309. if (copy_to_user(argp, &ttymajor, sizeof(int)))
  1310. return -EFAULT;
  1311. return 0;
  1312. case MOXA_GET_CUMAJOR:
  1313. if (copy_to_user(argp, &calloutmajor, sizeof(int)))
  1314. return -EFAULT;
  1315. return 0;
  1316. case MOXA_CHKPORTENABLE:
  1317. result = 0;
  1318. for (i = 0; i < MXSER_BOARDS; i++)
  1319. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
  1320. if (mxser_boards[i].ports[j].ioaddr)
  1321. result |= (1 << i);
  1322. return put_user(result, (unsigned long __user *)argp);
  1323. case MOXA_GETDATACOUNT:
  1324. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1325. return -EFAULT;
  1326. return 0;
  1327. case MOXA_GETMSTATUS:
  1328. for (i = 0; i < MXSER_BOARDS; i++)
  1329. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1330. port = &mxser_boards[i].ports[j];
  1331. GMStatus[i].ri = 0;
  1332. if (!port->ioaddr) {
  1333. GMStatus[i].dcd = 0;
  1334. GMStatus[i].dsr = 0;
  1335. GMStatus[i].cts = 0;
  1336. continue;
  1337. }
  1338. if (!port->tty || !port->tty->termios)
  1339. GMStatus[i].cflag =
  1340. port->normal_termios.c_cflag;
  1341. else
  1342. GMStatus[i].cflag =
  1343. port->tty->termios->c_cflag;
  1344. status = inb(port->ioaddr + UART_MSR);
  1345. if (status & 0x80 /*UART_MSR_DCD */ )
  1346. GMStatus[i].dcd = 1;
  1347. else
  1348. GMStatus[i].dcd = 0;
  1349. if (status & 0x20 /*UART_MSR_DSR */ )
  1350. GMStatus[i].dsr = 1;
  1351. else
  1352. GMStatus[i].dsr = 0;
  1353. if (status & 0x10 /*UART_MSR_CTS */ )
  1354. GMStatus[i].cts = 1;
  1355. else
  1356. GMStatus[i].cts = 0;
  1357. }
  1358. if (copy_to_user(argp, GMStatus,
  1359. sizeof(struct mxser_mstatus) * MXSER_PORTS))
  1360. return -EFAULT;
  1361. return 0;
  1362. case MOXA_ASPP_MON_EXT: {
  1363. int status, p, shiftbit;
  1364. unsigned long opmode;
  1365. unsigned cflag, iflag;
  1366. for (i = 0; i < MXSER_BOARDS; i++)
  1367. for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
  1368. port = &mxser_boards[i].ports[j];
  1369. if (!port->ioaddr)
  1370. continue;
  1371. status = mxser_get_msr(port->ioaddr, 0, i);
  1372. if (status & UART_MSR_TERI)
  1373. port->icount.rng++;
  1374. if (status & UART_MSR_DDSR)
  1375. port->icount.dsr++;
  1376. if (status & UART_MSR_DDCD)
  1377. port->icount.dcd++;
  1378. if (status & UART_MSR_DCTS)
  1379. port->icount.cts++;
  1380. port->mon_data.modem_status = status;
  1381. mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
  1382. mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
  1383. mon_data_ext.up_rxcnt[i] =
  1384. port->mon_data.up_rxcnt;
  1385. mon_data_ext.up_txcnt[i] =
  1386. port->mon_data.up_txcnt;
  1387. mon_data_ext.modem_status[i] =
  1388. port->mon_data.modem_status;
  1389. mon_data_ext.baudrate[i] = port->realbaud;
  1390. if (!port->tty || !port->tty->termios) {
  1391. cflag = port->normal_termios.c_cflag;
  1392. iflag = port->normal_termios.c_iflag;
  1393. } else {
  1394. cflag = port->tty->termios->c_cflag;
  1395. iflag = port->tty->termios->c_iflag;
  1396. }
  1397. mon_data_ext.databits[i] = cflag & CSIZE;
  1398. mon_data_ext.stopbits[i] = cflag & CSTOPB;
  1399. mon_data_ext.parity[i] =
  1400. cflag & (PARENB | PARODD | CMSPAR);
  1401. mon_data_ext.flowctrl[i] = 0x00;
  1402. if (cflag & CRTSCTS)
  1403. mon_data_ext.flowctrl[i] |= 0x03;
  1404. if (iflag & (IXON | IXOFF))
  1405. mon_data_ext.flowctrl[i] |= 0x0C;
  1406. if (port->type == PORT_16550A)
  1407. mon_data_ext.fifo[i] = 1;
  1408. else
  1409. mon_data_ext.fifo[i] = 0;
  1410. p = i % 4;
  1411. shiftbit = p * 2;
  1412. opmode = inb(port->opmode_ioaddr) >> shiftbit;
  1413. opmode &= OP_MODE_MASK;
  1414. mon_data_ext.iftype[i] = opmode;
  1415. }
  1416. if (copy_to_user(argp, &mon_data_ext,
  1417. sizeof(mon_data_ext)))
  1418. return -EFAULT;
  1419. return 0;
  1420. } default:
  1421. return -ENOIOCTLCMD;
  1422. }
  1423. return 0;
  1424. }
  1425. static int mxser_ioctl(struct tty_struct *tty, struct file *file,
  1426. unsigned int cmd, unsigned long arg)
  1427. {
  1428. struct mxser_port *info = tty->driver_data;
  1429. struct async_icount cprev, cnow; /* kernel counter temps */
  1430. struct serial_icounter_struct __user *p_cuser;
  1431. unsigned long templ;
  1432. unsigned long flags;
  1433. unsigned int i;
  1434. void __user *argp = (void __user *)arg;
  1435. int retval;
  1436. if (tty->index == MXSER_PORTS)
  1437. return mxser_ioctl_special(cmd, argp);
  1438. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1439. int p;
  1440. unsigned long opmode;
  1441. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1442. int shiftbit;
  1443. unsigned char val, mask;
  1444. p = tty->index % 4;
  1445. if (cmd == MOXA_SET_OP_MODE) {
  1446. if (get_user(opmode, (int __user *) argp))
  1447. return -EFAULT;
  1448. if (opmode != RS232_MODE &&
  1449. opmode != RS485_2WIRE_MODE &&
  1450. opmode != RS422_MODE &&
  1451. opmode != RS485_4WIRE_MODE)
  1452. return -EFAULT;
  1453. mask = ModeMask[p];
  1454. shiftbit = p * 2;
  1455. val = inb(info->opmode_ioaddr);
  1456. val &= mask;
  1457. val |= (opmode << shiftbit);
  1458. outb(val, info->opmode_ioaddr);
  1459. } else {
  1460. shiftbit = p * 2;
  1461. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1462. opmode &= OP_MODE_MASK;
  1463. if (copy_to_user(argp, &opmode, sizeof(int)))
  1464. return -EFAULT;
  1465. }
  1466. return 0;
  1467. }
  1468. if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
  1469. int speed;
  1470. if (get_user(speed, (int __user *)argp))
  1471. return -EFAULT;
  1472. if (speed <= 0 || speed > info->max_baud)
  1473. return -EFAULT;
  1474. if (!info->tty || !info->tty->termios || !info->ioaddr)
  1475. return 0;
  1476. info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
  1477. for (i = 0; i < BAUD_TABLE_NO; i++)
  1478. if (speed == mxvar_baud_table[i])
  1479. break;
  1480. if (i == BAUD_TABLE_NO) {
  1481. info->tty->termios->c_cflag |= B_SPEC;
  1482. } else if (speed != 0)
  1483. info->tty->termios->c_cflag |= mxvar_baud_table1[i];
  1484. info->speed = speed;
  1485. spin_lock_irqsave(&info->slock, flags);
  1486. mxser_change_speed(info, NULL);
  1487. spin_unlock_irqrestore(&info->slock, flags);
  1488. return 0;
  1489. } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
  1490. if (copy_to_user(argp, &info->speed, sizeof(int)))
  1491. return -EFAULT;
  1492. return 0;
  1493. }
  1494. if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
  1495. test_bit(TTY_IO_ERROR, &tty->flags))
  1496. return -EIO;
  1497. switch (cmd) {
  1498. case TCSBRK: /* SVID version: non-zero arg --> no break */
  1499. retval = tty_check_change(tty);
  1500. if (retval)
  1501. return retval;
  1502. tty_wait_until_sent(tty, 0);
  1503. if (!arg)
  1504. mxser_send_break(info, HZ / 4); /* 1/4 second */
  1505. return 0;
  1506. case TCSBRKP: /* support for POSIX tcsendbreak() */
  1507. retval = tty_check_change(tty);
  1508. if (retval)
  1509. return retval;
  1510. tty_wait_until_sent(tty, 0);
  1511. mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
  1512. return 0;
  1513. case TIOCGSOFTCAR:
  1514. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
  1515. case TIOCSSOFTCAR:
  1516. if (get_user(templ, (unsigned long __user *) argp))
  1517. return -EFAULT;
  1518. arg = templ;
  1519. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
  1520. return 0;
  1521. case TIOCGSERIAL:
  1522. return mxser_get_serial_info(info, argp);
  1523. case TIOCSSERIAL:
  1524. return mxser_set_serial_info(info, argp);
  1525. case TIOCSERGETLSR: /* Get line status register */
  1526. return mxser_get_lsr_info(info, argp);
  1527. /*
  1528. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1529. * - mask passed in arg for lines of interest
  1530. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1531. * Caller should use TIOCGICOUNT to see which one it was
  1532. */
  1533. case TIOCMIWAIT: {
  1534. DECLARE_WAITQUEUE(wait, current);
  1535. int ret;
  1536. spin_lock_irqsave(&info->slock, flags);
  1537. cprev = info->icount; /* note the counters on entry */
  1538. spin_unlock_irqrestore(&info->slock, flags);
  1539. add_wait_queue(&info->delta_msr_wait, &wait);
  1540. while (1) {
  1541. spin_lock_irqsave(&info->slock, flags);
  1542. cnow = info->icount; /* atomic copy */
  1543. spin_unlock_irqrestore(&info->slock, flags);
  1544. set_current_state(TASK_INTERRUPTIBLE);
  1545. if (((arg & TIOCM_RNG) &&
  1546. (cnow.rng != cprev.rng)) ||
  1547. ((arg & TIOCM_DSR) &&
  1548. (cnow.dsr != cprev.dsr)) ||
  1549. ((arg & TIOCM_CD) &&
  1550. (cnow.dcd != cprev.dcd)) ||
  1551. ((arg & TIOCM_CTS) &&
  1552. (cnow.cts != cprev.cts))) {
  1553. ret = 0;
  1554. break;
  1555. }
  1556. /* see if a signal did it */
  1557. if (signal_pending(current)) {
  1558. ret = -ERESTARTSYS;
  1559. break;
  1560. }
  1561. cprev = cnow;
  1562. }
  1563. current->state = TASK_RUNNING;
  1564. remove_wait_queue(&info->delta_msr_wait, &wait);
  1565. break;
  1566. }
  1567. /* NOTREACHED */
  1568. /*
  1569. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1570. * Return: write counters to the user passed counter struct
  1571. * NB: both 1->0 and 0->1 transitions are counted except for
  1572. * RI where only 0->1 is counted.
  1573. */
  1574. case TIOCGICOUNT:
  1575. spin_lock_irqsave(&info->slock, flags);
  1576. cnow = info->icount;
  1577. spin_unlock_irqrestore(&info->slock, flags);
  1578. p_cuser = argp;
  1579. if (put_user(cnow.frame, &p_cuser->frame))
  1580. return -EFAULT;
  1581. if (put_user(cnow.brk, &p_cuser->brk))
  1582. return -EFAULT;
  1583. if (put_user(cnow.overrun, &p_cuser->overrun))
  1584. return -EFAULT;
  1585. if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  1586. return -EFAULT;
  1587. if (put_user(cnow.parity, &p_cuser->parity))
  1588. return -EFAULT;
  1589. if (put_user(cnow.rx, &p_cuser->rx))
  1590. return -EFAULT;
  1591. if (put_user(cnow.tx, &p_cuser->tx))
  1592. return -EFAULT;
  1593. put_user(cnow.cts, &p_cuser->cts);
  1594. put_user(cnow.dsr, &p_cuser->dsr);
  1595. put_user(cnow.rng, &p_cuser->rng);
  1596. put_user(cnow.dcd, &p_cuser->dcd);
  1597. return 0;
  1598. case MOXA_HighSpeedOn:
  1599. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
  1600. case MOXA_SDS_RSTICOUNTER:
  1601. info->mon_data.rxcnt = 0;
  1602. info->mon_data.txcnt = 0;
  1603. return 0;
  1604. case MOXA_ASPP_SETBAUD:{
  1605. long baud;
  1606. if (get_user(baud, (long __user *)argp))
  1607. return -EFAULT;
  1608. spin_lock_irqsave(&info->slock, flags);
  1609. mxser_set_baud(info, baud);
  1610. spin_unlock_irqrestore(&info->slock, flags);
  1611. return 0;
  1612. }
  1613. case MOXA_ASPP_GETBAUD:
  1614. if (copy_to_user(argp, &info->realbaud, sizeof(long)))
  1615. return -EFAULT;
  1616. return 0;
  1617. case MOXA_ASPP_OQUEUE:{
  1618. int len, lsr;
  1619. len = mxser_chars_in_buffer(tty);
  1620. lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
  1621. len += (lsr ? 0 : 1);
  1622. if (copy_to_user(argp, &len, sizeof(int)))
  1623. return -EFAULT;
  1624. return 0;
  1625. }
  1626. case MOXA_ASPP_MON: {
  1627. int mcr, status;
  1628. status = mxser_get_msr(info->ioaddr, 1, tty->index);
  1629. mxser_check_modem_status(info, status);
  1630. mcr = inb(info->ioaddr + UART_MCR);
  1631. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1632. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1633. else
  1634. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1635. if (mcr & MOXA_MUST_MCR_TX_XON)
  1636. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1637. else
  1638. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1639. if (info->tty->hw_stopped)
  1640. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1641. else
  1642. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1643. if (copy_to_user(argp, &info->mon_data,
  1644. sizeof(struct mxser_mon)))
  1645. return -EFAULT;
  1646. return 0;
  1647. }
  1648. case MOXA_ASPP_LSTATUS: {
  1649. if (copy_to_user(argp, &info->err_shadow,
  1650. sizeof(unsigned char)))
  1651. return -EFAULT;
  1652. info->err_shadow = 0;
  1653. return 0;
  1654. }
  1655. case MOXA_SET_BAUD_METHOD: {
  1656. int method;
  1657. if (get_user(method, (int __user *)argp))
  1658. return -EFAULT;
  1659. mxser_set_baud_method[tty->index] = method;
  1660. if (copy_to_user(argp, &method, sizeof(int)))
  1661. return -EFAULT;
  1662. return 0;
  1663. }
  1664. default:
  1665. return -ENOIOCTLCMD;
  1666. }
  1667. return 0;
  1668. }
  1669. static void mxser_stoprx(struct tty_struct *tty)
  1670. {
  1671. struct mxser_port *info = tty->driver_data;
  1672. info->ldisc_stop_rx = 1;
  1673. if (I_IXOFF(tty)) {
  1674. if (info->board->chip_flag) {
  1675. info->IER &= ~MOXA_MUST_RECV_ISR;
  1676. outb(info->IER, info->ioaddr + UART_IER);
  1677. } else {
  1678. info->x_char = STOP_CHAR(tty);
  1679. outb(0, info->ioaddr + UART_IER);
  1680. info->IER |= UART_IER_THRI;
  1681. outb(info->IER, info->ioaddr + UART_IER);
  1682. }
  1683. }
  1684. if (info->tty->termios->c_cflag & CRTSCTS) {
  1685. info->MCR &= ~UART_MCR_RTS;
  1686. outb(info->MCR, info->ioaddr + UART_MCR);
  1687. }
  1688. }
  1689. /*
  1690. * This routine is called by the upper-layer tty layer to signal that
  1691. * incoming characters should be throttled.
  1692. */
  1693. static void mxser_throttle(struct tty_struct *tty)
  1694. {
  1695. mxser_stoprx(tty);
  1696. }
  1697. static void mxser_unthrottle(struct tty_struct *tty)
  1698. {
  1699. struct mxser_port *info = tty->driver_data;
  1700. /* startrx */
  1701. info->ldisc_stop_rx = 0;
  1702. if (I_IXOFF(tty)) {
  1703. if (info->x_char)
  1704. info->x_char = 0;
  1705. else {
  1706. if (info->board->chip_flag) {
  1707. info->IER |= MOXA_MUST_RECV_ISR;
  1708. outb(info->IER, info->ioaddr + UART_IER);
  1709. } else {
  1710. info->x_char = START_CHAR(tty);
  1711. outb(0, info->ioaddr + UART_IER);
  1712. info->IER |= UART_IER_THRI;
  1713. outb(info->IER, info->ioaddr + UART_IER);
  1714. }
  1715. }
  1716. }
  1717. if (info->tty->termios->c_cflag & CRTSCTS) {
  1718. info->MCR |= UART_MCR_RTS;
  1719. outb(info->MCR, info->ioaddr + UART_MCR);
  1720. }
  1721. }
  1722. /*
  1723. * mxser_stop() and mxser_start()
  1724. *
  1725. * This routines are called before setting or resetting tty->stopped.
  1726. * They enable or disable transmitter interrupts, as necessary.
  1727. */
  1728. static void mxser_stop(struct tty_struct *tty)
  1729. {
  1730. struct mxser_port *info = tty->driver_data;
  1731. unsigned long flags;
  1732. spin_lock_irqsave(&info->slock, flags);
  1733. if (info->IER & UART_IER_THRI) {
  1734. info->IER &= ~UART_IER_THRI;
  1735. outb(info->IER, info->ioaddr + UART_IER);
  1736. }
  1737. spin_unlock_irqrestore(&info->slock, flags);
  1738. }
  1739. static void mxser_start(struct tty_struct *tty)
  1740. {
  1741. struct mxser_port *info = tty->driver_data;
  1742. unsigned long flags;
  1743. spin_lock_irqsave(&info->slock, flags);
  1744. if (info->xmit_cnt && info->xmit_buf) {
  1745. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  1746. info->IER |= UART_IER_THRI;
  1747. outb(info->IER, info->ioaddr + UART_IER);
  1748. }
  1749. spin_unlock_irqrestore(&info->slock, flags);
  1750. }
  1751. static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1752. {
  1753. struct mxser_port *info = tty->driver_data;
  1754. unsigned long flags;
  1755. if ((tty->termios->c_cflag != old_termios->c_cflag) ||
  1756. (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
  1757. spin_lock_irqsave(&info->slock, flags);
  1758. mxser_change_speed(info, old_termios);
  1759. spin_unlock_irqrestore(&info->slock, flags);
  1760. if ((old_termios->c_cflag & CRTSCTS) &&
  1761. !(tty->termios->c_cflag & CRTSCTS)) {
  1762. tty->hw_stopped = 0;
  1763. mxser_start(tty);
  1764. }
  1765. }
  1766. /* Handle sw stopped */
  1767. if ((old_termios->c_iflag & IXON) &&
  1768. !(tty->termios->c_iflag & IXON)) {
  1769. tty->stopped = 0;
  1770. if (info->board->chip_flag) {
  1771. spin_lock_irqsave(&info->slock, flags);
  1772. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
  1773. spin_unlock_irqrestore(&info->slock, flags);
  1774. }
  1775. mxser_start(tty);
  1776. }
  1777. }
  1778. /*
  1779. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1780. */
  1781. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1782. {
  1783. struct mxser_port *info = tty->driver_data;
  1784. unsigned long orig_jiffies, char_time;
  1785. int lsr;
  1786. if (info->type == PORT_UNKNOWN)
  1787. return;
  1788. if (info->xmit_fifo_size == 0)
  1789. return; /* Just in case.... */
  1790. orig_jiffies = jiffies;
  1791. /*
  1792. * Set the check interval to be 1/5 of the estimated time to
  1793. * send a single character, and make it at least 1. The check
  1794. * interval should also be less than the timeout.
  1795. *
  1796. * Note: we have to use pretty tight timings here to satisfy
  1797. * the NIST-PCTS.
  1798. */
  1799. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1800. char_time = char_time / 5;
  1801. if (char_time == 0)
  1802. char_time = 1;
  1803. if (timeout && timeout < char_time)
  1804. char_time = timeout;
  1805. /*
  1806. * If the transmitter hasn't cleared in twice the approximate
  1807. * amount of time to send the entire FIFO, it probably won't
  1808. * ever clear. This assumes the UART isn't doing flow
  1809. * control, which is currently the case. Hence, if it ever
  1810. * takes longer than info->timeout, this is probably due to a
  1811. * UART bug of some kind. So, we clamp the timeout parameter at
  1812. * 2*info->timeout.
  1813. */
  1814. if (!timeout || timeout > 2 * info->timeout)
  1815. timeout = 2 * info->timeout;
  1816. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1817. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
  1818. timeout, char_time);
  1819. printk("jiff=%lu...", jiffies);
  1820. #endif
  1821. while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
  1822. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1823. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1824. #endif
  1825. schedule_timeout_interruptible(char_time);
  1826. if (signal_pending(current))
  1827. break;
  1828. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1829. break;
  1830. }
  1831. set_current_state(TASK_RUNNING);
  1832. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1833. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1834. #endif
  1835. }
  1836. /*
  1837. * This routine is called by tty_hangup() when a hangup is signaled.
  1838. */
  1839. static void mxser_hangup(struct tty_struct *tty)
  1840. {
  1841. struct mxser_port *info = tty->driver_data;
  1842. mxser_flush_buffer(tty);
  1843. mxser_shutdown(info);
  1844. info->event = 0;
  1845. info->count = 0;
  1846. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1847. info->tty = NULL;
  1848. wake_up_interruptible(&info->open_wait);
  1849. }
  1850. /*
  1851. * mxser_rs_break() --- routine which turns the break handling on or off
  1852. */
  1853. static void mxser_rs_break(struct tty_struct *tty, int break_state)
  1854. {
  1855. struct mxser_port *info = tty->driver_data;
  1856. unsigned long flags;
  1857. spin_lock_irqsave(&info->slock, flags);
  1858. if (break_state == -1)
  1859. outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
  1860. info->ioaddr + UART_LCR);
  1861. else
  1862. outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
  1863. info->ioaddr + UART_LCR);
  1864. spin_unlock_irqrestore(&info->slock, flags);
  1865. }
  1866. static void mxser_receive_chars(struct mxser_port *port, int *status)
  1867. {
  1868. struct tty_struct *tty = port->tty;
  1869. unsigned char ch, gdl;
  1870. int ignored = 0;
  1871. int cnt = 0;
  1872. int recv_room;
  1873. int max = 256;
  1874. recv_room = tty->receive_room;
  1875. if ((recv_room == 0) && (!port->ldisc_stop_rx))
  1876. mxser_stoprx(tty);
  1877. if (port->board->chip_flag != MOXA_OTHER_UART) {
  1878. if (*status & UART_LSR_SPECIAL)
  1879. goto intr_old;
  1880. if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
  1881. (*status & MOXA_MUST_LSR_RERR))
  1882. goto intr_old;
  1883. if (*status & MOXA_MUST_LSR_RERR)
  1884. goto intr_old;
  1885. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1886. if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
  1887. gdl &= MOXA_MUST_GDL_MASK;
  1888. if (gdl >= recv_room) {
  1889. if (!port->ldisc_stop_rx)
  1890. mxser_stoprx(tty);
  1891. }
  1892. while (gdl--) {
  1893. ch = inb(port->ioaddr + UART_RX);
  1894. tty_insert_flip_char(tty, ch, 0);
  1895. cnt++;
  1896. }
  1897. goto end_intr;
  1898. }
  1899. intr_old:
  1900. do {
  1901. if (max-- < 0)
  1902. break;
  1903. ch = inb(port->ioaddr + UART_RX);
  1904. if (port->board->chip_flag && (*status & UART_LSR_OE))
  1905. outb(0x23, port->ioaddr + UART_FCR);
  1906. *status &= port->read_status_mask;
  1907. if (*status & port->ignore_status_mask) {
  1908. if (++ignored > 100)
  1909. break;
  1910. } else {
  1911. char flag = 0;
  1912. if (*status & UART_LSR_SPECIAL) {
  1913. if (*status & UART_LSR_BI) {
  1914. flag = TTY_BREAK;
  1915. port->icount.brk++;
  1916. if (port->flags & ASYNC_SAK)
  1917. do_SAK(tty);
  1918. } else if (*status & UART_LSR_PE) {
  1919. flag = TTY_PARITY;
  1920. port->icount.parity++;
  1921. } else if (*status & UART_LSR_FE) {
  1922. flag = TTY_FRAME;
  1923. port->icount.frame++;
  1924. } else if (*status & UART_LSR_OE) {
  1925. flag = TTY_OVERRUN;
  1926. port->icount.overrun++;
  1927. } else
  1928. flag = TTY_BREAK;
  1929. }
  1930. tty_insert_flip_char(tty, ch, flag);
  1931. cnt++;
  1932. if (cnt >= recv_room) {
  1933. if (!port->ldisc_stop_rx)
  1934. mxser_stoprx(tty);
  1935. break;
  1936. }
  1937. }
  1938. if (port->board->chip_flag)
  1939. break;
  1940. *status = inb(port->ioaddr + UART_LSR);
  1941. } while (*status & UART_LSR_DR);
  1942. end_intr:
  1943. mxvar_log.rxcnt[port->tty->index] += cnt;
  1944. port->mon_data.rxcnt += cnt;
  1945. port->mon_data.up_rxcnt += cnt;
  1946. tty_flip_buffer_push(tty);
  1947. }
  1948. static void mxser_transmit_chars(struct mxser_port *port)
  1949. {
  1950. int count, cnt;
  1951. if (port->x_char) {
  1952. outb(port->x_char, port->ioaddr + UART_TX);
  1953. port->x_char = 0;
  1954. mxvar_log.txcnt[port->tty->index]++;
  1955. port->mon_data.txcnt++;
  1956. port->mon_data.up_txcnt++;
  1957. port->icount.tx++;
  1958. return;
  1959. }
  1960. if (port->xmit_buf == 0)
  1961. return;
  1962. if ((port->xmit_cnt <= 0) || port->tty->stopped ||
  1963. (port->tty->hw_stopped &&
  1964. (port->type != PORT_16550A) &&
  1965. (!port->board->chip_flag))) {
  1966. port->IER &= ~UART_IER_THRI;
  1967. outb(port->IER, port->ioaddr + UART_IER);
  1968. return;
  1969. }
  1970. cnt = port->xmit_cnt;
  1971. count = port->xmit_fifo_size;
  1972. do {
  1973. outb(port->xmit_buf[port->xmit_tail++],
  1974. port->ioaddr + UART_TX);
  1975. port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  1976. if (--port->xmit_cnt <= 0)
  1977. break;
  1978. } while (--count > 0);
  1979. mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
  1980. port->mon_data.txcnt += (cnt - port->xmit_cnt);
  1981. port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
  1982. port->icount.tx += (cnt - port->xmit_cnt);
  1983. if (port->xmit_cnt < WAKEUP_CHARS)
  1984. tty_wakeup(port->tty);
  1985. if (port->xmit_cnt <= 0) {
  1986. port->IER &= ~UART_IER_THRI;
  1987. outb(port->IER, port->ioaddr + UART_IER);
  1988. }
  1989. }
  1990. /*
  1991. * This is the serial driver's generic interrupt routine
  1992. */
  1993. static irqreturn_t mxser_interrupt(int irq, void *dev_id)
  1994. {
  1995. int status, iir, i;
  1996. struct mxser_board *brd = NULL;
  1997. struct mxser_port *port;
  1998. int max, irqbits, bits, msr;
  1999. unsigned int int_cnt, pass_counter = 0;
  2000. int handled = IRQ_NONE;
  2001. for (i = 0; i < MXSER_BOARDS; i++)
  2002. if (dev_id == &mxser_boards[i]) {
  2003. brd = dev_id;
  2004. break;
  2005. }
  2006. if (i == MXSER_BOARDS)
  2007. goto irq_stop;
  2008. if (brd == NULL)
  2009. goto irq_stop;
  2010. max = brd->info->nports;
  2011. while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
  2012. irqbits = inb(brd->vector) & brd->vector_mask;
  2013. if (irqbits == brd->vector_mask)
  2014. break;
  2015. handled = IRQ_HANDLED;
  2016. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  2017. if (irqbits == brd->vector_mask)
  2018. break;
  2019. if (bits & irqbits)
  2020. continue;
  2021. port = &brd->ports[i];
  2022. int_cnt = 0;
  2023. spin_lock(&port->slock);
  2024. do {
  2025. iir = inb(port->ioaddr + UART_IIR);
  2026. if (iir & UART_IIR_NO_INT)
  2027. break;
  2028. iir &= MOXA_MUST_IIR_MASK;
  2029. if (!port->tty ||
  2030. (port->flags & ASYNC_CLOSING) ||
  2031. !(port->flags &
  2032. ASYNC_INITIALIZED)) {
  2033. status = inb(port->ioaddr + UART_LSR);
  2034. outb(0x27, port->ioaddr + UART_FCR);
  2035. inb(port->ioaddr + UART_MSR);
  2036. break;
  2037. }
  2038. status = inb(port->ioaddr + UART_LSR);
  2039. if (status & UART_LSR_PE)
  2040. port->err_shadow |= NPPI_NOTIFY_PARITY;
  2041. if (status & UART_LSR_FE)
  2042. port->err_shadow |= NPPI_NOTIFY_FRAMING;
  2043. if (status & UART_LSR_OE)
  2044. port->err_shadow |=
  2045. NPPI_NOTIFY_HW_OVERRUN;
  2046. if (status & UART_LSR_BI)
  2047. port->err_shadow |= NPPI_NOTIFY_BREAK;
  2048. if (port->board->chip_flag) {
  2049. if (iir == MOXA_MUST_IIR_GDA ||
  2050. iir == MOXA_MUST_IIR_RDA ||
  2051. iir == MOXA_MUST_IIR_RTO ||
  2052. iir == MOXA_MUST_IIR_LSR)
  2053. mxser_receive_chars(port,
  2054. &status);
  2055. } else {
  2056. status &= port->read_status_mask;
  2057. if (status & UART_LSR_DR)
  2058. mxser_receive_chars(port,
  2059. &status);
  2060. }
  2061. msr = inb(port->ioaddr + UART_MSR);
  2062. if (msr & UART_MSR_ANY_DELTA)
  2063. mxser_check_modem_status(port, msr);
  2064. if (port->board->chip_flag) {
  2065. if (iir == 0x02 && (status &
  2066. UART_LSR_THRE))
  2067. mxser_transmit_chars(port);
  2068. } else {
  2069. if (status & UART_LSR_THRE)
  2070. mxser_transmit_chars(port);
  2071. }
  2072. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  2073. spin_unlock(&port->slock);
  2074. }
  2075. }
  2076. irq_stop:
  2077. return handled;
  2078. }
  2079. static const struct tty_operations mxser_ops = {
  2080. .open = mxser_open,
  2081. .close = mxser_close,
  2082. .write = mxser_write,
  2083. .put_char = mxser_put_char,
  2084. .flush_chars = mxser_flush_chars,
  2085. .write_room = mxser_write_room,
  2086. .chars_in_buffer = mxser_chars_in_buffer,
  2087. .flush_buffer = mxser_flush_buffer,
  2088. .ioctl = mxser_ioctl,
  2089. .throttle = mxser_throttle,
  2090. .unthrottle = mxser_unthrottle,
  2091. .set_termios = mxser_set_termios,
  2092. .stop = mxser_stop,
  2093. .start = mxser_start,
  2094. .hangup = mxser_hangup,
  2095. .break_ctl = mxser_rs_break,
  2096. .wait_until_sent = mxser_wait_until_sent,
  2097. .tiocmget = mxser_tiocmget,
  2098. .tiocmset = mxser_tiocmset,
  2099. };
  2100. /*
  2101. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  2102. */
  2103. static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
  2104. unsigned int irq)
  2105. {
  2106. if (irq)
  2107. free_irq(brd->irq, brd);
  2108. if (pdev != NULL) { /* PCI */
  2109. #ifdef CONFIG_PCI
  2110. pci_release_region(pdev, 2);
  2111. pci_release_region(pdev, 3);
  2112. #endif
  2113. } else {
  2114. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2115. release_region(brd->vector, 1);
  2116. }
  2117. }
  2118. static int __devinit mxser_initbrd(struct mxser_board *brd,
  2119. struct pci_dev *pdev)
  2120. {
  2121. struct mxser_port *info;
  2122. unsigned int i;
  2123. int retval;
  2124. printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
  2125. for (i = 0; i < brd->info->nports; i++) {
  2126. info = &brd->ports[i];
  2127. info->board = brd;
  2128. info->stop_rx = 0;
  2129. info->ldisc_stop_rx = 0;
  2130. /* Enhance mode enabled here */
  2131. if (brd->chip_flag != MOXA_OTHER_UART)
  2132. ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
  2133. info->flags = ASYNC_SHARE_IRQ;
  2134. info->type = brd->uart_type;
  2135. process_txrx_fifo(info);
  2136. info->custom_divisor = info->baud_base * 16;
  2137. info->close_delay = 5 * HZ / 10;
  2138. info->closing_wait = 30 * HZ;
  2139. info->normal_termios = mxvar_sdriver->init_termios;
  2140. init_waitqueue_head(&info->open_wait);
  2141. init_waitqueue_head(&info->delta_msr_wait);
  2142. info->speed = 9600;
  2143. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  2144. info->err_shadow = 0;
  2145. spin_lock_init(&info->slock);
  2146. /* before set INT ISR, disable all int */
  2147. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  2148. info->ioaddr + UART_IER);
  2149. }
  2150. retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
  2151. brd);
  2152. if (retval) {
  2153. printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
  2154. "conflict with another device.\n",
  2155. brd->info->name, brd->irq);
  2156. /* We hold resources, we need to release them. */
  2157. mxser_release_res(brd, pdev, 0);
  2158. }
  2159. return retval;
  2160. }
  2161. static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
  2162. {
  2163. int id, i, bits;
  2164. unsigned short regs[16], irq;
  2165. unsigned char scratch, scratch2;
  2166. brd->chip_flag = MOXA_OTHER_UART;
  2167. id = mxser_read_register(cap, regs);
  2168. switch (id) {
  2169. case C168_ASIC_ID:
  2170. brd->info = &mxser_cards[0];
  2171. break;
  2172. case C104_ASIC_ID:
  2173. brd->info = &mxser_cards[1];
  2174. break;
  2175. case CI104J_ASIC_ID:
  2176. brd->info = &mxser_cards[2];
  2177. break;
  2178. case C102_ASIC_ID:
  2179. brd->info = &mxser_cards[5];
  2180. break;
  2181. case CI132_ASIC_ID:
  2182. brd->info = &mxser_cards[6];
  2183. break;
  2184. case CI134_ASIC_ID:
  2185. brd->info = &mxser_cards[7];
  2186. break;
  2187. default:
  2188. return 0;
  2189. }
  2190. irq = 0;
  2191. /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
  2192. Flag-hack checks if configuration should be read as 2-port here. */
  2193. if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
  2194. irq = regs[9] & 0xF000;
  2195. irq = irq | (irq >> 4);
  2196. if (irq != (regs[9] & 0xFF00))
  2197. return MXSER_ERR_IRQ_CONFLIT;
  2198. } else if (brd->info->nports == 4) {
  2199. irq = regs[9] & 0xF000;
  2200. irq = irq | (irq >> 4);
  2201. irq = irq | (irq >> 8);
  2202. if (irq != regs[9])
  2203. return MXSER_ERR_IRQ_CONFLIT;
  2204. } else if (brd->info->nports == 8) {
  2205. irq = regs[9] & 0xF000;
  2206. irq = irq | (irq >> 4);
  2207. irq = irq | (irq >> 8);
  2208. if ((irq != regs[9]) || (irq != regs[10]))
  2209. return MXSER_ERR_IRQ_CONFLIT;
  2210. }
  2211. if (!irq)
  2212. return MXSER_ERR_IRQ;
  2213. brd->irq = ((int)(irq & 0xF000) >> 12);
  2214. for (i = 0; i < 8; i++)
  2215. brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
  2216. if ((regs[12] & 0x80) == 0)
  2217. return MXSER_ERR_VECTOR;
  2218. brd->vector = (int)regs[11]; /* interrupt vector */
  2219. if (id == 1)
  2220. brd->vector_mask = 0x00FF;
  2221. else
  2222. brd->vector_mask = 0x000F;
  2223. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2224. if (regs[12] & bits) {
  2225. brd->ports[i].baud_base = 921600;
  2226. brd->ports[i].max_baud = 921600;
  2227. } else {
  2228. brd->ports[i].baud_base = 115200;
  2229. brd->ports[i].max_baud = 115200;
  2230. }
  2231. }
  2232. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2233. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2234. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2235. outb(scratch2, cap + UART_LCR);
  2236. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2237. scratch = inb(cap + UART_IIR);
  2238. if (scratch & 0xC0)
  2239. brd->uart_type = PORT_16550A;
  2240. else
  2241. brd->uart_type = PORT_16450;
  2242. if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
  2243. "mxser(IO)"))
  2244. return MXSER_ERR_IOADDR;
  2245. if (!request_region(brd->vector, 1, "mxser(vector)")) {
  2246. release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
  2247. return MXSER_ERR_VECTOR;
  2248. }
  2249. return brd->info->nports;
  2250. }
  2251. static int __devinit mxser_probe(struct pci_dev *pdev,
  2252. const struct pci_device_id *ent)
  2253. {
  2254. #ifdef CONFIG_PCI
  2255. struct mxser_board *brd;
  2256. unsigned int i, j;
  2257. unsigned long ioaddress;
  2258. int retval = -EINVAL;
  2259. for (i = 0; i < MXSER_BOARDS; i++)
  2260. if (mxser_boards[i].info == NULL)
  2261. break;
  2262. if (i >= MXSER_BOARDS) {
  2263. printk(KERN_ERR "Too many Smartio/Industio family boards found "
  2264. "(maximum %d), board not configured\n", MXSER_BOARDS);
  2265. goto err;
  2266. }
  2267. brd = &mxser_boards[i];
  2268. brd->idx = i * MXSER_PORTS_PER_BOARD;
  2269. printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
  2270. mxser_cards[ent->driver_data].name,
  2271. pdev->bus->number, PCI_SLOT(pdev->devfn));
  2272. retval = pci_enable_device(pdev);
  2273. if (retval) {
  2274. printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
  2275. goto err;
  2276. }
  2277. /* io address */
  2278. ioaddress = pci_resource_start(pdev, 2);
  2279. retval = pci_request_region(pdev, 2, "mxser(IO)");
  2280. if (retval)
  2281. goto err;
  2282. brd->info = &mxser_cards[ent->driver_data];
  2283. for (i = 0; i < brd->info->nports; i++)
  2284. brd->ports[i].ioaddr = ioaddress + 8 * i;
  2285. /* vector */
  2286. ioaddress = pci_resource_start(pdev, 3);
  2287. retval = pci_request_region(pdev, 3, "mxser(vector)");
  2288. if (retval)
  2289. goto err_relio;
  2290. brd->vector = ioaddress;
  2291. /* irq */
  2292. brd->irq = pdev->irq;
  2293. brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
  2294. brd->uart_type = PORT_16550A;
  2295. brd->vector_mask = 0;
  2296. for (i = 0; i < brd->info->nports; i++) {
  2297. for (j = 0; j < UART_INFO_NUM; j++) {
  2298. if (Gpci_uart_info[j].type == brd->chip_flag) {
  2299. brd->ports[i].max_baud =
  2300. Gpci_uart_info[j].max_baud;
  2301. /* exception....CP-102 */
  2302. if (brd->info->flags & MXSER_HIGHBAUD)
  2303. brd->ports[i].max_baud = 921600;
  2304. break;
  2305. }
  2306. }
  2307. }
  2308. if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
  2309. for (i = 0; i < brd->info->nports; i++) {
  2310. if (i < 4)
  2311. brd->ports[i].opmode_ioaddr = ioaddress + 4;
  2312. else
  2313. brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
  2314. }
  2315. outb(0, ioaddress + 4); /* default set to RS232 mode */
  2316. outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
  2317. }
  2318. for (i = 0; i < brd->info->nports; i++) {
  2319. brd->vector_mask |= (1 << i);
  2320. brd->ports[i].baud_base = 921600;
  2321. }
  2322. /* mxser_initbrd will hook ISR. */
  2323. retval = mxser_initbrd(brd, pdev);
  2324. if (retval)
  2325. goto err_null;
  2326. for (i = 0; i < brd->info->nports; i++)
  2327. tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
  2328. pci_set_drvdata(pdev, brd);
  2329. return 0;
  2330. err_relio:
  2331. pci_release_region(pdev, 2);
  2332. err_null:
  2333. brd->info = NULL;
  2334. err:
  2335. return retval;
  2336. #else
  2337. return -ENODEV;
  2338. #endif
  2339. }
  2340. static void __devexit mxser_remove(struct pci_dev *pdev)
  2341. {
  2342. struct mxser_board *brd = pci_get_drvdata(pdev);
  2343. unsigned int i;
  2344. for (i = 0; i < brd->info->nports; i++)
  2345. tty_unregister_device(mxvar_sdriver, brd->idx + i);
  2346. mxser_release_res(brd, pdev, 1);
  2347. brd->info = NULL;
  2348. }
  2349. static struct pci_driver mxser_driver = {
  2350. .name = "mxser",
  2351. .id_table = mxser_pcibrds,
  2352. .probe = mxser_probe,
  2353. .remove = __devexit_p(mxser_remove)
  2354. };
  2355. static int __init mxser_module_init(void)
  2356. {
  2357. struct mxser_board *brd;
  2358. unsigned long cap;
  2359. unsigned int i, m, isaloop;
  2360. int retval, b;
  2361. pr_debug("Loading module mxser ...\n");
  2362. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  2363. if (!mxvar_sdriver)
  2364. return -ENOMEM;
  2365. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
  2366. MXSER_VERSION);
  2367. /* Initialize the tty_driver structure */
  2368. mxvar_sdriver->owner = THIS_MODULE;
  2369. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  2370. mxvar_sdriver->name = "ttyMI";
  2371. mxvar_sdriver->major = ttymajor;
  2372. mxvar_sdriver->minor_start = 0;
  2373. mxvar_sdriver->num = MXSER_PORTS + 1;
  2374. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  2375. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  2376. mxvar_sdriver->init_termios = tty_std_termios;
  2377. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  2378. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
  2379. tty_set_operations(mxvar_sdriver, &mxser_ops);
  2380. retval = tty_register_driver(mxvar_sdriver);
  2381. if (retval) {
  2382. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
  2383. "tty driver !\n");
  2384. goto err_put;
  2385. }
  2386. mxvar_diagflag = 0;
  2387. m = 0;
  2388. /* Start finding ISA boards here */
  2389. for (isaloop = 0; isaloop < 2; isaloop++)
  2390. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  2391. if (!isaloop)
  2392. cap = mxserBoardCAP[b]; /* predefined */
  2393. else
  2394. cap = ioaddr[b]; /* module param */
  2395. if (!cap)
  2396. continue;
  2397. brd = &mxser_boards[m];
  2398. retval = mxser_get_ISA_conf(cap, brd);
  2399. if (retval != 0)
  2400. printk(KERN_INFO "Found MOXA %s board "
  2401. "(CAP=0x%x)\n",
  2402. brd->info->name, ioaddr[b]);
  2403. if (retval <= 0) {
  2404. if (retval == MXSER_ERR_IRQ)
  2405. printk(KERN_ERR "Invalid interrupt "
  2406. "number, board not "
  2407. "configured\n");
  2408. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  2409. printk(KERN_ERR "Invalid interrupt "
  2410. "number, board not "
  2411. "configured\n");
  2412. else if (retval == MXSER_ERR_VECTOR)
  2413. printk(KERN_ERR "Invalid interrupt "
  2414. "vector, board not "
  2415. "configured\n");
  2416. else if (retval == MXSER_ERR_IOADDR)
  2417. printk(KERN_ERR "Invalid I/O address, "
  2418. "board not configured\n");
  2419. brd->info = NULL;
  2420. continue;
  2421. }
  2422. /* mxser_initbrd will hook ISR. */
  2423. if (mxser_initbrd(brd, NULL) < 0) {
  2424. brd->info = NULL;
  2425. continue;
  2426. }
  2427. brd->idx = m * MXSER_PORTS_PER_BOARD;
  2428. for (i = 0; i < brd->info->nports; i++)
  2429. tty_register_device(mxvar_sdriver, brd->idx + i,
  2430. NULL);
  2431. m++;
  2432. }
  2433. retval = pci_register_driver(&mxser_driver);
  2434. if (retval) {
  2435. printk(KERN_ERR "Can't register pci driver\n");
  2436. if (!m) {
  2437. retval = -ENODEV;
  2438. goto err_unr;
  2439. } /* else: we have some ISA cards under control */
  2440. }
  2441. pr_debug("Done.\n");
  2442. return 0;
  2443. err_unr:
  2444. tty_unregister_driver(mxvar_sdriver);
  2445. err_put:
  2446. put_tty_driver(mxvar_sdriver);
  2447. return retval;
  2448. }
  2449. static void __exit mxser_module_exit(void)
  2450. {
  2451. unsigned int i, j;
  2452. pr_debug("Unloading module mxser ...\n");
  2453. pci_unregister_driver(&mxser_driver);
  2454. for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
  2455. if (mxser_boards[i].info != NULL)
  2456. for (j = 0; j < mxser_boards[i].info->nports; j++)
  2457. tty_unregister_device(mxvar_sdriver,
  2458. mxser_boards[i].idx + j);
  2459. tty_unregister_driver(mxvar_sdriver);
  2460. put_tty_driver(mxvar_sdriver);
  2461. for (i = 0; i < MXSER_BOARDS; i++)
  2462. if (mxser_boards[i].info != NULL)
  2463. mxser_release_res(&mxser_boards[i], NULL, 1);
  2464. pr_debug("Done.\n");
  2465. }
  2466. module_init(mxser_module_init);
  2467. module_exit(mxser_module_exit);