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>
74 /* Log successful handshake and protocol operations */
75 //#define DEBUG_PROTO_S
77 /* Log handshake and protocol errors: */
78 //#define DEBUG_PROTO_E
80 /* Log Linediscipline operations (open, close, read, write...): */
83 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
86 /* Macro helpers for debug output: */
87 #define TRACE(format, args...) printk("r3964: " format "\n" , ## args);
90 #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args);
92 #define TRACE_M(fmt, arg...) /**/
96 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args);
98 #define TRACE_PS(fmt, arg...) /**/
102 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args);
104 #define TRACE_PE(fmt, arg...) /**/
108 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args);
110 #define TRACE_L(fmt, arg...) /**/
114 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args);
116 #define TRACE_Q(fmt, arg...) /**/
119 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
120 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
121 static void put_char(struct r3964_info *pInfo, unsigned char ch);
122 static void trigger_transmit(struct r3964_info *pInfo);
123 static void retry_transmit(struct r3964_info *pInfo);
124 static void transmit_block(struct r3964_info *pInfo);
125 static void receive_char(struct r3964_info *pInfo, const unsigned char c);
126 static void receive_error(struct r3964_info *pInfo, const char flag);
127 static void on_timeout(unsigned long priv);
128 static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg);
129 static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char *buf);
130 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
131 int error_code, struct r3964_block_header *pBlock);
132 static struct r3964_message* remove_msg(struct r3964_info *pInfo,
133 struct r3964_client_info *pClient);
134 static void remove_client_block(struct r3964_info *pInfo,
135 struct r3964_client_info *pClient);
137 static int r3964_open(struct tty_struct *tty);
138 static void r3964_close(struct tty_struct *tty);
139 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
140 unsigned char *buf, size_t nr);
141 static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
142 const unsigned char * buf, size_t nr);
143 static int r3964_ioctl(struct tty_struct * tty, struct file * file,
144 unsigned int cmd, unsigned long arg);
145 static void r3964_set_termios(struct tty_struct *tty, struct termios * old);
146 static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
147 struct poll_table_struct *wait);
148 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
149 char *fp, int count);
150 static int r3964_receive_room(struct tty_struct *tty);
152 static struct tty_ldisc tty_ldisc_N_R3964 = {
153 TTY_LDISC_MAGIC, /* magic */
157 r3964_open, /* open */
158 r3964_close, /* close */
159 0, /* flush_buffer */
160 0, /* chars_in_buffer */
161 r3964_read, /* read */
162 r3964_write, /* write */
163 r3964_ioctl, /* ioctl */
164 r3964_set_termios, /* set_termios */
165 r3964_poll, /* poll */
166 r3964_receive_buf, /* receive_buf */
167 r3964_receive_room, /* receive_room */
173 static void dump_block(const unsigned char *block, unsigned int length)
176 char linebuf[16*3+1];
178 for(i=0;i<length;i+=16)
180 for(j=0;(j<16) && (j+i<length);j++)
182 sprintf(linebuf+3*j,"%02x ",block[i+j]);
185 TRACE_PS("%s",linebuf);
192 /*************************************************************
193 * Driver initialisation
194 *************************************************************/
197 /*************************************************************
198 * Module support routines
199 *************************************************************/
201 static void __exit r3964_exit(void)
205 TRACE_M ("cleanup_module()");
207 status=tty_register_ldisc(N_R3964, NULL);
211 printk(KERN_ERR "r3964: error unregistering linediscipline: %d\n", status);
215 TRACE_L("linediscipline successfully unregistered");
220 static int __init r3964_init(void)
224 printk ("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
227 * Register the tty line discipline
230 status = tty_register_ldisc (N_R3964, &tty_ldisc_N_R3964);
233 TRACE_L("line discipline %d registered", N_R3964);
234 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
235 tty_ldisc_N_R3964.num);
236 TRACE_L("open=%x", (int)tty_ldisc_N_R3964.open);
237 TRACE_L("tty_ldisc_N_R3964 = %x", (int)&tty_ldisc_N_R3964);
241 printk (KERN_ERR "r3964: error registering line discipline: %d\n", status);
246 module_init(r3964_init);
247 module_exit(r3964_exit);
250 /*************************************************************
251 * Protocol implementation routines
252 *************************************************************/
254 static void add_tx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
258 spin_lock_irqsave(&pInfo->lock, flags);
260 pHeader->next = NULL;
262 if(pInfo->tx_last == NULL)
264 pInfo->tx_first = pInfo->tx_last = pHeader;
268 pInfo->tx_last->next = pHeader;
269 pInfo->tx_last = pHeader;
272 spin_unlock_irqrestore(&pInfo->lock, flags);
274 TRACE_Q("add_tx_queue %x, length %d, tx_first = %x",
275 (int)pHeader, pHeader->length, (int)pInfo->tx_first );
278 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
280 struct r3964_block_header *pHeader;
283 struct r3964_block_header *pDump;
286 pHeader = pInfo->tx_first;
292 printk("r3964: remove_from_tx_queue: %x, length %d - ",
293 (int)pHeader, (int)pHeader->length );
294 for(pDump=pHeader;pDump;pDump=pDump->next)
295 printk("%x ", (int)pDump);
304 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
309 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
312 wake_up_interruptible (&pInfo->read_wait);
315 spin_lock_irqsave(&pInfo->lock, flags);
317 pInfo->tx_first = pHeader->next;
318 if(pInfo->tx_first==NULL)
320 pInfo->tx_last = NULL;
323 spin_unlock_irqrestore(&pInfo->lock, flags);
326 TRACE_M("remove_from_tx_queue - kfree %x",(int)pHeader);
328 TRACE_Q("remove_from_tx_queue: tx_first = %x, tx_last = %x",
329 (int)pInfo->tx_first, (int)pInfo->tx_last );
332 static void add_rx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
336 spin_lock_irqsave(&pInfo->lock, flags);
338 pHeader->next = NULL;
340 if(pInfo->rx_last == NULL)
342 pInfo->rx_first = pInfo->rx_last = pHeader;
346 pInfo->rx_last->next = pHeader;
347 pInfo->rx_last = pHeader;
349 pInfo->blocks_in_rx_queue++;
351 spin_unlock_irqrestore(&pInfo->lock, flags);
353 TRACE_Q("add_rx_queue: %x, length = %d, rx_first = %x, count = %d",
354 (int)pHeader, pHeader->length,
355 (int)pInfo->rx_first, pInfo->blocks_in_rx_queue);
358 static void remove_from_rx_queue(struct r3964_info *pInfo,
359 struct r3964_block_header *pHeader)
362 struct r3964_block_header *pFind;
367 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
368 (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
369 TRACE_Q("remove_from_rx_queue: %x, length %d",
370 (int)pHeader, (int)pHeader->length );
372 spin_lock_irqsave(&pInfo->lock, flags);
374 if(pInfo->rx_first == pHeader)
376 /* Remove the first block in the linked list: */
377 pInfo->rx_first = pHeader->next;
379 if(pInfo->rx_first==NULL)
381 pInfo->rx_last = NULL;
383 pInfo->blocks_in_rx_queue--;
387 /* Find block to remove: */
388 for(pFind=pInfo->rx_first; pFind; pFind=pFind->next)
390 if(pFind->next == pHeader)
393 pFind->next = pHeader->next;
394 pInfo->blocks_in_rx_queue--;
395 if(pFind->next==NULL)
397 /* Oh, removed the last one! */
398 pInfo->rx_last = pFind;
405 spin_unlock_irqrestore(&pInfo->lock, flags);
408 TRACE_M("remove_from_rx_queue - kfree %x",(int)pHeader);
410 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
411 (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
414 static void put_char(struct r3964_info *pInfo, unsigned char ch)
416 struct tty_struct *tty = pInfo->tty;
421 if(tty->driver->put_char)
423 tty->driver->put_char(tty, ch);
428 static void flush(struct r3964_info *pInfo)
430 struct tty_struct *tty = pInfo->tty;
435 if(tty->driver->flush_chars)
437 tty->driver->flush_chars(tty);
441 static void trigger_transmit(struct r3964_info *pInfo)
446 spin_lock_irqsave(&pInfo->lock, flags);
448 if((pInfo->state == R3964_IDLE) && (pInfo->tx_first!=NULL))
450 pInfo->state = R3964_TX_REQUEST;
452 pInfo->flags &= ~R3964_ERROR;
453 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
455 spin_unlock_irqrestore(&pInfo->lock, flags);
457 TRACE_PS("trigger_transmit - sent STX");
459 put_char(pInfo, STX);
466 spin_unlock_irqrestore(&pInfo->lock, flags);
470 static void retry_transmit(struct r3964_info *pInfo)
472 if(pInfo->nRetry<R3964_MAX_RETRIES)
474 TRACE_PE("transmission failed. Retry #%d",
477 put_char(pInfo, STX);
479 pInfo->state = R3964_TX_REQUEST;
481 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
485 TRACE_PE("transmission failed after %d retries",
488 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
490 put_char(pInfo, NAK);
492 pInfo->state = R3964_IDLE;
494 trigger_transmit(pInfo);
499 static void transmit_block(struct r3964_info *pInfo)
501 struct tty_struct *tty = pInfo->tty;
502 struct r3964_block_header *pBlock = pInfo->tx_first;
505 if((tty==NULL) || (pBlock==NULL))
510 if(tty->driver->write_room)
511 room=tty->driver->write_room(tty);
513 TRACE_PS("transmit_block %x, room %d, length %d",
514 (int)pBlock, room, pBlock->length);
516 while(pInfo->tx_position < pBlock->length)
521 if(pBlock->data[pInfo->tx_position]==DLE)
523 /* send additional DLE char: */
524 put_char(pInfo, DLE);
526 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
531 if((pInfo->tx_position == pBlock->length) && (room>=3))
533 put_char(pInfo, DLE);
534 put_char(pInfo, ETX);
535 if(pInfo->flags & R3964_BCC)
537 put_char(pInfo, pInfo->bcc);
539 pInfo->state = R3964_WAIT_FOR_TX_ACK;
540 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
545 static void on_receive_block(struct r3964_info *pInfo)
548 struct r3964_client_info *pClient;
549 struct r3964_block_header *pBlock;
551 length=pInfo->rx_position;
553 /* compare byte checksum characters: */
554 if(pInfo->flags & R3964_BCC)
556 if(pInfo->bcc!=pInfo->last_rx)
558 TRACE_PE("checksum error - got %x but expected %x",
559 pInfo->last_rx, pInfo->bcc);
560 pInfo->flags |= R3964_CHECKSUM;
564 /* check for errors (parity, overrun,...): */
565 if(pInfo->flags & R3964_ERROR)
567 TRACE_PE("on_receive_block - transmission failed error %x",
568 pInfo->flags & R3964_ERROR);
570 put_char(pInfo, NAK);
572 if(pInfo->nRetry<R3964_MAX_RETRIES)
574 pInfo->state=R3964_WAIT_FOR_RX_REPEAT;
576 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
580 TRACE_PE("on_receive_block - failed after max retries");
581 pInfo->state=R3964_IDLE;
587 /* received block; submit DLE: */
588 put_char(pInfo, DLE);
590 del_timer_sync(&pInfo->tmr);
591 TRACE_PS(" rx success: got %d chars", length);
593 /* prepare struct r3964_block_header: */
594 pBlock = kmalloc(length+sizeof(struct r3964_block_header), GFP_KERNEL);
595 TRACE_M("on_receive_block - kmalloc %x",(int)pBlock);
600 pBlock->length = length;
601 pBlock->data = ((unsigned char*)pBlock)+sizeof(struct r3964_block_header);
604 pBlock->owner = NULL;
606 memcpy(pBlock->data, pInfo->rx_buf, length);
608 /* queue block into rx_queue: */
609 add_rx_queue(pInfo, pBlock);
611 /* notify attached client processes: */
612 for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
614 if(pClient->sig_flags & R3964_SIG_DATA)
616 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock);
619 wake_up_interruptible (&pInfo->read_wait);
621 pInfo->state = R3964_IDLE;
623 trigger_transmit(pInfo);
627 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
631 case R3964_TX_REQUEST:
634 TRACE_PS("TX_REQUEST - got DLE");
636 pInfo->state = R3964_TRANSMITTING;
637 pInfo->tx_position = 0;
639 transmit_block(pInfo);
645 TRACE_PE("TX_REQUEST - init conflict");
646 if(pInfo->priority == R3964_SLAVE)
648 goto start_receiving;
653 TRACE_PE("TX_REQUEST - secondary init conflict!?"
654 " Switching to SLAVE mode for next rx.");
655 goto start_receiving;
660 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
661 retry_transmit(pInfo);
664 case R3964_TRANSMITTING:
667 TRACE_PE("TRANSMITTING - got NAK");
668 retry_transmit(pInfo);
672 TRACE_PE("TRANSMITTING - got invalid char");
674 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
675 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
678 case R3964_WAIT_FOR_TX_ACK:
681 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
682 remove_from_tx_queue(pInfo, R3964_OK);
684 pInfo->state = R3964_IDLE;
685 trigger_transmit(pInfo);
689 retry_transmit(pInfo);
692 case R3964_WAIT_FOR_RX_REPEAT:
697 /* Prevent rx_queue from overflow: */
698 if(pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE)
700 TRACE_PE("IDLE - got STX but no space in rx_queue!");
701 pInfo->state=R3964_WAIT_FOR_RX_BUF;
702 mod_timer(&pInfo->tmr, R3964_TO_NO_BUF);
706 /* Ok, start receiving: */
707 TRACE_PS("IDLE - got STX");
708 pInfo->rx_position = 0;
710 pInfo->flags &= ~R3964_ERROR;
711 pInfo->state=R3964_RECEIVING;
712 mod_timer(&pInfo->tmr, R3964_TO_ZVZ);
714 put_char(pInfo, DLE);
719 case R3964_RECEIVING:
720 if(pInfo->rx_position < RX_BUF_SIZE)
726 if(pInfo->last_rx==DLE)
731 pInfo->last_rx = DLE;
734 else if((c==ETX) && (pInfo->last_rx==DLE))
736 if(pInfo->flags & R3964_BCC)
738 pInfo->state = R3964_WAIT_FOR_BCC;
739 mod_timer(&pInfo->tmr, R3964_TO_ZVZ);
743 on_receive_block(pInfo);
750 pInfo->rx_buf[pInfo->rx_position++] = c;
751 mod_timer(&pInfo->tmr, R3964_TO_ZVZ);
754 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
756 case R3964_WAIT_FOR_BCC:
758 on_receive_block(pInfo);
763 static void receive_error(struct r3964_info *pInfo, const char flag)
770 TRACE_PE("received break")
771 pInfo->flags |= R3964_BREAK;
774 TRACE_PE("parity error")
775 pInfo->flags |= R3964_PARITY;
778 TRACE_PE("frame error")
779 pInfo->flags |= R3964_FRAME;
782 TRACE_PE("frame overrun")
783 pInfo->flags |= R3964_OVERRUN;
786 TRACE_PE("receive_error - unknown flag %d", flag);
787 pInfo->flags |= R3964_UNKNOWN;
792 static void on_timeout(unsigned long priv)
794 struct r3964_info *pInfo = (void *)priv;
798 case R3964_TX_REQUEST:
799 TRACE_PE("TX_REQUEST - timeout");
800 retry_transmit(pInfo);
802 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
803 put_char(pInfo, NAK);
805 retry_transmit(pInfo);
807 case R3964_WAIT_FOR_TX_ACK:
808 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
809 retry_transmit(pInfo);
811 case R3964_WAIT_FOR_RX_BUF:
812 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
813 put_char(pInfo, NAK);
815 pInfo->state=R3964_IDLE;
817 case R3964_RECEIVING:
818 TRACE_PE("RECEIVING - timeout after %d chars",
820 put_char(pInfo, NAK);
822 pInfo->state=R3964_IDLE;
824 case R3964_WAIT_FOR_RX_REPEAT:
825 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
826 pInfo->state=R3964_IDLE;
828 case R3964_WAIT_FOR_BCC:
829 TRACE_PE("WAIT_FOR_BCC - timeout");
830 put_char(pInfo, NAK);
832 pInfo->state=R3964_IDLE;
837 static struct r3964_client_info *findClient(
838 struct r3964_info *pInfo, pid_t pid)
840 struct r3964_client_info *pClient;
842 for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
844 if(pClient->pid == pid)
852 static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg)
854 struct r3964_client_info *pClient;
855 struct r3964_client_info **ppClient;
856 struct r3964_message *pMsg;
858 if((arg & R3964_SIG_ALL)==0)
860 /* Remove client from client list */
861 for(ppClient=&pInfo->firstClient; *ppClient; ppClient=&(*ppClient)->next)
865 if(pClient->pid == pid)
867 TRACE_PS("removing client %d from client list", pid);
868 *ppClient = pClient->next;
869 while(pClient->msg_count)
871 pMsg=remove_msg(pInfo, pClient);
875 TRACE_M("enable_signals - msg kfree %x",(int)pMsg);
879 TRACE_M("enable_signals - kfree %x",(int)pClient);
887 pClient=findClient(pInfo, pid);
890 /* update signal options */
891 pClient->sig_flags=arg;
895 /* add client to client list */
896 pClient=kmalloc(sizeof(struct r3964_client_info), GFP_KERNEL);
897 TRACE_M("enable_signals - kmalloc %x",(int)pClient);
901 TRACE_PS("add client %d to client list", pid);
902 spin_lock_init(&pClient->lock);
903 pClient->sig_flags=arg;
905 pClient->next=pInfo->firstClient;
906 pClient->first_msg = NULL;
907 pClient->last_msg = NULL;
908 pClient->next_block_to_read = NULL;
909 pClient->msg_count = 0;
910 pInfo->firstClient=pClient;
917 static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char *buf)
919 struct r3964_client_info *pClient;
920 struct r3964_block_header *block;
927 pClient=findClient(pInfo,pid);
933 block=pClient->next_block_to_read;
940 if (copy_to_user (buf, block->data, block->length))
943 remove_client_block(pInfo, pClient);
944 return block->length;
950 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
951 int error_code, struct r3964_block_header *pBlock)
953 struct r3964_message *pMsg;
956 if(pClient->msg_count<R3964_MAX_MSG_COUNT-1)
960 pMsg = kmalloc(sizeof(struct r3964_message), GFP_KERNEL);
961 TRACE_M("add_msg - kmalloc %x",(int)pMsg);
966 spin_lock_irqsave(&pClient->lock, flags);
968 pMsg->msg_id = msg_id;
970 pMsg->error_code = error_code;
971 pMsg->block = pBlock;
974 if(pClient->last_msg==NULL)
976 pClient->first_msg=pClient->last_msg=pMsg;
980 pClient->last_msg->next = pMsg;
981 pClient->last_msg=pMsg;
984 pClient->msg_count++;
990 spin_unlock_irqrestore(&pClient->lock, flags);
994 if((pClient->last_msg->msg_id == R3964_MSG_ACK)
995 && (pClient->last_msg->error_code==R3964_OVERFLOW))
997 pClient->last_msg->arg++;
998 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
1002 msg_id = R3964_MSG_ACK;
1004 error_code = R3964_OVERFLOW;
1006 TRACE_PE("add_msg - queue OVERFLOW-msg");
1007 goto queue_the_message;
1010 /* Send SIGIO signal to client process: */
1011 if(pClient->sig_flags & R3964_USE_SIGIO)
1013 kill_proc(pClient->pid, SIGIO, 1);
1017 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
1018 struct r3964_client_info *pClient)
1020 struct r3964_message *pMsg=NULL;
1021 unsigned long flags;
1023 if(pClient->first_msg)
1025 spin_lock_irqsave(&pClient->lock, flags);
1027 pMsg = pClient->first_msg;
1028 pClient->first_msg = pMsg->next;
1029 if(pClient->first_msg==NULL)
1031 pClient->last_msg = NULL;
1034 pClient->msg_count--;
1037 remove_client_block(pInfo, pClient);
1038 pClient->next_block_to_read = pMsg->block;
1040 spin_unlock_irqrestore(&pClient->lock, flags);
1045 static void remove_client_block(struct r3964_info *pInfo,
1046 struct r3964_client_info *pClient)
1048 struct r3964_block_header *block;
1050 TRACE_PS("remove_client_block PID %d", pClient->pid);
1052 block=pClient->next_block_to_read;
1058 remove_from_rx_queue(pInfo, block);
1061 pClient->next_block_to_read = NULL;
1065 /*************************************************************
1066 * Line discipline routines
1067 *************************************************************/
1069 static int r3964_open(struct tty_struct *tty)
1071 struct r3964_info *pInfo;
1076 TRACE_L("tty=%x, PID=%d, disc_data=%x",
1077 (int)tty, current->pid, (int)tty->disc_data);
1079 pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
1080 TRACE_M("r3964_open - info kmalloc %x",(int)pInfo);
1084 printk(KERN_ERR "r3964: failed to alloc info structure\n");
1088 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
1089 TRACE_M("r3964_open - rx_buf kmalloc %x",(int)pInfo->rx_buf);
1093 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
1095 TRACE_M("r3964_open - info kfree %x",(int)pInfo);
1099 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
1100 TRACE_M("r3964_open - tx_buf kmalloc %x",(int)pInfo->tx_buf);
1104 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
1105 kfree(pInfo->rx_buf);
1106 TRACE_M("r3964_open - rx_buf kfree %x",(int)pInfo->rx_buf);
1108 TRACE_M("r3964_open - info kfree %x",(int)pInfo);
1112 spin_lock_init(&pInfo->lock);
1114 init_waitqueue_head (&pInfo->read_wait);
1115 pInfo->priority = R3964_MASTER;
1116 pInfo->rx_first = pInfo->rx_last = NULL;
1117 pInfo->tx_first = pInfo->tx_last = NULL;
1118 pInfo->rx_position = 0;
1119 pInfo->tx_position = 0;
1121 pInfo->blocks_in_rx_queue = 0;
1122 pInfo->firstClient=NULL;
1123 pInfo->state=R3964_IDLE;
1124 pInfo->flags = R3964_DEBUG;
1127 tty->disc_data = pInfo;
1129 init_timer(&pInfo->tmr);
1130 pInfo->tmr.data = (unsigned long)pInfo;
1131 pInfo->tmr.function = on_timeout;
1136 static void r3964_close(struct tty_struct *tty)
1138 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1139 struct r3964_client_info *pClient, *pNext;
1140 struct r3964_message *pMsg;
1141 struct r3964_block_header *pHeader, *pNextHeader;
1142 unsigned long flags;
1147 * Make sure that our task queue isn't activated. If it
1148 * is, take it out of the linked list.
1150 del_timer_sync(&pInfo->tmr);
1152 /* Remove client-structs and message queues: */
1153 pClient=pInfo->firstClient;
1156 pNext=pClient->next;
1157 while(pClient->msg_count)
1159 pMsg=remove_msg(pInfo, pClient);
1163 TRACE_M("r3964_close - msg kfree %x",(int)pMsg);
1167 TRACE_M("r3964_close - client kfree %x",(int)pClient);
1170 /* Remove jobs from tx_queue: */
1171 spin_lock_irqsave(&pInfo->lock, flags);
1172 pHeader=pInfo->tx_first;
1173 pInfo->tx_first=pInfo->tx_last=NULL;
1174 spin_unlock_irqrestore(&pInfo->lock, flags);
1178 pNextHeader=pHeader->next;
1180 pHeader=pNextHeader;
1184 wake_up_interruptible(&pInfo->read_wait);
1185 kfree(pInfo->rx_buf);
1186 TRACE_M("r3964_close - rx_buf kfree %x",(int)pInfo->rx_buf);
1187 kfree(pInfo->tx_buf);
1188 TRACE_M("r3964_close - tx_buf kfree %x",(int)pInfo->tx_buf);
1190 TRACE_M("r3964_close - info kfree %x",(int)pInfo);
1195 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1196 unsigned char *buf, size_t nr)
1198 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1199 struct r3964_client_info *pClient;
1200 struct r3964_message *pMsg;
1201 struct r3964_client_message theMsg;
1202 DECLARE_WAITQUEUE (wait, current);
1204 int pid = current->pid;
1209 pClient=findClient(pInfo, pid);
1212 pMsg = remove_msg(pInfo, pClient);
1215 /* no messages available. */
1216 if (file->f_flags & O_NONBLOCK)
1220 /* block until there is a message: */
1221 add_wait_queue(&pInfo->read_wait, &wait);
1223 current->state = TASK_INTERRUPTIBLE;
1224 pMsg = remove_msg(pInfo, pClient);
1225 if (!pMsg && !signal_pending(current))
1230 current->state = TASK_RUNNING;
1231 remove_wait_queue(&pInfo->read_wait, &wait);
1234 /* If we still haven't got a message, we must have been signalled */
1236 if (!pMsg) return -EINTR;
1238 /* deliver msg to client process: */
1239 theMsg.msg_id = pMsg->msg_id;
1240 theMsg.arg = pMsg->arg;
1241 theMsg.error_code = pMsg->error_code;
1242 count = sizeof(struct r3964_client_message);
1245 TRACE_M("r3964_read - msg kfree %x",(int)pMsg);
1247 if (copy_to_user(buf,&theMsg, count))
1250 TRACE_PS("read - return %d", count);
1256 static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
1257 const unsigned char *data, size_t count)
1259 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1260 struct r3964_block_header *pHeader;
1261 struct r3964_client_info *pClient;
1262 unsigned char *new_data;
1266 TRACE_L("write request, %d characters", count);
1268 * Verify the pointers
1274 status = verify_area (VERIFY_READ, data, count);
1281 * Ensure that the caller does not wish to send too much.
1283 if (count > R3964_MTU)
1285 if (pInfo->flags & R3964_DEBUG)
1287 TRACE_L (KERN_WARNING
1288 "r3964_write: truncating user packet "
1289 "from %u to mtu %d", count, R3964_MTU);
1294 * Allocate a buffer for the data and fetch it from the user space.
1296 new_data = kmalloc (count+sizeof(struct r3964_block_header), GFP_KERNEL);
1297 TRACE_M("r3964_write - kmalloc %x",(int)new_data);
1298 if (new_data == NULL) {
1299 if (pInfo->flags & R3964_DEBUG)
1302 "r3964_write: no memory\n");
1307 pHeader = (struct r3964_block_header *)new_data;
1308 pHeader->data = new_data + sizeof(struct r3964_block_header);
1309 pHeader->length = count;
1311 pHeader->owner = NULL;
1315 pClient=findClient(pInfo, pid);
1318 pHeader->owner = pClient;
1321 __copy_from_user(pHeader->data, data, count); /* We already verified this */
1323 if(pInfo->flags & R3964_DEBUG)
1325 dump_block(pHeader->data, count);
1329 * Add buffer to transmit-queue:
1331 add_tx_queue(pInfo, pHeader);
1332 trigger_transmit(pInfo);
1337 static int r3964_ioctl(struct tty_struct * tty, struct file * file,
1338 unsigned int cmd, unsigned long arg)
1340 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1345 case R3964_ENABLE_SIGNALS:
1346 return enable_signals(pInfo, current->pid, arg);
1347 case R3964_SETPRIORITY:
1348 if(arg<R3964_MASTER || arg>R3964_SLAVE)
1350 pInfo->priority = arg & 0xff;
1354 pInfo->flags |= R3964_BCC;
1356 pInfo->flags &= ~R3964_BCC;
1358 case R3964_READ_TELEGRAM:
1359 return read_telegram(pInfo, current->pid, (unsigned char *)arg);
1361 return -ENOIOCTLCMD;
1365 static void r3964_set_termios(struct tty_struct *tty, struct termios * old)
1367 TRACE_L("set_termios");
1370 /* Called without the kernel lock held - fine */
1371 static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
1372 struct poll_table_struct *wait)
1374 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1375 int pid=current->pid;
1376 struct r3964_client_info *pClient;
1377 struct r3964_message *pMsg=NULL;
1378 unsigned long flags;
1379 int result = POLLOUT;
1383 pClient=findClient(pInfo,pid);
1386 poll_wait(file, &pInfo->read_wait, wait);
1387 spin_lock_irqsave(&pInfo->lock, flags);
1388 pMsg=pClient->first_msg;
1389 spin_unlock_irqrestore(&pInfo->lock, flags);
1391 result |= POLLIN | POLLRDNORM;
1400 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1401 char *fp, int count)
1403 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1404 const unsigned char *p;
1408 for (i=count, p = cp, f = fp; i; i--, p++) {
1411 if(flags==TTY_NORMAL)
1413 receive_char(pInfo, *p);
1417 receive_error(pInfo, flags);
1423 static int r3964_receive_room(struct tty_struct *tty)
1425 TRACE_L("receive_room");
1430 MODULE_LICENSE("GPL");