[SERIAL] Clean up and fix tty transmission start/stoping
[pandora-kernel.git] / drivers / serial / jsm / jsm_tty.c
1 /************************************************************************
2  * Copyright 2003 Digi International (www.digi.com)
3  *
4  * Copyright (C) 2004 IBM Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  * PURPOSE.  See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19  * MA  02111-1307, USA.
20  *
21  * Contact Information:
22  * Scott H Kilau <Scott_Kilau@digi.com>
23  * Wendy Xiong   <wendyx@us.ltcfwd.linux.ibm.com>
24  *
25  ***********************************************************************/
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial_reg.h>
29 #include <linux/delay.h>        /* For udelay */
30 #include <linux/pci.h>
31
32 #include "jsm.h"
33
34 static void jsm_carrier(struct jsm_channel *ch);
35
36 static inline int jsm_get_mstat(struct jsm_channel *ch)
37 {
38         unsigned char mstat;
39         unsigned result;
40
41         jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "start\n");
42
43         mstat = (ch->ch_mostat | ch->ch_mistat);
44
45         result = 0;
46
47         if (mstat & UART_MCR_DTR)
48                 result |= TIOCM_DTR;
49         if (mstat & UART_MCR_RTS)
50                 result |= TIOCM_RTS;
51         if (mstat & UART_MSR_CTS)
52                 result |= TIOCM_CTS;
53         if (mstat & UART_MSR_DSR)
54                 result |= TIOCM_DSR;
55         if (mstat & UART_MSR_RI)
56                 result |= TIOCM_RI;
57         if (mstat & UART_MSR_DCD)
58                 result |= TIOCM_CD;
59
60         jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
61         return result;
62 }
63
64 static unsigned int jsm_tty_tx_empty(struct uart_port *port)
65 {
66         return TIOCSER_TEMT;
67 }
68
69 /*
70  * Return modem signals to ld.
71  */
72 static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
73 {
74         int result;
75         struct jsm_channel *channel = (struct jsm_channel *)port;
76
77         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
78
79         result = jsm_get_mstat(channel);
80
81         if (result < 0)
82                 return -ENXIO;
83
84         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
85
86         return result;
87 }
88
89 /*
90  * jsm_set_modem_info()
91  *
92  * Set modem signals, called by ld.
93  */
94 static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
95 {
96         struct jsm_channel *channel = (struct jsm_channel *)port;
97
98         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
99
100         if (mctrl & TIOCM_RTS)
101                 channel->ch_mostat |= UART_MCR_RTS;
102         else
103                 channel->ch_mostat &= ~UART_MCR_RTS;
104
105         if (mctrl & TIOCM_DTR)
106                 channel->ch_mostat |= UART_MCR_DTR;
107         else
108                 channel->ch_mostat &= ~UART_MCR_DTR;
109
110         channel->ch_bd->bd_ops->assert_modem_signals(channel);
111
112         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
113         udelay(10);
114 }
115
116 static void jsm_tty_start_tx(struct uart_port *port)
117 {
118         struct jsm_channel *channel = (struct jsm_channel *)port;
119
120         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
121
122         channel->ch_flags &= ~(CH_STOP);
123         jsm_tty_write(port);
124
125         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
126 }
127
128 static void jsm_tty_stop_tx(struct uart_port *port)
129 {
130         struct jsm_channel *channel = (struct jsm_channel *)port;
131
132         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
133
134         channel->ch_flags |= (CH_STOP);
135
136         jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
137 }
138
139 static void jsm_tty_send_xchar(struct uart_port *port, char ch)
140 {
141         unsigned long lock_flags;
142         struct jsm_channel *channel = (struct jsm_channel *)port;
143
144         spin_lock_irqsave(&port->lock, lock_flags);
145         if (ch == port->info->tty->termios->c_cc[VSTART])
146                 channel->ch_bd->bd_ops->send_start_character(channel);
147
148         if (ch == port->info->tty->termios->c_cc[VSTOP])
149                 channel->ch_bd->bd_ops->send_stop_character(channel);
150         spin_unlock_irqrestore(&port->lock, lock_flags);
151 }
152
153 static void jsm_tty_stop_rx(struct uart_port *port)
154 {
155         struct jsm_channel *channel = (struct jsm_channel *)port;
156
157         channel->ch_bd->bd_ops->disable_receiver(channel);
158 }
159
160 static void jsm_tty_break(struct uart_port *port, int break_state)
161 {
162         unsigned long lock_flags;
163         struct jsm_channel *channel = (struct jsm_channel *)port;
164
165         spin_lock_irqsave(&port->lock, lock_flags);
166         if (break_state == -1)
167                 channel->ch_bd->bd_ops->send_break(channel);
168         else
169                 channel->ch_bd->bd_ops->clear_break(channel, 0);
170
171         spin_unlock_irqrestore(&port->lock, lock_flags);
172 }
173
174 static int jsm_tty_open(struct uart_port *port)
175 {
176         struct jsm_board *brd;
177         int rc = 0;
178         struct jsm_channel *channel = (struct jsm_channel *)port;
179
180         /* Get board pointer from our array of majors we have allocated */
181         brd = channel->ch_bd;
182
183         /*
184          * Allocate channel buffers for read/write/error.
185          * Set flag, so we don't get trounced on.
186          */
187         channel->ch_flags |= (CH_OPENING);
188
189         /* Drop locks, as malloc with GFP_KERNEL can sleep */
190
191         if (!channel->ch_rqueue) {
192                 channel->ch_rqueue = (u8 *) kmalloc(RQUEUESIZE, GFP_KERNEL);
193                 if (!channel->ch_rqueue) {
194                         jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
195                                 "unable to allocate read queue buf");
196                         return -ENOMEM;
197                 }
198                 memset(channel->ch_rqueue, 0, RQUEUESIZE);
199         }
200         if (!channel->ch_equeue) {
201                 channel->ch_equeue = (u8 *) kmalloc(EQUEUESIZE, GFP_KERNEL);
202                 if (!channel->ch_equeue) {
203                         jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
204                                 "unable to allocate error queue buf");
205                         return -ENOMEM;
206                 }
207                 memset(channel->ch_equeue, 0, EQUEUESIZE);
208         }
209         if (!channel->ch_wqueue) {
210                 channel->ch_wqueue = (u8 *) kmalloc(WQUEUESIZE, GFP_KERNEL);
211                 if (!channel->ch_wqueue) {
212                         jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
213                                 "unable to allocate write queue buf");
214                         return -ENOMEM;
215                 }
216                 memset(channel->ch_wqueue, 0, WQUEUESIZE);
217         }
218
219         channel->ch_flags &= ~(CH_OPENING);
220         /*
221          * Initialize if neither terminal is open.
222          */
223         jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev,
224                 "jsm_open: initializing channel in open...\n");
225
226         /*
227          * Flush input queues.
228          */
229         channel->ch_r_head = channel->ch_r_tail = 0;
230         channel->ch_e_head = channel->ch_e_tail = 0;
231         channel->ch_w_head = channel->ch_w_tail = 0;
232
233         brd->bd_ops->flush_uart_write(channel);
234         brd->bd_ops->flush_uart_read(channel);
235
236         channel->ch_flags = 0;
237         channel->ch_cached_lsr = 0;
238         channel->ch_stops_sent = 0;
239
240         channel->ch_c_cflag     = port->info->tty->termios->c_cflag;
241         channel->ch_c_iflag     = port->info->tty->termios->c_iflag;
242         channel->ch_c_oflag     = port->info->tty->termios->c_oflag;
243         channel->ch_c_lflag     = port->info->tty->termios->c_lflag;
244         channel->ch_startc = port->info->tty->termios->c_cc[VSTART];
245         channel->ch_stopc = port->info->tty->termios->c_cc[VSTOP];
246
247         /* Tell UART to init itself */
248         brd->bd_ops->uart_init(channel);
249
250         /*
251          * Run param in case we changed anything
252          */
253         brd->bd_ops->param(channel);
254
255         jsm_carrier(channel);
256
257         channel->ch_open_count++;
258
259         jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev, "finish\n");
260         return rc;
261 }
262
263 static void jsm_tty_close(struct uart_port *port)
264 {
265         struct jsm_board *bd;
266         struct termios *ts;
267         struct jsm_channel *channel = (struct jsm_channel *)port;
268
269         jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "start\n");
270
271         bd = channel->ch_bd;
272         ts = channel->uart_port.info->tty->termios;
273
274         channel->ch_flags &= ~(CH_STOPI);
275
276         channel->ch_open_count--;
277
278         /*
279          * If we have HUPCL set, lower DTR and RTS
280          */
281         if (channel->ch_c_cflag & HUPCL) {
282                 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev,
283                         "Close. HUPCL set, dropping DTR/RTS\n");
284
285                 /* Drop RTS/DTR */
286                 channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
287                 bd->bd_ops->assert_modem_signals(channel);
288         }
289
290         channel->ch_old_baud = 0;
291
292         /* Turn off UART interrupts for this port */
293         channel->ch_bd->bd_ops->uart_off(channel);
294
295         jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "finish\n");
296 }
297
298 static void jsm_tty_set_termios(struct uart_port *port,
299                                  struct termios *termios,
300                                  struct termios *old_termios)
301 {
302         unsigned long lock_flags;
303         struct jsm_channel *channel = (struct jsm_channel *)port;
304
305         spin_lock_irqsave(&port->lock, lock_flags);
306         channel->ch_c_cflag     = termios->c_cflag;
307         channel->ch_c_iflag     = termios->c_iflag;
308         channel->ch_c_oflag     = termios->c_oflag;
309         channel->ch_c_lflag     = termios->c_lflag;
310         channel->ch_startc      = termios->c_cc[VSTART];
311         channel->ch_stopc       = termios->c_cc[VSTOP];
312
313         channel->ch_bd->bd_ops->param(channel);
314         jsm_carrier(channel);
315         spin_unlock_irqrestore(&port->lock, lock_flags);
316 }
317
318 static const char *jsm_tty_type(struct uart_port *port)
319 {
320         return "jsm";
321 }
322
323 static void jsm_tty_release_port(struct uart_port *port)
324 {
325 }
326
327 static int jsm_tty_request_port(struct uart_port *port)
328 {
329         return 0;
330 }
331
332 static void jsm_config_port(struct uart_port *port, int flags)
333 {
334         port->type = PORT_JSM;
335 }
336
337 static struct uart_ops jsm_ops = {
338         .tx_empty       = jsm_tty_tx_empty,
339         .set_mctrl      = jsm_tty_set_mctrl,
340         .get_mctrl      = jsm_tty_get_mctrl,
341         .stop_tx        = jsm_tty_stop_tx,
342         .start_tx       = jsm_tty_start_tx,
343         .send_xchar     = jsm_tty_send_xchar,
344         .stop_rx        = jsm_tty_stop_rx,
345         .break_ctl      = jsm_tty_break,
346         .startup        = jsm_tty_open,
347         .shutdown       = jsm_tty_close,
348         .set_termios    = jsm_tty_set_termios,
349         .type           = jsm_tty_type,
350         .release_port   = jsm_tty_release_port,
351         .request_port   = jsm_tty_request_port,
352         .config_port    = jsm_config_port,
353 };
354
355 /*
356  * jsm_tty_init()
357  *
358  * Init the tty subsystem.  Called once per board after board has been
359  * downloaded and init'ed.
360  */
361 int jsm_tty_init(struct jsm_board *brd)
362 {
363         int i;
364         void __iomem *vaddr;
365         struct jsm_channel *ch;
366
367         if (!brd)
368                 return -ENXIO;
369
370         jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
371
372         /*
373          * Initialize board structure elements.
374          */
375
376         brd->nasync = brd->maxports;
377
378         /*
379          * Allocate channel memory that might not have been allocated
380          * when the driver was first loaded.
381          */
382         for (i = 0; i < brd->nasync; i++) {
383                 if (!brd->channels[i]) {
384
385                         /*
386                          * Okay to malloc with GFP_KERNEL, we are not at
387                          * interrupt context, and there are no locks held.
388                          */
389                         brd->channels[i] = kmalloc(sizeof(struct jsm_channel), GFP_KERNEL);
390                         if (!brd->channels[i]) {
391                                 jsm_printk(CORE, ERR, &brd->pci_dev,
392                                         "%s:%d Unable to allocate memory for channel struct\n",
393                                                          __FILE__, __LINE__);
394                         }
395                         memset(brd->channels[i], 0, sizeof(struct jsm_channel));
396                 }
397         }
398
399         ch = brd->channels[0];
400         vaddr = brd->re_map_membase;
401
402         /* Set up channel variables */
403         for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
404
405                 if (!brd->channels[i])
406                         continue;
407
408                 spin_lock_init(&ch->ch_lock);
409
410                 if (brd->bd_uart_offset == 0x200)
411                         ch->ch_neo_uart =  vaddr + (brd->bd_uart_offset * i);
412
413                 ch->ch_bd = brd;
414                 ch->ch_portnum = i;
415
416                 /* .25 second delay */
417                 ch->ch_close_delay = 250;
418
419                 init_waitqueue_head(&ch->ch_flags_wait);
420         }
421
422         jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
423         return 0;
424 }
425
426 int jsm_uart_port_init(struct jsm_board *brd)
427 {
428         int i;
429         struct jsm_channel *ch;
430
431         if (!brd)
432                 return -ENXIO;
433
434         jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
435
436         /*
437          * Initialize board structure elements.
438          */
439
440         brd->nasync = brd->maxports;
441
442         /* Set up channel variables */
443         for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
444
445                 if (!brd->channels[i])
446                         continue;
447
448                 brd->channels[i]->uart_port.irq = brd->irq;
449                 brd->channels[i]->uart_port.type = PORT_JSM;
450                 brd->channels[i]->uart_port.iotype = UPIO_MEM;
451                 brd->channels[i]->uart_port.membase = brd->re_map_membase;
452                 brd->channels[i]->uart_port.fifosize = 16;
453                 brd->channels[i]->uart_port.ops = &jsm_ops;
454                 brd->channels[i]->uart_port.line = brd->channels[i]->ch_portnum + brd->boardnum * 2;
455                 if (uart_add_one_port (&jsm_uart_driver, &brd->channels[i]->uart_port))
456                         printk(KERN_INFO "Added device failed\n");
457                 else
458                         printk(KERN_INFO "Added device \n");
459         }
460
461         jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
462         return 0;
463 }
464
465 int jsm_remove_uart_port(struct jsm_board *brd)
466 {
467         int i;
468         struct jsm_channel *ch;
469
470         if (!brd)
471                 return -ENXIO;
472
473         jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
474
475         /*
476          * Initialize board structure elements.
477          */
478
479         brd->nasync = brd->maxports;
480
481         /* Set up channel variables */
482         for (i = 0; i < brd->nasync; i++) {
483
484                 if (!brd->channels[i])
485                         continue;
486
487                 ch = brd->channels[i];
488
489                 uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
490         }
491
492         jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
493         return 0;
494 }
495
496 void jsm_input(struct jsm_channel *ch)
497 {
498         struct jsm_board *bd;
499         struct tty_struct *tp;
500         u32 rmask;
501         u16 head;
502         u16 tail;
503         int data_len;
504         unsigned long lock_flags;
505         int flip_len;
506         int len = 0;
507         int n = 0;
508         char *buf = NULL;
509         char *buf2 = NULL;
510         int s = 0;
511         int i = 0;
512
513         jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
514
515         if (!ch)
516                 return;
517
518         tp = ch->uart_port.info->tty;
519
520         bd = ch->ch_bd;
521         if(!bd)
522                 return;
523
524         spin_lock_irqsave(&ch->ch_lock, lock_flags);
525
526         /*
527          *Figure the number of characters in the buffer.
528          *Exit immediately if none.
529          */
530
531         rmask = RQUEUEMASK;
532
533         head = ch->ch_r_head & rmask;
534         tail = ch->ch_r_tail & rmask;
535
536         data_len = (head - tail) & rmask;
537         if (data_len == 0) {
538                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
539                 return;
540         }
541
542         jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
543
544         /*
545          *If the device is not open, or CREAD is off, flush
546          *input data and return immediately.
547          */
548         if (!tp ||
549                 !(tp->termios->c_cflag & CREAD) ) {
550
551                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
552                         "input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum);
553                 ch->ch_r_head = tail;
554
555                 /* Force queue flow control to be released, if needed */
556                 jsm_check_queue_flow_control(ch);
557
558                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
559                 return;
560         }
561
562         /*
563          * If we are throttled, simply don't read any data.
564          */
565         if (ch->ch_flags & CH_STOPI) {
566                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
567                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
568                         "Port %d throttled, not reading any data. head: %x tail: %x\n",
569                         ch->ch_portnum, head, tail);
570                 return;
571         }
572
573         jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start 2\n");
574
575         /*
576          * If the rxbuf is empty and we are not throttled, put as much
577          * as we can directly into the linux TTY flip buffer.
578          * The jsm_rawreadok case takes advantage of carnal knowledge that
579          * the char_buf and the flag_buf are next to each other and
580          * are each of (2 * TTY_FLIPBUF_SIZE) size.
581          *
582          * NOTE: if(!tty->real_raw), the call to ldisc.receive_buf
583          *actually still uses the flag buffer, so you can't
584          *use it for input data
585          */
586         if (jsm_rawreadok) {
587                 if (tp->real_raw)
588                         flip_len = MYFLIPLEN;
589                 else
590                         flip_len = 2 * TTY_FLIPBUF_SIZE;
591         } else
592                 flip_len = TTY_FLIPBUF_SIZE - tp->flip.count;
593
594         len = min(data_len, flip_len);
595         len = min(len, (N_TTY_BUF_SIZE - 1) - tp->read_cnt);
596
597         if (len <= 0) {
598                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
599                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "jsm_input 1\n");
600                 return;
601         }
602
603         /*
604          * If we're bypassing flip buffers on rx, we can blast it
605          * right into the beginning of the buffer.
606          */
607         if (jsm_rawreadok) {
608                 if (tp->real_raw) {
609                         if (ch->ch_flags & CH_FLIPBUF_IN_USE) {
610                                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
611                                         "JSM - FLIPBUF in use. delaying input\n");
612                                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
613                                 return;
614                         }
615                         ch->ch_flags |= CH_FLIPBUF_IN_USE;
616                         buf = ch->ch_bd->flipbuf;
617                         buf2 = NULL;
618                 } else {
619                         buf = tp->flip.char_buf;
620                         buf2 = tp->flip.flag_buf;
621                 }
622         } else {
623                 buf = tp->flip.char_buf_ptr;
624                 buf2 = tp->flip.flag_buf_ptr;
625         }
626
627         n = len;
628
629         /*
630          * n now contains the most amount of data we can copy,
631          * bounded either by the flip buffer size or the amount
632          * of data the card actually has pending...
633          */
634         while (n) {
635                 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
636                 s = min(s, n);
637
638                 if (s <= 0)
639                         break;
640
641                 memcpy(buf, ch->ch_rqueue + tail, s);
642
643                 /* buf2 is only set when port isn't raw */
644                 if (buf2)
645                         memcpy(buf2, ch->ch_equeue + tail, s);
646
647                 tail += s;
648                 buf += s;
649                 if (buf2)
650                         buf2 += s;
651                 n -= s;
652                 /* Flip queue if needed */
653                 tail &= rmask;
654         }
655
656         /*
657          * In high performance mode, we don't have to update
658          * flag_buf or any of the counts or pointers into flip buf.
659          */
660         if (!jsm_rawreadok) {
661                 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
662                         for (i = 0; i < len; i++) {
663                                 /*
664                                  * Give the Linux ld the flags in the
665                                  * format it likes.
666                                  */
667                                 if (tp->flip.flag_buf_ptr[i] & UART_LSR_BI)
668                                         tp->flip.flag_buf_ptr[i] = TTY_BREAK;
669                                 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_PE)
670                                         tp->flip.flag_buf_ptr[i] = TTY_PARITY;
671                                 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_FE)
672                                         tp->flip.flag_buf_ptr[i] = TTY_FRAME;
673                                 else
674                                         tp->flip.flag_buf_ptr[i] = TTY_NORMAL;
675                         }
676                 } else {
677                         memset(tp->flip.flag_buf_ptr, 0, len);
678                 }
679
680                 tp->flip.char_buf_ptr += len;
681                 tp->flip.flag_buf_ptr += len;
682                 tp->flip.count += len;
683         }
684         else if (!tp->real_raw) {
685                 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
686                         for (i = 0; i < len; i++) {
687                                 /*
688                                  * Give the Linux ld the flags in the
689                                  * format it likes.
690                                  */
691                                 if (tp->flip.flag_buf_ptr[i] & UART_LSR_BI)
692                                         tp->flip.flag_buf_ptr[i] = TTY_BREAK;
693                                 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_PE)
694                                         tp->flip.flag_buf_ptr[i] = TTY_PARITY;
695                                 else if (tp->flip.flag_buf_ptr[i] & UART_LSR_FE)
696                                         tp->flip.flag_buf_ptr[i] = TTY_FRAME;
697                                 else
698                                         tp->flip.flag_buf_ptr[i] = TTY_NORMAL;
699                         }
700                 } else
701                         memset(tp->flip.flag_buf, 0, len);
702         }
703
704         /*
705          * If we're doing raw reads, jam it right into the
706          * line disc bypassing the flip buffers.
707          */
708         if (jsm_rawreadok) {
709                 if (tp->real_raw) {
710                         ch->ch_r_tail = tail & rmask;
711                         ch->ch_e_tail = tail & rmask;
712
713                         jsm_check_queue_flow_control(ch);
714
715                         /* !!! WE *MUST* LET GO OF ALL LOCKS BEFORE CALLING RECEIVE BUF !!! */
716
717                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
718
719                         jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
720                                 "jsm_input. %d real_raw len:%d calling receive_buf for board %d\n",
721                                 __LINE__, len, ch->ch_bd->boardnum);
722                         tp->ldisc.receive_buf(tp, ch->ch_bd->flipbuf, NULL, len);
723
724                         /* Allow use of channel flip buffer again */
725                         spin_lock_irqsave(&ch->ch_lock, lock_flags);
726                         ch->ch_flags &= ~CH_FLIPBUF_IN_USE;
727                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
728
729                 } else {
730                         ch->ch_r_tail = tail & rmask;
731                         ch->ch_e_tail = tail & rmask;
732
733                         jsm_check_queue_flow_control(ch);
734
735                         /* !!! WE *MUST* LET GO OF ALL LOCKS BEFORE CALLING RECEIVE BUF !!! */
736                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
737
738                         jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
739                                 "jsm_input. %d not real_raw len:%d calling receive_buf for board %d\n",
740                                 __LINE__, len, ch->ch_bd->boardnum);
741
742                         tp->ldisc.receive_buf(tp, tp->flip.char_buf, tp->flip.flag_buf, len);
743                 }
744         } else {
745                 ch->ch_r_tail = tail & rmask;
746                 ch->ch_e_tail = tail & rmask;
747
748                 jsm_check_queue_flow_control(ch);
749
750                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
751
752                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
753                         "jsm_input. %d not jsm_read raw okay scheduling flip\n", __LINE__);
754                 tty_schedule_flip(tp);
755         }
756
757         jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
758 }
759
760 static void jsm_carrier(struct jsm_channel *ch)
761 {
762         struct jsm_board *bd;
763
764         int virt_carrier = 0;
765         int phys_carrier = 0;
766
767         jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev, "start\n");
768         if (!ch)
769                 return;
770
771         bd = ch->ch_bd;
772
773         if (!bd)
774                 return;
775
776         if (ch->ch_mistat & UART_MSR_DCD) {
777                 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
778                         "mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
779                 phys_carrier = 1;
780         }
781
782         if (ch->ch_c_cflag & CLOCAL)
783                 virt_carrier = 1;
784
785         jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
786                 "DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier);
787
788         /*
789          * Test for a VIRTUAL carrier transition to HIGH.
790          */
791         if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
792
793                 /*
794                  * When carrier rises, wake any threads waiting
795                  * for carrier in the open routine.
796                  */
797
798                 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
799                         "carrier: virt DCD rose\n");
800
801                 if (waitqueue_active(&(ch->ch_flags_wait)))
802                         wake_up_interruptible(&ch->ch_flags_wait);
803         }
804
805         /*
806          * Test for a PHYSICAL carrier transition to HIGH.
807          */
808         if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
809
810                 /*
811                  * When carrier rises, wake any threads waiting
812                  * for carrier in the open routine.
813                  */
814
815                 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
816                         "carrier: physical DCD rose\n");
817
818                 if (waitqueue_active(&(ch->ch_flags_wait)))
819                         wake_up_interruptible(&ch->ch_flags_wait);
820         }
821
822         /*
823          *  Test for a PHYSICAL transition to low, so long as we aren't
824          *  currently ignoring physical transitions (which is what "virtual
825          *  carrier" indicates).
826          *
827          *  The transition of the virtual carrier to low really doesn't
828          *  matter... it really only means "ignore carrier state", not
829          *  "make pretend that carrier is there".
830          */
831         if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
832                         && (phys_carrier == 0)) {
833                 /*
834                  *      When carrier drops:
835                  *
836                  *      Drop carrier on all open units.
837                  *
838                  *      Flush queues, waking up any task waiting in the
839                  *      line discipline.
840                  *
841                  *      Send a hangup to the control terminal.
842                  *
843                  *      Enable all select calls.
844                  */
845                 if (waitqueue_active(&(ch->ch_flags_wait)))
846                         wake_up_interruptible(&ch->ch_flags_wait);
847         }
848
849         /*
850          *  Make sure that our cached values reflect the current reality.
851          */
852         if (virt_carrier == 1)
853                 ch->ch_flags |= CH_FCAR;
854         else
855                 ch->ch_flags &= ~CH_FCAR;
856
857         if (phys_carrier == 1)
858                 ch->ch_flags |= CH_CD;
859         else
860                 ch->ch_flags &= ~CH_CD;
861 }
862
863
864 void jsm_check_queue_flow_control(struct jsm_channel *ch)
865 {
866         int qleft = 0;
867
868         /* Store how much space we have left in the queue */
869         if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
870                 qleft += RQUEUEMASK + 1;
871
872         /*
873          * Check to see if we should enforce flow control on our queue because
874          * the ld (or user) isn't reading data out of our queue fast enuf.
875          *
876          * NOTE: This is done based on what the current flow control of the
877          * port is set for.
878          *
879          * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
880          *      This will cause the UART's FIFO to back up, and force
881          *      the RTS signal to be dropped.
882          * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
883          *      the other side, in hopes it will stop sending data to us.
884          * 3) NONE - Nothing we can do.  We will simply drop any extra data
885          *      that gets sent into us when the queue fills up.
886          */
887         if (qleft < 256) {
888                 /* HWFLOW */
889                 if (ch->ch_c_cflag & CRTSCTS) {
890                         if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
891                                 ch->ch_bd->bd_ops->disable_receiver(ch);
892                                 ch->ch_flags |= (CH_RECEIVER_OFF);
893                                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
894                                         "Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
895                                         qleft);
896                         }
897                 }
898                 /* SWFLOW */
899                 else if (ch->ch_c_iflag & IXOFF) {
900                         if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
901                                 ch->ch_bd->bd_ops->send_stop_character(ch);
902                                 ch->ch_stops_sent++;
903                                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
904                                         "Sending stop char! Times sent: %x\n", ch->ch_stops_sent);
905                         }
906                 }
907         }
908
909         /*
910          * Check to see if we should unenforce flow control because
911          * ld (or user) finally read enuf data out of our queue.
912          *
913          * NOTE: This is done based on what the current flow control of the
914          * port is set for.
915          *
916          * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
917          *      This will cause the UART's FIFO to raise RTS back up,
918          *      which will allow the other side to start sending data again.
919          * 2) SWFLOW (IXOFF) - Send a start character to
920          *      the other side, so it will start sending data to us again.
921          * 3) NONE - Do nothing. Since we didn't do anything to turn off the
922          *      other side, we don't need to do anything now.
923          */
924         if (qleft > (RQUEUESIZE / 2)) {
925                 /* HWFLOW */
926                 if (ch->ch_c_cflag & CRTSCTS) {
927                         if (ch->ch_flags & CH_RECEIVER_OFF) {
928                                 ch->ch_bd->bd_ops->enable_receiver(ch);
929                                 ch->ch_flags &= ~(CH_RECEIVER_OFF);
930                                 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
931                                         "Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
932                                         qleft);
933                         }
934                 }
935                 /* SWFLOW */
936                 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
937                         ch->ch_stops_sent = 0;
938                         ch->ch_bd->bd_ops->send_start_character(ch);
939                         jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "Sending start char!\n");
940                 }
941         }
942 }
943
944 /*
945  * jsm_tty_write()
946  *
947  * Take data from the user or kernel and send it out to the FEP.
948  * In here exists all the Transparent Print magic as well.
949  */
950 int jsm_tty_write(struct uart_port *port)
951 {
952         int bufcount = 0, n = 0;
953         int data_count = 0,data_count1 =0;
954         u16 head;
955         u16 tail;
956         u16 tmask;
957         u32 remain;
958         int temp_tail = port->info->xmit.tail;
959         struct jsm_channel *channel = (struct jsm_channel *)port;
960
961         tmask = WQUEUEMASK;
962         head = (channel->ch_w_head) & tmask;
963         tail = (channel->ch_w_tail) & tmask;
964
965         if ((bufcount = tail - head - 1) < 0)
966                 bufcount += WQUEUESIZE;
967
968         n = bufcount;
969
970         n = min(n, 56);
971         remain = WQUEUESIZE - head;
972
973         data_count = 0;
974         if (n >= remain) {
975                 n -= remain;
976                 while ((port->info->xmit.head != temp_tail) &&
977                 (data_count < remain)) {
978                         channel->ch_wqueue[head++] =
979                         port->info->xmit.buf[temp_tail];
980
981                         temp_tail++;
982                         temp_tail &= (UART_XMIT_SIZE - 1);
983                         data_count++;
984                 }
985                 if (data_count == remain) head = 0;
986         }
987
988         data_count1 = 0;
989         if (n > 0) {
990                 remain = n;
991                 while ((port->info->xmit.head != temp_tail) &&
992                         (data_count1 < remain)) {
993                         channel->ch_wqueue[head++] =
994                                 port->info->xmit.buf[temp_tail];
995
996                         temp_tail++;
997                         temp_tail &= (UART_XMIT_SIZE - 1);
998                         data_count1++;
999
1000                 }
1001         }
1002
1003         port->info->xmit.tail = temp_tail;
1004
1005         data_count += data_count1;
1006         if (data_count) {
1007                 head &= tmask;
1008                 channel->ch_w_head = head;
1009         }
1010
1011         if (data_count) {
1012                 channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
1013         }
1014
1015         return data_count;
1016 }