1 // SPDX-License-Identifier: GPL-2.0+
2 /************************************************************************
3 * Copyright 2003 Digi International (www.digi.com)
5 * Copyright (C) 2004 IBM Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * Contact Information:
18 * Scott H Kilau <Scott_Kilau@digi.com>
19 * Ananda Venkatarman <mansarov@us.ibm.com>
21 * 01/19/06: changed jsm_input routine to use the dynamically allocated
22 * tty_buffer changes. Contributors: Scott Kilau and Ananda V.
23 ***********************************************************************/
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/serial_reg.h>
27 #include <linux/delay.h> /* For udelay */
28 #include <linux/pci.h>
29 #include <linux/slab.h>
33 static DECLARE_BITMAP(linemap, MAXLINES);
35 static void jsm_carrier(struct jsm_channel *ch);
37 static inline int jsm_get_mstat(struct jsm_channel *ch)
42 jsm_dbg(IOCTL, &ch->ch_bd->pci_dev, "start\n");
44 mstat = (ch->ch_mostat | ch->ch_mistat);
48 if (mstat & UART_MCR_DTR)
50 if (mstat & UART_MCR_RTS)
52 if (mstat & UART_MSR_CTS)
54 if (mstat & UART_MSR_DSR)
56 if (mstat & UART_MSR_RI)
58 if (mstat & UART_MSR_DCD)
61 jsm_dbg(IOCTL, &ch->ch_bd->pci_dev, "finish\n");
65 static unsigned int jsm_tty_tx_empty(struct uart_port *port)
71 * Return modem signals to ld.
73 static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
76 struct jsm_channel *channel =
77 container_of(port, struct jsm_channel, uart_port);
79 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "start\n");
81 result = jsm_get_mstat(channel);
86 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "finish\n");
92 * jsm_set_modem_info()
94 * Set modem signals, called by ld.
96 static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
98 struct jsm_channel *channel =
99 container_of(port, struct jsm_channel, uart_port);
101 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "start\n");
103 if (mctrl & TIOCM_RTS)
104 channel->ch_mostat |= UART_MCR_RTS;
106 channel->ch_mostat &= ~UART_MCR_RTS;
108 if (mctrl & TIOCM_DTR)
109 channel->ch_mostat |= UART_MCR_DTR;
111 channel->ch_mostat &= ~UART_MCR_DTR;
113 channel->ch_bd->bd_ops->assert_modem_signals(channel);
115 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "finish\n");
122 * Take data from the user or kernel and send it out to the FEP.
123 * In here exists all the Transparent Print magic as well.
125 static void jsm_tty_write(struct uart_port *port)
127 struct jsm_channel *channel;
129 channel = container_of(port, struct jsm_channel, uart_port);
130 channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
133 static void jsm_tty_start_tx(struct uart_port *port)
135 struct jsm_channel *channel =
136 container_of(port, struct jsm_channel, uart_port);
138 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "start\n");
140 channel->ch_flags &= ~(CH_STOP);
143 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "finish\n");
146 static void jsm_tty_stop_tx(struct uart_port *port)
148 struct jsm_channel *channel =
149 container_of(port, struct jsm_channel, uart_port);
151 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "start\n");
153 channel->ch_flags |= (CH_STOP);
155 jsm_dbg(IOCTL, &channel->ch_bd->pci_dev, "finish\n");
158 static void jsm_tty_send_xchar(struct uart_port *port, char ch)
160 unsigned long lock_flags;
161 struct jsm_channel *channel =
162 container_of(port, struct jsm_channel, uart_port);
163 struct ktermios *termios;
165 spin_lock_irqsave(&port->lock, lock_flags);
166 termios = &port->state->port.tty->termios;
167 if (ch == termios->c_cc[VSTART])
168 channel->ch_bd->bd_ops->send_start_character(channel);
170 if (ch == termios->c_cc[VSTOP])
171 channel->ch_bd->bd_ops->send_stop_character(channel);
172 spin_unlock_irqrestore(&port->lock, lock_flags);
175 static void jsm_tty_stop_rx(struct uart_port *port)
177 struct jsm_channel *channel =
178 container_of(port, struct jsm_channel, uart_port);
180 channel->ch_bd->bd_ops->disable_receiver(channel);
183 static void jsm_tty_break(struct uart_port *port, int break_state)
185 unsigned long lock_flags;
186 struct jsm_channel *channel =
187 container_of(port, struct jsm_channel, uart_port);
189 spin_lock_irqsave(&port->lock, lock_flags);
190 if (break_state == -1)
191 channel->ch_bd->bd_ops->send_break(channel);
193 channel->ch_bd->bd_ops->clear_break(channel);
195 spin_unlock_irqrestore(&port->lock, lock_flags);
198 static int jsm_tty_open(struct uart_port *port)
200 struct jsm_board *brd;
201 struct jsm_channel *channel =
202 container_of(port, struct jsm_channel, uart_port);
203 struct ktermios *termios;
205 /* Get board pointer from our array of majors we have allocated */
206 brd = channel->ch_bd;
209 * Allocate channel buffers for read/write/error.
210 * Set flag, so we don't get trounced on.
212 channel->ch_flags |= (CH_OPENING);
214 /* Drop locks, as malloc with GFP_KERNEL can sleep */
216 if (!channel->ch_rqueue) {
217 channel->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
218 if (!channel->ch_rqueue) {
219 jsm_dbg(INIT, &channel->ch_bd->pci_dev,
220 "unable to allocate read queue buf\n");
224 if (!channel->ch_equeue) {
225 channel->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
226 if (!channel->ch_equeue) {
227 jsm_dbg(INIT, &channel->ch_bd->pci_dev,
228 "unable to allocate error queue buf\n");
233 channel->ch_flags &= ~(CH_OPENING);
235 * Initialize if neither terminal is open.
237 jsm_dbg(OPEN, &channel->ch_bd->pci_dev,
238 "jsm_open: initializing channel in open...\n");
241 * Flush input queues.
243 channel->ch_r_head = channel->ch_r_tail = 0;
244 channel->ch_e_head = channel->ch_e_tail = 0;
246 brd->bd_ops->flush_uart_write(channel);
247 brd->bd_ops->flush_uart_read(channel);
249 channel->ch_flags = 0;
250 channel->ch_cached_lsr = 0;
251 channel->ch_stops_sent = 0;
253 termios = &port->state->port.tty->termios;
254 channel->ch_c_cflag = termios->c_cflag;
255 channel->ch_c_iflag = termios->c_iflag;
256 channel->ch_c_oflag = termios->c_oflag;
257 channel->ch_c_lflag = termios->c_lflag;
258 channel->ch_startc = termios->c_cc[VSTART];
259 channel->ch_stopc = termios->c_cc[VSTOP];
261 /* Tell UART to init itself */
262 brd->bd_ops->uart_init(channel);
265 * Run param in case we changed anything
267 brd->bd_ops->param(channel);
269 jsm_carrier(channel);
271 channel->ch_open_count++;
273 jsm_dbg(OPEN, &channel->ch_bd->pci_dev, "finish\n");
277 static void jsm_tty_close(struct uart_port *port)
279 struct jsm_board *bd;
280 struct jsm_channel *channel =
281 container_of(port, struct jsm_channel, uart_port);
283 jsm_dbg(CLOSE, &channel->ch_bd->pci_dev, "start\n");
287 channel->ch_flags &= ~(CH_STOPI);
289 channel->ch_open_count--;
292 * If we have HUPCL set, lower DTR and RTS
294 if (channel->ch_c_cflag & HUPCL) {
295 jsm_dbg(CLOSE, &channel->ch_bd->pci_dev,
296 "Close. HUPCL set, dropping DTR/RTS\n");
299 channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
300 bd->bd_ops->assert_modem_signals(channel);
303 /* Turn off UART interrupts for this port */
304 channel->ch_bd->bd_ops->uart_off(channel);
306 jsm_dbg(CLOSE, &channel->ch_bd->pci_dev, "finish\n");
309 static void jsm_tty_set_termios(struct uart_port *port,
310 struct ktermios *termios,
311 struct ktermios *old_termios)
313 unsigned long lock_flags;
314 struct jsm_channel *channel =
315 container_of(port, struct jsm_channel, uart_port);
317 spin_lock_irqsave(&port->lock, lock_flags);
318 channel->ch_c_cflag = termios->c_cflag;
319 channel->ch_c_iflag = termios->c_iflag;
320 channel->ch_c_oflag = termios->c_oflag;
321 channel->ch_c_lflag = termios->c_lflag;
322 channel->ch_startc = termios->c_cc[VSTART];
323 channel->ch_stopc = termios->c_cc[VSTOP];
325 channel->ch_bd->bd_ops->param(channel);
326 jsm_carrier(channel);
327 spin_unlock_irqrestore(&port->lock, lock_flags);
330 static const char *jsm_tty_type(struct uart_port *port)
335 static void jsm_tty_release_port(struct uart_port *port)
339 static int jsm_tty_request_port(struct uart_port *port)
344 static void jsm_config_port(struct uart_port *port, int flags)
346 port->type = PORT_JSM;
349 static const struct uart_ops jsm_ops = {
350 .tx_empty = jsm_tty_tx_empty,
351 .set_mctrl = jsm_tty_set_mctrl,
352 .get_mctrl = jsm_tty_get_mctrl,
353 .stop_tx = jsm_tty_stop_tx,
354 .start_tx = jsm_tty_start_tx,
355 .send_xchar = jsm_tty_send_xchar,
356 .stop_rx = jsm_tty_stop_rx,
357 .break_ctl = jsm_tty_break,
358 .startup = jsm_tty_open,
359 .shutdown = jsm_tty_close,
360 .set_termios = jsm_tty_set_termios,
361 .type = jsm_tty_type,
362 .release_port = jsm_tty_release_port,
363 .request_port = jsm_tty_request_port,
364 .config_port = jsm_config_port,
370 * Init the tty subsystem. Called once per board after board has been
371 * downloaded and init'ed.
373 int jsm_tty_init(struct jsm_board *brd)
377 struct jsm_channel *ch;
382 jsm_dbg(INIT, &brd->pci_dev, "start\n");
385 * Initialize board structure elements.
388 brd->nasync = brd->maxports;
391 * Allocate channel memory that might not have been allocated
392 * when the driver was first loaded.
394 for (i = 0; i < brd->nasync; i++) {
395 if (!brd->channels[i]) {
398 * Okay to malloc with GFP_KERNEL, we are not at
399 * interrupt context, and there are no locks held.
401 brd->channels[i] = kzalloc(sizeof(struct jsm_channel), GFP_KERNEL);
402 if (!brd->channels[i]) {
403 jsm_dbg(CORE, &brd->pci_dev,
404 "%s:%d Unable to allocate memory for channel struct\n",
410 ch = brd->channels[0];
411 vaddr = brd->re_map_membase;
413 /* Set up channel variables */
414 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
416 if (!brd->channels[i])
419 spin_lock_init(&ch->ch_lock);
421 if (brd->bd_uart_offset == 0x200)
422 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
424 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
429 /* .25 second delay */
430 ch->ch_close_delay = 250;
432 init_waitqueue_head(&ch->ch_flags_wait);
435 jsm_dbg(INIT, &brd->pci_dev, "finish\n");
439 int jsm_uart_port_init(struct jsm_board *brd)
443 struct jsm_channel *ch;
448 jsm_dbg(INIT, &brd->pci_dev, "start\n");
451 * Initialize board structure elements.
454 brd->nasync = brd->maxports;
456 /* Set up channel variables */
457 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
459 if (!brd->channels[i])
462 brd->channels[i]->uart_port.irq = brd->irq;
463 brd->channels[i]->uart_port.uartclk = 14745600;
464 brd->channels[i]->uart_port.type = PORT_JSM;
465 brd->channels[i]->uart_port.iotype = UPIO_MEM;
466 brd->channels[i]->uart_port.membase = brd->re_map_membase;
467 brd->channels[i]->uart_port.fifosize = 16;
468 brd->channels[i]->uart_port.ops = &jsm_ops;
469 line = find_first_zero_bit(linemap, MAXLINES);
470 if (line >= MAXLINES) {
471 printk(KERN_INFO "jsm: linemap is full, added device failed\n");
474 set_bit(line, linemap);
475 brd->channels[i]->uart_port.line = line;
476 rc = uart_add_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
478 printk(KERN_INFO "jsm: Port %d failed. Aborting...\n", i);
481 printk(KERN_INFO "jsm: Port %d added\n", i);
484 jsm_dbg(INIT, &brd->pci_dev, "finish\n");
488 int jsm_remove_uart_port(struct jsm_board *brd)
491 struct jsm_channel *ch;
496 jsm_dbg(INIT, &brd->pci_dev, "start\n");
499 * Initialize board structure elements.
502 brd->nasync = brd->maxports;
504 /* Set up channel variables */
505 for (i = 0; i < brd->nasync; i++) {
507 if (!brd->channels[i])
510 ch = brd->channels[i];
512 clear_bit(ch->uart_port.line, linemap);
513 uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
516 jsm_dbg(INIT, &brd->pci_dev, "finish\n");
520 void jsm_input(struct jsm_channel *ch)
522 struct jsm_board *bd;
523 struct tty_struct *tp;
524 struct tty_port *port;
529 unsigned long lock_flags;
534 jsm_dbg(READ, &ch->ch_bd->pci_dev, "start\n");
539 port = &ch->uart_port.state->port;
546 spin_lock_irqsave(&ch->ch_lock, lock_flags);
549 *Figure the number of characters in the buffer.
550 *Exit immediately if none.
555 head = ch->ch_r_head & rmask;
556 tail = ch->ch_r_tail & rmask;
558 data_len = (head - tail) & rmask;
560 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
564 jsm_dbg(READ, &ch->ch_bd->pci_dev, "start\n");
567 *If the device is not open, or CREAD is off, flush
568 *input data and return immediately.
570 if (!tp || !C_CREAD(tp)) {
572 jsm_dbg(READ, &ch->ch_bd->pci_dev,
573 "input. dropping %d bytes on port %d...\n",
574 data_len, ch->ch_portnum);
575 ch->ch_r_head = tail;
577 /* Force queue flow control to be released, if needed */
578 jsm_check_queue_flow_control(ch);
580 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
585 * If we are throttled, simply don't read any data.
587 if (ch->ch_flags & CH_STOPI) {
588 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
589 jsm_dbg(READ, &ch->ch_bd->pci_dev,
590 "Port %d throttled, not reading any data. head: %x tail: %x\n",
591 ch->ch_portnum, head, tail);
595 jsm_dbg(READ, &ch->ch_bd->pci_dev, "start 2\n");
597 len = tty_buffer_request_room(port, data_len);
600 * len now contains the most amount of data we can copy,
601 * bounded either by the flip buffer size or the amount
602 * of data the card actually has pending...
605 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
612 * If conditions are such that ld needs to see all
613 * UART errors, we will have to walk each character
614 * and error byte and send them to the buffer one at
618 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
619 for (i = 0; i < s; i++) {
621 * Give the Linux ld the flags in the
624 if (*(ch->ch_equeue +tail +i) & UART_LSR_BI)
625 tty_insert_flip_char(port, *(ch->ch_rqueue +tail +i), TTY_BREAK);
626 else if (*(ch->ch_equeue +tail +i) & UART_LSR_PE)
627 tty_insert_flip_char(port, *(ch->ch_rqueue +tail +i), TTY_PARITY);
628 else if (*(ch->ch_equeue +tail +i) & UART_LSR_FE)
629 tty_insert_flip_char(port, *(ch->ch_rqueue +tail +i), TTY_FRAME);
631 tty_insert_flip_char(port, *(ch->ch_rqueue +tail +i), TTY_NORMAL);
634 tty_insert_flip_string(port, ch->ch_rqueue + tail, s);
638 /* Flip queue if needed */
642 ch->ch_r_tail = tail & rmask;
643 ch->ch_e_tail = tail & rmask;
644 jsm_check_queue_flow_control(ch);
645 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
647 /* Tell the tty layer its okay to "eat" the data now */
648 tty_flip_buffer_push(port);
650 jsm_dbg(IOCTL, &ch->ch_bd->pci_dev, "finish\n");
653 static void jsm_carrier(struct jsm_channel *ch)
655 struct jsm_board *bd;
657 int virt_carrier = 0;
658 int phys_carrier = 0;
660 jsm_dbg(CARR, &ch->ch_bd->pci_dev, "start\n");
669 if (ch->ch_mistat & UART_MSR_DCD) {
670 jsm_dbg(CARR, &ch->ch_bd->pci_dev, "mistat: %x D_CD: %x\n",
671 ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
675 if (ch->ch_c_cflag & CLOCAL)
678 jsm_dbg(CARR, &ch->ch_bd->pci_dev, "DCD: physical: %d virt: %d\n",
679 phys_carrier, virt_carrier);
682 * Test for a VIRTUAL carrier transition to HIGH.
684 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
687 * When carrier rises, wake any threads waiting
688 * for carrier in the open routine.
691 jsm_dbg(CARR, &ch->ch_bd->pci_dev, "carrier: virt DCD rose\n");
693 if (waitqueue_active(&(ch->ch_flags_wait)))
694 wake_up_interruptible(&ch->ch_flags_wait);
698 * Test for a PHYSICAL carrier transition to HIGH.
700 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
703 * When carrier rises, wake any threads waiting
704 * for carrier in the open routine.
707 jsm_dbg(CARR, &ch->ch_bd->pci_dev,
708 "carrier: physical DCD rose\n");
710 if (waitqueue_active(&(ch->ch_flags_wait)))
711 wake_up_interruptible(&ch->ch_flags_wait);
715 * Test for a PHYSICAL transition to low, so long as we aren't
716 * currently ignoring physical transitions (which is what "virtual
717 * carrier" indicates).
719 * The transition of the virtual carrier to low really doesn't
720 * matter... it really only means "ignore carrier state", not
721 * "make pretend that carrier is there".
723 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
724 && (phys_carrier == 0)) {
726 * When carrier drops:
728 * Drop carrier on all open units.
730 * Flush queues, waking up any task waiting in the
733 * Send a hangup to the control terminal.
735 * Enable all select calls.
737 if (waitqueue_active(&(ch->ch_flags_wait)))
738 wake_up_interruptible(&ch->ch_flags_wait);
742 * Make sure that our cached values reflect the current reality.
744 if (virt_carrier == 1)
745 ch->ch_flags |= CH_FCAR;
747 ch->ch_flags &= ~CH_FCAR;
749 if (phys_carrier == 1)
750 ch->ch_flags |= CH_CD;
752 ch->ch_flags &= ~CH_CD;
756 void jsm_check_queue_flow_control(struct jsm_channel *ch)
758 struct board_ops *bd_ops = ch->ch_bd->bd_ops;
761 /* Store how much space we have left in the queue */
762 if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
763 qleft += RQUEUEMASK + 1;
766 * Check to see if we should enforce flow control on our queue because
767 * the ld (or user) isn't reading data out of our queue fast enuf.
769 * NOTE: This is done based on what the current flow control of the
772 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
773 * This will cause the UART's FIFO to back up, and force
774 * the RTS signal to be dropped.
775 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
776 * the other side, in hopes it will stop sending data to us.
777 * 3) NONE - Nothing we can do. We will simply drop any extra data
778 * that gets sent into us when the queue fills up.
782 if (ch->ch_c_cflag & CRTSCTS) {
783 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
784 bd_ops->disable_receiver(ch);
785 ch->ch_flags |= (CH_RECEIVER_OFF);
786 jsm_dbg(READ, &ch->ch_bd->pci_dev,
787 "Internal queue hit hilevel mark (%d)! Turning off interrupts\n",
792 else if (ch->ch_c_iflag & IXOFF) {
793 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
794 bd_ops->send_stop_character(ch);
796 jsm_dbg(READ, &ch->ch_bd->pci_dev,
797 "Sending stop char! Times sent: %x\n",
804 * Check to see if we should unenforce flow control because
805 * ld (or user) finally read enuf data out of our queue.
807 * NOTE: This is done based on what the current flow control of the
810 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
811 * This will cause the UART's FIFO to raise RTS back up,
812 * which will allow the other side to start sending data again.
813 * 2) SWFLOW (IXOFF) - Send a start character to
814 * the other side, so it will start sending data to us again.
815 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
816 * other side, we don't need to do anything now.
818 if (qleft > (RQUEUESIZE / 2)) {
820 if (ch->ch_c_cflag & CRTSCTS) {
821 if (ch->ch_flags & CH_RECEIVER_OFF) {
822 bd_ops->enable_receiver(ch);
823 ch->ch_flags &= ~(CH_RECEIVER_OFF);
824 jsm_dbg(READ, &ch->ch_bd->pci_dev,
825 "Internal queue hit lowlevel mark (%d)! Turning on interrupts\n",
830 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
831 ch->ch_stops_sent = 0;
832 bd_ops->send_start_character(ch);
833 jsm_dbg(READ, &ch->ch_bd->pci_dev,
834 "Sending start char!\n");