esp.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. /*
  2. * esp.c - driver for Hayes ESP serial cards
  3. *
  4. * --- Notices from serial.c, upon which this driver is based ---
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  8. * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
  9. * much more extensible to support other serial cards based on the
  10. * 16450/16550A UART's. Added support for the AST FourPort and the
  11. * Accent Async board.
  12. *
  13. * set_serial_info fixed to set the flags, custom divisor, and uart
  14. * type fields. Fix suggested by Michael K. Johnson 12/12/92.
  15. *
  16. * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
  17. *
  18. * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
  19. *
  20. * rs_set_termios fixed to look also for changes of the input
  21. * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
  22. * Bernd Anhäupl 05/17/96.
  23. *
  24. * --- End of notices from serial.c ---
  25. *
  26. * Support for the ESP serial card by Andrew J. Robinson
  27. * <arobinso@nyx.net> (Card detection routine taken from a patch
  28. * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
  29. * by Chris Faylor.
  30. *
  31. * Most recent changes: (Andrew J. Robinson)
  32. * Support for PIO mode. This allows the driver to work properly with
  33. * multiport cards.
  34. *
  35. * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
  36. * several cleanups, use module_init/module_exit, etc
  37. *
  38. * This module exports the following rs232 io functions:
  39. *
  40. * int espserial_init(void);
  41. */
  42. #include <linux/module.h>
  43. #include <linux/errno.h>
  44. #include <linux/signal.h>
  45. #include <linux/sched.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/tty.h>
  48. #include <linux/tty_flip.h>
  49. #include <linux/serial.h>
  50. #include <linux/serialP.h>
  51. #include <linux/serial_reg.h>
  52. #include <linux/major.h>
  53. #include <linux/string.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/ptrace.h>
  56. #include <linux/ioport.h>
  57. #include <linux/mm.h>
  58. #include <linux/init.h>
  59. #include <linux/delay.h>
  60. #include <linux/bitops.h>
  61. #include <asm/system.h>
  62. #include <asm/io.h>
  63. #include <asm/dma.h>
  64. #include <linux/slab.h>
  65. #include <asm/uaccess.h>
  66. #include <linux/hayesesp.h>
  67. #define NR_PORTS 64 /* maximum number of ports */
  68. #define NR_PRIMARY 8 /* maximum number of primary ports */
  69. #define REGION_SIZE 8 /* size of io region to request */
  70. /* The following variables can be set by giving module options */
  71. static int irq[NR_PRIMARY]; /* IRQ for each base port */
  72. static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
  73. static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
  74. static unsigned int rx_trigger = ESP_RX_TRIGGER;
  75. static unsigned int tx_trigger = ESP_TX_TRIGGER;
  76. static unsigned int flow_off = ESP_FLOW_OFF;
  77. static unsigned int flow_on = ESP_FLOW_ON;
  78. static unsigned int rx_timeout = ESP_RX_TMOUT;
  79. static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
  80. MODULE_LICENSE("GPL");
  81. module_param_array(irq, int, NULL, 0);
  82. module_param_array(divisor, uint, NULL, 0);
  83. module_param(dma, uint, 0);
  84. module_param(rx_trigger, uint, 0);
  85. module_param(tx_trigger, uint, 0);
  86. module_param(flow_off, uint, 0);
  87. module_param(flow_on, uint, 0);
  88. module_param(rx_timeout, uint, 0);
  89. module_param(pio_threshold, uint, 0);
  90. /* END */
  91. static char *dma_buffer;
  92. static int dma_bytes;
  93. static struct esp_pio_buffer *free_pio_buf;
  94. #define DMA_BUFFER_SZ 1024
  95. #define WAKEUP_CHARS 1024
  96. static char serial_name[] __initdata = "ESP serial driver";
  97. static char serial_version[] __initdata = "2.2";
  98. static struct tty_driver *esp_driver;
  99. /*
  100. * Serial driver configuration section. Here are the various options:
  101. *
  102. * SERIAL_PARANOIA_CHECK
  103. * Check the magic number for the esp_structure where
  104. * ever possible.
  105. */
  106. #undef SERIAL_PARANOIA_CHECK
  107. #define SERIAL_DO_RESTART
  108. #undef SERIAL_DEBUG_INTR
  109. #undef SERIAL_DEBUG_OPEN
  110. #undef SERIAL_DEBUG_FLOW
  111. #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
  112. #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
  113. tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
  114. #else
  115. #define DBG_CNT(s)
  116. #endif
  117. static struct esp_struct *ports;
  118. static void change_speed(struct esp_struct *info);
  119. static void rs_wait_until_sent(struct tty_struct *, int);
  120. /*
  121. * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
  122. * times the normal 1.8432 Mhz clock of most serial boards).
  123. */
  124. #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
  125. /* Standard COM flags (except for COM4, because of the 8514 problem) */
  126. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  127. static inline int serial_paranoia_check(struct esp_struct *info,
  128. char *name, const char *routine)
  129. {
  130. #ifdef SERIAL_PARANOIA_CHECK
  131. static const char badmagic[] = KERN_WARNING
  132. "Warning: bad magic number for serial struct (%s) in %s\n";
  133. static const char badinfo[] = KERN_WARNING
  134. "Warning: null esp_struct for (%s) in %s\n";
  135. if (!info) {
  136. printk(badinfo, name, routine);
  137. return 1;
  138. }
  139. if (info->magic != ESP_MAGIC) {
  140. printk(badmagic, name, routine);
  141. return 1;
  142. }
  143. #endif
  144. return 0;
  145. }
  146. static inline unsigned int serial_in(struct esp_struct *info, int offset)
  147. {
  148. return inb(info->port + offset);
  149. }
  150. static inline void serial_out(struct esp_struct *info, int offset,
  151. unsigned char value)
  152. {
  153. outb(value, info->port+offset);
  154. }
  155. /*
  156. * ------------------------------------------------------------
  157. * rs_stop() and rs_start()
  158. *
  159. * This routines are called before setting or resetting tty->stopped.
  160. * They enable or disable transmitter interrupts, as necessary.
  161. * ------------------------------------------------------------
  162. */
  163. static void rs_stop(struct tty_struct *tty)
  164. {
  165. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  166. unsigned long flags;
  167. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  168. return;
  169. spin_lock_irqsave(&info->lock, flags);
  170. if (info->IER & UART_IER_THRI) {
  171. info->IER &= ~UART_IER_THRI;
  172. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  173. serial_out(info, UART_ESI_CMD2, info->IER);
  174. }
  175. spin_unlock_irqrestore(&info->lock, flags);
  176. }
  177. static void rs_start(struct tty_struct *tty)
  178. {
  179. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  180. unsigned long flags;
  181. if (serial_paranoia_check(info, tty->name, "rs_start"))
  182. return;
  183. spin_lock_irqsave(&info->lock, flags);
  184. if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
  185. info->IER |= UART_IER_THRI;
  186. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  187. serial_out(info, UART_ESI_CMD2, info->IER);
  188. }
  189. spin_unlock_irqrestore(&info->lock, flags);
  190. }
  191. /*
  192. * ----------------------------------------------------------------------
  193. *
  194. * Here starts the interrupt handling routines. All of the following
  195. * subroutines are declared as inline and are folded into
  196. * rs_interrupt(). They were separated out for readability's sake.
  197. *
  198. * Note: rs_interrupt() is a "fast" interrupt, which means that it
  199. * runs with interrupts turned off. People who may want to modify
  200. * rs_interrupt() should try to keep the interrupt handler as fast as
  201. * possible. After you are done making modifications, it is not a bad
  202. * idea to do:
  203. *
  204. * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  205. *
  206. * and look at the resulting assemble code in serial.s.
  207. *
  208. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  209. * -----------------------------------------------------------------------
  210. */
  211. static DEFINE_SPINLOCK(pio_lock);
  212. static inline struct esp_pio_buffer *get_pio_buffer(void)
  213. {
  214. struct esp_pio_buffer *buf;
  215. unsigned long flags;
  216. spin_lock_irqsave(&pio_lock, flags);
  217. if (free_pio_buf) {
  218. buf = free_pio_buf;
  219. free_pio_buf = buf->next;
  220. } else {
  221. buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
  222. }
  223. spin_unlock_irqrestore(&pio_lock, flags);
  224. return buf;
  225. }
  226. static inline void release_pio_buffer(struct esp_pio_buffer *buf)
  227. {
  228. unsigned long flags;
  229. spin_lock_irqsave(&pio_lock, flags);
  230. buf->next = free_pio_buf;
  231. free_pio_buf = buf;
  232. spin_unlock_irqrestore(&pio_lock, flags);
  233. }
  234. static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
  235. {
  236. struct tty_struct *tty = info->tty;
  237. int i;
  238. struct esp_pio_buffer *pio_buf;
  239. struct esp_pio_buffer *err_buf;
  240. unsigned char status_mask;
  241. pio_buf = get_pio_buffer();
  242. if (!pio_buf)
  243. return;
  244. err_buf = get_pio_buffer();
  245. if (!err_buf) {
  246. release_pio_buffer(pio_buf);
  247. return;
  248. }
  249. status_mask = (info->read_status_mask >> 2) & 0x07;
  250. for (i = 0; i < num_bytes - 1; i += 2) {
  251. *((unsigned short *)(pio_buf->data + i)) =
  252. inw(info->port + UART_ESI_RX);
  253. err_buf->data[i] = serial_in(info, UART_ESI_RWS);
  254. err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
  255. err_buf->data[i] &= status_mask;
  256. }
  257. if (num_bytes & 0x0001) {
  258. pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
  259. err_buf->data[num_bytes - 1] =
  260. (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
  261. }
  262. /* make sure everything is still ok since interrupts were enabled */
  263. tty = info->tty;
  264. if (!tty) {
  265. release_pio_buffer(pio_buf);
  266. release_pio_buffer(err_buf);
  267. info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
  268. return;
  269. }
  270. status_mask = (info->ignore_status_mask >> 2) & 0x07;
  271. for (i = 0; i < num_bytes; i++) {
  272. if (!(err_buf->data[i] & status_mask)) {
  273. int flag = 0;
  274. if (err_buf->data[i] & 0x04) {
  275. flag = TTY_BREAK;
  276. if (info->flags & ASYNC_SAK)
  277. do_SAK(tty);
  278. }
  279. else if (err_buf->data[i] & 0x02)
  280. flag = TTY_FRAME;
  281. else if (err_buf->data[i] & 0x01)
  282. flag = TTY_PARITY;
  283. tty_insert_flip_char(tty, pio_buf->data[i], flag);
  284. }
  285. }
  286. tty_schedule_flip(tty);
  287. info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
  288. release_pio_buffer(pio_buf);
  289. release_pio_buffer(err_buf);
  290. }
  291. static inline void receive_chars_dma(struct esp_struct *info, int num_bytes)
  292. {
  293. unsigned long flags;
  294. info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
  295. dma_bytes = num_bytes;
  296. info->stat_flags |= ESP_STAT_DMA_RX;
  297. flags=claim_dma_lock();
  298. disable_dma(dma);
  299. clear_dma_ff(dma);
  300. set_dma_mode(dma, DMA_MODE_READ);
  301. set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
  302. set_dma_count(dma, dma_bytes);
  303. enable_dma(dma);
  304. release_dma_lock(flags);
  305. serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
  306. }
  307. static inline void receive_chars_dma_done(struct esp_struct *info,
  308. int status)
  309. {
  310. struct tty_struct *tty = info->tty;
  311. int num_bytes;
  312. unsigned long flags;
  313. flags=claim_dma_lock();
  314. disable_dma(dma);
  315. clear_dma_ff(dma);
  316. info->stat_flags &= ~ESP_STAT_DMA_RX;
  317. num_bytes = dma_bytes - get_dma_residue(dma);
  318. release_dma_lock(flags);
  319. info->icount.rx += num_bytes;
  320. if (num_bytes > 0) {
  321. tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
  322. status &= (0x1c & info->read_status_mask);
  323. /* Is the status significant or do we throw the last byte ? */
  324. if (!(status & info->ignore_status_mask)) {
  325. int statflag = 0;
  326. if (status & 0x10) {
  327. statflag = TTY_BREAK;
  328. (info->icount.brk)++;
  329. if (info->flags & ASYNC_SAK)
  330. do_SAK(tty);
  331. } else if (status & 0x08) {
  332. statflag = TTY_FRAME;
  333. (info->icount.frame)++;
  334. }
  335. else if (status & 0x04) {
  336. statflag = TTY_PARITY;
  337. (info->icount.parity)++;
  338. }
  339. tty_insert_flip_char(tty, dma_buffer[num_bytes - 1], statflag);
  340. }
  341. tty_schedule_flip(tty);
  342. }
  343. if (dma_bytes != num_bytes) {
  344. num_bytes = dma_bytes - num_bytes;
  345. dma_bytes = 0;
  346. receive_chars_dma(info, num_bytes);
  347. } else
  348. dma_bytes = 0;
  349. }
  350. /* Caller must hold info->lock */
  351. static inline void transmit_chars_pio(struct esp_struct *info,
  352. int space_avail)
  353. {
  354. int i;
  355. struct esp_pio_buffer *pio_buf;
  356. pio_buf = get_pio_buffer();
  357. if (!pio_buf)
  358. return;
  359. while (space_avail && info->xmit_cnt) {
  360. if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
  361. memcpy(pio_buf->data,
  362. &(info->xmit_buf[info->xmit_tail]),
  363. space_avail);
  364. } else {
  365. i = ESP_XMIT_SIZE - info->xmit_tail;
  366. memcpy(pio_buf->data,
  367. &(info->xmit_buf[info->xmit_tail]), i);
  368. memcpy(&(pio_buf->data[i]), info->xmit_buf,
  369. space_avail - i);
  370. }
  371. info->xmit_cnt -= space_avail;
  372. info->xmit_tail = (info->xmit_tail + space_avail) &
  373. (ESP_XMIT_SIZE - 1);
  374. for (i = 0; i < space_avail - 1; i += 2) {
  375. outw(*((unsigned short *)(pio_buf->data + i)),
  376. info->port + UART_ESI_TX);
  377. }
  378. if (space_avail & 0x0001)
  379. serial_out(info, UART_ESI_TX,
  380. pio_buf->data[space_avail - 1]);
  381. if (info->xmit_cnt) {
  382. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  383. serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
  384. space_avail = serial_in(info, UART_ESI_STAT1) << 8;
  385. space_avail |= serial_in(info, UART_ESI_STAT2);
  386. if (space_avail > info->xmit_cnt)
  387. space_avail = info->xmit_cnt;
  388. }
  389. }
  390. if (info->xmit_cnt < WAKEUP_CHARS) {
  391. if (info->tty)
  392. tty_wakeup(info->tty);
  393. #ifdef SERIAL_DEBUG_INTR
  394. printk("THRE...");
  395. #endif
  396. if (info->xmit_cnt <= 0) {
  397. info->IER &= ~UART_IER_THRI;
  398. serial_out(info, UART_ESI_CMD1,
  399. ESI_SET_SRV_MASK);
  400. serial_out(info, UART_ESI_CMD2, info->IER);
  401. }
  402. }
  403. release_pio_buffer(pio_buf);
  404. }
  405. /* Caller must hold info->lock */
  406. static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
  407. {
  408. unsigned long flags;
  409. dma_bytes = num_bytes;
  410. if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
  411. memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
  412. dma_bytes);
  413. } else {
  414. int i = ESP_XMIT_SIZE - info->xmit_tail;
  415. memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
  416. i);
  417. memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
  418. }
  419. info->xmit_cnt -= dma_bytes;
  420. info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
  421. if (info->xmit_cnt < WAKEUP_CHARS) {
  422. if (info->tty)
  423. tty_wakeup(info->tty);
  424. #ifdef SERIAL_DEBUG_INTR
  425. printk("THRE...");
  426. #endif
  427. if (info->xmit_cnt <= 0) {
  428. info->IER &= ~UART_IER_THRI;
  429. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  430. serial_out(info, UART_ESI_CMD2, info->IER);
  431. }
  432. }
  433. info->stat_flags |= ESP_STAT_DMA_TX;
  434. flags=claim_dma_lock();
  435. disable_dma(dma);
  436. clear_dma_ff(dma);
  437. set_dma_mode(dma, DMA_MODE_WRITE);
  438. set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
  439. set_dma_count(dma, dma_bytes);
  440. enable_dma(dma);
  441. release_dma_lock(flags);
  442. serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
  443. }
  444. static inline void transmit_chars_dma_done(struct esp_struct *info)
  445. {
  446. int num_bytes;
  447. unsigned long flags;
  448. flags=claim_dma_lock();
  449. disable_dma(dma);
  450. clear_dma_ff(dma);
  451. num_bytes = dma_bytes - get_dma_residue(dma);
  452. info->icount.tx += dma_bytes;
  453. release_dma_lock(flags);
  454. if (dma_bytes != num_bytes) {
  455. dma_bytes -= num_bytes;
  456. memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
  457. flags=claim_dma_lock();
  458. disable_dma(dma);
  459. clear_dma_ff(dma);
  460. set_dma_mode(dma, DMA_MODE_WRITE);
  461. set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
  462. set_dma_count(dma, dma_bytes);
  463. enable_dma(dma);
  464. release_dma_lock(flags);
  465. serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
  466. } else {
  467. dma_bytes = 0;
  468. info->stat_flags &= ~ESP_STAT_DMA_TX;
  469. }
  470. }
  471. static inline void check_modem_status(struct esp_struct *info)
  472. {
  473. int status;
  474. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  475. status = serial_in(info, UART_ESI_STAT2);
  476. if (status & UART_MSR_ANY_DELTA) {
  477. /* update input line counters */
  478. if (status & UART_MSR_TERI)
  479. info->icount.rng++;
  480. if (status & UART_MSR_DDSR)
  481. info->icount.dsr++;
  482. if (status & UART_MSR_DDCD)
  483. info->icount.dcd++;
  484. if (status & UART_MSR_DCTS)
  485. info->icount.cts++;
  486. wake_up_interruptible(&info->delta_msr_wait);
  487. }
  488. if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
  489. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  490. printk("ttys%d CD now %s...", info->line,
  491. (status & UART_MSR_DCD) ? "on" : "off");
  492. #endif
  493. if (status & UART_MSR_DCD)
  494. wake_up_interruptible(&info->open_wait);
  495. else {
  496. #ifdef SERIAL_DEBUG_OPEN
  497. printk("scheduling hangup...");
  498. #endif
  499. tty_hangup(info->tty);
  500. }
  501. }
  502. }
  503. /*
  504. * This is the serial driver's interrupt routine
  505. */
  506. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  507. {
  508. struct esp_struct * info;
  509. unsigned err_status;
  510. unsigned int scratch;
  511. #ifdef SERIAL_DEBUG_INTR
  512. printk("rs_interrupt_single(%d)...", irq);
  513. #endif
  514. info = (struct esp_struct *)dev_id;
  515. err_status = 0;
  516. scratch = serial_in(info, UART_ESI_SID);
  517. spin_lock(&info->lock);
  518. if (!info->tty) {
  519. spin_unlock(&info->lock);
  520. return IRQ_NONE;
  521. }
  522. if (scratch & 0x04) { /* error */
  523. serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
  524. err_status = serial_in(info, UART_ESI_STAT1);
  525. serial_in(info, UART_ESI_STAT2);
  526. if (err_status & 0x01)
  527. info->stat_flags |= ESP_STAT_RX_TIMEOUT;
  528. if (err_status & 0x20) /* UART status */
  529. check_modem_status(info);
  530. if (err_status & 0x80) /* Start break */
  531. wake_up_interruptible(&info->break_wait);
  532. }
  533. if ((scratch & 0x88) || /* DMA completed or timed out */
  534. (err_status & 0x1c) /* receive error */) {
  535. if (info->stat_flags & ESP_STAT_DMA_RX)
  536. receive_chars_dma_done(info, err_status);
  537. else if (info->stat_flags & ESP_STAT_DMA_TX)
  538. transmit_chars_dma_done(info);
  539. }
  540. if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
  541. ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
  542. (info->IER & UART_IER_RDI)) {
  543. int num_bytes;
  544. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  545. serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
  546. num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
  547. num_bytes |= serial_in(info, UART_ESI_STAT2);
  548. num_bytes = tty_buffer_request_room(info->tty, num_bytes);
  549. if (num_bytes) {
  550. if (dma_bytes ||
  551. (info->stat_flags & ESP_STAT_USE_PIO) ||
  552. (num_bytes <= info->config.pio_threshold))
  553. receive_chars_pio(info, num_bytes);
  554. else
  555. receive_chars_dma(info, num_bytes);
  556. }
  557. }
  558. if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
  559. (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
  560. if ((info->xmit_cnt <= 0) || info->tty->stopped) {
  561. info->IER &= ~UART_IER_THRI;
  562. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  563. serial_out(info, UART_ESI_CMD2, info->IER);
  564. } else {
  565. int num_bytes;
  566. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  567. serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
  568. num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
  569. num_bytes |= serial_in(info, UART_ESI_STAT2);
  570. if (num_bytes > info->xmit_cnt)
  571. num_bytes = info->xmit_cnt;
  572. if (num_bytes) {
  573. if (dma_bytes ||
  574. (info->stat_flags & ESP_STAT_USE_PIO) ||
  575. (num_bytes <= info->config.pio_threshold))
  576. transmit_chars_pio(info, num_bytes);
  577. else
  578. transmit_chars_dma(info, num_bytes);
  579. }
  580. }
  581. }
  582. info->last_active = jiffies;
  583. #ifdef SERIAL_DEBUG_INTR
  584. printk("end.\n");
  585. #endif
  586. spin_unlock(&info->lock);
  587. return IRQ_HANDLED;
  588. }
  589. /*
  590. * -------------------------------------------------------------------
  591. * Here ends the serial interrupt routines.
  592. * -------------------------------------------------------------------
  593. */
  594. /*
  595. * ---------------------------------------------------------------
  596. * Low level utility subroutines for the serial driver: routines to
  597. * figure out the appropriate timeout for an interrupt chain, routines
  598. * to initialize and startup a serial port, and routines to shutdown a
  599. * serial port. Useful stuff like that.
  600. *
  601. * Caller should hold lock
  602. * ---------------------------------------------------------------
  603. */
  604. static inline void esp_basic_init(struct esp_struct * info)
  605. {
  606. /* put ESPC in enhanced mode */
  607. serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
  608. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  609. serial_out(info, UART_ESI_CMD2, 0x01);
  610. else
  611. serial_out(info, UART_ESI_CMD2, 0x31);
  612. /* disable interrupts for now */
  613. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  614. serial_out(info, UART_ESI_CMD2, 0x00);
  615. /* set interrupt and DMA channel */
  616. serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
  617. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  618. serial_out(info, UART_ESI_CMD2, 0x01);
  619. else
  620. serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
  621. serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
  622. if (info->line % 8) /* secondary port */
  623. serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
  624. else if (info->irq == 9)
  625. serial_out(info, UART_ESI_CMD2, 0x02);
  626. else
  627. serial_out(info, UART_ESI_CMD2, info->irq);
  628. /* set error status mask (check this) */
  629. serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
  630. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  631. serial_out(info, UART_ESI_CMD2, 0xa1);
  632. else
  633. serial_out(info, UART_ESI_CMD2, 0xbd);
  634. serial_out(info, UART_ESI_CMD2, 0x00);
  635. /* set DMA timeout */
  636. serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
  637. serial_out(info, UART_ESI_CMD2, 0xff);
  638. /* set FIFO trigger levels */
  639. serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
  640. serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
  641. serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
  642. serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
  643. serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
  644. /* Set clock scaling and wait states */
  645. serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
  646. serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
  647. /* set reinterrupt pacing */
  648. serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
  649. serial_out(info, UART_ESI_CMD2, 0xff);
  650. }
  651. static int startup(struct esp_struct * info)
  652. {
  653. unsigned long flags;
  654. int retval=0;
  655. unsigned int num_chars;
  656. spin_lock_irqsave(&info->lock, flags);
  657. if (info->flags & ASYNC_INITIALIZED)
  658. goto out;
  659. if (!info->xmit_buf) {
  660. info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
  661. retval = -ENOMEM;
  662. if (!info->xmit_buf)
  663. goto out;
  664. }
  665. #ifdef SERIAL_DEBUG_OPEN
  666. printk("starting up ttys%d (irq %d)...", info->line, info->irq);
  667. #endif
  668. /* Flush the RX buffer. Using the ESI flush command may cause */
  669. /* wild interrupts, so read all the data instead. */
  670. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  671. serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
  672. num_chars = serial_in(info, UART_ESI_STAT1) << 8;
  673. num_chars |= serial_in(info, UART_ESI_STAT2);
  674. while (num_chars > 1) {
  675. inw(info->port + UART_ESI_RX);
  676. num_chars -= 2;
  677. }
  678. if (num_chars)
  679. serial_in(info, UART_ESI_RX);
  680. /* set receive character timeout */
  681. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  682. serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
  683. /* clear all flags except the "never DMA" flag */
  684. info->stat_flags &= ESP_STAT_NEVER_DMA;
  685. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  686. info->stat_flags |= ESP_STAT_USE_PIO;
  687. spin_unlock_irqrestore(&info->lock, flags);
  688. /*
  689. * Allocate the IRQ
  690. */
  691. retval = request_irq(info->irq, rs_interrupt_single, IRQF_SHARED,
  692. "esp serial", info);
  693. if (retval) {
  694. if (capable(CAP_SYS_ADMIN)) {
  695. if (info->tty)
  696. set_bit(TTY_IO_ERROR,
  697. &info->tty->flags);
  698. retval = 0;
  699. }
  700. goto out_unlocked;
  701. }
  702. if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
  703. dma_buffer = (char *)__get_dma_pages(
  704. GFP_KERNEL, get_order(DMA_BUFFER_SZ));
  705. /* use PIO mode if DMA buf/chan cannot be allocated */
  706. if (!dma_buffer)
  707. info->stat_flags |= ESP_STAT_USE_PIO;
  708. else if (request_dma(dma, "esp serial")) {
  709. free_pages((unsigned long)dma_buffer,
  710. get_order(DMA_BUFFER_SZ));
  711. dma_buffer = NULL;
  712. info->stat_flags |= ESP_STAT_USE_PIO;
  713. }
  714. }
  715. info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
  716. spin_lock_irqsave(&info->lock, flags);
  717. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  718. serial_out(info, UART_ESI_CMD2, UART_MCR);
  719. serial_out(info, UART_ESI_CMD2, info->MCR);
  720. /*
  721. * Finally, enable interrupts
  722. */
  723. /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
  724. info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
  725. UART_IER_DMA_TC;
  726. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  727. serial_out(info, UART_ESI_CMD2, info->IER);
  728. if (info->tty)
  729. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  730. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  731. spin_unlock_irqrestore(&info->lock, flags);
  732. /*
  733. * Set up the tty->alt_speed kludge
  734. */
  735. if (info->tty) {
  736. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  737. info->tty->alt_speed = 57600;
  738. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  739. info->tty->alt_speed = 115200;
  740. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  741. info->tty->alt_speed = 230400;
  742. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  743. info->tty->alt_speed = 460800;
  744. }
  745. /*
  746. * set the speed of the serial port
  747. */
  748. change_speed(info);
  749. info->flags |= ASYNC_INITIALIZED;
  750. return 0;
  751. out:
  752. spin_unlock_irqrestore(&info->lock, flags);
  753. out_unlocked:
  754. return retval;
  755. }
  756. /*
  757. * This routine will shutdown a serial port; interrupts are disabled, and
  758. * DTR is dropped if the hangup on close termio flag is on.
  759. */
  760. static void shutdown(struct esp_struct * info)
  761. {
  762. unsigned long flags, f;
  763. if (!(info->flags & ASYNC_INITIALIZED))
  764. return;
  765. #ifdef SERIAL_DEBUG_OPEN
  766. printk("Shutting down serial port %d (irq %d)....", info->line,
  767. info->irq);
  768. #endif
  769. spin_lock_irqsave(&info->lock, flags);
  770. /*
  771. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  772. * here so the queue might never be waken up
  773. */
  774. wake_up_interruptible(&info->delta_msr_wait);
  775. wake_up_interruptible(&info->break_wait);
  776. /* stop a DMA transfer on the port being closed */
  777. /* DMA lock is higher priority always */
  778. if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
  779. f=claim_dma_lock();
  780. disable_dma(dma);
  781. clear_dma_ff(dma);
  782. release_dma_lock(f);
  783. dma_bytes = 0;
  784. }
  785. /*
  786. * Free the IRQ
  787. */
  788. free_irq(info->irq, info);
  789. if (dma_buffer) {
  790. struct esp_struct *current_port = ports;
  791. while (current_port) {
  792. if ((current_port != info) &&
  793. (current_port->flags & ASYNC_INITIALIZED))
  794. break;
  795. current_port = current_port->next_port;
  796. }
  797. if (!current_port) {
  798. free_dma(dma);
  799. free_pages((unsigned long)dma_buffer,
  800. get_order(DMA_BUFFER_SZ));
  801. dma_buffer = NULL;
  802. }
  803. }
  804. if (info->xmit_buf) {
  805. free_page((unsigned long) info->xmit_buf);
  806. info->xmit_buf = NULL;
  807. }
  808. info->IER = 0;
  809. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  810. serial_out(info, UART_ESI_CMD2, 0x00);
  811. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  812. info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
  813. info->MCR &= ~UART_MCR_OUT2;
  814. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  815. serial_out(info, UART_ESI_CMD2, UART_MCR);
  816. serial_out(info, UART_ESI_CMD2, info->MCR);
  817. if (info->tty)
  818. set_bit(TTY_IO_ERROR, &info->tty->flags);
  819. info->flags &= ~ASYNC_INITIALIZED;
  820. spin_unlock_irqrestore(&info->lock, flags);
  821. }
  822. /*
  823. * This routine is called to set the UART divisor registers to match
  824. * the specified baud rate for a serial port.
  825. */
  826. static void change_speed(struct esp_struct *info)
  827. {
  828. unsigned short port;
  829. int quot = 0;
  830. unsigned cflag,cval;
  831. int baud, bits;
  832. unsigned char flow1 = 0, flow2 = 0;
  833. unsigned long flags;
  834. if (!info->tty || !info->tty->termios)
  835. return;
  836. cflag = info->tty->termios->c_cflag;
  837. port = info->port;
  838. /* byte size and parity */
  839. switch (cflag & CSIZE) {
  840. case CS5: cval = 0x00; bits = 7; break;
  841. case CS6: cval = 0x01; bits = 8; break;
  842. case CS7: cval = 0x02; bits = 9; break;
  843. case CS8: cval = 0x03; bits = 10; break;
  844. default: cval = 0x00; bits = 7; break;
  845. }
  846. if (cflag & CSTOPB) {
  847. cval |= 0x04;
  848. bits++;
  849. }
  850. if (cflag & PARENB) {
  851. cval |= UART_LCR_PARITY;
  852. bits++;
  853. }
  854. if (!(cflag & PARODD))
  855. cval |= UART_LCR_EPAR;
  856. #ifdef CMSPAR
  857. if (cflag & CMSPAR)
  858. cval |= UART_LCR_SPAR;
  859. #endif
  860. baud = tty_get_baud_rate(info->tty);
  861. if (baud == 38400 &&
  862. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  863. quot = info->custom_divisor;
  864. else {
  865. if (baud == 134)
  866. /* Special case since 134 is really 134.5 */
  867. quot = (2*BASE_BAUD / 269);
  868. else if (baud)
  869. quot = BASE_BAUD / baud;
  870. }
  871. /* If the quotient is ever zero, default to 9600 bps */
  872. if (!quot)
  873. quot = BASE_BAUD / 9600;
  874. info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
  875. /* CTS flow control flag and modem status interrupts */
  876. /* info->IER &= ~UART_IER_MSI; */
  877. if (cflag & CRTSCTS) {
  878. info->flags |= ASYNC_CTS_FLOW;
  879. /* info->IER |= UART_IER_MSI; */
  880. flow1 = 0x04;
  881. flow2 = 0x10;
  882. } else
  883. info->flags &= ~ASYNC_CTS_FLOW;
  884. if (cflag & CLOCAL)
  885. info->flags &= ~ASYNC_CHECK_CD;
  886. else {
  887. info->flags |= ASYNC_CHECK_CD;
  888. /* info->IER |= UART_IER_MSI; */
  889. }
  890. /*
  891. * Set up parity check flag
  892. */
  893. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  894. if (I_INPCK(info->tty))
  895. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  896. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  897. info->read_status_mask |= UART_LSR_BI;
  898. info->ignore_status_mask = 0;
  899. #if 0
  900. /* This should be safe, but for some broken bits of hardware... */
  901. if (I_IGNPAR(info->tty)) {
  902. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  903. info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
  904. }
  905. #endif
  906. if (I_IGNBRK(info->tty)) {
  907. info->ignore_status_mask |= UART_LSR_BI;
  908. info->read_status_mask |= UART_LSR_BI;
  909. /*
  910. * If we're ignore parity and break indicators, ignore
  911. * overruns too. (For real raw support).
  912. */
  913. if (I_IGNPAR(info->tty)) {
  914. info->ignore_status_mask |= UART_LSR_OE | \
  915. UART_LSR_PE | UART_LSR_FE;
  916. info->read_status_mask |= UART_LSR_OE | \
  917. UART_LSR_PE | UART_LSR_FE;
  918. }
  919. }
  920. if (I_IXOFF(info->tty))
  921. flow1 |= 0x81;
  922. spin_lock_irqsave(&info->lock, flags);
  923. /* set baud */
  924. serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
  925. serial_out(info, UART_ESI_CMD2, quot >> 8);
  926. serial_out(info, UART_ESI_CMD2, quot & 0xff);
  927. /* set data bits, parity, etc. */
  928. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  929. serial_out(info, UART_ESI_CMD2, UART_LCR);
  930. serial_out(info, UART_ESI_CMD2, cval);
  931. /* Enable flow control */
  932. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
  933. serial_out(info, UART_ESI_CMD2, flow1);
  934. serial_out(info, UART_ESI_CMD2, flow2);
  935. /* set flow control characters (XON/XOFF only) */
  936. if (I_IXOFF(info->tty)) {
  937. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
  938. serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
  939. serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
  940. serial_out(info, UART_ESI_CMD2, 0x10);
  941. serial_out(info, UART_ESI_CMD2, 0x21);
  942. switch (cflag & CSIZE) {
  943. case CS5:
  944. serial_out(info, UART_ESI_CMD2, 0x1f);
  945. break;
  946. case CS6:
  947. serial_out(info, UART_ESI_CMD2, 0x3f);
  948. break;
  949. case CS7:
  950. case CS8:
  951. serial_out(info, UART_ESI_CMD2, 0x7f);
  952. break;
  953. default:
  954. serial_out(info, UART_ESI_CMD2, 0xff);
  955. break;
  956. }
  957. }
  958. /* Set high/low water */
  959. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
  960. serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
  961. serial_out(info, UART_ESI_CMD2, info->config.flow_off);
  962. serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
  963. serial_out(info, UART_ESI_CMD2, info->config.flow_on);
  964. spin_unlock_irqrestore(&info->lock, flags);
  965. }
  966. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  967. {
  968. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  969. unsigned long flags;
  970. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  971. return;
  972. if (!info->xmit_buf)
  973. return;
  974. spin_lock_irqsave(&info->lock, flags);
  975. if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
  976. info->xmit_buf[info->xmit_head++] = ch;
  977. info->xmit_head &= ESP_XMIT_SIZE-1;
  978. info->xmit_cnt++;
  979. }
  980. spin_unlock_irqrestore(&info->lock, flags);
  981. }
  982. static void rs_flush_chars(struct tty_struct *tty)
  983. {
  984. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  985. unsigned long flags;
  986. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  987. return;
  988. spin_lock_irqsave(&info->lock, flags);
  989. if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
  990. goto out;
  991. if (!(info->IER & UART_IER_THRI)) {
  992. info->IER |= UART_IER_THRI;
  993. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  994. serial_out(info, UART_ESI_CMD2, info->IER);
  995. }
  996. out:
  997. spin_unlock_irqrestore(&info->lock, flags);
  998. }
  999. static int rs_write(struct tty_struct * tty,
  1000. const unsigned char *buf, int count)
  1001. {
  1002. int c, t, ret = 0;
  1003. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1004. unsigned long flags;
  1005. if (serial_paranoia_check(info, tty->name, "rs_write"))
  1006. return 0;
  1007. if (!info->xmit_buf)
  1008. return 0;
  1009. while (1) {
  1010. /* Thanks to R. Wolff for suggesting how to do this with */
  1011. /* interrupts enabled */
  1012. c = count;
  1013. t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
  1014. if (t < c)
  1015. c = t;
  1016. t = ESP_XMIT_SIZE - info->xmit_head;
  1017. if (t < c)
  1018. c = t;
  1019. if (c <= 0)
  1020. break;
  1021. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  1022. info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
  1023. info->xmit_cnt += c;
  1024. buf += c;
  1025. count -= c;
  1026. ret += c;
  1027. }
  1028. spin_lock_irqsave(&info->lock, flags);
  1029. if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
  1030. info->IER |= UART_IER_THRI;
  1031. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1032. serial_out(info, UART_ESI_CMD2, info->IER);
  1033. }
  1034. spin_unlock_irqrestore(&info->lock, flags);
  1035. return ret;
  1036. }
  1037. static int rs_write_room(struct tty_struct *tty)
  1038. {
  1039. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1040. int ret;
  1041. unsigned long flags;
  1042. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  1043. return 0;
  1044. spin_lock_irqsave(&info->lock, flags);
  1045. ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
  1046. if (ret < 0)
  1047. ret = 0;
  1048. spin_unlock_irqrestore(&info->lock, flags);
  1049. return ret;
  1050. }
  1051. static int rs_chars_in_buffer(struct tty_struct *tty)
  1052. {
  1053. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1054. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  1055. return 0;
  1056. return info->xmit_cnt;
  1057. }
  1058. static void rs_flush_buffer(struct tty_struct *tty)
  1059. {
  1060. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1061. unsigned long flags;
  1062. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  1063. return;
  1064. spin_lock_irqsave(&info->lock, flags);
  1065. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1066. spin_unlock_irqrestore(&info->lock, flags);
  1067. tty_wakeup(tty);
  1068. }
  1069. /*
  1070. * ------------------------------------------------------------
  1071. * rs_throttle()
  1072. *
  1073. * This routine is called by the upper-layer tty layer to signal that
  1074. * incoming characters should be throttled.
  1075. * ------------------------------------------------------------
  1076. */
  1077. static void rs_throttle(struct tty_struct * tty)
  1078. {
  1079. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1080. unsigned long flags;
  1081. #ifdef SERIAL_DEBUG_THROTTLE
  1082. char buf[64];
  1083. printk("throttle %s: %d....\n", tty_name(tty, buf),
  1084. tty->ldisc.chars_in_buffer(tty));
  1085. #endif
  1086. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  1087. return;
  1088. spin_lock_irqsave(&info->lock, flags);
  1089. info->IER &= ~UART_IER_RDI;
  1090. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1091. serial_out(info, UART_ESI_CMD2, info->IER);
  1092. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  1093. serial_out(info, UART_ESI_CMD2, 0x00);
  1094. spin_unlock_irqrestore(&info->lock, flags);
  1095. }
  1096. static void rs_unthrottle(struct tty_struct * tty)
  1097. {
  1098. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1099. unsigned long flags;
  1100. #ifdef SERIAL_DEBUG_THROTTLE
  1101. char buf[64];
  1102. printk("unthrottle %s: %d....\n", tty_name(tty, buf),
  1103. tty->ldisc.chars_in_buffer(tty));
  1104. #endif
  1105. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  1106. return;
  1107. spin_lock_irqsave(&info->lock, flags);
  1108. info->IER |= UART_IER_RDI;
  1109. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1110. serial_out(info, UART_ESI_CMD2, info->IER);
  1111. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  1112. serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
  1113. spin_unlock_irqrestore(&info->lock, flags);
  1114. }
  1115. /*
  1116. * ------------------------------------------------------------
  1117. * rs_ioctl() and friends
  1118. * ------------------------------------------------------------
  1119. */
  1120. static int get_serial_info(struct esp_struct * info,
  1121. struct serial_struct __user *retinfo)
  1122. {
  1123. struct serial_struct tmp;
  1124. lock_kernel();
  1125. memset(&tmp, 0, sizeof(tmp));
  1126. tmp.type = PORT_16550A;
  1127. tmp.line = info->line;
  1128. tmp.port = info->port;
  1129. tmp.irq = info->irq;
  1130. tmp.flags = info->flags;
  1131. tmp.xmit_fifo_size = 1024;
  1132. tmp.baud_base = BASE_BAUD;
  1133. tmp.close_delay = info->close_delay;
  1134. tmp.closing_wait = info->closing_wait;
  1135. tmp.custom_divisor = info->custom_divisor;
  1136. tmp.hub6 = 0;
  1137. unlock_kernel();
  1138. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  1139. return -EFAULT;
  1140. return 0;
  1141. }
  1142. static int get_esp_config(struct esp_struct * info,
  1143. struct hayes_esp_config __user *retinfo)
  1144. {
  1145. struct hayes_esp_config tmp;
  1146. if (!retinfo)
  1147. return -EFAULT;
  1148. memset(&tmp, 0, sizeof(tmp));
  1149. lock_kernel();
  1150. tmp.rx_timeout = info->config.rx_timeout;
  1151. tmp.rx_trigger = info->config.rx_trigger;
  1152. tmp.tx_trigger = info->config.tx_trigger;
  1153. tmp.flow_off = info->config.flow_off;
  1154. tmp.flow_on = info->config.flow_on;
  1155. tmp.pio_threshold = info->config.pio_threshold;
  1156. tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
  1157. unlock_kernel();
  1158. return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
  1159. }
  1160. static int set_serial_info(struct esp_struct * info,
  1161. struct serial_struct __user *new_info)
  1162. {
  1163. struct serial_struct new_serial;
  1164. struct esp_struct old_info;
  1165. unsigned int change_irq;
  1166. int retval = 0;
  1167. struct esp_struct *current_async;
  1168. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  1169. return -EFAULT;
  1170. old_info = *info;
  1171. if ((new_serial.type != PORT_16550A) ||
  1172. (new_serial.hub6) ||
  1173. (info->port != new_serial.port) ||
  1174. (new_serial.baud_base != BASE_BAUD) ||
  1175. (new_serial.irq > 15) ||
  1176. (new_serial.irq < 2) ||
  1177. (new_serial.irq == 6) ||
  1178. (new_serial.irq == 8) ||
  1179. (new_serial.irq == 13))
  1180. return -EINVAL;
  1181. change_irq = new_serial.irq != info->irq;
  1182. if (change_irq && (info->line % 8))
  1183. return -EINVAL;
  1184. if (!capable(CAP_SYS_ADMIN)) {
  1185. if (change_irq ||
  1186. (new_serial.close_delay != info->close_delay) ||
  1187. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  1188. (info->flags & ~ASYNC_USR_MASK)))
  1189. return -EPERM;
  1190. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  1191. (new_serial.flags & ASYNC_USR_MASK));
  1192. info->custom_divisor = new_serial.custom_divisor;
  1193. } else {
  1194. if (new_serial.irq == 2)
  1195. new_serial.irq = 9;
  1196. if (change_irq) {
  1197. current_async = ports;
  1198. while (current_async) {
  1199. if ((current_async->line >= info->line) &&
  1200. (current_async->line < (info->line + 8))) {
  1201. if (current_async == info) {
  1202. if (current_async->count > 1)
  1203. return -EBUSY;
  1204. } else if (current_async->count)
  1205. return -EBUSY;
  1206. }
  1207. current_async = current_async->next_port;
  1208. }
  1209. }
  1210. /*
  1211. * OK, past this point, all the error checking has been done.
  1212. * At this point, we start making changes.....
  1213. */
  1214. info->flags = ((info->flags & ~ASYNC_FLAGS) |
  1215. (new_serial.flags & ASYNC_FLAGS));
  1216. info->custom_divisor = new_serial.custom_divisor;
  1217. info->close_delay = new_serial.close_delay * HZ/100;
  1218. info->closing_wait = new_serial.closing_wait * HZ/100;
  1219. if (change_irq) {
  1220. /*
  1221. * We need to shutdown the serial port at the old
  1222. * port/irq combination.
  1223. */
  1224. shutdown(info);
  1225. current_async = ports;
  1226. while (current_async) {
  1227. if ((current_async->line >= info->line) &&
  1228. (current_async->line < (info->line + 8)))
  1229. current_async->irq = new_serial.irq;
  1230. current_async = current_async->next_port;
  1231. }
  1232. serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
  1233. if (info->irq == 9)
  1234. serial_out(info, UART_ESI_CMD2, 0x02);
  1235. else
  1236. serial_out(info, UART_ESI_CMD2, info->irq);
  1237. }
  1238. }
  1239. if (info->flags & ASYNC_INITIALIZED) {
  1240. if (((old_info.flags & ASYNC_SPD_MASK) !=
  1241. (info->flags & ASYNC_SPD_MASK)) ||
  1242. (old_info.custom_divisor != info->custom_divisor)) {
  1243. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  1244. info->tty->alt_speed = 57600;
  1245. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  1246. info->tty->alt_speed = 115200;
  1247. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  1248. info->tty->alt_speed = 230400;
  1249. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  1250. info->tty->alt_speed = 460800;
  1251. change_speed(info);
  1252. }
  1253. } else
  1254. retval = startup(info);
  1255. return retval;
  1256. }
  1257. static int set_esp_config(struct esp_struct * info,
  1258. struct hayes_esp_config __user * new_info)
  1259. {
  1260. struct hayes_esp_config new_config;
  1261. unsigned int change_dma;
  1262. int retval = 0;
  1263. struct esp_struct *current_async;
  1264. unsigned long flags;
  1265. /* Perhaps a non-sysadmin user should be able to do some of these */
  1266. /* operations. I haven't decided yet. */
  1267. if (!capable(CAP_SYS_ADMIN))
  1268. return -EPERM;
  1269. if (copy_from_user(&new_config, new_info, sizeof(new_config)))
  1270. return -EFAULT;
  1271. if ((new_config.flow_on >= new_config.flow_off) ||
  1272. (new_config.rx_trigger < 1) ||
  1273. (new_config.tx_trigger < 1) ||
  1274. (new_config.flow_off < 1) ||
  1275. (new_config.flow_on < 1) ||
  1276. (new_config.rx_trigger > 1023) ||
  1277. (new_config.tx_trigger > 1023) ||
  1278. (new_config.flow_off > 1023) ||
  1279. (new_config.flow_on > 1023) ||
  1280. (new_config.pio_threshold < 0) ||
  1281. (new_config.pio_threshold > 1024))
  1282. return -EINVAL;
  1283. if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
  1284. new_config.dma_channel = 0;
  1285. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  1286. change_dma = new_config.dma_channel;
  1287. else
  1288. change_dma = (new_config.dma_channel != dma);
  1289. if (change_dma) {
  1290. if (new_config.dma_channel) {
  1291. /* PIO mode to DMA mode transition OR */
  1292. /* change current DMA channel */
  1293. current_async = ports;
  1294. while (current_async) {
  1295. if (current_async == info) {
  1296. if (current_async->count > 1)
  1297. return -EBUSY;
  1298. } else if (current_async->count)
  1299. return -EBUSY;
  1300. current_async =
  1301. current_async->next_port;
  1302. }
  1303. shutdown(info);
  1304. dma = new_config.dma_channel;
  1305. info->stat_flags &= ~ESP_STAT_NEVER_DMA;
  1306. /* all ports must use the same DMA channel */
  1307. spin_lock_irqsave(&info->lock, flags);
  1308. current_async = ports;
  1309. while (current_async) {
  1310. esp_basic_init(current_async);
  1311. current_async = current_async->next_port;
  1312. }
  1313. spin_unlock_irqrestore(&info->lock, flags);
  1314. } else {
  1315. /* DMA mode to PIO mode only */
  1316. if (info->count > 1)
  1317. return -EBUSY;
  1318. shutdown(info);
  1319. spin_lock_irqsave(&info->lock, flags);
  1320. info->stat_flags |= ESP_STAT_NEVER_DMA;
  1321. esp_basic_init(info);
  1322. spin_unlock_irqrestore(&info->lock, flags);
  1323. }
  1324. }
  1325. info->config.pio_threshold = new_config.pio_threshold;
  1326. if ((new_config.flow_off != info->config.flow_off) ||
  1327. (new_config.flow_on != info->config.flow_on)) {
  1328. unsigned long flags;
  1329. info->config.flow_off = new_config.flow_off;
  1330. info->config.flow_on = new_config.flow_on;
  1331. spin_lock_irqsave(&info->lock, flags);
  1332. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
  1333. serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
  1334. serial_out(info, UART_ESI_CMD2, new_config.flow_off);
  1335. serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
  1336. serial_out(info, UART_ESI_CMD2, new_config.flow_on);
  1337. spin_unlock_irqrestore(&info->lock, flags);
  1338. }
  1339. if ((new_config.rx_trigger != info->config.rx_trigger) ||
  1340. (new_config.tx_trigger != info->config.tx_trigger)) {
  1341. unsigned long flags;
  1342. info->config.rx_trigger = new_config.rx_trigger;
  1343. info->config.tx_trigger = new_config.tx_trigger;
  1344. spin_lock_irqsave(&info->lock, flags);
  1345. serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
  1346. serial_out(info, UART_ESI_CMD2,
  1347. new_config.rx_trigger >> 8);
  1348. serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
  1349. serial_out(info, UART_ESI_CMD2,
  1350. new_config.tx_trigger >> 8);
  1351. serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
  1352. spin_unlock_irqrestore(&info->lock, flags);
  1353. }
  1354. if (new_config.rx_timeout != info->config.rx_timeout) {
  1355. unsigned long flags;
  1356. info->config.rx_timeout = new_config.rx_timeout;
  1357. spin_lock_irqsave(&info->lock, flags);
  1358. if (info->IER & UART_IER_RDI) {
  1359. serial_out(info, UART_ESI_CMD1,
  1360. ESI_SET_RX_TIMEOUT);
  1361. serial_out(info, UART_ESI_CMD2,
  1362. new_config.rx_timeout);
  1363. }
  1364. spin_unlock_irqrestore(&info->lock, flags);
  1365. }
  1366. if (!(info->flags & ASYNC_INITIALIZED))
  1367. retval = startup(info);
  1368. return retval;
  1369. }
  1370. /*
  1371. * get_lsr_info - get line status register info
  1372. *
  1373. * Purpose: Let user call ioctl() to get info when the UART physically
  1374. * is emptied. On bus types like RS485, the transmitter must
  1375. * release the bus after transmitting. This must be done when
  1376. * the transmit shift register is empty, not be done when the
  1377. * transmit holding register is empty. This functionality
  1378. * allows an RS485 driver to be written in user space.
  1379. */
  1380. static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
  1381. {
  1382. unsigned char status;
  1383. unsigned int result;
  1384. unsigned long flags;
  1385. spin_lock_irqsave(&info->lock, flags);
  1386. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  1387. status = serial_in(info, UART_ESI_STAT1);
  1388. spin_unlock_irqrestore(&info->lock, flags);
  1389. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1390. return put_user(result,value);
  1391. }
  1392. static int esp_tiocmget(struct tty_struct *tty, struct file *file)
  1393. {
  1394. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1395. unsigned char control, status;
  1396. unsigned long flags;
  1397. if (serial_paranoia_check(info, tty->name, __FUNCTION__))
  1398. return -ENODEV;
  1399. if (tty->flags & (1 << TTY_IO_ERROR))
  1400. return -EIO;
  1401. control = info->MCR;
  1402. spin_lock_irqsave(&info->lock, flags);
  1403. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  1404. status = serial_in(info, UART_ESI_STAT2);
  1405. spin_unlock_irqrestore(&info->lock, flags);
  1406. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
  1407. | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
  1408. | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
  1409. | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
  1410. | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
  1411. | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1412. }
  1413. static int esp_tiocmset(struct tty_struct *tty, struct file *file,
  1414. unsigned int set, unsigned int clear)
  1415. {
  1416. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1417. unsigned long flags;
  1418. if (serial_paranoia_check(info, tty->name, __FUNCTION__))
  1419. return -ENODEV;
  1420. if (tty->flags & (1 << TTY_IO_ERROR))
  1421. return -EIO;
  1422. spin_lock_irqsave(&info->lock, flags);
  1423. if (set & TIOCM_RTS)
  1424. info->MCR |= UART_MCR_RTS;
  1425. if (set & TIOCM_DTR)
  1426. info->MCR |= UART_MCR_DTR;
  1427. if (clear & TIOCM_RTS)
  1428. info->MCR &= ~UART_MCR_RTS;
  1429. if (clear & TIOCM_DTR)
  1430. info->MCR &= ~UART_MCR_DTR;
  1431. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1432. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1433. serial_out(info, UART_ESI_CMD2, info->MCR);
  1434. spin_unlock_irqrestore(&info->lock, flags);
  1435. return 0;
  1436. }
  1437. /*
  1438. * rs_break() --- routine which turns the break handling on or off
  1439. */
  1440. static void esp_break(struct tty_struct *tty, int break_state)
  1441. {
  1442. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1443. unsigned long flags;
  1444. if (serial_paranoia_check(info, tty->name, "esp_break"))
  1445. return;
  1446. if (break_state == -1) {
  1447. spin_lock_irqsave(&info->lock, flags);
  1448. serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
  1449. serial_out(info, UART_ESI_CMD2, 0x01);
  1450. spin_unlock_irqrestore(&info->lock, flags);
  1451. /* FIXME - new style wait needed here */
  1452. interruptible_sleep_on(&info->break_wait);
  1453. } else {
  1454. spin_lock_irqsave(&info->lock, flags);
  1455. serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
  1456. serial_out(info, UART_ESI_CMD2, 0x00);
  1457. spin_unlock_irqrestore(&info->lock, flags);
  1458. }
  1459. }
  1460. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1461. unsigned int cmd, unsigned long arg)
  1462. {
  1463. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1464. struct async_icount cprev, cnow; /* kernel counter temps */
  1465. struct serial_icounter_struct __user *p_cuser; /* user space */
  1466. void __user *argp = (void __user *)arg;
  1467. unsigned long flags;
  1468. int ret;
  1469. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1470. return -ENODEV;
  1471. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1472. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  1473. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
  1474. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
  1475. (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
  1476. if (tty->flags & (1 << TTY_IO_ERROR))
  1477. return -EIO;
  1478. }
  1479. switch (cmd) {
  1480. case TIOCGSERIAL:
  1481. return get_serial_info(info, argp);
  1482. case TIOCSSERIAL:
  1483. lock_kernel();
  1484. ret = set_serial_info(info, argp);
  1485. unlock_kernel();
  1486. return ret;
  1487. case TIOCSERCONFIG:
  1488. /* do not reconfigure after initial configuration */
  1489. return 0;
  1490. case TIOCSERGWILD:
  1491. return put_user(0L, (unsigned long __user *)argp);
  1492. case TIOCSERGETLSR: /* Get line status register */
  1493. return get_lsr_info(info, argp);
  1494. case TIOCSERSWILD:
  1495. if (!capable(CAP_SYS_ADMIN))
  1496. return -EPERM;
  1497. return 0;
  1498. /*
  1499. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1500. * - mask passed in arg for lines of interest
  1501. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1502. * Caller should use TIOCGICOUNT to see which one it was
  1503. */
  1504. case TIOCMIWAIT:
  1505. spin_lock_irqsave(&info->lock, flags);
  1506. cprev = info->icount; /* note the counters on entry */
  1507. spin_unlock_irqrestore(&info->lock, flags);
  1508. while (1) {
  1509. /* FIXME: convert to new style wakeup */
  1510. interruptible_sleep_on(&info->delta_msr_wait);
  1511. /* see if a signal did it */
  1512. if (signal_pending(current))
  1513. return -ERESTARTSYS;
  1514. spin_lock_irqsave(&info->lock, flags);
  1515. cnow = info->icount; /* atomic copy */
  1516. spin_unlock_irqrestore(&info->lock, flags);
  1517. if (cnow.rng == cprev.rng &&
  1518. cnow.dsr == cprev.dsr &&
  1519. cnow.dcd == cprev.dcd &&
  1520. cnow.cts == cprev.cts)
  1521. return -EIO; /* no change => error */
  1522. if (((arg & TIOCM_RNG) &&
  1523. (cnow.rng != cprev.rng)) ||
  1524. ((arg & TIOCM_DSR) &&
  1525. (cnow.dsr != cprev.dsr)) ||
  1526. ((arg & TIOCM_CD) &&
  1527. (cnow.dcd != cprev.dcd)) ||
  1528. ((arg & TIOCM_CTS) &&
  1529. (cnow.cts != cprev.cts)) ) {
  1530. return 0;
  1531. }
  1532. cprev = cnow;
  1533. }
  1534. /* NOTREACHED */
  1535. /*
  1536. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1537. * Return: write counters to the user passed counter struct
  1538. * NB: both 1->0 and 0->1 transitions are counted except for
  1539. * RI where only 0->1 is counted.
  1540. */
  1541. case TIOCGICOUNT:
  1542. spin_lock_irqsave(&info->lock, flags);
  1543. cnow = info->icount;
  1544. spin_unlock_irqrestore(&info->lock, flags);
  1545. p_cuser = argp;
  1546. if (put_user(cnow.cts, &p_cuser->cts) ||
  1547. put_user(cnow.dsr, &p_cuser->dsr) ||
  1548. put_user(cnow.rng, &p_cuser->rng) ||
  1549. put_user(cnow.dcd, &p_cuser->dcd))
  1550. return -EFAULT;
  1551. return 0;
  1552. case TIOCGHAYESESP:
  1553. return get_esp_config(info, argp);
  1554. case TIOCSHAYESESP:
  1555. lock_kernel();
  1556. ret = set_esp_config(info, argp);
  1557. unlock_kernel();
  1558. return ret;
  1559. default:
  1560. return -ENOIOCTLCMD;
  1561. }
  1562. return 0;
  1563. }
  1564. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1565. {
  1566. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1567. unsigned long flags;
  1568. change_speed(info);
  1569. spin_lock_irqsave(&info->lock, flags);
  1570. /* Handle transition to B0 status */
  1571. if ((old_termios->c_cflag & CBAUD) &&
  1572. !(tty->termios->c_cflag & CBAUD)) {
  1573. info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
  1574. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1575. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1576. serial_out(info, UART_ESI_CMD2, info->MCR);
  1577. }
  1578. /* Handle transition away from B0 status */
  1579. if (!(old_termios->c_cflag & CBAUD) &&
  1580. (tty->termios->c_cflag & CBAUD)) {
  1581. info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1582. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1583. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1584. serial_out(info, UART_ESI_CMD2, info->MCR);
  1585. }
  1586. spin_unlock_irqrestore(&info->lock, flags);
  1587. /* Handle turning of CRTSCTS */
  1588. if ((old_termios->c_cflag & CRTSCTS) &&
  1589. !(tty->termios->c_cflag & CRTSCTS)) {
  1590. rs_start(tty);
  1591. }
  1592. }
  1593. /*
  1594. * ------------------------------------------------------------
  1595. * rs_close()
  1596. *
  1597. * This routine is called when the serial port gets closed. First, we
  1598. * wait for the last remaining data to be sent. Then, we unlink its
  1599. * async structure from the interrupt chain if necessary, and we free
  1600. * that IRQ if nothing is left in the chain.
  1601. * ------------------------------------------------------------
  1602. */
  1603. static void rs_close(struct tty_struct *tty, struct file * filp)
  1604. {
  1605. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1606. unsigned long flags;
  1607. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1608. return;
  1609. spin_lock_irqsave(&info->lock, flags);
  1610. if (tty_hung_up_p(filp)) {
  1611. DBG_CNT("before DEC-hung");
  1612. goto out;
  1613. }
  1614. #ifdef SERIAL_DEBUG_OPEN
  1615. printk("rs_close ttys%d, count = %d\n", info->line, info->count);
  1616. #endif
  1617. if ((tty->count == 1) && (info->count != 1)) {
  1618. /*
  1619. * Uh, oh. tty->count is 1, which means that the tty
  1620. * structure will be freed. Info->count should always
  1621. * be one in these conditions. If it's greater than
  1622. * one, we've got real problems, since it means the
  1623. * serial port won't be shutdown.
  1624. */
  1625. printk("rs_close: bad serial port count; tty->count is 1, "
  1626. "info->count is %d\n", info->count);
  1627. info->count = 1;
  1628. }
  1629. if (--info->count < 0) {
  1630. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1631. info->line, info->count);
  1632. info->count = 0;
  1633. }
  1634. if (info->count) {
  1635. DBG_CNT("before DEC-2");
  1636. goto out;
  1637. }
  1638. info->flags |= ASYNC_CLOSING;
  1639. spin_unlock_irqrestore(&info->lock, flags);
  1640. /*
  1641. * Now we wait for the transmit buffer to clear; and we notify
  1642. * the line discipline to only process XON/XOFF characters.
  1643. */
  1644. tty->closing = 1;
  1645. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1646. tty_wait_until_sent(tty, info->closing_wait);
  1647. /*
  1648. * At this point we stop accepting input. To do this, we
  1649. * disable the receive line status interrupts, and tell the
  1650. * interrupt driver to stop checking the data ready bit in the
  1651. * line status register.
  1652. */
  1653. /* info->IER &= ~UART_IER_RLSI; */
  1654. info->IER &= ~UART_IER_RDI;
  1655. info->read_status_mask &= ~UART_LSR_DR;
  1656. if (info->flags & ASYNC_INITIALIZED) {
  1657. spin_lock_irqsave(&info->lock, flags);
  1658. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1659. serial_out(info, UART_ESI_CMD2, info->IER);
  1660. /* disable receive timeout */
  1661. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  1662. serial_out(info, UART_ESI_CMD2, 0x00);
  1663. spin_unlock_irqrestore(&info->lock, flags);
  1664. /*
  1665. * Before we drop DTR, make sure the UART transmitter
  1666. * has completely drained; this is especially
  1667. * important if there is a transmit FIFO!
  1668. */
  1669. rs_wait_until_sent(tty, info->timeout);
  1670. }
  1671. shutdown(info);
  1672. if (tty->driver->flush_buffer)
  1673. tty->driver->flush_buffer(tty);
  1674. tty_ldisc_flush(tty);
  1675. tty->closing = 0;
  1676. info->tty = NULL;
  1677. if (info->blocked_open) {
  1678. if (info->close_delay) {
  1679. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1680. }
  1681. wake_up_interruptible(&info->open_wait);
  1682. }
  1683. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1684. wake_up_interruptible(&info->close_wait);
  1685. return;
  1686. out:
  1687. spin_unlock_irqrestore(&info->lock, flags);
  1688. }
  1689. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1690. {
  1691. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1692. unsigned long orig_jiffies, char_time;
  1693. unsigned long flags;
  1694. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1695. return;
  1696. orig_jiffies = jiffies;
  1697. char_time = ((info->timeout - HZ / 50) / 1024) / 5;
  1698. if (!char_time)
  1699. char_time = 1;
  1700. spin_lock_irqsave(&info->lock, flags);
  1701. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  1702. serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
  1703. while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
  1704. (serial_in(info, UART_ESI_STAT2) != 0xff)) {
  1705. spin_unlock_irqrestore(&info->lock, flags);
  1706. msleep_interruptible(jiffies_to_msecs(char_time));
  1707. if (signal_pending(current))
  1708. break;
  1709. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1710. break;
  1711. spin_lock_irqsave(&info->lock, flags);
  1712. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  1713. serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
  1714. }
  1715. spin_unlock_irqrestore(&info->lock, flags);
  1716. set_current_state(TASK_RUNNING);
  1717. }
  1718. /*
  1719. * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
  1720. */
  1721. static void esp_hangup(struct tty_struct *tty)
  1722. {
  1723. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1724. if (serial_paranoia_check(info, tty->name, "esp_hangup"))
  1725. return;
  1726. rs_flush_buffer(tty);
  1727. shutdown(info);
  1728. info->count = 0;
  1729. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1730. info->tty = NULL;
  1731. wake_up_interruptible(&info->open_wait);
  1732. }
  1733. /*
  1734. * ------------------------------------------------------------
  1735. * esp_open() and friends
  1736. * ------------------------------------------------------------
  1737. */
  1738. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1739. struct esp_struct *info)
  1740. {
  1741. DECLARE_WAITQUEUE(wait, current);
  1742. int retval;
  1743. int do_clocal = 0;
  1744. unsigned long flags;
  1745. /*
  1746. * If the device is in the middle of being closed, then block
  1747. * until it's done, and then try again.
  1748. */
  1749. if (tty_hung_up_p(filp) ||
  1750. (info->flags & ASYNC_CLOSING)) {
  1751. if (info->flags & ASYNC_CLOSING)
  1752. interruptible_sleep_on(&info->close_wait);
  1753. #ifdef SERIAL_DO_RESTART
  1754. if (info->flags & ASYNC_HUP_NOTIFY)
  1755. return -EAGAIN;
  1756. else
  1757. return -ERESTARTSYS;
  1758. #else
  1759. return -EAGAIN;
  1760. #endif
  1761. }
  1762. /*
  1763. * If non-blocking mode is set, or the port is not enabled,
  1764. * then make the check up front and then exit.
  1765. */
  1766. if ((filp->f_flags & O_NONBLOCK) ||
  1767. (tty->flags & (1 << TTY_IO_ERROR))) {
  1768. info->flags |= ASYNC_NORMAL_ACTIVE;
  1769. return 0;
  1770. }
  1771. if (tty->termios->c_cflag & CLOCAL)
  1772. do_clocal = 1;
  1773. /*
  1774. * Block waiting for the carrier detect and the line to become
  1775. * free (i.e., not in use by the callout). While we are in
  1776. * this loop, info->count is dropped by one, so that
  1777. * rs_close() knows when to free things. We restore it upon
  1778. * exit, either normal or abnormal.
  1779. */
  1780. retval = 0;
  1781. add_wait_queue(&info->open_wait, &wait);
  1782. #ifdef SERIAL_DEBUG_OPEN
  1783. printk("block_til_ready before block: ttys%d, count = %d\n",
  1784. info->line, info->count);
  1785. #endif
  1786. spin_lock_irqsave(&info->lock, flags);
  1787. if (!tty_hung_up_p(filp))
  1788. info->count--;
  1789. info->blocked_open++;
  1790. while (1) {
  1791. if ((tty->termios->c_cflag & CBAUD)) {
  1792. unsigned int scratch;
  1793. serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
  1794. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1795. scratch = serial_in(info, UART_ESI_STAT1);
  1796. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1797. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1798. serial_out(info, UART_ESI_CMD2,
  1799. scratch | UART_MCR_DTR | UART_MCR_RTS);
  1800. }
  1801. set_current_state(TASK_INTERRUPTIBLE);
  1802. if (tty_hung_up_p(filp) ||
  1803. !(info->flags & ASYNC_INITIALIZED)) {
  1804. #ifdef SERIAL_DO_RESTART
  1805. if (info->flags & ASYNC_HUP_NOTIFY)
  1806. retval = -EAGAIN;
  1807. else
  1808. retval = -ERESTARTSYS;
  1809. #else
  1810. retval = -EAGAIN;
  1811. #endif
  1812. break;
  1813. }
  1814. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  1815. if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
  1816. do_clocal = 1;
  1817. if (!(info->flags & ASYNC_CLOSING) &&
  1818. (do_clocal))
  1819. break;
  1820. if (signal_pending(current)) {
  1821. retval = -ERESTARTSYS;
  1822. break;
  1823. }
  1824. #ifdef SERIAL_DEBUG_OPEN
  1825. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1826. info->line, info->count);
  1827. #endif
  1828. spin_unlock_irqrestore(&info->lock, flags);
  1829. schedule();
  1830. spin_lock_irqsave(&info->lock, flags);
  1831. }
  1832. set_current_state(TASK_RUNNING);
  1833. remove_wait_queue(&info->open_wait, &wait);
  1834. if (!tty_hung_up_p(filp))
  1835. info->count++;
  1836. info->blocked_open--;
  1837. spin_unlock_irqrestore(&info->lock, flags);
  1838. #ifdef SERIAL_DEBUG_OPEN
  1839. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1840. info->line, info->count);
  1841. #endif
  1842. if (retval)
  1843. return retval;
  1844. info->flags |= ASYNC_NORMAL_ACTIVE;
  1845. return 0;
  1846. }
  1847. /*
  1848. * This routine is called whenever a serial port is opened. It
  1849. * enables interrupts for a serial port, linking in its async structure into
  1850. * the IRQ chain. It also performs the serial-specific
  1851. * initialization for the tty structure.
  1852. */
  1853. static int esp_open(struct tty_struct *tty, struct file * filp)
  1854. {
  1855. struct esp_struct *info;
  1856. int retval, line;
  1857. unsigned long flags;
  1858. line = tty->index;
  1859. if ((line < 0) || (line >= NR_PORTS))
  1860. return -ENODEV;
  1861. /* find the port in the chain */
  1862. info = ports;
  1863. while (info && (info->line != line))
  1864. info = info->next_port;
  1865. if (!info) {
  1866. serial_paranoia_check(info, tty->name, "esp_open");
  1867. return -ENODEV;
  1868. }
  1869. #ifdef SERIAL_DEBUG_OPEN
  1870. printk("esp_open %s, count = %d\n", tty->name, info->count);
  1871. #endif
  1872. spin_lock_irqsave(&info->lock, flags);
  1873. info->count++;
  1874. tty->driver_data = info;
  1875. info->tty = tty;
  1876. spin_unlock_irqrestore(&info->lock, flags);
  1877. /*
  1878. * Start up serial port
  1879. */
  1880. retval = startup(info);
  1881. if (retval)
  1882. return retval;
  1883. retval = block_til_ready(tty, filp, info);
  1884. if (retval) {
  1885. #ifdef SERIAL_DEBUG_OPEN
  1886. printk("esp_open returning after block_til_ready with %d\n",
  1887. retval);
  1888. #endif
  1889. return retval;
  1890. }
  1891. #ifdef SERIAL_DEBUG_OPEN
  1892. printk("esp_open %s successful...", tty->name);
  1893. #endif
  1894. return 0;
  1895. }
  1896. /*
  1897. * ---------------------------------------------------------------------
  1898. * espserial_init() and friends
  1899. *
  1900. * espserial_init() is called at boot-time to initialize the serial driver.
  1901. * ---------------------------------------------------------------------
  1902. */
  1903. /*
  1904. * This routine prints out the appropriate serial driver version
  1905. * number, and identifies which options were configured into this
  1906. * driver.
  1907. */
  1908. static inline void show_serial_version(void)
  1909. {
  1910. printk(KERN_INFO "%s version %s (DMA %u)\n",
  1911. serial_name, serial_version, dma);
  1912. }
  1913. /*
  1914. * This routine is called by espserial_init() to initialize a specific serial
  1915. * port.
  1916. */
  1917. static inline int autoconfig(struct esp_struct * info)
  1918. {
  1919. int port_detected = 0;
  1920. unsigned long flags;
  1921. if (!request_region(info->port, REGION_SIZE, "esp serial"))
  1922. return -EIO;
  1923. spin_lock_irqsave(&info->lock, flags);
  1924. /*
  1925. * Check for ESP card
  1926. */
  1927. if (serial_in(info, UART_ESI_BASE) == 0xf3) {
  1928. serial_out(info, UART_ESI_CMD1, 0x00);
  1929. serial_out(info, UART_ESI_CMD1, 0x01);
  1930. if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
  1931. port_detected = 1;
  1932. if (!(info->irq)) {
  1933. serial_out(info, UART_ESI_CMD1, 0x02);
  1934. if (serial_in(info, UART_ESI_STAT1) & 0x01)
  1935. info->irq = 3;
  1936. else
  1937. info->irq = 4;
  1938. }
  1939. /* put card in enhanced mode */
  1940. /* this prevents access through */
  1941. /* the "old" IO ports */
  1942. esp_basic_init(info);
  1943. /* clear out MCR */
  1944. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1945. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1946. serial_out(info, UART_ESI_CMD2, 0x00);
  1947. }
  1948. }
  1949. if (!port_detected)
  1950. release_region(info->port, REGION_SIZE);
  1951. spin_unlock_irqrestore(&info->lock, flags);
  1952. return (port_detected);
  1953. }
  1954. static const struct tty_operations esp_ops = {
  1955. .open = esp_open,
  1956. .close = rs_close,
  1957. .write = rs_write,
  1958. .put_char = rs_put_char,
  1959. .flush_chars = rs_flush_chars,
  1960. .write_room = rs_write_room,
  1961. .chars_in_buffer = rs_chars_in_buffer,
  1962. .flush_buffer = rs_flush_buffer,
  1963. .ioctl = rs_ioctl,
  1964. .throttle = rs_throttle,
  1965. .unthrottle = rs_unthrottle,
  1966. .set_termios = rs_set_termios,
  1967. .stop = rs_stop,
  1968. .start = rs_start,
  1969. .hangup = esp_hangup,
  1970. .break_ctl = esp_break,
  1971. .wait_until_sent = rs_wait_until_sent,
  1972. .tiocmget = esp_tiocmget,
  1973. .tiocmset = esp_tiocmset,
  1974. };
  1975. /*
  1976. * The serial driver boot-time initialization code!
  1977. */
  1978. static int __init espserial_init(void)
  1979. {
  1980. int i, offset;
  1981. struct esp_struct * info;
  1982. struct esp_struct *last_primary = NULL;
  1983. int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
  1984. esp_driver = alloc_tty_driver(NR_PORTS);
  1985. if (!esp_driver)
  1986. return -ENOMEM;
  1987. for (i = 0; i < NR_PRIMARY; i++) {
  1988. if (irq[i] != 0) {
  1989. if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
  1990. (irq[i] == 8) || (irq[i] == 13))
  1991. irq[i] = 0;
  1992. else if (irq[i] == 2)
  1993. irq[i] = 9;
  1994. }
  1995. }
  1996. if ((dma != 1) && (dma != 3))
  1997. dma = 0;
  1998. if ((rx_trigger < 1) || (rx_trigger > 1023))
  1999. rx_trigger = 768;
  2000. if ((tx_trigger < 1) || (tx_trigger > 1023))
  2001. tx_trigger = 768;
  2002. if ((flow_off < 1) || (flow_off > 1023))
  2003. flow_off = 1016;
  2004. if ((flow_on < 1) || (flow_on > 1023))
  2005. flow_on = 944;
  2006. if ((rx_timeout < 0) || (rx_timeout > 255))
  2007. rx_timeout = 128;
  2008. if (flow_on >= flow_off)
  2009. flow_on = flow_off - 1;
  2010. show_serial_version();
  2011. /* Initialize the tty_driver structure */
  2012. esp_driver->owner = THIS_MODULE;
  2013. esp_driver->name = "ttyP";
  2014. esp_driver->major = ESP_IN_MAJOR;
  2015. esp_driver->minor_start = 0;
  2016. esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2017. esp_driver->subtype = SERIAL_TYPE_NORMAL;
  2018. esp_driver->init_termios = tty_std_termios;
  2019. esp_driver->init_termios.c_cflag =
  2020. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2021. esp_driver->flags = TTY_DRIVER_REAL_RAW;
  2022. tty_set_operations(esp_driver, &esp_ops);
  2023. if (tty_register_driver(esp_driver))
  2024. {
  2025. printk(KERN_ERR "Couldn't register esp serial driver");
  2026. put_tty_driver(esp_driver);
  2027. return 1;
  2028. }
  2029. info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
  2030. if (!info)
  2031. {
  2032. printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
  2033. tty_unregister_driver(esp_driver);
  2034. put_tty_driver(esp_driver);
  2035. return 1;
  2036. }
  2037. spin_lock_init(&info->lock);
  2038. /* rx_trigger, tx_trigger are needed by autoconfig */
  2039. info->config.rx_trigger = rx_trigger;
  2040. info->config.tx_trigger = tx_trigger;
  2041. i = 0;
  2042. offset = 0;
  2043. do {
  2044. info->port = esp[i] + offset;
  2045. info->irq = irq[i];
  2046. info->line = (i * 8) + (offset / 8);
  2047. if (!autoconfig(info)) {
  2048. i++;
  2049. offset = 0;
  2050. continue;
  2051. }
  2052. info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
  2053. info->flags = STD_COM_FLAGS;
  2054. if (info->custom_divisor)
  2055. info->flags |= ASYNC_SPD_CUST;
  2056. info->magic = ESP_MAGIC;
  2057. info->close_delay = 5*HZ/10;
  2058. info->closing_wait = 30*HZ;
  2059. info->config.rx_timeout = rx_timeout;
  2060. info->config.flow_on = flow_on;
  2061. info->config.flow_off = flow_off;
  2062. info->config.pio_threshold = pio_threshold;
  2063. info->next_port = ports;
  2064. init_waitqueue_head(&info->open_wait);
  2065. init_waitqueue_head(&info->close_wait);
  2066. init_waitqueue_head(&info->delta_msr_wait);
  2067. init_waitqueue_head(&info->break_wait);
  2068. ports = info;
  2069. printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
  2070. info->line, info->port, info->irq);
  2071. if (info->line % 8) {
  2072. printk("secondary port\n");
  2073. /* 8 port cards can't do DMA */
  2074. info->stat_flags |= ESP_STAT_NEVER_DMA;
  2075. if (last_primary)
  2076. last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
  2077. } else {
  2078. printk("primary port\n");
  2079. last_primary = info;
  2080. irq[i] = info->irq;
  2081. }
  2082. if (!dma)
  2083. info->stat_flags |= ESP_STAT_NEVER_DMA;
  2084. info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
  2085. if (!info)
  2086. {
  2087. printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
  2088. /* allow use of the already detected ports */
  2089. return 0;
  2090. }
  2091. spin_lock_init(&info->lock);
  2092. /* rx_trigger, tx_trigger are needed by autoconfig */
  2093. info->config.rx_trigger = rx_trigger;
  2094. info->config.tx_trigger = tx_trigger;
  2095. if (offset == 56) {
  2096. i++;
  2097. offset = 0;
  2098. } else {
  2099. offset += 8;
  2100. }
  2101. } while (i < NR_PRIMARY);
  2102. /* free the last port memory allocation */
  2103. kfree(info);
  2104. return 0;
  2105. }
  2106. static void __exit espserial_exit(void)
  2107. {
  2108. int e1;
  2109. struct esp_struct *temp_async;
  2110. struct esp_pio_buffer *pio_buf;
  2111. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  2112. if ((e1 = tty_unregister_driver(esp_driver)))
  2113. printk("SERIAL: failed to unregister serial driver (%d)\n",
  2114. e1);
  2115. put_tty_driver(esp_driver);
  2116. while (ports) {
  2117. if (ports->port) {
  2118. release_region(ports->port, REGION_SIZE);
  2119. }
  2120. temp_async = ports->next_port;
  2121. kfree(ports);
  2122. ports = temp_async;
  2123. }
  2124. if (dma_buffer)
  2125. free_pages((unsigned long)dma_buffer,
  2126. get_order(DMA_BUFFER_SZ));
  2127. while (free_pio_buf) {
  2128. pio_buf = free_pio_buf->next;
  2129. kfree(free_pio_buf);
  2130. free_pio_buf = pio_buf;
  2131. }
  2132. }
  2133. module_init(espserial_init);
  2134. module_exit(espserial_exit);