blob: a5c59fc2b0ff44335bf61d94ba33b039e6103ac5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Alan Coxfb100b62008-04-30 00:54:16 -070011 * Accent Async board.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
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.
Jan Engelhardt96de0e22007-10-19 23:21:04 +020022 * Bernd Anhäupl 05/17/96.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
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
43#include <linux/module.h>
44#include <linux/errno.h>
45#include <linux/signal.h>
46#include <linux/sched.h>
47#include <linux/interrupt.h>
48#include <linux/tty.h>
49#include <linux/tty_flip.h>
50#include <linux/serial.h>
51#include <linux/serialP.h>
52#include <linux/serial_reg.h>
53#include <linux/major.h>
54#include <linux/string.h>
55#include <linux/fcntl.h>
56#include <linux/ptrace.h>
57#include <linux/ioport.h>
58#include <linux/mm.h>
59#include <linux/init.h>
60#include <linux/delay.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070061#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/system.h>
Alan Coxfb100b62008-04-30 00:54:16 -070064#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <asm/dma.h>
67#include <linux/slab.h>
Alan Coxfb100b62008-04-30 00:54:16 -070068#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include <linux/hayesesp.h>
71
72#define NR_PORTS 64 /* maximum number of ports */
73#define NR_PRIMARY 8 /* maximum number of primary ports */
74#define REGION_SIZE 8 /* size of io region to request */
75
76/* The following variables can be set by giving module options */
77static int irq[NR_PRIMARY]; /* IRQ for each base port */
78static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
79static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
80static unsigned int rx_trigger = ESP_RX_TRIGGER;
81static unsigned int tx_trigger = ESP_TX_TRIGGER;
82static unsigned int flow_off = ESP_FLOW_OFF;
83static unsigned int flow_on = ESP_FLOW_ON;
84static unsigned int rx_timeout = ESP_RX_TMOUT;
85static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
86
87MODULE_LICENSE("GPL");
88
89module_param_array(irq, int, NULL, 0);
90module_param_array(divisor, uint, NULL, 0);
91module_param(dma, uint, 0);
92module_param(rx_trigger, uint, 0);
93module_param(tx_trigger, uint, 0);
94module_param(flow_off, uint, 0);
95module_param(flow_on, uint, 0);
96module_param(rx_timeout, uint, 0);
97module_param(pio_threshold, uint, 0);
98
99/* END */
100
101static char *dma_buffer;
102static int dma_bytes;
103static struct esp_pio_buffer *free_pio_buf;
104
105#define DMA_BUFFER_SZ 1024
106
107#define WAKEUP_CHARS 1024
108
109static char serial_name[] __initdata = "ESP serial driver";
110static char serial_version[] __initdata = "2.2";
111
112static struct tty_driver *esp_driver;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * Serial driver configuration section. Here are the various options:
116 *
117 * SERIAL_PARANOIA_CHECK
118 * Check the magic number for the esp_structure where
119 * ever possible.
120 */
121
122#undef SERIAL_PARANOIA_CHECK
123#define SERIAL_DO_RESTART
124
125#undef SERIAL_DEBUG_INTR
126#undef SERIAL_DEBUG_OPEN
127#undef SERIAL_DEBUG_FLOW
128
129#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
Alan Coxfb100b62008-04-30 00:54:16 -0700130#define DBG_CNT(s) printk(KERN_DEBUG "(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
Alan Cox4982d6b2008-07-16 21:55:11 +0100131 tty->name, info->port.flags, \
Alan Coxfb100b62008-04-30 00:54:16 -0700132 serial_driver.refcount, \
Alan Cox4982d6b2008-07-16 21:55:11 +0100133 info->port.count, tty->count, s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#else
135#define DBG_CNT(s)
136#endif
137
138static struct esp_struct *ports;
139
140static void change_speed(struct esp_struct *info);
141static void rs_wait_until_sent(struct tty_struct *, int);
142
143/*
144 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
145 * times the normal 1.8432 Mhz clock of most serial boards).
146 */
147#define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
148
149/* Standard COM flags (except for COM4, because of the 8514 problem) */
150#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static inline int serial_paranoia_check(struct esp_struct *info,
153 char *name, const char *routine)
154{
155#ifdef SERIAL_PARANOIA_CHECK
156 static const char badmagic[] = KERN_WARNING
157 "Warning: bad magic number for serial struct (%s) in %s\n";
158 static const char badinfo[] = KERN_WARNING
159 "Warning: null esp_struct for (%s) in %s\n";
160
161 if (!info) {
162 printk(badinfo, name, routine);
163 return 1;
164 }
165 if (info->magic != ESP_MAGIC) {
166 printk(badmagic, name, routine);
167 return 1;
168 }
169#endif
170 return 0;
171}
172
173static inline unsigned int serial_in(struct esp_struct *info, int offset)
174{
Alan Cox4982d6b2008-07-16 21:55:11 +0100175 return inb(info->io_port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static inline void serial_out(struct esp_struct *info, int offset,
179 unsigned char value)
180{
Alan Cox4982d6b2008-07-16 21:55:11 +0100181 outb(value, info->io_port+offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/*
185 * ------------------------------------------------------------
186 * rs_stop() and rs_start()
187 *
188 * This routines are called before setting or resetting tty->stopped.
189 * They enable or disable transmitter interrupts, as necessary.
190 * ------------------------------------------------------------
191 */
192static void rs_stop(struct tty_struct *tty)
193{
Alan Coxfb100b62008-04-30 00:54:16 -0700194 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned long flags;
196
197 if (serial_paranoia_check(info, tty->name, "rs_stop"))
198 return;
199
200 spin_lock_irqsave(&info->lock, flags);
201 if (info->IER & UART_IER_THRI) {
202 info->IER &= ~UART_IER_THRI;
203 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
204 serial_out(info, UART_ESI_CMD2, info->IER);
205 }
206 spin_unlock_irqrestore(&info->lock, flags);
207}
208
209static void rs_start(struct tty_struct *tty)
210{
Alan Coxfb100b62008-04-30 00:54:16 -0700211 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 return;
Alan Coxfb100b62008-04-30 00:54:16 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 spin_lock_irqsave(&info->lock, flags);
218 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
219 info->IER |= UART_IER_THRI;
220 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
221 serial_out(info, UART_ESI_CMD2, info->IER);
222 }
223 spin_unlock_irqrestore(&info->lock, flags);
224}
225
226/*
227 * ----------------------------------------------------------------------
228 *
229 * Here starts the interrupt handling routines. All of the following
230 * subroutines are declared as inline and are folded into
231 * rs_interrupt(). They were separated out for readability's sake.
232 *
233 * Note: rs_interrupt() is a "fast" interrupt, which means that it
234 * runs with interrupts turned off. People who may want to modify
235 * rs_interrupt() should try to keep the interrupt handler as fast as
236 * possible. After you are done making modifications, it is not a bad
237 * idea to do:
Alan Coxfb100b62008-04-30 00:54:16 -0700238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
240 *
241 * and look at the resulting assemble code in serial.s.
242 *
243 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
244 * -----------------------------------------------------------------------
245 */
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static DEFINE_SPINLOCK(pio_lock);
248
249static inline struct esp_pio_buffer *get_pio_buffer(void)
250{
251 struct esp_pio_buffer *buf;
252 unsigned long flags;
253
254 spin_lock_irqsave(&pio_lock, flags);
255 if (free_pio_buf) {
256 buf = free_pio_buf;
257 free_pio_buf = buf->next;
258 } else {
259 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
260 }
261 spin_unlock_irqrestore(&pio_lock, flags);
262 return buf;
263}
264
265static inline void release_pio_buffer(struct esp_pio_buffer *buf)
266{
267 unsigned long flags;
268 spin_lock_irqsave(&pio_lock, flags);
269 buf->next = free_pio_buf;
270 free_pio_buf = buf;
271 spin_unlock_irqrestore(&pio_lock, flags);
272}
273
274static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
275{
Alan Cox4982d6b2008-07-16 21:55:11 +0100276 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int i;
278 struct esp_pio_buffer *pio_buf;
279 struct esp_pio_buffer *err_buf;
280 unsigned char status_mask;
281
282 pio_buf = get_pio_buffer();
283
284 if (!pio_buf)
285 return;
286
287 err_buf = get_pio_buffer();
288
289 if (!err_buf) {
290 release_pio_buffer(pio_buf);
291 return;
292 }
293
294 status_mask = (info->read_status_mask >> 2) & 0x07;
Alan Coxfb100b62008-04-30 00:54:16 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 for (i = 0; i < num_bytes - 1; i += 2) {
297 *((unsigned short *)(pio_buf->data + i)) =
Alan Cox4982d6b2008-07-16 21:55:11 +0100298 inw(info->io_port + UART_ESI_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
300 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
301 err_buf->data[i] &= status_mask;
302 }
303
304 if (num_bytes & 0x0001) {
305 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
306 err_buf->data[num_bytes - 1] =
307 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
308 }
309
310 /* make sure everything is still ok since interrupts were enabled */
Alan Cox4982d6b2008-07-16 21:55:11 +0100311 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (!tty) {
314 release_pio_buffer(pio_buf);
315 release_pio_buffer(err_buf);
316 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
317 return;
318 }
319
320 status_mask = (info->ignore_status_mask >> 2) & 0x07;
321
322 for (i = 0; i < num_bytes; i++) {
323 if (!(err_buf->data[i] & status_mask)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800324 int flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (err_buf->data[i] & 0x04) {
Alan Cox33f0f882006-01-09 20:54:13 -0800327 flag = TTY_BREAK;
Alan Cox4982d6b2008-07-16 21:55:11 +0100328 if (info->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 do_SAK(tty);
Alan Coxfb100b62008-04-30 00:54:16 -0700330 } else if (err_buf->data[i] & 0x02)
Alan Cox33f0f882006-01-09 20:54:13 -0800331 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 else if (err_buf->data[i] & 0x01)
Alan Cox33f0f882006-01-09 20:54:13 -0800333 flag = TTY_PARITY;
334 tty_insert_flip_char(tty, pio_buf->data[i], flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 }
337
Paul Fulghum808249c2006-02-03 03:04:41 -0800338 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
341 release_pio_buffer(pio_buf);
342 release_pio_buffer(err_buf);
343}
344
Alan Coxfb100b62008-04-30 00:54:16 -0700345static void program_isa_dma(int dma, int dir, unsigned long addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -0700348
349 flags = claim_dma_lock();
350 disable_dma(dma);
351 clear_dma_ff(dma);
352 set_dma_mode(dma, dir);
353 set_dma_addr(dma, addr);
354 set_dma_count(dma, len);
355 enable_dma(dma);
356 release_dma_lock(flags);
357}
358
359static void receive_chars_dma(struct esp_struct *info, int num_bytes)
360{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
362 dma_bytes = num_bytes;
363 info->stat_flags |= ESP_STAT_DMA_RX;
Alan Coxfb100b62008-04-30 00:54:16 -0700364
365 program_isa_dma(dma, DMA_MODE_READ, isa_virt_to_bus(dma_buffer),
366 dma_bytes);
367 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370static inline void receive_chars_dma_done(struct esp_struct *info,
371 int status)
372{
Alan Cox4982d6b2008-07-16 21:55:11 +0100373 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int num_bytes;
375 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -0700376
377 flags = claim_dma_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 disable_dma(dma);
379 clear_dma_ff(dma);
380
381 info->stat_flags &= ~ESP_STAT_DMA_RX;
382 num_bytes = dma_bytes - get_dma_residue(dma);
383 release_dma_lock(flags);
Alan Coxfb100b62008-04-30 00:54:16 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 info->icount.rx += num_bytes;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (num_bytes > 0) {
Alan Cox33f0f882006-01-09 20:54:13 -0800388 tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 status &= (0x1c & info->read_status_mask);
Alan Coxfb100b62008-04-30 00:54:16 -0700391
Alan Cox33f0f882006-01-09 20:54:13 -0800392 /* Is the status significant or do we throw the last byte ? */
393 if (!(status & info->ignore_status_mask)) {
394 int statflag = 0;
395
396 if (status & 0x10) {
397 statflag = TTY_BREAK;
398 (info->icount.brk)++;
Alan Cox4982d6b2008-07-16 21:55:11 +0100399 if (info->port.flags & ASYNC_SAK)
Alan Cox33f0f882006-01-09 20:54:13 -0800400 do_SAK(tty);
401 } else if (status & 0x08) {
402 statflag = TTY_FRAME;
Alan Coxfb100b62008-04-30 00:54:16 -0700403 info->icount.frame++;
404 } else if (status & 0x04) {
Alan Cox33f0f882006-01-09 20:54:13 -0800405 statflag = TTY_PARITY;
Alan Coxfb100b62008-04-30 00:54:16 -0700406 info->icount.parity++;
Alan Cox33f0f882006-01-09 20:54:13 -0800407 }
Alan Coxfb100b62008-04-30 00:54:16 -0700408 tty_insert_flip_char(tty, dma_buffer[num_bytes - 1],
409 statflag);
Alan Cox33f0f882006-01-09 20:54:13 -0800410 }
Paul Fulghum808249c2006-02-03 03:04:41 -0800411 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 if (dma_bytes != num_bytes) {
415 num_bytes = dma_bytes - num_bytes;
416 dma_bytes = 0;
417 receive_chars_dma(info, num_bytes);
418 } else
419 dma_bytes = 0;
420}
421
422/* Caller must hold info->lock */
423
424static inline void transmit_chars_pio(struct esp_struct *info,
425 int space_avail)
426{
427 int i;
428 struct esp_pio_buffer *pio_buf;
429
430 pio_buf = get_pio_buffer();
431
432 if (!pio_buf)
433 return;
434
435 while (space_avail && info->xmit_cnt) {
436 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
437 memcpy(pio_buf->data,
438 &(info->xmit_buf[info->xmit_tail]),
439 space_avail);
440 } else {
441 i = ESP_XMIT_SIZE - info->xmit_tail;
442 memcpy(pio_buf->data,
443 &(info->xmit_buf[info->xmit_tail]), i);
444 memcpy(&(pio_buf->data[i]), info->xmit_buf,
445 space_avail - i);
446 }
447
448 info->xmit_cnt -= space_avail;
449 info->xmit_tail = (info->xmit_tail + space_avail) &
450 (ESP_XMIT_SIZE - 1);
451
452 for (i = 0; i < space_avail - 1; i += 2) {
453 outw(*((unsigned short *)(pio_buf->data + i)),
Alan Cox4982d6b2008-07-16 21:55:11 +0100454 info->io_port + UART_ESI_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
457 if (space_avail & 0x0001)
458 serial_out(info, UART_ESI_TX,
459 pio_buf->data[space_avail - 1]);
460
461 if (info->xmit_cnt) {
462 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
463 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
464 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
465 space_avail |= serial_in(info, UART_ESI_STAT2);
466
467 if (space_avail > info->xmit_cnt)
468 space_avail = info->xmit_cnt;
469 }
470 }
471
472 if (info->xmit_cnt < WAKEUP_CHARS) {
Alan Cox4982d6b2008-07-16 21:55:11 +0100473 if (info->port.tty)
474 tty_wakeup(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476#ifdef SERIAL_DEBUG_INTR
477 printk("THRE...");
478#endif
479
480 if (info->xmit_cnt <= 0) {
481 info->IER &= ~UART_IER_THRI;
482 serial_out(info, UART_ESI_CMD1,
483 ESI_SET_SRV_MASK);
484 serial_out(info, UART_ESI_CMD2, info->IER);
485 }
486 }
487
488 release_pio_buffer(pio_buf);
489}
490
491/* Caller must hold info->lock */
492static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
493{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 dma_bytes = num_bytes;
495
496 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
497 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
498 dma_bytes);
499 } else {
500 int i = ESP_XMIT_SIZE - info->xmit_tail;
501 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
502 i);
503 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
504 }
505
506 info->xmit_cnt -= dma_bytes;
507 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
508
509 if (info->xmit_cnt < WAKEUP_CHARS) {
Alan Cox4982d6b2008-07-16 21:55:11 +0100510 if (info->port.tty)
511 tty_wakeup(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513#ifdef SERIAL_DEBUG_INTR
514 printk("THRE...");
515#endif
516
517 if (info->xmit_cnt <= 0) {
518 info->IER &= ~UART_IER_THRI;
519 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
520 serial_out(info, UART_ESI_CMD2, info->IER);
521 }
522 }
523
524 info->stat_flags |= ESP_STAT_DMA_TX;
Alan Coxfb100b62008-04-30 00:54:16 -0700525
526 program_isa_dma(dma, DMA_MODE_WRITE, isa_virt_to_bus(dma_buffer),
527 dma_bytes);
528 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static inline void transmit_chars_dma_done(struct esp_struct *info)
532{
533 int num_bytes;
534 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Alan Coxfb100b62008-04-30 00:54:16 -0700536 flags = claim_dma_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 disable_dma(dma);
538 clear_dma_ff(dma);
539
540 num_bytes = dma_bytes - get_dma_residue(dma);
541 info->icount.tx += dma_bytes;
542 release_dma_lock(flags);
543
544 if (dma_bytes != num_bytes) {
545 dma_bytes -= num_bytes;
546 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
Alan Coxfb100b62008-04-30 00:54:16 -0700547
548 program_isa_dma(dma, DMA_MODE_WRITE,
549 isa_virt_to_bus(dma_buffer), dma_bytes);
550
551 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 } else {
553 dma_bytes = 0;
554 info->stat_flags &= ~ESP_STAT_DMA_TX;
555 }
556}
557
Alan Coxfb100b62008-04-30 00:54:16 -0700558static void check_modem_status(struct esp_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int status;
Alan Coxfb100b62008-04-30 00:54:16 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
563 status = serial_in(info, UART_ESI_STAT2);
564
565 if (status & UART_MSR_ANY_DELTA) {
566 /* update input line counters */
567 if (status & UART_MSR_TERI)
568 info->icount.rng++;
569 if (status & UART_MSR_DDSR)
570 info->icount.dsr++;
571 if (status & UART_MSR_DDCD)
572 info->icount.dcd++;
573 if (status & UART_MSR_DCTS)
574 info->icount.cts++;
575 wake_up_interruptible(&info->delta_msr_wait);
576 }
577
Alan Cox4982d6b2008-07-16 21:55:11 +0100578 if ((info->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
580 printk("ttys%d CD now %s...", info->line,
581 (status & UART_MSR_DCD) ? "on" : "off");
Alan Coxfb100b62008-04-30 00:54:16 -0700582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (status & UART_MSR_DCD)
Alan Cox4982d6b2008-07-16 21:55:11 +0100584 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 else {
586#ifdef SERIAL_DEBUG_OPEN
587 printk("scheduling hangup...");
588#endif
Alan Cox4982d6b2008-07-16 21:55:11 +0100589 tty_hangup(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 }
592}
593
594/*
595 * This is the serial driver's interrupt routine
596 */
David Howells7d12e782006-10-05 14:55:46 +0100597static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Alan Coxfb100b62008-04-30 00:54:16 -0700599 struct esp_struct *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 unsigned err_status;
601 unsigned int scratch;
602
603#ifdef SERIAL_DEBUG_INTR
604 printk("rs_interrupt_single(%d)...", irq);
605#endif
606 info = (struct esp_struct *)dev_id;
607 err_status = 0;
608 scratch = serial_in(info, UART_ESI_SID);
609
610 spin_lock(&info->lock);
Alan Coxfb100b62008-04-30 00:54:16 -0700611
Alan Cox4982d6b2008-07-16 21:55:11 +0100612 if (!info->port.tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 spin_unlock(&info->lock);
614 return IRQ_NONE;
615 }
616
617 if (scratch & 0x04) { /* error */
618 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
619 err_status = serial_in(info, UART_ESI_STAT1);
620 serial_in(info, UART_ESI_STAT2);
621
622 if (err_status & 0x01)
623 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
624
625 if (err_status & 0x20) /* UART status */
626 check_modem_status(info);
627
628 if (err_status & 0x80) /* Start break */
629 wake_up_interruptible(&info->break_wait);
630 }
Alan Coxfb100b62008-04-30 00:54:16 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if ((scratch & 0x88) || /* DMA completed or timed out */
633 (err_status & 0x1c) /* receive error */) {
634 if (info->stat_flags & ESP_STAT_DMA_RX)
635 receive_chars_dma_done(info, err_status);
636 else if (info->stat_flags & ESP_STAT_DMA_TX)
637 transmit_chars_dma_done(info);
638 }
639
640 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
641 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
642 (info->IER & UART_IER_RDI)) {
643 int num_bytes;
644
645 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
646 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
647 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
648 num_bytes |= serial_in(info, UART_ESI_STAT2);
649
Alan Cox4982d6b2008-07-16 21:55:11 +0100650 num_bytes = tty_buffer_request_room(info->port.tty, num_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (num_bytes) {
653 if (dma_bytes ||
654 (info->stat_flags & ESP_STAT_USE_PIO) ||
655 (num_bytes <= info->config.pio_threshold))
656 receive_chars_pio(info, num_bytes);
657 else
658 receive_chars_dma(info, num_bytes);
659 }
660 }
Alan Coxfb100b62008-04-30 00:54:16 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
663 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
Alan Cox4982d6b2008-07-16 21:55:11 +0100664 if ((info->xmit_cnt <= 0) || info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 info->IER &= ~UART_IER_THRI;
666 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
667 serial_out(info, UART_ESI_CMD2, info->IER);
668 } else {
669 int num_bytes;
670
671 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
672 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
673 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
674 num_bytes |= serial_in(info, UART_ESI_STAT2);
675
676 if (num_bytes > info->xmit_cnt)
677 num_bytes = info->xmit_cnt;
678
679 if (num_bytes) {
680 if (dma_bytes ||
681 (info->stat_flags & ESP_STAT_USE_PIO) ||
682 (num_bytes <= info->config.pio_threshold))
683 transmit_chars_pio(info, num_bytes);
684 else
685 transmit_chars_dma(info, num_bytes);
686 }
687 }
688 }
689
690 info->last_active = jiffies;
691
692#ifdef SERIAL_DEBUG_INTR
693 printk("end.\n");
694#endif
695 spin_unlock(&info->lock);
696 return IRQ_HANDLED;
697}
698
699/*
700 * -------------------------------------------------------------------
701 * Here ends the serial interrupt routines.
702 * -------------------------------------------------------------------
703 */
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
706 * ---------------------------------------------------------------
707 * Low level utility subroutines for the serial driver: routines to
708 * figure out the appropriate timeout for an interrupt chain, routines
709 * to initialize and startup a serial port, and routines to shutdown a
710 * serial port. Useful stuff like that.
711 *
712 * Caller should hold lock
713 * ---------------------------------------------------------------
714 */
715
Alan Coxfb100b62008-04-30 00:54:16 -0700716static void esp_basic_init(struct esp_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 /* put ESPC in enhanced mode */
719 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
Alan Coxfb100b62008-04-30 00:54:16 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (info->stat_flags & ESP_STAT_NEVER_DMA)
722 serial_out(info, UART_ESI_CMD2, 0x01);
723 else
724 serial_out(info, UART_ESI_CMD2, 0x31);
725
726 /* disable interrupts for now */
727 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
728 serial_out(info, UART_ESI_CMD2, 0x00);
729
730 /* set interrupt and DMA channel */
731 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
732
733 if (info->stat_flags & ESP_STAT_NEVER_DMA)
734 serial_out(info, UART_ESI_CMD2, 0x01);
735 else
736 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
737
738 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
739
740 if (info->line % 8) /* secondary port */
741 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
742 else if (info->irq == 9)
743 serial_out(info, UART_ESI_CMD2, 0x02);
744 else
745 serial_out(info, UART_ESI_CMD2, info->irq);
746
747 /* set error status mask (check this) */
748 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
749
750 if (info->stat_flags & ESP_STAT_NEVER_DMA)
751 serial_out(info, UART_ESI_CMD2, 0xa1);
752 else
753 serial_out(info, UART_ESI_CMD2, 0xbd);
754
755 serial_out(info, UART_ESI_CMD2, 0x00);
756
757 /* set DMA timeout */
758 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
759 serial_out(info, UART_ESI_CMD2, 0xff);
760
761 /* set FIFO trigger levels */
762 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
763 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
764 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
765 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
766 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
767
768 /* Set clock scaling and wait states */
769 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
770 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
771
772 /* set reinterrupt pacing */
773 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
774 serial_out(info, UART_ESI_CMD2, 0xff);
775}
776
Alan Coxfb100b62008-04-30 00:54:16 -0700777static int startup(struct esp_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -0700780 int retval = 0;
781 unsigned int num_chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Alan Coxfb100b62008-04-30 00:54:16 -0700783 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Alan Cox4982d6b2008-07-16 21:55:11 +0100785 if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto out;
787
788 if (!info->xmit_buf) {
789 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
790 retval = -ENOMEM;
791 if (!info->xmit_buf)
792 goto out;
793 }
794
795#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -0700796 printk(KERN_DEBUG "starting up ttys%d (irq %d)...",
797 info->line, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798#endif
799
800 /* Flush the RX buffer. Using the ESI flush command may cause */
801 /* wild interrupts, so read all the data instead. */
802
803 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
804 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
805 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
806 num_chars |= serial_in(info, UART_ESI_STAT2);
807
808 while (num_chars > 1) {
Alan Cox4982d6b2008-07-16 21:55:11 +0100809 inw(info->io_port + UART_ESI_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 num_chars -= 2;
811 }
812
813 if (num_chars)
814 serial_in(info, UART_ESI_RX);
815
816 /* set receive character timeout */
817 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
818 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
819
820 /* clear all flags except the "never DMA" flag */
821 info->stat_flags &= ESP_STAT_NEVER_DMA;
822
823 if (info->stat_flags & ESP_STAT_NEVER_DMA)
824 info->stat_flags |= ESP_STAT_USE_PIO;
825
826 spin_unlock_irqrestore(&info->lock, flags);
827
828 /*
829 * Allocate the IRQ
830 */
831
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700832 retval = request_irq(info->irq, rs_interrupt_single, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 "esp serial", info);
834
835 if (retval) {
836 if (capable(CAP_SYS_ADMIN)) {
Alan Cox4982d6b2008-07-16 21:55:11 +0100837 if (info->port.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 set_bit(TTY_IO_ERROR,
Alan Cox4982d6b2008-07-16 21:55:11 +0100839 &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 retval = 0;
841 }
842 goto out_unlocked;
843 }
844
845 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
846 dma_buffer = (char *)__get_dma_pages(
847 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
848
849 /* use PIO mode if DMA buf/chan cannot be allocated */
850 if (!dma_buffer)
851 info->stat_flags |= ESP_STAT_USE_PIO;
852 else if (request_dma(dma, "esp serial")) {
853 free_pages((unsigned long)dma_buffer,
854 get_order(DMA_BUFFER_SZ));
855 dma_buffer = NULL;
856 info->stat_flags |= ESP_STAT_USE_PIO;
857 }
Alan Coxfb100b62008-04-30 00:54:16 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
862
863 spin_lock_irqsave(&info->lock, flags);
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);
Alan Coxfb100b62008-04-30 00:54:16 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * Finally, enable interrupts
870 */
871 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
872 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
873 UART_IER_DMA_TC;
874 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
875 serial_out(info, UART_ESI_CMD2, info->IER);
Alan Coxfb100b62008-04-30 00:54:16 -0700876
Alan Cox4982d6b2008-07-16 21:55:11 +0100877 if (info->port.tty)
878 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
880 spin_unlock_irqrestore(&info->lock, flags);
881
882 /*
883 * Set up the tty->alt_speed kludge
884 */
Alan Cox4982d6b2008-07-16 21:55:11 +0100885 if (info->port.tty) {
886 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
887 info->port.tty->alt_speed = 57600;
888 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
889 info->port.tty->alt_speed = 115200;
890 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
891 info->port.tty->alt_speed = 230400;
892 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
893 info->port.tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Alan Coxfb100b62008-04-30 00:54:16 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * set the speed of the serial port
898 */
899 change_speed(info);
Alan Cox4982d6b2008-07-16 21:55:11 +0100900 info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
902
903out:
904 spin_unlock_irqrestore(&info->lock, flags);
905out_unlocked:
906 return retval;
907}
908
909/*
910 * This routine will shutdown a serial port; interrupts are disabled, and
911 * DTR is dropped if the hangup on close termio flag is on.
912 */
Alan Coxfb100b62008-04-30 00:54:16 -0700913static void shutdown(struct esp_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 unsigned long flags, f;
916
Alan Cox4982d6b2008-07-16 21:55:11 +0100917 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return;
919
920#ifdef SERIAL_DEBUG_OPEN
921 printk("Shutting down serial port %d (irq %d)....", info->line,
922 info->irq);
923#endif
Alan Coxfb100b62008-04-30 00:54:16 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 spin_lock_irqsave(&info->lock, flags);
926 /*
927 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
928 * here so the queue might never be waken up
929 */
930 wake_up_interruptible(&info->delta_msr_wait);
931 wake_up_interruptible(&info->break_wait);
932
933 /* stop a DMA transfer on the port being closed */
934 /* DMA lock is higher priority always */
935 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
Alan Coxfb100b62008-04-30 00:54:16 -0700936 f = claim_dma_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 disable_dma(dma);
938 clear_dma_ff(dma);
939 release_dma_lock(f);
Alan Coxfb100b62008-04-30 00:54:16 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 dma_bytes = 0;
942 }
Alan Coxfb100b62008-04-30 00:54:16 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * Free the IRQ
946 */
947 free_irq(info->irq, info);
948
949 if (dma_buffer) {
950 struct esp_struct *current_port = ports;
951
952 while (current_port) {
953 if ((current_port != info) &&
Alan Cox4982d6b2008-07-16 21:55:11 +0100954 (current_port->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956
957 current_port = current_port->next_port;
958 }
959
960 if (!current_port) {
961 free_dma(dma);
962 free_pages((unsigned long)dma_buffer,
963 get_order(DMA_BUFFER_SZ));
964 dma_buffer = NULL;
Alan Coxfb100b62008-04-30 00:54:16 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
968 if (info->xmit_buf) {
969 free_page((unsigned long) info->xmit_buf);
970 info->xmit_buf = NULL;
971 }
972
973 info->IER = 0;
974 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
975 serial_out(info, UART_ESI_CMD2, 0x00);
976
Alan Cox4982d6b2008-07-16 21:55:11 +0100977 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
979
980 info->MCR &= ~UART_MCR_OUT2;
981 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
982 serial_out(info, UART_ESI_CMD2, UART_MCR);
983 serial_out(info, UART_ESI_CMD2, info->MCR);
984
Alan Cox4982d6b2008-07-16 21:55:11 +0100985 if (info->port.tty)
986 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Alan Coxfb100b62008-04-30 00:54:16 -0700987
Alan Cox4982d6b2008-07-16 21:55:11 +0100988 info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 spin_unlock_irqrestore(&info->lock, flags);
990}
991
992/*
993 * This routine is called to set the UART divisor registers to match
994 * the specified baud rate for a serial port.
995 */
996static void change_speed(struct esp_struct *info)
997{
998 unsigned short port;
999 int quot = 0;
Alan Coxfb100b62008-04-30 00:54:16 -07001000 unsigned cflag, cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int baud, bits;
1002 unsigned char flow1 = 0, flow2 = 0;
1003 unsigned long flags;
1004
Alan Cox4982d6b2008-07-16 21:55:11 +01001005 if (!info->port.tty || !info->port.tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return;
Alan Cox4982d6b2008-07-16 21:55:11 +01001007 cflag = info->port.tty->termios->c_cflag;
1008 port = info->io_port;
Alan Coxfb100b62008-04-30 00:54:16 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* byte size and parity */
1011 switch (cflag & CSIZE) {
Alan Coxfb100b62008-04-30 00:54:16 -07001012 case CS5: cval = 0x00; bits = 7; break;
1013 case CS6: cval = 0x01; bits = 8; break;
1014 case CS7: cval = 0x02; bits = 9; break;
1015 case CS8: cval = 0x03; bits = 10; break;
1016 default: cval = 0x00; bits = 7; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 if (cflag & CSTOPB) {
1019 cval |= 0x04;
1020 bits++;
1021 }
1022 if (cflag & PARENB) {
1023 cval |= UART_LCR_PARITY;
1024 bits++;
1025 }
1026 if (!(cflag & PARODD))
1027 cval |= UART_LCR_EPAR;
1028#ifdef CMSPAR
1029 if (cflag & CMSPAR)
1030 cval |= UART_LCR_SPAR;
1031#endif
Alan Cox4982d6b2008-07-16 21:55:11 +01001032 baud = tty_get_baud_rate(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (baud == 38400 &&
Alan Cox4982d6b2008-07-16 21:55:11 +01001034 ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 quot = info->custom_divisor;
1036 else {
Alan Coxfb100b62008-04-30 00:54:16 -07001037 if (baud == 134) /* Special case since 134 is really 134.5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 quot = (2*BASE_BAUD / 269);
1039 else if (baud)
1040 quot = BASE_BAUD / baud;
1041 }
1042 /* If the quotient is ever zero, default to 9600 bps */
1043 if (!quot)
1044 quot = BASE_BAUD / 9600;
Alan Coxfb100b62008-04-30 00:54:16 -07001045
1046 if (baud) {
1047 /* Actual rate */
1048 baud = BASE_BAUD/quot;
Alan Cox4982d6b2008-07-16 21:55:11 +01001049 tty_encode_baud_rate(info->port.tty, baud, baud);
Alan Coxfb100b62008-04-30 00:54:16 -07001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1052
1053 /* CTS flow control flag and modem status interrupts */
1054 /* info->IER &= ~UART_IER_MSI; */
1055 if (cflag & CRTSCTS) {
Alan Cox4982d6b2008-07-16 21:55:11 +01001056 info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* info->IER |= UART_IER_MSI; */
1058 flow1 = 0x04;
1059 flow2 = 0x10;
1060 } else
Alan Cox4982d6b2008-07-16 21:55:11 +01001061 info->port.flags &= ~ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (cflag & CLOCAL)
Alan Cox4982d6b2008-07-16 21:55:11 +01001063 info->port.flags &= ~ASYNC_CHECK_CD;
Alan Coxfb100b62008-04-30 00:54:16 -07001064 else
Alan Cox4982d6b2008-07-16 21:55:11 +01001065 info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /*
1068 * Set up parity check flag
1069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox4982d6b2008-07-16 21:55:11 +01001071 if (I_INPCK(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox4982d6b2008-07-16 21:55:11 +01001073 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 info->read_status_mask |= UART_LSR_BI;
Alan Coxfb100b62008-04-30 00:54:16 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 info->ignore_status_mask = 0;
1077#if 0
1078 /* This should be safe, but for some broken bits of hardware... */
Alan Cox4982d6b2008-07-16 21:55:11 +01001079 if (I_IGNPAR(info->port.tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1081 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1082 }
1083#endif
Alan Cox4982d6b2008-07-16 21:55:11 +01001084 if (I_IGNBRK(info->port.tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 info->ignore_status_mask |= UART_LSR_BI;
1086 info->read_status_mask |= UART_LSR_BI;
1087 /*
Alan Coxfb100b62008-04-30 00:54:16 -07001088 * If we're ignore parity and break indicators, ignore
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * overruns too. (For real raw support).
1090 */
Alan Cox4982d6b2008-07-16 21:55:11 +01001091 if (I_IGNPAR(info->port.tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 info->ignore_status_mask |= UART_LSR_OE | \
1093 UART_LSR_PE | UART_LSR_FE;
1094 info->read_status_mask |= UART_LSR_OE | \
1095 UART_LSR_PE | UART_LSR_FE;
1096 }
1097 }
1098
Alan Cox4982d6b2008-07-16 21:55:11 +01001099 if (I_IXOFF(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 flow1 |= 0x81;
1101
1102 spin_lock_irqsave(&info->lock, flags);
1103 /* set baud */
1104 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1105 serial_out(info, UART_ESI_CMD2, quot >> 8);
1106 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1107
1108 /* set data bits, parity, etc. */
1109 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1110 serial_out(info, UART_ESI_CMD2, UART_LCR);
1111 serial_out(info, UART_ESI_CMD2, cval);
1112
1113 /* Enable flow control */
1114 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1115 serial_out(info, UART_ESI_CMD2, flow1);
1116 serial_out(info, UART_ESI_CMD2, flow2);
1117
1118 /* set flow control characters (XON/XOFF only) */
Alan Cox4982d6b2008-07-16 21:55:11 +01001119 if (I_IXOFF(info->port.tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
Alan Cox4982d6b2008-07-16 21:55:11 +01001121 serial_out(info, UART_ESI_CMD2, START_CHAR(info->port.tty));
1122 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->port.tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 serial_out(info, UART_ESI_CMD2, 0x10);
1124 serial_out(info, UART_ESI_CMD2, 0x21);
1125 switch (cflag & CSIZE) {
Alan Coxfb100b62008-04-30 00:54:16 -07001126 case CS5:
1127 serial_out(info, UART_ESI_CMD2, 0x1f);
1128 break;
1129 case CS6:
1130 serial_out(info, UART_ESI_CMD2, 0x3f);
1131 break;
1132 case CS7:
1133 case CS8:
1134 serial_out(info, UART_ESI_CMD2, 0x7f);
1135 break;
1136 default:
1137 serial_out(info, UART_ESI_CMD2, 0xff);
1138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 }
1141
1142 /* Set high/low water */
1143 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1144 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1145 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1146 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1147 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1148
1149 spin_unlock_irqrestore(&info->lock, flags);
1150}
1151
Alan Cox76b25a52008-04-30 00:54:03 -07001152static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Alan Coxfb100b62008-04-30 00:54:16 -07001154 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unsigned long flags;
Alan Cox76b25a52008-04-30 00:54:03 -07001156 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
Alan Cox76b25a52008-04-30 00:54:03 -07001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Eric Sesterhenna2f20c72006-06-25 05:48:46 -07001161 if (!info->xmit_buf)
Alan Cox76b25a52008-04-30 00:54:03 -07001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 spin_lock_irqsave(&info->lock, flags);
1165 if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
1166 info->xmit_buf[info->xmit_head++] = ch;
1167 info->xmit_head &= ESP_XMIT_SIZE-1;
1168 info->xmit_cnt++;
Alan Cox76b25a52008-04-30 00:54:03 -07001169 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox76b25a52008-04-30 00:54:03 -07001172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175static void rs_flush_chars(struct tty_struct *tty)
1176{
Alan Coxfb100b62008-04-30 00:54:16 -07001177 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1181 return;
1182
1183 spin_lock_irqsave(&info->lock, flags);
1184
1185 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1186 goto out;
1187
1188 if (!(info->IER & UART_IER_THRI)) {
1189 info->IER |= UART_IER_THRI;
1190 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1191 serial_out(info, UART_ESI_CMD2, info->IER);
1192 }
1193out:
1194 spin_unlock_irqrestore(&info->lock, flags);
1195}
1196
Alan Coxfb100b62008-04-30 00:54:16 -07001197static int rs_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 const unsigned char *buf, int count)
1199{
1200 int c, t, ret = 0;
Alan Coxfb100b62008-04-30 00:54:16 -07001201 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 unsigned long flags;
1203
1204 if (serial_paranoia_check(info, tty->name, "rs_write"))
1205 return 0;
1206
Eric Sesterhenna2f20c72006-06-25 05:48:46 -07001207 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
Alan Coxfb100b62008-04-30 00:54:16 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 while (1) {
1211 /* Thanks to R. Wolff for suggesting how to do this with */
1212 /* interrupts enabled */
1213
1214 c = count;
1215 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
Alan Coxfb100b62008-04-30 00:54:16 -07001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (t < c)
1218 c = t;
1219
1220 t = ESP_XMIT_SIZE - info->xmit_head;
Alan Coxfb100b62008-04-30 00:54:16 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (t < c)
1223 c = t;
1224
1225 if (c <= 0)
1226 break;
1227
1228 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1229
1230 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1231 info->xmit_cnt += c;
1232 buf += c;
1233 count -= c;
1234 ret += c;
1235 }
1236
1237 spin_lock_irqsave(&info->lock, flags);
1238
1239 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1240 info->IER |= UART_IER_THRI;
1241 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1242 serial_out(info, UART_ESI_CMD2, info->IER);
1243 }
1244
1245 spin_unlock_irqrestore(&info->lock, flags);
1246 return ret;
1247}
1248
1249static int rs_write_room(struct tty_struct *tty)
1250{
Alan Coxfb100b62008-04-30 00:54:16 -07001251 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int ret;
1253 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1256 return 0;
1257
1258 spin_lock_irqsave(&info->lock, flags);
1259
1260 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1261 if (ret < 0)
1262 ret = 0;
1263 spin_unlock_irqrestore(&info->lock, flags);
1264 return ret;
1265}
1266
1267static int rs_chars_in_buffer(struct tty_struct *tty)
1268{
Alan Coxfb100b62008-04-30 00:54:16 -07001269 struct esp_struct *info = tty->driver_data;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1272 return 0;
1273 return info->xmit_cnt;
1274}
1275
1276static void rs_flush_buffer(struct tty_struct *tty)
1277{
Alan Coxfb100b62008-04-30 00:54:16 -07001278 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -07001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1282 return;
1283 spin_lock_irqsave(&info->lock, flags);
1284 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1285 spin_unlock_irqrestore(&info->lock, flags);
1286 tty_wakeup(tty);
1287}
1288
1289/*
1290 * ------------------------------------------------------------
1291 * rs_throttle()
Alan Coxfb100b62008-04-30 00:54:16 -07001292 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * This routine is called by the upper-layer tty layer to signal that
1294 * incoming characters should be throttled.
1295 * ------------------------------------------------------------
1296 */
Alan Coxfb100b62008-04-30 00:54:16 -07001297static void rs_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Alan Coxfb100b62008-04-30 00:54:16 -07001299 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 unsigned long flags;
1301#ifdef SERIAL_DEBUG_THROTTLE
1302 char buf[64];
Alan Coxfb100b62008-04-30 00:54:16 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 printk("throttle %s: %d....\n", tty_name(tty, buf),
Alan Coxfb100b62008-04-30 00:54:16 -07001305 tty_chars_in_buffer(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306#endif
1307
1308 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1309 return;
1310
1311 spin_lock_irqsave(&info->lock, flags);
1312 info->IER &= ~UART_IER_RDI;
1313 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1314 serial_out(info, UART_ESI_CMD2, info->IER);
1315 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1316 serial_out(info, UART_ESI_CMD2, 0x00);
1317 spin_unlock_irqrestore(&info->lock, flags);
1318}
1319
Alan Coxfb100b62008-04-30 00:54:16 -07001320static void rs_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Alan Coxfb100b62008-04-30 00:54:16 -07001322 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned long flags;
1324#ifdef SERIAL_DEBUG_THROTTLE
1325 char buf[64];
Alan Coxfb100b62008-04-30 00:54:16 -07001326
1327 printk(KERN_DEBUG "unthrottle %s: %d....\n", tty_name(tty, buf),
1328 tty_chars_in_buffer(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329#endif
1330
1331 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1332 return;
Alan Coxfb100b62008-04-30 00:54:16 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 spin_lock_irqsave(&info->lock, flags);
1335 info->IER |= UART_IER_RDI;
1336 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1337 serial_out(info, UART_ESI_CMD2, info->IER);
1338 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1339 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1340 spin_unlock_irqrestore(&info->lock, flags);
1341}
1342
1343/*
1344 * ------------------------------------------------------------
1345 * rs_ioctl() and friends
1346 * ------------------------------------------------------------
1347 */
1348
Alan Coxfb100b62008-04-30 00:54:16 -07001349static int get_serial_info(struct esp_struct *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct serial_struct __user *retinfo)
1351{
1352 struct serial_struct tmp;
Alan Coxfb100b62008-04-30 00:54:16 -07001353
Alan Cox5a4bc8c12008-04-30 00:53:18 -07001354 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 memset(&tmp, 0, sizeof(tmp));
1356 tmp.type = PORT_16550A;
1357 tmp.line = info->line;
Alan Cox4982d6b2008-07-16 21:55:11 +01001358 tmp.port = info->io_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 tmp.irq = info->irq;
Alan Cox4982d6b2008-07-16 21:55:11 +01001360 tmp.flags = info->port.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 tmp.xmit_fifo_size = 1024;
1362 tmp.baud_base = BASE_BAUD;
1363 tmp.close_delay = info->close_delay;
1364 tmp.closing_wait = info->closing_wait;
1365 tmp.custom_divisor = info->custom_divisor;
1366 tmp.hub6 = 0;
Alan Cox5a4bc8c12008-04-30 00:53:18 -07001367 unlock_kernel();
Alan Coxfb100b62008-04-30 00:54:16 -07001368 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EFAULT;
1370 return 0;
1371}
1372
Alan Coxfb100b62008-04-30 00:54:16 -07001373static int get_esp_config(struct esp_struct *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 struct hayes_esp_config __user *retinfo)
1375{
1376 struct hayes_esp_config tmp;
Alan Coxfb100b62008-04-30 00:54:16 -07001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!retinfo)
1379 return -EFAULT;
1380
1381 memset(&tmp, 0, sizeof(tmp));
Alan Cox5a4bc8c12008-04-30 00:53:18 -07001382 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 tmp.rx_timeout = info->config.rx_timeout;
1384 tmp.rx_trigger = info->config.rx_trigger;
1385 tmp.tx_trigger = info->config.tx_trigger;
1386 tmp.flow_off = info->config.flow_off;
1387 tmp.flow_on = info->config.flow_on;
1388 tmp.pio_threshold = info->config.pio_threshold;
1389 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
Alan Cox5a4bc8c12008-04-30 00:53:18 -07001390 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1393}
1394
Alan Coxfb100b62008-04-30 00:54:16 -07001395static int set_serial_info(struct esp_struct *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct serial_struct __user *new_info)
1397{
1398 struct serial_struct new_serial;
1399 struct esp_struct old_info;
1400 unsigned int change_irq;
1401 int retval = 0;
1402 struct esp_struct *current_async;
1403
Alan Coxfb100b62008-04-30 00:54:16 -07001404 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -EFAULT;
1406 old_info = *info;
1407
1408 if ((new_serial.type != PORT_16550A) ||
1409 (new_serial.hub6) ||
Alan Cox4982d6b2008-07-16 21:55:11 +01001410 (info->io_port != new_serial.port) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 (new_serial.baud_base != BASE_BAUD) ||
1412 (new_serial.irq > 15) ||
1413 (new_serial.irq < 2) ||
1414 (new_serial.irq == 6) ||
1415 (new_serial.irq == 8) ||
1416 (new_serial.irq == 13))
1417 return -EINVAL;
1418
1419 change_irq = new_serial.irq != info->irq;
1420
1421 if (change_irq && (info->line % 8))
1422 return -EINVAL;
1423
1424 if (!capable(CAP_SYS_ADMIN)) {
Alan Coxfb100b62008-04-30 00:54:16 -07001425 if (change_irq ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 (new_serial.close_delay != info->close_delay) ||
1427 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox4982d6b2008-07-16 21:55:11 +01001428 (info->port.flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EPERM;
Alan Cox4982d6b2008-07-16 21:55:11 +01001430 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 (new_serial.flags & ASYNC_USR_MASK));
1432 info->custom_divisor = new_serial.custom_divisor;
1433 } else {
1434 if (new_serial.irq == 2)
1435 new_serial.irq = 9;
1436
1437 if (change_irq) {
1438 current_async = ports;
1439
1440 while (current_async) {
1441 if ((current_async->line >= info->line) &&
1442 (current_async->line < (info->line + 8))) {
1443 if (current_async == info) {
Alan Cox4982d6b2008-07-16 21:55:11 +01001444 if (current_async->port.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EBUSY;
Alan Cox4982d6b2008-07-16 21:55:11 +01001446 } else if (current_async->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EBUSY;
1448 }
1449
1450 current_async = current_async->next_port;
1451 }
1452 }
1453
1454 /*
1455 * OK, past this point, all the error checking has been done.
1456 * At this point, we start making changes.....
1457 */
1458
Alan Cox4982d6b2008-07-16 21:55:11 +01001459 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 (new_serial.flags & ASYNC_FLAGS));
1461 info->custom_divisor = new_serial.custom_divisor;
1462 info->close_delay = new_serial.close_delay * HZ/100;
1463 info->closing_wait = new_serial.closing_wait * HZ/100;
1464
1465 if (change_irq) {
1466 /*
1467 * We need to shutdown the serial port at the old
1468 * port/irq combination.
1469 */
1470 shutdown(info);
1471
1472 current_async = ports;
1473
1474 while (current_async) {
1475 if ((current_async->line >= info->line) &&
1476 (current_async->line < (info->line + 8)))
1477 current_async->irq = new_serial.irq;
1478
1479 current_async = current_async->next_port;
1480 }
1481
1482 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1483 if (info->irq == 9)
1484 serial_out(info, UART_ESI_CMD2, 0x02);
1485 else
1486 serial_out(info, UART_ESI_CMD2, info->irq);
1487 }
1488 }
1489
Alan Cox4982d6b2008-07-16 21:55:11 +01001490 if (info->port.flags & ASYNC_INITIALIZED) {
1491 if (((old_info.port.flags & ASYNC_SPD_MASK) !=
1492 (info->port.flags & ASYNC_SPD_MASK)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 (old_info.custom_divisor != info->custom_divisor)) {
Alan Cox4982d6b2008-07-16 21:55:11 +01001494 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1495 info->port.tty->alt_speed = 57600;
1496 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1497 info->port.tty->alt_speed = 115200;
1498 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1499 info->port.tty->alt_speed = 230400;
1500 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1501 info->port.tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 change_speed(info);
1503 }
1504 } else
1505 retval = startup(info);
1506
1507 return retval;
1508}
1509
Alan Coxfb100b62008-04-30 00:54:16 -07001510static int set_esp_config(struct esp_struct *info,
1511 struct hayes_esp_config __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct hayes_esp_config new_config;
1514 unsigned int change_dma;
1515 int retval = 0;
1516 struct esp_struct *current_async;
1517 unsigned long flags;
1518
1519 /* Perhaps a non-sysadmin user should be able to do some of these */
1520 /* operations. I haven't decided yet. */
1521
1522 if (!capable(CAP_SYS_ADMIN))
1523 return -EPERM;
1524
1525 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1526 return -EFAULT;
1527
1528 if ((new_config.flow_on >= new_config.flow_off) ||
1529 (new_config.rx_trigger < 1) ||
1530 (new_config.tx_trigger < 1) ||
1531 (new_config.flow_off < 1) ||
1532 (new_config.flow_on < 1) ||
1533 (new_config.rx_trigger > 1023) ||
1534 (new_config.tx_trigger > 1023) ||
1535 (new_config.flow_off > 1023) ||
1536 (new_config.flow_on > 1023) ||
1537 (new_config.pio_threshold < 0) ||
1538 (new_config.pio_threshold > 1024))
1539 return -EINVAL;
1540
1541 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1542 new_config.dma_channel = 0;
1543
1544 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1545 change_dma = new_config.dma_channel;
1546 else
1547 change_dma = (new_config.dma_channel != dma);
1548
1549 if (change_dma) {
1550 if (new_config.dma_channel) {
1551 /* PIO mode to DMA mode transition OR */
1552 /* change current DMA channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 current_async = ports;
1554
1555 while (current_async) {
1556 if (current_async == info) {
Alan Cox4982d6b2008-07-16 21:55:11 +01001557 if (current_async->port.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EBUSY;
Alan Cox4982d6b2008-07-16 21:55:11 +01001559 } else if (current_async->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EBUSY;
Alan Coxfb100b62008-04-30 00:54:16 -07001561
1562 current_async = current_async->next_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564
1565 shutdown(info);
1566 dma = new_config.dma_channel;
1567 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
Alan Coxfb100b62008-04-30 00:54:16 -07001568
1569 /* all ports must use the same DMA channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 spin_lock_irqsave(&info->lock, flags);
1572 current_async = ports;
1573
1574 while (current_async) {
1575 esp_basic_init(current_async);
1576 current_async = current_async->next_port;
1577 }
1578 spin_unlock_irqrestore(&info->lock, flags);
1579 } else {
1580 /* DMA mode to PIO mode only */
Alan Cox4982d6b2008-07-16 21:55:11 +01001581 if (info->port.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return -EBUSY;
1583
1584 shutdown(info);
1585 spin_lock_irqsave(&info->lock, flags);
1586 info->stat_flags |= ESP_STAT_NEVER_DMA;
1587 esp_basic_init(info);
1588 spin_unlock_irqrestore(&info->lock, flags);
1589 }
1590 }
1591
1592 info->config.pio_threshold = new_config.pio_threshold;
1593
1594 if ((new_config.flow_off != info->config.flow_off) ||
1595 (new_config.flow_on != info->config.flow_on)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 info->config.flow_off = new_config.flow_off;
1597 info->config.flow_on = new_config.flow_on;
1598
1599 spin_lock_irqsave(&info->lock, flags);
1600 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1601 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1602 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1603 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1604 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1605 spin_unlock_irqrestore(&info->lock, flags);
1606 }
1607
1608 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1609 (new_config.tx_trigger != info->config.tx_trigger)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 info->config.rx_trigger = new_config.rx_trigger;
1611 info->config.tx_trigger = new_config.tx_trigger;
1612 spin_lock_irqsave(&info->lock, flags);
1613 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1614 serial_out(info, UART_ESI_CMD2,
1615 new_config.rx_trigger >> 8);
1616 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1617 serial_out(info, UART_ESI_CMD2,
1618 new_config.tx_trigger >> 8);
1619 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1620 spin_unlock_irqrestore(&info->lock, flags);
1621 }
1622
1623 if (new_config.rx_timeout != info->config.rx_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 info->config.rx_timeout = new_config.rx_timeout;
1625 spin_lock_irqsave(&info->lock, flags);
1626
1627 if (info->IER & UART_IER_RDI) {
1628 serial_out(info, UART_ESI_CMD1,
1629 ESI_SET_RX_TIMEOUT);
1630 serial_out(info, UART_ESI_CMD2,
1631 new_config.rx_timeout);
1632 }
1633
1634 spin_unlock_irqrestore(&info->lock, flags);
1635 }
1636
Alan Cox4982d6b2008-07-16 21:55:11 +01001637 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 retval = startup(info);
1639
1640 return retval;
1641}
1642
1643/*
1644 * get_lsr_info - get line status register info
1645 *
1646 * Purpose: Let user call ioctl() to get info when the UART physically
1647 * is emptied. On bus types like RS485, the transmitter must
1648 * release the bus after transmitting. This must be done when
1649 * the transmit shift register is empty, not be done when the
1650 * transmit holding register is empty. This functionality
Alan Coxfb100b62008-04-30 00:54:16 -07001651 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
Alan Coxfb100b62008-04-30 00:54:16 -07001653static int get_lsr_info(struct esp_struct *info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 unsigned char status;
1656 unsigned int result;
1657 unsigned long flags;
1658
1659 spin_lock_irqsave(&info->lock, flags);
1660 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1661 status = serial_in(info, UART_ESI_STAT1);
1662 spin_unlock_irqrestore(&info->lock, flags);
1663 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
Alan Coxfb100b62008-04-30 00:54:16 -07001664 return put_user(result, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667
1668static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1669{
Alan Coxfb100b62008-04-30 00:54:16 -07001670 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 unsigned char control, status;
1672 unsigned long flags;
1673
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001674 if (serial_paranoia_check(info, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -ENODEV;
1676 if (tty->flags & (1 << TTY_IO_ERROR))
1677 return -EIO;
1678
1679 control = info->MCR;
1680
1681 spin_lock_irqsave(&info->lock, flags);
1682 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1683 status = serial_in(info, UART_ESI_STAT2);
1684 spin_unlock_irqrestore(&info->lock, flags);
1685
1686 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1687 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1688 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1689 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1690 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1691 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1692}
1693
1694static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1695 unsigned int set, unsigned int clear)
1696{
Alan Coxfb100b62008-04-30 00:54:16 -07001697 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 unsigned long flags;
1699
Harvey Harrisonbf9d8922008-04-30 00:55:10 -07001700 if (serial_paranoia_check(info, tty->name, __func__))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return -ENODEV;
1702 if (tty->flags & (1 << TTY_IO_ERROR))
1703 return -EIO;
1704
1705 spin_lock_irqsave(&info->lock, flags);
1706
1707 if (set & TIOCM_RTS)
1708 info->MCR |= UART_MCR_RTS;
1709 if (set & TIOCM_DTR)
1710 info->MCR |= UART_MCR_DTR;
1711
1712 if (clear & TIOCM_RTS)
1713 info->MCR &= ~UART_MCR_RTS;
1714 if (clear & TIOCM_DTR)
1715 info->MCR &= ~UART_MCR_DTR;
1716
1717 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1718 serial_out(info, UART_ESI_CMD2, UART_MCR);
1719 serial_out(info, UART_ESI_CMD2, info->MCR);
1720
1721 spin_unlock_irqrestore(&info->lock, flags);
1722 return 0;
1723}
1724
1725/*
1726 * rs_break() --- routine which turns the break handling on or off
1727 */
Alan Cox9e989662008-07-22 11:18:03 +01001728static int esp_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Alan Coxfb100b62008-04-30 00:54:16 -07001730 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 unsigned long flags;
Alan Coxfb100b62008-04-30 00:54:16 -07001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (serial_paranoia_check(info, tty->name, "esp_break"))
Alan Cox9e989662008-07-22 11:18:03 +01001734 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 if (break_state == -1) {
1737 spin_lock_irqsave(&info->lock, flags);
1738 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1739 serial_out(info, UART_ESI_CMD2, 0x01);
1740 spin_unlock_irqrestore(&info->lock, flags);
1741
1742 /* FIXME - new style wait needed here */
1743 interruptible_sleep_on(&info->break_wait);
1744 } else {
1745 spin_lock_irqsave(&info->lock, flags);
1746 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1747 serial_out(info, UART_ESI_CMD2, 0x00);
1748 spin_unlock_irqrestore(&info->lock, flags);
1749 }
Alan Cox9e989662008-07-22 11:18:03 +01001750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
Alan Coxfb100b62008-04-30 00:54:16 -07001753static int rs_ioctl(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 unsigned int cmd, unsigned long arg)
1755{
Alan Coxfb100b62008-04-30 00:54:16 -07001756 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct async_icount cprev, cnow; /* kernel counter temps */
1758 struct serial_icounter_struct __user *p_cuser; /* user space */
1759 void __user *argp = (void __user *)arg;
1760 unsigned long flags;
Alan Cox5a4bc8c12008-04-30 00:53:18 -07001761 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1764 return -ENODEV;
1765
1766 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1767 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1768 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1769 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1770 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1771 if (tty->flags & (1 << TTY_IO_ERROR))
1772 return -EIO;
1773 }
Alan Coxfb100b62008-04-30 00:54:16 -07001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 switch (cmd) {
Alan Coxfb100b62008-04-30 00:54:16 -07001776 case TIOCGSERIAL:
1777 return get_serial_info(info, argp);
1778 case TIOCSSERIAL:
1779 lock_kernel();
1780 ret = set_serial_info(info, argp);
1781 unlock_kernel();
1782 return ret;
1783 case TIOCSERGWILD:
1784 return put_user(0L, (unsigned long __user *)argp);
1785 case TIOCSERGETLSR: /* Get line status register */
1786 return get_lsr_info(info, argp);
1787 case TIOCSERSWILD:
1788 if (!capable(CAP_SYS_ADMIN))
1789 return -EPERM;
1790 return 0;
1791 /*
1792 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1793 * - mask passed in arg for lines of interest
1794 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1795 * Caller should use TIOCGICOUNT to see which one it was
1796 */
1797 case TIOCMIWAIT:
1798 spin_lock_irqsave(&info->lock, flags);
1799 cprev = info->icount; /* note the counters on entry */
1800 spin_unlock_irqrestore(&info->lock, flags);
1801 while (1) {
1802 /* FIXME: convert to new style wakeup */
1803 interruptible_sleep_on(&info->delta_msr_wait);
1804 /* see if a signal did it */
1805 if (signal_pending(current))
1806 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 spin_lock_irqsave(&info->lock, flags);
Alan Coxfb100b62008-04-30 00:54:16 -07001808 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 spin_unlock_irqrestore(&info->lock, flags);
Alan Coxfb100b62008-04-30 00:54:16 -07001810 if (cnow.rng == cprev.rng &&
1811 cnow.dsr == cprev.dsr &&
1812 cnow.dcd == cprev.dcd &&
1813 cnow.cts == cprev.cts)
1814 return -EIO; /* no change => error */
1815 if (((arg & TIOCM_RNG) &&
1816 (cnow.rng != cprev.rng)) ||
1817 ((arg & TIOCM_DSR) &&
1818 (cnow.dsr != cprev.dsr)) ||
1819 ((arg & TIOCM_CD) &&
1820 (cnow.dcd != cprev.dcd)) ||
1821 ((arg & TIOCM_CTS) &&
1822 (cnow.cts != cprev.cts))) {
1823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
Alan Coxfb100b62008-04-30 00:54:16 -07001825 cprev = cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Alan Coxfb100b62008-04-30 00:54:16 -07001827 /* NOTREACHED */
1828 /*
1829 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1830 * Return: write counters to the user passed counter struct
1831 * NB: both 1->0 and 0->1 transitions are counted except for
1832 * RI where only 0->1 is counted.
1833 */
1834 case TIOCGICOUNT:
1835 spin_lock_irqsave(&info->lock, flags);
1836 cnow = info->icount;
1837 spin_unlock_irqrestore(&info->lock, flags);
1838 p_cuser = argp;
1839 if (put_user(cnow.cts, &p_cuser->cts) ||
1840 put_user(cnow.dsr, &p_cuser->dsr) ||
1841 put_user(cnow.rng, &p_cuser->rng) ||
1842 put_user(cnow.dcd, &p_cuser->dcd))
1843 return -EFAULT;
1844 return 0;
1845 case TIOCGHAYESESP:
1846 return get_esp_config(info, argp);
1847 case TIOCSHAYESESP:
1848 lock_kernel();
1849 ret = set_esp_config(info, argp);
1850 unlock_kernel();
1851 return ret;
1852 default:
1853 return -ENOIOCTLCMD;
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return 0;
1856}
1857
Alan Cox606d0992006-12-08 02:38:45 -08001858static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Alan Coxfb100b62008-04-30 00:54:16 -07001860 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 unsigned long flags;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 change_speed(info);
1864
1865 spin_lock_irqsave(&info->lock, flags);
1866
1867 /* Handle transition to B0 status */
1868 if ((old_termios->c_cflag & CBAUD) &&
1869 !(tty->termios->c_cflag & CBAUD)) {
1870 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1871 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1872 serial_out(info, UART_ESI_CMD2, UART_MCR);
1873 serial_out(info, UART_ESI_CMD2, info->MCR);
1874 }
1875
1876 /* Handle transition away from B0 status */
1877 if (!(old_termios->c_cflag & CBAUD) &&
1878 (tty->termios->c_cflag & CBAUD)) {
1879 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1880 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1881 serial_out(info, UART_ESI_CMD2, UART_MCR);
1882 serial_out(info, UART_ESI_CMD2, info->MCR);
1883 }
1884
1885 spin_unlock_irqrestore(&info->lock, flags);
1886
1887 /* Handle turning of CRTSCTS */
1888 if ((old_termios->c_cflag & CRTSCTS) &&
1889 !(tty->termios->c_cflag & CRTSCTS)) {
1890 rs_start(tty);
1891 }
1892}
1893
1894/*
1895 * ------------------------------------------------------------
1896 * rs_close()
Alan Coxfb100b62008-04-30 00:54:16 -07001897 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 * This routine is called when the serial port gets closed. First, we
1899 * wait for the last remaining data to be sent. Then, we unlink its
1900 * async structure from the interrupt chain if necessary, and we free
1901 * that IRQ if nothing is left in the chain.
1902 * ------------------------------------------------------------
1903 */
Alan Coxfb100b62008-04-30 00:54:16 -07001904static void rs_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Alan Coxfb100b62008-04-30 00:54:16 -07001906 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 unsigned long flags;
1908
1909 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1910 return;
Alan Coxfb100b62008-04-30 00:54:16 -07001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 spin_lock_irqsave(&info->lock, flags);
Alan Coxfb100b62008-04-30 00:54:16 -07001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (tty_hung_up_p(filp)) {
1915 DBG_CNT("before DEC-hung");
1916 goto out;
1917 }
Alan Coxfb100b62008-04-30 00:54:16 -07001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -07001920 printk(KERN_DEBUG "rs_close ttys%d, count = %d\n",
Alan Cox4982d6b2008-07-16 21:55:11 +01001921 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922#endif
Alan Cox4982d6b2008-07-16 21:55:11 +01001923 if (tty->count == 1 && info->port.count != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /*
1925 * Uh, oh. tty->count is 1, which means that the tty
1926 * structure will be freed. Info->count should always
1927 * be one in these conditions. If it's greater than
1928 * one, we've got real problems, since it means the
1929 * serial port won't be shutdown.
1930 */
Alan Cox4982d6b2008-07-16 21:55:11 +01001931 printk(KERN_DEBUG "rs_close: bad serial port count; tty->count is 1, info->port.count is %d\n", info->port.count);
1932 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Alan Cox4982d6b2008-07-16 21:55:11 +01001934 if (--info->port.count < 0) {
Alan Coxfb100b62008-04-30 00:54:16 -07001935 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
Alan Cox4982d6b2008-07-16 21:55:11 +01001936 info->line, info->port.count);
1937 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Alan Cox4982d6b2008-07-16 21:55:11 +01001939 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 DBG_CNT("before DEC-2");
1941 goto out;
1942 }
Alan Cox4982d6b2008-07-16 21:55:11 +01001943 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 spin_unlock_irqrestore(&info->lock, flags);
1946 /*
Alan Coxfb100b62008-04-30 00:54:16 -07001947 * Now we wait for the transmit buffer to clear; and we notify
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * the line discipline to only process XON/XOFF characters.
1949 */
1950 tty->closing = 1;
1951 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1952 tty_wait_until_sent(tty, info->closing_wait);
1953 /*
1954 * At this point we stop accepting input. To do this, we
1955 * disable the receive line status interrupts, and tell the
1956 * interrupt driver to stop checking the data ready bit in the
1957 * line status register.
1958 */
1959 /* info->IER &= ~UART_IER_RLSI; */
1960 info->IER &= ~UART_IER_RDI;
1961 info->read_status_mask &= ~UART_LSR_DR;
Alan Cox4982d6b2008-07-16 21:55:11 +01001962 if (info->port.flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 spin_lock_irqsave(&info->lock, flags);
1965 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1966 serial_out(info, UART_ESI_CMD2, info->IER);
1967
1968 /* disable receive timeout */
1969 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1970 serial_out(info, UART_ESI_CMD2, 0x00);
1971
1972 spin_unlock_irqrestore(&info->lock, flags);
1973
1974 /*
1975 * Before we drop DTR, make sure the UART transmitter
1976 * has completely drained; this is especially
1977 * important if there is a transmit FIFO!
1978 */
1979 rs_wait_until_sent(tty, info->timeout);
1980 }
1981 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001982 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 tty_ldisc_flush(tty);
1984 tty->closing = 0;
Alan Cox4982d6b2008-07-16 21:55:11 +01001985 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Alan Cox4982d6b2008-07-16 21:55:11 +01001987 if (info->port.blocked_open) {
Alan Coxfb100b62008-04-30 00:54:16 -07001988 if (info->close_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 msleep_interruptible(jiffies_to_msecs(info->close_delay));
Alan Cox4982d6b2008-07-16 21:55:11 +01001990 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Alan Cox4982d6b2008-07-16 21:55:11 +01001992 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1993 wake_up_interruptible(&info->port.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return;
1995
1996out:
1997 spin_unlock_irqrestore(&info->lock, flags);
1998}
1999
2000static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2001{
Alan Coxfb100b62008-04-30 00:54:16 -07002002 struct esp_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned long orig_jiffies, char_time;
2004 unsigned long flags;
2005
2006 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2007 return;
2008
2009 orig_jiffies = jiffies;
2010 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2011
2012 if (!char_time)
2013 char_time = 1;
2014
2015 spin_lock_irqsave(&info->lock, flags);
2016 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2017 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2018
2019 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2020 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2021
2022 spin_unlock_irqrestore(&info->lock, flags);
2023 msleep_interruptible(jiffies_to_msecs(char_time));
2024
2025 if (signal_pending(current))
Harvey Harrisond3ceb652008-04-30 00:53:50 -07002026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (timeout && time_after(jiffies, orig_jiffies + timeout))
Harvey Harrisond3ceb652008-04-30 00:53:50 -07002029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 spin_lock_irqsave(&info->lock, flags);
2032 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2033 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2034 }
2035 spin_unlock_irqrestore(&info->lock, flags);
2036 set_current_state(TASK_RUNNING);
2037}
2038
2039/*
2040 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2041 */
2042static void esp_hangup(struct tty_struct *tty)
2043{
Alan Coxfb100b62008-04-30 00:54:16 -07002044 struct esp_struct *info = tty->driver_data;
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2047 return;
Alan Coxfb100b62008-04-30 00:54:16 -07002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 rs_flush_buffer(tty);
2050 shutdown(info);
Alan Cox4982d6b2008-07-16 21:55:11 +01002051 info->port.count = 0;
2052 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2053 info->port.tty = NULL;
2054 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Alan Cox31f35932009-01-02 13:45:05 +00002057static int esp_carrier_raised(struct tty_port *port)
2058{
2059 struct esp_struct *info = container_of(port, struct esp_struct, port);
2060 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2061 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2062 return 1;
2063 return 0;
2064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066/*
2067 * ------------------------------------------------------------
2068 * esp_open() and friends
2069 * ------------------------------------------------------------
2070 */
Alan Coxfb100b62008-04-30 00:54:16 -07002071static int block_til_ready(struct tty_struct *tty, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct esp_struct *info)
2073{
2074 DECLARE_WAITQUEUE(wait, current);
2075 int retval;
2076 int do_clocal = 0;
2077 unsigned long flags;
Alan Cox31f35932009-01-02 13:45:05 +00002078 int cd;
2079 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /*
2082 * If the device is in the middle of being closed, then block
2083 * until it's done, and then try again.
2084 */
2085 if (tty_hung_up_p(filp) ||
Alan Cox31f35932009-01-02 13:45:05 +00002086 (port->flags & ASYNC_CLOSING)) {
2087 if (port->flags & ASYNC_CLOSING)
2088 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089#ifdef SERIAL_DO_RESTART
Alan Cox31f35932009-01-02 13:45:05 +00002090 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return -EAGAIN;
2092 else
2093 return -ERESTARTSYS;
2094#else
2095 return -EAGAIN;
2096#endif
2097 }
2098
2099 /*
2100 * If non-blocking mode is set, or the port is not enabled,
2101 * then make the check up front and then exit.
2102 */
2103 if ((filp->f_flags & O_NONBLOCK) ||
2104 (tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox31f35932009-01-02 13:45:05 +00002105 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return 0;
2107 }
2108
2109 if (tty->termios->c_cflag & CLOCAL)
2110 do_clocal = 1;
2111
2112 /*
2113 * Block waiting for the carrier detect and the line to become
2114 * free (i.e., not in use by the callout). While we are in
Alan Cox31f35932009-01-02 13:45:05 +00002115 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * rs_close() knows when to free things. We restore it upon
2117 * exit, either normal or abnormal.
2118 */
2119 retval = 0;
Alan Cox31f35932009-01-02 13:45:05 +00002120 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -07002122 printk(KERN_DEBUG "block_til_ready before block: ttys%d, count = %d\n",
Alan Cox31f35932009-01-02 13:45:05 +00002123 info->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#endif
2125 spin_lock_irqsave(&info->lock, flags);
Alan Coxfb100b62008-04-30 00:54:16 -07002126 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +00002127 port->count--;
2128 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 while (1) {
2130 if ((tty->termios->c_cflag & CBAUD)) {
2131 unsigned int scratch;
2132
2133 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2134 serial_out(info, UART_ESI_CMD2, UART_MCR);
2135 scratch = serial_in(info, UART_ESI_STAT1);
2136 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2137 serial_out(info, UART_ESI_CMD2, UART_MCR);
2138 serial_out(info, UART_ESI_CMD2,
2139 scratch | UART_MCR_DTR | UART_MCR_RTS);
2140 }
2141 set_current_state(TASK_INTERRUPTIBLE);
2142 if (tty_hung_up_p(filp) ||
Alan Cox31f35932009-01-02 13:45:05 +00002143 !(port->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144#ifdef SERIAL_DO_RESTART
Alan Cox31f35932009-01-02 13:45:05 +00002145 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 retval = -EAGAIN;
2147 else
Alan Coxfb100b62008-04-30 00:54:16 -07002148 retval = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149#else
2150 retval = -EAGAIN;
2151#endif
2152 break;
2153 }
2154
Alan Cox31f35932009-01-02 13:45:05 +00002155 cd = tty_port_carrier_raised(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Alan Cox31f35932009-01-02 13:45:05 +00002157 if (!(port->flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 (do_clocal))
2159 break;
2160 if (signal_pending(current)) {
2161 retval = -ERESTARTSYS;
2162 break;
2163 }
2164#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -07002165 printk(KERN_DEBUG "block_til_ready blocking: ttys%d, count = %d\n",
Alan Cox31f35932009-01-02 13:45:05 +00002166 info->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#endif
2168 spin_unlock_irqrestore(&info->lock, flags);
2169 schedule();
2170 spin_lock_irqsave(&info->lock, flags);
2171 }
2172 set_current_state(TASK_RUNNING);
Alan Cox31f35932009-01-02 13:45:05 +00002173 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +00002175 port->count++;
2176 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 spin_unlock_irqrestore(&info->lock, flags);
2178#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -07002179 printk(KERN_DEBUG "block_til_ready after blocking: ttys%d, count = %d\n",
Alan Cox31f35932009-01-02 13:45:05 +00002180 info->line, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181#endif
2182 if (retval)
2183 return retval;
Alan Cox31f35932009-01-02 13:45:05 +00002184 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return 0;
Alan Coxfb100b62008-04-30 00:54:16 -07002186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188/*
2189 * This routine is called whenever a serial port is opened. It
2190 * enables interrupts for a serial port, linking in its async structure into
2191 * the IRQ chain. It also performs the serial-specific
2192 * initialization for the tty structure.
2193 */
Alan Coxfb100b62008-04-30 00:54:16 -07002194static int esp_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct esp_struct *info;
2197 int retval, line;
2198 unsigned long flags;
2199
2200 line = tty->index;
2201 if ((line < 0) || (line >= NR_PORTS))
2202 return -ENODEV;
2203
2204 /* find the port in the chain */
2205
2206 info = ports;
2207
2208 while (info && (info->line != line))
2209 info = info->next_port;
2210
2211 if (!info) {
2212 serial_paranoia_check(info, tty->name, "esp_open");
2213 return -ENODEV;
2214 }
2215
2216#ifdef SERIAL_DEBUG_OPEN
Alan Cox4982d6b2008-07-16 21:55:11 +01002217 printk(KERN_DEBUG "esp_open %s, count = %d\n", tty->name, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218#endif
2219 spin_lock_irqsave(&info->lock, flags);
Alan Cox4982d6b2008-07-16 21:55:11 +01002220 info->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 tty->driver_data = info;
Alan Cox4982d6b2008-07-16 21:55:11 +01002222 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Alan Cox5552c282006-02-14 17:50:54 +00002224 spin_unlock_irqrestore(&info->lock, flags);
Alan Coxfb100b62008-04-30 00:54:16 -07002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * Start up serial port
2228 */
2229 retval = startup(info);
2230 if (retval)
2231 return retval;
2232
2233 retval = block_til_ready(tty, filp, info);
2234 if (retval) {
2235#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -07002236 printk(KERN_DEBUG "esp_open returning after block_til_ready with %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 retval);
2238#endif
2239 return retval;
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241#ifdef SERIAL_DEBUG_OPEN
Alan Coxfb100b62008-04-30 00:54:16 -07002242 printk(KERN_DEBUG "esp_open %s successful...", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243#endif
2244 return 0;
2245}
2246
2247/*
2248 * ---------------------------------------------------------------------
2249 * espserial_init() and friends
2250 *
2251 * espserial_init() is called at boot-time to initialize the serial driver.
2252 * ---------------------------------------------------------------------
2253 */
2254
2255/*
2256 * This routine prints out the appropriate serial driver version
2257 * number, and identifies which options were configured into this
2258 * driver.
2259 */
Alan Coxfb100b62008-04-30 00:54:16 -07002260
Randy Dunlap9133df72009-04-06 19:00:26 -07002261static void __init show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Alan Coxfb100b62008-04-30 00:54:16 -07002263 printk(KERN_INFO "%s version %s (DMA %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 serial_name, serial_version, dma);
2265}
2266
2267/*
2268 * This routine is called by espserial_init() to initialize a specific serial
2269 * port.
2270 */
Alan Coxfb100b62008-04-30 00:54:16 -07002271static int autoconfig(struct esp_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 int port_detected = 0;
2274 unsigned long flags;
2275
Alan Cox4982d6b2008-07-16 21:55:11 +01002276 if (!request_region(info->io_port, REGION_SIZE, "esp serial"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return -EIO;
2278
2279 spin_lock_irqsave(&info->lock, flags);
2280 /*
2281 * Check for ESP card
2282 */
2283
2284 if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2285 serial_out(info, UART_ESI_CMD1, 0x00);
2286 serial_out(info, UART_ESI_CMD1, 0x01);
2287
2288 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2289 port_detected = 1;
2290
2291 if (!(info->irq)) {
2292 serial_out(info, UART_ESI_CMD1, 0x02);
2293
2294 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2295 info->irq = 3;
2296 else
2297 info->irq = 4;
2298 }
2299
2300
2301 /* put card in enhanced mode */
2302 /* this prevents access through */
2303 /* the "old" IO ports */
2304 esp_basic_init(info);
2305
2306 /* clear out MCR */
2307 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2308 serial_out(info, UART_ESI_CMD2, UART_MCR);
2309 serial_out(info, UART_ESI_CMD2, 0x00);
2310 }
2311 }
2312 if (!port_detected)
Alan Cox4982d6b2008-07-16 21:55:11 +01002313 release_region(info->io_port, REGION_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 spin_unlock_irqrestore(&info->lock, flags);
2316 return (port_detected);
2317}
2318
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002319static const struct tty_operations esp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 .open = esp_open,
2321 .close = rs_close,
2322 .write = rs_write,
2323 .put_char = rs_put_char,
2324 .flush_chars = rs_flush_chars,
2325 .write_room = rs_write_room,
2326 .chars_in_buffer = rs_chars_in_buffer,
2327 .flush_buffer = rs_flush_buffer,
2328 .ioctl = rs_ioctl,
2329 .throttle = rs_throttle,
2330 .unthrottle = rs_unthrottle,
2331 .set_termios = rs_set_termios,
2332 .stop = rs_stop,
2333 .start = rs_start,
2334 .hangup = esp_hangup,
2335 .break_ctl = esp_break,
2336 .wait_until_sent = rs_wait_until_sent,
2337 .tiocmget = esp_tiocmget,
2338 .tiocmset = esp_tiocmset,
2339};
2340
Alan Cox31f35932009-01-02 13:45:05 +00002341static const struct tty_port_operations esp_port_ops = {
2342 .esp_carrier_raised,
2343};
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
2346 * The serial driver boot-time initialization code!
2347 */
2348static int __init espserial_init(void)
2349{
2350 int i, offset;
Alan Coxfb100b62008-04-30 00:54:16 -07002351 struct esp_struct *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 struct esp_struct *last_primary = NULL;
Alan Coxfb100b62008-04-30 00:54:16 -07002353 int esp[] = { 0x100, 0x140, 0x180, 0x200, 0x240, 0x280, 0x300, 0x380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 esp_driver = alloc_tty_driver(NR_PORTS);
2356 if (!esp_driver)
2357 return -ENOMEM;
Alan Coxfb100b62008-04-30 00:54:16 -07002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 for (i = 0; i < NR_PRIMARY; i++) {
2360 if (irq[i] != 0) {
2361 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2362 (irq[i] == 8) || (irq[i] == 13))
2363 irq[i] = 0;
2364 else if (irq[i] == 2)
2365 irq[i] = 9;
2366 }
2367 }
2368
2369 if ((dma != 1) && (dma != 3))
2370 dma = 0;
2371
2372 if ((rx_trigger < 1) || (rx_trigger > 1023))
2373 rx_trigger = 768;
2374
2375 if ((tx_trigger < 1) || (tx_trigger > 1023))
2376 tx_trigger = 768;
2377
2378 if ((flow_off < 1) || (flow_off > 1023))
2379 flow_off = 1016;
Alan Coxfb100b62008-04-30 00:54:16 -07002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if ((flow_on < 1) || (flow_on > 1023))
2382 flow_on = 944;
2383
2384 if ((rx_timeout < 0) || (rx_timeout > 255))
2385 rx_timeout = 128;
Alan Coxfb100b62008-04-30 00:54:16 -07002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (flow_on >= flow_off)
2388 flow_on = flow_off - 1;
2389
2390 show_serial_version();
2391
2392 /* Initialize the tty_driver structure */
Alan Coxfb100b62008-04-30 00:54:16 -07002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 esp_driver->owner = THIS_MODULE;
2395 esp_driver->name = "ttyP";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 esp_driver->major = ESP_IN_MAJOR;
2397 esp_driver->minor_start = 0;
2398 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2399 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2400 esp_driver->init_termios = tty_std_termios;
2401 esp_driver->init_termios.c_cflag =
2402 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Coxfb100b62008-04-30 00:54:16 -07002403 esp_driver->init_termios.c_ispeed = 9600;
2404 esp_driver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2406 tty_set_operations(esp_driver, &esp_ops);
Alan Coxfb100b62008-04-30 00:54:16 -07002407 if (tty_register_driver(esp_driver)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 printk(KERN_ERR "Couldn't register esp serial driver");
2409 put_tty_driver(esp_driver);
2410 return 1;
2411 }
2412
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002413 info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Alan Coxfb100b62008-04-30 00:54:16 -07002415 if (!info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2417 tty_unregister_driver(esp_driver);
2418 put_tty_driver(esp_driver);
2419 return 1;
2420 }
2421
Ingo Molnar8f56a312006-01-16 22:14:00 -08002422 spin_lock_init(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /* rx_trigger, tx_trigger are needed by autoconfig */
2424 info->config.rx_trigger = rx_trigger;
2425 info->config.tx_trigger = tx_trigger;
2426
2427 i = 0;
2428 offset = 0;
2429
2430 do {
Alan Cox31f35932009-01-02 13:45:05 +00002431 tty_port_init(&info->port);
2432 info->port.ops = &esp_port_ops;
Alan Cox4982d6b2008-07-16 21:55:11 +01002433 info->io_port = esp[i] + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 info->irq = irq[i];
2435 info->line = (i * 8) + (offset / 8);
2436
2437 if (!autoconfig(info)) {
2438 i++;
2439 offset = 0;
2440 continue;
2441 }
2442
2443 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
Alan Cox4982d6b2008-07-16 21:55:11 +01002444 info->port.flags = STD_COM_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (info->custom_divisor)
Alan Cox4982d6b2008-07-16 21:55:11 +01002446 info->port.flags |= ASYNC_SPD_CUST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 info->magic = ESP_MAGIC;
2448 info->close_delay = 5*HZ/10;
2449 info->closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 info->config.rx_timeout = rx_timeout;
2451 info->config.flow_on = flow_on;
2452 info->config.flow_off = flow_off;
2453 info->config.pio_threshold = pio_threshold;
2454 info->next_port = ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 init_waitqueue_head(&info->delta_msr_wait);
2456 init_waitqueue_head(&info->break_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ports = info;
2458 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
Alan Cox4982d6b2008-07-16 21:55:11 +01002459 info->line, info->io_port, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 if (info->line % 8) {
2462 printk("secondary port\n");
2463 /* 8 port cards can't do DMA */
2464 info->stat_flags |= ESP_STAT_NEVER_DMA;
2465
2466 if (last_primary)
2467 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2468 } else {
2469 printk("primary port\n");
2470 last_primary = info;
2471 irq[i] = info->irq;
2472 }
2473
2474 if (!dma)
2475 info->stat_flags |= ESP_STAT_NEVER_DMA;
2476
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002477 info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
Alan Coxfb100b62008-04-30 00:54:16 -07002478 if (!info) {
2479 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /* allow use of the already detected ports */
2481 return 0;
2482 }
2483
Ingo Molnar80d38f92008-03-07 10:47:43 +01002484 spin_lock_init(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /* rx_trigger, tx_trigger are needed by autoconfig */
2486 info->config.rx_trigger = rx_trigger;
2487 info->config.tx_trigger = tx_trigger;
2488
2489 if (offset == 56) {
2490 i++;
2491 offset = 0;
2492 } else {
2493 offset += 8;
2494 }
2495 } while (i < NR_PRIMARY);
2496
2497 /* free the last port memory allocation */
2498 kfree(info);
2499
2500 return 0;
2501}
2502
Alan Coxfb100b62008-04-30 00:54:16 -07002503static void __exit espserial_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 int e1;
2506 struct esp_struct *temp_async;
2507 struct esp_pio_buffer *pio_buf;
2508
Alan Coxfb100b62008-04-30 00:54:16 -07002509 e1 = tty_unregister_driver(esp_driver);
2510 if (e1)
2511 printk(KERN_ERR "esp: failed to unregister driver (%d)\n", e1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 put_tty_driver(esp_driver);
2513
2514 while (ports) {
Alan Cox4982d6b2008-07-16 21:55:11 +01002515 if (ports->io_port)
2516 release_region(ports->io_port, REGION_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 temp_async = ports->next_port;
2518 kfree(ports);
2519 ports = temp_async;
2520 }
2521
2522 if (dma_buffer)
2523 free_pages((unsigned long)dma_buffer,
2524 get_order(DMA_BUFFER_SZ));
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 while (free_pio_buf) {
2527 pio_buf = free_pio_buf->next;
2528 kfree(free_pio_buf);
2529 free_pio_buf = pio_buf;
2530 }
2531}
2532
2533module_init(espserial_init);
2534module_exit(espserial_exit);