mxser.c 49 KB

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