esp.c 68 KB

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