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
33 static char * tmp_buf;
34 static DECLARE_MUTEX(tmp_buf_sem);
35
36 static 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 " __FUNCTION__ "\n")
45 #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit " __FUNCTION__ "\n")
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
59 MODULE_PARM(gs_debug, "i");
60
61
62 void 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
104 int 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 copy_from_user (port->xmit_buf + port->xmit_head, buf, c);
147 else
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 }
156 up (& port->port_write_sem);
157
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
179 int gs_write(struct tty_struct * tty, int from_user,
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 save_flags(flags);
212 if (from_user) {
213 down(&tmp_buf_sem);
214 while (1) {
215 c = count;
216
217 /* This is safe because we "OWN" the "head". Noone else can
218 change the "head": we own the port_write_sem. */
219 /* Don't overrun the end of the buffer */
220 t = SERIAL_XMIT_SIZE - port->xmit_head;
221 if (t < c) c = t;
222
223 /* This is safe because the xmit_cnt can only decrease. This
224 would increase "t", so we might copy too little chars. */
225 /* Don't copy past the "head" of the buffer */
226 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
227 if (t < c) c = t;
228
229 /* Can't copy more? break out! */
230 if (c <= 0) break;
231
232 c -= copy_from_user(tmp_buf, buf, c);
233 if (!c) {
234 if (!total)
235 total = -EFAULT;
236 break;
237 }
238 cli();
239 t = SERIAL_XMIT_SIZE - port->xmit_head;
240 if (t < c) c = t;
241 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
242 if (t < c) c = t;
243
244 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
245 port->xmit_head = ((port->xmit_head + c) &
246 (SERIAL_XMIT_SIZE-1));
247 port->xmit_cnt += c;
248 restore_flags(flags);
249 buf += c;
250 count -= c;
251 total += c;
252 }
253 up(&tmp_buf_sem);
254 } else {
255 while (1) {
256 cli();
257 c = count;
258
259 /* This is safe because we "OWN" the "head". Noone else can
260 change the "head": we own the port_write_sem. */
261 /* Don't overrun the end of the buffer */
262 t = SERIAL_XMIT_SIZE - port->xmit_head;
263 if (t < c) c = t;
264
265 /* This is safe because the xmit_cnt can only decrease. This
266 would increase "t", so we might copy too little chars. */
267 /* Don't copy past the "head" of the buffer */
268 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
269 if (t < c) c = t;
270
271 /* Can't copy more? break out! */
272 if (c <= 0) {
273 restore_flags(flags);
274 break;
275 }
276 memcpy(port->xmit_buf + port->xmit_head, buf, c);
277 port->xmit_head = ((port->xmit_head + c) &
278 (SERIAL_XMIT_SIZE-1));
279 port->xmit_cnt += c;
280 restore_flags(flags);
281 buf += c;
282 count -= c;
283 total += c;
284 }
285 }
286
287 if (port->xmit_cnt &&
288 !tty->stopped &&
289 !tty->hw_stopped &&
290 !(port->flags & GS_TX_INTEN)) {
291 port->flags |= GS_TX_INTEN;
292 port->rd->enable_tx_interrupts (port);
293 }
294 func_exit ();
295 return total;
296 }
297
298 #endif
299
300
301
302 int gs_write_room(struct tty_struct * tty)
303 {
304 struct gs_port *port = tty->driver_data;
305 int ret;
306
307 func_enter ();
308 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
309 if (ret < 0)
310 ret = 0;
311 func_exit ();
312 return ret;
313 }
314
315
316 int gs_chars_in_buffer(struct tty_struct *tty)
317 {
318 struct gs_port *port = tty->driver_data;
319 func_enter ();
320
321 func_exit ();
322 return port->xmit_cnt;
323 }
324
325
326 int gs_real_chars_in_buffer(struct tty_struct *tty)
327 {
328 struct gs_port *port;
329 func_enter ();
330
331 if (!tty) return 0;
332 port = tty->driver_data;
333
334 if (!port->rd) return 0;
335 if (!port->rd->chars_in_buffer) return 0;
336
337 func_exit ();
338 return port->xmit_cnt + port->rd->chars_in_buffer (port);
339 }
340
341
342 static int gs_wait_tx_flushed (void * ptr, int timeout)
343 {
344 struct gs_port *port = ptr;
345 long end_jiffies;
346 int jiffies_to_transmit, charsleft = 0, rv = 0;
347 int rcib;
348
349 func_enter();
350
351 gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
352 if (port) {
353 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
354 port->xmit_cnt, port->xmit_buf, port->tty);
355 }
356
357 if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
358 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
359 func_exit();
360 return -EINVAL; /* This is an error which we don't know how to handle. */
361 }
362
363 rcib = gs_real_chars_in_buffer(port->tty);
364
365 if(rcib <= 0) {
366 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
367 func_exit();
368 return rv;
369 }
370 /* stop trying: now + twice the time it would normally take + seconds */
371 if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
372 end_jiffies = jiffies;
373 if (timeout != MAX_SCHEDULE_TIMEOUT)
374 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
375 end_jiffies += timeout;
376
377 gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
378 jiffies, end_jiffies, end_jiffies-jiffies);
379
380 /* the expression is actually jiffies < end_jiffies, but that won't
381 work around the wraparound. Tricky eh? */
382 while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
383 time_after (end_jiffies, jiffies)) {
384 /* Units check:
385 chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
386 check! */
387
388 charsleft += 16; /* Allow 16 chars more to be transmitted ... */
389 jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
390 /* ^^^ Round up.... */
391 if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
392
393 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
394 "(%d chars).\n", jiffies_to_transmit, charsleft);
395
396 set_current_state (TASK_INTERRUPTIBLE);
397 schedule_timeout(jiffies_to_transmit);
398 if (signal_pending (current)) {
399 gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
400 rv = -EINTR;
401 break;
402 }
403 }
404
405 gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
406 set_current_state (TASK_RUNNING);
407
408 func_exit();
409 return rv;
410 }
411
412
413
414 void gs_flush_buffer(struct tty_struct *tty)
415 {
416 struct gs_port *port;
417 unsigned long flags;
418
419 func_enter ();
420
421 if (!tty) return;
422
423 port = tty->driver_data;
424
425 if (!port) return;
426
427 /* XXX Would the write semaphore do? */
428 save_flags(flags); cli();
429 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
430 restore_flags(flags);
431
432 wake_up_interruptible(&tty->write_wait);
433 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
434 tty->ldisc.write_wakeup)
435 (tty->ldisc.write_wakeup)(tty);
436 func_exit ();
437 }
438
439
440 void gs_flush_chars(struct tty_struct * tty)
441 {
442 struct gs_port *port;
443
444 func_enter ();
445
446 if (!tty) return;
447
448 port = tty->driver_data;
449
450 if (!port) return;
451
452 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
453 !port->xmit_buf) {
454 func_exit ();
455 return;
456 }
457
458 /* Beats me -- REW */
459 port->flags |= GS_TX_INTEN;
460 port->rd->enable_tx_interrupts (port);
461 func_exit ();
462 }
463
464
465 void gs_stop(struct tty_struct * tty)
466 {
467 struct gs_port *port;
468
469 func_enter ();
470
471 if (!tty) return;
472
473 port = tty->driver_data;
474
475 if (!port) return;
476
477 if (port->xmit_cnt &&
478 port->xmit_buf &&
479 (port->flags & GS_TX_INTEN) ) {
480 port->flags &= ~GS_TX_INTEN;
481 port->rd->disable_tx_interrupts (port);
482 }
483 func_exit ();
484 }
485
486
487 void gs_start(struct tty_struct * tty)
488 {
489 struct gs_port *port;
490
491 if (!tty) return;
492
493 port = tty->driver_data;
494
495 if (!port) return;
496
497 if (port->xmit_cnt &&
498 port->xmit_buf &&
499 !(port->flags & GS_TX_INTEN) ) {
500 port->flags |= GS_TX_INTEN;
501 port->rd->enable_tx_interrupts (port);
502 }
503 func_exit ();
504 }
505
506
507 void gs_shutdown_port (struct gs_port *port)
508 {
509 long flags;
510
511 func_enter();
512
513 if (!port) return;
514
515 if (!(port->flags & ASYNC_INITIALIZED))
516 return;
517
518 save_flags (flags);
519 cli ();
520
521 if (port->xmit_buf) {
522 free_page((unsigned long) port->xmit_buf);
523 port->xmit_buf = 0;
524 }
525
526 if (port->tty)
527 set_bit(TTY_IO_ERROR, &port->tty->flags);
528
529 port->rd->shutdown_port (port);
530
531 port->flags &= ~ASYNC_INITIALIZED;
532 restore_flags (flags);
533
534 func_exit();
535 }
536
537
538 void gs_hangup(struct tty_struct *tty)
539 {
540 struct gs_port *port;
541
542 func_enter ();
543
544 if (!tty) return;
545
546 port = tty->driver_data;
547 tty = port->tty;
548 if (!tty)
549 return;
550
551 gs_shutdown_port (port);
552 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE |GS_ACTIVE);
553 port->tty = NULL;
554 port->count = 0;
555
556 wake_up_interruptible(&port->open_wait);
557 func_exit ();
558 }
559
560
561 void gs_do_softint(void *private_)
562 {
563 struct gs_port *port = private_;
564 struct tty_struct *tty;
565
566 func_enter ();
567
568 if (!port) return;
569
570 tty = port->tty;
571
572 if (!tty) return;
573
574 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
575 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
576 tty->ldisc.write_wakeup)
577 (tty->ldisc.write_wakeup)(tty);
578 wake_up_interruptible(&tty->write_wait);
579 }
580 func_exit ();
581 }
582
583
584 int gs_block_til_ready(void *port_, struct file * filp)
585 {
586 struct gs_port *port = port_;
587 DECLARE_WAITQUEUE(wait, current);
588 int retval;
589 int do_clocal = 0;
590 int CD;
591 struct tty_struct *tty;
592
593 func_enter ();
594
595 if (!port) return 0;
596
597 tty = port->tty;
598
599 if (!tty) return 0;
600
601 gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
602 /*
603 * If the device is in the middle of being closed, then block
604 * until it's done, and then try again.
605 */
606 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
607 interruptible_sleep_on(&port->close_wait);
608 if (port->flags & ASYNC_HUP_NOTIFY)
609 return -EAGAIN;
610 else
611 return -ERESTARTSYS;
612 }
613
614 gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
615
616 /*
617 * If this is a callout device, then just make sure the normal
618 * device isn't being used.
619 */
620 if (tty->driver.subtype == GS_TYPE_CALLOUT) {
621 if (port->flags & ASYNC_NORMAL_ACTIVE)
622 return -EBUSY;
623 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
624 (port->flags & ASYNC_SESSION_LOCKOUT) &&
625 (port->session != current->session))
626 return -EBUSY;
627 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
628 (port->flags & ASYNC_PGRP_LOCKOUT) &&
629 (port->pgrp != current->pgrp))
630 return -EBUSY;
631 port->flags |= ASYNC_CALLOUT_ACTIVE;
632 return 0;
633 }
634
635 gs_dprintk (GS_DEBUG_BTR, "after subtype\n");
636
637 /*
638 * If non-blocking mode is set, or the port is not enabled,
639 * then make the check up front and then exit.
640 */
641 if ((filp->f_flags & O_NONBLOCK) ||
642 (tty->flags & (1 << TTY_IO_ERROR))) {
643 if (port->flags & ASYNC_CALLOUT_ACTIVE)
644 return -EBUSY;
645 port->flags |= ASYNC_NORMAL_ACTIVE;
646 return 0;
647 }
648
649 gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
650
651 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
652 if (port->normal_termios.c_cflag & CLOCAL)
653 do_clocal = 1;
654 } else {
655 if (C_CLOCAL(tty))
656 do_clocal = 1;
657 }
658
659 /*
660 * Block waiting for the carrier detect and the line to become
661 * free (i.e., not in use by the callout). While we are in
662 * this loop, port->count is dropped by one, so that
663 * rs_close() knows when to free things. We restore it upon
664 * exit, either normal or abnormal.
665 */
666 retval = 0;
667
668 add_wait_queue(&port->open_wait, &wait);
669
670 gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
671 cli();
672 if (!tty_hung_up_p(filp))
673 port->count--;
674 sti();
675 port->blocked_open++;
676 while (1) {
677 CD = port->rd->get_CD (port);
678 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
679 set_current_state (TASK_INTERRUPTIBLE);
680 if (tty_hung_up_p(filp) ||
681 !(port->flags & ASYNC_INITIALIZED)) {
682 if (port->flags & ASYNC_HUP_NOTIFY)
683 retval = -EAGAIN;
684 else
685 retval = -ERESTARTSYS;
686 break;
687 }
688 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
689 !(port->flags & ASYNC_CLOSING) &&
690 (do_clocal || CD))
691 break;
692 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
693 (int)signal_pending (current), *(long*)(¤t->blocked));
694 if (signal_pending(current)) {
695 retval = -ERESTARTSYS;
696 break;
697 }
698 schedule();
699 }
700 gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
701 port->blocked_open);
702 set_current_state (TASK_RUNNING);
703 remove_wait_queue(&port->open_wait, &wait);
704 if (!tty_hung_up_p(filp))
705 port->count++;
706 port->blocked_open--;
707 if (retval)
708 return retval;
709
710 port->flags |= ASYNC_NORMAL_ACTIVE;
711 func_exit ();
712 return 0;
713 }
714
715
716 void gs_close(struct tty_struct * tty, struct file * filp)
717 {
718 unsigned long flags;
719 struct gs_port *port;
720
721 func_enter ();
722
723 if (!tty) return;
724
725 port = (struct gs_port *) tty->driver_data;
726
727 if (!port) return;
728
729 if (!port->tty) {
730 /* This seems to happen when this is called from vhangup. */
731 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
732 port->tty = tty;
733 }
734
735 save_flags(flags); cli();
736
737 if (tty_hung_up_p(filp)) {
738 restore_flags(flags);
739 port->rd->hungup (port);
740 func_exit ();
741 return;
742 }
743
744 if ((tty->count == 1) && (port->count != 1)) {
745 printk(KERN_ERR "gs: gs_close: bad port count;"
746 " tty->count is 1, port count is %d\n", port->count);
747 port->count = 1;
748 }
749 if (--port->count < 0) {
750 printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
751 port->count = 0;
752 }
753 if (port->count) {
754 gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %d\n", port->count);
755 restore_flags(flags);
756 func_exit ();
757 return;
758 }
759 port->flags |= ASYNC_CLOSING;
760
761 /*
762 * Save the termios structure, since this port may have
763 * separate termios for callout and dialin.
764 */
765 if (port->flags & ASYNC_NORMAL_ACTIVE)
766 port->normal_termios = *tty->termios;
767 if (port->flags & ASYNC_CALLOUT_ACTIVE)
768 port->callout_termios = *tty->termios;
769 /*
770 * Now we wait for the transmit buffer to clear; and we notify
771 * the line discipline to only process XON/XOFF characters.
772 */
773 tty->closing = 1;
774 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
775 tty_wait_until_sent(tty, port->closing_wait); */
776
777 /*
778 * At this point we stop accepting input. To do this, we
779 * disable the receive line status interrupts, and tell the
780 * interrupt driver to stop checking the data ready bit in the
781 * line status register.
782 */
783
784 port->rd->disable_rx_interrupts (port);
785
786 /* close has no way of returning "EINTR", so discard return value */
787 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
788 gs_wait_tx_flushed (port, port->closing_wait);
789
790 port->flags &= ~GS_ACTIVE;
791
792 if (tty->driver.flush_buffer)
793 tty->driver.flush_buffer(tty);
794 if (tty->ldisc.flush_buffer)
795 tty->ldisc.flush_buffer(tty);
796 tty->closing = 0;
797
798 port->event = 0;
799 port->rd->close (port);
800 port->rd->shutdown_port (port);
801 port->tty = 0;
802
803 if (port->blocked_open) {
804 if (port->close_delay) {
805 set_current_state (TASK_INTERRUPTIBLE);
806 schedule_timeout(port->close_delay);
807 }
808 wake_up_interruptible(&port->open_wait);
809 }
810 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
811 ASYNC_CLOSING | ASYNC_INITIALIZED);
812 wake_up_interruptible(&port->close_wait);
813
814 restore_flags(flags);
815 func_exit ();
816 }
817
818
819 static unsigned int gs_baudrates[] = {
820 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
821 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
822 };
823
824
825 void gs_set_termios (struct tty_struct * tty,
826 struct termios * old_termios)
827 {
828 struct gs_port *port;
829 int baudrate, tmp, rv;
830 struct termios *tiosp;
831
832 func_enter();
833
834 if (!tty) return;
835
836 port = tty->driver_data;
837
838 if (!port) return;
839
840 tiosp = tty->termios;
841
842 if (gs_debug & GS_DEBUG_TERMIOS) {
843 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
844 }
845
846 #if 0
847 /* This is an optimization that is only allowed for dumb cards */
848 /* Smart cards require knowledge of iflags and oflags too: that
849 might change hardware cooking mode.... */
850 #endif
851 if (old_termios) {
852 if( (tiosp->c_iflag == old_termios->c_iflag)
853 && (tiosp->c_oflag == old_termios->c_oflag)
854 && (tiosp->c_cflag == old_termios->c_cflag)
855 && (tiosp->c_lflag == old_termios->c_lflag)
856 && (tiosp->c_line == old_termios->c_line)
857 && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
858 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
859 return /* 0 */;
860 }
861 } else
862 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
863 "no optimization\n");
864
865 if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
866 if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
867 if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
868 if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
869 if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
870 if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
871 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
872 }
873
874 baudrate = tiosp->c_cflag & CBAUD;
875 if (baudrate & CBAUDEX) {
876 baudrate &= ~CBAUDEX;
877 if ((baudrate < 1) || (baudrate > 4))
878 tiosp->c_cflag &= ~CBAUDEX;
879 else
880 baudrate += 15;
881 }
882
883 baudrate = gs_baudrates[baudrate];
884 if ((tiosp->c_cflag & CBAUD) == B38400) {
885 if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
886 baudrate = 57600;
887 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
888 baudrate = 115200;
889 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
890 baudrate = 230400;
891 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
892 baudrate = 460800;
893 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
894 baudrate = (port->baud_base / port->custom_divisor);
895 }
896
897 /* I recommend using THIS instead of the mess in termios (and
898 duplicating the above code). Next we should create a clean
899 interface towards this variable. If your card supports arbitrary
900 baud rates, (e.g. CD1400 or 16550 based cards) then everything
901 will be very easy..... */
902 port->baud = baudrate;
903
904 /* Two timer ticks seems enough to wakeup something like SLIP driver */
905 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
906 tmp = (baudrate / 10 / HZ) * 2;
907
908 if (tmp < 0) tmp = 0;
909 if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
910
911 port->wakeup_chars = tmp;
912
913 /* We should really wait for the characters to be all sent before
914 changing the settings. -- CAL */
915 rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
916 if (rv < 0) return /* rv */;
917
918 rv = port->rd->set_real_termios(port);
919 if (rv < 0) return /* rv */;
920
921 if ((!old_termios ||
922 (old_termios->c_cflag & CRTSCTS)) &&
923 !( tiosp->c_cflag & CRTSCTS)) {
924 tty->stopped = 0;
925 gs_start(tty);
926 }
927
928 #ifdef tytso_patch_94Nov25_1726
929 /* This "makes sense", Why is it commented out? */
930
931 if (!(old_termios->c_cflag & CLOCAL) &&
932 (tty->termios->c_cflag & CLOCAL))
933 wake_up_interruptible(&info->open_wait);
934 #endif
935
936 func_exit();
937 return /* 0 */;
938 }
939
940
941
942 /* Must be called with interrupts enabled */
943 int gs_init_port(struct gs_port *port)
944 {
945 unsigned long flags;
946 unsigned long page;
947
948 save_flags (flags);
949 if (!tmp_buf) {
950 page = get_free_page(GFP_KERNEL);
951
952 cli (); /* Don't expect this to make a difference. */
953 if (tmp_buf)
954 free_page(page);
955 else
956 tmp_buf = (unsigned char *) page;
957 restore_flags (flags);
958
959 if (!tmp_buf) {
960 return -ENOMEM;
961 }
962 }
963
964 if (port->flags & ASYNC_INITIALIZED)
965 return 0;
966
967 if (!port->xmit_buf) {
968 /* We may sleep in get_free_page() */
969 unsigned long tmp;
970
971 tmp = get_free_page(GFP_KERNEL);
972
973 /* Spinlock? */
974 cli ();
975 if (port->xmit_buf)
976 free_page (tmp);
977 else
978 port->xmit_buf = (unsigned char *) tmp;
979 restore_flags (flags);
980
981 if (!port->xmit_buf)
982 return -ENOMEM;
983 }
984
985 cli();
986
987 if (port->tty)
988 clear_bit(TTY_IO_ERROR, &port->tty->flags);
989
990 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
991
992 gs_set_termios(port->tty, NULL);
993
994 port->flags |= ASYNC_INITIALIZED;
995 port->flags &= ~GS_TX_INTEN;
996
997 restore_flags(flags);
998 return 0;
999 }
1000
1001
1002 int gs_setserial(struct gs_port *port, struct serial_struct *sp)
1003 {
1004 struct serial_struct sio;
1005
1006 copy_from_user(&sio, sp, sizeof(struct serial_struct));
1007
1008 if (!capable(CAP_SYS_ADMIN)) {
1009 if ((sio.baud_base != port->baud_base) ||
1010 (sio.close_delay != port->close_delay) ||
1011 ((sio.flags & ~ASYNC_USR_MASK) !=
1012 (port->flags & ~ASYNC_USR_MASK)))
1013 return(-EPERM);
1014 }
1015
1016 port->flags = (port->flags & ~ASYNC_USR_MASK) |
1017 (sio.flags & ASYNC_USR_MASK);
1018
1019 port->baud_base = sio.baud_base;
1020 port->close_delay = sio.close_delay;
1021 port->closing_wait = sio.closing_wait;
1022 port->custom_divisor = sio.custom_divisor;
1023
1024 gs_set_termios (port->tty, NULL);
1025
1026 return 0;
1027 }
1028
1029
1030 /*****************************************************************************/
1031
1032 /*
1033 * Generate the serial struct info.
1034 */
1035
1036 void gs_getserial(struct gs_port *port, struct serial_struct *sp)
1037 {
1038 struct serial_struct sio;
1039
1040 memset(&sio, 0, sizeof(struct serial_struct));
1041 sio.flags = port->flags;
1042 sio.baud_base = port->baud_base;
1043 sio.close_delay = port->close_delay;
1044 sio.closing_wait = port->closing_wait;
1045 sio.custom_divisor = port->custom_divisor;
1046 sio.hub6 = 0;
1047
1048 /* If you want you can override these. */
1049 sio.type = PORT_UNKNOWN;
1050 sio.xmit_fifo_size = -1;
1051 sio.line = -1;
1052 sio.port = -1;
1053 sio.irq = -1;
1054
1055 if (port->rd->getserial)
1056 port->rd->getserial (port, &sio);
1057
1058 copy_to_user(sp, &sio, sizeof(struct serial_struct));
1059 }
1060
1061
1062 void gs_got_break(struct gs_port *port)
1063 {
1064 if (port->flags & ASYNC_SAK) {
1065 do_SAK (port->tty);
1066 }
1067 *(port->tty->flip.flag_buf_ptr) = TTY_BREAK;
1068 port->tty->flip.flag_buf_ptr++;
1069 port->tty->flip.char_buf_ptr++;
1070 port->tty->flip.count++;
1071 }
1072
1073
1074 EXPORT_SYMBOL(gs_put_char);
1075 EXPORT_SYMBOL(gs_write);
1076 EXPORT_SYMBOL(gs_write_room);
1077 EXPORT_SYMBOL(gs_chars_in_buffer);
1078 EXPORT_SYMBOL(gs_flush_buffer);
1079 EXPORT_SYMBOL(gs_flush_chars);
1080 EXPORT_SYMBOL(gs_stop);
1081 EXPORT_SYMBOL(gs_start);
1082 EXPORT_SYMBOL(gs_hangup);
1083 EXPORT_SYMBOL(gs_do_softint);
1084 EXPORT_SYMBOL(gs_block_til_ready);
1085 EXPORT_SYMBOL(gs_close);
1086 EXPORT_SYMBOL(gs_set_termios);
1087 EXPORT_SYMBOL(gs_init_port);
1088 EXPORT_SYMBOL(gs_setserial);
1089 EXPORT_SYMBOL(gs_getserial);
1090 EXPORT_SYMBOL(gs_got_break);
1091
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.