blob: 165cd79faea200652c829e7263fcaf91c81ac4de [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 Slaby0f9b9682012-03-05 14:52:21 +01001045 bool change_spd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int retval = 0;
1047
1048 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1049 return -EFAULT;
Alan Coxe18ce492008-04-30 00:53:16 -07001050
Arnd Bergmannec79d602010-06-01 22:53:01 +02001051 tty_lock();
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001052 change_spd = ((new_serial.flags ^ state->flags) & ASYNC_SPD_MASK) ||
1053 new_serial.custom_divisor != state->custom_divisor;
1054 if (new_serial.irq != state->irq || new_serial.port != state->port ||
1055 new_serial.xmit_fifo_size != state->xmit_fifo_size) {
1056 tty_unlock();
1057 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (!serial_isroot()) {
1061 if ((new_serial.baud_base != state->baud_base) ||
1062 (new_serial.close_delay != state->close_delay) ||
1063 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1064 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1065 (state->flags & ~ASYNC_USR_MASK)))
1066 return -EPERM;
1067 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1068 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 state->custom_divisor = new_serial.custom_divisor;
1070 goto check_and_exit;
1071 }
1072
Alan Coxe18ce492008-04-30 00:53:16 -07001073 if (new_serial.baud_base < 9600) {
Arnd Bergmannec79d602010-06-01 22:53:01 +02001074 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -07001076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 /*
1079 * OK, past this point, all the error checking has been done.
1080 * At this point, we start making changes.....
1081 */
1082
1083 state->baud_base = new_serial.baud_base;
1084 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1085 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 state->custom_divisor = new_serial.custom_divisor;
1087 state->close_delay = new_serial.close_delay * HZ/100;
1088 state->closing_wait = new_serial.closing_wait * HZ/100;
Jiri Slaby916b7652012-03-05 14:52:20 +01001089 state->tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091check_and_exit:
Jiri Slabyfef21072012-03-05 14:52:14 +01001092 if (state->flags & ASYNC_INITIALIZED) {
Jiri Slaby0f9b9682012-03-05 14:52:21 +01001093 if (change_spd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby916b7652012-03-05 14:52:20 +01001095 state->tty->alt_speed = 57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby916b7652012-03-05 14:52:20 +01001097 state->tty->alt_speed = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby916b7652012-03-05 14:52:20 +01001099 state->tty->alt_speed = 230400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby916b7652012-03-05 14:52:20 +01001101 state->tty->alt_speed = 460800;
1102 change_speed(state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 } else
Jiri Slaby916b7652012-03-05 14:52:20 +01001105 retval = startup(state);
Arnd Bergmannec79d602010-06-01 22:53:01 +02001106 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return retval;
1108}
1109
1110
1111/*
1112 * get_lsr_info - get line status register info
1113 *
1114 * Purpose: Let user call ioctl() to get info when the UART physically
1115 * is emptied. On bus types like RS485, the transmitter must
1116 * release the bus after transmitting. This must be done when
1117 * the transmit shift register is empty, not be done when the
1118 * transmit holding register is empty. This functionality
1119 * allows an RS485 driver to be written in user space.
1120 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001121static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 unsigned char status;
1124 unsigned int result;
1125 unsigned long flags;
1126
1127 local_irq_save(flags);
1128 status = custom.serdatr;
1129 mb();
1130 local_irq_restore(flags);
1131 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1132 if (copy_to_user(value, &result, sizeof(int)))
1133 return -EFAULT;
1134 return 0;
1135}
1136
1137
Alan Cox60b33c12011-02-14 16:26:14 +00001138static int rs_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Jiri Slaby916b7652012-03-05 14:52:20 +01001140 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 unsigned char control, status;
1142 unsigned long flags;
1143
1144 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1145 return -ENODEV;
1146 if (tty->flags & (1 << TTY_IO_ERROR))
1147 return -EIO;
1148
1149 control = info->MCR;
1150 local_irq_save(flags);
1151 status = ciab.pra;
1152 local_irq_restore(flags);
1153 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1154 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1155 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1156 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1157 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1158}
1159
Alan Cox20b9d172011-02-14 16:26:50 +00001160static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1161 unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Jiri Slaby916b7652012-03-05 14:52:20 +01001163 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 unsigned long flags;
1165
1166 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1167 return -ENODEV;
1168 if (tty->flags & (1 << TTY_IO_ERROR))
1169 return -EIO;
1170
1171 local_irq_save(flags);
1172 if (set & TIOCM_RTS)
1173 info->MCR |= SER_RTS;
1174 if (set & TIOCM_DTR)
1175 info->MCR |= SER_DTR;
1176 if (clear & TIOCM_RTS)
1177 info->MCR &= ~SER_RTS;
1178 if (clear & TIOCM_DTR)
1179 info->MCR &= ~SER_DTR;
1180 rtsdtr_ctrl(info->MCR);
1181 local_irq_restore(flags);
1182 return 0;
1183}
1184
1185/*
1186 * rs_break() --- routine which turns the break handling on or off
1187 */
Alan Cox9e989662008-07-22 11:18:03 +01001188static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Jiri Slaby916b7652012-03-05 14:52:20 +01001190 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 unsigned long flags;
1192
1193 if (serial_paranoia_check(info, tty->name, "rs_break"))
Geert Uytterhoeven970a8a52008-08-06 22:19:39 +02001194 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 local_irq_save(flags);
1197 if (break_state == -1)
1198 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1199 else
1200 custom.adkcon = AC_UARTBRK;
1201 mb();
1202 local_irq_restore(flags);
Alan Cox9e989662008-07-22 11:18:03 +01001203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Alan Cox05871022010-09-16 18:21:52 +01001206/*
1207 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1208 * Return: write counters to the user passed counter struct
1209 * NB: both 1->0 and 0->1 transitions are counted except for
1210 * RI where only 0->1 is counted.
1211 */
1212static int rs_get_icount(struct tty_struct *tty,
1213 struct serial_icounter_struct *icount)
1214{
Jiri Slaby916b7652012-03-05 14:52:20 +01001215 struct serial_state *info = tty->driver_data;
Alan Cox05871022010-09-16 18:21:52 +01001216 struct async_icount cnow;
1217 unsigned long flags;
1218
1219 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001220 cnow = info->icount;
Alan Cox05871022010-09-16 18:21:52 +01001221 local_irq_restore(flags);
1222 icount->cts = cnow.cts;
1223 icount->dsr = cnow.dsr;
1224 icount->rng = cnow.rng;
1225 icount->dcd = cnow.dcd;
1226 icount->rx = cnow.rx;
1227 icount->tx = cnow.tx;
1228 icount->frame = cnow.frame;
1229 icount->overrun = cnow.overrun;
1230 icount->parity = cnow.parity;
1231 icount->brk = cnow.brk;
1232 icount->buf_overrun = cnow.buf_overrun;
1233
1234 return 0;
1235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Alan Cox6caa76b2011-02-14 16:27:22 +00001237static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 unsigned int cmd, unsigned long arg)
1239{
Jiri Slaby916b7652012-03-05 14:52:20 +01001240 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct async_icount cprev, cnow; /* kernel counter temps */
Al Viroab14cae2006-01-12 01:06:30 -08001242 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 unsigned long flags;
1244
1245 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1246 return -ENODEV;
1247
1248 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1249 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1250 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1251 if (tty->flags & (1 << TTY_IO_ERROR))
1252 return -EIO;
1253 }
1254
1255 switch (cmd) {
1256 case TIOCGSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001257 return get_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 case TIOCSSERIAL:
Al Viroab14cae2006-01-12 01:06:30 -08001259 return set_serial_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 case TIOCSERCONFIG:
1261 return 0;
1262
1263 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001264 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 case TIOCSERGSTRUCT:
Al Viroab14cae2006-01-12 01:06:30 -08001267 if (copy_to_user(argp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001268 info, sizeof(struct serial_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return -EFAULT;
1270 return 0;
1271
1272 /*
1273 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1274 * - mask passed in arg for lines of interest
1275 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1276 * Caller should use TIOCGICOUNT to see which one it was
1277 */
1278 case TIOCMIWAIT:
1279 local_irq_save(flags);
1280 /* note the counters on entry */
Jiri Slaby916b7652012-03-05 14:52:20 +01001281 cprev = info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 local_irq_restore(flags);
1283 while (1) {
1284 interruptible_sleep_on(&info->delta_msr_wait);
1285 /* see if a signal did it */
1286 if (signal_pending(current))
1287 return -ERESTARTSYS;
1288 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001289 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 local_irq_restore(flags);
1291 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1292 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1293 return -EIO; /* no change => error */
1294 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1295 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1296 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1297 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1298 return 0;
1299 }
1300 cprev = cnow;
1301 }
1302 /* NOTREACHED */
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 case TIOCSERGWILD:
1305 case TIOCSERSWILD:
1306 /* "setserial -W" is called in Debian boot */
1307 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1308 return 0;
1309
1310 default:
1311 return -ENOIOCTLCMD;
1312 }
1313 return 0;
1314}
1315
Alan Cox606d0992006-12-08 02:38:45 -08001316static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Jiri Slaby916b7652012-03-05 14:52:20 +01001318 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 unsigned long flags;
1320 unsigned int cflag = tty->termios->c_cflag;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 change_speed(info, old_termios);
1323
1324 /* Handle transition to B0 status */
1325 if ((old_termios->c_cflag & CBAUD) &&
1326 !(cflag & CBAUD)) {
1327 info->MCR &= ~(SER_DTR|SER_RTS);
1328 local_irq_save(flags);
1329 rtsdtr_ctrl(info->MCR);
1330 local_irq_restore(flags);
1331 }
1332
1333 /* Handle transition away from B0 status */
1334 if (!(old_termios->c_cflag & CBAUD) &&
1335 (cflag & CBAUD)) {
1336 info->MCR |= SER_DTR;
1337 if (!(tty->termios->c_cflag & CRTSCTS) ||
1338 !test_bit(TTY_THROTTLED, &tty->flags)) {
1339 info->MCR |= SER_RTS;
1340 }
1341 local_irq_save(flags);
1342 rtsdtr_ctrl(info->MCR);
1343 local_irq_restore(flags);
1344 }
1345
1346 /* Handle turning off CRTSCTS */
1347 if ((old_termios->c_cflag & CRTSCTS) &&
1348 !(tty->termios->c_cflag & CRTSCTS)) {
1349 tty->hw_stopped = 0;
1350 rs_start(tty);
1351 }
1352
1353#if 0
1354 /*
1355 * No need to wake up processes in open wait, since they
1356 * sample the CLOCAL flag once, and don't recheck it.
1357 * XXX It's not clear whether the current behavior is correct
1358 * or not. Hence, this may change.....
1359 */
1360 if (!(old_termios->c_cflag & CLOCAL) &&
1361 (tty->termios->c_cflag & CLOCAL))
1362 wake_up_interruptible(&info->open_wait);
1363#endif
1364}
1365
1366/*
1367 * ------------------------------------------------------------
1368 * rs_close()
1369 *
1370 * This routine is called when the serial port gets closed. First, we
1371 * wait for the last remaining data to be sent. Then, we unlink its
1372 * async structure from the interrupt chain if necessary, and we free
1373 * that IRQ if nothing is left in the chain.
1374 * ------------------------------------------------------------
1375 */
1376static void rs_close(struct tty_struct *tty, struct file * filp)
1377{
Jiri Slaby916b7652012-03-05 14:52:20 +01001378 struct serial_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned long flags;
1380
Jiri Slaby916b7652012-03-05 14:52:20 +01001381 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 local_irq_save(flags);
1385
1386 if (tty_hung_up_p(filp)) {
1387 DBG_CNT("before DEC-hung");
1388 local_irq_restore(flags);
1389 return;
1390 }
1391
1392#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby916b7652012-03-05 14:52:20 +01001393 printk("rs_close ttys%d, count = %d\n", state->line, state->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394#endif
1395 if ((tty->count == 1) && (state->count != 1)) {
1396 /*
1397 * Uh, oh. tty->count is 1, which means that the tty
1398 * structure will be freed. state->count should always
1399 * be one in these conditions. If it's greater than
1400 * one, we've got real problems, since it means the
1401 * serial port won't be shutdown.
1402 */
1403 printk("rs_close: bad serial port count; tty->count is 1, "
1404 "state->count is %d\n", state->count);
1405 state->count = 1;
1406 }
1407 if (--state->count < 0) {
1408 printk("rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slabyd8522562012-03-05 14:52:16 +01001409 state->line, state->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 state->count = 0;
1411 }
1412 if (state->count) {
1413 DBG_CNT("before DEC-2");
1414 local_irq_restore(flags);
1415 return;
1416 }
Jiri Slabyfef21072012-03-05 14:52:14 +01001417 state->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * Now we wait for the transmit buffer to clear; and we notify
1420 * the line discipline to only process XON/XOFF characters.
1421 */
1422 tty->closing = 1;
Jiri Slabyd8522562012-03-05 14:52:16 +01001423 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1424 tty_wait_until_sent(tty, state->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /*
1426 * At this point we stop accepting input. To do this, we
1427 * disable the receive line status interrupts, and tell the
1428 * interrupt driver to stop checking the data ready bit in the
1429 * line status register.
1430 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001431 state->read_status_mask &= ~UART_LSR_DR;
Jiri Slabyfef21072012-03-05 14:52:14 +01001432 if (state->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* disable receive interrupts */
1434 custom.intena = IF_RBF;
1435 mb();
1436 /* clear any pending receive interrupt */
1437 custom.intreq = IF_RBF;
1438 mb();
1439
1440 /*
1441 * Before we drop DTR, make sure the UART transmitter
1442 * has completely drained; this is especially
1443 * important if there is a transmit FIFO!
1444 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001445 rs_wait_until_sent(tty, state->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001447 shutdown(state);
Alan Cox978e5952008-04-30 00:53:59 -07001448 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 tty_ldisc_flush(tty);
1451 tty->closing = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +01001452 state->tty = NULL;
1453 if (state->blocked_open) {
Jiri Slabyd8522562012-03-05 14:52:16 +01001454 if (state->close_delay) {
1455 msleep_interruptible(jiffies_to_msecs(state->close_delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001457 wake_up_interruptible(&state->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Jiri Slabyfef21072012-03-05 14:52:14 +01001459 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby916b7652012-03-05 14:52:20 +01001460 wake_up_interruptible(&state->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 local_irq_restore(flags);
1462}
1463
1464/*
1465 * rs_wait_until_sent() --- wait until the transmitter is empty
1466 */
1467static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1468{
Jiri Slaby916b7652012-03-05 14:52:20 +01001469 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 unsigned long orig_jiffies, char_time;
1471 int lsr;
1472
1473 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1474 return;
1475
Jiri Slaby916b7652012-03-05 14:52:20 +01001476 if (info->xmit_fifo_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return; /* Just in case.... */
1478
1479 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001480
Arnd Bergmann20365212010-06-01 22:53:07 +02001481 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * Set the check interval to be 1/5 of the estimated time to
1483 * send a single character, and make it at least 1. The check
1484 * interval should also be less than the timeout.
1485 *
1486 * Note: we have to use pretty tight timings here to satisfy
1487 * the NIST-PCTS.
1488 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001489 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 char_time = char_time / 5;
1491 if (char_time == 0)
1492 char_time = 1;
1493 if (timeout)
1494 char_time = min_t(unsigned long, char_time, timeout);
1495 /*
1496 * If the transmitter hasn't cleared in twice the approximate
1497 * amount of time to send the entire FIFO, it probably won't
1498 * ever clear. This assumes the UART isn't doing flow
1499 * control, which is currently the case. Hence, if it ever
1500 * takes longer than info->timeout, this is probably due to a
1501 * UART bug of some kind. So, we clamp the timeout parameter at
1502 * 2*info->timeout.
1503 */
1504 if (!timeout || timeout > 2*info->timeout)
1505 timeout = 2*info->timeout;
1506#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1507 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1508 printk("jiff=%lu...", jiffies);
1509#endif
1510 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1511#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1512 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1513#endif
1514 msleep_interruptible(jiffies_to_msecs(char_time));
1515 if (signal_pending(current))
1516 break;
1517 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1518 break;
1519 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001520 __set_current_state(TASK_RUNNING);
Jiri Slabyeff4b0b2011-07-14 14:35:13 +02001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1523 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1524#endif
1525}
1526
1527/*
1528 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1529 */
1530static void rs_hangup(struct tty_struct *tty)
1531{
Jiri Slaby916b7652012-03-05 14:52:20 +01001532 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1535 return;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 rs_flush_buffer(tty);
1538 shutdown(info);
Jiri Slaby916b7652012-03-05 14:52:20 +01001539 info->count = 0;
1540 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 info->tty = NULL;
1542 wake_up_interruptible(&info->open_wait);
1543}
1544
1545/*
1546 * ------------------------------------------------------------
1547 * rs_open() and friends
1548 * ------------------------------------------------------------
1549 */
1550static int block_til_ready(struct tty_struct *tty, struct file * filp,
Jiri Slaby916b7652012-03-05 14:52:20 +01001551 struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553#ifdef DECLARE_WAITQUEUE
1554 DECLARE_WAITQUEUE(wait, current);
1555#else
1556 struct wait_queue wait = { current, NULL };
1557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int retval;
1559 int do_clocal = 0, extra_count = 0;
1560 unsigned long flags;
1561
1562 /*
1563 * If the device is in the middle of being closed, then block
1564 * until it's done, and then try again.
1565 */
1566 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001567 (info->flags & ASYNC_CLOSING)) {
1568 if (info->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 interruptible_sleep_on(&info->close_wait);
1570#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001571 return ((info->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 -EAGAIN : -ERESTARTSYS);
1573#else
1574 return -EAGAIN;
1575#endif
1576 }
1577
1578 /*
1579 * If non-blocking mode is set, or the port is not enabled,
1580 * then make the check up front and then exit.
1581 */
1582 if ((filp->f_flags & O_NONBLOCK) ||
1583 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby916b7652012-03-05 14:52:20 +01001584 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586 }
1587
1588 if (tty->termios->c_cflag & CLOCAL)
1589 do_clocal = 1;
1590
1591 /*
1592 * Block waiting for the carrier detect and the line to become
1593 * free (i.e., not in use by the callout). While we are in
Jiri Slaby916b7652012-03-05 14:52:20 +01001594 * this loop, info->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * rs_close() knows when to free things. We restore it upon
1596 * exit, either normal or abnormal.
1597 */
1598 retval = 0;
1599 add_wait_queue(&info->open_wait, &wait);
1600#ifdef SERIAL_DEBUG_OPEN
1601 printk("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slaby916b7652012-03-05 14:52:20 +01001602 info->line, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603#endif
1604 local_irq_save(flags);
1605 if (!tty_hung_up_p(filp)) {
1606 extra_count = 1;
Jiri Slaby916b7652012-03-05 14:52:20 +01001607 info->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 local_irq_restore(flags);
1610 info->blocked_open++;
1611 while (1) {
1612 local_irq_save(flags);
1613 if (tty->termios->c_cflag & CBAUD)
1614 rtsdtr_ctrl(SER_DTR|SER_RTS);
1615 local_irq_restore(flags);
1616 set_current_state(TASK_INTERRUPTIBLE);
1617 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001618 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001620 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 retval = -EAGAIN;
1622 else
1623 retval = -ERESTARTSYS;
1624#else
1625 retval = -EAGAIN;
1626#endif
1627 break;
1628 }
Jiri Slaby916b7652012-03-05 14:52:20 +01001629 if (!(info->flags & ASYNC_CLOSING) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1631 break;
1632 if (signal_pending(current)) {
1633 retval = -ERESTARTSYS;
1634 break;
1635 }
1636#ifdef SERIAL_DEBUG_OPEN
1637 printk("block_til_ready blocking: ttys%d, count = %d\n",
Jiri Slaby916b7652012-03-05 14:52:20 +01001638 info->line, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639#endif
Arnd Bergmanne142a312010-06-01 22:53:10 +02001640 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001642 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001644 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 remove_wait_queue(&info->open_wait, &wait);
1646 if (extra_count)
Jiri Slaby916b7652012-03-05 14:52:20 +01001647 info->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 info->blocked_open--;
1649#ifdef SERIAL_DEBUG_OPEN
1650 printk("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slaby916b7652012-03-05 14:52:20 +01001651 info->line, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652#endif
1653 if (retval)
1654 return retval;
Jiri Slaby916b7652012-03-05 14:52:20 +01001655 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657}
1658
1659/*
1660 * This routine is called whenever a serial port is opened. It
1661 * enables interrupts for a serial port, linking in its async structure into
1662 * the IRQ chain. It also performs the serial-specific
1663 * initialization for the tty structure.
1664 */
1665static int rs_open(struct tty_struct *tty, struct file * filp)
1666{
Jiri Slaby916b7652012-03-05 14:52:20 +01001667 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +01001668 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jiri Slaby916b7652012-03-05 14:52:20 +01001670 info->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 info->tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +01001672 tty->driver_data = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (serial_paranoia_check(info, tty->name, "rs_open"))
1674 return -ENODEV;
1675
1676#ifdef SERIAL_DEBUG_OPEN
Jiri Slaby916b7652012-03-05 14:52:20 +01001677 printk("rs_open %s, count = %d\n", tty->name, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678#endif
Jiri Slaby916b7652012-03-05 14:52:20 +01001679 tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * If the port is the middle of closing, bail out now
1683 */
1684 if (tty_hung_up_p(filp) ||
Jiri Slaby916b7652012-03-05 14:52:20 +01001685 (info->flags & ASYNC_CLOSING)) {
1686 if (info->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 interruptible_sleep_on(&info->close_wait);
1688#ifdef SERIAL_DO_RESTART
Jiri Slaby916b7652012-03-05 14:52:20 +01001689 return ((info->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 -EAGAIN : -ERESTARTSYS);
1691#else
1692 return -EAGAIN;
1693#endif
1694 }
1695
1696 /*
1697 * Start up serial port
1698 */
1699 retval = startup(info);
1700 if (retval) {
1701 return retval;
1702 }
1703
1704 retval = block_til_ready(tty, filp, info);
1705 if (retval) {
1706#ifdef SERIAL_DEBUG_OPEN
1707 printk("rs_open returning after block_til_ready with %d\n",
1708 retval);
1709#endif
1710 return retval;
1711 }
1712
1713#ifdef SERIAL_DEBUG_OPEN
1714 printk("rs_open %s successful...", tty->name);
1715#endif
1716 return 0;
1717}
1718
1719/*
1720 * /proc fs routines....
1721 */
1722
Alexey Dobriyand5940272009-03-31 15:19:23 -07001723static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 unsigned long flags;
1727
Alexey Dobriyand5940272009-03-31 15:19:23 -07001728 seq_printf(m, "%d: uart:amiga_builtin",state->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 local_irq_save(flags);
1731 status = ciab.pra;
Jiri Slaby916b7652012-03-05 14:52:20 +01001732 control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 local_irq_restore(flags);
1734
1735 stat_buf[0] = 0;
1736 stat_buf[1] = 0;
1737 if(!(control & SER_RTS))
1738 strcat(stat_buf, "|RTS");
1739 if(!(status & SER_CTS))
1740 strcat(stat_buf, "|CTS");
1741 if(!(control & SER_DTR))
1742 strcat(stat_buf, "|DTR");
1743 if(!(status & SER_DSR))
1744 strcat(stat_buf, "|DSR");
1745 if(!(status & SER_DCD))
1746 strcat(stat_buf, "|CD");
1747
Jiri Slaby916b7652012-03-05 14:52:20 +01001748 if (state->quot)
1749 seq_printf(m, " baud:%d", state->baud_base / state->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Alexey Dobriyand5940272009-03-31 15:19:23 -07001751 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001754 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001757 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001760 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001763 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 /*
1766 * Last thing is the RS-232 status lines
1767 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001768 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Alexey Dobriyand5940272009-03-31 15:19:23 -07001771static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Alexey Dobriyand5940272009-03-31 15:19:23 -07001773 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1774 line_info(m, &rs_table[0]);
1775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Alexey Dobriyand5940272009-03-31 15:19:23 -07001778static int rs_proc_open(struct inode *inode, struct file *file)
1779{
1780 return single_open(file, rs_proc_show, NULL);
1781}
1782
1783static const struct file_operations rs_proc_fops = {
1784 .owner = THIS_MODULE,
1785 .open = rs_proc_open,
1786 .read = seq_read,
1787 .llseek = seq_lseek,
1788 .release = single_release,
1789};
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
1792 * ---------------------------------------------------------------------
1793 * rs_init() and friends
1794 *
1795 * rs_init() is called at boot-time to initialize the serial driver.
1796 * ---------------------------------------------------------------------
1797 */
1798
1799/*
1800 * This routine prints out the appropriate serial driver version
1801 * number, and identifies which options were configured into this
1802 * driver.
1803 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001804static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1807}
1808
1809
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001810static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 .open = rs_open,
1812 .close = rs_close,
1813 .write = rs_write,
1814 .put_char = rs_put_char,
1815 .flush_chars = rs_flush_chars,
1816 .write_room = rs_write_room,
1817 .chars_in_buffer = rs_chars_in_buffer,
1818 .flush_buffer = rs_flush_buffer,
1819 .ioctl = rs_ioctl,
1820 .throttle = rs_throttle,
1821 .unthrottle = rs_unthrottle,
1822 .set_termios = rs_set_termios,
1823 .stop = rs_stop,
1824 .start = rs_start,
1825 .hangup = rs_hangup,
1826 .break_ctl = rs_break,
1827 .send_xchar = rs_send_xchar,
1828 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 .tiocmget = rs_tiocmget,
1830 .tiocmset = rs_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001831 .get_icount = rs_get_icount,
Alexey Dobriyand5940272009-03-31 15:19:23 -07001832 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833};
1834
1835/*
1836 * The serial driver boot-time initialization code!
1837 */
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001838static int __init amiga_serial_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 unsigned long flags;
1841 struct serial_state * state;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001842 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jiri Slaby410235f2012-03-05 14:52:01 +01001844 serial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (!serial_driver)
1846 return -ENOMEM;
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 show_serial_version();
1849
1850 /* Initialize the tty_driver structure */
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 serial_driver->driver_name = "amiserial";
1853 serial_driver->name = "ttyS";
1854 serial_driver->major = TTY_MAJOR;
1855 serial_driver->minor_start = 64;
1856 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1857 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1858 serial_driver->init_termios = tty_std_termios;
1859 serial_driver->init_termios.c_cflag =
1860 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1861 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1862 tty_set_operations(serial_driver, &serial_ops);
1863
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001864 error = tty_register_driver(serial_driver);
1865 if (error)
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001866 goto fail_put_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 state = rs_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 state->port = (int)&custom.serdatr; /* Just to give it a value */
1870 state->line = 0;
1871 state->custom_divisor = 0;
1872 state->close_delay = 5*HZ/10;
1873 state->closing_wait = 30*HZ;
1874 state->icount.cts = state->icount.dsr =
1875 state->icount.rng = state->icount.dcd = 0;
1876 state->icount.rx = state->icount.tx = 0;
1877 state->icount.frame = state->icount.parity = 0;
1878 state->icount.overrun = state->icount.brk = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +01001879 init_waitqueue_head(&state->open_wait);
1880 init_waitqueue_head(&state->close_wait);
1881 init_waitqueue_head(&state->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1884 state->line);
1885
1886 /* Hardware set up */
1887
1888 state->baud_base = amiga_colorclock;
1889 state->xmit_fifo_size = 1;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001892 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1893 if (error)
1894 goto fail_unregister;
1895
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001896 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001897 "serial RX", state);
1898 if (error)
1899 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Julia Lawallc70c0362010-04-06 14:34:46 -07001901 local_irq_save(flags);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* turn off Rx and Tx interrupts */
1904 custom.intena = IF_RBF | IF_TBE;
1905 mb();
1906
1907 /* clear any pending interrupt */
1908 custom.intreq = IF_RBF | IF_TBE;
1909 mb();
1910
1911 local_irq_restore(flags);
1912
1913 /*
1914 * set the appropriate directions for the modem control flags,
1915 * and clear RTS and DTR
1916 */
1917 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1918 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1919
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001920 platform_set_drvdata(pdev, state);
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001923
1924fail_free_irq:
1925 free_irq(IRQ_AMIGA_TBE, state);
1926fail_unregister:
1927 tty_unregister_driver(serial_driver);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001928fail_put_tty_driver:
1929 put_tty_driver(serial_driver);
1930 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001933static int __exit amiga_serial_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 int error;
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001936 struct serial_state *state = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if ((error = tty_unregister_driver(serial_driver)))
1940 printk("SERIAL: failed to unregister serial driver (%d)\n",
1941 error);
1942 put_tty_driver(serial_driver);
1943
Jiri Slaby916b7652012-03-05 14:52:20 +01001944 free_irq(IRQ_AMIGA_TBE, state);
1945 free_irq(IRQ_AMIGA_RBF, state);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001946
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001947 platform_set_drvdata(pdev, NULL);
1948
1949 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001952static struct platform_driver amiga_serial_driver = {
1953 .remove = __exit_p(amiga_serial_remove),
1954 .driver = {
1955 .name = "amiga-serial",
1956 .owner = THIS_MODULE,
1957 },
1958};
1959
1960static int __init amiga_serial_init(void)
1961{
1962 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1963}
1964
1965module_init(amiga_serial_init);
1966
1967static void __exit amiga_serial_exit(void)
1968{
1969 platform_driver_unregister(&amiga_serial_driver);
1970}
1971
1972module_exit(amiga_serial_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01001975#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/*
1978 * ------------------------------------------------------------
1979 * Serial console driver
1980 * ------------------------------------------------------------
1981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983static void amiga_serial_putc(char c)
1984{
1985 custom.serdat = (unsigned char)c | 0x100;
1986 while (!(custom.serdatr & 0x2000))
1987 barrier();
1988}
1989
1990/*
1991 * Print a string to the serial port trying not to disturb
1992 * any possible real use of the port...
1993 *
1994 * The console must be locked when we get here.
1995 */
1996static void serial_console_write(struct console *co, const char *s,
1997 unsigned count)
1998{
1999 unsigned short intena = custom.intenar;
2000
2001 custom.intena = IF_TBE;
2002
2003 while (count--) {
2004 if (*s == '\n')
2005 amiga_serial_putc('\r');
2006 amiga_serial_putc(*s++);
2007 }
2008
2009 custom.intena = IF_SETCLR | (intena & IF_TBE);
2010}
2011
2012static struct tty_driver *serial_console_device(struct console *c, int *index)
2013{
2014 *index = 0;
2015 return serial_driver;
2016}
2017
2018static struct console sercons = {
2019 .name = "ttyS",
2020 .write = serial_console_write,
2021 .device = serial_console_device,
2022 .flags = CON_PRINTBUFFER,
2023 .index = -1,
2024};
2025
2026/*
2027 * Register console.
2028 */
2029static int __init amiserial_console_init(void)
2030{
2031 register_console(&sercons);
2032 return 0;
2033}
2034console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01002035
2036#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038MODULE_LICENSE("GPL");
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02002039MODULE_ALIAS("platform:amiga-serial");