1 // SPDX-License-Identifier: GPL-1.0+
2 /* r3964 linediscipline for linux
4 * -----------------------------------------------------------
6 * Philips Automation Projects
8 * -----------------------------------------------------------
13 * Revision 1.10 2001/03/18 13:02:24 dwmw2
14 * Fix timer usage, use spinlocks properly.
16 * Revision 1.9 2001/03/18 12:52:14 dwmw2
17 * Merge changes in 2.4.2
19 * Revision 1.8 2000/03/23 14:14:54 dwmw2
20 * Fix race in sleeping in r3964_read()
22 * Revision 1.7 1999/28/08 11:41:50 dwmw2
25 * Revision 1.6 1998/09/30 00:40:40 dwmw2
26 * Fixed compilation on 2.0.x kernels
27 * Updated to newly registered tty-ldisc number 9
29 * Revision 1.5 1998/09/04 21:57:36 dwmw2
30 * Signal handling bug fixes, port to 2.1.x.
32 * Revision 1.4 1998/04/02 20:26:59 lhaag
33 * select, blocking, ...
35 * Revision 1.3 1998/02/12 18:58:43 root
36 * fixed some memory leaks
37 * calculation of checksum characters
39 * Revision 1.2 1998/02/07 13:03:34 root
42 * Revision 1.1 1998/02/06 19:21:03 root
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/types.h>
52 #include <linux/fcntl.h>
53 #include <linux/interrupt.h>
54 #include <linux/ptrace.h>
55 #include <linux/ioport.h>
57 #include <linux/slab.h>
58 #include <linux/tty.h>
59 #include <linux/errno.h>
60 #include <linux/string.h> /* used in new tty drivers */
61 #include <linux/signal.h> /* used in new tty drivers */
62 #include <linux/ioctl.h>
63 #include <linux/n_r3964.h>
64 #include <linux/poll.h>
65 #include <linux/init.h>
66 #include <linux/uaccess.h>
68 /*#define DEBUG_QUEUE*/
70 /* Log successful handshake and protocol operations */
71 /*#define DEBUG_PROTO_S*/
73 /* Log handshake and protocol errors: */
74 /*#define DEBUG_PROTO_E*/
76 /* Log Linediscipline operations (open, close, read, write...): */
77 /*#define DEBUG_LDISC*/
79 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
80 /*#define DEBUG_MODUL*/
82 /* Macro helpers for debug output: */
83 #define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
86 #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
88 #define TRACE_M(fmt, arg...) do {} while (0)
91 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
93 #define TRACE_PS(fmt, arg...) do {} while (0)
96 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
98 #define TRACE_PE(fmt, arg...) do {} while (0)
101 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
103 #define TRACE_L(fmt, arg...) do {} while (0)
106 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
108 #define TRACE_Q(fmt, arg...) do {} while (0)
110 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
111 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
112 static void put_char(struct r3964_info *pInfo, unsigned char ch);
113 static void trigger_transmit(struct r3964_info *pInfo);
114 static void retry_transmit(struct r3964_info *pInfo);
115 static void transmit_block(struct r3964_info *pInfo);
116 static void receive_char(struct r3964_info *pInfo, const unsigned char c);
117 static void receive_error(struct r3964_info *pInfo, const char flag);
118 static void on_timeout(struct timer_list *t);
119 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
120 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
121 unsigned char __user * buf);
122 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
123 int error_code, struct r3964_block_header *pBlock);
124 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
125 struct r3964_client_info *pClient);
126 static void remove_client_block(struct r3964_info *pInfo,
127 struct r3964_client_info *pClient);
129 static int r3964_open(struct tty_struct *tty);
130 static void r3964_close(struct tty_struct *tty);
131 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
132 unsigned char __user * buf, size_t nr);
133 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
134 const unsigned char *buf, size_t nr);
135 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
136 unsigned int cmd, unsigned long arg);
137 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
138 static __poll_t r3964_poll(struct tty_struct *tty, struct file *file,
139 struct poll_table_struct *wait);
140 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
141 char *fp, int count);
143 static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
144 .owner = THIS_MODULE,
145 .magic = TTY_LDISC_MAGIC,
148 .close = r3964_close,
150 .write = r3964_write,
151 .ioctl = r3964_ioctl,
152 .set_termios = r3964_set_termios,
154 .receive_buf = r3964_receive_buf,
157 static void dump_block(const unsigned char *block, unsigned int length)
160 char linebuf[16 * 3 + 1];
162 for (i = 0; i < length; i += 16) {
163 for (j = 0; (j < 16) && (j + i < length); j++) {
164 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
166 linebuf[3 * j] = '\0';
167 TRACE_PS("%s", linebuf);
171 /*************************************************************
172 * Driver initialisation
173 *************************************************************/
175 /*************************************************************
176 * Module support routines
177 *************************************************************/
179 static void __exit r3964_exit(void)
183 TRACE_M("cleanup_module()");
185 status = tty_unregister_ldisc(N_R3964);
188 printk(KERN_ERR "r3964: error unregistering linediscipline: "
191 TRACE_L("linediscipline successfully unregistered");
195 static int __init r3964_init(void)
199 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
202 * Register the tty line discipline
205 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
207 TRACE_L("line discipline %d registered", N_R3964);
208 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
209 tty_ldisc_N_R3964.num);
210 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
211 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
213 printk(KERN_ERR "r3964: error registering line discipline: "
219 module_init(r3964_init);
220 module_exit(r3964_exit);
222 /*************************************************************
223 * Protocol implementation routines
224 *************************************************************/
226 static void add_tx_queue(struct r3964_info *pInfo,
227 struct r3964_block_header *pHeader)
231 spin_lock_irqsave(&pInfo->lock, flags);
233 pHeader->next = NULL;
235 if (pInfo->tx_last == NULL) {
236 pInfo->tx_first = pInfo->tx_last = pHeader;
238 pInfo->tx_last->next = pHeader;
239 pInfo->tx_last = pHeader;
242 spin_unlock_irqrestore(&pInfo->lock, flags);
244 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
245 pHeader, pHeader->length, pInfo->tx_first);
248 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
250 struct r3964_block_header *pHeader;
253 struct r3964_block_header *pDump;
256 pHeader = pInfo->tx_first;
262 printk("r3964: remove_from_tx_queue: %p, length %u - ",
263 pHeader, pHeader->length);
264 for (pDump = pHeader; pDump; pDump = pDump->next)
265 printk("%p ", pDump);
269 if (pHeader->owner) {
271 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
274 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
277 wake_up_interruptible(&pInfo->tty->read_wait);
280 spin_lock_irqsave(&pInfo->lock, flags);
282 pInfo->tx_first = pHeader->next;
283 if (pInfo->tx_first == NULL) {
284 pInfo->tx_last = NULL;
287 spin_unlock_irqrestore(&pInfo->lock, flags);
290 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
292 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
293 pInfo->tx_first, pInfo->tx_last);
296 static void add_rx_queue(struct r3964_info *pInfo,
297 struct r3964_block_header *pHeader)
301 spin_lock_irqsave(&pInfo->lock, flags);
303 pHeader->next = NULL;
305 if (pInfo->rx_last == NULL) {
306 pInfo->rx_first = pInfo->rx_last = pHeader;
308 pInfo->rx_last->next = pHeader;
309 pInfo->rx_last = pHeader;
311 pInfo->blocks_in_rx_queue++;
313 spin_unlock_irqrestore(&pInfo->lock, flags);
315 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
316 pHeader, pHeader->length,
317 pInfo->rx_first, pInfo->blocks_in_rx_queue);
320 static void remove_from_rx_queue(struct r3964_info *pInfo,
321 struct r3964_block_header *pHeader)
324 struct r3964_block_header *pFind;
329 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
330 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
331 TRACE_Q("remove_from_rx_queue: %p, length %u",
332 pHeader, pHeader->length);
334 spin_lock_irqsave(&pInfo->lock, flags);
336 if (pInfo->rx_first == pHeader) {
337 /* Remove the first block in the linked list: */
338 pInfo->rx_first = pHeader->next;
340 if (pInfo->rx_first == NULL) {
341 pInfo->rx_last = NULL;
343 pInfo->blocks_in_rx_queue--;
345 /* Find block to remove: */
346 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
347 if (pFind->next == pHeader) {
349 pFind->next = pHeader->next;
350 pInfo->blocks_in_rx_queue--;
351 if (pFind->next == NULL) {
352 /* Oh, removed the last one! */
353 pInfo->rx_last = pFind;
360 spin_unlock_irqrestore(&pInfo->lock, flags);
363 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
365 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
366 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
369 static void put_char(struct r3964_info *pInfo, unsigned char ch)
371 struct tty_struct *tty = pInfo->tty;
372 /* FIXME: put_char should not be called from an IRQ */
373 tty_put_char(tty, ch);
377 static void flush(struct r3964_info *pInfo)
379 struct tty_struct *tty = pInfo->tty;
381 if (tty == NULL || tty->ops->flush_chars == NULL)
383 tty->ops->flush_chars(tty);
386 static void trigger_transmit(struct r3964_info *pInfo)
390 spin_lock_irqsave(&pInfo->lock, flags);
392 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
393 pInfo->state = R3964_TX_REQUEST;
395 pInfo->flags &= ~R3964_ERROR;
396 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
398 spin_unlock_irqrestore(&pInfo->lock, flags);
400 TRACE_PS("trigger_transmit - sent STX");
402 put_char(pInfo, STX);
407 spin_unlock_irqrestore(&pInfo->lock, flags);
411 static void retry_transmit(struct r3964_info *pInfo)
413 if (pInfo->nRetry < R3964_MAX_RETRIES) {
414 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
416 put_char(pInfo, STX);
418 pInfo->state = R3964_TX_REQUEST;
420 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
422 TRACE_PE("transmission failed after %d retries",
425 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
427 put_char(pInfo, NAK);
429 pInfo->state = R3964_IDLE;
431 trigger_transmit(pInfo);
435 static void transmit_block(struct r3964_info *pInfo)
437 struct tty_struct *tty = pInfo->tty;
438 struct r3964_block_header *pBlock = pInfo->tx_first;
441 if (tty == NULL || pBlock == NULL) {
445 room = tty_write_room(tty);
447 TRACE_PS("transmit_block %p, room %d, length %d",
448 pBlock, room, pBlock->length);
450 while (pInfo->tx_position < pBlock->length) {
454 if (pBlock->data[pInfo->tx_position] == DLE) {
455 /* send additional DLE char: */
456 put_char(pInfo, DLE);
458 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
463 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
464 put_char(pInfo, DLE);
465 put_char(pInfo, ETX);
466 if (pInfo->flags & R3964_BCC) {
467 put_char(pInfo, pInfo->bcc);
469 pInfo->state = R3964_WAIT_FOR_TX_ACK;
470 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
475 static void on_receive_block(struct r3964_info *pInfo)
478 struct r3964_client_info *pClient;
479 struct r3964_block_header *pBlock;
481 length = pInfo->rx_position;
483 /* compare byte checksum characters: */
484 if (pInfo->flags & R3964_BCC) {
485 if (pInfo->bcc != pInfo->last_rx) {
486 TRACE_PE("checksum error - got %x but expected %x",
487 pInfo->last_rx, pInfo->bcc);
488 pInfo->flags |= R3964_CHECKSUM;
492 /* check for errors (parity, overrun,...): */
493 if (pInfo->flags & R3964_ERROR) {
494 TRACE_PE("on_receive_block - transmission failed error %x",
495 pInfo->flags & R3964_ERROR);
497 put_char(pInfo, NAK);
499 if (pInfo->nRetry < R3964_MAX_RETRIES) {
500 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
502 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
504 TRACE_PE("on_receive_block - failed after max retries");
505 pInfo->state = R3964_IDLE;
510 /* received block; submit DLE: */
511 put_char(pInfo, DLE);
513 del_timer_sync(&pInfo->tmr);
514 TRACE_PS(" rx success: got %d chars", length);
516 /* prepare struct r3964_block_header: */
517 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
519 TRACE_M("on_receive_block - kmalloc %p", pBlock);
524 pBlock->length = length;
525 pBlock->data = ((unsigned char *)pBlock) +
526 sizeof(struct r3964_block_header);
529 pBlock->owner = NULL;
531 memcpy(pBlock->data, pInfo->rx_buf, length);
533 /* queue block into rx_queue: */
534 add_rx_queue(pInfo, pBlock);
536 /* notify attached client processes: */
537 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
538 if (pClient->sig_flags & R3964_SIG_DATA) {
539 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
543 wake_up_interruptible(&pInfo->tty->read_wait);
545 pInfo->state = R3964_IDLE;
547 trigger_transmit(pInfo);
550 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
552 switch (pInfo->state) {
553 case R3964_TX_REQUEST:
555 TRACE_PS("TX_REQUEST - got DLE");
557 pInfo->state = R3964_TRANSMITTING;
558 pInfo->tx_position = 0;
560 transmit_block(pInfo);
561 } else if (c == STX) {
562 if (pInfo->nRetry == 0) {
563 TRACE_PE("TX_REQUEST - init conflict");
564 if (pInfo->priority == R3964_SLAVE) {
565 goto start_receiving;
568 TRACE_PE("TX_REQUEST - secondary init "
569 "conflict!? Switching to SLAVE mode "
571 goto start_receiving;
574 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
575 retry_transmit(pInfo);
578 case R3964_TRANSMITTING:
580 TRACE_PE("TRANSMITTING - got NAK");
581 retry_transmit(pInfo);
583 TRACE_PE("TRANSMITTING - got invalid char");
585 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
586 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
589 case R3964_WAIT_FOR_TX_ACK:
591 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
592 remove_from_tx_queue(pInfo, R3964_OK);
594 pInfo->state = R3964_IDLE;
595 trigger_transmit(pInfo);
597 retry_transmit(pInfo);
600 case R3964_WAIT_FOR_RX_REPEAT:
604 /* Prevent rx_queue from overflow: */
605 if (pInfo->blocks_in_rx_queue >=
606 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
607 TRACE_PE("IDLE - got STX but no space in "
609 pInfo->state = R3964_WAIT_FOR_RX_BUF;
610 mod_timer(&pInfo->tmr,
611 jiffies + R3964_TO_NO_BUF);
615 /* Ok, start receiving: */
616 TRACE_PS("IDLE - got STX");
617 pInfo->rx_position = 0;
619 pInfo->flags &= ~R3964_ERROR;
620 pInfo->state = R3964_RECEIVING;
621 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
623 put_char(pInfo, DLE);
628 case R3964_RECEIVING:
629 if (pInfo->rx_position < RX_BUF_SIZE) {
633 if (pInfo->last_rx == DLE) {
637 pInfo->last_rx = DLE;
639 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
640 if (pInfo->flags & R3964_BCC) {
641 pInfo->state = R3964_WAIT_FOR_BCC;
642 mod_timer(&pInfo->tmr,
643 jiffies + R3964_TO_ZVZ);
645 on_receive_block(pInfo);
650 pInfo->rx_buf[pInfo->rx_position++] = c;
651 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
654 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
656 case R3964_WAIT_FOR_BCC:
658 on_receive_block(pInfo);
663 static void receive_error(struct r3964_info *pInfo, const char flag)
669 TRACE_PE("received break");
670 pInfo->flags |= R3964_BREAK;
673 TRACE_PE("parity error");
674 pInfo->flags |= R3964_PARITY;
677 TRACE_PE("frame error");
678 pInfo->flags |= R3964_FRAME;
681 TRACE_PE("frame overrun");
682 pInfo->flags |= R3964_OVERRUN;
685 TRACE_PE("receive_error - unknown flag %d", flag);
686 pInfo->flags |= R3964_UNKNOWN;
691 static void on_timeout(struct timer_list *t)
693 struct r3964_info *pInfo = from_timer(pInfo, t, tmr);
695 switch (pInfo->state) {
696 case R3964_TX_REQUEST:
697 TRACE_PE("TX_REQUEST - timeout");
698 retry_transmit(pInfo);
700 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
701 put_char(pInfo, NAK);
703 retry_transmit(pInfo);
705 case R3964_WAIT_FOR_TX_ACK:
706 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
707 retry_transmit(pInfo);
709 case R3964_WAIT_FOR_RX_BUF:
710 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
711 put_char(pInfo, NAK);
713 pInfo->state = R3964_IDLE;
715 case R3964_RECEIVING:
716 TRACE_PE("RECEIVING - timeout after %d chars",
718 put_char(pInfo, NAK);
720 pInfo->state = R3964_IDLE;
722 case R3964_WAIT_FOR_RX_REPEAT:
723 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
724 pInfo->state = R3964_IDLE;
726 case R3964_WAIT_FOR_BCC:
727 TRACE_PE("WAIT_FOR_BCC - timeout");
728 put_char(pInfo, NAK);
730 pInfo->state = R3964_IDLE;
735 static struct r3964_client_info *findClient(struct r3964_info *pInfo,
738 struct r3964_client_info *pClient;
740 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
741 if (pClient->pid == pid) {
748 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
750 struct r3964_client_info *pClient;
751 struct r3964_client_info **ppClient;
752 struct r3964_message *pMsg;
754 if ((arg & R3964_SIG_ALL) == 0) {
755 /* Remove client from client list */
756 for (ppClient = &pInfo->firstClient; *ppClient;
757 ppClient = &(*ppClient)->next) {
760 if (pClient->pid == pid) {
761 TRACE_PS("removing client %d from client list",
763 *ppClient = pClient->next;
764 while (pClient->msg_count) {
765 pMsg = remove_msg(pInfo, pClient);
768 TRACE_M("enable_signals - msg "
772 put_pid(pClient->pid);
774 TRACE_M("enable_signals - kfree %p", pClient);
780 pClient = findClient(pInfo, pid);
782 /* update signal options */
783 pClient->sig_flags = arg;
785 /* add client to client list */
786 pClient = kmalloc(sizeof(struct r3964_client_info),
788 TRACE_M("enable_signals - kmalloc %p", pClient);
792 TRACE_PS("add client %d to client list", pid_nr(pid));
793 spin_lock_init(&pClient->lock);
794 pClient->sig_flags = arg;
795 pClient->pid = get_pid(pid);
796 pClient->next = pInfo->firstClient;
797 pClient->first_msg = NULL;
798 pClient->last_msg = NULL;
799 pClient->next_block_to_read = NULL;
800 pClient->msg_count = 0;
801 pInfo->firstClient = pClient;
808 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
809 unsigned char __user * buf)
811 struct r3964_client_info *pClient;
812 struct r3964_block_header *block;
818 pClient = findClient(pInfo, pid);
819 if (pClient == NULL) {
823 block = pClient->next_block_to_read;
827 if (copy_to_user(buf, block->data, block->length))
830 remove_client_block(pInfo, pClient);
831 return block->length;
837 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
838 int error_code, struct r3964_block_header *pBlock)
840 struct r3964_message *pMsg;
843 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
846 pMsg = kmalloc(sizeof(struct r3964_message),
847 error_code ? GFP_ATOMIC : GFP_KERNEL);
848 TRACE_M("add_msg - kmalloc %p", pMsg);
853 spin_lock_irqsave(&pClient->lock, flags);
855 pMsg->msg_id = msg_id;
857 pMsg->error_code = error_code;
858 pMsg->block = pBlock;
861 if (pClient->last_msg == NULL) {
862 pClient->first_msg = pClient->last_msg = pMsg;
864 pClient->last_msg->next = pMsg;
865 pClient->last_msg = pMsg;
868 pClient->msg_count++;
870 if (pBlock != NULL) {
873 spin_unlock_irqrestore(&pClient->lock, flags);
875 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
876 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
877 pClient->last_msg->arg++;
878 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
880 msg_id = R3964_MSG_ACK;
882 error_code = R3964_OVERFLOW;
884 TRACE_PE("add_msg - queue OVERFLOW-msg");
885 goto queue_the_message;
888 /* Send SIGIO signal to client process: */
889 if (pClient->sig_flags & R3964_USE_SIGIO) {
890 kill_pid(pClient->pid, SIGIO, 1);
894 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
895 struct r3964_client_info *pClient)
897 struct r3964_message *pMsg = NULL;
900 if (pClient->first_msg) {
901 spin_lock_irqsave(&pClient->lock, flags);
903 pMsg = pClient->first_msg;
904 pClient->first_msg = pMsg->next;
905 if (pClient->first_msg == NULL) {
906 pClient->last_msg = NULL;
909 pClient->msg_count--;
911 remove_client_block(pInfo, pClient);
912 pClient->next_block_to_read = pMsg->block;
914 spin_unlock_irqrestore(&pClient->lock, flags);
919 static void remove_client_block(struct r3964_info *pInfo,
920 struct r3964_client_info *pClient)
922 struct r3964_block_header *block;
924 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
926 block = pClient->next_block_to_read;
929 if (block->locks == 0) {
930 remove_from_rx_queue(pInfo, block);
933 pClient->next_block_to_read = NULL;
936 /*************************************************************
937 * Line discipline routines
938 *************************************************************/
940 static int r3964_open(struct tty_struct *tty)
942 struct r3964_info *pInfo;
945 TRACE_L("tty=%p, PID=%d, disc_data=%p",
946 tty, current->pid, tty->disc_data);
948 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
949 TRACE_M("r3964_open - info kmalloc %p", pInfo);
952 printk(KERN_ERR "r3964: failed to alloc info structure\n");
956 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
957 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
959 if (!pInfo->rx_buf) {
960 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
962 TRACE_M("r3964_open - info kfree %p", pInfo);
966 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
967 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
969 if (!pInfo->tx_buf) {
970 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
971 kfree(pInfo->rx_buf);
972 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
974 TRACE_M("r3964_open - info kfree %p", pInfo);
978 spin_lock_init(&pInfo->lock);
979 mutex_init(&pInfo->read_lock);
981 pInfo->priority = R3964_MASTER;
982 pInfo->rx_first = pInfo->rx_last = NULL;
983 pInfo->tx_first = pInfo->tx_last = NULL;
984 pInfo->rx_position = 0;
985 pInfo->tx_position = 0;
987 pInfo->blocks_in_rx_queue = 0;
988 pInfo->firstClient = NULL;
989 pInfo->state = R3964_IDLE;
990 pInfo->flags = R3964_DEBUG;
993 tty->disc_data = pInfo;
994 tty->receive_room = 65536;
996 timer_setup(&pInfo->tmr, on_timeout, 0);
1001 static void r3964_close(struct tty_struct *tty)
1003 struct r3964_info *pInfo = tty->disc_data;
1004 struct r3964_client_info *pClient, *pNext;
1005 struct r3964_message *pMsg;
1006 struct r3964_block_header *pHeader, *pNextHeader;
1007 unsigned long flags;
1012 * Make sure that our task queue isn't activated. If it
1013 * is, take it out of the linked list.
1015 del_timer_sync(&pInfo->tmr);
1017 /* Remove client-structs and message queues: */
1018 pClient = pInfo->firstClient;
1020 pNext = pClient->next;
1021 while (pClient->msg_count) {
1022 pMsg = remove_msg(pInfo, pClient);
1025 TRACE_M("r3964_close - msg kfree %p", pMsg);
1028 put_pid(pClient->pid);
1030 TRACE_M("r3964_close - client kfree %p", pClient);
1033 /* Remove jobs from tx_queue: */
1034 spin_lock_irqsave(&pInfo->lock, flags);
1035 pHeader = pInfo->tx_first;
1036 pInfo->tx_first = pInfo->tx_last = NULL;
1037 spin_unlock_irqrestore(&pInfo->lock, flags);
1040 pNextHeader = pHeader->next;
1042 pHeader = pNextHeader;
1046 kfree(pInfo->rx_buf);
1047 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1048 kfree(pInfo->tx_buf);
1049 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1051 TRACE_M("r3964_close - info kfree %p", pInfo);
1054 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1055 unsigned char __user * buf, size_t nr)
1057 struct r3964_info *pInfo = tty->disc_data;
1058 struct r3964_client_info *pClient;
1059 struct r3964_message *pMsg;
1060 struct r3964_client_message theMsg;
1066 * Internal serialization of reads.
1068 if (file->f_flags & O_NONBLOCK) {
1069 if (!mutex_trylock(&pInfo->read_lock))
1072 if (mutex_lock_interruptible(&pInfo->read_lock))
1073 return -ERESTARTSYS;
1076 pClient = findClient(pInfo, task_pid(current));
1078 pMsg = remove_msg(pInfo, pClient);
1080 /* no messages available. */
1081 if (file->f_flags & O_NONBLOCK) {
1085 /* block until there is a message: */
1086 wait_event_interruptible(tty->read_wait,
1087 (pMsg = remove_msg(pInfo, pClient)));
1090 /* If we still haven't got a message, we must have been signalled */
1097 /* deliver msg to client process: */
1098 theMsg.msg_id = pMsg->msg_id;
1099 theMsg.arg = pMsg->arg;
1100 theMsg.error_code = pMsg->error_code;
1101 ret = sizeof(struct r3964_client_message);
1104 TRACE_M("r3964_read - msg kfree %p", pMsg);
1106 if (copy_to_user(buf, &theMsg, ret)) {
1111 TRACE_PS("read - return %d", ret);
1116 mutex_unlock(&pInfo->read_lock);
1120 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1121 const unsigned char *data, size_t count)
1123 struct r3964_info *pInfo = tty->disc_data;
1124 struct r3964_block_header *pHeader;
1125 struct r3964_client_info *pClient;
1126 unsigned char *new_data;
1128 TRACE_L("write request, %d characters", count);
1130 * Verify the pointers
1137 * Ensure that the caller does not wish to send too much.
1139 if (count > R3964_MTU) {
1140 if (pInfo->flags & R3964_DEBUG) {
1141 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1142 "packet from %u to mtu %d", count, R3964_MTU);
1147 * Allocate a buffer for the data and copy it from the buffer with header prepended
1149 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1151 TRACE_M("r3964_write - kmalloc %p", new_data);
1152 if (new_data == NULL) {
1153 if (pInfo->flags & R3964_DEBUG) {
1154 printk(KERN_ERR "r3964_write: no memory\n");
1159 pHeader = (struct r3964_block_header *)new_data;
1160 pHeader->data = new_data + sizeof(struct r3964_block_header);
1161 pHeader->length = count;
1163 pHeader->owner = NULL;
1165 pClient = findClient(pInfo, task_pid(current));
1167 pHeader->owner = pClient;
1170 memcpy(pHeader->data, data, count); /* We already verified this */
1172 if (pInfo->flags & R3964_DEBUG) {
1173 dump_block(pHeader->data, count);
1177 * Add buffer to transmit-queue:
1179 add_tx_queue(pInfo, pHeader);
1180 trigger_transmit(pInfo);
1185 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1186 unsigned int cmd, unsigned long arg)
1188 struct r3964_info *pInfo = tty->disc_data;
1192 case R3964_ENABLE_SIGNALS:
1193 return enable_signals(pInfo, task_pid(current), arg);
1194 case R3964_SETPRIORITY:
1195 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1197 pInfo->priority = arg & 0xff;
1201 pInfo->flags |= R3964_BCC;
1203 pInfo->flags &= ~R3964_BCC;
1205 case R3964_READ_TELEGRAM:
1206 return read_telegram(pInfo, task_pid(current),
1207 (unsigned char __user *)arg);
1209 return -ENOIOCTLCMD;
1213 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1215 TRACE_L("set_termios");
1218 /* Called without the kernel lock held - fine */
1219 static __poll_t r3964_poll(struct tty_struct *tty, struct file *file,
1220 struct poll_table_struct *wait)
1222 struct r3964_info *pInfo = tty->disc_data;
1223 struct r3964_client_info *pClient;
1224 struct r3964_message *pMsg = NULL;
1225 unsigned long flags;
1226 __poll_t result = EPOLLOUT;
1230 pClient = findClient(pInfo, task_pid(current));
1232 poll_wait(file, &tty->read_wait, wait);
1233 spin_lock_irqsave(&pInfo->lock, flags);
1234 pMsg = pClient->first_msg;
1235 spin_unlock_irqrestore(&pInfo->lock, flags);
1237 result |= EPOLLIN | EPOLLRDNORM;
1244 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1245 char *fp, int count)
1247 struct r3964_info *pInfo = tty->disc_data;
1248 const unsigned char *p;
1249 char *f, flags = TTY_NORMAL;
1252 for (i = count, p = cp, f = fp; i; i--, p++) {
1255 if (flags == TTY_NORMAL) {
1256 receive_char(pInfo, *p);
1258 receive_error(pInfo, flags);
1264 MODULE_LICENSE("GPL");
1265 MODULE_ALIAS_LDISC(N_R3964);