1/*
2 *  generic_serial.c
3 *
4 *  Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl
5 *
6 *  written for the SX serial driver.
7 *     Contains the code that should be shared over all the serial drivers.
8 *
9 *  Credit for the idea to do it this way might go to Alan Cox.
10 *
11 *
12 *  Version 0.1 -- December, 1998. Initial version.
13 *  Version 0.2 -- March, 1999.    Some more routines. Bugfixes. Etc.
14 *  Version 0.5 -- August, 1999.   Some more fixes. Reformat for Linus.
15 *
16 *  BitWizard is actively maintaining this file. We sometimes find
17 *  that someone submitted changes to this file. We really appreciate
18 *  your help, but please submit changes through us. We're doing our
19 *  best to be responsive.  -- REW
20 * */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/tty.h>
25#include <linux/serial.h>
26#include <linux/mm.h>
27#include <linux/generic_serial.h>
28#include <asm/semaphore.h>
29#include <asm/uaccess.h>
30
31#define DEBUG
32
33static char *                  tmp_buf;
34static DECLARE_MUTEX(tmp_buf_sem);
35
36static int gs_debug;
37
38#ifdef DEBUG
39#define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
40#else
41#define gs_dprintk(f, str...) /* nothing */
42#endif
43
44#define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
45#define func_exit()  gs_dprintk (GS_DEBUG_FLOW, "gs: exit  %s\n", __FUNCTION__)
46
47#if NEW_WRITE_LOCKING
48#define DECL      /* Nothing */
49#define LOCKIT    down (& port->port_write_sem);
50#define RELEASEIT up (&port->port_write_sem);
51#else
52#define DECL      unsigned long flags;
53#define LOCKIT    save_flags (flags);cli ()
54#define RELEASEIT restore_flags (flags)
55#endif
56
57#define RS_EVENT_WRITE_WAKEUP	1
58
59MODULE_PARM(gs_debug, "i");
60
61
62void gs_put_char(struct tty_struct * tty, unsigned char ch)
63{
64	struct gs_port *port;
65	DECL
66
67	func_enter ();
68
69	if (!tty) return;
70
71	port = tty->driver_data;
72
73	if (!port) return;
74
75	if (! (port->flags & ASYNC_INITIALIZED)) return;
76
77	/* Take a lock on the serial tranmit buffer! */
78	LOCKIT;
79
80	if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
81		/* Sorry, buffer is full, drop character. Update statistics???? -- REW */
82		RELEASEIT;
83		return;
84	}
85
86	port->xmit_buf[port->xmit_head++] = ch;
87	port->xmit_head &= SERIAL_XMIT_SIZE - 1;
88	port->xmit_cnt++;  /* Characters in buffer */
89
90	RELEASEIT;
91	func_exit ();
92}
93
94
95#ifdef NEW_WRITE_LOCKING
96
97/*
98> Problems to take into account are:
99>       -1- Interrupts that empty part of the buffer.
100>       -2- page faults on the access to userspace.
101>       -3- Other processes that are also trying to do a "write".
102*/
103
104int gs_write(struct tty_struct * tty, int from_user,
105                    const unsigned char *buf, int count)
106{
107	struct gs_port *port;
108	int c, total = 0;
109	int t;
110
111	func_enter ();
112
113	if (!tty) return 0;
114
115	port = tty->driver;
116
117	if (!port) return 0;
118
119	if (! (port->flags & ASYNC_INITIALIZED))
120		return 0;
121
122	/* get exclusive "write" access to this port (problem 3) */
123	/* This is not a spinlock because we can have a disk access (page
124		 fault) in copy_from_user */
125	down (& port->port_write_sem);
126
127	while (1) {
128
129		c = count;
130
131		/* This is safe because we "OWN" the "head". Noone else can
132		   change the "head": we own the port_write_sem. */
133		/* Don't overrun the end of the buffer */
134		t = SERIAL_XMIT_SIZE - port->xmit_head;
135		if (t < c) c = t;
136
137		/* This is safe because the xmit_cnt can only decrease. This
138		   would increase "t", so we might copy too little chars. */
139		/* Don't copy past the "head" of the buffer */
140		t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
141		if (t < c) c = t;
142
143		/* Can't copy more? break out! */
144		if (c <= 0) break;
145		if (from_user)
146                       if (copy_from_user (port->xmit_buf + port->xmit_head,
147                                           buf, c)) {
148                               up (& port->port_write_sem);
149                               return -EFAULT;
150                       }
151
152		else
153			memcpy         (port->xmit_buf + port->xmit_head, buf, c);
154
155		port -> xmit_cnt += c;
156		port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
157		buf += c;
158		count -= c;
159		total += c;
160	}
161	up (& port->port_write_sem);
162
163	gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
164	            (port->flags & GS_TX_INTEN)?"enabled": "disabled");
165
166	if (port->xmit_cnt &&
167	    !tty->stopped &&
168	    !tty->hw_stopped &&
169	    !(port->flags & GS_TX_INTEN)) {
170		port->flags |= GS_TX_INTEN;
171		port->rd->enable_tx_interrupts (port);
172	}
173	func_exit ();
174	return total;
175}
176#else
177/*
178> Problems to take into account are:
179>       -1- Interrupts that empty part of the buffer.
180>       -2- page faults on the access to userspace.
181>       -3- Other processes that are also trying to do a "write".
182*/
183
184int gs_write(struct tty_struct * tty, int from_user,
185                    const unsigned char *buf, int count)
186{
187	struct gs_port *port;
188	int c, total = 0;
189	int t;
190	unsigned long flags;
191
192	func_enter ();
193
194	/* The standard serial driver returns 0 in this case.
195	   That sounds to me as "No error, I just didn't get to writing any
196	   bytes. Feel free to try again."
197	   The "official" way to write n bytes from buf is:
198
199		 for (nwritten = 0;nwritten < n;nwritten += rv) {
200			 rv = write (fd, buf+nwritten, n-nwritten);
201			 if (rv < 0) break; // Error: bail out. //
202		 }
203
204	   which will loop endlessly in this case. The manual page for write
205	   agrees with me. In practise almost everybody writes
206	   "write (fd, buf,n);" but some people might have had to deal with
207	   incomplete writes in the past and correctly implemented it by now...
208	 */
209
210	if (!tty) return -EIO;
211
212	port = tty->driver_data;
213	if (!port || !port->xmit_buf || !tmp_buf)
214		return -EIO;
215
216	save_flags(flags);
217	if (from_user) {
218		down(&tmp_buf_sem);
219		while (1) {
220			c = count;
221
222			/* Note: This part can be done without
223			 * interrupt routine protection since
224			 * the interrupt routines may only modify
225			 * shared variables in safe ways, in the worst
226			 * case causing us to loop twice in the code
227			 * below. See comments below. */
228
229			/* Don't overrun the end of the buffer */
230			t = SERIAL_XMIT_SIZE - port->xmit_head;
231			if (t < c) c = t;
232
233			/* This is safe because the xmit_cnt can only decrease. This
234			   would increase "t", so we might copy too little chars. */
235			/* Don't copy past the "head" of the buffer */
236			t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
237			if (t < c) c = t;
238
239			/* Can't copy more? break out! */
240			if (c <= 0) break;
241
242			c -= copy_from_user(tmp_buf, buf, c);
243			if (!c) {
244				if (!total)
245					total = -EFAULT;
246				break;
247			}
248			cli();
249			t = SERIAL_XMIT_SIZE - port->xmit_head;
250			if (t < c) c = t;
251			t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
252			if (t < c) c = t;
253
254			memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
255			port->xmit_head = ((port->xmit_head + c) &
256			                   (SERIAL_XMIT_SIZE-1));
257			port->xmit_cnt += c;
258			restore_flags(flags);
259			buf += c;
260			count -= c;
261			total += c;
262		}
263		up(&tmp_buf_sem);
264	} else {
265		while (1) {
266			cli();
267			c = count;
268
269			/* This is safe because we "OWN" the "head". Noone else can
270			   change the "head": we own the port_write_sem. */
271			/* Don't overrun the end of the buffer */
272			t = SERIAL_XMIT_SIZE - port->xmit_head;
273			if (t < c) c = t;
274
275			/* This is safe because the xmit_cnt can only decrease. This
276			   would increase "t", so we might copy too little chars. */
277			/* Don't copy past the "head" of the buffer */
278			t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
279			if (t < c) c = t;
280
281			/* Can't copy more? break out! */
282			if (c <= 0) {
283				restore_flags(flags);
284				break;
285			}
286			memcpy(port->xmit_buf + port->xmit_head, buf, c);
287			port->xmit_head = ((port->xmit_head + c) &
288			                   (SERIAL_XMIT_SIZE-1));
289			port->xmit_cnt += c;
290			restore_flags(flags);
291			buf += c;
292			count -= c;
293			total += c;
294		}
295	}
296
297	if (port->xmit_cnt &&
298	    !tty->stopped &&
299	    !tty->hw_stopped &&
300	    !(port->flags & GS_TX_INTEN)) {
301		port->flags |= GS_TX_INTEN;
302		port->rd->enable_tx_interrupts (port);
303	}
304	func_exit ();
305	return total;
306}
307
308#endif
309
310
311
312int gs_write_room(struct tty_struct * tty)
313{
314	struct gs_port *port = tty->driver_data;
315	int ret;
316
317	func_enter ();
318	ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
319	if (ret < 0)
320		ret = 0;
321	func_exit ();
322	return ret;
323}
324
325
326int gs_chars_in_buffer(struct tty_struct *tty)
327{
328	struct gs_port *port = tty->driver_data;
329	func_enter ();
330
331	func_exit ();
332	return port->xmit_cnt;
333}
334
335
336int gs_real_chars_in_buffer(struct tty_struct *tty)
337{
338	struct gs_port *port;
339	func_enter ();
340
341	if (!tty) return 0;
342	port = tty->driver_data;
343
344	if (!port->rd) return 0;
345	if (!port->rd->chars_in_buffer) return 0;
346
347	func_exit ();
348	return port->xmit_cnt + port->rd->chars_in_buffer (port);
349}
350
351
352static int gs_wait_tx_flushed (void * ptr, int timeout)
353{
354	struct gs_port *port = ptr;
355	long end_jiffies;
356	int jiffies_to_transmit, charsleft = 0, rv = 0;
357	int rcib;
358
359	func_enter();
360
361	gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
362	if (port) {
363		gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
364		port->xmit_cnt, port->xmit_buf, port->tty);
365	}
366
367	if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
368		gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
369		func_exit();
370		return -EINVAL;  /* This is an error which we don't know how to handle. */
371	}
372
373	rcib = gs_real_chars_in_buffer(port->tty);
374
375	if(rcib <= 0) {
376		gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
377		func_exit();
378		return rv;
379	}
380	/* stop trying: now + twice the time it would normally take +  seconds */
381	if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
382	end_jiffies  = jiffies;
383	if (timeout !=  MAX_SCHEDULE_TIMEOUT)
384		end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
385	end_jiffies += timeout;
386
387	gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
388		    jiffies, end_jiffies, end_jiffies-jiffies);
389
390	/* the expression is actually jiffies < end_jiffies, but that won't
391	   work around the wraparound. Tricky eh? */
392	while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
393	        time_after (end_jiffies, jiffies)) {
394		/* Units check:
395		   chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
396		   check! */
397
398		charsleft += 16; /* Allow 16 chars more to be transmitted ... */
399		jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
400		/*                                ^^^ Round up.... */
401		if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
402
403		gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
404			    "(%d chars).\n", jiffies_to_transmit, charsleft);
405
406		set_current_state (TASK_INTERRUPTIBLE);
407		schedule_timeout(jiffies_to_transmit);
408		if (signal_pending (current)) {
409			gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
410			rv = -EINTR;
411			break;
412		}
413	}
414
415	gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
416	set_current_state (TASK_RUNNING);
417
418	func_exit();
419	return rv;
420}
421
422
423
424void gs_flush_buffer(struct tty_struct *tty)
425{
426	struct gs_port *port;
427	unsigned long flags;
428
429	func_enter ();
430
431	if (!tty) return;
432
433	port = tty->driver_data;
434
435	if (!port) return;
436
437	save_flags(flags); cli();
438	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
439	restore_flags(flags);
440
441	wake_up_interruptible(&tty->write_wait);
442	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
443	    tty->ldisc.write_wakeup)
444		(tty->ldisc.write_wakeup)(tty);
445	func_exit ();
446}
447
448
449void gs_flush_chars(struct tty_struct * tty)
450{
451	struct gs_port *port;
452
453	func_enter ();
454
455	if (!tty) return;
456
457	port = tty->driver_data;
458
459	if (!port) return;
460
461	if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
462	    !port->xmit_buf) {
463		func_exit ();
464		return;
465	}
466
467	/* Beats me -- REW */
468	port->flags |= GS_TX_INTEN;
469	port->rd->enable_tx_interrupts (port);
470	func_exit ();
471}
472
473
474void gs_stop(struct tty_struct * tty)
475{
476	struct gs_port *port;
477
478	func_enter ();
479
480	if (!tty) return;
481
482	port = tty->driver_data;
483
484	if (!port) return;
485
486	if (port->xmit_cnt &&
487	    port->xmit_buf &&
488	    (port->flags & GS_TX_INTEN) ) {
489		port->flags &= ~GS_TX_INTEN;
490		port->rd->disable_tx_interrupts (port);
491	}
492	func_exit ();
493}
494
495
496void gs_start(struct tty_struct * tty)
497{
498	struct gs_port *port;
499
500	if (!tty) return;
501
502	port = tty->driver_data;
503
504	if (!port) return;
505
506	if (port->xmit_cnt &&
507	    port->xmit_buf &&
508	    !(port->flags & GS_TX_INTEN) ) {
509		port->flags |= GS_TX_INTEN;
510		port->rd->enable_tx_interrupts (port);
511	}
512	func_exit ();
513}
514
515
516void gs_shutdown_port (struct gs_port *port)
517{
518	unsigned long flags;
519
520	func_enter();
521
522	if (!port) return;
523
524	if (!(port->flags & ASYNC_INITIALIZED))
525		return;
526
527	save_flags (flags);
528	cli ();
529
530	if (port->xmit_buf) {
531		free_page((unsigned long) port->xmit_buf);
532		port->xmit_buf = 0;
533	}
534
535	if (port->tty)
536		set_bit(TTY_IO_ERROR, &port->tty->flags);
537
538	port->rd->shutdown_port (port);
539
540	port->flags &= ~ASYNC_INITIALIZED;
541	restore_flags (flags);
542
543	func_exit();
544}
545
546
547void gs_hangup(struct tty_struct *tty)
548{
549	struct gs_port   *port;
550
551	func_enter ();
552
553	if (!tty) return;
554
555	port = tty->driver_data;
556	tty = port->tty;
557	if (!tty)
558		return;
559
560	gs_shutdown_port (port);
561	port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE |GS_ACTIVE);
562	port->tty = NULL;
563	port->count = 0;
564
565	wake_up_interruptible(&port->open_wait);
566	func_exit ();
567}
568
569
570void gs_do_softint(void *private_)
571{
572	struct gs_port *port = private_;
573	struct tty_struct *tty;
574
575	func_enter ();
576
577	if (!port) return;
578
579	tty = port->tty;
580
581	if (!tty) return;
582
583	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
584		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
585		    tty->ldisc.write_wakeup)
586			(tty->ldisc.write_wakeup)(tty);
587		wake_up_interruptible(&tty->write_wait);
588	}
589	func_exit ();
590}
591
592
593int gs_block_til_ready(void *port_, struct file * filp)
594{
595	struct gs_port *port = port_;
596	DECLARE_WAITQUEUE(wait, current);
597	int    retval;
598	int    do_clocal = 0;
599	int    CD;
600	struct tty_struct *tty;
601	unsigned long flags;
602
603	func_enter ();
604
605	if (!port) return 0;
606
607	tty = port->tty;
608
609	if (!tty) return 0;
610
611	gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
612	/*
613	 * If the device is in the middle of being closed, then block
614	 * until it's done, and then try again.
615	 */
616	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
617		interruptible_sleep_on(&port->close_wait);
618		if (port->flags & ASYNC_HUP_NOTIFY)
619			return -EAGAIN;
620		else
621			return -ERESTARTSYS;
622	}
623
624	gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
625
626	/*
627	 * If this is a callout device, then just make sure the normal
628	 * device isn't being used.
629	 */
630	if (tty->driver.subtype == GS_TYPE_CALLOUT) {
631		if (port->flags & ASYNC_NORMAL_ACTIVE)
632			return -EBUSY;
633		if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
634		    (port->flags & ASYNC_SESSION_LOCKOUT) &&
635		    (port->session != current->session))
636			return -EBUSY;
637		if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
638		    (port->flags & ASYNC_PGRP_LOCKOUT) &&
639		    (port->pgrp != current->pgrp))
640			return -EBUSY;
641		port->flags |= ASYNC_CALLOUT_ACTIVE;
642		return 0;
643	}
644
645	gs_dprintk (GS_DEBUG_BTR, "after subtype\n");
646
647	/*
648	 * If non-blocking mode is set, or the port is not enabled,
649	 * then make the check up front and then exit.
650	 */
651	if ((filp->f_flags & O_NONBLOCK) ||
652	    (tty->flags & (1 << TTY_IO_ERROR))) {
653		if (port->flags & ASYNC_CALLOUT_ACTIVE)
654			return -EBUSY;
655		port->flags |= ASYNC_NORMAL_ACTIVE;
656		return 0;
657	}
658
659	gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
660
661	if (port->flags & ASYNC_CALLOUT_ACTIVE) {
662		if (port->normal_termios.c_cflag & CLOCAL)
663			do_clocal = 1;
664	} else {
665		if (C_CLOCAL(tty))
666			do_clocal = 1;
667	}
668
669	/*
670	 * Block waiting for the carrier detect and the line to become
671	 * free (i.e., not in use by the callout).  While we are in
672	 * this loop, port->count is dropped by one, so that
673	 * rs_close() knows when to free things.  We restore it upon
674	 * exit, either normal or abnormal.
675	 */
676	retval = 0;
677
678	add_wait_queue(&port->open_wait, &wait);
679
680	gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
681	save_flags(flags);
682	cli();
683	if (!tty_hung_up_p(filp))
684		port->count--;
685	restore_flags(flags);
686	port->blocked_open++;
687	while (1) {
688		CD = port->rd->get_CD (port);
689		gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
690		set_current_state (TASK_INTERRUPTIBLE);
691		if (tty_hung_up_p(filp) ||
692		    !(port->flags & ASYNC_INITIALIZED)) {
693			if (port->flags & ASYNC_HUP_NOTIFY)
694				retval = -EAGAIN;
695			else
696				retval = -ERESTARTSYS;
697			break;
698		}
699		if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
700		    !(port->flags & ASYNC_CLOSING) &&
701		    (do_clocal || CD))
702			break;
703		gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
704		(int)signal_pending (current), *(long*)(&current->blocked));
705		if (signal_pending(current)) {
706			retval = -ERESTARTSYS;
707			break;
708		}
709		schedule();
710	}
711	gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
712		    port->blocked_open);
713	set_current_state (TASK_RUNNING);
714	remove_wait_queue(&port->open_wait, &wait);
715	if (!tty_hung_up_p(filp))
716		port->count++;
717	port->blocked_open--;
718	if (retval)
719		return retval;
720
721	port->flags |= ASYNC_NORMAL_ACTIVE;
722	func_exit ();
723	return 0;
724}
725
726
727void gs_close(struct tty_struct * tty, struct file * filp)
728{
729	unsigned long flags;
730	struct gs_port *port;
731
732	func_enter ();
733
734	if (!tty) return;
735
736	port = (struct gs_port *) tty->driver_data;
737
738	if (!port) return;
739
740	if (!port->tty) {
741		/* This seems to happen when this is called from vhangup. */
742		gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
743		port->tty = tty;
744	}
745
746	save_flags(flags); cli();
747
748	if (tty_hung_up_p(filp)) {
749		restore_flags(flags);
750		port->rd->hungup (port);
751		func_exit ();
752		return;
753	}
754
755	if ((tty->count == 1) && (port->count != 1)) {
756		printk(KERN_ERR "gs: gs_close: bad port count;"
757		       " tty->count is 1, port count is %d\n", port->count);
758		port->count = 1;
759	}
760	if (--port->count < 0) {
761		printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
762		port->count = 0;
763	}
764	if (port->count) {
765		gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %d\n", port->count);
766		restore_flags(flags);
767		func_exit ();
768		return;
769	}
770	port->flags |= ASYNC_CLOSING;
771
772	/*
773	 * Save the termios structure, since this port may have
774	 * separate termios for callout and dialin.
775	 */
776	if (port->flags & ASYNC_NORMAL_ACTIVE)
777		port->normal_termios = *tty->termios;
778	if (port->flags & ASYNC_CALLOUT_ACTIVE)
779		port->callout_termios = *tty->termios;
780	/*
781	 * Now we wait for the transmit buffer to clear; and we notify
782	 * the line discipline to only process XON/XOFF characters.
783	 */
784	tty->closing = 1;
785	/* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
786	   tty_wait_until_sent(tty, port->closing_wait); */
787
788	/*
789	 * At this point we stop accepting input.  To do this, we
790	 * disable the receive line status interrupts, and tell the
791	 * interrupt driver to stop checking the data ready bit in the
792	 * line status register.
793	 */
794
795	port->rd->disable_rx_interrupts (port);
796
797	/* close has no way of returning "EINTR", so discard return value */
798	if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
799		gs_wait_tx_flushed (port, port->closing_wait);
800
801	port->flags &= ~GS_ACTIVE;
802
803	if (tty->driver.flush_buffer)
804		tty->driver.flush_buffer(tty);
805	if (tty->ldisc.flush_buffer)
806		tty->ldisc.flush_buffer(tty);
807	tty->closing = 0;
808
809	port->event = 0;
810	port->rd->close (port);
811	port->rd->shutdown_port (port);
812	port->tty = 0;
813
814	if (port->blocked_open) {
815		if (port->close_delay) {
816			set_current_state (TASK_INTERRUPTIBLE);
817			schedule_timeout(port->close_delay);
818		}
819		wake_up_interruptible(&port->open_wait);
820	}
821	port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
822	                 ASYNC_CLOSING | ASYNC_INITIALIZED);
823	wake_up_interruptible(&port->close_wait);
824
825	restore_flags(flags);
826	func_exit ();
827}
828
829
830static unsigned int     gs_baudrates[] = {
831  0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
832  9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
833};
834
835
836void gs_set_termios (struct tty_struct * tty,
837                     struct termios * old_termios)
838{
839	struct gs_port *port;
840	int baudrate, tmp, rv;
841	struct termios *tiosp;
842
843	func_enter();
844
845	if (!tty) return;
846
847	port = tty->driver_data;
848
849	if (!port) return;
850
851	tiosp = tty->termios;
852
853	if (gs_debug & GS_DEBUG_TERMIOS) {
854		gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
855	}
856
857	if (old_termios) {
858		if(   (tiosp->c_iflag == old_termios->c_iflag)
859		   && (tiosp->c_oflag == old_termios->c_oflag)
860		   && (tiosp->c_cflag == old_termios->c_cflag)
861		   && (tiosp->c_lflag == old_termios->c_lflag)
862		   && (tiosp->c_line  == old_termios->c_line)
863		   && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
864			gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
865			return /* 0 */;
866		}
867	} else
868		gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
869		           "no optimization\n");
870
871	if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
872		if(tiosp->c_iflag != old_termios->c_iflag)  printk("c_iflag changed\n");
873		if(tiosp->c_oflag != old_termios->c_oflag)  printk("c_oflag changed\n");
874		if(tiosp->c_cflag != old_termios->c_cflag)  printk("c_cflag changed\n");
875		if(tiosp->c_lflag != old_termios->c_lflag)  printk("c_lflag changed\n");
876		if(tiosp->c_line  != old_termios->c_line)   printk("c_line changed\n");
877		if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
878	}
879
880	baudrate = tiosp->c_cflag & CBAUD;
881	if (baudrate & CBAUDEX) {
882		baudrate &= ~CBAUDEX;
883		if ((baudrate < 1) || (baudrate > 4))
884			tiosp->c_cflag &= ~CBAUDEX;
885		else
886			baudrate += 15;
887	}
888
889	baudrate = gs_baudrates[baudrate];
890	if ((tiosp->c_cflag & CBAUD) == B38400) {
891		if (     (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
892			baudrate = 57600;
893		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
894			baudrate = 115200;
895		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
896			baudrate = 230400;
897		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
898			baudrate = 460800;
899		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
900			baudrate = (port->baud_base / port->custom_divisor);
901	}
902
903	/* I recommend using THIS instead of the mess in termios (and
904	   duplicating the above code). Next we should create a clean
905	   interface towards this variable. If your card supports arbitrary
906	   baud rates, (e.g. CD1400 or 16550 based cards) then everything
907	   will be very easy..... */
908	port->baud = baudrate;
909
910	/* Two timer ticks seems enough to wakeup something like SLIP driver */
911	/* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
912	tmp = (baudrate / 10 / HZ) * 2;
913
914	if (tmp <                 0) tmp = 0;
915	if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
916
917	port->wakeup_chars = tmp;
918
919	/* We should really wait for the characters to be all sent before
920	   changing the settings. -- CAL */
921	rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
922	if (rv < 0) return /* rv */;
923
924	rv = port->rd->set_real_termios(port);
925	if (rv < 0) return /* rv */;
926
927	if ((!old_termios ||
928	     (old_termios->c_cflag & CRTSCTS)) &&
929	    !(      tiosp->c_cflag & CRTSCTS)) {
930		tty->stopped = 0;
931		gs_start(tty);
932	}
933
934#ifdef tytso_patch_94Nov25_1726
935	/* This "makes sense", Why is it commented out? */
936
937	if (!(old_termios->c_cflag & CLOCAL) &&
938	    (tty->termios->c_cflag & CLOCAL))
939		wake_up_interruptible(&info->open_wait);
940#endif
941
942	func_exit();
943	return /* 0 */;
944}
945
946
947
948/* Must be called with interrupts enabled */
949int gs_init_port(struct gs_port *port)
950{
951	unsigned long flags;
952	unsigned long page;
953
954	save_flags (flags);
955	if (!tmp_buf) {
956		page = get_free_page(GFP_KERNEL);
957
958		cli (); /* Don't expect this to make a difference. */
959		if (tmp_buf)
960			free_page(page);
961		else
962			tmp_buf = (unsigned char *) page;
963		restore_flags (flags);
964
965		if (!tmp_buf) {
966			return -ENOMEM;
967		}
968	}
969
970	if (port->flags & ASYNC_INITIALIZED)
971		return 0;
972
973	if (!port->xmit_buf) {
974		/* We may sleep in get_free_page() */
975		unsigned long tmp;
976
977		tmp = get_free_page(GFP_KERNEL);
978
979		/* Spinlock? */
980		cli ();
981		if (port->xmit_buf)
982			free_page (tmp);
983		else
984			port->xmit_buf = (unsigned char *) tmp;
985		restore_flags (flags);
986
987		if (!port->xmit_buf)
988			return -ENOMEM;
989	}
990
991	cli();
992
993	if (port->tty)
994		clear_bit(TTY_IO_ERROR, &port->tty->flags);
995
996	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
997
998	gs_set_termios(port->tty, NULL);
999
1000	port->flags |= ASYNC_INITIALIZED;
1001	port->flags &= ~GS_TX_INTEN;
1002
1003	restore_flags(flags);
1004	return 0;
1005}
1006
1007
1008int gs_setserial(struct gs_port *port, struct serial_struct *sp)
1009{
1010	struct serial_struct sio;
1011
1012	if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1013		return(-EFAULT);
1014
1015	if (!capable(CAP_SYS_ADMIN)) {
1016		if ((sio.baud_base != port->baud_base) ||
1017		    (sio.close_delay != port->close_delay) ||
1018		    ((sio.flags & ~ASYNC_USR_MASK) !=
1019		     (port->flags & ~ASYNC_USR_MASK)))
1020			return(-EPERM);
1021	}
1022
1023	port->flags = (port->flags & ~ASYNC_USR_MASK) |
1024		(sio.flags & ASYNC_USR_MASK);
1025
1026	port->baud_base = sio.baud_base;
1027	port->close_delay = sio.close_delay;
1028	port->closing_wait = sio.closing_wait;
1029	port->custom_divisor = sio.custom_divisor;
1030
1031	gs_set_termios (port->tty, NULL);
1032
1033	return 0;
1034}
1035
1036
1037/*****************************************************************************/
1038
1039/*
1040 *      Generate the serial struct info.
1041 */
1042
1043int gs_getserial(struct gs_port *port, struct serial_struct *sp)
1044{
1045	struct serial_struct    sio;
1046
1047	memset(&sio, 0, sizeof(struct serial_struct));
1048	sio.flags = port->flags;
1049	sio.baud_base = port->baud_base;
1050	sio.close_delay = port->close_delay;
1051	sio.closing_wait = port->closing_wait;
1052	sio.custom_divisor = port->custom_divisor;
1053	sio.hub6 = 0;
1054
1055	/* If you want you can override these. */
1056	sio.type = PORT_UNKNOWN;
1057	sio.xmit_fifo_size = -1;
1058	sio.line = -1;
1059	sio.port = -1;
1060	sio.irq = -1;
1061
1062	if (port->rd->getserial)
1063		port->rd->getserial (port, &sio);
1064
1065	if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
1066		return -EFAULT;
1067	return 0;
1068
1069}
1070
1071
1072void gs_got_break(struct gs_port *port)
1073{
1074	if (port->flags & ASYNC_SAK) {
1075		do_SAK (port->tty);
1076	}
1077	*(port->tty->flip.flag_buf_ptr) = TTY_BREAK;
1078	port->tty->flip.flag_buf_ptr++;
1079	port->tty->flip.char_buf_ptr++;
1080	port->tty->flip.count++;
1081}
1082
1083
1084EXPORT_SYMBOL(gs_put_char);
1085EXPORT_SYMBOL(gs_write);
1086EXPORT_SYMBOL(gs_write_room);
1087EXPORT_SYMBOL(gs_chars_in_buffer);
1088EXPORT_SYMBOL(gs_flush_buffer);
1089EXPORT_SYMBOL(gs_flush_chars);
1090EXPORT_SYMBOL(gs_stop);
1091EXPORT_SYMBOL(gs_start);
1092EXPORT_SYMBOL(gs_hangup);
1093EXPORT_SYMBOL(gs_do_softint);
1094EXPORT_SYMBOL(gs_block_til_ready);
1095EXPORT_SYMBOL(gs_close);
1096EXPORT_SYMBOL(gs_set_termios);
1097EXPORT_SYMBOL(gs_init_port);
1098EXPORT_SYMBOL(gs_setserial);
1099EXPORT_SYMBOL(gs_getserial);
1100EXPORT_SYMBOL(gs_got_break);
1101
1102MODULE_LICENSE("GPL");
1103