esp.c 69 KB

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