mxser.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
  4. *
  5. * Copyright (C) 1999-2006 Moxa Technologies ([email protected]).
  6. * Copyright (C) 2006-2008 Jiri Slaby <[email protected]>
  7. *
  8. * This code is loosely based on the 1.8 moxa driver which is based on
  9. * Linux serial driver, written by Linus Torvalds, Theodore T'so and
  10. * others.
  11. *
  12. * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
  13. * <[email protected]>. The original 1.8 code is available on
  14. * www.moxa.com.
  15. * - Fixed x86_64 cleanness
  16. */
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/timer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/serial.h>
  26. #include <linux/serial_reg.h>
  27. #include <linux/major.h>
  28. #include <linux/string.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/ioport.h>
  32. #include <linux/mm.h>
  33. #include <linux/delay.h>
  34. #include <linux/pci.h>
  35. #include <linux/bitops.h>
  36. #include <linux/slab.h>
  37. #include <linux/ratelimit.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <linux/uaccess.h>
  41. /*
  42. * Semi-public control interfaces
  43. */
  44. /*
  45. * MOXA ioctls
  46. */
  47. #define MOXA 0x400
  48. #define MOXA_SET_OP_MODE (MOXA + 66)
  49. #define MOXA_GET_OP_MODE (MOXA + 67)
  50. #define RS232_MODE 0
  51. #define RS485_2WIRE_MODE 1
  52. #define RS422_MODE 2
  53. #define RS485_4WIRE_MODE 3
  54. #define OP_MODE_MASK 3
  55. /* --------------------------------------------------- */
  56. /*
  57. * Follow just what Moxa Must chip defines.
  58. *
  59. * When LCR register (offset 0x03) is written the following value, the Must chip
  60. * will enter enhanced mode. And a write to EFR (offset 0x02) bit 6,7 will
  61. * change bank.
  62. */
  63. #define MOXA_MUST_ENTER_ENHANCED 0xBF
  64. /* when enhanced mode is enabled, access to general bank register */
  65. #define MOXA_MUST_GDL_REGISTER 0x07
  66. #define MOXA_MUST_GDL_MASK 0x7F
  67. #define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
  68. #define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
  69. /* enhanced register bank select and enhanced mode setting register */
  70. /* This works only when LCR register equals to 0xBF */
  71. #define MOXA_MUST_EFR_REGISTER 0x02
  72. #define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enhanced mode enable */
  73. /* enhanced register bank set 0, 1, 2 */
  74. #define MOXA_MUST_EFR_BANK0 0x00
  75. #define MOXA_MUST_EFR_BANK1 0x40
  76. #define MOXA_MUST_EFR_BANK2 0x80
  77. #define MOXA_MUST_EFR_BANK3 0xC0
  78. #define MOXA_MUST_EFR_BANK_MASK 0xC0
  79. /* set XON1 value register, when LCR=0xBF and change to bank0 */
  80. #define MOXA_MUST_XON1_REGISTER 0x04
  81. /* set XON2 value register, when LCR=0xBF and change to bank0 */
  82. #define MOXA_MUST_XON2_REGISTER 0x05
  83. /* set XOFF1 value register, when LCR=0xBF and change to bank0 */
  84. #define MOXA_MUST_XOFF1_REGISTER 0x06
  85. /* set XOFF2 value register, when LCR=0xBF and change to bank0 */
  86. #define MOXA_MUST_XOFF2_REGISTER 0x07
  87. #define MOXA_MUST_RBRTL_REGISTER 0x04
  88. #define MOXA_MUST_RBRTH_REGISTER 0x05
  89. #define MOXA_MUST_RBRTI_REGISTER 0x06
  90. #define MOXA_MUST_THRTL_REGISTER 0x07
  91. #define MOXA_MUST_ENUM_REGISTER 0x04
  92. #define MOXA_MUST_HWID_REGISTER 0x05
  93. #define MOXA_MUST_ECR_REGISTER 0x06
  94. #define MOXA_MUST_CSR_REGISTER 0x07
  95. #define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20 /* good data mode enable */
  96. #define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10 /* only good data put into RxFIFO */
  97. #define MOXA_MUST_IER_ECTSI 0x80 /* enable CTS interrupt */
  98. #define MOXA_MUST_IER_ERTSI 0x40 /* enable RTS interrupt */
  99. #define MOXA_MUST_IER_XINT 0x20 /* enable Xon/Xoff interrupt */
  100. #define MOXA_MUST_IER_EGDAI 0x10 /* enable GDA interrupt */
  101. #define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
  102. /* GDA interrupt pending */
  103. #define MOXA_MUST_IIR_GDA 0x1C
  104. #define MOXA_MUST_IIR_RDA 0x04
  105. #define MOXA_MUST_IIR_RTO 0x0C
  106. #define MOXA_MUST_IIR_LSR 0x06
  107. /* received Xon/Xoff or specical interrupt pending */
  108. #define MOXA_MUST_IIR_XSC 0x10
  109. /* RTS/CTS change state interrupt pending */
  110. #define MOXA_MUST_IIR_RTSCTS 0x20
  111. #define MOXA_MUST_IIR_MASK 0x3E
  112. #define MOXA_MUST_MCR_XON_FLAG 0x40
  113. #define MOXA_MUST_MCR_XON_ANY 0x80
  114. #define MOXA_MUST_MCR_TX_XON 0x08
  115. #define MOXA_MUST_EFR_SF_MASK 0x0F /* software flow control on chip mask value */
  116. #define MOXA_MUST_EFR_SF_TX1 0x08 /* send Xon1/Xoff1 */
  117. #define MOXA_MUST_EFR_SF_TX2 0x04 /* send Xon2/Xoff2 */
  118. #define MOXA_MUST_EFR_SF_TX12 0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */
  119. #define MOXA_MUST_EFR_SF_TX_NO 0x00 /* don't send Xon/Xoff */
  120. #define MOXA_MUST_EFR_SF_TX_MASK 0x0C /* Tx software flow control mask */
  121. #define MOXA_MUST_EFR_SF_RX_NO 0x00 /* don't receive Xon/Xoff */
  122. #define MOXA_MUST_EFR_SF_RX1 0x02 /* receive Xon1/Xoff1 */
  123. #define MOXA_MUST_EFR_SF_RX2 0x01 /* receive Xon2/Xoff2 */
  124. #define MOXA_MUST_EFR_SF_RX12 0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */
  125. #define MOXA_MUST_EFR_SF_RX_MASK 0x03 /* Rx software flow control mask */
  126. #define MXSERMAJOR 174
  127. #define MXSER_BOARDS 4 /* Max. boards */
  128. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  129. #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
  130. #define MXSER_ISR_PASS_LIMIT 100
  131. #define WAKEUP_CHARS 256
  132. #define MXSER_BAUD_BASE 921600
  133. #define MXSER_CUSTOM_DIVISOR (MXSER_BAUD_BASE * 16)
  134. #define PCI_DEVICE_ID_MOXA_RC7000 0x0001
  135. #define PCI_DEVICE_ID_MOXA_CP102 0x1020
  136. #define PCI_DEVICE_ID_MOXA_CP102UL 0x1021
  137. #define PCI_DEVICE_ID_MOXA_CP102U 0x1022
  138. #define PCI_DEVICE_ID_MOXA_CP102UF 0x1023
  139. #define PCI_DEVICE_ID_MOXA_C104 0x1040
  140. #define PCI_DEVICE_ID_MOXA_CP104U 0x1041
  141. #define PCI_DEVICE_ID_MOXA_CP104JU 0x1042
  142. #define PCI_DEVICE_ID_MOXA_CP104EL 0x1043
  143. #define PCI_DEVICE_ID_MOXA_POS104UL 0x1044
  144. #define PCI_DEVICE_ID_MOXA_CB108 0x1080
  145. #define PCI_DEVICE_ID_MOXA_CP112UL 0x1120
  146. #define PCI_DEVICE_ID_MOXA_CT114 0x1140
  147. #define PCI_DEVICE_ID_MOXA_CP114 0x1141
  148. #define PCI_DEVICE_ID_MOXA_CB114 0x1142
  149. #define PCI_DEVICE_ID_MOXA_CP114UL 0x1143
  150. #define PCI_DEVICE_ID_MOXA_CP118U 0x1180
  151. #define PCI_DEVICE_ID_MOXA_CP118EL 0x1181
  152. #define PCI_DEVICE_ID_MOXA_CP132 0x1320
  153. #define PCI_DEVICE_ID_MOXA_CP132U 0x1321
  154. #define PCI_DEVICE_ID_MOXA_CP134U 0x1340
  155. #define PCI_DEVICE_ID_MOXA_CB134I 0x1341
  156. #define PCI_DEVICE_ID_MOXA_CP138U 0x1380
  157. #define PCI_DEVICE_ID_MOXA_C168 0x1680
  158. #define PCI_DEVICE_ID_MOXA_CP168U 0x1681
  159. #define PCI_DEVICE_ID_MOXA_CP168EL 0x1682
  160. #define MXSER_NPORTS(ddata) ((ddata) & 0xffU)
  161. #define MXSER_HIGHBAUD 0x0100
  162. enum mxser_must_hwid {
  163. MOXA_OTHER_UART = 0x00,
  164. MOXA_MUST_MU150_HWID = 0x01,
  165. MOXA_MUST_MU860_HWID = 0x02,
  166. };
  167. static const struct {
  168. u8 type;
  169. u8 fifo_size;
  170. u8 rx_high_water;
  171. u8 rx_low_water;
  172. speed_t max_baud;
  173. } Gpci_uart_info[] = {
  174. { MOXA_OTHER_UART, 16, 14, 1, 921600 },
  175. { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
  176. { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
  177. };
  178. #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
  179. /* driver_data correspond to the lines in the structure above
  180. see also ISA probe function before you change something */
  181. static const struct pci_device_id mxser_pcibrds[] = {
  182. { PCI_DEVICE_DATA(MOXA, C168, 8) },
  183. { PCI_DEVICE_DATA(MOXA, C104, 4) },
  184. { PCI_DEVICE_DATA(MOXA, CP132, 2) },
  185. { PCI_DEVICE_DATA(MOXA, CP114, 4) },
  186. { PCI_DEVICE_DATA(MOXA, CT114, 4) },
  187. { PCI_DEVICE_DATA(MOXA, CP102, 2 | MXSER_HIGHBAUD) },
  188. { PCI_DEVICE_DATA(MOXA, CP104U, 4) },
  189. { PCI_DEVICE_DATA(MOXA, CP168U, 8) },
  190. { PCI_DEVICE_DATA(MOXA, CP132U, 2) },
  191. { PCI_DEVICE_DATA(MOXA, CP134U, 4) },
  192. { PCI_DEVICE_DATA(MOXA, CP104JU, 4) },
  193. { PCI_DEVICE_DATA(MOXA, RC7000, 8) }, /* RC7000 */
  194. { PCI_DEVICE_DATA(MOXA, CP118U, 8) },
  195. { PCI_DEVICE_DATA(MOXA, CP102UL, 2) },
  196. { PCI_DEVICE_DATA(MOXA, CP102U, 2) },
  197. { PCI_DEVICE_DATA(MOXA, CP118EL, 8) },
  198. { PCI_DEVICE_DATA(MOXA, CP168EL, 8) },
  199. { PCI_DEVICE_DATA(MOXA, CP104EL, 4) },
  200. { PCI_DEVICE_DATA(MOXA, CB108, 8) },
  201. { PCI_DEVICE_DATA(MOXA, CB114, 4) },
  202. { PCI_DEVICE_DATA(MOXA, CB134I, 4) },
  203. { PCI_DEVICE_DATA(MOXA, CP138U, 8) },
  204. { PCI_DEVICE_DATA(MOXA, POS104UL, 4) },
  205. { PCI_DEVICE_DATA(MOXA, CP114UL, 4) },
  206. { PCI_DEVICE_DATA(MOXA, CP102UF, 2) },
  207. { PCI_DEVICE_DATA(MOXA, CP112UL, 2) },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  211. static int ttymajor = MXSERMAJOR;
  212. /* Variables for insmod */
  213. MODULE_AUTHOR("Casper Yang");
  214. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  215. module_param(ttymajor, int, 0);
  216. MODULE_LICENSE("GPL");
  217. struct mxser_board;
  218. struct mxser_port {
  219. struct tty_port port;
  220. struct mxser_board *board;
  221. unsigned long ioaddr;
  222. unsigned long opmode_ioaddr;
  223. u8 rx_high_water;
  224. u8 rx_low_water;
  225. int type; /* UART type */
  226. unsigned char x_char; /* xon/xoff character */
  227. u8 IER; /* Interrupt Enable Register */
  228. u8 MCR; /* Modem control register */
  229. u8 FCR; /* FIFO control register */
  230. struct async_icount icount; /* kernel counters for 4 input interrupts */
  231. unsigned int timeout;
  232. u8 read_status_mask;
  233. u8 ignore_status_mask;
  234. u8 xmit_fifo_size;
  235. spinlock_t slock;
  236. };
  237. struct mxser_board {
  238. unsigned int idx;
  239. unsigned short nports;
  240. int irq;
  241. unsigned long vector;
  242. enum mxser_must_hwid must_hwid;
  243. speed_t max_baud;
  244. struct mxser_port ports[];
  245. };
  246. static DECLARE_BITMAP(mxser_boards, MXSER_BOARDS);
  247. static struct tty_driver *mxvar_sdriver;
  248. static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set,
  249. bool restore_LCR)
  250. {
  251. u8 oldlcr, efr;
  252. oldlcr = inb(baseio + UART_LCR);
  253. outb(MOXA_MUST_ENTER_ENHANCED, baseio + UART_LCR);
  254. efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
  255. efr &= ~clear;
  256. efr |= set;
  257. outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
  258. if (restore_LCR)
  259. outb(oldlcr, baseio + UART_LCR);
  260. return oldlcr;
  261. }
  262. static u8 mxser_must_select_bank(unsigned long baseio, u8 bank)
  263. {
  264. return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank,
  265. false);
  266. }
  267. static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
  268. {
  269. u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
  270. outb(value, baseio + MOXA_MUST_XON1_REGISTER);
  271. outb(oldlcr, baseio + UART_LCR);
  272. }
  273. static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
  274. {
  275. u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
  276. outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
  277. outb(oldlcr, baseio + UART_LCR);
  278. }
  279. static void mxser_set_must_fifo_value(struct mxser_port *info)
  280. {
  281. u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1);
  282. outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
  283. outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
  284. outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
  285. outb(oldlcr, info->ioaddr + UART_LCR);
  286. }
  287. static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
  288. {
  289. u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
  290. outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
  291. outb(oldlcr, baseio + UART_LCR);
  292. }
  293. static u8 mxser_get_must_hardware_id(unsigned long baseio)
  294. {
  295. u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
  296. u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER);
  297. outb(oldlcr, baseio + UART_LCR);
  298. return id;
  299. }
  300. static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
  301. {
  302. __mxser_must_set_EFR(baseio, clear, set, true);
  303. }
  304. static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
  305. {
  306. mxser_must_set_EFR(baseio,
  307. enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
  308. enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
  309. }
  310. static void mxser_must_no_sw_flow_control(unsigned long baseio)
  311. {
  312. mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0);
  313. }
  314. static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable)
  315. {
  316. mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK,
  317. enable ? MOXA_MUST_EFR_SF_TX1 : 0);
  318. }
  319. static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable)
  320. {
  321. mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK,
  322. enable ? MOXA_MUST_EFR_SF_RX1 : 0);
  323. }
  324. static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
  325. {
  326. u8 oldmcr, hwid;
  327. int i;
  328. outb(0, io + UART_LCR);
  329. mxser_must_set_enhance_mode(io, false);
  330. oldmcr = inb(io + UART_MCR);
  331. outb(0, io + UART_MCR);
  332. mxser_set_must_xon1_value(io, 0x11);
  333. if (inb(io + UART_MCR) != 0) {
  334. outb(oldmcr, io + UART_MCR);
  335. return MOXA_OTHER_UART;
  336. }
  337. hwid = mxser_get_must_hardware_id(io);
  338. for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
  339. if (hwid == Gpci_uart_info[i].type)
  340. return hwid;
  341. return MOXA_OTHER_UART;
  342. }
  343. static bool mxser_16550A_or_MUST(struct mxser_port *info)
  344. {
  345. return info->type == PORT_16550A || info->board->must_hwid;
  346. }
  347. static void mxser_process_txrx_fifo(struct mxser_port *info)
  348. {
  349. unsigned int i;
  350. if (info->type == PORT_16450 || info->type == PORT_8250) {
  351. info->rx_high_water = 1;
  352. info->rx_low_water = 1;
  353. info->xmit_fifo_size = 1;
  354. return;
  355. }
  356. for (i = 0; i < UART_INFO_NUM; i++)
  357. if (info->board->must_hwid == Gpci_uart_info[i].type) {
  358. info->rx_low_water = Gpci_uart_info[i].rx_low_water;
  359. info->rx_high_water = Gpci_uart_info[i].rx_high_water;
  360. info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
  361. break;
  362. }
  363. }
  364. static void __mxser_start_tx(struct mxser_port *info)
  365. {
  366. outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
  367. info->IER |= UART_IER_THRI;
  368. outb(info->IER, info->ioaddr + UART_IER);
  369. }
  370. static void mxser_start_tx(struct mxser_port *info)
  371. {
  372. unsigned long flags;
  373. spin_lock_irqsave(&info->slock, flags);
  374. __mxser_start_tx(info);
  375. spin_unlock_irqrestore(&info->slock, flags);
  376. }
  377. static void __mxser_stop_tx(struct mxser_port *info)
  378. {
  379. info->IER &= ~UART_IER_THRI;
  380. outb(info->IER, info->ioaddr + UART_IER);
  381. }
  382. static int mxser_carrier_raised(struct tty_port *port)
  383. {
  384. struct mxser_port *mp = container_of(port, struct mxser_port, port);
  385. return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
  386. }
  387. static void mxser_dtr_rts(struct tty_port *port, int on)
  388. {
  389. struct mxser_port *mp = container_of(port, struct mxser_port, port);
  390. unsigned long flags;
  391. u8 mcr;
  392. spin_lock_irqsave(&mp->slock, flags);
  393. mcr = inb(mp->ioaddr + UART_MCR);
  394. if (on)
  395. mcr |= UART_MCR_DTR | UART_MCR_RTS;
  396. else
  397. mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
  398. outb(mcr, mp->ioaddr + UART_MCR);
  399. spin_unlock_irqrestore(&mp->slock, flags);
  400. }
  401. static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
  402. {
  403. struct mxser_port *info = tty->driver_data;
  404. unsigned int quot = 0, baud;
  405. unsigned char cval;
  406. u64 timeout;
  407. if (newspd > info->board->max_baud)
  408. return -1;
  409. if (newspd == 134) {
  410. quot = 2 * MXSER_BAUD_BASE / 269;
  411. tty_encode_baud_rate(tty, 134, 134);
  412. } else if (newspd) {
  413. quot = MXSER_BAUD_BASE / newspd;
  414. if (quot == 0)
  415. quot = 1;
  416. baud = MXSER_BAUD_BASE / quot;
  417. tty_encode_baud_rate(tty, baud, baud);
  418. } else {
  419. quot = 0;
  420. }
  421. /*
  422. * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
  423. * u64 domain
  424. */
  425. timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
  426. do_div(timeout, MXSER_BAUD_BASE);
  427. info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
  428. if (quot) {
  429. info->MCR |= UART_MCR_DTR;
  430. outb(info->MCR, info->ioaddr + UART_MCR);
  431. } else {
  432. info->MCR &= ~UART_MCR_DTR;
  433. outb(info->MCR, info->ioaddr + UART_MCR);
  434. return 0;
  435. }
  436. cval = inb(info->ioaddr + UART_LCR);
  437. outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
  438. outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
  439. outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
  440. outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
  441. if (C_BAUD(tty) == BOTHER) {
  442. quot = MXSER_BAUD_BASE % newspd;
  443. quot *= 8;
  444. if (quot % newspd > newspd / 2) {
  445. quot /= newspd;
  446. quot++;
  447. } else
  448. quot /= newspd;
  449. mxser_set_must_enum_value(info->ioaddr, quot);
  450. } else {
  451. mxser_set_must_enum_value(info->ioaddr, 0);
  452. }
  453. return 0;
  454. }
  455. static void mxser_handle_cts(struct tty_struct *tty, struct mxser_port *info,
  456. u8 msr)
  457. {
  458. bool cts = msr & UART_MSR_CTS;
  459. if (tty->hw_stopped) {
  460. if (cts) {
  461. tty->hw_stopped = 0;
  462. if (!mxser_16550A_or_MUST(info))
  463. __mxser_start_tx(info);
  464. tty_wakeup(tty);
  465. }
  466. return;
  467. } else if (cts)
  468. return;
  469. tty->hw_stopped = 1;
  470. if (!mxser_16550A_or_MUST(info))
  471. __mxser_stop_tx(info);
  472. }
  473. /*
  474. * This routine is called to set the UART divisor registers to match
  475. * the specified baud rate for a serial port.
  476. */
  477. static void mxser_change_speed(struct tty_struct *tty,
  478. const struct ktermios *old_termios)
  479. {
  480. struct mxser_port *info = tty->driver_data;
  481. unsigned cflag, cval;
  482. cflag = tty->termios.c_cflag;
  483. if (mxser_set_baud(tty, tty_get_baud_rate(tty))) {
  484. /* Use previous rate on a failure */
  485. if (old_termios) {
  486. speed_t baud = tty_termios_baud_rate(old_termios);
  487. tty_encode_baud_rate(tty, baud, baud);
  488. }
  489. }
  490. /* byte size and parity */
  491. cval = UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
  492. if (cflag & CSTOPB)
  493. cval |= UART_LCR_STOP;
  494. if (cflag & PARENB)
  495. cval |= UART_LCR_PARITY;
  496. if (!(cflag & PARODD))
  497. cval |= UART_LCR_EPAR;
  498. if (cflag & CMSPAR)
  499. cval |= UART_LCR_SPAR;
  500. info->FCR = 0;
  501. if (info->board->must_hwid) {
  502. info->FCR |= UART_FCR_ENABLE_FIFO |
  503. MOXA_MUST_FCR_GDA_MODE_ENABLE;
  504. mxser_set_must_fifo_value(info);
  505. } else if (info->type != PORT_8250 && info->type != PORT_16450) {
  506. info->FCR |= UART_FCR_ENABLE_FIFO;
  507. switch (info->rx_high_water) {
  508. case 1:
  509. info->FCR |= UART_FCR_TRIGGER_1;
  510. break;
  511. case 4:
  512. info->FCR |= UART_FCR_TRIGGER_4;
  513. break;
  514. case 8:
  515. info->FCR |= UART_FCR_TRIGGER_8;
  516. break;
  517. default:
  518. info->FCR |= UART_FCR_TRIGGER_14;
  519. break;
  520. }
  521. }
  522. /* CTS flow control flag and modem status interrupts */
  523. info->IER &= ~UART_IER_MSI;
  524. info->MCR &= ~UART_MCR_AFE;
  525. tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
  526. if (cflag & CRTSCTS) {
  527. info->IER |= UART_IER_MSI;
  528. if (mxser_16550A_or_MUST(info)) {
  529. info->MCR |= UART_MCR_AFE;
  530. } else {
  531. mxser_handle_cts(tty, info,
  532. inb(info->ioaddr + UART_MSR));
  533. }
  534. }
  535. outb(info->MCR, info->ioaddr + UART_MCR);
  536. tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
  537. if (~cflag & CLOCAL)
  538. info->IER |= UART_IER_MSI;
  539. outb(info->IER, info->ioaddr + UART_IER);
  540. /*
  541. * Set up parity check flag
  542. */
  543. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  544. if (I_INPCK(tty))
  545. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  546. if (I_BRKINT(tty) || I_PARMRK(tty))
  547. info->read_status_mask |= UART_LSR_BI;
  548. info->ignore_status_mask = 0;
  549. if (I_IGNBRK(tty)) {
  550. info->ignore_status_mask |= UART_LSR_BI;
  551. info->read_status_mask |= UART_LSR_BI;
  552. /*
  553. * If we're ignore parity and break indicators, ignore
  554. * overruns too. (For real raw support).
  555. */
  556. if (I_IGNPAR(tty)) {
  557. info->ignore_status_mask |=
  558. UART_LSR_OE |
  559. UART_LSR_PE |
  560. UART_LSR_FE;
  561. info->read_status_mask |=
  562. UART_LSR_OE |
  563. UART_LSR_PE |
  564. UART_LSR_FE;
  565. }
  566. }
  567. if (info->board->must_hwid) {
  568. mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
  569. mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
  570. mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty));
  571. mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty));
  572. }
  573. outb(info->FCR, info->ioaddr + UART_FCR);
  574. outb(cval, info->ioaddr + UART_LCR);
  575. }
  576. static u8 mxser_check_modem_status(struct tty_struct *tty,
  577. struct mxser_port *port)
  578. {
  579. u8 msr = inb(port->ioaddr + UART_MSR);
  580. if (!(msr & UART_MSR_ANY_DELTA))
  581. return msr;
  582. /* update input line counters */
  583. if (msr & UART_MSR_TERI)
  584. port->icount.rng++;
  585. if (msr & UART_MSR_DDSR)
  586. port->icount.dsr++;
  587. if (msr & UART_MSR_DDCD)
  588. port->icount.dcd++;
  589. if (msr & UART_MSR_DCTS)
  590. port->icount.cts++;
  591. wake_up_interruptible(&port->port.delta_msr_wait);
  592. if (tty_port_check_carrier(&port->port) && (msr & UART_MSR_DDCD)) {
  593. if (msr & UART_MSR_DCD)
  594. wake_up_interruptible(&port->port.open_wait);
  595. }
  596. if (tty_port_cts_enabled(&port->port))
  597. mxser_handle_cts(tty, port, msr);
  598. return msr;
  599. }
  600. static void mxser_disable_and_clear_FIFO(struct mxser_port *info)
  601. {
  602. u8 fcr = UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
  603. if (info->board->must_hwid)
  604. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  605. outb(fcr, info->ioaddr + UART_FCR);
  606. }
  607. static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
  608. {
  609. struct mxser_port *info = container_of(port, struct mxser_port, port);
  610. unsigned long flags;
  611. int ret;
  612. ret = tty_port_alloc_xmit_buf(port);
  613. if (ret < 0)
  614. return ret;
  615. spin_lock_irqsave(&info->slock, flags);
  616. if (!info->type) {
  617. set_bit(TTY_IO_ERROR, &tty->flags);
  618. spin_unlock_irqrestore(&info->slock, flags);
  619. ret = 0;
  620. goto err_free_xmit;
  621. }
  622. /*
  623. * Clear the FIFO buffers and disable them
  624. * (they will be reenabled in mxser_change_speed())
  625. */
  626. mxser_disable_and_clear_FIFO(info);
  627. /*
  628. * At this point there's no way the LSR could still be 0xFF;
  629. * if it is, then bail out, because there's likely no UART
  630. * here.
  631. */
  632. if (inb(info->ioaddr + UART_LSR) == 0xff) {
  633. spin_unlock_irqrestore(&info->slock, flags);
  634. if (capable(CAP_SYS_ADMIN)) {
  635. set_bit(TTY_IO_ERROR, &tty->flags);
  636. return 0;
  637. }
  638. ret = -ENODEV;
  639. goto err_free_xmit;
  640. }
  641. /*
  642. * Clear the interrupt registers.
  643. */
  644. (void) inb(info->ioaddr + UART_LSR);
  645. (void) inb(info->ioaddr + UART_RX);
  646. (void) inb(info->ioaddr + UART_IIR);
  647. (void) inb(info->ioaddr + UART_MSR);
  648. /*
  649. * Now, initialize the UART
  650. */
  651. outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
  652. info->MCR = UART_MCR_DTR | UART_MCR_RTS;
  653. outb(info->MCR, info->ioaddr + UART_MCR);
  654. /*
  655. * Finally, enable interrupts
  656. */
  657. info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  658. if (info->board->must_hwid)
  659. info->IER |= MOXA_MUST_IER_EGDAI;
  660. outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
  661. /*
  662. * And clear the interrupt registers again for luck.
  663. */
  664. (void) inb(info->ioaddr + UART_LSR);
  665. (void) inb(info->ioaddr + UART_RX);
  666. (void) inb(info->ioaddr + UART_IIR);
  667. (void) inb(info->ioaddr + UART_MSR);
  668. clear_bit(TTY_IO_ERROR, &tty->flags);
  669. kfifo_reset(&port->xmit_fifo);
  670. /*
  671. * and set the speed of the serial port
  672. */
  673. mxser_change_speed(tty, NULL);
  674. spin_unlock_irqrestore(&info->slock, flags);
  675. return 0;
  676. err_free_xmit:
  677. tty_port_free_xmit_buf(port);
  678. return ret;
  679. }
  680. /*
  681. * To stop accepting input, we disable the receive line status interrupts, and
  682. * tell the interrupt driver to stop checking the data ready bit in the line
  683. * status register.
  684. */
  685. static void mxser_stop_rx(struct mxser_port *info)
  686. {
  687. info->IER &= ~UART_IER_RLSI;
  688. if (info->board->must_hwid)
  689. info->IER &= ~MOXA_MUST_RECV_ISR;
  690. outb(info->IER, info->ioaddr + UART_IER);
  691. }
  692. /*
  693. * This routine will shutdown a serial port
  694. */
  695. static void mxser_shutdown_port(struct tty_port *port)
  696. {
  697. struct mxser_port *info = container_of(port, struct mxser_port, port);
  698. unsigned long flags;
  699. spin_lock_irqsave(&info->slock, flags);
  700. mxser_stop_rx(info);
  701. /*
  702. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  703. * here so the queue might never be waken up
  704. */
  705. wake_up_interruptible(&info->port.delta_msr_wait);
  706. info->IER = 0;
  707. outb(0x00, info->ioaddr + UART_IER);
  708. /* clear Rx/Tx FIFO's */
  709. mxser_disable_and_clear_FIFO(info);
  710. /* read data port to reset things */
  711. (void) inb(info->ioaddr + UART_RX);
  712. if (info->board->must_hwid)
  713. mxser_must_no_sw_flow_control(info->ioaddr);
  714. spin_unlock_irqrestore(&info->slock, flags);
  715. /* make sure ISR is not running while we free the buffer */
  716. synchronize_irq(info->board->irq);
  717. tty_port_free_xmit_buf(port);
  718. }
  719. /*
  720. * This routine is called whenever a serial port is opened. It
  721. * enables interrupts for a serial port, linking in its async structure into
  722. * the IRQ chain. It also performs the serial-specific
  723. * initialization for the tty structure.
  724. */
  725. static int mxser_open(struct tty_struct *tty, struct file *filp)
  726. {
  727. struct tty_port *tport = tty->port;
  728. struct mxser_port *port = container_of(tport, struct mxser_port, port);
  729. tty->driver_data = port;
  730. return tty_port_open(tport, tty, filp);
  731. }
  732. static void mxser_flush_buffer(struct tty_struct *tty)
  733. {
  734. struct mxser_port *info = tty->driver_data;
  735. unsigned long flags;
  736. spin_lock_irqsave(&info->slock, flags);
  737. kfifo_reset(&info->port.xmit_fifo);
  738. outb(info->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  739. info->ioaddr + UART_FCR);
  740. spin_unlock_irqrestore(&info->slock, flags);
  741. tty_wakeup(tty);
  742. }
  743. static void mxser_close(struct tty_struct *tty, struct file *filp)
  744. {
  745. tty_port_close(tty->port, tty, filp);
  746. }
  747. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  748. {
  749. struct mxser_port *info = tty->driver_data;
  750. unsigned long flags;
  751. int written;
  752. bool is_empty;
  753. spin_lock_irqsave(&info->slock, flags);
  754. written = kfifo_in(&info->port.xmit_fifo, buf, count);
  755. is_empty = kfifo_is_empty(&info->port.xmit_fifo);
  756. spin_unlock_irqrestore(&info->slock, flags);
  757. if (!is_empty && !tty->flow.stopped)
  758. if (!tty->hw_stopped || mxser_16550A_or_MUST(info))
  759. mxser_start_tx(info);
  760. return written;
  761. }
  762. static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
  763. {
  764. struct mxser_port *info = tty->driver_data;
  765. unsigned long flags;
  766. int ret;
  767. spin_lock_irqsave(&info->slock, flags);
  768. ret = kfifo_put(&info->port.xmit_fifo, ch);
  769. spin_unlock_irqrestore(&info->slock, flags);
  770. return ret;
  771. }
  772. static void mxser_flush_chars(struct tty_struct *tty)
  773. {
  774. struct mxser_port *info = tty->driver_data;
  775. if (kfifo_is_empty(&info->port.xmit_fifo) || tty->flow.stopped ||
  776. (tty->hw_stopped && !mxser_16550A_or_MUST(info)))
  777. return;
  778. mxser_start_tx(info);
  779. }
  780. static unsigned int mxser_write_room(struct tty_struct *tty)
  781. {
  782. struct mxser_port *info = tty->driver_data;
  783. return kfifo_avail(&info->port.xmit_fifo);
  784. }
  785. static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
  786. {
  787. struct mxser_port *info = tty->driver_data;
  788. return kfifo_len(&info->port.xmit_fifo);
  789. }
  790. /*
  791. * ------------------------------------------------------------
  792. * friends of mxser_ioctl()
  793. * ------------------------------------------------------------
  794. */
  795. static int mxser_get_serial_info(struct tty_struct *tty,
  796. struct serial_struct *ss)
  797. {
  798. struct mxser_port *info = tty->driver_data;
  799. struct tty_port *port = &info->port;
  800. unsigned int closing_wait, close_delay;
  801. mutex_lock(&port->mutex);
  802. close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
  803. closing_wait = info->port.closing_wait;
  804. if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
  805. closing_wait = jiffies_to_msecs(closing_wait) / 10;
  806. ss->type = info->type;
  807. ss->line = tty->index;
  808. ss->port = info->ioaddr;
  809. ss->irq = info->board->irq;
  810. ss->flags = info->port.flags;
  811. ss->baud_base = MXSER_BAUD_BASE;
  812. ss->close_delay = close_delay;
  813. ss->closing_wait = closing_wait;
  814. ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
  815. mutex_unlock(&port->mutex);
  816. return 0;
  817. }
  818. static int mxser_set_serial_info(struct tty_struct *tty,
  819. struct serial_struct *ss)
  820. {
  821. struct mxser_port *info = tty->driver_data;
  822. struct tty_port *port = &info->port;
  823. speed_t baud;
  824. unsigned long sl_flags;
  825. unsigned int old_speed, close_delay, closing_wait;
  826. int retval = 0;
  827. if (tty_io_error(tty))
  828. return -EIO;
  829. mutex_lock(&port->mutex);
  830. if (ss->irq != info->board->irq ||
  831. ss->port != info->ioaddr) {
  832. mutex_unlock(&port->mutex);
  833. return -EINVAL;
  834. }
  835. old_speed = port->flags & ASYNC_SPD_MASK;
  836. close_delay = msecs_to_jiffies(ss->close_delay * 10);
  837. closing_wait = ss->closing_wait;
  838. if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
  839. closing_wait = msecs_to_jiffies(closing_wait * 10);
  840. if (!capable(CAP_SYS_ADMIN)) {
  841. if ((ss->baud_base != MXSER_BAUD_BASE) ||
  842. (close_delay != port->close_delay) ||
  843. (closing_wait != port->closing_wait) ||
  844. ((ss->flags & ~ASYNC_USR_MASK) != (port->flags & ~ASYNC_USR_MASK))) {
  845. mutex_unlock(&port->mutex);
  846. return -EPERM;
  847. }
  848. port->flags = (port->flags & ~ASYNC_USR_MASK) |
  849. (ss->flags & ASYNC_USR_MASK);
  850. } else {
  851. /*
  852. * OK, past this point, all the error checking has been done.
  853. * At this point, we start making changes.....
  854. */
  855. port->flags = ((port->flags & ~ASYNC_FLAGS) |
  856. (ss->flags & ASYNC_FLAGS));
  857. port->close_delay = close_delay;
  858. port->closing_wait = closing_wait;
  859. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
  860. (ss->baud_base != MXSER_BAUD_BASE ||
  861. ss->custom_divisor !=
  862. MXSER_CUSTOM_DIVISOR)) {
  863. if (ss->custom_divisor == 0) {
  864. mutex_unlock(&port->mutex);
  865. return -EINVAL;
  866. }
  867. baud = ss->baud_base / ss->custom_divisor;
  868. tty_encode_baud_rate(tty, baud, baud);
  869. }
  870. info->type = ss->type;
  871. mxser_process_txrx_fifo(info);
  872. }
  873. if (tty_port_initialized(port)) {
  874. if (old_speed != (port->flags & ASYNC_SPD_MASK)) {
  875. spin_lock_irqsave(&info->slock, sl_flags);
  876. mxser_change_speed(tty, NULL);
  877. spin_unlock_irqrestore(&info->slock, sl_flags);
  878. }
  879. } else {
  880. retval = mxser_activate(port, tty);
  881. if (retval == 0)
  882. tty_port_set_initialized(port, 1);
  883. }
  884. mutex_unlock(&port->mutex);
  885. return retval;
  886. }
  887. /*
  888. * mxser_get_lsr_info - get line status register info
  889. *
  890. * Purpose: Let user call ioctl() to get info when the UART physically
  891. * is emptied. On bus types like RS485, the transmitter must
  892. * release the bus after transmitting. This must be done when
  893. * the transmit shift register is empty, not be done when the
  894. * transmit holding register is empty. This functionality
  895. * allows an RS485 driver to be written in user space.
  896. */
  897. static int mxser_get_lsr_info(struct mxser_port *info,
  898. unsigned int __user *value)
  899. {
  900. unsigned char status;
  901. unsigned int result;
  902. unsigned long flags;
  903. spin_lock_irqsave(&info->slock, flags);
  904. status = inb(info->ioaddr + UART_LSR);
  905. spin_unlock_irqrestore(&info->slock, flags);
  906. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  907. return put_user(result, value);
  908. }
  909. static int mxser_tiocmget(struct tty_struct *tty)
  910. {
  911. struct mxser_port *info = tty->driver_data;
  912. unsigned char control;
  913. unsigned long flags;
  914. u8 msr;
  915. if (tty_io_error(tty))
  916. return -EIO;
  917. spin_lock_irqsave(&info->slock, flags);
  918. control = info->MCR;
  919. msr = mxser_check_modem_status(tty, info);
  920. spin_unlock_irqrestore(&info->slock, flags);
  921. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  922. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
  923. ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |
  924. ((msr & UART_MSR_RI) ? TIOCM_RNG : 0) |
  925. ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0) |
  926. ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0);
  927. }
  928. static int mxser_tiocmset(struct tty_struct *tty,
  929. unsigned int set, unsigned int clear)
  930. {
  931. struct mxser_port *info = tty->driver_data;
  932. unsigned long flags;
  933. if (tty_io_error(tty))
  934. return -EIO;
  935. spin_lock_irqsave(&info->slock, flags);
  936. if (set & TIOCM_RTS)
  937. info->MCR |= UART_MCR_RTS;
  938. if (set & TIOCM_DTR)
  939. info->MCR |= UART_MCR_DTR;
  940. if (clear & TIOCM_RTS)
  941. info->MCR &= ~UART_MCR_RTS;
  942. if (clear & TIOCM_DTR)
  943. info->MCR &= ~UART_MCR_DTR;
  944. outb(info->MCR, info->ioaddr + UART_MCR);
  945. spin_unlock_irqrestore(&info->slock, flags);
  946. return 0;
  947. }
  948. static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
  949. struct async_icount *cprev)
  950. {
  951. struct async_icount cnow;
  952. unsigned long flags;
  953. int ret;
  954. spin_lock_irqsave(&info->slock, flags);
  955. cnow = info->icount; /* atomic copy */
  956. spin_unlock_irqrestore(&info->slock, flags);
  957. ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  958. ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  959. ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  960. ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
  961. *cprev = cnow;
  962. return ret;
  963. }
  964. /* We should likely switch to TIOCGRS485/TIOCSRS485. */
  965. static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
  966. int __user *u_opmode)
  967. {
  968. int opmode, p = index % 4;
  969. int shiftbit = p * 2;
  970. u8 val;
  971. if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
  972. return -EFAULT;
  973. if (set) {
  974. if (get_user(opmode, u_opmode))
  975. return -EFAULT;
  976. if (opmode & ~OP_MODE_MASK)
  977. return -EINVAL;
  978. spin_lock_irq(&port->slock);
  979. val = inb(port->opmode_ioaddr);
  980. val &= ~(OP_MODE_MASK << shiftbit);
  981. val |= (opmode << shiftbit);
  982. outb(val, port->opmode_ioaddr);
  983. spin_unlock_irq(&port->slock);
  984. return 0;
  985. }
  986. spin_lock_irq(&port->slock);
  987. opmode = inb(port->opmode_ioaddr) >> shiftbit;
  988. spin_unlock_irq(&port->slock);
  989. return put_user(opmode & OP_MODE_MASK, u_opmode);
  990. }
  991. static int mxser_ioctl(struct tty_struct *tty,
  992. unsigned int cmd, unsigned long arg)
  993. {
  994. struct mxser_port *info = tty->driver_data;
  995. struct async_icount cnow;
  996. unsigned long flags;
  997. void __user *argp = (void __user *)arg;
  998. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
  999. return mxser_ioctl_op_mode(info, tty->index,
  1000. cmd == MOXA_SET_OP_MODE, argp);
  1001. if (cmd != TIOCMIWAIT && tty_io_error(tty))
  1002. return -EIO;
  1003. switch (cmd) {
  1004. case TIOCSERGETLSR: /* Get line status register */
  1005. return mxser_get_lsr_info(info, argp);
  1006. /*
  1007. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1008. * - mask passed in arg for lines of interest
  1009. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1010. * Caller should use TIOCGICOUNT to see which one it was
  1011. */
  1012. case TIOCMIWAIT:
  1013. spin_lock_irqsave(&info->slock, flags);
  1014. cnow = info->icount; /* note the counters on entry */
  1015. spin_unlock_irqrestore(&info->slock, flags);
  1016. return wait_event_interruptible(info->port.delta_msr_wait,
  1017. mxser_cflags_changed(info, arg, &cnow));
  1018. default:
  1019. return -ENOIOCTLCMD;
  1020. }
  1021. return 0;
  1022. }
  1023. /*
  1024. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1025. * Return: write counters to the user passed counter struct
  1026. * NB: both 1->0 and 0->1 transitions are counted except for
  1027. * RI where only 0->1 is counted.
  1028. */
  1029. static int mxser_get_icount(struct tty_struct *tty,
  1030. struct serial_icounter_struct *icount)
  1031. {
  1032. struct mxser_port *info = tty->driver_data;
  1033. struct async_icount cnow;
  1034. unsigned long flags;
  1035. spin_lock_irqsave(&info->slock, flags);
  1036. cnow = info->icount;
  1037. spin_unlock_irqrestore(&info->slock, flags);
  1038. icount->frame = cnow.frame;
  1039. icount->brk = cnow.brk;
  1040. icount->overrun = cnow.overrun;
  1041. icount->buf_overrun = cnow.buf_overrun;
  1042. icount->parity = cnow.parity;
  1043. icount->rx = cnow.rx;
  1044. icount->tx = cnow.tx;
  1045. icount->cts = cnow.cts;
  1046. icount->dsr = cnow.dsr;
  1047. icount->rng = cnow.rng;
  1048. icount->dcd = cnow.dcd;
  1049. return 0;
  1050. }
  1051. /*
  1052. * This routine is called by the upper-layer tty layer to signal that
  1053. * incoming characters should be throttled.
  1054. */
  1055. static void mxser_throttle(struct tty_struct *tty)
  1056. {
  1057. struct mxser_port *info = tty->driver_data;
  1058. if (I_IXOFF(tty)) {
  1059. if (info->board->must_hwid) {
  1060. info->IER &= ~MOXA_MUST_RECV_ISR;
  1061. outb(info->IER, info->ioaddr + UART_IER);
  1062. } else {
  1063. info->x_char = STOP_CHAR(tty);
  1064. outb(0, info->ioaddr + UART_IER);
  1065. info->IER |= UART_IER_THRI;
  1066. outb(info->IER, info->ioaddr + UART_IER);
  1067. }
  1068. }
  1069. if (C_CRTSCTS(tty)) {
  1070. info->MCR &= ~UART_MCR_RTS;
  1071. outb(info->MCR, info->ioaddr + UART_MCR);
  1072. }
  1073. }
  1074. static void mxser_unthrottle(struct tty_struct *tty)
  1075. {
  1076. struct mxser_port *info = tty->driver_data;
  1077. /* startrx */
  1078. if (I_IXOFF(tty)) {
  1079. if (info->x_char)
  1080. info->x_char = 0;
  1081. else {
  1082. if (info->board->must_hwid) {
  1083. info->IER |= MOXA_MUST_RECV_ISR;
  1084. outb(info->IER, info->ioaddr + UART_IER);
  1085. } else {
  1086. info->x_char = START_CHAR(tty);
  1087. outb(0, info->ioaddr + UART_IER);
  1088. info->IER |= UART_IER_THRI;
  1089. outb(info->IER, info->ioaddr + UART_IER);
  1090. }
  1091. }
  1092. }
  1093. if (C_CRTSCTS(tty)) {
  1094. info->MCR |= UART_MCR_RTS;
  1095. outb(info->MCR, info->ioaddr + UART_MCR);
  1096. }
  1097. }
  1098. /*
  1099. * mxser_stop() and mxser_start()
  1100. *
  1101. * This routines are called before setting or resetting tty->flow.stopped.
  1102. * They enable or disable transmitter interrupts, as necessary.
  1103. */
  1104. static void mxser_stop(struct tty_struct *tty)
  1105. {
  1106. struct mxser_port *info = tty->driver_data;
  1107. unsigned long flags;
  1108. spin_lock_irqsave(&info->slock, flags);
  1109. if (info->IER & UART_IER_THRI)
  1110. __mxser_stop_tx(info);
  1111. spin_unlock_irqrestore(&info->slock, flags);
  1112. }
  1113. static void mxser_start(struct tty_struct *tty)
  1114. {
  1115. struct mxser_port *info = tty->driver_data;
  1116. unsigned long flags;
  1117. spin_lock_irqsave(&info->slock, flags);
  1118. if (!kfifo_is_empty(&info->port.xmit_fifo))
  1119. __mxser_start_tx(info);
  1120. spin_unlock_irqrestore(&info->slock, flags);
  1121. }
  1122. static void mxser_set_termios(struct tty_struct *tty,
  1123. const struct ktermios *old_termios)
  1124. {
  1125. struct mxser_port *info = tty->driver_data;
  1126. unsigned long flags;
  1127. spin_lock_irqsave(&info->slock, flags);
  1128. mxser_change_speed(tty, old_termios);
  1129. spin_unlock_irqrestore(&info->slock, flags);
  1130. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
  1131. tty->hw_stopped = 0;
  1132. mxser_start(tty);
  1133. }
  1134. /* Handle sw stopped */
  1135. if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
  1136. tty->flow.stopped = 0;
  1137. if (info->board->must_hwid) {
  1138. spin_lock_irqsave(&info->slock, flags);
  1139. mxser_must_set_rx_sw_flow_control(info->ioaddr, false);
  1140. spin_unlock_irqrestore(&info->slock, flags);
  1141. }
  1142. mxser_start(tty);
  1143. }
  1144. }
  1145. static bool mxser_tx_empty(struct mxser_port *info)
  1146. {
  1147. unsigned long flags;
  1148. u8 lsr;
  1149. spin_lock_irqsave(&info->slock, flags);
  1150. lsr = inb(info->ioaddr + UART_LSR);
  1151. spin_unlock_irqrestore(&info->slock, flags);
  1152. return !(lsr & UART_LSR_TEMT);
  1153. }
  1154. /*
  1155. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1156. */
  1157. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1158. {
  1159. struct mxser_port *info = tty->driver_data;
  1160. unsigned long expire, char_time;
  1161. if (info->type == PORT_UNKNOWN)
  1162. return;
  1163. if (info->xmit_fifo_size == 0)
  1164. return; /* Just in case.... */
  1165. /*
  1166. * Set the check interval to be 1/5 of the estimated time to
  1167. * send a single character, and make it at least 1. The check
  1168. * interval should also be less than the timeout.
  1169. *
  1170. * Note: we have to use pretty tight timings here to satisfy
  1171. * the NIST-PCTS.
  1172. */
  1173. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1174. char_time = char_time / 5;
  1175. if (char_time == 0)
  1176. char_time = 1;
  1177. if (timeout && timeout < char_time)
  1178. char_time = timeout;
  1179. char_time = jiffies_to_msecs(char_time);
  1180. /*
  1181. * If the transmitter hasn't cleared in twice the approximate
  1182. * amount of time to send the entire FIFO, it probably won't
  1183. * ever clear. This assumes the UART isn't doing flow
  1184. * control, which is currently the case. Hence, if it ever
  1185. * takes longer than info->timeout, this is probably due to a
  1186. * UART bug of some kind. So, we clamp the timeout parameter at
  1187. * 2*info->timeout.
  1188. */
  1189. if (!timeout || timeout > 2 * info->timeout)
  1190. timeout = 2 * info->timeout;
  1191. expire = jiffies + timeout;
  1192. while (mxser_tx_empty(info)) {
  1193. msleep_interruptible(char_time);
  1194. if (signal_pending(current))
  1195. break;
  1196. if (time_after(jiffies, expire))
  1197. break;
  1198. }
  1199. }
  1200. /*
  1201. * This routine is called by tty_hangup() when a hangup is signaled.
  1202. */
  1203. static void mxser_hangup(struct tty_struct *tty)
  1204. {
  1205. struct mxser_port *info = tty->driver_data;
  1206. mxser_flush_buffer(tty);
  1207. tty_port_hangup(&info->port);
  1208. }
  1209. /*
  1210. * mxser_rs_break() --- routine which turns the break handling on or off
  1211. */
  1212. static int mxser_rs_break(struct tty_struct *tty, int break_state)
  1213. {
  1214. struct mxser_port *info = tty->driver_data;
  1215. unsigned long flags;
  1216. u8 lcr;
  1217. spin_lock_irqsave(&info->slock, flags);
  1218. lcr = inb(info->ioaddr + UART_LCR);
  1219. if (break_state == -1)
  1220. lcr |= UART_LCR_SBC;
  1221. else
  1222. lcr &= ~UART_LCR_SBC;
  1223. outb(lcr, info->ioaddr + UART_LCR);
  1224. spin_unlock_irqrestore(&info->slock, flags);
  1225. return 0;
  1226. }
  1227. static bool mxser_receive_chars_new(struct mxser_port *port, u8 status)
  1228. {
  1229. enum mxser_must_hwid hwid = port->board->must_hwid;
  1230. u8 gdl;
  1231. if (hwid == MOXA_OTHER_UART)
  1232. return false;
  1233. if (status & (UART_LSR_BRK_ERROR_BITS | MOXA_MUST_LSR_RERR))
  1234. return false;
  1235. gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
  1236. if (hwid == MOXA_MUST_MU150_HWID)
  1237. gdl &= MOXA_MUST_GDL_MASK;
  1238. while (gdl--) {
  1239. u8 ch = inb(port->ioaddr + UART_RX);
  1240. if (!tty_insert_flip_char(&port->port, ch, 0))
  1241. port->icount.buf_overrun++;
  1242. }
  1243. return true;
  1244. }
  1245. static u8 mxser_receive_chars_old(struct tty_struct *tty,
  1246. struct mxser_port *port, u8 status)
  1247. {
  1248. enum mxser_must_hwid hwid = port->board->must_hwid;
  1249. int ignored = 0;
  1250. int max = 256;
  1251. u8 ch;
  1252. do {
  1253. if (max-- < 0)
  1254. break;
  1255. ch = inb(port->ioaddr + UART_RX);
  1256. if (hwid && (status & UART_LSR_OE))
  1257. outb(port->FCR | UART_FCR_CLEAR_RCVR,
  1258. port->ioaddr + UART_FCR);
  1259. status &= port->read_status_mask;
  1260. if (status & port->ignore_status_mask) {
  1261. if (++ignored > 100)
  1262. break;
  1263. } else {
  1264. char flag = 0;
  1265. if (status & UART_LSR_BRK_ERROR_BITS) {
  1266. if (status & UART_LSR_BI) {
  1267. flag = TTY_BREAK;
  1268. port->icount.brk++;
  1269. if (port->port.flags & ASYNC_SAK)
  1270. do_SAK(tty);
  1271. } else if (status & UART_LSR_PE) {
  1272. flag = TTY_PARITY;
  1273. port->icount.parity++;
  1274. } else if (status & UART_LSR_FE) {
  1275. flag = TTY_FRAME;
  1276. port->icount.frame++;
  1277. } else if (status & UART_LSR_OE) {
  1278. flag = TTY_OVERRUN;
  1279. port->icount.overrun++;
  1280. }
  1281. }
  1282. if (!tty_insert_flip_char(&port->port, ch, flag)) {
  1283. port->icount.buf_overrun++;
  1284. break;
  1285. }
  1286. }
  1287. if (hwid)
  1288. break;
  1289. status = inb(port->ioaddr + UART_LSR);
  1290. } while (status & UART_LSR_DR);
  1291. return status;
  1292. }
  1293. static u8 mxser_receive_chars(struct tty_struct *tty,
  1294. struct mxser_port *port, u8 status)
  1295. {
  1296. if (!mxser_receive_chars_new(port, status))
  1297. status = mxser_receive_chars_old(tty, port, status);
  1298. tty_flip_buffer_push(&port->port);
  1299. return status;
  1300. }
  1301. static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
  1302. {
  1303. int count;
  1304. if (port->x_char) {
  1305. outb(port->x_char, port->ioaddr + UART_TX);
  1306. port->x_char = 0;
  1307. port->icount.tx++;
  1308. return;
  1309. }
  1310. if (kfifo_is_empty(&port->port.xmit_fifo) || tty->flow.stopped ||
  1311. (tty->hw_stopped && !mxser_16550A_or_MUST(port))) {
  1312. __mxser_stop_tx(port);
  1313. return;
  1314. }
  1315. count = port->xmit_fifo_size;
  1316. do {
  1317. unsigned char c;
  1318. if (!kfifo_get(&port->port.xmit_fifo, &c))
  1319. break;
  1320. outb(c, port->ioaddr + UART_TX);
  1321. port->icount.tx++;
  1322. } while (--count > 0);
  1323. if (kfifo_len(&port->port.xmit_fifo) < WAKEUP_CHARS)
  1324. tty_wakeup(tty);
  1325. if (kfifo_is_empty(&port->port.xmit_fifo))
  1326. __mxser_stop_tx(port);
  1327. }
  1328. static bool mxser_port_isr(struct mxser_port *port)
  1329. {
  1330. struct tty_struct *tty;
  1331. u8 iir, status;
  1332. bool error = false;
  1333. iir = inb(port->ioaddr + UART_IIR);
  1334. if (iir & UART_IIR_NO_INT)
  1335. return true;
  1336. iir &= MOXA_MUST_IIR_MASK;
  1337. tty = tty_port_tty_get(&port->port);
  1338. if (!tty) {
  1339. status = inb(port->ioaddr + UART_LSR);
  1340. outb(port->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  1341. port->ioaddr + UART_FCR);
  1342. inb(port->ioaddr + UART_MSR);
  1343. error = true;
  1344. goto put_tty;
  1345. }
  1346. status = inb(port->ioaddr + UART_LSR);
  1347. if (port->board->must_hwid) {
  1348. if (iir == MOXA_MUST_IIR_GDA ||
  1349. iir == MOXA_MUST_IIR_RDA ||
  1350. iir == MOXA_MUST_IIR_RTO ||
  1351. iir == MOXA_MUST_IIR_LSR)
  1352. status = mxser_receive_chars(tty, port, status);
  1353. } else {
  1354. status &= port->read_status_mask;
  1355. if (status & UART_LSR_DR)
  1356. status = mxser_receive_chars(tty, port, status);
  1357. }
  1358. mxser_check_modem_status(tty, port);
  1359. if (port->board->must_hwid) {
  1360. if (iir == 0x02 && (status & UART_LSR_THRE))
  1361. mxser_transmit_chars(tty, port);
  1362. } else {
  1363. if (status & UART_LSR_THRE)
  1364. mxser_transmit_chars(tty, port);
  1365. }
  1366. put_tty:
  1367. tty_kref_put(tty);
  1368. return error;
  1369. }
  1370. /*
  1371. * This is the serial driver's generic interrupt routine
  1372. */
  1373. static irqreturn_t mxser_interrupt(int irq, void *dev_id)
  1374. {
  1375. struct mxser_board *brd = dev_id;
  1376. struct mxser_port *port;
  1377. unsigned int int_cnt, pass_counter = 0;
  1378. unsigned int i, max = brd->nports;
  1379. int handled = IRQ_NONE;
  1380. u8 irqbits, bits, mask = BIT(max) - 1;
  1381. while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
  1382. irqbits = inb(brd->vector) & mask;
  1383. if (irqbits == mask)
  1384. break;
  1385. handled = IRQ_HANDLED;
  1386. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  1387. if (irqbits == mask)
  1388. break;
  1389. if (bits & irqbits)
  1390. continue;
  1391. port = &brd->ports[i];
  1392. int_cnt = 0;
  1393. spin_lock(&port->slock);
  1394. do {
  1395. if (mxser_port_isr(port))
  1396. break;
  1397. } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
  1398. spin_unlock(&port->slock);
  1399. }
  1400. }
  1401. return handled;
  1402. }
  1403. static const struct tty_operations mxser_ops = {
  1404. .open = mxser_open,
  1405. .close = mxser_close,
  1406. .write = mxser_write,
  1407. .put_char = mxser_put_char,
  1408. .flush_chars = mxser_flush_chars,
  1409. .write_room = mxser_write_room,
  1410. .chars_in_buffer = mxser_chars_in_buffer,
  1411. .flush_buffer = mxser_flush_buffer,
  1412. .ioctl = mxser_ioctl,
  1413. .throttle = mxser_throttle,
  1414. .unthrottle = mxser_unthrottle,
  1415. .set_termios = mxser_set_termios,
  1416. .stop = mxser_stop,
  1417. .start = mxser_start,
  1418. .hangup = mxser_hangup,
  1419. .break_ctl = mxser_rs_break,
  1420. .wait_until_sent = mxser_wait_until_sent,
  1421. .tiocmget = mxser_tiocmget,
  1422. .tiocmset = mxser_tiocmset,
  1423. .set_serial = mxser_set_serial_info,
  1424. .get_serial = mxser_get_serial_info,
  1425. .get_icount = mxser_get_icount,
  1426. };
  1427. static const struct tty_port_operations mxser_port_ops = {
  1428. .carrier_raised = mxser_carrier_raised,
  1429. .dtr_rts = mxser_dtr_rts,
  1430. .activate = mxser_activate,
  1431. .shutdown = mxser_shutdown_port,
  1432. };
  1433. /*
  1434. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  1435. */
  1436. static void mxser_initbrd(struct mxser_board *brd, bool high_baud)
  1437. {
  1438. struct mxser_port *info;
  1439. unsigned int i;
  1440. bool is_mu860;
  1441. brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
  1442. is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID;
  1443. for (i = 0; i < UART_INFO_NUM; i++) {
  1444. if (Gpci_uart_info[i].type == brd->must_hwid) {
  1445. brd->max_baud = Gpci_uart_info[i].max_baud;
  1446. /* exception....CP-102 */
  1447. if (high_baud)
  1448. brd->max_baud = 921600;
  1449. break;
  1450. }
  1451. }
  1452. if (is_mu860) {
  1453. /* set to RS232 mode by default */
  1454. outb(0, brd->vector + 4);
  1455. outb(0, brd->vector + 0x0c);
  1456. }
  1457. for (i = 0; i < brd->nports; i++) {
  1458. info = &brd->ports[i];
  1459. if (is_mu860) {
  1460. if (i < 4)
  1461. info->opmode_ioaddr = brd->vector + 4;
  1462. else
  1463. info->opmode_ioaddr = brd->vector + 0x0c;
  1464. }
  1465. tty_port_init(&info->port);
  1466. info->port.ops = &mxser_port_ops;
  1467. info->board = brd;
  1468. /* Enhance mode enabled here */
  1469. if (brd->must_hwid != MOXA_OTHER_UART)
  1470. mxser_must_set_enhance_mode(info->ioaddr, true);
  1471. info->type = PORT_16550A;
  1472. mxser_process_txrx_fifo(info);
  1473. info->port.close_delay = 5 * HZ / 10;
  1474. info->port.closing_wait = 30 * HZ;
  1475. spin_lock_init(&info->slock);
  1476. /* before set INT ISR, disable all int */
  1477. outb(inb(info->ioaddr + UART_IER) & 0xf0,
  1478. info->ioaddr + UART_IER);
  1479. }
  1480. }
  1481. static int mxser_probe(struct pci_dev *pdev,
  1482. const struct pci_device_id *ent)
  1483. {
  1484. struct mxser_board *brd;
  1485. unsigned int i, base;
  1486. unsigned long ioaddress;
  1487. unsigned short nports = MXSER_NPORTS(ent->driver_data);
  1488. struct device *tty_dev;
  1489. int retval = -EINVAL;
  1490. i = find_first_zero_bit(mxser_boards, MXSER_BOARDS);
  1491. if (i >= MXSER_BOARDS) {
  1492. dev_err(&pdev->dev, "too many boards found (maximum %d), board "
  1493. "not configured\n", MXSER_BOARDS);
  1494. goto err;
  1495. }
  1496. brd = devm_kzalloc(&pdev->dev, struct_size(brd, ports, nports),
  1497. GFP_KERNEL);
  1498. if (!brd)
  1499. goto err;
  1500. brd->idx = i;
  1501. __set_bit(brd->idx, mxser_boards);
  1502. base = i * MXSER_PORTS_PER_BOARD;
  1503. retval = pcim_enable_device(pdev);
  1504. if (retval) {
  1505. dev_err(&pdev->dev, "PCI enable failed\n");
  1506. goto err_zero;
  1507. }
  1508. /* io address */
  1509. ioaddress = pci_resource_start(pdev, 2);
  1510. retval = pci_request_region(pdev, 2, "mxser(IO)");
  1511. if (retval)
  1512. goto err_zero;
  1513. brd->nports = nports;
  1514. for (i = 0; i < nports; i++)
  1515. brd->ports[i].ioaddr = ioaddress + 8 * i;
  1516. /* vector */
  1517. ioaddress = pci_resource_start(pdev, 3);
  1518. retval = pci_request_region(pdev, 3, "mxser(vector)");
  1519. if (retval)
  1520. goto err_zero;
  1521. brd->vector = ioaddress;
  1522. /* irq */
  1523. brd->irq = pdev->irq;
  1524. mxser_initbrd(brd, ent->driver_data & MXSER_HIGHBAUD);
  1525. retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt,
  1526. IRQF_SHARED, "mxser", brd);
  1527. if (retval) {
  1528. dev_err(&pdev->dev, "request irq failed");
  1529. goto err_relbrd;
  1530. }
  1531. for (i = 0; i < nports; i++) {
  1532. tty_dev = tty_port_register_device(&brd->ports[i].port,
  1533. mxvar_sdriver, base + i, &pdev->dev);
  1534. if (IS_ERR(tty_dev)) {
  1535. retval = PTR_ERR(tty_dev);
  1536. for (; i > 0; i--)
  1537. tty_unregister_device(mxvar_sdriver,
  1538. base + i - 1);
  1539. goto err_relbrd;
  1540. }
  1541. }
  1542. pci_set_drvdata(pdev, brd);
  1543. return 0;
  1544. err_relbrd:
  1545. for (i = 0; i < nports; i++)
  1546. tty_port_destroy(&brd->ports[i].port);
  1547. err_zero:
  1548. __clear_bit(brd->idx, mxser_boards);
  1549. err:
  1550. return retval;
  1551. }
  1552. static void mxser_remove(struct pci_dev *pdev)
  1553. {
  1554. struct mxser_board *brd = pci_get_drvdata(pdev);
  1555. unsigned int i, base = brd->idx * MXSER_PORTS_PER_BOARD;
  1556. for (i = 0; i < brd->nports; i++) {
  1557. tty_unregister_device(mxvar_sdriver, base + i);
  1558. tty_port_destroy(&brd->ports[i].port);
  1559. }
  1560. __clear_bit(brd->idx, mxser_boards);
  1561. }
  1562. static struct pci_driver mxser_driver = {
  1563. .name = "mxser",
  1564. .id_table = mxser_pcibrds,
  1565. .probe = mxser_probe,
  1566. .remove = mxser_remove
  1567. };
  1568. static int __init mxser_module_init(void)
  1569. {
  1570. int retval;
  1571. mxvar_sdriver = tty_alloc_driver(MXSER_PORTS, TTY_DRIVER_REAL_RAW |
  1572. TTY_DRIVER_DYNAMIC_DEV);
  1573. if (IS_ERR(mxvar_sdriver))
  1574. return PTR_ERR(mxvar_sdriver);
  1575. /* Initialize the tty_driver structure */
  1576. mxvar_sdriver->name = "ttyMI";
  1577. mxvar_sdriver->major = ttymajor;
  1578. mxvar_sdriver->minor_start = 0;
  1579. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  1580. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  1581. mxvar_sdriver->init_termios = tty_std_termios;
  1582. mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  1583. tty_set_operations(mxvar_sdriver, &mxser_ops);
  1584. retval = tty_register_driver(mxvar_sdriver);
  1585. if (retval) {
  1586. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
  1587. "tty driver !\n");
  1588. goto err_put;
  1589. }
  1590. retval = pci_register_driver(&mxser_driver);
  1591. if (retval) {
  1592. printk(KERN_ERR "mxser: can't register pci driver\n");
  1593. goto err_unr;
  1594. }
  1595. return 0;
  1596. err_unr:
  1597. tty_unregister_driver(mxvar_sdriver);
  1598. err_put:
  1599. tty_driver_kref_put(mxvar_sdriver);
  1600. return retval;
  1601. }
  1602. static void __exit mxser_module_exit(void)
  1603. {
  1604. pci_unregister_driver(&mxser_driver);
  1605. tty_unregister_driver(mxvar_sdriver);
  1606. tty_driver_kref_put(mxvar_sdriver);
  1607. }
  1608. module_init(mxser_module_init);
  1609. module_exit(mxser_module_exit);