esp.c 66 KB

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