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.1.1.1 2005/04/11 02:50:17 jack
19 * Revision 1.1.1.1 2005/01/10 13:16:18 jack
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 on_timer_1(void*);
120 static void on_timer_2(void*);
121 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
122 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
123 static void put_char(struct r3964_info *pInfo, unsigned char ch);
124 static void trigger_transmit(struct r3964_info *pInfo);
125 static void retry_transmit(struct r3964_info *pInfo);
126 static void transmit_block(struct r3964_info *pInfo);
127 static void receive_char(struct r3964_info *pInfo, const unsigned char c);
128 static void receive_error(struct r3964_info *pInfo, const char flag);
129 static void on_timeout(struct r3964_info *pInfo);
130 static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg);
131 static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char *buf);
132 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
133 int error_code, struct r3964_block_header *pBlock);
134 static struct r3964_message* remove_msg(struct r3964_info *pInfo,
135 struct r3964_client_info *pClient);
136 static void remove_client_block(struct r3964_info *pInfo,
137 struct r3964_client_info *pClient);
139 static int r3964_open(struct tty_struct *tty);
140 static void r3964_close(struct tty_struct *tty);
141 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
142 unsigned char *buf, size_t nr);
143 static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
144 const unsigned char * buf, size_t nr);
145 static int r3964_ioctl(struct tty_struct * tty, struct file * file,
146 unsigned int cmd, unsigned long arg);
147 static void r3964_set_termios(struct tty_struct *tty, struct termios * old);
148 static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
149 struct poll_table_struct *wait);
150 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
151 char *fp, int count);
152 static int r3964_receive_room(struct tty_struct *tty);
154 static struct tty_ldisc tty_ldisc_N_R3964 = {
155 TTY_LDISC_MAGIC, /* magic */
159 r3964_open, /* open */
160 r3964_close, /* close */
161 0, /* flush_buffer */
162 0, /* chars_in_buffer */
163 r3964_read, /* read */
164 r3964_write, /* write */
165 r3964_ioctl, /* ioctl */
166 r3964_set_termios, /* set_termios */
167 r3964_poll, /* poll */
168 r3964_receive_buf, /* receive_buf */
169 r3964_receive_room, /* receive_room */
175 static void dump_block(const unsigned char *block, unsigned int length)
178 char linebuf[16*3+1];
180 for(i=0;i<length;i+=16)
182 for(j=0;(j<16) && (j+i<length);j++)
184 sprintf(linebuf+3*j,"%02x ",block[i+j]);
187 TRACE_PS("%s",linebuf);
194 /*************************************************************
195 * Driver initialisation
196 *************************************************************/
199 /*************************************************************
200 * Module support routines
201 *************************************************************/
203 static void __exit r3964_exit(void)
207 TRACE_M ("cleanup_module()");
209 status=tty_register_ldisc(N_R3964, NULL);
213 printk(KERN_ERR "r3964: error unregistering linediscipline: %d\n", status);
217 TRACE_L("linediscipline successfully unregistered");
222 static int __init r3964_init(void)
226 printk ("r3964: Philips r3964 Driver $Revision: 1.1.1.1 $\n");
229 * Register the tty line discipline
232 status = tty_register_ldisc (N_R3964, &tty_ldisc_N_R3964);
235 TRACE_L("line discipline %d registered", N_R3964);
236 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
237 tty_ldisc_N_R3964.num);
238 TRACE_L("open=%x", (int)tty_ldisc_N_R3964.open);
239 TRACE_L("tty_ldisc_N_R3964 = %x", (int)&tty_ldisc_N_R3964);
243 printk (KERN_ERR "r3964: error registering line discipline: %d\n", status);
248 module_init(r3964_init);
249 module_exit(r3964_exit);
252 /*************************************************************
253 * Protocol implementation routines
254 *************************************************************/
256 static void on_timer_1(void *arg)
258 struct r3964_info *pInfo = (struct r3964_info *)arg;
260 if(pInfo->count_down)
262 if(!--pInfo->count_down)
267 queue_task(&pInfo->bh_2, &tq_timer);
270 static void on_timer_2(void *arg)
272 struct r3964_info *pInfo = (struct r3964_info *)arg;
274 if(pInfo->count_down)
276 if(!--pInfo->count_down)
281 queue_task(&pInfo->bh_1, &tq_timer);
284 static void add_tx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
291 pHeader->next = NULL;
293 if(pInfo->tx_last == NULL)
295 pInfo->tx_first = pInfo->tx_last = pHeader;
299 pInfo->tx_last->next = pHeader;
300 pInfo->tx_last = pHeader;
303 restore_flags(flags);
305 TRACE_Q("add_tx_queue %x, length %d, tx_first = %x",
306 (int)pHeader, pHeader->length, (int)pInfo->tx_first );
309 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
311 struct r3964_block_header *pHeader;
314 struct r3964_block_header *pDump;
317 pHeader = pInfo->tx_first;
323 printk("r3964: remove_from_tx_queue: %x, length %d - ",
324 (int)pHeader, (int)pHeader->length );
325 for(pDump=pHeader;pDump;pDump=pDump->next)
326 printk("%x ", (int)pDump);
335 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
340 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
343 wake_up_interruptible (&pInfo->read_wait);
349 pInfo->tx_first = pHeader->next;
350 if(pInfo->tx_first==NULL)
352 pInfo->tx_last = NULL;
355 restore_flags(flags);
358 TRACE_M("remove_from_tx_queue - kfree %x",(int)pHeader);
360 TRACE_Q("remove_from_tx_queue: tx_first = %x, tx_last = %x",
361 (int)pInfo->tx_first, (int)pInfo->tx_last );
364 static void add_rx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
371 pHeader->next = NULL;
373 if(pInfo->rx_last == NULL)
375 pInfo->rx_first = pInfo->rx_last = pHeader;
379 pInfo->rx_last->next = pHeader;
380 pInfo->rx_last = pHeader;
382 pInfo->blocks_in_rx_queue++;
384 restore_flags(flags);
386 TRACE_Q("add_rx_queue: %x, length = %d, rx_first = %x, count = %d",
387 (int)pHeader, pHeader->length,
388 (int)pInfo->rx_first, pInfo->blocks_in_rx_queue);
391 static void remove_from_rx_queue(struct r3964_info *pInfo,
392 struct r3964_block_header *pHeader)
395 struct r3964_block_header *pFind;
400 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
401 (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
402 TRACE_Q("remove_from_rx_queue: %x, length %d",
403 (int)pHeader, (int)pHeader->length );
408 if(pInfo->rx_first == pHeader)
410 /* Remove the first block in the linked list: */
411 pInfo->rx_first = pHeader->next;
413 if(pInfo->rx_first==NULL)
415 pInfo->rx_last = NULL;
417 pInfo->blocks_in_rx_queue--;
421 /* Find block to remove: */
422 for(pFind=pInfo->rx_first; pFind; pFind=pFind->next)
424 if(pFind->next == pHeader)
427 pFind->next = pHeader->next;
428 pInfo->blocks_in_rx_queue--;
429 if(pFind->next==NULL)
431 /* Oh, removed the last one! */
432 pInfo->rx_last = pFind;
439 restore_flags(flags);
442 TRACE_M("remove_from_rx_queue - kfree %x",(int)pHeader);
444 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
445 (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
448 static void put_char(struct r3964_info *pInfo, unsigned char ch)
450 struct tty_struct *tty = pInfo->tty;
455 if(tty->driver.put_char)
457 tty->driver.put_char(tty, ch);
462 static void flush(struct r3964_info *pInfo)
464 struct tty_struct *tty = pInfo->tty;
469 if(tty->driver.flush_chars)
471 tty->driver.flush_chars(tty);
475 static void trigger_transmit(struct r3964_info *pInfo)
483 if((pInfo->state == R3964_IDLE) && (pInfo->tx_first!=NULL))
485 pInfo->state = R3964_TX_REQUEST;
486 pInfo->count_down = R3964_TO_QVZ;
488 pInfo->flags &= ~R3964_ERROR;
490 restore_flags(flags);
492 TRACE_PS("trigger_transmit - sent STX");
494 put_char(pInfo, STX);
501 restore_flags(flags);
505 static void retry_transmit(struct r3964_info *pInfo)
507 if(pInfo->nRetry<R3964_MAX_RETRIES)
509 TRACE_PE("transmission failed. Retry #%d",
512 put_char(pInfo, STX);
514 pInfo->state = R3964_TX_REQUEST;
515 pInfo->count_down = R3964_TO_QVZ;
520 TRACE_PE("transmission failed after %d retries",
523 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
525 put_char(pInfo, NAK);
527 pInfo->state = R3964_IDLE;
529 trigger_transmit(pInfo);
534 static void transmit_block(struct r3964_info *pInfo)
536 struct tty_struct *tty = pInfo->tty;
537 struct r3964_block_header *pBlock = pInfo->tx_first;
540 if((tty==NULL) || (pBlock==NULL))
545 if(tty->driver.write_room)
546 room=tty->driver.write_room(tty);
548 TRACE_PS("transmit_block %x, room %d, length %d",
549 (int)pBlock, room, pBlock->length);
551 while(pInfo->tx_position < pBlock->length)
556 if(pBlock->data[pInfo->tx_position]==DLE)
558 /* send additional DLE char: */
559 put_char(pInfo, DLE);
561 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
566 if((pInfo->tx_position == pBlock->length) && (room>=3))
568 put_char(pInfo, DLE);
569 put_char(pInfo, ETX);
570 if(pInfo->flags & R3964_BCC)
572 put_char(pInfo, pInfo->bcc);
574 pInfo->state = R3964_WAIT_FOR_TX_ACK;
575 pInfo->count_down = R3964_TO_QVZ;
580 static void on_receive_block(struct r3964_info *pInfo)
583 struct r3964_client_info *pClient;
584 struct r3964_block_header *pBlock;
586 length=pInfo->rx_position;
588 /* compare byte checksum characters: */
589 if(pInfo->flags & R3964_BCC)
591 if(pInfo->bcc!=pInfo->last_rx)
593 TRACE_PE("checksum error - got %x but expected %x",
594 pInfo->last_rx, pInfo->bcc);
595 pInfo->flags |= R3964_CHECKSUM;
599 /* check for errors (parity, overrun,...): */
600 if(pInfo->flags & R3964_ERROR)
602 TRACE_PE("on_receive_block - transmission failed error %x",
603 pInfo->flags & R3964_ERROR);
605 put_char(pInfo, NAK);
607 if(pInfo->nRetry<R3964_MAX_RETRIES)
609 pInfo->state=R3964_WAIT_FOR_RX_REPEAT;
610 pInfo->count_down = R3964_TO_RX_PANIC;
615 TRACE_PE("on_receive_block - failed after max retries");
616 pInfo->state=R3964_IDLE;
622 /* received block; submit DLE: */
623 put_char(pInfo, DLE);
626 TRACE_PS(" rx success: got %d chars", length);
628 /* prepare struct r3964_block_header: */
629 pBlock = kmalloc(length+sizeof(struct r3964_block_header), GFP_KERNEL);
630 TRACE_M("on_receive_block - kmalloc %x",(int)pBlock);
635 pBlock->length = length;
636 pBlock->data = ((unsigned char*)pBlock)+sizeof(struct r3964_block_header);
639 pBlock->owner = NULL;
641 memcpy(pBlock->data, pInfo->rx_buf, length);
643 /* queue block into rx_queue: */
644 add_rx_queue(pInfo, pBlock);
646 /* notify attached client processes: */
647 for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
649 if(pClient->sig_flags & R3964_SIG_DATA)
651 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock);
654 wake_up_interruptible (&pInfo->read_wait);
656 pInfo->state = R3964_IDLE;
658 trigger_transmit(pInfo);
662 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
666 case R3964_TX_REQUEST:
669 TRACE_PS("TX_REQUEST - got DLE");
671 pInfo->state = R3964_TRANSMITTING;
672 pInfo->tx_position = 0;
674 transmit_block(pInfo);
680 TRACE_PE("TX_REQUEST - init conflict");
681 if(pInfo->priority == R3964_SLAVE)
683 goto start_receiving;
688 TRACE_PE("TX_REQUEST - secondary init conflict!?"
689 " Switching to SLAVE mode for next rx.");
690 goto start_receiving;
695 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
696 retry_transmit(pInfo);
699 case R3964_TRANSMITTING:
702 TRACE_PE("TRANSMITTING - got NAK");
703 retry_transmit(pInfo);
707 TRACE_PE("TRANSMITTING - got illegal char");
709 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
710 pInfo->count_down = R3964_TO_ZVZ;
713 case R3964_WAIT_FOR_TX_ACK:
716 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
717 remove_from_tx_queue(pInfo, R3964_OK);
719 pInfo->state = R3964_IDLE;
720 trigger_transmit(pInfo);
724 retry_transmit(pInfo);
727 case R3964_WAIT_FOR_RX_REPEAT:
732 /* Prevent rx_queue from overflow: */
733 if(pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE)
735 TRACE_PE("IDLE - got STX but no space in rx_queue!");
736 pInfo->state=R3964_WAIT_FOR_RX_BUF;
737 pInfo->count_down = R3964_TO_NO_BUF;
741 /* Ok, start receiving: */
742 TRACE_PS("IDLE - got STX");
743 pInfo->rx_position = 0;
745 pInfo->flags &= ~R3964_ERROR;
746 pInfo->state=R3964_RECEIVING;
747 pInfo->count_down = R3964_TO_ZVZ;
749 put_char(pInfo, DLE);
754 case R3964_RECEIVING:
755 if(pInfo->rx_position < RX_BUF_SIZE)
761 if(pInfo->last_rx==DLE)
766 pInfo->last_rx = DLE;
769 else if((c==ETX) && (pInfo->last_rx==DLE))
771 if(pInfo->flags & R3964_BCC)
773 pInfo->state = R3964_WAIT_FOR_BCC;
774 pInfo->count_down = R3964_TO_ZVZ;
778 on_receive_block(pInfo);
785 pInfo->rx_buf[pInfo->rx_position++] = c;
786 pInfo->count_down = R3964_TO_ZVZ;
789 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
791 case R3964_WAIT_FOR_BCC:
793 on_receive_block(pInfo);
798 static void receive_error(struct r3964_info *pInfo, const char flag)
805 TRACE_PE("received break")
806 pInfo->flags |= R3964_BREAK;
809 TRACE_PE("parity error")
810 pInfo->flags |= R3964_PARITY;
813 TRACE_PE("frame error")
814 pInfo->flags |= R3964_FRAME;
817 TRACE_PE("frame overrun")
818 pInfo->flags |= R3964_OVERRUN;
821 TRACE_PE("receive_error - unknown flag %d", flag);
822 pInfo->flags |= R3964_UNKNOWN;
827 static void on_timeout(struct r3964_info *pInfo)
831 case R3964_TX_REQUEST:
832 TRACE_PE("TX_REQUEST - timeout");
833 retry_transmit(pInfo);
835 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
836 put_char(pInfo, NAK);
838 retry_transmit(pInfo);
840 case R3964_WAIT_FOR_TX_ACK:
841 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
842 retry_transmit(pInfo);
844 case R3964_WAIT_FOR_RX_BUF:
845 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
846 put_char(pInfo, NAK);
848 pInfo->state=R3964_IDLE;
850 case R3964_RECEIVING:
851 TRACE_PE("RECEIVING - timeout after %d chars",
853 put_char(pInfo, NAK);
855 pInfo->state=R3964_IDLE;
857 case R3964_WAIT_FOR_RX_REPEAT:
858 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
859 pInfo->state=R3964_IDLE;
861 case R3964_WAIT_FOR_BCC:
862 TRACE_PE("WAIT_FOR_BCC - timeout");
863 put_char(pInfo, NAK);
865 pInfo->state=R3964_IDLE;
870 static struct r3964_client_info *findClient(
871 struct r3964_info *pInfo, pid_t pid)
873 struct r3964_client_info *pClient;
875 for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
877 if(pClient->pid == pid)
885 static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg)
887 struct r3964_client_info *pClient;
888 struct r3964_client_info **ppClient;
889 struct r3964_message *pMsg;
891 if((arg & R3964_SIG_ALL)==0)
893 /* Remove client from client list */
894 for(ppClient=&pInfo->firstClient; *ppClient; ppClient=&(*ppClient)->next)
898 if(pClient->pid == pid)
900 TRACE_PS("removing client %d from client list", pid);
901 *ppClient = pClient->next;
902 while(pClient->msg_count)
904 pMsg=remove_msg(pInfo, pClient);
908 TRACE_M("enable_signals - msg kfree %x",(int)pMsg);
912 TRACE_M("enable_signals - kfree %x",(int)pClient);
920 pClient=findClient(pInfo, pid);
923 /* update signal options */
924 pClient->sig_flags=arg;
928 /* add client to client list */
929 pClient=kmalloc(sizeof(struct r3964_client_info), GFP_KERNEL);
930 TRACE_M("enable_signals - kmalloc %x",(int)pClient);
934 TRACE_PS("add client %d to client list", pid);
935 pClient->sig_flags=arg;
937 pClient->next=pInfo->firstClient;
938 pClient->first_msg = NULL;
939 pClient->last_msg = NULL;
940 pClient->next_block_to_read = NULL;
941 pClient->msg_count = 0;
942 pInfo->firstClient=pClient;
949 static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char *buf)
951 struct r3964_client_info *pClient;
952 struct r3964_block_header *block;
959 pClient=findClient(pInfo,pid);
965 block=pClient->next_block_to_read;
972 if (copy_to_user (buf, block->data, block->length))
975 remove_client_block(pInfo, pClient);
976 return block->length;
982 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
983 int error_code, struct r3964_block_header *pBlock)
985 struct r3964_message *pMsg;
988 if(pClient->msg_count<R3964_MAX_MSG_COUNT-1)
992 pMsg = kmalloc(sizeof(struct r3964_message), GFP_KERNEL);
993 TRACE_M("add_msg - kmalloc %x",(int)pMsg);
1001 pMsg->msg_id = msg_id;
1003 pMsg->error_code = error_code;
1004 pMsg->block = pBlock;
1007 if(pClient->last_msg==NULL)
1009 pClient->first_msg=pClient->last_msg=pMsg;
1013 pClient->last_msg->next = pMsg;
1014 pClient->last_msg=pMsg;
1017 pClient->msg_count++;
1023 restore_flags(flags);
1027 if((pClient->last_msg->msg_id == R3964_MSG_ACK)
1028 && (pClient->last_msg->error_code==R3964_OVERFLOW))
1030 pClient->last_msg->arg++;
1031 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
1035 msg_id = R3964_MSG_ACK;
1037 error_code = R3964_OVERFLOW;
1039 TRACE_PE("add_msg - queue OVERFLOW-msg");
1040 goto queue_the_message;
1043 /* Send SIGIO signal to client process: */
1044 if(pClient->sig_flags & R3964_USE_SIGIO)
1046 kill_proc(pClient->pid, SIGIO, 1);
1050 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
1051 struct r3964_client_info *pClient)
1053 struct r3964_message *pMsg=NULL;
1054 unsigned long flags;
1056 if(pClient->first_msg)
1061 pMsg = pClient->first_msg;
1062 pClient->first_msg = pMsg->next;
1063 if(pClient->first_msg==NULL)
1065 pClient->last_msg = NULL;
1068 pClient->msg_count--;
1071 remove_client_block(pInfo, pClient);
1072 pClient->next_block_to_read = pMsg->block;
1074 restore_flags(flags);
1079 static void remove_client_block(struct r3964_info *pInfo,
1080 struct r3964_client_info *pClient)
1082 struct r3964_block_header *block;
1084 TRACE_PS("remove_client_block PID %d", pClient->pid);
1086 block=pClient->next_block_to_read;
1092 remove_from_rx_queue(pInfo, block);
1095 pClient->next_block_to_read = NULL;
1099 /*************************************************************
1100 * Line discipline routines
1101 *************************************************************/
1103 static int r3964_open(struct tty_struct *tty)
1105 struct r3964_info *pInfo;
1110 TRACE_L("tty=%x, PID=%d, disc_data=%x",
1111 (int)tty, current->pid, (int)tty->disc_data);
1113 pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
1114 TRACE_M("r3964_open - info kmalloc %x",(int)pInfo);
1118 printk(KERN_ERR "r3964: failed to alloc info structure\n");
1122 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
1123 TRACE_M("r3964_open - rx_buf kmalloc %x",(int)pInfo->rx_buf);
1127 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
1129 TRACE_M("r3964_open - info kfree %x",(int)pInfo);
1133 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
1134 TRACE_M("r3964_open - tx_buf kmalloc %x",(int)pInfo->tx_buf);
1138 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
1139 kfree(pInfo->rx_buf);
1140 TRACE_M("r3964_open - rx_buf kfree %x",(int)pInfo->rx_buf);
1142 TRACE_M("r3964_open - info kfree %x",(int)pInfo);
1147 init_waitqueue_head (&pInfo->read_wait);
1148 pInfo->priority = R3964_MASTER;
1149 pInfo->rx_first = pInfo->rx_last = NULL;
1150 pInfo->tx_first = pInfo->tx_last = NULL;
1151 pInfo->rx_position = 0;
1152 pInfo->tx_position = 0;
1154 pInfo->blocks_in_rx_queue = 0;
1155 pInfo->firstClient=NULL;
1156 pInfo->state=R3964_IDLE;
1157 pInfo->flags = R3964_DEBUG;
1158 pInfo->count_down = 0;
1161 tty->disc_data = pInfo;
1164 * Add 'on_timer' to timer task queue
1165 * (will be called from timer bh)
1167 INIT_LIST_HEAD(&pInfo->bh_1.list);
1168 pInfo->bh_1.sync = 0;
1169 pInfo->bh_1.routine = &on_timer_1;
1170 pInfo->bh_1.data = pInfo;
1172 INIT_LIST_HEAD(&pInfo->bh_2.list);
1173 pInfo->bh_2.sync = 0;
1174 pInfo->bh_2.routine = &on_timer_2;
1175 pInfo->bh_2.data = pInfo;
1177 queue_task(&pInfo->bh_1, &tq_timer);
1182 static void r3964_close(struct tty_struct *tty)
1184 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1185 struct r3964_client_info *pClient, *pNext;
1186 struct r3964_message *pMsg;
1187 struct r3964_block_header *pHeader, *pNextHeader;
1188 unsigned long flags;
1193 * Make sure that our task queue isn't activated. If it
1194 * is, take it out of the linked list.
1196 spin_lock_irqsave(&tqueue_lock, flags);
1197 if (pInfo->bh_1.sync)
1198 list_del(&pInfo->bh_1.list);
1199 if (pInfo->bh_2.sync)
1200 list_del(&pInfo->bh_2.list);
1201 spin_unlock_irqrestore(&tqueue_lock, flags);
1203 /* Remove client-structs and message queues: */
1204 pClient=pInfo->firstClient;
1207 pNext=pClient->next;
1208 while(pClient->msg_count)
1210 pMsg=remove_msg(pInfo, pClient);
1214 TRACE_M("r3964_close - msg kfree %x",(int)pMsg);
1218 TRACE_M("r3964_close - client kfree %x",(int)pClient);
1221 /* Remove jobs from tx_queue: */
1224 pHeader=pInfo->tx_first;
1225 pInfo->tx_first=pInfo->tx_last=NULL;
1226 restore_flags(flags);
1230 pNextHeader=pHeader->next;
1232 pHeader=pNextHeader;
1236 wake_up_interruptible(&pInfo->read_wait);
1237 kfree(pInfo->rx_buf);
1238 TRACE_M("r3964_close - rx_buf kfree %x",(int)pInfo->rx_buf);
1239 kfree(pInfo->tx_buf);
1240 TRACE_M("r3964_close - tx_buf kfree %x",(int)pInfo->tx_buf);
1242 TRACE_M("r3964_close - info kfree %x",(int)pInfo);
1247 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1248 unsigned char *buf, size_t nr)
1250 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1251 struct r3964_client_info *pClient;
1252 struct r3964_message *pMsg;
1253 struct r3964_client_message theMsg;
1254 DECLARE_WAITQUEUE (wait, current);
1256 int pid = current->pid;
1261 pClient=findClient(pInfo, pid);
1264 pMsg = remove_msg(pInfo, pClient);
1267 /* no messages available. */
1268 if (file->f_flags & O_NONBLOCK)
1272 /* block until there is a message: */
1273 add_wait_queue(&pInfo->read_wait, &wait);
1275 current->state = TASK_INTERRUPTIBLE;
1276 pMsg = remove_msg(pInfo, pClient);
1277 if (!pMsg && !signal_pending(current))
1282 current->state = TASK_RUNNING;
1283 remove_wait_queue(&pInfo->read_wait, &wait);
1286 /* If we still haven't got a message, we must have been signalled */
1288 if (!pMsg) return -EINTR;
1290 /* deliver msg to client process: */
1291 theMsg.msg_id = pMsg->msg_id;
1292 theMsg.arg = pMsg->arg;
1293 theMsg.error_code = pMsg->error_code;
1294 count = sizeof(struct r3964_client_message);
1297 TRACE_M("r3964_read - msg kfree %x",(int)pMsg);
1299 if (copy_to_user(buf,&theMsg, count))
1302 TRACE_PS("read - return %d", count);
1308 static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
1309 const unsigned char *data, size_t count)
1311 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1312 struct r3964_block_header *pHeader;
1313 struct r3964_client_info *pClient;
1314 unsigned char *new_data;
1318 TRACE_L("write request, %d characters", count);
1320 * Verify the pointers
1326 status = verify_area (VERIFY_READ, data, count);
1333 * Ensure that the caller does not wish to send too much.
1335 if (count > R3964_MTU)
1337 if (pInfo->flags & R3964_DEBUG)
1339 TRACE_L (KERN_WARNING
1340 "r3964_write: truncating user packet "
1341 "from %u to mtu %d", count, R3964_MTU);
1346 * Allocate a buffer for the data and fetch it from the user space.
1348 new_data = kmalloc (count+sizeof(struct r3964_block_header), GFP_KERNEL);
1349 TRACE_M("r3964_write - kmalloc %x",(int)new_data);
1350 if (new_data == NULL) {
1351 if (pInfo->flags & R3964_DEBUG)
1354 "r3964_write: no memory\n");
1359 pHeader = (struct r3964_block_header *)new_data;
1360 pHeader->data = new_data + sizeof(struct r3964_block_header);
1361 pHeader->length = count;
1363 pHeader->owner = NULL;
1367 pClient=findClient(pInfo, pid);
1370 pHeader->owner = pClient;
1373 __copy_from_user(pHeader->data, data, count); /* We already verified this */
1375 if(pInfo->flags & R3964_DEBUG)
1377 dump_block(pHeader->data, count);
1381 * Add buffer to transmit-queue:
1383 add_tx_queue(pInfo, pHeader);
1384 trigger_transmit(pInfo);
1389 static int r3964_ioctl(struct tty_struct * tty, struct file * file,
1390 unsigned int cmd, unsigned long arg)
1392 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1397 case R3964_ENABLE_SIGNALS:
1398 return enable_signals(pInfo, current->pid, arg);
1399 case R3964_SETPRIORITY:
1400 if(arg<R3964_MASTER || arg>R3964_SLAVE)
1402 pInfo->priority = arg & 0xff;
1406 pInfo->flags |= R3964_BCC;
1408 pInfo->flags &= ~R3964_BCC;
1410 case R3964_READ_TELEGRAM:
1411 return read_telegram(pInfo, current->pid, (unsigned char *)arg);
1413 return -ENOIOCTLCMD;
1417 static void r3964_set_termios(struct tty_struct *tty, struct termios * old)
1419 TRACE_L("set_termios");
1422 /* Called without the kernel lock held - fine */
1423 static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
1424 struct poll_table_struct *wait)
1426 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1427 int pid=current->pid;
1428 struct r3964_client_info *pClient;
1429 struct r3964_message *pMsg=NULL;
1430 unsigned long flags;
1431 int result = POLLOUT;
1435 pClient=findClient(pInfo,pid);
1438 poll_wait(file, &pInfo->read_wait, wait);
1441 pMsg=pClient->first_msg;
1442 restore_flags(flags);
1444 result |= POLLIN | POLLRDNORM;
1453 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1454 char *fp, int count)
1456 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1457 const unsigned char *p;
1461 for (i=count, p = cp, f = fp; i; i--, p++) {
1464 if(flags==TTY_NORMAL)
1466 receive_char(pInfo, *p);
1470 receive_error(pInfo, flags);
1476 static int r3964_receive_room(struct tty_struct *tty)
1478 TRACE_L("receive_room");
1483 MODULE_LICENSE("GPL");