1 /* r3964 linediscipline for linux
3 * -----------------------------------------------------------
5 * Philips Automation Projects
7 * http://www.pap-philips.de
8 * -----------------------------------------------------------
9 * This software may be used and distributed according to the terms of
10 * the GNU General Public License, incorporated herein by reference.
16 * Revision 1.10 2001/03/18 13:02:24 dwmw2
17 * Fix timer usage, use spinlocks properly.
19 * Revision 1.9 2001/03/18 12:52:14 dwmw2
20 * Merge changes in 2.4.2
22 * Revision 1.8 2000/03/23 14:14:54 dwmw2
23 * Fix race in sleeping in r3964_read()
25 * Revision 1.7 1999/28/08 11:41:50 dwmw2
28 * Revision 1.6 1998/09/30 00:40:40 dwmw2
29 * Fixed compilation on 2.0.x kernels
30 * Updated to newly registered tty-ldisc number 9
32 * Revision 1.5 1998/09/04 21:57:36 dwmw2
33 * Signal handling bug fixes, port to 2.1.x.
35 * Revision 1.4 1998/04/02 20:26:59 lhaag
36 * select, blocking, ...
38 * Revision 1.3 1998/02/12 18:58:43 root
39 * fixed some memory leaks
40 * calculation of checksum characters
42 * Revision 1.2 1998/02/07 13:03:34 root
45 * Revision 1.1 1998/02/06 19:21:03 root
51 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/types.h>
55 #include <linux/fcntl.h>
56 #include <linux/interrupt.h>
57 #include <linux/ptrace.h>
58 #include <linux/ioport.h>
60 #include <linux/slab.h>
61 #include <linux/tty.h>
62 #include <linux/errno.h>
63 #include <linux/string.h> /* used in new tty drivers */
64 #include <linux/signal.h> /* used in new tty drivers */
65 #include <linux/ioctl.h>
66 #include <linux/n_r3964.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <asm/uaccess.h>
71 /*#define DEBUG_QUEUE*/
73 /* Log successful handshake and protocol operations */
74 /*#define DEBUG_PROTO_S*/
76 /* Log handshake and protocol errors: */
77 /*#define DEBUG_PROTO_E*/
79 /* Log Linediscipline operations (open, close, read, write...): */
80 /*#define DEBUG_LDISC*/
82 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
83 /*#define DEBUG_MODUL*/
85 /* Macro helpers for debug output: */
86 #define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
89 #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
91 #define TRACE_M(fmt, arg...) do {} while (0)
94 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
96 #define TRACE_PS(fmt, arg...) do {} while (0)
99 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
101 #define TRACE_PE(fmt, arg...) do {} while (0)
104 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
106 #define TRACE_L(fmt, arg...) do {} while (0)
109 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
111 #define TRACE_Q(fmt, arg...) do {} while (0)
113 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
114 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
115 static void put_char(struct r3964_info *pInfo, unsigned char ch);
116 static void trigger_transmit(struct r3964_info *pInfo);
117 static void retry_transmit(struct r3964_info *pInfo);
118 static void transmit_block(struct r3964_info *pInfo);
119 static void receive_char(struct r3964_info *pInfo, const unsigned char c);
120 static void receive_error(struct r3964_info *pInfo, const char flag);
121 static void on_timeout(unsigned long priv);
122 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
123 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
124 unsigned char __user * buf);
125 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
126 int error_code, struct r3964_block_header *pBlock);
127 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
128 struct r3964_client_info *pClient);
129 static void remove_client_block(struct r3964_info *pInfo,
130 struct r3964_client_info *pClient);
132 static int r3964_open(struct tty_struct *tty);
133 static void r3964_close(struct tty_struct *tty);
134 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
135 unsigned char __user * buf, size_t nr);
136 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
137 const unsigned char *buf, size_t nr);
138 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
139 unsigned int cmd, unsigned long arg);
140 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
141 static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
142 struct poll_table_struct *wait);
143 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
144 char *fp, int count);
146 static struct tty_ldisc tty_ldisc_N_R3964 = {
147 .owner = THIS_MODULE,
148 .magic = TTY_LDISC_MAGIC,
151 .close = r3964_close,
153 .write = r3964_write,
154 .ioctl = r3964_ioctl,
155 .set_termios = r3964_set_termios,
157 .receive_buf = r3964_receive_buf,
160 static void dump_block(const unsigned char *block, unsigned int length)
163 char linebuf[16 * 3 + 1];
165 for (i = 0; i < length; i += 16) {
166 for (j = 0; (j < 16) && (j + i < length); j++) {
167 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
169 linebuf[3 * j] = '\0';
170 TRACE_PS("%s", linebuf);
174 /*************************************************************
175 * Driver initialisation
176 *************************************************************/
178 /*************************************************************
179 * Module support routines
180 *************************************************************/
182 static void __exit r3964_exit(void)
186 TRACE_M("cleanup_module()");
188 status = tty_unregister_ldisc(N_R3964);
191 printk(KERN_ERR "r3964: error unregistering linediscipline: "
194 TRACE_L("linediscipline successfully unregistered");
198 static int __init r3964_init(void)
202 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
205 * Register the tty line discipline
208 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
210 TRACE_L("line discipline %d registered", N_R3964);
211 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
212 tty_ldisc_N_R3964.num);
213 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
214 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
216 printk(KERN_ERR "r3964: error registering line discipline: "
222 module_init(r3964_init);
223 module_exit(r3964_exit);
225 /*************************************************************
226 * Protocol implementation routines
227 *************************************************************/
229 static void add_tx_queue(struct r3964_info *pInfo,
230 struct r3964_block_header *pHeader)
234 spin_lock_irqsave(&pInfo->lock, flags);
236 pHeader->next = NULL;
238 if (pInfo->tx_last == NULL) {
239 pInfo->tx_first = pInfo->tx_last = pHeader;
241 pInfo->tx_last->next = pHeader;
242 pInfo->tx_last = pHeader;
245 spin_unlock_irqrestore(&pInfo->lock, flags);
247 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
248 pHeader, pHeader->length, pInfo->tx_first);
251 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
253 struct r3964_block_header *pHeader;
256 struct r3964_block_header *pDump;
259 pHeader = pInfo->tx_first;
265 printk("r3964: remove_from_tx_queue: %p, length %u - ",
266 pHeader, pHeader->length);
267 for (pDump = pHeader; pDump; pDump = pDump->next)
268 printk("%p ", pDump);
272 if (pHeader->owner) {
274 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
277 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
280 wake_up_interruptible(&pInfo->read_wait);
283 spin_lock_irqsave(&pInfo->lock, flags);
285 pInfo->tx_first = pHeader->next;
286 if (pInfo->tx_first == NULL) {
287 pInfo->tx_last = NULL;
290 spin_unlock_irqrestore(&pInfo->lock, flags);
293 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
295 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
296 pInfo->tx_first, pInfo->tx_last);
299 static void add_rx_queue(struct r3964_info *pInfo,
300 struct r3964_block_header *pHeader)
304 spin_lock_irqsave(&pInfo->lock, flags);
306 pHeader->next = NULL;
308 if (pInfo->rx_last == NULL) {
309 pInfo->rx_first = pInfo->rx_last = pHeader;
311 pInfo->rx_last->next = pHeader;
312 pInfo->rx_last = pHeader;
314 pInfo->blocks_in_rx_queue++;
316 spin_unlock_irqrestore(&pInfo->lock, flags);
318 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
319 pHeader, pHeader->length,
320 pInfo->rx_first, pInfo->blocks_in_rx_queue);
323 static void remove_from_rx_queue(struct r3964_info *pInfo,
324 struct r3964_block_header *pHeader)
327 struct r3964_block_header *pFind;
332 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
333 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
334 TRACE_Q("remove_from_rx_queue: %p, length %u",
335 pHeader, pHeader->length);
337 spin_lock_irqsave(&pInfo->lock, flags);
339 if (pInfo->rx_first == pHeader) {
340 /* Remove the first block in the linked list: */
341 pInfo->rx_first = pHeader->next;
343 if (pInfo->rx_first == NULL) {
344 pInfo->rx_last = NULL;
346 pInfo->blocks_in_rx_queue--;
348 /* Find block to remove: */
349 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
350 if (pFind->next == pHeader) {
352 pFind->next = pHeader->next;
353 pInfo->blocks_in_rx_queue--;
354 if (pFind->next == NULL) {
355 /* Oh, removed the last one! */
356 pInfo->rx_last = pFind;
363 spin_unlock_irqrestore(&pInfo->lock, flags);
366 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
368 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
369 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
372 static void put_char(struct r3964_info *pInfo, unsigned char ch)
374 struct tty_struct *tty = pInfo->tty;
379 if (tty->driver->put_char) {
380 tty->driver->put_char(tty, ch);
385 static void flush(struct r3964_info *pInfo)
387 struct tty_struct *tty = pInfo->tty;
392 if (tty->driver->flush_chars) {
393 tty->driver->flush_chars(tty);
397 static void trigger_transmit(struct r3964_info *pInfo)
401 spin_lock_irqsave(&pInfo->lock, flags);
403 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
404 pInfo->state = R3964_TX_REQUEST;
406 pInfo->flags &= ~R3964_ERROR;
407 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
409 spin_unlock_irqrestore(&pInfo->lock, flags);
411 TRACE_PS("trigger_transmit - sent STX");
413 put_char(pInfo, STX);
418 spin_unlock_irqrestore(&pInfo->lock, flags);
422 static void retry_transmit(struct r3964_info *pInfo)
424 if (pInfo->nRetry < R3964_MAX_RETRIES) {
425 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
427 put_char(pInfo, STX);
429 pInfo->state = R3964_TX_REQUEST;
431 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
433 TRACE_PE("transmission failed after %d retries",
436 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
438 put_char(pInfo, NAK);
440 pInfo->state = R3964_IDLE;
442 trigger_transmit(pInfo);
446 static void transmit_block(struct r3964_info *pInfo)
448 struct tty_struct *tty = pInfo->tty;
449 struct r3964_block_header *pBlock = pInfo->tx_first;
452 if ((tty == NULL) || (pBlock == NULL)) {
456 if (tty->driver->write_room)
457 room = tty->driver->write_room(tty);
459 TRACE_PS("transmit_block %p, room %d, length %d",
460 pBlock, room, pBlock->length);
462 while (pInfo->tx_position < pBlock->length) {
466 if (pBlock->data[pInfo->tx_position] == DLE) {
467 /* send additional DLE char: */
468 put_char(pInfo, DLE);
470 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
475 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
476 put_char(pInfo, DLE);
477 put_char(pInfo, ETX);
478 if (pInfo->flags & R3964_BCC) {
479 put_char(pInfo, pInfo->bcc);
481 pInfo->state = R3964_WAIT_FOR_TX_ACK;
482 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
487 static void on_receive_block(struct r3964_info *pInfo)
490 struct r3964_client_info *pClient;
491 struct r3964_block_header *pBlock;
493 length = pInfo->rx_position;
495 /* compare byte checksum characters: */
496 if (pInfo->flags & R3964_BCC) {
497 if (pInfo->bcc != pInfo->last_rx) {
498 TRACE_PE("checksum error - got %x but expected %x",
499 pInfo->last_rx, pInfo->bcc);
500 pInfo->flags |= R3964_CHECKSUM;
504 /* check for errors (parity, overrun,...): */
505 if (pInfo->flags & R3964_ERROR) {
506 TRACE_PE("on_receive_block - transmission failed error %x",
507 pInfo->flags & R3964_ERROR);
509 put_char(pInfo, NAK);
511 if (pInfo->nRetry < R3964_MAX_RETRIES) {
512 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
514 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
516 TRACE_PE("on_receive_block - failed after max retries");
517 pInfo->state = R3964_IDLE;
522 /* received block; submit DLE: */
523 put_char(pInfo, DLE);
525 del_timer_sync(&pInfo->tmr);
526 TRACE_PS(" rx success: got %d chars", length);
528 /* prepare struct r3964_block_header: */
529 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
531 TRACE_M("on_receive_block - kmalloc %p", pBlock);
536 pBlock->length = length;
537 pBlock->data = ((unsigned char *)pBlock) +
538 sizeof(struct r3964_block_header);
541 pBlock->owner = NULL;
543 memcpy(pBlock->data, pInfo->rx_buf, length);
545 /* queue block into rx_queue: */
546 add_rx_queue(pInfo, pBlock);
548 /* notify attached client processes: */
549 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
550 if (pClient->sig_flags & R3964_SIG_DATA) {
551 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
555 wake_up_interruptible(&pInfo->read_wait);
557 pInfo->state = R3964_IDLE;
559 trigger_transmit(pInfo);
562 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
564 switch (pInfo->state) {
565 case R3964_TX_REQUEST:
567 TRACE_PS("TX_REQUEST - got DLE");
569 pInfo->state = R3964_TRANSMITTING;
570 pInfo->tx_position = 0;
572 transmit_block(pInfo);
573 } else if (c == STX) {
574 if (pInfo->nRetry == 0) {
575 TRACE_PE("TX_REQUEST - init conflict");
576 if (pInfo->priority == R3964_SLAVE) {
577 goto start_receiving;
580 TRACE_PE("TX_REQUEST - secondary init "
581 "conflict!? Switching to SLAVE mode "
583 goto start_receiving;
586 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
587 retry_transmit(pInfo);
590 case R3964_TRANSMITTING:
592 TRACE_PE("TRANSMITTING - got NAK");
593 retry_transmit(pInfo);
595 TRACE_PE("TRANSMITTING - got invalid char");
597 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
598 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
601 case R3964_WAIT_FOR_TX_ACK:
603 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
604 remove_from_tx_queue(pInfo, R3964_OK);
606 pInfo->state = R3964_IDLE;
607 trigger_transmit(pInfo);
609 retry_transmit(pInfo);
612 case R3964_WAIT_FOR_RX_REPEAT:
616 /* Prevent rx_queue from overflow: */
617 if (pInfo->blocks_in_rx_queue >=
618 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
619 TRACE_PE("IDLE - got STX but no space in "
621 pInfo->state = R3964_WAIT_FOR_RX_BUF;
622 mod_timer(&pInfo->tmr,
623 jiffies + R3964_TO_NO_BUF);
627 /* Ok, start receiving: */
628 TRACE_PS("IDLE - got STX");
629 pInfo->rx_position = 0;
631 pInfo->flags &= ~R3964_ERROR;
632 pInfo->state = R3964_RECEIVING;
633 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
635 put_char(pInfo, DLE);
640 case R3964_RECEIVING:
641 if (pInfo->rx_position < RX_BUF_SIZE) {
645 if (pInfo->last_rx == DLE) {
649 pInfo->last_rx = DLE;
651 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
652 if (pInfo->flags & R3964_BCC) {
653 pInfo->state = R3964_WAIT_FOR_BCC;
654 mod_timer(&pInfo->tmr,
655 jiffies + R3964_TO_ZVZ);
657 on_receive_block(pInfo);
662 pInfo->rx_buf[pInfo->rx_position++] = c;
663 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
666 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
668 case R3964_WAIT_FOR_BCC:
670 on_receive_block(pInfo);
675 static void receive_error(struct r3964_info *pInfo, const char flag)
681 TRACE_PE("received break");
682 pInfo->flags |= R3964_BREAK;
685 TRACE_PE("parity error");
686 pInfo->flags |= R3964_PARITY;
689 TRACE_PE("frame error");
690 pInfo->flags |= R3964_FRAME;
693 TRACE_PE("frame overrun");
694 pInfo->flags |= R3964_OVERRUN;
697 TRACE_PE("receive_error - unknown flag %d", flag);
698 pInfo->flags |= R3964_UNKNOWN;
703 static void on_timeout(unsigned long priv)
705 struct r3964_info *pInfo = (void *)priv;
707 switch (pInfo->state) {
708 case R3964_TX_REQUEST:
709 TRACE_PE("TX_REQUEST - timeout");
710 retry_transmit(pInfo);
712 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
713 put_char(pInfo, NAK);
715 retry_transmit(pInfo);
717 case R3964_WAIT_FOR_TX_ACK:
718 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
719 retry_transmit(pInfo);
721 case R3964_WAIT_FOR_RX_BUF:
722 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
723 put_char(pInfo, NAK);
725 pInfo->state = R3964_IDLE;
727 case R3964_RECEIVING:
728 TRACE_PE("RECEIVING - timeout after %d chars",
730 put_char(pInfo, NAK);
732 pInfo->state = R3964_IDLE;
734 case R3964_WAIT_FOR_RX_REPEAT:
735 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
736 pInfo->state = R3964_IDLE;
738 case R3964_WAIT_FOR_BCC:
739 TRACE_PE("WAIT_FOR_BCC - timeout");
740 put_char(pInfo, NAK);
742 pInfo->state = R3964_IDLE;
747 static struct r3964_client_info *findClient(struct r3964_info *pInfo,
750 struct r3964_client_info *pClient;
752 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
753 if (pClient->pid == pid) {
760 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
762 struct r3964_client_info *pClient;
763 struct r3964_client_info **ppClient;
764 struct r3964_message *pMsg;
766 if ((arg & R3964_SIG_ALL) == 0) {
767 /* Remove client from client list */
768 for (ppClient = &pInfo->firstClient; *ppClient;
769 ppClient = &(*ppClient)->next) {
772 if (pClient->pid == pid) {
773 TRACE_PS("removing client %d from client list",
775 *ppClient = pClient->next;
776 while (pClient->msg_count) {
777 pMsg = remove_msg(pInfo, pClient);
780 TRACE_M("enable_signals - msg "
784 put_pid(pClient->pid);
786 TRACE_M("enable_signals - kfree %p", pClient);
792 pClient = findClient(pInfo, pid);
794 /* update signal options */
795 pClient->sig_flags = arg;
797 /* add client to client list */
798 pClient = kmalloc(sizeof(struct r3964_client_info),
800 TRACE_M("enable_signals - kmalloc %p", pClient);
804 TRACE_PS("add client %d to client list", pid_nr(pid));
805 spin_lock_init(&pClient->lock);
806 pClient->sig_flags = arg;
807 pClient->pid = get_pid(pid);
808 pClient->next = pInfo->firstClient;
809 pClient->first_msg = NULL;
810 pClient->last_msg = NULL;
811 pClient->next_block_to_read = NULL;
812 pClient->msg_count = 0;
813 pInfo->firstClient = pClient;
820 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
821 unsigned char __user * buf)
823 struct r3964_client_info *pClient;
824 struct r3964_block_header *block;
830 pClient = findClient(pInfo, pid);
831 if (pClient == NULL) {
835 block = pClient->next_block_to_read;
839 if (copy_to_user(buf, block->data, block->length))
842 remove_client_block(pInfo, pClient);
843 return block->length;
849 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
850 int error_code, struct r3964_block_header *pBlock)
852 struct r3964_message *pMsg;
855 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
858 pMsg = kmalloc(sizeof(struct r3964_message),
859 error_code ? GFP_ATOMIC : GFP_KERNEL);
860 TRACE_M("add_msg - kmalloc %p", pMsg);
865 spin_lock_irqsave(&pClient->lock, flags);
867 pMsg->msg_id = msg_id;
869 pMsg->error_code = error_code;
870 pMsg->block = pBlock;
873 if (pClient->last_msg == NULL) {
874 pClient->first_msg = pClient->last_msg = pMsg;
876 pClient->last_msg->next = pMsg;
877 pClient->last_msg = pMsg;
880 pClient->msg_count++;
882 if (pBlock != NULL) {
885 spin_unlock_irqrestore(&pClient->lock, flags);
887 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
888 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
889 pClient->last_msg->arg++;
890 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
892 msg_id = R3964_MSG_ACK;
894 error_code = R3964_OVERFLOW;
896 TRACE_PE("add_msg - queue OVERFLOW-msg");
897 goto queue_the_message;
900 /* Send SIGIO signal to client process: */
901 if (pClient->sig_flags & R3964_USE_SIGIO) {
902 kill_pid(pClient->pid, SIGIO, 1);
906 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
907 struct r3964_client_info *pClient)
909 struct r3964_message *pMsg = NULL;
912 if (pClient->first_msg) {
913 spin_lock_irqsave(&pClient->lock, flags);
915 pMsg = pClient->first_msg;
916 pClient->first_msg = pMsg->next;
917 if (pClient->first_msg == NULL) {
918 pClient->last_msg = NULL;
921 pClient->msg_count--;
923 remove_client_block(pInfo, pClient);
924 pClient->next_block_to_read = pMsg->block;
926 spin_unlock_irqrestore(&pClient->lock, flags);
931 static void remove_client_block(struct r3964_info *pInfo,
932 struct r3964_client_info *pClient)
934 struct r3964_block_header *block;
936 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
938 block = pClient->next_block_to_read;
941 if (block->locks == 0) {
942 remove_from_rx_queue(pInfo, block);
945 pClient->next_block_to_read = NULL;
948 /*************************************************************
949 * Line discipline routines
950 *************************************************************/
952 static int r3964_open(struct tty_struct *tty)
954 struct r3964_info *pInfo;
957 TRACE_L("tty=%p, PID=%d, disc_data=%p",
958 tty, current->pid, tty->disc_data);
960 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
961 TRACE_M("r3964_open - info kmalloc %p", pInfo);
964 printk(KERN_ERR "r3964: failed to alloc info structure\n");
968 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
969 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
971 if (!pInfo->rx_buf) {
972 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
974 TRACE_M("r3964_open - info kfree %p", pInfo);
978 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
979 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
981 if (!pInfo->tx_buf) {
982 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
983 kfree(pInfo->rx_buf);
984 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
986 TRACE_M("r3964_open - info kfree %p", pInfo);
990 spin_lock_init(&pInfo->lock);
992 init_waitqueue_head(&pInfo->read_wait);
993 pInfo->priority = R3964_MASTER;
994 pInfo->rx_first = pInfo->rx_last = NULL;
995 pInfo->tx_first = pInfo->tx_last = NULL;
996 pInfo->rx_position = 0;
997 pInfo->tx_position = 0;
999 pInfo->blocks_in_rx_queue = 0;
1000 pInfo->firstClient = NULL;
1001 pInfo->state = R3964_IDLE;
1002 pInfo->flags = R3964_DEBUG;
1005 tty->disc_data = pInfo;
1006 tty->receive_room = 65536;
1008 setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
1013 static void r3964_close(struct tty_struct *tty)
1015 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1016 struct r3964_client_info *pClient, *pNext;
1017 struct r3964_message *pMsg;
1018 struct r3964_block_header *pHeader, *pNextHeader;
1019 unsigned long flags;
1024 * Make sure that our task queue isn't activated. If it
1025 * is, take it out of the linked list.
1027 del_timer_sync(&pInfo->tmr);
1029 /* Remove client-structs and message queues: */
1030 pClient = pInfo->firstClient;
1032 pNext = pClient->next;
1033 while (pClient->msg_count) {
1034 pMsg = remove_msg(pInfo, pClient);
1037 TRACE_M("r3964_close - msg kfree %p", pMsg);
1040 put_pid(pClient->pid);
1042 TRACE_M("r3964_close - client kfree %p", pClient);
1045 /* Remove jobs from tx_queue: */
1046 spin_lock_irqsave(&pInfo->lock, flags);
1047 pHeader = pInfo->tx_first;
1048 pInfo->tx_first = pInfo->tx_last = NULL;
1049 spin_unlock_irqrestore(&pInfo->lock, flags);
1052 pNextHeader = pHeader->next;
1054 pHeader = pNextHeader;
1058 wake_up_interruptible(&pInfo->read_wait);
1059 kfree(pInfo->rx_buf);
1060 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1061 kfree(pInfo->tx_buf);
1062 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1064 TRACE_M("r3964_close - info kfree %p", pInfo);
1067 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1068 unsigned char __user * buf, size_t nr)
1070 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1071 struct r3964_client_info *pClient;
1072 struct r3964_message *pMsg;
1073 struct r3964_client_message theMsg;
1074 DECLARE_WAITQUEUE(wait, current);
1080 pClient = findClient(pInfo, task_pid(current));
1082 pMsg = remove_msg(pInfo, pClient);
1084 /* no messages available. */
1085 if (file->f_flags & O_NONBLOCK) {
1088 /* block until there is a message: */
1089 add_wait_queue(&pInfo->read_wait, &wait);
1091 __set_current_state(TASK_INTERRUPTIBLE);
1092 pMsg = remove_msg(pInfo, pClient);
1093 if (!pMsg && !signal_pending(current)) {
1097 __set_current_state(TASK_RUNNING);
1098 remove_wait_queue(&pInfo->read_wait, &wait);
1101 /* If we still haven't got a message, we must have been signalled */
1106 /* deliver msg to client process: */
1107 theMsg.msg_id = pMsg->msg_id;
1108 theMsg.arg = pMsg->arg;
1109 theMsg.error_code = pMsg->error_code;
1110 count = sizeof(struct r3964_client_message);
1113 TRACE_M("r3964_read - msg kfree %p", pMsg);
1115 if (copy_to_user(buf, &theMsg, count))
1118 TRACE_PS("read - return %d", count);
1124 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1125 const unsigned char *data, size_t count)
1127 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1128 struct r3964_block_header *pHeader;
1129 struct r3964_client_info *pClient;
1130 unsigned char *new_data;
1132 TRACE_L("write request, %d characters", count);
1134 * Verify the pointers
1141 * Ensure that the caller does not wish to send too much.
1143 if (count > R3964_MTU) {
1144 if (pInfo->flags & R3964_DEBUG) {
1145 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1146 "packet from %u to mtu %d", count, R3964_MTU);
1151 * Allocate a buffer for the data and copy it from the buffer with header prepended
1153 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1155 TRACE_M("r3964_write - kmalloc %p", new_data);
1156 if (new_data == NULL) {
1157 if (pInfo->flags & R3964_DEBUG) {
1158 printk(KERN_ERR "r3964_write: no memory\n");
1163 pHeader = (struct r3964_block_header *)new_data;
1164 pHeader->data = new_data + sizeof(struct r3964_block_header);
1165 pHeader->length = count;
1167 pHeader->owner = NULL;
1169 pClient = findClient(pInfo, task_pid(current));
1171 pHeader->owner = pClient;
1174 memcpy(pHeader->data, data, count); /* We already verified this */
1176 if (pInfo->flags & R3964_DEBUG) {
1177 dump_block(pHeader->data, count);
1181 * Add buffer to transmit-queue:
1183 add_tx_queue(pInfo, pHeader);
1184 trigger_transmit(pInfo);
1189 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1190 unsigned int cmd, unsigned long arg)
1192 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1196 case R3964_ENABLE_SIGNALS:
1197 return enable_signals(pInfo, task_pid(current), arg);
1198 case R3964_SETPRIORITY:
1199 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1201 pInfo->priority = arg & 0xff;
1205 pInfo->flags |= R3964_BCC;
1207 pInfo->flags &= ~R3964_BCC;
1209 case R3964_READ_TELEGRAM:
1210 return read_telegram(pInfo, task_pid(current),
1211 (unsigned char __user *)arg);
1213 return -ENOIOCTLCMD;
1217 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1219 TRACE_L("set_termios");
1222 /* Called without the kernel lock held - fine */
1223 static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1224 struct poll_table_struct *wait)
1226 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1227 struct r3964_client_info *pClient;
1228 struct r3964_message *pMsg = NULL;
1229 unsigned long flags;
1230 int result = POLLOUT;
1234 pClient = findClient(pInfo, task_pid(current));
1236 poll_wait(file, &pInfo->read_wait, wait);
1237 spin_lock_irqsave(&pInfo->lock, flags);
1238 pMsg = pClient->first_msg;
1239 spin_unlock_irqrestore(&pInfo->lock, flags);
1241 result |= POLLIN | POLLRDNORM;
1248 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1249 char *fp, int count)
1251 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1252 const unsigned char *p;
1256 for (i = count, p = cp, f = fp; i; i--, p++) {
1259 if (flags == TTY_NORMAL) {
1260 receive_char(pInfo, *p);
1262 receive_error(pInfo, flags);
1268 MODULE_LICENSE("GPL");
1269 MODULE_ALIAS_LDISC(N_R3964);