esp.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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(void *private_)
  605. {
  606. struct esp_struct *info = (struct esp_struct *) private_;
  607. struct tty_struct *tty;
  608. tty = info->tty;
  609. if (!tty)
  610. return;
  611. if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
  612. tty_wakeup(tty);
  613. }
  614. }
  615. /*
  616. * This routine is called from the scheduler tqueue when the interrupt
  617. * routine has signalled that a hangup has occurred. The path of
  618. * hangup processing is:
  619. *
  620. * serial interrupt routine -> (scheduler tqueue) ->
  621. * do_serial_hangup() -> tty->hangup() -> esp_hangup()
  622. *
  623. */
  624. static void do_serial_hangup(void *private_)
  625. {
  626. struct esp_struct *info = (struct esp_struct *) private_;
  627. struct tty_struct *tty;
  628. tty = info->tty;
  629. if (tty)
  630. tty_hangup(tty);
  631. }
  632. /*
  633. * ---------------------------------------------------------------
  634. * Low level utility subroutines for the serial driver: routines to
  635. * figure out the appropriate timeout for an interrupt chain, routines
  636. * to initialize and startup a serial port, and routines to shutdown a
  637. * serial port. Useful stuff like that.
  638. *
  639. * Caller should hold lock
  640. * ---------------------------------------------------------------
  641. */
  642. static inline void esp_basic_init(struct esp_struct * info)
  643. {
  644. /* put ESPC in enhanced mode */
  645. serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
  646. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  647. serial_out(info, UART_ESI_CMD2, 0x01);
  648. else
  649. serial_out(info, UART_ESI_CMD2, 0x31);
  650. /* disable interrupts for now */
  651. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  652. serial_out(info, UART_ESI_CMD2, 0x00);
  653. /* set interrupt and DMA channel */
  654. serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
  655. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  656. serial_out(info, UART_ESI_CMD2, 0x01);
  657. else
  658. serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
  659. serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
  660. if (info->line % 8) /* secondary port */
  661. serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
  662. else if (info->irq == 9)
  663. serial_out(info, UART_ESI_CMD2, 0x02);
  664. else
  665. serial_out(info, UART_ESI_CMD2, info->irq);
  666. /* set error status mask (check this) */
  667. serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
  668. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  669. serial_out(info, UART_ESI_CMD2, 0xa1);
  670. else
  671. serial_out(info, UART_ESI_CMD2, 0xbd);
  672. serial_out(info, UART_ESI_CMD2, 0x00);
  673. /* set DMA timeout */
  674. serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
  675. serial_out(info, UART_ESI_CMD2, 0xff);
  676. /* set FIFO trigger levels */
  677. serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
  678. serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
  679. serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
  680. serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
  681. serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
  682. /* Set clock scaling and wait states */
  683. serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
  684. serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
  685. /* set reinterrupt pacing */
  686. serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
  687. serial_out(info, UART_ESI_CMD2, 0xff);
  688. }
  689. static int startup(struct esp_struct * info)
  690. {
  691. unsigned long flags;
  692. int retval=0;
  693. unsigned int num_chars;
  694. spin_lock_irqsave(&info->lock, flags);
  695. if (info->flags & ASYNC_INITIALIZED)
  696. goto out;
  697. if (!info->xmit_buf) {
  698. info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
  699. retval = -ENOMEM;
  700. if (!info->xmit_buf)
  701. goto out;
  702. }
  703. #ifdef SERIAL_DEBUG_OPEN
  704. printk("starting up ttys%d (irq %d)...", info->line, info->irq);
  705. #endif
  706. /* Flush the RX buffer. Using the ESI flush command may cause */
  707. /* wild interrupts, so read all the data instead. */
  708. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  709. serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
  710. num_chars = serial_in(info, UART_ESI_STAT1) << 8;
  711. num_chars |= serial_in(info, UART_ESI_STAT2);
  712. while (num_chars > 1) {
  713. inw(info->port + UART_ESI_RX);
  714. num_chars -= 2;
  715. }
  716. if (num_chars)
  717. serial_in(info, UART_ESI_RX);
  718. /* set receive character timeout */
  719. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  720. serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
  721. /* clear all flags except the "never DMA" flag */
  722. info->stat_flags &= ESP_STAT_NEVER_DMA;
  723. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  724. info->stat_flags |= ESP_STAT_USE_PIO;
  725. spin_unlock_irqrestore(&info->lock, flags);
  726. /*
  727. * Allocate the IRQ
  728. */
  729. retval = request_irq(info->irq, rs_interrupt_single, IRQF_SHARED,
  730. "esp serial", info);
  731. if (retval) {
  732. if (capable(CAP_SYS_ADMIN)) {
  733. if (info->tty)
  734. set_bit(TTY_IO_ERROR,
  735. &info->tty->flags);
  736. retval = 0;
  737. }
  738. goto out_unlocked;
  739. }
  740. if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
  741. dma_buffer = (char *)__get_dma_pages(
  742. GFP_KERNEL, get_order(DMA_BUFFER_SZ));
  743. /* use PIO mode if DMA buf/chan cannot be allocated */
  744. if (!dma_buffer)
  745. info->stat_flags |= ESP_STAT_USE_PIO;
  746. else if (request_dma(dma, "esp serial")) {
  747. free_pages((unsigned long)dma_buffer,
  748. get_order(DMA_BUFFER_SZ));
  749. dma_buffer = NULL;
  750. info->stat_flags |= ESP_STAT_USE_PIO;
  751. }
  752. }
  753. info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
  754. spin_lock_irqsave(&info->lock, flags);
  755. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  756. serial_out(info, UART_ESI_CMD2, UART_MCR);
  757. serial_out(info, UART_ESI_CMD2, info->MCR);
  758. /*
  759. * Finally, enable interrupts
  760. */
  761. /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
  762. info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
  763. UART_IER_DMA_TC;
  764. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  765. serial_out(info, UART_ESI_CMD2, info->IER);
  766. if (info->tty)
  767. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  768. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  769. spin_unlock_irqrestore(&info->lock, flags);
  770. /*
  771. * Set up the tty->alt_speed kludge
  772. */
  773. if (info->tty) {
  774. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  775. info->tty->alt_speed = 57600;
  776. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  777. info->tty->alt_speed = 115200;
  778. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  779. info->tty->alt_speed = 230400;
  780. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  781. info->tty->alt_speed = 460800;
  782. }
  783. /*
  784. * set the speed of the serial port
  785. */
  786. change_speed(info);
  787. info->flags |= ASYNC_INITIALIZED;
  788. return 0;
  789. out:
  790. spin_unlock_irqrestore(&info->lock, flags);
  791. out_unlocked:
  792. return retval;
  793. }
  794. /*
  795. * This routine will shutdown a serial port; interrupts are disabled, and
  796. * DTR is dropped if the hangup on close termio flag is on.
  797. */
  798. static void shutdown(struct esp_struct * info)
  799. {
  800. unsigned long flags, f;
  801. if (!(info->flags & ASYNC_INITIALIZED))
  802. return;
  803. #ifdef SERIAL_DEBUG_OPEN
  804. printk("Shutting down serial port %d (irq %d)....", info->line,
  805. info->irq);
  806. #endif
  807. spin_lock_irqsave(&info->lock, flags);
  808. /*
  809. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  810. * here so the queue might never be waken up
  811. */
  812. wake_up_interruptible(&info->delta_msr_wait);
  813. wake_up_interruptible(&info->break_wait);
  814. /* stop a DMA transfer on the port being closed */
  815. /* DMA lock is higher priority always */
  816. if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
  817. f=claim_dma_lock();
  818. disable_dma(dma);
  819. clear_dma_ff(dma);
  820. release_dma_lock(f);
  821. dma_bytes = 0;
  822. }
  823. /*
  824. * Free the IRQ
  825. */
  826. free_irq(info->irq, info);
  827. if (dma_buffer) {
  828. struct esp_struct *current_port = ports;
  829. while (current_port) {
  830. if ((current_port != info) &&
  831. (current_port->flags & ASYNC_INITIALIZED))
  832. break;
  833. current_port = current_port->next_port;
  834. }
  835. if (!current_port) {
  836. free_dma(dma);
  837. free_pages((unsigned long)dma_buffer,
  838. get_order(DMA_BUFFER_SZ));
  839. dma_buffer = NULL;
  840. }
  841. }
  842. if (info->xmit_buf) {
  843. free_page((unsigned long) info->xmit_buf);
  844. info->xmit_buf = NULL;
  845. }
  846. info->IER = 0;
  847. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  848. serial_out(info, UART_ESI_CMD2, 0x00);
  849. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  850. info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
  851. info->MCR &= ~UART_MCR_OUT2;
  852. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  853. serial_out(info, UART_ESI_CMD2, UART_MCR);
  854. serial_out(info, UART_ESI_CMD2, info->MCR);
  855. if (info->tty)
  856. set_bit(TTY_IO_ERROR, &info->tty->flags);
  857. info->flags &= ~ASYNC_INITIALIZED;
  858. spin_unlock_irqrestore(&info->lock, flags);
  859. }
  860. /*
  861. * This routine is called to set the UART divisor registers to match
  862. * the specified baud rate for a serial port.
  863. */
  864. static void change_speed(struct esp_struct *info)
  865. {
  866. unsigned short port;
  867. int quot = 0;
  868. unsigned cflag,cval;
  869. int baud, bits;
  870. unsigned char flow1 = 0, flow2 = 0;
  871. unsigned long flags;
  872. if (!info->tty || !info->tty->termios)
  873. return;
  874. cflag = info->tty->termios->c_cflag;
  875. port = info->port;
  876. /* byte size and parity */
  877. switch (cflag & CSIZE) {
  878. case CS5: cval = 0x00; bits = 7; break;
  879. case CS6: cval = 0x01; bits = 8; break;
  880. case CS7: cval = 0x02; bits = 9; break;
  881. case CS8: cval = 0x03; bits = 10; break;
  882. default: cval = 0x00; bits = 7; break;
  883. }
  884. if (cflag & CSTOPB) {
  885. cval |= 0x04;
  886. bits++;
  887. }
  888. if (cflag & PARENB) {
  889. cval |= UART_LCR_PARITY;
  890. bits++;
  891. }
  892. if (!(cflag & PARODD))
  893. cval |= UART_LCR_EPAR;
  894. #ifdef CMSPAR
  895. if (cflag & CMSPAR)
  896. cval |= UART_LCR_SPAR;
  897. #endif
  898. baud = tty_get_baud_rate(info->tty);
  899. if (baud == 38400 &&
  900. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  901. quot = info->custom_divisor;
  902. else {
  903. if (baud == 134)
  904. /* Special case since 134 is really 134.5 */
  905. quot = (2*BASE_BAUD / 269);
  906. else if (baud)
  907. quot = BASE_BAUD / baud;
  908. }
  909. /* If the quotient is ever zero, default to 9600 bps */
  910. if (!quot)
  911. quot = BASE_BAUD / 9600;
  912. info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
  913. /* CTS flow control flag and modem status interrupts */
  914. /* info->IER &= ~UART_IER_MSI; */
  915. if (cflag & CRTSCTS) {
  916. info->flags |= ASYNC_CTS_FLOW;
  917. /* info->IER |= UART_IER_MSI; */
  918. flow1 = 0x04;
  919. flow2 = 0x10;
  920. } else
  921. info->flags &= ~ASYNC_CTS_FLOW;
  922. if (cflag & CLOCAL)
  923. info->flags &= ~ASYNC_CHECK_CD;
  924. else {
  925. info->flags |= ASYNC_CHECK_CD;
  926. /* info->IER |= UART_IER_MSI; */
  927. }
  928. /*
  929. * Set up parity check flag
  930. */
  931. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  932. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  933. if (I_INPCK(info->tty))
  934. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  935. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  936. info->read_status_mask |= UART_LSR_BI;
  937. info->ignore_status_mask = 0;
  938. #if 0
  939. /* This should be safe, but for some broken bits of hardware... */
  940. if (I_IGNPAR(info->tty)) {
  941. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  942. info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
  943. }
  944. #endif
  945. if (I_IGNBRK(info->tty)) {
  946. info->ignore_status_mask |= UART_LSR_BI;
  947. info->read_status_mask |= UART_LSR_BI;
  948. /*
  949. * If we're ignore parity and break indicators, ignore
  950. * overruns too. (For real raw support).
  951. */
  952. if (I_IGNPAR(info->tty)) {
  953. info->ignore_status_mask |= UART_LSR_OE | \
  954. UART_LSR_PE | UART_LSR_FE;
  955. info->read_status_mask |= UART_LSR_OE | \
  956. UART_LSR_PE | UART_LSR_FE;
  957. }
  958. }
  959. if (I_IXOFF(info->tty))
  960. flow1 |= 0x81;
  961. spin_lock_irqsave(&info->lock, flags);
  962. /* set baud */
  963. serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
  964. serial_out(info, UART_ESI_CMD2, quot >> 8);
  965. serial_out(info, UART_ESI_CMD2, quot & 0xff);
  966. /* set data bits, parity, etc. */
  967. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  968. serial_out(info, UART_ESI_CMD2, UART_LCR);
  969. serial_out(info, UART_ESI_CMD2, cval);
  970. /* Enable flow control */
  971. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
  972. serial_out(info, UART_ESI_CMD2, flow1);
  973. serial_out(info, UART_ESI_CMD2, flow2);
  974. /* set flow control characters (XON/XOFF only) */
  975. if (I_IXOFF(info->tty)) {
  976. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
  977. serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
  978. serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
  979. serial_out(info, UART_ESI_CMD2, 0x10);
  980. serial_out(info, UART_ESI_CMD2, 0x21);
  981. switch (cflag & CSIZE) {
  982. case CS5:
  983. serial_out(info, UART_ESI_CMD2, 0x1f);
  984. break;
  985. case CS6:
  986. serial_out(info, UART_ESI_CMD2, 0x3f);
  987. break;
  988. case CS7:
  989. case CS8:
  990. serial_out(info, UART_ESI_CMD2, 0x7f);
  991. break;
  992. default:
  993. serial_out(info, UART_ESI_CMD2, 0xff);
  994. break;
  995. }
  996. }
  997. /* Set high/low water */
  998. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
  999. serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
  1000. serial_out(info, UART_ESI_CMD2, info->config.flow_off);
  1001. serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
  1002. serial_out(info, UART_ESI_CMD2, info->config.flow_on);
  1003. spin_unlock_irqrestore(&info->lock, flags);
  1004. }
  1005. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  1006. {
  1007. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1008. unsigned long flags;
  1009. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  1010. return;
  1011. if (!info->xmit_buf)
  1012. return;
  1013. spin_lock_irqsave(&info->lock, flags);
  1014. if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
  1015. info->xmit_buf[info->xmit_head++] = ch;
  1016. info->xmit_head &= ESP_XMIT_SIZE-1;
  1017. info->xmit_cnt++;
  1018. }
  1019. spin_unlock_irqrestore(&info->lock, flags);
  1020. }
  1021. static void rs_flush_chars(struct tty_struct *tty)
  1022. {
  1023. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1024. unsigned long flags;
  1025. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  1026. return;
  1027. spin_lock_irqsave(&info->lock, flags);
  1028. if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
  1029. goto out;
  1030. if (!(info->IER & UART_IER_THRI)) {
  1031. info->IER |= UART_IER_THRI;
  1032. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1033. serial_out(info, UART_ESI_CMD2, info->IER);
  1034. }
  1035. out:
  1036. spin_unlock_irqrestore(&info->lock, flags);
  1037. }
  1038. static int rs_write(struct tty_struct * tty,
  1039. const unsigned char *buf, int count)
  1040. {
  1041. int c, t, ret = 0;
  1042. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1043. unsigned long flags;
  1044. if (serial_paranoia_check(info, tty->name, "rs_write"))
  1045. return 0;
  1046. if (!info->xmit_buf)
  1047. return 0;
  1048. while (1) {
  1049. /* Thanks to R. Wolff for suggesting how to do this with */
  1050. /* interrupts enabled */
  1051. c = count;
  1052. t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
  1053. if (t < c)
  1054. c = t;
  1055. t = ESP_XMIT_SIZE - info->xmit_head;
  1056. if (t < c)
  1057. c = t;
  1058. if (c <= 0)
  1059. break;
  1060. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  1061. info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
  1062. info->xmit_cnt += c;
  1063. buf += c;
  1064. count -= c;
  1065. ret += c;
  1066. }
  1067. spin_lock_irqsave(&info->lock, flags);
  1068. if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
  1069. info->IER |= UART_IER_THRI;
  1070. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1071. serial_out(info, UART_ESI_CMD2, info->IER);
  1072. }
  1073. spin_unlock_irqrestore(&info->lock, flags);
  1074. return ret;
  1075. }
  1076. static int rs_write_room(struct tty_struct *tty)
  1077. {
  1078. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1079. int ret;
  1080. unsigned long flags;
  1081. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  1082. return 0;
  1083. spin_lock_irqsave(&info->lock, flags);
  1084. ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
  1085. if (ret < 0)
  1086. ret = 0;
  1087. spin_unlock_irqrestore(&info->lock, flags);
  1088. return ret;
  1089. }
  1090. static int rs_chars_in_buffer(struct tty_struct *tty)
  1091. {
  1092. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1093. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  1094. return 0;
  1095. return info->xmit_cnt;
  1096. }
  1097. static void rs_flush_buffer(struct tty_struct *tty)
  1098. {
  1099. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1100. unsigned long flags;
  1101. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  1102. return;
  1103. spin_lock_irqsave(&info->lock, flags);
  1104. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1105. spin_unlock_irqrestore(&info->lock, flags);
  1106. tty_wakeup(tty);
  1107. }
  1108. /*
  1109. * ------------------------------------------------------------
  1110. * rs_throttle()
  1111. *
  1112. * This routine is called by the upper-layer tty layer to signal that
  1113. * incoming characters should be throttled.
  1114. * ------------------------------------------------------------
  1115. */
  1116. static void rs_throttle(struct tty_struct * tty)
  1117. {
  1118. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1119. unsigned long flags;
  1120. #ifdef SERIAL_DEBUG_THROTTLE
  1121. char buf[64];
  1122. printk("throttle %s: %d....\n", tty_name(tty, buf),
  1123. tty->ldisc.chars_in_buffer(tty));
  1124. #endif
  1125. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  1126. return;
  1127. spin_lock_irqsave(&info->lock, flags);
  1128. info->IER &= ~UART_IER_RDI;
  1129. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1130. serial_out(info, UART_ESI_CMD2, info->IER);
  1131. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  1132. serial_out(info, UART_ESI_CMD2, 0x00);
  1133. spin_unlock_irqrestore(&info->lock, flags);
  1134. }
  1135. static void rs_unthrottle(struct tty_struct * tty)
  1136. {
  1137. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1138. unsigned long flags;
  1139. #ifdef SERIAL_DEBUG_THROTTLE
  1140. char buf[64];
  1141. printk("unthrottle %s: %d....\n", tty_name(tty, buf),
  1142. tty->ldisc.chars_in_buffer(tty));
  1143. #endif
  1144. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  1145. return;
  1146. spin_lock_irqsave(&info->lock, flags);
  1147. info->IER |= UART_IER_RDI;
  1148. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1149. serial_out(info, UART_ESI_CMD2, info->IER);
  1150. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  1151. serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
  1152. spin_unlock_irqrestore(&info->lock, flags);
  1153. }
  1154. /*
  1155. * ------------------------------------------------------------
  1156. * rs_ioctl() and friends
  1157. * ------------------------------------------------------------
  1158. */
  1159. static int get_serial_info(struct esp_struct * info,
  1160. struct serial_struct __user *retinfo)
  1161. {
  1162. struct serial_struct tmp;
  1163. memset(&tmp, 0, sizeof(tmp));
  1164. tmp.type = PORT_16550A;
  1165. tmp.line = info->line;
  1166. tmp.port = info->port;
  1167. tmp.irq = info->irq;
  1168. tmp.flags = info->flags;
  1169. tmp.xmit_fifo_size = 1024;
  1170. tmp.baud_base = BASE_BAUD;
  1171. tmp.close_delay = info->close_delay;
  1172. tmp.closing_wait = info->closing_wait;
  1173. tmp.custom_divisor = info->custom_divisor;
  1174. tmp.hub6 = 0;
  1175. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  1176. return -EFAULT;
  1177. return 0;
  1178. }
  1179. static int get_esp_config(struct esp_struct * info,
  1180. struct hayes_esp_config __user *retinfo)
  1181. {
  1182. struct hayes_esp_config tmp;
  1183. if (!retinfo)
  1184. return -EFAULT;
  1185. memset(&tmp, 0, sizeof(tmp));
  1186. tmp.rx_timeout = info->config.rx_timeout;
  1187. tmp.rx_trigger = info->config.rx_trigger;
  1188. tmp.tx_trigger = info->config.tx_trigger;
  1189. tmp.flow_off = info->config.flow_off;
  1190. tmp.flow_on = info->config.flow_on;
  1191. tmp.pio_threshold = info->config.pio_threshold;
  1192. tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
  1193. return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
  1194. }
  1195. static int set_serial_info(struct esp_struct * info,
  1196. struct serial_struct __user *new_info)
  1197. {
  1198. struct serial_struct new_serial;
  1199. struct esp_struct old_info;
  1200. unsigned int change_irq;
  1201. int retval = 0;
  1202. struct esp_struct *current_async;
  1203. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  1204. return -EFAULT;
  1205. old_info = *info;
  1206. if ((new_serial.type != PORT_16550A) ||
  1207. (new_serial.hub6) ||
  1208. (info->port != new_serial.port) ||
  1209. (new_serial.baud_base != BASE_BAUD) ||
  1210. (new_serial.irq > 15) ||
  1211. (new_serial.irq < 2) ||
  1212. (new_serial.irq == 6) ||
  1213. (new_serial.irq == 8) ||
  1214. (new_serial.irq == 13))
  1215. return -EINVAL;
  1216. change_irq = new_serial.irq != info->irq;
  1217. if (change_irq && (info->line % 8))
  1218. return -EINVAL;
  1219. if (!capable(CAP_SYS_ADMIN)) {
  1220. if (change_irq ||
  1221. (new_serial.close_delay != info->close_delay) ||
  1222. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  1223. (info->flags & ~ASYNC_USR_MASK)))
  1224. return -EPERM;
  1225. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  1226. (new_serial.flags & ASYNC_USR_MASK));
  1227. info->custom_divisor = new_serial.custom_divisor;
  1228. } else {
  1229. if (new_serial.irq == 2)
  1230. new_serial.irq = 9;
  1231. if (change_irq) {
  1232. current_async = ports;
  1233. while (current_async) {
  1234. if ((current_async->line >= info->line) &&
  1235. (current_async->line < (info->line + 8))) {
  1236. if (current_async == info) {
  1237. if (current_async->count > 1)
  1238. return -EBUSY;
  1239. } else if (current_async->count)
  1240. return -EBUSY;
  1241. }
  1242. current_async = current_async->next_port;
  1243. }
  1244. }
  1245. /*
  1246. * OK, past this point, all the error checking has been done.
  1247. * At this point, we start making changes.....
  1248. */
  1249. info->flags = ((info->flags & ~ASYNC_FLAGS) |
  1250. (new_serial.flags & ASYNC_FLAGS));
  1251. info->custom_divisor = new_serial.custom_divisor;
  1252. info->close_delay = new_serial.close_delay * HZ/100;
  1253. info->closing_wait = new_serial.closing_wait * HZ/100;
  1254. if (change_irq) {
  1255. /*
  1256. * We need to shutdown the serial port at the old
  1257. * port/irq combination.
  1258. */
  1259. shutdown(info);
  1260. current_async = ports;
  1261. while (current_async) {
  1262. if ((current_async->line >= info->line) &&
  1263. (current_async->line < (info->line + 8)))
  1264. current_async->irq = new_serial.irq;
  1265. current_async = current_async->next_port;
  1266. }
  1267. serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
  1268. if (info->irq == 9)
  1269. serial_out(info, UART_ESI_CMD2, 0x02);
  1270. else
  1271. serial_out(info, UART_ESI_CMD2, info->irq);
  1272. }
  1273. }
  1274. if (info->flags & ASYNC_INITIALIZED) {
  1275. if (((old_info.flags & ASYNC_SPD_MASK) !=
  1276. (info->flags & ASYNC_SPD_MASK)) ||
  1277. (old_info.custom_divisor != info->custom_divisor)) {
  1278. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  1279. info->tty->alt_speed = 57600;
  1280. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  1281. info->tty->alt_speed = 115200;
  1282. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  1283. info->tty->alt_speed = 230400;
  1284. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  1285. info->tty->alt_speed = 460800;
  1286. change_speed(info);
  1287. }
  1288. } else
  1289. retval = startup(info);
  1290. return retval;
  1291. }
  1292. static int set_esp_config(struct esp_struct * info,
  1293. struct hayes_esp_config __user * new_info)
  1294. {
  1295. struct hayes_esp_config new_config;
  1296. unsigned int change_dma;
  1297. int retval = 0;
  1298. struct esp_struct *current_async;
  1299. unsigned long flags;
  1300. /* Perhaps a non-sysadmin user should be able to do some of these */
  1301. /* operations. I haven't decided yet. */
  1302. if (!capable(CAP_SYS_ADMIN))
  1303. return -EPERM;
  1304. if (copy_from_user(&new_config, new_info, sizeof(new_config)))
  1305. return -EFAULT;
  1306. if ((new_config.flow_on >= new_config.flow_off) ||
  1307. (new_config.rx_trigger < 1) ||
  1308. (new_config.tx_trigger < 1) ||
  1309. (new_config.flow_off < 1) ||
  1310. (new_config.flow_on < 1) ||
  1311. (new_config.rx_trigger > 1023) ||
  1312. (new_config.tx_trigger > 1023) ||
  1313. (new_config.flow_off > 1023) ||
  1314. (new_config.flow_on > 1023) ||
  1315. (new_config.pio_threshold < 0) ||
  1316. (new_config.pio_threshold > 1024))
  1317. return -EINVAL;
  1318. if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
  1319. new_config.dma_channel = 0;
  1320. if (info->stat_flags & ESP_STAT_NEVER_DMA)
  1321. change_dma = new_config.dma_channel;
  1322. else
  1323. change_dma = (new_config.dma_channel != dma);
  1324. if (change_dma) {
  1325. if (new_config.dma_channel) {
  1326. /* PIO mode to DMA mode transition OR */
  1327. /* change current DMA channel */
  1328. current_async = ports;
  1329. while (current_async) {
  1330. if (current_async == info) {
  1331. if (current_async->count > 1)
  1332. return -EBUSY;
  1333. } else if (current_async->count)
  1334. return -EBUSY;
  1335. current_async =
  1336. current_async->next_port;
  1337. }
  1338. shutdown(info);
  1339. dma = new_config.dma_channel;
  1340. info->stat_flags &= ~ESP_STAT_NEVER_DMA;
  1341. /* all ports must use the same DMA channel */
  1342. spin_lock_irqsave(&info->lock, flags);
  1343. current_async = ports;
  1344. while (current_async) {
  1345. esp_basic_init(current_async);
  1346. current_async = current_async->next_port;
  1347. }
  1348. spin_unlock_irqrestore(&info->lock, flags);
  1349. } else {
  1350. /* DMA mode to PIO mode only */
  1351. if (info->count > 1)
  1352. return -EBUSY;
  1353. shutdown(info);
  1354. spin_lock_irqsave(&info->lock, flags);
  1355. info->stat_flags |= ESP_STAT_NEVER_DMA;
  1356. esp_basic_init(info);
  1357. spin_unlock_irqrestore(&info->lock, flags);
  1358. }
  1359. }
  1360. info->config.pio_threshold = new_config.pio_threshold;
  1361. if ((new_config.flow_off != info->config.flow_off) ||
  1362. (new_config.flow_on != info->config.flow_on)) {
  1363. unsigned long flags;
  1364. info->config.flow_off = new_config.flow_off;
  1365. info->config.flow_on = new_config.flow_on;
  1366. spin_lock_irqsave(&info->lock, flags);
  1367. serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
  1368. serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
  1369. serial_out(info, UART_ESI_CMD2, new_config.flow_off);
  1370. serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
  1371. serial_out(info, UART_ESI_CMD2, new_config.flow_on);
  1372. spin_unlock_irqrestore(&info->lock, flags);
  1373. }
  1374. if ((new_config.rx_trigger != info->config.rx_trigger) ||
  1375. (new_config.tx_trigger != info->config.tx_trigger)) {
  1376. unsigned long flags;
  1377. info->config.rx_trigger = new_config.rx_trigger;
  1378. info->config.tx_trigger = new_config.tx_trigger;
  1379. spin_lock_irqsave(&info->lock, flags);
  1380. serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
  1381. serial_out(info, UART_ESI_CMD2,
  1382. new_config.rx_trigger >> 8);
  1383. serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
  1384. serial_out(info, UART_ESI_CMD2,
  1385. new_config.tx_trigger >> 8);
  1386. serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
  1387. spin_unlock_irqrestore(&info->lock, flags);
  1388. }
  1389. if (new_config.rx_timeout != info->config.rx_timeout) {
  1390. unsigned long flags;
  1391. info->config.rx_timeout = new_config.rx_timeout;
  1392. spin_lock_irqsave(&info->lock, flags);
  1393. if (info->IER & UART_IER_RDI) {
  1394. serial_out(info, UART_ESI_CMD1,
  1395. ESI_SET_RX_TIMEOUT);
  1396. serial_out(info, UART_ESI_CMD2,
  1397. new_config.rx_timeout);
  1398. }
  1399. spin_unlock_irqrestore(&info->lock, flags);
  1400. }
  1401. if (!(info->flags & ASYNC_INITIALIZED))
  1402. retval = startup(info);
  1403. return retval;
  1404. }
  1405. /*
  1406. * get_lsr_info - get line status register info
  1407. *
  1408. * Purpose: Let user call ioctl() to get info when the UART physically
  1409. * is emptied. On bus types like RS485, the transmitter must
  1410. * release the bus after transmitting. This must be done when
  1411. * the transmit shift register is empty, not be done when the
  1412. * transmit holding register is empty. This functionality
  1413. * allows an RS485 driver to be written in user space.
  1414. */
  1415. static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
  1416. {
  1417. unsigned char status;
  1418. unsigned int result;
  1419. unsigned long flags;
  1420. spin_lock_irqsave(&info->lock, flags);
  1421. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  1422. status = serial_in(info, UART_ESI_STAT1);
  1423. spin_unlock_irqrestore(&info->lock, flags);
  1424. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  1425. return put_user(result,value);
  1426. }
  1427. static int esp_tiocmget(struct tty_struct *tty, struct file *file)
  1428. {
  1429. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1430. unsigned char control, status;
  1431. unsigned long flags;
  1432. if (serial_paranoia_check(info, tty->name, __FUNCTION__))
  1433. return -ENODEV;
  1434. if (tty->flags & (1 << TTY_IO_ERROR))
  1435. return -EIO;
  1436. control = info->MCR;
  1437. spin_lock_irqsave(&info->lock, flags);
  1438. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  1439. status = serial_in(info, UART_ESI_STAT2);
  1440. spin_unlock_irqrestore(&info->lock, flags);
  1441. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
  1442. | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
  1443. | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
  1444. | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
  1445. | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
  1446. | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1447. }
  1448. static int esp_tiocmset(struct tty_struct *tty, struct file *file,
  1449. unsigned int set, unsigned int clear)
  1450. {
  1451. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1452. unsigned long flags;
  1453. if (serial_paranoia_check(info, tty->name, __FUNCTION__))
  1454. return -ENODEV;
  1455. if (tty->flags & (1 << TTY_IO_ERROR))
  1456. return -EIO;
  1457. spin_lock_irqsave(&info->lock, flags);
  1458. if (set & TIOCM_RTS)
  1459. info->MCR |= UART_MCR_RTS;
  1460. if (set & TIOCM_DTR)
  1461. info->MCR |= UART_MCR_DTR;
  1462. if (clear & TIOCM_RTS)
  1463. info->MCR &= ~UART_MCR_RTS;
  1464. if (clear & TIOCM_DTR)
  1465. info->MCR &= ~UART_MCR_DTR;
  1466. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1467. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1468. serial_out(info, UART_ESI_CMD2, info->MCR);
  1469. spin_unlock_irqrestore(&info->lock, flags);
  1470. return 0;
  1471. }
  1472. /*
  1473. * rs_break() --- routine which turns the break handling on or off
  1474. */
  1475. static void esp_break(struct tty_struct *tty, int break_state)
  1476. {
  1477. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1478. unsigned long flags;
  1479. if (serial_paranoia_check(info, tty->name, "esp_break"))
  1480. return;
  1481. if (break_state == -1) {
  1482. spin_lock_irqsave(&info->lock, flags);
  1483. serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
  1484. serial_out(info, UART_ESI_CMD2, 0x01);
  1485. spin_unlock_irqrestore(&info->lock, flags);
  1486. /* FIXME - new style wait needed here */
  1487. interruptible_sleep_on(&info->break_wait);
  1488. } else {
  1489. spin_lock_irqsave(&info->lock, flags);
  1490. serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
  1491. serial_out(info, UART_ESI_CMD2, 0x00);
  1492. spin_unlock_irqrestore(&info->lock, flags);
  1493. }
  1494. }
  1495. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1496. unsigned int cmd, unsigned long arg)
  1497. {
  1498. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1499. struct async_icount cprev, cnow; /* kernel counter temps */
  1500. struct serial_icounter_struct __user *p_cuser; /* user space */
  1501. void __user *argp = (void __user *)arg;
  1502. unsigned long flags;
  1503. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1504. return -ENODEV;
  1505. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1506. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  1507. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
  1508. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
  1509. (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
  1510. if (tty->flags & (1 << TTY_IO_ERROR))
  1511. return -EIO;
  1512. }
  1513. switch (cmd) {
  1514. case TIOCGSERIAL:
  1515. return get_serial_info(info, argp);
  1516. case TIOCSSERIAL:
  1517. return set_serial_info(info, argp);
  1518. case TIOCSERCONFIG:
  1519. /* do not reconfigure after initial configuration */
  1520. return 0;
  1521. case TIOCSERGWILD:
  1522. return put_user(0L, (unsigned long __user *)argp);
  1523. case TIOCSERGETLSR: /* Get line status register */
  1524. return get_lsr_info(info, argp);
  1525. case TIOCSERSWILD:
  1526. if (!capable(CAP_SYS_ADMIN))
  1527. return -EPERM;
  1528. return 0;
  1529. /*
  1530. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1531. * - mask passed in arg for lines of interest
  1532. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1533. * Caller should use TIOCGICOUNT to see which one it was
  1534. */
  1535. case TIOCMIWAIT:
  1536. spin_lock_irqsave(&info->lock, flags);
  1537. cprev = info->icount; /* note the counters on entry */
  1538. spin_unlock_irqrestore(&info->lock, flags);
  1539. while (1) {
  1540. /* FIXME: convert to new style wakeup */
  1541. interruptible_sleep_on(&info->delta_msr_wait);
  1542. /* see if a signal did it */
  1543. if (signal_pending(current))
  1544. return -ERESTARTSYS;
  1545. spin_lock_irqsave(&info->lock, flags);
  1546. cnow = info->icount; /* atomic copy */
  1547. spin_unlock_irqrestore(&info->lock, flags);
  1548. if (cnow.rng == cprev.rng &&
  1549. cnow.dsr == cprev.dsr &&
  1550. cnow.dcd == cprev.dcd &&
  1551. cnow.cts == cprev.cts)
  1552. return -EIO; /* no change => error */
  1553. if (((arg & TIOCM_RNG) &&
  1554. (cnow.rng != cprev.rng)) ||
  1555. ((arg & TIOCM_DSR) &&
  1556. (cnow.dsr != cprev.dsr)) ||
  1557. ((arg & TIOCM_CD) &&
  1558. (cnow.dcd != cprev.dcd)) ||
  1559. ((arg & TIOCM_CTS) &&
  1560. (cnow.cts != cprev.cts)) ) {
  1561. return 0;
  1562. }
  1563. cprev = cnow;
  1564. }
  1565. /* NOTREACHED */
  1566. /*
  1567. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1568. * Return: write counters to the user passed counter struct
  1569. * NB: both 1->0 and 0->1 transitions are counted except for
  1570. * RI where only 0->1 is counted.
  1571. */
  1572. case TIOCGICOUNT:
  1573. spin_lock_irqsave(&info->lock, flags);
  1574. cnow = info->icount;
  1575. spin_unlock_irqrestore(&info->lock, flags);
  1576. p_cuser = argp;
  1577. if (put_user(cnow.cts, &p_cuser->cts) ||
  1578. put_user(cnow.dsr, &p_cuser->dsr) ||
  1579. put_user(cnow.rng, &p_cuser->rng) ||
  1580. put_user(cnow.dcd, &p_cuser->dcd))
  1581. return -EFAULT;
  1582. return 0;
  1583. case TIOCGHAYESESP:
  1584. return get_esp_config(info, argp);
  1585. case TIOCSHAYESESP:
  1586. return set_esp_config(info, argp);
  1587. default:
  1588. return -ENOIOCTLCMD;
  1589. }
  1590. return 0;
  1591. }
  1592. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1593. {
  1594. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1595. unsigned long flags;
  1596. if ( (tty->termios->c_cflag == old_termios->c_cflag)
  1597. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  1598. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1599. return;
  1600. change_speed(info);
  1601. spin_lock_irqsave(&info->lock, flags);
  1602. /* Handle transition to B0 status */
  1603. if ((old_termios->c_cflag & CBAUD) &&
  1604. !(tty->termios->c_cflag & CBAUD)) {
  1605. info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
  1606. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1607. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1608. serial_out(info, UART_ESI_CMD2, info->MCR);
  1609. }
  1610. /* Handle transition away from B0 status */
  1611. if (!(old_termios->c_cflag & CBAUD) &&
  1612. (tty->termios->c_cflag & CBAUD)) {
  1613. info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1614. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1615. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1616. serial_out(info, UART_ESI_CMD2, info->MCR);
  1617. }
  1618. spin_unlock_irqrestore(&info->lock, flags);
  1619. /* Handle turning of CRTSCTS */
  1620. if ((old_termios->c_cflag & CRTSCTS) &&
  1621. !(tty->termios->c_cflag & CRTSCTS)) {
  1622. rs_start(tty);
  1623. }
  1624. }
  1625. /*
  1626. * ------------------------------------------------------------
  1627. * rs_close()
  1628. *
  1629. * This routine is called when the serial port gets closed. First, we
  1630. * wait for the last remaining data to be sent. Then, we unlink its
  1631. * async structure from the interrupt chain if necessary, and we free
  1632. * that IRQ if nothing is left in the chain.
  1633. * ------------------------------------------------------------
  1634. */
  1635. static void rs_close(struct tty_struct *tty, struct file * filp)
  1636. {
  1637. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1638. unsigned long flags;
  1639. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1640. return;
  1641. spin_lock_irqsave(&info->lock, flags);
  1642. if (tty_hung_up_p(filp)) {
  1643. DBG_CNT("before DEC-hung");
  1644. goto out;
  1645. }
  1646. #ifdef SERIAL_DEBUG_OPEN
  1647. printk("rs_close ttys%d, count = %d\n", info->line, info->count);
  1648. #endif
  1649. if ((tty->count == 1) && (info->count != 1)) {
  1650. /*
  1651. * Uh, oh. tty->count is 1, which means that the tty
  1652. * structure will be freed. Info->count should always
  1653. * be one in these conditions. If it's greater than
  1654. * one, we've got real problems, since it means the
  1655. * serial port won't be shutdown.
  1656. */
  1657. printk("rs_close: bad serial port count; tty->count is 1, "
  1658. "info->count is %d\n", info->count);
  1659. info->count = 1;
  1660. }
  1661. if (--info->count < 0) {
  1662. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1663. info->line, info->count);
  1664. info->count = 0;
  1665. }
  1666. if (info->count) {
  1667. DBG_CNT("before DEC-2");
  1668. goto out;
  1669. }
  1670. info->flags |= ASYNC_CLOSING;
  1671. spin_unlock_irqrestore(&info->lock, flags);
  1672. /*
  1673. * Now we wait for the transmit buffer to clear; and we notify
  1674. * the line discipline to only process XON/XOFF characters.
  1675. */
  1676. tty->closing = 1;
  1677. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1678. tty_wait_until_sent(tty, info->closing_wait);
  1679. /*
  1680. * At this point we stop accepting input. To do this, we
  1681. * disable the receive line status interrupts, and tell the
  1682. * interrupt driver to stop checking the data ready bit in the
  1683. * line status register.
  1684. */
  1685. /* info->IER &= ~UART_IER_RLSI; */
  1686. info->IER &= ~UART_IER_RDI;
  1687. info->read_status_mask &= ~UART_LSR_DR;
  1688. if (info->flags & ASYNC_INITIALIZED) {
  1689. spin_lock_irqsave(&info->lock, flags);
  1690. serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
  1691. serial_out(info, UART_ESI_CMD2, info->IER);
  1692. /* disable receive timeout */
  1693. serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
  1694. serial_out(info, UART_ESI_CMD2, 0x00);
  1695. spin_unlock_irqrestore(&info->lock, flags);
  1696. /*
  1697. * Before we drop DTR, make sure the UART transmitter
  1698. * has completely drained; this is especially
  1699. * important if there is a transmit FIFO!
  1700. */
  1701. rs_wait_until_sent(tty, info->timeout);
  1702. }
  1703. shutdown(info);
  1704. if (tty->driver->flush_buffer)
  1705. tty->driver->flush_buffer(tty);
  1706. tty_ldisc_flush(tty);
  1707. tty->closing = 0;
  1708. info->event = 0;
  1709. info->tty = NULL;
  1710. if (info->blocked_open) {
  1711. if (info->close_delay) {
  1712. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1713. }
  1714. wake_up_interruptible(&info->open_wait);
  1715. }
  1716. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1717. wake_up_interruptible(&info->close_wait);
  1718. return;
  1719. out:
  1720. spin_unlock_irqrestore(&info->lock, flags);
  1721. }
  1722. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1723. {
  1724. struct esp_struct *info = (struct esp_struct *)tty->driver_data;
  1725. unsigned long orig_jiffies, char_time;
  1726. unsigned long flags;
  1727. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1728. return;
  1729. orig_jiffies = jiffies;
  1730. char_time = ((info->timeout - HZ / 50) / 1024) / 5;
  1731. if (!char_time)
  1732. char_time = 1;
  1733. spin_lock_irqsave(&info->lock, flags);
  1734. serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
  1735. serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
  1736. while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
  1737. (serial_in(info, UART_ESI_STAT2) != 0xff)) {
  1738. spin_unlock_irqrestore(&info->lock, flags);
  1739. msleep_interruptible(jiffies_to_msecs(char_time));
  1740. if (signal_pending(current))
  1741. break;
  1742. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1743. break;
  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. }
  1748. spin_unlock_irqrestore(&info->lock, flags);
  1749. set_current_state(TASK_RUNNING);
  1750. }
  1751. /*
  1752. * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
  1753. */
  1754. static void esp_hangup(struct tty_struct *tty)
  1755. {
  1756. struct esp_struct * info = (struct esp_struct *)tty->driver_data;
  1757. if (serial_paranoia_check(info, tty->name, "esp_hangup"))
  1758. return;
  1759. rs_flush_buffer(tty);
  1760. shutdown(info);
  1761. info->event = 0;
  1762. info->count = 0;
  1763. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1764. info->tty = NULL;
  1765. wake_up_interruptible(&info->open_wait);
  1766. }
  1767. /*
  1768. * ------------------------------------------------------------
  1769. * esp_open() and friends
  1770. * ------------------------------------------------------------
  1771. */
  1772. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1773. struct esp_struct *info)
  1774. {
  1775. DECLARE_WAITQUEUE(wait, current);
  1776. int retval;
  1777. int do_clocal = 0;
  1778. unsigned long flags;
  1779. /*
  1780. * If the device is in the middle of being closed, then block
  1781. * until it's done, and then try again.
  1782. */
  1783. if (tty_hung_up_p(filp) ||
  1784. (info->flags & ASYNC_CLOSING)) {
  1785. if (info->flags & ASYNC_CLOSING)
  1786. interruptible_sleep_on(&info->close_wait);
  1787. #ifdef SERIAL_DO_RESTART
  1788. if (info->flags & ASYNC_HUP_NOTIFY)
  1789. return -EAGAIN;
  1790. else
  1791. return -ERESTARTSYS;
  1792. #else
  1793. return -EAGAIN;
  1794. #endif
  1795. }
  1796. /*
  1797. * If non-blocking mode is set, or the port is not enabled,
  1798. * then make the check up front and then exit.
  1799. */
  1800. if ((filp->f_flags & O_NONBLOCK) ||
  1801. (tty->flags & (1 << TTY_IO_ERROR))) {
  1802. info->flags |= ASYNC_NORMAL_ACTIVE;
  1803. return 0;
  1804. }
  1805. if (tty->termios->c_cflag & CLOCAL)
  1806. do_clocal = 1;
  1807. /*
  1808. * Block waiting for the carrier detect and the line to become
  1809. * free (i.e., not in use by the callout). While we are in
  1810. * this loop, info->count is dropped by one, so that
  1811. * rs_close() knows when to free things. We restore it upon
  1812. * exit, either normal or abnormal.
  1813. */
  1814. retval = 0;
  1815. add_wait_queue(&info->open_wait, &wait);
  1816. #ifdef SERIAL_DEBUG_OPEN
  1817. printk("block_til_ready before block: ttys%d, count = %d\n",
  1818. info->line, info->count);
  1819. #endif
  1820. spin_lock_irqsave(&info->lock, flags);
  1821. if (!tty_hung_up_p(filp))
  1822. info->count--;
  1823. info->blocked_open++;
  1824. while (1) {
  1825. if ((tty->termios->c_cflag & CBAUD)) {
  1826. unsigned int scratch;
  1827. serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
  1828. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1829. scratch = serial_in(info, UART_ESI_STAT1);
  1830. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1831. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1832. serial_out(info, UART_ESI_CMD2,
  1833. scratch | UART_MCR_DTR | UART_MCR_RTS);
  1834. }
  1835. set_current_state(TASK_INTERRUPTIBLE);
  1836. if (tty_hung_up_p(filp) ||
  1837. !(info->flags & ASYNC_INITIALIZED)) {
  1838. #ifdef SERIAL_DO_RESTART
  1839. if (info->flags & ASYNC_HUP_NOTIFY)
  1840. retval = -EAGAIN;
  1841. else
  1842. retval = -ERESTARTSYS;
  1843. #else
  1844. retval = -EAGAIN;
  1845. #endif
  1846. break;
  1847. }
  1848. serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
  1849. if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
  1850. do_clocal = 1;
  1851. if (!(info->flags & ASYNC_CLOSING) &&
  1852. (do_clocal))
  1853. break;
  1854. if (signal_pending(current)) {
  1855. retval = -ERESTARTSYS;
  1856. break;
  1857. }
  1858. #ifdef SERIAL_DEBUG_OPEN
  1859. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1860. info->line, info->count);
  1861. #endif
  1862. spin_unlock_irqrestore(&info->lock, flags);
  1863. schedule();
  1864. spin_lock_irqsave(&info->lock, flags);
  1865. }
  1866. set_current_state(TASK_RUNNING);
  1867. remove_wait_queue(&info->open_wait, &wait);
  1868. if (!tty_hung_up_p(filp))
  1869. info->count++;
  1870. info->blocked_open--;
  1871. spin_unlock_irqrestore(&info->lock, flags);
  1872. #ifdef SERIAL_DEBUG_OPEN
  1873. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1874. info->line, info->count);
  1875. #endif
  1876. if (retval)
  1877. return retval;
  1878. info->flags |= ASYNC_NORMAL_ACTIVE;
  1879. return 0;
  1880. }
  1881. /*
  1882. * This routine is called whenever a serial port is opened. It
  1883. * enables interrupts for a serial port, linking in its async structure into
  1884. * the IRQ chain. It also performs the serial-specific
  1885. * initialization for the tty structure.
  1886. */
  1887. static int esp_open(struct tty_struct *tty, struct file * filp)
  1888. {
  1889. struct esp_struct *info;
  1890. int retval, line;
  1891. unsigned long flags;
  1892. line = tty->index;
  1893. if ((line < 0) || (line >= NR_PORTS))
  1894. return -ENODEV;
  1895. /* find the port in the chain */
  1896. info = ports;
  1897. while (info && (info->line != line))
  1898. info = info->next_port;
  1899. if (!info) {
  1900. serial_paranoia_check(info, tty->name, "esp_open");
  1901. return -ENODEV;
  1902. }
  1903. #ifdef SERIAL_DEBUG_OPEN
  1904. printk("esp_open %s, count = %d\n", tty->name, info->count);
  1905. #endif
  1906. spin_lock_irqsave(&info->lock, flags);
  1907. info->count++;
  1908. tty->driver_data = info;
  1909. info->tty = tty;
  1910. spin_unlock_irqrestore(&info->lock, flags);
  1911. /*
  1912. * Start up serial port
  1913. */
  1914. retval = startup(info);
  1915. if (retval)
  1916. return retval;
  1917. retval = block_til_ready(tty, filp, info);
  1918. if (retval) {
  1919. #ifdef SERIAL_DEBUG_OPEN
  1920. printk("esp_open returning after block_til_ready with %d\n",
  1921. retval);
  1922. #endif
  1923. return retval;
  1924. }
  1925. #ifdef SERIAL_DEBUG_OPEN
  1926. printk("esp_open %s successful...", tty->name);
  1927. #endif
  1928. return 0;
  1929. }
  1930. /*
  1931. * ---------------------------------------------------------------------
  1932. * espserial_init() and friends
  1933. *
  1934. * espserial_init() is called at boot-time to initialize the serial driver.
  1935. * ---------------------------------------------------------------------
  1936. */
  1937. /*
  1938. * This routine prints out the appropriate serial driver version
  1939. * number, and identifies which options were configured into this
  1940. * driver.
  1941. */
  1942. static inline void show_serial_version(void)
  1943. {
  1944. printk(KERN_INFO "%s version %s (DMA %u)\n",
  1945. serial_name, serial_version, dma);
  1946. }
  1947. /*
  1948. * This routine is called by espserial_init() to initialize a specific serial
  1949. * port.
  1950. */
  1951. static inline int autoconfig(struct esp_struct * info)
  1952. {
  1953. int port_detected = 0;
  1954. unsigned long flags;
  1955. if (!request_region(info->port, REGION_SIZE, "esp serial"))
  1956. return -EIO;
  1957. spin_lock_irqsave(&info->lock, flags);
  1958. /*
  1959. * Check for ESP card
  1960. */
  1961. if (serial_in(info, UART_ESI_BASE) == 0xf3) {
  1962. serial_out(info, UART_ESI_CMD1, 0x00);
  1963. serial_out(info, UART_ESI_CMD1, 0x01);
  1964. if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
  1965. port_detected = 1;
  1966. if (!(info->irq)) {
  1967. serial_out(info, UART_ESI_CMD1, 0x02);
  1968. if (serial_in(info, UART_ESI_STAT1) & 0x01)
  1969. info->irq = 3;
  1970. else
  1971. info->irq = 4;
  1972. }
  1973. /* put card in enhanced mode */
  1974. /* this prevents access through */
  1975. /* the "old" IO ports */
  1976. esp_basic_init(info);
  1977. /* clear out MCR */
  1978. serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
  1979. serial_out(info, UART_ESI_CMD2, UART_MCR);
  1980. serial_out(info, UART_ESI_CMD2, 0x00);
  1981. }
  1982. }
  1983. if (!port_detected)
  1984. release_region(info->port, REGION_SIZE);
  1985. spin_unlock_irqrestore(&info->lock, flags);
  1986. return (port_detected);
  1987. }
  1988. static const struct tty_operations esp_ops = {
  1989. .open = esp_open,
  1990. .close = rs_close,
  1991. .write = rs_write,
  1992. .put_char = rs_put_char,
  1993. .flush_chars = rs_flush_chars,
  1994. .write_room = rs_write_room,
  1995. .chars_in_buffer = rs_chars_in_buffer,
  1996. .flush_buffer = rs_flush_buffer,
  1997. .ioctl = rs_ioctl,
  1998. .throttle = rs_throttle,
  1999. .unthrottle = rs_unthrottle,
  2000. .set_termios = rs_set_termios,
  2001. .stop = rs_stop,
  2002. .start = rs_start,
  2003. .hangup = esp_hangup,
  2004. .break_ctl = esp_break,
  2005. .wait_until_sent = rs_wait_until_sent,
  2006. .tiocmget = esp_tiocmget,
  2007. .tiocmset = esp_tiocmset,
  2008. };
  2009. /*
  2010. * The serial driver boot-time initialization code!
  2011. */
  2012. static int __init espserial_init(void)
  2013. {
  2014. int i, offset;
  2015. struct esp_struct * info;
  2016. struct esp_struct *last_primary = NULL;
  2017. int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
  2018. esp_driver = alloc_tty_driver(NR_PORTS);
  2019. if (!esp_driver)
  2020. return -ENOMEM;
  2021. for (i = 0; i < NR_PRIMARY; i++) {
  2022. if (irq[i] != 0) {
  2023. if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
  2024. (irq[i] == 8) || (irq[i] == 13))
  2025. irq[i] = 0;
  2026. else if (irq[i] == 2)
  2027. irq[i] = 9;
  2028. }
  2029. }
  2030. if ((dma != 1) && (dma != 3))
  2031. dma = 0;
  2032. if ((rx_trigger < 1) || (rx_trigger > 1023))
  2033. rx_trigger = 768;
  2034. if ((tx_trigger < 1) || (tx_trigger > 1023))
  2035. tx_trigger = 768;
  2036. if ((flow_off < 1) || (flow_off > 1023))
  2037. flow_off = 1016;
  2038. if ((flow_on < 1) || (flow_on > 1023))
  2039. flow_on = 944;
  2040. if ((rx_timeout < 0) || (rx_timeout > 255))
  2041. rx_timeout = 128;
  2042. if (flow_on >= flow_off)
  2043. flow_on = flow_off - 1;
  2044. show_serial_version();
  2045. /* Initialize the tty_driver structure */
  2046. esp_driver->owner = THIS_MODULE;
  2047. esp_driver->name = "ttyP";
  2048. esp_driver->major = ESP_IN_MAJOR;
  2049. esp_driver->minor_start = 0;
  2050. esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2051. esp_driver->subtype = SERIAL_TYPE_NORMAL;
  2052. esp_driver->init_termios = tty_std_termios;
  2053. esp_driver->init_termios.c_cflag =
  2054. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2055. esp_driver->flags = TTY_DRIVER_REAL_RAW;
  2056. tty_set_operations(esp_driver, &esp_ops);
  2057. if (tty_register_driver(esp_driver))
  2058. {
  2059. printk(KERN_ERR "Couldn't register esp serial driver");
  2060. put_tty_driver(esp_driver);
  2061. return 1;
  2062. }
  2063. info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
  2064. if (!info)
  2065. {
  2066. printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
  2067. tty_unregister_driver(esp_driver);
  2068. put_tty_driver(esp_driver);
  2069. return 1;
  2070. }
  2071. memset((void *)info, 0, sizeof(struct esp_struct));
  2072. spin_lock_init(&info->lock);
  2073. /* rx_trigger, tx_trigger are needed by autoconfig */
  2074. info->config.rx_trigger = rx_trigger;
  2075. info->config.tx_trigger = tx_trigger;
  2076. i = 0;
  2077. offset = 0;
  2078. do {
  2079. info->port = esp[i] + offset;
  2080. info->irq = irq[i];
  2081. info->line = (i * 8) + (offset / 8);
  2082. if (!autoconfig(info)) {
  2083. i++;
  2084. offset = 0;
  2085. continue;
  2086. }
  2087. info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
  2088. info->flags = STD_COM_FLAGS;
  2089. if (info->custom_divisor)
  2090. info->flags |= ASYNC_SPD_CUST;
  2091. info->magic = ESP_MAGIC;
  2092. info->close_delay = 5*HZ/10;
  2093. info->closing_wait = 30*HZ;
  2094. INIT_WORK(&info->tqueue, do_softint, info);
  2095. INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
  2096. info->config.rx_timeout = rx_timeout;
  2097. info->config.flow_on = flow_on;
  2098. info->config.flow_off = flow_off;
  2099. info->config.pio_threshold = pio_threshold;
  2100. info->next_port = ports;
  2101. init_waitqueue_head(&info->open_wait);
  2102. init_waitqueue_head(&info->close_wait);
  2103. init_waitqueue_head(&info->delta_msr_wait);
  2104. init_waitqueue_head(&info->break_wait);
  2105. ports = info;
  2106. printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
  2107. info->line, info->port, info->irq);
  2108. if (info->line % 8) {
  2109. printk("secondary port\n");
  2110. /* 8 port cards can't do DMA */
  2111. info->stat_flags |= ESP_STAT_NEVER_DMA;
  2112. if (last_primary)
  2113. last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
  2114. } else {
  2115. printk("primary port\n");
  2116. last_primary = info;
  2117. irq[i] = info->irq;
  2118. }
  2119. if (!dma)
  2120. info->stat_flags |= ESP_STAT_NEVER_DMA;
  2121. info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
  2122. if (!info)
  2123. {
  2124. printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
  2125. /* allow use of the already detected ports */
  2126. return 0;
  2127. }
  2128. memset((void *)info, 0, sizeof(struct esp_struct));
  2129. /* rx_trigger, tx_trigger are needed by autoconfig */
  2130. info->config.rx_trigger = rx_trigger;
  2131. info->config.tx_trigger = tx_trigger;
  2132. if (offset == 56) {
  2133. i++;
  2134. offset = 0;
  2135. } else {
  2136. offset += 8;
  2137. }
  2138. } while (i < NR_PRIMARY);
  2139. /* free the last port memory allocation */
  2140. kfree(info);
  2141. return 0;
  2142. }
  2143. static void __exit espserial_exit(void)
  2144. {
  2145. int e1;
  2146. struct esp_struct *temp_async;
  2147. struct esp_pio_buffer *pio_buf;
  2148. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  2149. if ((e1 = tty_unregister_driver(esp_driver)))
  2150. printk("SERIAL: failed to unregister serial driver (%d)\n",
  2151. e1);
  2152. put_tty_driver(esp_driver);
  2153. while (ports) {
  2154. if (ports->port) {
  2155. release_region(ports->port, REGION_SIZE);
  2156. }
  2157. temp_async = ports->next_port;
  2158. kfree(ports);
  2159. ports = temp_async;
  2160. }
  2161. if (dma_buffer)
  2162. free_pages((unsigned long)dma_buffer,
  2163. get_order(DMA_BUFFER_SZ));
  2164. while (free_pio_buf) {
  2165. pio_buf = free_pio_buf->next;
  2166. kfree(free_pio_buf);
  2167. free_pio_buf = pio_buf;
  2168. }
  2169. }
  2170. module_init(espserial_init);
  2171. module_exit(espserial_exit);