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 <linux/interrupt.h>
29#include <linux/tty_flip.h>
30#include <linux/delay.h>
31#include <asm/semaphore.h>
32#include <asm/uaccess.h>
33
34#define DEBUG
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#define NEW_WRITE_LOCKING 1
47#if NEW_WRITE_LOCKING
48#define DECL      /* Nothing */
49#define LOCKIT    mutex_lock(& port->port_write_mutex);
50#define RELEASEIT mutex_unlock(&port->port_write_mutex);
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_param(gs_debug, int, 0644);
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,
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_data;
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	mutex_lock(& port->port_write_mutex);
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_mutex. */
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
146		memcpy (port->xmit_buf + port->xmit_head, buf, c);
147
148		port -> xmit_cnt += c;
149		port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
150		buf += c;
151		count -= c;
152		total += c;
153	}
154	mutex_unlock(& port->port_write_mutex);
155
156	gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
157	            (port->flags & GS_TX_INTEN)?"enabled": "disabled");
158
159	if (port->xmit_cnt &&
160	    !tty->stopped &&
161	    !tty->hw_stopped &&
162	    !(port->flags & GS_TX_INTEN)) {
163		port->flags |= GS_TX_INTEN;
164		port->rd->enable_tx_interrupts (port);
165	}
166	func_exit ();
167	return total;
168}
169#else
170/*
171> Problems to take into account are:
172>       -1- Interrupts that empty part of the buffer.
173>       -2- page faults on the access to userspace.
174>       -3- Other processes that are also trying to do a "write".
175*/
176
177int gs_write(struct tty_struct * tty,
178                    const unsigned char *buf, int count)
179{
180	struct gs_port *port;
181	int c, total = 0;
182	int t;
183	unsigned long flags;
184
185	func_enter ();
186
187	/* The standard serial driver returns 0 in this case.
188	   That sounds to me as "No error, I just didn't get to writing any
189	   bytes. Feel free to try again."
190	   The "official" way to write n bytes from buf is:
191
192		 for (nwritten = 0;nwritten < n;nwritten += rv) {
193			 rv = write (fd, buf+nwritten, n-nwritten);
194			 if (rv < 0) break; // Error: bail out. //
195		 }
196
197	   which will loop endlessly in this case. The manual page for write
198	   agrees with me. In practise almost everybody writes
199	   "write (fd, buf,n);" but some people might have had to deal with
200	   incomplete writes in the past and correctly implemented it by now...
201	 */
202
203	if (!tty) return -EIO;
204
205	port = tty->driver_data;
206	if (!port || !port->xmit_buf)
207		return -EIO;
208
209	local_save_flags(flags);
210	while (1) {
211		cli();
212		c = count;
213
214		/* This is safe because we "OWN" the "head". Noone else can
215		   change the "head": we own the port_write_mutex. */
216		/* Don't overrun the end of the buffer */
217		t = SERIAL_XMIT_SIZE - port->xmit_head;
218		if (t < c) c = t;
219
220		/* This is safe because the xmit_cnt can only decrease. This
221		   would increase "t", so we might copy too little chars. */
222		/* Don't copy past the "head" of the buffer */
223		t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
224		if (t < c) c = t;
225
226		/* Can't copy more? break out! */
227		if (c <= 0) {
228			local_restore_flags(flags);
229			break;
230		}
231		memcpy(port->xmit_buf + port->xmit_head, buf, c);
232		port->xmit_head = ((port->xmit_head + c) &
233		                   (SERIAL_XMIT_SIZE-1));
234		port->xmit_cnt += c;
235		local_restore_flags(flags);
236		buf += c;
237		count -= c;
238		total += c;
239	}
240
241	if (port->xmit_cnt &&
242	    !tty->stopped &&
243	    !tty->hw_stopped &&
244	    !(port->flags & GS_TX_INTEN)) {
245		port->flags |= GS_TX_INTEN;
246		port->rd->enable_tx_interrupts (port);
247	}
248	func_exit ();
249	return total;
250}
251
252#endif
253
254
255
256int gs_write_room(struct tty_struct * tty)
257{
258	struct gs_port *port = tty->driver_data;
259	int ret;
260
261	func_enter ();
262	ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
263	if (ret < 0)
264		ret = 0;
265	func_exit ();
266	return ret;
267}
268
269
270int gs_chars_in_buffer(struct tty_struct *tty)
271{
272	struct gs_port *port = tty->driver_data;
273	func_enter ();
274
275	func_exit ();
276	return port->xmit_cnt;
277}
278
279
280static int gs_real_chars_in_buffer(struct tty_struct *tty)
281{
282	struct gs_port *port;
283	func_enter ();
284
285	if (!tty) return 0;
286	port = tty->driver_data;
287
288	if (!port->rd) return 0;
289	if (!port->rd->chars_in_buffer) return 0;
290
291	func_exit ();
292	return port->xmit_cnt + port->rd->chars_in_buffer (port);
293}
294
295
296static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
297{
298	struct gs_port *port = ptr;
299	unsigned long end_jiffies;
300	int jiffies_to_transmit, charsleft = 0, rv = 0;
301	int rcib;
302
303	func_enter();
304
305	gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
306	if (port) {
307		gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
308		port->xmit_cnt, port->xmit_buf, port->tty);
309	}
310
311	if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
312		gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
313		func_exit();
314		return -EINVAL;  /* This is an error which we don't know how to handle. */
315	}
316
317	rcib = gs_real_chars_in_buffer(port->tty);
318
319	if(rcib <= 0) {
320		gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
321		func_exit();
322		return rv;
323	}
324	/* stop trying: now + twice the time it would normally take +  seconds */
325	if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
326	end_jiffies  = jiffies;
327	if (timeout !=  MAX_SCHEDULE_TIMEOUT)
328		end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
329	end_jiffies += timeout;
330
331	gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
332		    jiffies, end_jiffies, end_jiffies-jiffies);
333
334	while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
335	        time_after (end_jiffies, jiffies)) {
336		/* Units check:
337		   chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
338		   check! */
339
340		charsleft += 16; /* Allow 16 chars more to be transmitted ... */
341		jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
342		/*                                ^^^ Round up.... */
343		if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
344
345		gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
346			    "(%d chars).\n", jiffies_to_transmit, charsleft);
347
348		msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit));
349		if (signal_pending (current)) {
350			gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
351			rv = -EINTR;
352			break;
353		}
354	}
355
356	gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
357	set_current_state (TASK_RUNNING);
358
359	func_exit();
360	return rv;
361}
362
363
364
365void gs_flush_buffer(struct tty_struct *tty)
366{
367	struct gs_port *port;
368	unsigned long flags;
369
370	func_enter ();
371
372	if (!tty) return;
373
374	port = tty->driver_data;
375
376	if (!port) return;
377
378	spin_lock_irqsave (&port->driver_lock, flags);
379	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
380	spin_unlock_irqrestore (&port->driver_lock, flags);
381
382	tty_wakeup(tty);
383	func_exit ();
384}
385
386
387void gs_flush_chars(struct tty_struct * tty)
388{
389	struct gs_port *port;
390
391	func_enter ();
392
393	if (!tty) return;
394
395	port = tty->driver_data;
396
397	if (!port) return;
398
399	if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
400	    !port->xmit_buf) {
401		func_exit ();
402		return;
403	}
404
405	/* Beats me -- REW */
406	port->flags |= GS_TX_INTEN;
407	port->rd->enable_tx_interrupts (port);
408	func_exit ();
409}
410
411
412void gs_stop(struct tty_struct * tty)
413{
414	struct gs_port *port;
415
416	func_enter ();
417
418	if (!tty) return;
419
420	port = tty->driver_data;
421
422	if (!port) return;
423
424	if (port->xmit_cnt &&
425	    port->xmit_buf &&
426	    (port->flags & GS_TX_INTEN) ) {
427		port->flags &= ~GS_TX_INTEN;
428		port->rd->disable_tx_interrupts (port);
429	}
430	func_exit ();
431}
432
433
434void gs_start(struct tty_struct * tty)
435{
436	struct gs_port *port;
437
438	if (!tty) return;
439
440	port = tty->driver_data;
441
442	if (!port) return;
443
444	if (port->xmit_cnt &&
445	    port->xmit_buf &&
446	    !(port->flags & GS_TX_INTEN) ) {
447		port->flags |= GS_TX_INTEN;
448		port->rd->enable_tx_interrupts (port);
449	}
450	func_exit ();
451}
452
453
454static void gs_shutdown_port (struct gs_port *port)
455{
456	unsigned long flags;
457
458	func_enter();
459
460	if (!port) return;
461
462	if (!(port->flags & ASYNC_INITIALIZED))
463		return;
464
465	spin_lock_irqsave(&port->driver_lock, flags);
466
467	if (port->xmit_buf) {
468		free_page((unsigned long) port->xmit_buf);
469		port->xmit_buf = NULL;
470	}
471
472	if (port->tty)
473		set_bit(TTY_IO_ERROR, &port->tty->flags);
474
475	port->rd->shutdown_port (port);
476
477	port->flags &= ~ASYNC_INITIALIZED;
478	spin_unlock_irqrestore(&port->driver_lock, flags);
479
480	func_exit();
481}
482
483
484void gs_hangup(struct tty_struct *tty)
485{
486	struct gs_port   *port;
487
488	func_enter ();
489
490	if (!tty) return;
491
492	port = tty->driver_data;
493	tty = port->tty;
494	if (!tty)
495		return;
496
497	gs_shutdown_port (port);
498	port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
499	port->tty = NULL;
500	port->count = 0;
501
502	wake_up_interruptible(&port->open_wait);
503	func_exit ();
504}
505
506
507int gs_block_til_ready(void *port_, struct file * filp)
508{
509	struct gs_port *port = port_;
510	DECLARE_WAITQUEUE(wait, current);
511	int    retval;
512	int    do_clocal = 0;
513	int    CD;
514	struct tty_struct *tty;
515	unsigned long flags;
516
517	func_enter ();
518
519	if (!port) return 0;
520
521	tty = port->tty;
522
523	if (!tty) return 0;
524
525	gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
526	/*
527	 * If the device is in the middle of being closed, then block
528	 * until it's done, and then try again.
529	 */
530	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
531		interruptible_sleep_on(&port->close_wait);
532		if (port->flags & ASYNC_HUP_NOTIFY)
533			return -EAGAIN;
534		else
535			return -ERESTARTSYS;
536	}
537
538	gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
539
540	/*
541	 * If non-blocking mode is set, or the port is not enabled,
542	 * then make the check up front and then exit.
543	 */
544	if ((filp->f_flags & O_NONBLOCK) ||
545	    (tty->flags & (1 << TTY_IO_ERROR))) {
546		port->flags |= ASYNC_NORMAL_ACTIVE;
547		return 0;
548	}
549
550	gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
551
552	if (C_CLOCAL(tty))
553		do_clocal = 1;
554
555	/*
556	 * Block waiting for the carrier detect and the line to become
557	 * free (i.e., not in use by the callout).  While we are in
558	 * this loop, port->count is dropped by one, so that
559	 * rs_close() knows when to free things.  We restore it upon
560	 * exit, either normal or abnormal.
561	 */
562	retval = 0;
563
564	add_wait_queue(&port->open_wait, &wait);
565
566	gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
567	spin_lock_irqsave(&port->driver_lock, flags);
568	if (!tty_hung_up_p(filp)) {
569		port->count--;
570	}
571	spin_unlock_irqrestore(&port->driver_lock, flags);
572	port->blocked_open++;
573	while (1) {
574		CD = port->rd->get_CD (port);
575		gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
576		set_current_state (TASK_INTERRUPTIBLE);
577		if (tty_hung_up_p(filp) ||
578		    !(port->flags & ASYNC_INITIALIZED)) {
579			if (port->flags & ASYNC_HUP_NOTIFY)
580				retval = -EAGAIN;
581			else
582				retval = -ERESTARTSYS;
583			break;
584		}
585		if (!(port->flags & ASYNC_CLOSING) &&
586		    (do_clocal || CD))
587			break;
588		gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
589		(int)signal_pending (current), *(long*)(&current->blocked));
590		if (signal_pending(current)) {
591			retval = -ERESTARTSYS;
592			break;
593		}
594		schedule();
595	}
596	gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
597		    port->blocked_open);
598	set_current_state (TASK_RUNNING);
599	remove_wait_queue(&port->open_wait, &wait);
600	if (!tty_hung_up_p(filp)) {
601		port->count++;
602	}
603	port->blocked_open--;
604	if (retval)
605		return retval;
606
607	port->flags |= ASYNC_NORMAL_ACTIVE;
608	func_exit ();
609	return 0;
610}
611
612
613void gs_close(struct tty_struct * tty, struct file * filp)
614{
615	unsigned long flags;
616	struct gs_port *port;
617
618	func_enter ();
619
620	if (!tty) return;
621
622	port = (struct gs_port *) tty->driver_data;
623
624	if (!port) return;
625
626	if (!port->tty) {
627		/* This seems to happen when this is called from vhangup. */
628		gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
629		port->tty = tty;
630	}
631
632	spin_lock_irqsave(&port->driver_lock, flags);
633
634	if (tty_hung_up_p(filp)) {
635		spin_unlock_irqrestore(&port->driver_lock, flags);
636		if (port->rd->hungup)
637			port->rd->hungup (port);
638		func_exit ();
639		return;
640	}
641
642	if ((tty->count == 1) && (port->count != 1)) {
643		printk(KERN_ERR "gs: gs_close port %p: bad port count;"
644		       " tty->count is 1, port count is %d\n", port, port->count);
645		port->count = 1;
646	}
647	if (--port->count < 0) {
648		printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
649		port->count = 0;
650	}
651
652	if (port->count) {
653		gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
654		spin_unlock_irqrestore(&port->driver_lock, flags);
655		func_exit ();
656		return;
657	}
658	port->flags |= ASYNC_CLOSING;
659
660	/*
661	 * Now we wait for the transmit buffer to clear; and we notify
662	 * the line discipline to only process XON/XOFF characters.
663	 */
664	tty->closing = 1;
665	/* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
666	   tty_wait_until_sent(tty, port->closing_wait); */
667
668	/*
669	 * At this point we stop accepting input.  To do this, we
670	 * disable the receive line status interrupts, and tell the
671	 * interrupt driver to stop checking the data ready bit in the
672	 * line status register.
673	 */
674
675	port->rd->disable_rx_interrupts (port);
676	spin_unlock_irqrestore(&port->driver_lock, flags);
677
678	/* close has no way of returning "EINTR", so discard return value */
679	if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
680		gs_wait_tx_flushed (port, port->closing_wait);
681
682	port->flags &= ~GS_ACTIVE;
683
684	if (tty->driver->flush_buffer)
685		tty->driver->flush_buffer(tty);
686
687	tty_ldisc_flush(tty);
688	tty->closing = 0;
689
690	port->event = 0;
691	port->rd->close (port);
692	port->rd->shutdown_port (port);
693	port->tty = NULL;
694
695	if (port->blocked_open) {
696		if (port->close_delay) {
697			spin_unlock_irqrestore(&port->driver_lock, flags);
698			msleep_interruptible(jiffies_to_msecs(port->close_delay));
699			spin_lock_irqsave(&port->driver_lock, flags);
700		}
701		wake_up_interruptible(&port->open_wait);
702	}
703	port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
704	wake_up_interruptible(&port->close_wait);
705
706	func_exit ();
707}
708
709
710void gs_set_termios (struct tty_struct * tty,
711                     struct ktermios * old_termios)
712{
713	struct gs_port *port;
714	int baudrate, tmp, rv;
715	struct ktermios *tiosp;
716
717	func_enter();
718
719	if (!tty) return;
720
721	port = tty->driver_data;
722
723	if (!port) return;
724	if (!port->tty) {
725		/* This seems to happen when this is called after gs_close. */
726		gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
727		port->tty = tty;
728	}
729
730
731	tiosp = tty->termios;
732
733	if (gs_debug & GS_DEBUG_TERMIOS) {
734		gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
735	}
736
737	/* This is an optimization that is only allowed for dumb cards */
738	/* Smart cards require knowledge of iflags and oflags too: that
739	   might change hardware cooking mode.... */
740	if (old_termios) {
741		if(   (tiosp->c_iflag == old_termios->c_iflag)
742		   && (tiosp->c_oflag == old_termios->c_oflag)
743		   && (tiosp->c_cflag == old_termios->c_cflag)
744		   && (tiosp->c_lflag == old_termios->c_lflag)
745		   && (tiosp->c_line  == old_termios->c_line)
746		   && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
747			gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
748			return /* 0 */;
749		}
750	} else
751		gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
752		           "no optimization\n");
753
754	if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
755		if(tiosp->c_iflag != old_termios->c_iflag)  printk("c_iflag changed\n");
756		if(tiosp->c_oflag != old_termios->c_oflag)  printk("c_oflag changed\n");
757		if(tiosp->c_cflag != old_termios->c_cflag)  printk("c_cflag changed\n");
758		if(tiosp->c_lflag != old_termios->c_lflag)  printk("c_lflag changed\n");
759		if(tiosp->c_line  != old_termios->c_line)   printk("c_line changed\n");
760		if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
761	}
762
763	baudrate = tty_get_baud_rate(tty);
764
765	if ((tiosp->c_cflag & CBAUD) == B38400) {
766		if (     (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
767			baudrate = 57600;
768		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
769			baudrate = 115200;
770		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
771			baudrate = 230400;
772		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
773			baudrate = 460800;
774		else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
775			baudrate = (port->baud_base / port->custom_divisor);
776	}
777
778	/* I recommend using THIS instead of the mess in termios (and
779	   duplicating the above code). Next we should create a clean
780	   interface towards this variable. If your card supports arbitrary
781	   baud rates, (e.g. CD1400 or 16550 based cards) then everything
782	   will be very easy..... */
783	port->baud = baudrate;
784
785	/* Two timer ticks seems enough to wakeup something like SLIP driver */
786	/* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
787	tmp = (baudrate / 10 / HZ) * 2;
788
789	if (tmp <                 0) tmp = 0;
790	if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
791
792	port->wakeup_chars = tmp;
793
794	/* We should really wait for the characters to be all sent before
795	   changing the settings. -- CAL */
796	rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
797	if (rv < 0) return /* rv */;
798
799	rv = port->rd->set_real_termios(port);
800	if (rv < 0) return /* rv */;
801
802	if ((!old_termios ||
803	     (old_termios->c_cflag & CRTSCTS)) &&
804	    !(      tiosp->c_cflag & CRTSCTS)) {
805		tty->stopped = 0;
806		gs_start(tty);
807	}
808
809#ifdef tytso_patch_94Nov25_1726
810	/* This "makes sense", Why is it commented out? */
811
812	if (!(old_termios->c_cflag & CLOCAL) &&
813	    (tty->termios->c_cflag & CLOCAL))
814		wake_up_interruptible(&port->gs.open_wait);
815#endif
816
817	func_exit();
818	return /* 0 */;
819}
820
821
822
823/* Must be called with interrupts enabled */
824int gs_init_port(struct gs_port *port)
825{
826	unsigned long flags;
827
828	func_enter ();
829
830	if (port->flags & ASYNC_INITIALIZED) {
831		func_exit ();
832		return 0;
833	}
834	if (!port->xmit_buf) {
835		/* We may sleep in get_zeroed_page() */
836		unsigned long tmp;
837
838		tmp = get_zeroed_page(GFP_KERNEL);
839		spin_lock_irqsave (&port->driver_lock, flags);
840		if (port->xmit_buf)
841			free_page (tmp);
842		else
843			port->xmit_buf = (unsigned char *) tmp;
844		spin_unlock_irqrestore(&port->driver_lock, flags);
845		if (!port->xmit_buf) {
846			func_exit ();
847			return -ENOMEM;
848		}
849	}
850
851	spin_lock_irqsave (&port->driver_lock, flags);
852	if (port->tty)
853		clear_bit(TTY_IO_ERROR, &port->tty->flags);
854	mutex_init(&port->port_write_mutex);
855	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
856	spin_unlock_irqrestore(&port->driver_lock, flags);
857	gs_set_termios(port->tty, NULL);
858	spin_lock_irqsave (&port->driver_lock, flags);
859	port->flags |= ASYNC_INITIALIZED;
860	port->flags &= ~GS_TX_INTEN;
861
862	spin_unlock_irqrestore(&port->driver_lock, flags);
863	func_exit ();
864	return 0;
865}
866
867
868int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
869{
870	struct serial_struct sio;
871
872	if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
873		return(-EFAULT);
874
875	if (!capable(CAP_SYS_ADMIN)) {
876		if ((sio.baud_base != port->baud_base) ||
877		    (sio.close_delay != port->close_delay) ||
878		    ((sio.flags & ~ASYNC_USR_MASK) !=
879		     (port->flags & ~ASYNC_USR_MASK)))
880			return(-EPERM);
881	}
882
883	port->flags = (port->flags & ~ASYNC_USR_MASK) |
884		(sio.flags & ASYNC_USR_MASK);
885
886	port->baud_base = sio.baud_base;
887	port->close_delay = sio.close_delay;
888	port->closing_wait = sio.closing_wait;
889	port->custom_divisor = sio.custom_divisor;
890
891	gs_set_termios (port->tty, NULL);
892
893	return 0;
894}
895
896
897/*****************************************************************************/
898
899/*
900 *      Generate the serial struct info.
901 */
902
903int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
904{
905	struct serial_struct    sio;
906
907	memset(&sio, 0, sizeof(struct serial_struct));
908	sio.flags = port->flags;
909	sio.baud_base = port->baud_base;
910	sio.close_delay = port->close_delay;
911	sio.closing_wait = port->closing_wait;
912	sio.custom_divisor = port->custom_divisor;
913	sio.hub6 = 0;
914
915	/* If you want you can override these. */
916	sio.type = PORT_UNKNOWN;
917	sio.xmit_fifo_size = -1;
918	sio.line = -1;
919	sio.port = -1;
920	sio.irq = -1;
921
922	if (port->rd->getserial)
923		port->rd->getserial (port, &sio);
924
925	if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
926		return -EFAULT;
927	return 0;
928
929}
930
931
932void gs_got_break(struct gs_port *port)
933{
934	func_enter ();
935
936	tty_insert_flip_char(port->tty, 0, TTY_BREAK);
937	tty_schedule_flip(port->tty);
938	if (port->flags & ASYNC_SAK) {
939		do_SAK (port->tty);
940	}
941
942	func_exit ();
943}
944
945
946EXPORT_SYMBOL(gs_put_char);
947EXPORT_SYMBOL(gs_write);
948EXPORT_SYMBOL(gs_write_room);
949EXPORT_SYMBOL(gs_chars_in_buffer);
950EXPORT_SYMBOL(gs_flush_buffer);
951EXPORT_SYMBOL(gs_flush_chars);
952EXPORT_SYMBOL(gs_stop);
953EXPORT_SYMBOL(gs_start);
954EXPORT_SYMBOL(gs_hangup);
955EXPORT_SYMBOL(gs_block_til_ready);
956EXPORT_SYMBOL(gs_close);
957EXPORT_SYMBOL(gs_set_termios);
958EXPORT_SYMBOL(gs_init_port);
959EXPORT_SYMBOL(gs_setserial);
960EXPORT_SYMBOL(gs_getserial);
961EXPORT_SYMBOL(gs_got_break);
962
963MODULE_LICENSE("GPL");
964