esp.c 68 KB

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