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