]>
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", \ | |
01bd730d | 48 | tty->name, (info->tport.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 | ||
588993dd JS |
105 | static void change_speed(struct tty_struct *tty, struct serial_state *info, |
106 | struct ktermios *old); | |
1da177e4 LT |
107 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout); |
108 | ||
109 | ||
110 | static struct serial_state rs_table[1]; | |
111 | ||
fe971071 | 112 | #define NR_PORTS ARRAY_SIZE(rs_table) |
1da177e4 | 113 | |
1da177e4 LT |
114 | #include <asm/uaccess.h> |
115 | ||
116 | #define serial_isroot() (capable(CAP_SYS_ADMIN)) | |
117 | ||
118 | ||
916b7656 | 119 | static inline int serial_paranoia_check(struct serial_state *info, |
1da177e4 LT |
120 | char *name, const char *routine) |
121 | { | |
122 | #ifdef SERIAL_PARANOIA_CHECK | |
123 | static const char *badmagic = | |
124 | "Warning: bad magic number for serial struct (%s) in %s\n"; | |
125 | static const char *badinfo = | |
126 | "Warning: null async_struct for (%s) in %s\n"; | |
127 | ||
128 | if (!info) { | |
129 | printk(badinfo, name, routine); | |
130 | return 1; | |
131 | } | |
132 | if (info->magic != SERIAL_MAGIC) { | |
133 | printk(badmagic, name, routine); | |
134 | return 1; | |
135 | } | |
136 | #endif | |
137 | return 0; | |
138 | } | |
139 | ||
140 | /* some serial hardware definitions */ | |
141 | #define SDR_OVRUN (1<<15) | |
142 | #define SDR_RBF (1<<14) | |
143 | #define SDR_TBE (1<<13) | |
144 | #define SDR_TSRE (1<<12) | |
145 | ||
146 | #define SERPER_PARENB (1<<15) | |
147 | ||
148 | #define AC_SETCLR (1<<15) | |
149 | #define AC_UARTBRK (1<<11) | |
150 | ||
151 | #define SER_DTR (1<<7) | |
152 | #define SER_RTS (1<<6) | |
153 | #define SER_DCD (1<<5) | |
154 | #define SER_CTS (1<<4) | |
155 | #define SER_DSR (1<<3) | |
156 | ||
157 | static __inline__ void rtsdtr_ctrl(int bits) | |
158 | { | |
159 | ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR)); | |
160 | } | |
161 | ||
162 | /* | |
163 | * ------------------------------------------------------------ | |
164 | * rs_stop() and rs_start() | |
165 | * | |
166 | * This routines are called before setting or resetting tty->stopped. | |
167 | * They enable or disable transmitter interrupts, as necessary. | |
168 | * ------------------------------------------------------------ | |
169 | */ | |
170 | static void rs_stop(struct tty_struct *tty) | |
171 | { | |
916b7656 | 172 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
173 | unsigned long flags; |
174 | ||
175 | if (serial_paranoia_check(info, tty->name, "rs_stop")) | |
176 | return; | |
177 | ||
178 | local_irq_save(flags); | |
179 | if (info->IER & UART_IER_THRI) { | |
180 | info->IER &= ~UART_IER_THRI; | |
181 | /* disable Tx interrupt and remove any pending interrupts */ | |
182 | custom.intena = IF_TBE; | |
183 | mb(); | |
184 | custom.intreq = IF_TBE; | |
185 | mb(); | |
186 | } | |
187 | local_irq_restore(flags); | |
188 | } | |
189 | ||
190 | static void rs_start(struct tty_struct *tty) | |
191 | { | |
916b7656 | 192 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
193 | unsigned long flags; |
194 | ||
195 | if (serial_paranoia_check(info, tty->name, "rs_start")) | |
196 | return; | |
197 | ||
198 | local_irq_save(flags); | |
199 | if (info->xmit.head != info->xmit.tail | |
200 | && info->xmit.buf | |
201 | && !(info->IER & UART_IER_THRI)) { | |
202 | info->IER |= UART_IER_THRI; | |
203 | custom.intena = IF_SETCLR | IF_TBE; | |
204 | mb(); | |
205 | /* set a pending Tx Interrupt, transmitter should restart now */ | |
206 | custom.intreq = IF_SETCLR | IF_TBE; | |
207 | mb(); | |
208 | } | |
209 | local_irq_restore(flags); | |
210 | } | |
211 | ||
212 | /* | |
213 | * ---------------------------------------------------------------------- | |
214 | * | |
215 | * Here starts the interrupt handling routines. All of the following | |
216 | * subroutines are declared as inline and are folded into | |
217 | * rs_interrupt(). They were separated out for readability's sake. | |
218 | * | |
219 | * Note: rs_interrupt() is a "fast" interrupt, which means that it | |
220 | * runs with interrupts turned off. People who may want to modify | |
221 | * rs_interrupt() should try to keep the interrupt handler as fast as | |
222 | * possible. After you are done making modifications, it is not a bad | |
223 | * idea to do: | |
224 | * | |
225 | * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c | |
226 | * | |
227 | * and look at the resulting assemble code in serial.s. | |
228 | * | |
229 | * - Ted Ts'o (tytso@mit.edu), 7-Mar-93 | |
230 | * ----------------------------------------------------------------------- | |
231 | */ | |
232 | ||
916b7656 | 233 | static void receive_chars(struct serial_state *info) |
1da177e4 LT |
234 | { |
235 | int status; | |
236 | int serdatr; | |
87758791 | 237 | struct tty_struct *tty = info->tport.tty; |
33f0f88f | 238 | unsigned char ch, flag; |
1da177e4 | 239 | struct async_icount *icount; |
33f0f88f | 240 | int oe = 0; |
1da177e4 | 241 | |
916b7656 | 242 | icount = &info->icount; |
1da177e4 LT |
243 | |
244 | status = UART_LSR_DR; /* We obviously have a character! */ | |
245 | serdatr = custom.serdatr; | |
246 | mb(); | |
247 | custom.intreq = IF_RBF; | |
248 | mb(); | |
249 | ||
250 | if((serdatr & 0x1ff) == 0) | |
251 | status |= UART_LSR_BI; | |
252 | if(serdatr & SDR_OVRUN) | |
253 | status |= UART_LSR_OE; | |
254 | ||
255 | ch = serdatr & 0xff; | |
1da177e4 LT |
256 | icount->rx++; |
257 | ||
258 | #ifdef SERIAL_DEBUG_INTR | |
259 | printk("DR%02x:%02x...", ch, status); | |
260 | #endif | |
33f0f88f | 261 | flag = TTY_NORMAL; |
1da177e4 LT |
262 | |
263 | /* | |
264 | * We don't handle parity or frame errors - but I have left | |
265 | * the code in, since I'm not sure that the errors can't be | |
266 | * detected. | |
267 | */ | |
268 | ||
269 | if (status & (UART_LSR_BI | UART_LSR_PE | | |
270 | UART_LSR_FE | UART_LSR_OE)) { | |
271 | /* | |
272 | * For statistics only | |
273 | */ | |
274 | if (status & UART_LSR_BI) { | |
275 | status &= ~(UART_LSR_FE | UART_LSR_PE); | |
276 | icount->brk++; | |
277 | } else if (status & UART_LSR_PE) | |
278 | icount->parity++; | |
279 | else if (status & UART_LSR_FE) | |
280 | icount->frame++; | |
281 | if (status & UART_LSR_OE) | |
282 | icount->overrun++; | |
283 | ||
284 | /* | |
285 | * Now check to see if character should be | |
286 | * ignored, and mask off conditions which | |
287 | * should be ignored. | |
288 | */ | |
289 | if (status & info->ignore_status_mask) | |
33f0f88f | 290 | goto out; |
1da177e4 LT |
291 | |
292 | status &= info->read_status_mask; | |
293 | ||
294 | if (status & (UART_LSR_BI)) { | |
295 | #ifdef SERIAL_DEBUG_INTR | |
296 | printk("handling break...."); | |
297 | #endif | |
33f0f88f | 298 | flag = TTY_BREAK; |
01bd730d | 299 | if (info->tport.flags & ASYNC_SAK) |
1da177e4 LT |
300 | do_SAK(tty); |
301 | } else if (status & UART_LSR_PE) | |
33f0f88f | 302 | flag = TTY_PARITY; |
1da177e4 | 303 | else if (status & UART_LSR_FE) |
33f0f88f | 304 | flag = TTY_FRAME; |
1da177e4 LT |
305 | if (status & UART_LSR_OE) { |
306 | /* | |
307 | * Overrun is special, since it's | |
308 | * reported immediately, and doesn't | |
309 | * affect the current character | |
310 | */ | |
33f0f88f | 311 | oe = 1; |
1da177e4 LT |
312 | } |
313 | } | |
33f0f88f AC |
314 | tty_insert_flip_char(tty, ch, flag); |
315 | if (oe == 1) | |
316 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | |
1da177e4 | 317 | tty_flip_buffer_push(tty); |
33f0f88f AC |
318 | out: |
319 | return; | |
1da177e4 LT |
320 | } |
321 | ||
916b7656 | 322 | static void transmit_chars(struct serial_state *info) |
1da177e4 LT |
323 | { |
324 | custom.intreq = IF_TBE; | |
325 | mb(); | |
326 | if (info->x_char) { | |
327 | custom.serdat = info->x_char | 0x100; | |
328 | mb(); | |
916b7656 | 329 | info->icount.tx++; |
1da177e4 LT |
330 | info->x_char = 0; |
331 | return; | |
332 | } | |
333 | if (info->xmit.head == info->xmit.tail | |
87758791 JS |
334 | || info->tport.tty->stopped |
335 | || info->tport.tty->hw_stopped) { | |
1da177e4 LT |
336 | info->IER &= ~UART_IER_THRI; |
337 | custom.intena = IF_TBE; | |
338 | mb(); | |
339 | return; | |
340 | } | |
341 | ||
342 | custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100; | |
343 | mb(); | |
344 | info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1); | |
916b7656 | 345 | info->icount.tx++; |
1da177e4 LT |
346 | |
347 | if (CIRC_CNT(info->xmit.head, | |
348 | info->xmit.tail, | |
349 | SERIAL_XMIT_SIZE) < WAKEUP_CHARS) | |
87758791 | 350 | tty_wakeup(info->tport.tty); |
1da177e4 LT |
351 | |
352 | #ifdef SERIAL_DEBUG_INTR | |
353 | printk("THRE..."); | |
354 | #endif | |
355 | if (info->xmit.head == info->xmit.tail) { | |
356 | custom.intena = IF_TBE; | |
357 | mb(); | |
358 | info->IER &= ~UART_IER_THRI; | |
359 | } | |
360 | } | |
361 | ||
916b7656 | 362 | static void check_modem_status(struct serial_state *info) |
1da177e4 LT |
363 | { |
364 | unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR); | |
365 | unsigned char dstatus; | |
366 | struct async_icount *icount; | |
367 | ||
368 | /* Determine bits that have changed */ | |
369 | dstatus = status ^ current_ctl_bits; | |
370 | current_ctl_bits = status; | |
371 | ||
372 | if (dstatus) { | |
916b7656 | 373 | icount = &info->icount; |
1da177e4 LT |
374 | /* update input line counters */ |
375 | if (dstatus & SER_DSR) | |
376 | icount->dsr++; | |
377 | if (dstatus & SER_DCD) { | |
378 | icount->dcd++; | |
379 | #ifdef CONFIG_HARD_PPS | |
01bd730d | 380 | if ((info->tport.flags & ASYNC_HARDPPS_CD) && |
1da177e4 LT |
381 | !(status & SER_DCD)) |
382 | hardpps(); | |
383 | #endif | |
384 | } | |
385 | if (dstatus & SER_CTS) | |
386 | icount->cts++; | |
87758791 | 387 | wake_up_interruptible(&info->tport.delta_msr_wait); |
1da177e4 LT |
388 | } |
389 | ||
01bd730d | 390 | if ((info->tport.flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) { |
1da177e4 LT |
391 | #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR)) |
392 | printk("ttyS%d CD now %s...", info->line, | |
393 | (!(status & SER_DCD)) ? "on" : "off"); | |
394 | #endif | |
395 | if (!(status & SER_DCD)) | |
87758791 | 396 | wake_up_interruptible(&info->tport.open_wait); |
1da177e4 LT |
397 | else { |
398 | #ifdef SERIAL_DEBUG_OPEN | |
399 | printk("doing serial hangup..."); | |
400 | #endif | |
87758791 JS |
401 | if (info->tport.tty) |
402 | tty_hangup(info->tport.tty); | |
1da177e4 LT |
403 | } |
404 | } | |
01bd730d | 405 | if (info->tport.flags & ASYNC_CTS_FLOW) { |
87758791 | 406 | if (info->tport.tty->hw_stopped) { |
1da177e4 LT |
407 | if (!(status & SER_CTS)) { |
408 | #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW)) | |
409 | printk("CTS tx start..."); | |
410 | #endif | |
87758791 | 411 | info->tport.tty->hw_stopped = 0; |
1da177e4 LT |
412 | info->IER |= UART_IER_THRI; |
413 | custom.intena = IF_SETCLR | IF_TBE; | |
414 | mb(); | |
415 | /* set a pending Tx Interrupt, transmitter should restart now */ | |
416 | custom.intreq = IF_SETCLR | IF_TBE; | |
417 | mb(); | |
87758791 | 418 | tty_wakeup(info->tport.tty); |
1da177e4 LT |
419 | return; |
420 | } | |
421 | } else { | |
422 | if ((status & SER_CTS)) { | |
423 | #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW)) | |
424 | printk("CTS tx stop..."); | |
425 | #endif | |
87758791 | 426 | info->tport.tty->hw_stopped = 1; |
1da177e4 LT |
427 | info->IER &= ~UART_IER_THRI; |
428 | /* disable Tx interrupt and remove any pending interrupts */ | |
429 | custom.intena = IF_TBE; | |
430 | mb(); | |
431 | custom.intreq = IF_TBE; | |
432 | mb(); | |
433 | } | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
7d12e780 | 438 | static irqreturn_t ser_vbl_int( int irq, void *data) |
1da177e4 LT |
439 | { |
440 | /* vbl is just a periodic interrupt we tie into to update modem status */ | |
916b7656 | 441 | struct serial_state *info = data; |
1da177e4 LT |
442 | /* |
443 | * TBD - is it better to unregister from this interrupt or to | |
444 | * ignore it if MSI is clear ? | |
445 | */ | |
446 | if(info->IER & UART_IER_MSI) | |
447 | check_modem_status(info); | |
448 | return IRQ_HANDLED; | |
449 | } | |
450 | ||
7d12e780 | 451 | static irqreturn_t ser_rx_int(int irq, void *dev_id) |
1da177e4 | 452 | { |
916b7656 | 453 | struct serial_state *info = dev_id; |
1da177e4 LT |
454 | |
455 | #ifdef SERIAL_DEBUG_INTR | |
456 | printk("ser_rx_int..."); | |
457 | #endif | |
458 | ||
87758791 | 459 | if (!info->tport.tty) |
1da177e4 LT |
460 | return IRQ_NONE; |
461 | ||
462 | receive_chars(info); | |
1da177e4 LT |
463 | #ifdef SERIAL_DEBUG_INTR |
464 | printk("end.\n"); | |
465 | #endif | |
466 | return IRQ_HANDLED; | |
467 | } | |
468 | ||
7d12e780 | 469 | static irqreturn_t ser_tx_int(int irq, void *dev_id) |
1da177e4 | 470 | { |
916b7656 | 471 | struct serial_state *info = dev_id; |
1da177e4 LT |
472 | |
473 | if (custom.serdatr & SDR_TBE) { | |
474 | #ifdef SERIAL_DEBUG_INTR | |
475 | printk("ser_tx_int..."); | |
476 | #endif | |
477 | ||
87758791 | 478 | if (!info->tport.tty) |
1da177e4 LT |
479 | return IRQ_NONE; |
480 | ||
481 | transmit_chars(info); | |
1da177e4 LT |
482 | #ifdef SERIAL_DEBUG_INTR |
483 | printk("end.\n"); | |
484 | #endif | |
485 | } | |
486 | return IRQ_HANDLED; | |
487 | } | |
488 | ||
489 | /* | |
490 | * ------------------------------------------------------------------- | |
491 | * Here ends the serial interrupt routines. | |
492 | * ------------------------------------------------------------------- | |
493 | */ | |
494 | ||
1da177e4 LT |
495 | /* |
496 | * --------------------------------------------------------------- | |
497 | * Low level utility subroutines for the serial driver: routines to | |
498 | * figure out the appropriate timeout for an interrupt chain, routines | |
499 | * to initialize and startup a serial port, and routines to shutdown a | |
500 | * serial port. Useful stuff like that. | |
501 | * --------------------------------------------------------------- | |
502 | */ | |
503 | ||
588993dd | 504 | static int startup(struct tty_struct *tty, struct serial_state *info) |
1da177e4 | 505 | { |
01bd730d | 506 | struct tty_port *port = &info->tport; |
1da177e4 LT |
507 | unsigned long flags; |
508 | int retval=0; | |
509 | unsigned long page; | |
510 | ||
511 | page = get_zeroed_page(GFP_KERNEL); | |
512 | if (!page) | |
513 | return -ENOMEM; | |
514 | ||
515 | local_irq_save(flags); | |
516 | ||
01bd730d | 517 | if (port->flags & ASYNC_INITIALIZED) { |
1da177e4 LT |
518 | free_page(page); |
519 | goto errout; | |
520 | } | |
521 | ||
522 | if (info->xmit.buf) | |
523 | free_page(page); | |
524 | else | |
525 | info->xmit.buf = (unsigned char *) page; | |
526 | ||
527 | #ifdef SERIAL_DEBUG_OPEN | |
528 | printk("starting up ttys%d ...", info->line); | |
529 | #endif | |
530 | ||
531 | /* Clear anything in the input buffer */ | |
532 | ||
533 | custom.intreq = IF_RBF; | |
534 | mb(); | |
535 | ||
536 | retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info); | |
537 | if (retval) { | |
538 | if (serial_isroot()) { | |
588993dd | 539 | set_bit(TTY_IO_ERROR, &tty->flags); |
1da177e4 LT |
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 | 553 | info->MCR = 0; |
588993dd | 554 | if (C_BAUD(tty)) |
1da177e4 LT |
555 | info->MCR = SER_DTR | SER_RTS; |
556 | rtsdtr_ctrl(info->MCR); | |
557 | ||
588993dd | 558 | clear_bit(TTY_IO_ERROR, &tty->flags); |
1da177e4 LT |
559 | info->xmit.head = info->xmit.tail = 0; |
560 | ||
561 | /* | |
562 | * Set up the tty->alt_speed kludge | |
563 | */ | |
01bd730d | 564 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
588993dd | 565 | tty->alt_speed = 57600; |
01bd730d | 566 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
588993dd | 567 | tty->alt_speed = 115200; |
01bd730d | 568 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
588993dd | 569 | tty->alt_speed = 230400; |
01bd730d | 570 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
588993dd | 571 | tty->alt_speed = 460800; |
1da177e4 LT |
572 | |
573 | /* | |
574 | * and set the speed of the serial port | |
575 | */ | |
588993dd | 576 | change_speed(tty, info, NULL); |
1da177e4 | 577 | |
01bd730d | 578 | port->flags |= ASYNC_INITIALIZED; |
1da177e4 LT |
579 | local_irq_restore(flags); |
580 | return 0; | |
581 | ||
582 | errout: | |
583 | local_irq_restore(flags); | |
584 | return retval; | |
585 | } | |
586 | ||
587 | /* | |
588 | * This routine will shutdown a serial port; interrupts are disabled, and | |
589 | * DTR is dropped if the hangup on close termio flag is on. | |
590 | */ | |
588993dd | 591 | static void shutdown(struct tty_struct *tty, struct serial_state *info) |
1da177e4 LT |
592 | { |
593 | unsigned long flags; | |
594 | struct serial_state *state; | |
595 | ||
01bd730d | 596 | if (!(info->tport.flags & ASYNC_INITIALIZED)) |
1da177e4 LT |
597 | return; |
598 | ||
916b7656 | 599 | state = info; |
1da177e4 LT |
600 | |
601 | #ifdef SERIAL_DEBUG_OPEN | |
602 | printk("Shutting down serial port %d ....\n", info->line); | |
603 | #endif | |
604 | ||
605 | local_irq_save(flags); /* Disable interrupts */ | |
606 | ||
607 | /* | |
608 | * clear delta_msr_wait queue to avoid mem leaks: we may free the irq | |
609 | * here so the queue might never be waken up | |
610 | */ | |
87758791 | 611 | wake_up_interruptible(&info->tport.delta_msr_wait); |
1da177e4 | 612 | |
1da177e4 LT |
613 | /* |
614 | * Free the IRQ, if necessary | |
615 | */ | |
616 | free_irq(IRQ_AMIGA_VERTB, info); | |
617 | ||
618 | if (info->xmit.buf) { | |
619 | free_page((unsigned long) info->xmit.buf); | |
620 | info->xmit.buf = NULL; | |
621 | } | |
622 | ||
623 | info->IER = 0; | |
624 | custom.intena = IF_RBF | IF_TBE; | |
625 | mb(); | |
626 | ||
627 | /* disable break condition */ | |
628 | custom.adkcon = AC_UARTBRK; | |
629 | mb(); | |
630 | ||
588993dd | 631 | if (tty->termios->c_cflag & HUPCL) |
1da177e4 LT |
632 | info->MCR &= ~(SER_DTR|SER_RTS); |
633 | rtsdtr_ctrl(info->MCR); | |
634 | ||
588993dd | 635 | set_bit(TTY_IO_ERROR, &tty->flags); |
1da177e4 | 636 | |
01bd730d | 637 | info->tport.flags &= ~ASYNC_INITIALIZED; |
1da177e4 LT |
638 | local_irq_restore(flags); |
639 | } | |
640 | ||
641 | ||
642 | /* | |
643 | * This routine is called to set the UART divisor registers to match | |
644 | * the specified baud rate for a serial port. | |
645 | */ | |
588993dd | 646 | static void change_speed(struct tty_struct *tty, struct serial_state *info, |
606d099c | 647 | struct ktermios *old_termios) |
1da177e4 | 648 | { |
01bd730d | 649 | struct tty_port *port = &info->tport; |
1da177e4 LT |
650 | int quot = 0, baud_base, baud; |
651 | unsigned cflag, cval = 0; | |
652 | int bits; | |
653 | unsigned long flags; | |
654 | ||
588993dd | 655 | cflag = tty->termios->c_cflag; |
1da177e4 LT |
656 | |
657 | /* Byte size is always 8 bits plus parity bit if requested */ | |
658 | ||
659 | cval = 3; bits = 10; | |
660 | if (cflag & CSTOPB) { | |
661 | cval |= 0x04; | |
662 | bits++; | |
663 | } | |
664 | if (cflag & PARENB) { | |
665 | cval |= UART_LCR_PARITY; | |
666 | bits++; | |
667 | } | |
668 | if (!(cflag & PARODD)) | |
669 | cval |= UART_LCR_EPAR; | |
670 | #ifdef CMSPAR | |
671 | if (cflag & CMSPAR) | |
672 | cval |= UART_LCR_SPAR; | |
673 | #endif | |
674 | ||
675 | /* Determine divisor based on baud rate */ | |
588993dd | 676 | baud = tty_get_baud_rate(tty); |
1da177e4 LT |
677 | if (!baud) |
678 | baud = 9600; /* B0 transition handled in rs_set_termios */ | |
916b7656 | 679 | baud_base = info->baud_base; |
01bd730d | 680 | if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) |
916b7656 | 681 | quot = info->custom_divisor; |
1da177e4 LT |
682 | else { |
683 | if (baud == 134) | |
684 | /* Special case since 134 is really 134.5 */ | |
685 | quot = (2*baud_base / 269); | |
686 | else if (baud) | |
687 | quot = baud_base / baud; | |
688 | } | |
689 | /* If the quotient is zero refuse the change */ | |
690 | if (!quot && old_termios) { | |
db0ef08e | 691 | /* FIXME: Will need updating for new tty in the end */ |
588993dd JS |
692 | tty->termios->c_cflag &= ~CBAUD; |
693 | tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD); | |
694 | baud = tty_get_baud_rate(tty); | |
1da177e4 LT |
695 | if (!baud) |
696 | baud = 9600; | |
697 | if (baud == 38400 && | |
01bd730d | 698 | (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) |
916b7656 | 699 | quot = info->custom_divisor; |
1da177e4 LT |
700 | else { |
701 | if (baud == 134) | |
702 | /* Special case since 134 is really 134.5 */ | |
703 | quot = (2*baud_base / 269); | |
704 | else if (baud) | |
705 | quot = baud_base / baud; | |
706 | } | |
707 | } | |
708 | /* As a last resort, if the quotient is zero, default to 9600 bps */ | |
709 | if (!quot) | |
710 | quot = baud_base / 9600; | |
711 | info->quot = quot; | |
916b7656 | 712 | info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base); |
1da177e4 LT |
713 | info->timeout += HZ/50; /* Add .02 seconds of slop */ |
714 | ||
715 | /* CTS flow control flag and modem status interrupts */ | |
716 | info->IER &= ~UART_IER_MSI; | |
01bd730d | 717 | if (port->flags & ASYNC_HARDPPS_CD) |
1da177e4 LT |
718 | info->IER |= UART_IER_MSI; |
719 | if (cflag & CRTSCTS) { | |
01bd730d | 720 | port->flags |= ASYNC_CTS_FLOW; |
1da177e4 LT |
721 | info->IER |= UART_IER_MSI; |
722 | } else | |
01bd730d | 723 | port->flags &= ~ASYNC_CTS_FLOW; |
1da177e4 | 724 | if (cflag & CLOCAL) |
01bd730d | 725 | port->flags &= ~ASYNC_CHECK_CD; |
1da177e4 | 726 | else { |
01bd730d | 727 | port->flags |= ASYNC_CHECK_CD; |
1da177e4 LT |
728 | info->IER |= UART_IER_MSI; |
729 | } | |
730 | /* TBD: | |
4b512d26 | 731 | * Does clearing IER_MSI imply that we should disable the VBL interrupt ? |
1da177e4 LT |
732 | */ |
733 | ||
734 | /* | |
735 | * Set up parity check flag | |
736 | */ | |
1da177e4 LT |
737 | |
738 | info->read_status_mask = UART_LSR_OE | UART_LSR_DR; | |
588993dd | 739 | if (I_INPCK(tty)) |
1da177e4 | 740 | info->read_status_mask |= UART_LSR_FE | UART_LSR_PE; |
588993dd | 741 | if (I_BRKINT(tty) || I_PARMRK(tty)) |
1da177e4 LT |
742 | info->read_status_mask |= UART_LSR_BI; |
743 | ||
744 | /* | |
745 | * Characters to ignore | |
746 | */ | |
747 | info->ignore_status_mask = 0; | |
588993dd | 748 | if (I_IGNPAR(tty)) |
1da177e4 | 749 | info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; |
588993dd | 750 | if (I_IGNBRK(tty)) { |
1da177e4 LT |
751 | info->ignore_status_mask |= UART_LSR_BI; |
752 | /* | |
753 | * If we're ignore parity and break indicators, ignore | |
754 | * overruns too. (For real raw support). | |
755 | */ | |
588993dd | 756 | if (I_IGNPAR(tty)) |
1da177e4 LT |
757 | info->ignore_status_mask |= UART_LSR_OE; |
758 | } | |
759 | /* | |
760 | * !!! ignore all characters if CREAD is not set | |
761 | */ | |
762 | if ((cflag & CREAD) == 0) | |
763 | info->ignore_status_mask |= UART_LSR_DR; | |
764 | local_irq_save(flags); | |
765 | ||
766 | { | |
767 | short serper; | |
768 | ||
769 | /* Set up the baud rate */ | |
770 | serper = quot - 1; | |
771 | ||
772 | /* Enable or disable parity bit */ | |
773 | ||
774 | if(cval & UART_LCR_PARITY) | |
775 | serper |= (SERPER_PARENB); | |
776 | ||
777 | custom.serper = serper; | |
778 | mb(); | |
779 | } | |
780 | ||
1da177e4 LT |
781 | local_irq_restore(flags); |
782 | } | |
783 | ||
257afa3c | 784 | static int rs_put_char(struct tty_struct *tty, unsigned char ch) |
1da177e4 | 785 | { |
916b7656 | 786 | struct serial_state *info; |
1da177e4 LT |
787 | unsigned long flags; |
788 | ||
d9ad7ef1 TJRMI |
789 | info = tty->driver_data; |
790 | ||
1da177e4 | 791 | if (serial_paranoia_check(info, tty->name, "rs_put_char")) |
257afa3c | 792 | return 0; |
1da177e4 | 793 | |
d9ad7ef1 | 794 | if (!info->xmit.buf) |
257afa3c | 795 | return 0; |
1da177e4 LT |
796 | |
797 | local_irq_save(flags); | |
798 | if (CIRC_SPACE(info->xmit.head, | |
799 | info->xmit.tail, | |
800 | SERIAL_XMIT_SIZE) == 0) { | |
801 | local_irq_restore(flags); | |
257afa3c | 802 | return 0; |
1da177e4 LT |
803 | } |
804 | ||
805 | info->xmit.buf[info->xmit.head++] = ch; | |
806 | info->xmit.head &= SERIAL_XMIT_SIZE-1; | |
807 | local_irq_restore(flags); | |
257afa3c | 808 | return 1; |
1da177e4 LT |
809 | } |
810 | ||
811 | static void rs_flush_chars(struct tty_struct *tty) | |
812 | { | |
916b7656 | 813 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
814 | unsigned long flags; |
815 | ||
816 | if (serial_paranoia_check(info, tty->name, "rs_flush_chars")) | |
817 | return; | |
818 | ||
819 | if (info->xmit.head == info->xmit.tail | |
820 | || tty->stopped | |
821 | || tty->hw_stopped | |
822 | || !info->xmit.buf) | |
823 | return; | |
824 | ||
825 | local_irq_save(flags); | |
826 | info->IER |= UART_IER_THRI; | |
827 | custom.intena = IF_SETCLR | IF_TBE; | |
828 | mb(); | |
829 | /* set a pending Tx Interrupt, transmitter should restart now */ | |
830 | custom.intreq = IF_SETCLR | IF_TBE; | |
831 | mb(); | |
832 | local_irq_restore(flags); | |
833 | } | |
834 | ||
835 | static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count) | |
836 | { | |
837 | int c, ret = 0; | |
916b7656 | 838 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
839 | unsigned long flags; |
840 | ||
841 | if (serial_paranoia_check(info, tty->name, "rs_write")) | |
842 | return 0; | |
843 | ||
b3218a79 | 844 | if (!info->xmit.buf) |
1da177e4 LT |
845 | return 0; |
846 | ||
3abf3bed | 847 | local_irq_save(flags); |
1da177e4 LT |
848 | while (1) { |
849 | c = CIRC_SPACE_TO_END(info->xmit.head, | |
850 | info->xmit.tail, | |
851 | SERIAL_XMIT_SIZE); | |
852 | if (count < c) | |
853 | c = count; | |
854 | if (c <= 0) { | |
855 | break; | |
856 | } | |
857 | memcpy(info->xmit.buf + info->xmit.head, buf, c); | |
858 | info->xmit.head = ((info->xmit.head + c) & | |
859 | (SERIAL_XMIT_SIZE-1)); | |
860 | buf += c; | |
861 | count -= c; | |
862 | ret += c; | |
863 | } | |
864 | local_irq_restore(flags); | |
865 | ||
866 | if (info->xmit.head != info->xmit.tail | |
867 | && !tty->stopped | |
868 | && !tty->hw_stopped | |
869 | && !(info->IER & UART_IER_THRI)) { | |
870 | info->IER |= UART_IER_THRI; | |
871 | local_irq_disable(); | |
872 | custom.intena = IF_SETCLR | IF_TBE; | |
873 | mb(); | |
874 | /* set a pending Tx Interrupt, transmitter should restart now */ | |
875 | custom.intreq = IF_SETCLR | IF_TBE; | |
876 | mb(); | |
877 | local_irq_restore(flags); | |
878 | } | |
879 | return ret; | |
880 | } | |
881 | ||
882 | static int rs_write_room(struct tty_struct *tty) | |
883 | { | |
916b7656 | 884 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
885 | |
886 | if (serial_paranoia_check(info, tty->name, "rs_write_room")) | |
887 | return 0; | |
888 | return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); | |
889 | } | |
890 | ||
891 | static int rs_chars_in_buffer(struct tty_struct *tty) | |
892 | { | |
916b7656 | 893 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
894 | |
895 | if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer")) | |
896 | return 0; | |
897 | return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); | |
898 | } | |
899 | ||
900 | static void rs_flush_buffer(struct tty_struct *tty) | |
901 | { | |
916b7656 | 902 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
903 | unsigned long flags; |
904 | ||
905 | if (serial_paranoia_check(info, tty->name, "rs_flush_buffer")) | |
906 | return; | |
907 | local_irq_save(flags); | |
908 | info->xmit.head = info->xmit.tail = 0; | |
909 | local_irq_restore(flags); | |
1da177e4 LT |
910 | tty_wakeup(tty); |
911 | } | |
912 | ||
913 | /* | |
914 | * This function is used to send a high-priority XON/XOFF character to | |
915 | * the device | |
916 | */ | |
917 | static void rs_send_xchar(struct tty_struct *tty, char ch) | |
918 | { | |
916b7656 | 919 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
920 | unsigned long flags; |
921 | ||
922 | if (serial_paranoia_check(info, tty->name, "rs_send_char")) | |
923 | return; | |
924 | ||
925 | info->x_char = ch; | |
926 | if (ch) { | |
927 | /* Make sure transmit interrupts are on */ | |
928 | ||
929 | /* Check this ! */ | |
930 | local_irq_save(flags); | |
931 | if(!(custom.intenar & IF_TBE)) { | |
932 | custom.intena = IF_SETCLR | IF_TBE; | |
933 | mb(); | |
934 | /* set a pending Tx Interrupt, transmitter should restart now */ | |
935 | custom.intreq = IF_SETCLR | IF_TBE; | |
936 | mb(); | |
937 | } | |
938 | local_irq_restore(flags); | |
939 | ||
940 | info->IER |= UART_IER_THRI; | |
941 | } | |
942 | } | |
943 | ||
944 | /* | |
945 | * ------------------------------------------------------------ | |
946 | * rs_throttle() | |
947 | * | |
948 | * This routine is called by the upper-layer tty layer to signal that | |
949 | * incoming characters should be throttled. | |
950 | * ------------------------------------------------------------ | |
951 | */ | |
952 | static void rs_throttle(struct tty_struct * tty) | |
953 | { | |
916b7656 | 954 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
955 | unsigned long flags; |
956 | #ifdef SERIAL_DEBUG_THROTTLE | |
957 | char buf[64]; | |
958 | ||
959 | printk("throttle %s: %d....\n", tty_name(tty, buf), | |
960 | tty->ldisc.chars_in_buffer(tty)); | |
961 | #endif | |
962 | ||
963 | if (serial_paranoia_check(info, tty->name, "rs_throttle")) | |
964 | return; | |
965 | ||
966 | if (I_IXOFF(tty)) | |
967 | rs_send_xchar(tty, STOP_CHAR(tty)); | |
968 | ||
969 | if (tty->termios->c_cflag & CRTSCTS) | |
970 | info->MCR &= ~SER_RTS; | |
971 | ||
972 | local_irq_save(flags); | |
973 | rtsdtr_ctrl(info->MCR); | |
974 | local_irq_restore(flags); | |
975 | } | |
976 | ||
977 | static void rs_unthrottle(struct tty_struct * tty) | |
978 | { | |
916b7656 | 979 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
980 | unsigned long flags; |
981 | #ifdef SERIAL_DEBUG_THROTTLE | |
982 | char buf[64]; | |
983 | ||
984 | printk("unthrottle %s: %d....\n", tty_name(tty, buf), | |
985 | tty->ldisc.chars_in_buffer(tty)); | |
986 | #endif | |
987 | ||
988 | if (serial_paranoia_check(info, tty->name, "rs_unthrottle")) | |
989 | return; | |
990 | ||
991 | if (I_IXOFF(tty)) { | |
992 | if (info->x_char) | |
993 | info->x_char = 0; | |
994 | else | |
995 | rs_send_xchar(tty, START_CHAR(tty)); | |
996 | } | |
997 | if (tty->termios->c_cflag & CRTSCTS) | |
998 | info->MCR |= SER_RTS; | |
999 | local_irq_save(flags); | |
1000 | rtsdtr_ctrl(info->MCR); | |
1001 | local_irq_restore(flags); | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * ------------------------------------------------------------ | |
1006 | * rs_ioctl() and friends | |
1007 | * ------------------------------------------------------------ | |
1008 | */ | |
1009 | ||
916b7656 | 1010 | static int get_serial_info(struct serial_state *state, |
ab14caec | 1011 | struct serial_struct __user * retinfo) |
1da177e4 LT |
1012 | { |
1013 | struct serial_struct tmp; | |
1da177e4 LT |
1014 | |
1015 | if (!retinfo) | |
1016 | return -EFAULT; | |
1017 | memset(&tmp, 0, sizeof(tmp)); | |
ec79d605 | 1018 | tty_lock(); |
1da177e4 LT |
1019 | tmp.type = state->type; |
1020 | tmp.line = state->line; | |
1021 | tmp.port = state->port; | |
1022 | tmp.irq = state->irq; | |
01bd730d | 1023 | tmp.flags = state->tport.flags; |
1da177e4 LT |
1024 | tmp.xmit_fifo_size = state->xmit_fifo_size; |
1025 | tmp.baud_base = state->baud_base; | |
799be6ff JS |
1026 | tmp.close_delay = state->tport.close_delay; |
1027 | tmp.closing_wait = state->tport.closing_wait; | |
1da177e4 | 1028 | tmp.custom_divisor = state->custom_divisor; |
ec79d605 | 1029 | tty_unlock(); |
1da177e4 LT |
1030 | if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) |
1031 | return -EFAULT; | |
1032 | return 0; | |
1033 | } | |
1034 | ||
588993dd | 1035 | static int set_serial_info(struct tty_struct *tty, struct serial_state *state, |
ab14caec | 1036 | struct serial_struct __user * new_info) |
1da177e4 | 1037 | { |
01bd730d | 1038 | struct tty_port *port = &state->tport; |
1da177e4 | 1039 | struct serial_struct new_serial; |
0f9b9684 | 1040 | bool change_spd; |
1da177e4 LT |
1041 | int retval = 0; |
1042 | ||
1043 | if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) | |
1044 | return -EFAULT; | |
e18ce49b | 1045 | |
ec79d605 | 1046 | tty_lock(); |
01bd730d | 1047 | change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) || |
0f9b9684 JS |
1048 | new_serial.custom_divisor != state->custom_divisor; |
1049 | if (new_serial.irq != state->irq || new_serial.port != state->port || | |
1050 | new_serial.xmit_fifo_size != state->xmit_fifo_size) { | |
1051 | tty_unlock(); | |
1052 | return -EINVAL; | |
e18ce49b | 1053 | } |
1da177e4 LT |
1054 | |
1055 | if (!serial_isroot()) { | |
1056 | if ((new_serial.baud_base != state->baud_base) || | |
01bd730d | 1057 | (new_serial.close_delay != port->close_delay) || |
1da177e4 LT |
1058 | (new_serial.xmit_fifo_size != state->xmit_fifo_size) || |
1059 | ((new_serial.flags & ~ASYNC_USR_MASK) != | |
01bd730d | 1060 | (port->flags & ~ASYNC_USR_MASK))) |
1da177e4 | 1061 | return -EPERM; |
01bd730d | 1062 | port->flags = ((port->flags & ~ASYNC_USR_MASK) | |
1da177e4 | 1063 | (new_serial.flags & ASYNC_USR_MASK)); |
1da177e4 LT |
1064 | state->custom_divisor = new_serial.custom_divisor; |
1065 | goto check_and_exit; | |
1066 | } | |
1067 | ||
e18ce49b | 1068 | if (new_serial.baud_base < 9600) { |
ec79d605 | 1069 | tty_unlock(); |
1da177e4 | 1070 | return -EINVAL; |
e18ce49b | 1071 | } |
1da177e4 LT |
1072 | |
1073 | /* | |
1074 | * OK, past this point, all the error checking has been done. | |
1075 | * At this point, we start making changes..... | |
1076 | */ | |
1077 | ||
1078 | state->baud_base = new_serial.baud_base; | |
01bd730d | 1079 | port->flags = ((port->flags & ~ASYNC_FLAGS) | |
1da177e4 | 1080 | (new_serial.flags & ASYNC_FLAGS)); |
1da177e4 | 1081 | state->custom_divisor = new_serial.custom_divisor; |
01bd730d JS |
1082 | port->close_delay = new_serial.close_delay * HZ/100; |
1083 | port->closing_wait = new_serial.closing_wait * HZ/100; | |
1084 | tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0; | |
1da177e4 LT |
1085 | |
1086 | check_and_exit: | |
01bd730d | 1087 | if (port->flags & ASYNC_INITIALIZED) { |
0f9b9684 | 1088 | if (change_spd) { |
01bd730d | 1089 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
588993dd | 1090 | tty->alt_speed = 57600; |
01bd730d | 1091 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
588993dd | 1092 | tty->alt_speed = 115200; |
01bd730d | 1093 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
588993dd | 1094 | tty->alt_speed = 230400; |
01bd730d | 1095 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
588993dd JS |
1096 | tty->alt_speed = 460800; |
1097 | change_speed(tty, state, NULL); | |
1da177e4 LT |
1098 | } |
1099 | } else | |
588993dd | 1100 | retval = startup(tty, state); |
ec79d605 | 1101 | tty_unlock(); |
1da177e4 LT |
1102 | return retval; |
1103 | } | |
1104 | ||
1105 | ||
1106 | /* | |
1107 | * get_lsr_info - get line status register info | |
1108 | * | |
1109 | * Purpose: Let user call ioctl() to get info when the UART physically | |
1110 | * is emptied. On bus types like RS485, the transmitter must | |
1111 | * release the bus after transmitting. This must be done when | |
1112 | * the transmit shift register is empty, not be done when the | |
1113 | * transmit holding register is empty. This functionality | |
1114 | * allows an RS485 driver to be written in user space. | |
1115 | */ | |
916b7656 | 1116 | static int get_lsr_info(struct serial_state *info, unsigned int __user *value) |
1da177e4 LT |
1117 | { |
1118 | unsigned char status; | |
1119 | unsigned int result; | |
1120 | unsigned long flags; | |
1121 | ||
1122 | local_irq_save(flags); | |
1123 | status = custom.serdatr; | |
1124 | mb(); | |
1125 | local_irq_restore(flags); | |
1126 | result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0); | |
1127 | if (copy_to_user(value, &result, sizeof(int))) | |
1128 | return -EFAULT; | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | ||
60b33c13 | 1133 | static int rs_tiocmget(struct tty_struct *tty) |
1da177e4 | 1134 | { |
916b7656 | 1135 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1136 | unsigned char control, status; |
1137 | unsigned long flags; | |
1138 | ||
1139 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) | |
1140 | return -ENODEV; | |
1141 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
1142 | return -EIO; | |
1143 | ||
1144 | control = info->MCR; | |
1145 | local_irq_save(flags); | |
1146 | status = ciab.pra; | |
1147 | local_irq_restore(flags); | |
1148 | return ((control & SER_RTS) ? TIOCM_RTS : 0) | |
1149 | | ((control & SER_DTR) ? TIOCM_DTR : 0) | |
1150 | | (!(status & SER_DCD) ? TIOCM_CAR : 0) | |
1151 | | (!(status & SER_DSR) ? TIOCM_DSR : 0) | |
1152 | | (!(status & SER_CTS) ? TIOCM_CTS : 0); | |
1153 | } | |
1154 | ||
20b9d177 AC |
1155 | static int rs_tiocmset(struct tty_struct *tty, unsigned int set, |
1156 | unsigned int clear) | |
1da177e4 | 1157 | { |
916b7656 | 1158 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1159 | unsigned long flags; |
1160 | ||
1161 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) | |
1162 | return -ENODEV; | |
1163 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
1164 | return -EIO; | |
1165 | ||
1166 | local_irq_save(flags); | |
1167 | if (set & TIOCM_RTS) | |
1168 | info->MCR |= SER_RTS; | |
1169 | if (set & TIOCM_DTR) | |
1170 | info->MCR |= SER_DTR; | |
1171 | if (clear & TIOCM_RTS) | |
1172 | info->MCR &= ~SER_RTS; | |
1173 | if (clear & TIOCM_DTR) | |
1174 | info->MCR &= ~SER_DTR; | |
1175 | rtsdtr_ctrl(info->MCR); | |
1176 | local_irq_restore(flags); | |
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | /* | |
1181 | * rs_break() --- routine which turns the break handling on or off | |
1182 | */ | |
9e98966c | 1183 | static int rs_break(struct tty_struct *tty, int break_state) |
1da177e4 | 1184 | { |
916b7656 | 1185 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1186 | unsigned long flags; |
1187 | ||
1188 | if (serial_paranoia_check(info, tty->name, "rs_break")) | |
970a8a51 | 1189 | return -EINVAL; |
1da177e4 LT |
1190 | |
1191 | local_irq_save(flags); | |
1192 | if (break_state == -1) | |
1193 | custom.adkcon = AC_SETCLR | AC_UARTBRK; | |
1194 | else | |
1195 | custom.adkcon = AC_UARTBRK; | |
1196 | mb(); | |
1197 | local_irq_restore(flags); | |
9e98966c | 1198 | return 0; |
1da177e4 LT |
1199 | } |
1200 | ||
0587102c AC |
1201 | /* |
1202 | * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) | |
1203 | * Return: write counters to the user passed counter struct | |
1204 | * NB: both 1->0 and 0->1 transitions are counted except for | |
1205 | * RI where only 0->1 is counted. | |
1206 | */ | |
1207 | static int rs_get_icount(struct tty_struct *tty, | |
1208 | struct serial_icounter_struct *icount) | |
1209 | { | |
916b7656 | 1210 | struct serial_state *info = tty->driver_data; |
0587102c AC |
1211 | struct async_icount cnow; |
1212 | unsigned long flags; | |
1213 | ||
1214 | local_irq_save(flags); | |
916b7656 | 1215 | cnow = info->icount; |
0587102c AC |
1216 | local_irq_restore(flags); |
1217 | icount->cts = cnow.cts; | |
1218 | icount->dsr = cnow.dsr; | |
1219 | icount->rng = cnow.rng; | |
1220 | icount->dcd = cnow.dcd; | |
1221 | icount->rx = cnow.rx; | |
1222 | icount->tx = cnow.tx; | |
1223 | icount->frame = cnow.frame; | |
1224 | icount->overrun = cnow.overrun; | |
1225 | icount->parity = cnow.parity; | |
1226 | icount->brk = cnow.brk; | |
1227 | icount->buf_overrun = cnow.buf_overrun; | |
1228 | ||
1229 | return 0; | |
1230 | } | |
1da177e4 | 1231 | |
6caa76b7 | 1232 | static int rs_ioctl(struct tty_struct *tty, |
1da177e4 LT |
1233 | unsigned int cmd, unsigned long arg) |
1234 | { | |
916b7656 | 1235 | struct serial_state *info = tty->driver_data; |
1da177e4 | 1236 | struct async_icount cprev, cnow; /* kernel counter temps */ |
ab14caec | 1237 | void __user *argp = (void __user *)arg; |
1da177e4 LT |
1238 | unsigned long flags; |
1239 | ||
1240 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) | |
1241 | return -ENODEV; | |
1242 | ||
1243 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && | |
1244 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && | |
1245 | (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { | |
1246 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
1247 | return -EIO; | |
1248 | } | |
1249 | ||
1250 | switch (cmd) { | |
1251 | case TIOCGSERIAL: | |
ab14caec | 1252 | return get_serial_info(info, argp); |
1da177e4 | 1253 | case TIOCSSERIAL: |
588993dd | 1254 | return set_serial_info(tty, info, argp); |
1da177e4 LT |
1255 | case TIOCSERCONFIG: |
1256 | return 0; | |
1257 | ||
1258 | case TIOCSERGETLSR: /* Get line status register */ | |
ab14caec | 1259 | return get_lsr_info(info, argp); |
1da177e4 LT |
1260 | |
1261 | case TIOCSERGSTRUCT: | |
ab14caec | 1262 | if (copy_to_user(argp, |
916b7656 | 1263 | info, sizeof(struct serial_state))) |
1da177e4 LT |
1264 | return -EFAULT; |
1265 | return 0; | |
1266 | ||
1267 | /* | |
1268 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change | |
1269 | * - mask passed in arg for lines of interest | |
1270 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) | |
1271 | * Caller should use TIOCGICOUNT to see which one it was | |
1272 | */ | |
1273 | case TIOCMIWAIT: | |
1274 | local_irq_save(flags); | |
1275 | /* note the counters on entry */ | |
916b7656 | 1276 | cprev = info->icount; |
1da177e4 LT |
1277 | local_irq_restore(flags); |
1278 | while (1) { | |
87758791 | 1279 | interruptible_sleep_on(&info->tport.delta_msr_wait); |
1da177e4 LT |
1280 | /* see if a signal did it */ |
1281 | if (signal_pending(current)) | |
1282 | return -ERESTARTSYS; | |
1283 | local_irq_save(flags); | |
916b7656 | 1284 | cnow = info->icount; /* atomic copy */ |
1da177e4 LT |
1285 | local_irq_restore(flags); |
1286 | if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && | |
1287 | cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) | |
1288 | return -EIO; /* no change => error */ | |
1289 | if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || | |
1290 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || | |
1291 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || | |
1292 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) { | |
1293 | return 0; | |
1294 | } | |
1295 | cprev = cnow; | |
1296 | } | |
1297 | /* NOTREACHED */ | |
1298 | ||
1da177e4 LT |
1299 | case TIOCSERGWILD: |
1300 | case TIOCSERSWILD: | |
1301 | /* "setserial -W" is called in Debian boot */ | |
1302 | printk ("TIOCSER?WILD ioctl obsolete, ignored.\n"); | |
1303 | return 0; | |
1304 | ||
1305 | default: | |
1306 | return -ENOIOCTLCMD; | |
1307 | } | |
1308 | return 0; | |
1309 | } | |
1310 | ||
606d099c | 1311 | static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios) |
1da177e4 | 1312 | { |
916b7656 | 1313 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1314 | unsigned long flags; |
1315 | unsigned int cflag = tty->termios->c_cflag; | |
1316 | ||
588993dd | 1317 | change_speed(tty, info, old_termios); |
1da177e4 LT |
1318 | |
1319 | /* Handle transition to B0 status */ | |
1320 | if ((old_termios->c_cflag & CBAUD) && | |
1321 | !(cflag & CBAUD)) { | |
1322 | info->MCR &= ~(SER_DTR|SER_RTS); | |
1323 | local_irq_save(flags); | |
1324 | rtsdtr_ctrl(info->MCR); | |
1325 | local_irq_restore(flags); | |
1326 | } | |
1327 | ||
1328 | /* Handle transition away from B0 status */ | |
1329 | if (!(old_termios->c_cflag & CBAUD) && | |
1330 | (cflag & CBAUD)) { | |
1331 | info->MCR |= SER_DTR; | |
1332 | if (!(tty->termios->c_cflag & CRTSCTS) || | |
1333 | !test_bit(TTY_THROTTLED, &tty->flags)) { | |
1334 | info->MCR |= SER_RTS; | |
1335 | } | |
1336 | local_irq_save(flags); | |
1337 | rtsdtr_ctrl(info->MCR); | |
1338 | local_irq_restore(flags); | |
1339 | } | |
1340 | ||
1341 | /* Handle turning off CRTSCTS */ | |
1342 | if ((old_termios->c_cflag & CRTSCTS) && | |
1343 | !(tty->termios->c_cflag & CRTSCTS)) { | |
1344 | tty->hw_stopped = 0; | |
1345 | rs_start(tty); | |
1346 | } | |
1347 | ||
1348 | #if 0 | |
1349 | /* | |
1350 | * No need to wake up processes in open wait, since they | |
1351 | * sample the CLOCAL flag once, and don't recheck it. | |
1352 | * XXX It's not clear whether the current behavior is correct | |
1353 | * or not. Hence, this may change..... | |
1354 | */ | |
1355 | if (!(old_termios->c_cflag & CLOCAL) && | |
1356 | (tty->termios->c_cflag & CLOCAL)) | |
1357 | wake_up_interruptible(&info->open_wait); | |
1358 | #endif | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * ------------------------------------------------------------ | |
1363 | * rs_close() | |
1364 | * | |
1365 | * This routine is called when the serial port gets closed. First, we | |
1366 | * wait for the last remaining data to be sent. Then, we unlink its | |
1367 | * async structure from the interrupt chain if necessary, and we free | |
1368 | * that IRQ if nothing is left in the chain. | |
1369 | * ------------------------------------------------------------ | |
1370 | */ | |
1371 | static void rs_close(struct tty_struct *tty, struct file * filp) | |
1372 | { | |
916b7656 | 1373 | struct serial_state *state = tty->driver_data; |
1da177e4 LT |
1374 | unsigned long flags; |
1375 | ||
916b7656 | 1376 | if (!state || serial_paranoia_check(state, tty->name, "rs_close")) |
1da177e4 LT |
1377 | return; |
1378 | ||
1da177e4 LT |
1379 | local_irq_save(flags); |
1380 | ||
1381 | if (tty_hung_up_p(filp)) { | |
1382 | DBG_CNT("before DEC-hung"); | |
1383 | local_irq_restore(flags); | |
1384 | return; | |
1385 | } | |
1386 | ||
1387 | #ifdef SERIAL_DEBUG_OPEN | |
12c80354 | 1388 | printk("rs_close ttys%d, count = %d\n", state->line, state->tport.count); |
1da177e4 | 1389 | #endif |
12c80354 | 1390 | if ((tty->count == 1) && (state->tport.count != 1)) { |
1da177e4 LT |
1391 | /* |
1392 | * Uh, oh. tty->count is 1, which means that the tty | |
12c80354 | 1393 | * structure will be freed. state->tport.count should always |
1da177e4 LT |
1394 | * be one in these conditions. If it's greater than |
1395 | * one, we've got real problems, since it means the | |
1396 | * serial port won't be shutdown. | |
1397 | */ | |
1398 | printk("rs_close: bad serial port count; tty->count is 1, " | |
12c80354 JS |
1399 | "state->tport.count is %d\n", state->tport.count); |
1400 | state->tport.count = 1; | |
1da177e4 | 1401 | } |
12c80354 | 1402 | if (--state->tport.count < 0) { |
1da177e4 | 1403 | printk("rs_close: bad serial port count for ttys%d: %d\n", |
12c80354 JS |
1404 | state->line, state->tport.count); |
1405 | state->tport.count = 0; | |
1da177e4 | 1406 | } |
12c80354 | 1407 | if (state->tport.count) { |
1da177e4 LT |
1408 | DBG_CNT("before DEC-2"); |
1409 | local_irq_restore(flags); | |
1410 | return; | |
1411 | } | |
01bd730d | 1412 | state->tport.flags |= ASYNC_CLOSING; |
1da177e4 LT |
1413 | /* |
1414 | * Now we wait for the transmit buffer to clear; and we notify | |
1415 | * the line discipline to only process XON/XOFF characters. | |
1416 | */ | |
1417 | tty->closing = 1; | |
799be6ff JS |
1418 | if (state->tport.closing_wait != ASYNC_CLOSING_WAIT_NONE) |
1419 | tty_wait_until_sent(tty, state->tport.closing_wait); | |
1da177e4 LT |
1420 | /* |
1421 | * At this point we stop accepting input. To do this, we | |
1422 | * disable the receive line status interrupts, and tell the | |
1423 | * interrupt driver to stop checking the data ready bit in the | |
1424 | * line status register. | |
1425 | */ | |
916b7656 | 1426 | state->read_status_mask &= ~UART_LSR_DR; |
01bd730d | 1427 | if (state->tport.flags & ASYNC_INITIALIZED) { |
1da177e4 LT |
1428 | /* disable receive interrupts */ |
1429 | custom.intena = IF_RBF; | |
1430 | mb(); | |
1431 | /* clear any pending receive interrupt */ | |
1432 | custom.intreq = IF_RBF; | |
1433 | mb(); | |
1434 | ||
1435 | /* | |
1436 | * Before we drop DTR, make sure the UART transmitter | |
1437 | * has completely drained; this is especially | |
1438 | * important if there is a transmit FIFO! | |
1439 | */ | |
916b7656 | 1440 | rs_wait_until_sent(tty, state->timeout); |
1da177e4 | 1441 | } |
588993dd | 1442 | shutdown(tty, state); |
978e595f | 1443 | rs_flush_buffer(tty); |
1da177e4 LT |
1444 | |
1445 | tty_ldisc_flush(tty); | |
1446 | tty->closing = 0; | |
87758791 JS |
1447 | state->tport.tty = NULL; |
1448 | if (state->tport.blocked_open) { | |
799be6ff JS |
1449 | if (state->tport.close_delay) { |
1450 | msleep_interruptible(jiffies_to_msecs(state->tport.close_delay)); | |
1da177e4 | 1451 | } |
87758791 | 1452 | wake_up_interruptible(&state->tport.open_wait); |
1da177e4 | 1453 | } |
01bd730d | 1454 | state->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); |
87758791 | 1455 | wake_up_interruptible(&state->tport.close_wait); |
1da177e4 LT |
1456 | local_irq_restore(flags); |
1457 | } | |
1458 | ||
1459 | /* | |
1460 | * rs_wait_until_sent() --- wait until the transmitter is empty | |
1461 | */ | |
1462 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout) | |
1463 | { | |
916b7656 | 1464 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1465 | unsigned long orig_jiffies, char_time; |
1466 | int lsr; | |
1467 | ||
1468 | if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent")) | |
1469 | return; | |
1470 | ||
916b7656 | 1471 | if (info->xmit_fifo_size == 0) |
1da177e4 LT |
1472 | return; /* Just in case.... */ |
1473 | ||
1474 | orig_jiffies = jiffies; | |
978e595f | 1475 | |
1da177e4 LT |
1476 | /* |
1477 | * Set the check interval to be 1/5 of the estimated time to | |
1478 | * send a single character, and make it at least 1. The check | |
1479 | * interval should also be less than the timeout. | |
1480 | * | |
1481 | * Note: we have to use pretty tight timings here to satisfy | |
1482 | * the NIST-PCTS. | |
1483 | */ | |
916b7656 | 1484 | char_time = (info->timeout - HZ/50) / info->xmit_fifo_size; |
1da177e4 LT |
1485 | char_time = char_time / 5; |
1486 | if (char_time == 0) | |
1487 | char_time = 1; | |
1488 | if (timeout) | |
1489 | char_time = min_t(unsigned long, char_time, timeout); | |
1490 | /* | |
1491 | * If the transmitter hasn't cleared in twice the approximate | |
1492 | * amount of time to send the entire FIFO, it probably won't | |
1493 | * ever clear. This assumes the UART isn't doing flow | |
1494 | * control, which is currently the case. Hence, if it ever | |
1495 | * takes longer than info->timeout, this is probably due to a | |
1496 | * UART bug of some kind. So, we clamp the timeout parameter at | |
1497 | * 2*info->timeout. | |
1498 | */ | |
1499 | if (!timeout || timeout > 2*info->timeout) | |
1500 | timeout = 2*info->timeout; | |
1501 | #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT | |
1502 | printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time); | |
1503 | printk("jiff=%lu...", jiffies); | |
1504 | #endif | |
1505 | while(!((lsr = custom.serdatr) & SDR_TSRE)) { | |
1506 | #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT | |
1507 | printk("serdatr = %d (jiff=%lu)...", lsr, jiffies); | |
1508 | #endif | |
1509 | msleep_interruptible(jiffies_to_msecs(char_time)); | |
1510 | if (signal_pending(current)) | |
1511 | break; | |
1512 | if (timeout && time_after(jiffies, orig_jiffies + timeout)) | |
1513 | break; | |
1514 | } | |
cc0a8fbb | 1515 | __set_current_state(TASK_RUNNING); |
eff4b0b9 | 1516 | |
1da177e4 LT |
1517 | #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT |
1518 | printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); | |
1519 | #endif | |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * rs_hangup() --- called by tty_hangup() when a hangup is signaled. | |
1524 | */ | |
1525 | static void rs_hangup(struct tty_struct *tty) | |
1526 | { | |
916b7656 | 1527 | struct serial_state *info = tty->driver_data; |
1da177e4 LT |
1528 | |
1529 | if (serial_paranoia_check(info, tty->name, "rs_hangup")) | |
1530 | return; | |
1531 | ||
1da177e4 | 1532 | rs_flush_buffer(tty); |
588993dd | 1533 | shutdown(tty, info); |
12c80354 | 1534 | info->tport.count = 0; |
01bd730d | 1535 | info->tport.flags &= ~ASYNC_NORMAL_ACTIVE; |
87758791 JS |
1536 | info->tport.tty = NULL; |
1537 | wake_up_interruptible(&info->tport.open_wait); | |
1da177e4 LT |
1538 | } |
1539 | ||
1540 | /* | |
1541 | * ------------------------------------------------------------ | |
1542 | * rs_open() and friends | |
1543 | * ------------------------------------------------------------ | |
1544 | */ | |
1545 | static int block_til_ready(struct tty_struct *tty, struct file * filp, | |
916b7656 | 1546 | struct serial_state *info) |
1da177e4 LT |
1547 | { |
1548 | #ifdef DECLARE_WAITQUEUE | |
1549 | DECLARE_WAITQUEUE(wait, current); | |
1550 | #else | |
1551 | struct wait_queue wait = { current, NULL }; | |
1552 | #endif | |
01bd730d | 1553 | struct tty_port *port = &info->tport; |
1da177e4 LT |
1554 | int retval; |
1555 | int do_clocal = 0, extra_count = 0; | |
1556 | unsigned long flags; | |
1557 | ||
1558 | /* | |
1559 | * If the device is in the middle of being closed, then block | |
1560 | * until it's done, and then try again. | |
1561 | */ | |
1562 | if (tty_hung_up_p(filp) || | |
01bd730d JS |
1563 | (port->flags & ASYNC_CLOSING)) { |
1564 | if (port->flags & ASYNC_CLOSING) | |
1565 | interruptible_sleep_on(&port->close_wait); | |
1da177e4 | 1566 | #ifdef SERIAL_DO_RESTART |
01bd730d | 1567 | return ((port->flags & ASYNC_HUP_NOTIFY) ? |
1da177e4 LT |
1568 | -EAGAIN : -ERESTARTSYS); |
1569 | #else | |
1570 | return -EAGAIN; | |
1571 | #endif | |
1572 | } | |
1573 | ||
1574 | /* | |
1575 | * If non-blocking mode is set, or the port is not enabled, | |
1576 | * then make the check up front and then exit. | |
1577 | */ | |
1578 | if ((filp->f_flags & O_NONBLOCK) || | |
1579 | (tty->flags & (1 << TTY_IO_ERROR))) { | |
01bd730d | 1580 | port->flags |= ASYNC_NORMAL_ACTIVE; |
1da177e4 LT |
1581 | return 0; |
1582 | } | |
1583 | ||
1584 | if (tty->termios->c_cflag & CLOCAL) | |
1585 | do_clocal = 1; | |
1586 | ||
1587 | /* | |
1588 | * Block waiting for the carrier detect and the line to become | |
1589 | * free (i.e., not in use by the callout). While we are in | |
01bd730d | 1590 | * this loop, port->count is dropped by one, so that |
1da177e4 LT |
1591 | * rs_close() knows when to free things. We restore it upon |
1592 | * exit, either normal or abnormal. | |
1593 | */ | |
1594 | retval = 0; | |
01bd730d | 1595 | add_wait_queue(&port->open_wait, &wait); |
1da177e4 LT |
1596 | #ifdef SERIAL_DEBUG_OPEN |
1597 | printk("block_til_ready before block: ttys%d, count = %d\n", | |
01bd730d | 1598 | info->line, port->count); |
1da177e4 LT |
1599 | #endif |
1600 | local_irq_save(flags); | |
1601 | if (!tty_hung_up_p(filp)) { | |
1602 | extra_count = 1; | |
01bd730d | 1603 | port->count--; |
1da177e4 LT |
1604 | } |
1605 | local_irq_restore(flags); | |
01bd730d | 1606 | port->blocked_open++; |
1da177e4 LT |
1607 | while (1) { |
1608 | local_irq_save(flags); | |
1609 | if (tty->termios->c_cflag & CBAUD) | |
1610 | rtsdtr_ctrl(SER_DTR|SER_RTS); | |
1611 | local_irq_restore(flags); | |
1612 | set_current_state(TASK_INTERRUPTIBLE); | |
1613 | if (tty_hung_up_p(filp) || | |
01bd730d | 1614 | !(port->flags & ASYNC_INITIALIZED)) { |
1da177e4 | 1615 | #ifdef SERIAL_DO_RESTART |
01bd730d | 1616 | if (port->flags & ASYNC_HUP_NOTIFY) |
1da177e4 LT |
1617 | retval = -EAGAIN; |
1618 | else | |
1619 | retval = -ERESTARTSYS; | |
1620 | #else | |
1621 | retval = -EAGAIN; | |
1622 | #endif | |
1623 | break; | |
1624 | } | |
01bd730d | 1625 | if (!(port->flags & ASYNC_CLOSING) && |
1da177e4 LT |
1626 | (do_clocal || (!(ciab.pra & SER_DCD)) )) |
1627 | break; | |
1628 | if (signal_pending(current)) { | |
1629 | retval = -ERESTARTSYS; | |
1630 | break; | |
1631 | } | |
1632 | #ifdef SERIAL_DEBUG_OPEN | |
1633 | printk("block_til_ready blocking: ttys%d, count = %d\n", | |
01bd730d | 1634 | info->line, port->count); |
1da177e4 | 1635 | #endif |
e142a31d | 1636 | tty_unlock(); |
1da177e4 | 1637 | schedule(); |
e142a31d | 1638 | tty_lock(); |
1da177e4 | 1639 | } |
cc0a8fbb | 1640 | __set_current_state(TASK_RUNNING); |
01bd730d | 1641 | remove_wait_queue(&port->open_wait, &wait); |
1da177e4 | 1642 | if (extra_count) |
01bd730d JS |
1643 | port->count++; |
1644 | port->blocked_open--; | |
1da177e4 LT |
1645 | #ifdef SERIAL_DEBUG_OPEN |
1646 | printk("block_til_ready after blocking: ttys%d, count = %d\n", | |
01bd730d | 1647 | info->line, port->count); |
1da177e4 LT |
1648 | #endif |
1649 | if (retval) | |
1650 | return retval; | |
01bd730d | 1651 | port->flags |= ASYNC_NORMAL_ACTIVE; |
1da177e4 LT |
1652 | return 0; |
1653 | } | |
1654 | ||
1655 | /* | |
1656 | * This routine is called whenever a serial port is opened. It | |
1657 | * enables interrupts for a serial port, linking in its async structure into | |
1658 | * the IRQ chain. It also performs the serial-specific | |
1659 | * initialization for the tty structure. | |
1660 | */ | |
1661 | static int rs_open(struct tty_struct *tty, struct file * filp) | |
1662 | { | |
916b7656 | 1663 | struct serial_state *info = rs_table + tty->index; |
410235fd | 1664 | int retval; |
1da177e4 | 1665 | |
12c80354 | 1666 | info->tport.count++; |
87758791 | 1667 | info->tport.tty = tty; |
916b7656 | 1668 | tty->driver_data = info; |
87758791 | 1669 | tty->port = &info->tport; |
1da177e4 LT |
1670 | if (serial_paranoia_check(info, tty->name, "rs_open")) |
1671 | return -ENODEV; | |
1672 | ||
1673 | #ifdef SERIAL_DEBUG_OPEN | |
916b7656 | 1674 | printk("rs_open %s, count = %d\n", tty->name, info->count); |
1da177e4 | 1675 | #endif |
01bd730d | 1676 | tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
1da177e4 | 1677 | |
1da177e4 LT |
1678 | /* |
1679 | * If the port is the middle of closing, bail out now | |
1680 | */ | |
1681 | if (tty_hung_up_p(filp) || | |
01bd730d JS |
1682 | (info->tport.flags & ASYNC_CLOSING)) { |
1683 | if (info->tport.flags & ASYNC_CLOSING) | |
87758791 | 1684 | interruptible_sleep_on(&info->tport.close_wait); |
1da177e4 | 1685 | #ifdef SERIAL_DO_RESTART |
01bd730d | 1686 | return ((info->tport.flags & ASYNC_HUP_NOTIFY) ? |
1da177e4 LT |
1687 | -EAGAIN : -ERESTARTSYS); |
1688 | #else | |
1689 | return -EAGAIN; | |
1690 | #endif | |
1691 | } | |
1692 | ||
1693 | /* | |
1694 | * Start up serial port | |
1695 | */ | |
588993dd | 1696 | retval = startup(tty, info); |
1da177e4 LT |
1697 | if (retval) { |
1698 | return retval; | |
1699 | } | |
1700 | ||
1701 | retval = block_til_ready(tty, filp, info); | |
1702 | if (retval) { | |
1703 | #ifdef SERIAL_DEBUG_OPEN | |
1704 | printk("rs_open returning after block_til_ready with %d\n", | |
1705 | retval); | |
1706 | #endif | |
1707 | return retval; | |
1708 | } | |
1709 | ||
1710 | #ifdef SERIAL_DEBUG_OPEN | |
1711 | printk("rs_open %s successful...", tty->name); | |
1712 | #endif | |
1713 | return 0; | |
1714 | } | |
1715 | ||
1716 | /* | |
1717 | * /proc fs routines.... | |
1718 | */ | |
1719 | ||
d594027d | 1720 | static inline void line_info(struct seq_file *m, struct serial_state *state) |
1da177e4 | 1721 | { |
1da177e4 | 1722 | char stat_buf[30], control, status; |
1da177e4 LT |
1723 | unsigned long flags; |
1724 | ||
d594027d | 1725 | seq_printf(m, "%d: uart:amiga_builtin",state->line); |
1da177e4 | 1726 | |
1da177e4 LT |
1727 | local_irq_save(flags); |
1728 | status = ciab.pra; | |
01bd730d | 1729 | control = (state->tport.flags & ASYNC_INITIALIZED) ? state->MCR : status; |
1da177e4 LT |
1730 | local_irq_restore(flags); |
1731 | ||
1732 | stat_buf[0] = 0; | |
1733 | stat_buf[1] = 0; | |
1734 | if(!(control & SER_RTS)) | |
1735 | strcat(stat_buf, "|RTS"); | |
1736 | if(!(status & SER_CTS)) | |
1737 | strcat(stat_buf, "|CTS"); | |
1738 | if(!(control & SER_DTR)) | |
1739 | strcat(stat_buf, "|DTR"); | |
1740 | if(!(status & SER_DSR)) | |
1741 | strcat(stat_buf, "|DSR"); | |
1742 | if(!(status & SER_DCD)) | |
1743 | strcat(stat_buf, "|CD"); | |
1744 | ||
916b7656 JS |
1745 | if (state->quot) |
1746 | seq_printf(m, " baud:%d", state->baud_base / state->quot); | |
1da177e4 | 1747 | |
d594027d | 1748 | seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx); |
1da177e4 LT |
1749 | |
1750 | if (state->icount.frame) | |
d594027d | 1751 | seq_printf(m, " fe:%d", state->icount.frame); |
1da177e4 LT |
1752 | |
1753 | if (state->icount.parity) | |
d594027d | 1754 | seq_printf(m, " pe:%d", state->icount.parity); |
1da177e4 LT |
1755 | |
1756 | if (state->icount.brk) | |
d594027d | 1757 | seq_printf(m, " brk:%d", state->icount.brk); |
1da177e4 LT |
1758 | |
1759 | if (state->icount.overrun) | |
d594027d | 1760 | seq_printf(m, " oe:%d", state->icount.overrun); |
1da177e4 LT |
1761 | |
1762 | /* | |
1763 | * Last thing is the RS-232 status lines | |
1764 | */ | |
d594027d | 1765 | seq_printf(m, " %s\n", stat_buf+1); |
1da177e4 LT |
1766 | } |
1767 | ||
d594027d | 1768 | static int rs_proc_show(struct seq_file *m, void *v) |
1da177e4 | 1769 | { |
d594027d AD |
1770 | seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version); |
1771 | line_info(m, &rs_table[0]); | |
1772 | return 0; | |
1da177e4 LT |
1773 | } |
1774 | ||
d594027d AD |
1775 | static int rs_proc_open(struct inode *inode, struct file *file) |
1776 | { | |
1777 | return single_open(file, rs_proc_show, NULL); | |
1778 | } | |
1779 | ||
1780 | static const struct file_operations rs_proc_fops = { | |
1781 | .owner = THIS_MODULE, | |
1782 | .open = rs_proc_open, | |
1783 | .read = seq_read, | |
1784 | .llseek = seq_lseek, | |
1785 | .release = single_release, | |
1786 | }; | |
1787 | ||
1da177e4 LT |
1788 | /* |
1789 | * --------------------------------------------------------------------- | |
1790 | * rs_init() and friends | |
1791 | * | |
1792 | * rs_init() is called at boot-time to initialize the serial driver. | |
1793 | * --------------------------------------------------------------------- | |
1794 | */ | |
1795 | ||
1796 | /* | |
1797 | * This routine prints out the appropriate serial driver version | |
1798 | * number, and identifies which options were configured into this | |
1799 | * driver. | |
1800 | */ | |
41c28ff1 | 1801 | static void show_serial_version(void) |
1da177e4 LT |
1802 | { |
1803 | printk(KERN_INFO "%s version %s\n", serial_name, serial_version); | |
1804 | } | |
1805 | ||
1806 | ||
b68e31d0 | 1807 | static const struct tty_operations serial_ops = { |
1da177e4 LT |
1808 | .open = rs_open, |
1809 | .close = rs_close, | |
1810 | .write = rs_write, | |
1811 | .put_char = rs_put_char, | |
1812 | .flush_chars = rs_flush_chars, | |
1813 | .write_room = rs_write_room, | |
1814 | .chars_in_buffer = rs_chars_in_buffer, | |
1815 | .flush_buffer = rs_flush_buffer, | |
1816 | .ioctl = rs_ioctl, | |
1817 | .throttle = rs_throttle, | |
1818 | .unthrottle = rs_unthrottle, | |
1819 | .set_termios = rs_set_termios, | |
1820 | .stop = rs_stop, | |
1821 | .start = rs_start, | |
1822 | .hangup = rs_hangup, | |
1823 | .break_ctl = rs_break, | |
1824 | .send_xchar = rs_send_xchar, | |
1825 | .wait_until_sent = rs_wait_until_sent, | |
1da177e4 LT |
1826 | .tiocmget = rs_tiocmget, |
1827 | .tiocmset = rs_tiocmset, | |
0587102c | 1828 | .get_icount = rs_get_icount, |
d594027d | 1829 | .proc_fops = &rs_proc_fops, |
1da177e4 LT |
1830 | }; |
1831 | ||
1832 | /* | |
1833 | * The serial driver boot-time initialization code! | |
1834 | */ | |
826e8c8c | 1835 | static int __init amiga_serial_probe(struct platform_device *pdev) |
1da177e4 LT |
1836 | { |
1837 | unsigned long flags; | |
1838 | struct serial_state * state; | |
5edc304f | 1839 | int error; |
1da177e4 | 1840 | |
410235fd | 1841 | serial_driver = alloc_tty_driver(NR_PORTS); |
1da177e4 LT |
1842 | if (!serial_driver) |
1843 | return -ENOMEM; | |
1844 | ||
1da177e4 LT |
1845 | show_serial_version(); |
1846 | ||
1847 | /* Initialize the tty_driver structure */ | |
1848 | ||
1da177e4 LT |
1849 | serial_driver->driver_name = "amiserial"; |
1850 | serial_driver->name = "ttyS"; | |
1851 | serial_driver->major = TTY_MAJOR; | |
1852 | serial_driver->minor_start = 64; | |
1853 | serial_driver->type = TTY_DRIVER_TYPE_SERIAL; | |
1854 | serial_driver->subtype = SERIAL_TYPE_NORMAL; | |
1855 | serial_driver->init_termios = tty_std_termios; | |
1856 | serial_driver->init_termios.c_cflag = | |
1857 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; | |
1858 | serial_driver->flags = TTY_DRIVER_REAL_RAW; | |
1859 | tty_set_operations(serial_driver, &serial_ops); | |
1860 | ||
5edc304f GU |
1861 | error = tty_register_driver(serial_driver); |
1862 | if (error) | |
826e8c8c | 1863 | goto fail_put_tty_driver; |
1da177e4 LT |
1864 | |
1865 | state = rs_table; | |
1da177e4 LT |
1866 | state->port = (int)&custom.serdatr; /* Just to give it a value */ |
1867 | state->line = 0; | |
1868 | state->custom_divisor = 0; | |
1da177e4 LT |
1869 | state->icount.cts = state->icount.dsr = |
1870 | state->icount.rng = state->icount.dcd = 0; | |
1871 | state->icount.rx = state->icount.tx = 0; | |
1872 | state->icount.frame = state->icount.parity = 0; | |
1873 | state->icount.overrun = state->icount.brk = 0; | |
87758791 | 1874 | tty_port_init(&state->tport); |
1da177e4 LT |
1875 | |
1876 | printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n", | |
1877 | state->line); | |
1878 | ||
1879 | /* Hardware set up */ | |
1880 | ||
1881 | state->baud_base = amiga_colorclock; | |
1882 | state->xmit_fifo_size = 1; | |
1883 | ||
1da177e4 | 1884 | /* set ISRs, and then disable the rx interrupts */ |
5edc304f GU |
1885 | error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state); |
1886 | if (error) | |
1887 | goto fail_unregister; | |
1888 | ||
9cfb5c05 | 1889 | error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0, |
5edc304f GU |
1890 | "serial RX", state); |
1891 | if (error) | |
1892 | goto fail_free_irq; | |
1da177e4 | 1893 | |
c70c036f JL |
1894 | local_irq_save(flags); |
1895 | ||
1da177e4 LT |
1896 | /* turn off Rx and Tx interrupts */ |
1897 | custom.intena = IF_RBF | IF_TBE; | |
1898 | mb(); | |
1899 | ||
1900 | /* clear any pending interrupt */ | |
1901 | custom.intreq = IF_RBF | IF_TBE; | |
1902 | mb(); | |
1903 | ||
1904 | local_irq_restore(flags); | |
1905 | ||
1906 | /* | |
1907 | * set the appropriate directions for the modem control flags, | |
1908 | * and clear RTS and DTR | |
1909 | */ | |
1910 | ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */ | |
1911 | ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */ | |
1912 | ||
826e8c8c GU |
1913 | platform_set_drvdata(pdev, state); |
1914 | ||
1da177e4 | 1915 | return 0; |
5edc304f GU |
1916 | |
1917 | fail_free_irq: | |
1918 | free_irq(IRQ_AMIGA_TBE, state); | |
1919 | fail_unregister: | |
1920 | tty_unregister_driver(serial_driver); | |
5edc304f GU |
1921 | fail_put_tty_driver: |
1922 | put_tty_driver(serial_driver); | |
1923 | return error; | |
1da177e4 LT |
1924 | } |
1925 | ||
826e8c8c | 1926 | static int __exit amiga_serial_remove(struct platform_device *pdev) |
1da177e4 LT |
1927 | { |
1928 | int error; | |
826e8c8c | 1929 | struct serial_state *state = platform_get_drvdata(pdev); |
1da177e4 LT |
1930 | |
1931 | /* printk("Unloading %s: version %s\n", serial_name, serial_version); */ | |
1da177e4 LT |
1932 | if ((error = tty_unregister_driver(serial_driver))) |
1933 | printk("SERIAL: failed to unregister serial driver (%d)\n", | |
1934 | error); | |
1935 | put_tty_driver(serial_driver); | |
1936 | ||
916b7656 JS |
1937 | free_irq(IRQ_AMIGA_TBE, state); |
1938 | free_irq(IRQ_AMIGA_RBF, state); | |
5edc304f | 1939 | |
826e8c8c GU |
1940 | platform_set_drvdata(pdev, NULL); |
1941 | ||
1942 | return error; | |
1943 | } | |
1944 | ||
1945 | static struct platform_driver amiga_serial_driver = { | |
1946 | .remove = __exit_p(amiga_serial_remove), | |
1947 | .driver = { | |
1948 | .name = "amiga-serial", | |
1949 | .owner = THIS_MODULE, | |
1950 | }, | |
1951 | }; | |
1952 | ||
1953 | static int __init amiga_serial_init(void) | |
1954 | { | |
1955 | return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe); | |
1956 | } | |
1957 | ||
1958 | module_init(amiga_serial_init); | |
1959 | ||
1960 | static void __exit amiga_serial_exit(void) | |
1961 | { | |
1962 | platform_driver_unregister(&amiga_serial_driver); | |
1da177e4 LT |
1963 | } |
1964 | ||
826e8c8c | 1965 | module_exit(amiga_serial_exit); |
1da177e4 LT |
1966 | |
1967 | ||
d1a35e4d GU |
1968 | #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE) |
1969 | ||
1da177e4 LT |
1970 | /* |
1971 | * ------------------------------------------------------------ | |
1972 | * Serial console driver | |
1973 | * ------------------------------------------------------------ | |
1974 | */ | |
1da177e4 LT |
1975 | |
1976 | static void amiga_serial_putc(char c) | |
1977 | { | |
1978 | custom.serdat = (unsigned char)c | 0x100; | |
1979 | while (!(custom.serdatr & 0x2000)) | |
1980 | barrier(); | |
1981 | } | |
1982 | ||
1983 | /* | |
1984 | * Print a string to the serial port trying not to disturb | |
1985 | * any possible real use of the port... | |
1986 | * | |
1987 | * The console must be locked when we get here. | |
1988 | */ | |
1989 | static void serial_console_write(struct console *co, const char *s, | |
1990 | unsigned count) | |
1991 | { | |
1992 | unsigned short intena = custom.intenar; | |
1993 | ||
1994 | custom.intena = IF_TBE; | |
1995 | ||
1996 | while (count--) { | |
1997 | if (*s == '\n') | |
1998 | amiga_serial_putc('\r'); | |
1999 | amiga_serial_putc(*s++); | |
2000 | } | |
2001 | ||
2002 | custom.intena = IF_SETCLR | (intena & IF_TBE); | |
2003 | } | |
2004 | ||
2005 | static struct tty_driver *serial_console_device(struct console *c, int *index) | |
2006 | { | |
2007 | *index = 0; | |
2008 | return serial_driver; | |
2009 | } | |
2010 | ||
2011 | static struct console sercons = { | |
2012 | .name = "ttyS", | |
2013 | .write = serial_console_write, | |
2014 | .device = serial_console_device, | |
2015 | .flags = CON_PRINTBUFFER, | |
2016 | .index = -1, | |
2017 | }; | |
2018 | ||
2019 | /* | |
2020 | * Register console. | |
2021 | */ | |
2022 | static int __init amiserial_console_init(void) | |
2023 | { | |
2024 | register_console(&sercons); | |
2025 | return 0; | |
2026 | } | |
2027 | console_initcall(amiserial_console_init); | |
d1a35e4d GU |
2028 | |
2029 | #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */ | |
1da177e4 LT |
2030 | |
2031 | MODULE_LICENSE("GPL"); | |
826e8c8c | 2032 | MODULE_ALIAS("platform:amiga-serial"); |