blob: 410e8e7e6bfe33a6502dc6880b792076c0d2c1a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Serial driver for the amiga builtin port.
3 *
4 * This code was created by taking serial.c version 4.30 from kernel
5 * release 2.3.22, replacing all hardware related stuff with the
6 * corresponding amiga hardware actions, and removing all irrelevant
7 * code. As a consequence, it uses many of the constants and names
8 * associated with the registers and bits of 16550 compatible UARTS -
9 * but only to keep track of status, etc in the state variables. It
10 * was done this was to make it easier to keep the code in line with
11 * (non hardware specific) changes to serial.c.
12 *
13 * The port is registered with the tty driver as minor device 64, and
14 * therefore other ports should should only use 65 upwards.
15 *
16 * Richard Lucock 28/12/99
17 *
18 * Copyright (C) 1991, 1992 Linus Torvalds
19 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
20 * 1998, 1999 Theodore Ts'o
21 *
22 */
23
24/*
25 * Serial driver configuration section. Here are the various options:
26 *
27 * SERIAL_PARANOIA_CHECK
28 * Check the magic number for the async_structure where
29 * ever possible.
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33
34#undef SERIAL_PARANOIA_CHECK
35#define SERIAL_DO_RESTART
36
37/* Set of debugging defines */
38
39#undef SERIAL_DEBUG_INTR
40#undef SERIAL_DEBUG_OPEN
41#undef SERIAL_DEBUG_FLOW
42#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
43
44/* Sanity checks */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
Jiri Slaby916b7652012-03-05 14:52:20 +010048 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#else
50#define DBG_CNT(s)
51#endif
52
53/*
54 * End of serial driver configuration section.
55 */
56
57#include <linux/module.h>
58
59#include <linux/types.h>
60#include <linux/serial.h>
61#include <linux/serialP.h>
62#include <linux/serial_reg.h>
63static char *serial_version = "4.30";
64
65#include <linux/errno.h>
66#include <linux/signal.h>
67#include <linux/sched.h>
68#include <linux/kernel.h>
69#include <linux/timer.h>
70#include <linux/interrupt.h>
71#include <linux/tty.h>
72#include <linux/tty_flip.h>
73#include <linux/console.h>
74#include <linux/major.h>
75#include <linux/string.h>
76#include <linux/fcntl.h>
77#include <linux/ptrace.h>
78#include <linux/ioport.h>
79#include <linux/mm.h>
Alexey Dobriyand5940272009-03-31 15:19:23 -070080#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/slab.h>
82#include <linux/init.h>
83#include <linux/bitops.h>
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +020084#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <asm/setup.h>
87
88#include <asm/system.h>
89
90#include <asm/irq.h>
91
92#include <asm/amigahw.h>
93#include <asm/amigaints.h>
94
Al Virob4290a22006-01-12 01:06:12 -080095#define custom amiga_custom
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static char *serial_name = "Amiga-builtin serial driver";
97
98static struct tty_driver *serial_driver;
99
100/* number of characters left in xmit buffer before we ask for more */
101#define WAKEUP_CHARS 256
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static unsigned char current_ctl_bits;
104
Jiri Slaby916b7652012-03-05 14:52:20 +0100105static void change_speed(struct serial_state *info, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
107
108
109static struct serial_state rs_table[1];
110
Tobias Klauserfe971072006-01-09 20:54:02 -0800111#define NR_PORTS ARRAY_SIZE(rs_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <asm/uaccess.h>
114
115#define serial_isroot() (capable(CAP_SYS_ADMIN))
116
117
Jiri Slaby916b7652012-03-05 14:52:20 +0100118static inline int serial_paranoia_check(struct serial_state *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 char *name, const char *routine)
120{
121#ifdef SERIAL_PARANOIA_CHECK
122 static const char *badmagic =
123 "Warning: bad magic number for serial struct (%s) in %s\n";
124 static const char *badinfo =
125 "Warning: null async_struct for (%s) in %s\n";
126
127 if (!info) {
128 printk(badinfo, name, routine);
129 return 1;
130 }
131 if (info->magic != SERIAL_MAGIC) {
132 printk(badmagic, name, routine);
133 return 1;
134 }
135#endif
136 return 0;
137}
138
139/* some serial hardware definitions */
140#define SDR_OVRUN (1<<15)
141#define SDR_RBF (1<<14)
142#define SDR_TBE (1<<13)
143#define SDR_TSRE (1<<12)
144
145#define SERPER_PARENB (1<<15)
146
147#define AC_SETCLR (1<<15)
148#define AC_UARTBRK (1<<11)
149
150#define SER_DTR (1<<7)
151#define SER_RTS (1<<6)
152#define SER_DCD (1<<5)
153#define SER_CTS (1<<4)
154#define SER_DSR (1<<3)
155
156static __inline__ void rtsdtr_ctrl(int bits)
157{
158 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
159}
160
161/*
162 * ------------------------------------------------------------
163 * rs_stop() and rs_start()
164 *
165 * This routines are called before setting or resetting tty->stopped.
166 * They enable or disable transmitter interrupts, as necessary.
167 * ------------------------------------------------------------
168 */
169static void rs_stop(struct tty_struct *tty)
170{
Jiri Slaby916b7652012-03-05 14:52:20 +0100171 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned long flags;
173
174 if (serial_paranoia_check(info, tty->name, "rs_stop"))
175 return;
176
177 local_irq_save(flags);
178 if (info->IER & UART_IER_THRI) {
179 info->IER &= ~UART_IER_THRI;
180 /* disable Tx interrupt and remove any pending interrupts */
181 custom.intena = IF_TBE;
182 mb();
183 custom.intreq = IF_TBE;
184 mb();
185 }
186 local_irq_restore(flags);
187}
188
189static void rs_start(struct tty_struct *tty)
190{
Jiri Slaby916b7652012-03-05 14:52:20 +0100191 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned long flags;
193
194 if (serial_paranoia_check(info, tty->name, "rs_start"))
195 return;
196
197 local_irq_save(flags);
198 if (info->xmit.head != info->xmit.tail
199 && info->xmit.buf
200 && !(info->IER & UART_IER_THRI)) {
201 info->IER |= UART_IER_THRI;
202 custom.intena = IF_SETCLR | IF_TBE;
203 mb();
204 /* set a pending Tx Interrupt, transmitter should restart now */
205 custom.intreq = IF_SETCLR | IF_TBE;
206 mb();
207 }
208 local_irq_restore(flags);
209}
210
211/*
212 * ----------------------------------------------------------------------
213 *
214 * Here starts the interrupt handling routines. All of the following
215 * subroutines are declared as inline and are folded into
216 * rs_interrupt(). They were separated out for readability's sake.
217 *
218 * Note: rs_interrupt() is a "fast" interrupt, which means that it
219 * runs with interrupts turned off. People who may want to modify
220 * rs_interrupt() should try to keep the interrupt handler as fast as
221 * possible. After you are done making modifications, it is not a bad
222 * idea to do:
223 *
224 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
225 *
226 * and look at the resulting assemble code in serial.s.
227 *
228 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
229 * -----------------------------------------------------------------------
230 */
231
Jiri Slaby916b7652012-03-05 14:52:20 +0100232static void receive_chars(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int status;
235 int serdatr;
236 struct tty_struct *tty = info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800237 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct async_icount *icount;
Alan Cox33f0f882006-01-09 20:54:13 -0800239 int oe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jiri Slaby916b7652012-03-05 14:52:20 +0100241 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 status = UART_LSR_DR; /* We obviously have a character! */
244 serdatr = custom.serdatr;
245 mb();
246 custom.intreq = IF_RBF;
247 mb();
248
249 if((serdatr & 0x1ff) == 0)
250 status |= UART_LSR_BI;
251 if(serdatr & SDR_OVRUN)
252 status |= UART_LSR_OE;
253
254 ch = serdatr & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 icount->rx++;
256
257#ifdef SERIAL_DEBUG_INTR
258 printk("DR%02x:%02x...", ch, status);
259#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800260 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /*
263 * We don't handle parity or frame errors - but I have left
264 * the code in, since I'm not sure that the errors can't be
265 * detected.
266 */
267
268 if (status & (UART_LSR_BI | UART_LSR_PE |
269 UART_LSR_FE | UART_LSR_OE)) {
270 /*
271 * For statistics only
272 */
273 if (status & UART_LSR_BI) {
274 status &= ~(UART_LSR_FE | UART_LSR_PE);
275 icount->brk++;
276 } else if (status & UART_LSR_PE)
277 icount->parity++;
278 else if (status & UART_LSR_FE)
279 icount->frame++;
280 if (status & UART_LSR_OE)
281 icount->overrun++;
282
283 /*
284 * Now check to see if character should be
285 * ignored, and mask off conditions which
286 * should be ignored.
287 */
288 if (status & info->ignore_status_mask)
Alan Cox33f0f882006-01-09 20:54:13 -0800289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 status &= info->read_status_mask;
292
293 if (status & (UART_LSR_BI)) {
294#ifdef SERIAL_DEBUG_INTR
295 printk("handling break....");
296#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800297 flag = TTY_BREAK;
Jiri Slaby916b7652012-03-05 14:52:20 +0100298 if (info->flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 do_SAK(tty);
300 } else if (status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800301 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else if (status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800303 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (status & UART_LSR_OE) {
305 /*
306 * Overrun is special, since it's
307 * reported immediately, and doesn't
308 * affect the current character
309 */
Alan Cox33f0f882006-01-09 20:54:13 -0800310 oe = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 }
Alan Cox33f0f882006-01-09 20:54:13 -0800313 tty_insert_flip_char(tty, ch, flag);
314 if (oe == 1)
315 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 tty_flip_buffer_push(tty);
Alan Cox33f0f882006-01-09 20:54:13 -0800317out:
318 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Jiri Slaby916b7652012-03-05 14:52:20 +0100321static void transmit_chars(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 custom.intreq = IF_TBE;
324 mb();
325 if (info->x_char) {
326 custom.serdat = info->x_char | 0x100;
327 mb();
Jiri Slaby916b7652012-03-05 14:52:20 +0100328 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 info->x_char = 0;
330 return;
331 }
332 if (info->xmit.head == info->xmit.tail
333 || info->tty->stopped
334 || info->tty->hw_stopped) {
335 info->IER &= ~UART_IER_THRI;
336 custom.intena = IF_TBE;
337 mb();
338 return;
339 }
340
341 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
342 mb();
343 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
Jiri Slaby916b7652012-03-05 14:52:20 +0100344 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (CIRC_CNT(info->xmit.head,
347 info->xmit.tail,
348 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
Jiri Slabyc5f05082012-03-05 14:52:13 +0100349 tty_wakeup(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351#ifdef SERIAL_DEBUG_INTR
352 printk("THRE...");
353#endif
354 if (info->xmit.head == info->xmit.tail) {
355 custom.intena = IF_TBE;
356 mb();
357 info->IER &= ~UART_IER_THRI;
358 }
359}
360
Jiri Slaby916b7652012-03-05 14:52:20 +0100361static void check_modem_status(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
364 unsigned char dstatus;
365 struct async_icount *icount;
366
367 /* Determine bits that have changed */
368 dstatus = status ^ current_ctl_bits;
369 current_ctl_bits = status;
370
371 if (dstatus) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100372 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* update input line counters */
374 if (dstatus & SER_DSR)
375 icount->dsr++;
376 if (dstatus & SER_DCD) {
377 icount->dcd++;
378#ifdef CONFIG_HARD_PPS
Jiri Slaby916b7652012-03-05 14:52:20 +0100379 if ((info->flags & ASYNC_HARDPPS_CD) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 !(status & SER_DCD))
381 hardpps();
382#endif
383 }
384 if (dstatus & SER_CTS)
385 icount->cts++;
386 wake_up_interruptible(&info->delta_msr_wait);
387 }
388
Jiri Slaby916b7652012-03-05 14:52:20 +0100389 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
391 printk("ttyS%d CD now %s...", info->line,
392 (!(status & SER_DCD)) ? "on" : "off");
393#endif
394 if (!(status & SER_DCD))
395 wake_up_interruptible(&info->open_wait);
396 else {
397#ifdef SERIAL_DEBUG_OPEN
398 printk("doing serial hangup...");
399#endif
400 if (info->tty)
401 tty_hangup(info->tty);
402 }
403 }
Jiri Slaby916b7652012-03-05 14:52:20 +0100404 if (info->flags & ASYNC_CTS_FLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (info->tty->hw_stopped) {
406 if (!(status & SER_CTS)) {
407#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
408 printk("CTS tx start...");
409#endif
410 info->tty->hw_stopped = 0;
411 info->IER |= UART_IER_THRI;
412 custom.intena = IF_SETCLR | IF_TBE;
413 mb();
414 /* set a pending Tx Interrupt, transmitter should restart now */
415 custom.intreq = IF_SETCLR | IF_TBE;
416 mb();
Jiri Slabyc5f05082012-03-05 14:52:13 +0100417 tty_wakeup(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return;
419 }
420 } else {
421 if ((status & SER_CTS)) {
422#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
423 printk("CTS tx stop...");
424#endif
425 info->tty->hw_stopped = 1;
426 info->IER &= ~UART_IER_THRI;
427 /* disable Tx interrupt and remove any pending interrupts */
428 custom.intena = IF_TBE;
429 mb();
430 custom.intreq = IF_TBE;
431 mb();
432 }
433 }
434 }
435}
436
David Howells7d12e782006-10-05 14:55:46 +0100437static irqreturn_t ser_vbl_int( int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 /* vbl is just a periodic interrupt we tie into to update modem status */
Jiri Slaby916b7652012-03-05 14:52:20 +0100440 struct serial_state *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * TBD - is it better to unregister from this interrupt or to
443 * ignore it if MSI is clear ?
444 */
445 if(info->IER & UART_IER_MSI)
446 check_modem_status(info);
447 return IRQ_HANDLED;
448}
449
David Howells7d12e782006-10-05 14:55:46 +0100450static irqreturn_t ser_rx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Jiri Slaby916b7652012-03-05 14:52:20 +0100452 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454#ifdef SERIAL_DEBUG_INTR
455 printk("ser_rx_int...");
456#endif
457
Jiri Slaby916b7652012-03-05 14:52:20 +0100458 if (!info->tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return IRQ_NONE;
460
461 receive_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#ifdef SERIAL_DEBUG_INTR
463 printk("end.\n");
464#endif
465 return IRQ_HANDLED;
466}
467
David Howells7d12e782006-10-05 14:55:46 +0100468static irqreturn_t ser_tx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jiri Slaby916b7652012-03-05 14:52:20 +0100470 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (custom.serdatr & SDR_TBE) {
473#ifdef SERIAL_DEBUG_INTR
474 printk("ser_tx_int...");
475#endif
476
Jiri Slaby916b7652012-03-05 14:52:20 +0100477 if (!info->tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return IRQ_NONE;
479
480 transmit_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#ifdef SERIAL_DEBUG_INTR
482 printk("end.\n");
483#endif
484 }
485 return IRQ_HANDLED;
486}
487
488/*
489 * -------------------------------------------------------------------
490 * Here ends the serial interrupt routines.
491 * -------------------------------------------------------------------
492 */
493
494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * ---------------------------------------------------------------
496 * Low level utility subroutines for the serial driver: routines to
497 * figure out the appropriate timeout for an interrupt chain, routines
498 * to initialize and startup a serial port, and routines to shutdown a
499 * serial port. Useful stuff like that.
500 * ---------------------------------------------------------------
501 */
502
Jiri Slaby916b7652012-03-05 14:52:20 +0100503static int startup(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 unsigned long flags;
506 int retval=0;
507 unsigned long page;
508
509 page = get_zeroed_page(GFP_KERNEL);
510 if (!page)
511 return -ENOMEM;
512
513 local_irq_save(flags);
514
Jiri Slaby916b7652012-03-05 14:52:20 +0100515 if (info->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 free_page(page);
517 goto errout;
518 }
519
520 if (info->xmit.buf)
521 free_page(page);
522 else
523 info->xmit.buf = (unsigned char *) page;
524
525#ifdef SERIAL_DEBUG_OPEN
526 printk("starting up ttys%d ...", info->line);
527#endif
528
529 /* Clear anything in the input buffer */
530
531 custom.intreq = IF_RBF;
532 mb();
533
534 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
535 if (retval) {
536 if (serial_isroot()) {
537 if (info->tty)
538 set_bit(TTY_IO_ERROR,
539 &info->tty->flags);
540 retval = 0;
541 }
542 goto errout;
543 }
544
545 /* enable both Rx and Tx interrupts */
546 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
547 mb();
548 info->IER = UART_IER_MSI;
549
550 /* remember current state of the DCD and CTS bits */
551 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 info->MCR = 0;
554 if (info->tty->termios->c_cflag & CBAUD)
555 info->MCR = SER_DTR | SER_RTS;
556 rtsdtr_ctrl(info->MCR);
557
558 if (info->tty)
559 clear_bit(TTY_IO_ERROR, &info->tty->flags);
560 info->xmit.head = info->xmit.tail = 0;
561
562 /*
563 * Set up the tty->alt_speed kludge
564 */
565 if (info->tty) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100566 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 info->tty->alt_speed = 57600;
Jiri Slaby916b7652012-03-05 14:52:20 +0100568 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 info->tty->alt_speed = 115200;
Jiri Slaby916b7652012-03-05 14:52:20 +0100570 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 info->tty->alt_speed = 230400;
Jiri Slaby916b7652012-03-05 14:52:20 +0100572 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 info->tty->alt_speed = 460800;
574 }
575
576 /*
577 * and set the speed of the serial port
578 */
579 change_speed(info, NULL);
580
Jiri Slaby916b7652012-03-05 14:52:20 +0100581 info->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 local_irq_restore(flags);
583 return 0;
584
585errout:
586 local_irq_restore(flags);
587 return retval;
588}
589
590/*
591 * This routine will shutdown a serial port; interrupts are disabled, and
592 * DTR is dropped if the hangup on close termio flag is on.
593 */
Jiri Slaby916b7652012-03-05 14:52:20 +0100594static void shutdown(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 unsigned long flags;
597 struct serial_state *state;
598
Jiri Slaby916b7652012-03-05 14:52:20 +0100599 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return;
601
Jiri Slaby916b7652012-03-05 14:52:20 +0100602 state = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604#ifdef SERIAL_DEBUG_OPEN
605 printk("Shutting down serial port %d ....\n", info->line);
606#endif
607
608 local_irq_save(flags); /* Disable interrupts */
609
610 /*
611 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
612 * here so the queue might never be waken up
613 */
614 wake_up_interruptible(&info->delta_msr_wait);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * Free the IRQ, if necessary
618 */
619 free_irq(IRQ_AMIGA_VERTB, info);
620
621 if (info->xmit.buf) {
622 free_page((unsigned long) info->xmit.buf);
623 info->xmit.buf = NULL;
624 }
625
626 info->IER = 0;
627 custom.intena = IF_RBF | IF_TBE;
628 mb();
629
630 /* disable break condition */
631 custom.adkcon = AC_UARTBRK;
632 mb();
633
634 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
635 info->MCR &= ~(SER_DTR|SER_RTS);
636 rtsdtr_ctrl(info->MCR);
637
638 if (info->tty)
639 set_bit(TTY_IO_ERROR, &info->tty->flags);
640
Jiri Slaby916b7652012-03-05 14:52:20 +0100641 info->flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 local_irq_restore(flags);
643}
644
645
646/*
647 * This routine is called to set the UART divisor registers to match
648 * the specified baud rate for a serial port.
649 */
Jiri Slaby916b7652012-03-05 14:52:20 +0100650static void change_speed(struct serial_state *info,
Alan Cox606d0992006-12-08 02:38:45 -0800651 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 int quot = 0, baud_base, baud;
654 unsigned cflag, cval = 0;
655 int bits;
656 unsigned long flags;
657
658 if (!info->tty || !info->tty->termios)
659 return;
660 cflag = info->tty->termios->c_cflag;
661
662 /* Byte size is always 8 bits plus parity bit if requested */
663
664 cval = 3; bits = 10;
665 if (cflag & CSTOPB) {
666 cval |= 0x04;
667 bits++;
668 }
669 if (cflag & PARENB) {
670 cval |= UART_LCR_PARITY;
671 bits++;
672 }
673 if (!(cflag & PARODD))
674 cval |= UART_LCR_EPAR;
675#ifdef CMSPAR
676 if (cflag & CMSPAR)
677 cval |= UART_LCR_SPAR;
678#endif
679
680 /* Determine divisor based on baud rate */
681 baud = tty_get_baud_rate(info->tty);
682 if (!baud)
683 baud = 9600; /* B0 transition handled in rs_set_termios */
Jiri Slaby916b7652012-03-05 14:52:20 +0100684 baud_base = info->baud_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (baud == 38400 &&
Jiri Slaby916b7652012-03-05 14:52:20 +0100686 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
687 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 else {
689 if (baud == 134)
690 /* Special case since 134 is really 134.5 */
691 quot = (2*baud_base / 269);
692 else if (baud)
693 quot = baud_base / baud;
694 }
695 /* If the quotient is zero refuse the change */
696 if (!quot && old_termios) {
Alan Coxdb0ef082007-07-15 23:41:47 -0700697 /* FIXME: Will need updating for new tty in the end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 info->tty->termios->c_cflag &= ~CBAUD;
699 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
700 baud = tty_get_baud_rate(info->tty);
701 if (!baud)
702 baud = 9600;
703 if (baud == 38400 &&
Jiri Slaby916b7652012-03-05 14:52:20 +0100704 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
705 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 else {
707 if (baud == 134)
708 /* Special case since 134 is really 134.5 */
709 quot = (2*baud_base / 269);
710 else if (baud)
711 quot = baud_base / baud;
712 }
713 }
714 /* As a last resort, if the quotient is zero, default to 9600 bps */
715 if (!quot)
716 quot = baud_base / 9600;
717 info->quot = quot;
Jiri Slaby916b7652012-03-05 14:52:20 +0100718 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 info->timeout += HZ/50; /* Add .02 seconds of slop */
720
721 /* CTS flow control flag and modem status interrupts */
722 info->IER &= ~UART_IER_MSI;
Jiri Slaby916b7652012-03-05 14:52:20 +0100723 if (info->flags & ASYNC_HARDPPS_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 info->IER |= UART_IER_MSI;
725 if (cflag & CRTSCTS) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100726 info->flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 info->IER |= UART_IER_MSI;
728 } else
Jiri Slaby916b7652012-03-05 14:52:20 +0100729 info->flags &= ~ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (cflag & CLOCAL)
Jiri Slaby916b7652012-03-05 14:52:20 +0100731 info->flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 else {
Jiri Slaby916b7652012-03-05 14:52:20 +0100733 info->flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 info->IER |= UART_IER_MSI;
735 }
736 /* TBD:
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300737 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
739
740 /*
741 * Set up parity check flag
742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
745 if (I_INPCK(info->tty))
746 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
747 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
748 info->read_status_mask |= UART_LSR_BI;
749
750 /*
751 * Characters to ignore
752 */
753 info->ignore_status_mask = 0;
754 if (I_IGNPAR(info->tty))
755 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
756 if (I_IGNBRK(info->tty)) {
757 info->ignore_status_mask |= UART_LSR_BI;
758 /*
759 * If we're ignore parity and break indicators, ignore
760 * overruns too. (For real raw support).
761 */
762 if (I_IGNPAR(info->tty))
763 info->ignore_status_mask |= UART_LSR_OE;
764 }
765 /*
766 * !!! ignore all characters if CREAD is not set
767 */
768 if ((cflag & CREAD) == 0)
769 info->ignore_status_mask |= UART_LSR_DR;
770 local_irq_save(flags);
771
772 {
773 short serper;
774
775 /* Set up the baud rate */
776 serper = quot - 1;
777
778 /* Enable or disable parity bit */
779
780 if(cval & UART_LCR_PARITY)
781 serper |= (SERPER_PARENB);
782
783 custom.serper = serper;
784 mb();
785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 local_irq_restore(flags);
788}
789
Alan Cox257afa32008-04-30 00:54:02 -0700790static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jiri Slaby916b7652012-03-05 14:52:20 +0100792 struct serial_state *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 unsigned long flags;
794
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700795 info = tty->driver_data;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
Alan Cox257afa32008-04-30 00:54:02 -0700798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700800 if (!info->xmit.buf)
Alan Cox257afa32008-04-30 00:54:02 -0700801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 local_irq_save(flags);
804 if (CIRC_SPACE(info->xmit.head,
805 info->xmit.tail,
806 SERIAL_XMIT_SIZE) == 0) {
807 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 info->xmit.buf[info->xmit.head++] = ch;
812 info->xmit.head &= SERIAL_XMIT_SIZE-1;
813 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700814 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817static void rs_flush_chars(struct tty_struct *tty)
818{
Jiri Slaby916b7652012-03-05 14:52:20 +0100819 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned long flags;
821
822 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
823 return;
824
825 if (info->xmit.head == info->xmit.tail
826 || tty->stopped
827 || tty->hw_stopped
828 || !info->xmit.buf)
829 return;
830
831 local_irq_save(flags);
832 info->IER |= UART_IER_THRI;
833 custom.intena = IF_SETCLR | IF_TBE;
834 mb();
835 /* set a pending Tx Interrupt, transmitter should restart now */
836 custom.intreq = IF_SETCLR | IF_TBE;
837 mb();
838 local_irq_restore(flags);
839}
840
841static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
842{
843 int c, ret = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +0100844 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned long flags;
846
847 if (serial_paranoia_check(info, tty->name, "rs_write"))
848 return 0;
849
Jiri Slabyb3218a72006-10-04 02:15:27 -0700850 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852
Jiri Kosina3abf3be2007-02-10 01:46:48 -0800853 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 while (1) {
855 c = CIRC_SPACE_TO_END(info->xmit.head,
856 info->xmit.tail,
857 SERIAL_XMIT_SIZE);
858 if (count < c)
859 c = count;
860 if (c <= 0) {
861 break;
862 }
863 memcpy(info->xmit.buf + info->xmit.head, buf, c);
864 info->xmit.head = ((info->xmit.head + c) &
865 (SERIAL_XMIT_SIZE-1));
866 buf += c;
867 count -= c;
868 ret += c;
869 }
870 local_irq_restore(flags);
871
872 if (info->xmit.head != info->xmit.tail
873 && !tty->stopped
874 && !tty->hw_stopped
875 && !(info->IER & UART_IER_THRI)) {
876 info->IER |= UART_IER_THRI;
877 local_irq_disable();
878 custom.intena = IF_SETCLR | IF_TBE;
879 mb();
880 /* set a pending Tx Interrupt, transmitter should restart now */
881 custom.intreq = IF_SETCLR | IF_TBE;
882 mb();
883 local_irq_restore(flags);
884 }
885 return ret;
886}
887
888static int rs_write_room(struct tty_struct *tty)
889{
Jiri Slaby916b7652012-03-05 14:52:20 +0100890 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
893 return 0;
894 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
895}
896
897static int rs_chars_in_buffer(struct tty_struct *tty)
898{
Jiri Slaby916b7652012-03-05 14:52:20 +0100899 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
902 return 0;
903 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
904}
905
906static void rs_flush_buffer(struct tty_struct *tty)
907{
Jiri Slaby916b7652012-03-05 14:52:20 +0100908 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned long flags;
910
911 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
912 return;
913 local_irq_save(flags);
914 info->xmit.head = info->xmit.tail = 0;
915 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 tty_wakeup(tty);
917}
918
919/*
920 * This function is used to send a high-priority XON/XOFF character to
921 * the device
922 */
923static void rs_send_xchar(struct tty_struct *tty, char ch)
924{
Jiri Slaby916b7652012-03-05 14:52:20 +0100925 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 unsigned long flags;
927
928 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
929 return;
930
931 info->x_char = ch;
932 if (ch) {
933 /* Make sure transmit interrupts are on */
934
935 /* Check this ! */
936 local_irq_save(flags);
937 if(!(custom.intenar & IF_TBE)) {
938 custom.intena = IF_SETCLR | IF_TBE;
939 mb();
940 /* set a pending Tx Interrupt, transmitter should restart now */
941 custom.intreq = IF_SETCLR | IF_TBE;
942 mb();
943 }
944 local_irq_restore(flags);
945
946 info->IER |= UART_IER_THRI;
947 }
948}
949
950/*
951 * ------------------------------------------------------------
952 * rs_throttle()
953 *
954 * This routine is called by the upper-layer tty layer to signal that
955 * incoming characters should be throttled.
956 * ------------------------------------------------------------
957 */
958static void rs_throttle(struct tty_struct * tty)
959{
Jiri Slaby916b7652012-03-05 14:52:20 +0100960 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 unsigned long flags;
962#ifdef SERIAL_DEBUG_THROTTLE
963 char buf[64];
964
965 printk("throttle %s: %d....\n", tty_name(tty, buf),
966 tty->ldisc.chars_in_buffer(tty));
967#endif
968
969 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
970 return;
971
972 if (I_IXOFF(tty))
973 rs_send_xchar(tty, STOP_CHAR(tty));
974
975 if (tty->termios->c_cflag & CRTSCTS)
976 info->MCR &= ~SER_RTS;
977
978 local_irq_save(flags);
979 rtsdtr_ctrl(info->MCR);
980 local_irq_restore(flags);
981}
982
983static void rs_unthrottle(struct tty_struct * tty)
984{
Jiri Slaby916b7652012-03-05 14:52:20 +0100985 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 unsigned long flags;
987#ifdef SERIAL_DEBUG_THROTTLE
988 char buf[64];
989
990 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
991 tty->ldisc.chars_in_buffer(tty));
992#endif
993
994 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
995 return;
996
997 if (I_IXOFF(tty)) {
998 if (info->x_char)
999 info->x_char = 0;
1000 else
1001 rs_send_xchar(tty, START_CHAR(tty));
1002 }
1003 if (tty->termios->c_cflag & CRTSCTS)
1004 info->MCR |= SER_RTS;
1005 local_irq_save(flags);
1006 rtsdtr_ctrl(info->MCR);
1007 local_irq_restore(flags);
1008}
1009
1010/*
1011 * ------------------------------------------------------------
1012 * rs_ioctl() and friends
1013 * ------------------------------------------------------------
1014 */
1015
Jiri Slaby916b7652012-03-05 14:52:20 +01001016static int get_serial_info(struct serial_state *state,
Al Viroab14cae2006-01-12 01:06:30 -08001017 struct serial_struct __user * retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct serial_struct tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!retinfo)
1022 return -EFAULT;
1023 memset(&tmp, 0, sizeof(tmp));
Arnd Bergmannec79d602010-06-01 22:53:01 +02001024 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 tmp.type = state->type;
1026 tmp.line = state->line;
1027 tmp.port = state->port;
1028 tmp.irq = state->irq;
1029 tmp.flags = state->flags;
1030 tmp.xmit_fifo_size = state->xmit_fifo_size;
1031 tmp.baud_base = state->baud_base;
1032 tmp.close_delay = state->close_delay;
1033 tmp.closing_wait = state->closing_wait;
1034 tmp.custom_divisor = state->custom_divisor;
Arnd Bergmannec79d602010-06-01 22:53:01 +02001035 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1037 return -EFAULT;
1038 return 0;
1039}
1040
Jiri Slaby916b7652012-03-05 14:52:20 +01001041static int set_serial_info(struct serial_state *state,
Al Viroab14cae2006-01-12 01:06:30 -08001042 struct serial_struct __user * new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct serial_struct new_serial;
Jiri Slaby916b7652012-03-05 14:52:20 +01001045 struct serial_state old_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unsigned int change_irq,change_port;
1047 int retval = 0;
1048
1049 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1050 return -EFAULT;
Alan Coxe18ce492008-04-30 00:53:16 -07001051
Arnd Bergmannec79d602010-06-01 22:53:01 +02001052 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 old_state = *state;
1054
1055 change_irq = new_serial.irq != state->irq;
1056 change_port = (new_serial.port != state->port);
Alan Coxe18ce492008-04-30 00:53:16 -07001057 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001058 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (!serial_isroot()) {
1063 if ((new_serial.baud_base != state->baud_base) ||
1064 (new_serial.close_delay != state->close_delay) ||
1065 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1066 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1067 (state->flags & ~ASYNC_USR_MASK)))
1068 return -EPERM;
1069 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1070 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 state->custom_divisor = new_serial.custom_divisor;
1072 goto check_and_exit;
1073 }
1074
Alan Coxe18ce492008-04-30 00:53:16 -07001075 if (new_serial.baud_base < 9600) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001076 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
1081 * OK, past this point, all the error checking has been done.
1082 * At this point, we start making changes.....
1083 */
1084
1085 state->baud_base = new_serial.baud_base;
1086 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1087 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 state->custom_divisor = new_serial.custom_divisor;
1089 state->close_delay = new_serial.close_delay * HZ/100;
1090 state->closing_wait = new_serial.closing_wait * HZ/100;
Jiri Slaby916b7652012-03-05 14:52:20 +01001091 state->tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093check_and_exit:
Jiri Slabyfef21072012-03-05 14:52:14 +01001094 if (state->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (((old_state.flags & ASYNC_SPD_MASK) !=
1096 (state->flags & ASYNC_SPD_MASK)) ||
1097 (old_state.custom_divisor != state->custom_divisor)) {
1098 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby916b7652012-03-05 14:52:20 +01001099 state->tty->alt_speed = 57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby916b7652012-03-05 14:52:20 +01001101 state->tty->alt_speed = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby916b7652012-03-05 14:52:20 +01001103 state->tty->alt_speed = 230400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby916b7652012-03-05 14:52:20 +01001105 state->tty->alt_speed = 460800;
1106 change_speed(state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108 } else
Jiri Slaby916b7652012-03-05 14:52:20 +01001109 retval = startup(state);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001110 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return retval;
1112}
1113
1114
1115/*
1116 * get_lsr_info - get line status register info
1117 *
1118 * Purpose: Let user call ioctl() to get info when the UART physically
1119 * is emptied. On bus types like RS485, the transmitter must
1120 * release the bus after transmitting. This must be done when
1121 * the transmit shift register is empty, not be done when the
1122 * transmit holding register is empty. This functionality
1123 * allows an RS485 driver to be written in user space.
1124 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001125static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 unsigned char status;
1128 unsigned int result;
1129 unsigned long flags;
1130
1131 local_irq_save(flags);
1132 status = custom.serdatr;
1133 mb();
1134 local_irq_restore(flags);
1135 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1136 if (copy_to_user(value, &result, sizeof(int)))
1137 return -EFAULT;
1138 return 0;
1139}
1140
1141
Alan Cox60b33c12011-02-14 16:26:14 +00001142static int rs_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Jiri Slaby916b7652012-03-05 14:52:20 +01001144 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 unsigned char control, status;
1146 unsigned long flags;
1147
1148 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1149 return -ENODEV;
1150 if (tty->flags & (1 << TTY_IO_ERROR))
1151 return -EIO;
1152
1153 control = info->MCR;
1154 local_irq_save(flags);
1155 status = ciab.pra;
1156 local_irq_restore(flags);
1157 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1158 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1159 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1160 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1161 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1162}
1163
Alan Cox20b9d172011-02-14 16:26:50 +00001164static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1165 unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Jiri Slaby916b7652012-03-05 14:52:20 +01001167 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 unsigned long flags;
1169
1170 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1171 return -ENODEV;
1172 if (tty->flags & (1 << TTY_IO_ERROR))
1173 return -EIO;
1174
1175 local_irq_save(flags);
1176 if (set & TIOCM_RTS)
1177 info->MCR |= SER_RTS;
1178 if (set & TIOCM_DTR)
1179 info->MCR |= SER_DTR;
1180 if (clear & TIOCM_RTS)
1181 info->MCR &= ~SER_RTS;
1182 if (clear & TIOCM_DTR)
1183 info->MCR &= ~SER_DTR;
1184 rtsdtr_ctrl(info->MCR);
1185 local_irq_restore(flags);
1186 return 0;
1187}
1188
1189/*
1190 * rs_break() --- routine which turns the break handling on or off
1191 */
Alan Cox9e989662008-07-22 11:18:03 +01001192static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jiri Slaby916b7652012-03-05 14:52:20 +01001194 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned long flags;
1196
1197 if (serial_paranoia_check(info, tty->name, "rs_break"))
Geert Uytterhoeven970a8a52008-08-06 22:19:39 +02001198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 local_irq_save(flags);
1201 if (break_state == -1)
1202 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1203 else
1204 custom.adkcon = AC_UARTBRK;
1205 mb();
1206 local_irq_restore(flags);
Alan Cox9e989662008-07-22 11:18:03 +01001207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Alan Cox05871022010-09-16 18:21:52 +01001210/*
1211 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1212 * Return: write counters to the user passed counter struct
1213 * NB: both 1->0 and 0->1 transitions are counted except for
1214 * RI where only 0->1 is counted.
1215 */
1216static int rs_get_icount(struct tty_struct *tty,
1217 struct serial_icounter_struct *icount)
1218{
Jiri Slaby916b7652012-03-05 14:52:20 +01001219 struct serial_state *info = tty->driver_data;
Alan Cox05871022010-09-16 18:21:52 +01001220 struct async_icount cnow;
1221 unsigned long flags;
1222
1223 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001224 cnow = info->icount;
Alan Cox05871022010-09-16 18:21:52 +01001225 local_irq_restore(flags);
1226 icount->cts = cnow.cts;
1227 icount->dsr = cnow.dsr;
1228 icount->rng = cnow.rng;
1229 icount->dcd = cnow.dcd;
1230 icount->rx = cnow.rx;
1231 icount->tx = cnow.tx;
1232 icount->frame = cnow.frame;
1233 icount->overrun = cnow.overrun;
1234 icount->parity = cnow.parity;
1235 icount->brk = cnow.brk;
1236 icount->buf_overrun = cnow.buf_overrun;
1237
1238 return 0;
1239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Alan Cox6caa76b2011-02-14 16:27:22 +00001241static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 unsigned int cmd, unsigned long arg)
1243{
Jiri Slaby916b7652012-03-05 14:52:20 +01001244 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct async_icount cprev, cnow; /* kernel counter temps */
Al Viroab14cae2006-01-12 01:06:30 -08001246 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned long flags;
1248
1249 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1250 return -ENODEV;
1251
1252 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1253 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1254 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1255 if (tty->flags & (1 << TTY_IO_ERROR))
1256 return -EIO;
1257 }
1258
1259 switch (cmd) {
1260 case TIOCGSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001261 return get_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 case TIOCSSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001263 return set_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 case TIOCSERCONFIG:
1265 return 0;
1266
1267 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001268 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 case TIOCSERGSTRUCT:
Al Viroab14cae2006-01-12 01:06:30 -08001271 if (copy_to_user(argp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001272 info, sizeof(struct serial_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -EFAULT;
1274 return 0;
1275
1276 /*
1277 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1278 * - mask passed in arg for lines of interest
1279 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1280 * Caller should use TIOCGICOUNT to see which one it was
1281 */
1282 case TIOCMIWAIT:
1283 local_irq_save(flags);
1284 /* note the counters on entry */
Jiri Slaby916b7652012-03-05 14:52:20 +01001285 cprev = info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 local_irq_restore(flags);
1287 while (1) {
1288 interruptible_sleep_on(&info->delta_msr_wait);
1289 /* see if a signal did it */
1290 if (signal_pending(current))
1291 return -ERESTARTSYS;
1292 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001293 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 local_irq_restore(flags);
1295 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1296 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1297 return -EIO; /* no change => error */
1298 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1299 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1300 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1301 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1302 return 0;
1303 }
1304 cprev = cnow;
1305 }
1306 /* NOTREACHED */
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 case TIOCSERGWILD:
1309 case TIOCSERSWILD:
1310 /* "setserial -W" is called in Debian boot */
1311 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1312 return 0;
1313
1314 default:
1315 return -ENOIOCTLCMD;
1316 }
1317 return 0;
1318}
1319
Alan Cox606d0992006-12-08 02:38:45 -08001320static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Jiri Slaby916b7652012-03-05 14:52:20 +01001322 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned long flags;
1324 unsigned int cflag = tty->termios->c_cflag;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 change_speed(info, old_termios);
1327
1328 /* Handle transition to B0 status */
1329 if ((old_termios->c_cflag & CBAUD) &&
1330 !(cflag & CBAUD)) {
1331 info->MCR &= ~(SER_DTR|SER_RTS);
1332 local_irq_save(flags);
1333 rtsdtr_ctrl(info->MCR);
1334 local_irq_restore(flags);
1335 }
1336
1337 /* Handle transition away from B0 status */
1338 if (!(old_termios->c_cflag & CBAUD) &&
1339 (cflag & CBAUD)) {
1340 info->MCR |= SER_DTR;
1341 if (!(tty->termios->c_cflag & CRTSCTS) ||
1342 !test_bit(TTY_THROTTLED, &tty->flags)) {
1343 info->MCR |= SER_RTS;
1344 }
1345 local_irq_save(flags);
1346 rtsdtr_ctrl(info->MCR);
1347 local_irq_restore(flags);
1348 }
1349
1350 /* Handle turning off CRTSCTS */
1351 if ((old_termios->c_cflag & CRTSCTS) &&
1352 !(tty->termios->c_cflag & CRTSCTS)) {
1353 tty->hw_stopped = 0;
1354 rs_start(tty);
1355 }
1356
1357#if 0
1358 /*
1359 * No need to wake up processes in open wait, since they
1360 * sample the CLOCAL flag once, and don't recheck it.
1361 * XXX It's not clear whether the current behavior is correct
1362 * or not. Hence, this may change.....
1363 */
1364 if (!(old_termios->c_cflag & CLOCAL) &&
1365 (tty->termios->c_cflag & CLOCAL))
1366 wake_up_interruptible(&info->open_wait);
1367#endif
1368}
1369
1370/*
1371 * ------------------------------------------------------------
1372 * rs_close()
1373 *
1374 * This routine is called when the serial port gets closed. First, we
1375 * wait for the last remaining data to be sent. Then, we unlink its
1376 * async structure from the interrupt chain if necessary, and we free
1377 * that IRQ if nothing is left in the chain.
1378 * ------------------------------------------------------------
1379 */
1380static void rs_close(struct tty_struct *tty, struct file * filp)
1381{
Jiri Slaby916b7652012-03-05 14:52:20 +01001382 struct serial_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 unsigned long flags;
1384
Jiri Slaby916b7652012-03-05 14:52:20 +01001385 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return;
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 local_irq_save(flags);
1389
1390 if (tty_hung_up_p(filp)) {
1391 DBG_CNT("before DEC-hung");
1392 local_irq_restore(flags);
1393 return;
1394 }
1395
1396#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby916b7652012-03-05 14:52:20 +01001397 printk("rs_close ttys%d, count = %d\n", state->line, state->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398#endif
1399 if ((tty->count == 1) && (state->count != 1)) {
1400 /*
1401 * Uh, oh. tty->count is 1, which means that the tty
1402 * structure will be freed. state->count should always
1403 * be one in these conditions. If it's greater than
1404 * one, we've got real problems, since it means the
1405 * serial port won't be shutdown.
1406 */
1407 printk("rs_close: bad serial port count; tty->count is 1, "
1408 "state->count is %d\n", state->count);
1409 state->count = 1;
1410 }
1411 if (--state->count < 0) {
1412 printk("rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slabyd8522562012-03-05 14:52:16 +01001413 state->line, state->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 state->count = 0;
1415 }
1416 if (state->count) {
1417 DBG_CNT("before DEC-2");
1418 local_irq_restore(flags);
1419 return;
1420 }
Jiri Slabyfef21072012-03-05 14:52:14 +01001421 state->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
1423 * Now we wait for the transmit buffer to clear; and we notify
1424 * the line discipline to only process XON/XOFF characters.
1425 */
1426 tty->closing = 1;
Jiri Slabyd8522562012-03-05 14:52:16 +01001427 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1428 tty_wait_until_sent(tty, state->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
1430 * At this point we stop accepting input. To do this, we
1431 * disable the receive line status interrupts, and tell the
1432 * interrupt driver to stop checking the data ready bit in the
1433 * line status register.
1434 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001435 state->read_status_mask &= ~UART_LSR_DR;
Jiri Slabyfef21072012-03-05 14:52:14 +01001436 if (state->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* disable receive interrupts */
1438 custom.intena = IF_RBF;
1439 mb();
1440 /* clear any pending receive interrupt */
1441 custom.intreq = IF_RBF;
1442 mb();
1443
1444 /*
1445 * Before we drop DTR, make sure the UART transmitter
1446 * has completely drained; this is especially
1447 * important if there is a transmit FIFO!
1448 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001449 rs_wait_until_sent(tty, state->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001451 shutdown(state);
Alan Cox978e5952008-04-30 00:53:59 -07001452 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 tty_ldisc_flush(tty);
1455 tty->closing = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +01001456 state->tty = NULL;
1457 if (state->blocked_open) {
Jiri Slabyd8522562012-03-05 14:52:16 +01001458 if (state->close_delay) {
1459 msleep_interruptible(jiffies_to_msecs(state->close_delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001461 wake_up_interruptible(&state->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Jiri Slabyfef21072012-03-05 14:52:14 +01001463 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby916b7652012-03-05 14:52:20 +01001464 wake_up_interruptible(&state->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 local_irq_restore(flags);
1466}
1467
1468/*
1469 * rs_wait_until_sent() --- wait until the transmitter is empty
1470 */
1471static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1472{
Jiri Slaby916b7652012-03-05 14:52:20 +01001473 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned long orig_jiffies, char_time;
1475 int lsr;
1476
1477 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1478 return;
1479
Jiri Slaby916b7652012-03-05 14:52:20 +01001480 if (info->xmit_fifo_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return; /* Just in case.... */
1482
1483 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001484
Arnd Bergmann20365212010-06-01 22:53:07 +02001485 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * Set the check interval to be 1/5 of the estimated time to
1487 * send a single character, and make it at least 1. The check
1488 * interval should also be less than the timeout.
1489 *
1490 * Note: we have to use pretty tight timings here to satisfy
1491 * the NIST-PCTS.
1492 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001493 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 char_time = char_time / 5;
1495 if (char_time == 0)
1496 char_time = 1;
1497 if (timeout)
1498 char_time = min_t(unsigned long, char_time, timeout);
1499 /*
1500 * If the transmitter hasn't cleared in twice the approximate
1501 * amount of time to send the entire FIFO, it probably won't
1502 * ever clear. This assumes the UART isn't doing flow
1503 * control, which is currently the case. Hence, if it ever
1504 * takes longer than info->timeout, this is probably due to a
1505 * UART bug of some kind. So, we clamp the timeout parameter at
1506 * 2*info->timeout.
1507 */
1508 if (!timeout || timeout > 2*info->timeout)
1509 timeout = 2*info->timeout;
1510#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1511 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1512 printk("jiff=%lu...", jiffies);
1513#endif
1514 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1515#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1516 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1517#endif
1518 msleep_interruptible(jiffies_to_msecs(char_time));
1519 if (signal_pending(current))
1520 break;
1521 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1522 break;
1523 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001524 __set_current_state(TASK_RUNNING);
Jiri Slabyeff4b0b2011-07-14 14:35:13 +02001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1527 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1528#endif
1529}
1530
1531/*
1532 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1533 */
1534static void rs_hangup(struct tty_struct *tty)
1535{
Jiri Slaby916b7652012-03-05 14:52:20 +01001536 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1539 return;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 rs_flush_buffer(tty);
1542 shutdown(info);
Jiri Slaby916b7652012-03-05 14:52:20 +01001543 info->count = 0;
1544 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 info->tty = NULL;
1546 wake_up_interruptible(&info->open_wait);
1547}
1548
1549/*
1550 * ------------------------------------------------------------
1551 * rs_open() and friends
1552 * ------------------------------------------------------------
1553 */
1554static int block_til_ready(struct tty_struct *tty, struct file * filp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001555 struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557#ifdef DECLARE_WAITQUEUE
1558 DECLARE_WAITQUEUE(wait, current);
1559#else
1560 struct wait_queue wait = { current, NULL };
1561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int retval;
1563 int do_clocal = 0, extra_count = 0;
1564 unsigned long flags;
1565
1566 /*
1567 * If the device is in the middle of being closed, then block
1568 * until it's done, and then try again.
1569 */
1570 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001571 (info->flags & ASYNC_CLOSING)) {
1572 if (info->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 interruptible_sleep_on(&info->close_wait);
1574#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001575 return ((info->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 -EAGAIN : -ERESTARTSYS);
1577#else
1578 return -EAGAIN;
1579#endif
1580 }
1581
1582 /*
1583 * If non-blocking mode is set, or the port is not enabled,
1584 * then make the check up front and then exit.
1585 */
1586 if ((filp->f_flags & O_NONBLOCK) ||
1587 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby916b7652012-03-05 14:52:20 +01001588 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
1590 }
1591
1592 if (tty->termios->c_cflag & CLOCAL)
1593 do_clocal = 1;
1594
1595 /*
1596 * Block waiting for the carrier detect and the line to become
1597 * free (i.e., not in use by the callout). While we are in
Jiri Slaby916b7652012-03-05 14:52:20 +01001598 * this loop, info->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * rs_close() knows when to free things. We restore it upon
1600 * exit, either normal or abnormal.
1601 */
1602 retval = 0;
1603 add_wait_queue(&info->open_wait, &wait);
1604#ifdef SERIAL_DEBUG_OPEN
1605 printk("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slaby916b7652012-03-05 14:52:20 +01001606 info->line, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607#endif
1608 local_irq_save(flags);
1609 if (!tty_hung_up_p(filp)) {
1610 extra_count = 1;
Jiri Slaby916b7652012-03-05 14:52:20 +01001611 info->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613 local_irq_restore(flags);
1614 info->blocked_open++;
1615 while (1) {
1616 local_irq_save(flags);
1617 if (tty->termios->c_cflag & CBAUD)
1618 rtsdtr_ctrl(SER_DTR|SER_RTS);
1619 local_irq_restore(flags);
1620 set_current_state(TASK_INTERRUPTIBLE);
1621 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001622 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001624 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 retval = -EAGAIN;
1626 else
1627 retval = -ERESTARTSYS;
1628#else
1629 retval = -EAGAIN;
1630#endif
1631 break;
1632 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001633 if (!(info->flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1635 break;
1636 if (signal_pending(current)) {
1637 retval = -ERESTARTSYS;
1638 break;
1639 }
1640#ifdef SERIAL_DEBUG_OPEN
1641 printk("block_til_ready blocking: ttys%d, count = %d\n",
Jiri Slaby916b7652012-03-05 14:52:20 +01001642 info->line, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643#endif
Arnd Bergmanne142a312010-06-01 22:53:10 +02001644 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001646 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001648 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 remove_wait_queue(&info->open_wait, &wait);
1650 if (extra_count)
Jiri Slaby916b7652012-03-05 14:52:20 +01001651 info->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 info->blocked_open--;
1653#ifdef SERIAL_DEBUG_OPEN
1654 printk("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slaby916b7652012-03-05 14:52:20 +01001655 info->line, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656#endif
1657 if (retval)
1658 return retval;
Jiri Slaby916b7652012-03-05 14:52:20 +01001659 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return 0;
1661}
1662
1663/*
1664 * This routine is called whenever a serial port is opened. It
1665 * enables interrupts for a serial port, linking in its async structure into
1666 * the IRQ chain. It also performs the serial-specific
1667 * initialization for the tty structure.
1668 */
1669static int rs_open(struct tty_struct *tty, struct file * filp)
1670{
Jiri Slaby916b7652012-03-05 14:52:20 +01001671 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +01001672 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jiri Slaby916b7652012-03-05 14:52:20 +01001674 info->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 info->tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +01001676 tty->driver_data = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (serial_paranoia_check(info, tty->name, "rs_open"))
1678 return -ENODEV;
1679
1680#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby916b7652012-03-05 14:52:20 +01001681 printk("rs_open %s, count = %d\n", tty->name, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682#endif
Jiri Slaby916b7652012-03-05 14:52:20 +01001683 tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /*
1686 * If the port is the middle of closing, bail out now
1687 */
1688 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001689 (info->flags & ASYNC_CLOSING)) {
1690 if (info->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 interruptible_sleep_on(&info->close_wait);
1692#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001693 return ((info->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 -EAGAIN : -ERESTARTSYS);
1695#else
1696 return -EAGAIN;
1697#endif
1698 }
1699
1700 /*
1701 * Start up serial port
1702 */
1703 retval = startup(info);
1704 if (retval) {
1705 return retval;
1706 }
1707
1708 retval = block_til_ready(tty, filp, info);
1709 if (retval) {
1710#ifdef SERIAL_DEBUG_OPEN
1711 printk("rs_open returning after block_til_ready with %d\n",
1712 retval);
1713#endif
1714 return retval;
1715 }
1716
1717#ifdef SERIAL_DEBUG_OPEN
1718 printk("rs_open %s successful...", tty->name);
1719#endif
1720 return 0;
1721}
1722
1723/*
1724 * /proc fs routines....
1725 */
1726
Alexey Dobriyand5940272009-03-31 15:19:23 -07001727static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 unsigned long flags;
1731
Alexey Dobriyand5940272009-03-31 15:19:23 -07001732 seq_printf(m, "%d: uart:amiga_builtin",state->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 local_irq_save(flags);
1735 status = ciab.pra;
Jiri Slaby916b7652012-03-05 14:52:20 +01001736 control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 local_irq_restore(flags);
1738
1739 stat_buf[0] = 0;
1740 stat_buf[1] = 0;
1741 if(!(control & SER_RTS))
1742 strcat(stat_buf, "|RTS");
1743 if(!(status & SER_CTS))
1744 strcat(stat_buf, "|CTS");
1745 if(!(control & SER_DTR))
1746 strcat(stat_buf, "|DTR");
1747 if(!(status & SER_DSR))
1748 strcat(stat_buf, "|DSR");
1749 if(!(status & SER_DCD))
1750 strcat(stat_buf, "|CD");
1751
Jiri Slaby916b7652012-03-05 14:52:20 +01001752 if (state->quot)
1753 seq_printf(m, " baud:%d", state->baud_base / state->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Alexey Dobriyand5940272009-03-31 15:19:23 -07001755 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001758 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001761 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001764 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001767 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /*
1770 * Last thing is the RS-232 status lines
1771 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001772 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Alexey Dobriyand5940272009-03-31 15:19:23 -07001775static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Alexey Dobriyand5940272009-03-31 15:19:23 -07001777 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1778 line_info(m, &rs_table[0]);
1779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Alexey Dobriyand5940272009-03-31 15:19:23 -07001782static int rs_proc_open(struct inode *inode, struct file *file)
1783{
1784 return single_open(file, rs_proc_show, NULL);
1785}
1786
1787static const struct file_operations rs_proc_fops = {
1788 .owner = THIS_MODULE,
1789 .open = rs_proc_open,
1790 .read = seq_read,
1791 .llseek = seq_lseek,
1792 .release = single_release,
1793};
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
1796 * ---------------------------------------------------------------------
1797 * rs_init() and friends
1798 *
1799 * rs_init() is called at boot-time to initialize the serial driver.
1800 * ---------------------------------------------------------------------
1801 */
1802
1803/*
1804 * This routine prints out the appropriate serial driver version
1805 * number, and identifies which options were configured into this
1806 * driver.
1807 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001808static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1811}
1812
1813
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001814static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 .open = rs_open,
1816 .close = rs_close,
1817 .write = rs_write,
1818 .put_char = rs_put_char,
1819 .flush_chars = rs_flush_chars,
1820 .write_room = rs_write_room,
1821 .chars_in_buffer = rs_chars_in_buffer,
1822 .flush_buffer = rs_flush_buffer,
1823 .ioctl = rs_ioctl,
1824 .throttle = rs_throttle,
1825 .unthrottle = rs_unthrottle,
1826 .set_termios = rs_set_termios,
1827 .stop = rs_stop,
1828 .start = rs_start,
1829 .hangup = rs_hangup,
1830 .break_ctl = rs_break,
1831 .send_xchar = rs_send_xchar,
1832 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 .tiocmget = rs_tiocmget,
1834 .tiocmset = rs_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001835 .get_icount = rs_get_icount,
Alexey Dobriyand5940272009-03-31 15:19:23 -07001836 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837};
1838
1839/*
1840 * The serial driver boot-time initialization code!
1841 */
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001842static int __init amiga_serial_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 unsigned long flags;
1845 struct serial_state * state;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001846 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Jiri Slaby410235f2012-03-05 14:52:01 +01001848 serial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (!serial_driver)
1850 return -ENOMEM;
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 show_serial_version();
1853
1854 /* Initialize the tty_driver structure */
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 serial_driver->driver_name = "amiserial";
1857 serial_driver->name = "ttyS";
1858 serial_driver->major = TTY_MAJOR;
1859 serial_driver->minor_start = 64;
1860 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1861 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1862 serial_driver->init_termios = tty_std_termios;
1863 serial_driver->init_termios.c_cflag =
1864 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1865 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1866 tty_set_operations(serial_driver, &serial_ops);
1867
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001868 error = tty_register_driver(serial_driver);
1869 if (error)
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001870 goto fail_put_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 state = rs_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 state->port = (int)&custom.serdatr; /* Just to give it a value */
1874 state->line = 0;
1875 state->custom_divisor = 0;
1876 state->close_delay = 5*HZ/10;
1877 state->closing_wait = 30*HZ;
1878 state->icount.cts = state->icount.dsr =
1879 state->icount.rng = state->icount.dcd = 0;
1880 state->icount.rx = state->icount.tx = 0;
1881 state->icount.frame = state->icount.parity = 0;
1882 state->icount.overrun = state->icount.brk = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +01001883 init_waitqueue_head(&state->open_wait);
1884 init_waitqueue_head(&state->close_wait);
1885 init_waitqueue_head(&state->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1888 state->line);
1889
1890 /* Hardware set up */
1891
1892 state->baud_base = amiga_colorclock;
1893 state->xmit_fifo_size = 1;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001896 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1897 if (error)
1898 goto fail_unregister;
1899
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001900 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001901 "serial RX", state);
1902 if (error)
1903 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Julia Lawallc70c0362010-04-06 14:34:46 -07001905 local_irq_save(flags);
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* turn off Rx and Tx interrupts */
1908 custom.intena = IF_RBF | IF_TBE;
1909 mb();
1910
1911 /* clear any pending interrupt */
1912 custom.intreq = IF_RBF | IF_TBE;
1913 mb();
1914
1915 local_irq_restore(flags);
1916
1917 /*
1918 * set the appropriate directions for the modem control flags,
1919 * and clear RTS and DTR
1920 */
1921 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1922 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1923
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001924 platform_set_drvdata(pdev, state);
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001927
1928fail_free_irq:
1929 free_irq(IRQ_AMIGA_TBE, state);
1930fail_unregister:
1931 tty_unregister_driver(serial_driver);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001932fail_put_tty_driver:
1933 put_tty_driver(serial_driver);
1934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001937static int __exit amiga_serial_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 int error;
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001940 struct serial_state *state = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if ((error = tty_unregister_driver(serial_driver)))
1944 printk("SERIAL: failed to unregister serial driver (%d)\n",
1945 error);
1946 put_tty_driver(serial_driver);
1947
Jiri Slaby916b7652012-03-05 14:52:20 +01001948 free_irq(IRQ_AMIGA_TBE, state);
1949 free_irq(IRQ_AMIGA_RBF, state);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001950
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001951 platform_set_drvdata(pdev, NULL);
1952
1953 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001956static struct platform_driver amiga_serial_driver = {
1957 .remove = __exit_p(amiga_serial_remove),
1958 .driver = {
1959 .name = "amiga-serial",
1960 .owner = THIS_MODULE,
1961 },
1962};
1963
1964static int __init amiga_serial_init(void)
1965{
1966 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1967}
1968
1969module_init(amiga_serial_init);
1970
1971static void __exit amiga_serial_exit(void)
1972{
1973 platform_driver_unregister(&amiga_serial_driver);
1974}
1975
1976module_exit(amiga_serial_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01001979#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/*
1982 * ------------------------------------------------------------
1983 * Serial console driver
1984 * ------------------------------------------------------------
1985 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987static void amiga_serial_putc(char c)
1988{
1989 custom.serdat = (unsigned char)c | 0x100;
1990 while (!(custom.serdatr & 0x2000))
1991 barrier();
1992}
1993
1994/*
1995 * Print a string to the serial port trying not to disturb
1996 * any possible real use of the port...
1997 *
1998 * The console must be locked when we get here.
1999 */
2000static void serial_console_write(struct console *co, const char *s,
2001 unsigned count)
2002{
2003 unsigned short intena = custom.intenar;
2004
2005 custom.intena = IF_TBE;
2006
2007 while (count--) {
2008 if (*s == '\n')
2009 amiga_serial_putc('\r');
2010 amiga_serial_putc(*s++);
2011 }
2012
2013 custom.intena = IF_SETCLR | (intena & IF_TBE);
2014}
2015
2016static struct tty_driver *serial_console_device(struct console *c, int *index)
2017{
2018 *index = 0;
2019 return serial_driver;
2020}
2021
2022static struct console sercons = {
2023 .name = "ttyS",
2024 .write = serial_console_write,
2025 .device = serial_console_device,
2026 .flags = CON_PRINTBUFFER,
2027 .index = -1,
2028};
2029
2030/*
2031 * Register console.
2032 */
2033static int __init amiserial_console_init(void)
2034{
2035 register_console(&sercons);
2036 return 0;
2037}
2038console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002039
2040#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042MODULE_LICENSE("GPL");
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002043MODULE_ALIAS("platform:amiga-serial");