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 void *cookie, unsigned char *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);
138 static int r3964_compat_ioctl(struct tty_struct *tty, struct file *file,
139 unsigned int cmd, unsigned long arg);
141 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
142 static __poll_t r3964_poll(struct tty_struct *tty, struct file *file,
143 struct poll_table_struct *wait);
144 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
145 char *fp, int count);
147 static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
148 .owner = THIS_MODULE,
151 .close = r3964_close,
153 .write = r3964_write,
154 .ioctl = r3964_ioctl,
156 .compat_ioctl = r3964_compat_ioctl,
158 .set_termios = r3964_set_termios,
160 .receive_buf = r3964_receive_buf,
163 static void dump_block(const unsigned char *block, unsigned int length)
166 char linebuf[16 * 3 + 1];
168 for (i = 0; i < length; i += 16) {
169 for (j = 0; (j < 16) && (j + i < length); j++) {
170 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
172 linebuf[3 * j] = '\0';
173 TRACE_PS("%s", linebuf);
177 /*************************************************************
178 * Driver initialisation
179 *************************************************************/
181 /*************************************************************
182 * Module support routines
183 *************************************************************/
185 static void __exit r3964_exit(void)
189 TRACE_M("cleanup_module()");
191 status = tty_unregister_ldisc(N_R3964);
194 printk(KERN_ERR "r3964: error unregistering linediscipline: "
197 TRACE_L("linediscipline successfully unregistered");
201 static int __init r3964_init(void)
205 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
208 * Register the tty line discipline
211 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
213 TRACE_L("line discipline %d registered", N_R3964);
214 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
215 tty_ldisc_N_R3964.num);
216 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
217 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
219 printk(KERN_ERR "r3964: error registering line discipline: "
225 module_init(r3964_init);
226 module_exit(r3964_exit);
228 /*************************************************************
229 * Protocol implementation routines
230 *************************************************************/
232 static void add_tx_queue(struct r3964_info *pInfo,
233 struct r3964_block_header *pHeader)
237 spin_lock_irqsave(&pInfo->lock, flags);
239 pHeader->next = NULL;
241 if (pInfo->tx_last == NULL) {
242 pInfo->tx_first = pInfo->tx_last = pHeader;
244 pInfo->tx_last->next = pHeader;
245 pInfo->tx_last = pHeader;
248 spin_unlock_irqrestore(&pInfo->lock, flags);
250 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
251 pHeader, pHeader->length, pInfo->tx_first);
254 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
256 struct r3964_block_header *pHeader;
259 struct r3964_block_header *pDump;
262 pHeader = pInfo->tx_first;
268 printk("r3964: remove_from_tx_queue: %p, length %u - ",
269 pHeader, pHeader->length);
270 for (pDump = pHeader; pDump; pDump = pDump->next)
271 printk("%p ", pDump);
275 if (pHeader->owner) {
277 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
280 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
283 wake_up_interruptible(&pInfo->tty->read_wait);
286 spin_lock_irqsave(&pInfo->lock, flags);
288 pInfo->tx_first = pHeader->next;
289 if (pInfo->tx_first == NULL) {
290 pInfo->tx_last = NULL;
293 spin_unlock_irqrestore(&pInfo->lock, flags);
296 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
298 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
299 pInfo->tx_first, pInfo->tx_last);
302 static void add_rx_queue(struct r3964_info *pInfo,
303 struct r3964_block_header *pHeader)
307 spin_lock_irqsave(&pInfo->lock, flags);
309 pHeader->next = NULL;
311 if (pInfo->rx_last == NULL) {
312 pInfo->rx_first = pInfo->rx_last = pHeader;
314 pInfo->rx_last->next = pHeader;
315 pInfo->rx_last = pHeader;
317 pInfo->blocks_in_rx_queue++;
319 spin_unlock_irqrestore(&pInfo->lock, flags);
321 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
322 pHeader, pHeader->length,
323 pInfo->rx_first, pInfo->blocks_in_rx_queue);
326 static void remove_from_rx_queue(struct r3964_info *pInfo,
327 struct r3964_block_header *pHeader)
330 struct r3964_block_header *pFind;
335 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
336 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
337 TRACE_Q("remove_from_rx_queue: %p, length %u",
338 pHeader, pHeader->length);
340 spin_lock_irqsave(&pInfo->lock, flags);
342 if (pInfo->rx_first == pHeader) {
343 /* Remove the first block in the linked list: */
344 pInfo->rx_first = pHeader->next;
346 if (pInfo->rx_first == NULL) {
347 pInfo->rx_last = NULL;
349 pInfo->blocks_in_rx_queue--;
351 /* Find block to remove: */
352 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
353 if (pFind->next == pHeader) {
355 pFind->next = pHeader->next;
356 pInfo->blocks_in_rx_queue--;
357 if (pFind->next == NULL) {
358 /* Oh, removed the last one! */
359 pInfo->rx_last = pFind;
366 spin_unlock_irqrestore(&pInfo->lock, flags);
369 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
371 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
372 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
375 static void put_char(struct r3964_info *pInfo, unsigned char ch)
377 struct tty_struct *tty = pInfo->tty;
378 /* FIXME: put_char should not be called from an IRQ */
379 tty_put_char(tty, ch);
383 static void flush(struct r3964_info *pInfo)
385 struct tty_struct *tty = pInfo->tty;
387 if (tty == NULL || tty->ops->flush_chars == NULL)
389 tty->ops->flush_chars(tty);
392 static void trigger_transmit(struct r3964_info *pInfo)
396 spin_lock_irqsave(&pInfo->lock, flags);
398 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
399 pInfo->state = R3964_TX_REQUEST;
401 pInfo->flags &= ~R3964_ERROR;
402 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
404 spin_unlock_irqrestore(&pInfo->lock, flags);
406 TRACE_PS("trigger_transmit - sent STX");
408 put_char(pInfo, STX);
413 spin_unlock_irqrestore(&pInfo->lock, flags);
417 static void retry_transmit(struct r3964_info *pInfo)
419 if (pInfo->nRetry < R3964_MAX_RETRIES) {
420 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
422 put_char(pInfo, STX);
424 pInfo->state = R3964_TX_REQUEST;
426 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
428 TRACE_PE("transmission failed after %d retries",
431 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
433 put_char(pInfo, NAK);
435 pInfo->state = R3964_IDLE;
437 trigger_transmit(pInfo);
441 static void transmit_block(struct r3964_info *pInfo)
443 struct tty_struct *tty = pInfo->tty;
444 struct r3964_block_header *pBlock = pInfo->tx_first;
447 if (tty == NULL || pBlock == NULL) {
451 room = tty_write_room(tty);
453 TRACE_PS("transmit_block %p, room %d, length %d",
454 pBlock, room, pBlock->length);
456 while (pInfo->tx_position < pBlock->length) {
460 if (pBlock->data[pInfo->tx_position] == DLE) {
461 /* send additional DLE char: */
462 put_char(pInfo, DLE);
464 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
469 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
470 put_char(pInfo, DLE);
471 put_char(pInfo, ETX);
472 if (pInfo->flags & R3964_BCC) {
473 put_char(pInfo, pInfo->bcc);
475 pInfo->state = R3964_WAIT_FOR_TX_ACK;
476 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
481 static void on_receive_block(struct r3964_info *pInfo)
484 struct r3964_client_info *pClient;
485 struct r3964_block_header *pBlock;
487 length = pInfo->rx_position;
489 /* compare byte checksum characters: */
490 if (pInfo->flags & R3964_BCC) {
491 if (pInfo->bcc != pInfo->last_rx) {
492 TRACE_PE("checksum error - got %x but expected %x",
493 pInfo->last_rx, pInfo->bcc);
494 pInfo->flags |= R3964_CHECKSUM;
498 /* check for errors (parity, overrun,...): */
499 if (pInfo->flags & R3964_ERROR) {
500 TRACE_PE("on_receive_block - transmission failed error %x",
501 pInfo->flags & R3964_ERROR);
503 put_char(pInfo, NAK);
505 if (pInfo->nRetry < R3964_MAX_RETRIES) {
506 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
508 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
510 TRACE_PE("on_receive_block - failed after max retries");
511 pInfo->state = R3964_IDLE;
516 /* received block; submit DLE: */
517 put_char(pInfo, DLE);
519 del_timer_sync(&pInfo->tmr);
520 TRACE_PS(" rx success: got %d chars", length);
522 /* prepare struct r3964_block_header: */
523 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
525 TRACE_M("on_receive_block - kmalloc %p", pBlock);
530 pBlock->length = length;
531 pBlock->data = ((unsigned char *)pBlock) +
532 sizeof(struct r3964_block_header);
535 pBlock->owner = NULL;
537 memcpy(pBlock->data, pInfo->rx_buf, length);
539 /* queue block into rx_queue: */
540 add_rx_queue(pInfo, pBlock);
542 /* notify attached client processes: */
543 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
544 if (pClient->sig_flags & R3964_SIG_DATA) {
545 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
549 wake_up_interruptible(&pInfo->tty->read_wait);
551 pInfo->state = R3964_IDLE;
553 trigger_transmit(pInfo);
556 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
558 switch (pInfo->state) {
559 case R3964_TX_REQUEST:
561 TRACE_PS("TX_REQUEST - got DLE");
563 pInfo->state = R3964_TRANSMITTING;
564 pInfo->tx_position = 0;
566 transmit_block(pInfo);
567 } else if (c == STX) {
568 if (pInfo->nRetry == 0) {
569 TRACE_PE("TX_REQUEST - init conflict");
570 if (pInfo->priority == R3964_SLAVE) {
571 goto start_receiving;
574 TRACE_PE("TX_REQUEST - secondary init "
575 "conflict!? Switching to SLAVE mode "
577 goto start_receiving;
580 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
581 retry_transmit(pInfo);
584 case R3964_TRANSMITTING:
586 TRACE_PE("TRANSMITTING - got NAK");
587 retry_transmit(pInfo);
589 TRACE_PE("TRANSMITTING - got invalid char");
591 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
592 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
595 case R3964_WAIT_FOR_TX_ACK:
597 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
598 remove_from_tx_queue(pInfo, R3964_OK);
600 pInfo->state = R3964_IDLE;
601 trigger_transmit(pInfo);
603 retry_transmit(pInfo);
606 case R3964_WAIT_FOR_RX_REPEAT:
609 /* Prevent rx_queue from overflow: */
610 if (pInfo->blocks_in_rx_queue >=
611 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
612 TRACE_PE("IDLE - got STX but no space in "
614 pInfo->state = R3964_WAIT_FOR_RX_BUF;
615 mod_timer(&pInfo->tmr,
616 jiffies + R3964_TO_NO_BUF);
620 /* Ok, start receiving: */
621 TRACE_PS("IDLE - got STX");
622 pInfo->rx_position = 0;
624 pInfo->flags &= ~R3964_ERROR;
625 pInfo->state = R3964_RECEIVING;
626 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
628 put_char(pInfo, DLE);
633 case R3964_RECEIVING:
634 if (pInfo->rx_position < RX_BUF_SIZE) {
638 if (pInfo->last_rx == DLE) {
642 pInfo->last_rx = DLE;
644 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
645 if (pInfo->flags & R3964_BCC) {
646 pInfo->state = R3964_WAIT_FOR_BCC;
647 mod_timer(&pInfo->tmr,
648 jiffies + R3964_TO_ZVZ);
650 on_receive_block(pInfo);
655 pInfo->rx_buf[pInfo->rx_position++] = c;
656 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
659 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
661 case R3964_WAIT_FOR_BCC:
663 on_receive_block(pInfo);
668 static void receive_error(struct r3964_info *pInfo, const char flag)
674 TRACE_PE("received break");
675 pInfo->flags |= R3964_BREAK;
678 TRACE_PE("parity error");
679 pInfo->flags |= R3964_PARITY;
682 TRACE_PE("frame error");
683 pInfo->flags |= R3964_FRAME;
686 TRACE_PE("frame overrun");
687 pInfo->flags |= R3964_OVERRUN;
690 TRACE_PE("receive_error - unknown flag %d", flag);
691 pInfo->flags |= R3964_UNKNOWN;
696 static void on_timeout(struct timer_list *t)
698 struct r3964_info *pInfo = from_timer(pInfo, t, tmr);
700 switch (pInfo->state) {
701 case R3964_TX_REQUEST:
702 TRACE_PE("TX_REQUEST - timeout");
703 retry_transmit(pInfo);
705 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
706 put_char(pInfo, NAK);
708 retry_transmit(pInfo);
710 case R3964_WAIT_FOR_TX_ACK:
711 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
712 retry_transmit(pInfo);
714 case R3964_WAIT_FOR_RX_BUF:
715 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
716 put_char(pInfo, NAK);
718 pInfo->state = R3964_IDLE;
720 case R3964_RECEIVING:
721 TRACE_PE("RECEIVING - timeout after %d chars",
723 put_char(pInfo, NAK);
725 pInfo->state = R3964_IDLE;
727 case R3964_WAIT_FOR_RX_REPEAT:
728 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
729 pInfo->state = R3964_IDLE;
731 case R3964_WAIT_FOR_BCC:
732 TRACE_PE("WAIT_FOR_BCC - timeout");
733 put_char(pInfo, NAK);
735 pInfo->state = R3964_IDLE;
740 static struct r3964_client_info *findClient(struct r3964_info *pInfo,
743 struct r3964_client_info *pClient;
745 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
746 if (pClient->pid == pid) {
753 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
755 struct r3964_client_info *pClient;
756 struct r3964_client_info **ppClient;
757 struct r3964_message *pMsg;
759 if ((arg & R3964_SIG_ALL) == 0) {
760 /* Remove client from client list */
761 for (ppClient = &pInfo->firstClient; *ppClient;
762 ppClient = &(*ppClient)->next) {
765 if (pClient->pid == pid) {
766 TRACE_PS("removing client %d from client list",
768 *ppClient = pClient->next;
769 while (pClient->msg_count) {
770 pMsg = remove_msg(pInfo, pClient);
773 TRACE_M("enable_signals - msg "
777 put_pid(pClient->pid);
779 TRACE_M("enable_signals - kfree %p", pClient);
785 pClient = findClient(pInfo, pid);
787 /* update signal options */
788 pClient->sig_flags = arg;
790 /* add client to client list */
791 pClient = kmalloc(sizeof(struct r3964_client_info),
793 TRACE_M("enable_signals - kmalloc %p", pClient);
797 TRACE_PS("add client %d to client list", pid_nr(pid));
798 spin_lock_init(&pClient->lock);
799 pClient->sig_flags = arg;
800 pClient->pid = get_pid(pid);
801 pClient->next = pInfo->firstClient;
802 pClient->first_msg = NULL;
803 pClient->last_msg = NULL;
804 pClient->next_block_to_read = NULL;
805 pClient->msg_count = 0;
806 pInfo->firstClient = pClient;
813 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
814 unsigned char __user * buf)
816 struct r3964_client_info *pClient;
817 struct r3964_block_header *block;
823 pClient = findClient(pInfo, pid);
824 if (pClient == NULL) {
828 block = pClient->next_block_to_read;
832 if (copy_to_user(buf, block->data, block->length))
835 remove_client_block(pInfo, pClient);
836 return block->length;
842 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
843 int error_code, struct r3964_block_header *pBlock)
845 struct r3964_message *pMsg;
848 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
851 pMsg = kmalloc(sizeof(struct r3964_message),
852 error_code ? GFP_ATOMIC : GFP_KERNEL);
853 TRACE_M("add_msg - kmalloc %p", pMsg);
858 spin_lock_irqsave(&pClient->lock, flags);
860 pMsg->msg_id = msg_id;
862 pMsg->error_code = error_code;
863 pMsg->block = pBlock;
866 if (pClient->last_msg == NULL) {
867 pClient->first_msg = pClient->last_msg = pMsg;
869 pClient->last_msg->next = pMsg;
870 pClient->last_msg = pMsg;
873 pClient->msg_count++;
875 if (pBlock != NULL) {
878 spin_unlock_irqrestore(&pClient->lock, flags);
880 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
881 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
882 pClient->last_msg->arg++;
883 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
885 msg_id = R3964_MSG_ACK;
887 error_code = R3964_OVERFLOW;
889 TRACE_PE("add_msg - queue OVERFLOW-msg");
890 goto queue_the_message;
893 /* Send SIGIO signal to client process: */
894 if (pClient->sig_flags & R3964_USE_SIGIO) {
895 kill_pid(pClient->pid, SIGIO, 1);
899 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
900 struct r3964_client_info *pClient)
902 struct r3964_message *pMsg = NULL;
905 if (pClient->first_msg) {
906 spin_lock_irqsave(&pClient->lock, flags);
908 pMsg = pClient->first_msg;
909 pClient->first_msg = pMsg->next;
910 if (pClient->first_msg == NULL) {
911 pClient->last_msg = NULL;
914 pClient->msg_count--;
916 remove_client_block(pInfo, pClient);
917 pClient->next_block_to_read = pMsg->block;
919 spin_unlock_irqrestore(&pClient->lock, flags);
924 static void remove_client_block(struct r3964_info *pInfo,
925 struct r3964_client_info *pClient)
927 struct r3964_block_header *block;
929 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
931 block = pClient->next_block_to_read;
934 if (block->locks == 0) {
935 remove_from_rx_queue(pInfo, block);
938 pClient->next_block_to_read = NULL;
941 /*************************************************************
942 * Line discipline routines
943 *************************************************************/
945 static int r3964_open(struct tty_struct *tty)
947 struct r3964_info *pInfo;
950 TRACE_L("tty=%p, PID=%d, disc_data=%p",
951 tty, current->pid, tty->disc_data);
953 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
954 TRACE_M("r3964_open - info kmalloc %p", pInfo);
957 printk(KERN_ERR "r3964: failed to alloc info structure\n");
961 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
962 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
964 if (!pInfo->rx_buf) {
965 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
967 TRACE_M("r3964_open - info kfree %p", pInfo);
971 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
972 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
974 if (!pInfo->tx_buf) {
975 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
976 kfree(pInfo->rx_buf);
977 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
979 TRACE_M("r3964_open - info kfree %p", pInfo);
983 spin_lock_init(&pInfo->lock);
984 mutex_init(&pInfo->read_lock);
986 pInfo->priority = R3964_MASTER;
987 pInfo->rx_first = pInfo->rx_last = NULL;
988 pInfo->tx_first = pInfo->tx_last = NULL;
989 pInfo->rx_position = 0;
990 pInfo->tx_position = 0;
992 pInfo->blocks_in_rx_queue = 0;
993 pInfo->firstClient = NULL;
994 pInfo->state = R3964_IDLE;
995 pInfo->flags = R3964_DEBUG;
998 tty->disc_data = pInfo;
999 tty->receive_room = 65536;
1001 timer_setup(&pInfo->tmr, on_timeout, 0);
1006 static void r3964_close(struct tty_struct *tty)
1008 struct r3964_info *pInfo = tty->disc_data;
1009 struct r3964_client_info *pClient, *pNext;
1010 struct r3964_message *pMsg;
1011 struct r3964_block_header *pHeader, *pNextHeader;
1012 unsigned long flags;
1017 * Make sure that our task queue isn't activated. If it
1018 * is, take it out of the linked list.
1020 del_timer_sync(&pInfo->tmr);
1022 /* Remove client-structs and message queues: */
1023 pClient = pInfo->firstClient;
1025 pNext = pClient->next;
1026 while (pClient->msg_count) {
1027 pMsg = remove_msg(pInfo, pClient);
1030 TRACE_M("r3964_close - msg kfree %p", pMsg);
1033 put_pid(pClient->pid);
1035 TRACE_M("r3964_close - client kfree %p", pClient);
1038 /* Remove jobs from tx_queue: */
1039 spin_lock_irqsave(&pInfo->lock, flags);
1040 pHeader = pInfo->tx_first;
1041 pInfo->tx_first = pInfo->tx_last = NULL;
1042 spin_unlock_irqrestore(&pInfo->lock, flags);
1045 pNextHeader = pHeader->next;
1047 pHeader = pNextHeader;
1051 kfree(pInfo->rx_buf);
1052 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1053 kfree(pInfo->tx_buf);
1054 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1056 TRACE_M("r3964_close - info kfree %p", pInfo);
1059 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1060 unsigned char *kbuf, size_t nr,
1061 void **cookie, unsigned long offset)
1063 struct r3964_info *pInfo = tty->disc_data;
1064 struct r3964_client_info *pClient;
1065 struct r3964_message *pMsg;
1066 struct r3964_client_message theMsg;
1072 * Internal serialization of reads.
1074 if (file->f_flags & O_NONBLOCK) {
1075 if (!mutex_trylock(&pInfo->read_lock))
1078 if (mutex_lock_interruptible(&pInfo->read_lock))
1079 return -ERESTARTSYS;
1082 pClient = findClient(pInfo, task_pid(current));
1084 pMsg = remove_msg(pInfo, pClient);
1086 /* no messages available. */
1087 if (tty_io_nonblock(tty, file)) {
1091 /* block until there is a message: */
1092 wait_event_interruptible(tty->read_wait,
1093 (pMsg = remove_msg(pInfo, pClient)));
1096 /* If we still haven't got a message, we must have been signalled */
1103 /* deliver msg to client process: */
1104 theMsg.msg_id = pMsg->msg_id;
1105 theMsg.arg = pMsg->arg;
1106 theMsg.error_code = pMsg->error_code;
1107 ret = sizeof(struct r3964_client_message);
1110 TRACE_M("r3964_read - msg kfree %p", pMsg);
1112 memcpy(kbuf, &theMsg, ret);
1114 TRACE_PS("read - return %d", ret);
1119 mutex_unlock(&pInfo->read_lock);
1123 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1124 const unsigned char *data, size_t count)
1126 struct r3964_info *pInfo = tty->disc_data;
1127 struct r3964_block_header *pHeader;
1128 struct r3964_client_info *pClient;
1129 unsigned char *new_data;
1131 TRACE_L("write request, %d characters", count);
1133 * Verify the pointers
1140 * Ensure that the caller does not wish to send too much.
1142 if (count > R3964_MTU) {
1143 if (pInfo->flags & R3964_DEBUG) {
1144 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1145 "packet from %u to mtu %d", count, R3964_MTU);
1150 * Allocate a buffer for the data and copy it from the buffer with header prepended
1152 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1154 TRACE_M("r3964_write - kmalloc %p", new_data);
1155 if (new_data == NULL) {
1156 if (pInfo->flags & R3964_DEBUG) {
1157 printk(KERN_ERR "r3964_write: no memory\n");
1162 pHeader = (struct r3964_block_header *)new_data;
1163 pHeader->data = new_data + sizeof(struct r3964_block_header);
1164 pHeader->length = count;
1166 pHeader->owner = NULL;
1168 pClient = findClient(pInfo, task_pid(current));
1170 pHeader->owner = pClient;
1173 memcpy(pHeader->data, data, count); /* We already verified this */
1175 if (pInfo->flags & R3964_DEBUG) {
1176 dump_block(pHeader->data, count);
1180 * Add buffer to transmit-queue:
1182 add_tx_queue(pInfo, pHeader);
1183 trigger_transmit(pInfo);
1188 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1189 unsigned int cmd, unsigned long arg)
1191 struct r3964_info *pInfo = tty->disc_data;
1195 case R3964_ENABLE_SIGNALS:
1196 return enable_signals(pInfo, task_pid(current), arg);
1197 case R3964_SETPRIORITY:
1198 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1200 pInfo->priority = arg & 0xff;
1204 pInfo->flags |= R3964_BCC;
1206 pInfo->flags &= ~R3964_BCC;
1208 case R3964_READ_TELEGRAM:
1209 return read_telegram(pInfo, task_pid(current),
1210 (unsigned char __user *)arg);
1212 return -ENOIOCTLCMD;
1216 #ifdef CONFIG_COMPAT
1217 static int r3964_compat_ioctl(struct tty_struct *tty, struct file *file,
1218 unsigned int cmd, unsigned long arg)
1221 case R3964_ENABLE_SIGNALS:
1222 case R3964_SETPRIORITY:
1224 return r3964_ioctl(tty, file, cmd, arg);
1226 return -ENOIOCTLCMD;
1231 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1233 TRACE_L("set_termios");
1236 /* Called without the kernel lock held - fine */
1237 static __poll_t r3964_poll(struct tty_struct *tty, struct file *file,
1238 struct poll_table_struct *wait)
1240 struct r3964_info *pInfo = tty->disc_data;
1241 struct r3964_client_info *pClient;
1242 struct r3964_message *pMsg = NULL;
1243 unsigned long flags;
1244 __poll_t result = EPOLLOUT;
1248 pClient = findClient(pInfo, task_pid(current));
1250 poll_wait(file, &tty->read_wait, wait);
1251 spin_lock_irqsave(&pInfo->lock, flags);
1252 pMsg = pClient->first_msg;
1253 spin_unlock_irqrestore(&pInfo->lock, flags);
1255 result |= EPOLLIN | EPOLLRDNORM;
1262 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1263 char *fp, int count)
1265 struct r3964_info *pInfo = tty->disc_data;
1266 const unsigned char *p;
1267 char *f, flags = TTY_NORMAL;
1270 for (i = count, p = cp, f = fp; i; i--, p++) {
1273 if (flags == TTY_NORMAL) {
1274 receive_char(pInfo, *p);
1276 receive_error(pInfo, flags);
1282 MODULE_LICENSE("GPL");
1283 MODULE_ALIAS_LDISC(N_R3964);