1 /* -*- linux-c -*- */
2 /* $Id: 8253xsyn.c,v 1.17 2002/02/10 22:17:25 martillo Exp $
3  * 8253xsyn.c: SYNC TTY Driver for the SIEMENS SAB8253X DUSCC.
4  *
5  * Implementation, modifications and extensions
6  * Copyright (C) 2001 By Joachim Martillo, Telford Tools, Inc.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13 
14 /* Standard in kernel modules */
15 #define DEFINE_VARIABLE
16 #include <linux/module.h>   /* Specifically, a module */
17 #include <asm/io.h>
18 #include <linux/timer.h>
19 #include <linux/interrupt.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/mm.h>
23 #include <linux/version.h>
24 #include <asm/uaccess.h>
25 #include "8253xctl.h"
26 #include "8253x.h"
27 #include <linux/pci.h>
28 #include <linux/fs.h>
29 
30 #ifdef MODULE
31 #undef XCONFIG_SERIAL_CONSOLE
32 #endif
33 
34 
sab8253x_flush_to_ldiscS(void * private_)35 static void sab8253x_flush_to_ldiscS(void *private_) /* need a separate version for sync
36 						 there are no flags associated with
37 						 received sync TTY data*/
38 {
39 	struct tty_struct *tty = (struct tty_struct *) private_;
40 	unsigned char	*cp;
41 	int		count;
42 	struct sab_port *port;
43 	struct sk_buff *skb;
44 
45 	if(tty)
46 	{
47 		port = (struct sab_port *)tty->driver_data;
48 	}
49 	else
50 	{
51 		return;
52 	}
53 	if(port == NULL)
54 	{
55 		return;
56 	}
57 
58 	if (test_bit(TTY_DONT_FLIP, &tty->flags))
59 	{
60 		queue_task(&tty->flip.tqueue, &tq_timer);
61 		return;
62 	}
63 	/* note that a hangup may have occurred -- perhaps should check for that */
64 	port->DoingInterrupt = 1;
65 	while(port->sab8253xc_rcvbuflist && (skb_queue_len(port->sab8253xc_rcvbuflist) > 0))
66 	{
67 		skb = skb_dequeue(port->sab8253xc_rcvbuflist);
68 		count = skb->data_len;
69 		cp = skb->data;
70 		(*tty->ldisc.receive_buf)(tty, cp, 0, count);
71 		dev_kfree_skb_any(skb);
72 	}
73 	port->DoingInterrupt = 0;
74 }
75 
sab8253x_flush_charsS(struct tty_struct * tty)76 void sab8253x_flush_charsS(struct tty_struct *tty)
77 {
78 	struct sab_port *port = (struct sab_port *)tty->driver_data;
79 
80 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_flush_chars"))
81 	{
82 		return;
83 	}
84 
85 	if ((Sab8253xCountTransmit(port) <= 0) || tty->stopped || tty->hw_stopped)
86 	{				/* can't flush */
87 		return;
88 	}
89 
90 	sab8253x_start_txS(port);
91 }
92 
93 /*
94  * ------------------------------------------------------------
95  * sab8253x_stopS() and sab8253x_startS()
96  *
97  * This routines are called before setting or resetting tty->stopped.
98  * They enable or disable transmitter interrupts, as necessary.
99  * ------------------------------------------------------------
100  */
101 
sab8253x_stopS(struct tty_struct * tty)102 void sab8253x_stopS(struct tty_struct *tty)
103 {
104 	struct sab_port *port = (struct sab_port *)tty->driver_data;
105 	/* can't do anything here */
106 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_stop"))
107 	{
108 		return;
109 	}
110 	/*  interrupt handles it all*/
111 	/* turning off XPR is not an option in sync mode */
112 }
113 
sab8253x_startS(struct tty_struct * tty)114 void sab8253x_startS(struct tty_struct *tty)
115 {
116 	struct sab_port *port = (struct sab_port *)tty->driver_data;
117 
118 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_start"))
119 	{
120 		return;
121 	}
122 	sab8253x_start_txS(port);
123 }
124 
125 
sab8253x_receive_charsS(struct sab_port * port,union sab8253x_irq_status * stat)126 static void sab8253x_receive_charsS(struct sab_port *port,
127 			     union sab8253x_irq_status *stat)
128 {
129 	struct tty_struct *tty = port->tty;
130 	unsigned char buf[32];
131 	int free_fifo = 0;
132 	int reset_fifo = 0;
133 	int msg_done = 0;
134 	int msg_bad = 0;
135 	int count = 0;
136 	int total_size = 0;
137 	int rstatus = 0;
138 	struct sk_buff *skb;
139 
140 	/* Read number of BYTES (Character + Status) available. */
141 
142 	if((stat->images[ISR1_IDX] & SAB82532_ISR1_RDO) || (stat->images[ISR0_IDX] & SAB82532_ISR0_RFO) )
143 	{
144 		++msg_bad;
145 		++free_fifo;
146 		++reset_fifo;
147 	}
148 	else
149 	{
150 		if (stat->images[ISR0_IDX] & SAB82532_ISR0_RPF)
151 		{
152 			count = port->recv_fifo_size;
153 			++free_fifo;
154 		}
155 
156 		if (stat->images[ISR0_IDX] & SAB82532_ISR0_RME)
157 		{
158 			count = READB(port, rbcl);
159 			count &= (port->recv_fifo_size - 1);
160 			++msg_done;
161 			++free_fifo;
162 
163 			total_size = READB(port, rbch);
164 			if(total_size & SAB82532_RBCH_OV) /* need to revisit for 4096 byte frames */
165 			{
166 				msg_bad++;
167 			}
168 
169 			rstatus = READB(port, rsta);
170 			if((rstatus & SAB82532_RSTA_VFR) == 0)
171 			{
172 				msg_bad++;
173 			}
174 			if(rstatus & SAB82532_RSTA_RDO)
175 			{
176 				msg_bad++;
177 			}
178 			if((rstatus & SAB82532_RSTA_CRC) == 0)
179 			{
180 				msg_bad++;
181 			}
182 			if(rstatus & SAB82532_RSTA_RAB)
183 			{
184 				msg_bad++;
185 			}
186 		}
187 	}
188 
189 	/* Read the FIFO. */
190 
191 	(*port->readfifo)(port, buf, count);
192 
193 
194 	/* Issue Receive Message Complete command. */
195 
196 	if (free_fifo)
197 	{
198 		sab8253x_cec_wait(port);
199 		WRITEB(port, cmdr, SAB82532_CMDR_RMC);
200 	}
201 
202 	if(reset_fifo)
203 	{
204 		sab8253x_cec_wait(port);
205 		WRITEB(port, cmdr, SAB82532_CMDR_RHR);
206 	}
207 
208 	if(msg_bad)
209 	{
210 		port->msgbufindex = 0;
211 		return;
212 	}
213 
214 	memcpy(&port->msgbuf[port->msgbufindex], buf, count);
215 	port->msgbufindex += count;
216 
217 #ifdef CONSOLE_SUPPORT
218 	if (port->is_console)
219 	{
220 		wake_up(&keypress_wait);
221 	}
222 #endif
223 
224 	if(msg_done)
225 	{
226 
227 		if(port->msgbufindex <= 3) /* min is 1 char + 2 CRC + status byte */
228 		{
229 			port->msgbufindex = 0;
230 			return;
231 		}
232 
233 		total_size = port->msgbufindex - 3; /* strip off the crc16 and the status byte */
234 		port->msgbufindex = 0;
235 
236 		/* ignore the receive buffer waiting -- we know the correct size here */
237 
238 		if (!tty)
239 		{
240 			return;
241 		}
242 		if(skb = dev_alloc_skb(total_size), skb)
243 		{
244 			memcpy(skb->data, &port->msgbuf[0], total_size);
245 			skb->tail = (skb->data + total_size);
246 			skb->data_len = total_size;
247 			skb->len = total_size;
248 			skb_queue_tail(port->sab8253xc_rcvbuflist, skb);
249 		}
250 		queue_task(&tty->flip.tqueue, &tq_timer); /* clear out flip buffer as fast as possible
251 							   * maybe should not be done unconditionally hear
252 							   * but should be within the above consequence
253 							   * clause */
254 	}
255 }
256 
257 
sab8253x_check_statusS(struct sab_port * port,union sab8253x_irq_status * stat)258 static void sab8253x_check_statusS(struct sab_port *port,
259 			    union sab8253x_irq_status *stat)
260 {
261 	struct tty_struct *tty = port->tty;
262 	int modem_change = 0;
263 	mctlsig_t         *sig;
264 
265 	if (!tty)
266 	{
267 		return;
268 	}
269 
270 	/* check_modem:*/
271 	/* Checking DCD */
272 	sig = &port->dcd;
273 	if (stat->images[sig->irq] & sig->irqmask)
274 	{
275 		sig->val = ISON(port,dcd);
276 		port->icount.dcd++;
277 		modem_change++;
278 	}
279 	/* Checking CTS */
280 	sig = &port->cts;
281 	if (stat->images[sig->irq] & sig->irqmask)
282 	{
283 		sig->val = ISON(port,cts);
284 		port->icount.cts++;
285 		modem_change++;
286 	}
287 	/* Checking DSR */
288 	sig = &port->dsr;
289 	if (stat->images[sig->irq] & sig->irqmask)
290 	{
291 		sig->val = ISON(port,dsr);
292 		port->icount.dsr++;
293 		modem_change++;
294 	}
295 	if (modem_change)
296 	{
297 		wake_up_interruptible(&port->delta_msr_wait);
298 	}
299 
300 	sig = &port->dcd;
301 	if ((port->flags & FLAG8253X_CHECK_CD) &&
302 	    (stat->images[sig->irq] & sig->irqmask))
303 	{
304 
305 		if (sig->val)
306 		{
307 			wake_up_interruptible(&port->open_wait);
308 		}
309 		else if (!((port->flags & FLAG8253X_CALLOUT_ACTIVE) &&
310 			   (port->flags & FLAG8253X_CALLOUT_NOHUP)))
311 		{
312 #if 0				/* requires more investigation */
313 			MOD_INC_USE_COUNT;
314 			if (schedule_task(&port->tqueue_hangup) == 0)
315 			{
316 				MOD_DEC_USE_COUNT;
317 			}
318 #endif
319 		}
320 	}
321 
322 	sig = &port->cts;
323 	if (port->flags & FLAG8253X_CTS_FLOW)
324 	{				/* not setting this yet */
325 		if (port->tty->hw_stopped)
326 		{
327 			if (sig->val)
328 			{
329 				port->tty->hw_stopped = 0;
330 				sab8253x_sched_event(port, SAB8253X_EVENT_WRITE_WAKEUP);
331 				sab8253x_start_txS(port);
332 			}
333 		}
334 
335 		else
336 		{
337 			if(!(getccr2configS(port) & SAB82532_CCR2_TOE))
338 			{
339 				if (!(sig->val))
340 				{
341 					port->tty->hw_stopped = 1;
342 				}
343 			}
344 		}
345 	}
346 }
347 
348 /*
349  * This routine is called to set the UART divisor registers to match
350  * the specified baud rate for a serial port.
351  */
sab8253x_change_speedS(struct sab_port * port)352 static void sab8253x_change_speedS(struct sab_port *port)
353 {
354 	unsigned long	flags,baud;
355 	tcflag_t	cflag;
356 	u8	        ccr2=0,ccr4=0,ebrg=0;
357 	int		i, bits;
358 #ifdef DEBUGGING
359 	printk("Change speed!  ");
360 #endif
361 	if (!port->tty || !port->tty->termios)
362 	{
363 #ifdef DEBUGGING
364 		printk("NOT!\n");
365 #endif
366 		return;
367 	}
368 
369 #ifdef DEBUGGING
370 	printk(" for real.\n");
371 #endif
372 
373 	cflag = port->tty->termios->c_cflag;
374 
375 	/* Byte size and parity */
376 	switch (cflag & CSIZE)
377 	{
378 	case CS5:
379 		bits = 7;
380 		break;
381 	case CS6:
382 		bits = 8;
383 		break;
384 	case CS7:
385 		bits = 9;
386 		break;
387 	default:
388 	case CS8:
389 		bits = 10;
390 		break;
391 	}
392 
393 	if (cflag & CSTOPB)
394 	{
395 		bits++;
396 	}
397 
398 	if (cflag & PARENB)
399 	{
400 		bits++;
401 	}
402 
403 	/* Determine EBRG values based on the "encoded"baud rate */
404 	i = cflag & CBAUD;
405 	switch(i)
406 	{
407 	case B0:
408 		baud=0;
409 		break;
410 	case  B50:
411 		baud=100;
412 		break;
413 	case  B75:
414 		baud=150;
415 		break;
416 	case  B110:
417 		baud=220;
418 		break;
419 	case  B134:
420 		baud=269;
421 		break;
422 	case  B150:
423 		baud=300;
424 		break;
425 	case  B200:
426 		baud=400;
427 		break;
428 	case B300:
429 		baud=600;
430 		break;
431 	case B600:
432 		baud=1200;
433 		break;
434 	case B1200:
435 		baud=2400;
436 		break;
437 	case B1800:
438 		baud=3600;
439 		break;
440 	case B2400:
441 		baud=4800;
442 		break;
443 	case B4800:
444 		baud=9600;
445 		break;
446 	case B9600:
447 		baud=19200;
448 		break;
449 	case B19200:
450 		baud=38400;
451 		break;
452 	case  B38400:
453 		if(port->custspeed)
454 		{
455 			baud=port->custspeed<<1;
456 		}
457 		else
458 		{
459 			baud=76800;
460 		}
461 		break;
462 	case B57600:
463 		baud=115200;
464 		break;
465 #ifdef SKIPTHIS
466 	case B76800:
467 		baud=153600;
468 		break;
469 	case B153600:
470 		baud=307200;
471 		break;
472 #endif
473 	case B230400:
474 		baud=460800;
475 		break;
476 	case  B460800:
477 		baud=921600;
478 		break;
479 	case B115200:
480 	default:
481 		baud=230400;
482 		break;
483 	}
484 
485 	if(!sab8253x_baud(port,baud,&ebrg,&ccr2,&ccr4,&(port->baud)))
486 	{
487 		printk("Aurora Warning. baudrate %ld could not be set! Using 115200",baud);
488 		baud=230400;
489 		sab8253x_baud(port,baud,&ebrg,&ccr2,&ccr4,&(port->baud));
490 	}
491 
492 	if (port->baud)
493 		port->timeout = (port->xmit_fifo_size * HZ * bits) / port->baud;
494 	else
495 		port->timeout = 0;
496 	port->timeout += HZ / 50;		/* Add .02 seconds of slop */
497 
498 	/* CTS flow control flags */
499 	if (cflag & CRTSCTS)
500 		port->flags |= FLAG8253X_CTS_FLOW;
501 	else
502 		port->flags &= ~(FLAG8253X_CTS_FLOW);
503 
504 	if (cflag & CLOCAL)
505 		port->flags &= ~(FLAG8253X_CHECK_CD);
506 	else
507 		port->flags |= FLAG8253X_CHECK_CD;
508 	if (port->tty)
509 		port->tty->hw_stopped = 0;
510 
511 	/*
512 	 * Set up parity check flag
513 	 * XXX: not implemented, yet.
514 	 */
515 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
516 
517 	/*
518 	 * Characters to ignore
519 	 * XXX: not implemented, yet.
520 	 */
521 
522 	/*
523 	 * !!! ignore all characters if CREAD is not set
524 	 * XXX: not implemented, yet.
525 	 */
526 	if ((cflag & CREAD) == 0)
527 		port->ignore_status_mask |= SAB82532_ISR0_RPF;
528 
529 	save_flags(flags);
530 	cli();
531 	sab8253x_cec_wait(port);
532 
533 	WRITEB(port, bgr, ebrg);
534 	WRITEB(port, ccr2, READB(port, ccr2) & ~(0xc0)); /* clear out current baud rage */
535 	WRITEB(port, ccr2, READB(port, ccr2) | ccr2);
536 	WRITEB(port, ccr4, (READB(port,ccr4) & ~SAB82532_CCR4_EBRG) | ccr4);
537 
538 	if (port->flags & FLAG8253X_CTS_FLOW)
539 	{
540 		WRITEB(port, mode, READB(port,mode) & ~(SAB82532_MODE_RTS));
541 		port->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
542 		WRITEB(port, imr1, port->interrupt_mask1);
543 	}
544 	else
545 	{
546 		WRITEB(port, mode, READB(port,mode) | SAB82532_MODE_RTS);
547 		port->interrupt_mask1 |= SAB82532_IMR1_CSC;
548 		WRITEB(port, imr1, port->interrupt_mask1);
549 	}
550 	WRITEB(port, mode, READB(port, mode) | SAB82532_MODE_RAC);
551 	restore_flags(flags);
552 }
553 
sab8253x_set_termiosS(struct tty_struct * tty,struct termios * old_termios)554 void sab8253x_set_termiosS(struct tty_struct *tty,
555 			   struct termios *old_termios)
556 {
557 	struct sab_port *port = (struct sab_port *)tty->driver_data;
558 
559 	if((tty->termios->c_cflag == old_termios->c_cflag) &&
560 	   (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag)))
561 	{
562 		return;
563 	}
564 	if(!port)
565 	{
566 		return;
567 	}
568 	sab8253x_change_speedS(port);
569 
570 	/* Handle transition to B0 status */
571 	if ((old_termios->c_cflag & CBAUD) &&
572 	    !(tty->termios->c_cflag & CBAUD))
573 	{
574 		LOWER(port,rts);
575 		LOWER(port,dtr);
576 	}
577 
578 	/* Handle transition away from B0 status */
579 	if (!(old_termios->c_cflag & CBAUD) &&
580 	    (tty->termios->c_cflag & CBAUD))
581 	{
582 		RAISE(port,dtr);
583 		if (!tty->hw_stopped ||
584 		    !(tty->termios->c_cflag & CRTSCTS))
585 		{
586 			RAISE(port,rts);
587 		}
588 	}
589 
590 	/* Handle turning off CRTSCTS */
591 	if ((old_termios->c_cflag & CRTSCTS) &&
592 	    !(tty->termios->c_cflag & CRTSCTS))
593 	{
594 		tty->hw_stopped = 0;
595 		sab8253x_startS(tty);
596 	}
597 }
598 
sab8253x_startupS(struct sab_port * port)599 static int sab8253x_startupS(struct sab_port *port)
600 {
601 	unsigned long flags;
602 	int retval = 0;
603 
604 	save_flags(flags); cli();
605 
606 	port->msgbufindex = 0;
607 	port->xmit_buf = NULL;
608 	port->buffergreedy = 0;
609 
610 	if (port->flags & FLAG8253X_INITIALIZED)
611 	{
612 		goto errout;
613 	}
614 
615 	if (!port->regs)
616 	{
617 		if (port->tty)
618 		{
619 			set_bit(TTY_IO_ERROR, &port->tty->flags);
620 		}
621 		retval = -ENODEV;
622 		goto errout;
623 	}
624 	/*
625 	 * Initialize the Hardware
626 	 */
627 	sab8253x_init_lineS(port);
628 
629 #if 0				/* maybe should be conditional */
630 	if (port->tty->termios->c_cflag & CBAUD)
631 	{
632 #endif
633 		/* Activate RTS */
634 		RAISE(port,rts);
635 		/* Activate DTR */
636 		RAISE(port,dtr);
637 #if 0
638 	}
639 #endif
640 
641 	/*
642 	 * Initialize the modem signals values
643 	 */
644 	port->dcd.val=ISON(port,dcd);
645 	port->cts.val=ISON(port,cts);
646 	port->dsr.val=ISON(port,dsr);
647 	/*
648 	 * Finally, enable interrupts
649 	 */
650 
651 	port->interrupt_mask0 = SAB82532_IMR0_RFS | SAB82532_IMR0_PCE |
652 		SAB82532_IMR0_PLLA | SAB82532_IMR0_RSC | SAB82532_IMR0_CDSC;
653 
654 	/*((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? SAB82532_IMR0_CDSC : 0); */
655 
656 	WRITEB(port,imr0,port->interrupt_mask0);
657 	port->interrupt_mask1 = SAB82532_IMR1_EOP | SAB82532_IMR1_XMR |
658 		SAB82532_IMR1_TIN | SAB82532_IMR1_XPR;
659 	WRITEB(port, imr1, port->interrupt_mask1);
660 	port->all_sent = 1;
661 
662 	if (port->tty)
663 	{
664 		clear_bit(TTY_IO_ERROR, &port->tty->flags);
665 	}
666 	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
667 
668 	/*
669 	 * and set the speed of the serial port
670 	 */
671 	sab8253x_change_speedS(port);
672 
673 	port->flags |= FLAG8253X_INITIALIZED;
674 	port->receive_chars = sab8253x_receive_charsS;
675 	port->transmit_chars = sab8253x_transmit_charsS;
676 	port->check_status = sab8253x_check_statusS;
677 	port->receive_test = (SAB82532_ISR0_RME | SAB82532_ISR0_RFO | SAB82532_ISR0_RPF);
678 	port->transmit_test = (SAB82532_ISR1_ALLS | SAB82532_ISR1_RDO | SAB82532_ISR1_XPR |
679 			       SAB82532_ISR1_XDU | SAB82532_ISR1_CSC);
680 	port->check_status_test = (SAB82532_ISR1_CSC);
681 
682 	/*((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? 0 : SAB82532_ISR0_CDSC));*/
683 
684 
685 	restore_flags(flags);
686 	return 0;
687 
688  errout:
689 	restore_flags(flags);
690 	return retval;
691 }
692 
sab8253x_shutdownS(struct sab_port * port)693 static void sab8253x_shutdownS(struct sab_port *port)
694 {
695 	unsigned long flags;
696 
697 	if (!(port->flags & FLAG8253X_INITIALIZED))
698 	{
699 		return;
700 	}
701 
702 	save_flags(flags); cli(); /* Disable interrupts */
703 
704 	/*
705 	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
706 	 * here so the queue might never be waken up
707 	 */
708 	wake_up_interruptible(&port->delta_msr_wait);
709 
710 	if (port->xmit_buf)
711 	{
712 		port->xmit_buf = 0;
713 	}
714 #ifdef XCONFIG_SERIAL_CONSOLE
715 	if (port->is_console)
716 	{
717 		port->interrupt_mask0 =
718 			SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
719 			/*SAB82532_IMR0_TIME |*/
720 			SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
721 		WRITEB(port,imr0,port->interrupt_mask0);
722 		port->interrupt_mask1 =
723 			SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
724 			SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
725 			SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
726 			SAB82532_IMR1_XPR;
727 		WRITEB(port,imr1,port->interrupt_mask1);
728 		if (port->tty)
729 		{
730 			set_bit(TTY_IO_ERROR, &port->tty->flags);
731 		}
732 		port->flags &= ~FLAG8253X_INITIALIZED;
733 		restore_flags(flags);
734 		return;
735 	}
736 #endif
737 
738 	/* Disable Interrupts */
739 
740 	port->interrupt_mask0 = 0xff;
741 	WRITEB(port, imr0, port->interrupt_mask0);
742 	port->interrupt_mask1 = 0xff;
743 	WRITEB(port, imr1, port->interrupt_mask1);
744 
745 	if (!port->tty || (port->tty->termios->c_cflag & HUPCL))
746 	{
747 		LOWER(port,rts);
748 		LOWER(port,dtr);
749 	}
750 
751 	/* Disable Receiver */
752 	CLEAR_REG_BIT(port,mode,SAB82532_MODE_RAC);
753 
754 	/* Power Down */
755 	CLEAR_REG_BIT(port,ccr0,SAB82532_CCR0_PU);
756 
757 	if (port->tty)
758 	{
759 		set_bit(TTY_IO_ERROR, &port->tty->flags);
760 	}
761 
762 	port->flags &= ~FLAG8253X_INITIALIZED;
763 	restore_flags(flags);
764 }
765 
sab8253x_writeS(struct tty_struct * tty,int from_user,const unsigned char * buf,int count)766 int sab8253x_writeS(struct tty_struct * tty, int from_user,
767 		    const unsigned char *buf, int count)
768 {
769 	struct sab_port *port = (struct sab_port *)tty->driver_data;
770 	struct sk_buff *skb;
771 	int truelength = 0;
772 	int do_queue = 1;
773 
774 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_write"))
775 	{
776 		return 0;
777 	}
778 
779 	if(count == 0)
780 	{
781 		return 0;
782 	}
783 
784 	if(port->active2.transmit == NULL)
785 	{
786 		return 0;
787 	}
788 
789 	if((port->active2.transmit->Count & OWNER) == OWN_SAB)
790 	{
791 		sab8253x_start_txS(port);	/* no descriptor slot */
792 		return 0;
793 	}
794 
795 #ifndef FREEININTERRUPT
796 	skb = port->active2.transmit->HostVaddr; /* current slot value */
797 
798 	if(port->buffergreedy == 0)	/* are we avoiding buffer free's */
799 	{				/* no */
800 		if((skb != NULL) || /* not OWN_SAB from above */
801 		   (port->active2.transmit->crcindex != 0))
802 		{
803 			register RING_DESCRIPTOR *freeme;
804 
805 			freeme = port->active2.transmit;
806 			do
807 			{
808 				if((freeme->crcindex == 0) && (freeme->HostVaddr == NULL))
809 				{
810 					break;
811 				}
812 				if(freeme->HostVaddr)
813 				{
814 					skb_unlink((struct sk_buff*)freeme->HostVaddr);
815 					dev_kfree_skb_any((struct sk_buff*)freeme->HostVaddr);
816 					freeme->HostVaddr = NULL;
817 				}
818 				freeme->sendcrc = 0;
819 				freeme->crcindex = 0;
820 				freeme = (RING_DESCRIPTOR*) freeme->VNext;
821 			}
822 			while((freeme->Count & OWNER) != OWN_SAB);
823 		}
824 		skb = NULL;		/* buffer was freed */
825 	}
826 
827 	if(skb != NULL)		/* potentially useful */
828 	{
829 		truelength = (skb->end - skb->head);
830 		if(truelength >= count)
831 		{
832 			skb->data = skb->head; /* this buffer is already queued */
833 			skb->tail = skb->head;
834 			do_queue = 0;
835 		}
836 		else
837 		{
838 			skb_unlink(skb);
839 			dev_kfree_skb_any(skb);
840 			skb = NULL;
841 			port->active2.transmit->HostVaddr = NULL;
842 		}
843 	}
844 	/* in all cases the following is allowed */
845 	port->active2.transmit->sendcrc = 0;
846 	port->active2.transmit->crcindex = 0;
847 #endif
848 
849 	if(skb == NULL)
850 	{
851 		if(port->DoingInterrupt)
852 		{
853 			skb = alloc_skb(count, GFP_ATOMIC);
854 		}
855 		else
856 		{
857 			skb = alloc_skb(count, GFP_KERNEL);
858 		}
859 	}
860 
861 	if(skb == NULL)
862 	{
863 		printk(KERN_ALERT "sab8253xs: no skbuffs available.\n");
864 		return 0;
865 	}
866 	if(from_user)
867 	{
868 		copy_from_user(skb->data, buf, count);
869 	}
870 	else
871 	{
872 		memcpy(skb->data, buf, count);
873 	}
874 	skb->tail = (skb->data + count);
875 	skb->data_len = count;
876 	skb->len = count;
877 
878 	if(do_queue)
879 	{
880 		skb_queue_head(port->sab8253xbuflist, skb);
881 	}
882 
883 	port->active2.transmit->HostVaddr = skb;
884 	port->active2.transmit->sendcrc = 0;
885 	port->active2.transmit->crcindex = 0;
886 	port->active2.transmit->Count = (OWN_SAB|count);
887 	port->active2.transmit = port->active2.transmit->VNext;
888 
889 	sab8253x_start_txS(port);
890 	return count;
891 }
892 
sab8253x_throttleS(struct tty_struct * tty)893 void sab8253x_throttleS(struct tty_struct * tty)
894 {
895 	struct sab_port *port = (struct sab_port *)tty->driver_data;
896 
897 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_throttleS"))
898 	{
899 		return;
900 	}
901 
902 	if (!tty)
903 	{
904 		return;
905 	}
906 
907 	if (I_IXOFF(tty))
908 	{
909 		sab8253x_send_xcharS(tty, STOP_CHAR(tty));
910 	}
911 }
912 
sab8253x_unthrottleS(struct tty_struct * tty)913 void sab8253x_unthrottleS(struct tty_struct * tty)
914 {
915 	struct sab_port *port = (struct sab_port *)tty->driver_data;
916 
917 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_unthrottle"))
918 	{
919 		return;
920 	}
921 
922 	if (!tty)
923 	{
924 		return;
925 	}
926 
927 	if (I_IXOFF(tty))
928 	{
929 		sab8253x_send_xcharS(tty, START_CHAR(tty));
930 	}
931 }
932 
sab8253x_send_xcharS(struct tty_struct * tty,char ch)933 void sab8253x_send_xcharS(struct tty_struct *tty, char ch)
934 {
935 	struct sab_port *port = (struct sab_port *)tty->driver_data;
936 	unsigned long flags;
937 	int stopped;
938 	int hw_stopped;
939 
940 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_send_xcharS"))
941 	{
942 		return;
943 	}
944 
945 	if (!tty)
946 	{
947 		return;
948 	}
949 
950 	if(port->sabnext2.transmit == NULL)
951 	{
952 		return;
953 	}
954 
955 	save_flags(flags); cli();
956 
957 	if((port->sabnext2.transmit->Count & OWNER) == OWN_SAB) /* may overwrite a character
958 								 * -- but putting subsequent
959 								 * XONs or XOFFs later in the
960 								 * stream could cause problems
961 								 * with the XON and XOFF protocol */
962 	{
963 		port->sabnext2.transmit->sendcrc = 1;
964 		port->sabnext2.transmit->crcindex = 3;
965 		port->sabnext2.transmit->crc = (ch << 24); /* LITTLE ENDIAN */
966 		restore_flags(flags);
967 	}
968 	else
969 	{
970 		restore_flags(flags);
971 		sab8253x_writeS(tty, 0, &ch, 1);
972 	}
973 
974 	stopped = tty->stopped;
975 	hw_stopped = tty->hw_stopped;
976 	tty->stopped = 0;
977 	tty->hw_stopped = 0;
978 
979 	sab8253x_start_txS(port);
980 
981 	tty->stopped = stopped;
982 	tty->hw_stopped = hw_stopped;
983 }
984 
985 
sab8253x_breakS(struct tty_struct * tty,int break_state)986 void sab8253x_breakS(struct tty_struct *tty, int break_state)
987 {
988 	struct sab_port *port = (struct sab_port *) tty->driver_data;
989 
990 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_breakS"))
991 	{
992 		return;
993 	} /* can't break in sync mode */
994 }
995 
sab8253x_closeS(struct tty_struct * tty,struct file * filp)996 void sab8253x_closeS(struct tty_struct *tty, struct file * filp)
997 {
998 	struct sab_port *port = (struct sab_port *)tty->driver_data;
999 	unsigned long flags;
1000 
1001 	MOD_DEC_USE_COUNT;
1002 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_closeS"))
1003 	{
1004 		return;
1005 	}
1006 
1007 	if(port->open_type == OPEN_SYNC_NET)
1008 	{				/* port->tty field should already be NULL */
1009 		return;
1010 	}
1011 
1012 	save_flags(flags); cli();
1013 	--(port->count);
1014 	if (tty_hung_up_p(filp))
1015 	{
1016 		if(port->count == 0)	/* I think the reason for the weirdness
1017 					   relates to freeing of structures in
1018 					   the tty driver */
1019 		{
1020 			port->open_type = OPEN_NOT;
1021 		}
1022 		else if(port->count < 0)
1023 		{
1024 			printk(KERN_ALERT "XX20: port->count went negative.\n");
1025 			port->count = 0;
1026 			port->open_type = OPEN_NOT;
1027 		}
1028 		restore_flags(flags);
1029 		return;
1030 	}
1031 
1032 #if 0
1033 	if ((tty->count == 1) && (port->count != 0))
1034 	{
1035 		/*
1036 		 * Uh, oh.  tty->count is 1, which means that the tty
1037 		 * structure will be freed.  port->count should always
1038 		 * be one in these conditions.  If it's greater than
1039 		 * one, we've got real problems, since it means the
1040 		 * serial port won't be shutdown.
1041 		 */
1042 		printk("sab8253x_close: bad serial port count; tty->count is 1,"
1043 		       " port->count is %d\n", port->count);
1044 		port->count = 0;
1045 	}
1046 #endif
1047 
1048 	if (port->count < 0)
1049 	{
1050 		printk(KERN_ALERT "sab8253x_close: bad serial port count for ttys%d: %d\n",
1051 		       port->line, port->count);
1052 		port->count = 0;
1053 	}
1054 	if (port->count)
1055 	{
1056 		restore_flags(flags);
1057 		return;
1058 	}
1059 	port->flags |= FLAG8253X_CLOSING;
1060 
1061 	/*
1062 	 * Save the termios structure, since this port may have
1063 	 * separate termios for callout and dialin.
1064 	 */
1065 	if (port->flags & FLAG8253X_NORMAL_ACTIVE)
1066 	{
1067 		port->normal_termios = *tty->termios;
1068 	}
1069 	if (port->flags & FLAG8253X_CALLOUT_ACTIVE)
1070 	{
1071 		port->callout_termios = *tty->termios;
1072 	}
1073 	/*
1074 	 * Now we wait for the transmit buffer to clear; and we notify
1075 	 * the line discipline to only process XON/XOFF characters.
1076 	 */
1077 	tty->closing = 1;
1078 	if (port->closing_wait != SAB8253X_CLOSING_WAIT_NONE)
1079 	{
1080 		tty_wait_until_sent(tty, port->closing_wait);
1081 	}
1082 
1083 	/*
1084 	 * At this point we stop accepting input.  To do this, we
1085 	 * disable the receive line status interrupts, and turn off
1086 	 * the receiver.
1087 	 */
1088 
1089 #if 0
1090 	port->interrupt_mask0 |= SAB82532_IMR0_TCD; /* not needed for sync */
1091 #endif
1092 	WRITEB(port,imr0,port->interrupt_mask0);
1093 
1094 	CLEAR_REG_BIT(port, mode, SAB82532_MODE_RAC); /* turn off receiver */
1095 
1096 	if (port->flags & FLAG8253X_INITIALIZED)
1097 	{
1098 		/*
1099 		 * Before we drop DTR, make sure the UART transmitter
1100 		 * has completely drained; this is especially
1101 		 * important if there is a transmit FIFO!
1102 		 */
1103 		sab8253x_wait_until_sent(tty, port->timeout);
1104 	}
1105 	sab8253x_shutdownS(port);
1106 	Sab8253xCleanUpTransceiveN(port);
1107 	if (tty->driver.flush_buffer)
1108 	{
1109 		tty->driver.flush_buffer(tty);
1110 	}
1111 	tty_ldisc_flush(tty);
1112 	tty->closing = 0;
1113 	port->event = 0;
1114 	port->tty = 0;
1115 	if (port->blocked_open)
1116 	{
1117 		if (port->close_delay)
1118 		{
1119 			current->state = TASK_INTERRUPTIBLE;
1120 			schedule_timeout(port->close_delay);
1121 		}
1122 		wake_up_interruptible(&port->open_wait);
1123 	}
1124 	port->flags &= ~(FLAG8253X_NORMAL_ACTIVE|FLAG8253X_CALLOUT_ACTIVE|
1125 			 FLAG8253X_CLOSING);
1126 	wake_up_interruptible(&port->close_wait);
1127 	port->open_type = OPEN_NOT;
1128 	restore_flags(flags);
1129 }
1130 
1131 
sab8253x_hangupS(struct tty_struct * tty)1132 void sab8253x_hangupS(struct tty_struct *tty)
1133 {
1134 	struct sab_port * port = (struct sab_port *)tty->driver_data;
1135 
1136 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_hangupS"))
1137 	{
1138 		return;
1139 	}
1140 
1141 #ifdef XCONFIG_SERIAL_CONSOLE
1142 	if (port->is_console)
1143 	{
1144 		return;
1145 	}
1146 #endif
1147 
1148 	sab8253x_flush_buffer(tty);
1149 	if(port)
1150 	{
1151 		sab8253x_shutdownS(port);
1152 		Sab8253xCleanUpTransceiveN(port);
1153 		port->event = 0;
1154 		port->flags &= ~(FLAG8253X_NORMAL_ACTIVE|FLAG8253X_CALLOUT_ACTIVE);
1155 		port->tty = 0;
1156 		wake_up_interruptible(&port->open_wait);
1157 	}
1158 }
1159 
sab8253x_openS(struct tty_struct * tty,struct file * filp)1160 int sab8253x_openS(struct tty_struct *tty, struct file * filp)
1161 {
1162 	struct sab_port	*port;
1163 	int retval, line;
1164 	int counter;
1165 	unsigned long flags;
1166 
1167 	MOD_INC_USE_COUNT;
1168 	line = MINOR(tty->device) - tty->driver.minor_start;
1169 
1170 	for(counter = 0, port = AuraPortRoot;
1171 	    (counter < line) && (port != NULL);
1172 	    ++counter)
1173 	{
1174 		port = port->next;
1175 	}
1176 
1177 	if (!port)
1178 	{
1179 		printk(KERN_ALERT "sab8253x_openS: can't find structure for line %d\n",
1180 		       line);
1181 		return -ENODEV;
1182 	}
1183 
1184 	save_flags(flags);		/* Need to protect port->tty element */
1185 	cli();
1186 
1187 	if(port->tty == 0)
1188 	{
1189 		port->tty = tty;
1190 		tty->flip.tqueue.routine = sab8253x_flush_to_ldiscS;
1191 	}
1192 	tty->driver_data = port;
1193 
1194 	if(port->function != FUNCTION_NR)
1195 	{
1196 		++(port->count);
1197 		restore_flags(flags);
1198 		return -ENODEV;		/* only allowed if there are no restrictions on the port */
1199 	}
1200 
1201 	if(port->open_type == OPEN_SYNC_NET)
1202 	{
1203 		port->tty = NULL;	/* Don't bother with open counting here
1204 					   but make sure the tty field is NULL*/
1205 		restore_flags(flags);
1206 		return -EBUSY;
1207 	}
1208 
1209 	restore_flags(flags);
1210 
1211 	if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_openS"))
1212 	{
1213 		++(port->count);
1214 		return -ENODEV;
1215 	}
1216 
1217 #ifdef DEBUG_OPEN
1218 	printk("sab8253x_open %s%d, count = %d\n", tty->driver.name, port->line,
1219 	       port->count);
1220 #endif
1221 
1222 	/*
1223 	 * If the port is in the middle of closing, bail out now.
1224 	 */
1225 	if (tty_hung_up_p(filp) ||
1226 	    (port->flags & FLAG8253X_CLOSING))
1227 	{
1228 
1229 		if (port->flags & FLAG8253X_CLOSING)
1230 		{
1231 			interruptible_sleep_on(&port->close_wait);
1232 		}
1233 #ifdef SERIAL_DO_RESTART
1234 		++(port->count);
1235 		return ((port->flags & FLAG8253X_HUP_NOTIFY) ?
1236 			-EAGAIN : -ERESTARTSYS);
1237 #else
1238 		++(port->count);
1239 		return -EAGAIN;
1240 #endif
1241 	}
1242 
1243 	if(port->flags & FLAG8253X_NORMAL_ACTIVE)
1244 	{
1245 		if(port->open_type == OPEN_ASYNC)
1246 		{
1247 			++(port->count);
1248 			return -EBUSY;	/* can't reopen in sync mode */
1249 		}
1250 	}
1251 	if(port->open_type > OPEN_SYNC) /* can reopen a SYNC_TTY */
1252 	{
1253 		return -EBUSY;
1254 	}
1255 	if(Sab8253xSetUpLists(port))
1256 	{
1257 		++(port->count);
1258 		return -ENODEV;
1259 	}
1260 	if(Sab8253xInitDescriptors2(port, sab8253xs_listsize, sab8253xs_rbufsize))
1261 	{
1262 		++(port->count);
1263 		return -ENODEV;
1264 	}
1265 
1266 	retval = sab8253x_startupS(port);
1267 	if (retval)
1268 	{
1269 		++(port->count);
1270 		return retval;		/* does not check channel mode */
1271 	}
1272 
1273 	retval = sab8253x_block_til_ready(tty, filp, port); /* checks channel mode */
1274 	++(port->count);
1275 	if (retval)
1276 	{
1277 		return retval;
1278 	}
1279 
1280 	port->tty = tty;		/* may change here once through the block */
1281 	/* because now the port belongs to an new tty */
1282 	tty->flip.tqueue.routine = sab8253x_flush_to_ldiscS;
1283 	if(Sab8253xSetUpLists(port))
1284 	{
1285 		return -ENODEV;
1286 	}
1287 	if(Sab8253xInitDescriptors2(port, sab8253xs_listsize, sab8253xs_rbufsize))
1288 	{
1289 		Sab8253xCleanUpTransceiveN(port);	/* the network functions should be okay -- only difference */
1290 		/* is the crc32 that is appended */
1291 		return -ENODEV;
1292 	}
1293 
1294 	/*
1295 	 * Start up serial port
1296 	 */
1297 	retval = sab8253x_startupS(port); /* in case cu was running the first time
1298 					   * the function was called*/
1299 	if (retval)
1300 	{
1301 		return retval;		/* does not check channel mode */
1302 	}
1303 
1304 	if ((port->count == 1) &&
1305 	    (port->flags & FLAG8253X_SPLIT_TERMIOS))
1306 	{
1307 		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1308 		{
1309 			*tty->termios = port->normal_termios;
1310 		}
1311 		else
1312 		{
1313 			*tty->termios = port->callout_termios;
1314 		}
1315 		sab8253x_change_speedS(port);
1316 	}
1317 
1318 
1319 #ifdef XCONFIG_SERIAL_CONSOLE
1320 	if (sab8253x_console.cflag && sab8253x_console.index == line)
1321 	{
1322 		tty->termios->c_cflag = sab8253x_console.cflag;
1323 		sab8253x_console.cflag = 0;
1324 		change_speed(port);
1325 	}
1326 #endif
1327 
1328 	port->session = current->session;
1329 	port->pgrp = current->pgrp;
1330 	port->open_type = OPEN_SYNC;
1331 	return 0;
1332 }
1333 
1334 
1335 
1336 
1337