esp.c 68 KB

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