esp.c 69 KB

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