mxser.c 47 KB

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