Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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 | ||
1da177e4 LT |
32 | #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 | ||
1da177e4 LT |
46 | #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT) |
47 | #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \ | |
916b7656 | 48 | tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s) |
1da177e4 LT |
49 | #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> | |
63 | static 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> | |
d594027d | 80 | #include <linux/seq_file.h> |
1da177e4 LT |
81 | #include <linux/slab.h> |
82 | #include <linux/init.h> | |
83 | #include <linux/bitops.h> | |
826e8c8c | 84 | #include <linux/platform_device.h> |
1da177e4 LT |
85 | |
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 | ||
b4290a23 | 95 | #define custom amiga_custom |
1da177e4 LT |
96 | static char *serial_name = "Amiga-builtin serial driver"; |
97 | ||
98 | static 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 | ||
1da177e4 LT |
103 | static unsigned char current_ctl_bits; |
104 | ||
916b7656 | 105 | static void change_speed(struct serial_state *info, struct ktermios *old); |
1da177e4 LT |
106 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout); |
107 | ||
108 | ||
109 | static struct serial_state rs_table[1]; | |
110 | ||
fe971071 | 111 | #define NR_PORTS ARRAY_SIZE(rs_table) |
1da177e4 | 112 | |
1da177e4 LT |
113 | #include <asm/uaccess.h> |
114 | ||
115 | #define serial_isroot() (capable(CAP_SYS_ADMIN)) | |
116 | ||
117 | ||
916b7656 | 118 | static inline int serial_paranoia_check(struct serial_state *info, |
1da177e4 LT |
119 | 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 | ||
156 | static __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 | */ | |
169 | static void rs_stop(struct tty_struct *tty) | |
170 | { | |
916b7656 | 171 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
172 | 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 | ||
189 | static void rs_start(struct tty_struct *tty) | |
190 | { | |
916b7656 | 191 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
192 | 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 | ||
916b7656 | 232 | static void receive_chars(struct serial_state *info) |
1da177e4 LT |
233 | { |
234 | int status; | |
235 | int serdatr; | |
236 | struct tty_struct *tty = info->tty; | |
33f0f88f | 237 | unsigned char ch, flag; |
1da177e4 | 238 | struct async_icount *icount; |
33f0f88f | 239 | int oe = 0; |
1da177e4 | 240 | |
916b7656 | 241 | icount = &info->icount; |
1da177e4 LT |
242 | |
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; | |
1da177e4 LT |
255 | icount->rx++; |
256 | ||
257 | #ifdef SERIAL_DEBUG_INTR | |
258 | printk("DR%02x:%02x...", ch, status); | |
259 | #endif | |
33f0f88f | 260 | flag = TTY_NORMAL; |
1da177e4 LT |
261 | |
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) | |
33f0f88f | 289 | goto out; |
1da177e4 LT |
290 | |
291 | status &= info->read_status_mask; | |
292 | ||
293 | if (status & (UART_LSR_BI)) { | |
294 | #ifdef SERIAL_DEBUG_INTR | |
295 | printk("handling break...."); | |
296 | #endif | |
33f0f88f | 297 | flag = TTY_BREAK; |
916b7656 | 298 | if (info->flags & ASYNC_SAK) |
1da177e4 LT |
299 | do_SAK(tty); |
300 | } else if (status & UART_LSR_PE) | |
33f0f88f | 301 | flag = TTY_PARITY; |
1da177e4 | 302 | else if (status & UART_LSR_FE) |
33f0f88f | 303 | flag = TTY_FRAME; |
1da177e4 LT |
304 | 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 | */ | |
33f0f88f | 310 | oe = 1; |
1da177e4 LT |
311 | } |
312 | } | |
33f0f88f AC |
313 | tty_insert_flip_char(tty, ch, flag); |
314 | if (oe == 1) | |
315 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | |
1da177e4 | 316 | tty_flip_buffer_push(tty); |
33f0f88f AC |
317 | out: |
318 | return; | |
1da177e4 LT |
319 | } |
320 | ||
916b7656 | 321 | static void transmit_chars(struct serial_state *info) |
1da177e4 LT |
322 | { |
323 | custom.intreq = IF_TBE; | |
324 | mb(); | |
325 | if (info->x_char) { | |
326 | custom.serdat = info->x_char | 0x100; | |
327 | mb(); | |
916b7656 | 328 | info->icount.tx++; |
1da177e4 LT |
329 | 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); | |
916b7656 | 344 | info->icount.tx++; |
1da177e4 LT |
345 | |
346 | if (CIRC_CNT(info->xmit.head, | |
347 | info->xmit.tail, | |
348 | SERIAL_XMIT_SIZE) < WAKEUP_CHARS) | |
c5f0508b | 349 | tty_wakeup(info->tty); |
1da177e4 LT |
350 | |
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 | ||
916b7656 | 361 | static void check_modem_status(struct serial_state *info) |
1da177e4 LT |
362 | { |
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) { | |
916b7656 | 372 | icount = &info->icount; |
1da177e4 LT |
373 | /* 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 | |
916b7656 | 379 | if ((info->flags & ASYNC_HARDPPS_CD) && |
1da177e4 LT |
380 | !(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 | ||
916b7656 | 389 | if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) { |
1da177e4 LT |
390 | #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 | } | |
916b7656 | 404 | if (info->flags & ASYNC_CTS_FLOW) { |
1da177e4 LT |
405 | 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(); | |
c5f0508b | 417 | tty_wakeup(info->tty); |
1da177e4 LT |
418 | 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 | ||
7d12e780 | 437 | static irqreturn_t ser_vbl_int( int irq, void *data) |
1da177e4 LT |
438 | { |
439 | /* vbl is just a periodic interrupt we tie into to update modem status */ | |
916b7656 | 440 | struct serial_state *info = data; |
1da177e4 LT |
441 | /* |
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 | ||
7d12e780 | 450 | static irqreturn_t ser_rx_int(int irq, void *dev_id) |
1da177e4 | 451 | { |
916b7656 | 452 | struct serial_state *info = dev_id; |
1da177e4 LT |
453 | |
454 | #ifdef SERIAL_DEBUG_INTR | |
455 | printk("ser_rx_int..."); | |
456 | #endif | |
457 | ||
916b7656 | 458 | if (!info->tty) |
1da177e4 LT |
459 | return IRQ_NONE; |
460 | ||
461 | receive_chars(info); | |
1da177e4 LT |
462 | #ifdef SERIAL_DEBUG_INTR |
463 | printk("end.\n"); | |
464 | #endif | |
465 | return IRQ_HANDLED; | |
466 | } | |
467 | ||
7d12e780 | 468 | static irqreturn_t ser_tx_int(int irq, void *dev_id) |
1da177e4 | 469 | { |
916b7656 | 470 | struct serial_state *info = dev_id; |
1da177e4 LT |
471 | |
472 | if (custom.serdatr & SDR_TBE) { | |
473 | #ifdef SERIAL_DEBUG_INTR | |
474 | printk("ser_tx_int..."); | |
475 | #endif | |
476 | ||
916b7656 | 477 | if (!info->tty) |
1da177e4 LT |
478 | return IRQ_NONE; |
479 | ||
480 | transmit_chars(info); | |
1da177e4 LT |
481 | #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 | ||
1da177e4 LT |
494 | /* |
495 | * --------------------------------------------------------------- | |
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 | ||
916b7656 | 503 | static int startup(struct serial_state *info) |
1da177e4 LT |
504 | { |
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 | ||
916b7656 | 515 | if (info->flags & ASYNC_INITIALIZED) { |
1da177e4 LT |
516 | 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 | ||
1da177e4 LT |
553 | 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) { | |
916b7656 | 566 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
1da177e4 | 567 | info->tty->alt_speed = 57600; |
916b7656 | 568 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
1da177e4 | 569 | info->tty->alt_speed = 115200; |
916b7656 | 570 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
1da177e4 | 571 | info->tty->alt_speed = 230400; |
916b7656 | 572 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
1da177e4 LT |
573 | info->tty->alt_speed = 460800; |
574 | } | |
575 | ||
576 | /* | |
577 | * and set the speed of the serial port | |
578 | */ | |
579 | change_speed(info, NULL); | |
580 | ||
916b7656 | 581 | info->flags |= ASYNC_INITIALIZED; |
1da177e4 LT |
582 | local_irq_restore(flags); |
583 | return 0; | |
584 | ||
585 | errout: | |
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 | */ | |
916b7656 | 594 | static void shutdown(struct serial_state *info) |
1da177e4 LT |
595 | { |
596 | unsigned long flags; | |
597 | struct serial_state *state; | |
598 | ||
916b7656 | 599 | if (!(info->flags & ASYNC_INITIALIZED)) |
1da177e4 LT |
600 | return; |
601 | ||
916b7656 | 602 | state = info; |
1da177e4 LT |
603 | |
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 | ||
1da177e4 LT |
616 | /* |
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 | ||
916b7656 | 641 | info->flags &= ~ASYNC_INITIALIZED; |
1da177e4 LT |
642 | 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 | */ | |
916b7656 | 650 | static void change_speed(struct serial_state *info, |
606d099c | 651 | struct ktermios *old_termios) |
1da177e4 LT |
652 | { |
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 */ | |
916b7656 | 684 | baud_base = info->baud_base; |
1da177e4 | 685 | if (baud == 38400 && |
916b7656 JS |
686 | ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) |
687 | quot = info->custom_divisor; | |
1da177e4 LT |
688 | 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) { | |
db0ef08e | 697 | /* FIXME: Will need updating for new tty in the end */ |
1da177e4 LT |
698 | 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 && | |
916b7656 JS |
704 | ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) |
705 | quot = info->custom_divisor; | |
1da177e4 LT |
706 | 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; | |
916b7656 | 718 | info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base); |
1da177e4 LT |
719 | 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; | |
916b7656 | 723 | if (info->flags & ASYNC_HARDPPS_CD) |
1da177e4 LT |
724 | info->IER |= UART_IER_MSI; |
725 | if (cflag & CRTSCTS) { | |
916b7656 | 726 | info->flags |= ASYNC_CTS_FLOW; |
1da177e4 LT |
727 | info->IER |= UART_IER_MSI; |
728 | } else | |
916b7656 | 729 | info->flags &= ~ASYNC_CTS_FLOW; |
1da177e4 | 730 | if (cflag & CLOCAL) |
916b7656 | 731 | info->flags &= ~ASYNC_CHECK_CD; |
1da177e4 | 732 | else { |
916b7656 | 733 | info->flags |= ASYNC_CHECK_CD; |
1da177e4 LT |
734 | info->IER |= UART_IER_MSI; |
735 | } | |
736 | /* TBD: | |
4b512d26 | 737 | * Does clearing IER_MSI imply that we should disable the VBL interrupt ? |
1da177e4 LT |
738 | */ |
739 | ||
740 | /* | |
741 | * Set up parity check flag | |
742 | */ | |
1da177e4 LT |
743 | |
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 | ||
1da177e4 LT |
787 | local_irq_restore(flags); |
788 | } | |
789 | ||
257afa3c | 790 | static int rs_put_char(struct tty_struct *tty, unsigned char ch) |
1da177e4 | 791 | { |
916b7656 | 792 | struct serial_state *info; |
1da177e4 LT |
793 | unsigned long flags; |
794 | ||
d9ad7ef1 TJRMI |
795 | info = tty->driver_data; |
796 | ||
1da177e4 | 797 | if (serial_paranoia_check(info, tty->name, "rs_put_char")) |
257afa3c | 798 | return 0; |
1da177e4 | 799 | |
d9ad7ef1 | 800 | if (!info->xmit.buf) |
257afa3c | 801 | return 0; |
1da177e4 LT |
802 | |
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); | |
257afa3c | 808 | return 0; |
1da177e4 LT |
809 | } |
810 | ||
811 | info->xmit.buf[info->xmit.head++] = ch; | |
812 | info->xmit.head &= SERIAL_XMIT_SIZE-1; | |
813 | local_irq_restore(flags); | |
257afa3c | 814 | return 1; |
1da177e4 LT |
815 | } |
816 | ||
817 | static void rs_flush_chars(struct tty_struct *tty) | |
818 | { | |
916b7656 | 819 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
820 | 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 | ||
841 | static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count) | |
842 | { | |
843 | int c, ret = 0; | |
916b7656 | 844 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
845 | unsigned long flags; |
846 | ||
847 | if (serial_paranoia_check(info, tty->name, "rs_write")) | |
848 | return 0; | |
849 | ||
b3218a79 | 850 | if (!info->xmit.buf) |
1da177e4 LT |
851 | return 0; |
852 | ||
3abf3bed | 853 | local_irq_save(flags); |
1da177e4 LT |
854 | 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 | ||
888 | static int rs_write_room(struct tty_struct *tty) | |
889 | { | |
916b7656 | 890 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
891 | |
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 | ||
897 | static int rs_chars_in_buffer(struct tty_struct *tty) | |
898 | { | |
916b7656 | 899 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
900 | |
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 | ||
906 | static void rs_flush_buffer(struct tty_struct *tty) | |
907 | { | |
916b7656 | 908 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
909 | 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); | |
1da177e4 LT |
916 | tty_wakeup(tty); |
917 | } | |
918 | ||
919 | /* | |
920 | * This function is used to send a high-priority XON/XOFF character to | |
921 | * the device | |
922 | */ | |
923 | static void rs_send_xchar(struct tty_struct *tty, char ch) | |
924 | { | |
916b7656 | 925 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
926 | 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 | */ | |
958 | static void rs_throttle(struct tty_struct * tty) | |
959 | { | |
916b7656 | 960 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
961 | 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 | ||
983 | static void rs_unthrottle(struct tty_struct * tty) | |
984 | { | |
916b7656 | 985 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
986 | 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 | ||
916b7656 | 1016 | static int get_serial_info(struct serial_state *state, |
ab14caec | 1017 | struct serial_struct __user * retinfo) |
1da177e4 LT |
1018 | { |
1019 | struct serial_struct tmp; | |
1da177e4 LT |
1020 | |
1021 | if (!retinfo) | |
1022 | return -EFAULT; | |
1023 | memset(&tmp, 0, sizeof(tmp)); | |
ec79d605 | 1024 | tty_lock(); |
1da177e4 LT |
1025 | 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; | |
ec79d605 | 1035 | tty_unlock(); |
1da177e4 LT |
1036 | if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) |
1037 | return -EFAULT; | |
1038 | return 0; | |
1039 | } | |
1040 | ||
916b7656 | 1041 | static int set_serial_info(struct serial_state *state, |
ab14caec | 1042 | struct serial_struct __user * new_info) |
1da177e4 LT |
1043 | { |
1044 | struct serial_struct new_serial; | |
916b7656 | 1045 | struct serial_state old_state; |
1da177e4 LT |
1046 | 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; | |
e18ce49b | 1051 | |
ec79d605 | 1052 | tty_lock(); |
1da177e4 LT |
1053 | old_state = *state; |
1054 | ||
1055 | change_irq = new_serial.irq != state->irq; | |
1056 | change_port = (new_serial.port != state->port); | |
e18ce49b | 1057 | if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) { |
ec79d605 | 1058 | tty_unlock(); |
1da177e4 | 1059 | return -EINVAL; |
e18ce49b | 1060 | } |
1da177e4 LT |
1061 | |
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)); | |
1da177e4 LT |
1071 | state->custom_divisor = new_serial.custom_divisor; |
1072 | goto check_and_exit; | |
1073 | } | |
1074 | ||
e18ce49b | 1075 | if (new_serial.baud_base < 9600) { |
ec79d605 | 1076 | tty_unlock(); |
1da177e4 | 1077 | return -EINVAL; |
e18ce49b | 1078 | } |
1da177e4 LT |
1079 | |
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)); | |
1da177e4 LT |
1088 | 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; | |
916b7656 | 1091 | state->tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
1da177e4 LT |
1092 | |
1093 | check_and_exit: | |
fef21073 | 1094 | if (state->flags & ASYNC_INITIALIZED) { |
1da177e4 LT |
1095 | 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) | |
916b7656 | 1099 | state->tty->alt_speed = 57600; |
1da177e4 | 1100 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
916b7656 | 1101 | state->tty->alt_speed = 115200; |
1da177e4 | 1102 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
916b7656 | 1103 | state->tty->alt_speed = 230400; |
1da177e4 | 1104 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
916b7656 JS |
1105 | state->tty->alt_speed = 460800; |
1106 | change_speed(state, NULL); | |
1da177e4 LT |
1107 | } |
1108 | } else | |
916b7656 | 1109 | retval = startup(state); |
ec79d605 | 1110 | tty_unlock(); |
1da177e4 LT |
1111 | 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 | */ | |
916b7656 | 1125 | static int get_lsr_info(struct serial_state *info, unsigned int __user *value) |
1da177e4 LT |
1126 | { |
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 | ||
60b33c13 | 1142 | static int rs_tiocmget(struct tty_struct *tty) |
1da177e4 | 1143 | { |
916b7656 | 1144 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1145 | 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 | ||
20b9d177 AC |
1164 | static int rs_tiocmset(struct tty_struct *tty, unsigned int set, |
1165 | unsigned int clear) | |
1da177e4 | 1166 | { |
916b7656 | 1167 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1168 | 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 | */ | |
9e98966c | 1192 | static int rs_break(struct tty_struct *tty, int break_state) |
1da177e4 | 1193 | { |
916b7656 | 1194 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1195 | unsigned long flags; |
1196 | ||
1197 | if (serial_paranoia_check(info, tty->name, "rs_break")) | |
970a8a51 | 1198 | return -EINVAL; |
1da177e4 LT |
1199 | |
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); | |
9e98966c | 1207 | return 0; |
1da177e4 LT |
1208 | } |
1209 | ||
0587102c AC |
1210 | /* |
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 | */ | |
1216 | static int rs_get_icount(struct tty_struct *tty, | |
1217 | struct serial_icounter_struct *icount) | |
1218 | { | |
916b7656 | 1219 | struct serial_state *info = tty->driver_data; |
0587102c AC |
1220 | struct async_icount cnow; |
1221 | unsigned long flags; | |
1222 | ||
1223 | local_irq_save(flags); | |
916b7656 | 1224 | cnow = info->icount; |
0587102c AC |
1225 | 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 | } | |
1da177e4 | 1240 | |
6caa76b7 | 1241 | static int rs_ioctl(struct tty_struct *tty, |
1da177e4 LT |
1242 | unsigned int cmd, unsigned long arg) |
1243 | { | |
916b7656 | 1244 | struct serial_state *info = tty->driver_data; |
1da177e4 | 1245 | struct async_icount cprev, cnow; /* kernel counter temps */ |
ab14caec | 1246 | void __user *argp = (void __user *)arg; |
1da177e4 LT |
1247 | 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: | |
ab14caec | 1261 | return get_serial_info(info, argp); |
1da177e4 | 1262 | case TIOCSSERIAL: |
ab14caec | 1263 | return set_serial_info(info, argp); |
1da177e4 LT |
1264 | case TIOCSERCONFIG: |
1265 | return 0; | |
1266 | ||
1267 | case TIOCSERGETLSR: /* Get line status register */ | |
ab14caec | 1268 | return get_lsr_info(info, argp); |
1da177e4 LT |
1269 | |
1270 | case TIOCSERGSTRUCT: | |
ab14caec | 1271 | if (copy_to_user(argp, |
916b7656 | 1272 | info, sizeof(struct serial_state))) |
1da177e4 LT |
1273 | 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 */ | |
916b7656 | 1285 | cprev = info->icount; |
1da177e4 LT |
1286 | 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); | |
916b7656 | 1293 | cnow = info->icount; /* atomic copy */ |
1da177e4 LT |
1294 | 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 | ||
1da177e4 LT |
1308 | 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 | ||
606d099c | 1320 | static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios) |
1da177e4 | 1321 | { |
916b7656 | 1322 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1323 | unsigned long flags; |
1324 | unsigned int cflag = tty->termios->c_cflag; | |
1325 | ||
1da177e4 LT |
1326 | 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 | */ | |
1380 | static void rs_close(struct tty_struct *tty, struct file * filp) | |
1381 | { | |
916b7656 | 1382 | struct serial_state *state = tty->driver_data; |
1da177e4 LT |
1383 | unsigned long flags; |
1384 | ||
916b7656 | 1385 | if (!state || serial_paranoia_check(state, tty->name, "rs_close")) |
1da177e4 LT |
1386 | return; |
1387 | ||
1da177e4 LT |
1388 | 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 | |
916b7656 | 1397 | printk("rs_close ttys%d, count = %d\n", state->line, state->count); |
1da177e4 LT |
1398 | #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", | |
d8522563 | 1413 | state->line, state->count); |
1da177e4 LT |
1414 | state->count = 0; |
1415 | } | |
1416 | if (state->count) { | |
1417 | DBG_CNT("before DEC-2"); | |
1418 | local_irq_restore(flags); | |
1419 | return; | |
1420 | } | |
fef21073 | 1421 | state->flags |= ASYNC_CLOSING; |
1da177e4 LT |
1422 | /* |
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; | |
d8522563 JS |
1427 | if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
1428 | tty_wait_until_sent(tty, state->closing_wait); | |
1da177e4 LT |
1429 | /* |
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 | */ | |
916b7656 | 1435 | state->read_status_mask &= ~UART_LSR_DR; |
fef21073 | 1436 | if (state->flags & ASYNC_INITIALIZED) { |
1da177e4 LT |
1437 | /* 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 | */ | |
916b7656 | 1449 | rs_wait_until_sent(tty, state->timeout); |
1da177e4 | 1450 | } |
916b7656 | 1451 | shutdown(state); |
978e595f | 1452 | rs_flush_buffer(tty); |
1da177e4 LT |
1453 | |
1454 | tty_ldisc_flush(tty); | |
1455 | tty->closing = 0; | |
916b7656 JS |
1456 | state->tty = NULL; |
1457 | if (state->blocked_open) { | |
d8522563 JS |
1458 | if (state->close_delay) { |
1459 | msleep_interruptible(jiffies_to_msecs(state->close_delay)); | |
1da177e4 | 1460 | } |
916b7656 | 1461 | wake_up_interruptible(&state->open_wait); |
1da177e4 | 1462 | } |
fef21073 | 1463 | state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); |
916b7656 | 1464 | wake_up_interruptible(&state->close_wait); |
1da177e4 LT |
1465 | local_irq_restore(flags); |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * rs_wait_until_sent() --- wait until the transmitter is empty | |
1470 | */ | |
1471 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout) | |
1472 | { | |
916b7656 | 1473 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1474 | 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 | ||
916b7656 | 1480 | if (info->xmit_fifo_size == 0) |
1da177e4 LT |
1481 | return; /* Just in case.... */ |
1482 | ||
1483 | orig_jiffies = jiffies; | |
978e595f | 1484 | |
1da177e4 LT |
1485 | /* |
1486 | * 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 | */ | |
916b7656 | 1493 | char_time = (info->timeout - HZ/50) / info->xmit_fifo_size; |
1da177e4 LT |
1494 | 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 | } | |
cc0a8fbb | 1524 | __set_current_state(TASK_RUNNING); |
eff4b0b9 | 1525 | |
1da177e4 LT |
1526 | #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 | */ | |
1534 | static void rs_hangup(struct tty_struct *tty) | |
1535 | { | |
916b7656 | 1536 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1537 | |
1538 | if (serial_paranoia_check(info, tty->name, "rs_hangup")) | |
1539 | return; | |
1540 | ||
1da177e4 LT |
1541 | rs_flush_buffer(tty); |
1542 | shutdown(info); | |
916b7656 JS |
1543 | info->count = 0; |
1544 | info->flags &= ~ASYNC_NORMAL_ACTIVE; | |
1da177e4 LT |
1545 | info->tty = NULL; |
1546 | wake_up_interruptible(&info->open_wait); | |
1547 | } | |
1548 | ||
1549 | /* | |
1550 | * ------------------------------------------------------------ | |
1551 | * rs_open() and friends | |
1552 | * ------------------------------------------------------------ | |
1553 | */ | |
1554 | static int block_til_ready(struct tty_struct *tty, struct file * filp, | |
916b7656 | 1555 | struct serial_state *info) |
1da177e4 LT |
1556 | { |
1557 | #ifdef DECLARE_WAITQUEUE | |
1558 | DECLARE_WAITQUEUE(wait, current); | |
1559 | #else | |
1560 | struct wait_queue wait = { current, NULL }; | |
1561 | #endif | |
1da177e4 LT |
1562 | 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) || | |
916b7656 JS |
1571 | (info->flags & ASYNC_CLOSING)) { |
1572 | if (info->flags & ASYNC_CLOSING) | |
1da177e4 LT |
1573 | interruptible_sleep_on(&info->close_wait); |
1574 | #ifdef SERIAL_DO_RESTART | |
916b7656 | 1575 | return ((info->flags & ASYNC_HUP_NOTIFY) ? |
1da177e4 LT |
1576 | -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))) { | |
916b7656 | 1588 | info->flags |= ASYNC_NORMAL_ACTIVE; |
1da177e4 LT |
1589 | 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 | |
916b7656 | 1598 | * this loop, info->count is dropped by one, so that |
1da177e4 LT |
1599 | * 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", | |
916b7656 | 1606 | info->line, info->count); |
1da177e4 LT |
1607 | #endif |
1608 | local_irq_save(flags); | |
1609 | if (!tty_hung_up_p(filp)) { | |
1610 | extra_count = 1; | |
916b7656 | 1611 | info->count--; |
1da177e4 LT |
1612 | } |
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) || | |
916b7656 | 1622 | !(info->flags & ASYNC_INITIALIZED)) { |
1da177e4 | 1623 | #ifdef SERIAL_DO_RESTART |
916b7656 | 1624 | if (info->flags & ASYNC_HUP_NOTIFY) |
1da177e4 LT |
1625 | retval = -EAGAIN; |
1626 | else | |
1627 | retval = -ERESTARTSYS; | |
1628 | #else | |
1629 | retval = -EAGAIN; | |
1630 | #endif | |
1631 | break; | |
1632 | } | |
916b7656 | 1633 | if (!(info->flags & ASYNC_CLOSING) && |
1da177e4 LT |
1634 | (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", | |
916b7656 | 1642 | info->line, info->count); |
1da177e4 | 1643 | #endif |
e142a31d | 1644 | tty_unlock(); |
1da177e4 | 1645 | schedule(); |
e142a31d | 1646 | tty_lock(); |
1da177e4 | 1647 | } |
cc0a8fbb | 1648 | __set_current_state(TASK_RUNNING); |
1da177e4 LT |
1649 | remove_wait_queue(&info->open_wait, &wait); |
1650 | if (extra_count) | |
916b7656 | 1651 | info->count++; |
1da177e4 LT |
1652 | info->blocked_open--; |
1653 | #ifdef SERIAL_DEBUG_OPEN | |
1654 | printk("block_til_ready after blocking: ttys%d, count = %d\n", | |
916b7656 | 1655 | info->line, info->count); |
1da177e4 LT |
1656 | #endif |
1657 | if (retval) | |
1658 | return retval; | |
916b7656 | 1659 | info->flags |= ASYNC_NORMAL_ACTIVE; |
1da177e4 LT |
1660 | 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 | */ | |
1669 | static int rs_open(struct tty_struct *tty, struct file * filp) | |
1670 | { | |
916b7656 | 1671 | struct serial_state *info = rs_table + tty->index; |
410235fd | 1672 | int retval; |
1da177e4 | 1673 | |
916b7656 | 1674 | info->count++; |
1da177e4 | 1675 | info->tty = tty; |
916b7656 | 1676 | tty->driver_data = info; |
1da177e4 LT |
1677 | if (serial_paranoia_check(info, tty->name, "rs_open")) |
1678 | return -ENODEV; | |
1679 | ||
1680 | #ifdef SERIAL_DEBUG_OPEN | |
916b7656 | 1681 | printk("rs_open %s, count = %d\n", tty->name, info->count); |
1da177e4 | 1682 | #endif |
916b7656 | 1683 | tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
1da177e4 | 1684 | |
1da177e4 LT |
1685 | /* |
1686 | * If the port is the middle of closing, bail out now | |
1687 | */ | |
1688 | if (tty_hung_up_p(filp) || | |
916b7656 JS |
1689 | (info->flags & ASYNC_CLOSING)) { |
1690 | if (info->flags & ASYNC_CLOSING) | |
1da177e4 LT |
1691 | interruptible_sleep_on(&info->close_wait); |
1692 | #ifdef SERIAL_DO_RESTART | |
916b7656 | 1693 | return ((info->flags & ASYNC_HUP_NOTIFY) ? |
1da177e4 LT |
1694 | -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 | ||
d594027d | 1727 | static inline void line_info(struct seq_file *m, struct serial_state *state) |
1da177e4 | 1728 | { |
1da177e4 | 1729 | char stat_buf[30], control, status; |
1da177e4 LT |
1730 | unsigned long flags; |
1731 | ||
d594027d | 1732 | seq_printf(m, "%d: uart:amiga_builtin",state->line); |
1da177e4 | 1733 | |
1da177e4 LT |
1734 | local_irq_save(flags); |
1735 | status = ciab.pra; | |
916b7656 | 1736 | control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status; |
1da177e4 LT |
1737 | 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 | ||
916b7656 JS |
1752 | if (state->quot) |
1753 | seq_printf(m, " baud:%d", state->baud_base / state->quot); | |
1da177e4 | 1754 | |
d594027d | 1755 | seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx); |
1da177e4 LT |
1756 | |
1757 | if (state->icount.frame) | |
d594027d | 1758 | seq_printf(m, " fe:%d", state->icount.frame); |
1da177e4 LT |
1759 | |
1760 | if (state->icount.parity) | |
d594027d | 1761 | seq_printf(m, " pe:%d", state->icount.parity); |
1da177e4 LT |
1762 | |
1763 | if (state->icount.brk) | |
d594027d | 1764 | seq_printf(m, " brk:%d", state->icount.brk); |
1da177e4 LT |
1765 | |
1766 | if (state->icount.overrun) | |
d594027d | 1767 | seq_printf(m, " oe:%d", state->icount.overrun); |
1da177e4 LT |
1768 | |
1769 | /* | |
1770 | * Last thing is the RS-232 status lines | |
1771 | */ | |
d594027d | 1772 | seq_printf(m, " %s\n", stat_buf+1); |
1da177e4 LT |
1773 | } |
1774 | ||
d594027d | 1775 | static int rs_proc_show(struct seq_file *m, void *v) |
1da177e4 | 1776 | { |
d594027d AD |
1777 | seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version); |
1778 | line_info(m, &rs_table[0]); | |
1779 | return 0; | |
1da177e4 LT |
1780 | } |
1781 | ||
d594027d AD |
1782 | static int rs_proc_open(struct inode *inode, struct file *file) |
1783 | { | |
1784 | return single_open(file, rs_proc_show, NULL); | |
1785 | } | |
1786 | ||
1787 | static 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 | ||
1da177e4 LT |
1795 | /* |
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 | */ | |
41c28ff1 | 1808 | static void show_serial_version(void) |
1da177e4 LT |
1809 | { |
1810 | printk(KERN_INFO "%s version %s\n", serial_name, serial_version); | |
1811 | } | |
1812 | ||
1813 | ||
b68e31d0 | 1814 | static const struct tty_operations serial_ops = { |
1da177e4 LT |
1815 | .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, | |
1da177e4 LT |
1833 | .tiocmget = rs_tiocmget, |
1834 | .tiocmset = rs_tiocmset, | |
0587102c | 1835 | .get_icount = rs_get_icount, |
d594027d | 1836 | .proc_fops = &rs_proc_fops, |
1da177e4 LT |
1837 | }; |
1838 | ||
1839 | /* | |
1840 | * The serial driver boot-time initialization code! | |
1841 | */ | |
826e8c8c | 1842 | static int __init amiga_serial_probe(struct platform_device *pdev) |
1da177e4 LT |
1843 | { |
1844 | unsigned long flags; | |
1845 | struct serial_state * state; | |
5edc304f | 1846 | int error; |
1da177e4 | 1847 | |
410235fd | 1848 | serial_driver = alloc_tty_driver(NR_PORTS); |
1da177e4 LT |
1849 | if (!serial_driver) |
1850 | return -ENOMEM; | |
1851 | ||
1da177e4 LT |
1852 | show_serial_version(); |
1853 | ||
1854 | /* Initialize the tty_driver structure */ | |
1855 | ||
1da177e4 LT |
1856 | 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 | ||
5edc304f GU |
1868 | error = tty_register_driver(serial_driver); |
1869 | if (error) | |
826e8c8c | 1870 | goto fail_put_tty_driver; |
1da177e4 LT |
1871 | |
1872 | state = rs_table; | |
1da177e4 LT |
1873 | 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; | |
916b7656 JS |
1883 | init_waitqueue_head(&state->open_wait); |
1884 | init_waitqueue_head(&state->close_wait); | |
1885 | init_waitqueue_head(&state->delta_msr_wait); | |
1da177e4 LT |
1886 | |
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 | ||
1da177e4 | 1895 | /* set ISRs, and then disable the rx interrupts */ |
5edc304f GU |
1896 | error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state); |
1897 | if (error) | |
1898 | goto fail_unregister; | |
1899 | ||
9cfb5c05 | 1900 | error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0, |
5edc304f GU |
1901 | "serial RX", state); |
1902 | if (error) | |
1903 | goto fail_free_irq; | |
1da177e4 | 1904 | |
c70c036f JL |
1905 | local_irq_save(flags); |
1906 | ||
1da177e4 LT |
1907 | /* 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 | ||
826e8c8c GU |
1924 | platform_set_drvdata(pdev, state); |
1925 | ||
1da177e4 | 1926 | return 0; |
5edc304f GU |
1927 | |
1928 | fail_free_irq: | |
1929 | free_irq(IRQ_AMIGA_TBE, state); | |
1930 | fail_unregister: | |
1931 | tty_unregister_driver(serial_driver); | |
5edc304f GU |
1932 | fail_put_tty_driver: |
1933 | put_tty_driver(serial_driver); | |
1934 | return error; | |
1da177e4 LT |
1935 | } |
1936 | ||
826e8c8c | 1937 | static int __exit amiga_serial_remove(struct platform_device *pdev) |
1da177e4 LT |
1938 | { |
1939 | int error; | |
826e8c8c | 1940 | struct serial_state *state = platform_get_drvdata(pdev); |
1da177e4 LT |
1941 | |
1942 | /* printk("Unloading %s: version %s\n", serial_name, serial_version); */ | |
1da177e4 LT |
1943 | 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 | ||
916b7656 JS |
1948 | free_irq(IRQ_AMIGA_TBE, state); |
1949 | free_irq(IRQ_AMIGA_RBF, state); | |
5edc304f | 1950 | |
826e8c8c GU |
1951 | platform_set_drvdata(pdev, NULL); |
1952 | ||
1953 | return error; | |
1954 | } | |
1955 | ||
1956 | static 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 | ||
1964 | static int __init amiga_serial_init(void) | |
1965 | { | |
1966 | return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe); | |
1967 | } | |
1968 | ||
1969 | module_init(amiga_serial_init); | |
1970 | ||
1971 | static void __exit amiga_serial_exit(void) | |
1972 | { | |
1973 | platform_driver_unregister(&amiga_serial_driver); | |
1da177e4 LT |
1974 | } |
1975 | ||
826e8c8c | 1976 | module_exit(amiga_serial_exit); |
1da177e4 LT |
1977 | |
1978 | ||
d1a35e4d GU |
1979 | #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE) |
1980 | ||
1da177e4 LT |
1981 | /* |
1982 | * ------------------------------------------------------------ | |
1983 | * Serial console driver | |
1984 | * ------------------------------------------------------------ | |
1985 | */ | |
1da177e4 LT |
1986 | |
1987 | static 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 | */ | |
2000 | static 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 | ||
2016 | static struct tty_driver *serial_console_device(struct console *c, int *index) | |
2017 | { | |
2018 | *index = 0; | |
2019 | return serial_driver; | |
2020 | } | |
2021 | ||
2022 | static 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 | */ | |
2033 | static int __init amiserial_console_init(void) | |
2034 | { | |
2035 | register_console(&sercons); | |
2036 | return 0; | |
2037 | } | |
2038 | console_initcall(amiserial_console_init); | |
d1a35e4d GU |
2039 | |
2040 | #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */ | |
1da177e4 LT |
2041 | |
2042 | MODULE_LICENSE("GPL"); | |
826e8c8c | 2043 | MODULE_ALIAS("platform:amiga-serial"); |