esp.c 69 KB

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